Debian Codegraph @

gm/cubicpaths.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/SkMatrix.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkPath.h"
15 #include "include/core/SkPathBuilder.h"
16 #include "include/core/SkPoint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkSize.h"
20 #include "include/core/SkString.h"
21 #include "include/core/SkTypeface.h"
22 #include "include/core/SkTypes.h"
23 #include "include/effects/SkGradient.h"
24 #include "src/base/SkRandom.h"
25 #include "src/core/SkColorPriv.h"
26 #include "tools/ToolUtils.h"
27 #include "tools/fonts/FontToolUtils.h"
28
29 // skbug.com/40032398 shows that this cubic, when slightly clipped, creates big
30 // (incorrect) changes to its control points.
31 class ClippedCubicGM : public skiagm::GM {
32 SkString getName() const override { return SkString("clippedcubic"); }
33
34 SkISize getISize() override { return {1240, 390}; }
35
36 void onDraw(SkCanvas* canvas) override {
37 SkPath path = SkPathBuilder()
38 .moveTo(0, 0)
39 .cubicTo(140, 150, 40, 10, 170, 150)
40 .detach();
41
42 SkPaint paint;
43 SkRect bounds = path.getBounds();
44
45 for (SkScalar dy = -1; dy <= 1; dy += 1) {
46 canvas->save();
47 for (SkScalar dx = -1; dx <= 1; dx += 1) {
48 canvas->save();
49 canvas->clipRect(bounds);
50 canvas->translate(dx, dy);
51 canvas->drawPath(path, paint);
52 canvas->restore();
53
54 canvas->translate(bounds.width(), 0);
55 }
56 canvas->restore();
57 canvas->translate(0, bounds.height());
58 }
59 }
60 };
61
62
63 class ClippedCubic2GM : public skiagm::GM {
64 SkString getName() const override { return SkString("clippedcubic2"); }
65
66 SkISize getISize() override { return {1240, 390}; }
67
68 void onDraw(SkCanvas* canvas) override {
69 canvas->save();
70 canvas->translate(-2, 120);
71 drawOne(canvas, fPath, SkRect::MakeLTRB(0, 0, 80, 150));
72 canvas->translate(0, 170);
73 drawOne(canvas, fPath, SkRect::MakeLTRB(0, 0, 80, 100));
74 canvas->translate(0, 170);
75 drawOne(canvas, fPath, SkRect::MakeLTRB(0, 0, 30, 150));
76 canvas->translate(0, 170);
77 drawOne(canvas, fPath, SkRect::MakeLTRB(0, 0, 10, 150));
78 canvas->restore();
79 canvas->save();
80 canvas->translate(20, -2);
81 drawOne(canvas, fFlipped, SkRect::MakeLTRB(0, 0, 150, 80));
82 canvas->translate(170, 0);
83 drawOne(canvas, fFlipped, SkRect::MakeLTRB(0, 0, 100, 80));
84 canvas->translate(170, 0);
85 drawOne(canvas, fFlipped, SkRect::MakeLTRB(0, 0, 150, 30));
86 canvas->translate(170, 0);
87 drawOne(canvas, fFlipped, SkRect::MakeLTRB(0, 0, 150, 10));
88 canvas->restore();
89 }
90
91 void drawOne(SkCanvas* canvas, const SkPath& path, const SkRect& clip) {
92 SkPaint framePaint, fillPaint;
93 framePaint.setStyle(SkPaint::kStroke_Style);
94 canvas->drawRect(clip, framePaint);
95 canvas->drawPath(path, framePaint);
96 canvas->save();
97 canvas->clipRect(clip);
98 canvas->drawPath(path, fillPaint);
99 canvas->restore();
100 }
101
102 void onOnceBeforeDraw() override {
103 SkPathBuilder builder;
104 builder.moveTo(69.7030518991886f, 0);
105 builder.cubicTo( 69.7030518991886f, 21.831149999999997f,
106 58.08369508178456f, 43.66448333333333f, 34.8449814469765f, 65.5f);
107 builder.cubicTo( 11.608591683531916f, 87.33115f, -0.010765133872116195f, 109.16448333333332f,
108 -0.013089005235602302f, 131);
109 builder.close();
110 fPath = builder.detach();
111
112 SkMatrix matrix;
113 matrix.reset();
114 matrix.setScaleX(0);
115 matrix.setScaleY(0);
116 matrix.setSkewX(1);
117 matrix.setSkewY(1);
118 fFlipped = fPath.makeTransform(matrix);
119 }
120
121 SkPath fPath;
122 SkPath fFlipped;
123 private:
124 using INHERITED = skiagm::GM;
125 };
126
127 class CubicPathGM : public skiagm::GM {
128 SkString getName() const override { return SkString("cubicpath"); }
129
130 SkISize getISize() override { return {1240, 390}; }
131
132 void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
133 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
134 SkPaint::Style style, SkPathFillType fill,
135 SkScalar strokeWidth) {
136 path.setFillType(fill);
137 SkPaint paint;
138 paint.setStrokeCap(cap);
139 paint.setStrokeWidth(strokeWidth);
140 paint.setStrokeJoin(join);
141 paint.setColor(color);
142 paint.setStyle(style);
143 canvas->save();
144 canvas->clipRect(clip);
145 canvas->drawPath(path, paint);
146 canvas->restore();
147 }
148
149 void onDraw(SkCanvas* canvas) override {
150 struct FillAndName {
151 SkPathFillType fFill;
152 const char* fName;
153 };
154 constexpr FillAndName gFills[] = {
155 {SkPathFillType::kWinding, "Winding"},
156 {SkPathFillType::kEvenOdd, "Even / Odd"},
157 {SkPathFillType::kInverseWinding, "Inverse Winding"},
158 {SkPathFillType::kInverseEvenOdd, "Inverse Even / Odd"},
159 };
160 struct StyleAndName {
161 SkPaint::Style fStyle;
162 const char* fName;
163 };
164 constexpr StyleAndName gStyles[] = {
165 {SkPaint::kFill_Style, "Fill"},
166 {SkPaint::kStroke_Style, "Stroke"},
167 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
168 };
169 struct CapAndName {
170 SkPaint::Cap fCap;
171 SkPaint::Join fJoin;
172 const char* fName;
173 };
174 constexpr CapAndName gCaps[] = {
175 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
176 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
177 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
178 };
179 struct PathAndName {
180 SkPath fPath;
181 const char* fName;
182 };
183 PathAndName path;
184 path.fPath = SkPathBuilder()
185 .moveTo(25*SK_Scalar1, 10*SK_Scalar1)
186 .cubicTo(40*SK_Scalar1, 20*SK_Scalar1,
187 60*SK_Scalar1, 20*SK_Scalar1,
188 75*SK_Scalar1, 10*SK_Scalar1)
189 .detach();
190 path.fName = "moveTo-cubic";
191
192 SkPaint titlePaint;
193 titlePaint.setColor(SK_ColorBLACK);
194 titlePaint.setAntiAlias(true);
195 SkFont font(ToolUtils::DefaultPortableTypeface(), 15);
196 const char title[] = "Cubic Drawn Into Rectangle Clips With "
197 "Indicated Style, Fill and Linecaps, with stroke width 10";
198 canvas->drawString(title, 20, 20, font, titlePaint);
199
200 SkRandom rand;
201 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
202 canvas->save();
203 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
204 canvas->save();
205 for (size_t cap = 0; cap < std::size(gCaps); ++cap) {
206 if (0 < cap) {
207 canvas->translate((rect.width() + 40 * SK_Scalar1) * std::size(gStyles), 0);
208 }
209 canvas->save();
210 for (size_t fill = 0; fill < std::size(gFills); ++fill) {
211 if (0 < fill) {
212 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
213 }
214 canvas->save();
215 for (size_t style = 0; style < std::size(gStyles); ++style) {
216 if (0 < style) {
217 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
218 }
219
220 SkColor color = 0xff007000;
221 this->drawPath(path.fPath, canvas, color, rect,
222 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
223 gFills[fill].fFill, SK_Scalar1*10);
224
225 SkPaint rectPaint;
226 rectPaint.setColor(SK_ColorBLACK);
227 rectPaint.setStyle(SkPaint::kStroke_Style);
228 rectPaint.setStrokeWidth(-1);
229 rectPaint.setAntiAlias(true);
230 canvas->drawRect(rect, rectPaint);
231
232 SkPaint labelPaint;
233 labelPaint.setColor(color);
234 font.setSize(10);
235 canvas->drawString(gStyles[style].fName, 0, rect.height() + 12, font, labelPaint);
236 canvas->drawString(gFills[fill].fName, 0, rect.height() + 24, font, labelPaint);
237 canvas->drawString(gCaps[cap].fName, 0, rect.height() + 36, font, labelPaint);
238 }
239 canvas->restore();
240 }
241 canvas->restore();
242 }
243 canvas->restore();
244 canvas->restore();
245 }
246 };
247
248 class CubicClosePathGM : public skiagm::GM {
249 SkString getName() const override { return SkString("cubicclosepath"); }
250
251 SkISize getISize() override { return {1240, 390}; }
252
253 void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
254 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
255 SkPaint::Style style, SkPathFillType fill,
256 SkScalar strokeWidth) {
257 path.setFillType(fill);
258 SkPaint paint;
259 paint.setStrokeCap(cap);
260 paint.setStrokeWidth(strokeWidth);
261 paint.setStrokeJoin(join);
262 paint.setColor(color);
263 paint.setStyle(style);
264 canvas->save();
265 canvas->clipRect(clip);
266 canvas->drawPath(path, paint);
267 canvas->restore();
268 }
269
270 void onDraw(SkCanvas* canvas) override {
271 struct FillAndName {
272 SkPathFillType fFill;
273 const char* fName;
274 };
275 constexpr FillAndName gFills[] = {
276 {SkPathFillType::kWinding, "Winding"},
277 {SkPathFillType::kEvenOdd, "Even / Odd"},
278 {SkPathFillType::kInverseWinding, "Inverse Winding"},
279 {SkPathFillType::kInverseEvenOdd, "Inverse Even / Odd"},
280 };
281 struct StyleAndName {
282 SkPaint::Style fStyle;
283 const char* fName;
284 };
285 constexpr StyleAndName gStyles[] = {
286 {SkPaint::kFill_Style, "Fill"},
287 {SkPaint::kStroke_Style, "Stroke"},
288 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
289 };
290 struct CapAndName {
291 SkPaint::Cap fCap;
292 SkPaint::Join fJoin;
293 const char* fName;
294 };
295 constexpr CapAndName gCaps[] = {
296 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
297 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
298 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
299 };
300 struct PathAndName {
301 SkPath fPath;
302 const char* fName;
303 };
304 PathAndName path;
305 path.fPath = SkPathBuilder()
306 .moveTo(25*SK_Scalar1, 10*SK_Scalar1)
307 .cubicTo(40*SK_Scalar1, 20*SK_Scalar1,
308 60*SK_Scalar1, 20*SK_Scalar1,
309 75*SK_Scalar1, 10*SK_Scalar1)
310 .close()
311 .detach();
312 path.fName = "moveTo-cubic-close";
313
314 SkPaint titlePaint;
315 titlePaint.setColor(SK_ColorBLACK);
316 titlePaint.setAntiAlias(true);
317 SkFont font(ToolUtils::DefaultPortableTypeface(), 15);
318 const char title[] = "Cubic Closed Drawn Into Rectangle Clips With "
319 "Indicated Style, Fill and Linecaps, with stroke width 10";
320 canvas->drawString(title, 20, 20, font, titlePaint);
321
322 SkRandom rand;
323 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
324 canvas->save();
325 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
326 canvas->save();
327 for (size_t cap = 0; cap < std::size(gCaps); ++cap) {
328 if (0 < cap) {
329 canvas->translate((rect.width() + 40 * SK_Scalar1) * std::size(gStyles), 0);
330 }
331 canvas->save();
332 for (size_t fill = 0; fill < std::size(gFills); ++fill) {
333 if (0 < fill) {
334 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
335 }
336 canvas->save();
337 for (size_t style = 0; style < std::size(gStyles); ++style) {
338 if (0 < style) {
339 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
340 }
341
342 SkColor color = 0xff007000;
343 this->drawPath(path.fPath, canvas, color, rect,
344 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
345 gFills[fill].fFill, SK_Scalar1*10);
346
347 SkPaint rectPaint;
348 rectPaint.setColor(SK_ColorBLACK);
349 rectPaint.setStyle(SkPaint::kStroke_Style);
350 rectPaint.setStrokeWidth(-1);
351 rectPaint.setAntiAlias(true);
352 canvas->drawRect(rect, rectPaint);
353
354 SkPaint labelPaint;
355 labelPaint.setColor(color);
356 labelPaint.setAntiAlias(true);
357 font.setSize(10);
358 canvas->drawString(gStyles[style].fName, 0, rect.height() + 12, font, labelPaint);
359 canvas->drawString(gFills[fill].fName, 0, rect.height() + 24, font, labelPaint);
360 canvas->drawString(gCaps[cap].fName, 0, rect.height() + 36, font, labelPaint);
361 }
362 canvas->restore();
363 }
364 canvas->restore();
365 }
366 canvas->restore();
367 canvas->restore();
368 }
369 };
370
371 class CubicPathShaderGM : public skiagm::GM {
372 SkString getName() const override { return SkString("cubicpath_shader"); }
373
374 SkISize getISize() override { return {1240, 390}; }
375
376 void drawPath(SkPath& path,SkCanvas* canvas,
377 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
378 SkPaint::Style style, SkPathFillType fill,
379 SkScalar strokeWidth) {
380 const SkScalar s = 50.f;
381 const SkPoint kPts[] = { { 0, 0 }, { s, s } };
382 const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
383
384 SkColorConverter conv({0x80F00080, 0xF0F08000, 0x800080F0 });
385 path.setFillType(fill);
386
387 SkPaint paint;
388 paint.setStrokeCap(cap);
389 paint.setStrokeWidth(strokeWidth);
390 paint.setStrokeJoin(join);
391 paint.setShader(SkShaders::LinearGradient(kPts,
392 {{conv.colors4f(), kPos, SkTileMode::kClamp}, {}}));
393 paint.setStyle(style);
394 canvas->save();
395 canvas->clipRect(clip);
396 canvas->drawPath(path, paint);
397 canvas->restore();
398 }
399
400 void onDraw(SkCanvas* canvas) override {
401 struct FillAndName {
402 SkPathFillType fFill;
403 const char* fName;
404 };
405 constexpr FillAndName gFills[] = {
406 {SkPathFillType::kWinding, "Winding"},
407 {SkPathFillType::kEvenOdd, "Even / Odd"},
408 {SkPathFillType::kInverseWinding, "Inverse Winding"},
409 {SkPathFillType::kInverseEvenOdd, "Inverse Even / Odd"},
410 };
411 struct StyleAndName {
412 SkPaint::Style fStyle;
413 const char* fName;
414 };
415 constexpr StyleAndName gStyles[] = {
416 {SkPaint::kFill_Style, "Fill"},
417 {SkPaint::kStroke_Style, "Stroke"},
418 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
419 };
420 struct CapAndName {
421 SkPaint::Cap fCap;
422 SkPaint::Join fJoin;
423 const char* fName;
424 };
425 constexpr CapAndName gCaps[] = {
426 {SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"},
427 {SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"},
428 {SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"}
429 };
430 struct PathAndName {
431 SkPath fPath;
432 const char* fName;
433 };
434 PathAndName path;
435 path.fPath = SkPathBuilder()
436 .moveTo(25*SK_Scalar1, 10*SK_Scalar1)
437 .cubicTo(40*SK_Scalar1, 20*SK_Scalar1,
438 60*SK_Scalar1, 20*SK_Scalar1,
439 75*SK_Scalar1, 10*SK_Scalar1)
440 .detach();
441 path.fName = "moveTo-cubic";
442
443 SkPaint titlePaint;
444 titlePaint.setColor(SK_ColorBLACK);
445 titlePaint.setAntiAlias(true);
446 SkFont font(ToolUtils::DefaultPortableTypeface(), 15);
447 const char title[] = "Cubic Drawn Into Rectangle Clips With "
448 "Indicated Style, Fill and Linecaps, with stroke width 10";
449 canvas->drawString(title, 20, 20, font, titlePaint);
450
451 SkRandom rand;
452 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
453 canvas->save();
454 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
455 canvas->save();
456 for (size_t cap = 0; cap < std::size(gCaps); ++cap) {
457 if (0 < cap) {
458 canvas->translate((rect.width() + 40 * SK_Scalar1) * std::size(gStyles), 0);
459 }
460 canvas->save();
461 for (size_t fill = 0; fill < std::size(gFills); ++fill) {
462 if (0 < fill) {
463 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
464 }
465 canvas->save();
466 for (size_t style = 0; style < std::size(gStyles); ++style) {
467 if (0 < style) {
468 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
469 }
470
471 SkColor color = 0xff007000;
472 this->drawPath(path.fPath, canvas, rect,
473 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
474 gFills[fill].fFill, SK_Scalar1*10);
475
476 SkPaint rectPaint;
477 rectPaint.setColor(SK_ColorBLACK);
478 rectPaint.setStyle(SkPaint::kStroke_Style);
479 rectPaint.setStrokeWidth(-1);
480 rectPaint.setAntiAlias(true);
481 canvas->drawRect(rect, rectPaint);
482
483 SkPaint labelPaint;
484 labelPaint.setColor(color);
485 font.setSize(10);
486 canvas->drawString(gStyles[style].fName, 0, rect.height() + 12, font, labelPaint);
487 canvas->drawString(gFills[fill].fName, 0, rect.height() + 24, font, labelPaint);
488 canvas->drawString(gCaps[cap].fName, 0, rect.height() + 36, font, labelPaint);
489 }
490 canvas->restore();
491 }
492 canvas->restore();
493 }
494 canvas->restore();
495 canvas->restore();
496 }
497 };
498
499 DEF_SIMPLE_GM(bug5099, canvas, 50, 50) {
500 SkPaint p;
501 p.setColor(SK_ColorRED);
502 p.setAntiAlias(true);
503 p.setStyle(SkPaint::kStroke_Style);
504 p.setStrokeWidth(10);
505
506 SkPath path = SkPathBuilder()
507 .moveTo(6, 27)
508 .cubicTo(31.5f, 1.5f, 3.5f, 4.5f, 29, 29)
509 .detach();
510 canvas->drawPath(path, p);
511 }
512
513 DEF_SIMPLE_GM(bug6083, canvas, 100, 50) {
514 SkPaint p;
515 p.setColor(SK_ColorRED);
516 p.setAntiAlias(true);
517 p.setStyle(SkPaint::kStroke_Style);
518 p.setStrokeWidth(15);
519 canvas->translate(-500, -130);
520
521 SkPathBuilder builder;
522 builder.moveTo(500.988f, 155.200f).lineTo(526.109f, 155.200f);
523
524 SkPath path = builder.snapshot();
525 SkPoint p1 = { 526.109f, 155.200f };
526 SkPoint p2 = { 525.968f, 212.968f };
527 SkPoint p3 = { 526.109f, 241.840f };
528
529 builder.cubicTo(p1, p2, p3);
530 canvas->drawPath(builder.detach(), p);
531 canvas->translate(50, 0);
532
533 p2.set(525.968f, 213.172f);
534 builder.moveTo(500.988f, 155.200f);
535 builder.lineTo(526.109f, 155.200f);
536 builder.cubicTo(p1, p2, p3);
537 canvas->drawPath(builder.detach(), p);
538 }
539
540 //////////////////////////////////////////////////////////////////////////////
541
542 DEF_GM( return new CubicPathGM; )
543 DEF_GM( return new CubicPathShaderGM; )
544 DEF_GM( return new CubicClosePathGM; )
545 DEF_GM( return new ClippedCubicGM; )
546 DEF_GM( return new ClippedCubic2GM; )