blob: 85709347d13d6fdfabc2c00a21b74522d65644ca [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"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkFont.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPaint.h"
13#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkRect.h"
15#include "include/core/SkScalar.h"
16#include "include/core/SkSize.h"
17#include "include/core/SkString.h"
18#include "include/core/SkTypeface.h"
19#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/utils/SkRandom.h"
21#include "tools/ToolUtils.h"
schenney@chromium.org4da06ab2011-12-20 15:14:18 +000022
23namespace skiagm {
24
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000025class QuadPathGM : public GM {
26public:
27 QuadPathGM() {}
28
29protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000030
mtklein36352bf2015-03-25 18:17:31 -070031 SkString onShortName() override {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000032 return SkString("quadpath");
33 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000034
mtklein36352bf2015-03-25 18:17:31 -070035 SkISize onISize() override { return SkISize::Make(1240, 390); }
rmistry@google.comd6176b02012-08-23 18:14:13 +000036
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000037 void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
38 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
Mike Reed7d34dc72019-11-26 12:17:17 -050039 SkPaint::Style style, SkPathFillType fill,
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000040 SkScalar strokeWidth) {
41 path.setFillType(fill);
42 SkPaint paint;
43 paint.setStrokeCap(cap);
44 paint.setStrokeWidth(strokeWidth);
45 paint.setStrokeJoin(join);
46 paint.setColor(color);
47 paint.setStyle(style);
48 canvas->save();
49 canvas->clipRect(clip);
50 canvas->drawPath(path, paint);
51 canvas->restore();
52 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000053
mtklein36352bf2015-03-25 18:17:31 -070054 void onDraw(SkCanvas* canvas) override {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000055 struct FillAndName {
Mike Reed7d34dc72019-11-26 12:17:17 -050056 SkPathFillType fFill;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000057 const char* fName;
58 };
mtkleindbfd7ab2016-09-01 11:24:54 -070059 constexpr FillAndName gFills[] = {
Mike Reed7d34dc72019-11-26 12:17:17 -050060 {SkPathFillType::kWinding, "Winding"},
61 {SkPathFillType::kEvenOdd, "Even / Odd"},
62 {SkPathFillType::kInverseWinding, "Inverse Winding"},
63 {SkPathFillType::kInverseEvenOdd, "Inverse Even / Odd"},
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000064 };
65 struct StyleAndName {
66 SkPaint::Style fStyle;
67 const char* fName;
68 };
mtkleindbfd7ab2016-09-01 11:24:54 -070069 constexpr StyleAndName gStyles[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000070 {SkPaint::kFill_Style, "Fill"},
71 {SkPaint::kStroke_Style, "Stroke"},
72 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
73 };
74 struct CapAndName {
75 SkPaint::Cap fCap;
76 SkPaint::Join fJoin;
77 const char* fName;
78 };
mtkleindbfd7ab2016-09-01 11:24:54 -070079 constexpr CapAndName gCaps[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000080 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
81 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
82 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
83 };
84 struct PathAndName {
85 SkPath fPath;
86 const char* fName;
87 };
88 PathAndName path;
89 path.fPath.moveTo(25*SK_Scalar1, 10*SK_Scalar1);
90 path.fPath.quadTo(50*SK_Scalar1, 20*SK_Scalar1,
91 75*SK_Scalar1, 10*SK_Scalar1);
92 path.fName = "moveTo-quad";
93
94 SkPaint titlePaint;
Mike Kleinea3f0142019-03-20 11:12:10 -050095 SkFont font(ToolUtils::create_portable_typeface(), 15);
96 SkFont labelFont(ToolUtils::create_portable_typeface(), 10);
Hal Canarydf2d27e2019-01-08 09:38:02 -050097
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000098 const char title[] = "Quad Drawn Into Rectangle Clips With "
99 "Indicated Style, Fill and Linecaps, with stroke width 10";
Hal Canarydf2d27e2019-01-08 09:38:02 -0500100 canvas->drawString(title, 20.0f, 20.0f, font, titlePaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000101
scroggof9d61012014-12-15 12:54:51 -0800102 SkRandom rand;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000103 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
104 canvas->save();
105 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
106 canvas->save();
107 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
108 if (0 < cap) {
109 canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0);
110 }
111 canvas->save();
112 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
113 if (0 < fill) {
114 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
115 }
116 canvas->save();
117 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
118 if (0 < style) {
119 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
120 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000121
Mike Kleinea3f0142019-03-20 11:12:10 -0500122 SkColor color = ToolUtils::color_to_565(0xff007000);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000123 this->drawPath(path.fPath, canvas, color, rect,
124 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
125 gFills[fill].fFill, SK_Scalar1*10);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000126
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000127 SkPaint rectPaint;
128 rectPaint.setColor(SK_ColorBLACK);
129 rectPaint.setStyle(SkPaint::kStroke_Style);
130 rectPaint.setStrokeWidth(-1);
131 rectPaint.setAntiAlias(true);
132 canvas->drawRect(rect, rectPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000133
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000134 SkPaint labelPaint;
135 labelPaint.setColor(color);
Hal Canarydf2d27e2019-01-08 09:38:02 -0500136 canvas->drawString(gStyles[style].fName, 0, rect.height() + 12.0f,
137 labelFont, labelPaint);
138 canvas->drawString(gFills[fill].fName, 0, rect.height() + 24.0f,
139 labelFont, labelPaint);
140 canvas->drawString(gCaps[cap].fName, 0, rect.height() + 36.0f,
141 labelFont, labelPaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000142 }
143 canvas->restore();
144 }
145 canvas->restore();
146 }
147 canvas->restore();
148 canvas->restore();
149 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000150
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000151private:
152 typedef GM INHERITED;
153};
154
155class QuadClosePathGM : public GM {
156public:
157 QuadClosePathGM() {}
158
159protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000160
mtklein36352bf2015-03-25 18:17:31 -0700161 SkString onShortName() override {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000162 return SkString("quadclosepath");
163 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000164
mtklein36352bf2015-03-25 18:17:31 -0700165 SkISize onISize() override { return SkISize::Make(1240, 390); }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000166
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000167 void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
168 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
Mike Reed7d34dc72019-11-26 12:17:17 -0500169 SkPaint::Style style, SkPathFillType fill,
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000170 SkScalar strokeWidth) {
171 path.setFillType(fill);
172 SkPaint paint;
173 paint.setStrokeCap(cap);
174 paint.setStrokeWidth(strokeWidth);
175 paint.setStrokeJoin(join);
176 paint.setColor(color);
177 paint.setStyle(style);
178 canvas->save();
179 canvas->clipRect(clip);
180 canvas->drawPath(path, paint);
181 canvas->restore();
182 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000183
mtklein36352bf2015-03-25 18:17:31 -0700184 void onDraw(SkCanvas* canvas) override {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000185 struct FillAndName {
Mike Reed7d34dc72019-11-26 12:17:17 -0500186 SkPathFillType fFill;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000187 const char* fName;
188 };
mtkleindbfd7ab2016-09-01 11:24:54 -0700189 constexpr FillAndName gFills[] = {
Mike Reed7d34dc72019-11-26 12:17:17 -0500190 {SkPathFillType::kWinding, "Winding"},
191 {SkPathFillType::kEvenOdd, "Even / Odd"},
192 {SkPathFillType::kInverseWinding, "Inverse Winding"},
193 {SkPathFillType::kInverseEvenOdd, "Inverse Even / Odd"},
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000194 };
195 struct StyleAndName {
196 SkPaint::Style fStyle;
197 const char* fName;
198 };
mtkleindbfd7ab2016-09-01 11:24:54 -0700199 constexpr StyleAndName gStyles[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000200 {SkPaint::kFill_Style, "Fill"},
201 {SkPaint::kStroke_Style, "Stroke"},
202 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
203 };
204 struct CapAndName {
205 SkPaint::Cap fCap;
206 SkPaint::Join fJoin;
207 const char* fName;
208 };
mtkleindbfd7ab2016-09-01 11:24:54 -0700209 constexpr CapAndName gCaps[] = {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000210 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
211 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
212 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
213 };
214 struct PathAndName {
215 SkPath fPath;
216 const char* fName;
217 };
218 PathAndName path;
219 path.fPath.moveTo(25*SK_Scalar1, 10*SK_Scalar1);
220 path.fPath.quadTo(50*SK_Scalar1, 20*SK_Scalar1,
221 75*SK_Scalar1, 10*SK_Scalar1);
222 path.fPath.close();
223 path.fName = "moveTo-quad-close";
224
225 SkPaint titlePaint;
Mike Kleinea3f0142019-03-20 11:12:10 -0500226 SkFont font(ToolUtils::create_portable_typeface(), 15);
227 SkFont labelFont(ToolUtils::create_portable_typeface(), 10);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000228 const char title[] = "Quad Closed Drawn Into Rectangle Clips With "
229 "Indicated Style, Fill and Linecaps, with stroke width 10";
Hal Canarydf2d27e2019-01-08 09:38:02 -0500230 canvas->drawString(title, 20.0f, 20.0f, font, titlePaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000231
scroggof9d61012014-12-15 12:54:51 -0800232 SkRandom rand;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000233 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
234 canvas->save();
235 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
236 canvas->save();
237 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
238 if (0 < cap) {
239 canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0);
240 }
241 canvas->save();
242 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
243 if (0 < fill) {
244 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
245 }
246 canvas->save();
247 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
248 if (0 < style) {
249 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
250 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000251
Mike Kleinea3f0142019-03-20 11:12:10 -0500252 SkColor color = ToolUtils::color_to_565(0xff007000);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000253 this->drawPath(path.fPath, canvas, color, rect,
254 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
255 gFills[fill].fFill, SK_Scalar1*10);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000256
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000257 SkPaint rectPaint;
258 rectPaint.setColor(SK_ColorBLACK);
259 rectPaint.setStyle(SkPaint::kStroke_Style);
260 rectPaint.setStrokeWidth(-1);
261 rectPaint.setAntiAlias(true);
262 canvas->drawRect(rect, rectPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000263
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000264 SkPaint labelPaint;
265 labelPaint.setColor(color);
Hal Canarydf2d27e2019-01-08 09:38:02 -0500266 canvas->drawString(gStyles[style].fName, 0, rect.height() + 12.0f,
267 labelFont, labelPaint);
268 canvas->drawString(gFills[fill].fName, 0, rect.height() + 24.0f,
269 labelFont, labelPaint);
270 canvas->drawString(gCaps[cap].fName, 0, rect.height() + 36.0f,
271 labelFont, labelPaint);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000272 }
273 canvas->restore();
274 }
275 canvas->restore();
276 }
277 canvas->restore();
278 canvas->restore();
279 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000280
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000281private:
282 typedef GM INHERITED;
283};
284
285//////////////////////////////////////////////////////////////////////////////
286
Hal Canarye964c182019-01-23 10:22:01 -0500287DEF_GM( return new QuadPathGM; )
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000288
Hal Canarye964c182019-01-23 10:22:01 -0500289DEF_GM( return new QuadClosePathGM; )
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000290
schenney@chromium.org0b71f6c2012-01-04 00:18:53 +0000291}