• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/core/SkBitmap.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkDrawable.h"
11 #include "include/core/SkPath.h"
12 #include "src/core/SkAdvancedTypefaceMetrics.h"
13 #include "src/core/SkGlyph.h"
14 #include "src/core/SkRectPriv.h"
15 #include "tools/fonts/RandomScalerContext.h"
16 
17 class SkDescriptor;
18 
19 class RandomScalerContext : public SkScalerContext {
20 public:
21     RandomScalerContext(sk_sp<SkRandomTypeface>,
22                         const SkScalerContextEffects&,
23                         const SkDescriptor*,
24                         bool fFakeIt);
25 
26 protected:
27     bool     generateAdvance(SkGlyph*) override;
28     void     generateMetrics(SkGlyph*, SkArenaAlloc*) override;
29     void     generateImage(const SkGlyph&) override;
30     bool     generatePath(const SkGlyph&, SkPath*) override;
31     sk_sp<SkDrawable> generateDrawable(const SkGlyph&) override;
32     void     generateFontMetrics(SkFontMetrics*) override;
33 
34 private:
getRandomTypeface() const35     SkRandomTypeface* getRandomTypeface() const {
36         return static_cast<SkRandomTypeface*>(this->getTypeface());
37     }
38     std::unique_ptr<SkScalerContext> fProxy;
39     // Many of the SkGlyphs returned are the same as those created by the fProxy.
40     // When they are not, the originals are kept here.
41     SkTHashMap<SkPackedGlyphID, SkGlyph> fProxyGlyphs;
42     bool                             fFakeIt;
43 };
44 
RandomScalerContext(sk_sp<SkRandomTypeface> face,const SkScalerContextEffects & effects,const SkDescriptor * desc,bool fakeIt)45 RandomScalerContext::RandomScalerContext(sk_sp<SkRandomTypeface>       face,
46                                          const SkScalerContextEffects& effects,
47                                          const SkDescriptor*           desc,
48                                          bool                          fakeIt)
49         : SkScalerContext(std::move(face), effects, desc)
50         , fProxy(getRandomTypeface()->proxy()->createScalerContext(SkScalerContextEffects(), desc))
51         , fFakeIt(fakeIt) {
52     fProxy->forceGenerateImageFromPath();
53 }
54 
generateAdvance(SkGlyph * glyph)55 bool RandomScalerContext::generateAdvance(SkGlyph* glyph) { return fProxy->generateAdvance(glyph); }
56 
generateMetrics(SkGlyph * glyph,SkArenaAlloc * alloc)57 void RandomScalerContext::generateMetrics(SkGlyph* glyph, SkArenaAlloc* alloc) {
58     // Here we will change the mask format of the glyph
59     // NOTE: this may be overridden by the base class (e.g. if a mask filter is applied).
60     SkMask::Format format = SkMask::kA8_Format;
61     switch (glyph->getGlyphID() % 4) {
62         case 0: format = SkMask::kLCD16_Format; break;
63         case 1: format = SkMask::kA8_Format; break;
64         case 2: format = SkMask::kARGB32_Format; break;
65         case 3: format = SkMask::kBW_Format; break;
66     }
67 
68     *glyph = fProxy->internalMakeGlyph(glyph->getPackedID(), format, alloc);
69 
70     if (fFakeIt || (glyph->getGlyphID() % 4) != 2) {
71         return;
72     }
73 
74     fProxy->getPath(*glyph, alloc);
75     if (!glyph->path()) {
76         return;
77     }
78 
79     // The proxy glyph has a path, but this glyph does not.
80     // Stash the proxy glyph so it can be used later.
81     const SkGlyph* proxyGlyph = fProxyGlyphs.set(glyph->getPackedID(), std::move(*glyph));
82     const SkPath& proxyPath = *proxyGlyph->path();
83 
84     *glyph = SkGlyph(glyph->getPackedID());
85     glyph->setPath(alloc, nullptr, false);
86     glyph->fMaskFormat = SkMask::kARGB32_Format;
87     glyph->fAdvanceX = proxyGlyph->fAdvanceX;
88     glyph->fAdvanceY = proxyGlyph->fAdvanceY;
89 
90     SkRect         storage;
91     const SkPaint& paint = this->getRandomTypeface()->paint();
92     const SkRect&  newBounds =
93             paint.doComputeFastBounds(proxyPath.getBounds(), &storage, 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 }
101 
generateImage(const SkGlyph & glyph)102 void RandomScalerContext::generateImage(const SkGlyph& glyph) {
103     if (fFakeIt) {
104         sk_bzero(glyph.fImage, glyph.imageSize());
105         return;
106     }
107 
108     SkGlyph* proxyGlyph = fProxyGlyphs.find(glyph.getPackedID());
109     if (!proxyGlyph || !proxyGlyph->path()) {
110         fProxy->getImage(glyph);
111         return;
112     }
113     const SkPath& path = *proxyGlyph->path();
114     const bool hairline = proxyGlyph->pathIsHairline();
115 
116     SkBitmap bm;
117     bm.installPixels(SkImageInfo::MakeN32Premul(glyph.fWidth, glyph.fHeight),
118                      glyph.fImage,
119                      glyph.rowBytes());
120     bm.eraseColor(0);
121 
122     SkCanvas canvas(bm);
123     canvas.translate(-SkIntToScalar(glyph.fLeft), -SkIntToScalar(glyph.fTop));
124     SkPaint paint = this->getRandomTypeface()->paint();
125     if (hairline) {
126         // We have a device path with effects already applied which is normally a fill path.
127         // However here we do not have a fill path and there is no area to fill.
128         paint.setStyle(SkPaint::kStroke_Style);
129         paint.setStroke(0);
130     }
131     canvas.drawPath(path, paint); //Need to modify the paint if the devPath is hairline
132 }
133 
generatePath(const SkGlyph & glyph,SkPath * path)134 bool RandomScalerContext::generatePath(const SkGlyph& glyph, SkPath* path) {
135     SkGlyph* shadowProxyGlyph = fProxyGlyphs.find(glyph.getPackedID());
136     if (shadowProxyGlyph && shadowProxyGlyph->path()) {
137         path->reset();
138         return false;
139     }
140     return fProxy->generatePath(glyph, path);
141 }
142 
generateDrawable(const SkGlyph & glyph)143 sk_sp<SkDrawable> RandomScalerContext::generateDrawable(const SkGlyph& glyph) {
144     SkGlyph* shadowProxyGlyph = fProxyGlyphs.find(glyph.getPackedID());
145     if (shadowProxyGlyph && shadowProxyGlyph->path()) {
146         return nullptr;
147     }
148     return fProxy->generateDrawable(glyph);
149 }
150 
generateFontMetrics(SkFontMetrics * metrics)151 void RandomScalerContext::generateFontMetrics(SkFontMetrics* metrics) {
152     fProxy->getFontMetrics(metrics);
153 }
154 
155 ///////////////////////////////////////////////////////////////////////////////
156 
SkRandomTypeface(sk_sp<SkTypeface> proxy,const SkPaint & paint,bool fakeIt)157 SkRandomTypeface::SkRandomTypeface(sk_sp<SkTypeface> proxy, const SkPaint& paint, bool fakeIt)
158         : SkTypeface(proxy->fontStyle(), false)
159         , fProxy(std::move(proxy))
160         , fPaint(paint)
161         , fFakeIt(fakeIt) {}
162 
onCreateScalerContext(const SkScalerContextEffects & effects,const SkDescriptor * desc) const163 std::unique_ptr<SkScalerContext> SkRandomTypeface::onCreateScalerContext(
164     const SkScalerContextEffects& effects, const SkDescriptor* desc) const
165 {
166     return std::make_unique<RandomScalerContext>(
167             sk_ref_sp(const_cast<SkRandomTypeface*>(this)), effects, desc, fFakeIt);
168 }
169 
onFilterRec(SkScalerContextRec * rec) const170 void SkRandomTypeface::onFilterRec(SkScalerContextRec* rec) const {
171     fProxy->filterRec(rec);
172     rec->setHinting(SkFontHinting::kNone);
173     rec->fMaskFormat = SkMask::kARGB32_Format;
174 }
175 
getGlyphToUnicodeMap(SkUnichar * glyphToUnicode) const176 void SkRandomTypeface::getGlyphToUnicodeMap(SkUnichar* glyphToUnicode) const {
177     fProxy->getGlyphToUnicodeMap(glyphToUnicode);
178 }
179 
onGetAdvancedMetrics() const180 std::unique_ptr<SkAdvancedTypefaceMetrics> SkRandomTypeface::onGetAdvancedMetrics() const {
181     return fProxy->getAdvancedMetrics();
182 }
183 
onOpenStream(int * ttcIndex) const184 std::unique_ptr<SkStreamAsset> SkRandomTypeface::onOpenStream(int* ttcIndex) const {
185     return fProxy->openStream(ttcIndex);
186 }
187 
onMakeClone(const SkFontArguments & args) const188 sk_sp<SkTypeface> SkRandomTypeface::onMakeClone(const SkFontArguments& args) const {
189     sk_sp<SkTypeface> proxy = fProxy->makeClone(args);
190     if (!proxy) {
191         return nullptr;
192     }
193     return sk_make_sp<SkRandomTypeface>(proxy, fPaint, fFakeIt);
194 }
195 
onGetFontDescriptor(SkFontDescriptor * desc,bool * isLocal) const196 void SkRandomTypeface::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const {
197     // TODO: anything that uses this typeface isn't correctly serializable, since this typeface
198     // cannot be deserialized.
199     fProxy->getFontDescriptor(desc, isLocal);
200 }
201 
onCharsToGlyphs(const SkUnichar * uni,int count,SkGlyphID glyphs[]) const202 void SkRandomTypeface::onCharsToGlyphs(const SkUnichar* uni, int count, SkGlyphID glyphs[]) const {
203     fProxy->unicharsToGlyphs(uni, count, glyphs);
204 }
205 
onCountGlyphs() const206 int SkRandomTypeface::onCountGlyphs() const { return fProxy->countGlyphs(); }
207 
onGetUPEM() const208 int SkRandomTypeface::onGetUPEM() const { return fProxy->getUnitsPerEm(); }
209 
onGetFamilyName(SkString * familyName) const210 void SkRandomTypeface::onGetFamilyName(SkString* familyName) const {
211     fProxy->getFamilyName(familyName);
212 }
213 
onGetPostScriptName(SkString * postScriptName) const214 bool SkRandomTypeface::onGetPostScriptName(SkString* postScriptName) const {
215     return fProxy->getPostScriptName(postScriptName);
216 }
217 
onCreateFamilyNameIterator() const218 SkTypeface::LocalizedStrings* SkRandomTypeface::onCreateFamilyNameIterator() const {
219     return fProxy->createFamilyNameIterator();
220 }
221 
getPostScriptGlyphNames(SkString * names) const222 void SkRandomTypeface::getPostScriptGlyphNames(SkString* names) const {
223     return fProxy->getPostScriptGlyphNames(names);
224 }
225 
onGlyphMaskNeedsCurrentColor() const226 bool SkRandomTypeface::onGlyphMaskNeedsCurrentColor() const {
227     return fProxy->glyphMaskNeedsCurrentColor();
228 }
229 
onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],int coordinateCount) const230 int SkRandomTypeface::onGetVariationDesignPosition(
231         SkFontArguments::VariationPosition::Coordinate coordinates[],
232         int                                            coordinateCount) const {
233     return fProxy->onGetVariationDesignPosition(coordinates, coordinateCount);
234 }
235 
onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],int parameterCount) const236 int SkRandomTypeface::onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],
237                                                      int parameterCount) const {
238     return fProxy->onGetVariationDesignParameters(parameters, parameterCount);
239 }
240 
onGetTableTags(SkFontTableTag tags[]) const241 int SkRandomTypeface::onGetTableTags(SkFontTableTag tags[]) const {
242     return fProxy->getTableTags(tags);
243 }
244 
onGetTableData(SkFontTableTag tag,size_t offset,size_t length,void * data) const245 size_t SkRandomTypeface::onGetTableData(SkFontTableTag tag,
246                                         size_t         offset,
247                                         size_t         length,
248                                         void*          data) const {
249     return fProxy->getTableData(tag, offset, length, data);
250 }
251