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