Flutter Engine
The Flutter Engine
scaledemoji.cpp
Go to the documentation of this file.
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"
11#include "include/core/SkFont.h"
17#include "include/core/SkSize.h"
21#include "src/base/SkUTF.h"
22#include "tools/ToolUtils.h"
24
25#include <string.h>
26#include <initializer_list>
27
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
39namespace skiagm {
40
41class ScaledEmojiGM : public GM {
42public:
44
45protected:
47
48 void onOnceBeforeDraw() override { fEmojiFont = ToolUtils::EmojiSample(fFormat); }
49
50 SkString getName() const override {
51 return SkString("scaledemoji_") += ToolUtils::NameForFontFormat(fFormat);
52 }
53
54 SkISize getISize() override { return SkISize::Make(1200, 1200); }
55
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
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
87private:
89 using INHERITED = GM;
90};
91
92class ScaledEmojiPosGM : public GM {
93public:
95
96protected:
98
99 void onOnceBeforeDraw() override { fEmojiFont = ToolUtils::EmojiSample(fFormat); }
100
101 SkString getName() const override {
102 return SkString("scaledemojipos_") += ToolUtils::NameForFontFormat(fFormat);
103 }
104
105 SkISize getISize() override { return SkISize::Make(1200, 1200); }
106
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
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
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
146private:
148 using INHERITED = GM;
149};
150
152public:
154
155protected:
158
159 void onOnceBeforeDraw() override {
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 != ' ') {
169 ++count;
170 }
171 }
172 }
173
174 SkString getName() const override {
175 return SkString("scaledemojiperspective_") += ToolUtils::NameForFontFormat(fFormat);
176 }
177
178 SkISize getISize() override { return SkISize::Make(1200, 1200); }
179
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
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
224private:
226 using INHERITED = GM;
227};
228
229//////////////////////////////////////////////////////////////////////////////
230
231DEF_GM(return new ScaledEmojiGM(ToolUtils::EmojiFontFormat::Cbdt);)
232DEF_GM(return new ScaledEmojiPosGM(ToolUtils::EmojiFontFormat::Cbdt);)
233DEF_GM(return new ScaledEmojiPerspectiveGM(ToolUtils::EmojiFontFormat::Cbdt);)
234DEF_GM(return new ScaledEmojiGM(ToolUtils::EmojiFontFormat::Sbix);)
235DEF_GM(return new ScaledEmojiPosGM(ToolUtils::EmojiFontFormat::Sbix);)
236DEF_GM(return new ScaledEmojiPerspectiveGM(ToolUtils::EmojiFontFormat::Sbix);)
237DEF_GM(return new ScaledEmojiGM(ToolUtils::EmojiFontFormat::ColrV0);)
238DEF_GM(return new ScaledEmojiPosGM(ToolUtils::EmojiFontFormat::ColrV0);)
239DEF_GM(return new ScaledEmojiPerspectiveGM(ToolUtils::EmojiFontFormat::ColrV0);)
240DEF_GM(return new ScaledEmojiGM(ToolUtils::EmojiFontFormat::Svg);)
241DEF_GM(return new ScaledEmojiPosGM(ToolUtils::EmojiFontFormat::Svg);)
242DEF_GM(return new ScaledEmojiPerspectiveGM(ToolUtils::EmojiFontFormat::Svg);)
243DEF_GM(return new ScaledEmojiGM(ToolUtils::EmojiFontFormat::Test);)
244DEF_GM(return new ScaledEmojiPosGM(ToolUtils::EmojiFontFormat::Test);)
245DEF_GM(return new ScaledEmojiPerspectiveGM(ToolUtils::EmojiFontFormat::Test);)
246
247} // namespace skiagm
int count
Definition: FontMgrTest.cpp:50
constexpr SkColor SK_ColorGRAY
Definition: SkColor.h:113
SkTextEncoding
Definition: SkFontTypes.h:11
@ kUTF8
uses bytes to represent UTF-8 or ASCII
static bool rotate(const SkDCubic &cubic, int zero, int index, SkDCubic &rotPath)
SK_API SkString SkStringPrintf(const char *format,...) SK_PRINTF_LIKE(1
Creates a new string and writes into it using a printf()-style format.
int32_t SkUnichar
Definition: SkTypes.h:175
void restore()
Definition: SkCanvas.cpp:461
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.cpp:2413
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition: SkCanvas.h:1182
int save()
Definition: SkCanvas.cpp:447
void concat(const SkMatrix &matrix)
Definition: SkCanvas.cpp:1318
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
Definition: SkCanvas.cpp:2484
Definition: SkFont.h:35
@ kAlias
no transparent pixels on glyph edges
SkMatrix & postTranslate(SkScalar dx, SkScalar dy)
Definition: SkMatrix.cpp:281
SkMatrix & postRotate(SkScalar degrees, SkScalar px, SkScalar py)
Definition: SkMatrix.cpp:474
SkMatrix & postConcat(const SkMatrix &other)
Definition: SkMatrix.cpp:683
SkMatrix & setPerspY(SkScalar v)
Definition: SkMatrix.h:544
void appendUnichar(SkUnichar uni)
Definition: SkString.h:207
const char * c_str() const
Definition: SkString.h:133
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
SkString getName() const override
Definition: scaledemoji.cpp:50
SkISize getISize() override
Definition: scaledemoji.cpp:54
ScaledEmojiGM(ToolUtils::EmojiFontFormat format)
Definition: scaledemoji.cpp:43
void onOnceBeforeDraw() override
Definition: scaledemoji.cpp:48
ToolUtils::EmojiTestSample fEmojiFont
Definition: scaledemoji.cpp:46
DrawResult onDraw(SkCanvas *canvas, SkString *errorMsg) override
Definition: scaledemoji.cpp:56
SkString getName() const override
ToolUtils::EmojiTestSample fEmojiFont
DrawResult onDraw(SkCanvas *canvas, SkString *errorMsg) override
ScaledEmojiPerspectiveGM(ToolUtils::EmojiFontFormat format)
ToolUtils::EmojiTestSample fEmojiFont
Definition: scaledemoji.cpp:97
DrawResult onDraw(SkCanvas *canvas, SkString *errorMsg) override
ScaledEmojiPosGM(ToolUtils::EmojiFontFormat format)
Definition: scaledemoji.cpp:94
SkString getName() const override
SkISize getISize() override
void onOnceBeforeDraw() override
Definition: scaledemoji.cpp:99
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
uint32_t uint32_t * format
std::u16string text
double y
SK_SPI SkUnichar NextUTF8(const char **ptr, const char *end)
Definition: SkUTF.cpp:118
SkString NameForFontFormat(EmojiFontFormat format)
EmojiTestSample EmojiSample()
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
font
Font Metadata and Metrics.
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
DrawResult
Definition: gm.h:104
static sk_sp< SkTextBlob > make_hpos_test_blob_utf8(const char *text, const SkFont &font)
Definition: scaledemoji.cpp:28
SkScalar fLeading
distance to add between lines, typically positive or zero
Definition: SkFontMetrics.h:57
SkScalar fAscent
distance to reserve above baseline, typically negative
Definition: SkFontMetrics.h:54
SkScalar fDescent
distance to reserve below baseline, typically positive
Definition: SkFontMetrics.h:55
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
sk_sp< SkTypeface > typeface
Definition: FontToolUtils.h:48