blob: 4fb66414feeb03cf59dffc595528a84a37fe9660 [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
12namespace skiagm {
13
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000014class LinePathGM : public GM {
15public:
16 LinePathGM() {}
17
18protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000019
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000020 SkString onShortName() {
21 return SkString("linepath");
22 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000023
tfarinaf5393182014-06-09 23:59:03 -070024 SkISize onISize() { return SkISize::Make(1240, 390); }
rmistry@google.comd6176b02012-08-23 18:14:13 +000025
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000026 void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
27 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
28 SkPaint::Style style, SkPath::FillType fill,
29 SkScalar strokeWidth) {
30 path.setFillType(fill);
31 SkPaint paint;
32 paint.setStrokeCap(cap);
33 paint.setStrokeWidth(strokeWidth);
34 paint.setStrokeJoin(join);
35 paint.setColor(color);
36 paint.setStyle(style);
37 canvas->save();
38 canvas->clipRect(clip);
39 canvas->drawPath(path, paint);
40 canvas->restore();
41 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000042
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000043 virtual void onDraw(SkCanvas* canvas) {
44 struct FillAndName {
45 SkPath::FillType fFill;
46 const char* fName;
47 };
48 static const FillAndName gFills[] = {
49 {SkPath::kWinding_FillType, "Winding"},
50 {SkPath::kEvenOdd_FillType, "Even / Odd"},
51 {SkPath::kInverseWinding_FillType, "Inverse Winding"},
52 {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
53 };
54 struct StyleAndName {
55 SkPaint::Style fStyle;
56 const char* fName;
57 };
58 static const StyleAndName gStyles[] = {
59 {SkPaint::kFill_Style, "Fill"},
60 {SkPaint::kStroke_Style, "Stroke"},
61 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
62 };
63 struct CapAndName {
64 SkPaint::Cap fCap;
65 SkPaint::Join fJoin;
66 const char* fName;
67 };
68 static const CapAndName gCaps[] = {
69 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
70 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
71 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
72 };
73 struct PathAndName {
74 SkPath fPath;
75 const char* fName;
76 };
77 PathAndName path;
78 path.fPath.moveTo(25*SK_Scalar1, 15*SK_Scalar1);
79 path.fPath.lineTo(75*SK_Scalar1, 15*SK_Scalar1);
80 path.fName = "moveTo-line";
81
82 SkPaint titlePaint;
83 titlePaint.setColor(SK_ColorBLACK);
84 titlePaint.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -040085 sk_tool_utils::set_portable_typeface(&titlePaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000086 titlePaint.setLCDRenderText(true);
87 titlePaint.setTextSize(15 * SK_Scalar1);
88 const char title[] = "Line Drawn Into Rectangle Clips With "
89 "Indicated Style, Fill and Linecaps, with stroke width 10";
90 canvas->drawText(title, strlen(title),
91 20 * SK_Scalar1,
92 20 * SK_Scalar1,
93 titlePaint);
94
scroggof9d61012014-12-15 12:54:51 -080095 SkRandom rand;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000096 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
97 canvas->save();
98 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
99 canvas->save();
100 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
101 if (0 < cap) {
102 canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0);
103 }
104 canvas->save();
105 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
106 if (0 < fill) {
107 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
108 }
109 canvas->save();
110 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
111 if (0 < style) {
112 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000113 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000114
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000115 SkColor color = 0xff007000;
116 this->drawPath(path.fPath, canvas, color, rect,
117 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
118 gFills[fill].fFill, SK_Scalar1*10);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000119
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000120 SkPaint rectPaint;
121 rectPaint.setColor(SK_ColorBLACK);
122 rectPaint.setStyle(SkPaint::kStroke_Style);
123 rectPaint.setStrokeWidth(-1);
124 rectPaint.setAntiAlias(true);
125 canvas->drawRect(rect, rectPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000126
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000127 SkPaint labelPaint;
128 labelPaint.setColor(color);
129 labelPaint.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -0400130 sk_tool_utils::set_portable_typeface(&labelPaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000131 labelPaint.setLCDRenderText(true);
132 labelPaint.setTextSize(10 * SK_Scalar1);
133 canvas->drawText(gStyles[style].fName,
134 strlen(gStyles[style].fName),
135 0, rect.height() + 12 * SK_Scalar1,
136 labelPaint);
137 canvas->drawText(gFills[fill].fName,
138 strlen(gFills[fill].fName),
139 0, rect.height() + 24 * SK_Scalar1,
140 labelPaint);
141 canvas->drawText(gCaps[cap].fName,
142 strlen(gCaps[cap].fName),
143 0, rect.height() + 36 * SK_Scalar1,
144 labelPaint);
145 }
146 canvas->restore();
147 }
148 canvas->restore();
149 }
150 canvas->restore();
151 canvas->restore();
152 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000153
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000154private:
155 typedef GM INHERITED;
156};
157
158class LineClosePathGM : public GM {
159public:
160 LineClosePathGM() {}
161
162protected:
163 SkString onShortName() {
164 return SkString("lineclosepath");
165 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000166
tfarinaf5393182014-06-09 23:59:03 -0700167 SkISize onISize() { return SkISize::Make(1240, 390); }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000168
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000169 void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
170 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
171 SkPaint::Style style, SkPath::FillType fill,
172 SkScalar strokeWidth) {
173 path.setFillType(fill);
174 SkPaint paint;
175 paint.setStrokeCap(cap);
176 paint.setStrokeWidth(strokeWidth);
177 paint.setStrokeJoin(join);
178 paint.setColor(color);
179 paint.setStyle(style);
180 canvas->save();
181 canvas->clipRect(clip);
182 canvas->drawPath(path, paint);
183 canvas->restore();
184 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000185
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000186 virtual void onDraw(SkCanvas* canvas) {
187 struct FillAndName {
188 SkPath::FillType fFill;
189 const char* fName;
190 };
191 static const FillAndName gFills[] = {
192 {SkPath::kWinding_FillType, "Winding"},
193 {SkPath::kEvenOdd_FillType, "Even / Odd"},
194 {SkPath::kInverseWinding_FillType, "Inverse Winding"},
195 {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
196 };
197 struct StyleAndName {
198 SkPaint::Style fStyle;
199 const char* fName;
200 };
201 static const StyleAndName gStyles[] = {
202 {SkPaint::kFill_Style, "Fill"},
203 {SkPaint::kStroke_Style, "Stroke"},
204 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
205 };
206 struct CapAndName {
207 SkPaint::Cap fCap;
208 SkPaint::Join fJoin;
209 const char* fName;
210 };
211 static const CapAndName gCaps[] = {
212 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
213 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
214 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
215 };
216 struct PathAndName {
217 SkPath fPath;
218 const char* fName;
219 };
220 PathAndName path;
221 path.fPath.moveTo(25*SK_Scalar1, 15*SK_Scalar1);
222 path.fPath.lineTo(75*SK_Scalar1, 15*SK_Scalar1);
223 path.fPath.close();
224 path.fName = "moveTo-line-close";
225
226 SkPaint titlePaint;
227 titlePaint.setColor(SK_ColorBLACK);
228 titlePaint.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -0400229 sk_tool_utils::set_portable_typeface(&titlePaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000230 titlePaint.setLCDRenderText(true);
231 titlePaint.setTextSize(15 * SK_Scalar1);
232 const char title[] = "Line Closed Drawn Into Rectangle Clips With "
233 "Indicated Style, Fill and Linecaps, with stroke width 10";
234 canvas->drawText(title, strlen(title),
235 20 * SK_Scalar1,
236 20 * SK_Scalar1,
237 titlePaint);
238
scroggof9d61012014-12-15 12:54:51 -0800239 SkRandom rand;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000240 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
241 canvas->save();
242 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
243 canvas->save();
244 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
245 if (0 < cap) {
246 canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0);
247 }
248 canvas->save();
249 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
250 if (0 < fill) {
251 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
252 }
253 canvas->save();
254 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
255 if (0 < style) {
256 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
257 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000258
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000259 SkColor color = 0xff007000;
260 this->drawPath(path.fPath, canvas, color, rect,
261 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
262 gFills[fill].fFill, SK_Scalar1*10);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000263
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000264 SkPaint rectPaint;
265 rectPaint.setColor(SK_ColorBLACK);
266 rectPaint.setStyle(SkPaint::kStroke_Style);
267 rectPaint.setStrokeWidth(-1);
268 rectPaint.setAntiAlias(true);
269 canvas->drawRect(rect, rectPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000270
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000271 SkPaint labelPaint;
272 labelPaint.setColor(color);
273 labelPaint.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -0400274 sk_tool_utils::set_portable_typeface(&labelPaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000275 labelPaint.setLCDRenderText(true);
276 labelPaint.setTextSize(10 * SK_Scalar1);
277 canvas->drawText(gStyles[style].fName,
278 strlen(gStyles[style].fName),
279 0, rect.height() + 12 * SK_Scalar1,
280 labelPaint);
281 canvas->drawText(gFills[fill].fName,
282 strlen(gFills[fill].fName),
283 0, rect.height() + 24 * SK_Scalar1,
284 labelPaint);
285 canvas->drawText(gCaps[cap].fName,
286 strlen(gCaps[cap].fName),
287 0, rect.height() + 36 * SK_Scalar1,
288 labelPaint);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000289 }
290 canvas->restore();
291 }
292 canvas->restore();
293 }
294 canvas->restore();
295 canvas->restore();
296 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000297
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000298private:
299 typedef GM INHERITED;
300};
301
302//////////////////////////////////////////////////////////////////////////////
303
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000304static GM* LinePathFactory(void*) { return new LinePathGM; }
305static GMRegistry regLinePath(LinePathFactory);
306
307static GM* LineClosePathFactory(void*) { return new LineClosePathGM; }
308static GMRegistry regLineClosePath(LineClosePathFactory);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000309
310}