blob: ed8c82929d60d74dfaa0dce48c95c581b71ed5d6 [file] [log] [blame]
reed@google.comed3ee642012-02-14 16:12:49 +00001/*
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 "SkCanvas.h"
10#include "SkPath.h"
11#include "SkGradientShader.h"
12#include "SkTypeface.h"
13
14static SkShader* make_heatGradient(const SkPoint pts[2]) {
reed@google.comed3ee642012-02-14 16:12:49 +000015 const SkColor bw[] = { SK_ColorBLACK, SK_ColorWHITE };
16
17 return SkGradientShader::CreateLinear(pts, bw, NULL,
18 SK_ARRAY_COUNT(bw),
19 SkShader::kClamp_TileMode);
20}
21
22static bool setFont(SkPaint* paint, const char name[]) {
Cary Clark992c7b02014-07-31 08:58:44 -040023 SkTypeface* tf = sk_tool_utils::create_portable_typeface(name, SkTypeface::kNormal);
reed@google.comed3ee642012-02-14 16:12:49 +000024 if (tf) {
25 paint->setTypeface(tf)->unref();
26 return true;
27 }
reed@google.comce151d02012-03-08 16:10:57 +000028 return false;
reed@google.comed3ee642012-02-14 16:12:49 +000029}
30
reed@google.comed3ee642012-02-14 16:12:49 +000031/**
32 Test a set of clipping problems discovered while writing blitAntiRect,
33 and test all the code paths through the clipping blitters.
34 Each region should show as a blue center surrounded by a 2px green
35 border, with no red.
36*/
rmistry@google.comd6176b02012-08-23 18:14:13 +000037
reed@google.comed3ee642012-02-14 16:12:49 +000038#define HEIGHT 480
39
reed8367b8c2014-08-22 08:30:20 -070040class GammaTextGM : public skiagm::GM {
reed@google.comed3ee642012-02-14 16:12:49 +000041protected:
mtklein36352bf2015-03-25 18:17:31 -070042 SkString onShortName() override {
reed@google.comed3ee642012-02-14 16:12:49 +000043 return SkString("gammatext");
44 }
45
mtklein36352bf2015-03-25 18:17:31 -070046 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070047 return SkISize::Make(1024, HEIGHT);
reed@google.comed3ee642012-02-14 16:12:49 +000048 }
49
50 static void drawGrad(SkCanvas* canvas) {
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +000051 SkPoint pts[] = { { 0, 0 }, { 0, SkIntToScalar(HEIGHT) } };
reed@google.comed3ee642012-02-14 16:12:49 +000052 SkShader* s = make_heatGradient(pts);
rmistry@google.comd6176b02012-08-23 18:14:13 +000053
reed@google.comed3ee642012-02-14 16:12:49 +000054 canvas->clear(SK_ColorRED);
55 SkPaint paint;
56 paint.setShader(s)->unref();
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +000057 SkRect r = { 0, 0, SkIntToScalar(1024), SkIntToScalar(HEIGHT) };
reed@google.comed3ee642012-02-14 16:12:49 +000058 canvas->drawRect(r, paint);
59 }
60
mtklein36352bf2015-03-25 18:17:31 -070061 void onDraw(SkCanvas* canvas) override {
reed@google.comed3ee642012-02-14 16:12:49 +000062 drawGrad(canvas);
63
64 const SkColor fg[] = {
65 0xFFFFFFFF,
66 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF,
67 0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
68 0xFF000000,
69 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000070
reed@google.comed3ee642012-02-14 16:12:49 +000071 const char* text = "Hamburgefons";
72 size_t len = strlen(text);
73
74 SkPaint paint;
75 setFont(&paint, "Times");
76 paint.setTextSize(SkIntToScalar(16));
77 paint.setAntiAlias(true);
78 paint.setLCDRenderText(true);
79
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +000080 SkScalar x = SkIntToScalar(10);
reed@google.comed3ee642012-02-14 16:12:49 +000081 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
82 paint.setColor(fg[i]);
rmistry@google.comd6176b02012-08-23 18:14:13 +000083
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +000084 SkScalar y = SkIntToScalar(40);
85 SkScalar stopy = SkIntToScalar(HEIGHT);
reed@google.comed3ee642012-02-14 16:12:49 +000086 while (y < stopy) {
reed04f7e142014-11-13 12:46:24 -080087 canvas->drawText(text, len, x, y, paint);
reed@google.comed3ee642012-02-14 16:12:49 +000088 y += paint.getTextSize() * 2;
89 }
90 x += SkIntToScalar(1024) / SK_ARRAY_COUNT(fg);
91 }
reed@google.comed3ee642012-02-14 16:12:49 +000092 }
93
94private:
reed8367b8c2014-08-22 08:30:20 -070095 typedef skiagm::GM INHERITED;
reed@google.comed3ee642012-02-14 16:12:49 +000096};
97
bungeman67b21a72015-02-19 07:45:14 -080098DEF_GM( return new GammaTextGM; )
reed8367b8c2014-08-22 08:30:20 -070099
reed@google.comed3ee642012-02-14 16:12:49 +0000100//////////////////////////////////////////////////////////////////////////////
101
reed8367b8c2014-08-22 08:30:20 -0700102static SkShader* make_gradient(SkColor c) {
103 const SkPoint pts[] = { { 0, 0 }, { 240, 0 } };
104 SkColor colors[2];
105 colors[0] = c;
106 colors[1] = SkColorSetA(c, 0);
107 return SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode);
reed@google.comed3ee642012-02-14 16:12:49 +0000108}
reed8367b8c2014-08-22 08:30:20 -0700109
110static void set_face(SkPaint* paint) {
111 SkTypeface* face = SkTypeface::CreateFromName("serif", SkTypeface::kItalic);
112 SkSafeUnref(paint->setTypeface(face));
113}
114
115static void draw_pair(SkCanvas* canvas, SkPaint* paint, SkShader* shader) {
116 const char text[] = "Now is the time for all good";
117 const size_t len = strlen(text);
118
119 paint->setShader(NULL);
120 canvas->drawText(text, len, 10, 20, *paint);
121 paint->setShader(SkShader::CreateColorShader(paint->getColor()))->unref();
122 canvas->drawText(text, len, 10, 40, *paint);
123 paint->setShader(shader);
124 canvas->drawText(text, len, 10, 60, *paint);
125}
126
127class GammaShaderTextGM : public skiagm::GM {
128 SkShader* fShaders[3];
129 SkColor fColors[3];
130
131public:
132 GammaShaderTextGM() {
133 const SkColor colors[] = { SK_ColorBLACK, SK_ColorRED, SK_ColorBLUE };
134 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
135 fShaders[i] = NULL;
136 fColors[i] = colors[i];
137 }
138 }
139
mtklein36352bf2015-03-25 18:17:31 -0700140 ~GammaShaderTextGM() override {
reed8367b8c2014-08-22 08:30:20 -0700141 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
142 SkSafeUnref(fShaders[i]);
143 }
144 }
145
146protected:
mtklein36352bf2015-03-25 18:17:31 -0700147 SkString onShortName() override {
reed8367b8c2014-08-22 08:30:20 -0700148 return SkString("gammagradienttext");
149 }
150
mtklein36352bf2015-03-25 18:17:31 -0700151 SkISize onISize() override {
reed8367b8c2014-08-22 08:30:20 -0700152 return SkISize::Make(300, 300);
153 }
154
mtklein36352bf2015-03-25 18:17:31 -0700155 void onOnceBeforeDraw() override {
reed8367b8c2014-08-22 08:30:20 -0700156 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
157 fShaders[i] = make_gradient(fColors[i]);
158 }
159 }
160
mtklein36352bf2015-03-25 18:17:31 -0700161 void onDraw(SkCanvas* canvas) override {
reed8367b8c2014-08-22 08:30:20 -0700162 SkPaint paint;
163 paint.setAntiAlias(true);
164 paint.setLCDRenderText(true);
165 paint.setTextSize(18);
166 set_face(&paint);
167
168 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
169 paint.setColor(fColors[i]);
170 draw_pair(canvas, &paint, fShaders[i]);
171 canvas->translate(0, 80);
172 }
173 }
174
175private:
176 typedef skiagm::GM INHERITED;
177};
178
179DEF_GM( return new GammaShaderTextGM; )
180