joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 8 | #include "SkCanvas.h" |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 9 | #include "SkGlyph.h" |
| 10 | #include "SkMakeUnique.h" |
| 11 | #include "SkPath.h" |
| 12 | #include "SkRandomScalerContext.h" |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 13 | #include "SkRasterizer.h" |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 14 | |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 15 | class SkDescriptor; |
| 16 | |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 17 | class SkRandomScalerContext : public SkScalerContext { |
| 18 | public: |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 19 | SkRandomScalerContext(sk_sp<SkRandomTypeface>, const SkScalerContextEffects&, |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 20 | const SkDescriptor*, bool fFakeIt); |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 21 | |
| 22 | protected: |
| 23 | unsigned generateGlyphCount() override; |
| 24 | uint16_t generateCharToGlyph(SkUnichar) override; |
| 25 | void generateAdvance(SkGlyph*) override; |
| 26 | void generateMetrics(SkGlyph*) override; |
| 27 | void generateImage(const SkGlyph&) override; |
Ben Wagner | 6e9ac12 | 2016-11-11 14:31:06 -0500 | [diff] [blame] | 28 | void generatePath(SkGlyphID, SkPath*) override; |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 29 | void generateFontMetrics(SkPaint::FontMetrics*) override; |
| 30 | |
| 31 | private: |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 32 | SkRandomTypeface* getRandomTypeface() const { |
| 33 | return static_cast<SkRandomTypeface*>(this->getTypeface()); |
| 34 | } |
| 35 | std::unique_ptr<SkScalerContext> fProxy; |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 36 | bool fFakeIt; |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 39 | SkRandomScalerContext::SkRandomScalerContext(sk_sp<SkRandomTypeface> face, |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 40 | const SkScalerContextEffects& effects, |
| 41 | const SkDescriptor* desc, |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 42 | bool fakeIt) |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 43 | : SkScalerContext(std::move(face), effects, desc) |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 44 | , fFakeIt(fakeIt) { |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 45 | fProxy = this->getRandomTypeface()->proxy()->createScalerContext(effects, desc); |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 46 | } |
| 47 | |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 48 | unsigned SkRandomScalerContext::generateGlyphCount() { |
| 49 | return fProxy->getGlyphCount(); |
| 50 | } |
| 51 | |
| 52 | uint16_t SkRandomScalerContext::generateCharToGlyph(SkUnichar uni) { |
| 53 | return fProxy->charToGlyphID(uni); |
| 54 | } |
| 55 | |
| 56 | void SkRandomScalerContext::generateAdvance(SkGlyph* glyph) { |
| 57 | fProxy->getAdvance(glyph); |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void SkRandomScalerContext::generateMetrics(SkGlyph* glyph) { |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 61 | // Here we will change the mask format of the glyph |
| 62 | // NOTE this is being overridden by the base class |
robertphillips | 52b88cc | 2016-04-05 16:55:41 -0700 | [diff] [blame] | 63 | SkMask::Format format = SkMask::kARGB32_Format; // init to handle defective compilers |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 64 | switch (glyph->getGlyphID() % 4) { |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 65 | case 0: |
| 66 | format = SkMask::kLCD16_Format; |
| 67 | break; |
| 68 | case 1: |
| 69 | format = SkMask::kA8_Format; |
| 70 | break; |
| 71 | case 2: |
| 72 | format = SkMask::kARGB32_Format; |
| 73 | break; |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 74 | case 3: |
| 75 | format = SkMask::kBW_Format; |
| 76 | break; |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 77 | } |
| 78 | |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 79 | fProxy->getMetrics(glyph); |
| 80 | |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 81 | glyph->fMaskFormat = format; |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 82 | if (fFakeIt) { |
| 83 | return; |
| 84 | } |
| 85 | if (SkMask::kARGB32_Format == format) { |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 86 | SkPath path; |
Ben Wagner | 6e9ac12 | 2016-11-11 14:31:06 -0500 | [diff] [blame] | 87 | fProxy->getPath(glyph->getPackedID(), &path); |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 88 | |
| 89 | SkRect storage; |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 90 | const SkPaint& paint = this->getRandomTypeface()->paint(); |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 91 | const SkRect& newBounds = paint.doComputeFastBounds(path.getBounds(), |
| 92 | &storage, |
| 93 | SkPaint::kFill_Style); |
| 94 | SkIRect ibounds; |
| 95 | newBounds.roundOut(&ibounds); |
| 96 | glyph->fLeft = ibounds.fLeft; |
| 97 | glyph->fTop = ibounds.fTop; |
| 98 | glyph->fWidth = ibounds.width(); |
| 99 | glyph->fHeight = ibounds.height(); |
| 100 | } else { |
| 101 | SkPath devPath, fillPath; |
| 102 | SkMatrix fillToDevMatrix; |
| 103 | |
Ben Wagner | 6e9ac12 | 2016-11-11 14:31:06 -0500 | [diff] [blame] | 104 | this->internalGetPath(glyph->getPackedID(), &fillPath, &devPath, &fillToDevMatrix); |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 105 | |
| 106 | // just use devPath |
| 107 | const SkIRect ir = devPath.getBounds().roundOut(); |
| 108 | |
| 109 | if (ir.isEmpty() || !ir.is16Bit()) { |
| 110 | glyph->fLeft = 0; |
| 111 | glyph->fTop = 0; |
| 112 | glyph->fWidth = 0; |
| 113 | glyph->fHeight = 0; |
| 114 | return; |
| 115 | } |
| 116 | glyph->fLeft = ir.fLeft; |
| 117 | glyph->fTop = ir.fTop; |
| 118 | glyph->fWidth = SkToU16(ir.width()); |
| 119 | glyph->fHeight = SkToU16(ir.height()); |
| 120 | |
| 121 | if (glyph->fWidth > 0) { |
| 122 | switch (glyph->fMaskFormat) { |
| 123 | case SkMask::kLCD16_Format: |
| 124 | glyph->fWidth += 2; |
| 125 | glyph->fLeft -= 1; |
| 126 | break; |
| 127 | default: |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | } |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void SkRandomScalerContext::generateImage(const SkGlyph& glyph) { |
| 135 | SkMask::Format format = (SkMask::Format)glyph.fMaskFormat; |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 136 | switch (glyph.getGlyphID() % 4) { |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 137 | case 0: |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 138 | format = SkMask::kLCD16_Format; |
| 139 | break; |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 140 | case 1: |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 141 | format = SkMask::kA8_Format; |
| 142 | break; |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 143 | case 2: |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 144 | format = SkMask::kARGB32_Format; |
| 145 | break; |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 146 | case 3: |
| 147 | format = SkMask::kBW_Format; |
| 148 | break; |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 149 | } |
| 150 | const_cast<SkGlyph&>(glyph).fMaskFormat = format; |
| 151 | |
| 152 | // if the format is ARGB, we just draw the glyph from path ourselves. Otherwise, we force |
| 153 | // our proxy context to generate the image from paths. |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 154 | if (!fFakeIt) { |
| 155 | if (SkMask::kARGB32_Format == glyph.fMaskFormat) { |
| 156 | SkPath path; |
Ben Wagner | 6e9ac12 | 2016-11-11 14:31:06 -0500 | [diff] [blame] | 157 | fProxy->getPath(glyph.getPackedID(), &path); |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 158 | |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 159 | SkBitmap bm; |
| 160 | bm.installPixels(SkImageInfo::MakeN32Premul(glyph.fWidth, glyph.fHeight), |
| 161 | glyph.fImage, glyph.rowBytes()); |
| 162 | bm.eraseColor(0); |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 163 | |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 164 | SkCanvas canvas(bm); |
| 165 | canvas.translate(-SkIntToScalar(glyph.fLeft), |
| 166 | -SkIntToScalar(glyph.fTop)); |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 167 | canvas.drawPath(path, this->getRandomTypeface()->paint()); |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 168 | } else { |
| 169 | fProxy->forceGenerateImageFromPath(); |
| 170 | fProxy->getImage(glyph); |
| 171 | fProxy->forceOffGenerateImageFromPath(); |
| 172 | } |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 173 | } else { |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 174 | sk_bzero(glyph.fImage, glyph.computeImageSize()); |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Ben Wagner | 6e9ac12 | 2016-11-11 14:31:06 -0500 | [diff] [blame] | 178 | void SkRandomScalerContext::generatePath(SkGlyphID glyph, SkPath* path) { |
| 179 | fProxy->generatePath(glyph, path); |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void SkRandomScalerContext::generateFontMetrics(SkPaint::FontMetrics* metrics) { |
| 183 | fProxy->getFontMetrics(metrics); |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /////////////////////////////////////////////////////////////////////////////// |
| 187 | |
| 188 | #include "SkTypefaceCache.h" |
| 189 | |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 190 | SkRandomTypeface::SkRandomTypeface(sk_sp<SkTypeface> proxy, const SkPaint& paint, bool fakeIt) |
bungeman | e3aea10 | 2016-07-13 05:16:58 -0700 | [diff] [blame] | 191 | : SkTypeface(proxy->fontStyle(), false) |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 192 | , fProxy(std::move(proxy)) |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 193 | , fPaint(paint) |
| 194 | , fFakeIt(fakeIt) {} |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 195 | |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 196 | SkScalerContext* SkRandomTypeface::onCreateScalerContext(const SkScalerContextEffects& effects, |
| 197 | const SkDescriptor* desc) const { |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 198 | return new SkRandomScalerContext(sk_ref_sp(const_cast<SkRandomTypeface*>(this)), |
| 199 | effects, desc, fFakeIt); |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void SkRandomTypeface::onFilterRec(SkScalerContextRec* rec) const { |
| 203 | fProxy->filterRec(rec); |
| 204 | rec->setHinting(SkPaint::kNo_Hinting); |
| 205 | rec->fMaskFormat = SkMask::kARGB32_Format; |
| 206 | } |
| 207 | |
| 208 | SkAdvancedTypefaceMetrics* SkRandomTypeface::onGetAdvancedTypefaceMetrics( |
| 209 | PerGlyphInfo info, |
| 210 | const uint32_t* glyphIDs, |
| 211 | uint32_t glyphIDsCount) const { |
| 212 | return fProxy->getAdvancedTypefaceMetrics(info, glyphIDs, glyphIDsCount); |
| 213 | } |
| 214 | |
| 215 | SkStreamAsset* SkRandomTypeface::onOpenStream(int* ttcIndex) const { |
| 216 | return fProxy->openStream(ttcIndex); |
| 217 | } |
| 218 | |
| 219 | void SkRandomTypeface::onGetFontDescriptor(SkFontDescriptor* desc, |
| 220 | bool* isLocal) const { |
| 221 | fProxy->getFontDescriptor(desc, isLocal); |
| 222 | } |
| 223 | |
| 224 | int SkRandomTypeface::onCharsToGlyphs(const void* chars, Encoding encoding, |
| 225 | uint16_t glyphs[], int glyphCount) const { |
| 226 | return fProxy->charsToGlyphs(chars, encoding, glyphs, glyphCount); |
| 227 | } |
| 228 | |
| 229 | int SkRandomTypeface::onCountGlyphs() const { |
| 230 | return fProxy->countGlyphs(); |
| 231 | } |
| 232 | |
| 233 | int SkRandomTypeface::onGetUPEM() const { |
| 234 | return fProxy->getUnitsPerEm(); |
| 235 | } |
| 236 | |
| 237 | void SkRandomTypeface::onGetFamilyName(SkString* familyName) const { |
| 238 | fProxy->getFamilyName(familyName); |
| 239 | } |
| 240 | |
| 241 | SkTypeface::LocalizedStrings* SkRandomTypeface::onCreateFamilyNameIterator() const { |
| 242 | return fProxy->createFamilyNameIterator(); |
| 243 | } |
| 244 | |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 245 | int SkRandomTypeface::onGetVariationDesignPosition( |
| 246 | SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const |
| 247 | { |
| 248 | return fProxy->onGetVariationDesignPosition(coordinates, coordinateCount); |
| 249 | } |
| 250 | |
joshualitt | aa2f658 | 2015-07-29 10:14:58 -0700 | [diff] [blame] | 251 | int SkRandomTypeface::onGetTableTags(SkFontTableTag tags[]) const { |
| 252 | return fProxy->getTableTags(tags); |
| 253 | } |
| 254 | |
| 255 | size_t SkRandomTypeface::onGetTableData(SkFontTableTag tag, size_t offset, |
| 256 | size_t length, void* data) const { |
| 257 | return fProxy->getTableData(tag, offset, length, data); |
| 258 | } |
| 259 | |