Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
coloremoji.cpp
Go to the documentation of this file.
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"
12#include "include/core/SkFont.h"
18#include "include/core/SkRect.h"
22#include "include/core/SkSize.h"
30#include "src/core/SkFontPriv.h"
31#include "tools/ToolUtils.h"
33
34#if defined(SK_GRAPHITE)
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 */
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),
51}
52
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;
61 return SkImageFilters::ColorFilter(std::move(filter), std::move(input));
62}
63
65 return SkImageFilters::Blur(amount, amount, std::move(input));
66}
67
69 return SkColorFilters::Lighting(SkColorSetRGB(0x00, 0x80, 0xFF),
70 SkColorSetRGB(0xFF, 0x20, 0x00));
71}
72
73namespace skiagm {
74
75class ColorEmojiGM : public GM {
76public:
78
79protected:
84
85 SkString getName() const override {
86 return SkString("coloremoji_") += ToolUtils::NameForFontFormat(fFormat);
87 }
88
89 SkISize getISize() override { return SkISize::Make(650, 1200); }
90
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)
98 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
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.",
111 return DrawResult::kSkip;
112 }
113
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
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
224 using INHERITED = GM;
225};
226
227//////////////////////////////////////////////////////////////////////////////
228
234
235} // namespace skiagm
static sk_sp< SkColorFilter > make_grayscale()
uint32_t SkColor
Definition SkColor.h:37
#define SkColorSetRGB(r, g, b)
Definition SkColor.h:57
constexpr SkColor SK_ColorGRAY
Definition SkColor.h:113
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
@ kUTF8
uses bytes to represent UTF-8 or ASCII
#define SK_Scalar1
Definition SkScalar.h:18
#define SK_ScalarHalf
Definition SkScalar.h:19
#define SkIntToScalar(x)
Definition SkScalar.h:57
SK_API SkString static SkString SkStringPrintf()
Definition SkString.h:287
static constexpr bool SkToBool(const T &x)
Definition SkTo.h:35
static sk_sp< SkColorFilter > make_color_filter()
void drawRect(const SkRect &rect, const SkPaint &paint)
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
void restore()
Definition SkCanvas.cpp:465
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
void translate(SkScalar dx, SkScalar dy)
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition SkCanvas.h:1182
int save()
Definition SkCanvas.cpp:451
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
static sk_sp< SkColorFilter > Lighting(SkColor mul, SkColor add)
void setSize(SkScalar textSize)
Definition SkFont.cpp:129
SkScalar getMetrics(SkFontMetrics *metrics) const
Definition SkFont.cpp:316
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static sk_sp< SkImageFilter > ColorFilter(sk_sp< SkColorFilter > cf, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Blur(SkScalar sigmaX, SkScalar sigmaY, SkTileMode tileMode, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
void setStyle(Style style)
Definition SkPaint.cpp:105
void setColor(SkColor color)
Definition SkPaint.cpp:119
void setImageFilter(sk_sp< SkImageFilter > imageFilter)
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
void setShader(sk_sp< SkShader > shader)
void setColorFilter(sk_sp< SkColorFilter > colorFilter)
void setAlphaf(float a)
Definition SkPaint.cpp:130
DrawResult onDraw(SkCanvas *canvas, SkString *errorMsg) override
ToolUtils::EmojiTestSample emojiFont
ToolUtils::EmojiFontFormat fFormat
void modifyGrContextOptions(GrContextOptions *ctxOptions) override
SkString getName() const override
void onOnceBeforeDraw() override
SkISize getISize() override
ColorEmojiGM(ToolUtils::EmojiFontFormat format)
virtual void modifyGraphiteContextOptions(skgpu::graphite::ContextOptions *) const
Definition gm.h:179
const Paint & paint
static sk_sp< SkShader > MakeLinear()
static sk_sp< SkImageFilter > make_blur(float amount, sk_sp< SkImageFilter > input)
static sk_sp< SkColorFilter > make_color_filter()
float SkScalar
Definition extension.cpp:12
uint32_t uint32_t * format
#define DEF_GM(CODE)
Definition gm.h:40
std::u16string text
double y
SkString NameForFontFormat(EmojiFontFormat format)
EmojiTestSample EmojiSample()
DrawResult
Definition gm.h:104
size_t fGlyphCacheTextureMaximumBytes
SkScalar fLeading
distance to add between lines, typically positive or zero
SkScalar fAscent
distance to reserve above baseline, typically negative
SkScalar fDescent
distance to reserve below baseline, typically positive
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
void inset(float dx, float dy)
Definition SkRect.h:1060
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659
sk_sp< SkTypeface > typeface