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