blob: a3769d2728878753f5bf1f1f7b5feac2f1068c04 [file] [log] [blame]
reed@google.come8055352011-11-28 16:31:28 +00001/*
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.comfb823002011-11-30 14:29:55 +000012// test shader w/ transparency
reed@google.come8055352011-11-28 16:31:28 +000013static SkShader* make_grad(SkScalar width) {
reed@google.comfb823002011-11-30 14:29:55 +000014 SkColor colors[] = { SK_ColorRED, 0x0000FF00, SK_ColorBLUE };
reed@google.come8055352011-11-28 16:31:28 +000015 SkPoint pts[] = { { 0, 0 }, { width, 0 } };
reed@google.comfb823002011-11-30 14:29:55 +000016 return SkGradientShader::CreateLinear(pts, colors, NULL,
reed@google.come8055352011-11-28 16:31:28 +000017 SK_ARRAY_COUNT(colors),
18 SkShader::kMirror_TileMode);
19}
20
reed@google.comfb823002011-11-30 14:29:55 +000021// test opaque shader
reed@google.come8055352011-11-28 16:31:28 +000022static 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
30namespace skiagm {
31
32class GradTextGM : public GM {
33public:
34 GradTextGM () {}
35
36protected:
37
38 virtual SkString onShortName() {
39 return SkString("gradtext");
40 }
41
reed@google.comfb823002011-11-30 14:29:55 +000042 virtual SkISize onISize() { return make_isize(500, 480); }
reed@google.come8055352011-11-28 16:31:28 +000043
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.comfb823002011-11-30 14:29:55 +000056 canvas->translate(0, paint.getTextSize() * 4/3);
reed@google.come8055352011-11-28 16:31:28 +000057 draw_text(canvas, p);
58 p.setLCDRenderText(true);
reed@google.comfb823002011-11-30 14:29:55 +000059 canvas->translate(0, paint.getTextSize() * 4/3);
reed@google.come8055352011-11-28 16:31:28 +000060 draw_text(canvas, p);
61 }
62
63 virtual void onDraw(SkCanvas* canvas) {
reed@google.come8055352011-11-28 16:31:28 +000064 SkPaint paint;
reed@google.comfb823002011-11-30 14:29:55 +000065 paint.setTextSize(SkIntToScalar(26));
reed@google.come8055352011-11-28 16:31:28 +000066
reed@google.comfb823002011-11-30 14:29:55 +000067 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.come8055352011-11-28 16:31:28 +000085 }
86
87private:
88 typedef GM INHERITED;
89};
90
91//////////////////////////////////////////////////////////////////////////////
92
93static GM* MyFactory(void*) { return new GradTextGM; }
94static GMRegistry reg(MyFactory);
95
96}
97