djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 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" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 10 | |
| 11 | #include "Resources.h" |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 12 | #include "SkBlurImageFilter.h" |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 13 | #include "SkCanvas.h" |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 14 | #include "SkColorFilterImageFilter.h" |
| 15 | #include "SkColorMatrixFilter.h" |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 16 | #include "SkFont.h" |
Mike Reed | 9cd2a5c | 2019-01-22 15:17:59 -0500 | [diff] [blame] | 17 | #include "SkFontMetrics.h" |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 18 | #include "SkGradientShader.h" |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 19 | #include "SkStream.h" |
| 20 | #include "SkTypeface.h" |
| 21 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 22 | /* |
| 23 | * Spits out a dummy gradient to test blur with shader on paint |
| 24 | */ |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 25 | static sk_sp<SkShader> MakeLinear() { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 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 }; |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 29 | return SkGradientShader::MakeLinear(kPts, kColors, kPos, SK_ARRAY_COUNT(kColors), |
| 30 | SkShader::kClamp_TileMode); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 31 | } |
| 32 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 33 | static sk_sp<SkImageFilter> make_grayscale(sk_sp<SkImageFilter> input) { |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 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; |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 40 | sk_sp<SkColorFilter> filter(SkColorFilter::MakeMatrixFilterRowMajor255(matrix)); |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 41 | return SkColorFilterImageFilter::Make(std::move(filter), std::move(input)); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 42 | } |
| 43 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 44 | static sk_sp<SkImageFilter> make_blur(float amount, sk_sp<SkImageFilter> input) { |
| 45 | return SkBlurImageFilter::Make(amount, amount, std::move(input)); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 48 | static sk_sp<SkColorFilter> make_color_filter() { |
| 49 | return SkColorMatrixFilter::MakeLightingFilter(SkColorSetRGB(0x00, 0x80, 0xFF), |
| 50 | SkColorSetRGB(0xFF, 0x20, 0x00)); |
| 51 | } |
| 52 | |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 53 | namespace skiagm { |
| 54 | |
| 55 | class ColorEmojiGM : public GM { |
| 56 | public: |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 57 | ColorEmojiGM() { } |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 58 | |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 59 | protected: |
bungeman | 6bdc9cd | 2015-01-26 14:08:52 -0800 | [diff] [blame] | 60 | struct EmojiFont { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 61 | sk_sp<SkTypeface> typeface; |
bungeman | 6bdc9cd | 2015-01-26 14:08:52 -0800 | [diff] [blame] | 62 | const char* text; |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 63 | } emojiFont; |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 64 | virtual void onOnceBeforeDraw() override { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 65 | emojiFont.typeface = sk_tool_utils::emoji_typeface(); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 66 | emojiFont.text = sk_tool_utils::emoji_sample_text(); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 67 | } |
| 68 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 69 | SkString onShortName() override { |
Mike Klein | bea1f94 | 2019-03-08 11:11:55 -0600 | [diff] [blame^] | 70 | return SkString("coloremoji"); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 73 | SkISize onISize() override { return SkISize::Make(650, 1200); } |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 74 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 75 | void onDraw(SkCanvas* canvas) override { |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 76 | |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 77 | canvas->drawColor(SK_ColorGRAY); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 78 | |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 79 | SkFont font(emojiFont.typeface); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 80 | const char* text = emojiFont.text; |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 81 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 82 | // draw text at different point sizes |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 83 | constexpr SkScalar textSizes[] = { 10, 30, 50, }; |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 84 | SkFontMetrics metrics; |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 85 | SkScalar y = 0; |
Ben Wagner | 1e26ba9 | 2017-11-07 15:47:01 -0500 | [diff] [blame] | 86 | for (const bool& fakeBold : { false, true }) { |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 87 | font.setEmbolden(fakeBold); |
Ben Wagner | 1e26ba9 | 2017-11-07 15:47:01 -0500 | [diff] [blame] | 88 | for (const SkScalar& textSize : textSizes) { |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 89 | font.setSize(textSize); |
| 90 | font.getMetrics(&metrics); |
Ben Wagner | 1e26ba9 | 2017-11-07 15:47:01 -0500 | [diff] [blame] | 91 | y += -metrics.fAscent; |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 92 | canvas->drawSimpleText(text, strlen(text), kUTF8_SkTextEncoding, 10, y, font, SkPaint()); |
Ben Wagner | 1e26ba9 | 2017-11-07 15:47:01 -0500 | [diff] [blame] | 93 | y += metrics.fDescent + metrics.fLeading; |
| 94 | } |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 95 | } |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 96 | |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 97 | y += 20; |
| 98 | SkScalar savedY = y; |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 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++) { |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 103 | for (int makeMode = 0; makeMode < 2; ++makeMode) { |
| 104 | for (int alpha = 0; alpha < 2; ++alpha) { |
Herb Derby | 087fad7 | 2019-01-22 14:45:16 -0500 | [diff] [blame] | 105 | SkFont shaderFont(font.refTypefaceOrDefault()); |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 106 | SkPaint shaderPaint; |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 107 | if (SkToBool(makeLinear)) { |
| 108 | shaderPaint.setShader(MakeLinear()); |
| 109 | } |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 110 | |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 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) { |
Mike Reed | 9407e24 | 2019-02-15 16:13:57 -0500 | [diff] [blame] | 124 | shaderPaint.setAlphaf(0.5f); |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 125 | } |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 126 | shaderFont.setSize(30); |
| 127 | shaderFont.getMetrics(&metrics); |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 128 | y += -metrics.fAscent; |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 129 | canvas->drawSimpleText(text, strlen(text), kUTF8_SkTextEncoding, 380, y, |
| 130 | shaderFont, shaderPaint); |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 131 | y += metrics.fDescent + metrics.fLeading; |
| 132 | } |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 133 | } |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 134 | } |
| 135 | } |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 136 | } |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 137 | // setup work needed to draw text with different clips |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 138 | canvas->translate(10, savedY); |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 139 | font.setSize(40); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 140 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 141 | // compute the bounds of the text |
| 142 | SkRect bounds; |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 143 | font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 144 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 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; |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 149 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 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); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 156 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 157 | const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip }; |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 158 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 159 | SkPaint clipHairline; |
| 160 | clipHairline.setColor(SK_ColorWHITE); |
| 161 | clipHairline.setStyle(SkPaint::kStroke_Style); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 162 | |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 163 | SkPaint paint; |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 164 | for (const SkRect& clipRect : clipRects) { |
| 165 | canvas->translate(0, bounds.height()); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 166 | canvas->save(); |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 167 | canvas->drawRect(clipRect, clipHairline); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 168 | paint.setAlpha(0x20); |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 169 | canvas->drawSimpleText(text, strlen(text), kUTF8_SkTextEncoding, 0, 0, font, paint); |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 170 | canvas->clipRect(clipRect); |
Mike Reed | 9407e24 | 2019-02-15 16:13:57 -0500 | [diff] [blame] | 171 | paint.setAlphaf(1.0f); |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 172 | canvas->drawSimpleText(text, strlen(text), kUTF8_SkTextEncoding, 0, 0, font, paint); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 173 | canvas->restore(); |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 174 | canvas->translate(0, SkIntToScalar(25)); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 178 | typedef GM INHERITED; |
| 179 | }; |
| 180 | |
| 181 | ////////////////////////////////////////////////////////////////////////////// |
| 182 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 183 | DEF_GM(return new ColorEmojiGM;) |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 184 | |
| 185 | } |