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