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 return SkString("coloremoji");
71 }
72
onISize()73 SkISize onISize() override { return SkISize::Make(650, 1200); }
74
onDraw(SkCanvas * canvas)75 void onDraw(SkCanvas* canvas) override {
76
77 canvas->drawColor(SK_ColorGRAY);
78
79 SkFont font(emojiFont.typeface);
80 const char* text = emojiFont.text;
81
82 // draw text at different point sizes
83 constexpr SkScalar textSizes[] = { 10, 30, 50, };
84 SkFontMetrics metrics;
85 SkScalar y = 0;
86 for (const bool& fakeBold : { false, true }) {
87 font.setEmbolden(fakeBold);
88 for (const SkScalar& textSize : textSizes) {
89 font.setSize(textSize);
90 font.getMetrics(&metrics);
91 y += -metrics.fAscent;
92 canvas->drawSimpleText(text, strlen(text), kUTF8_SkTextEncoding, 10, y, font, SkPaint());
93 y += metrics.fDescent + metrics.fLeading;
94 }
95 }
96
97 y += 20;
98 SkScalar savedY = y;
99 // draw with shaders and image filters
100 for (int makeLinear = 0; makeLinear < 2; makeLinear++) {
101 for (int makeBlur = 0; makeBlur < 2; makeBlur++) {
102 for (int makeGray = 0; makeGray < 2; makeGray++) {
103 for (int makeMode = 0; makeMode < 2; ++makeMode) {
104 for (int alpha = 0; alpha < 2; ++alpha) {
105 SkFont shaderFont(font.refTypefaceOrDefault());
106 SkPaint shaderPaint;
107 if (SkToBool(makeLinear)) {
108 shaderPaint.setShader(MakeLinear());
109 }
110
111 if (SkToBool(makeBlur) && SkToBool(makeGray)) {
112 sk_sp<SkImageFilter> grayScale(make_grayscale(nullptr));
113 sk_sp<SkImageFilter> blur(make_blur(3.0f, std::move(grayScale)));
114 shaderPaint.setImageFilter(std::move(blur));
115 } else if (SkToBool(makeBlur)) {
116 shaderPaint.setImageFilter(make_blur(3.0f, nullptr));
117 } else if (SkToBool(makeGray)) {
118 shaderPaint.setImageFilter(make_grayscale(nullptr));
119 }
120 if (makeMode) {
121 shaderPaint.setColorFilter(make_color_filter());
122 }
123 if (alpha) {
124 shaderPaint.setAlphaf(0.5f);
125 }
126 shaderFont.setSize(30);
127 shaderFont.getMetrics(&metrics);
128 y += -metrics.fAscent;
129 canvas->drawSimpleText(text, strlen(text), kUTF8_SkTextEncoding, 380, y,
130 shaderFont, shaderPaint);
131 y += metrics.fDescent + metrics.fLeading;
132 }
133 }
134 }
135 }
136 }
137 // setup work needed to draw text with different clips
138 canvas->translate(10, savedY);
139 font.setSize(40);
140
141 // compute the bounds of the text
142 SkRect bounds;
143 font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds);
144
145 const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf;
146 const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf;
147 const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf;
148 const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf;
149
150 SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(),
151 boundsHalfWidth, boundsHalfHeight);
152 SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.centerY(),
153 boundsHalfWidth, boundsHalfHeight);
154 SkRect interiorClip = bounds;
155 interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight);
156
157 const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip };
158
159 SkPaint clipHairline;
160 clipHairline.setColor(SK_ColorWHITE);
161 clipHairline.setStyle(SkPaint::kStroke_Style);
162
163 SkPaint paint;
164 for (const SkRect& clipRect : clipRects) {
165 canvas->translate(0, bounds.height());
166 canvas->save();
167 canvas->drawRect(clipRect, clipHairline);
168 paint.setAlpha(0x20);
169 canvas->drawSimpleText(text, strlen(text), kUTF8_SkTextEncoding, 0, 0, font, paint);
170 canvas->clipRect(clipRect);
171 paint.setAlphaf(1.0f);
172 canvas->drawSimpleText(text, strlen(text), kUTF8_SkTextEncoding, 0, 0, font, paint);
173 canvas->restore();
174 canvas->translate(0, SkIntToScalar(25));
175 }
176 }
177
178 typedef GM INHERITED;
179 };
180
181 //////////////////////////////////////////////////////////////////////////////
182
183 DEF_GM(return new ColorEmojiGM;)
184
185 }
186