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