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