2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "SkGradientShader.h"
11 #include "SkTypeface.h"
13 // test shader w/ transparency
14 static SkShader* make_grad(SkScalar width) {
15 SkColor colors[] = { SK_ColorRED, 0x0000FF00, SK_ColorBLUE };
16 SkPoint pts[] = { { 0, 0 }, { width, 0 } };
17 return SkGradientShader::CreateLinear(pts, colors, NULL,
18 SK_ARRAY_COUNT(colors),
19 SkShader::kMirror_TileMode);
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);
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);
40 // Replicate chrome layout test - clipped pathed gradient-shaded text
41 class ChromeGradTextGM1 : public GM {
43 ChromeGradTextGM1() { }
46 virtual SkString onShortName() { return SkString("chrome_gradtext1"); }
47 virtual SkISize onISize() { return SkISize::Make(500, 480); }
48 virtual void onDraw(SkCanvas* canvas) {
50 sk_tool_utils::set_portable_typeface(&paint);
51 SkRect r = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
55 paint.setColor(SK_ColorRED);
56 canvas->drawRect(r, paint);
58 // Minimal repro doesn't require AA, LCD, or a nondefault typeface
59 paint.setShader(make_chrome_solid())->unref();
60 paint.setTextSize(SkIntToScalar(500));
62 canvas->drawText("I", 1, 0, 100, paint);
69 // Replicate chrome layout test - switching between solid & gadient text
70 class ChromeGradTextGM2 : public GM {
72 ChromeGradTextGM2() { }
75 virtual SkString onShortName() { return SkString("chrome_gradtext2"); }
76 virtual SkISize onISize() { return SkISize::Make(500, 480); }
77 virtual void onDraw(SkCanvas* canvas) {
79 sk_tool_utils::set_portable_typeface(&paint);
81 paint.setStyle(SkPaint::kFill_Style);
82 canvas->drawText("Normal Fill Text", 16, 0, 50, paint);
83 paint.setStyle(SkPaint::kStroke_Style);
84 canvas->drawText("Normal Stroke Text", 18, 0, 100, paint);
86 // Minimal repro doesn't require AA, LCD, or a nondefault typeface
87 paint.setShader(make_chrome_solid())->unref();
89 paint.setStyle(SkPaint::kFill_Style);
90 canvas->drawText("Gradient Fill Text", 18, 0, 150, paint);
91 paint.setStyle(SkPaint::kStroke_Style);
92 canvas->drawText("Gradient Stroke Text", 20, 0, 200, paint);
100 class GradTextGM : public GM {
105 SkString onShortName() SK_OVERRIDE {
106 return SkString("gradtext");
109 SkISize onISize() SK_OVERRIDE { return SkISize::Make(500, 480); }
111 static void draw_text(SkCanvas* canvas, const SkPaint& paint) {
112 const char* text = "When in the course of human events";
113 size_t len = strlen(text);
114 canvas->drawText(text, len, 0, 0, paint);
117 static void draw_text3(SkCanvas* canvas, const SkPaint& paint) {
120 p.setAntiAlias(false);
121 draw_text(canvas, p);
122 p.setAntiAlias(true);
123 canvas->translate(0, paint.getTextSize() * 4/3);
124 draw_text(canvas, p);
125 p.setLCDRenderText(true);
126 canvas->translate(0, paint.getTextSize() * 4/3);
127 draw_text(canvas, p);
130 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
132 sk_tool_utils::set_portable_typeface(&paint);
133 paint.setTextSize(SkIntToScalar(26));
135 const SkISize& size = this->getISize();
136 SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()),
137 SkIntToScalar(size.height()) / 2);
138 canvas->drawRect(r, paint);
140 canvas->translate(SkIntToScalar(20), paint.getTextSize());
142 for (int i = 0; i < 2; ++i) {
143 paint.setShader(make_grad(SkIntToScalar(80)))->unref();
144 draw_text3(canvas, paint);
146 canvas->translate(0, paint.getTextSize() * 2);
148 paint.setShader(make_grad2(SkIntToScalar(80)))->unref();
149 draw_text3(canvas, paint);
151 canvas->translate(0, paint.getTextSize() * 2);
156 typedef GM INHERITED;
159 //////////////////////////////////////////////////////////////////////////////
161 static GM* MyFactory(void*) { return new GradTextGM; }
162 static GM* CMyFactory(void*) { return new ChromeGradTextGM1; }
163 static GM* CMyFactory2(void*) { return new ChromeGradTextGM2; }
165 static GMRegistry reg(MyFactory);
166 static GMRegistry Creg(CMyFactory);
167 static GMRegistry Creg2(CMyFactory2);