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" |
| 11 | |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 12 | // test shader w/ transparency |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 13 | static SkShader* make_grad(SkScalar width) { |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 14 | SkColor colors[] = { SK_ColorRED, 0x0000FF00, SK_ColorBLUE }; |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 15 | SkPoint pts[] = { { 0, 0 }, { width, 0 } }; |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 16 | return SkGradientShader::CreateLinear(pts, colors, NULL, |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 17 | SK_ARRAY_COUNT(colors), |
| 18 | SkShader::kMirror_TileMode); |
| 19 | } |
| 20 | |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 21 | // test opaque shader |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 22 | static SkShader* make_grad2(SkScalar width) { |
| 23 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
| 24 | SkPoint pts[] = { { 0, 0 }, { width, 0 } }; |
| 25 | return SkGradientShader::CreateLinear(pts, colors, NULL, |
| 26 | SK_ARRAY_COUNT(colors), |
| 27 | SkShader::kMirror_TileMode); |
| 28 | } |
| 29 | |
| 30 | namespace skiagm { |
| 31 | |
| 32 | class GradTextGM : public GM { |
| 33 | public: |
| 34 | GradTextGM () {} |
| 35 | |
| 36 | protected: |
| 37 | |
| 38 | virtual SkString onShortName() { |
| 39 | return SkString("gradtext"); |
| 40 | } |
| 41 | |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 42 | virtual SkISize onISize() { return make_isize(500, 480); } |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 43 | |
| 44 | static void draw_text(SkCanvas* canvas, const SkPaint& paint) { |
| 45 | const char* text = "When in the course of human events"; |
| 46 | size_t len = strlen(text); |
| 47 | canvas->drawText(text, len, 0, 0, paint); |
| 48 | } |
| 49 | |
| 50 | static void draw_text3(SkCanvas* canvas, const SkPaint& paint) { |
| 51 | SkPaint p(paint); |
| 52 | |
| 53 | p.setAntiAlias(false); |
| 54 | draw_text(canvas, p); |
| 55 | p.setAntiAlias(true); |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 56 | canvas->translate(0, paint.getTextSize() * 4/3); |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 57 | draw_text(canvas, p); |
| 58 | p.setLCDRenderText(true); |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 59 | canvas->translate(0, paint.getTextSize() * 4/3); |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 60 | draw_text(canvas, p); |
| 61 | } |
| 62 | |
| 63 | virtual void onDraw(SkCanvas* canvas) { |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 64 | SkPaint paint; |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 65 | paint.setTextSize(SkIntToScalar(26)); |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 66 | |
reed@google.com | fb82300 | 2011-11-30 14:29:55 +0000 | [diff] [blame] | 67 | const SkISize& size = this->getISize(); |
| 68 | SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()), |
| 69 | SkIntToScalar(size.height()) / 2); |
| 70 | canvas->drawRect(r, paint); |
| 71 | |
| 72 | canvas->translate(SkIntToScalar(20), paint.getTextSize()); |
| 73 | |
| 74 | for (int i = 0; i < 2; ++i) { |
| 75 | paint.setShader(make_grad(SkIntToScalar(80)))->unref(); |
| 76 | draw_text3(canvas, paint); |
| 77 | |
| 78 | canvas->translate(0, paint.getTextSize() * 2); |
| 79 | |
| 80 | paint.setShader(make_grad2(SkIntToScalar(80)))->unref(); |
| 81 | draw_text3(canvas, paint); |
| 82 | |
| 83 | canvas->translate(0, paint.getTextSize() * 2); |
| 84 | } |
reed@google.com | e805535 | 2011-11-28 16:31:28 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | private: |
| 88 | typedef GM INHERITED; |
| 89 | }; |
| 90 | |
| 91 | ////////////////////////////////////////////////////////////////////////////// |
| 92 | |
| 93 | static GM* MyFactory(void*) { return new GradTextGM; } |
| 94 | static GMRegistry reg(MyFactory); |
| 95 | |
| 96 | } |
| 97 | |