blob: 359f62dd39467a5a9d16c7d2b3958704c11fb812 [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"
9#include "SkCanvas.h"
10#include "SkPaint.h"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkPath.h"
schenney@chromium.org4da06ab2011-12-20 15:14:18 +000012#include "SkRandom.h"
13
14namespace skiagm {
15
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000016class QuadPathGM : public GM {
17public:
18 QuadPathGM() {}
19
20protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000021
mtklein36352bf2015-03-25 18:17:31 -070022 SkString onShortName() override {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000023 return SkString("quadpath");
24 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000025
mtklein36352bf2015-03-25 18:17:31 -070026 SkISize onISize() override { return SkISize::Make(1240, 390); }
rmistry@google.comd6176b02012-08-23 18:14:13 +000027
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000028 void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
29 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
30 SkPaint::Style style, SkPath::FillType fill,
31 SkScalar strokeWidth) {
32 path.setFillType(fill);
33 SkPaint paint;
34 paint.setStrokeCap(cap);
35 paint.setStrokeWidth(strokeWidth);
36 paint.setStrokeJoin(join);
37 paint.setColor(color);
38 paint.setStyle(style);
39 canvas->save();
40 canvas->clipRect(clip);
41 canvas->drawPath(path, paint);
42 canvas->restore();
43 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000044
mtklein36352bf2015-03-25 18:17:31 -070045 void onDraw(SkCanvas* canvas) override {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000046 struct FillAndName {
47 SkPath::FillType fFill;
48 const char* fName;
49 };
50 static const FillAndName gFills[] = {
51 {SkPath::kWinding_FillType, "Winding"},
52 {SkPath::kEvenOdd_FillType, "Even / Odd"},
53 {SkPath::kInverseWinding_FillType, "Inverse Winding"},
54 {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
55 };
56 struct StyleAndName {
57 SkPaint::Style fStyle;
58 const char* fName;
59 };
60 static const StyleAndName gStyles[] = {
61 {SkPaint::kFill_Style, "Fill"},
62 {SkPaint::kStroke_Style, "Stroke"},
63 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
64 };
65 struct CapAndName {
66 SkPaint::Cap fCap;
67 SkPaint::Join fJoin;
68 const char* fName;
69 };
70 static const CapAndName gCaps[] = {
71 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
72 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
73 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
74 };
75 struct PathAndName {
76 SkPath fPath;
77 const char* fName;
78 };
79 PathAndName path;
80 path.fPath.moveTo(25*SK_Scalar1, 10*SK_Scalar1);
81 path.fPath.quadTo(50*SK_Scalar1, 20*SK_Scalar1,
82 75*SK_Scalar1, 10*SK_Scalar1);
83 path.fName = "moveTo-quad";
84
85 SkPaint titlePaint;
86 titlePaint.setColor(SK_ColorBLACK);
87 titlePaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070088 sk_tool_utils::set_portable_typeface(&titlePaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000089 titlePaint.setTextSize(15 * SK_Scalar1);
90 const char title[] = "Quad Drawn Into Rectangle Clips With "
91 "Indicated Style, Fill and Linecaps, with stroke width 10";
92 canvas->drawText(title, strlen(title),
93 20 * SK_Scalar1,
94 20 * SK_Scalar1,
95 titlePaint);
96
scroggof9d61012014-12-15 12:54:51 -080097 SkRandom rand;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000098 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
99 canvas->save();
100 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
101 canvas->save();
102 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
103 if (0 < cap) {
104 canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0);
105 }
106 canvas->save();
107 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
108 if (0 < fill) {
109 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
110 }
111 canvas->save();
112 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
113 if (0 < style) {
114 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
115 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000116
caryclark5df05752015-07-15 11:57:00 -0700117 SkColor color = sk_tool_utils::color_to_565(0xff007000);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000118 this->drawPath(path.fPath, canvas, color, rect,
119 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
120 gFills[fill].fFill, SK_Scalar1*10);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000121
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000122 SkPaint rectPaint;
123 rectPaint.setColor(SK_ColorBLACK);
124 rectPaint.setStyle(SkPaint::kStroke_Style);
125 rectPaint.setStrokeWidth(-1);
126 rectPaint.setAntiAlias(true);
127 canvas->drawRect(rect, rectPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000128
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000129 SkPaint labelPaint;
130 labelPaint.setColor(color);
131 labelPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700132 sk_tool_utils::set_portable_typeface(&labelPaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000133 labelPaint.setTextSize(10 * SK_Scalar1);
134 canvas->drawText(gStyles[style].fName,
135 strlen(gStyles[style].fName),
136 0, rect.height() + 12 * SK_Scalar1,
137 labelPaint);
138 canvas->drawText(gFills[fill].fName,
139 strlen(gFills[fill].fName),
140 0, rect.height() + 24 * SK_Scalar1,
141 labelPaint);
142 canvas->drawText(gCaps[cap].fName,
143 strlen(gCaps[cap].fName),
144 0, rect.height() + 36 * SK_Scalar1,
145 labelPaint);
146 }
147 canvas->restore();
148 }
149 canvas->restore();
150 }
151 canvas->restore();
152 canvas->restore();
153 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000154
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000155private:
156 typedef GM INHERITED;
157};
158
159class QuadClosePathGM : public GM {
160public:
161 QuadClosePathGM() {}
162
163protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000164
mtklein36352bf2015-03-25 18:17:31 -0700165 SkString onShortName() override {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000166 return SkString("quadclosepath");
167 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000168
mtklein36352bf2015-03-25 18:17:31 -0700169 SkISize onISize() override { return SkISize::Make(1240, 390); }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000170
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000171 void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
172 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
173 SkPaint::Style style, SkPath::FillType fill,
174 SkScalar strokeWidth) {
175 path.setFillType(fill);
176 SkPaint paint;
177 paint.setStrokeCap(cap);
178 paint.setStrokeWidth(strokeWidth);
179 paint.setStrokeJoin(join);
180 paint.setColor(color);
181 paint.setStyle(style);
182 canvas->save();
183 canvas->clipRect(clip);
184 canvas->drawPath(path, paint);
185 canvas->restore();
186 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000187
mtklein36352bf2015-03-25 18:17:31 -0700188 void onDraw(SkCanvas* canvas) override {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000189 struct FillAndName {
190 SkPath::FillType fFill;
191 const char* fName;
192 };
193 static const FillAndName gFills[] = {
194 {SkPath::kWinding_FillType, "Winding"},
195 {SkPath::kEvenOdd_FillType, "Even / Odd"},
196 {SkPath::kInverseWinding_FillType, "Inverse Winding"},
197 {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
198 };
199 struct StyleAndName {
200 SkPaint::Style fStyle;
201 const char* fName;
202 };
203 static const StyleAndName gStyles[] = {
204 {SkPaint::kFill_Style, "Fill"},
205 {SkPaint::kStroke_Style, "Stroke"},
206 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
207 };
208 struct CapAndName {
209 SkPaint::Cap fCap;
210 SkPaint::Join fJoin;
211 const char* fName;
212 };
213 static const CapAndName gCaps[] = {
214 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
215 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
216 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
217 };
218 struct PathAndName {
219 SkPath fPath;
220 const char* fName;
221 };
222 PathAndName path;
223 path.fPath.moveTo(25*SK_Scalar1, 10*SK_Scalar1);
224 path.fPath.quadTo(50*SK_Scalar1, 20*SK_Scalar1,
225 75*SK_Scalar1, 10*SK_Scalar1);
226 path.fPath.close();
227 path.fName = "moveTo-quad-close";
228
229 SkPaint titlePaint;
230 titlePaint.setColor(SK_ColorBLACK);
231 titlePaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700232 sk_tool_utils::set_portable_typeface(&titlePaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000233 titlePaint.setTextSize(15 * SK_Scalar1);
234 const char title[] = "Quad Closed Drawn Into Rectangle Clips With "
235 "Indicated Style, Fill and Linecaps, with stroke width 10";
236 canvas->drawText(title, strlen(title),
237 20 * SK_Scalar1,
238 20 * SK_Scalar1,
239 titlePaint);
240
scroggof9d61012014-12-15 12:54:51 -0800241 SkRandom rand;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000242 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
243 canvas->save();
244 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
245 canvas->save();
246 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
247 if (0 < cap) {
248 canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0);
249 }
250 canvas->save();
251 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
252 if (0 < fill) {
253 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
254 }
255 canvas->save();
256 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
257 if (0 < style) {
258 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
259 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000260
caryclark5df05752015-07-15 11:57:00 -0700261 SkColor color = sk_tool_utils::color_to_565(0xff007000);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000262 this->drawPath(path.fPath, canvas, color, rect,
263 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
264 gFills[fill].fFill, SK_Scalar1*10);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000265
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000266 SkPaint rectPaint;
267 rectPaint.setColor(SK_ColorBLACK);
268 rectPaint.setStyle(SkPaint::kStroke_Style);
269 rectPaint.setStrokeWidth(-1);
270 rectPaint.setAntiAlias(true);
271 canvas->drawRect(rect, rectPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000272
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000273 SkPaint labelPaint;
274 labelPaint.setColor(color);
275 labelPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700276 sk_tool_utils::set_portable_typeface(&labelPaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000277 labelPaint.setTextSize(10 * SK_Scalar1);
278 canvas->drawText(gStyles[style].fName,
279 strlen(gStyles[style].fName),
280 0, rect.height() + 12 * SK_Scalar1,
281 labelPaint);
282 canvas->drawText(gFills[fill].fName,
283 strlen(gFills[fill].fName),
284 0, rect.height() + 24 * SK_Scalar1,
285 labelPaint);
286 canvas->drawText(gCaps[cap].fName,
287 strlen(gCaps[cap].fName),
288 0, rect.height() + 36 * SK_Scalar1,
289 labelPaint);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000290 }
291 canvas->restore();
292 }
293 canvas->restore();
294 }
295 canvas->restore();
296 canvas->restore();
297 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000298
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000299private:
300 typedef GM INHERITED;
301};
302
303//////////////////////////////////////////////////////////////////////////////
304
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000305static GM* QuadPathFactory(void*) { return new QuadPathGM; }
306static GMRegistry regQuadPath(QuadPathFactory);
307
308static GM* QuadClosePathFactory(void*) { return new QuadClosePathGM; }
309static GMRegistry regQuadClosePath(QuadClosePathFactory);
310
schenney@chromium.org0b71f6c2012-01-04 00:18:53 +0000311}