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