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.h"
9 #include "sk_tool_utils.h"
10
11 #include "Resources.h"
12 #include "SkBlurImageFilter.h"
13 #include "SkCanvas.h"
14 #include "SkColorFilterImageFilter.h"
15 #include "SkColorMatrixFilter.h"
16 #include "SkFont.h"
17 #include "SkFontMetrics.h"
18 #include "SkGradientShader.h"
19 #include "SkStream.h"
20 #include "SkTypeface.h"
21
22 /*
23 * Spits out a dummy gradient to test blur with shader on paint
24 */
MakeLinear()25 static sk_sp<SkShader> MakeLinear() {
26 constexpr SkPoint kPts[] = { { 0, 0 }, { 32, 32 } };
27 constexpr SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
28 constexpr SkColor kColors[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
29 return SkGradientShader::MakeLinear(kPts, kColors, kPos, SK_ARRAY_COUNT(kColors),
30 SkShader::kClamp_TileMode);
31 }
32
make_grayscale(sk_sp<SkImageFilter> input)33 static sk_sp<SkImageFilter> make_grayscale(sk_sp<SkImageFilter> input) {
34 SkScalar matrix[20];
35 memset(matrix, 0, 20 * sizeof(SkScalar));
36 matrix[0] = matrix[5] = matrix[10] = 0.2126f;
37 matrix[1] = matrix[6] = matrix[11] = 0.7152f;
38 matrix[2] = matrix[7] = matrix[12] = 0.0722f;
39 matrix[18] = 1.0f;
40 sk_sp<SkColorFilter> filter(SkColorFilter::MakeMatrixFilterRowMajor255(matrix));
41 return SkColorFilterImageFilter::Make(std::move(filter), std::move(input));
42 }
43
make_blur(float amount,sk_sp<SkImageFilter> input)44 static sk_sp<SkImageFilter> make_blur(float amount, sk_sp<SkImageFilter> input) {
45 return SkBlurImageFilter::Make(amount, amount, std::move(input));
46 }
47
make_color_filter()48 static sk_sp<SkColorFilter> make_color_filter() {
49 return SkColorMatrixFilter::MakeLightingFilter(SkColorSetRGB(0x00, 0x80, 0xFF),
50 SkColorSetRGB(0xFF, 0x20, 0x00));
51 }
52
53 namespace skiagm {
54
55 class ColorEmojiGM : public GM {
56 public:
ColorEmojiGM()57 ColorEmojiGM() { }
58
59 protected:
60 struct EmojiFont {
61 sk_sp<SkTypeface> typeface;
62 const char* text;
63 } emojiFont;
onOnceBeforeDraw()64 virtual void onOnceBeforeDraw() override {
65 emojiFont.typeface = sk_tool_utils::emoji_typeface();
66 emojiFont.text = sk_tool_utils::emoji_sample_text();
67 }
68
onShortName()69 SkString onShortName() override {
70 SkString name("coloremoji");
71 name.append(sk_tool_utils::platform_font_manager());
72 return name;
73 }
74
onISize()75 SkISize onISize() override { return SkISize::Make(650, 1200); }
76
onDraw(SkCanvas * canvas)77 void onDraw(SkCanvas* canvas) override {
78
79 canvas->drawColor(SK_ColorGRAY);
80
81 SkFont font(emojiFont.typeface);
82 const char* text = emojiFont.text;
83
84 // draw text at different point sizes
85 constexpr SkScalar textSizes[] = { 10, 30, 50, };
86 SkFontMetrics metrics;
87 SkScalar y = 0;
88 for (const bool& fakeBold : { false, true }) {
89 font.setEmbolden(fakeBold);
90 for (const SkScalar& textSize : textSizes) {
91 font.setSize(textSize);
92 font.getMetrics(&metrics);
93 y += -metrics.fAscent;
94 canvas->drawSimpleText(text, strlen(text), kUTF8_SkTextEncoding, 10, y, font, SkPaint());
95 y += metrics.fDescent + metrics.fLeading;
96 }
97 }
98
99 y += 20;
100 SkScalar savedY = y;
101 // draw with shaders and image filters
102 for (int makeLinear = 0; makeLinear < 2; makeLinear++) {
103 for (int makeBlur = 0; makeBlur < 2; makeBlur++) {
104 for (int makeGray = 0; makeGray < 2; makeGray++) {
105 for (int makeMode = 0; makeMode < 2; ++makeMode) {
106 for (int alpha = 0; alpha < 2; ++alpha) {
107 SkFont shaderFont(font.refTypefaceOrDefault());
108 SkPaint shaderPaint;
109 if (SkToBool(makeLinear)) {
110 shaderPaint.setShader(MakeLinear());
111 }
112
113 if (SkToBool(makeBlur) && SkToBool(makeGray)) {
114 sk_sp<SkImageFilter> grayScale(make_grayscale(nullptr));
115 sk_sp<SkImageFilter> blur(make_blur(3.0f, std::move(grayScale)));
116 shaderPaint.setImageFilter(std::move(blur));
117 } else if (SkToBool(makeBlur)) {
118 shaderPaint.setImageFilter(make_blur(3.0f, nullptr));
119 } else if (SkToBool(makeGray)) {
120 shaderPaint.setImageFilter(make_grayscale(nullptr));
121 }
122 if (makeMode) {
123 shaderPaint.setColorFilter(make_color_filter());
124 }
125 if (alpha) {
126 shaderPaint.setAlpha(0x80);
127 }
128 shaderFont.setSize(30);
129 shaderFont.getMetrics(&metrics);
130 y += -metrics.fAscent;
131 canvas->drawSimpleText(text, strlen(text), kUTF8_SkTextEncoding, 380, y,
132 shaderFont, shaderPaint);
133 y += metrics.fDescent + metrics.fLeading;
134 }
135 }
136 }
137 }
138 }
139 // setup work needed to draw text with different clips
140 canvas->translate(10, savedY);
141 font.setSize(40);
142
143 // compute the bounds of the text
144 SkRect bounds;
145 font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds);
146
147 const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
148 const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
149 const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf;
150 const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf;
151
152 SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(),
153 boundsHalfWidth, boundsHalfHeight);
154 SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.centerY(),
155 boundsHalfWidth, boundsHalfHeight);
156 SkRect interiorClip = bounds;
157 interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight);
158
159 const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip };
160
161 SkPaint clipHairline;
162 clipHairline.setColor(SK_ColorWHITE);
163 clipHairline.setStyle(SkPaint::kStroke_Style);
164
165 SkPaint paint;
166 for (const SkRect& clipRect : clipRects) {
167 canvas->translate(0, bounds.height());
168 canvas->save();
169 canvas->drawRect(clipRect, clipHairline);
170 paint.setAlpha(0x20);
171 canvas->drawSimpleText(text, strlen(text), kUTF8_SkTextEncoding, 0, 0, font, paint);
172 canvas->clipRect(clipRect);
173 paint.setAlpha(0xFF);
174 canvas->drawSimpleText(text, strlen(text), kUTF8_SkTextEncoding, 0, 0, font, paint);
175 canvas->restore();
176 canvas->translate(0, SkIntToScalar(25));
177 }
178 }
179
180 typedef GM INHERITED;
181 };
182
183 //////////////////////////////////////////////////////////////////////////////
184
185 DEF_GM(return new ColorEmojiGM;)
186
187 }
188