xref: /aosp_15_r20/external/skia/gm/scaledemoji.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2018 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 "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkFontMetrics.h"
13 #include "include/core/SkFontTypes.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkSize.h"
18 #include "include/core/SkString.h"
19 #include "include/core/SkTextBlob.h"
20 #include "include/core/SkTypeface.h"
21 #include "src/base/SkUTF.h"
22 #include "tools/ToolUtils.h"
23 #include "tools/fonts/FontToolUtils.h"
24 
25 #include <string.h>
26 #include <initializer_list>
27 
make_hpos_test_blob_utf8(const char * text,const SkFont & font)28 static sk_sp<SkTextBlob> make_hpos_test_blob_utf8(const char* text, const SkFont& font) {
29     constexpr SkTextEncoding enc = SkTextEncoding::kUTF8;
30     SkTextBlobBuilder builder;
31     size_t len = strlen(text);
32     int glyphCount = font.countText(text, len, enc);
33     const auto& buffer = builder.allocRunPosH(font, glyphCount, 0);
34     (void)font.textToGlyphs(text, len, enc, buffer.glyphs, glyphCount);
35     font.getXPos(buffer.glyphs, glyphCount, buffer.pos);
36     return builder.make();
37 }
38 
39 namespace skiagm {
40 
41 class ScaledEmojiGM : public GM {
42 public:
ScaledEmojiGM(ToolUtils::EmojiFontFormat format)43     ScaledEmojiGM(ToolUtils::EmojiFontFormat format) : fFormat(format) {}
44 
45 protected:
46     ToolUtils::EmojiTestSample fEmojiFont;
47 
onOnceBeforeDraw()48     void onOnceBeforeDraw() override { fEmojiFont = ToolUtils::EmojiSample(fFormat); }
49 
getName() const50     SkString getName() const override {
51         return SkString("scaledemoji_") += ToolUtils::NameForFontFormat(fFormat);
52     }
53 
getISize()54     SkISize getISize() override { return SkISize::Make(1200, 1200); }
55 
onDraw(SkCanvas * canvas,SkString * errorMsg)56     DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
57         if (!fEmojiFont.typeface) {
58             *errorMsg = SkStringPrintf("Unable to instantiate emoji test font of format %s.",
59                                        ToolUtils::NameForFontFormat(fFormat).c_str());
60             return DrawResult::kSkip;
61         }
62 
63         canvas->drawColor(SK_ColorGRAY);
64 
65         SkPaint paint;
66         SkFont font(fEmojiFont.typeface);
67         font.setEdging(SkFont::Edging::kAlias);
68 
69         const char* text = fEmojiFont.sampleText;
70 
71         // draw text at different point sizes
72         // Testing GPU bitmap path, SDF path with no scaling,
73         // SDF path with scaling, path rendering with scaling
74         SkFontMetrics metrics;
75         SkScalar y = 0;
76         for (SkScalar textSize : {70, 180, 270, 340}) {
77             font.setSize(textSize);
78             font.getMetrics(&metrics);
79             y += -metrics.fAscent;
80             canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 10, y, font, paint);
81             y += metrics.fDescent + metrics.fLeading;
82         }
83 
84         return DrawResult::kOk;
85     }
86 
87 private:
88     ToolUtils::EmojiFontFormat fFormat;
89     using INHERITED = GM;
90 };
91 
92 class ScaledEmojiPosGM : public GM {
93 public:
ScaledEmojiPosGM(ToolUtils::EmojiFontFormat format)94     ScaledEmojiPosGM(ToolUtils::EmojiFontFormat format) : fFormat(format) {}
95 
96 protected:
97     ToolUtils::EmojiTestSample fEmojiFont;
98 
onOnceBeforeDraw()99     void onOnceBeforeDraw() override { fEmojiFont = ToolUtils::EmojiSample(fFormat); }
100 
getName() const101     SkString getName() const override {
102         return SkString("scaledemojipos_") += ToolUtils::NameForFontFormat(fFormat);
103     }
104 
getISize()105     SkISize getISize() override { return SkISize::Make(1200, 1200); }
106 
onDraw(SkCanvas * canvas,SkString * errorMsg)107     DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
108         if (!fEmojiFont.typeface) {
109             *errorMsg = SkStringPrintf("Unable to instantiate emoji test font of format %s.",
110                                        ToolUtils::NameForFontFormat(fFormat).c_str());
111             return DrawResult::kSkip;
112         }
113 
114         canvas->drawColor(SK_ColorGRAY);
115 
116         SkPaint paint;
117         SkFont font(fEmojiFont.typeface, 12);
118         const char* text = fEmojiFont.sampleText;
119 
120         // draw text at different point sizes
121         // Testing GPU bitmap path, SDF path with no scaling,
122         // SDF path with scaling, path rendering with scaling
123         SkFontMetrics metrics;
124         SkScalar y = 0;
125         for (SkScalar textSize : {70, 180, 270, 340}) {
126             font.setSize(textSize);
127             font.getMetrics(&metrics);
128             y += -metrics.fAscent;
129 
130             sk_sp<SkTextBlob> blob = make_hpos_test_blob_utf8(text, font);
131             // Draw with an origin.
132             canvas->drawTextBlob(blob, 10, y, paint);
133 
134             // Draw with shifted canvas.
135             canvas->save();
136             canvas->translate(750, 0);
137             canvas->drawTextBlob(blob, 10, y, paint);
138             canvas->restore();
139 
140             y += metrics.fDescent + metrics.fLeading;
141         }
142 
143         return DrawResult::kOk;
144     }
145 
146 private:
147     ToolUtils::EmojiFontFormat fFormat;
148     using INHERITED = GM;
149 };
150 
151 class ScaledEmojiPerspectiveGM : public GM {
152 public:
ScaledEmojiPerspectiveGM(ToolUtils::EmojiFontFormat format)153     ScaledEmojiPerspectiveGM(ToolUtils::EmojiFontFormat format) : fFormat(format) {}
154 
155 protected:
156     ToolUtils::EmojiTestSample fEmojiFont;
157     SkString fStripSpacesSampleText;
158 
onOnceBeforeDraw()159     void onOnceBeforeDraw() override {
160         fEmojiFont = ToolUtils::EmojiSample(fFormat);
161 
162         int count = 0;
163         const char* ch_ptr = fEmojiFont.sampleText;
164         const char* ch_end = ch_ptr + strlen(ch_ptr);
165         while (ch_ptr < ch_end && count < 2) {
166             SkUnichar ch = SkUTF::NextUTF8(&ch_ptr, ch_end);
167             if (ch != ' ') {
168                 fStripSpacesSampleText.appendUnichar(ch);
169                 ++count;
170             }
171         }
172     }
173 
getName() const174     SkString getName() const override {
175         return SkString("scaledemojiperspective_") += ToolUtils::NameForFontFormat(fFormat);
176     }
177 
getISize()178     SkISize getISize() override { return SkISize::Make(1200, 1200); }
179 
onDraw(SkCanvas * canvas,SkString * errorMsg)180     DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
181         if (!fEmojiFont.typeface) {
182             *errorMsg = SkStringPrintf("Unable to instantiate emoji test font of format %s.",
183                                        ToolUtils::NameForFontFormat(fFormat).c_str());
184             return DrawResult::kSkip;
185         }
186 
187         canvas->drawColor(SK_ColorGRAY);
188         SkMatrix taper;
189         taper.setPerspY(-0.0025f);
190 
191         SkPaint paint;
192         SkFont font(fEmojiFont.typeface, 40);
193         sk_sp<SkTextBlob> blob = make_hpos_test_blob_utf8(fStripSpacesSampleText.c_str(), font);
194 
195         // draw text at different point sizes
196         // Testing GPU bitmap path, SDF path with no scaling,
197         // SDF path with scaling, path rendering with scaling
198         SkFontMetrics metrics;
199         font.getMetrics(&metrics);
200         for (auto rotate : {0.0, 45.0, 90.0, 135.0, 180.0, 225.0, 270.0, 315.0}) {
201             canvas->save();
202             SkMatrix perspective;
203             perspective.postTranslate(-600, -600);
204             perspective.postConcat(taper);
205             perspective.postRotate(rotate);
206             perspective.postTranslate(600, 600);
207             canvas->concat(perspective);
208             SkScalar y = 670;
209             for (int i = 0; i < 5; i++) {
210 
211                 y += -metrics.fAscent;
212 
213                 // Draw with an origin.
214                 canvas->drawTextBlob(blob, 565, y, paint);
215 
216                 y += metrics.fDescent + metrics.fLeading;
217             }
218             canvas->restore();
219         }
220 
221         return DrawResult::kOk;
222     }
223 
224 private:
225     ToolUtils::EmojiFontFormat fFormat;
226     using INHERITED = GM;
227 };
228 
229 //////////////////////////////////////////////////////////////////////////////
230 
231 DEF_GM(return new ScaledEmojiGM(ToolUtils::EmojiFontFormat::Cbdt);)
232 DEF_GM(return new ScaledEmojiPosGM(ToolUtils::EmojiFontFormat::Cbdt);)
233 DEF_GM(return new ScaledEmojiPerspectiveGM(ToolUtils::EmojiFontFormat::Cbdt);)
234 DEF_GM(return new ScaledEmojiGM(ToolUtils::EmojiFontFormat::Sbix);)
235 DEF_GM(return new ScaledEmojiPosGM(ToolUtils::EmojiFontFormat::Sbix);)
236 DEF_GM(return new ScaledEmojiPerspectiveGM(ToolUtils::EmojiFontFormat::Sbix);)
237 DEF_GM(return new ScaledEmojiGM(ToolUtils::EmojiFontFormat::ColrV0);)
238 DEF_GM(return new ScaledEmojiPosGM(ToolUtils::EmojiFontFormat::ColrV0);)
239 DEF_GM(return new ScaledEmojiPerspectiveGM(ToolUtils::EmojiFontFormat::ColrV0);)
240 DEF_GM(return new ScaledEmojiGM(ToolUtils::EmojiFontFormat::Svg);)
241 DEF_GM(return new ScaledEmojiPosGM(ToolUtils::EmojiFontFormat::Svg);)
242 DEF_GM(return new ScaledEmojiPerspectiveGM(ToolUtils::EmojiFontFormat::Svg);)
243 DEF_GM(return new ScaledEmojiGM(ToolUtils::EmojiFontFormat::Test);)
244 DEF_GM(return new ScaledEmojiPosGM(ToolUtils::EmojiFontFormat::Test);)
245 DEF_GM(return new ScaledEmojiPerspectiveGM(ToolUtils::EmojiFontFormat::Test);)
246 
247 }  // namespace skiagm
248