blob: aac9bde1b324cf8c254ab19aed44d5a73ede5cf0 [file] [log] [blame]
caryclark55d49052016-01-23 05:07:04 -08001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gm.h"
9#include "SkParsePath.h"
10#include "SkPath.h"
11
12/*
13The arcto test below should draw the same as this SVG:
14(Note that Skia's arcTo Direction parameter value is opposite SVG's sweep value, e.g. 0 / 1)
15
16<svg width="500" height="600">
17<path d="M 50,100 A50,50, 0,0,1, 150,200" style="stroke:#660000; fill:none; stroke-width:2" />
18<path d="M100,100 A50,100, 0,0,1, 200,200" style="stroke:#660000; fill:none; stroke-width:2" />
19<path d="M150,100 A50,50, 45,0,1, 250,200" style="stroke:#660000; fill:none; stroke-width:2" />
20<path d="M200,100 A50,100, 45,0,1, 300,200" style="stroke:#660000; fill:none; stroke-width:2" />
21
22<path d="M150,200 A50,50, 0,1,0, 150,300" style="stroke:#660000; fill:none; stroke-width:2" />
23<path d="M200,200 A50,100, 0,1,0, 200,300" style="stroke:#660000; fill:none; stroke-width:2" />
24<path d="M250,200 A50,50, 45,1,0, 250,300" style="stroke:#660000; fill:none; stroke-width:2" />
25<path d="M300,200 A50,100, 45,1,0, 300,300" style="stroke:#660000; fill:none; stroke-width:2" />
26
27<path d="M250,400 A120,80 0 0,0 250,500"
28 fill="none" stroke="red" stroke-width="5" />
29
30<path d="M250,400 A120,80 0 1,1 250,500"
31 fill="none" stroke="green" stroke-width="5"/>
32
33<path d="M250,400 A120,80 0 1,0 250,500"
34 fill="none" stroke="purple" stroke-width="5"/>
35
36<path d="M250,400 A120,80 0 0,1 250,500"
37 fill="none" stroke="blue" stroke-width="5"/>
caryclarkfc752532016-01-25 05:39:04 -080038
39<path d="M100,100 A 0, 0 0 0,1 200,200"
40 fill="none" stroke="blue" stroke-width="5" stroke-linecap="round"/>
41
42<path d="M200,100 A 80,80 0 0,1 200,100"
43 fill="none" stroke="blue" stroke-width="5" stroke-linecap="round"/>
caryclark55d49052016-01-23 05:07:04 -080044</svg>
45 */
46
47DEF_SIMPLE_GM(arcto, canvas, 500, 600) {
48 SkPaint paint;
49 paint.setAntiAlias(true);
50 paint.setStyle(SkPaint::kStroke_Style);
51 paint.setStrokeWidth(2);
52 paint.setColor(0xFF660000);
caryclarkfc752532016-01-25 05:39:04 -080053// canvas->scale(2, 2); // for testing on retina
caryclark55d49052016-01-23 05:07:04 -080054 SkRect oval = SkRect::MakeXYWH(100, 100, 100, 100);
55 SkPath svgArc;
56
57 for (int angle = 0; angle <= 45; angle += 45) {
58 for (int oHeight = 2; oHeight >= 1; --oHeight) {
59 SkScalar ovalHeight = oval.height() / oHeight;
60 svgArc.moveTo(oval.fLeft, oval.fTop);
61 svgArc.arcTo(oval.width() / 2, ovalHeight, SkIntToScalar(angle), SkPath::kSmall_ArcSize,
62 SkPath::kCW_Direction, oval.right(), oval.bottom());
63 canvas->drawPath(svgArc, paint);
64 svgArc.reset();
65
66 svgArc.moveTo(oval.fLeft + 100, oval.fTop + 100);
67 svgArc.arcTo(oval.width() / 2, ovalHeight, SkIntToScalar(angle), SkPath::kLarge_ArcSize,
68 SkPath::kCCW_Direction, oval.right(), oval.bottom() + 100);
69 canvas->drawPath(svgArc, paint);
70 oval.offset(50, 0);
71 svgArc.reset();
72
73 }
74 }
75
76 paint.setStrokeWidth(5);
77 const SkColor purple = 0xFF800080;
78 const SkColor darkgreen = 0xFF008000;
79 const SkColor colors[] = { SK_ColorRED, darkgreen, purple, SK_ColorBLUE };
80 const char* arcstrs[] = {
81 "M250,400 A120,80 0 0,0 250,500",
82 "M250,400 A120,80 0 1,1 250,500",
83 "M250,400 A120,80 0 1,0 250,500",
84 "M250,400 A120,80 0 0,1 250,500"
85 };
86 int cIndex = 0;
87 for (const char* arcstr : arcstrs) {
88 SkParsePath::FromSVGString(arcstr, &svgArc);
89 paint.setColor(colors[cIndex++]);
90 canvas->drawPath(svgArc, paint);
91 }
caryclarkfc752532016-01-25 05:39:04 -080092
93 // test that zero length arcs still draw round cap
94 paint.setStrokeCap(SkPaint::kRound_Cap);
95 SkPath path;
96 path.moveTo(100, 100);
97 path.arcTo(0, 0, 0, SkPath::kLarge_ArcSize, SkPath::kCW_Direction, 200, 200);
98 canvas->drawPath(path, paint);
99
100 path.reset();
101 path.moveTo(200, 100);
102 path.arcTo(80, 80, 0, SkPath::kLarge_ArcSize, SkPath::kCW_Direction, 200, 100);
103 canvas->drawPath(path, paint);
caryclark55d49052016-01-23 05:07:04 -0800104}