blob: a74ba8a8e63ab1a141540e402e55cd6b8ec74836 [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
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000825static void test_conservativelyContains(skiatest::Reporter* reporter) {
826 SkPath path;
827
828 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
829 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
830
831 // A circle that bounds kBaseRect (with a significant amount of slop)
832 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
833 circleR = SkScalarMul(circleR, SkFloatToScalar(1.75f)) / 2;
834 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
835
836 // round-rect radii
837 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
skia.committer@gmail.comcec8de62012-11-14 02:01:22 +0000838
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000839 static const struct {
840 SkRect fQueryRect;
841 bool fInRect;
842 bool fInCircle;
843 bool fInRR;
844 } kQueries[] = {
845 {kBaseRect, true, true, false},
846
847 // rect well inside of kBaseRect
848 {SkRect::MakeLTRB(kBaseRect.fLeft + SkFloatToScalar(0.25f)*kBaseRect.width(),
849 kBaseRect.fTop + SkFloatToScalar(0.25f)*kBaseRect.height(),
850 kBaseRect.fRight - SkFloatToScalar(0.25f)*kBaseRect.width(),
851 kBaseRect.fBottom - SkFloatToScalar(0.25f)*kBaseRect.height()),
852 true, true, true},
853
854 // rects with edges off by one from kBaseRect's edges
855 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
856 kBaseRect.width(), kBaseRect.height() + 1),
857 false, true, false},
858 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
859 kBaseRect.width() + 1, kBaseRect.height()),
860 false, true, false},
861 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
862 kBaseRect.width() + 1, kBaseRect.height() + 1),
863 false, true, false},
864 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
865 kBaseRect.width(), kBaseRect.height()),
866 false, true, false},
867 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
868 kBaseRect.width(), kBaseRect.height()),
869 false, true, false},
870 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
871 kBaseRect.width() + 2, kBaseRect.height()),
872 false, true, false},
873 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
874 kBaseRect.width() + 2, kBaseRect.height()),
875 false, true, false},
876
877 // zero-w/h rects at each corner of kBaseRect
878 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false},
879 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false},
880 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false},
881 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false},
882
883 // far away rect
884 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
885 SkIntToScalar(10), SkIntToScalar(10)),
886 false, false, false},
887
888 // very large rect containing kBaseRect
889 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
890 kBaseRect.fTop - 5 * kBaseRect.height(),
891 11 * kBaseRect.width(), 11 * kBaseRect.height()),
892 false, false, false},
893
894 // skinny rect that spans same y-range as kBaseRect
895 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
896 SkIntToScalar(1), kBaseRect.height()),
897 true, true, true},
898
899 // short rect that spans same x-range as kBaseRect
900 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
901 true, true, true},
902
903 // skinny rect that spans slightly larger y-range than kBaseRect
904 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
905 SkIntToScalar(1), kBaseRect.height() + 1),
906 false, true, false},
907
908 // short rect that spans slightly larger x-range than kBaseRect
909 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
910 kBaseRect.width() + 1, SkScalar(1)),
911 false, true, false},
912 };
913
914 for (int inv = 0; inv < 4; ++inv) {
915 for (int q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
916 SkRect qRect = kQueries[q].fQueryRect;
917 if (inv & 0x1) {
918 SkTSwap(qRect.fLeft, qRect.fRight);
919 }
920 if (inv & 0x2) {
921 SkTSwap(qRect.fTop, qRect.fBottom);
922 }
923 for (int d = 0; d < 2; ++d) {
924 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
925 path.reset();
926 path.addRect(kBaseRect, dir);
927 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
928 path.conservativelyContainsRect(qRect));
929
930 path.reset();
931 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
932 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
933 path.conservativelyContainsRect(qRect));
934
935 path.reset();
936 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
937 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
938 path.conservativelyContainsRect(qRect));
939 }
940 // Slightly non-convex shape, shouldn't contain any rects.
941 path.reset();
942 path.moveTo(0, 0);
943 path.lineTo(SkIntToScalar(50), SkFloatToScalar(0.05f));
944 path.lineTo(SkIntToScalar(100), 0);
945 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
946 path.lineTo(0, SkIntToScalar(100));
947 path.close();
948 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
949 }
950 }
951
952 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
953 path.reset();
954 path.moveTo(0, 0);
955 path.lineTo(SkIntToScalar(100), 0);
956 path.lineTo(0, SkIntToScalar(100));
957
958 // inside, on along top edge
959 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
960 SkIntToScalar(10),
961 SkIntToScalar(10))));
962 // above
963 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
964 SkRect::MakeXYWH(SkIntToScalar(50),
965 SkIntToScalar(-10),
966 SkIntToScalar(10),
967 SkIntToScalar(10))));
968 // to the left
969 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
970 SkIntToScalar(5),
971 SkIntToScalar(5),
972 SkIntToScalar(5))));
973
974 // outside the diagonal edge
975 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
976 SkIntToScalar(200),
977 SkIntToScalar(20),
978 SkIntToScalar(5))));
979}
980
caryclark@google.comf1316942011-07-26 19:54:45 +0000981// Simple isRect test is inline TestPath, below.
982// test_isRect provides more extensive testing.
983static void test_isRect(skiatest::Reporter* reporter) {
984 // passing tests (all moveTo / lineTo...
985 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
986 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
987 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
988 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
989 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
990 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
991 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
992 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
993 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
994 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f},
995 {1, 0}, {.5f, 0}};
996 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1},
997 {0, 1}, {0, .5f}};
998 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
999 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1000 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
rmistry@google.comd6176b02012-08-23 18:14:13 +00001001
caryclark@google.comf1316942011-07-26 19:54:45 +00001002 // failing tests
1003 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1004 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1005 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1006 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1007 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1008 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1009 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1010 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
rmistry@google.comd6176b02012-08-23 18:14:13 +00001011
caryclark@google.comf1316942011-07-26 19:54:45 +00001012 // failing, no close
1013 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
1014 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
1015
1016 size_t testLen[] = {
1017 sizeof(r1), sizeof(r2), sizeof(r3), sizeof(r4), sizeof(r5), sizeof(r6),
1018 sizeof(r7), sizeof(r8), sizeof(r9), sizeof(ra), sizeof(rb), sizeof(rc),
1019 sizeof(rd), sizeof(re),
1020 sizeof(f1), sizeof(f2), sizeof(f3), sizeof(f4), sizeof(f5), sizeof(f6),
1021 sizeof(f7), sizeof(f8),
rmistry@google.comd6176b02012-08-23 18:14:13 +00001022 sizeof(c1), sizeof(c2)
caryclark@google.comf1316942011-07-26 19:54:45 +00001023 };
1024 SkPoint* tests[] = {
1025 r1, r2, r3, r4, r5, r6, r7, r8, r9, ra, rb, rc, rd, re,
1026 f1, f2, f3, f4, f5, f6, f7, f8,
rmistry@google.comd6176b02012-08-23 18:14:13 +00001027 c1, c2
caryclark@google.comf1316942011-07-26 19:54:45 +00001028 };
1029 SkPoint* lastPass = re;
1030 SkPoint* lastClose = f8;
1031 bool fail = false;
1032 bool close = true;
1033 const size_t testCount = sizeof(tests) / sizeof(tests[0]);
1034 size_t index;
1035 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1036 SkPath path;
1037 path.moveTo(tests[testIndex][0].fX, tests[testIndex][0].fY);
1038 for (index = 1; index < testLen[testIndex] / sizeof(SkPoint); ++index) {
1039 path.lineTo(tests[testIndex][index].fX, tests[testIndex][index].fY);
1040 }
1041 if (close) {
1042 path.close();
1043 }
1044 REPORTER_ASSERT(reporter, fail ^ path.isRect(0));
1045 if (tests[testIndex] == lastPass) {
1046 fail = true;
1047 }
1048 if (tests[testIndex] == lastClose) {
1049 close = false;
1050 }
1051 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001052
caryclark@google.comf1316942011-07-26 19:54:45 +00001053 // fail, close then line
1054 SkPath path1;
1055 path1.moveTo(r1[0].fX, r1[0].fY);
1056 for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
1057 path1.lineTo(r1[index].fX, r1[index].fY);
1058 }
1059 path1.close();
1060 path1.lineTo(1, 0);
1061 REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001062
caryclark@google.comf1316942011-07-26 19:54:45 +00001063 // fail, move in the middle
1064 path1.reset();
1065 path1.moveTo(r1[0].fX, r1[0].fY);
1066 for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
1067 if (index == 2) {
1068 path1.moveTo(1, .5f);
1069 }
1070 path1.lineTo(r1[index].fX, r1[index].fY);
1071 }
1072 path1.close();
1073 REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
1074
1075 // fail, move on the edge
1076 path1.reset();
1077 for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
1078 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
1079 path1.lineTo(r1[index].fX, r1[index].fY);
1080 }
1081 path1.close();
1082 REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001083
caryclark@google.comf1316942011-07-26 19:54:45 +00001084 // fail, quad
1085 path1.reset();
1086 path1.moveTo(r1[0].fX, r1[0].fY);
1087 for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
1088 if (index == 2) {
1089 path1.quadTo(1, .5f, 1, .5f);
1090 }
1091 path1.lineTo(r1[index].fX, r1[index].fY);
1092 }
1093 path1.close();
1094 REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001095
caryclark@google.comf1316942011-07-26 19:54:45 +00001096 // fail, cubic
1097 path1.reset();
1098 path1.moveTo(r1[0].fX, r1[0].fY);
1099 for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
1100 if (index == 2) {
1101 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
1102 }
1103 path1.lineTo(r1[index].fX, r1[index].fY);
1104 }
1105 path1.close();
1106 REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
1107}
1108
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001109static void write_and_read_back(skiatest::Reporter* reporter,
1110 const SkPath& p) {
1111 SkWriter32 writer(100);
1112 writer.writePath(p);
1113 size_t size = writer.size();
1114 SkAutoMalloc storage(size);
1115 writer.flatten(storage.get());
1116 SkReader32 reader(storage.get(), size);
1117
1118 SkPath readBack;
1119 REPORTER_ASSERT(reporter, readBack != p);
1120 reader.readPath(&readBack);
1121 REPORTER_ASSERT(reporter, readBack == p);
1122
rmistry@google.comd6176b02012-08-23 18:14:13 +00001123 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001124 p.getConvexityOrUnknown());
1125
1126 REPORTER_ASSERT(reporter, readBack.isOval(NULL) == p.isOval(NULL));
1127
1128 const SkRect& origBounds = p.getBounds();
1129 const SkRect& readBackBounds = readBack.getBounds();
1130
1131 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
1132}
1133
reed@google.com53effc52011-09-21 19:05:12 +00001134static void test_flattening(skiatest::Reporter* reporter) {
1135 SkPath p;
1136
1137 static const SkPoint pts[] = {
1138 { 0, 0 },
1139 { SkIntToScalar(10), SkIntToScalar(10) },
1140 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
1141 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
1142 };
1143 p.moveTo(pts[0]);
1144 p.lineTo(pts[1]);
1145 p.quadTo(pts[2], pts[3]);
1146 p.cubicTo(pts[4], pts[5], pts[6]);
1147
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001148 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001149
1150 // create a buffer that should be much larger than the path so we don't
1151 // kill our stack if writer goes too far.
1152 char buffer[1024];
1153 uint32_t size1 = p.writeToMemory(NULL);
1154 uint32_t size2 = p.writeToMemory(buffer);
1155 REPORTER_ASSERT(reporter, size1 == size2);
1156
1157 SkPath p2;
1158 uint32_t size3 = p2.readFromMemory(buffer);
1159 REPORTER_ASSERT(reporter, size1 == size3);
1160 REPORTER_ASSERT(reporter, p == p2);
1161
1162 char buffer2[1024];
1163 size3 = p2.writeToMemory(buffer2);
1164 REPORTER_ASSERT(reporter, size1 == size3);
1165 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001166
1167 // test persistence of the oval flag & convexity
1168 {
1169 SkPath oval;
1170 SkRect rect = SkRect::MakeWH(10, 10);
1171 oval.addOval(rect);
1172
1173 write_and_read_back(reporter, oval);
1174 }
reed@google.com53effc52011-09-21 19:05:12 +00001175}
1176
1177static void test_transform(skiatest::Reporter* reporter) {
1178 SkPath p, p1;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001179
reed@google.com53effc52011-09-21 19:05:12 +00001180 static const SkPoint pts[] = {
1181 { 0, 0 },
1182 { SkIntToScalar(10), SkIntToScalar(10) },
1183 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
1184 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
1185 };
1186 p.moveTo(pts[0]);
1187 p.lineTo(pts[1]);
1188 p.quadTo(pts[2], pts[3]);
1189 p.cubicTo(pts[4], pts[5], pts[6]);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001190
reed@google.com53effc52011-09-21 19:05:12 +00001191 SkMatrix matrix;
1192 matrix.reset();
1193 p.transform(matrix, &p1);
1194 REPORTER_ASSERT(reporter, p == p1);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001195
reed@google.com53effc52011-09-21 19:05:12 +00001196 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
1197 p.transform(matrix, &p1);
1198 SkPoint pts1[7];
1199 int count = p1.getPoints(pts1, 7);
1200 REPORTER_ASSERT(reporter, 7 == count);
1201 for (int i = 0; i < count; ++i) {
1202 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
1203 REPORTER_ASSERT(reporter, newPt == pts1[i]);
1204 }
1205}
1206
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001207static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001208 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00001209 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001210
schenney@chromium.org7e963602012-06-13 17:05:43 +00001211 struct zeroPathTestData {
1212 const char* testPath;
1213 const size_t numResultPts;
1214 const SkRect resultBound;
1215 const SkPath::Verb* resultVerbs;
1216 const size_t numResultVerbs;
1217 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001218
schenney@chromium.org7e963602012-06-13 17:05:43 +00001219 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
1220 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
1221 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
1222 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
1223 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
1224 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
1225 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
1226 static const SkPath::Verb resultVerbs8[] = {
1227 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
1228 };
1229 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
1230 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
1231 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
1232 static const SkPath::Verb resultVerbs12[] = {
1233 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
1234 };
1235 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
1236 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
1237 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
1238 static const SkPath::Verb resultVerbs16[] = {
1239 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
1240 };
1241 static const struct zeroPathTestData gZeroLengthTests[] = {
1242 { "M 1 1", 1, {0, 0, 0, 0}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001243 { "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 +00001244 { "M 1 1 z", 1, {0, 0, 0, 0}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001245 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
1246 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
1247 { "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) },
1248 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
1249 { "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) },
1250 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
1251 { "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) },
1252 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
1253 { "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) },
1254 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
1255 { "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 +00001256 SK_ARRAY_COUNT(resultVerbs14)
1257 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001258 { "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) },
1259 { "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 +00001260 SK_ARRAY_COUNT(resultVerbs16)
1261 }
1262 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001263
schenney@chromium.org7e963602012-06-13 17:05:43 +00001264 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
1265 p.reset();
1266 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
1267 REPORTER_ASSERT(reporter, valid);
1268 REPORTER_ASSERT(reporter, !p.isEmpty());
1269 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
1270 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
1271 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
1272 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
1273 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
1274 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00001275 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001276}
1277
1278struct SegmentInfo {
1279 SkPath fPath;
1280 int fPointCount;
1281};
1282
reed@google.com10296cc2011-09-21 12:29:05 +00001283#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
1284
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001285static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00001286 SkPath p, p2;
1287
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001288 p.moveTo(0, 0);
1289 p.quadTo(100, 100, 200, 200);
1290 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
1291 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00001292 p2 = p;
1293 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001294 p.cubicTo(100, 100, 200, 200, 300, 300);
1295 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
1296 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00001297 p2 = p;
1298 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
1299
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001300 p.reset();
1301 p.moveTo(0, 0);
1302 p.cubicTo(100, 100, 200, 200, 300, 300);
1303 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00001304 p2 = p;
1305 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001306
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001307 REPORTER_ASSERT(reporter, !p.isEmpty());
1308}
1309
1310static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00001311 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001312 SkPoint pts[4];
1313
1314 // Test an iterator with no path
1315 SkPath::Iter noPathIter;
1316 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00001317
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001318 // Test that setting an empty path works
1319 noPathIter.setPath(p, false);
1320 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00001321
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001322 // Test that close path makes no difference for an empty path
1323 noPathIter.setPath(p, true);
1324 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00001325
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001326 // Test an iterator with an initial empty path
1327 SkPath::Iter iter(p, false);
1328 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1329
1330 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00001331 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001332 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1333
rmistry@google.comd6176b02012-08-23 18:14:13 +00001334
schenney@chromium.org7e963602012-06-13 17:05:43 +00001335 struct iterTestData {
1336 const char* testPath;
1337 const bool forceClose;
1338 const bool consumeDegenerates;
1339 const size_t* numResultPtsPerVerb;
1340 const SkPoint* resultPts;
1341 const SkPath::Verb* resultVerbs;
1342 const size_t numResultVerbs;
1343 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001344
schenney@chromium.org7e963602012-06-13 17:05:43 +00001345 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
1346 static const SkPath::Verb resultVerbs2[] = {
1347 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
1348 };
1349 static const SkPath::Verb resultVerbs3[] = {
1350 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
1351 };
1352 static const SkPath::Verb resultVerbs4[] = {
1353 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
1354 };
1355 static const SkPath::Verb resultVerbs5[] = {
1356 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
1357 };
1358 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00001359 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
1360 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
1361 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
1362 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001363 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00001364 static const SkPoint resultPts2[] = {
1365 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
1366 };
1367 static const SkPoint resultPts3[] = {
1368 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
1369 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
1370 };
1371 static const SkPoint resultPts4[] = {
1372 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
1373 };
1374 static const SkPoint resultPts5[] = {
1375 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
1376 };
1377 static const struct iterTestData gIterTests[] = {
1378 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001379 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1380 { "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 +00001381 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1382 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1383 { "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) },
1384 { "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 +00001385 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
1386 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
1387 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1388 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1389 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
1390 { "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 +00001391 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001392
schenney@chromium.org7e963602012-06-13 17:05:43 +00001393 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
1394 p.reset();
1395 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
1396 REPORTER_ASSERT(reporter, valid);
1397 iter.setPath(p, gIterTests[i].forceClose);
1398 int j = 0, l = 0;
1399 do {
1400 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
1401 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
1402 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
1403 }
1404 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
1405 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
1406 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001407
1408 // The GM degeneratesegments.cpp test is more extensive
1409}
1410
1411static void test_raw_iter(skiatest::Reporter* reporter) {
1412 SkPath p;
1413 SkPoint pts[4];
1414
1415 // Test an iterator with no path
1416 SkPath::RawIter noPathIter;
1417 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
1418 // Test that setting an empty path works
1419 noPathIter.setPath(p);
1420 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001421
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001422 // Test an iterator with an initial empty path
1423 SkPath::RawIter iter(p);
1424 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1425
1426 // Test that a move-only path returns the move.
1427 p.moveTo(SK_Scalar1, 0);
1428 iter.setPath(p);
1429 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1430 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
1431 REPORTER_ASSERT(reporter, pts[0].fY == 0);
1432 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1433
1434 // No matter how many moves we add, we should get them all back
1435 p.moveTo(SK_Scalar1*2, SK_Scalar1);
1436 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
1437 iter.setPath(p);
1438 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1439 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
1440 REPORTER_ASSERT(reporter, pts[0].fY == 0);
1441 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1442 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
1443 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
1444 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1445 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
1446 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
1447 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1448
1449 // Initial close is never ever stored
1450 p.reset();
1451 p.close();
1452 iter.setPath(p);
1453 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1454
1455 // Move/close sequences
1456 p.reset();
1457 p.close(); // Not stored, no purpose
1458 p.moveTo(SK_Scalar1, 0);
1459 p.close();
1460 p.close(); // Not stored, no purpose
1461 p.moveTo(SK_Scalar1*2, SK_Scalar1);
1462 p.close();
1463 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
1464 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
1465 p.close();
1466 iter.setPath(p);
1467 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1468 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
1469 REPORTER_ASSERT(reporter, pts[0].fY == 0);
1470 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
1471 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
1472 REPORTER_ASSERT(reporter, pts[0].fY == 0);
1473 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1474 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
1475 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
1476 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
1477 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
1478 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
1479 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1480 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
1481 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
1482 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1483 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
1484 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
1485 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
1486 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
1487 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
1488 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1489
1490 // Generate random paths and verify
1491 SkPoint randomPts[25];
1492 for (int i = 0; i < 5; ++i) {
1493 for (int j = 0; j < 5; ++j) {
1494 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
1495 }
1496 }
1497
1498 // Max of 10 segments, max 3 points per segment
1499 SkRandom rand(9876543);
1500 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00001501 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001502 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00001503
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001504 for (int i = 0; i < 500; ++i) {
1505 p.reset();
1506 bool lastWasClose = true;
1507 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00001508 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001509 int numPoints = 0;
1510 int numVerbs = (rand.nextU() >> 16) % 10;
1511 int numIterVerbs = 0;
1512 for (int j = 0; j < numVerbs; ++j) {
1513 do {
1514 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
1515 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001516 switch (nextVerb) {
1517 case SkPath::kMove_Verb:
1518 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
1519 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00001520 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001521 numPoints += 1;
1522 lastWasClose = false;
1523 haveMoveTo = true;
1524 break;
1525 case SkPath::kLine_Verb:
1526 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00001527 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001528 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
1529 haveMoveTo = true;
1530 }
1531 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
1532 p.lineTo(expectedPts[numPoints]);
1533 numPoints += 1;
1534 lastWasClose = false;
1535 break;
1536 case SkPath::kQuad_Verb:
1537 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00001538 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001539 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
1540 haveMoveTo = true;
1541 }
1542 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
1543 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
1544 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
1545 numPoints += 2;
1546 lastWasClose = false;
1547 break;
1548 case SkPath::kCubic_Verb:
1549 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00001550 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001551 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
1552 haveMoveTo = true;
1553 }
1554 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
1555 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
1556 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
1557 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
1558 expectedPts[numPoints + 2]);
1559 numPoints += 3;
1560 lastWasClose = false;
1561 break;
1562 case SkPath::kClose_Verb:
1563 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00001564 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001565 lastWasClose = true;
1566 break;
1567 default:;
1568 }
1569 expectedVerbs[numIterVerbs++] = nextVerb;
1570 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001571
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001572 iter.setPath(p);
1573 numVerbs = numIterVerbs;
1574 numIterVerbs = 0;
1575 int numIterPts = 0;
1576 SkPoint lastMoveTo;
1577 SkPoint lastPt;
1578 lastMoveTo.set(0, 0);
1579 lastPt.set(0, 0);
1580 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
1581 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
1582 numIterVerbs++;
1583 switch (nextVerb) {
1584 case SkPath::kMove_Verb:
1585 REPORTER_ASSERT(reporter, numIterPts < numPoints);
1586 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
1587 lastPt = lastMoveTo = pts[0];
1588 numIterPts += 1;
1589 break;
1590 case SkPath::kLine_Verb:
1591 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
1592 REPORTER_ASSERT(reporter, pts[0] == lastPt);
1593 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
1594 lastPt = pts[1];
1595 numIterPts += 1;
1596 break;
1597 case SkPath::kQuad_Verb:
1598 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
1599 REPORTER_ASSERT(reporter, pts[0] == lastPt);
1600 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
1601 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
1602 lastPt = pts[2];
1603 numIterPts += 2;
1604 break;
1605 case SkPath::kCubic_Verb:
1606 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
1607 REPORTER_ASSERT(reporter, pts[0] == lastPt);
1608 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
1609 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
1610 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
1611 lastPt = pts[3];
1612 numIterPts += 3;
1613 break;
1614 case SkPath::kClose_Verb:
1615 REPORTER_ASSERT(reporter, pts[0] == lastMoveTo);
1616 lastPt = lastMoveTo;
1617 break;
1618 default:;
1619 }
1620 }
1621 REPORTER_ASSERT(reporter, numIterPts == numPoints);
1622 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
1623 }
1624}
1625
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001626static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001627 const SkPath& path,
1628 bool expectedCircle,
1629 SkPath::Direction expectedDir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001630 SkRect rect;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001631 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
1632 REPORTER_ASSERT(reporter, path.cheapIsDirection(expectedDir));
skia.committer@gmail.comfbb0ed92012-11-13 21:46:06 +00001633
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001634 if (expectedCircle) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001635 REPORTER_ASSERT(reporter, rect.height() == rect.width());
1636 }
1637}
1638
1639static void test_circle_skew(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001640 const SkPath& path,
1641 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001642 SkPath tmp;
1643
1644 SkMatrix m;
1645 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
1646 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001647 // this matrix reverses the direction.
1648 if (SkPath::kCCW_Direction == dir) {
1649 dir = SkPath::kCW_Direction;
1650 } else {
1651 SkASSERT(SkPath::kCW_Direction == dir);
1652 dir = SkPath::kCCW_Direction;
1653 }
1654 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001655}
1656
1657static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001658 const SkPath& path,
1659 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001660 SkPath tmp;
1661
1662 // translate at small offset
1663 SkMatrix m;
1664 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
1665 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001666 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001667
1668 tmp.reset();
1669 m.reset();
1670
1671 // translate at a relatively big offset
1672 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
1673 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001674 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001675}
1676
1677static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001678 const SkPath& path,
1679 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001680 for (int angle = 0; angle < 360; ++angle) {
1681 SkPath tmp;
1682 SkMatrix m;
1683 m.setRotate(SkIntToScalar(angle));
1684 path.transform(m, &tmp);
1685
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001686 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001687 // degrees is not an oval anymore, this can be improved. we made this
1688 // for the simplicity of our implementation.
1689 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001690 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001691 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001692 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001693 }
1694 }
1695}
1696
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001697static void test_circle_mirror_x(skiatest::Reporter* reporter,
1698 const SkPath& path,
1699 SkPath::Direction dir) {
1700 SkPath tmp;
1701 SkMatrix m;
1702 m.reset();
1703 m.setScaleX(-SK_Scalar1);
1704 path.transform(m, &tmp);
1705
1706 if (SkPath::kCW_Direction == dir) {
1707 dir = SkPath::kCCW_Direction;
1708 } else {
1709 SkASSERT(SkPath::kCCW_Direction == dir);
1710 dir = SkPath::kCW_Direction;
1711 }
1712
1713 check_for_circle(reporter, tmp, true, dir);
1714}
1715
1716static void test_circle_mirror_y(skiatest::Reporter* reporter,
1717 const SkPath& path,
1718 SkPath::Direction dir) {
1719 SkPath tmp;
1720 SkMatrix m;
1721 m.reset();
1722 m.setScaleY(-SK_Scalar1);
1723 path.transform(m, &tmp);
1724
1725 if (SkPath::kCW_Direction == dir) {
1726 dir = SkPath::kCCW_Direction;
1727 } else {
1728 SkASSERT(SkPath::kCCW_Direction == dir);
1729 dir = SkPath::kCW_Direction;
1730 }
1731
1732 check_for_circle(reporter, tmp, true, dir);
1733}
1734
1735static void test_circle_mirror_xy(skiatest::Reporter* reporter,
1736 const SkPath& path,
1737 SkPath::Direction dir) {
1738 SkPath tmp;
1739 SkMatrix m;
1740 m.reset();
1741 m.setScaleX(-SK_Scalar1);
1742 m.setScaleY(-SK_Scalar1);
1743 path.transform(m, &tmp);
1744
1745 check_for_circle(reporter, tmp, true, dir);
1746}
1747
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001748static void test_circle_with_direction(skiatest::Reporter* reporter,
1749 SkPath::Direction dir) {
1750 SkPath path;
1751
1752 // circle at origin
1753 path.addCircle(0, 0, SkIntToScalar(20), dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001754 check_for_circle(reporter, path, true, dir);
1755 test_circle_rotate(reporter, path, dir);
1756 test_circle_translate(reporter, path, dir);
1757 test_circle_skew(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001758
1759 // circle at an offset at (10, 10)
1760 path.reset();
1761 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
1762 SkIntToScalar(20), dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001763 check_for_circle(reporter, path, true, dir);
1764 test_circle_rotate(reporter, path, dir);
1765 test_circle_translate(reporter, path, dir);
1766 test_circle_skew(reporter, path, dir);
1767 test_circle_mirror_x(reporter, path, dir);
1768 test_circle_mirror_y(reporter, path, dir);
1769 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001770}
1771
1772static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
1773 SkPath path;
1774 SkPath circle;
1775 SkPath rect;
1776 SkPath empty;
1777
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001778 static const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
1779 static const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
1780
1781 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001782 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
1783 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
1784
1785 SkMatrix translate;
1786 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
1787
1788 // For simplicity, all the path concatenation related operations
1789 // would mark it non-circle, though in theory it's still a circle.
1790
1791 // empty + circle (translate)
1792 path = empty;
1793 path.addPath(circle, translate);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001794 check_for_circle(reporter, path, false, kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001795
1796 // circle + empty (translate)
1797 path = circle;
1798 path.addPath(empty, translate);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001799 check_for_circle(reporter, path, false, kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001800
1801 // test reverseAddPath
1802 path = circle;
1803 path.reverseAddPath(rect);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001804 check_for_circle(reporter, path, false, kCircleDirOpposite);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001805}
1806
1807static void test_circle(skiatest::Reporter* reporter) {
1808 test_circle_with_direction(reporter, SkPath::kCW_Direction);
1809 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
1810
1811 // multiple addCircle()
1812 SkPath path;
1813 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
1814 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001815 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001816
1817 // some extra lineTo() would make isOval() fail
1818 path.reset();
1819 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
1820 path.lineTo(0, 0);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001821 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001822
1823 // not back to the original point
1824 path.reset();
1825 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
1826 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001827 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001828
1829 test_circle_with_add_paths(reporter);
1830}
1831
1832static void test_oval(skiatest::Reporter* reporter) {
1833 SkRect rect;
1834 SkMatrix m;
1835 SkPath path;
1836
1837 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
1838 path.addOval(rect);
1839
1840 REPORTER_ASSERT(reporter, path.isOval(NULL));
1841
1842 m.setRotate(SkIntToScalar(90));
1843 SkPath tmp;
1844 path.transform(m, &tmp);
1845 // an oval rotated 90 degrees is still an oval.
1846 REPORTER_ASSERT(reporter, tmp.isOval(NULL));
1847
1848 m.reset();
1849 m.setRotate(SkIntToScalar(30));
1850 tmp.reset();
1851 path.transform(m, &tmp);
1852 // an oval rotated 30 degrees is not an oval anymore.
1853 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
1854
1855 // since empty path being transformed.
1856 path.reset();
1857 tmp.reset();
1858 m.reset();
1859 path.transform(m, &tmp);
1860 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
1861
1862 // empty path is not an oval
1863 tmp.reset();
1864 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
1865
1866 // only has moveTo()s
1867 tmp.reset();
1868 tmp.moveTo(0, 0);
1869 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
1870 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
1871
1872 // mimic WebKit's calling convention,
1873 // call moveTo() first and then call addOval()
1874 path.reset();
1875 path.moveTo(0, 0);
1876 path.addOval(rect);
1877 REPORTER_ASSERT(reporter, path.isOval(NULL));
1878
1879 // copy path
1880 path.reset();
1881 tmp.reset();
1882 tmp.addOval(rect);
1883 path = tmp;
1884 REPORTER_ASSERT(reporter, path.isOval(NULL));
1885}
1886
caryclark@google.com42639cd2012-06-06 12:03:39 +00001887static void TestPath(skiatest::Reporter* reporter) {
reed@android.com60bc6d52010-02-11 11:09:39 +00001888 SkTSize<SkScalar>::Make(3,4);
1889
reed@android.com3abec1d2009-03-02 05:36:20 +00001890 SkPath p, p2;
1891 SkRect bounds, bounds2;
reed@android.com80e39a72009-04-02 16:59:40 +00001892
reed@android.com3abec1d2009-03-02 05:36:20 +00001893 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001894 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00001895 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00001896 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00001897 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00001898 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
1899 REPORTER_ASSERT(reporter, !p.isInverseFillType());
1900 REPORTER_ASSERT(reporter, p == p2);
1901 REPORTER_ASSERT(reporter, !(p != p2));
1902
reed@android.comd252db02009-04-01 18:31:44 +00001903 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00001904
reed@android.com3abec1d2009-03-02 05:36:20 +00001905 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00001906
reed@android.com6b82d1a2009-06-03 02:35:01 +00001907 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
1908 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00001909 // we have quads or cubics
1910 REPORTER_ASSERT(reporter, p.getSegmentMasks() & kCurveSegmentMask);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001911 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00001912
reed@android.com6b82d1a2009-06-03 02:35:01 +00001913 p.reset();
reed@google.com10296cc2011-09-21 12:29:05 +00001914 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001915 REPORTER_ASSERT(reporter, p.isEmpty());
reed@google.com10296cc2011-09-21 12:29:05 +00001916
reed@android.com6b82d1a2009-06-03 02:35:01 +00001917 p.addOval(bounds);
1918 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001919 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00001920
reed@android.com6b82d1a2009-06-03 02:35:01 +00001921 p.reset();
reed@android.com3abec1d2009-03-02 05:36:20 +00001922 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00001923 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00001924 // we have only lines
1925 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001926 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00001927
1928 REPORTER_ASSERT(reporter, p != p2);
1929 REPORTER_ASSERT(reporter, !(p == p2));
1930
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00001931 // do getPoints and getVerbs return the right result
1932 REPORTER_ASSERT(reporter, p.getPoints(NULL, 0) == 4);
1933 REPORTER_ASSERT(reporter, p.getVerbs(NULL, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00001934 SkPoint pts[4];
1935 int count = p.getPoints(pts, 4);
1936 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00001937 uint8_t verbs[6];
1938 verbs[5] = 0xff;
1939 p.getVerbs(verbs, 5);
1940 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
1941 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
1942 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
1943 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
1944 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
1945 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00001946 bounds2.set(pts, 4);
1947 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00001948
reed@android.com3abec1d2009-03-02 05:36:20 +00001949 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
1950 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00001951 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00001952
reed@android.com3abec1d2009-03-02 05:36:20 +00001953 REPORTER_ASSERT(reporter, p.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00001954 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00001955 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
1956 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00001957
reed@android.com3abec1d2009-03-02 05:36:20 +00001958 // now force p to not be a rect
1959 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
1960 p.addRect(bounds);
1961 REPORTER_ASSERT(reporter, !p.isRect(NULL));
reed@android.com3abec1d2009-03-02 05:36:20 +00001962
reed@google.com7e6c4d12012-05-10 14:05:43 +00001963 test_isLine(reporter);
1964 test_isRect(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001965 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00001966 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00001967 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00001968 test_convexity2(reporter);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001969 test_conservativelyContains(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001970 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001971 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00001972 test_flattening(reporter);
1973 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00001974 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001975 test_iter(reporter);
1976 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001977 test_circle(reporter);
1978 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00001979 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00001980 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00001981 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00001982 test_isfinite_after_transform(reporter);
reed@google.com8cae8352012-09-14 15:18:41 +00001983 test_tricky_cubic(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00001984 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00001985 test_arb_zero_rad_round_rect_is_rect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00001986 test_addrect_isfinite(reporter);
reed@android.com3abec1d2009-03-02 05:36:20 +00001987}
1988
1989#include "TestClassDef.h"
1990DEFINE_TESTCLASS("Path", PathTestClass, TestPath)