blob: 53a93278781072756dee24ef44bd4cc21c3a7649 [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 virtual uint32_t onGetFlags() const SK_OVERRIDE {
20 return kSkipTiled_Flag;
21 }
22
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000023 SkString onShortName() {
24 return SkString("linepath");
25 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000026
tfarinaf5393182014-06-09 23:59:03 -070027 SkISize onISize() { return SkISize::Make(1240, 390); }
rmistry@google.comd6176b02012-08-23 18:14:13 +000028
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000029 void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
30 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
31 SkPaint::Style style, SkPath::FillType fill,
32 SkScalar strokeWidth) {
33 path.setFillType(fill);
34 SkPaint paint;
35 paint.setStrokeCap(cap);
36 paint.setStrokeWidth(strokeWidth);
37 paint.setStrokeJoin(join);
38 paint.setColor(color);
39 paint.setStyle(style);
40 canvas->save();
41 canvas->clipRect(clip);
42 canvas->drawPath(path, paint);
43 canvas->restore();
44 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000045
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000046 virtual void onDraw(SkCanvas* canvas) {
47 struct FillAndName {
48 SkPath::FillType fFill;
49 const char* fName;
50 };
51 static const FillAndName gFills[] = {
52 {SkPath::kWinding_FillType, "Winding"},
53 {SkPath::kEvenOdd_FillType, "Even / Odd"},
54 {SkPath::kInverseWinding_FillType, "Inverse Winding"},
55 {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
56 };
57 struct StyleAndName {
58 SkPaint::Style fStyle;
59 const char* fName;
60 };
61 static const StyleAndName gStyles[] = {
62 {SkPaint::kFill_Style, "Fill"},
63 {SkPaint::kStroke_Style, "Stroke"},
64 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
65 };
66 struct CapAndName {
67 SkPaint::Cap fCap;
68 SkPaint::Join fJoin;
69 const char* fName;
70 };
71 static const CapAndName gCaps[] = {
72 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
73 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
74 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
75 };
76 struct PathAndName {
77 SkPath fPath;
78 const char* fName;
79 };
80 PathAndName path;
81 path.fPath.moveTo(25*SK_Scalar1, 15*SK_Scalar1);
82 path.fPath.lineTo(75*SK_Scalar1, 15*SK_Scalar1);
83 path.fName = "moveTo-line";
84
85 SkPaint titlePaint;
86 titlePaint.setColor(SK_ColorBLACK);
87 titlePaint.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -040088 sk_tool_utils::set_portable_typeface(&titlePaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000089 titlePaint.setLCDRenderText(true);
90 titlePaint.setTextSize(15 * SK_Scalar1);
91 const char title[] = "Line Drawn Into Rectangle Clips With "
92 "Indicated Style, Fill and Linecaps, with stroke width 10";
93 canvas->drawText(title, strlen(title),
94 20 * SK_Scalar1,
95 20 * SK_Scalar1,
96 titlePaint);
97
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000098 SkLCGRandom rand;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +000099 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
100 canvas->save();
101 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
102 canvas->save();
103 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
104 if (0 < cap) {
105 canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0);
106 }
107 canvas->save();
108 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
109 if (0 < fill) {
110 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
111 }
112 canvas->save();
113 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
114 if (0 < style) {
115 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000116 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000117
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000118 SkColor color = 0xff007000;
119 this->drawPath(path.fPath, canvas, color, rect,
120 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
121 gFills[fill].fFill, SK_Scalar1*10);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000122
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000123 SkPaint rectPaint;
124 rectPaint.setColor(SK_ColorBLACK);
125 rectPaint.setStyle(SkPaint::kStroke_Style);
126 rectPaint.setStrokeWidth(-1);
127 rectPaint.setAntiAlias(true);
128 canvas->drawRect(rect, rectPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000129
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000130 SkPaint labelPaint;
131 labelPaint.setColor(color);
132 labelPaint.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -0400133 sk_tool_utils::set_portable_typeface(&labelPaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000134 labelPaint.setLCDRenderText(true);
135 labelPaint.setTextSize(10 * SK_Scalar1);
136 canvas->drawText(gStyles[style].fName,
137 strlen(gStyles[style].fName),
138 0, rect.height() + 12 * SK_Scalar1,
139 labelPaint);
140 canvas->drawText(gFills[fill].fName,
141 strlen(gFills[fill].fName),
142 0, rect.height() + 24 * SK_Scalar1,
143 labelPaint);
144 canvas->drawText(gCaps[cap].fName,
145 strlen(gCaps[cap].fName),
146 0, rect.height() + 36 * SK_Scalar1,
147 labelPaint);
148 }
149 canvas->restore();
150 }
151 canvas->restore();
152 }
153 canvas->restore();
154 canvas->restore();
155 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000156
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000157private:
158 typedef GM INHERITED;
159};
160
161class LineClosePathGM : public GM {
162public:
163 LineClosePathGM() {}
164
165protected:
166 SkString onShortName() {
167 return SkString("lineclosepath");
168 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000169
tfarinaf5393182014-06-09 23:59:03 -0700170 SkISize onISize() { return SkISize::Make(1240, 390); }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000171
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000172 void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
173 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
174 SkPaint::Style style, SkPath::FillType fill,
175 SkScalar strokeWidth) {
176 path.setFillType(fill);
177 SkPaint paint;
178 paint.setStrokeCap(cap);
179 paint.setStrokeWidth(strokeWidth);
180 paint.setStrokeJoin(join);
181 paint.setColor(color);
182 paint.setStyle(style);
183 canvas->save();
184 canvas->clipRect(clip);
185 canvas->drawPath(path, paint);
186 canvas->restore();
187 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000188
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000189 virtual void onDraw(SkCanvas* canvas) {
190 struct FillAndName {
191 SkPath::FillType fFill;
192 const char* fName;
193 };
194 static const FillAndName gFills[] = {
195 {SkPath::kWinding_FillType, "Winding"},
196 {SkPath::kEvenOdd_FillType, "Even / Odd"},
197 {SkPath::kInverseWinding_FillType, "Inverse Winding"},
198 {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
199 };
200 struct StyleAndName {
201 SkPaint::Style fStyle;
202 const char* fName;
203 };
204 static const StyleAndName gStyles[] = {
205 {SkPaint::kFill_Style, "Fill"},
206 {SkPaint::kStroke_Style, "Stroke"},
207 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
208 };
209 struct CapAndName {
210 SkPaint::Cap fCap;
211 SkPaint::Join fJoin;
212 const char* fName;
213 };
214 static const CapAndName gCaps[] = {
215 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
216 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
217 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
218 };
219 struct PathAndName {
220 SkPath fPath;
221 const char* fName;
222 };
223 PathAndName path;
224 path.fPath.moveTo(25*SK_Scalar1, 15*SK_Scalar1);
225 path.fPath.lineTo(75*SK_Scalar1, 15*SK_Scalar1);
226 path.fPath.close();
227 path.fName = "moveTo-line-close";
228
229 SkPaint titlePaint;
230 titlePaint.setColor(SK_ColorBLACK);
231 titlePaint.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -0400232 sk_tool_utils::set_portable_typeface(&titlePaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000233 titlePaint.setLCDRenderText(true);
234 titlePaint.setTextSize(15 * SK_Scalar1);
235 const char title[] = "Line Closed Drawn Into Rectangle Clips With "
236 "Indicated Style, Fill and Linecaps, with stroke width 10";
237 canvas->drawText(title, strlen(title),
238 20 * SK_Scalar1,
239 20 * SK_Scalar1,
240 titlePaint);
241
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000242 SkLCGRandom rand;
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000243 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
244 canvas->save();
245 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
246 canvas->save();
247 for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
248 if (0 < cap) {
249 canvas->translate((rect.width() + 40 * SK_Scalar1) * SK_ARRAY_COUNT(gStyles), 0);
250 }
251 canvas->save();
252 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
253 if (0 < fill) {
254 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
255 }
256 canvas->save();
257 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
258 if (0 < style) {
259 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
260 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000261
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000262 SkColor color = 0xff007000;
263 this->drawPath(path.fPath, canvas, color, rect,
264 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
265 gFills[fill].fFill, SK_Scalar1*10);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000266
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000267 SkPaint rectPaint;
268 rectPaint.setColor(SK_ColorBLACK);
269 rectPaint.setStyle(SkPaint::kStroke_Style);
270 rectPaint.setStrokeWidth(-1);
271 rectPaint.setAntiAlias(true);
272 canvas->drawRect(rect, rectPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000273
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000274 SkPaint labelPaint;
275 labelPaint.setColor(color);
276 labelPaint.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -0400277 sk_tool_utils::set_portable_typeface(&labelPaint);
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000278 labelPaint.setLCDRenderText(true);
279 labelPaint.setTextSize(10 * SK_Scalar1);
280 canvas->drawText(gStyles[style].fName,
281 strlen(gStyles[style].fName),
282 0, rect.height() + 12 * SK_Scalar1,
283 labelPaint);
284 canvas->drawText(gFills[fill].fName,
285 strlen(gFills[fill].fName),
286 0, rect.height() + 24 * SK_Scalar1,
287 labelPaint);
288 canvas->drawText(gCaps[cap].fName,
289 strlen(gCaps[cap].fName),
290 0, rect.height() + 36 * SK_Scalar1,
291 labelPaint);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000292 }
293 canvas->restore();
294 }
295 canvas->restore();
296 }
297 canvas->restore();
298 canvas->restore();
299 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000300
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000301private:
302 typedef GM INHERITED;
303};
304
305//////////////////////////////////////////////////////////////////////////////
306
schenney@chromium.org45cbfdd2011-12-20 21:48:14 +0000307static GM* LinePathFactory(void*) { return new LinePathGM; }
308static GMRegistry regLinePath(LinePathFactory);
309
310static GM* LineClosePathFactory(void*) { return new LineClosePathGM; }
311static GMRegistry regLineClosePath(LineClosePathFactory);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000312
313}