blob: 46763a9e3490588538c2aae8f57c18bd9c3b19b5 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkPaint.h"
11#include "include/core/SkPath.h"
12#include "include/utils/SkRandom.h"
13#include "tools/ToolUtils.h"
schenney@chromium.org4da06ab2011-12-20 15:14:18 +000014
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;
Mike Kleinea3f0142019-03-20 11:12:10 -050087 SkFont font(ToolUtils::create_portable_typeface(), 15);
88 SkFont labelFont(ToolUtils::create_portable_typeface(), 10);
Hal Canarydf2d27e2019-01-08 09:38:02 -050089
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000090 const char title[] = "Quad Drawn Into Rectangle Clips With "
91 "Indicated Style, Fill and Linecaps, with stroke width 10";
Hal Canarydf2d27e2019-01-08 09:38:02 -050092 canvas->drawString(title, 20.0f, 20.0f, font, titlePaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000093
scroggof9d61012014-12-15 12:54:51 -080094 SkRandom rand;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000095 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
96 canvas->save();
97 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
98 canvas->save();
99 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
100 if (0 < cap) {
101 canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0);
102 }
103 canvas->save();
104 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
105 if (0 < fill) {
106 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
107 }
108 canvas->save();
109 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
110 if (0 < style) {
111 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
112 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000113
Mike Kleinea3f0142019-03-20 11:12:10 -0500114 SkColor color = ToolUtils::color_to_565(0xff007000);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000115 this->drawPath(path.fPath, canvas, color, rect,
116 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
117 gFills[fill].fFill, SK_Scalar1*10);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000118
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000119 SkPaint rectPaint;
120 rectPaint.setColor(SK_ColorBLACK);
121 rectPaint.setStyle(SkPaint::kStroke_Style);
122 rectPaint.setStrokeWidth(-1);
123 rectPaint.setAntiAlias(true);
124 canvas->drawRect(rect, rectPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000125
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000126 SkPaint labelPaint;
127 labelPaint.setColor(color);
Hal Canarydf2d27e2019-01-08 09:38:02 -0500128 canvas->drawString(gStyles[style].fName, 0, rect.height() + 12.0f,
129 labelFont, labelPaint);
130 canvas->drawString(gFills[fill].fName, 0, rect.height() + 24.0f,
131 labelFont, labelPaint);
132 canvas->drawString(gCaps[cap].fName, 0, rect.height() + 36.0f,
133 labelFont, labelPaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000134 }
135 canvas->restore();
136 }
137 canvas->restore();
138 }
139 canvas->restore();
140 canvas->restore();
141 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000142
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000143private:
144 typedef GM INHERITED;
145};
146
147class QuadClosePathGM : public GM {
148public:
149 QuadClosePathGM() {}
150
151protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000152
mtklein36352bf2015-03-25 18:17:31 -0700153 SkString onShortName() override {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000154 return SkString("quadclosepath");
155 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000156
mtklein36352bf2015-03-25 18:17:31 -0700157 SkISize onISize() override { return SkISize::Make(1240, 390); }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000158
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000159 void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
160 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
161 SkPaint::Style style, SkPath::FillType fill,
162 SkScalar strokeWidth) {
163 path.setFillType(fill);
164 SkPaint paint;
165 paint.setStrokeCap(cap);
166 paint.setStrokeWidth(strokeWidth);
167 paint.setStrokeJoin(join);
168 paint.setColor(color);
169 paint.setStyle(style);
170 canvas->save();
171 canvas->clipRect(clip);
172 canvas->drawPath(path, paint);
173 canvas->restore();
174 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000175
mtklein36352bf2015-03-25 18:17:31 -0700176 void onDraw(SkCanvas* canvas) override {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000177 struct FillAndName {
178 SkPath::FillType fFill;
179 const char* fName;
180 };
mtkleindbfd7ab2016-09-01 11:24:54 -0700181 constexpr FillAndName gFills[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000182 {SkPath::kWinding_FillType, "Winding"},
183 {SkPath::kEvenOdd_FillType, "Even / Odd"},
184 {SkPath::kInverseWinding_FillType, "Inverse Winding"},
185 {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
186 };
187 struct StyleAndName {
188 SkPaint::Style fStyle;
189 const char* fName;
190 };
mtkleindbfd7ab2016-09-01 11:24:54 -0700191 constexpr StyleAndName gStyles[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000192 {SkPaint::kFill_Style, "Fill"},
193 {SkPaint::kStroke_Style, "Stroke"},
194 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
195 };
196 struct CapAndName {
197 SkPaint::Cap fCap;
198 SkPaint::Join fJoin;
199 const char* fName;
200 };
mtkleindbfd7ab2016-09-01 11:24:54 -0700201 constexpr CapAndName gCaps[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000202 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
203 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
204 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
205 };
206 struct PathAndName {
207 SkPath fPath;
208 const char* fName;
209 };
210 PathAndName path;
211 path.fPath.moveTo(25*SK_Scalar1, 10*SK_Scalar1);
212 path.fPath.quadTo(50*SK_Scalar1, 20*SK_Scalar1,
213 75*SK_Scalar1, 10*SK_Scalar1);
214 path.fPath.close();
215 path.fName = "moveTo-quad-close";
216
217 SkPaint titlePaint;
Mike Kleinea3f0142019-03-20 11:12:10 -0500218 SkFont font(ToolUtils::create_portable_typeface(), 15);
219 SkFont labelFont(ToolUtils::create_portable_typeface(), 10);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000220 const char title[] = "Quad Closed Drawn Into Rectangle Clips With "
221 "Indicated Style, Fill and Linecaps, with stroke width 10";
Hal Canarydf2d27e2019-01-08 09:38:02 -0500222 canvas->drawString(title, 20.0f, 20.0f, font, titlePaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000223
scroggof9d61012014-12-15 12:54:51 -0800224 SkRandom rand;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000225 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
226 canvas->save();
227 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
228 canvas->save();
229 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
230 if (0 < cap) {
231 canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0);
232 }
233 canvas->save();
234 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
235 if (0 < fill) {
236 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
237 }
238 canvas->save();
239 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
240 if (0 < style) {
241 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
242 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000243
Mike Kleinea3f0142019-03-20 11:12:10 -0500244 SkColor color = ToolUtils::color_to_565(0xff007000);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000245 this->drawPath(path.fPath, canvas, color, rect,
246 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
247 gFills[fill].fFill, SK_Scalar1*10);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000248
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000249 SkPaint rectPaint;
250 rectPaint.setColor(SK_ColorBLACK);
251 rectPaint.setStyle(SkPaint::kStroke_Style);
252 rectPaint.setStrokeWidth(-1);
253 rectPaint.setAntiAlias(true);
254 canvas->drawRect(rect, rectPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000255
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000256 SkPaint labelPaint;
257 labelPaint.setColor(color);
Hal Canarydf2d27e2019-01-08 09:38:02 -0500258 canvas->drawString(gStyles[style].fName, 0, rect.height() + 12.0f,
259 labelFont, labelPaint);
260 canvas->drawString(gFills[fill].fName, 0, rect.height() + 24.0f,
261 labelFont, labelPaint);
262 canvas->drawString(gCaps[cap].fName, 0, rect.height() + 36.0f,
263 labelFont, labelPaint);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000264 }
265 canvas->restore();
266 }
267 canvas->restore();
268 }
269 canvas->restore();
270 canvas->restore();
271 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000272
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000273private:
274 typedef GM INHERITED;
275};
276
277//////////////////////////////////////////////////////////////////////////////
278
Hal Canarye964c182019-01-23 10:22:01 -0500279DEF_GM( return new QuadPathGM; )
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000280
Hal Canarye964c182019-01-23 10:22:01 -0500281DEF_GM( return new QuadClosePathGM; )
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000282
schenney@chromium.org0b71f6c2012-01-04 00:18:53 +0000283}