blob: 87c3b389684dc171161280a9c9940d061ae4ed50 [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"
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000011#include "SkTypeface.h"
reed@google.come8055352011-11-28 16:31:28 +000012
reed@google.comfb823002011-11-30 14:29:55 +000013// test shader w/ transparency
reed@google.come8055352011-11-28 16:31:28 +000014static SkShader* make_grad(SkScalar width) {
reed@google.comfb823002011-11-30 14:29:55 +000015 SkColor colors[] = { SK_ColorRED, 0x0000FF00, SK_ColorBLUE };
reed@google.come8055352011-11-28 16:31:28 +000016 SkPoint pts[] = { { 0, 0 }, { width, 0 } };
reed@google.comfb823002011-11-30 14:29:55 +000017 return SkGradientShader::CreateLinear(pts, colors, NULL,
reed@google.come8055352011-11-28 16:31:28 +000018 SK_ARRAY_COUNT(colors),
19 SkShader::kMirror_TileMode);
20}
21
reed@google.comfb823002011-11-30 14:29:55 +000022// test opaque shader
reed@google.come8055352011-11-28 16:31:28 +000023static 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);
29}
30
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000031static 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);
36}
37
reed@google.come8055352011-11-28 16:31:28 +000038namespace skiagm {
rmistry@google.comae933ce2012-08-23 18:19:56 +000039
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000040// Replicate chrome layout test - clipped pathed gradient-shaded text
41class ChromeGradTextGM1 : public GM {
42public:
43 ChromeGradTextGM1() { }
44protected:
45
46 virtual SkString onShortName() { return SkString("chrome_gradtext1"); }
tfarinaf5393182014-06-09 23:59:03 -070047 virtual SkISize onISize() { return SkISize::Make(500, 480); }
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000048 virtual void onDraw(SkCanvas* canvas) {
49 SkPaint paint;
Cary Clark992c7b02014-07-31 08:58:44 -040050 sk_tool_utils::set_portable_typeface(&paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000051 SkRect r = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
52
53 canvas->clipRect(r);
54
55 paint.setColor(SK_ColorRED);
56 canvas->drawRect(r, paint);
57
58 // Minimal repro doesn't require AA, LCD, or a nondefault typeface
59 paint.setShader(make_chrome_solid())->unref();
60 paint.setTextSize(SkIntToScalar(500));
61
62 canvas->drawText("I", 1, 0, 100, paint);
63 }
64private:
65 typedef GM INHERITED;
66};
67
68
69// Replicate chrome layout test - switching between solid & gadient text
70class ChromeGradTextGM2 : public GM {
71public:
72 ChromeGradTextGM2() { }
73protected:
74
75 virtual SkString onShortName() { return SkString("chrome_gradtext2"); }
tfarinaf5393182014-06-09 23:59:03 -070076 virtual SkISize onISize() { return SkISize::Make(500, 480); }
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000077 virtual void onDraw(SkCanvas* canvas) {
78 SkPaint paint;
Cary Clark992c7b02014-07-31 08:58:44 -040079 sk_tool_utils::set_portable_typeface(&paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000080
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);
85
86 // Minimal repro doesn't require AA, LCD, or a nondefault typeface
87 paint.setShader(make_chrome_solid())->unref();
88
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);
93 }
94private:
95 typedef GM INHERITED;
96};
97
98
99
reed@google.come8055352011-11-28 16:31:28 +0000100class GradTextGM : public GM {
101public:
102 GradTextGM () {}
103
104protected:
mtklein72c9faa2015-01-09 10:06:39 -0800105 SkString onShortName() SK_OVERRIDE {
reed@google.come8055352011-11-28 16:31:28 +0000106 return SkString("gradtext");
107 }
108
reed438b0d72014-12-19 07:40:26 -0800109 SkISize onISize() SK_OVERRIDE { return SkISize::Make(500, 480); }
reed@google.come8055352011-11-28 16:31:28 +0000110
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);
115 }
116
117 static void draw_text3(SkCanvas* canvas, const SkPaint& paint) {
118 SkPaint p(paint);
119
120 p.setAntiAlias(false);
121 draw_text(canvas, p);
122 p.setAntiAlias(true);
reed@google.comfb823002011-11-30 14:29:55 +0000123 canvas->translate(0, paint.getTextSize() * 4/3);
reed@google.come8055352011-11-28 16:31:28 +0000124 draw_text(canvas, p);
125 p.setLCDRenderText(true);
reed@google.comfb823002011-11-30 14:29:55 +0000126 canvas->translate(0, paint.getTextSize() * 4/3);
reed@google.come8055352011-11-28 16:31:28 +0000127 draw_text(canvas, p);
128 }
129
reed438b0d72014-12-19 07:40:26 -0800130 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
reed@google.come8055352011-11-28 16:31:28 +0000131 SkPaint paint;
Cary Clark992c7b02014-07-31 08:58:44 -0400132 sk_tool_utils::set_portable_typeface(&paint);
reed@google.comfb823002011-11-30 14:29:55 +0000133 paint.setTextSize(SkIntToScalar(26));
reed@google.come8055352011-11-28 16:31:28 +0000134
reed@google.comfb823002011-11-30 14:29:55 +0000135 const SkISize& size = this->getISize();
136 SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()),
137 SkIntToScalar(size.height()) / 2);
138 canvas->drawRect(r, paint);
139
140 canvas->translate(SkIntToScalar(20), paint.getTextSize());
141
142 for (int i = 0; i < 2; ++i) {
143 paint.setShader(make_grad(SkIntToScalar(80)))->unref();
144 draw_text3(canvas, paint);
145
146 canvas->translate(0, paint.getTextSize() * 2);
147
148 paint.setShader(make_grad2(SkIntToScalar(80)))->unref();
149 draw_text3(canvas, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000150
reed@google.comfb823002011-11-30 14:29:55 +0000151 canvas->translate(0, paint.getTextSize() * 2);
152 }
reed@google.come8055352011-11-28 16:31:28 +0000153 }
154
155private:
156 typedef GM INHERITED;
157};
158
159//////////////////////////////////////////////////////////////////////////////
160
161static GM* MyFactory(void*) { return new GradTextGM; }
tomhudson@google.comcb325ce2012-07-11 14:41:19 +0000162static GM* CMyFactory(void*) { return new ChromeGradTextGM1; }
163static GM* CMyFactory2(void*) { return new ChromeGradTextGM2; }
reed@google.come8055352011-11-28 16:31:28 +0000164
tomhudson@google.comcb325ce2012-07-11 14:41:19 +0000165static GMRegistry reg(MyFactory);
166static GMRegistry Creg(CMyFactory);
167static GMRegistry Creg2(CMyFactory2);
reed@google.come8055352011-11-28 16:31:28 +0000168}