reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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" |
| 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/SkFont.h" |
| 12 | #include "include/core/SkPaint.h" |
| 13 | #include "include/core/SkPoint.h" |
| 14 | #include "include/core/SkRect.h" |
| 15 | #include "include/core/SkRefCnt.h" |
| 16 | #include "include/core/SkScalar.h" |
| 17 | #include "include/core/SkShader.h" |
| 18 | #include "include/core/SkSize.h" |
| 19 | #include "include/core/SkString.h" |
| 20 | #include "include/core/SkTileMode.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "include/core/SkTypeface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 22 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "include/effects/SkGradientShader.h" |
| 24 | #include "tools/ToolUtils.h" |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 25 | |
Hal Canary | bd865e2 | 2019-07-18 11:51:19 -0400 | [diff] [blame] | 26 | namespace { |
| 27 | |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 28 | // test shader w/ transparency |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 29 | static sk_sp<SkShader> make_grad(SkScalar width) { |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 30 | SkColor colors[] = { SK_ColorRED, 0x0000FF00, SK_ColorBLUE }; |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 31 | SkPoint pts[] = { { 0, 0 }, { width, 0 } }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 32 | return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 33 | SkTileMode::kMirror); |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 34 | } |
| 35 | |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 36 | // test opaque shader |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 37 | static sk_sp<SkShader> make_grad2(SkScalar width) { |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 38 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
| 39 | SkPoint pts[] = { { 0, 0 }, { width, 0 } }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 40 | return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 41 | SkTileMode::kMirror); |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 42 | } |
| 43 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 44 | static sk_sp<SkShader> make_chrome_solid() { |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 45 | SkColor colors[] = { SK_ColorGREEN, SK_ColorGREEN }; |
| 46 | SkPoint pts[] = { { 0, 0 }, { 1, 0 } }; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 47 | return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp); |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 48 | } |
| 49 | |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 50 | // Replicate chrome layout test - clipped pathed gradient-shaded text |
Hal Canary | bd865e2 | 2019-07-18 11:51:19 -0400 | [diff] [blame] | 51 | class ChromeGradTextGM1 : public skiagm::GM { |
| 52 | SkString onShortName() override { return SkString("chrome_gradtext1"); } |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 53 | |
Hal Canary | bd865e2 | 2019-07-18 11:51:19 -0400 | [diff] [blame] | 54 | SkISize onISize() override { return {500, 480}; } |
| 55 | |
| 56 | void onDraw(SkCanvas* canvas) override { |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 57 | SkPaint paint; |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 58 | SkRect r = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100)); |
| 59 | |
| 60 | canvas->clipRect(r); |
| 61 | |
| 62 | paint.setColor(SK_ColorRED); |
| 63 | canvas->drawRect(r, paint); |
| 64 | |
| 65 | // Minimal repro doesn't require AA, LCD, or a nondefault typeface |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 66 | paint.setShader(make_chrome_solid()); |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 67 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 68 | SkFont font(ToolUtils::create_portable_typeface(), 500); |
Mike Reed | 1af9b48 | 2019-01-07 11:01:57 -0500 | [diff] [blame] | 69 | font.setEdging(SkFont::Edging::kAlias); |
| 70 | |
| 71 | canvas->drawString("I", 0, 100, font, paint); |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 72 | } |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
caryclark | 5df0575 | 2015-07-15 11:57:00 -0700 | [diff] [blame] | 75 | // Replicate chrome layout test - switching between solid & gradient text |
Hal Canary | bd865e2 | 2019-07-18 11:51:19 -0400 | [diff] [blame] | 76 | class ChromeGradTextGM2 : public skiagm::GM { |
| 77 | SkString onShortName() override { return SkString("chrome_gradtext2"); } |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 78 | |
Hal Canary | bd865e2 | 2019-07-18 11:51:19 -0400 | [diff] [blame] | 79 | SkISize onISize() override { return {500, 480}; } |
| 80 | |
| 81 | void onDraw(SkCanvas* canvas) override { |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 82 | SkPaint paint; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 83 | SkFont font(ToolUtils::create_portable_typeface()); |
Mike Reed | 1af9b48 | 2019-01-07 11:01:57 -0500 | [diff] [blame] | 84 | font.setEdging(SkFont::Edging::kAlias); |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 85 | |
| 86 | paint.setStyle(SkPaint::kFill_Style); |
Mike Reed | 1af9b48 | 2019-01-07 11:01:57 -0500 | [diff] [blame] | 87 | canvas->drawString("Normal Fill Text", 0, 50, font, paint); |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 88 | paint.setStyle(SkPaint::kStroke_Style); |
Mike Reed | 1af9b48 | 2019-01-07 11:01:57 -0500 | [diff] [blame] | 89 | canvas->drawString("Normal Stroke Text", 0, 100, font, paint); |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 90 | |
| 91 | // Minimal repro doesn't require AA, LCD, or a nondefault typeface |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 92 | paint.setShader(make_chrome_solid()); |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 93 | |
| 94 | paint.setStyle(SkPaint::kFill_Style); |
Mike Reed | 1af9b48 | 2019-01-07 11:01:57 -0500 | [diff] [blame] | 95 | canvas->drawString("Gradient Fill Text", 0, 150, font, paint); |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 96 | paint.setStyle(SkPaint::kStroke_Style); |
Mike Reed | 1af9b48 | 2019-01-07 11:01:57 -0500 | [diff] [blame] | 97 | canvas->drawString("Gradient Stroke Text", 0, 200, font, paint); |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 98 | } |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 99 | }; |
Hal Canary | bd865e2 | 2019-07-18 11:51:19 -0400 | [diff] [blame] | 100 | } // namespace |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 101 | |
Hal Canary | e964c18 | 2019-01-23 10:22:01 -0500 | [diff] [blame] | 102 | DEF_GM( return new ChromeGradTextGM1; ) |
| 103 | DEF_GM( return new ChromeGradTextGM2; ) |
Hal Canary | 6ac0df8 | 2019-01-07 16:01:22 -0500 | [diff] [blame] | 104 | |
| 105 | DEF_SIMPLE_GM(gradtext, canvas, 500, 480) { |
| 106 | static constexpr float kTextSize = 26.0f; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 107 | SkFont font(ToolUtils::create_portable_typeface(), kTextSize); |
Hal Canary | 6ac0df8 | 2019-01-07 16:01:22 -0500 | [diff] [blame] | 108 | |
| 109 | canvas->drawRect({0, 0, 500, 240}, SkPaint()); |
| 110 | canvas->translate(20.0f, kTextSize); |
| 111 | |
| 112 | SkPaint paints[2]; |
| 113 | paints[0].setShader(make_grad(80.0f)); |
| 114 | paints[1].setShader(make_grad2(80.0f)); |
| 115 | |
| 116 | static const SkFont::Edging edgings[3] = { |
| 117 | SkFont::Edging::kAlias, |
| 118 | SkFont::Edging::kAntiAlias, |
| 119 | SkFont::Edging::kSubpixelAntiAlias, |
| 120 | }; |
| 121 | for (int i = 0; i < 2; ++i) { |
| 122 | for (const SkPaint& paint : paints) { |
| 123 | for (SkFont::Edging edging : edgings) { |
| 124 | font.setEdging(edging); |
| 125 | canvas->drawString("When in the course of human events", 0, 0, font, paint); |
| 126 | canvas->translate(0, kTextSize * 4/3); |
| 127 | } |
| 128 | canvas->translate(0, kTextSize * 2/3); |
| 129 | } |
| 130 | } |
| 131 | } |