• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "sk_tool_utils.h"
10 #include "SkCanvas.h"
11 #include "SkGradientShader.h"
12 #include "SkTypeface.h"
13 
14 // test shader w/ transparency
make_grad(SkScalar width)15 static sk_sp<SkShader> make_grad(SkScalar width) {
16     SkColor colors[] = { SK_ColorRED, 0x0000FF00, SK_ColorBLUE };
17     SkPoint pts[] = { { 0, 0 }, { width, 0 } };
18     return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
19                                         SkShader::kMirror_TileMode);
20 }
21 
22 // test opaque shader
make_grad2(SkScalar width)23 static sk_sp<SkShader> make_grad2(SkScalar width) {
24     SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
25     SkPoint pts[] = { { 0, 0 }, { width, 0 } };
26     return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
27                                         SkShader::kMirror_TileMode);
28 }
29 
make_chrome_solid()30 static sk_sp<SkShader> make_chrome_solid() {
31     SkColor colors[] = { SK_ColorGREEN, SK_ColorGREEN };
32     SkPoint pts[] = { { 0, 0 }, { 1, 0 } };
33     return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
34 }
35 
36 namespace skiagm {
37 
38 // Replicate chrome layout test - clipped pathed gradient-shaded text
39 class ChromeGradTextGM1 : public GM {
40 public:
ChromeGradTextGM1()41     ChromeGradTextGM1() { }
42 protected:
43 
onShortName()44     virtual SkString onShortName() { return SkString("chrome_gradtext1"); }
onISize()45     virtual SkISize onISize() { return SkISize::Make(500, 480); }
onDraw(SkCanvas * canvas)46     virtual void onDraw(SkCanvas* canvas) {
47         SkPaint paint;
48         sk_tool_utils::set_portable_typeface(&paint);
49         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
57         paint.setShader(make_chrome_solid());
58         paint.setTextSize(SkIntToScalar(500));
59 
60         canvas->drawString("I", 0, 100, paint);
61     }
62 private:
63     typedef GM INHERITED;
64 };
65 
66 
67 // Replicate chrome layout test - switching between solid & gradient text
68 class ChromeGradTextGM2 : public GM {
69 public:
ChromeGradTextGM2()70     ChromeGradTextGM2() { }
71 protected:
72 
onShortName()73     virtual SkString onShortName() { return SkString("chrome_gradtext2"); }
onISize()74     virtual SkISize onISize() { return SkISize::Make(500, 480); }
onDraw(SkCanvas * canvas)75     virtual void onDraw(SkCanvas* canvas) {
76         SkPaint paint;
77         sk_tool_utils::set_portable_typeface(&paint);
78 
79         paint.setStyle(SkPaint::kFill_Style);
80         canvas->drawString("Normal Fill Text", 0, 50, paint);
81         paint.setStyle(SkPaint::kStroke_Style);
82         canvas->drawString("Normal Stroke Text", 0, 100, paint);
83 
84         // Minimal repro doesn't require AA, LCD, or a nondefault typeface
85         paint.setShader(make_chrome_solid());
86 
87         paint.setStyle(SkPaint::kFill_Style);
88         canvas->drawString("Gradient Fill Text", 0, 150, paint);
89         paint.setStyle(SkPaint::kStroke_Style);
90         canvas->drawString("Gradient Stroke Text", 0, 200, paint);
91     }
92 private:
93     typedef GM INHERITED;
94 };
95 
96 
97 
98 class GradTextGM : public GM {
99 public:
GradTextGM()100     GradTextGM () {}
101 
102 protected:
onShortName()103     SkString onShortName() override {
104         return SkString("gradtext");
105     }
106 
onISize()107     SkISize onISize() override { return SkISize::Make(500, 480); }
108 
draw_text(SkCanvas * canvas,const SkPaint & paint)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 
draw_text3(SkCanvas * canvas,const SkPaint & paint)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);
121         canvas->translate(0, paint.getTextSize() * 4/3);
122         draw_text(canvas, p);
123         p.setLCDRenderText(true);
124         canvas->translate(0, paint.getTextSize() * 4/3);
125         draw_text(canvas, p);
126     }
127 
onDraw(SkCanvas * canvas)128     void onDraw(SkCanvas* canvas) override {
129         SkPaint paint;
130         sk_tool_utils::set_portable_typeface(&paint);
131         paint.setTextSize(SkIntToScalar(26));
132 
133         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) {
141             paint.setShader(make_grad(SkIntToScalar(80)));
142             draw_text3(canvas, paint);
143 
144             canvas->translate(0, paint.getTextSize() * 2);
145 
146             paint.setShader(make_grad2(SkIntToScalar(80)));
147             draw_text3(canvas, paint);
148 
149             canvas->translate(0, paint.getTextSize() * 2);
150         }
151     }
152 
153 private:
154     typedef GM INHERITED;
155 };
156 
157 //////////////////////////////////////////////////////////////////////////////
158 
MyFactory(void *)159 static GM* MyFactory(void*) { return new GradTextGM; }
CMyFactory(void *)160 static GM* CMyFactory(void*) { return new ChromeGradTextGM1; }
CMyFactory2(void *)161 static GM* CMyFactory2(void*) { return new ChromeGradTextGM2; }
162 
163 static GMRegistry reg(MyFactory);
164 static GMRegistry Creg(CMyFactory);
165 static GMRegistry Creg2(CMyFactory2);
166 }
167