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 // test shader w/ transparency
make_grad(SkScalar width)13 static SkShader* make_grad(SkScalar width) {
14 SkColor colors[] = { SK_ColorRED, 0x0000FF00, SK_ColorBLUE };
15 SkPoint pts[] = { { 0, 0 }, { width, 0 } };
16 return SkGradientShader::CreateLinear(pts, colors, NULL,
17 SK_ARRAY_COUNT(colors),
18 SkShader::kMirror_TileMode);
19 }
20
21 // test opaque shader
make_grad2(SkScalar width)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:
GradTextGM()34 GradTextGM () {}
35
36 protected:
37
onShortName()38 virtual SkString onShortName() {
39 return SkString("gradtext");
40 }
41
onISize()42 virtual SkISize onISize() { return make_isize(500, 480); }
43
draw_text(SkCanvas * canvas,const SkPaint & paint)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
draw_text3(SkCanvas * canvas,const SkPaint & paint)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);
56 canvas->translate(0, paint.getTextSize() * 4/3);
57 draw_text(canvas, p);
58 p.setLCDRenderText(true);
59 canvas->translate(0, paint.getTextSize() * 4/3);
60 draw_text(canvas, p);
61 }
62
onDraw(SkCanvas * canvas)63 virtual void onDraw(SkCanvas* canvas) {
64 SkPaint paint;
65 paint.setTextSize(SkIntToScalar(26));
66
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 }
85 }
86
87 private:
88 typedef GM INHERITED;
89 };
90
91 //////////////////////////////////////////////////////////////////////////////
92
MyFactory(void *)93 static GM* MyFactory(void*) { return new GradTextGM; }
94 static GMRegistry reg(MyFactory);
95
96 }
97
98