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 "SkPath.h"
12 #include "SkGradientShader.h"
13 #include "SkTypeface.h"
14
make_heatGradient(const SkPoint pts[2])15 static sk_sp<SkShader> make_heatGradient(const SkPoint pts[2]) {
16 const SkColor bw[] = { SK_ColorBLACK, SK_ColorWHITE };
17
18 return SkGradientShader::MakeLinear(pts, bw, nullptr, SK_ARRAY_COUNT(bw),
19 SkShader::kClamp_TileMode);
20 }
21
22 /**
23 Test a set of clipping problems discovered while writing blitAntiRect,
24 and test all the code paths through the clipping blitters.
25 Each region should show as a blue center surrounded by a 2px green
26 border, with no red.
27 */
28
29 #define HEIGHT 480
30
31 class GammaTextGM : public skiagm::GM {
32 protected:
onShortName()33 SkString onShortName() override {
34 return SkString("gammatext");
35 }
36
onISize()37 SkISize onISize() override {
38 return SkISize::Make(1024, HEIGHT);
39 }
40
drawGrad(SkCanvas * canvas)41 static void drawGrad(SkCanvas* canvas) {
42 const SkPoint pts[] = { { 0, 0 }, { 0, SkIntToScalar(HEIGHT) } };
43
44 canvas->clear(SK_ColorRED);
45 SkPaint paint;
46 paint.setShader(make_heatGradient(pts));
47 SkRect r = { 0, 0, SkIntToScalar(1024), SkIntToScalar(HEIGHT) };
48 canvas->drawRect(r, paint);
49 }
50
onDraw(SkCanvas * canvas)51 void onDraw(SkCanvas* canvas) override {
52 drawGrad(canvas);
53
54 const SkColor fg[] = {
55 0xFFFFFFFF,
56 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF,
57 0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
58 0xFF000000,
59 };
60
61 const char* text = "Hamburgefons";
62
63 SkPaint paint;
64 SkFont font(nullptr, 16);
65 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
66
67 SkScalar x = SkIntToScalar(10);
68 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
69 paint.setColor(fg[i]);
70
71 SkScalar y = SkIntToScalar(40);
72 SkScalar stopy = SkIntToScalar(HEIGHT);
73 while (y < stopy) {
74 canvas->drawString(text, x, y, font, paint);
75 y += font.getSize() * 2;
76 }
77 x += SkIntToScalar(1024) / SK_ARRAY_COUNT(fg);
78 }
79 }
80
81 private:
82 typedef skiagm::GM INHERITED;
83 };
84
DEF_GM(return new GammaTextGM;)85 DEF_GM( return new GammaTextGM; )
86
87 //////////////////////////////////////////////////////////////////////////////
88
89 static sk_sp<SkShader> make_gradient(SkColor c) {
90 const SkPoint pts[] = { { 0, 0 }, { 240, 0 } };
91 SkColor colors[2];
92 colors[0] = c;
93 colors[1] = SkColorSetA(c, 0);
94 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
95 }
96
draw_pair(SkCanvas * canvas,const SkFont & font,SkColor color,const sk_sp<SkShader> & shader)97 static void draw_pair(SkCanvas* canvas, const SkFont& font, SkColor color,
98 const sk_sp<SkShader>& shader) {
99 static const char text[] = "Now is the time for all good";
100 SkPaint paint;
101 paint.setColor(color);
102 canvas->drawString(text, 10, 20, font, paint);
103 paint.setShader(SkShader::MakeColorShader(paint.getColor()));
104 canvas->drawString(text, 10, 40, font, paint);
105 paint.setShader(shader);
106 canvas->drawString(text, 10, 60, font, paint);
107 }
108
109 class GammaShaderTextGM : public skiagm::GM {
110 sk_sp<SkShader> fShaders[3];
111 SkColor fColors[3];
112
113 public:
GammaShaderTextGM()114 GammaShaderTextGM() {
115 const SkColor colors[] = { SK_ColorBLACK, SK_ColorRED, SK_ColorBLUE };
116 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
117 fColors[i] = colors[i];
118 }
119 }
120
121 protected:
onShortName()122 SkString onShortName() override {
123 return SkString("gammagradienttext");
124 }
125
onISize()126 SkISize onISize() override {
127 return SkISize::Make(300, 300);
128 }
129
onOnceBeforeDraw()130 void onOnceBeforeDraw() override {
131 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
132 fShaders[i] = make_gradient(fColors[i]);
133 }
134 }
135
onDraw(SkCanvas * canvas)136 void onDraw(SkCanvas* canvas) override {
137 SkPaint paint;
138 paint.setAntiAlias(true);
139 SkFont font(SkTypeface::MakeFromName("serif", SkFontStyle::Italic()), 18);
140 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
141
142 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
143 draw_pair(canvas, font, fColors[i], fShaders[i]);
144 canvas->translate(0, 80);
145 }
146 }
147
148 private:
149 typedef skiagm::GM INHERITED;
150 };
151
152 DEF_GM( return new GammaShaderTextGM; )
153