Flutter Engine
The Flutter Engine
fontations.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2023 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"
10#include "include/core/SkFont.h"
16#include "tools/Resources.h"
17
18
19namespace skiagm {
20
21namespace {
22const SkScalar kTextSizes[] = {12, 18, 30, 120};
23const char kReportFontName[] = "fonts/Roboto-Regular.ttf";
24const SkScalar kDumpFontSize = 20.0f;
25
26// TODO(drott): Test these dumps is in a unit test instead of dumping them to GM surface.
27void dumpToCanvas(SkCanvas* canvas, SkString text, sk_sp<SkTypeface> reportTypeface) {
28 canvas->drawSimpleText(text.c_str(),
29 text.size() - 1,
31 0,
32 0,
33 SkFont(reportTypeface, kDumpFontSize),
34 SkPaint());
35}
36
37void dumpLocalizedStrings(SkCanvas* canvas,
38 sk_sp<SkTypeface> typeface,
39 sk_sp<SkTypeface> reportTypeface) {
40 auto family_names = typeface->createFamilyNameIterator();
42 SkString localizedName;
43 while (family_names->next(&famName)) {
44 localizedName.printf(
45 "Name: %s Language: %s\n", famName.fString.c_str(), famName.fLanguage.c_str());
46 dumpToCanvas(canvas, localizedName, reportTypeface);
47 canvas->translate(0, kDumpFontSize * 1.2);
48 }
49 family_names->unref();
50}
51
52void dumpGlyphCount(SkCanvas* canvas,
53 sk_sp<SkTypeface> typeface,
54 sk_sp<SkTypeface> reportTypeface) {
55 SkString glyphCount;
56 glyphCount.printf("Num glyphs: %d\n", typeface->countGlyphs());
57 dumpToCanvas(canvas, glyphCount, reportTypeface);
58}
59
60void dumpFamilyAndPostscriptName(SkCanvas* canvas,
61 sk_sp<SkTypeface> typeface,
62 sk_sp<SkTypeface> reportTypeface) {
64 typeface->getFamilyName(&name);
65 SkString nameDump;
66 nameDump.printf("Family name: %s\n", name.c_str());
67 dumpToCanvas(canvas, nameDump, reportTypeface);
68
69 if (typeface->getPostScriptName(&name)) {
70 canvas->translate(0, kDumpFontSize * 1.2);
71 nameDump.printf("PS Name: %s\n", name.c_str());
72 dumpToCanvas(canvas, nameDump, reportTypeface);
73 } else {
74 canvas->translate(0, kDumpFontSize * 1.2);
75 nameDump.printf("No Postscript name.");
76 dumpToCanvas(canvas, nameDump, reportTypeface);
77 }
78}
79
80} // namespace
81
82class FontationsTypefaceGM : public GM {
83public:
85 kMakeWithFontArguments,
86 kCloneWithFontArguments,
87 };
89 const char* testFontFilename,
90 std::initializer_list<SkFontArguments::VariationPosition::Coordinate>
91 specifiedVariations,
93 : fTestName(testName)
94 , fTestFontFilename(testFontFilename)
95 , fConstruction(construction) {
97 fVariationPosition.coordinateCount = specifiedVariations.size();
98 fCoordinates = std::make_unique<SkFontArguments::VariationPosition::Coordinate[]>(
99 specifiedVariations.size());
100 for (size_t i = 0; i < specifiedVariations.size(); ++i) {
101 fCoordinates[i] = std::data(specifiedVariations)[i];
102 }
103
104 fVariationPosition.coordinates = fCoordinates.get();
105 }
106
107protected:
108 void onOnceBeforeDraw() override {
110 fTestTypeface = SkTypeface_Make_Fontations(
111 GetResourceAsStream(fTestFontFilename),
112 SkFontArguments().setVariationDesignPosition(fVariationPosition));
113 } else {
114 fTestTypeface = SkTypeface_Make_Fontations(GetResourceAsStream(fTestFontFilename),
116 ->makeClone(SkFontArguments().setVariationDesignPosition(
117 fVariationPosition));
118 }
119 fReportTypeface =
121 }
122
123 SkString getName() const override {
124 return SkStringPrintf("typeface_fontations_%s", fTestName.c_str());
125 }
126
127 SkISize getISize() override { return SkISize::Make(400, 200); }
128
129 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
131 paint.setColor(SK_ColorBLACK);
132
133 if (!fTestTypeface) {
134 *errorMsg = "Unable to initialize typeface.";
135 return DrawResult::kSkip;
136 }
137
138 SkFont font(fTestTypeface);
139 const char32_t testText[] = U"abc";
140 size_t testTextBytesize = std::char_traits<char32_t>::length(testText) * sizeof(char32_t);
141 SkScalar x = 100;
142 SkScalar y = 150;
143
144 for (SkScalar textSize : kTextSizes) {
145 font.setSize(textSize);
146 y += font.getSpacing();
147
148 /* Draw origin marker as a green dot. */
149 paint.setColor(SK_ColorGREEN);
150 canvas->drawRect(SkRect::MakeXYWH(x, y, 2, 2), paint);
151 paint.setColor(SK_ColorBLACK);
152
153 canvas->drawSimpleText(
154 testText, testTextBytesize, SkTextEncoding::kUTF32, x, y, font, paint);
155 }
156
157 canvas->translate(100, 470);
158 dumpGlyphCount(canvas, fTestTypeface, fReportTypeface);
159 canvas->translate(0, kDumpFontSize * 1.2);
160 dumpLocalizedStrings(canvas, fTestTypeface, fReportTypeface);
161 canvas->translate(0, kDumpFontSize * 1.2);
162 dumpFamilyAndPostscriptName(canvas, fTestTypeface, fReportTypeface);
163
164 return DrawResult::kOk;
165 }
166
167private:
168 using INHERITED = GM;
169
170 const SkString fTestName;
171 const char* fTestFontFilename;
172 sk_sp<SkTypeface> fTestTypeface;
173 sk_sp<SkTypeface> fReportTypeface;
174 SkFontArguments::VariationPosition fVariationPosition;
175 std::unique_ptr<SkFontArguments::VariationPosition::Coordinate[]> fCoordinates;
176 TypefaceConstruction fConstruction;
177};
178
179namespace {
180SkFourByteTag constexpr operator"" _t(const char* tagName, size_t size) {
181 SkASSERT(size == 4);
182 return SkSetFourByteTag(tagName[0], tagName[1], tagName[2], tagName[3]);
183}
184} // namespace
185DEF_GM(return new FontationsTypefaceGM("roboto", "fonts/Roboto-Regular.ttf", {});)
187 "distortable_light",
188 "fonts/Distortable.ttf",
189 {{"wght"_t, 0.5f}}))
190DEF_GM(return new FontationsTypefaceGM(
191 "distortable_bold",
192 "fonts/Distortable.ttf",
193 {{"wght"_t, 2.0f}},
195
196} // namespace skiagm
std::unique_ptr< SkStreamAsset > GetResourceAsStream(const char *resource, bool useFileStream)
Definition: Resources.cpp:31
#define SkASSERT(cond)
Definition: SkAssert.h:116
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
@ kUTF8
uses bytes to represent UTF-8 or ASCII
@ kUTF32
uses four byte words to represent all of Unicode
SK_API SkString SkStringPrintf(const char *format,...) SK_PRINTF_LIKE(1
Creates a new string and writes into it using a printf()-style format.
SK_API sk_sp< SkTypeface > SkTypeface_Make_Fontations(std::unique_ptr< SkStreamAsset > fontData, const SkFontArguments &args)
uint32_t SkFourByteTag
Definition: SkTypes.h:166
static constexpr SkFourByteTag SkSetFourByteTag(char a, char b, char c, char d)
Definition: SkTypes.h:167
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
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
Definition: SkFont.h:35
void printf(const char format[],...) SK_PRINTF_LIKE(2
Definition: SkString.cpp:534
const char * c_str() const
Definition: SkString.h:133
int countGlyphs() const
Definition: SkTypeface.cpp:432
LocalizedStrings * createFamilyNameIterator() const
Definition: SkTypeface.cpp:455
void getFamilyName(SkString *name) const
Definition: SkTypeface.cpp:459
bool getPostScriptName(SkString *name) const
Definition: SkTypeface.cpp:464
sk_sp< SkTypeface > makeClone(const SkFontArguments &) const
Definition: SkTypeface.cpp:190
SkString getName() const override
Definition: fontations.cpp:123
void onOnceBeforeDraw() override
Definition: fontations.cpp:108
FontationsTypefaceGM(const char *testName, const char *testFontFilename, std::initializer_list< SkFontArguments::VariationPosition::Coordinate > specifiedVariations, TypefaceConstruction construction=TypefaceConstruction::kMakeWithFontArguments)
Definition: fontations.cpp:88
DrawResult onDraw(SkCanvas *canvas, SkString *errorMsg) override
Definition: fontations.cpp:129
SkISize getISize() override
Definition: fontations.cpp:127
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
void setBGColor(SkColor)
Definition: gm.cpp:159
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
size_t length
std::u16string text
double y
double x
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
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
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63