blob: 59db9a0fc24ade09e9f08c794e7763522963226a [file] [log] [blame]
djsollen@google.com6def2a22013-09-17 15:30:21 +00001/*
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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
tfarinabcbc1782014-06-18 14:32:48 -070010
11#include "Resources.h"
joshualitt290c09b2014-12-19 13:45:20 -080012#include "SkBlurImageFilter.h"
Brian Salomon6f1d36c2017-01-13 12:02:17 -050013#include "SkCanvas.h"
joshualitt290c09b2014-12-19 13:45:20 -080014#include "SkColorFilterImageFilter.h"
15#include "SkColorMatrixFilter.h"
joshualitt290c09b2014-12-19 13:45:20 -080016#include "SkGradientShader.h"
djsollen@google.com6def2a22013-09-17 15:30:21 +000017#include "SkStream.h"
18#include "SkTypeface.h"
19
joshualitt290c09b2014-12-19 13:45:20 -080020/*
21 * Spits out a dummy gradient to test blur with shader on paint
22 */
reed2ad1aa62016-03-09 09:50:50 -080023static sk_sp<SkShader> MakeLinear() {
mtkleindbfd7ab2016-09-01 11:24:54 -070024 constexpr SkPoint kPts[] = { { 0, 0 }, { 32, 32 } };
25 constexpr SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
26 constexpr SkColor kColors[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
reed2ad1aa62016-03-09 09:50:50 -080027 return SkGradientShader::MakeLinear(kPts, kColors, kPos, SK_ARRAY_COUNT(kColors),
28 SkShader::kClamp_TileMode);
joshualitt290c09b2014-12-19 13:45:20 -080029}
30
robertphillips6e7025a2016-04-04 04:31:25 -070031static sk_sp<SkImageFilter> make_grayscale(sk_sp<SkImageFilter> input) {
joshualitt290c09b2014-12-19 13:45:20 -080032 SkScalar matrix[20];
33 memset(matrix, 0, 20 * sizeof(SkScalar));
34 matrix[0] = matrix[5] = matrix[10] = 0.2126f;
35 matrix[1] = matrix[6] = matrix[11] = 0.7152f;
36 matrix[2] = matrix[7] = matrix[12] = 0.0722f;
37 matrix[18] = 1.0f;
robertphillips6e7025a2016-04-04 04:31:25 -070038 sk_sp<SkColorFilter> filter(SkColorFilter::MakeMatrixFilterRowMajor255(matrix));
robertphillips5605b562016-04-05 11:50:42 -070039 return SkColorFilterImageFilter::Make(std::move(filter), std::move(input));
joshualitt290c09b2014-12-19 13:45:20 -080040}
41
robertphillips6e7025a2016-04-04 04:31:25 -070042static sk_sp<SkImageFilter> make_blur(float amount, sk_sp<SkImageFilter> input) {
43 return SkBlurImageFilter::Make(amount, amount, std::move(input));
joshualitt290c09b2014-12-19 13:45:20 -080044}
45
Brian Salomon6f1d36c2017-01-13 12:02:17 -050046static sk_sp<SkColorFilter> make_color_filter() {
47 return SkColorMatrixFilter::MakeLightingFilter(SkColorSetRGB(0x00, 0x80, 0xFF),
48 SkColorSetRGB(0xFF, 0x20, 0x00));
49}
50
djsollen@google.com6def2a22013-09-17 15:30:21 +000051namespace skiagm {
52
53class ColorEmojiGM : public GM {
54public:
caryclarkc3dcb672015-07-21 12:27:36 -070055 ColorEmojiGM() { }
djsollen@google.com6def2a22013-09-17 15:30:21 +000056
djsollen@google.com6def2a22013-09-17 15:30:21 +000057protected:
bungeman6bdc9cd2015-01-26 14:08:52 -080058 struct EmojiFont {
bungeman13b9c952016-05-12 10:09:30 -070059 sk_sp<SkTypeface> typeface;
bungeman6bdc9cd2015-01-26 14:08:52 -080060 const char* text;
caryclarkc3dcb672015-07-21 12:27:36 -070061 } emojiFont;
mtklein36352bf2015-03-25 18:17:31 -070062 virtual void onOnceBeforeDraw() override {
bungeman13b9c952016-05-12 10:09:30 -070063 emojiFont.typeface = sk_tool_utils::emoji_typeface();
caryclarkc3dcb672015-07-21 12:27:36 -070064 emojiFont.text = sk_tool_utils::emoji_sample_text();
djsollen@google.com6def2a22013-09-17 15:30:21 +000065 }
66
mtklein36352bf2015-03-25 18:17:31 -070067 SkString onShortName() override {
caryclarkc3dcb672015-07-21 12:27:36 -070068 SkString name("coloremoji");
69 name.append(sk_tool_utils::platform_os_emoji());
70 return name;
djsollen@google.com6def2a22013-09-17 15:30:21 +000071 }
72
Brian Salomon6f1d36c2017-01-13 12:02:17 -050073 SkISize onISize() override { return SkISize::Make(650, 1200); }
djsollen@google.com6def2a22013-09-17 15:30:21 +000074
mtklein36352bf2015-03-25 18:17:31 -070075 void onDraw(SkCanvas* canvas) override {
djsollen@google.com6def2a22013-09-17 15:30:21 +000076
caryclarkc3dcb672015-07-21 12:27:36 -070077 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
djsollen@google.com6def2a22013-09-17 15:30:21 +000078
caryclarkc3dcb672015-07-21 12:27:36 -070079 SkPaint paint;
80 paint.setTypeface(emojiFont.typeface);
81 const char* text = emojiFont.text;
djsollen@google.com6def2a22013-09-17 15:30:21 +000082
caryclarkc3dcb672015-07-21 12:27:36 -070083 // draw text at different point sizes
mtkleindbfd7ab2016-09-01 11:24:54 -070084 constexpr SkScalar textSizes[] = { 10, 30, 50, };
bungeman53790512016-07-21 13:32:09 -070085 SkPaint::FontMetrics metrics;
86 SkScalar y = 0;
Ben Wagner1e26ba92017-11-07 15:47:01 -050087 for (const bool& fakeBold : { false, true }) {
88 paint.setFakeBoldText(fakeBold);
89 for (const SkScalar& textSize : textSizes) {
90 paint.setTextSize(textSize);
91 paint.getFontMetrics(&metrics);
92 y += -metrics.fAscent;
93 canvas->drawString(text, 10, y, paint);
94 y += metrics.fDescent + metrics.fLeading;
95 }
caryclarkc3dcb672015-07-21 12:27:36 -070096 }
djsollen@google.com6def2a22013-09-17 15:30:21 +000097
bungeman53790512016-07-21 13:32:09 -070098 y += 20;
99 SkScalar savedY = y;
caryclarkc3dcb672015-07-21 12:27:36 -0700100 // draw with shaders and image filters
101 for (int makeLinear = 0; makeLinear < 2; makeLinear++) {
102 for (int makeBlur = 0; makeBlur < 2; makeBlur++) {
103 for (int makeGray = 0; makeGray < 2; makeGray++) {
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500104 for (int makeMode = 0; makeMode < 2; ++makeMode) {
105 for (int alpha = 0; alpha < 2; ++alpha) {
106 SkPaint shaderPaint;
107 shaderPaint.setTypeface(sk_ref_sp(paint.getTypeface()));
108 if (SkToBool(makeLinear)) {
109 shaderPaint.setShader(MakeLinear());
110 }
caryclarkc3dcb672015-07-21 12:27:36 -0700111
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500112 if (SkToBool(makeBlur) && SkToBool(makeGray)) {
113 sk_sp<SkImageFilter> grayScale(make_grayscale(nullptr));
114 sk_sp<SkImageFilter> blur(make_blur(3.0f, std::move(grayScale)));
115 shaderPaint.setImageFilter(std::move(blur));
116 } else if (SkToBool(makeBlur)) {
117 shaderPaint.setImageFilter(make_blur(3.0f, nullptr));
118 } else if (SkToBool(makeGray)) {
119 shaderPaint.setImageFilter(make_grayscale(nullptr));
120 }
121 if (makeMode) {
122 shaderPaint.setColorFilter(make_color_filter());
123 }
124 if (alpha) {
125 shaderPaint.setAlpha(0x80);
126 }
127 shaderPaint.setTextSize(30);
128 shaderPaint.getFontMetrics(&metrics);
129 y += -metrics.fAscent;
Cary Clark2a475ea2017-04-28 15:35:12 -0400130 canvas->drawString(text, 380, y, shaderPaint);
Brian Salomon6f1d36c2017-01-13 12:02:17 -0500131 y += metrics.fDescent + metrics.fLeading;
132 }
caryclarkc3dcb672015-07-21 12:27:36 -0700133 }
joshualitt290c09b2014-12-19 13:45:20 -0800134 }
135 }
caryclarkc3dcb672015-07-21 12:27:36 -0700136 }
caryclarkc3dcb672015-07-21 12:27:36 -0700137 // setup work needed to draw text with different clips
bungeman53790512016-07-21 13:32:09 -0700138 canvas->translate(10, savedY);
caryclarkc3dcb672015-07-21 12:27:36 -0700139 paint.setTextSize(40);
djsollen@google.com6def2a22013-09-17 15:30:21 +0000140
caryclarkc3dcb672015-07-21 12:27:36 -0700141 // compute the bounds of the text
142 SkRect bounds;
143 paint.measureText(text, strlen(text), &bounds);
djsollen@google.com6def2a22013-09-17 15:30:21 +0000144
caryclarkc3dcb672015-07-21 12:27:36 -0700145 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;
djsollen@google.com6def2a22013-09-17 15:30:21 +0000149
caryclarkc3dcb672015-07-21 12:27:36 -0700150 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);
djsollen@google.com6def2a22013-09-17 15:30:21 +0000156
mtkleindbfd7ab2016-09-01 11:24:54 -0700157 const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip };
djsollen@google.com6def2a22013-09-17 15:30:21 +0000158
caryclarkc3dcb672015-07-21 12:27:36 -0700159 SkPaint clipHairline;
160 clipHairline.setColor(SK_ColorWHITE);
161 clipHairline.setStyle(SkPaint::kStroke_Style);
djsollen@google.com6def2a22013-09-17 15:30:21 +0000162
bungeman53790512016-07-21 13:32:09 -0700163 for (const SkRect& clipRect : clipRects) {
164 canvas->translate(0, bounds.height());
caryclarkc3dcb672015-07-21 12:27:36 -0700165 canvas->save();
bungeman53790512016-07-21 13:32:09 -0700166 canvas->drawRect(clipRect, clipHairline);
caryclarkc3dcb672015-07-21 12:27:36 -0700167 paint.setAlpha(0x20);
Cary Clark2a475ea2017-04-28 15:35:12 -0400168 canvas->drawString(text, 0, 0, paint);
bungeman53790512016-07-21 13:32:09 -0700169 canvas->clipRect(clipRect);
caryclarkc3dcb672015-07-21 12:27:36 -0700170 paint.setAlpha(0xFF);
Cary Clark2a475ea2017-04-28 15:35:12 -0400171 canvas->drawString(text, 0, 0, paint);
caryclarkc3dcb672015-07-21 12:27:36 -0700172 canvas->restore();
bungeman53790512016-07-21 13:32:09 -0700173 canvas->translate(0, SkIntToScalar(25));
djsollen@google.com6def2a22013-09-17 15:30:21 +0000174 }
175 }
176
djsollen@google.com6def2a22013-09-17 15:30:21 +0000177 typedef GM INHERITED;
178};
179
180//////////////////////////////////////////////////////////////////////////////
181
robertphillips6e7025a2016-04-04 04:31:25 -0700182DEF_GM(return new ColorEmojiGM;)
djsollen@google.com6def2a22013-09-17 15:30:21 +0000183
184}