blob: f171522caa69dee4c5461d1c8075cb68867d06b1 [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);
caryclark5fb6bd42014-06-23 11:25:00 -0700133 sk_tool_utils::set_portable_typeface(&titlePaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000134 titlePaint.setLCDRenderText(true);
135 titlePaint.setTextSize(15 * SK_Scalar1);
136 const char title[] = "Cubic Drawn Into Rectangle Clips With "
137 "Indicated Style, Fill and Linecaps, with stroke width 10";
138 canvas->drawText(title, strlen(title),
139 20 * SK_Scalar1,
140 20 * SK_Scalar1,
141 titlePaint);
142
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000143 SkLCGRandom rand;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000144 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
145 canvas->save();
146 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
147 canvas->save();
148 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
149 if (0 < cap) {
150 canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0);
151 }
152 canvas->save();
153 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
154 if (0 < fill) {
155 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
156 }
157 canvas->save();
158 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
159 if (0 < style) {
160 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
161 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000162
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000163 SkColor color = 0xff007000;
164 this->drawPath(path.fPath, canvas, color, rect,
165 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
166 gFills[fill].fFill, SK_Scalar1*10);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000167
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000168 SkPaint rectPaint;
169 rectPaint.setColor(SK_ColorBLACK);
170 rectPaint.setStyle(SkPaint::kStroke_Style);
171 rectPaint.setStrokeWidth(-1);
172 rectPaint.setAntiAlias(true);
173 canvas->drawRect(rect, rectPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000174
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000175 SkPaint labelPaint;
176 labelPaint.setColor(color);
177 labelPaint.setAntiAlias(true);
caryclark5fb6bd42014-06-23 11:25:00 -0700178 sk_tool_utils::set_portable_typeface(&labelPaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000179 labelPaint.setLCDRenderText(true);
180 labelPaint.setTextSize(10 * SK_Scalar1);
181 canvas->drawText(gStyles[style].fName,
182 strlen(gStyles[style].fName),
183 0, rect.height() + 12 * SK_Scalar1,
184 labelPaint);
185 canvas->drawText(gFills[fill].fName,
186 strlen(gFills[fill].fName),
187 0, rect.height() + 24 * SK_Scalar1,
188 labelPaint);
189 canvas->drawText(gCaps[cap].fName,
190 strlen(gCaps[cap].fName),
191 0, rect.height() + 36 * SK_Scalar1,
192 labelPaint);
193 }
194 canvas->restore();
195 }
196 canvas->restore();
197 }
198 canvas->restore();
199 canvas->restore();
200 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000201
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000202private:
reed@google.com27b90fa2013-03-08 18:44:01 +0000203 typedef skiagm::GM INHERITED;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000204};
205
reed@google.com27b90fa2013-03-08 18:44:01 +0000206class CubicClosePathGM : public skiagm::GM {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000207public:
208 CubicClosePathGM() {}
209
210protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000211 virtual uint32_t onGetFlags() const SK_OVERRIDE {
212 return kSkipTiled_Flag;
213 }
214
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000215 SkString onShortName() {
216 return SkString("cubicclosepath");
217 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000218
reed@google.com27b90fa2013-03-08 18:44:01 +0000219 SkISize onISize() { return SkISize::Make(1240, 390); }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000220
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000221 void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
222 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
223 SkPaint::Style style, SkPath::FillType fill,
224 SkScalar strokeWidth) {
225 path.setFillType(fill);
226 SkPaint paint;
227 paint.setStrokeCap(cap);
228 paint.setStrokeWidth(strokeWidth);
229 paint.setStrokeJoin(join);
230 paint.setColor(color);
231 paint.setStyle(style);
232 canvas->save();
233 canvas->clipRect(clip);
234 canvas->drawPath(path, paint);
235 canvas->restore();
236 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000237
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000238 virtual void onDraw(SkCanvas* canvas) {
239 struct FillAndName {
240 SkPath::FillType fFill;
241 const char* fName;
242 };
243 static const FillAndName gFills[] = {
244 {SkPath::kWinding_FillType, "Winding"},
245 {SkPath::kEvenOdd_FillType, "Even / Odd"},
246 {SkPath::kInverseWinding_FillType, "Inverse Winding"},
247 {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
248 };
249 struct StyleAndName {
250 SkPaint::Style fStyle;
251 const char* fName;
252 };
253 static const StyleAndName gStyles[] = {
254 {SkPaint::kFill_Style, "Fill"},
255 {SkPaint::kStroke_Style, "Stroke"},
256 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
257 };
258 struct CapAndName {
259 SkPaint::Cap fCap;
260 SkPaint::Join fJoin;
261 const char* fName;
262 };
263 static const CapAndName gCaps[] = {
264 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
265 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
266 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
267 };
268 struct PathAndName {
269 SkPath fPath;
270 const char* fName;
271 };
272 PathAndName path;
273 path.fPath.moveTo(25*SK_Scalar1, 10*SK_Scalar1);
274 path.fPath.cubicTo(40*SK_Scalar1, 20*SK_Scalar1,
275 60*SK_Scalar1, 20*SK_Scalar1,
276 75*SK_Scalar1, 10*SK_Scalar1);
277 path.fPath.close();
278 path.fName = "moveTo-cubic-close";
279
280 SkPaint titlePaint;
281 titlePaint.setColor(SK_ColorBLACK);
282 titlePaint.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -0400283 sk_tool_utils::set_portable_typeface(&titlePaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000284 titlePaint.setLCDRenderText(true);
285 titlePaint.setTextSize(15 * SK_Scalar1);
286 const char title[] = "Cubic Closed Drawn Into Rectangle Clips With "
287 "Indicated Style, Fill and Linecaps, with stroke width 10";
288 canvas->drawText(title, strlen(title),
289 20 * SK_Scalar1,
290 20 * SK_Scalar1,
291 titlePaint);
292
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000293 SkLCGRandom rand;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000294 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
295 canvas->save();
296 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
297 canvas->save();
298 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
299 if (0 < cap) {
300 canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0);
301 }
302 canvas->save();
303 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
304 if (0 < fill) {
305 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
306 }
307 canvas->save();
308 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
309 if (0 < style) {
310 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
311 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000312
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000313 SkColor color = 0xff007000;
314 this->drawPath(path.fPath, canvas, color, rect,
315 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
316 gFills[fill].fFill, SK_Scalar1*10);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000317
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000318 SkPaint rectPaint;
319 rectPaint.setColor(SK_ColorBLACK);
320 rectPaint.setStyle(SkPaint::kStroke_Style);
321 rectPaint.setStrokeWidth(-1);
322 rectPaint.setAntiAlias(true);
323 canvas->drawRect(rect, rectPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000324
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000325 SkPaint labelPaint;
326 labelPaint.setColor(color);
327 labelPaint.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -0400328 sk_tool_utils::set_portable_typeface(&labelPaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000329 labelPaint.setLCDRenderText(true);
330 labelPaint.setTextSize(10 * SK_Scalar1);
331 canvas->drawText(gStyles[style].fName,
332 strlen(gStyles[style].fName),
333 0, rect.height() + 12 * SK_Scalar1,
334 labelPaint);
335 canvas->drawText(gFills[fill].fName,
336 strlen(gFills[fill].fName),
337 0, rect.height() + 24 * SK_Scalar1,
338 labelPaint);
339 canvas->drawText(gCaps[cap].fName,
340 strlen(gCaps[cap].fName),
341 0, rect.height() + 36 * SK_Scalar1,
342 labelPaint);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000343 }
344 canvas->restore();
345 }
346 canvas->restore();
347 }
348 canvas->restore();
349 canvas->restore();
350 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000351
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000352private:
reed@google.com27b90fa2013-03-08 18:44:01 +0000353 typedef skiagm::GM INHERITED;
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000354};
355
356//////////////////////////////////////////////////////////////////////////////
357
reed@google.com27b90fa2013-03-08 18:44:01 +0000358DEF_GM( return new CubicPathGM; )
359DEF_GM( return new CubicClosePathGM; )
reed@google.com22999c62013-05-23 19:30:48 +0000360DEF_GM( return new ClippedCubicGM; )