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