blob: 39417010c1192d8b55185ede320316764d11cf93 [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 */
7#include "gm.h"
8#include "SkCanvas.h"
9#include "SkPaint.h"
10#include "SkRandom.h"
11
reed@google.com22999c62013-05-23 19:30:48 +000012// skbug.com/1316 shows that this cubic, when slightly clipped, creates big
13// (incorrect) changes to its control points.
14class ClippedCubicGM : public skiagm::GM {
15public:
16 ClippedCubicGM() {}
skia.committer@gmail.com3e2345a2013-05-24 07:01:26 +000017
reed@google.com22999c62013-05-23 19:30:48 +000018protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000019 virtual uint32_t onGetFlags() const SK_OVERRIDE {
20 return kSkipTiled_Flag;
21 }
22
reed@google.com22999c62013-05-23 19:30:48 +000023 SkString onShortName() {
reed@google.com03ca64b2013-05-23 19:39:15 +000024 return SkString("clippedcubic");
reed@google.com22999c62013-05-23 19:30:48 +000025 }
skia.committer@gmail.com3e2345a2013-05-24 07:01:26 +000026
reed@google.com22999c62013-05-23 19:30:48 +000027 SkISize onISize() { return SkISize::Make(1240, 390); }
skia.committer@gmail.com3e2345a2013-05-24 07:01:26 +000028
reed@google.com22999c62013-05-23 19:30:48 +000029 virtual void onDraw(SkCanvas* canvas) {
30 SkPath path;
31 path.moveTo(0, 0);
32 path.cubicTo(140, 150, 40, 10, 170, 150);
skia.committer@gmail.com3e2345a2013-05-24 07:01:26 +000033
reed@google.com22999c62013-05-23 19:30:48 +000034 SkPaint paint;
35 SkRect bounds = path.getBounds();
skia.committer@gmail.com3e2345a2013-05-24 07:01:26 +000036
reed@google.com0f8990c2013-05-23 19:47:05 +000037 for (SkScalar dy = -1; dy <= 1; dy += 1) {
reed@google.com22999c62013-05-23 19:30:48 +000038 canvas->save();
reed@google.com0f8990c2013-05-23 19:47:05 +000039 for (SkScalar dx = -1; dx <= 1; dx += 1) {
reed@google.com22999c62013-05-23 19:30:48 +000040 canvas->save();
41 canvas->clipRect(bounds);
42 canvas->translate(dx, dy);
43 canvas->drawPath(path, paint);
44 canvas->restore();
skia.committer@gmail.com3e2345a2013-05-24 07:01:26 +000045
reed@google.com22999c62013-05-23 19:30:48 +000046 canvas->translate(bounds.width(), 0);
47 }
48 canvas->restore();
49 canvas->translate(0, bounds.height());
50 }
51 }
skia.committer@gmail.com3e2345a2013-05-24 07:01:26 +000052
reed@google.com22999c62013-05-23 19:30:48 +000053private:
54 typedef skiagm::GM INHERITED;
55};
56
reed@google.com27b90fa2013-03-08 18:44:01 +000057class CubicPathGM : public skiagm::GM {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000058public:
59 CubicPathGM() {}
60
61protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000062 virtual uint32_t onGetFlags() const SK_OVERRIDE {
63 return kSkipTiled_Flag;
64 }
65
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000066 SkString onShortName() {
67 return SkString("cubicpath");
68 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000069
reed@google.com27b90fa2013-03-08 18:44:01 +000070 SkISize onISize() { return SkISize::Make(1240, 390); }
rmistry@google.comd6176b02012-08-23 18:14:13 +000071
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000072 void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
73 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
74 SkPaint::Style style, SkPath::FillType fill,
75 SkScalar strokeWidth) {
76 path.setFillType(fill);
77 SkPaint paint;
78 paint.setStrokeCap(cap);
79 paint.setStrokeWidth(strokeWidth);
80 paint.setStrokeJoin(join);
81 paint.setColor(color);
82 paint.setStyle(style);
83 canvas->save();
84 canvas->clipRect(clip);
85 canvas->drawPath(path, paint);
86 canvas->restore();
87 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000088
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000089 virtual void onDraw(SkCanvas* canvas) {
90 struct FillAndName {
91 SkPath::FillType fFill;
92 const char* fName;
93 };
94 static const FillAndName gFills[] = {
95 {SkPath::kWinding_FillType, "Winding"},
96 {SkPath::kEvenOdd_FillType, "Even / Odd"},
97 {SkPath::kInverseWinding_FillType, "Inverse Winding"},
98 {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
99 };
100 struct StyleAndName {
101 SkPaint::Style fStyle;
102 const char* fName;
103 };
104 static const StyleAndName gStyles[] = {
105 {SkPaint::kFill_Style, "Fill"},
106 {SkPaint::kStroke_Style, "Stroke"},
107 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
108 };
109 struct CapAndName {
110 SkPaint::Cap fCap;
111 SkPaint::Join fJoin;
112 const char* fName;
113 };
114 static const CapAndName gCaps[] = {
115 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
116 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
117 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
118 };
119 struct PathAndName {
120 SkPath fPath;
121 const char* fName;
122 };
123 PathAndName path;
124 path.fPath.moveTo(25*SK_Scalar1, 10*SK_Scalar1);
125 path.fPath.cubicTo(40*SK_Scalar1, 20*SK_Scalar1,
126 60*SK_Scalar1, 20*SK_Scalar1,
127 75*SK_Scalar1, 10*SK_Scalar1);
128 path.fName = "moveTo-cubic";
129
130 SkPaint titlePaint;
131 titlePaint.setColor(SK_ColorBLACK);
132 titlePaint.setAntiAlias(true);
133 titlePaint.setLCDRenderText(true);
134 titlePaint.setTextSize(15 * SK_Scalar1);
135 const char title[] = "Cubic Drawn Into Rectangle Clips With "
136 "Indicated Style, Fill and Linecaps, with stroke width 10";
137 canvas->drawText(title, strlen(title),
138 20 * SK_Scalar1,
139 20 * SK_Scalar1,
140 titlePaint);
141
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000142 SkLCGRandom rand;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000143 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
144 canvas->save();
145 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
146 canvas->save();
147 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
148 if (0 < cap) {
149 canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0);
150 }
151 canvas->save();
152 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
153 if (0 < fill) {
154 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
155 }
156 canvas->save();
157 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
158 if (0 < style) {
159 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
160 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000161
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000162 SkColor color = 0xff007000;
163 this->drawPath(path.fPath, canvas, color, rect,
164 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
165 gFills[fill].fFill, SK_Scalar1*10);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000166
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000167 SkPaint rectPaint;
168 rectPaint.setColor(SK_ColorBLACK);
169 rectPaint.setStyle(SkPaint::kStroke_Style);
170 rectPaint.setStrokeWidth(-1);
171 rectPaint.setAntiAlias(true);
172 canvas->drawRect(rect, rectPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000173
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000174 SkPaint labelPaint;
175 labelPaint.setColor(color);
176 labelPaint.setAntiAlias(true);
177 labelPaint.setLCDRenderText(true);
178 labelPaint.setTextSize(10 * SK_Scalar1);
179 canvas->drawText(gStyles[style].fName,
180 strlen(gStyles[style].fName),
181 0, rect.height() + 12 * SK_Scalar1,
182 labelPaint);
183 canvas->drawText(gFills[fill].fName,
184 strlen(gFills[fill].fName),
185 0, rect.height() + 24 * SK_Scalar1,
186 labelPaint);
187 canvas->drawText(gCaps[cap].fName,
188 strlen(gCaps[cap].fName),
189 0, rect.height() + 36 * SK_Scalar1,
190 labelPaint);
191 }
192 canvas->restore();
193 }
194 canvas->restore();
195 }
196 canvas->restore();
197 canvas->restore();
198 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000199
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000200private:
reed@google.com27b90fa2013-03-08 18:44:01 +0000201 typedef skiagm::GM INHERITED;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000202};
203
reed@google.com27b90fa2013-03-08 18:44:01 +0000204class CubicClosePathGM : public skiagm::GM {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000205public:
206 CubicClosePathGM() {}
207
208protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000209 virtual uint32_t onGetFlags() const SK_OVERRIDE {
210 return kSkipTiled_Flag;
211 }
212
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000213 SkString onShortName() {
214 return SkString("cubicclosepath");
215 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000216
reed@google.com27b90fa2013-03-08 18:44:01 +0000217 SkISize onISize() { return SkISize::Make(1240, 390); }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000218
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000219 void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
220 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
221 SkPaint::Style style, SkPath::FillType fill,
222 SkScalar strokeWidth) {
223 path.setFillType(fill);
224 SkPaint paint;
225 paint.setStrokeCap(cap);
226 paint.setStrokeWidth(strokeWidth);
227 paint.setStrokeJoin(join);
228 paint.setColor(color);
229 paint.setStyle(style);
230 canvas->save();
231 canvas->clipRect(clip);
232 canvas->drawPath(path, paint);
233 canvas->restore();
234 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000235
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000236 virtual void onDraw(SkCanvas* canvas) {
237 struct FillAndName {
238 SkPath::FillType fFill;
239 const char* fName;
240 };
241 static const FillAndName gFills[] = {
242 {SkPath::kWinding_FillType, "Winding"},
243 {SkPath::kEvenOdd_FillType, "Even / Odd"},
244 {SkPath::kInverseWinding_FillType, "Inverse Winding"},
245 {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
246 };
247 struct StyleAndName {
248 SkPaint::Style fStyle;
249 const char* fName;
250 };
251 static const StyleAndName gStyles[] = {
252 {SkPaint::kFill_Style, "Fill"},
253 {SkPaint::kStroke_Style, "Stroke"},
254 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
255 };
256 struct CapAndName {
257 SkPaint::Cap fCap;
258 SkPaint::Join fJoin;
259 const char* fName;
260 };
261 static const CapAndName gCaps[] = {
262 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
263 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
264 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
265 };
266 struct PathAndName {
267 SkPath fPath;
268 const char* fName;
269 };
270 PathAndName path;
271 path.fPath.moveTo(25*SK_Scalar1, 10*SK_Scalar1);
272 path.fPath.cubicTo(40*SK_Scalar1, 20*SK_Scalar1,
273 60*SK_Scalar1, 20*SK_Scalar1,
274 75*SK_Scalar1, 10*SK_Scalar1);
275 path.fPath.close();
276 path.fName = "moveTo-cubic-close";
277
278 SkPaint titlePaint;
279 titlePaint.setColor(SK_ColorBLACK);
280 titlePaint.setAntiAlias(true);
281 titlePaint.setLCDRenderText(true);
282 titlePaint.setTextSize(15 * SK_Scalar1);
283 const char title[] = "Cubic Closed Drawn Into Rectangle Clips With "
284 "Indicated Style, Fill and Linecaps, with stroke width 10";
285 canvas->drawText(title, strlen(title),
286 20 * SK_Scalar1,
287 20 * SK_Scalar1,
288 titlePaint);
289
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000290 SkLCGRandom rand;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000291 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
292 canvas->save();
293 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
294 canvas->save();
295 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
296 if (0 < cap) {
297 canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0);
298 }
299 canvas->save();
300 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
301 if (0 < fill) {
302 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
303 }
304 canvas->save();
305 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
306 if (0 < style) {
307 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
308 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000309
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000310 SkColor color = 0xff007000;
311 this->drawPath(path.fPath, canvas, color, rect,
312 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
313 gFills[fill].fFill, SK_Scalar1*10);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000314
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000315 SkPaint rectPaint;
316 rectPaint.setColor(SK_ColorBLACK);
317 rectPaint.setStyle(SkPaint::kStroke_Style);
318 rectPaint.setStrokeWidth(-1);
319 rectPaint.setAntiAlias(true);
320 canvas->drawRect(rect, rectPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000321
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000322 SkPaint labelPaint;
323 labelPaint.setColor(color);
324 labelPaint.setAntiAlias(true);
325 labelPaint.setLCDRenderText(true);
326 labelPaint.setTextSize(10 * SK_Scalar1);
327 canvas->drawText(gStyles[style].fName,
328 strlen(gStyles[style].fName),
329 0, rect.height() + 12 * SK_Scalar1,
330 labelPaint);
331 canvas->drawText(gFills[fill].fName,
332 strlen(gFills[fill].fName),
333 0, rect.height() + 24 * SK_Scalar1,
334 labelPaint);
335 canvas->drawText(gCaps[cap].fName,
336 strlen(gCaps[cap].fName),
337 0, rect.height() + 36 * SK_Scalar1,
338 labelPaint);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000339 }
340 canvas->restore();
341 }
342 canvas->restore();
343 }
344 canvas->restore();
345 canvas->restore();
346 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000347
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000348private:
reed@google.com27b90fa2013-03-08 18:44:01 +0000349 typedef skiagm::GM INHERITED;
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000350};
351
352//////////////////////////////////////////////////////////////////////////////
353
reed@google.com27b90fa2013-03-08 18:44:01 +0000354DEF_GM( return new CubicPathGM; )
355DEF_GM( return new CubicClosePathGM; )
reed@google.com22999c62013-05-23 19:30:48 +0000356DEF_GM( return new ClippedCubicGM; )