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/SkMakeUnique.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 unsigned generateGlyphCount() override;
28 bool generateAdvance(SkGlyph*) override;
29 void generateMetrics(SkGlyph*) override;
30 void generateImage(const SkGlyph&) override;
31 bool generatePath(SkGlyphID, SkPath*) 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 bool fFakeIt;
40 };
41
RandomScalerContext(sk_sp<SkRandomTypeface> face,const SkScalerContextEffects & effects,const SkDescriptor * desc,bool fakeIt)42 RandomScalerContext::RandomScalerContext(sk_sp<SkRandomTypeface> face,
43 const SkScalerContextEffects& effects,
44 const SkDescriptor* desc,
45 bool fakeIt)
46 : SkScalerContext(std::move(face), effects, desc)
47 , fProxy(getRandomTypeface()->proxy()->createScalerContext(SkScalerContextEffects(), desc))
48 , fFakeIt(fakeIt) {
49 fProxy->forceGenerateImageFromPath();
50 }
51
generateGlyphCount()52 unsigned RandomScalerContext::generateGlyphCount() { return fProxy->getGlyphCount(); }
53
generateAdvance(SkGlyph * glyph)54 bool RandomScalerContext::generateAdvance(SkGlyph* glyph) { return fProxy->generateAdvance(glyph); }
55
generateMetrics(SkGlyph * glyph)56 void RandomScalerContext::generateMetrics(SkGlyph* glyph) {
57 // Here we will change the mask format of the glyph
58 // NOTE: this may be overridden by the base class (e.g. if a mask filter is applied).
59 switch (glyph->getGlyphID() % 4) {
60 case 0: glyph->fMaskFormat = SkMask::kLCD16_Format; break;
61 case 1: glyph->fMaskFormat = SkMask::kA8_Format; break;
62 case 2: glyph->fMaskFormat = SkMask::kARGB32_Format; break;
63 case 3: glyph->fMaskFormat = SkMask::kBW_Format; break;
64 }
65
66 fProxy->getMetrics(glyph);
67
68 if (fFakeIt || (glyph->getGlyphID() % 4) != 2) {
69 return;
70 }
71
72 SkPath path;
73 if (!fProxy->getPath(glyph->getPackedID(), &path)) {
74 return;
75 }
76 glyph->fMaskFormat = SkMask::kARGB32_Format;
77
78 SkRect storage;
79 const SkPaint& paint = this->getRandomTypeface()->paint();
80 const SkRect& newBounds =
81 paint.doComputeFastBounds(path.getBounds(), &storage, SkPaint::kFill_Style);
82 SkIRect ibounds;
83 newBounds.roundOut(&ibounds);
84 glyph->fLeft = ibounds.fLeft;
85 glyph->fTop = ibounds.fTop;
86 glyph->fWidth = ibounds.width();
87 glyph->fHeight = ibounds.height();
88 }
89
generateImage(const SkGlyph & glyph)90 void RandomScalerContext::generateImage(const SkGlyph& glyph) {
91 // TODO: can force down but not up
92 /*
93 SkMask::Format format = (SkMask::Format)glyph.fMaskFormat;
94 switch (glyph.getGlyphID() % 4) {
95 case 0: format = SkMask::kLCD16_Format; break;
96 case 1: format = SkMask::kA8_Format; break;
97 case 2: format = SkMask::kARGB32_Format; break;
98 case 3: format = SkMask::kBW_Format; break;
99 }
100 const_cast<SkGlyph&>(glyph).fMaskFormat = format;
101 */
102
103 if (fFakeIt) {
104 sk_bzero(glyph.fImage, glyph.imageSize());
105 return;
106 }
107
108 if (SkMask::kARGB32_Format != glyph.fMaskFormat) {
109 fProxy->getImage(glyph);
110 return;
111 }
112
113 // If the format is ARGB, just draw the glyph from path.
114 SkPath path;
115 if (!fProxy->getPath(glyph.getPackedID(), &path)) {
116 fProxy->getImage(glyph);
117 return;
118 }
119
120 SkBitmap bm;
121 bm.installPixels(SkImageInfo::MakeN32Premul(glyph.fWidth, glyph.fHeight),
122 glyph.fImage,
123 glyph.rowBytes());
124 bm.eraseColor(0);
125
126 SkCanvas canvas(bm);
127 canvas.translate(-SkIntToScalar(glyph.fLeft), -SkIntToScalar(glyph.fTop));
128 canvas.drawPath(path, this->getRandomTypeface()->paint());
129 }
130
generatePath(SkGlyphID glyph,SkPath * path)131 bool RandomScalerContext::generatePath(SkGlyphID glyph, SkPath* path) {
132 return fProxy->generatePath(glyph, path);
133 }
134
generateFontMetrics(SkFontMetrics * metrics)135 void RandomScalerContext::generateFontMetrics(SkFontMetrics* metrics) {
136 fProxy->getFontMetrics(metrics);
137 }
138
139 ///////////////////////////////////////////////////////////////////////////////
140
SkRandomTypeface(sk_sp<SkTypeface> proxy,const SkPaint & paint,bool fakeIt)141 SkRandomTypeface::SkRandomTypeface(sk_sp<SkTypeface> proxy, const SkPaint& paint, bool fakeIt)
142 : SkTypeface(proxy->fontStyle(), false)
143 , fProxy(std::move(proxy))
144 , fPaint(paint)
145 , fFakeIt(fakeIt) {}
146
onCreateScalerContext(const SkScalerContextEffects & effects,const SkDescriptor * desc) const147 SkScalerContext* SkRandomTypeface::onCreateScalerContext(const SkScalerContextEffects& effects,
148 const SkDescriptor* desc) const {
149 return new RandomScalerContext(
150 sk_ref_sp(const_cast<SkRandomTypeface*>(this)), effects, desc, fFakeIt);
151 }
152
onFilterRec(SkScalerContextRec * rec) const153 void SkRandomTypeface::onFilterRec(SkScalerContextRec* rec) const {
154 fProxy->filterRec(rec);
155 rec->setHinting(SkFontHinting::kNone);
156 rec->fMaskFormat = SkMask::kARGB32_Format;
157 }
158
getGlyphToUnicodeMap(SkUnichar * glyphToUnicode) const159 void SkRandomTypeface::getGlyphToUnicodeMap(SkUnichar* glyphToUnicode) const {
160 fProxy->getGlyphToUnicodeMap(glyphToUnicode);
161 }
162
onGetAdvancedMetrics() const163 std::unique_ptr<SkAdvancedTypefaceMetrics> SkRandomTypeface::onGetAdvancedMetrics() const {
164 return fProxy->getAdvancedMetrics();
165 }
166
onOpenStream(int * ttcIndex) const167 std::unique_ptr<SkStreamAsset> SkRandomTypeface::onOpenStream(int* ttcIndex) const {
168 return fProxy->openStream(ttcIndex);
169 }
170
onMakeClone(const SkFontArguments & args) const171 sk_sp<SkTypeface> SkRandomTypeface::onMakeClone(const SkFontArguments& args) const {
172 sk_sp<SkTypeface> proxy = fProxy->makeClone(args);
173 if (!proxy) {
174 return nullptr;
175 }
176 return sk_make_sp<SkRandomTypeface>(proxy, fPaint, fFakeIt);
177 }
178
onGetFontDescriptor(SkFontDescriptor * desc,bool * isLocal) const179 void SkRandomTypeface::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const {
180 // TODO: anything that uses this typeface isn't correctly serializable, since this typeface
181 // cannot be deserialized.
182 fProxy->getFontDescriptor(desc, isLocal);
183 }
184
onCharsToGlyphs(const SkUnichar * uni,int count,SkGlyphID glyphs[]) const185 void SkRandomTypeface::onCharsToGlyphs(const SkUnichar* uni, int count, SkGlyphID glyphs[]) const {
186 fProxy->unicharsToGlyphs(uni, count, glyphs);
187 }
188
onCountGlyphs() const189 int SkRandomTypeface::onCountGlyphs() const { return fProxy->countGlyphs(); }
190
onGetUPEM() const191 int SkRandomTypeface::onGetUPEM() const { return fProxy->getUnitsPerEm(); }
192
onGetFamilyName(SkString * familyName) const193 void SkRandomTypeface::onGetFamilyName(SkString* familyName) const {
194 fProxy->getFamilyName(familyName);
195 }
196
onCreateFamilyNameIterator() const197 SkTypeface::LocalizedStrings* SkRandomTypeface::onCreateFamilyNameIterator() const {
198 return fProxy->createFamilyNameIterator();
199 }
200
getPostScriptGlyphNames(SkString * names) const201 void SkRandomTypeface::getPostScriptGlyphNames(SkString* names) const {
202 return fProxy->getPostScriptGlyphNames(names);
203 }
204
onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],int coordinateCount) const205 int SkRandomTypeface::onGetVariationDesignPosition(
206 SkFontArguments::VariationPosition::Coordinate coordinates[],
207 int coordinateCount) const {
208 return fProxy->onGetVariationDesignPosition(coordinates, coordinateCount);
209 }
210
onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],int parameterCount) const211 int SkRandomTypeface::onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],
212 int parameterCount) const {
213 return fProxy->onGetVariationDesignParameters(parameters, parameterCount);
214 }
215
onGetTableTags(SkFontTableTag tags[]) const216 int SkRandomTypeface::onGetTableTags(SkFontTableTag tags[]) const {
217 return fProxy->getTableTags(tags);
218 }
219
onGetTableData(SkFontTableTag tag,size_t offset,size_t length,void * data) const220 size_t SkRandomTypeface::onGetTableData(SkFontTableTag tag,
221 size_t offset,
222 size_t length,
223 void* data) const {
224 return fProxy->getTableData(tag, offset, length, data);
225 }
226