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