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