blob: d66454760bdd13b9a031dd592852931dc66eb44e [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
reed1a9b9642016-03-13 14:13:58 -070014static sk_sp<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 } };
reed1a9b9642016-03-13 14:13:58 -070017 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
18 SkShader::kMirror_TileMode);
reed@google.come8055352011-11-28 16:31:28 +000019}
20
reed@google.comfb823002011-11-30 14:29:55 +000021// test opaque shader
reed1a9b9642016-03-13 14:13:58 -070022static sk_sp<SkShader> make_grad2(SkScalar width) {
reed@google.come8055352011-11-28 16:31:28 +000023 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
24 SkPoint pts[] = { { 0, 0 }, { width, 0 } };
reed1a9b9642016-03-13 14:13:58 -070025 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
26 SkShader::kMirror_TileMode);
reed@google.come8055352011-11-28 16:31:28 +000027}
28
reed1a9b9642016-03-13 14:13:58 -070029static sk_sp<SkShader> make_chrome_solid() {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000030 SkColor colors[] = { SK_ColorGREEN, SK_ColorGREEN };
31 SkPoint pts[] = { { 0, 0 }, { 1, 0 } };
reed1a9b9642016-03-13 14:13:58 -070032 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000033}
34
reed@google.come8055352011-11-28 16:31:28 +000035namespace skiagm {
rmistry@google.comae933ce2012-08-23 18:19:56 +000036
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000037// Replicate chrome layout test - clipped pathed gradient-shaded text
38class ChromeGradTextGM1 : public GM {
39public:
40 ChromeGradTextGM1() { }
41protected:
42
43 virtual SkString onShortName() { return SkString("chrome_gradtext1"); }
tfarinaf5393182014-06-09 23:59:03 -070044 virtual SkISize onISize() { return SkISize::Make(500, 480); }
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000045 virtual void onDraw(SkCanvas* canvas) {
46 SkPaint paint;
caryclark1818acb2015-07-24 12:09:25 -070047 sk_tool_utils::set_portable_typeface(&paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000048 SkRect r = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
49
50 canvas->clipRect(r);
51
52 paint.setColor(SK_ColorRED);
53 canvas->drawRect(r, paint);
54
55 // Minimal repro doesn't require AA, LCD, or a nondefault typeface
reed1a9b9642016-03-13 14:13:58 -070056 paint.setShader(make_chrome_solid());
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000057 paint.setTextSize(SkIntToScalar(500));
58
59 canvas->drawText("I", 1, 0, 100, paint);
60 }
61private:
62 typedef GM INHERITED;
63};
64
65
caryclark5df05752015-07-15 11:57:00 -070066// Replicate chrome layout test - switching between solid & gradient text
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000067class ChromeGradTextGM2 : public GM {
68public:
69 ChromeGradTextGM2() { }
70protected:
71
72 virtual SkString onShortName() { return SkString("chrome_gradtext2"); }
tfarinaf5393182014-06-09 23:59:03 -070073 virtual SkISize onISize() { return SkISize::Make(500, 480); }
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000074 virtual void onDraw(SkCanvas* canvas) {
75 SkPaint paint;
caryclark1818acb2015-07-24 12:09:25 -070076 sk_tool_utils::set_portable_typeface(&paint);
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000077
78 paint.setStyle(SkPaint::kFill_Style);
79 canvas->drawText("Normal Fill Text", 16, 0, 50, paint);
80 paint.setStyle(SkPaint::kStroke_Style);
81 canvas->drawText("Normal Stroke Text", 18, 0, 100, paint);
82
83 // Minimal repro doesn't require AA, LCD, or a nondefault typeface
reed1a9b9642016-03-13 14:13:58 -070084 paint.setShader(make_chrome_solid());
tomhudson@google.comcb325ce2012-07-11 14:41:19 +000085
86 paint.setStyle(SkPaint::kFill_Style);
87 canvas->drawText("Gradient Fill Text", 18, 0, 150, paint);
88 paint.setStyle(SkPaint::kStroke_Style);
89 canvas->drawText("Gradient Stroke Text", 20, 0, 200, paint);
90 }
91private:
92 typedef GM INHERITED;
93};
94
95
96
reed@google.come8055352011-11-28 16:31:28 +000097class GradTextGM : public GM {
98public:
99 GradTextGM () {}
100
101protected:
mtklein36352bf2015-03-25 18:17:31 -0700102 SkString onShortName() override {
reed@google.come8055352011-11-28 16:31:28 +0000103 return SkString("gradtext");
104 }
105
mtklein36352bf2015-03-25 18:17:31 -0700106 SkISize onISize() override { return SkISize::Make(500, 480); }
reed@google.come8055352011-11-28 16:31:28 +0000107
108 static void draw_text(SkCanvas* canvas, const SkPaint& paint) {
109 const char* text = "When in the course of human events";
110 size_t len = strlen(text);
111 canvas->drawText(text, len, 0, 0, paint);
112 }
113
114 static void draw_text3(SkCanvas* canvas, const SkPaint& paint) {
115 SkPaint p(paint);
116
117 p.setAntiAlias(false);
118 draw_text(canvas, p);
119 p.setAntiAlias(true);
reed@google.comfb823002011-11-30 14:29:55 +0000120 canvas->translate(0, paint.getTextSize() * 4/3);
reed@google.come8055352011-11-28 16:31:28 +0000121 draw_text(canvas, p);
122 p.setLCDRenderText(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 }
126
mtklein36352bf2015-03-25 18:17:31 -0700127 void onDraw(SkCanvas* canvas) override {
reed@google.come8055352011-11-28 16:31:28 +0000128 SkPaint paint;
caryclark1818acb2015-07-24 12:09:25 -0700129 sk_tool_utils::set_portable_typeface(&paint);
reed@google.comfb823002011-11-30 14:29:55 +0000130 paint.setTextSize(SkIntToScalar(26));
reed@google.come8055352011-11-28 16:31:28 +0000131
reed@google.comfb823002011-11-30 14:29:55 +0000132 const SkISize& size = this->getISize();
133 SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()),
134 SkIntToScalar(size.height()) / 2);
135 canvas->drawRect(r, paint);
136
137 canvas->translate(SkIntToScalar(20), paint.getTextSize());
138
139 for (int i = 0; i < 2; ++i) {
reed1a9b9642016-03-13 14:13:58 -0700140 paint.setShader(make_grad(SkIntToScalar(80)));
reed@google.comfb823002011-11-30 14:29:55 +0000141 draw_text3(canvas, paint);
142
143 canvas->translate(0, paint.getTextSize() * 2);
144
reed1a9b9642016-03-13 14:13:58 -0700145 paint.setShader(make_grad2(SkIntToScalar(80)));
reed@google.comfb823002011-11-30 14:29:55 +0000146 draw_text3(canvas, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000147
reed@google.comfb823002011-11-30 14:29:55 +0000148 canvas->translate(0, paint.getTextSize() * 2);
149 }
reed@google.come8055352011-11-28 16:31:28 +0000150 }
151
152private:
153 typedef GM INHERITED;
154};
155
156//////////////////////////////////////////////////////////////////////////////
157
158static GM* MyFactory(void*) { return new GradTextGM; }
tomhudson@google.comcb325ce2012-07-11 14:41:19 +0000159static GM* CMyFactory(void*) { return new ChromeGradTextGM1; }
160static GM* CMyFactory2(void*) { return new ChromeGradTextGM2; }
reed@google.come8055352011-11-28 16:31:28 +0000161
tomhudson@google.comcb325ce2012-07-11 14:41:19 +0000162static GMRegistry reg(MyFactory);
163static GMRegistry Creg(CMyFactory);
164static GMRegistry Creg2(CMyFactory2);
reed@google.come8055352011-11-28 16:31:28 +0000165}