Flutter Engine
The Flutter Engine
palette.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2022 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"
22#include "tools/Resources.h"
23#include "tools/ToolUtils.h"
25
26#if defined(SK_TYPEFACE_FACTORY_FONTATIONS)
28#endif
29
30#include <string.h>
31
32namespace skiagm {
33
34// Copied from https://github.com/googlefonts/color-fonts#colrv1-test-font glyph descriptions
35// markdown file.
36namespace ColrV1TestDefinitions {
37const uint32_t color_circles_palette[] = {0xf0e00, 0xf0e01};
38};
39
40namespace {
41const char kColrCpalTestFontPath[] = "fonts/test_glyphs-glyf_colr_1.ttf";
42
43constexpr SkFontArguments::Palette::Override kColorOverridesAll[] = {
44 // A gradient of dark to light purple for the circle palette test glyph.
45 // Randomly ordered with `shuf`.
46 // Add a repeat (later overrides override earlier overrides).
47 // Add three out of bounds entries (font has 12 palette entries).
48 // clang-format off
49 { 6, 0xffffff00},
50 { 2, 0xff76078f},
51 { 4, 0xffb404c4},
52 { 1, 0xff510970},
53 { 6, 0xfffa00ff},
54 { 8, 0xff888888},
55 {10, 0xff888888},
56 { 9, 0xff888888},
57 { 7, 0xff888888},
58 {11, 0xff888888},
59 { 0, 0xff310b55},
60 { 3, 0xff9606aa},
61 { 5, 0xffd802e2},
62 {13, 0xff00ffff},
63 {12, 0xff00ffff},
64 {static_cast<uint16_t>(-1), 0xff00ff00},
65 // clang-format on
66};
67
68constexpr SkFontArguments::Palette::Override kColorOverridesOne[] = {
69 {2, 0xff02dfe2},
70};
71
72constexpr SkFontArguments::Palette kLightPaletteOverride{2, nullptr, 0};
73constexpr SkFontArguments::Palette kDarkPaletteOverride{1, nullptr, 0};
74constexpr SkFontArguments::Palette kOnePaletteOverride{
75 0, kColorOverridesOne, std::size(kColorOverridesOne)};
76constexpr SkFontArguments::Palette kAllPaletteOverride{
77 0, kColorOverridesAll, std::size(kColorOverridesAll)};
78
79sk_sp<SkTypeface> MakeTypefaceFromResource(const char* resource, const SkFontArguments& args) {
81}
82
83} // namespace
84
85class FontPaletteGM : public GM {
86public:
87 FontPaletteGM(const char* test_name, const SkFontArguments::Palette& paletteOverride)
88 : fName(test_name), fPalette(paletteOverride) {}
89
90protected:
94
95 void onOnceBeforeDraw() override {
96 SkFontArguments paletteArguments;
97 paletteArguments.setPalette(fPalette);
98
99 fTypefaceDefault = MakeTypefaceFromResource(kColrCpalTestFontPath, SkFontArguments());
101 fTypefaceDefault ? fTypefaceDefault->makeClone(paletteArguments) : nullptr;
102
103 fTypefaceFromStream = MakeTypefaceFromResource(kColrCpalTestFontPath, paletteArguments);
104 }
105
106 SkString getName() const override {
107 SkString gm_name = SkStringPrintf("font_palette_%s", fName.c_str());
108 return gm_name;
109 }
110
111 SkISize getISize() override { return SkISize::Make(1000, 400); }
112
113 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
114 canvas->drawColor(SK_ColorWHITE);
116
117 canvas->translate(10, 20);
118
120 *errorMsg = "Did not recognize COLR v1 test font format.";
121 return DrawResult::kSkip;
122 }
123
124 SkFontMetrics metrics;
125 SkScalar y = 0;
126 SkScalar textSize = 200;
127 for (auto& typeface : {fTypefaceFromStream, fTypefaceCloned}) {
128 SkFont defaultFont(fTypefaceDefault);
129 SkFont paletteFont(typeface);
130 defaultFont.setSize(textSize);
131 paletteFont.setSize(textSize);
132
133 defaultFont.getMetrics(&metrics);
134 y += -metrics.fAscent;
135 // Set a recognizable foreground color which is not to be overriden.
136 paint.setColor(SK_ColorGRAY);
137 // Draw the default palette on the left, for COLRv0 and COLRv1.
138 canvas->drawSimpleText(
142 0,
143 y,
144 defaultFont,
145 paint);
146 // Draw the overriden palette on the right.
147 canvas->drawSimpleText(
151 440,
152 y,
153 paletteFont,
154 paint);
155 y += metrics.fDescent + metrics.fLeading;
156 }
157 return DrawResult::kOk;
158 }
159
160private:
161 using INHERITED = GM;
162 SkString fName;
164};
165
167DEF_GM(return new FontPaletteGM("light", kLightPaletteOverride));
168DEF_GM(return new FontPaletteGM("dark", kDarkPaletteOverride));
169DEF_GM(return new FontPaletteGM("one", kOnePaletteOverride));
170DEF_GM(return new FontPaletteGM("all", kAllPaletteOverride));
171
172} // namespace skiagm
std::unique_ptr< SkStreamAsset > GetResourceAsStream(const char *resource, bool useFileStream)
Definition: Resources.cpp:31
constexpr SkColor SK_ColorGRAY
Definition: SkColor.h:113
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
@ kUTF32
uses four byte words to represent all of Unicode
static SkString resource(SkPDFResourceType type, int index)
SK_API SkString SkStringPrintf(const char *format,...) SK_PRINTF_LIKE(1
Creates a new string and writes into it using a printf()-style format.
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
sk_sp< SkTypeface > makeFromStream(std::unique_ptr< SkStreamAsset >, int ttcIndex=0) const
Definition: SkFontMgr.cpp:127
Definition: SkFont.h:35
void setSize(SkScalar textSize)
Definition: SkFont.cpp:129
SkScalar getMetrics(SkFontMetrics *metrics) const
Definition: SkFont.cpp:316
const char * c_str() const
Definition: SkString.h:133
sk_sp< SkTypeface > makeClone(const SkFontArguments &) const
Definition: SkTypeface.cpp:190
FontPaletteGM(const char *test_name, const SkFontArguments::Palette &paletteOverride)
Definition: palette.cpp:87
void onOnceBeforeDraw() override
Definition: palette.cpp:95
SkString getName() const override
Definition: palette.cpp:106
SkISize getISize() override
Definition: palette.cpp:111
sk_sp< SkTypeface > fTypefaceDefault
Definition: palette.cpp:91
DrawResult onDraw(SkCanvas *canvas, SkString *errorMsg) override
Definition: palette.cpp:113
sk_sp< SkTypeface > fTypefaceCloned
Definition: palette.cpp:93
sk_sp< SkTypeface > fTypefaceFromStream
Definition: palette.cpp:92
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
double y
sk_sp< SkFontMgr > TestFontMgr()
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
const uint32_t color_circles_palette[]
Definition: palette.cpp:37
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
DrawResult
Definition: gm.h:104
SkFontArguments & setPalette(Palette palette)
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