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