| 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 | |
| 12 | static SkShader* make_grad(SkScalar width) { |
| 13 | SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, 0 }; |
| 14 | SkScalar pos[] = { 0, SK_Scalar1 * 5 / 10, SK_Scalar1 }; |
| 15 | SkPoint pts[] = { { 0, 0 }, { width, 0 } }; |
| 16 | return SkGradientShader::CreateLinear(pts, colors, pos, |
| 17 | SK_ARRAY_COUNT(colors), |
| 18 | SkShader::kMirror_TileMode); |
| 19 | } |
| 20 | |
| 21 | static SkShader* make_grad2(SkScalar width) { |
| 22 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
| 23 | SkPoint pts[] = { { 0, 0 }, { width, 0 } }; |
| 24 | return SkGradientShader::CreateLinear(pts, colors, NULL, |
| 25 | SK_ARRAY_COUNT(colors), |
| 26 | SkShader::kMirror_TileMode); |
| 27 | } |
| 28 | |
| 29 | namespace skiagm { |
| 30 | |
| 31 | class GradTextGM : public GM { |
| 32 | public: |
| 33 | GradTextGM () {} |
| 34 | |
| 35 | protected: |
| 36 | |
| 37 | virtual SkString onShortName() { |
| 38 | return SkString("gradtext"); |
| 39 | } |
| 40 | |
| 41 | virtual SkISize onISize() { return make_isize(640, 480); } |
| 42 | |
| 43 | static void draw_text(SkCanvas* canvas, const SkPaint& paint) { |
| 44 | const char* text = "When in the course of human events"; |
| 45 | size_t len = strlen(text); |
| 46 | canvas->drawText(text, len, 0, 0, paint); |
| 47 | } |
| 48 | |
| 49 | static void draw_text3(SkCanvas* canvas, const SkPaint& paint) { |
| 50 | SkPaint p(paint); |
| 51 | |
| 52 | p.setAntiAlias(false); |
| 53 | draw_text(canvas, p); |
| 54 | p.setAntiAlias(true); |
| 55 | canvas->translate(0, SkIntToScalar(20)); |
| 56 | draw_text(canvas, p); |
| 57 | p.setLCDRenderText(true); |
| 58 | canvas->translate(0, SkIntToScalar(20)); |
| 59 | draw_text(canvas, p); |
| 60 | } |
| 61 | |
| 62 | virtual void onDraw(SkCanvas* canvas) { |
| 63 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 64 | |
| 65 | // The blits special-case opaque and non-paque shaders, so test both |
| 66 | |
| 67 | SkPaint paint; |
| 68 | paint.setTextSize(SkIntToScalar(16)); |
| 69 | paint.setShader(make_grad(SkIntToScalar(80)))->unref(); |
| 70 | |
| 71 | draw_text3(canvas, paint); |
| 72 | canvas->translate(0, SkIntToScalar(40)); |
| 73 | paint.setShader(make_grad2(SkIntToScalar(80)))->unref(); |
| 74 | draw_text3(canvas, paint); |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | typedef GM INHERITED; |
| 79 | }; |
| 80 | |
| 81 | ////////////////////////////////////////////////////////////////////////////// |
| 82 | |
| 83 | static GM* MyFactory(void*) { return new GradTextGM; } |
| 84 | static GMRegistry reg(MyFactory); |
| 85 | |
| 86 | } |
| 87 | |