OLD | NEW |
(Empty) | |
| 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" |
| 12 #include "SkRasterizer.h" |
| 13 |
| 14 class SkRandomScalerContext : public SkScalerContext { |
| 15 public: |
| 16 SkRandomScalerContext(SkRandomTypeface*, const SkDescriptor*, bool fFakeIt); |
| 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; |
| 31 bool fFakeIt; |
| 32 }; |
| 33 |
| 34 #define STD_SIZE 1 |
| 35 |
| 36 #include "SkDescriptor.h" |
| 37 |
| 38 SkRandomScalerContext::SkRandomScalerContext(SkRandomTypeface* face, const SkDes
criptor* desc, |
| 39 bool fakeIt) |
| 40 : SkScalerContext(face, desc) |
| 41 , fFace(face) |
| 42 , fFakeIt(fakeIt) { |
| 43 fProxy = face->proxy()->createScalerContext(desc); |
| 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); |
| 60 } |
| 61 |
| 62 void SkRandomScalerContext::generateMetrics(SkGlyph* glyph) { |
| 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; |
| 66 switch (glyph->getGlyphID() % 4) { |
| 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; |
| 76 case 3: |
| 77 format = SkMask::kBW_Format; |
| 78 break; |
| 79 } |
| 80 |
| 81 fProxy->getMetrics(glyph); |
| 82 |
| 83 glyph->fMaskFormat = format; |
| 84 if (fFakeIt) { |
| 85 return; |
| 86 } |
| 87 if (SkMask::kARGB32_Format == format) { |
| 88 SkPath path; |
| 89 fProxy->getPath(*glyph, &path); |
| 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 } |
| 134 } |
| 135 |
| 136 void SkRandomScalerContext::generateImage(const SkGlyph& glyph) { |
| 137 SkMask::Format format = (SkMask::Format)glyph.fMaskFormat; |
| 138 switch (glyph.getGlyphID() % 4) { |
| 139 case 0: |
| 140 format = SkMask::kLCD16_Format; |
| 141 break; |
| 142 case 1: |
| 143 format = SkMask::kA8_Format; |
| 144 break; |
| 145 case 2: |
| 146 format = SkMask::kARGB32_Format; |
| 147 break; |
| 148 case 3: |
| 149 format = SkMask::kBW_Format; |
| 150 break; |
| 151 } |
| 152 const_cast<SkGlyph&>(glyph).fMaskFormat = format; |
| 153 |
| 154 // if the format is ARGB, we just draw the glyph from path ourselves. Other
wise, we force |
| 155 // our proxy context to generate the image from paths. |
| 156 if (!fFakeIt) { |
| 157 if (SkMask::kARGB32_Format == glyph.fMaskFormat) { |
| 158 SkPath path; |
| 159 fProxy->getPath(glyph, &path); |
| 160 |
| 161 SkBitmap bm; |
| 162 bm.installPixels(SkImageInfo::MakeN32Premul(glyph.fWidth, glyph.fHei
ght), |
| 163 glyph.fImage, glyph.rowBytes()); |
| 164 bm.eraseColor(0); |
| 165 |
| 166 SkCanvas canvas(bm); |
| 167 canvas.translate(-SkIntToScalar(glyph.fLeft), |
| 168 -SkIntToScalar(glyph.fTop)); |
| 169 canvas.drawPath(path, fFace->paint()); |
| 170 } else { |
| 171 fProxy->forceGenerateImageFromPath(); |
| 172 fProxy->getImage(glyph); |
| 173 fProxy->forceOffGenerateImageFromPath(); |
| 174 } |
| 175 } else { |
| 176 sk_bzero(glyph.fImage, glyph.computeImageSize()); |
| 177 } |
| 178 } |
| 179 |
| 180 void SkRandomScalerContext::generatePath(const SkGlyph& glyph, SkPath* path) { |
| 181 fProxy->getPath(glyph, path); |
| 182 } |
| 183 |
| 184 void SkRandomScalerContext::generateFontMetrics(SkPaint::FontMetrics* metrics) { |
| 185 fProxy->getFontMetrics(metrics); |
| 186 } |
| 187 |
| 188 /////////////////////////////////////////////////////////////////////////////// |
| 189 |
| 190 #include "SkTypefaceCache.h" |
| 191 |
| 192 SkRandomTypeface::SkRandomTypeface(SkTypeface* proxy, const SkPaint& paint, bool
fakeIt) |
| 193 : SkTypeface(proxy->fontStyle(), SkTypefaceCache::NewFontID(), false) |
| 194 , fProxy(SkRef(proxy)) |
| 195 , fPaint(paint) |
| 196 , fFakeIt(fakeIt) {} |
| 197 |
| 198 SkRandomTypeface::~SkRandomTypeface() { |
| 199 fProxy->unref(); |
| 200 } |
| 201 |
| 202 SkScalerContext* SkRandomTypeface::onCreateScalerContext( |
| 203 const SkDescriptor* desc) const { |
| 204 return SkNEW_ARGS(SkRandomScalerContext, (const_cast<SkRandomTypeface*>(this
), desc, fFakeIt)); |
| 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() con
st { |
| 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 |
OLD | NEW |