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; |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 48 | sk_tool_utils::set_portable_typeface(&paint); |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 49 | SkRect r = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100)); |
| 50 | |
| 51 | canvas->clipRect(r); |
| 52 | |
| 53 | paint.setColor(SK_ColorRED); |
| 54 | canvas->drawRect(r, paint); |
| 55 | |
| 56 | // Minimal repro doesn't require AA, LCD, or a nondefault typeface |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 57 | paint.setShader(make_chrome_solid()); |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 58 | paint.setTextSize(SkIntToScalar(500)); |
| 59 | |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 60 | canvas->drawString("I", 0, 100, paint); |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 61 | } |
| 62 | private: |
| 63 | typedef GM INHERITED; |
| 64 | }; |
| 65 | |
| 66 | |
caryclark | 5df0575 | 2015-07-15 11:57:00 -0700 | [diff] [blame] | 67 | // Replicate chrome layout test - switching between solid & gradient text |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 68 | class ChromeGradTextGM2 : public GM { |
| 69 | public: |
| 70 | ChromeGradTextGM2() { } |
| 71 | protected: |
| 72 | |
| 73 | virtual SkString onShortName() { return SkString("chrome_gradtext2"); } |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 74 | virtual SkISize onISize() { return SkISize::Make(500, 480); } |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 75 | virtual void onDraw(SkCanvas* canvas) { |
| 76 | SkPaint paint; |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 77 | sk_tool_utils::set_portable_typeface(&paint); |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 78 | |
| 79 | paint.setStyle(SkPaint::kFill_Style); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 80 | canvas->drawString("Normal Fill Text", 0, 50, paint); |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 81 | paint.setStyle(SkPaint::kStroke_Style); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 82 | canvas->drawString("Normal Stroke Text", 0, 100, paint); |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 83 | |
| 84 | // Minimal repro doesn't require AA, LCD, or a nondefault typeface |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 85 | paint.setShader(make_chrome_solid()); |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 86 | |
| 87 | paint.setStyle(SkPaint::kFill_Style); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 88 | canvas->drawString("Gradient Fill Text", 0, 150, paint); |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 89 | paint.setStyle(SkPaint::kStroke_Style); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 90 | canvas->drawString("Gradient Stroke Text", 0, 200, paint); |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 91 | } |
| 92 | private: |
| 93 | typedef GM INHERITED; |
| 94 | }; |
| 95 | |
| 96 | |
| 97 | |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 98 | class GradTextGM : public GM { |
| 99 | public: |
| 100 | GradTextGM () {} |
| 101 | |
| 102 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 103 | SkString onShortName() override { |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 104 | return SkString("gradtext"); |
| 105 | } |
| 106 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 107 | SkISize onISize() override { return SkISize::Make(500, 480); } |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 108 | |
| 109 | static void draw_text(SkCanvas* canvas, const SkPaint& paint) { |
| 110 | const char* text = "When in the course of human events"; |
| 111 | size_t len = strlen(text); |
| 112 | canvas->drawText(text, len, 0, 0, paint); |
| 113 | } |
| 114 | |
| 115 | static void draw_text3(SkCanvas* canvas, const SkPaint& paint) { |
| 116 | SkPaint p(paint); |
| 117 | |
| 118 | p.setAntiAlias(false); |
| 119 | draw_text(canvas, p); |
| 120 | p.setAntiAlias(true); |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 121 | canvas->translate(0, paint.getTextSize() * 4/3); |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 122 | draw_text(canvas, p); |
| 123 | p.setLCDRenderText(true); |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 124 | canvas->translate(0, paint.getTextSize() * 4/3); |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 125 | draw_text(canvas, p); |
| 126 | } |
| 127 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 128 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 129 | SkPaint paint; |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 130 | sk_tool_utils::set_portable_typeface(&paint); |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 131 | paint.setTextSize(SkIntToScalar(26)); |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 132 | |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 133 | const SkISize& size = this->getISize(); |
| 134 | SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()), |
| 135 | SkIntToScalar(size.height()) / 2); |
| 136 | canvas->drawRect(r, paint); |
| 137 | |
| 138 | canvas->translate(SkIntToScalar(20), paint.getTextSize()); |
| 139 | |
| 140 | for (int i = 0; i < 2; ++i) { |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 141 | paint.setShader(make_grad(SkIntToScalar(80))); |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 142 | draw_text3(canvas, paint); |
| 143 | |
| 144 | canvas->translate(0, paint.getTextSize() * 2); |
| 145 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 146 | paint.setShader(make_grad2(SkIntToScalar(80))); |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 147 | draw_text3(canvas, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 148 | |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 149 | canvas->translate(0, paint.getTextSize() * 2); |
| 150 | } |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | private: |
| 154 | typedef GM INHERITED; |
| 155 | }; |
| 156 | |
| 157 | ////////////////////////////////////////////////////////////////////////////// |
| 158 | |
| 159 | static GM* MyFactory(void*) { return new GradTextGM; } |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 160 | static GM* CMyFactory(void*) { return new ChromeGradTextGM1; } |
| 161 | static GM* CMyFactory2(void*) { return new ChromeGradTextGM2; } |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 162 | |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 163 | static GMRegistry reg(MyFactory); |
| 164 | static GMRegistry Creg(CMyFactory); |
| 165 | static GMRegistry Creg2(CMyFactory2); |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 166 | } |