1 /*
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
12 namespace skiagm {
13
14 class LinePathGM : public GM {
15 public:
LinePathGM()16 LinePathGM() {}
17
18 protected:
19
onShortName()20 SkString onShortName() {
21 return SkString("linepath");
22 }
23
onISize()24 SkISize onISize() { return SkISize::Make(1240, 390); }
25
drawPath(SkPath & path,SkCanvas * canvas,SkColor color,const SkRect & clip,SkPaint::Cap cap,SkPaint::Join join,SkPaint::Style style,SkPath::FillType fill,SkScalar strokeWidth)26 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 }
42
onDraw(SkCanvas * canvas)43 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);
85 sk_tool_utils::set_portable_typeface(&titlePaint);
86 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
95 SkRandom rand;
96 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);
113 }
114
115 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);
119
120 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);
126
127 SkPaint labelPaint;
128 labelPaint.setColor(color);
129 labelPaint.setAntiAlias(true);
130 sk_tool_utils::set_portable_typeface(&labelPaint);
131 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 }
153
154 private:
155 typedef GM INHERITED;
156 };
157
158 class LineClosePathGM : public GM {
159 public:
LineClosePathGM()160 LineClosePathGM() {}
161
162 protected:
onShortName()163 SkString onShortName() {
164 return SkString("lineclosepath");
165 }
166
onISize()167 SkISize onISize() { return SkISize::Make(1240, 390); }
168
drawPath(SkPath & path,SkCanvas * canvas,SkColor color,const SkRect & clip,SkPaint::Cap cap,SkPaint::Join join,SkPaint::Style style,SkPath::FillType fill,SkScalar strokeWidth)169 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 }
185
onDraw(SkCanvas * canvas)186 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);
229 sk_tool_utils::set_portable_typeface(&titlePaint);
230 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
239 SkRandom rand;
240 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 }
258
259 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);
263
264 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);
270
271 SkPaint labelPaint;
272 labelPaint.setColor(color);
273 labelPaint.setAntiAlias(true);
274 sk_tool_utils::set_portable_typeface(&labelPaint);
275 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);
289 }
290 canvas->restore();
291 }
292 canvas->restore();
293 }
294 canvas->restore();
295 canvas->restore();
296 }
297
298 private:
299 typedef GM INHERITED;
300 };
301
302 //////////////////////////////////////////////////////////////////////////////
303
LinePathFactory(void *)304 static GM* LinePathFactory(void*) { return new LinePathGM; }
305 static GMRegistry regLinePath(LinePathFactory);
306
LineClosePathFactory(void *)307 static GM* LineClosePathFactory(void*) { return new LineClosePathGM; }
308 static GMRegistry regLineClosePath(LineClosePathFactory);
309
310 }
311