blob: b9add7afb59c0e0fedeb8b6f68ec9b71812a88d4 [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
28// Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
29// which triggered an assert, from a tricky cubic. This test replicates that
30// example, so we can ensure that we handle it (in SkEdge.cpp), and don't
31// assert in the SK_DEBUG build.
32static void test_tricky_cubic(skiatest::Reporter* reporter) {
33 const SkPoint pts[] = {
34 { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
35 { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
36 { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
37 { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
38 };
39
40 SkPath path;
41 path.moveTo(pts[0]);
42 path.cubicTo(pts[1], pts[2], pts[3]);
43
44 SkPaint paint;
45 paint.setAntiAlias(true);
46
47 SkSurface* surface = new_surface(19, 130);
48 surface->getCanvas()->drawPath(path, paint);
49 surface->unref();
50}
reed@android.com3abec1d2009-03-02 05:36:20 +000051
tomhudson@google.comed02c4d2012-08-10 14:10:45 +000052// Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
53//
54static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
55 SkPath path;
56 path.quadTo(157, 366, 286, 208);
57 path.arcTo(37, 442, 315, 163, 957494590897113.0f);
rmistry@google.comd6176b02012-08-23 18:14:13 +000058
tomhudson@google.comed02c4d2012-08-10 14:10:45 +000059 SkMatrix matrix;
60 matrix.setScale(1000*1000, 1000*1000);
61
62 // Be sure that path::transform correctly updates isFinite and the bounds
63 // if the transformation overflows. The previous bug was that isFinite was
64 // set to true in this case, but the bounds were not set to empty (which
65 // they should be).
66 while (path.isFinite()) {
67 REPORTER_ASSERT(reporter, path.getBounds().isFinite());
68 REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
69 path.transform(matrix);
70 }
71 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
72
73 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
74 path.transform(matrix);
75 // we need to still be non-finite
76 REPORTER_ASSERT(reporter, !path.isFinite());
77 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
78}
79
reed@google.com0bb18bb2012-07-26 15:20:36 +000080static void test_rect_isfinite(skiatest::Reporter* reporter) {
81 const SkScalar inf = SK_ScalarInfinity;
82 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +000083
reed@google.com0bb18bb2012-07-26 15:20:36 +000084 SkRect r;
85 r.setEmpty();
86 REPORTER_ASSERT(reporter, r.isFinite());
87 r.set(0, 0, inf, -inf);
88 REPORTER_ASSERT(reporter, !r.isFinite());
89 r.set(0, 0, nan, 0);
90 REPORTER_ASSERT(reporter, !r.isFinite());
rmistry@google.comd6176b02012-08-23 18:14:13 +000091
reed@google.com0bb18bb2012-07-26 15:20:36 +000092 SkPoint pts[] = {
93 { 0, 0 },
94 { SK_Scalar1, 0 },
95 { 0, SK_Scalar1 },
96 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000097
reed@google.com0bb18bb2012-07-26 15:20:36 +000098 bool isFine = r.setBoundsCheck(pts, 3);
99 REPORTER_ASSERT(reporter, isFine);
100 REPORTER_ASSERT(reporter, !r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000101
reed@google.com0bb18bb2012-07-26 15:20:36 +0000102 pts[1].set(inf, 0);
103 isFine = r.setBoundsCheck(pts, 3);
104 REPORTER_ASSERT(reporter, !isFine);
105 REPORTER_ASSERT(reporter, r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000106
reed@google.com0bb18bb2012-07-26 15:20:36 +0000107 pts[1].set(nan, 0);
108 isFine = r.setBoundsCheck(pts, 3);
109 REPORTER_ASSERT(reporter, !isFine);
110 REPORTER_ASSERT(reporter, r.isEmpty());
111}
112
113static void test_path_isfinite(skiatest::Reporter* reporter) {
114 const SkScalar inf = SK_ScalarInfinity;
115 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000116
reed@google.com0bb18bb2012-07-26 15:20:36 +0000117 SkPath path;
118 REPORTER_ASSERT(reporter, path.isFinite());
119
120 path.reset();
121 REPORTER_ASSERT(reporter, path.isFinite());
122
123 path.reset();
124 path.moveTo(SK_Scalar1, 0);
125 REPORTER_ASSERT(reporter, path.isFinite());
126
127 path.reset();
128 path.moveTo(inf, -inf);
129 REPORTER_ASSERT(reporter, !path.isFinite());
130
131 path.reset();
132 path.moveTo(nan, 0);
133 REPORTER_ASSERT(reporter, !path.isFinite());
134}
135
136static void test_isfinite(skiatest::Reporter* reporter) {
137 test_rect_isfinite(reporter);
138 test_path_isfinite(reporter);
139}
140
reed@google.com744faba2012-05-29 19:54:52 +0000141// assert that we always
142// start with a moveTo
143// only have 1 moveTo
144// only have Lines after that
145// end with a single close
146// only have (at most) 1 close
147//
148static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
149 const SkPoint srcPts[], int count, bool expectClose) {
150 SkPath::RawIter iter(path);
151 SkPoint pts[4];
reed@google.com744faba2012-05-29 19:54:52 +0000152
153 bool firstTime = true;
154 bool foundClose = false;
155 for (;;) {
156 switch (iter.next(pts)) {
157 case SkPath::kMove_Verb:
158 REPORTER_ASSERT(reporter, firstTime);
159 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
160 srcPts++;
161 firstTime = false;
162 break;
163 case SkPath::kLine_Verb:
164 REPORTER_ASSERT(reporter, !firstTime);
165 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
166 srcPts++;
167 break;
168 case SkPath::kQuad_Verb:
169 REPORTER_ASSERT(reporter, !"unexpected quad verb");
170 break;
171 case SkPath::kCubic_Verb:
172 REPORTER_ASSERT(reporter, !"unexpected cubic verb");
173 break;
174 case SkPath::kClose_Verb:
175 REPORTER_ASSERT(reporter, !firstTime);
176 REPORTER_ASSERT(reporter, !foundClose);
177 REPORTER_ASSERT(reporter, expectClose);
178 foundClose = true;
179 break;
180 case SkPath::kDone_Verb:
181 goto DONE;
182 }
183 }
184DONE:
185 REPORTER_ASSERT(reporter, foundClose == expectClose);
186}
187
188static void test_addPoly(skiatest::Reporter* reporter) {
189 SkPoint pts[32];
190 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000191
reed@google.com744faba2012-05-29 19:54:52 +0000192 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
193 pts[i].fX = rand.nextSScalar1();
194 pts[i].fY = rand.nextSScalar1();
195 }
196
197 for (int doClose = 0; doClose <= 1; ++doClose) {
198 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
199 SkPath path;
200 path.addPoly(pts, count, SkToBool(doClose));
201 test_poly(reporter, path, pts, count, SkToBool(doClose));
202 }
203 }
204}
205
reed@google.com8b06f1a2012-05-29 12:03:46 +0000206static void test_strokerec(skiatest::Reporter* reporter) {
207 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
208 REPORTER_ASSERT(reporter, rec.isFillStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000209
reed@google.com8b06f1a2012-05-29 12:03:46 +0000210 rec.setHairlineStyle();
211 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000212
reed@google.com8b06f1a2012-05-29 12:03:46 +0000213 rec.setStrokeStyle(SK_Scalar1, false);
214 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000215
reed@google.com8b06f1a2012-05-29 12:03:46 +0000216 rec.setStrokeStyle(SK_Scalar1, true);
217 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000218
reed@google.com8b06f1a2012-05-29 12:03:46 +0000219 rec.setStrokeStyle(0, false);
220 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000221
reed@google.com8b06f1a2012-05-29 12:03:46 +0000222 rec.setStrokeStyle(0, true);
223 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
224}
225
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000226/**
227 * cheapIsDirection can take a shortcut when a path is marked convex.
228 * This function ensures that we always test cheapIsDirection when the path
229 * is flagged with unknown convexity status.
230 */
231static void check_direction(SkPath* path,
232 SkPath::Direction expectedDir,
233 skiatest::Reporter* reporter) {
234 if (SkPath::kConvex_Convexity == path->getConvexity()) {
235 REPORTER_ASSERT(reporter, path->cheapIsDirection(expectedDir));
236 path->setConvexity(SkPath::kUnknown_Convexity);
237 }
238 REPORTER_ASSERT(reporter, path->cheapIsDirection(expectedDir));
239}
240
reed@google.com3e71a882012-01-10 18:44:37 +0000241static void test_direction(skiatest::Reporter* reporter) {
242 size_t i;
243 SkPath path;
244 REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL));
245 REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCW_Direction));
246 REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCCW_Direction));
247
248 static const char* gDegen[] = {
249 "M 10 10",
250 "M 10 10 M 20 20",
251 "M 10 10 L 20 20",
252 "M 10 10 L 10 10 L 10 10",
253 "M 10 10 Q 10 10 10 10",
254 "M 10 10 C 10 10 10 10 10 10",
255 };
256 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
257 path.reset();
258 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
259 REPORTER_ASSERT(reporter, valid);
260 REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL));
261 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000262
reed@google.com3e71a882012-01-10 18:44:37 +0000263 static const char* gCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000264 "M 10 10 L 10 10 Q 20 10 20 20",
reed@google.com3e71a882012-01-10 18:44:37 +0000265 "M 10 10 C 20 10 20 20 20 20",
reed@google.comd4146662012-01-31 15:42:29 +0000266 "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 +0000267 // rect with top two corners replaced by cubics with identical middle
268 // control points
269 "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 +0000270 };
271 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
272 path.reset();
273 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
274 REPORTER_ASSERT(reporter, valid);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000275 check_direction(&path, SkPath::kCW_Direction, reporter);
reed@google.com3e71a882012-01-10 18:44:37 +0000276 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000277
reed@google.com3e71a882012-01-10 18:44:37 +0000278 static const char* gCCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000279 "M 10 10 L 10 10 Q 20 10 20 -20",
reed@google.com3e71a882012-01-10 18:44:37 +0000280 "M 10 10 C 20 10 20 -20 20 -20",
reed@google.comd4146662012-01-31 15:42:29 +0000281 "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 +0000282 // rect with top two corners replaced by cubics with identical middle
283 // control points
284 "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 +0000285 };
286 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
287 path.reset();
288 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
289 REPORTER_ASSERT(reporter, valid);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000290 check_direction(&path, SkPath::kCCW_Direction, reporter);
reed@google.com3e71a882012-01-10 18:44:37 +0000291 }
reed@google.comac8543f2012-01-30 20:51:25 +0000292
293 // Test two donuts, each wound a different direction. Only the outer contour
294 // determines the cheap direction
295 path.reset();
296 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
297 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000298 check_direction(&path, SkPath::kCW_Direction, reporter);
299
reed@google.comac8543f2012-01-30 20:51:25 +0000300 path.reset();
301 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
302 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000303 check_direction(&path, SkPath::kCCW_Direction, reporter);
304
bsalomon@google.com6843ac42012-02-17 13:49:03 +0000305#ifdef SK_SCALAR_IS_FLOAT
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000306 // triangle with one point really far from the origin.
307 path.reset();
308 // the first point is roughly 1.05e10, 1.05e10
bsalomon@google.com53aab782012-02-23 14:54:49 +0000309 path.moveTo(SkFloatToScalar(SkBits2Float(0x501c7652)), SkFloatToScalar(SkBits2Float(0x501c7652)));
310 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
311 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
312 check_direction(&path, SkPath::kCCW_Direction, reporter);
313#endif
reed@google.com3e71a882012-01-10 18:44:37 +0000314}
315
reed@google.comffdb0182011-11-14 19:29:14 +0000316static void add_rect(SkPath* path, const SkRect& r) {
317 path->moveTo(r.fLeft, r.fTop);
318 path->lineTo(r.fRight, r.fTop);
319 path->lineTo(r.fRight, r.fBottom);
320 path->lineTo(r.fLeft, r.fBottom);
321 path->close();
322}
323
324static void test_bounds(skiatest::Reporter* reporter) {
325 static const SkRect rects[] = {
reed@google.com3563c9e2011-11-14 19:34:57 +0000326 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
327 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
328 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
329 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
reed@google.comffdb0182011-11-14 19:29:14 +0000330 };
331
332 SkPath path0, path1;
333 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
334 path0.addRect(rects[i]);
335 add_rect(&path1, rects[i]);
336 }
337
338 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
339}
340
reed@google.com55b5f4b2011-09-07 12:23:41 +0000341static void stroke_cubic(const SkPoint pts[4]) {
342 SkPath path;
343 path.moveTo(pts[0]);
344 path.cubicTo(pts[1], pts[2], pts[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000345
reed@google.com55b5f4b2011-09-07 12:23:41 +0000346 SkPaint paint;
347 paint.setStyle(SkPaint::kStroke_Style);
348 paint.setStrokeWidth(SK_Scalar1 * 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000349
reed@google.com55b5f4b2011-09-07 12:23:41 +0000350 SkPath fill;
351 paint.getFillPath(path, &fill);
352}
353
354// just ensure this can run w/o any SkASSERTS firing in the debug build
355// we used to assert due to differences in how we determine a degenerate vector
356// but that was fixed with the introduction of SkPoint::CanNormalize
357static void stroke_tiny_cubic() {
358 SkPoint p0[] = {
359 { 372.0f, 92.0f },
360 { 372.0f, 92.0f },
361 { 372.0f, 92.0f },
362 { 372.0f, 92.0f },
363 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000364
reed@google.com55b5f4b2011-09-07 12:23:41 +0000365 stroke_cubic(p0);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000366
reed@google.com55b5f4b2011-09-07 12:23:41 +0000367 SkPoint p1[] = {
368 { 372.0f, 92.0f },
369 { 372.0007f, 92.000755f },
370 { 371.99927f, 92.003922f },
371 { 371.99826f, 92.003899f },
372 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000373
reed@google.com55b5f4b2011-09-07 12:23:41 +0000374 stroke_cubic(p1);
375}
376
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000377static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
378 for (int i = 0; i < 2; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +0000379 SkPath::Iter iter(path, SkToBool(i));
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000380 SkPoint mv;
381 SkPoint pts[4];
382 SkPath::Verb v;
383 int nMT = 0;
384 int nCL = 0;
tomhudson@google.com221db3c2011-07-28 21:10:29 +0000385 mv.set(0, 0);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000386 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
387 switch (v) {
388 case SkPath::kMove_Verb:
389 mv = pts[0];
390 ++nMT;
391 break;
392 case SkPath::kClose_Verb:
393 REPORTER_ASSERT(reporter, mv == pts[0]);
394 ++nCL;
395 break;
396 default:
397 break;
398 }
399 }
400 // if we force a close on the interator we should have a close
401 // for every moveTo
402 REPORTER_ASSERT(reporter, !i || nMT == nCL);
403 }
404}
405
406static void test_close(skiatest::Reporter* reporter) {
407 SkPath closePt;
408 closePt.moveTo(0, 0);
409 closePt.close();
410 check_close(reporter, closePt);
411
412 SkPath openPt;
413 openPt.moveTo(0, 0);
414 check_close(reporter, openPt);
415
416 SkPath empty;
417 check_close(reporter, empty);
418 empty.close();
419 check_close(reporter, empty);
420
421 SkPath rect;
422 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
423 check_close(reporter, rect);
424 rect.close();
425 check_close(reporter, rect);
426
427 SkPath quad;
428 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
429 check_close(reporter, quad);
430 quad.close();
431 check_close(reporter, quad);
432
433 SkPath cubic;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000434 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000435 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
436 check_close(reporter, cubic);
437 cubic.close();
438 check_close(reporter, cubic);
439
440 SkPath line;
441 line.moveTo(SK_Scalar1, SK_Scalar1);
442 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
443 check_close(reporter, line);
444 line.close();
445 check_close(reporter, line);
446
447 SkPath rect2;
448 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
449 rect2.close();
450 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
451 check_close(reporter, rect2);
452 rect2.close();
453 check_close(reporter, rect2);
454
455 SkPath oval3;
456 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
457 oval3.close();
458 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
459 check_close(reporter, oval3);
460 oval3.close();
461 check_close(reporter, oval3);
462
463 SkPath moves;
464 moves.moveTo(SK_Scalar1, SK_Scalar1);
465 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
466 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
467 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
468 check_close(reporter, moves);
reed@google.com55b5f4b2011-09-07 12:23:41 +0000469
470 stroke_tiny_cubic();
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000471}
472
reed@google.com7c424812011-05-15 04:38:34 +0000473static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
474 SkPath::Convexity expected) {
475 SkPath::Convexity c = SkPath::ComputeConvexity(path);
476 REPORTER_ASSERT(reporter, c == expected);
477}
478
479static void test_convexity2(skiatest::Reporter* reporter) {
480 SkPath pt;
481 pt.moveTo(0, 0);
482 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +0000483 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000484
reed@google.com7c424812011-05-15 04:38:34 +0000485 SkPath line;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000486 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
487 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000488 line.close();
reed@google.comb54455e2011-05-16 14:16:04 +0000489 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000490
reed@google.com7c424812011-05-15 04:38:34 +0000491 SkPath triLeft;
492 triLeft.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000493 triLeft.lineTo(SK_Scalar1, 0);
494 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000495 triLeft.close();
496 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000497
reed@google.com7c424812011-05-15 04:38:34 +0000498 SkPath triRight;
499 triRight.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000500 triRight.lineTo(-SK_Scalar1, 0);
501 triRight.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000502 triRight.close();
503 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000504
reed@google.com7c424812011-05-15 04:38:34 +0000505 SkPath square;
506 square.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000507 square.lineTo(SK_Scalar1, 0);
508 square.lineTo(SK_Scalar1, SK_Scalar1);
509 square.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000510 square.close();
511 check_convexity(reporter, square, SkPath::kConvex_Convexity);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000512
reed@google.com7c424812011-05-15 04:38:34 +0000513 SkPath redundantSquare;
514 redundantSquare.moveTo(0, 0);
515 redundantSquare.lineTo(0, 0);
516 redundantSquare.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000517 redundantSquare.lineTo(SK_Scalar1, 0);
518 redundantSquare.lineTo(SK_Scalar1, 0);
519 redundantSquare.lineTo(SK_Scalar1, 0);
520 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
521 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
522 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
523 redundantSquare.lineTo(0, SK_Scalar1);
524 redundantSquare.lineTo(0, SK_Scalar1);
525 redundantSquare.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000526 redundantSquare.close();
527 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000528
reed@google.com7c424812011-05-15 04:38:34 +0000529 SkPath bowTie;
530 bowTie.moveTo(0, 0);
531 bowTie.lineTo(0, 0);
532 bowTie.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000533 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
534 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
535 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
536 bowTie.lineTo(SK_Scalar1, 0);
537 bowTie.lineTo(SK_Scalar1, 0);
538 bowTie.lineTo(SK_Scalar1, 0);
539 bowTie.lineTo(0, SK_Scalar1);
540 bowTie.lineTo(0, SK_Scalar1);
541 bowTie.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000542 bowTie.close();
543 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000544
reed@google.com7c424812011-05-15 04:38:34 +0000545 SkPath spiral;
546 spiral.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000547 spiral.lineTo(100*SK_Scalar1, 0);
548 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
549 spiral.lineTo(0, 100*SK_Scalar1);
550 spiral.lineTo(0, 50*SK_Scalar1);
551 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
552 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000553 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +0000554 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000555
reed@google.com7c424812011-05-15 04:38:34 +0000556 SkPath dent;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000557 dent.moveTo(0, 0);
558 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
559 dent.lineTo(0, 100*SK_Scalar1);
560 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
561 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000562 dent.close();
563 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
564}
565
reed@android.com6b82d1a2009-06-03 02:35:01 +0000566static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
567 const SkRect& bounds) {
568 REPORTER_ASSERT(reporter, p.isConvex());
569 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +0000570
reed@android.com6b82d1a2009-06-03 02:35:01 +0000571 SkPath p2(p);
572 REPORTER_ASSERT(reporter, p2.isConvex());
573 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
574
575 SkPath other;
576 other.swap(p2);
577 REPORTER_ASSERT(reporter, other.isConvex());
578 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
579}
580
reed@google.com04863fa2011-05-15 04:08:24 +0000581static void setFromString(SkPath* path, const char str[]) {
582 bool first = true;
583 while (str) {
584 SkScalar x, y;
585 str = SkParse::FindScalar(str, &x);
586 if (NULL == str) {
587 break;
588 }
589 str = SkParse::FindScalar(str, &y);
590 SkASSERT(str);
591 if (first) {
592 path->moveTo(x, y);
593 first = false;
594 } else {
595 path->lineTo(x, y);
596 }
597 }
598}
599
600static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +0000601 static const SkPath::Convexity C = SkPath::kConcave_Convexity;
602 static const SkPath::Convexity V = SkPath::kConvex_Convexity;
603
604 SkPath path;
605
reed@google.comb54455e2011-05-16 14:16:04 +0000606 REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path));
reed@google.come3543972012-01-10 18:59:22 +0000607 path.addCircle(0, 0, SkIntToScalar(10));
reed@google.com04863fa2011-05-15 04:08:24 +0000608 REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path));
reed@google.come3543972012-01-10 18:59:22 +0000609 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
reed@google.com04863fa2011-05-15 04:08:24 +0000610 REPORTER_ASSERT(reporter, C == SkPath::ComputeConvexity(path));
611 path.reset();
reed@google.come3543972012-01-10 18:59:22 +0000612 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
reed@google.com04863fa2011-05-15 04:08:24 +0000613 REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path));
reed@google.com3e71a882012-01-10 18:44:37 +0000614 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCCW_Direction));
reed@google.com04863fa2011-05-15 04:08:24 +0000615 path.reset();
reed@google.come3543972012-01-10 18:59:22 +0000616 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
reed@google.com04863fa2011-05-15 04:08:24 +0000617 REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path));
reed@google.com3e71a882012-01-10 18:44:37 +0000618 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCW_Direction));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000619
reed@google.com04863fa2011-05-15 04:08:24 +0000620 static const struct {
621 const char* fPathStr;
622 SkPath::Convexity fExpectedConvexity;
623 } gRec[] = {
reed@google.comb54455e2011-05-16 14:16:04 +0000624 { "", SkPath::kConvex_Convexity },
625 { "0 0", SkPath::kConvex_Convexity },
626 { "0 0 10 10", SkPath::kConvex_Convexity },
reed@google.com85b6e392011-05-15 20:25:17 +0000627 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity },
reed@google.com04863fa2011-05-15 04:08:24 +0000628 { "0 0 10 10 10 20", SkPath::kConvex_Convexity },
629 { "0 0 10 10 10 0", SkPath::kConvex_Convexity },
630 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity },
631 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity },
632 };
633
634 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
635 SkPath path;
636 setFromString(&path, gRec[i].fPathStr);
637 SkPath::Convexity c = SkPath::ComputeConvexity(path);
638 REPORTER_ASSERT(reporter, c == gRec[i].fExpectedConvexity);
639 }
640}
641
reed@google.com7e6c4d12012-05-10 14:05:43 +0000642static void test_isLine(skiatest::Reporter* reporter) {
643 SkPath path;
644 SkPoint pts[2];
645 const SkScalar value = SkIntToScalar(5);
646
647 REPORTER_ASSERT(reporter, !path.isLine(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000648
reed@google.com7e6c4d12012-05-10 14:05:43 +0000649 // set some non-zero values
650 pts[0].set(value, value);
651 pts[1].set(value, value);
652 REPORTER_ASSERT(reporter, !path.isLine(pts));
653 // check that pts was untouched
654 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
655 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
656
657 const SkScalar moveX = SkIntToScalar(1);
658 const SkScalar moveY = SkIntToScalar(2);
659 SkASSERT(value != moveX && value != moveY);
660
661 path.moveTo(moveX, moveY);
662 REPORTER_ASSERT(reporter, !path.isLine(NULL));
663 REPORTER_ASSERT(reporter, !path.isLine(pts));
664 // check that pts was untouched
665 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
666 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
667
668 const SkScalar lineX = SkIntToScalar(2);
669 const SkScalar lineY = SkIntToScalar(2);
670 SkASSERT(value != lineX && value != lineY);
671
672 path.lineTo(lineX, lineY);
673 REPORTER_ASSERT(reporter, path.isLine(NULL));
674
675 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
676 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
677 REPORTER_ASSERT(reporter, path.isLine(pts));
678 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
679 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
680
681 path.lineTo(0, 0); // too many points/verbs
682 REPORTER_ASSERT(reporter, !path.isLine(NULL));
683 REPORTER_ASSERT(reporter, !path.isLine(pts));
684 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
685 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
686}
687
caryclark@google.comf1316942011-07-26 19:54:45 +0000688// Simple isRect test is inline TestPath, below.
689// test_isRect provides more extensive testing.
690static void test_isRect(skiatest::Reporter* reporter) {
691 // passing tests (all moveTo / lineTo...
692 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
693 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
694 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
695 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
696 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
697 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
698 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
699 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
700 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
701 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f},
702 {1, 0}, {.5f, 0}};
703 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1},
704 {0, 1}, {0, .5f}};
705 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
706 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
707 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
rmistry@google.comd6176b02012-08-23 18:14:13 +0000708
caryclark@google.comf1316942011-07-26 19:54:45 +0000709 // failing tests
710 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
711 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
712 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
713 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
714 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
715 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
716 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
717 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
rmistry@google.comd6176b02012-08-23 18:14:13 +0000718
caryclark@google.comf1316942011-07-26 19:54:45 +0000719 // failing, no close
720 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
721 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
722
723 size_t testLen[] = {
724 sizeof(r1), sizeof(r2), sizeof(r3), sizeof(r4), sizeof(r5), sizeof(r6),
725 sizeof(r7), sizeof(r8), sizeof(r9), sizeof(ra), sizeof(rb), sizeof(rc),
726 sizeof(rd), sizeof(re),
727 sizeof(f1), sizeof(f2), sizeof(f3), sizeof(f4), sizeof(f5), sizeof(f6),
728 sizeof(f7), sizeof(f8),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000729 sizeof(c1), sizeof(c2)
caryclark@google.comf1316942011-07-26 19:54:45 +0000730 };
731 SkPoint* tests[] = {
732 r1, r2, r3, r4, r5, r6, r7, r8, r9, ra, rb, rc, rd, re,
733 f1, f2, f3, f4, f5, f6, f7, f8,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000734 c1, c2
caryclark@google.comf1316942011-07-26 19:54:45 +0000735 };
736 SkPoint* lastPass = re;
737 SkPoint* lastClose = f8;
738 bool fail = false;
739 bool close = true;
740 const size_t testCount = sizeof(tests) / sizeof(tests[0]);
741 size_t index;
742 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
743 SkPath path;
744 path.moveTo(tests[testIndex][0].fX, tests[testIndex][0].fY);
745 for (index = 1; index < testLen[testIndex] / sizeof(SkPoint); ++index) {
746 path.lineTo(tests[testIndex][index].fX, tests[testIndex][index].fY);
747 }
748 if (close) {
749 path.close();
750 }
751 REPORTER_ASSERT(reporter, fail ^ path.isRect(0));
752 if (tests[testIndex] == lastPass) {
753 fail = true;
754 }
755 if (tests[testIndex] == lastClose) {
756 close = false;
757 }
758 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000759
caryclark@google.comf1316942011-07-26 19:54:45 +0000760 // fail, close then line
761 SkPath path1;
762 path1.moveTo(r1[0].fX, r1[0].fY);
763 for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
764 path1.lineTo(r1[index].fX, r1[index].fY);
765 }
766 path1.close();
767 path1.lineTo(1, 0);
768 REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000769
caryclark@google.comf1316942011-07-26 19:54:45 +0000770 // fail, move in the middle
771 path1.reset();
772 path1.moveTo(r1[0].fX, r1[0].fY);
773 for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
774 if (index == 2) {
775 path1.moveTo(1, .5f);
776 }
777 path1.lineTo(r1[index].fX, r1[index].fY);
778 }
779 path1.close();
780 REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
781
782 // fail, move on the edge
783 path1.reset();
784 for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
785 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
786 path1.lineTo(r1[index].fX, r1[index].fY);
787 }
788 path1.close();
789 REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000790
caryclark@google.comf1316942011-07-26 19:54:45 +0000791 // fail, quad
792 path1.reset();
793 path1.moveTo(r1[0].fX, r1[0].fY);
794 for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
795 if (index == 2) {
796 path1.quadTo(1, .5f, 1, .5f);
797 }
798 path1.lineTo(r1[index].fX, r1[index].fY);
799 }
800 path1.close();
801 REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000802
caryclark@google.comf1316942011-07-26 19:54:45 +0000803 // fail, cubic
804 path1.reset();
805 path1.moveTo(r1[0].fX, r1[0].fY);
806 for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) {
807 if (index == 2) {
808 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
809 }
810 path1.lineTo(r1[index].fX, r1[index].fY);
811 }
812 path1.close();
813 REPORTER_ASSERT(reporter, fail ^ path1.isRect(0));
814}
815
robertphillips@google.com2972bb52012-08-07 17:32:51 +0000816static void write_and_read_back(skiatest::Reporter* reporter,
817 const SkPath& p) {
818 SkWriter32 writer(100);
819 writer.writePath(p);
820 size_t size = writer.size();
821 SkAutoMalloc storage(size);
822 writer.flatten(storage.get());
823 SkReader32 reader(storage.get(), size);
824
825 SkPath readBack;
826 REPORTER_ASSERT(reporter, readBack != p);
827 reader.readPath(&readBack);
828 REPORTER_ASSERT(reporter, readBack == p);
829
rmistry@google.comd6176b02012-08-23 18:14:13 +0000830 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +0000831 p.getConvexityOrUnknown());
832
833 REPORTER_ASSERT(reporter, readBack.isOval(NULL) == p.isOval(NULL));
834
835 const SkRect& origBounds = p.getBounds();
836 const SkRect& readBackBounds = readBack.getBounds();
837
838 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
839}
840
reed@google.com53effc52011-09-21 19:05:12 +0000841static void test_flattening(skiatest::Reporter* reporter) {
842 SkPath p;
843
844 static const SkPoint pts[] = {
845 { 0, 0 },
846 { SkIntToScalar(10), SkIntToScalar(10) },
847 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
848 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
849 };
850 p.moveTo(pts[0]);
851 p.lineTo(pts[1]);
852 p.quadTo(pts[2], pts[3]);
853 p.cubicTo(pts[4], pts[5], pts[6]);
854
robertphillips@google.com2972bb52012-08-07 17:32:51 +0000855 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000856
857 // create a buffer that should be much larger than the path so we don't
858 // kill our stack if writer goes too far.
859 char buffer[1024];
860 uint32_t size1 = p.writeToMemory(NULL);
861 uint32_t size2 = p.writeToMemory(buffer);
862 REPORTER_ASSERT(reporter, size1 == size2);
863
864 SkPath p2;
865 uint32_t size3 = p2.readFromMemory(buffer);
866 REPORTER_ASSERT(reporter, size1 == size3);
867 REPORTER_ASSERT(reporter, p == p2);
868
869 char buffer2[1024];
870 size3 = p2.writeToMemory(buffer2);
871 REPORTER_ASSERT(reporter, size1 == size3);
872 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +0000873
874 // test persistence of the oval flag & convexity
875 {
876 SkPath oval;
877 SkRect rect = SkRect::MakeWH(10, 10);
878 oval.addOval(rect);
879
880 write_and_read_back(reporter, oval);
881 }
reed@google.com53effc52011-09-21 19:05:12 +0000882}
883
884static void test_transform(skiatest::Reporter* reporter) {
885 SkPath p, p1;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000886
reed@google.com53effc52011-09-21 19:05:12 +0000887 static const SkPoint pts[] = {
888 { 0, 0 },
889 { SkIntToScalar(10), SkIntToScalar(10) },
890 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
891 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
892 };
893 p.moveTo(pts[0]);
894 p.lineTo(pts[1]);
895 p.quadTo(pts[2], pts[3]);
896 p.cubicTo(pts[4], pts[5], pts[6]);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000897
reed@google.com53effc52011-09-21 19:05:12 +0000898 SkMatrix matrix;
899 matrix.reset();
900 p.transform(matrix, &p1);
901 REPORTER_ASSERT(reporter, p == p1);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000902
reed@google.com53effc52011-09-21 19:05:12 +0000903 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
904 p.transform(matrix, &p1);
905 SkPoint pts1[7];
906 int count = p1.getPoints(pts1, 7);
907 REPORTER_ASSERT(reporter, 7 == count);
908 for (int i = 0; i < count; ++i) {
909 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
910 REPORTER_ASSERT(reporter, newPt == pts1[i]);
911 }
912}
913
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000914static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +0000915 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +0000916 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000917
schenney@chromium.org7e963602012-06-13 17:05:43 +0000918 struct zeroPathTestData {
919 const char* testPath;
920 const size_t numResultPts;
921 const SkRect resultBound;
922 const SkPath::Verb* resultVerbs;
923 const size_t numResultVerbs;
924 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000925
schenney@chromium.org7e963602012-06-13 17:05:43 +0000926 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
927 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
928 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
929 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
930 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
931 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
932 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
933 static const SkPath::Verb resultVerbs8[] = {
934 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
935 };
936 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
937 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
938 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
939 static const SkPath::Verb resultVerbs12[] = {
940 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
941 };
942 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
943 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
944 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
945 static const SkPath::Verb resultVerbs16[] = {
946 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
947 };
948 static const struct zeroPathTestData gZeroLengthTests[] = {
949 { "M 1 1", 1, {0, 0, 0, 0}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +0000950 { "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 +0000951 { "M 1 1 z", 1, {0, 0, 0, 0}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +0000952 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
953 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
954 { "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) },
955 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
956 { "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) },
957 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
958 { "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) },
959 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
960 { "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) },
961 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
962 { "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 +0000963 SK_ARRAY_COUNT(resultVerbs14)
964 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +0000965 { "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) },
966 { "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 +0000967 SK_ARRAY_COUNT(resultVerbs16)
968 }
969 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000970
schenney@chromium.org7e963602012-06-13 17:05:43 +0000971 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
972 p.reset();
973 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
974 REPORTER_ASSERT(reporter, valid);
975 REPORTER_ASSERT(reporter, !p.isEmpty());
976 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
977 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
978 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
979 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
980 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
981 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +0000982 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000983}
984
985struct SegmentInfo {
986 SkPath fPath;
987 int fPointCount;
988};
989
reed@google.com10296cc2011-09-21 12:29:05 +0000990#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
991
schenney@chromium.org6630d8d2012-01-04 21:05:51 +0000992static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +0000993 SkPath p, p2;
994
schenney@chromium.org6630d8d2012-01-04 21:05:51 +0000995 p.moveTo(0, 0);
996 p.quadTo(100, 100, 200, 200);
997 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
998 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +0000999 p2 = p;
1000 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001001 p.cubicTo(100, 100, 200, 200, 300, 300);
1002 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
1003 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00001004 p2 = p;
1005 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
1006
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001007 p.reset();
1008 p.moveTo(0, 0);
1009 p.cubicTo(100, 100, 200, 200, 300, 300);
1010 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00001011 p2 = p;
1012 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001013
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001014 REPORTER_ASSERT(reporter, !p.isEmpty());
1015}
1016
1017static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00001018 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001019 SkPoint pts[4];
1020
1021 // Test an iterator with no path
1022 SkPath::Iter noPathIter;
1023 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00001024
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001025 // Test that setting an empty path works
1026 noPathIter.setPath(p, false);
1027 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00001028
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001029 // Test that close path makes no difference for an empty path
1030 noPathIter.setPath(p, true);
1031 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00001032
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001033 // Test an iterator with an initial empty path
1034 SkPath::Iter iter(p, false);
1035 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1036
1037 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00001038 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001039 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1040
rmistry@google.comd6176b02012-08-23 18:14:13 +00001041
schenney@chromium.org7e963602012-06-13 17:05:43 +00001042 struct iterTestData {
1043 const char* testPath;
1044 const bool forceClose;
1045 const bool consumeDegenerates;
1046 const size_t* numResultPtsPerVerb;
1047 const SkPoint* resultPts;
1048 const SkPath::Verb* resultVerbs;
1049 const size_t numResultVerbs;
1050 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001051
schenney@chromium.org7e963602012-06-13 17:05:43 +00001052 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
1053 static const SkPath::Verb resultVerbs2[] = {
1054 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
1055 };
1056 static const SkPath::Verb resultVerbs3[] = {
1057 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
1058 };
1059 static const SkPath::Verb resultVerbs4[] = {
1060 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
1061 };
1062 static const SkPath::Verb resultVerbs5[] = {
1063 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
1064 };
1065 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00001066 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
1067 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
1068 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
1069 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001070 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00001071 static const SkPoint resultPts2[] = {
1072 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
1073 };
1074 static const SkPoint resultPts3[] = {
1075 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
1076 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
1077 };
1078 static const SkPoint resultPts4[] = {
1079 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
1080 };
1081 static const SkPoint resultPts5[] = {
1082 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
1083 };
1084 static const struct iterTestData gIterTests[] = {
1085 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001086 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1087 { "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 +00001088 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1089 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1090 { "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) },
1091 { "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 +00001092 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
1093 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
1094 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1095 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1096 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
1097 { "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 +00001098 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001099
schenney@chromium.org7e963602012-06-13 17:05:43 +00001100 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
1101 p.reset();
1102 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
1103 REPORTER_ASSERT(reporter, valid);
1104 iter.setPath(p, gIterTests[i].forceClose);
1105 int j = 0, l = 0;
1106 do {
1107 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
1108 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
1109 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
1110 }
1111 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
1112 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
1113 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001114
1115 // The GM degeneratesegments.cpp test is more extensive
1116}
1117
1118static void test_raw_iter(skiatest::Reporter* reporter) {
1119 SkPath p;
1120 SkPoint pts[4];
1121
1122 // Test an iterator with no path
1123 SkPath::RawIter noPathIter;
1124 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
1125 // Test that setting an empty path works
1126 noPathIter.setPath(p);
1127 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001128
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001129 // Test an iterator with an initial empty path
1130 SkPath::RawIter iter(p);
1131 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1132
1133 // Test that a move-only path returns the move.
1134 p.moveTo(SK_Scalar1, 0);
1135 iter.setPath(p);
1136 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1137 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
1138 REPORTER_ASSERT(reporter, pts[0].fY == 0);
1139 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1140
1141 // No matter how many moves we add, we should get them all back
1142 p.moveTo(SK_Scalar1*2, SK_Scalar1);
1143 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
1144 iter.setPath(p);
1145 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1146 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
1147 REPORTER_ASSERT(reporter, pts[0].fY == 0);
1148 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1149 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
1150 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
1151 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1152 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
1153 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
1154 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1155
1156 // Initial close is never ever stored
1157 p.reset();
1158 p.close();
1159 iter.setPath(p);
1160 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1161
1162 // Move/close sequences
1163 p.reset();
1164 p.close(); // Not stored, no purpose
1165 p.moveTo(SK_Scalar1, 0);
1166 p.close();
1167 p.close(); // Not stored, no purpose
1168 p.moveTo(SK_Scalar1*2, SK_Scalar1);
1169 p.close();
1170 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
1171 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
1172 p.close();
1173 iter.setPath(p);
1174 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1175 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
1176 REPORTER_ASSERT(reporter, pts[0].fY == 0);
1177 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
1178 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
1179 REPORTER_ASSERT(reporter, pts[0].fY == 0);
1180 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1181 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
1182 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
1183 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
1184 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
1185 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
1186 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1187 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
1188 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
1189 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
1190 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
1191 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
1192 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
1193 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
1194 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
1195 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1196
1197 // Generate random paths and verify
1198 SkPoint randomPts[25];
1199 for (int i = 0; i < 5; ++i) {
1200 for (int j = 0; j < 5; ++j) {
1201 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
1202 }
1203 }
1204
1205 // Max of 10 segments, max 3 points per segment
1206 SkRandom rand(9876543);
1207 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00001208 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001209 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00001210
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001211 for (int i = 0; i < 500; ++i) {
1212 p.reset();
1213 bool lastWasClose = true;
1214 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00001215 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001216 int numPoints = 0;
1217 int numVerbs = (rand.nextU() >> 16) % 10;
1218 int numIterVerbs = 0;
1219 for (int j = 0; j < numVerbs; ++j) {
1220 do {
1221 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
1222 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001223 switch (nextVerb) {
1224 case SkPath::kMove_Verb:
1225 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
1226 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00001227 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001228 numPoints += 1;
1229 lastWasClose = false;
1230 haveMoveTo = true;
1231 break;
1232 case SkPath::kLine_Verb:
1233 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00001234 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001235 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
1236 haveMoveTo = true;
1237 }
1238 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
1239 p.lineTo(expectedPts[numPoints]);
1240 numPoints += 1;
1241 lastWasClose = false;
1242 break;
1243 case SkPath::kQuad_Verb:
1244 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00001245 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001246 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
1247 haveMoveTo = true;
1248 }
1249 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
1250 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
1251 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
1252 numPoints += 2;
1253 lastWasClose = false;
1254 break;
1255 case SkPath::kCubic_Verb:
1256 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00001257 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001258 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
1259 haveMoveTo = true;
1260 }
1261 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
1262 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
1263 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
1264 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
1265 expectedPts[numPoints + 2]);
1266 numPoints += 3;
1267 lastWasClose = false;
1268 break;
1269 case SkPath::kClose_Verb:
1270 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00001271 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001272 lastWasClose = true;
1273 break;
1274 default:;
1275 }
1276 expectedVerbs[numIterVerbs++] = nextVerb;
1277 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001278
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001279 iter.setPath(p);
1280 numVerbs = numIterVerbs;
1281 numIterVerbs = 0;
1282 int numIterPts = 0;
1283 SkPoint lastMoveTo;
1284 SkPoint lastPt;
1285 lastMoveTo.set(0, 0);
1286 lastPt.set(0, 0);
1287 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
1288 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
1289 numIterVerbs++;
1290 switch (nextVerb) {
1291 case SkPath::kMove_Verb:
1292 REPORTER_ASSERT(reporter, numIterPts < numPoints);
1293 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
1294 lastPt = lastMoveTo = pts[0];
1295 numIterPts += 1;
1296 break;
1297 case SkPath::kLine_Verb:
1298 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
1299 REPORTER_ASSERT(reporter, pts[0] == lastPt);
1300 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
1301 lastPt = pts[1];
1302 numIterPts += 1;
1303 break;
1304 case SkPath::kQuad_Verb:
1305 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
1306 REPORTER_ASSERT(reporter, pts[0] == lastPt);
1307 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
1308 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
1309 lastPt = pts[2];
1310 numIterPts += 2;
1311 break;
1312 case SkPath::kCubic_Verb:
1313 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
1314 REPORTER_ASSERT(reporter, pts[0] == lastPt);
1315 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
1316 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
1317 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
1318 lastPt = pts[3];
1319 numIterPts += 3;
1320 break;
1321 case SkPath::kClose_Verb:
1322 REPORTER_ASSERT(reporter, pts[0] == lastMoveTo);
1323 lastPt = lastMoveTo;
1324 break;
1325 default:;
1326 }
1327 }
1328 REPORTER_ASSERT(reporter, numIterPts == numPoints);
1329 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
1330 }
1331}
1332
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001333static void check_for_circle(skiatest::Reporter* reporter,
1334 const SkPath& path, bool expected) {
1335 SkRect rect;
1336 REPORTER_ASSERT(reporter, path.isOval(&rect) == expected);
1337 if (expected) {
1338 REPORTER_ASSERT(reporter, rect.height() == rect.width());
1339 }
1340}
1341
1342static void test_circle_skew(skiatest::Reporter* reporter,
1343 const SkPath& path) {
1344 SkPath tmp;
1345
1346 SkMatrix m;
1347 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
1348 path.transform(m, &tmp);
1349 check_for_circle(reporter, tmp, false);
1350}
1351
1352static void test_circle_translate(skiatest::Reporter* reporter,
1353 const SkPath& path) {
1354 SkPath tmp;
1355
1356 // translate at small offset
1357 SkMatrix m;
1358 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
1359 path.transform(m, &tmp);
1360 check_for_circle(reporter, tmp, true);
1361
1362 tmp.reset();
1363 m.reset();
1364
1365 // translate at a relatively big offset
1366 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
1367 path.transform(m, &tmp);
1368 check_for_circle(reporter, tmp, true);
1369}
1370
1371static void test_circle_rotate(skiatest::Reporter* reporter,
1372 const SkPath& path) {
1373 for (int angle = 0; angle < 360; ++angle) {
1374 SkPath tmp;
1375 SkMatrix m;
1376 m.setRotate(SkIntToScalar(angle));
1377 path.transform(m, &tmp);
1378
1379 // TODO: a rotated circle whose rotated angle is not a mutiple of 90
1380 // degrees is not an oval anymore, this can be improved. we made this
1381 // for the simplicity of our implementation.
1382 if (angle % 90 == 0) {
1383 check_for_circle(reporter, tmp, true);
1384 } else {
1385 check_for_circle(reporter, tmp, false);
1386 }
1387 }
1388}
1389
1390static void test_circle_with_direction(skiatest::Reporter* reporter,
1391 SkPath::Direction dir) {
1392 SkPath path;
1393
1394 // circle at origin
1395 path.addCircle(0, 0, SkIntToScalar(20), dir);
1396 check_for_circle(reporter, path, true);
1397 test_circle_rotate(reporter, path);
1398 test_circle_translate(reporter, path);
1399 test_circle_skew(reporter, path);
1400
1401 // circle at an offset at (10, 10)
1402 path.reset();
1403 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
1404 SkIntToScalar(20), dir);
1405 check_for_circle(reporter, path, true);
1406 test_circle_rotate(reporter, path);
1407 test_circle_translate(reporter, path);
1408 test_circle_skew(reporter, path);
1409}
1410
1411static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
1412 SkPath path;
1413 SkPath circle;
1414 SkPath rect;
1415 SkPath empty;
1416
1417 circle.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
1418 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
1419 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
1420
1421 SkMatrix translate;
1422 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
1423
1424 // For simplicity, all the path concatenation related operations
1425 // would mark it non-circle, though in theory it's still a circle.
1426
1427 // empty + circle (translate)
1428 path = empty;
1429 path.addPath(circle, translate);
1430 check_for_circle(reporter, path, false);
1431
1432 // circle + empty (translate)
1433 path = circle;
1434 path.addPath(empty, translate);
1435 check_for_circle(reporter, path, false);
1436
1437 // test reverseAddPath
1438 path = circle;
1439 path.reverseAddPath(rect);
1440 check_for_circle(reporter, path, false);
1441}
1442
1443static void test_circle(skiatest::Reporter* reporter) {
1444 test_circle_with_direction(reporter, SkPath::kCW_Direction);
1445 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
1446
1447 // multiple addCircle()
1448 SkPath path;
1449 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
1450 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
1451 check_for_circle(reporter, path, false);
1452
1453 // some extra lineTo() would make isOval() fail
1454 path.reset();
1455 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
1456 path.lineTo(0, 0);
1457 check_for_circle(reporter, path, false);
1458
1459 // not back to the original point
1460 path.reset();
1461 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
1462 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
1463 check_for_circle(reporter, path, false);
1464
1465 test_circle_with_add_paths(reporter);
1466}
1467
1468static void test_oval(skiatest::Reporter* reporter) {
1469 SkRect rect;
1470 SkMatrix m;
1471 SkPath path;
1472
1473 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
1474 path.addOval(rect);
1475
1476 REPORTER_ASSERT(reporter, path.isOval(NULL));
1477
1478 m.setRotate(SkIntToScalar(90));
1479 SkPath tmp;
1480 path.transform(m, &tmp);
1481 // an oval rotated 90 degrees is still an oval.
1482 REPORTER_ASSERT(reporter, tmp.isOval(NULL));
1483
1484 m.reset();
1485 m.setRotate(SkIntToScalar(30));
1486 tmp.reset();
1487 path.transform(m, &tmp);
1488 // an oval rotated 30 degrees is not an oval anymore.
1489 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
1490
1491 // since empty path being transformed.
1492 path.reset();
1493 tmp.reset();
1494 m.reset();
1495 path.transform(m, &tmp);
1496 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
1497
1498 // empty path is not an oval
1499 tmp.reset();
1500 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
1501
1502 // only has moveTo()s
1503 tmp.reset();
1504 tmp.moveTo(0, 0);
1505 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
1506 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
1507
1508 // mimic WebKit's calling convention,
1509 // call moveTo() first and then call addOval()
1510 path.reset();
1511 path.moveTo(0, 0);
1512 path.addOval(rect);
1513 REPORTER_ASSERT(reporter, path.isOval(NULL));
1514
1515 // copy path
1516 path.reset();
1517 tmp.reset();
1518 tmp.addOval(rect);
1519 path = tmp;
1520 REPORTER_ASSERT(reporter, path.isOval(NULL));
1521}
1522
caryclark@google.com42639cd2012-06-06 12:03:39 +00001523static void TestPath(skiatest::Reporter* reporter) {
reed@android.com60bc6d52010-02-11 11:09:39 +00001524 {
1525 SkSize size;
1526 size.fWidth = 3.4f;
1527 size.width();
1528 size = SkSize::Make(3,4);
1529 SkISize isize = SkISize::Make(3,4);
1530 }
1531
1532 SkTSize<SkScalar>::Make(3,4);
1533
reed@android.com3abec1d2009-03-02 05:36:20 +00001534 SkPath p, p2;
1535 SkRect bounds, bounds2;
reed@android.com80e39a72009-04-02 16:59:40 +00001536
reed@android.com3abec1d2009-03-02 05:36:20 +00001537 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001538 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00001539 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00001540 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00001541 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00001542 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
1543 REPORTER_ASSERT(reporter, !p.isInverseFillType());
1544 REPORTER_ASSERT(reporter, p == p2);
1545 REPORTER_ASSERT(reporter, !(p != p2));
1546
reed@android.comd252db02009-04-01 18:31:44 +00001547 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00001548
reed@android.com3abec1d2009-03-02 05:36:20 +00001549 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00001550
reed@android.com6b82d1a2009-06-03 02:35:01 +00001551 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
1552 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00001553 // we have quads or cubics
1554 REPORTER_ASSERT(reporter, p.getSegmentMasks() & kCurveSegmentMask);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001555 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00001556
reed@android.com6b82d1a2009-06-03 02:35:01 +00001557 p.reset();
reed@google.com10296cc2011-09-21 12:29:05 +00001558 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001559 REPORTER_ASSERT(reporter, p.isEmpty());
reed@google.com10296cc2011-09-21 12:29:05 +00001560
reed@android.com6b82d1a2009-06-03 02:35:01 +00001561 p.addOval(bounds);
1562 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001563 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00001564
reed@android.com6b82d1a2009-06-03 02:35:01 +00001565 p.reset();
reed@android.com3abec1d2009-03-02 05:36:20 +00001566 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00001567 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00001568 // we have only lines
1569 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001570 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00001571
1572 REPORTER_ASSERT(reporter, p != p2);
1573 REPORTER_ASSERT(reporter, !(p == p2));
1574
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00001575 // do getPoints and getVerbs return the right result
1576 REPORTER_ASSERT(reporter, p.getPoints(NULL, 0) == 4);
1577 REPORTER_ASSERT(reporter, p.getVerbs(NULL, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00001578 SkPoint pts[4];
1579 int count = p.getPoints(pts, 4);
1580 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00001581 uint8_t verbs[6];
1582 verbs[5] = 0xff;
1583 p.getVerbs(verbs, 5);
1584 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
1585 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
1586 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
1587 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
1588 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
1589 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00001590 bounds2.set(pts, 4);
1591 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00001592
reed@android.com3abec1d2009-03-02 05:36:20 +00001593 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
1594 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00001595 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00001596
reed@android.com3abec1d2009-03-02 05:36:20 +00001597 REPORTER_ASSERT(reporter, p.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00001598 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00001599 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
1600 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00001601
reed@android.com3abec1d2009-03-02 05:36:20 +00001602 // now force p to not be a rect
1603 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
1604 p.addRect(bounds);
1605 REPORTER_ASSERT(reporter, !p.isRect(NULL));
reed@android.com3abec1d2009-03-02 05:36:20 +00001606
reed@google.com7e6c4d12012-05-10 14:05:43 +00001607 test_isLine(reporter);
1608 test_isRect(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001609 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00001610 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00001611 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00001612 test_convexity2(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001613 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001614 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00001615 test_flattening(reporter);
1616 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00001617 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001618 test_iter(reporter);
1619 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00001620 test_circle(reporter);
1621 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00001622 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00001623 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00001624 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00001625 test_isfinite_after_transform(reporter);
reed@google.com8cae8352012-09-14 15:18:41 +00001626 test_tricky_cubic(reporter);
reed@android.com3abec1d2009-03-02 05:36:20 +00001627}
1628
1629#include "TestClassDef.h"
1630DEFINE_TESTCLASS("Path", PathTestClass, TestPath)