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
15static 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
22static bool setFont(SkPaint* paint, const char name[]) {
23    paint->setTypeface(SkTypeface::MakeFromName(name, SkFontStyle()));
24    return SkToBool(paint->getTypeface());
25}
26
27/**
28   Test a set of clipping problems discovered while writing blitAntiRect,
29   and test all the code paths through the clipping blitters.
30   Each region should show as a blue center surrounded by a 2px green
31   border, with no red.
32*/
33
34#define HEIGHT 480
35
36class GammaTextGM : public skiagm::GM {
37protected:
38    SkString onShortName() override {
39        SkString name("gammatext");
40        name.append(sk_tool_utils::major_platform_os_name());
41        return name;
42    }
43
44    SkISize onISize() override {
45        return SkISize::Make(1024, HEIGHT);
46    }
47
48    static void drawGrad(SkCanvas* canvas) {
49        const SkPoint pts[] = { { 0, 0 }, { 0, SkIntToScalar(HEIGHT) } };
50
51        canvas->clear(SK_ColorRED);
52        SkPaint paint;
53        paint.setShader(make_heatGradient(pts));
54        SkRect r = { 0, 0, SkIntToScalar(1024), SkIntToScalar(HEIGHT) };
55        canvas->drawRect(r, paint);
56    }
57
58    void onDraw(SkCanvas* canvas) override {
59        drawGrad(canvas);
60
61        const SkColor fg[] = {
62            0xFFFFFFFF,
63            0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF,
64            0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
65            0xFF000000,
66        };
67
68        const char* text = "Hamburgefons";
69        size_t len = strlen(text);
70
71        SkPaint paint;
72        setFont(&paint, sk_tool_utils::platform_font_name("serif"));
73        paint.setTextSize(SkIntToScalar(16));
74        paint.setAntiAlias(true);
75        paint.setLCDRenderText(true);
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->drawText(text, len, x, y, paint);
85                y += paint.getTextSize() * 2;
86            }
87            x += SkIntToScalar(1024) / SK_ARRAY_COUNT(fg);
88        }
89    }
90
91private:
92    typedef skiagm::GM INHERITED;
93};
94
95DEF_GM( return new GammaTextGM; )
96
97//////////////////////////////////////////////////////////////////////////////
98
99static 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, SkShader::kClamp_TileMode);
105}
106
107static void set_face(SkPaint* paint) {
108    paint->setTypeface(SkTypeface::MakeFromName("serif",
109                           SkFontStyle::FromOldStyle(SkTypeface::kItalic)));
110}
111
112static void draw_pair(SkCanvas* canvas, SkPaint* paint, const sk_sp<SkShader>& shader) {
113    const char text[] = "Now is the time for all good";
114    const size_t len = strlen(text);
115
116    paint->setShader(nullptr);
117    canvas->drawText(text, len, 10, 20, *paint);
118    paint->setShader(SkShader::MakeColorShader(paint->getColor()));
119    canvas->drawText(text, len, 10, 40, *paint);
120    paint->setShader(shader);
121    canvas->drawText(text, len, 10, 60, *paint);
122}
123
124class GammaShaderTextGM : public skiagm::GM {
125    sk_sp<SkShader> fShaders[3];
126    SkColor fColors[3];
127
128public:
129    GammaShaderTextGM() {
130        const SkColor colors[] = { SK_ColorBLACK, SK_ColorRED, SK_ColorBLUE };
131        for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
132            fColors[i] = colors[i];
133        }
134    }
135
136protected:
137    SkString onShortName() override {
138        return SkString("gammagradienttext");
139    }
140
141    SkISize onISize() override {
142        return SkISize::Make(300, 300);
143    }
144
145    void onOnceBeforeDraw() override {
146        for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
147            fShaders[i] = make_gradient(fColors[i]);
148        }
149    }
150
151    void onDraw(SkCanvas* canvas) override {
152        SkPaint paint;
153        paint.setAntiAlias(true);
154        paint.setLCDRenderText(true);
155        paint.setTextSize(18);
156        set_face(&paint);
157
158        for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
159            paint.setColor(fColors[i]);
160            draw_pair(canvas, &paint, fShaders[i]);
161            canvas->translate(0, 80);
162        }
163    }
164
165private:
166    typedef skiagm::GM INHERITED;
167};
168
169DEF_GM( return new GammaShaderTextGM; )
170