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