blob: e52296d0169247429c0f99d51dc691fd7c94de5f [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.comf0ed80a2012-02-17 13:38:26 +0000347/**
348 * cheapIsDirection can take a shortcut when a path is marked convex.
349 * This function ensures that we always test cheapIsDirection when the path
350 * is flagged with unknown convexity status.
351 */
352static void check_direction(SkPath* path,
353 SkPath::Direction expectedDir,
354 skiatest::Reporter* reporter) {
355 if (SkPath::kConvex_Convexity == path->getConvexity()) {
356 REPORTER_ASSERT(reporter, path->cheapIsDirection(expectedDir));
357 path->setConvexity(SkPath::kUnknown_Convexity);
358 }
359 REPORTER_ASSERT(reporter, path->cheapIsDirection(expectedDir));
360}
361
reed@google.com3e71a882012-01-10 18:44:37 +0000362static void test_direction(skiatest::Reporter* reporter) {
363 size_t i;
364 SkPath path;
365 REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL));
366 REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCW_Direction));
367 REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCCW_Direction));
368
369 static const char* gDegen[] = {
370 "M 10 10",
371 "M 10 10 M 20 20",
372 "M 10 10 L 20 20",
373 "M 10 10 L 10 10 L 10 10",
374 "M 10 10 Q 10 10 10 10",
375 "M 10 10 C 10 10 10 10 10 10",
376 };
377 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
378 path.reset();
379 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
380 REPORTER_ASSERT(reporter, valid);
381 REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL));
382 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000383
reed@google.com3e71a882012-01-10 18:44:37 +0000384 static const char* gCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000385 "M 10 10 L 10 10 Q 20 10 20 20",
reed@google.com3e71a882012-01-10 18:44:37 +0000386 "M 10 10 C 20 10 20 20 20 20",
reed@google.comd4146662012-01-31 15:42:29 +0000387 "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 +0000388 // rect with top two corners replaced by cubics with identical middle
389 // control points
390 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10"
reed@google.com3e71a882012-01-10 18:44:37 +0000391 };
392 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
393 path.reset();
394 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
395 REPORTER_ASSERT(reporter, valid);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000396 check_direction(&path, SkPath::kCW_Direction, reporter);
reed@google.com3e71a882012-01-10 18:44:37 +0000397 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000398
reed@google.com3e71a882012-01-10 18:44:37 +0000399 static const char* gCCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000400 "M 10 10 L 10 10 Q 20 10 20 -20",
reed@google.com3e71a882012-01-10 18:44:37 +0000401 "M 10 10 C 20 10 20 -20 20 -20",
reed@google.comd4146662012-01-31 15:42:29 +0000402 "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 +0000403 // rect with top two corners replaced by cubics with identical middle
404 // control points
405 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10"
reed@google.com3e71a882012-01-10 18:44:37 +0000406 };
407 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
408 path.reset();
409 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
410 REPORTER_ASSERT(reporter, valid);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000411 check_direction(&path, SkPath::kCCW_Direction, reporter);
reed@google.com3e71a882012-01-10 18:44:37 +0000412 }
reed@google.comac8543f2012-01-30 20:51:25 +0000413
414 // Test two donuts, each wound a different direction. Only the outer contour
415 // determines the cheap direction
416 path.reset();
417 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
418 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000419 check_direction(&path, SkPath::kCW_Direction, reporter);
420
reed@google.comac8543f2012-01-30 20:51:25 +0000421 path.reset();
422 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
423 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000424 check_direction(&path, SkPath::kCCW_Direction, reporter);
425
bsalomon@google.com6843ac42012-02-17 13:49:03 +0000426#ifdef SK_SCALAR_IS_FLOAT
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000427 // triangle with one point really far from the origin.
428 path.reset();
429 // the first point is roughly 1.05e10, 1.05e10
bsalomon@google.com53aab782012-02-23 14:54:49 +0000430 path.moveTo(SkFloatToScalar(SkBits2Float(0x501c7652)), SkFloatToScalar(SkBits2Float(0x501c7652)));
431 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
432 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
433 check_direction(&path, SkPath::kCCW_Direction, reporter);
434#endif
reed@google.com3e71a882012-01-10 18:44:37 +0000435}
436
reed@google.comffdb0182011-11-14 19:29:14 +0000437static void add_rect(SkPath* path, const SkRect& r) {
438 path->moveTo(r.fLeft, r.fTop);
439 path->lineTo(r.fRight, r.fTop);
440 path->lineTo(r.fRight, r.fBottom);
441 path->lineTo(r.fLeft, r.fBottom);
442 path->close();
443}
444
445static void test_bounds(skiatest::Reporter* reporter) {
446 static const SkRect rects[] = {
reed@google.com3563c9e2011-11-14 19:34:57 +0000447 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
448 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
449 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
450 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
reed@google.comffdb0182011-11-14 19:29:14 +0000451 };
452
453 SkPath path0, path1;
454 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
455 path0.addRect(rects[i]);
456 add_rect(&path1, rects[i]);
457 }
458
459 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
460}
461
reed@google.com55b5f4b2011-09-07 12:23:41 +0000462static void stroke_cubic(const SkPoint pts[4]) {
463 SkPath path;
464 path.moveTo(pts[0]);
465 path.cubicTo(pts[1], pts[2], pts[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000466
reed@google.com55b5f4b2011-09-07 12:23:41 +0000467 SkPaint paint;
468 paint.setStyle(SkPaint::kStroke_Style);
469 paint.setStrokeWidth(SK_Scalar1 * 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000470
reed@google.com55b5f4b2011-09-07 12:23:41 +0000471 SkPath fill;
472 paint.getFillPath(path, &fill);
473}
474
475// just ensure this can run w/o any SkASSERTS firing in the debug build
476// we used to assert due to differences in how we determine a degenerate vector
477// but that was fixed with the introduction of SkPoint::CanNormalize
478static void stroke_tiny_cubic() {
479 SkPoint p0[] = {
480 { 372.0f, 92.0f },
481 { 372.0f, 92.0f },
482 { 372.0f, 92.0f },
483 { 372.0f, 92.0f },
484 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000485
reed@google.com55b5f4b2011-09-07 12:23:41 +0000486 stroke_cubic(p0);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000487
reed@google.com55b5f4b2011-09-07 12:23:41 +0000488 SkPoint p1[] = {
489 { 372.0f, 92.0f },
490 { 372.0007f, 92.000755f },
491 { 371.99927f, 92.003922f },
492 { 371.99826f, 92.003899f },
493 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000494
reed@google.com55b5f4b2011-09-07 12:23:41 +0000495 stroke_cubic(p1);
496}
497
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000498static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
499 for (int i = 0; i < 2; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +0000500 SkPath::Iter iter(path, SkToBool(i));
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000501 SkPoint mv;
502 SkPoint pts[4];
503 SkPath::Verb v;
504 int nMT = 0;
505 int nCL = 0;
tomhudson@google.com221db3c2011-07-28 21:10:29 +0000506 mv.set(0, 0);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000507 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
508 switch (v) {
509 case SkPath::kMove_Verb:
510 mv = pts[0];
511 ++nMT;
512 break;
513 case SkPath::kClose_Verb:
514 REPORTER_ASSERT(reporter, mv == pts[0]);
515 ++nCL;
516 break;
517 default:
518 break;
519 }
520 }
521 // if we force a close on the interator we should have a close
522 // for every moveTo
523 REPORTER_ASSERT(reporter, !i || nMT == nCL);
524 }
525}
526
527static void test_close(skiatest::Reporter* reporter) {
528 SkPath closePt;
529 closePt.moveTo(0, 0);
530 closePt.close();
531 check_close(reporter, closePt);
532
533 SkPath openPt;
534 openPt.moveTo(0, 0);
535 check_close(reporter, openPt);
536
537 SkPath empty;
538 check_close(reporter, empty);
539 empty.close();
540 check_close(reporter, empty);
541
542 SkPath rect;
543 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
544 check_close(reporter, rect);
545 rect.close();
546 check_close(reporter, rect);
547
548 SkPath quad;
549 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
550 check_close(reporter, quad);
551 quad.close();
552 check_close(reporter, quad);
553
554 SkPath cubic;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000555 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000556 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
557 check_close(reporter, cubic);
558 cubic.close();
559 check_close(reporter, cubic);
560
561 SkPath line;
562 line.moveTo(SK_Scalar1, SK_Scalar1);
563 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
564 check_close(reporter, line);
565 line.close();
566 check_close(reporter, line);
567
568 SkPath rect2;
569 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
570 rect2.close();
571 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
572 check_close(reporter, rect2);
573 rect2.close();
574 check_close(reporter, rect2);
575
576 SkPath oval3;
577 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
578 oval3.close();
579 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
580 check_close(reporter, oval3);
581 oval3.close();
582 check_close(reporter, oval3);
583
584 SkPath moves;
585 moves.moveTo(SK_Scalar1, SK_Scalar1);
586 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
587 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
588 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
589 check_close(reporter, moves);
reed@google.com55b5f4b2011-09-07 12:23:41 +0000590
591 stroke_tiny_cubic();
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000592}
593
reed@google.com7c424812011-05-15 04:38:34 +0000594static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
595 SkPath::Convexity expected) {
596 SkPath::Convexity c = SkPath::ComputeConvexity(path);
597 REPORTER_ASSERT(reporter, c == expected);
598}
599
600static void test_convexity2(skiatest::Reporter* reporter) {
601 SkPath pt;
602 pt.moveTo(0, 0);
603 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +0000604 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000605
reed@google.com7c424812011-05-15 04:38:34 +0000606 SkPath line;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000607 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
608 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000609 line.close();
reed@google.comb54455e2011-05-16 14:16:04 +0000610 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000611
reed@google.com7c424812011-05-15 04:38:34 +0000612 SkPath triLeft;
613 triLeft.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000614 triLeft.lineTo(SK_Scalar1, 0);
615 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000616 triLeft.close();
617 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000618
reed@google.com7c424812011-05-15 04:38:34 +0000619 SkPath triRight;
620 triRight.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000621 triRight.lineTo(-SK_Scalar1, 0);
622 triRight.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000623 triRight.close();
624 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000625
reed@google.com7c424812011-05-15 04:38:34 +0000626 SkPath square;
627 square.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000628 square.lineTo(SK_Scalar1, 0);
629 square.lineTo(SK_Scalar1, SK_Scalar1);
630 square.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000631 square.close();
632 check_convexity(reporter, square, SkPath::kConvex_Convexity);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000633
reed@google.com7c424812011-05-15 04:38:34 +0000634 SkPath redundantSquare;
635 redundantSquare.moveTo(0, 0);
636 redundantSquare.lineTo(0, 0);
637 redundantSquare.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000638 redundantSquare.lineTo(SK_Scalar1, 0);
639 redundantSquare.lineTo(SK_Scalar1, 0);
640 redundantSquare.lineTo(SK_Scalar1, 0);
641 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
642 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
643 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
644 redundantSquare.lineTo(0, SK_Scalar1);
645 redundantSquare.lineTo(0, SK_Scalar1);
646 redundantSquare.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000647 redundantSquare.close();
648 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000649
reed@google.com7c424812011-05-15 04:38:34 +0000650 SkPath bowTie;
651 bowTie.moveTo(0, 0);
652 bowTie.lineTo(0, 0);
653 bowTie.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000654 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
655 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
656 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
657 bowTie.lineTo(SK_Scalar1, 0);
658 bowTie.lineTo(SK_Scalar1, 0);
659 bowTie.lineTo(SK_Scalar1, 0);
660 bowTie.lineTo(0, SK_Scalar1);
661 bowTie.lineTo(0, SK_Scalar1);
662 bowTie.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000663 bowTie.close();
664 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000665
reed@google.com7c424812011-05-15 04:38:34 +0000666 SkPath spiral;
667 spiral.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000668 spiral.lineTo(100*SK_Scalar1, 0);
669 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
670 spiral.lineTo(0, 100*SK_Scalar1);
671 spiral.lineTo(0, 50*SK_Scalar1);
672 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
673 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000674 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +0000675 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000676
reed@google.com7c424812011-05-15 04:38:34 +0000677 SkPath dent;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000678 dent.moveTo(0, 0);
679 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
680 dent.lineTo(0, 100*SK_Scalar1);
681 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
682 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000683 dent.close();
684 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
685}
686
reed@android.com6b82d1a2009-06-03 02:35:01 +0000687static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
688 const SkRect& bounds) {
689 REPORTER_ASSERT(reporter, p.isConvex());
690 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +0000691
reed@android.com6b82d1a2009-06-03 02:35:01 +0000692 SkPath p2(p);
693 REPORTER_ASSERT(reporter, p2.isConvex());
694 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
695
696 SkPath other;
697 other.swap(p2);
698 REPORTER_ASSERT(reporter, other.isConvex());
699 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
700}
701
reed@google.com04863fa2011-05-15 04:08:24 +0000702static void setFromString(SkPath* path, const char str[]) {
703 bool first = true;
704 while (str) {
705 SkScalar x, y;
706 str = SkParse::FindScalar(str, &x);
707 if (NULL == str) {
708 break;
709 }
710 str = SkParse::FindScalar(str, &y);
711 SkASSERT(str);
712 if (first) {
713 path->moveTo(x, y);
714 first = false;
715 } else {
716 path->lineTo(x, y);
717 }
718 }
719}
720
721static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +0000722 static const SkPath::Convexity C = SkPath::kConcave_Convexity;
723 static const SkPath::Convexity V = SkPath::kConvex_Convexity;
724
725 SkPath path;
726
reed@google.comb54455e2011-05-16 14:16:04 +0000727 REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path));
reed@google.come3543972012-01-10 18:59:22 +0000728 path.addCircle(0, 0, SkIntToScalar(10));
reed@google.com04863fa2011-05-15 04:08:24 +0000729 REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path));
reed@google.come3543972012-01-10 18:59:22 +0000730 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
reed@google.com04863fa2011-05-15 04:08:24 +0000731 REPORTER_ASSERT(reporter, C == SkPath::ComputeConvexity(path));
732 path.reset();
reed@google.come3543972012-01-10 18:59:22 +0000733 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
reed@google.com04863fa2011-05-15 04:08:24 +0000734 REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path));
reed@google.com3e71a882012-01-10 18:44:37 +0000735 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCCW_Direction));
reed@google.com04863fa2011-05-15 04:08:24 +0000736 path.reset();
reed@google.come3543972012-01-10 18:59:22 +0000737 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
reed@google.com04863fa2011-05-15 04:08:24 +0000738 REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path));
reed@google.com3e71a882012-01-10 18:44:37 +0000739 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCW_Direction));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000740
reed@google.com04863fa2011-05-15 04:08:24 +0000741 static const struct {
742 const char* fPathStr;
743 SkPath::Convexity fExpectedConvexity;
744 } gRec[] = {
reed@google.comb54455e2011-05-16 14:16:04 +0000745 { "", SkPath::kConvex_Convexity },
746 { "0 0", SkPath::kConvex_Convexity },
747 { "0 0 10 10", SkPath::kConvex_Convexity },
reed@google.com85b6e392011-05-15 20:25:17 +0000748 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity },
reed@google.com04863fa2011-05-15 04:08:24 +0000749 { "0 0 10 10 10 20", SkPath::kConvex_Convexity },
750 { "0 0 10 10 10 0", SkPath::kConvex_Convexity },
751 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity },
752 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity },
753 };
754
755 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
756 SkPath path;
757 setFromString(&path, gRec[i].fPathStr);
758 SkPath::Convexity c = SkPath::ComputeConvexity(path);
759 REPORTER_ASSERT(reporter, c == gRec[i].fExpectedConvexity);
760 }
761}
762
reed@google.com7e6c4d12012-05-10 14:05:43 +0000763static void test_isLine(skiatest::Reporter* reporter) {
764 SkPath path;
765 SkPoint pts[2];
766 const SkScalar value = SkIntToScalar(5);
767
768 REPORTER_ASSERT(reporter, !path.isLine(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000769
reed@google.com7e6c4d12012-05-10 14:05:43 +0000770 // set some non-zero values
771 pts[0].set(value, value);
772 pts[1].set(value, value);
773 REPORTER_ASSERT(reporter, !path.isLine(pts));
774 // check that pts was untouched
775 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
776 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
777
778 const SkScalar moveX = SkIntToScalar(1);
779 const SkScalar moveY = SkIntToScalar(2);
780 SkASSERT(value != moveX && value != moveY);
781
782 path.moveTo(moveX, moveY);
783 REPORTER_ASSERT(reporter, !path.isLine(NULL));
784 REPORTER_ASSERT(reporter, !path.isLine(pts));
785 // check that pts was untouched
786 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
787 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
788
789 const SkScalar lineX = SkIntToScalar(2);
790 const SkScalar lineY = SkIntToScalar(2);
791 SkASSERT(value != lineX && value != lineY);
792
793 path.lineTo(lineX, lineY);
794 REPORTER_ASSERT(reporter, path.isLine(NULL));
795
796 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
797 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
798 REPORTER_ASSERT(reporter, path.isLine(pts));
799 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
800 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
801
802 path.lineTo(0, 0); // too many points/verbs
803 REPORTER_ASSERT(reporter, !path.isLine(NULL));
804 REPORTER_ASSERT(reporter, !path.isLine(pts));
805 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
806 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
807}
808
caryclark@google.comf1316942011-07-26 19:54:45 +0000809// Simple isRect test is inline TestPath, below.
810// test_isRect provides more extensive testing.
811static void test_isRect(skiatest::Reporter* reporter) {
812 // passing tests (all moveTo / lineTo...
813 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
814 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
815 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
816 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
817 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
818 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
819 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
820 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
821 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
822 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f},
823 {1, 0}, {.5f, 0}};
824 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1},
825 {0, 1}, {0, .5f}};
826 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
827 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
828 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
rmistry@google.comd6176b02012-08-23 18:14:13 +0000829
caryclark@google.comf1316942011-07-26 19:54:45 +0000830 // failing tests
831 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
832 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
833 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
834 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
835 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
836 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
837 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
838 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
rmistry@google.comd6176b02012-08-23 18:14:13 +0000839
caryclark@google.comf1316942011-07-26 19:54:45 +0000840 // failing, no close
841 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
842 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
843
844 size_t testLen[] = {
845 sizeof(r1), sizeof(r2), sizeof(r3), sizeof(r4), sizeof(r5), sizeof(r6),
846 sizeof(r7), sizeof(r8), sizeof(r9), sizeof(ra), sizeof(rb), sizeof(rc),
847 sizeof(rd), sizeof(re),
848 sizeof(f1), sizeof(f2), sizeof(f3), sizeof(f4), sizeof(f5), sizeof(f6),
849 sizeof(f7), sizeof(f8),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000850 sizeof(c1), sizeof(c2)
caryclark@google.comf1316942011-07-26 19:54:45 +0000851 };
852 SkPoint* tests[] = {
853 r1, r2, r3, r4, r5, r6, r7, r8, r9, ra, rb, rc, rd, re,
854 f1, f2, f3, f4, f5, f6, f7, f8,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000855 c1, c2
caryclark@google.comf1316942011-07-26 19:54:45 +0000856 };
857 SkPoint* lastPass = re;
858 SkPoint* lastClose = f8;
859 bool fail = false;
860 bool close = true;
861 const size_t testCount = sizeof(tests) / sizeof(tests[0]);
862 size_t index;
863 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
864 SkPath path;
865 path.moveTo(tests[testIndex][0].fX, tests[testIndex][0].fY);
866 for (index = 1; index < testLen[testIndex] / sizeof(SkPoint); ++index) {
867 path.lineTo(tests[testIndex][index].fX, tests[testIndex][index].fY);
868 }
869 if (close) {
870 path.close();
871 }
872 REPORTER_ASSERT(reporter, fail ^ path.isRect(0));
873 if (tests[testIndex] == lastPass) {
874 fail = true;
875 }
876 if (tests[testIndex] == lastClose) {
877 close = false;
878 }
879 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000880
caryclark@google.comf1316942011-07-26 19:54:45 +0000881 // fail, close then line
882 SkPath path1;
883 path1.moveTo(r1[0].fX, r1[0].fY);
884 for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
885 path1.lineTo(r1[index].fX, r1[index].fY);
886 }
887 path1.close();
888 path1.lineTo(1, 0);
889 REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000890
caryclark@google.comf1316942011-07-26 19:54:45 +0000891 // fail, move in the middle
892 path1.reset();
893 path1.moveTo(r1[0].fX, r1[0].fY);
894 for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
895 if (index == 2) {
896 path1.moveTo(1, .5f);
897 }
898 path1.lineTo(r1[index].fX, r1[index].fY);
899 }
900 path1.close();
901 REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
902
903 // fail, move on the edge
904 path1.reset();
905 for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
906 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
907 path1.lineTo(r1[index].fX, r1[index].fY);
908 }
909 path1.close();
910 REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000911
caryclark@google.comf1316942011-07-26 19:54:45 +0000912 // fail, quad
913 path1.reset();
914 path1.moveTo(r1[0].fX, r1[0].fY);
915 for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
916 if (index == 2) {
917 path1.quadTo(1, .5f, 1, .5f);
918 }
919 path1.lineTo(r1[index].fX, r1[index].fY);
920 }
921 path1.close();
922 REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000923
caryclark@google.comf1316942011-07-26 19:54:45 +0000924 // fail, cubic
925 path1.reset();
926 path1.moveTo(r1[0].fX, r1[0].fY);
927 for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
928 if (index == 2) {
929 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
930 }
931 path1.lineTo(r1[index].fX, r1[index].fY);
932 }
933 path1.close();
934 REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
935}
936
robertphillips@google.com2972bb52012-08-07 17:32:51 +0000937static void write_and_read_back(skiatest::Reporter* reporter,
938 const SkPath& p) {
939 SkWriter32 writer(100);
940 writer.writePath(p);
941 size_t size = writer.size();
942 SkAutoMalloc storage(size);
943 writer.flatten(storage.get());
944 SkReader32 reader(storage.get(), size);
945
946 SkPath readBack;
947 REPORTER_ASSERT(reporter, readBack != p);
948 reader.readPath(&readBack);
949 REPORTER_ASSERT(reporter, readBack == p);
950
rmistry@google.comd6176b02012-08-23 18:14:13 +0000951 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +0000952 p.getConvexityOrUnknown());
953
954 REPORTER_ASSERT(reporter, readBack.isOval(NULL) == p.isOval(NULL));
955
956 const SkRect& origBounds = p.getBounds();
957 const SkRect& readBackBounds = readBack.getBounds();
958
959 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
960}
961
reed@google.com53effc52011-09-21 19:05:12 +0000962static void test_flattening(skiatest::Reporter* reporter) {
963 SkPath p;
964
965 static const SkPoint pts[] = {
966 { 0, 0 },
967 { SkIntToScalar(10), SkIntToScalar(10) },
968 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
969 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
970 };
971 p.moveTo(pts[0]);
972 p.lineTo(pts[1]);
973 p.quadTo(pts[2], pts[3]);
974 p.cubicTo(pts[4], pts[5], pts[6]);
975
robertphillips@google.com2972bb52012-08-07 17:32:51 +0000976 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000977
978 // create a buffer that should be much larger than the path so we don't
979 // kill our stack if writer goes too far.
980 char buffer[1024];
981 uint32_t size1 = p.writeToMemory(NULL);
982 uint32_t size2 = p.writeToMemory(buffer);
983 REPORTER_ASSERT(reporter, size1 == size2);
984
985 SkPath p2;
986 uint32_t size3 = p2.readFromMemory(buffer);
987 REPORTER_ASSERT(reporter, size1 == size3);
988 REPORTER_ASSERT(reporter, p == p2);
989
990 char buffer2[1024];
991 size3 = p2.writeToMemory(buffer2);
992 REPORTER_ASSERT(reporter, size1 == size3);
993 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +0000994
995 // test persistence of the oval flag & convexity
996 {
997 SkPath oval;
998 SkRect rect = SkRect::MakeWH(10, 10);
999 oval.addOval(rect);
1000
1001 write_and_read_back(reporter, oval);
1002 }
reed@google.com53effc52011-09-21 19:05:12 +00001003}
1004
1005static void test_transform(skiatest::Reporter* reporter) {
1006 SkPath p, p1;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001007
reed@google.com53effc52011-09-21 19:05:12 +00001008 static const SkPoint pts[] = {
1009 { 0, 0 },
1010 { SkIntToScalar(10), SkIntToScalar(10) },
1011 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
1012 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
1013 };
1014 p.moveTo(pts[0]);
1015 p.lineTo(pts[1]);
1016 p.quadTo(pts[2], pts[3]);
1017 p.cubicTo(pts[4], pts[5], pts[6]);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001018
reed@google.com53effc52011-09-21 19:05:12 +00001019 SkMatrix matrix;
1020 matrix.reset();
1021 p.transform(matrix, &p1);
1022 REPORTER_ASSERT(reporter, p == p1);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001023
reed@google.com53effc52011-09-21 19:05:12 +00001024 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
1025 p.transform(matrix, &p1);
1026 SkPoint pts1[7];
1027 int count = p1.getPoints(pts1, 7);
1028 REPORTER_ASSERT(reporter, 7 == count);
1029 for (int i = 0; i < count; ++i) {
1030 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
1031 REPORTER_ASSERT(reporter, newPt == pts1[i]);
1032 }
1033}
1034
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001035static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001036 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00001037 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001038
schenney@chromium.org7e963602012-06-13 17:05:43 +00001039 struct zeroPathTestData {
1040 const char* testPath;
1041 const size_t numResultPts;
1042 const SkRect resultBound;
1043 const SkPath::Verb* resultVerbs;
1044 const size_t numResultVerbs;
1045 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001046
schenney@chromium.org7e963602012-06-13 17:05:43 +00001047 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
1048 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
1049 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
1050 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
1051 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
1052 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
1053 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
1054 static const SkPath::Verb resultVerbs8[] = {
1055 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
1056 };
1057 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
1058 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
1059 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
1060 static const SkPath::Verb resultVerbs12[] = {
1061 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
1062 };
1063 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
1064 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
1065 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
1066 static const SkPath::Verb resultVerbs16[] = {
1067 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
1068 };
1069 static const struct zeroPathTestData gZeroLengthTests[] = {
1070 { "M 1 1", 1, {0, 0, 0, 0}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001071 { "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 +00001072 { "M 1 1 z", 1, {0, 0, 0, 0}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001073 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
1074 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
1075 { "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) },
1076 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
1077 { "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) },
1078 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
1079 { "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) },
1080 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
1081 { "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) },
1082 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
1083 { "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 +00001084 SK_ARRAY_COUNT(resultVerbs14)
1085 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001086 { "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) },
1087 { "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 +00001088 SK_ARRAY_COUNT(resultVerbs16)
1089 }
1090 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001091
schenney@chromium.org7e963602012-06-13 17:05:43 +00001092 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
1093 p.reset();
1094 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
1095 REPORTER_ASSERT(reporter, valid);
1096 REPORTER_ASSERT(reporter, !p.isEmpty());
1097 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
1098 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
1099 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
1100 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
1101 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
1102 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00001103 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001104}
1105
1106struct SegmentInfo {
1107 SkPath fPath;
1108 int fPointCount;
1109};
1110
reed@google.com10296cc2011-09-21 12:29:05 +00001111#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
1112
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001113static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00001114 SkPath p, p2;
1115
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001116 p.moveTo(0, 0);
1117 p.quadTo(100, 100, 200, 200);
1118 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
1119 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00001120 p2 = p;
1121 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001122 p.cubicTo(100, 100, 200, 200, 300, 300);
1123 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
1124 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00001125 p2 = p;
1126 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
1127
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001128 p.reset();
1129 p.moveTo(0, 0);
1130 p.cubicTo(100, 100, 200, 200, 300, 300);
1131 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00001132 p2 = p;
1133 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001134
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001135 REPORTER_ASSERT(reporter, !p.isEmpty());
1136}
1137
1138static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00001139 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001140 SkPoint pts[4];
1141
1142 // Test an iterator with no path
1143 SkPath::Iter noPathIter;
1144 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00001145
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001146 // Test that setting an empty path works
1147 noPathIter.setPath(p, false);
1148 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00001149
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001150 // Test that close path makes no difference for an empty path
1151 noPathIter.setPath(p, true);
1152 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00001153
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001154 // Test an iterator with an initial empty path
1155 SkPath::Iter iter(p, false);
1156 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1157
1158 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00001159 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001160 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1161
rmistry@google.comd6176b02012-08-23 18:14:13 +00001162
schenney@chromium.org7e963602012-06-13 17:05:43 +00001163 struct iterTestData {
1164 const char* testPath;
1165 const bool forceClose;
1166 const bool consumeDegenerates;
1167 const size_t* numResultPtsPerVerb;
1168 const SkPoint* resultPts;
1169 const SkPath::Verb* resultVerbs;
1170 const size_t numResultVerbs;
1171 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001172
schenney@chromium.org7e963602012-06-13 17:05:43 +00001173 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
1174 static const SkPath::Verb resultVerbs2[] = {
1175 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
1176 };
1177 static const SkPath::Verb resultVerbs3[] = {
1178 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
1179 };
1180 static const SkPath::Verb resultVerbs4[] = {
1181 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
1182 };
1183 static const SkPath::Verb resultVerbs5[] = {
1184 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
1185 };
1186 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00001187 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
1188 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
1189 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
1190 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001191 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00001192 static const SkPoint resultPts2[] = {
1193 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
1194 };
1195 static const SkPoint resultPts3[] = {
1196 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
1197 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
1198 };
1199 static const SkPoint resultPts4[] = {
1200 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
1201 };
1202 static const SkPoint resultPts5[] = {
1203 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
1204 };
1205 static const struct iterTestData gIterTests[] = {
1206 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001207 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1208 { "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 +00001209 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1210 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1211 { "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) },
1212 { "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 +00001213 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
1214 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
1215 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1216 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1217 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
1218 { "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 +00001219 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001220
schenney@chromium.org7e963602012-06-13 17:05:43 +00001221 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
1222 p.reset();
1223 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
1224 REPORTER_ASSERT(reporter, valid);
1225 iter.setPath(p, gIterTests[i].forceClose);
1226 int j = 0, l = 0;
1227 do {
1228 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
1229 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
1230 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
1231 }
1232 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
1233 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
1234 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001235
1236 // The GM degeneratesegments.cpp test is more extensive
1237}
1238
1239static void test_raw_iter(skiatest::Reporter* reporter) {
1240 SkPath p;
1241 SkPoint pts[4];
1242
1243 // Test an iterator with no path
1244 SkPath::RawIter noPathIter;
1245 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
1246 // Test that setting an empty path works
1247 noPathIter.setPath(p);
1248 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001249
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001250 // Test an iterator with an initial empty path
1251 SkPath::RawIter iter(p);
1252 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1253
1254 // Test that a move-only path returns the move.
1255 p.moveTo(SK_Scalar1, 0);
1256 iter.setPath(p);
1257 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1258 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
1259 REPORTER_ASSERT(reporter, pts[0].fY == 0);
1260 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1261
1262 // No matter how many moves we add, we should get them all back
1263 p.moveTo(SK_Scalar1*2, SK_Scalar1);
1264 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
1265 iter.setPath(p);
1266 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1267 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
1268 REPORTER_ASSERT(reporter, pts[0].fY == 0);
1269 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1270 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
1271 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
1272 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1273 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
1274 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
1275 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1276
1277 // Initial close is never ever stored
1278 p.reset();
1279 p.close();
1280 iter.setPath(p);
1281 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1282
1283 // Move/close sequences
1284 p.reset();
1285 p.close(); // Not stored, no purpose
1286 p.moveTo(SK_Scalar1, 0);
1287 p.close();
1288 p.close(); // Not stored, no purpose
1289 p.moveTo(SK_Scalar1*2, SK_Scalar1);
1290 p.close();
1291 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
1292 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
1293 p.close();
1294 iter.setPath(p);
1295 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1296 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
1297 REPORTER_ASSERT(reporter, pts[0].fY == 0);
1298 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
1299 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
1300 REPORTER_ASSERT(reporter, pts[0].fY == 0);
1301 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1302 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
1303 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
1304 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
1305 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
1306 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
1307 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1308 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
1309 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
1310 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1311 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
1312 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
1313 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
1314 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
1315 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
1316 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1317
1318 // Generate random paths and verify
1319 SkPoint randomPts[25];
1320 for (int i = 0; i < 5; ++i) {
1321 for (int j = 0; j < 5; ++j) {
1322 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
1323 }
1324 }
1325
1326 // Max of 10 segments, max 3 points per segment
1327 SkRandom rand(9876543);
1328 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00001329 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001330 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00001331
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001332 for (int i = 0; i < 500; ++i) {
1333 p.reset();
1334 bool lastWasClose = true;
1335 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00001336 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001337 int numPoints = 0;
1338 int numVerbs = (rand.nextU() >> 16) % 10;
1339 int numIterVerbs = 0;
1340 for (int j = 0; j < numVerbs; ++j) {
1341 do {
1342 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
1343 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001344 switch (nextVerb) {
1345 case SkPath::kMove_Verb:
1346 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
1347 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00001348 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001349 numPoints += 1;
1350 lastWasClose = false;
1351 haveMoveTo = true;
1352 break;
1353 case SkPath::kLine_Verb:
1354 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00001355 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001356 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
1357 haveMoveTo = true;
1358 }
1359 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
1360 p.lineTo(expectedPts[numPoints]);
1361 numPoints += 1;
1362 lastWasClose = false;
1363 break;
1364 case SkPath::kQuad_Verb:
1365 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00001366 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001367 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
1368 haveMoveTo = true;
1369 }
1370 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
1371 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
1372 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
1373 numPoints += 2;
1374 lastWasClose = false;
1375 break;
1376 case SkPath::kCubic_Verb:
1377 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00001378 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001379 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
1380 haveMoveTo = true;
1381 }
1382 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
1383 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
1384 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
1385 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
1386 expectedPts[numPoints + 2]);
1387 numPoints += 3;
1388 lastWasClose = false;
1389 break;
1390 case SkPath::kClose_Verb:
1391 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00001392 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001393 lastWasClose = true;
1394 break;
1395 default:;
1396 }
1397 expectedVerbs[numIterVerbs++] = nextVerb;
1398 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001399
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001400 iter.setPath(p);
1401 numVerbs = numIterVerbs;
1402 numIterVerbs = 0;
1403 int numIterPts = 0;
1404 SkPoint lastMoveTo;
1405 SkPoint lastPt;
1406 lastMoveTo.set(0, 0);
1407 lastPt.set(0, 0);
1408 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
1409 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
1410 numIterVerbs++;
1411 switch (nextVerb) {
1412 case SkPath::kMove_Verb:
1413 REPORTER_ASSERT(reporter, numIterPts < numPoints);
1414 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
1415 lastPt = lastMoveTo = pts[0];
1416 numIterPts += 1;
1417 break;
1418 case SkPath::kLine_Verb:
1419 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
1420 REPORTER_ASSERT(reporter, pts[0] == lastPt);
1421 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
1422 lastPt = pts[1];
1423 numIterPts += 1;
1424 break;
1425 case SkPath::kQuad_Verb:
1426 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
1427 REPORTER_ASSERT(reporter, pts[0] == lastPt);
1428 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
1429 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
1430 lastPt = pts[2];
1431 numIterPts += 2;
1432 break;
1433 case SkPath::kCubic_Verb:
1434 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
1435 REPORTER_ASSERT(reporter, pts[0] == lastPt);
1436 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
1437 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
1438 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
1439 lastPt = pts[3];
1440 numIterPts += 3;
1441 break;
1442 case SkPath::kClose_Verb:
1443 REPORTER_ASSERT(reporter, pts[0] == lastMoveTo);
1444 lastPt = lastMoveTo;
1445 break;
1446 default:;
1447 }
1448 }
1449 REPORTER_ASSERT(reporter, numIterPts == numPoints);
1450 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
1451 }
1452}
1453
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001454static void check_for_circle(skiatest::Reporter* reporter,
1455 const SkPath& path, bool expected) {
1456 SkRect rect;
1457 REPORTER_ASSERT(reporter, path.isOval(&rect) == expected);
1458 if (expected) {
1459 REPORTER_ASSERT(reporter, rect.height() == rect.width());
1460 }
1461}
1462
1463static void test_circle_skew(skiatest::Reporter* reporter,
1464 const SkPath& path) {
1465 SkPath tmp;
1466
1467 SkMatrix m;
1468 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
1469 path.transform(m, &tmp);
1470 check_for_circle(reporter, tmp, false);
1471}
1472
1473static void test_circle_translate(skiatest::Reporter* reporter,
1474 const SkPath& path) {
1475 SkPath tmp;
1476
1477 // translate at small offset
1478 SkMatrix m;
1479 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
1480 path.transform(m, &tmp);
1481 check_for_circle(reporter, tmp, true);
1482
1483 tmp.reset();
1484 m.reset();
1485
1486 // translate at a relatively big offset
1487 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
1488 path.transform(m, &tmp);
1489 check_for_circle(reporter, tmp, true);
1490}
1491
1492static void test_circle_rotate(skiatest::Reporter* reporter,
1493 const SkPath& path) {
1494 for (int angle = 0; angle < 360; ++angle) {
1495 SkPath tmp;
1496 SkMatrix m;
1497 m.setRotate(SkIntToScalar(angle));
1498 path.transform(m, &tmp);
1499
1500 // TODO: a rotated circle whose rotated angle is not a mutiple of 90
1501 // degrees is not an oval anymore, this can be improved. we made this
1502 // for the simplicity of our implementation.
1503 if (angle % 90 == 0) {
1504 check_for_circle(reporter, tmp, true);
1505 } else {
1506 check_for_circle(reporter, tmp, false);
1507 }
1508 }
1509}
1510
1511static void test_circle_with_direction(skiatest::Reporter* reporter,
1512 SkPath::Direction dir) {
1513 SkPath path;
1514
1515 // circle at origin
1516 path.addCircle(0, 0, SkIntToScalar(20), dir);
1517 check_for_circle(reporter, path, true);
1518 test_circle_rotate(reporter, path);
1519 test_circle_translate(reporter, path);
1520 test_circle_skew(reporter, path);
1521
1522 // circle at an offset at (10, 10)
1523 path.reset();
1524 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
1525 SkIntToScalar(20), dir);
1526 check_for_circle(reporter, path, true);
1527 test_circle_rotate(reporter, path);
1528 test_circle_translate(reporter, path);
1529 test_circle_skew(reporter, path);
1530}
1531
1532static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
1533 SkPath path;
1534 SkPath circle;
1535 SkPath rect;
1536 SkPath empty;
1537
1538 circle.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
1539 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
1540 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
1541
1542 SkMatrix translate;
1543 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
1544
1545 // For simplicity, all the path concatenation related operations
1546 // would mark it non-circle, though in theory it's still a circle.
1547
1548 // empty + circle (translate)
1549 path = empty;
1550 path.addPath(circle, translate);
1551 check_for_circle(reporter, path, false);
1552
1553 // circle + empty (translate)
1554 path = circle;
1555 path.addPath(empty, translate);
1556 check_for_circle(reporter, path, false);
1557
1558 // test reverseAddPath
1559 path = circle;
1560 path.reverseAddPath(rect);
1561 check_for_circle(reporter, path, false);
1562}
1563
1564static void test_circle(skiatest::Reporter* reporter) {
1565 test_circle_with_direction(reporter, SkPath::kCW_Direction);
1566 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
1567
1568 // multiple addCircle()
1569 SkPath path;
1570 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
1571 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
1572 check_for_circle(reporter, path, false);
1573
1574 // some extra lineTo() would make isOval() fail
1575 path.reset();
1576 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
1577 path.lineTo(0, 0);
1578 check_for_circle(reporter, path, false);
1579
1580 // not back to the original point
1581 path.reset();
1582 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
1583 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
1584 check_for_circle(reporter, path, false);
1585
1586 test_circle_with_add_paths(reporter);
1587}
1588
1589static void test_oval(skiatest::Reporter* reporter) {
1590 SkRect rect;
1591 SkMatrix m;
1592 SkPath path;
1593
1594 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
1595 path.addOval(rect);
1596
1597 REPORTER_ASSERT(reporter, path.isOval(NULL));
1598
1599 m.setRotate(SkIntToScalar(90));
1600 SkPath tmp;
1601 path.transform(m, &tmp);
1602 // an oval rotated 90 degrees is still an oval.
1603 REPORTER_ASSERT(reporter, tmp.isOval(NULL));
1604
1605 m.reset();
1606 m.setRotate(SkIntToScalar(30));
1607 tmp.reset();
1608 path.transform(m, &tmp);
1609 // an oval rotated 30 degrees is not an oval anymore.
1610 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
1611
1612 // since empty path being transformed.
1613 path.reset();
1614 tmp.reset();
1615 m.reset();
1616 path.transform(m, &tmp);
1617 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
1618
1619 // empty path is not an oval
1620 tmp.reset();
1621 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
1622
1623 // only has moveTo()s
1624 tmp.reset();
1625 tmp.moveTo(0, 0);
1626 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
1627 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
1628
1629 // mimic WebKit's calling convention,
1630 // call moveTo() first and then call addOval()
1631 path.reset();
1632 path.moveTo(0, 0);
1633 path.addOval(rect);
1634 REPORTER_ASSERT(reporter, path.isOval(NULL));
1635
1636 // copy path
1637 path.reset();
1638 tmp.reset();
1639 tmp.addOval(rect);
1640 path = tmp;
1641 REPORTER_ASSERT(reporter, path.isOval(NULL));
1642}
1643
caryclark@google.com42639cd2012-06-06 12:03:39 +00001644static void TestPath(skiatest::Reporter* reporter) {
reed@android.com60bc6d52010-02-11 11:09:39 +00001645 SkTSize<SkScalar>::Make(3,4);
1646
reed@android.com3abec1d2009-03-02 05:36:20 +00001647 SkPath p, p2;
1648 SkRect bounds, bounds2;
reed@android.com80e39a72009-04-02 16:59:40 +00001649
reed@android.com3abec1d2009-03-02 05:36:20 +00001650 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001651 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00001652 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00001653 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00001654 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00001655 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
1656 REPORTER_ASSERT(reporter, !p.isInverseFillType());
1657 REPORTER_ASSERT(reporter, p == p2);
1658 REPORTER_ASSERT(reporter, !(p != p2));
1659
reed@android.comd252db02009-04-01 18:31:44 +00001660 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00001661
reed@android.com3abec1d2009-03-02 05:36:20 +00001662 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00001663
reed@android.com6b82d1a2009-06-03 02:35:01 +00001664 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
1665 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00001666 // we have quads or cubics
1667 REPORTER_ASSERT(reporter, p.getSegmentMasks() & kCurveSegmentMask);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001668 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00001669
reed@android.com6b82d1a2009-06-03 02:35:01 +00001670 p.reset();
reed@google.com10296cc2011-09-21 12:29:05 +00001671 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001672 REPORTER_ASSERT(reporter, p.isEmpty());
reed@google.com10296cc2011-09-21 12:29:05 +00001673
reed@android.com6b82d1a2009-06-03 02:35:01 +00001674 p.addOval(bounds);
1675 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001676 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00001677
reed@android.com6b82d1a2009-06-03 02:35:01 +00001678 p.reset();
reed@android.com3abec1d2009-03-02 05:36:20 +00001679 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00001680 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00001681 // we have only lines
1682 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001683 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00001684
1685 REPORTER_ASSERT(reporter, p != p2);
1686 REPORTER_ASSERT(reporter, !(p == p2));
1687
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00001688 // do getPoints and getVerbs return the right result
1689 REPORTER_ASSERT(reporter, p.getPoints(NULL, 0) == 4);
1690 REPORTER_ASSERT(reporter, p.getVerbs(NULL, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00001691 SkPoint pts[4];
1692 int count = p.getPoints(pts, 4);
1693 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00001694 uint8_t verbs[6];
1695 verbs[5] = 0xff;
1696 p.getVerbs(verbs, 5);
1697 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
1698 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
1699 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
1700 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
1701 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
1702 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00001703 bounds2.set(pts, 4);
1704 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00001705
reed@android.com3abec1d2009-03-02 05:36:20 +00001706 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
1707 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00001708 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00001709
reed@android.com3abec1d2009-03-02 05:36:20 +00001710 REPORTER_ASSERT(reporter, p.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00001711 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00001712 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
1713 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00001714
reed@android.com3abec1d2009-03-02 05:36:20 +00001715 // now force p to not be a rect
1716 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
1717 p.addRect(bounds);
1718 REPORTER_ASSERT(reporter, !p.isRect(NULL));
reed@android.com3abec1d2009-03-02 05:36:20 +00001719
reed@google.com7e6c4d12012-05-10 14:05:43 +00001720 test_isLine(reporter);
1721 test_isRect(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001722 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00001723 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00001724 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00001725 test_convexity2(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001726 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001727 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00001728 test_flattening(reporter);
1729 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00001730 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001731 test_iter(reporter);
1732 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001733 test_circle(reporter);
1734 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00001735 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00001736 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00001737 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00001738 test_isfinite_after_transform(reporter);
reed@google.com8cae8352012-09-14 15:18:41 +00001739 test_tricky_cubic(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00001740 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00001741 test_arb_zero_rad_round_rect_is_rect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00001742 test_addrect_isfinite(reporter);
reed@android.com3abec1d2009-03-02 05:36:20 +00001743}
1744
1745#include "TestClassDef.h"
1746DEFINE_TESTCLASS("Path", PathTestClass, TestPath)