blob: fb106859a4b054b0a8eb008cc995a7ba9259a9bd [file] [log] [blame]
schenney@chromium.org4da06ab2011-12-20 15:14:18 +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 */
bungemand3ebb482015-08-05 13:57:49 -07007
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00008#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
schenney@chromium.org4da06ab2011-12-20 15:14:18 +000010#include "SkCanvas.h"
11#include "SkPaint.h"
bungemand3ebb482015-08-05 13:57:49 -070012#include "SkPath.h"
schenney@chromium.org4da06ab2011-12-20 15:14:18 +000013#include "SkRandom.h"
14
15namespace skiagm {
16
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000017class QuadPathGM : public GM {
18public:
19 QuadPathGM() {}
20
21protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000022
mtklein36352bf2015-03-25 18:17:31 -070023 SkString onShortName() override {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000024 return SkString("quadpath");
25 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000026
mtklein36352bf2015-03-25 18:17:31 -070027 SkISize onISize() override { return SkISize::Make(1240, 390); }
rmistry@google.comd6176b02012-08-23 18:14:13 +000028
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000029 void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
30 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
31 SkPaint::Style style, SkPath::FillType fill,
32 SkScalar strokeWidth) {
33 path.setFillType(fill);
34 SkPaint paint;
35 paint.setStrokeCap(cap);
36 paint.setStrokeWidth(strokeWidth);
37 paint.setStrokeJoin(join);
38 paint.setColor(color);
39 paint.setStyle(style);
40 canvas->save();
41 canvas->clipRect(clip);
42 canvas->drawPath(path, paint);
43 canvas->restore();
44 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000045
mtklein36352bf2015-03-25 18:17:31 -070046 void onDraw(SkCanvas* canvas) override {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000047 struct FillAndName {
48 SkPath::FillType fFill;
49 const char* fName;
50 };
mtkleindbfd7ab2016-09-01 11:24:54 -070051 constexpr FillAndName gFills[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000052 {SkPath::kWinding_FillType, "Winding"},
53 {SkPath::kEvenOdd_FillType, "Even / Odd"},
54 {SkPath::kInverseWinding_FillType, "Inverse Winding"},
55 {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
56 };
57 struct StyleAndName {
58 SkPaint::Style fStyle;
59 const char* fName;
60 };
mtkleindbfd7ab2016-09-01 11:24:54 -070061 constexpr StyleAndName gStyles[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000062 {SkPaint::kFill_Style, "Fill"},
63 {SkPaint::kStroke_Style, "Stroke"},
64 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
65 };
66 struct CapAndName {
67 SkPaint::Cap fCap;
68 SkPaint::Join fJoin;
69 const char* fName;
70 };
mtkleindbfd7ab2016-09-01 11:24:54 -070071 constexpr CapAndName gCaps[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000072 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
73 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
74 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
75 };
76 struct PathAndName {
77 SkPath fPath;
78 const char* fName;
79 };
80 PathAndName path;
81 path.fPath.moveTo(25*SK_Scalar1, 10*SK_Scalar1);
82 path.fPath.quadTo(50*SK_Scalar1, 20*SK_Scalar1,
83 75*SK_Scalar1, 10*SK_Scalar1);
84 path.fName = "moveTo-quad";
85
86 SkPaint titlePaint;
87 titlePaint.setColor(SK_ColorBLACK);
88 titlePaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070089 sk_tool_utils::set_portable_typeface(&titlePaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000090 titlePaint.setTextSize(15 * SK_Scalar1);
91 const char title[] = "Quad Drawn Into Rectangle Clips With "
92 "Indicated Style, Fill and Linecaps, with stroke width 10";
Cary Clark2a475ea2017-04-28 15:35:12 -040093 canvas->drawString(title,
94 20 * SK_Scalar1,
95 20 * SK_Scalar1,
96 titlePaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000097
scroggof9d61012014-12-15 12:54:51 -080098 SkRandom rand;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000099 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
100 canvas->save();
101 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
102 canvas->save();
103 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
104 if (0 < cap) {
105 canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0);
106 }
107 canvas->save();
108 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
109 if (0 < fill) {
110 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
111 }
112 canvas->save();
113 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
114 if (0 < style) {
115 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
116 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000117
caryclark5df05752015-07-15 11:57:00 -0700118 SkColor color = sk_tool_utils::color_to_565(0xff007000);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000119 this->drawPath(path.fPath, canvas, color, rect,
120 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
121 gFills[fill].fFill, SK_Scalar1*10);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000122
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000123 SkPaint rectPaint;
124 rectPaint.setColor(SK_ColorBLACK);
125 rectPaint.setStyle(SkPaint::kStroke_Style);
126 rectPaint.setStrokeWidth(-1);
127 rectPaint.setAntiAlias(true);
128 canvas->drawRect(rect, rectPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000129
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000130 SkPaint labelPaint;
131 labelPaint.setColor(color);
132 labelPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700133 sk_tool_utils::set_portable_typeface(&labelPaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000134 labelPaint.setTextSize(10 * SK_Scalar1);
Cary Clark2a475ea2017-04-28 15:35:12 -0400135 canvas->drawString(gStyles[style].fName,
136 0, rect.height() + 12 * SK_Scalar1,
137 labelPaint);
138 canvas->drawString(gFills[fill].fName,
139 0, rect.height() + 24 * SK_Scalar1,
140 labelPaint);
141 canvas->drawString(gCaps[cap].fName,
142 0, rect.height() + 36 * SK_Scalar1,
143 labelPaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000144 }
145 canvas->restore();
146 }
147 canvas->restore();
148 }
149 canvas->restore();
150 canvas->restore();
151 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000152
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000153private:
154 typedef GM INHERITED;
155};
156
157class QuadClosePathGM : public GM {
158public:
159 QuadClosePathGM() {}
160
161protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000162
mtklein36352bf2015-03-25 18:17:31 -0700163 SkString onShortName() override {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000164 return SkString("quadclosepath");
165 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000166
mtklein36352bf2015-03-25 18:17:31 -0700167 SkISize onISize() override { return SkISize::Make(1240, 390); }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000168
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000169 void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
170 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
171 SkPaint::Style style, SkPath::FillType fill,
172 SkScalar strokeWidth) {
173 path.setFillType(fill);
174 SkPaint paint;
175 paint.setStrokeCap(cap);
176 paint.setStrokeWidth(strokeWidth);
177 paint.setStrokeJoin(join);
178 paint.setColor(color);
179 paint.setStyle(style);
180 canvas->save();
181 canvas->clipRect(clip);
182 canvas->drawPath(path, paint);
183 canvas->restore();
184 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000185
mtklein36352bf2015-03-25 18:17:31 -0700186 void onDraw(SkCanvas* canvas) override {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000187 struct FillAndName {
188 SkPath::FillType fFill;
189 const char* fName;
190 };
mtkleindbfd7ab2016-09-01 11:24:54 -0700191 constexpr FillAndName gFills[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000192 {SkPath::kWinding_FillType, "Winding"},
193 {SkPath::kEvenOdd_FillType, "Even / Odd"},
194 {SkPath::kInverseWinding_FillType, "Inverse Winding"},
195 {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
196 };
197 struct StyleAndName {
198 SkPaint::Style fStyle;
199 const char* fName;
200 };
mtkleindbfd7ab2016-09-01 11:24:54 -0700201 constexpr StyleAndName gStyles[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000202 {SkPaint::kFill_Style, "Fill"},
203 {SkPaint::kStroke_Style, "Stroke"},
204 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
205 };
206 struct CapAndName {
207 SkPaint::Cap fCap;
208 SkPaint::Join fJoin;
209 const char* fName;
210 };
mtkleindbfd7ab2016-09-01 11:24:54 -0700211 constexpr CapAndName gCaps[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000212 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
213 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
214 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
215 };
216 struct PathAndName {
217 SkPath fPath;
218 const char* fName;
219 };
220 PathAndName path;
221 path.fPath.moveTo(25*SK_Scalar1, 10*SK_Scalar1);
222 path.fPath.quadTo(50*SK_Scalar1, 20*SK_Scalar1,
223 75*SK_Scalar1, 10*SK_Scalar1);
224 path.fPath.close();
225 path.fName = "moveTo-quad-close";
226
227 SkPaint titlePaint;
228 titlePaint.setColor(SK_ColorBLACK);
229 titlePaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700230 sk_tool_utils::set_portable_typeface(&titlePaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000231 titlePaint.setTextSize(15 * SK_Scalar1);
232 const char title[] = "Quad Closed Drawn Into Rectangle Clips With "
233 "Indicated Style, Fill and Linecaps, with stroke width 10";
Cary Clark2a475ea2017-04-28 15:35:12 -0400234 canvas->drawString(title,
235 20 * SK_Scalar1,
236 20 * SK_Scalar1,
237 titlePaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000238
scroggof9d61012014-12-15 12:54:51 -0800239 SkRandom rand;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000240 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
241 canvas->save();
242 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
243 canvas->save();
244 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
245 if (0 < cap) {
246 canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0);
247 }
248 canvas->save();
249 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
250 if (0 < fill) {
251 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
252 }
253 canvas->save();
254 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
255 if (0 < style) {
256 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
257 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000258
caryclark5df05752015-07-15 11:57:00 -0700259 SkColor color = sk_tool_utils::color_to_565(0xff007000);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000260 this->drawPath(path.fPath, canvas, color, rect,
261 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
262 gFills[fill].fFill, SK_Scalar1*10);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000263
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000264 SkPaint rectPaint;
265 rectPaint.setColor(SK_ColorBLACK);
266 rectPaint.setStyle(SkPaint::kStroke_Style);
267 rectPaint.setStrokeWidth(-1);
268 rectPaint.setAntiAlias(true);
269 canvas->drawRect(rect, rectPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000270
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000271 SkPaint labelPaint;
272 labelPaint.setColor(color);
273 labelPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700274 sk_tool_utils::set_portable_typeface(&labelPaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000275 labelPaint.setTextSize(10 * SK_Scalar1);
Cary Clark2a475ea2017-04-28 15:35:12 -0400276 canvas->drawString(gStyles[style].fName,
277 0, rect.height() + 12 * SK_Scalar1,
278 labelPaint);
279 canvas->drawString(gFills[fill].fName,
280 0, rect.height() + 24 * SK_Scalar1,
281 labelPaint);
282 canvas->drawString(gCaps[cap].fName,
283 0, rect.height() + 36 * SK_Scalar1,
284 labelPaint);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000285 }
286 canvas->restore();
287 }
288 canvas->restore();
289 }
290 canvas->restore();
291 canvas->restore();
292 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000293
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000294private:
295 typedef GM INHERITED;
296};
297
298//////////////////////////////////////////////////////////////////////////////
299
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000300static GM* QuadPathFactory(void*) { return new QuadPathGM; }
301static GMRegistry regQuadPath(QuadPathFactory);
302
303static GM* QuadClosePathFactory(void*) { return new QuadClosePathGM; }
304static GMRegistry regQuadClosePath(QuadClosePathFactory);
305
schenney@chromium.org0b71f6c2012-01-04 00:18:53 +0000306}