blob: 9ab710a7ae9d441e28a45d14b77e179bca665dea [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.com27b90fa2013-03-08 18:44:01 +000012class CubicPathGM : public skiagm::GM {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000013public:
14 CubicPathGM() {}
15
16protected:
17 SkString onShortName() {
18 return SkString("cubicpath");
19 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000020
reed@google.com27b90fa2013-03-08 18:44:01 +000021 SkISize onISize() { return SkISize::Make(1240, 390); }
rmistry@google.comd6176b02012-08-23 18:14:13 +000022
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000023 void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
24 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
25 SkPaint::Style style, SkPath::FillType fill,
26 SkScalar strokeWidth) {
27 path.setFillType(fill);
28 SkPaint paint;
29 paint.setStrokeCap(cap);
30 paint.setStrokeWidth(strokeWidth);
31 paint.setStrokeJoin(join);
32 paint.setColor(color);
33 paint.setStyle(style);
34 canvas->save();
35 canvas->clipRect(clip);
36 canvas->drawPath(path, paint);
37 canvas->restore();
38 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000039
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000040 virtual void onDraw(SkCanvas* canvas) {
41 struct FillAndName {
42 SkPath::FillType fFill;
43 const char* fName;
44 };
45 static const FillAndName gFills[] = {
46 {SkPath::kWinding_FillType, "Winding"},
47 {SkPath::kEvenOdd_FillType, "Even / Odd"},
48 {SkPath::kInverseWinding_FillType, "Inverse Winding"},
49 {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
50 };
51 struct StyleAndName {
52 SkPaint::Style fStyle;
53 const char* fName;
54 };
55 static const StyleAndName gStyles[] = {
56 {SkPaint::kFill_Style, "Fill"},
57 {SkPaint::kStroke_Style, "Stroke"},
58 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
59 };
60 struct CapAndName {
61 SkPaint::Cap fCap;
62 SkPaint::Join fJoin;
63 const char* fName;
64 };
65 static const CapAndName gCaps[] = {
66 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
67 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
68 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
69 };
70 struct PathAndName {
71 SkPath fPath;
72 const char* fName;
73 };
74 PathAndName path;
75 path.fPath.moveTo(25*SK_Scalar1, 10*SK_Scalar1);
76 path.fPath.cubicTo(40*SK_Scalar1, 20*SK_Scalar1,
77 60*SK_Scalar1, 20*SK_Scalar1,
78 75*SK_Scalar1, 10*SK_Scalar1);
79 path.fName = "moveTo-cubic";
80
81 SkPaint titlePaint;
82 titlePaint.setColor(SK_ColorBLACK);
83 titlePaint.setAntiAlias(true);
84 titlePaint.setLCDRenderText(true);
85 titlePaint.setTextSize(15 * SK_Scalar1);
86 const char title[] = "Cubic Drawn Into Rectangle Clips With "
87 "Indicated Style, Fill and Linecaps, with stroke width 10";
88 canvas->drawText(title, strlen(title),
89 20 * SK_Scalar1,
90 20 * SK_Scalar1,
91 titlePaint);
92
93 SkRandom rand;
94 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
95 canvas->save();
96 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
97 canvas->save();
98 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
99 if (0 < cap) {
100 canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0);
101 }
102 canvas->save();
103 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
104 if (0 < fill) {
105 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
106 }
107 canvas->save();
108 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
109 if (0 < style) {
110 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
111 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000112
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000113 SkColor color = 0xff007000;
114 this->drawPath(path.fPath, canvas, color, rect,
115 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
116 gFills[fill].fFill, SK_Scalar1*10);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000117
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000118 SkPaint rectPaint;
119 rectPaint.setColor(SK_ColorBLACK);
120 rectPaint.setStyle(SkPaint::kStroke_Style);
121 rectPaint.setStrokeWidth(-1);
122 rectPaint.setAntiAlias(true);
123 canvas->drawRect(rect, rectPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000124
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000125 SkPaint labelPaint;
126 labelPaint.setColor(color);
127 labelPaint.setAntiAlias(true);
128 labelPaint.setLCDRenderText(true);
129 labelPaint.setTextSize(10 * SK_Scalar1);
130 canvas->drawText(gStyles[style].fName,
131 strlen(gStyles[style].fName),
132 0, rect.height() + 12 * SK_Scalar1,
133 labelPaint);
134 canvas->drawText(gFills[fill].fName,
135 strlen(gFills[fill].fName),
136 0, rect.height() + 24 * SK_Scalar1,
137 labelPaint);
138 canvas->drawText(gCaps[cap].fName,
139 strlen(gCaps[cap].fName),
140 0, rect.height() + 36 * SK_Scalar1,
141 labelPaint);
142 }
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:
reed@google.com27b90fa2013-03-08 18:44:01 +0000152 typedef skiagm::GM INHERITED;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000153};
154
reed@google.com27b90fa2013-03-08 18:44:01 +0000155class CubicClosePathGM : public skiagm::GM {
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000156public:
157 CubicClosePathGM() {}
158
159protected:
160 SkString onShortName() {
161 return SkString("cubicclosepath");
162 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000163
reed@google.com27b90fa2013-03-08 18:44:01 +0000164 SkISize onISize() { return SkISize::Make(1240, 390); }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000165
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000166 void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
167 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
168 SkPaint::Style style, SkPath::FillType fill,
169 SkScalar strokeWidth) {
170 path.setFillType(fill);
171 SkPaint paint;
172 paint.setStrokeCap(cap);
173 paint.setStrokeWidth(strokeWidth);
174 paint.setStrokeJoin(join);
175 paint.setColor(color);
176 paint.setStyle(style);
177 canvas->save();
178 canvas->clipRect(clip);
179 canvas->drawPath(path, paint);
180 canvas->restore();
181 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000182
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000183 virtual void onDraw(SkCanvas* canvas) {
184 struct FillAndName {
185 SkPath::FillType fFill;
186 const char* fName;
187 };
188 static const FillAndName gFills[] = {
189 {SkPath::kWinding_FillType, "Winding"},
190 {SkPath::kEvenOdd_FillType, "Even / Odd"},
191 {SkPath::kInverseWinding_FillType, "Inverse Winding"},
192 {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
193 };
194 struct StyleAndName {
195 SkPaint::Style fStyle;
196 const char* fName;
197 };
198 static const StyleAndName gStyles[] = {
199 {SkPaint::kFill_Style, "Fill"},
200 {SkPaint::kStroke_Style, "Stroke"},
201 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
202 };
203 struct CapAndName {
204 SkPaint::Cap fCap;
205 SkPaint::Join fJoin;
206 const char* fName;
207 };
208 static const CapAndName gCaps[] = {
209 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
210 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
211 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
212 };
213 struct PathAndName {
214 SkPath fPath;
215 const char* fName;
216 };
217 PathAndName path;
218 path.fPath.moveTo(25*SK_Scalar1, 10*SK_Scalar1);
219 path.fPath.cubicTo(40*SK_Scalar1, 20*SK_Scalar1,
220 60*SK_Scalar1, 20*SK_Scalar1,
221 75*SK_Scalar1, 10*SK_Scalar1);
222 path.fPath.close();
223 path.fName = "moveTo-cubic-close";
224
225 SkPaint titlePaint;
226 titlePaint.setColor(SK_ColorBLACK);
227 titlePaint.setAntiAlias(true);
228 titlePaint.setLCDRenderText(true);
229 titlePaint.setTextSize(15 * SK_Scalar1);
230 const char title[] = "Cubic Closed Drawn Into Rectangle Clips With "
231 "Indicated Style, Fill and Linecaps, with stroke width 10";
232 canvas->drawText(title, strlen(title),
233 20 * SK_Scalar1,
234 20 * SK_Scalar1,
235 titlePaint);
236
237 SkRandom rand;
238 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
239 canvas->save();
240 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
241 canvas->save();
242 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
243 if (0 < cap) {
244 canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0);
245 }
246 canvas->save();
247 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
248 if (0 < fill) {
249 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
250 }
251 canvas->save();
252 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
253 if (0 < style) {
254 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
255 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000256
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000257 SkColor color = 0xff007000;
258 this->drawPath(path.fPath, canvas, color, rect,
259 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
260 gFills[fill].fFill, SK_Scalar1*10);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000261
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000262 SkPaint rectPaint;
263 rectPaint.setColor(SK_ColorBLACK);
264 rectPaint.setStyle(SkPaint::kStroke_Style);
265 rectPaint.setStrokeWidth(-1);
266 rectPaint.setAntiAlias(true);
267 canvas->drawRect(rect, rectPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000268
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000269 SkPaint labelPaint;
270 labelPaint.setColor(color);
271 labelPaint.setAntiAlias(true);
272 labelPaint.setLCDRenderText(true);
273 labelPaint.setTextSize(10 * SK_Scalar1);
274 canvas->drawText(gStyles[style].fName,
275 strlen(gStyles[style].fName),
276 0, rect.height() + 12 * SK_Scalar1,
277 labelPaint);
278 canvas->drawText(gFills[fill].fName,
279 strlen(gFills[fill].fName),
280 0, rect.height() + 24 * SK_Scalar1,
281 labelPaint);
282 canvas->drawText(gCaps[cap].fName,
283 strlen(gCaps[cap].fName),
284 0, rect.height() + 36 * SK_Scalar1,
285 labelPaint);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000286 }
287 canvas->restore();
288 }
289 canvas->restore();
290 }
291 canvas->restore();
292 canvas->restore();
293 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000294
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000295private:
reed@google.com27b90fa2013-03-08 18:44:01 +0000296 typedef skiagm::GM INHERITED;
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000297};
298
299//////////////////////////////////////////////////////////////////////////////
300
reed@google.com27b90fa2013-03-08 18:44:01 +0000301DEF_GM( return new CubicPathGM; )
302DEF_GM( return new CubicClosePathGM; )