• 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/SkPath.h"
11 #include "src/core/SkAdvancedTypefaceMetrics.h"
12 #include "src/core/SkGlyph.h"
13 #include "src/core/SkRectPriv.h"
14 #include "tools/fonts/RandomScalerContext.h"
15 
16 class SkDescriptor;
17 
18 class RandomScalerContext : public SkScalerContext {
19 public:
20     RandomScalerContext(sk_sp<SkRandomTypeface>,
21                         const SkScalerContextEffects&,
22                         const SkDescriptor*,
23                         bool fFakeIt);
24 
25 protected:
26     bool     generateAdvance(SkGlyph*) override;
27     void     generateMetrics(SkGlyph*) override;
28     void     generateImage(const SkGlyph&) override;
29     bool     generatePath(SkGlyphID, SkPath*) override;
30     void     generateFontMetrics(SkFontMetrics*) override;
31 
32 private:
getRandomTypeface() const33     SkRandomTypeface* getRandomTypeface() const {
34         return static_cast<SkRandomTypeface*>(this->getTypeface());
35     }
36     std::unique_ptr<SkScalerContext> fProxy;
37     bool                             fFakeIt;
38 };
39 
RandomScalerContext(sk_sp<SkRandomTypeface> face,const SkScalerContextEffects & effects,const SkDescriptor * desc,bool fakeIt)40 RandomScalerContext::RandomScalerContext(sk_sp<SkRandomTypeface>       face,
41                                          const SkScalerContextEffects& effects,
42                                          const SkDescriptor*           desc,
43                                          bool                          fakeIt)
44         : SkScalerContext(std::move(face), effects, desc)
45         , fProxy(getRandomTypeface()->proxy()->createScalerContext(SkScalerContextEffects(), desc))
46         , fFakeIt(fakeIt) {
47     fProxy->forceGenerateImageFromPath();
48 }
49 
generateAdvance(SkGlyph * glyph)50 bool RandomScalerContext::generateAdvance(SkGlyph* glyph) { return fProxy->generateAdvance(glyph); }
51 
generateMetrics(SkGlyph * glyph)52 void RandomScalerContext::generateMetrics(SkGlyph* glyph) {
53     // Here we will change the mask format of the glyph
54     // NOTE: this may be overridden by the base class (e.g. if a mask filter is applied).
55     SkMask::Format format = SkMask::kA8_Format;
56     switch (glyph->getGlyphID() % 4) {
57         case 0: format = SkMask::kLCD16_Format; break;
58         case 1: format = SkMask::kA8_Format; break;
59         case 2: format = SkMask::kARGB32_Format; break;
60         case 3: format = SkMask::kBW_Format; break;
61     }
62 
63     *glyph = fProxy->internalMakeGlyph(glyph->getPackedID(), format);
64 
65     if (fFakeIt || (glyph->getGlyphID() % 4) != 2) {
66         return;
67     }
68 
69     SkPath path;
70     if (!fProxy->getPath(glyph->getPackedID(), &path)) {
71         return;
72     }
73     glyph->fMaskFormat = SkMask::kARGB32_Format;
74 
75     SkRect         storage;
76     const SkPaint& paint = this->getRandomTypeface()->paint();
77     const SkRect&  newBounds =
78             paint.doComputeFastBounds(path.getBounds(), &storage, SkPaint::kFill_Style);
79     SkIRect ibounds;
80     newBounds.roundOut(&ibounds);
81     glyph->fLeft   = ibounds.fLeft;
82     glyph->fTop    = ibounds.fTop;
83     glyph->fWidth  = ibounds.width();
84     glyph->fHeight = ibounds.height();
85 }
86 
generateImage(const SkGlyph & glyph)87 void RandomScalerContext::generateImage(const SkGlyph& glyph) {
88     // TODO: can force down but not up
89     /*
90     SkMask::Format format = (SkMask::Format)glyph.fMaskFormat;
91     switch (glyph.getGlyphID() % 4) {
92         case 0: format = SkMask::kLCD16_Format; break;
93         case 1: format = SkMask::kA8_Format; break;
94         case 2: format = SkMask::kARGB32_Format; break;
95         case 3: format = SkMask::kBW_Format; break;
96     }
97     const_cast<SkGlyph&>(glyph).fMaskFormat = format;
98     */
99 
100     if (fFakeIt) {
101         sk_bzero(glyph.fImage, glyph.imageSize());
102         return;
103     }
104 
105     if (SkMask::kARGB32_Format != glyph.fMaskFormat) {
106         fProxy->getImage(glyph);
107         return;
108     }
109 
110     // If the format is ARGB, just draw the glyph from path.
111     SkPath path;
112     if (!fProxy->getPath(glyph.getPackedID(), &path)) {
113         fProxy->getImage(glyph);
114         return;
115     }
116 
117     SkBitmap bm;
118     bm.installPixels(SkImageInfo::MakeN32Premul(glyph.fWidth, glyph.fHeight),
119                      glyph.fImage,
120                      glyph.rowBytes());
121     bm.eraseColor(0);
122 
123     SkCanvas canvas(bm);
124     canvas.translate(-SkIntToScalar(glyph.fLeft), -SkIntToScalar(glyph.fTop));
125     canvas.drawPath(path, this->getRandomTypeface()->paint());
126 }
127 
generatePath(SkGlyphID glyph,SkPath * path)128 bool RandomScalerContext::generatePath(SkGlyphID glyph, SkPath* path) {
129     return fProxy->generatePath(glyph, path);
130 }
131 
generateFontMetrics(SkFontMetrics * metrics)132 void RandomScalerContext::generateFontMetrics(SkFontMetrics* metrics) {
133     fProxy->getFontMetrics(metrics);
134 }
135 
136 ///////////////////////////////////////////////////////////////////////////////
137 
SkRandomTypeface(sk_sp<SkTypeface> proxy,const SkPaint & paint,bool fakeIt)138 SkRandomTypeface::SkRandomTypeface(sk_sp<SkTypeface> proxy, const SkPaint& paint, bool fakeIt)
139         : SkTypeface(proxy->fontStyle(), false)
140         , fProxy(std::move(proxy))
141         , fPaint(paint)
142         , fFakeIt(fakeIt) {}
143 
onCreateScalerContext(const SkScalerContextEffects & effects,const SkDescriptor * desc) const144 std::unique_ptr<SkScalerContext> SkRandomTypeface::onCreateScalerContext(
145     const SkScalerContextEffects& effects, const SkDescriptor* desc) const
146 {
147     return std::make_unique<RandomScalerContext>(
148             sk_ref_sp(const_cast<SkRandomTypeface*>(this)), effects, desc, fFakeIt);
149 }
150 
onFilterRec(SkScalerContextRec * rec) const151 void SkRandomTypeface::onFilterRec(SkScalerContextRec* rec) const {
152     fProxy->filterRec(rec);
153     rec->setHinting(SkFontHinting::kNone);
154     rec->fMaskFormat = SkMask::kARGB32_Format;
155 }
156 
getGlyphToUnicodeMap(SkUnichar * glyphToUnicode) const157 void SkRandomTypeface::getGlyphToUnicodeMap(SkUnichar* glyphToUnicode) const {
158     fProxy->getGlyphToUnicodeMap(glyphToUnicode);
159 }
160 
onGetAdvancedMetrics() const161 std::unique_ptr<SkAdvancedTypefaceMetrics> SkRandomTypeface::onGetAdvancedMetrics() const {
162     return fProxy->getAdvancedMetrics();
163 }
164 
onOpenStream(int * ttcIndex) const165 std::unique_ptr<SkStreamAsset> SkRandomTypeface::onOpenStream(int* ttcIndex) const {
166     return fProxy->openStream(ttcIndex);
167 }
168 
onMakeClone(const SkFontArguments & args) const169 sk_sp<SkTypeface> SkRandomTypeface::onMakeClone(const SkFontArguments& args) const {
170     sk_sp<SkTypeface> proxy = fProxy->makeClone(args);
171     if (!proxy) {
172         return nullptr;
173     }
174     return sk_make_sp<SkRandomTypeface>(proxy, fPaint, fFakeIt);
175 }
176 
onGetFontDescriptor(SkFontDescriptor * desc,bool * isLocal) const177 void SkRandomTypeface::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const {
178     // TODO: anything that uses this typeface isn't correctly serializable, since this typeface
179     // cannot be deserialized.
180     fProxy->getFontDescriptor(desc, isLocal);
181 }
182 
onCharsToGlyphs(const SkUnichar * uni,int count,SkGlyphID glyphs[]) const183 void SkRandomTypeface::onCharsToGlyphs(const SkUnichar* uni, int count, SkGlyphID glyphs[]) const {
184     fProxy->unicharsToGlyphs(uni, count, glyphs);
185 }
186 
onCountGlyphs() const187 int SkRandomTypeface::onCountGlyphs() const { return fProxy->countGlyphs(); }
188 
onGetUPEM() const189 int SkRandomTypeface::onGetUPEM() const { return fProxy->getUnitsPerEm(); }
190 
onGetFamilyName(SkString * familyName) const191 void SkRandomTypeface::onGetFamilyName(SkString* familyName) const {
192     fProxy->getFamilyName(familyName);
193 }
194 
onGetPostScriptName(SkString * postScriptName) const195 bool SkRandomTypeface::onGetPostScriptName(SkString* postScriptName) const {
196     return fProxy->getPostScriptName(postScriptName);
197 }
198 
onCreateFamilyNameIterator() const199 SkTypeface::LocalizedStrings* SkRandomTypeface::onCreateFamilyNameIterator() const {
200     return fProxy->createFamilyNameIterator();
201 }
202 
getPostScriptGlyphNames(SkString * names) const203 void SkRandomTypeface::getPostScriptGlyphNames(SkString* names) const {
204     return fProxy->getPostScriptGlyphNames(names);
205 }
206 
onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],int coordinateCount) const207 int SkRandomTypeface::onGetVariationDesignPosition(
208         SkFontArguments::VariationPosition::Coordinate coordinates[],
209         int                                            coordinateCount) const {
210     return fProxy->onGetVariationDesignPosition(coordinates, coordinateCount);
211 }
212 
onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],int parameterCount) const213 int SkRandomTypeface::onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],
214                                                      int parameterCount) const {
215     return fProxy->onGetVariationDesignParameters(parameters, parameterCount);
216 }
217 
onGetTableTags(SkFontTableTag tags[]) const218 int SkRandomTypeface::onGetTableTags(SkFontTableTag tags[]) const {
219     return fProxy->getTableTags(tags);
220 }
221 
onGetTableData(SkFontTableTag tag,size_t offset,size_t length,void * data) const222 size_t SkRandomTypeface::onGetTableData(SkFontTableTag tag,
223                                         size_t         offset,
224                                         size_t         length,
225                                         void*          data) const {
226     return fProxy->getTableData(tag, offset, length, data);
227 }
228