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 
8 #include "gm.h"
9 #include "sk_tool_utils.h"
10 #include "SkCanvas.h"
11 #include "SkPaint.h"
12 #include "SkPath.h"
13 #include "SkRandom.h"
14 
15 namespace skiagm {
16 
17 class QuadPathGM : public GM {
18 public:
QuadPathGM()19     QuadPathGM() {}
20 
21 protected:
22 
onShortName()23     SkString onShortName() override {
24         return SkString("quadpath");
25     }
26 
onISize()27     SkISize onISize() override { return SkISize::Make(1240, 390); }
28 
drawPath(SkPath & path,SkCanvas * canvas,SkColor color,const SkRect & clip,SkPaint::Cap cap,SkPaint::Join join,SkPaint::Style style,SkPath::FillType fill,SkScalar strokeWidth)29     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     }
45 
onDraw(SkCanvas * canvas)46     void onDraw(SkCanvas* canvas) override {
47         struct FillAndName {
48             SkPath::FillType fFill;
49             const char*      fName;
50         };
51         constexpr 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         constexpr 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         constexpr 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, 10*SK_Scalar1);
82         path.fPath.quadTo(50*SK_Scalar1, 20*SK_Scalar1,
83                           75*SK_Scalar1, 10*SK_Scalar1);
84         path.fName = "moveTo-quad";
85 
86         SkPaint titlePaint;
87         titlePaint.setColor(SK_ColorBLACK);
88         titlePaint.setAntiAlias(true);
89         sk_tool_utils::set_portable_typeface(&titlePaint);
90         titlePaint.setTextSize(15 * SK_Scalar1);
91         const char title[] = "Quad 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 
98         SkRandom rand;
99         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);
116                     }
117 
118                     SkColor color = sk_tool_utils::color_to_565(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);
122 
123                     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);
129 
130                     SkPaint labelPaint;
131                     labelPaint.setColor(color);
132                     labelPaint.setAntiAlias(true);
133                     sk_tool_utils::set_portable_typeface(&labelPaint);
134                     labelPaint.setTextSize(10 * SK_Scalar1);
135                     canvas->drawText(gStyles[style].fName,
136                                         strlen(gStyles[style].fName),
137                                         0, rect.height() + 12 * SK_Scalar1,
138                                         labelPaint);
139                     canvas->drawText(gFills[fill].fName,
140                                         strlen(gFills[fill].fName),
141                                         0, rect.height() + 24 * SK_Scalar1,
142                                         labelPaint);
143                     canvas->drawText(gCaps[cap].fName,
144                                         strlen(gCaps[cap].fName),
145                                         0, rect.height() + 36 * SK_Scalar1,
146                                         labelPaint);
147                 }
148                 canvas->restore();
149             }
150             canvas->restore();
151         }
152         canvas->restore();
153         canvas->restore();
154     }
155 
156 private:
157     typedef GM INHERITED;
158 };
159 
160 class QuadClosePathGM : public GM {
161 public:
QuadClosePathGM()162     QuadClosePathGM() {}
163 
164 protected:
165 
onShortName()166     SkString onShortName() override {
167         return SkString("quadclosepath");
168     }
169 
onISize()170     SkISize onISize() override { return SkISize::Make(1240, 390); }
171 
drawPath(SkPath & path,SkCanvas * canvas,SkColor color,const SkRect & clip,SkPaint::Cap cap,SkPaint::Join join,SkPaint::Style style,SkPath::FillType fill,SkScalar strokeWidth)172     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     }
188 
onDraw(SkCanvas * canvas)189     void onDraw(SkCanvas* canvas) override {
190         struct FillAndName {
191             SkPath::FillType fFill;
192             const char*      fName;
193         };
194         constexpr 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         constexpr 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         constexpr 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, 10*SK_Scalar1);
225         path.fPath.quadTo(50*SK_Scalar1, 20*SK_Scalar1,
226                           75*SK_Scalar1, 10*SK_Scalar1);
227         path.fPath.close();
228         path.fName = "moveTo-quad-close";
229 
230         SkPaint titlePaint;
231         titlePaint.setColor(SK_ColorBLACK);
232         titlePaint.setAntiAlias(true);
233         sk_tool_utils::set_portable_typeface(&titlePaint);
234         titlePaint.setTextSize(15 * SK_Scalar1);
235         const char title[] = "Quad 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 
242         SkRandom rand;
243         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                     }
261 
262                     SkColor color = sk_tool_utils::color_to_565(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);
266 
267                     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);
273 
274                     SkPaint labelPaint;
275                     labelPaint.setColor(color);
276                     labelPaint.setAntiAlias(true);
277                     sk_tool_utils::set_portable_typeface(&labelPaint);
278                     labelPaint.setTextSize(10 * SK_Scalar1);
279                     canvas->drawText(gStyles[style].fName,
280                                         strlen(gStyles[style].fName),
281                                         0, rect.height() + 12 * SK_Scalar1,
282                                         labelPaint);
283                     canvas->drawText(gFills[fill].fName,
284                                         strlen(gFills[fill].fName),
285                                         0, rect.height() + 24 * SK_Scalar1,
286                                         labelPaint);
287                     canvas->drawText(gCaps[cap].fName,
288                                         strlen(gCaps[cap].fName),
289                                         0, rect.height() + 36 * SK_Scalar1,
290                                         labelPaint);
291                 }
292                 canvas->restore();
293             }
294             canvas->restore();
295         }
296         canvas->restore();
297         canvas->restore();
298     }
299 
300 private:
301     typedef GM INHERITED;
302 };
303 
304 //////////////////////////////////////////////////////////////////////////////
305 
QuadPathFactory(void *)306 static GM* QuadPathFactory(void*) { return new QuadPathGM; }
307 static GMRegistry regQuadPath(QuadPathFactory);
308 
QuadClosePathFactory(void *)309 static GM* QuadClosePathFactory(void*) { return new QuadClosePathGM; }
310 static GMRegistry regQuadClosePath(QuadClosePathFactory);
311 
312 }
313