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"); } |
| 47 | virtual SkISize onISize() { return make_isize(500, 480); } |
| 48 | virtual void onDraw(SkCanvas* canvas) { |
| 49 | SkPaint paint; |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 50 | SkRect r = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100)); |
| 51 | |
| 52 | canvas->clipRect(r); |
| 53 | |
| 54 | paint.setColor(SK_ColorRED); |
| 55 | canvas->drawRect(r, paint); |
| 56 | |
| 57 | // Minimal repro doesn't require AA, LCD, or a nondefault typeface |
| 58 | paint.setShader(make_chrome_solid())->unref(); |
| 59 | paint.setTextSize(SkIntToScalar(500)); |
| 60 | |
| 61 | canvas->drawText("I", 1, 0, 100, paint); |
| 62 | } |
| 63 | private: |
| 64 | typedef GM INHERITED; |
| 65 | }; |
| 66 | |
| 67 | |
| 68 | // Replicate chrome layout test - switching between solid & gadient text |
| 69 | class ChromeGradTextGM2 : public GM { |
| 70 | public: |
| 71 | ChromeGradTextGM2() { } |
| 72 | protected: |
| 73 | |
| 74 | virtual SkString onShortName() { return SkString("chrome_gradtext2"); } |
| 75 | virtual SkISize onISize() { return make_isize(500, 480); } |
| 76 | virtual void onDraw(SkCanvas* canvas) { |
| 77 | SkPaint paint; |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 78 | |
| 79 | paint.setStyle(SkPaint::kFill_Style); |
| 80 | canvas->drawText("Normal Fill Text", 16, 0, 50, paint); |
| 81 | paint.setStyle(SkPaint::kStroke_Style); |
| 82 | canvas->drawText("Normal Stroke Text", 18, 0, 100, paint); |
| 83 | |
| 84 | // Minimal repro doesn't require AA, LCD, or a nondefault typeface |
| 85 | paint.setShader(make_chrome_solid())->unref(); |
| 86 | |
| 87 | paint.setStyle(SkPaint::kFill_Style); |
| 88 | canvas->drawText("Gradient Fill Text", 18, 0, 150, paint); |
| 89 | paint.setStyle(SkPaint::kStroke_Style); |
| 90 | canvas->drawText("Gradient Stroke Text", 20, 0, 200, paint); |
| 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: |
| 103 | |
| 104 | virtual SkString onShortName() { |
| 105 | return SkString("gradtext"); |
| 106 | } |
| 107 | |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 108 | virtual SkISize onISize() { return make_isize(500, 480); } |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 109 | |
| 110 | static void draw_text(SkCanvas* canvas, const SkPaint& paint) { |
| 111 | const char* text = "When in the course of human events"; |
| 112 | size_t len = strlen(text); |
| 113 | canvas->drawText(text, len, 0, 0, paint); |
| 114 | } |
| 115 | |
| 116 | static void draw_text3(SkCanvas* canvas, const SkPaint& paint) { |
| 117 | SkPaint p(paint); |
| 118 | |
| 119 | p.setAntiAlias(false); |
| 120 | draw_text(canvas, p); |
| 121 | p.setAntiAlias(true); |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 122 | canvas->translate(0, paint.getTextSize() * 4/3); |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 123 | draw_text(canvas, p); |
| 124 | p.setLCDRenderText(true); |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 125 | canvas->translate(0, paint.getTextSize() * 4/3); |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 126 | draw_text(canvas, p); |
| 127 | } |
| 128 | |
| 129 | virtual void onDraw(SkCanvas* canvas) { |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 130 | SkPaint 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) { |
| 141 | paint.setShader(make_grad(SkIntToScalar(80)))->unref(); |
| 142 | draw_text3(canvas, paint); |
| 143 | |
| 144 | canvas->translate(0, paint.getTextSize() * 2); |
| 145 | |
| 146 | paint.setShader(make_grad2(SkIntToScalar(80)))->unref(); |
| 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 | } |