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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkColorFilter.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkFont.h" |
| 13 | #include "include/core/SkFontMetrics.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "include/core/SkTypeface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 26 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "include/effects/SkColorMatrixFilter.h" |
| 28 | #include "include/effects/SkGradientShader.h" |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 29 | #include "include/effects/SkImageFilters.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 30 | #include "tools/ToolUtils.h" |
| 31 | |
| 32 | #include <string.h> |
| 33 | #include <initializer_list> |
| 34 | #include <utility> |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 35 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 36 | /* |
| 37 | * Spits out a dummy gradient to test blur with shader on paint |
| 38 | */ |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 39 | static sk_sp<SkShader> MakeLinear() { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 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 }; |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 43 | return SkGradientShader::MakeLinear(kPts, kColors, kPos, SK_ARRAY_COUNT(kColors), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 44 | SkTileMode::kClamp); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 45 | } |
| 46 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 47 | static sk_sp<SkImageFilter> make_grayscale(sk_sp<SkImageFilter> input) { |
Mike Reed | e869a1e | 2019-04-30 12:18:54 -0400 | [diff] [blame] | 48 | float matrix[20]; |
| 49 | memset(matrix, 0, 20 * sizeof(float)); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 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; |
Mike Reed | e869a1e | 2019-04-30 12:18:54 -0400 | [diff] [blame] | 54 | sk_sp<SkColorFilter> filter(SkColorFilters::Matrix(matrix)); |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 55 | return SkImageFilters::ColorFilter(std::move(filter), std::move(input)); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 56 | } |
| 57 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 58 | static sk_sp<SkImageFilter> make_blur(float amount, sk_sp<SkImageFilter> input) { |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 59 | return SkImageFilters::Blur(amount, amount, std::move(input)); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 62 | static sk_sp<SkColorFilter> make_color_filter() { |
| 63 | return SkColorMatrixFilter::MakeLightingFilter(SkColorSetRGB(0x00, 0x80, 0xFF), |
| 64 | SkColorSetRGB(0xFF, 0x20, 0x00)); |
| 65 | } |
| 66 | |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 67 | namespace skiagm { |
| 68 | |
| 69 | class ColorEmojiGM : public GM { |
| 70 | public: |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 71 | ColorEmojiGM() { } |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 72 | |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 73 | protected: |
bungeman | 6bdc9cd | 2015-01-26 14:08:52 -0800 | [diff] [blame] | 74 | struct EmojiFont { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 75 | sk_sp<SkTypeface> typeface; |
bungeman | 6bdc9cd | 2015-01-26 14:08:52 -0800 | [diff] [blame] | 76 | const char* text; |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 77 | } emojiFont; |
Robert Phillips | 83b749a | 2020-06-25 11:41:19 -0400 | [diff] [blame] | 78 | void onOnceBeforeDraw() override { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 79 | emojiFont.typeface = ToolUtils::emoji_typeface(); |
| 80 | emojiFont.text = ToolUtils::emoji_sample_text(); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 81 | } |
| 82 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 83 | SkString onShortName() override { |
Mike Klein | bea1f94 | 2019-03-08 11:11:55 -0600 | [diff] [blame] | 84 | return SkString("coloremoji"); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 87 | SkISize onISize() override { return SkISize::Make(650, 1200); } |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 88 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 89 | void onDraw(SkCanvas* canvas) override { |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 90 | |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 91 | canvas->drawColor(SK_ColorGRAY); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 92 | |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 93 | SkFont font(emojiFont.typeface); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 94 | char const * const text = emojiFont.text; |
| 95 | size_t textLen = strlen(text); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 96 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 97 | // draw text at different point sizes |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 98 | constexpr SkScalar textSizes[] = { 10, 30, 50, }; |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 99 | SkFontMetrics metrics; |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 100 | SkScalar y = 0; |
Ben Wagner | 1e26ba9 | 2017-11-07 15:47:01 -0500 | [diff] [blame] | 101 | for (const bool& fakeBold : { false, true }) { |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 102 | font.setEmbolden(fakeBold); |
Ben Wagner | 1e26ba9 | 2017-11-07 15:47:01 -0500 | [diff] [blame] | 103 | for (const SkScalar& textSize : textSizes) { |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 104 | font.setSize(textSize); |
| 105 | font.getMetrics(&metrics); |
Ben Wagner | 1e26ba9 | 2017-11-07 15:47:01 -0500 | [diff] [blame] | 106 | y += -metrics.fAscent; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 107 | canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, |
| 108 | 10, y, font, SkPaint()); |
Ben Wagner | 1e26ba9 | 2017-11-07 15:47:01 -0500 | [diff] [blame] | 109 | y += metrics.fDescent + metrics.fLeading; |
| 110 | } |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 111 | } |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 112 | |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 113 | y += 20; |
| 114 | SkScalar savedY = y; |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 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++) { |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 119 | for (int makeMode = 0; makeMode < 2; ++makeMode) { |
| 120 | for (int alpha = 0; alpha < 2; ++alpha) { |
Herb Derby | 087fad7 | 2019-01-22 14:45:16 -0500 | [diff] [blame] | 121 | SkFont shaderFont(font.refTypefaceOrDefault()); |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 122 | SkPaint shaderPaint; |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 123 | if (SkToBool(makeLinear)) { |
| 124 | shaderPaint.setShader(MakeLinear()); |
| 125 | } |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 126 | |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 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) { |
Mike Reed | 9407e24 | 2019-02-15 16:13:57 -0500 | [diff] [blame] | 140 | shaderPaint.setAlphaf(0.5f); |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 141 | } |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 142 | shaderFont.setSize(30); |
| 143 | shaderFont.getMetrics(&metrics); |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 144 | y += -metrics.fAscent; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 145 | canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 380, y, |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 146 | shaderFont, shaderPaint); |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 147 | y += metrics.fDescent + metrics.fLeading; |
| 148 | } |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 149 | } |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 150 | } |
| 151 | } |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 152 | } |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 153 | // setup work needed to draw text with different clips |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 154 | canvas->translate(10, savedY); |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 155 | font.setSize(40); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 156 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 157 | // compute the bounds of the text |
| 158 | SkRect bounds; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 159 | font.measureText(text, textLen, SkTextEncoding::kUTF8, &bounds); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 160 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 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; |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 165 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 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); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 172 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 173 | const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip }; |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 174 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 175 | SkPaint clipHairline; |
| 176 | clipHairline.setColor(SK_ColorWHITE); |
| 177 | clipHairline.setStyle(SkPaint::kStroke_Style); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 178 | |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 179 | SkPaint paint; |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 180 | for (const SkRect& clipRect : clipRects) { |
| 181 | canvas->translate(0, bounds.height()); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 182 | canvas->save(); |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 183 | canvas->drawRect(clipRect, clipHairline); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 184 | paint.setAlpha(0x20); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 185 | canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint); |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 186 | canvas->clipRect(clipRect); |
Mike Reed | 9407e24 | 2019-02-15 16:13:57 -0500 | [diff] [blame] | 187 | paint.setAlphaf(1.0f); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 188 | canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 189 | canvas->restore(); |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 190 | canvas->translate(0, SkIntToScalar(25)); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 194 | typedef GM INHERITED; |
| 195 | }; |
| 196 | |
| 197 | ////////////////////////////////////////////////////////////////////////////// |
| 198 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 199 | DEF_GM(return new ColorEmojiGM;) |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 200 | |
| 201 | } |