blob: cc0c079d77f84158dc7d025a7b9919cbed57d8c0 [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
reed@google.come8055352011-11-28 16:31:28 +000010#include "SkCanvas.h"
11#include "SkGradientShader.h"
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000012#include "SkTypeface.h"
reed@google.come8055352011-11-28 16:31:28 +000013
reed@google.comfb823002011-11-30 14:29:55 +000014// test shader w/ transparency
reed1a9b9642016-03-13 14:13:58 -070015static sk_sp<SkShader> make_grad(SkScalar width) {
reed@google.comfb823002011-11-30 14:29:55 +000016 SkColor colors[] = { SK_ColorRED, 0x0000FF00, SK_ColorBLUE };
reed@google.come8055352011-11-28 16:31:28 +000017 SkPoint pts[] = { { 0, 0 }, { width, 0 } };
reed1a9b9642016-03-13 14:13:58 -070018 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
19 SkShader::kMirror_TileMode);
reed@google.come8055352011-11-28 16:31:28 +000020}
21
reed@google.comfb823002011-11-30 14:29:55 +000022// test opaque shader
reed1a9b9642016-03-13 14:13:58 -070023static sk_sp<SkShader> make_grad2(SkScalar width) {
reed@google.come8055352011-11-28 16:31:28 +000024 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
25 SkPoint pts[] = { { 0, 0 }, { width, 0 } };
reed1a9b9642016-03-13 14:13:58 -070026 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
27 SkShader::kMirror_TileMode);
reed@google.come8055352011-11-28 16:31:28 +000028}
29
reed1a9b9642016-03-13 14:13:58 -070030static sk_sp<SkShader> make_chrome_solid() {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000031 SkColor colors[] = { SK_ColorGREEN, SK_ColorGREEN };
32 SkPoint pts[] = { { 0, 0 }, { 1, 0 } };
reed1a9b9642016-03-13 14:13:58 -070033 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000034}
35
reed@google.come8055352011-11-28 16:31:28 +000036namespace skiagm {
rmistry@google.comae933ce2012-08-23 18:19:56 +000037
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000038// Replicate chrome layout test - clipped pathed gradient-shaded text
39class ChromeGradTextGM1 : public GM {
40public:
41 ChromeGradTextGM1() { }
42protected:
43
44 virtual SkString onShortName() { return SkString("chrome_gradtext1"); }
tfarinaf5393182014-06-09 23:59:03 -070045 virtual SkISize onISize() { return SkISize::Make(500, 480); }
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000046 virtual void onDraw(SkCanvas* canvas) {
47 SkPaint paint;
caryclark1818acb2015-07-24 12:09:25 -070048 sk_tool_utils::set_portable_typeface(&paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000049 SkRect r = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
50
51 canvas->clipRect(r);
52
53 paint.setColor(SK_ColorRED);
54 canvas->drawRect(r, paint);
55
56 // Minimal repro doesn't require AA, LCD, or a nondefault typeface
reed1a9b9642016-03-13 14:13:58 -070057 paint.setShader(make_chrome_solid());
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000058 paint.setTextSize(SkIntToScalar(500));
59
Cary Clark2a475ea2017-04-28 15:35:12 -040060 canvas->drawString("I", 0, 100, paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000061 }
62private:
63 typedef GM INHERITED;
64};
65
66
caryclark5df05752015-07-15 11:57:00 -070067// Replicate chrome layout test - switching between solid & gradient text
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000068class ChromeGradTextGM2 : public GM {
69public:
70 ChromeGradTextGM2() { }
71protected:
72
73 virtual SkString onShortName() { return SkString("chrome_gradtext2"); }
tfarinaf5393182014-06-09 23:59:03 -070074 virtual SkISize onISize() { return SkISize::Make(500, 480); }
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000075 virtual void onDraw(SkCanvas* canvas) {
76 SkPaint paint;
caryclark1818acb2015-07-24 12:09:25 -070077 sk_tool_utils::set_portable_typeface(&paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000078
79 paint.setStyle(SkPaint::kFill_Style);
Cary Clark2a475ea2017-04-28 15:35:12 -040080 canvas->drawString("Normal Fill Text", 0, 50, paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000081 paint.setStyle(SkPaint::kStroke_Style);
Cary Clark2a475ea2017-04-28 15:35:12 -040082 canvas->drawString("Normal Stroke Text", 0, 100, paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000083
84 // Minimal repro doesn't require AA, LCD, or a nondefault typeface
reed1a9b9642016-03-13 14:13:58 -070085 paint.setShader(make_chrome_solid());
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000086
87 paint.setStyle(SkPaint::kFill_Style);
Cary Clark2a475ea2017-04-28 15:35:12 -040088 canvas->drawString("Gradient Fill Text", 0, 150, paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000089 paint.setStyle(SkPaint::kStroke_Style);
Cary Clark2a475ea2017-04-28 15:35:12 -040090 canvas->drawString("Gradient Stroke Text", 0, 200, paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000091 }
92private:
93 typedef GM INHERITED;
94};
95
96
97
reed@google.come8055352011-11-28 16:31:28 +000098class GradTextGM : public GM {
99public:
100 GradTextGM () {}
101
102protected:
mtklein36352bf2015-03-25 18:17:31 -0700103 SkString onShortName() override {
reed@google.come8055352011-11-28 16:31:28 +0000104 return SkString("gradtext");
105 }
106
mtklein36352bf2015-03-25 18:17:31 -0700107 SkISize onISize() override { return SkISize::Make(500, 480); }
reed@google.come8055352011-11-28 16:31:28 +0000108
109 static void draw_text(SkCanvas* canvas, const SkPaint& paint) {
110 const char* text = "When in the course of human events";
111 size_t len = strlen(text);
112 canvas->drawText(text, len, 0, 0, paint);
113 }
114
115 static void draw_text3(SkCanvas* canvas, const SkPaint& paint) {
116 SkPaint p(paint);
117
118 p.setAntiAlias(false);
119 draw_text(canvas, p);
120 p.setAntiAlias(true);
reed@google.comfb823002011-11-30 14:29:55 +0000121 canvas->translate(0, paint.getTextSize() * 4/3);
reed@google.come8055352011-11-28 16:31:28 +0000122 draw_text(canvas, p);
123 p.setLCDRenderText(true);
reed@google.comfb823002011-11-30 14:29:55 +0000124 canvas->translate(0, paint.getTextSize() * 4/3);
reed@google.come8055352011-11-28 16:31:28 +0000125 draw_text(canvas, p);
126 }
127
mtklein36352bf2015-03-25 18:17:31 -0700128 void onDraw(SkCanvas* canvas) override {
reed@google.come8055352011-11-28 16:31:28 +0000129 SkPaint paint;
caryclark1818acb2015-07-24 12:09:25 -0700130 sk_tool_utils::set_portable_typeface(&paint);
reed@google.comfb823002011-11-30 14:29:55 +0000131 paint.setTextSize(SkIntToScalar(26));
reed@google.come8055352011-11-28 16:31:28 +0000132
reed@google.comfb823002011-11-30 14:29:55 +0000133 const SkISize& size = this->getISize();
134 SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()),
135 SkIntToScalar(size.height()) / 2);
136 canvas->drawRect(r, paint);
137
138 canvas->translate(SkIntToScalar(20), paint.getTextSize());
139
140 for (int i = 0; i < 2; ++i) {
reed1a9b9642016-03-13 14:13:58 -0700141 paint.setShader(make_grad(SkIntToScalar(80)));
reed@google.comfb823002011-11-30 14:29:55 +0000142 draw_text3(canvas, paint);
143
144 canvas->translate(0, paint.getTextSize() * 2);
145
reed1a9b9642016-03-13 14:13:58 -0700146 paint.setShader(make_grad2(SkIntToScalar(80)));
reed@google.comfb823002011-11-30 14:29:55 +0000147 draw_text3(canvas, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000148
reed@google.comfb823002011-11-30 14:29:55 +0000149 canvas->translate(0, paint.getTextSize() * 2);
150 }
reed@google.come8055352011-11-28 16:31:28 +0000151 }
152
153private:
154 typedef GM INHERITED;
155};
156
157//////////////////////////////////////////////////////////////////////////////
158
159static GM* MyFactory(void*) { return new GradTextGM; }
tomhudson@google.comcb325ce2012-07-11 14:41:19 +0000160static GM* CMyFactory(void*) { return new ChromeGradTextGM1; }
161static GM* CMyFactory2(void*) { return new ChromeGradTextGM2; }
reed@google.come8055352011-11-28 16:31:28 +0000162
tomhudson@google.comcb325ce2012-07-11 14:41:19 +0000163static GMRegistry reg(MyFactory);
164static GMRegistry Creg(CMyFactory);
165static GMRegistry Creg2(CMyFactory2);
reed@google.come8055352011-11-28 16:31:28 +0000166}