blob: 627ed181b0c5a4c538c45a7efd8b95ed78b69ff7 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com3abec1d2009-03-02 05:36:20 +00008#include "Test.h"
reed@google.com8cae8352012-09-14 15:18:41 +00009#include "SkCanvas.h"
reed@google.com55b5f4b2011-09-07 12:23:41 +000010#include "SkPaint.h"
reed@android.com3abec1d2009-03-02 05:36:20 +000011#include "SkPath.h"
reed@google.com04863fa2011-05-15 04:08:24 +000012#include "SkParse.h"
reed@google.com3e71a882012-01-10 18:44:37 +000013#include "SkParsePath.h"
reed@google.com8b06f1a2012-05-29 12:03:46 +000014#include "SkPathEffect.h"
schenney@chromium.org6630d8d2012-01-04 21:05:51 +000015#include "SkRandom.h"
reed@google.com53effc52011-09-21 19:05:12 +000016#include "SkReader32.h"
reed@android.com60bc6d52010-02-11 11:09:39 +000017#include "SkSize.h"
reed@google.com53effc52011-09-21 19:05:12 +000018#include "SkWriter32.h"
reed@google.com8cae8352012-09-14 15:18:41 +000019#include "SkSurface.h"
20
21static SkSurface* new_surface(int w, int h) {
22 SkImage::Info info = {
23 w, h, SkImage::kPMColor_ColorType, SkImage::kPremul_AlphaType
24 };
25 return SkSurface::NewRaster(info, NULL);
26}
27
reed@google.coma8790de2012-10-24 21:04:04 +000028// Make sure we stay non-finite once we get there (unless we reset or rewind).
29static void test_addrect_isfinite(skiatest::Reporter* reporter) {
30 SkPath path;
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +000031
reed@google.coma8790de2012-10-24 21:04:04 +000032 path.addRect(SkRect::MakeWH(50, 100));
33 REPORTER_ASSERT(reporter, path.isFinite());
34
35 path.moveTo(0, 0);
36 path.lineTo(SK_ScalarInfinity, 42);
37 REPORTER_ASSERT(reporter, !path.isFinite());
38
39 path.addRect(SkRect::MakeWH(50, 100));
40 REPORTER_ASSERT(reporter, !path.isFinite());
41
42 path.reset();
43 REPORTER_ASSERT(reporter, path.isFinite());
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +000044
reed@google.coma8790de2012-10-24 21:04:04 +000045 path.addRect(SkRect::MakeWH(50, 100));
46 REPORTER_ASSERT(reporter, path.isFinite());
47}
48
reed@google.com8cae8352012-09-14 15:18:41 +000049// Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
50// which triggered an assert, from a tricky cubic. This test replicates that
51// example, so we can ensure that we handle it (in SkEdge.cpp), and don't
52// assert in the SK_DEBUG build.
53static void test_tricky_cubic(skiatest::Reporter* reporter) {
54 const SkPoint pts[] = {
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +000055 { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
56 { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
57 { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
58 { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
reed@google.com8cae8352012-09-14 15:18:41 +000059 };
60
61 SkPath path;
62 path.moveTo(pts[0]);
63 path.cubicTo(pts[1], pts[2], pts[3]);
64
65 SkPaint paint;
66 paint.setAntiAlias(true);
67
68 SkSurface* surface = new_surface(19, 130);
69 surface->getCanvas()->drawPath(path, paint);
70 surface->unref();
71}
reed@android.com3abec1d2009-03-02 05:36:20 +000072
tomhudson@google.comed02c4d2012-08-10 14:10:45 +000073// Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
74//
75static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
76 SkPath path;
77 path.quadTo(157, 366, 286, 208);
78 path.arcTo(37, 442, 315, 163, 957494590897113.0f);
rmistry@google.comd6176b02012-08-23 18:14:13 +000079
tomhudson@google.comed02c4d2012-08-10 14:10:45 +000080 SkMatrix matrix;
81 matrix.setScale(1000*1000, 1000*1000);
82
83 // Be sure that path::transform correctly updates isFinite and the bounds
84 // if the transformation overflows. The previous bug was that isFinite was
85 // set to true in this case, but the bounds were not set to empty (which
86 // they should be).
87 while (path.isFinite()) {
88 REPORTER_ASSERT(reporter, path.getBounds().isFinite());
89 REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
90 path.transform(matrix);
91 }
92 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
93
94 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
95 path.transform(matrix);
96 // we need to still be non-finite
97 REPORTER_ASSERT(reporter, !path.isFinite());
98 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
99}
100
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000101static void add_corner_arc(SkPath* path, const SkRect& rect,
102 SkScalar xIn, SkScalar yIn,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000103 int startAngle)
104{
105
106 SkScalar rx = SkMinScalar(rect.width(), xIn);
107 SkScalar ry = SkMinScalar(rect.height(), yIn);
108
109 SkRect arcRect;
110 arcRect.set(-rx, -ry, rx, ry);
111 switch (startAngle) {
112 case 0:
113 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
114 break;
115 case 90:
116 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
117 break;
118 case 180:
119 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
120 break;
121 case 270:
122 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
123 break;
124 default:
125 break;
126 }
127
128 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
129}
130
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000131static void make_arb_round_rect(SkPath* path, const SkRect& r,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000132 SkScalar xCorner, SkScalar yCorner) {
133 // we are lazy here and use the same x & y for each corner
134 add_corner_arc(path, r, xCorner, yCorner, 270);
135 add_corner_arc(path, r, xCorner, yCorner, 0);
136 add_corner_arc(path, r, xCorner, yCorner, 90);
137 add_corner_arc(path, r, xCorner, yCorner, 180);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000138 path->close();
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000139}
140
141// Chrome creates its own round rects with each corner possibly being different.
142// Performance will suffer if they are not convex.
143// Note: PathBench::ArbRoundRectBench performs almost exactly
144// the same test (but with drawing)
145static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
146 SkRandom rand;
147 SkRect r;
148
149 for (int i = 0; i < 5000; ++i) {
150
robertphillips@google.com158618e2012-10-23 16:56:56 +0000151 SkScalar size = rand.nextUScalar1() * 30;
152 if (size < SK_Scalar1) {
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000153 continue;
154 }
155 r.fLeft = rand.nextUScalar1() * 300;
156 r.fTop = rand.nextUScalar1() * 300;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000157 r.fRight = r.fLeft + 2 * size;
158 r.fBottom = r.fTop + 2 * size;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000159
160 SkPath temp;
161
162 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
163
robertphillips@google.comc7a37c72012-10-19 01:26:18 +0000164#ifdef SK_REDEFINE_ROOT2OVER2_TO_MAKE_ARCTOS_CONVEX
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000165 REPORTER_ASSERT(reporter, temp.isConvex());
robertphillips@google.comc7a37c72012-10-19 01:26:18 +0000166#endif
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000167 }
168}
169
robertphillips@google.com158618e2012-10-23 16:56:56 +0000170// Chrome will sometimes create a 0 radius round rect. The degenerate
171// quads prevent the path from being converted to a rect
172// Note: PathBench::ArbRoundRectBench performs almost exactly
173// the same test (but with drawing)
174static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
175 SkRandom rand;
176 SkRect r;
177
178 for (int i = 0; i < 5000; ++i) {
179
180 SkScalar size = rand.nextUScalar1() * 30;
181 if (size < SK_Scalar1) {
182 continue;
183 }
184 r.fLeft = rand.nextUScalar1() * 300;
185 r.fTop = rand.nextUScalar1() * 300;
186 r.fRight = r.fLeft + 2 * size;
187 r.fBottom = r.fTop + 2 * size;
188
189 SkPath temp;
190
191 make_arb_round_rect(&temp, r, 0, 0);
192
193#ifdef SK_REDEFINE_ROOT2OVER2_TO_MAKE_ARCTOS_CONVEX
194 SkRect result;
195 REPORTER_ASSERT(reporter, temp.isRect(&result));
196 REPORTER_ASSERT(reporter, r == result);
197#endif
198 }
199}
200
reed@google.com0bb18bb2012-07-26 15:20:36 +0000201static void test_rect_isfinite(skiatest::Reporter* reporter) {
202 const SkScalar inf = SK_ScalarInfinity;
203 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000204
reed@google.com0bb18bb2012-07-26 15:20:36 +0000205 SkRect r;
206 r.setEmpty();
207 REPORTER_ASSERT(reporter, r.isFinite());
208 r.set(0, 0, inf, -inf);
209 REPORTER_ASSERT(reporter, !r.isFinite());
210 r.set(0, 0, nan, 0);
211 REPORTER_ASSERT(reporter, !r.isFinite());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000212
reed@google.com0bb18bb2012-07-26 15:20:36 +0000213 SkPoint pts[] = {
214 { 0, 0 },
215 { SK_Scalar1, 0 },
216 { 0, SK_Scalar1 },
217 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000218
reed@google.com0bb18bb2012-07-26 15:20:36 +0000219 bool isFine = r.setBoundsCheck(pts, 3);
220 REPORTER_ASSERT(reporter, isFine);
221 REPORTER_ASSERT(reporter, !r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000222
reed@google.com0bb18bb2012-07-26 15:20:36 +0000223 pts[1].set(inf, 0);
224 isFine = r.setBoundsCheck(pts, 3);
225 REPORTER_ASSERT(reporter, !isFine);
226 REPORTER_ASSERT(reporter, r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000227
reed@google.com0bb18bb2012-07-26 15:20:36 +0000228 pts[1].set(nan, 0);
229 isFine = r.setBoundsCheck(pts, 3);
230 REPORTER_ASSERT(reporter, !isFine);
231 REPORTER_ASSERT(reporter, r.isEmpty());
232}
233
234static void test_path_isfinite(skiatest::Reporter* reporter) {
235 const SkScalar inf = SK_ScalarInfinity;
236 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000237
reed@google.com0bb18bb2012-07-26 15:20:36 +0000238 SkPath path;
239 REPORTER_ASSERT(reporter, path.isFinite());
240
241 path.reset();
242 REPORTER_ASSERT(reporter, path.isFinite());
243
244 path.reset();
245 path.moveTo(SK_Scalar1, 0);
246 REPORTER_ASSERT(reporter, path.isFinite());
247
248 path.reset();
249 path.moveTo(inf, -inf);
250 REPORTER_ASSERT(reporter, !path.isFinite());
251
252 path.reset();
253 path.moveTo(nan, 0);
254 REPORTER_ASSERT(reporter, !path.isFinite());
255}
256
257static void test_isfinite(skiatest::Reporter* reporter) {
258 test_rect_isfinite(reporter);
259 test_path_isfinite(reporter);
260}
261
reed@google.com744faba2012-05-29 19:54:52 +0000262// assert that we always
263// start with a moveTo
264// only have 1 moveTo
265// only have Lines after that
266// end with a single close
267// only have (at most) 1 close
268//
269static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
270 const SkPoint srcPts[], int count, bool expectClose) {
271 SkPath::RawIter iter(path);
272 SkPoint pts[4];
reed@google.com744faba2012-05-29 19:54:52 +0000273
274 bool firstTime = true;
275 bool foundClose = false;
276 for (;;) {
277 switch (iter.next(pts)) {
278 case SkPath::kMove_Verb:
279 REPORTER_ASSERT(reporter, firstTime);
280 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
281 srcPts++;
282 firstTime = false;
283 break;
284 case SkPath::kLine_Verb:
285 REPORTER_ASSERT(reporter, !firstTime);
286 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
287 srcPts++;
288 break;
289 case SkPath::kQuad_Verb:
290 REPORTER_ASSERT(reporter, !"unexpected quad verb");
291 break;
292 case SkPath::kCubic_Verb:
293 REPORTER_ASSERT(reporter, !"unexpected cubic verb");
294 break;
295 case SkPath::kClose_Verb:
296 REPORTER_ASSERT(reporter, !firstTime);
297 REPORTER_ASSERT(reporter, !foundClose);
298 REPORTER_ASSERT(reporter, expectClose);
299 foundClose = true;
300 break;
301 case SkPath::kDone_Verb:
302 goto DONE;
303 }
304 }
305DONE:
306 REPORTER_ASSERT(reporter, foundClose == expectClose);
307}
308
309static void test_addPoly(skiatest::Reporter* reporter) {
310 SkPoint pts[32];
311 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000312
reed@google.com744faba2012-05-29 19:54:52 +0000313 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
314 pts[i].fX = rand.nextSScalar1();
315 pts[i].fY = rand.nextSScalar1();
316 }
317
318 for (int doClose = 0; doClose <= 1; ++doClose) {
319 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
320 SkPath path;
321 path.addPoly(pts, count, SkToBool(doClose));
322 test_poly(reporter, path, pts, count, SkToBool(doClose));
323 }
324 }
325}
326
reed@google.com8b06f1a2012-05-29 12:03:46 +0000327static void test_strokerec(skiatest::Reporter* reporter) {
328 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
329 REPORTER_ASSERT(reporter, rec.isFillStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000330
reed@google.com8b06f1a2012-05-29 12:03:46 +0000331 rec.setHairlineStyle();
332 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000333
reed@google.com8b06f1a2012-05-29 12:03:46 +0000334 rec.setStrokeStyle(SK_Scalar1, false);
335 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000336
reed@google.com8b06f1a2012-05-29 12:03:46 +0000337 rec.setStrokeStyle(SK_Scalar1, true);
338 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000339
reed@google.com8b06f1a2012-05-29 12:03:46 +0000340 rec.setStrokeStyle(0, false);
341 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000342
reed@google.com8b06f1a2012-05-29 12:03:46 +0000343 rec.setStrokeStyle(0, true);
344 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
345}
346
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000347// Set this for paths that don't have a consistent direction such as a bowtie.
348// (cheapComputeDirection is not expected to catch these.)
349static const SkPath::Direction kDontCheckDir = static_cast<SkPath::Direction>(-1);
350
351static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
352 SkPath::Direction expected) {
353 if (expected == kDontCheckDir) {
354 return;
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000355 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000356 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
357
358 SkPath::Direction dir;
359 if (copy.cheapComputeDirection(&dir)) {
360 REPORTER_ASSERT(reporter, dir == expected);
361 } else {
362 REPORTER_ASSERT(reporter, SkPath::kUnknown_Direction == expected);
363 }
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000364}
365
reed@google.com3e71a882012-01-10 18:44:37 +0000366static void test_direction(skiatest::Reporter* reporter) {
367 size_t i;
368 SkPath path;
369 REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL));
370 REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCW_Direction));
371 REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCCW_Direction));
372
373 static const char* gDegen[] = {
374 "M 10 10",
375 "M 10 10 M 20 20",
376 "M 10 10 L 20 20",
377 "M 10 10 L 10 10 L 10 10",
378 "M 10 10 Q 10 10 10 10",
379 "M 10 10 C 10 10 10 10 10 10",
380 };
381 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
382 path.reset();
383 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
384 REPORTER_ASSERT(reporter, valid);
385 REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL));
386 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000387
reed@google.com3e71a882012-01-10 18:44:37 +0000388 static const char* gCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000389 "M 10 10 L 10 10 Q 20 10 20 20",
reed@google.com3e71a882012-01-10 18:44:37 +0000390 "M 10 10 C 20 10 20 20 20 20",
reed@google.comd4146662012-01-31 15:42:29 +0000391 "M 20 10 Q 20 20 30 20 L 10 20", // test double-back at y-max
bsalomon@google.com4eefe612012-07-10 18:28:12 +0000392 // rect with top two corners replaced by cubics with identical middle
393 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000394 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
395 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000396 };
397 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
398 path.reset();
399 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
400 REPORTER_ASSERT(reporter, valid);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000401 check_direction(reporter, path, SkPath::kCW_Direction);
reed@google.com3e71a882012-01-10 18:44:37 +0000402 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000403
reed@google.com3e71a882012-01-10 18:44:37 +0000404 static const char* gCCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000405 "M 10 10 L 10 10 Q 20 10 20 -20",
reed@google.com3e71a882012-01-10 18:44:37 +0000406 "M 10 10 C 20 10 20 -20 20 -20",
reed@google.comd4146662012-01-31 15:42:29 +0000407 "M 20 10 Q 20 20 10 20 L 30 20", // test double-back at y-max
bsalomon@google.com4eefe612012-07-10 18:28:12 +0000408 // rect with top two corners replaced by cubics with identical middle
409 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000410 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
411 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000412 };
413 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
414 path.reset();
415 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
416 REPORTER_ASSERT(reporter, valid);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000417 check_direction(reporter, path, SkPath::kCCW_Direction);
reed@google.com3e71a882012-01-10 18:44:37 +0000418 }
reed@google.comac8543f2012-01-30 20:51:25 +0000419
420 // Test two donuts, each wound a different direction. Only the outer contour
421 // determines the cheap direction
422 path.reset();
423 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
424 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000425 check_direction(reporter, path, SkPath::kCW_Direction);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000426
reed@google.comac8543f2012-01-30 20:51:25 +0000427 path.reset();
428 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
429 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000430 check_direction(reporter, path, SkPath::kCCW_Direction);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000431
bsalomon@google.com6843ac42012-02-17 13:49:03 +0000432#ifdef SK_SCALAR_IS_FLOAT
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000433 // triangle with one point really far from the origin.
434 path.reset();
435 // the first point is roughly 1.05e10, 1.05e10
bsalomon@google.com53aab782012-02-23 14:54:49 +0000436 path.moveTo(SkFloatToScalar(SkBits2Float(0x501c7652)), SkFloatToScalar(SkBits2Float(0x501c7652)));
437 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
438 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000439 check_direction(reporter, path, SkPath::kCCW_Direction);
bsalomon@google.com53aab782012-02-23 14:54:49 +0000440#endif
reed@google.com3e71a882012-01-10 18:44:37 +0000441}
442
reed@google.comffdb0182011-11-14 19:29:14 +0000443static void add_rect(SkPath* path, const SkRect& r) {
444 path->moveTo(r.fLeft, r.fTop);
445 path->lineTo(r.fRight, r.fTop);
446 path->lineTo(r.fRight, r.fBottom);
447 path->lineTo(r.fLeft, r.fBottom);
448 path->close();
449}
450
451static void test_bounds(skiatest::Reporter* reporter) {
452 static const SkRect rects[] = {
reed@google.com3563c9e2011-11-14 19:34:57 +0000453 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
454 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
455 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
456 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
reed@google.comffdb0182011-11-14 19:29:14 +0000457 };
458
459 SkPath path0, path1;
460 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
461 path0.addRect(rects[i]);
462 add_rect(&path1, rects[i]);
463 }
464
465 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
466}
467
reed@google.com55b5f4b2011-09-07 12:23:41 +0000468static void stroke_cubic(const SkPoint pts[4]) {
469 SkPath path;
470 path.moveTo(pts[0]);
471 path.cubicTo(pts[1], pts[2], pts[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000472
reed@google.com55b5f4b2011-09-07 12:23:41 +0000473 SkPaint paint;
474 paint.setStyle(SkPaint::kStroke_Style);
475 paint.setStrokeWidth(SK_Scalar1 * 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000476
reed@google.com55b5f4b2011-09-07 12:23:41 +0000477 SkPath fill;
478 paint.getFillPath(path, &fill);
479}
480
481// just ensure this can run w/o any SkASSERTS firing in the debug build
482// we used to assert due to differences in how we determine a degenerate vector
483// but that was fixed with the introduction of SkPoint::CanNormalize
484static void stroke_tiny_cubic() {
485 SkPoint p0[] = {
486 { 372.0f, 92.0f },
487 { 372.0f, 92.0f },
488 { 372.0f, 92.0f },
489 { 372.0f, 92.0f },
490 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000491
reed@google.com55b5f4b2011-09-07 12:23:41 +0000492 stroke_cubic(p0);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000493
reed@google.com55b5f4b2011-09-07 12:23:41 +0000494 SkPoint p1[] = {
495 { 372.0f, 92.0f },
496 { 372.0007f, 92.000755f },
497 { 371.99927f, 92.003922f },
498 { 371.99826f, 92.003899f },
499 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000500
reed@google.com55b5f4b2011-09-07 12:23:41 +0000501 stroke_cubic(p1);
502}
503
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000504static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
505 for (int i = 0; i < 2; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +0000506 SkPath::Iter iter(path, SkToBool(i));
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000507 SkPoint mv;
508 SkPoint pts[4];
509 SkPath::Verb v;
510 int nMT = 0;
511 int nCL = 0;
tomhudson@google.com221db3c2011-07-28 21:10:29 +0000512 mv.set(0, 0);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000513 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
514 switch (v) {
515 case SkPath::kMove_Verb:
516 mv = pts[0];
517 ++nMT;
518 break;
519 case SkPath::kClose_Verb:
520 REPORTER_ASSERT(reporter, mv == pts[0]);
521 ++nCL;
522 break;
523 default:
524 break;
525 }
526 }
527 // if we force a close on the interator we should have a close
528 // for every moveTo
529 REPORTER_ASSERT(reporter, !i || nMT == nCL);
530 }
531}
532
533static void test_close(skiatest::Reporter* reporter) {
534 SkPath closePt;
535 closePt.moveTo(0, 0);
536 closePt.close();
537 check_close(reporter, closePt);
538
539 SkPath openPt;
540 openPt.moveTo(0, 0);
541 check_close(reporter, openPt);
542
543 SkPath empty;
544 check_close(reporter, empty);
545 empty.close();
546 check_close(reporter, empty);
547
548 SkPath rect;
549 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
550 check_close(reporter, rect);
551 rect.close();
552 check_close(reporter, rect);
553
554 SkPath quad;
555 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
556 check_close(reporter, quad);
557 quad.close();
558 check_close(reporter, quad);
559
560 SkPath cubic;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000561 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000562 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
563 check_close(reporter, cubic);
564 cubic.close();
565 check_close(reporter, cubic);
566
567 SkPath line;
568 line.moveTo(SK_Scalar1, SK_Scalar1);
569 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
570 check_close(reporter, line);
571 line.close();
572 check_close(reporter, line);
573
574 SkPath rect2;
575 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
576 rect2.close();
577 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
578 check_close(reporter, rect2);
579 rect2.close();
580 check_close(reporter, rect2);
581
582 SkPath oval3;
583 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
584 oval3.close();
585 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
586 check_close(reporter, oval3);
587 oval3.close();
588 check_close(reporter, oval3);
589
590 SkPath moves;
591 moves.moveTo(SK_Scalar1, SK_Scalar1);
592 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
593 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
594 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
595 check_close(reporter, moves);
reed@google.com55b5f4b2011-09-07 12:23:41 +0000596
597 stroke_tiny_cubic();
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000598}
599
reed@google.com7c424812011-05-15 04:38:34 +0000600static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
601 SkPath::Convexity expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000602 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
603 SkPath::Convexity c = copy.getConvexity();
reed@google.com7c424812011-05-15 04:38:34 +0000604 REPORTER_ASSERT(reporter, c == expected);
605}
606
607static void test_convexity2(skiatest::Reporter* reporter) {
608 SkPath pt;
609 pt.moveTo(0, 0);
610 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +0000611 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000612 check_direction(reporter, pt, SkPath::kUnknown_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000613
reed@google.com7c424812011-05-15 04:38:34 +0000614 SkPath line;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000615 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
616 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000617 line.close();
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000618 check_convexity(reporter, line, SkPath::kConvex_Convexity);
619 check_direction(reporter, line, SkPath::kUnknown_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000620
reed@google.com7c424812011-05-15 04:38:34 +0000621 SkPath triLeft;
622 triLeft.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000623 triLeft.lineTo(SK_Scalar1, 0);
624 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000625 triLeft.close();
626 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000627 check_direction(reporter, triLeft, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000628
reed@google.com7c424812011-05-15 04:38:34 +0000629 SkPath triRight;
630 triRight.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000631 triRight.lineTo(-SK_Scalar1, 0);
632 triRight.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000633 triRight.close();
634 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000635 check_direction(reporter, triRight, SkPath::kCCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000636
reed@google.com7c424812011-05-15 04:38:34 +0000637 SkPath square;
638 square.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000639 square.lineTo(SK_Scalar1, 0);
640 square.lineTo(SK_Scalar1, SK_Scalar1);
641 square.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000642 square.close();
643 check_convexity(reporter, square, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000644 check_direction(reporter, square, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000645
reed@google.com7c424812011-05-15 04:38:34 +0000646 SkPath redundantSquare;
647 redundantSquare.moveTo(0, 0);
648 redundantSquare.lineTo(0, 0);
649 redundantSquare.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000650 redundantSquare.lineTo(SK_Scalar1, 0);
651 redundantSquare.lineTo(SK_Scalar1, 0);
652 redundantSquare.lineTo(SK_Scalar1, 0);
653 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
654 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
655 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
656 redundantSquare.lineTo(0, SK_Scalar1);
657 redundantSquare.lineTo(0, SK_Scalar1);
658 redundantSquare.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000659 redundantSquare.close();
660 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000661 check_direction(reporter, redundantSquare, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000662
reed@google.com7c424812011-05-15 04:38:34 +0000663 SkPath bowTie;
664 bowTie.moveTo(0, 0);
665 bowTie.lineTo(0, 0);
666 bowTie.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000667 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
668 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
669 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
670 bowTie.lineTo(SK_Scalar1, 0);
671 bowTie.lineTo(SK_Scalar1, 0);
672 bowTie.lineTo(SK_Scalar1, 0);
673 bowTie.lineTo(0, SK_Scalar1);
674 bowTie.lineTo(0, SK_Scalar1);
675 bowTie.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000676 bowTie.close();
677 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000678 check_direction(reporter, bowTie, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000679
reed@google.com7c424812011-05-15 04:38:34 +0000680 SkPath spiral;
681 spiral.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000682 spiral.lineTo(100*SK_Scalar1, 0);
683 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
684 spiral.lineTo(0, 100*SK_Scalar1);
685 spiral.lineTo(0, 50*SK_Scalar1);
686 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
687 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000688 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +0000689 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000690 check_direction(reporter, spiral, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000691
reed@google.com7c424812011-05-15 04:38:34 +0000692 SkPath dent;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000693 dent.moveTo(0, 0);
694 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
695 dent.lineTo(0, 100*SK_Scalar1);
696 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
697 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000698 dent.close();
699 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000700 check_direction(reporter, dent, SkPath::kCW_Direction);
reed@google.com7c424812011-05-15 04:38:34 +0000701}
702
reed@android.com6b82d1a2009-06-03 02:35:01 +0000703static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
704 const SkRect& bounds) {
705 REPORTER_ASSERT(reporter, p.isConvex());
706 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +0000707
reed@android.com6b82d1a2009-06-03 02:35:01 +0000708 SkPath p2(p);
709 REPORTER_ASSERT(reporter, p2.isConvex());
710 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
711
712 SkPath other;
713 other.swap(p2);
714 REPORTER_ASSERT(reporter, other.isConvex());
715 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
716}
717
reed@google.com04863fa2011-05-15 04:08:24 +0000718static void setFromString(SkPath* path, const char str[]) {
719 bool first = true;
720 while (str) {
721 SkScalar x, y;
722 str = SkParse::FindScalar(str, &x);
723 if (NULL == str) {
724 break;
725 }
726 str = SkParse::FindScalar(str, &y);
727 SkASSERT(str);
728 if (first) {
729 path->moveTo(x, y);
730 first = false;
731 } else {
732 path->lineTo(x, y);
733 }
734 }
735}
736
737static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +0000738 SkPath path;
739
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000740 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +0000741 path.addCircle(0, 0, SkIntToScalar(10));
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000742 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +0000743 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000744 check_convexity(reporter, path, SkPath::kConcave_Convexity);
745
reed@google.com04863fa2011-05-15 04:08:24 +0000746 path.reset();
reed@google.come3543972012-01-10 18:59:22 +0000747 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000748 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.com3e71a882012-01-10 18:44:37 +0000749 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCCW_Direction));
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000750
reed@google.com04863fa2011-05-15 04:08:24 +0000751 path.reset();
reed@google.come3543972012-01-10 18:59:22 +0000752 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000753 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.com3e71a882012-01-10 18:44:37 +0000754 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCW_Direction));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000755
reed@google.com04863fa2011-05-15 04:08:24 +0000756 static const struct {
757 const char* fPathStr;
758 SkPath::Convexity fExpectedConvexity;
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000759 SkPath::Direction fExpectedDirection;
reed@google.com04863fa2011-05-15 04:08:24 +0000760 } gRec[] = {
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000761 { "", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
762 { "0 0", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
763 { "0 0 10 10", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
764 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPath::kUnknown_Direction },
765 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPath::kCW_Direction },
766 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPath::kCCW_Direction },
767 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
768 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPath::kCW_Direction },
reed@google.com04863fa2011-05-15 04:08:24 +0000769 };
770
771 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
772 SkPath path;
773 setFromString(&path, gRec[i].fPathStr);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000774 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
775 check_direction(reporter, path, gRec[i].fExpectedDirection);
reed@google.com04863fa2011-05-15 04:08:24 +0000776 }
777}
778
reed@google.com7e6c4d12012-05-10 14:05:43 +0000779static void test_isLine(skiatest::Reporter* reporter) {
780 SkPath path;
781 SkPoint pts[2];
782 const SkScalar value = SkIntToScalar(5);
783
784 REPORTER_ASSERT(reporter, !path.isLine(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000785
reed@google.com7e6c4d12012-05-10 14:05:43 +0000786 // set some non-zero values
787 pts[0].set(value, value);
788 pts[1].set(value, value);
789 REPORTER_ASSERT(reporter, !path.isLine(pts));
790 // check that pts was untouched
791 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
792 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
793
794 const SkScalar moveX = SkIntToScalar(1);
795 const SkScalar moveY = SkIntToScalar(2);
796 SkASSERT(value != moveX && value != moveY);
797
798 path.moveTo(moveX, moveY);
799 REPORTER_ASSERT(reporter, !path.isLine(NULL));
800 REPORTER_ASSERT(reporter, !path.isLine(pts));
801 // check that pts was untouched
802 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
803 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
804
805 const SkScalar lineX = SkIntToScalar(2);
806 const SkScalar lineY = SkIntToScalar(2);
807 SkASSERT(value != lineX && value != lineY);
808
809 path.lineTo(lineX, lineY);
810 REPORTER_ASSERT(reporter, path.isLine(NULL));
811
812 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
813 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
814 REPORTER_ASSERT(reporter, path.isLine(pts));
815 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
816 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
817
818 path.lineTo(0, 0); // too many points/verbs
819 REPORTER_ASSERT(reporter, !path.isLine(NULL));
820 REPORTER_ASSERT(reporter, !path.isLine(pts));
821 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
822 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
823}
824
caryclark@google.comf1316942011-07-26 19:54:45 +0000825// Simple isRect test is inline TestPath, below.
826// test_isRect provides more extensive testing.
827static void test_isRect(skiatest::Reporter* reporter) {
828 // passing tests (all moveTo / lineTo...
829 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
830 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
831 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
832 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
833 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
834 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
835 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
836 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
837 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
838 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f},
839 {1, 0}, {.5f, 0}};
840 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1},
841 {0, 1}, {0, .5f}};
842 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
843 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
844 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
rmistry@google.comd6176b02012-08-23 18:14:13 +0000845
caryclark@google.comf1316942011-07-26 19:54:45 +0000846 // failing tests
847 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
848 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
849 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
850 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
851 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
852 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
853 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
854 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
rmistry@google.comd6176b02012-08-23 18:14:13 +0000855
caryclark@google.comf1316942011-07-26 19:54:45 +0000856 // failing, no close
857 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
858 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
859
860 size_t testLen[] = {
861 sizeof(r1), sizeof(r2), sizeof(r3), sizeof(r4), sizeof(r5), sizeof(r6),
862 sizeof(r7), sizeof(r8), sizeof(r9), sizeof(ra), sizeof(rb), sizeof(rc),
863 sizeof(rd), sizeof(re),
864 sizeof(f1), sizeof(f2), sizeof(f3), sizeof(f4), sizeof(f5), sizeof(f6),
865 sizeof(f7), sizeof(f8),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000866 sizeof(c1), sizeof(c2)
caryclark@google.comf1316942011-07-26 19:54:45 +0000867 };
868 SkPoint* tests[] = {
869 r1, r2, r3, r4, r5, r6, r7, r8, r9, ra, rb, rc, rd, re,
870 f1, f2, f3, f4, f5, f6, f7, f8,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000871 c1, c2
caryclark@google.comf1316942011-07-26 19:54:45 +0000872 };
873 SkPoint* lastPass = re;
874 SkPoint* lastClose = f8;
875 bool fail = false;
876 bool close = true;
877 const size_t testCount = sizeof(tests) / sizeof(tests[0]);
878 size_t index;
879 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
880 SkPath path;
881 path.moveTo(tests[testIndex][0].fX, tests[testIndex][0].fY);
882 for (index = 1; index < testLen[testIndex] / sizeof(SkPoint); ++index) {
883 path.lineTo(tests[testIndex][index].fX, tests[testIndex][index].fY);
884 }
885 if (close) {
886 path.close();
887 }
888 REPORTER_ASSERT(reporter, fail ^ path.isRect(0));
889 if (tests[testIndex] == lastPass) {
890 fail = true;
891 }
892 if (tests[testIndex] == lastClose) {
893 close = false;
894 }
895 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000896
caryclark@google.comf1316942011-07-26 19:54:45 +0000897 // fail, close then line
898 SkPath path1;
899 path1.moveTo(r1[0].fX, r1[0].fY);
900 for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
901 path1.lineTo(r1[index].fX, r1[index].fY);
902 }
903 path1.close();
904 path1.lineTo(1, 0);
905 REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000906
caryclark@google.comf1316942011-07-26 19:54:45 +0000907 // fail, move in the middle
908 path1.reset();
909 path1.moveTo(r1[0].fX, r1[0].fY);
910 for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
911 if (index == 2) {
912 path1.moveTo(1, .5f);
913 }
914 path1.lineTo(r1[index].fX, r1[index].fY);
915 }
916 path1.close();
917 REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
918
919 // fail, move on the edge
920 path1.reset();
921 for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
922 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
923 path1.lineTo(r1[index].fX, r1[index].fY);
924 }
925 path1.close();
926 REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000927
caryclark@google.comf1316942011-07-26 19:54:45 +0000928 // fail, quad
929 path1.reset();
930 path1.moveTo(r1[0].fX, r1[0].fY);
931 for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
932 if (index == 2) {
933 path1.quadTo(1, .5f, 1, .5f);
934 }
935 path1.lineTo(r1[index].fX, r1[index].fY);
936 }
937 path1.close();
938 REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000939
caryclark@google.comf1316942011-07-26 19:54:45 +0000940 // fail, cubic
941 path1.reset();
942 path1.moveTo(r1[0].fX, r1[0].fY);
943 for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
944 if (index == 2) {
945 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
946 }
947 path1.lineTo(r1[index].fX, r1[index].fY);
948 }
949 path1.close();
950 REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
951}
952
robertphillips@google.com2972bb52012-08-07 17:32:51 +0000953static void write_and_read_back(skiatest::Reporter* reporter,
954 const SkPath& p) {
955 SkWriter32 writer(100);
956 writer.writePath(p);
957 size_t size = writer.size();
958 SkAutoMalloc storage(size);
959 writer.flatten(storage.get());
960 SkReader32 reader(storage.get(), size);
961
962 SkPath readBack;
963 REPORTER_ASSERT(reporter, readBack != p);
964 reader.readPath(&readBack);
965 REPORTER_ASSERT(reporter, readBack == p);
966
rmistry@google.comd6176b02012-08-23 18:14:13 +0000967 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +0000968 p.getConvexityOrUnknown());
969
970 REPORTER_ASSERT(reporter, readBack.isOval(NULL) == p.isOval(NULL));
971
972 const SkRect& origBounds = p.getBounds();
973 const SkRect& readBackBounds = readBack.getBounds();
974
975 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
976}
977
reed@google.com53effc52011-09-21 19:05:12 +0000978static void test_flattening(skiatest::Reporter* reporter) {
979 SkPath p;
980
981 static const SkPoint pts[] = {
982 { 0, 0 },
983 { SkIntToScalar(10), SkIntToScalar(10) },
984 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
985 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
986 };
987 p.moveTo(pts[0]);
988 p.lineTo(pts[1]);
989 p.quadTo(pts[2], pts[3]);
990 p.cubicTo(pts[4], pts[5], pts[6]);
991
robertphillips@google.com2972bb52012-08-07 17:32:51 +0000992 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000993
994 // create a buffer that should be much larger than the path so we don't
995 // kill our stack if writer goes too far.
996 char buffer[1024];
997 uint32_t size1 = p.writeToMemory(NULL);
998 uint32_t size2 = p.writeToMemory(buffer);
999 REPORTER_ASSERT(reporter, size1 == size2);
1000
1001 SkPath p2;
1002 uint32_t size3 = p2.readFromMemory(buffer);
1003 REPORTER_ASSERT(reporter, size1 == size3);
1004 REPORTER_ASSERT(reporter, p == p2);
1005
1006 char buffer2[1024];
1007 size3 = p2.writeToMemory(buffer2);
1008 REPORTER_ASSERT(reporter, size1 == size3);
1009 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001010
1011 // test persistence of the oval flag & convexity
1012 {
1013 SkPath oval;
1014 SkRect rect = SkRect::MakeWH(10, 10);
1015 oval.addOval(rect);
1016
1017 write_and_read_back(reporter, oval);
1018 }
reed@google.com53effc52011-09-21 19:05:12 +00001019}
1020
1021static void test_transform(skiatest::Reporter* reporter) {
1022 SkPath p, p1;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001023
reed@google.com53effc52011-09-21 19:05:12 +00001024 static const SkPoint pts[] = {
1025 { 0, 0 },
1026 { SkIntToScalar(10), SkIntToScalar(10) },
1027 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
1028 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
1029 };
1030 p.moveTo(pts[0]);
1031 p.lineTo(pts[1]);
1032 p.quadTo(pts[2], pts[3]);
1033 p.cubicTo(pts[4], pts[5], pts[6]);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001034
reed@google.com53effc52011-09-21 19:05:12 +00001035 SkMatrix matrix;
1036 matrix.reset();
1037 p.transform(matrix, &p1);
1038 REPORTER_ASSERT(reporter, p == p1);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001039
reed@google.com53effc52011-09-21 19:05:12 +00001040 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
1041 p.transform(matrix, &p1);
1042 SkPoint pts1[7];
1043 int count = p1.getPoints(pts1, 7);
1044 REPORTER_ASSERT(reporter, 7 == count);
1045 for (int i = 0; i < count; ++i) {
1046 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
1047 REPORTER_ASSERT(reporter, newPt == pts1[i]);
1048 }
1049}
1050
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001051static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001052 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00001053 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001054
schenney@chromium.org7e963602012-06-13 17:05:43 +00001055 struct zeroPathTestData {
1056 const char* testPath;
1057 const size_t numResultPts;
1058 const SkRect resultBound;
1059 const SkPath::Verb* resultVerbs;
1060 const size_t numResultVerbs;
1061 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001062
schenney@chromium.org7e963602012-06-13 17:05:43 +00001063 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
1064 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
1065 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
1066 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
1067 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
1068 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
1069 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
1070 static const SkPath::Verb resultVerbs8[] = {
1071 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
1072 };
1073 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
1074 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
1075 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
1076 static const SkPath::Verb resultVerbs12[] = {
1077 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
1078 };
1079 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
1080 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
1081 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
1082 static const SkPath::Verb resultVerbs16[] = {
1083 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
1084 };
1085 static const struct zeroPathTestData gZeroLengthTests[] = {
1086 { "M 1 1", 1, {0, 0, 0, 0}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001087 { "M 1 1 M 2 1", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
schenney@chromium.org7e963602012-06-13 17:05:43 +00001088 { "M 1 1 z", 1, {0, 0, 0, 0}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001089 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
1090 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
1091 { "M 1 1 L 1 1 M 2 1 L 2 1", 4, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs6, SK_ARRAY_COUNT(resultVerbs6) },
1092 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
1093 { "M 1 1 L 1 1 z M 2 1 L 2 1 z", 4, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs8, SK_ARRAY_COUNT(resultVerbs8) },
1094 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
1095 { "M 1 1 Q 1 1 1 1 M 2 1 Q 2 1 2 1", 6, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs10, SK_ARRAY_COUNT(resultVerbs10) },
1096 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
1097 { "M 1 1 Q 1 1 1 1 z M 2 1 Q 2 1 2 1 z", 6, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs12, SK_ARRAY_COUNT(resultVerbs12) },
1098 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
1099 { "M 1 1 C 1 1 1 1 1 1 M 2 1 C 2 1 2 1 2 1", 8, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs14,
schenney@chromium.org7e963602012-06-13 17:05:43 +00001100 SK_ARRAY_COUNT(resultVerbs14)
1101 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001102 { "M 1 1 C 1 1 1 1 1 1 z", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs15, SK_ARRAY_COUNT(resultVerbs15) },
1103 { "M 1 1 C 1 1 1 1 1 1 z M 2 1 C 2 1 2 1 2 1 z", 8, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs16,
schenney@chromium.org7e963602012-06-13 17:05:43 +00001104 SK_ARRAY_COUNT(resultVerbs16)
1105 }
1106 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001107
schenney@chromium.org7e963602012-06-13 17:05:43 +00001108 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
1109 p.reset();
1110 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
1111 REPORTER_ASSERT(reporter, valid);
1112 REPORTER_ASSERT(reporter, !p.isEmpty());
1113 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
1114 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
1115 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
1116 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
1117 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
1118 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00001119 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001120}
1121
1122struct SegmentInfo {
1123 SkPath fPath;
1124 int fPointCount;
1125};
1126
reed@google.com10296cc2011-09-21 12:29:05 +00001127#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
1128
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001129static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00001130 SkPath p, p2;
1131
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001132 p.moveTo(0, 0);
1133 p.quadTo(100, 100, 200, 200);
1134 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
1135 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00001136 p2 = p;
1137 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001138 p.cubicTo(100, 100, 200, 200, 300, 300);
1139 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
1140 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00001141 p2 = p;
1142 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
1143
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001144 p.reset();
1145 p.moveTo(0, 0);
1146 p.cubicTo(100, 100, 200, 200, 300, 300);
1147 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00001148 p2 = p;
1149 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001150
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001151 REPORTER_ASSERT(reporter, !p.isEmpty());
1152}
1153
1154static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00001155 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001156 SkPoint pts[4];
1157
1158 // Test an iterator with no path
1159 SkPath::Iter noPathIter;
1160 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00001161
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001162 // Test that setting an empty path works
1163 noPathIter.setPath(p, false);
1164 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00001165
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001166 // Test that close path makes no difference for an empty path
1167 noPathIter.setPath(p, true);
1168 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00001169
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001170 // Test an iterator with an initial empty path
1171 SkPath::Iter iter(p, false);
1172 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1173
1174 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00001175 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001176 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1177
rmistry@google.comd6176b02012-08-23 18:14:13 +00001178
schenney@chromium.org7e963602012-06-13 17:05:43 +00001179 struct iterTestData {
1180 const char* testPath;
1181 const bool forceClose;
1182 const bool consumeDegenerates;
1183 const size_t* numResultPtsPerVerb;
1184 const SkPoint* resultPts;
1185 const SkPath::Verb* resultVerbs;
1186 const size_t numResultVerbs;
1187 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001188
schenney@chromium.org7e963602012-06-13 17:05:43 +00001189 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
1190 static const SkPath::Verb resultVerbs2[] = {
1191 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
1192 };
1193 static const SkPath::Verb resultVerbs3[] = {
1194 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
1195 };
1196 static const SkPath::Verb resultVerbs4[] = {
1197 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
1198 };
1199 static const SkPath::Verb resultVerbs5[] = {
1200 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
1201 };
1202 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00001203 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
1204 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
1205 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
1206 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001207 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00001208 static const SkPoint resultPts2[] = {
1209 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
1210 };
1211 static const SkPoint resultPts3[] = {
1212 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
1213 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
1214 };
1215 static const SkPoint resultPts4[] = {
1216 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
1217 };
1218 static const SkPoint resultPts5[] = {
1219 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
1220 };
1221 static const struct iterTestData gIterTests[] = {
1222 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001223 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1224 { "M 1 0 M 1 0 M 3 0 M 4 0 M 5 0", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.org7e963602012-06-13 17:05:43 +00001225 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1226 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1227 { "z M 1 0 z z M 2 0 z M 3 0 M 4 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1228 { "z M 1 0 z z M 2 0 z M 3 0 M 4 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001229 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
1230 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
1231 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1232 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1233 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
1234 { "M 1 0 L 1 0 M 0 0 z", true, false, resultPtsSizes5, resultPts5, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) }
schenney@chromium.org7e963602012-06-13 17:05:43 +00001235 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001236
schenney@chromium.org7e963602012-06-13 17:05:43 +00001237 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
1238 p.reset();
1239 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
1240 REPORTER_ASSERT(reporter, valid);
1241 iter.setPath(p, gIterTests[i].forceClose);
1242 int j = 0, l = 0;
1243 do {
1244 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
1245 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
1246 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
1247 }
1248 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
1249 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
1250 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001251
1252 // The GM degeneratesegments.cpp test is more extensive
1253}
1254
1255static void test_raw_iter(skiatest::Reporter* reporter) {
1256 SkPath p;
1257 SkPoint pts[4];
1258
1259 // Test an iterator with no path
1260 SkPath::RawIter noPathIter;
1261 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
1262 // Test that setting an empty path works
1263 noPathIter.setPath(p);
1264 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001265
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001266 // Test an iterator with an initial empty path
1267 SkPath::RawIter iter(p);
1268 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1269
1270 // Test that a move-only path returns the move.
1271 p.moveTo(SK_Scalar1, 0);
1272 iter.setPath(p);
1273 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1274 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
1275 REPORTER_ASSERT(reporter, pts[0].fY == 0);
1276 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1277
1278 // No matter how many moves we add, we should get them all back
1279 p.moveTo(SK_Scalar1*2, SK_Scalar1);
1280 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
1281 iter.setPath(p);
1282 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1283 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
1284 REPORTER_ASSERT(reporter, pts[0].fY == 0);
1285 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1286 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
1287 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
1288 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1289 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
1290 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
1291 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1292
1293 // Initial close is never ever stored
1294 p.reset();
1295 p.close();
1296 iter.setPath(p);
1297 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1298
1299 // Move/close sequences
1300 p.reset();
1301 p.close(); // Not stored, no purpose
1302 p.moveTo(SK_Scalar1, 0);
1303 p.close();
1304 p.close(); // Not stored, no purpose
1305 p.moveTo(SK_Scalar1*2, SK_Scalar1);
1306 p.close();
1307 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
1308 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
1309 p.close();
1310 iter.setPath(p);
1311 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1312 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
1313 REPORTER_ASSERT(reporter, pts[0].fY == 0);
1314 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
1315 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
1316 REPORTER_ASSERT(reporter, pts[0].fY == 0);
1317 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1318 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
1319 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
1320 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
1321 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
1322 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
1323 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1324 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
1325 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
1326 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1327 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
1328 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
1329 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
1330 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
1331 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
1332 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1333
1334 // Generate random paths and verify
1335 SkPoint randomPts[25];
1336 for (int i = 0; i < 5; ++i) {
1337 for (int j = 0; j < 5; ++j) {
1338 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
1339 }
1340 }
1341
1342 // Max of 10 segments, max 3 points per segment
1343 SkRandom rand(9876543);
1344 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00001345 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001346 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00001347
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001348 for (int i = 0; i < 500; ++i) {
1349 p.reset();
1350 bool lastWasClose = true;
1351 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00001352 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001353 int numPoints = 0;
1354 int numVerbs = (rand.nextU() >> 16) % 10;
1355 int numIterVerbs = 0;
1356 for (int j = 0; j < numVerbs; ++j) {
1357 do {
1358 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
1359 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001360 switch (nextVerb) {
1361 case SkPath::kMove_Verb:
1362 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
1363 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00001364 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001365 numPoints += 1;
1366 lastWasClose = false;
1367 haveMoveTo = true;
1368 break;
1369 case SkPath::kLine_Verb:
1370 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00001371 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001372 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
1373 haveMoveTo = true;
1374 }
1375 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
1376 p.lineTo(expectedPts[numPoints]);
1377 numPoints += 1;
1378 lastWasClose = false;
1379 break;
1380 case SkPath::kQuad_Verb:
1381 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00001382 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001383 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
1384 haveMoveTo = true;
1385 }
1386 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
1387 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
1388 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
1389 numPoints += 2;
1390 lastWasClose = false;
1391 break;
1392 case SkPath::kCubic_Verb:
1393 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00001394 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001395 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
1396 haveMoveTo = true;
1397 }
1398 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
1399 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
1400 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
1401 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
1402 expectedPts[numPoints + 2]);
1403 numPoints += 3;
1404 lastWasClose = false;
1405 break;
1406 case SkPath::kClose_Verb:
1407 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00001408 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001409 lastWasClose = true;
1410 break;
1411 default:;
1412 }
1413 expectedVerbs[numIterVerbs++] = nextVerb;
1414 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001415
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001416 iter.setPath(p);
1417 numVerbs = numIterVerbs;
1418 numIterVerbs = 0;
1419 int numIterPts = 0;
1420 SkPoint lastMoveTo;
1421 SkPoint lastPt;
1422 lastMoveTo.set(0, 0);
1423 lastPt.set(0, 0);
1424 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
1425 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
1426 numIterVerbs++;
1427 switch (nextVerb) {
1428 case SkPath::kMove_Verb:
1429 REPORTER_ASSERT(reporter, numIterPts < numPoints);
1430 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
1431 lastPt = lastMoveTo = pts[0];
1432 numIterPts += 1;
1433 break;
1434 case SkPath::kLine_Verb:
1435 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
1436 REPORTER_ASSERT(reporter, pts[0] == lastPt);
1437 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
1438 lastPt = pts[1];
1439 numIterPts += 1;
1440 break;
1441 case SkPath::kQuad_Verb:
1442 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
1443 REPORTER_ASSERT(reporter, pts[0] == lastPt);
1444 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
1445 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
1446 lastPt = pts[2];
1447 numIterPts += 2;
1448 break;
1449 case SkPath::kCubic_Verb:
1450 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
1451 REPORTER_ASSERT(reporter, pts[0] == lastPt);
1452 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
1453 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
1454 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
1455 lastPt = pts[3];
1456 numIterPts += 3;
1457 break;
1458 case SkPath::kClose_Verb:
1459 REPORTER_ASSERT(reporter, pts[0] == lastMoveTo);
1460 lastPt = lastMoveTo;
1461 break;
1462 default:;
1463 }
1464 }
1465 REPORTER_ASSERT(reporter, numIterPts == numPoints);
1466 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
1467 }
1468}
1469
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001470static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001471 const SkPath& path,
1472 bool expectedCircle,
1473 SkPath::Direction expectedDir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001474 SkRect rect;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001475 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
1476 REPORTER_ASSERT(reporter, path.cheapIsDirection(expectedDir));
skia.committer@gmail.comfbb0ed92012-11-13 21:46:06 +00001477
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001478 if (expectedCircle) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001479 REPORTER_ASSERT(reporter, rect.height() == rect.width());
1480 }
1481}
1482
1483static void test_circle_skew(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001484 const SkPath& path,
1485 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001486 SkPath tmp;
1487
1488 SkMatrix m;
1489 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
1490 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001491 // this matrix reverses the direction.
1492 if (SkPath::kCCW_Direction == dir) {
1493 dir = SkPath::kCW_Direction;
1494 } else {
1495 SkASSERT(SkPath::kCW_Direction == dir);
1496 dir = SkPath::kCCW_Direction;
1497 }
1498 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001499}
1500
1501static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001502 const SkPath& path,
1503 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001504 SkPath tmp;
1505
1506 // translate at small offset
1507 SkMatrix m;
1508 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
1509 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001510 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001511
1512 tmp.reset();
1513 m.reset();
1514
1515 // translate at a relatively big offset
1516 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
1517 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001518 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001519}
1520
1521static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001522 const SkPath& path,
1523 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001524 for (int angle = 0; angle < 360; ++angle) {
1525 SkPath tmp;
1526 SkMatrix m;
1527 m.setRotate(SkIntToScalar(angle));
1528 path.transform(m, &tmp);
1529
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001530 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001531 // degrees is not an oval anymore, this can be improved. we made this
1532 // for the simplicity of our implementation.
1533 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001534 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001535 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001536 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001537 }
1538 }
1539}
1540
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001541static void test_circle_mirror_x(skiatest::Reporter* reporter,
1542 const SkPath& path,
1543 SkPath::Direction dir) {
1544 SkPath tmp;
1545 SkMatrix m;
1546 m.reset();
1547 m.setScaleX(-SK_Scalar1);
1548 path.transform(m, &tmp);
1549
1550 if (SkPath::kCW_Direction == dir) {
1551 dir = SkPath::kCCW_Direction;
1552 } else {
1553 SkASSERT(SkPath::kCCW_Direction == dir);
1554 dir = SkPath::kCW_Direction;
1555 }
1556
1557 check_for_circle(reporter, tmp, true, dir);
1558}
1559
1560static void test_circle_mirror_y(skiatest::Reporter* reporter,
1561 const SkPath& path,
1562 SkPath::Direction dir) {
1563 SkPath tmp;
1564 SkMatrix m;
1565 m.reset();
1566 m.setScaleY(-SK_Scalar1);
1567 path.transform(m, &tmp);
1568
1569 if (SkPath::kCW_Direction == dir) {
1570 dir = SkPath::kCCW_Direction;
1571 } else {
1572 SkASSERT(SkPath::kCCW_Direction == dir);
1573 dir = SkPath::kCW_Direction;
1574 }
1575
1576 check_for_circle(reporter, tmp, true, dir);
1577}
1578
1579static void test_circle_mirror_xy(skiatest::Reporter* reporter,
1580 const SkPath& path,
1581 SkPath::Direction dir) {
1582 SkPath tmp;
1583 SkMatrix m;
1584 m.reset();
1585 m.setScaleX(-SK_Scalar1);
1586 m.setScaleY(-SK_Scalar1);
1587 path.transform(m, &tmp);
1588
1589 check_for_circle(reporter, tmp, true, dir);
1590}
1591
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001592static void test_circle_with_direction(skiatest::Reporter* reporter,
1593 SkPath::Direction dir) {
1594 SkPath path;
1595
1596 // circle at origin
1597 path.addCircle(0, 0, SkIntToScalar(20), dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001598 check_for_circle(reporter, path, true, dir);
1599 test_circle_rotate(reporter, path, dir);
1600 test_circle_translate(reporter, path, dir);
1601 test_circle_skew(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001602
1603 // circle at an offset at (10, 10)
1604 path.reset();
1605 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
1606 SkIntToScalar(20), dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001607 check_for_circle(reporter, path, true, dir);
1608 test_circle_rotate(reporter, path, dir);
1609 test_circle_translate(reporter, path, dir);
1610 test_circle_skew(reporter, path, dir);
1611 test_circle_mirror_x(reporter, path, dir);
1612 test_circle_mirror_y(reporter, path, dir);
1613 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001614}
1615
1616static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
1617 SkPath path;
1618 SkPath circle;
1619 SkPath rect;
1620 SkPath empty;
1621
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001622 static const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
1623 static const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
1624
1625 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001626 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
1627 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
1628
1629 SkMatrix translate;
1630 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
1631
1632 // For simplicity, all the path concatenation related operations
1633 // would mark it non-circle, though in theory it's still a circle.
1634
1635 // empty + circle (translate)
1636 path = empty;
1637 path.addPath(circle, translate);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001638 check_for_circle(reporter, path, false, kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001639
1640 // circle + empty (translate)
1641 path = circle;
1642 path.addPath(empty, translate);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001643 check_for_circle(reporter, path, false, kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001644
1645 // test reverseAddPath
1646 path = circle;
1647 path.reverseAddPath(rect);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001648 check_for_circle(reporter, path, false, kCircleDirOpposite);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001649}
1650
1651static void test_circle(skiatest::Reporter* reporter) {
1652 test_circle_with_direction(reporter, SkPath::kCW_Direction);
1653 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
1654
1655 // multiple addCircle()
1656 SkPath path;
1657 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
1658 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001659 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001660
1661 // some extra lineTo() would make isOval() fail
1662 path.reset();
1663 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
1664 path.lineTo(0, 0);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001665 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001666
1667 // not back to the original point
1668 path.reset();
1669 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
1670 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001671 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001672
1673 test_circle_with_add_paths(reporter);
1674}
1675
1676static void test_oval(skiatest::Reporter* reporter) {
1677 SkRect rect;
1678 SkMatrix m;
1679 SkPath path;
1680
1681 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
1682 path.addOval(rect);
1683
1684 REPORTER_ASSERT(reporter, path.isOval(NULL));
1685
1686 m.setRotate(SkIntToScalar(90));
1687 SkPath tmp;
1688 path.transform(m, &tmp);
1689 // an oval rotated 90 degrees is still an oval.
1690 REPORTER_ASSERT(reporter, tmp.isOval(NULL));
1691
1692 m.reset();
1693 m.setRotate(SkIntToScalar(30));
1694 tmp.reset();
1695 path.transform(m, &tmp);
1696 // an oval rotated 30 degrees is not an oval anymore.
1697 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
1698
1699 // since empty path being transformed.
1700 path.reset();
1701 tmp.reset();
1702 m.reset();
1703 path.transform(m, &tmp);
1704 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
1705
1706 // empty path is not an oval
1707 tmp.reset();
1708 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
1709
1710 // only has moveTo()s
1711 tmp.reset();
1712 tmp.moveTo(0, 0);
1713 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
1714 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
1715
1716 // mimic WebKit's calling convention,
1717 // call moveTo() first and then call addOval()
1718 path.reset();
1719 path.moveTo(0, 0);
1720 path.addOval(rect);
1721 REPORTER_ASSERT(reporter, path.isOval(NULL));
1722
1723 // copy path
1724 path.reset();
1725 tmp.reset();
1726 tmp.addOval(rect);
1727 path = tmp;
1728 REPORTER_ASSERT(reporter, path.isOval(NULL));
1729}
1730
caryclark@google.com42639cd2012-06-06 12:03:39 +00001731static void TestPath(skiatest::Reporter* reporter) {
reed@android.com60bc6d52010-02-11 11:09:39 +00001732 SkTSize<SkScalar>::Make(3,4);
1733
reed@android.com3abec1d2009-03-02 05:36:20 +00001734 SkPath p, p2;
1735 SkRect bounds, bounds2;
reed@android.com80e39a72009-04-02 16:59:40 +00001736
reed@android.com3abec1d2009-03-02 05:36:20 +00001737 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001738 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00001739 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00001740 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00001741 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00001742 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
1743 REPORTER_ASSERT(reporter, !p.isInverseFillType());
1744 REPORTER_ASSERT(reporter, p == p2);
1745 REPORTER_ASSERT(reporter, !(p != p2));
1746
reed@android.comd252db02009-04-01 18:31:44 +00001747 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00001748
reed@android.com3abec1d2009-03-02 05:36:20 +00001749 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00001750
reed@android.com6b82d1a2009-06-03 02:35:01 +00001751 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
1752 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00001753 // we have quads or cubics
1754 REPORTER_ASSERT(reporter, p.getSegmentMasks() & kCurveSegmentMask);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001755 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00001756
reed@android.com6b82d1a2009-06-03 02:35:01 +00001757 p.reset();
reed@google.com10296cc2011-09-21 12:29:05 +00001758 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001759 REPORTER_ASSERT(reporter, p.isEmpty());
reed@google.com10296cc2011-09-21 12:29:05 +00001760
reed@android.com6b82d1a2009-06-03 02:35:01 +00001761 p.addOval(bounds);
1762 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001763 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00001764
reed@android.com6b82d1a2009-06-03 02:35:01 +00001765 p.reset();
reed@android.com3abec1d2009-03-02 05:36:20 +00001766 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00001767 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00001768 // we have only lines
1769 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001770 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00001771
1772 REPORTER_ASSERT(reporter, p != p2);
1773 REPORTER_ASSERT(reporter, !(p == p2));
1774
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00001775 // do getPoints and getVerbs return the right result
1776 REPORTER_ASSERT(reporter, p.getPoints(NULL, 0) == 4);
1777 REPORTER_ASSERT(reporter, p.getVerbs(NULL, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00001778 SkPoint pts[4];
1779 int count = p.getPoints(pts, 4);
1780 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00001781 uint8_t verbs[6];
1782 verbs[5] = 0xff;
1783 p.getVerbs(verbs, 5);
1784 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
1785 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
1786 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
1787 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
1788 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
1789 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00001790 bounds2.set(pts, 4);
1791 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00001792
reed@android.com3abec1d2009-03-02 05:36:20 +00001793 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
1794 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00001795 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00001796
reed@android.com3abec1d2009-03-02 05:36:20 +00001797 REPORTER_ASSERT(reporter, p.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00001798 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00001799 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
1800 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00001801
reed@android.com3abec1d2009-03-02 05:36:20 +00001802 // now force p to not be a rect
1803 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
1804 p.addRect(bounds);
1805 REPORTER_ASSERT(reporter, !p.isRect(NULL));
reed@android.com3abec1d2009-03-02 05:36:20 +00001806
reed@google.com7e6c4d12012-05-10 14:05:43 +00001807 test_isLine(reporter);
1808 test_isRect(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001809 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00001810 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00001811 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00001812 test_convexity2(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001813 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001814 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00001815 test_flattening(reporter);
1816 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00001817 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001818 test_iter(reporter);
1819 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001820 test_circle(reporter);
1821 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00001822 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00001823 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00001824 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00001825 test_isfinite_after_transform(reporter);
reed@google.com8cae8352012-09-14 15:18:41 +00001826 test_tricky_cubic(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00001827 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00001828 test_arb_zero_rad_round_rect_is_rect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00001829 test_addrect_isfinite(reporter);
reed@android.com3abec1d2009-03-02 05:36:20 +00001830}
1831
1832#include "TestClassDef.h"
1833DEFINE_TESTCLASS("Path", PathTestClass, TestPath)