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 { |
| 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; |
| 50 | const SkISize& size = this->getISize(); |
| 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"); } |
| 76 | virtual SkISize onISize() { return make_isize(500, 480); } |
| 77 | virtual void onDraw(SkCanvas* canvas) { |
| 78 | SkPaint paint; |
| 79 | const SkISize& size = this->getISize(); |
| 80 | SkRect r = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100)); |
| 81 | |
| 82 | |
| 83 | paint.setStyle(SkPaint::kFill_Style); |
| 84 | canvas->drawText("Normal Fill Text", 16, 0, 50, paint); |
| 85 | paint.setStyle(SkPaint::kStroke_Style); |
| 86 | canvas->drawText("Normal Stroke Text", 18, 0, 100, paint); |
| 87 | |
| 88 | // Minimal repro doesn't require AA, LCD, or a nondefault typeface |
| 89 | paint.setShader(make_chrome_solid())->unref(); |
| 90 | |
| 91 | paint.setStyle(SkPaint::kFill_Style); |
| 92 | canvas->drawText("Gradient Fill Text", 18, 0, 150, paint); |
| 93 | paint.setStyle(SkPaint::kStroke_Style); |
| 94 | canvas->drawText("Gradient Stroke Text", 20, 0, 200, paint); |
| 95 | } |
| 96 | private: |
| 97 | typedef GM INHERITED; |
| 98 | }; |
| 99 | |
| 100 | |
| 101 | |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 102 | class GradTextGM : public GM { |
| 103 | public: |
| 104 | GradTextGM () {} |
| 105 | |
| 106 | protected: |
| 107 | |
| 108 | virtual SkString onShortName() { |
| 109 | return SkString("gradtext"); |
| 110 | } |
| 111 | |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 112 | virtual SkISize onISize() { return make_isize(500, 480); } |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 113 | |
| 114 | static void draw_text(SkCanvas* canvas, const SkPaint& paint) { |
| 115 | const char* text = "When in the course of human events"; |
| 116 | size_t len = strlen(text); |
| 117 | canvas->drawText(text, len, 0, 0, paint); |
| 118 | } |
| 119 | |
| 120 | static void draw_text3(SkCanvas* canvas, const SkPaint& paint) { |
| 121 | SkPaint p(paint); |
| 122 | |
| 123 | p.setAntiAlias(false); |
| 124 | draw_text(canvas, p); |
| 125 | p.setAntiAlias(true); |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 126 | canvas->translate(0, paint.getTextSize() * 4/3); |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 127 | draw_text(canvas, p); |
| 128 | p.setLCDRenderText(true); |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 129 | canvas->translate(0, paint.getTextSize() * 4/3); |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 130 | draw_text(canvas, p); |
| 131 | } |
| 132 | |
| 133 | virtual void onDraw(SkCanvas* canvas) { |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 134 | SkPaint paint; |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 135 | paint.setTextSize(SkIntToScalar(26)); |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 136 | |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 137 | const SkISize& size = this->getISize(); |
| 138 | SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()), |
| 139 | SkIntToScalar(size.height()) / 2); |
| 140 | canvas->drawRect(r, paint); |
| 141 | |
| 142 | canvas->translate(SkIntToScalar(20), paint.getTextSize()); |
| 143 | |
| 144 | for (int i = 0; i < 2; ++i) { |
| 145 | paint.setShader(make_grad(SkIntToScalar(80)))->unref(); |
| 146 | draw_text3(canvas, paint); |
| 147 | |
| 148 | canvas->translate(0, paint.getTextSize() * 2); |
| 149 | |
| 150 | paint.setShader(make_grad2(SkIntToScalar(80)))->unref(); |
| 151 | draw_text3(canvas, paint); |
| 152 | |
| 153 | canvas->translate(0, paint.getTextSize() * 2); |
| 154 | } |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | private: |
| 158 | typedef GM INHERITED; |
| 159 | }; |
| 160 | |
| 161 | ////////////////////////////////////////////////////////////////////////////// |
| 162 | |
| 163 | static GM* MyFactory(void*) { return new GradTextGM; } |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 164 | static GM* CMyFactory(void*) { return new ChromeGradTextGM1; } |
| 165 | static GM* CMyFactory2(void*) { return new ChromeGradTextGM2; } |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 166 | |
tomhudson@google.com | cb325ce | 2012-07-11 14:41:19 +0000 | [diff] [blame] | 167 | static GMRegistry reg(MyFactory); |
| 168 | static GMRegistry Creg(CMyFactory); |
| 169 | static GMRegistry Creg2(CMyFactory2); |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 170 | } |
| 171 | |