Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
28static sk_sp<SkTextBlob> make_hpos_test_blob_utf8(const char* text, const SkFont& font) {
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
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
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
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
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
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
192 SkFont font(fEmojiFont.typeface, 40);
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
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 static SkString SkStringPrintf()
Definition SkString.h:287
int32_t SkUnichar
Definition SkTypes.h:175
void restore()
Definition SkCanvas.cpp:465
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
void translate(SkScalar dx, SkScalar dy)
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition SkCanvas.h:1182
int save()
Definition SkCanvas.cpp:451
void concat(const SkMatrix &matrix)
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
@ 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
SkString getName() const override
SkISize getISize() override
ScaledEmojiGM(ToolUtils::EmojiFontFormat format)
void onOnceBeforeDraw() override
ToolUtils::EmojiTestSample fEmojiFont
DrawResult onDraw(SkCanvas *canvas, SkString *errorMsg) override
SkString getName() const override
ToolUtils::EmojiTestSample fEmojiFont
DrawResult onDraw(SkCanvas *canvas, SkString *errorMsg) override
ScaledEmojiPerspectiveGM(ToolUtils::EmojiFontFormat format)
ToolUtils::EmojiTestSample fEmojiFont
DrawResult onDraw(SkCanvas *canvas, SkString *errorMsg) override
ScaledEmojiPosGM(ToolUtils::EmojiFontFormat format)
SkString getName() const override
SkISize getISize() override
void onOnceBeforeDraw() override
const Paint & paint
float SkScalar
Definition extension.cpp:12
static const uint8_t buffer[]
uint32_t uint32_t * format
#define DEF_GM(CODE)
Definition gm.h:40
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()
DrawResult
Definition gm.h:104
static sk_sp< SkTextBlob > make_hpos_test_blob_utf8(const char *text, const SkFont &font)
SkScalar fLeading
distance to add between lines, typically positive or zero
SkScalar fAscent
distance to reserve above baseline, typically negative
SkScalar fDescent
distance to reserve below baseline, typically positive
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
sk_sp< SkTypeface > typeface