2 * Copyright 2021 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
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/SkPaint.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkSize.h"
17 #include "include/core/SkString.h"
18 #include "include/core/SkTypeface.h"
19 #include "tools/Resources.h"
20 #include "tools/ToolUtils.h"
23 #include <initializer_list>
27 class ColrV1GM : public GM {
30 // TODO(drott): Consolidate test fonts.
33 kColorFontsRepoGradients,
34 kColorFontsRepoScaling,
35 kColorFontsRepoExtendMode,
36 kColorFontsRepoRotate,
38 kColorFontsRepoTransform,
39 kColorFontsRepoClipBox,
40 kColorFontsRepoComposite,
41 kColorFontsRepoForeground,
42 kColorFontsRepoSweepPad,
43 kColorFontsRepoSweepReflect,
44 kColorFontsRepoSweepRepeat,
47 ColrV1GM(ColrV1TestType testType, SkScalar skewX, SkScalar rotateDeg)
48 : fSkewX(skewX), fRotateDeg(rotateDeg), fTestType(testType) {}
51 static SkString testTypeToString(ColrV1TestType testType) {
54 return SkString("skia");
55 case kColorFontsRepoGradients:
56 return SkString("gradients");
57 case kColorFontsRepoScaling:
58 return SkString("scaling");
59 case kColorFontsRepoExtendMode:
60 return SkString("extend_mode");
61 case kColorFontsRepoRotate:
62 return SkString("rotate");
63 case kColorFontsRepoSkew:
64 return SkString("skew");
65 case kColorFontsRepoTransform:
66 return SkString("transform");
67 case kColorFontsRepoClipBox:
68 return SkString("clipbox");
69 case kColorFontsRepoComposite:
70 return SkString("composite");
71 case kColorFontsRepoForeground:
72 return SkString("foreground");
73 case kColorFontsRepoSweepPad:
74 return SkString("sweep_pad");
75 case kColorFontsRepoSweepReflect:
76 return SkString("sweep_reflect");
77 case kColorFontsRepoSweepRepeat:
78 return SkString("sweep_repeat");
80 SkASSERT(false); /* not reached */
85 sk_sp<SkTypeface> fTypeface;
86 std::vector<uint16_t> fGlyphs;
87 size_t bytesize() { return fGlyphs.size() * sizeof(uint16_t); }
90 void onOnceBeforeDraw() override {
91 if (fTestType == kSkiaSampleFont) {
92 fEmojiFont.fTypeface = MakeResourceAsTypeface("fonts/colrv1_samples.ttf");
93 fEmojiFont.fGlyphs = {19, 33, 34, 35, 20, 21, 22, 23, 24, 25};
97 fEmojiFont.fTypeface = MakeResourceAsTypeface("fonts/more_samples-glyf_colr_1.ttf");
100 case kSkiaSampleFont:
103 case kColorFontsRepoGradients:
104 fEmojiFont.fGlyphs = {2, 5, 6, 7, 8, 55};
106 case kColorFontsRepoScaling:
107 fEmojiFont.fGlyphs = {9, 10, 11, 12, 13, 14};
109 case kColorFontsRepoExtendMode:
110 fEmojiFont.fGlyphs = {15, 16, 17, 18, 19, 20};
112 case kColorFontsRepoRotate:
113 fEmojiFont.fGlyphs = {21, 22, 23, 24};
115 case kColorFontsRepoSkew:
116 fEmojiFont.fGlyphs = {25, 26, 27, 28, 29, 30};
118 case kColorFontsRepoTransform:
119 fEmojiFont.fGlyphs = {31, 32, 33, 34};
121 case kColorFontsRepoClipBox:
122 fEmojiFont.fGlyphs = {35, 36, 37, 38, 39};
124 case kColorFontsRepoComposite:
125 fEmojiFont.fGlyphs = {40, 41, 42, 43, 44, 45, 46};
127 case kColorFontsRepoForeground:
128 fEmojiFont.fGlyphs = {47, 48, 49, 50, 51, 52, 53, 54};
130 case kColorFontsRepoSweepPad:
131 fEmojiFont.fGlyphs = {2, 58, 59, 60, 61, 62, 63, 64};
133 case kColorFontsRepoSweepReflect:
134 fEmojiFont.fGlyphs = {65, 66, 67, 68, 69, 70, 71, 72};
136 case kColorFontsRepoSweepRepeat:
137 fEmojiFont.fGlyphs = {73, 74, 75, 76, 77, 78, 79, 80};
142 SkString onShortName() override {
143 SkString gm_name = SkStringPrintf("colrv1_%s_samples",
144 testTypeToString(fTestType).c_str());
146 gm_name.append("_skew");
150 gm_name.append("_rotate");
155 SkISize onISize() override { return SkISize::Make(1400, 600); }
157 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
158 canvas->drawColor(SK_ColorWHITE);
161 canvas->translate(200, 20);
163 if (!fEmojiFont.fTypeface) {
164 *errorMsg = "Did not recognize COLR v1 font format.";
165 return DrawResult::kSkip;
168 canvas->rotate(fRotateDeg);
169 canvas->skew(fSkewX, 0);
171 SkFont font(fEmojiFont.fTypeface);
173 SkFontMetrics metrics;
175 std::vector<SkColor> paint_colors = {
176 SK_ColorBLACK, SK_ColorGREEN, SK_ColorRED, SK_ColorBLUE};
177 auto paint_color_iterator = paint_colors.begin();
178 for (SkScalar textSize : { 12, 18, 30, 120 }) {
179 font.setSize(textSize);
180 font.getMetrics(&metrics);
181 y += -metrics.fAscent;
182 paint.setColor(*paint_color_iterator);
183 canvas->drawSimpleText(fEmojiFont.fGlyphs.data(),
184 fEmojiFont.bytesize(),
185 SkTextEncoding::kGlyphID,
187 y += metrics.fDescent + metrics.fLeading;
188 paint_color_iterator++;
190 return DrawResult::kOk;
194 using INHERITED = GM;
197 ColrV1TestType fTestType;
200 DEF_GM(return new ColrV1GM(ColrV1GM::kSkiaSampleFont, 0.f, 0.f);)
201 DEF_GM(return new ColrV1GM(ColrV1GM::kSkiaSampleFont, -0.5f, 0.f);)
202 DEF_GM(return new ColrV1GM(ColrV1GM::kSkiaSampleFont, 0.f, 20.f);)
203 DEF_GM(return new ColrV1GM(ColrV1GM::kSkiaSampleFont, -0.5f, 20.f);)
204 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoGradients, 0.f, 0.f);)
205 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoScaling, 0.f, 0.f);)
206 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoExtendMode, 0.f, 0.f);)
207 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoRotate, 0.f, 0.f);)
208 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoSkew, 0.f, 0.f);)
209 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoTransform, 0.f, 0.f);)
210 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoClipBox, 0.f, 0.f);)
211 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoClipBox, -0.5f, 20.f);)
212 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoComposite, 0.f, 0.f);)
213 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoForeground, 0.f, 0.f);)
214 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoSweepPad, 0.f, 0.f);)
215 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoSweepReflect, 0.f, 0.f);)
216 DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoSweepRepeat, 0.f, 0.f);)
218 } // namespace skiagm