Debian Codegraph @

gm/quadpaths.cpp

raw
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/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPath.h"
14 #include "include/core/SkPathBuilder.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkSize.h"
18 #include "include/core/SkString.h"
19 #include "include/core/SkTypeface.h"
20 #include "include/core/SkTypes.h"
21 #include "src/base/SkRandom.h"
22 #include "tools/ToolUtils.h"
23 #include "tools/fonts/FontToolUtils.h"
24
25 namespace skiagm {
26
27 class QuadPathGM : public GM {
28 public:
29 QuadPathGM() {}
30
31 protected:
32 SkString getName() const override { return SkString("quadpath"); }
33
34 SkISize getISize() override { return SkISize::Make(1240, 390); }
35
36 void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
37 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
38 SkPaint::Style style, SkPathFillType fill,
39 SkScalar strokeWidth) {
40 path.setFillType(fill);
41 SkPaint paint;
42 paint.setStrokeCap(cap);
43 paint.setStrokeWidth(strokeWidth);
44 paint.setStrokeJoin(join);
45 paint.setColor(color);
46 paint.setStyle(style);
47 canvas->save();
48 canvas->clipRect(clip);
49 canvas->drawPath(path, paint);
50 canvas->restore();
51 }
52
53 void onDraw(SkCanvas* canvas) override {
54 struct FillAndName {
55 SkPathFillType fFill;
56 const char* fName;
57 };
58 constexpr FillAndName gFills[] = {
59 {SkPathFillType::kWinding, "Winding"},
60 {SkPathFillType::kEvenOdd, "Even / Odd"},
61 {SkPathFillType::kInverseWinding, "Inverse Winding"},
62 {SkPathFillType::kInverseEvenOdd, "Inverse Even / Odd"},
63 };
64 struct StyleAndName {
65 SkPaint::Style fStyle;
66 const char* fName;
67 };
68 constexpr StyleAndName gStyles[] = {
69 {SkPaint::kFill_Style, "Fill"},
70 {SkPaint::kStroke_Style, "Stroke"},
71 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
72 };
73 struct CapAndName {
74 SkPaint::Cap fCap;
75 SkPaint::Join fJoin;
76 const char* fName;
77 };
78 constexpr CapAndName gCaps[] = {
79 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
80 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
81 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
82 };
83 struct PathAndName {
84 SkPath fPath;
85 const char* fName;
86 };
87 PathAndName path;
88 path.fPath = SkPathBuilder()
89 .moveTo(25*SK_Scalar1, 10*SK_Scalar1)
90 .quadTo(50*SK_Scalar1, 20*SK_Scalar1,
91 75*SK_Scalar1, 10*SK_Scalar1)
92 .detach();
93 path.fName = "moveTo-quad";
94
95 SkPaint titlePaint;
96 SkFont font(ToolUtils::DefaultPortableTypeface(), 15);
97 SkFont labelFont(ToolUtils::DefaultPortableTypeface(), 10);
98
99 const char title[] = "Quad Drawn Into Rectangle Clips With "
100 "Indicated Style, Fill and Linecaps, with stroke width 10";
101 canvas->drawString(title, 20.0f, 20.0f, font, titlePaint);
102
103 SkRandom rand;
104 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
105 canvas->save();
106 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
107 canvas->save();
108 for (size_t cap = 0; cap < std::size(gCaps); ++cap) {
109 if (0 < cap) {
110 canvas->translate((rect.width() + 40 * SK_Scalar1) * std::size(gStyles), 0);
111 }
112 canvas->save();
113 for (size_t fill = 0; fill < std::size(gFills); ++fill) {
114 if (0 < fill) {
115 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
116 }
117 canvas->save();
118 for (size_t style = 0; style < std::size(gStyles); ++style) {
119 if (0 < style) {
120 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
121 }
122
123 SkColor color = ToolUtils::color_to_565(0xff007000);
124 this->drawPath(path.fPath, canvas, color, rect,
125 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
126 gFills[fill].fFill, SK_Scalar1*10);
127
128 SkPaint rectPaint;
129 rectPaint.setColor(SK_ColorBLACK);
130 rectPaint.setStyle(SkPaint::kStroke_Style);
131 rectPaint.setStrokeWidth(-1);
132 rectPaint.setAntiAlias(true);
133 canvas->drawRect(rect, rectPaint);
134
135 SkPaint labelPaint;
136 labelPaint.setColor(color);
137 canvas->drawString(gStyles[style].fName, 0, rect.height() + 12.0f,
138 labelFont, labelPaint);
139 canvas->drawString(gFills[fill].fName, 0, rect.height() + 24.0f,
140 labelFont, labelPaint);
141 canvas->drawString(gCaps[cap].fName, 0, rect.height() + 36.0f,
142 labelFont, labelPaint);
143 }
144 canvas->restore();
145 }
146 canvas->restore();
147 }
148 canvas->restore();
149 canvas->restore();
150 }
151
152 private:
153 using INHERITED = GM;
154 };
155
156 class QuadClosePathGM : public GM {
157 public:
158 QuadClosePathGM() {}
159
160 protected:
161 SkString getName() const override { return SkString("quadclosepath"); }
162
163 SkISize getISize() override { return SkISize::Make(1240, 390); }
164
165 void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
166 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
167 SkPaint::Style style, SkPathFillType fill,
168 SkScalar strokeWidth) {
169 path.setFillType(fill);
170 SkPaint paint;
171 paint.setStrokeCap(cap);
172 paint.setStrokeWidth(strokeWidth);
173 paint.setStrokeJoin(join);
174 paint.setColor(color);
175 paint.setStyle(style);
176 canvas->save();
177 canvas->clipRect(clip);
178 canvas->drawPath(path, paint);
179 canvas->restore();
180 }
181
182 void onDraw(SkCanvas* canvas) override {
183 struct FillAndName {
184 SkPathFillType fFill;
185 const char* fName;
186 };
187 constexpr FillAndName gFills[] = {
188 {SkPathFillType::kWinding, "Winding"},
189 {SkPathFillType::kEvenOdd, "Even / Odd"},
190 {SkPathFillType::kInverseWinding, "Inverse Winding"},
191 {SkPathFillType::kInverseEvenOdd, "Inverse Even / Odd"},
192 };
193 struct StyleAndName {
194 SkPaint::Style fStyle;
195 const char* fName;
196 };
197 constexpr StyleAndName gStyles[] = {
198 {SkPaint::kFill_Style, "Fill"},
199 {SkPaint::kStroke_Style, "Stroke"},
200 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
201 };
202 struct CapAndName {
203 SkPaint::Cap fCap;
204 SkPaint::Join fJoin;
205 const char* fName;
206 };
207 constexpr CapAndName gCaps[] = {
208 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
209 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
210 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
211 };
212 struct PathAndName {
213 SkPath fPath;
214 const char* fName;
215 };
216 PathAndName path;
217 path.fPath = SkPathBuilder()
218 .moveTo(25*SK_Scalar1, 10*SK_Scalar1)
219 .quadTo(50*SK_Scalar1, 20*SK_Scalar1,
220 75*SK_Scalar1, 10*SK_Scalar1)
221 .close()
222 .detach();
223 path.fName = "moveTo-quad-close";
224
225 SkPaint titlePaint;
226 SkFont font(ToolUtils::DefaultPortableTypeface(), 15);
227 SkFont labelFont(ToolUtils::DefaultPortableTypeface(), 10);
228 const char title[] = "Quad Closed Drawn Into Rectangle Clips With "
229 "Indicated Style, Fill and Linecaps, with stroke width 10";
230 canvas->drawString(title, 20.0f, 20.0f, font, titlePaint);
231
232 SkRandom rand;
233 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
234 canvas->save();
235 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
236 canvas->save();
237 for (size_t cap = 0; cap < std::size(gCaps); ++cap) {
238 if (0 < cap) {
239 canvas->translate((rect.width() + 40 * SK_Scalar1) * std::size(gStyles), 0);
240 }
241 canvas->save();
242 for (size_t fill = 0; fill < std::size(gFills); ++fill) {
243 if (0 < fill) {
244 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
245 }
246 canvas->save();
247 for (size_t style = 0; style < std::size(gStyles); ++style) {
248 if (0 < style) {
249 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
250 }
251
252 SkColor color = ToolUtils::color_to_565(0xff007000);
253 this->drawPath(path.fPath, canvas, color, rect,
254 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
255 gFills[fill].fFill, SK_Scalar1*10);
256
257 SkPaint rectPaint;
258 rectPaint.setColor(SK_ColorBLACK);
259 rectPaint.setStyle(SkPaint::kStroke_Style);
260 rectPaint.setStrokeWidth(-1);
261 rectPaint.setAntiAlias(true);
262 canvas->drawRect(rect, rectPaint);
263
264 SkPaint labelPaint;
265 labelPaint.setColor(color);
266 canvas->drawString(gStyles[style].fName, 0, rect.height() + 12.0f,
267 labelFont, labelPaint);
268 canvas->drawString(gFills[fill].fName, 0, rect.height() + 24.0f,
269 labelFont, labelPaint);
270 canvas->drawString(gCaps[cap].fName, 0, rect.height() + 36.0f,
271 labelFont, labelPaint);
272 }
273 canvas->restore();
274 }
275 canvas->restore();
276 }
277 canvas->restore();
278 canvas->restore();
279 }
280
281 private:
282 using INHERITED = GM;
283 };
284
285 //////////////////////////////////////////////////////////////////////////////
286
287 DEF_GM( return new QuadPathGM; )
288
289 DEF_GM( return new QuadClosePathGM; )
290
291 } // namespace skiagm