Debian Codegraph @

gm/coloremoji.cpp

raw
1 /*
2 * Copyright 2013 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"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkColorFilter.h"
12 #include "include/core/SkFont.h"
13 #include "include/core/SkFontMetrics.h"
14 #include "include/core/SkFontTypes.h"
15 #include "include/core/SkImageFilter.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkPoint.h"
18 #include "include/core/SkRect.h"
19 #include "include/core/SkRefCnt.h"
20 #include "include/core/SkScalar.h"
21 #include "include/core/SkShader.h"
22 #include "include/core/SkSize.h"
23 #include "include/core/SkString.h"
24 #include "include/core/SkTileMode.h"
25 #include "include/core/SkTypeface.h"
26 #include "include/core/SkTypes.h"
27 #include "include/effects/SkGradient.h"
28 #include "include/effects/SkImageFilters.h"
29 #include "src/core/SkColorPriv.h"
30 #include "src/core/SkFontPriv.h"
31 #include "tools/ToolUtils.h"
32 #include "tools/fonts/FontToolUtils.h"
33
34 #if defined(SK_GANESH)
35 #include "include/gpu/ganesh/GrContextOptions.h"
36 #endif
37
38 #if defined(SK_GRAPHITE)
39 #include "include/gpu/graphite/ContextOptions.h"
40 #endif
41
42 #include <string.h>
43 #include <initializer_list>
44 #include <utility>
45
46 /*
47 * Spits out an arbitrary gradient to test blur with shader on paint
48 */
49 static sk_sp<SkShader> MakeLinear() {
50 constexpr SkPoint kPts[] = { { 0, 0 }, { 32, 32 } };
51 constexpr SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
52 constexpr SkColor kColors[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
53 SkColorConverter conv(kColors);
54 return SkShaders::LinearGradient(kPts,
55 {{conv.colors4f(), kPos, SkTileMode::kClamp}, {}});
56 }
57
58 static sk_sp<SkImageFilter> make_grayscale(sk_sp<SkImageFilter> input) {
59 float matrix[20];
60 memset(matrix, 0, 20 * sizeof(float));
61 matrix[0] = matrix[5] = matrix[10] = 0.2126f;
62 matrix[1] = matrix[6] = matrix[11] = 0.7152f;
63 matrix[2] = matrix[7] = matrix[12] = 0.0722f;
64 matrix[18] = 1.0f;
65 sk_sp<SkColorFilter> filter(SkColorFilters::Matrix(matrix));
66 return SkImageFilters::ColorFilter(std::move(filter), std::move(input));
67 }
68
69 static sk_sp<SkImageFilter> make_blur(float amount, sk_sp<SkImageFilter> input) {
70 return SkImageFilters::Blur(amount, amount, std::move(input));
71 }
72
73 static sk_sp<SkColorFilter> make_color_filter() {
74 return SkColorFilters::Lighting(SkColorSetRGB(0x00, 0x80, 0xFF),
75 SkColorSetRGB(0xFF, 0x20, 0x00));
76 }
77
78 namespace skiagm {
79
80 class ColorEmojiGM : public GM {
81 public:
82 ColorEmojiGM(ToolUtils::EmojiFontFormat format) : fFormat(format) {}
83
84 protected:
85 ToolUtils::EmojiTestSample emojiFont;
86 void onOnceBeforeDraw() override {
87 emojiFont = ToolUtils::EmojiSample(fFormat);
88 }
89
90 SkString getName() const override {
91 return SkString("coloremoji_") += ToolUtils::NameForFontFormat(fFormat);
92 }
93
94 SkISize getISize() override { return SkISize::Make(650, 1200); }
95
96 #if defined(SK_GANESH)
97 void modifyGrContextOptions(GrContextOptions* ctxOptions) override {
98 // This will force multitexturing to verify that color text works with this,
99 // as well as with any additional color transformations.
100 ctxOptions->fGlyphCacheTextureMaximumBytes = 256 * 256 * 4;
101 }
102 #endif
103
104 #if defined(SK_GRAPHITE)
105 void modifyGraphiteContextOptions(skgpu::graphite::ContextOptions* ctxOptions) const override {
106 // This will force multitexturing to verify that color text works with this,
107 // as well as with any additional color transformations.
108 ctxOptions->fGlyphCacheTextureMaximumBytes = 256 * 256 * 4;
109 }
110 #endif
111
112 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
113 canvas->drawColor(SK_ColorGRAY);
114
115 if (!emojiFont.typeface) {
116 *errorMsg = SkStringPrintf("Unable to instantiate emoji test font of format %s.",
117 ToolUtils::NameForFontFormat(fFormat).c_str());
118 return DrawResult::kSkip;
119 }
120
121 SkFont font(emojiFont.typeface);
122 char const * const text = emojiFont.sampleText;
123 size_t textLen = strlen(text);
124
125 // draw text at different point sizes
126 constexpr SkScalar textSizes[] = { 10, 30, 50 };
127 SkFontMetrics metrics;
128 SkScalar y = 0;
129 for (const bool& fakeBold : { false, true }) {
130 font.setEmbolden(fakeBold);
131 for (const SkScalar& textSize : textSizes) {
132 font.setSize(textSize);
133 font.getMetrics(&metrics);
134 y += -metrics.fAscent;
135 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8,
136 10, y, font, SkPaint());
137 y += metrics.fDescent + metrics.fLeading;
138 }
139 }
140
141 // draw one more big one to max out one Plot
142 font.setSize(256);
143 font.getMetrics(&metrics);
144 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8,
145 190, -metrics.fAscent, font, SkPaint());
146
147 y += 20;
148 SkScalar savedY = y;
149 // draw with shaders and image filters
150 for (int makeLinear = 0; makeLinear < 2; makeLinear++) {
151 for (int makeBlur = 0; makeBlur < 2; makeBlur++) {
152 for (int makeGray = 0; makeGray < 2; makeGray++) {
153 for (int makeMode = 0; makeMode < 2; ++makeMode) {
154 for (int alpha = 0; alpha < 2; ++alpha) {
155 SkFont shaderFont(font.refTypeface());
156 SkPaint shaderPaint;
157 if (SkToBool(makeLinear)) {
158 shaderPaint.setShader(MakeLinear());
159 }
160
161 if (SkToBool(makeBlur) && SkToBool(makeGray)) {
162 sk_sp<SkImageFilter> grayScale(make_grayscale(nullptr));
163 sk_sp<SkImageFilter> blur(make_blur(3.0f, std::move(grayScale)));
164 shaderPaint.setImageFilter(std::move(blur));
165 } else if (SkToBool(makeBlur)) {
166 shaderPaint.setImageFilter(make_blur(3.0f, nullptr));
167 } else if (SkToBool(makeGray)) {
168 shaderPaint.setImageFilter(make_grayscale(nullptr));
169 }
170 if (makeMode) {
171 shaderPaint.setColorFilter(make_color_filter());
172 }
173 if (alpha) {
174 shaderPaint.setAlphaf(0.5f);
175 }
176 shaderFont.setSize(30);
177 shaderFont.getMetrics(&metrics);
178 y += -metrics.fAscent;
179 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 380, y,
180 shaderFont, shaderPaint);
181 y += metrics.fDescent + metrics.fLeading;
182 }
183 }
184 }
185 }
186 }
187 // setup work needed to draw text with different clips
188 canvas->translate(10, savedY);
189 font.setSize(40);
190
191 // compute the bounds of the text
192 SkRect bounds;
193 font.measureText(text, textLen, SkTextEncoding::kUTF8, &bounds);
194
195 const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
196 const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
197 const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf;
198 const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf;
199
200 SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(),
201 boundsHalfWidth, boundsHalfHeight);
202 SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.centerY(),
203 boundsHalfWidth, boundsHalfHeight);
204 SkRect interiorClip = bounds;
205 interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight);
206
207 const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip };
208
209 SkPaint clipHairline;
210 clipHairline.setColor(SK_ColorWHITE);
211 clipHairline.setStyle(SkPaint::kStroke_Style);
212
213 SkPaint paint;
214 for (const SkRect& clipRect : clipRects) {
215 canvas->translate(0, bounds.height());
216 canvas->save();
217 canvas->drawRect(clipRect, clipHairline);
218 paint.setAlpha(0x20);
219 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint);
220 canvas->clipRect(clipRect);
221 paint.setAlphaf(1.0f);
222 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint);
223 canvas->restore();
224 canvas->translate(0, SkIntToScalar(25));
225 }
226
227 return DrawResult::kOk;
228 }
229
230 ToolUtils::EmojiFontFormat fFormat;
231 using INHERITED = GM;
232 };
233
234 //////////////////////////////////////////////////////////////////////////////
235
236 DEF_GM(return new ColorEmojiGM(ToolUtils::EmojiFontFormat::ColrV0);)
237 DEF_GM(return new ColorEmojiGM(ToolUtils::EmojiFontFormat::Cbdt);)
238 DEF_GM(return new ColorEmojiGM(ToolUtils::EmojiFontFormat::Sbix);)
239 DEF_GM(return new ColorEmojiGM(ToolUtils::EmojiFontFormat::Test);)
240 DEF_GM(return new ColorEmojiGM(ToolUtils::EmojiFontFormat::Svg);)
241
242 } // namespace skiagm