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