blob: 7e4e6bc6f04a6e681f5baa931296caa1c5864d5f [file] [log] [blame]
reed@android.com3abec1d2009-03-02 05:36:20 +00001#include "Test.h"
2#include "SkPath.h"
reed@google.com04863fa2011-05-15 04:08:24 +00003#include "SkParse.h"
reed@android.com60bc6d52010-02-11 11:09:39 +00004#include "SkSize.h"
reed@android.com3abec1d2009-03-02 05:36:20 +00005
reed@google.com7c424812011-05-15 04:38:34 +00006static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
7 SkPath::Convexity expected) {
8 SkPath::Convexity c = SkPath::ComputeConvexity(path);
9 REPORTER_ASSERT(reporter, c == expected);
10}
11
12static void test_convexity2(skiatest::Reporter* reporter) {
13 SkPath pt;
14 pt.moveTo(0, 0);
15 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +000016 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
reed@google.com7c424812011-05-15 04:38:34 +000017
18 SkPath line;
19 line.moveTo(12, 20);
20 line.lineTo(-12, -20);
21 line.close();
reed@google.comb54455e2011-05-16 14:16:04 +000022 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
reed@google.com7c424812011-05-15 04:38:34 +000023
24 SkPath triLeft;
25 triLeft.moveTo(0, 0);
26 triLeft.lineTo(1, 0);
27 triLeft.lineTo(1, 1);
28 triLeft.close();
29 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
30
31 SkPath triRight;
32 triRight.moveTo(0, 0);
33 triRight.lineTo(-1, 0);
34 triRight.lineTo(1, 1);
35 triRight.close();
36 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
37
38 SkPath square;
39 square.moveTo(0, 0);
40 square.lineTo(1, 0);
41 square.lineTo(1, 1);
42 square.lineTo(0, 1);
43 square.close();
44 check_convexity(reporter, square, SkPath::kConvex_Convexity);
45
46 SkPath redundantSquare;
47 redundantSquare.moveTo(0, 0);
48 redundantSquare.lineTo(0, 0);
49 redundantSquare.lineTo(0, 0);
50 redundantSquare.lineTo(1, 0);
51 redundantSquare.lineTo(1, 0);
52 redundantSquare.lineTo(1, 0);
53 redundantSquare.lineTo(1, 1);
54 redundantSquare.lineTo(1, 1);
55 redundantSquare.lineTo(1, 1);
56 redundantSquare.lineTo(0, 1);
57 redundantSquare.lineTo(0, 1);
58 redundantSquare.lineTo(0, 1);
59 redundantSquare.close();
60 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
61
62 SkPath bowTie;
63 bowTie.moveTo(0, 0);
64 bowTie.lineTo(0, 0);
65 bowTie.lineTo(0, 0);
66 bowTie.lineTo(1, 1);
67 bowTie.lineTo(1, 1);
68 bowTie.lineTo(1, 1);
69 bowTie.lineTo(1, 0);
70 bowTie.lineTo(1, 0);
71 bowTie.lineTo(1, 0);
72 bowTie.lineTo(0, 1);
73 bowTie.lineTo(0, 1);
74 bowTie.lineTo(0, 1);
75 bowTie.close();
76 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
77
78 SkPath spiral;
79 spiral.moveTo(0, 0);
epoger@google.com2047f002011-05-17 17:36:59 +000080 spiral.lineTo(100, 0);
81 spiral.lineTo(100, 100);
82 spiral.lineTo(0, 100);
83 spiral.lineTo(0, 50);
84 spiral.lineTo(50, 50);
85 spiral.lineTo(50, 75);
reed@google.com7c424812011-05-15 04:38:34 +000086 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +000087 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
reed@google.com7c424812011-05-15 04:38:34 +000088
89 SkPath dent;
epoger@google.com1f753992011-05-18 20:23:30 +000090 dent.moveTo(SkIntToScalar(0), SkIntToScalar(0));
91 dent.lineTo(SkIntToScalar(100), SkIntToScalar(100));
92 dent.lineTo(SkIntToScalar(0), SkIntToScalar(100));
93 dent.lineTo(SkIntToScalar(-50), SkIntToScalar(200));
94 dent.lineTo(SkIntToScalar(-200), SkIntToScalar(100));
reed@google.com7c424812011-05-15 04:38:34 +000095 dent.close();
96 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
97}
98
reed@android.com6b82d1a2009-06-03 02:35:01 +000099static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
100 const SkRect& bounds) {
101 REPORTER_ASSERT(reporter, p.isConvex());
102 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +0000103
reed@android.com6b82d1a2009-06-03 02:35:01 +0000104 SkPath p2(p);
105 REPORTER_ASSERT(reporter, p2.isConvex());
106 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
107
108 SkPath other;
109 other.swap(p2);
110 REPORTER_ASSERT(reporter, other.isConvex());
111 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
112}
113
reed@google.com04863fa2011-05-15 04:08:24 +0000114static void setFromString(SkPath* path, const char str[]) {
115 bool first = true;
116 while (str) {
117 SkScalar x, y;
118 str = SkParse::FindScalar(str, &x);
119 if (NULL == str) {
120 break;
121 }
122 str = SkParse::FindScalar(str, &y);
123 SkASSERT(str);
124 if (first) {
125 path->moveTo(x, y);
126 first = false;
127 } else {
128 path->lineTo(x, y);
129 }
130 }
131}
132
133static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +0000134 static const SkPath::Convexity C = SkPath::kConcave_Convexity;
135 static const SkPath::Convexity V = SkPath::kConvex_Convexity;
136
137 SkPath path;
138
reed@google.comb54455e2011-05-16 14:16:04 +0000139 REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path));
reed@google.com04863fa2011-05-15 04:08:24 +0000140 path.addCircle(0, 0, 10);
141 REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path));
142 path.addCircle(0, 0, 10); // 2nd circle
143 REPORTER_ASSERT(reporter, C == SkPath::ComputeConvexity(path));
144 path.reset();
145 path.addRect(0, 0, 10, 10, SkPath::kCCW_Direction);
146 REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path));
147 path.reset();
148 path.addRect(0, 0, 10, 10, SkPath::kCW_Direction);
149 REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path));
150
151 static const struct {
152 const char* fPathStr;
153 SkPath::Convexity fExpectedConvexity;
154 } gRec[] = {
reed@google.comb54455e2011-05-16 14:16:04 +0000155 { "", SkPath::kConvex_Convexity },
156 { "0 0", SkPath::kConvex_Convexity },
157 { "0 0 10 10", SkPath::kConvex_Convexity },
reed@google.com85b6e392011-05-15 20:25:17 +0000158 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity },
reed@google.com04863fa2011-05-15 04:08:24 +0000159 { "0 0 10 10 10 20", SkPath::kConvex_Convexity },
160 { "0 0 10 10 10 0", SkPath::kConvex_Convexity },
161 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity },
162 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity },
163 };
164
165 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
166 SkPath path;
167 setFromString(&path, gRec[i].fPathStr);
168 SkPath::Convexity c = SkPath::ComputeConvexity(path);
169 REPORTER_ASSERT(reporter, c == gRec[i].fExpectedConvexity);
170 }
171}
172
173void TestPath(skiatest::Reporter* reporter);
174void TestPath(skiatest::Reporter* reporter) {
reed@android.com60bc6d52010-02-11 11:09:39 +0000175 {
176 SkSize size;
177 size.fWidth = 3.4f;
178 size.width();
179 size = SkSize::Make(3,4);
180 SkISize isize = SkISize::Make(3,4);
181 }
182
183 SkTSize<SkScalar>::Make(3,4);
184
reed@android.com3abec1d2009-03-02 05:36:20 +0000185 SkPath p, p2;
186 SkRect bounds, bounds2;
reed@android.com80e39a72009-04-02 16:59:40 +0000187
reed@android.com3abec1d2009-03-02 05:36:20 +0000188 REPORTER_ASSERT(reporter, p.isEmpty());
reed@google.comb54455e2011-05-16 14:16:04 +0000189 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +0000190 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
191 REPORTER_ASSERT(reporter, !p.isInverseFillType());
192 REPORTER_ASSERT(reporter, p == p2);
193 REPORTER_ASSERT(reporter, !(p != p2));
194
reed@android.comd252db02009-04-01 18:31:44 +0000195 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +0000196
reed@android.com3abec1d2009-03-02 05:36:20 +0000197 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +0000198
reed@android.com6b82d1a2009-06-03 02:35:01 +0000199 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
200 check_convex_bounds(reporter, p, bounds);
reed@google.com62047cf2011-02-07 19:39:09 +0000201
reed@android.com6b82d1a2009-06-03 02:35:01 +0000202 p.reset();
reed@android.com6b82d1a2009-06-03 02:35:01 +0000203 p.addOval(bounds);
204 check_convex_bounds(reporter, p, bounds);
reed@google.com62047cf2011-02-07 19:39:09 +0000205
reed@android.com6b82d1a2009-06-03 02:35:01 +0000206 p.reset();
reed@android.com3abec1d2009-03-02 05:36:20 +0000207 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +0000208 check_convex_bounds(reporter, p, bounds);
reed@android.com3abec1d2009-03-02 05:36:20 +0000209
210 REPORTER_ASSERT(reporter, p != p2);
211 REPORTER_ASSERT(reporter, !(p == p2));
212
213 // does getPoints return the right result
214 REPORTER_ASSERT(reporter, p.getPoints(NULL, 5) == 4);
215 SkPoint pts[4];
216 int count = p.getPoints(pts, 4);
217 REPORTER_ASSERT(reporter, count == 4);
218 bounds2.set(pts, 4);
219 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +0000220
reed@android.com3abec1d2009-03-02 05:36:20 +0000221 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
222 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +0000223 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +0000224
225#if 0 // isRect needs to be implemented
226 REPORTER_ASSERT(reporter, p.isRect(NULL));
227 bounds.setEmpty();
228 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
229 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +0000230
reed@android.com3abec1d2009-03-02 05:36:20 +0000231 // now force p to not be a rect
232 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
233 p.addRect(bounds);
234 REPORTER_ASSERT(reporter, !p.isRect(NULL));
235#endif
236
237 SkPoint pt;
238
239 p.moveTo(SK_Scalar1, 0);
240 p.getLastPt(&pt);
241 REPORTER_ASSERT(reporter, pt.fX == SK_Scalar1);
reed@google.com62047cf2011-02-07 19:39:09 +0000242
reed@google.com04863fa2011-05-15 04:08:24 +0000243 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +0000244 test_convexity2(reporter);
reed@android.com3abec1d2009-03-02 05:36:20 +0000245}
246
247#include "TestClassDef.h"
248DEFINE_TESTCLASS("Path", PathTestClass, TestPath)