blob: 1f0422d788267567891060f071cbbe6f5c27bcbd [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@google.comcc8be772013-10-15 15:35:29 +00007
reed@google.com8cae8352012-09-14 15:18:41 +00008#include "SkCanvas.h"
reed@google.com55b5f4b2011-09-07 12:23:41 +00009#include "SkPaint.h"
reed@google.com04863fa2011-05-15 04:08:24 +000010#include "SkParse.h"
reed@google.com3e71a882012-01-10 18:44:37 +000011#include "SkParsePath.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000012#include "SkPath.h"
reed@google.com8b06f1a2012-05-29 12:03:46 +000013#include "SkPathEffect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000014#include "SkRRect.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"
caryclark66a5d8b2014-06-24 08:30:15 -070018#include "SkStream.h"
reed@google.com8cae8352012-09-14 15:18:41 +000019#include "SkSurface.h"
mtklein@google.com9c9d4a72013-08-07 19:17:53 +000020#include "SkTypes.h"
21#include "SkWriter32.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000022#include "Test.h"
reed@google.com8cae8352012-09-14 15:18:41 +000023
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +000024static void make_path_crbug364224(SkPath* path) {
25 path->reset();
26 path->moveTo(3.747501373f, 2.724499941f);
27 path->lineTo(3.747501373f, 3.75f);
28 path->cubicTo(3.747501373f, 3.88774991f, 3.635501385f, 4.0f, 3.497501373f, 4.0f);
29 path->lineTo(0.7475013733f, 4.0f);
30 path->cubicTo(0.6095013618f, 4.0f, 0.4975013733f, 3.88774991f, 0.4975013733f, 3.75f);
31 path->lineTo(0.4975013733f, 1.0f);
32 path->cubicTo(0.4975013733f, 0.8622499704f, 0.6095013618f, 0.75f, 0.7475013733f,0.75f);
33 path->lineTo(3.497501373f, 0.75f);
34 path->cubicTo(3.50275135f, 0.75f, 3.5070014f, 0.7527500391f, 3.513001442f, 0.753000021f);
35 path->lineTo(3.715001345f, 0.5512499809f);
36 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
37 path->lineTo(0.7475013733f, 0.4999999702f);
38 path->cubicTo(0.4715013802f, 0.4999999702f, 0.2475013733f, 0.7239999771f, 0.2475013733f, 1.0f);
39 path->lineTo(0.2475013733f, 3.75f);
40 path->cubicTo(0.2475013733f, 4.026000023f, 0.4715013504f, 4.25f, 0.7475013733f, 4.25f);
41 path->lineTo(3.497501373f, 4.25f);
42 path->cubicTo(3.773501396f, 4.25f, 3.997501373f, 4.026000023f, 3.997501373f, 3.75f);
43 path->lineTo(3.997501373f, 2.474750042f);
44 path->lineTo(3.747501373f, 2.724499941f);
45 path->close();
46}
47
48static void make_path_crbug364224_simplified(SkPath* path) {
49 path->moveTo(3.747501373f, 2.724499941f);
50 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
51 path->close();
52}
53
54static void test_path_crbug364224() {
55 SkPath path;
56 SkPaint paint;
57 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(84, 88));
58 SkCanvas* canvas = surface->getCanvas();
59
60 make_path_crbug364224_simplified(&path);
61 canvas->drawPath(path, paint);
62
63 make_path_crbug364224(&path);
64 canvas->drawPath(path, paint);
65}
66
piotaixrfac4e0e2014-08-26 11:59:04 -070067/**
68 * In debug mode, this path was causing an assertion to fail in
69 * SkPathStroker::preJoinTo() and, in Release, the use of an unitialized value.
70 */
71static void make_path_crbugskia2820(SkPath* path, skiatest::Reporter* reporter) {
72 SkPoint orig, p1, p2, p3;
73 orig = SkPoint::Make(1.f, 1.f);
74 p1 = SkPoint::Make(1.f - SK_ScalarNearlyZero, 1.f);
75 p2 = SkPoint::Make(1.f, 1.f + SK_ScalarNearlyZero);
76 p3 = SkPoint::Make(2.f, 2.f);
77
78 path->reset();
79 path->moveTo(orig);
80 path->cubicTo(p1, p2, p3);
81 path->close();
82}
83
84static void test_path_crbugskia2820(skiatest::Reporter* reporter) {//GrContext* context) {
85 SkPath path;
86 make_path_crbugskia2820(&path, reporter);
87
88 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
89 stroke.setStrokeStyle(2 * SK_Scalar1);
90 stroke.applyToPath(&path, path);
91}
92
reed@google.comcc8be772013-10-15 15:35:29 +000093static void make_path0(SkPath* path) {
94 // from * https://code.google.com/p/skia/issues/detail?id=1706
95
96 path->moveTo(146.939f, 1012.84f);
97 path->lineTo(181.747f, 1009.18f);
98 path->lineTo(182.165f, 1013.16f);
99 path->lineTo(147.357f, 1016.82f);
100 path->lineTo(146.939f, 1012.84f);
101 path->close();
102}
103
104static void make_path1(SkPath* path) {
105 path->addRect(SkRect::MakeXYWH(10, 10, 10, 1));
106}
107
108typedef void (*PathProc)(SkPath*);
109
110/*
111 * Regression test: we used to crash (overwrite internal storage) during
112 * construction of the region when the path was INVERSE. That is now fixed,
113 * so test these regions (which used to assert/crash).
114 *
115 * https://code.google.com/p/skia/issues/detail?id=1706
116 */
117static void test_path_to_region(skiatest::Reporter* reporter) {
118 PathProc procs[] = {
119 make_path0,
120 make_path1,
121 };
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000122
reed@google.comcc8be772013-10-15 15:35:29 +0000123 SkRegion clip;
124 clip.setRect(0, 0, 1255, 1925);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000125
reed@google.comcc8be772013-10-15 15:35:29 +0000126 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
127 SkPath path;
128 procs[i](&path);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000129
reed@google.comcc8be772013-10-15 15:35:29 +0000130 SkRegion rgn;
131 rgn.setPath(path, clip);
132 path.toggleInverseFillType();
133 rgn.setPath(path, clip);
134 }
135}
136
caryclark@google.com56f233a2012-11-19 13:06:06 +0000137#if defined(WIN32)
138 #define SUPPRESS_VISIBILITY_WARNING
139#else
140 #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
141#endif
142
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +0000143static void test_path_close_issue1474(skiatest::Reporter* reporter) {
144 // This test checks that r{Line,Quad,Conic,Cubic}To following a close()
145 // are relative to the point we close to, not relative to the point we close from.
146 SkPath path;
147 SkPoint last;
148
149 // Test rLineTo().
150 path.rLineTo(0, 100);
151 path.rLineTo(100, 0);
152 path.close(); // Returns us back to 0,0.
153 path.rLineTo(50, 50); // This should go to 50,50.
154
155 path.getLastPt(&last);
156 REPORTER_ASSERT(reporter, 50 == last.fX);
157 REPORTER_ASSERT(reporter, 50 == last.fY);
158
159 // Test rQuadTo().
160 path.rewind();
161 path.rLineTo(0, 100);
162 path.rLineTo(100, 0);
163 path.close();
164 path.rQuadTo(50, 50, 75, 75);
165
166 path.getLastPt(&last);
167 REPORTER_ASSERT(reporter, 75 == last.fX);
168 REPORTER_ASSERT(reporter, 75 == last.fY);
169
170 // Test rConicTo().
171 path.rewind();
172 path.rLineTo(0, 100);
173 path.rLineTo(100, 0);
174 path.close();
175 path.rConicTo(50, 50, 85, 85, 2);
176
177 path.getLastPt(&last);
178 REPORTER_ASSERT(reporter, 85 == last.fX);
179 REPORTER_ASSERT(reporter, 85 == last.fY);
180
181 // Test rCubicTo().
182 path.rewind();
183 path.rLineTo(0, 100);
184 path.rLineTo(100, 0);
185 path.close();
186 path.rCubicTo(50, 50, 85, 85, 95, 95);
187
188 path.getLastPt(&last);
189 REPORTER_ASSERT(reporter, 95 == last.fX);
190 REPORTER_ASSERT(reporter, 95 == last.fY);
191}
192
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000193static void test_android_specific_behavior(skiatest::Reporter* reporter) {
194#ifdef SK_BUILD_FOR_ANDROID
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000195 // Make sure we treat fGenerationID and fSourcePath correctly for each of
196 // copy, assign, rewind, reset, and swap.
197 SkPath original, source, anotherSource;
198 original.setSourcePath(&source);
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000199 original.moveTo(0, 0);
200 original.lineTo(1, 1);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000201 REPORTER_ASSERT(reporter, original.getSourcePath() == &source);
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000202
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000203 uint32_t copyID, assignID;
204
205 // Test copy constructor. Copy generation ID, copy source path.
206 SkPath copy(original);
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000207 REPORTER_ASSERT(reporter, copy.getGenerationID() == original.getGenerationID());
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000208 REPORTER_ASSERT(reporter, copy.getSourcePath() == original.getSourcePath());
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000209
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000210 // Test assigment operator. Change generation ID, copy source path.
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000211 SkPath assign;
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000212 assignID = assign.getGenerationID();
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000213 assign = original;
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000214 REPORTER_ASSERT(reporter, assign.getGenerationID() != assignID);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000215 REPORTER_ASSERT(reporter, assign.getSourcePath() == original.getSourcePath());
216
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000217 // Test rewind. Change generation ID, don't touch source path.
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000218 copyID = copy.getGenerationID();
219 copy.rewind();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000220 REPORTER_ASSERT(reporter, copy.getGenerationID() != copyID);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000221 REPORTER_ASSERT(reporter, copy.getSourcePath() == original.getSourcePath());
222
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000223 // Test reset. Change generation ID, don't touch source path.
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000224 assignID = assign.getGenerationID();
225 assign.reset();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000226 REPORTER_ASSERT(reporter, assign.getGenerationID() != assignID);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000227 REPORTER_ASSERT(reporter, assign.getSourcePath() == original.getSourcePath());
228
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000229 // Test swap. Swap the generation IDs, swap source paths.
230 copy.reset();
231 copy.moveTo(2, 2);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000232 copy.setSourcePath(&anotherSource);
233 copyID = copy.getGenerationID();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000234 assign.moveTo(3, 3);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000235 assignID = assign.getGenerationID();
236 copy.swap(assign);
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000237 REPORTER_ASSERT(reporter, copy.getGenerationID() != copyID);
238 REPORTER_ASSERT(reporter, assign.getGenerationID() != assignID);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000239 REPORTER_ASSERT(reporter, copy.getSourcePath() == original.getSourcePath());
240 REPORTER_ASSERT(reporter, assign.getSourcePath() == &anotherSource);
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000241#endif
242}
243
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000244static void test_gen_id(skiatest::Reporter* reporter) {
245 SkPath a, b;
246 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
247
248 a.moveTo(0, 0);
249 const uint32_t z = a.getGenerationID();
250 REPORTER_ASSERT(reporter, z != b.getGenerationID());
251
252 a.reset();
253 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
254
255 a.moveTo(1, 1);
256 const uint32_t y = a.getGenerationID();
257 REPORTER_ASSERT(reporter, z != y);
258
259 b.moveTo(2, 2);
260 const uint32_t x = b.getGenerationID();
261 REPORTER_ASSERT(reporter, x != y && x != z);
262
263 a.swap(b);
264 REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x);
265
266 b = a;
267 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
268
269 SkPath c(a);
270 REPORTER_ASSERT(reporter, c.getGenerationID() == x);
271
272 c.lineTo(3, 3);
273 const uint32_t w = c.getGenerationID();
274 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
275 REPORTER_ASSERT(reporter, a.getGenerationID() == x);
276 REPORTER_ASSERT(reporter, w != x);
277
278#ifdef SK_BUILD_FOR_ANDROID
279 static bool kExpectGenIDToIgnoreFill = false;
280#else
281 static bool kExpectGenIDToIgnoreFill = true;
282#endif
283
284 c.toggleInverseFillType();
285 const uint32_t v = c.getGenerationID();
286 REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill);
287
288 c.rewind();
289 REPORTER_ASSERT(reporter, v != c.getGenerationID());
290}
291
reed@google.com3eff3592013-05-08 21:08:21 +0000292// This used to assert in the debug build, as the edges did not all line-up.
293static void test_bad_cubic_crbug234190() {
294 SkPath path;
295 path.moveTo(13.8509f, 3.16858f);
296 path.cubicTo(-2.35893e+08f, -4.21044e+08f,
297 -2.38991e+08f, -4.26573e+08f,
298 -2.41016e+08f, -4.30188e+08f);
299
300 SkPaint paint;
301 paint.setAntiAlias(true);
reed@google.comd28ba802013-09-20 19:33:52 +0000302 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(84, 88));
reed@google.com3eff3592013-05-08 21:08:21 +0000303 surface->getCanvas()->drawPath(path, paint);
304}
305
reed@google.com7a90daf2013-04-10 18:44:00 +0000306static void test_bad_cubic_crbug229478() {
307 const SkPoint pts[] = {
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000308 { 4595.91064f, -11596.9873f },
309 { 4597.2168f, -11595.9414f },
310 { 4598.52344f, -11594.8955f },
311 { 4599.83008f, -11593.8496f },
reed@google.com7a90daf2013-04-10 18:44:00 +0000312 };
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000313
reed@google.com7a90daf2013-04-10 18:44:00 +0000314 SkPath path;
315 path.moveTo(pts[0]);
316 path.cubicTo(pts[1], pts[2], pts[3]);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000317
reed@google.com7a90daf2013-04-10 18:44:00 +0000318 SkPaint paint;
319 paint.setStyle(SkPaint::kStroke_Style);
320 paint.setStrokeWidth(20);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000321
reed@google.com7a90daf2013-04-10 18:44:00 +0000322 SkPath dst;
323 // Before the fix, this would infinite-recurse, and run out of stack
324 // because we would keep trying to subdivide a degenerate cubic segment.
325 paint.getFillPath(path, &dst, NULL);
326}
327
reed@google.com64d62952013-01-18 17:49:28 +0000328static void build_path_170666(SkPath& path) {
329 path.moveTo(17.9459f, 21.6344f);
330 path.lineTo(139.545f, -47.8105f);
331 path.lineTo(139.545f, -47.8105f);
332 path.lineTo(131.07f, -47.3888f);
333 path.lineTo(131.07f, -47.3888f);
334 path.lineTo(122.586f, -46.9532f);
335 path.lineTo(122.586f, -46.9532f);
336 path.lineTo(18076.6f, 31390.9f);
337 path.lineTo(18076.6f, 31390.9f);
338 path.lineTo(18085.1f, 31390.5f);
339 path.lineTo(18085.1f, 31390.5f);
340 path.lineTo(18076.6f, 31390.9f);
341 path.lineTo(18076.6f, 31390.9f);
342 path.lineTo(17955, 31460.3f);
343 path.lineTo(17955, 31460.3f);
344 path.lineTo(17963.5f, 31459.9f);
345 path.lineTo(17963.5f, 31459.9f);
346 path.lineTo(17971.9f, 31459.5f);
347 path.lineTo(17971.9f, 31459.5f);
348 path.lineTo(17.9551f, 21.6205f);
349 path.lineTo(17.9551f, 21.6205f);
350 path.lineTo(9.47091f, 22.0561f);
351 path.lineTo(9.47091f, 22.0561f);
352 path.lineTo(17.9459f, 21.6344f);
353 path.lineTo(17.9459f, 21.6344f);
354 path.close();path.moveTo(0.995934f, 22.4779f);
355 path.lineTo(0.986725f, 22.4918f);
356 path.lineTo(0.986725f, 22.4918f);
357 path.lineTo(17955, 31460.4f);
358 path.lineTo(17955, 31460.4f);
359 path.lineTo(17971.9f, 31459.5f);
360 path.lineTo(17971.9f, 31459.5f);
361 path.lineTo(18093.6f, 31390.1f);
362 path.lineTo(18093.6f, 31390.1f);
363 path.lineTo(18093.6f, 31390);
364 path.lineTo(18093.6f, 31390);
365 path.lineTo(139.555f, -47.8244f);
366 path.lineTo(139.555f, -47.8244f);
367 path.lineTo(122.595f, -46.9671f);
368 path.lineTo(122.595f, -46.9671f);
369 path.lineTo(0.995934f, 22.4779f);
370 path.lineTo(0.995934f, 22.4779f);
371 path.close();
372 path.moveTo(5.43941f, 25.5223f);
373 path.lineTo(798267, -28871.1f);
374 path.lineTo(798267, -28871.1f);
375 path.lineTo(3.12512e+06f, -113102);
376 path.lineTo(3.12512e+06f, -113102);
377 path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813);
378 path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f);
379 path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f);
380 path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f);
381 path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f);
382 path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f);
383 path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f);
384 path.lineTo(2.78271e+08f, -1.00733e+07f);
385 path.lineTo(2.78271e+08f, -1.00733e+07f);
386 path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f);
387 path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f);
388 path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f);
389 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
390 path.lineTo(2.77473e+08f, -1.00444e+07f);
391 path.lineTo(2.77473e+08f, -1.00444e+07f);
392 path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f);
393 path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f);
394 path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f);
395 path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f);
396 path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f);
397 path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814);
398 path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103);
399 path.lineTo(798284, -28872);
400 path.lineTo(798284, -28872);
401 path.lineTo(22.4044f, 24.6677f);
402 path.lineTo(22.4044f, 24.6677f);
403 path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f);
404 path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f);
405 path.close();
406}
407
408static void build_path_simple_170666(SkPath& path) {
409 path.moveTo(126.677f, 24.1591f);
410 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
411}
412
413// This used to assert in the SK_DEBUG build, as the clip step would fail with
414// too-few interations in our cubic-line intersection code. That code now runs
415// 24 interations (instead of 16).
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000416static void test_crbug_170666() {
reed@google.com64d62952013-01-18 17:49:28 +0000417 SkPath path;
418 SkPaint paint;
419 paint.setAntiAlias(true);
420
reed@google.comd28ba802013-09-20 19:33:52 +0000421 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(1000, 1000));
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000422
reed@google.com64d62952013-01-18 17:49:28 +0000423 build_path_simple_170666(path);
424 surface->getCanvas()->drawPath(path, paint);
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000425
reed@google.com64d62952013-01-18 17:49:28 +0000426 build_path_170666(path);
427 surface->getCanvas()->drawPath(path, paint);
428}
429
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000430static void test_addrect(skiatest::Reporter* reporter) {
431 SkPath path;
432 path.lineTo(0, 0);
433 path.addRect(SkRect::MakeWH(50, 100));
434 REPORTER_ASSERT(reporter, path.isRect(NULL));
435
436 path.reset();
437 path.lineTo(FLT_EPSILON, FLT_EPSILON);
438 path.addRect(SkRect::MakeWH(50, 100));
439 REPORTER_ASSERT(reporter, !path.isRect(NULL));
440
441 path.reset();
442 path.quadTo(0, 0, 0, 0);
443 path.addRect(SkRect::MakeWH(50, 100));
444 REPORTER_ASSERT(reporter, !path.isRect(NULL));
445
446 path.reset();
447 path.conicTo(0, 0, 0, 0, 0.5f);
448 path.addRect(SkRect::MakeWH(50, 100));
449 REPORTER_ASSERT(reporter, !path.isRect(NULL));
450
451 path.reset();
452 path.cubicTo(0, 0, 0, 0, 0, 0);
453 path.addRect(SkRect::MakeWH(50, 100));
454 REPORTER_ASSERT(reporter, !path.isRect(NULL));
455}
456
reed@google.coma8790de2012-10-24 21:04:04 +0000457// Make sure we stay non-finite once we get there (unless we reset or rewind).
458static void test_addrect_isfinite(skiatest::Reporter* reporter) {
459 SkPath path;
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000460
reed@google.coma8790de2012-10-24 21:04:04 +0000461 path.addRect(SkRect::MakeWH(50, 100));
462 REPORTER_ASSERT(reporter, path.isFinite());
463
464 path.moveTo(0, 0);
465 path.lineTo(SK_ScalarInfinity, 42);
466 REPORTER_ASSERT(reporter, !path.isFinite());
467
468 path.addRect(SkRect::MakeWH(50, 100));
469 REPORTER_ASSERT(reporter, !path.isFinite());
470
471 path.reset();
472 REPORTER_ASSERT(reporter, path.isFinite());
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000473
reed@google.coma8790de2012-10-24 21:04:04 +0000474 path.addRect(SkRect::MakeWH(50, 100));
475 REPORTER_ASSERT(reporter, path.isFinite());
476}
477
reed@google.com848148e2013-01-15 15:51:59 +0000478static void build_big_path(SkPath* path, bool reducedCase) {
479 if (reducedCase) {
480 path->moveTo(577330, 1971.72f);
481 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
482 } else {
483 path->moveTo(60.1631f, 7.70567f);
484 path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
485 path->lineTo(577379, 1977.77f);
486 path->quadTo(577364, 1979.57f, 577325, 1980.26f);
487 path->quadTo(577286, 1980.95f, 577245, 1980.13f);
488 path->quadTo(577205, 1979.3f, 577187, 1977.45f);
489 path->quadTo(577168, 1975.6f, 577183, 1973.8f);
490 path->quadTo(577198, 1972, 577238, 1971.31f);
491 path->quadTo(577277, 1970.62f, 577317, 1971.45f);
492 path->quadTo(577330, 1971.72f, 577341, 1972.11f);
493 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
494 path->moveTo(306.718f, -32.912f);
495 path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
496 }
497}
498
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000499static void test_clipped_cubic() {
reed@google.comd28ba802013-09-20 19:33:52 +0000500 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(640, 480));
reed@google.com848148e2013-01-15 15:51:59 +0000501
502 // This path used to assert, because our cubic-chopping code incorrectly
503 // moved control points after the chop. This test should be run in SK_DEBUG
504 // mode to ensure that we no long assert.
505 SkPath path;
506 for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
507 build_big_path(&path, SkToBool(doReducedCase));
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +0000508
reed@google.com848148e2013-01-15 15:51:59 +0000509 SkPaint paint;
510 for (int doAA = 0; doAA <= 1; ++doAA) {
511 paint.setAntiAlias(SkToBool(doAA));
512 surface->getCanvas()->drawPath(path, paint);
513 }
514 }
515}
516
reed@google.com8cae8352012-09-14 15:18:41 +0000517// Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
518// which triggered an assert, from a tricky cubic. This test replicates that
519// example, so we can ensure that we handle it (in SkEdge.cpp), and don't
520// assert in the SK_DEBUG build.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000521static void test_tricky_cubic() {
reed@google.com8cae8352012-09-14 15:18:41 +0000522 const SkPoint pts[] = {
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +0000523 { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
524 { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
525 { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
526 { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
reed@google.com8cae8352012-09-14 15:18:41 +0000527 };
528
529 SkPath path;
530 path.moveTo(pts[0]);
531 path.cubicTo(pts[1], pts[2], pts[3]);
532
533 SkPaint paint;
534 paint.setAntiAlias(true);
535
reed@google.comd28ba802013-09-20 19:33:52 +0000536 SkSurface* surface = SkSurface::NewRasterPMColor(19, 130);
reed@google.com8cae8352012-09-14 15:18:41 +0000537 surface->getCanvas()->drawPath(path, paint);
538 surface->unref();
539}
reed@android.com3abec1d2009-03-02 05:36:20 +0000540
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000541// Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
542//
543static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
544 SkPath path;
545 path.quadTo(157, 366, 286, 208);
546 path.arcTo(37, 442, 315, 163, 957494590897113.0f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000547
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000548 SkMatrix matrix;
549 matrix.setScale(1000*1000, 1000*1000);
550
551 // Be sure that path::transform correctly updates isFinite and the bounds
552 // if the transformation overflows. The previous bug was that isFinite was
553 // set to true in this case, but the bounds were not set to empty (which
554 // they should be).
555 while (path.isFinite()) {
556 REPORTER_ASSERT(reporter, path.getBounds().isFinite());
557 REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
558 path.transform(matrix);
559 }
560 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
561
562 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
563 path.transform(matrix);
564 // we need to still be non-finite
565 REPORTER_ASSERT(reporter, !path.isFinite());
566 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
567}
568
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000569static void add_corner_arc(SkPath* path, const SkRect& rect,
570 SkScalar xIn, SkScalar yIn,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000571 int startAngle)
572{
573
574 SkScalar rx = SkMinScalar(rect.width(), xIn);
575 SkScalar ry = SkMinScalar(rect.height(), yIn);
576
577 SkRect arcRect;
578 arcRect.set(-rx, -ry, rx, ry);
579 switch (startAngle) {
580 case 0:
581 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
582 break;
583 case 90:
584 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
585 break;
586 case 180:
587 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
588 break;
589 case 270:
590 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
591 break;
592 default:
593 break;
594 }
595
596 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
597}
598
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000599static void make_arb_round_rect(SkPath* path, const SkRect& r,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000600 SkScalar xCorner, SkScalar yCorner) {
601 // we are lazy here and use the same x & y for each corner
602 add_corner_arc(path, r, xCorner, yCorner, 270);
603 add_corner_arc(path, r, xCorner, yCorner, 0);
604 add_corner_arc(path, r, xCorner, yCorner, 90);
605 add_corner_arc(path, r, xCorner, yCorner, 180);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000606 path->close();
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000607}
608
609// Chrome creates its own round rects with each corner possibly being different.
610// Performance will suffer if they are not convex.
611// Note: PathBench::ArbRoundRectBench performs almost exactly
612// the same test (but with drawing)
613static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000614 SkRandom rand;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000615 SkRect r;
616
617 for (int i = 0; i < 5000; ++i) {
618
robertphillips@google.com158618e2012-10-23 16:56:56 +0000619 SkScalar size = rand.nextUScalar1() * 30;
620 if (size < SK_Scalar1) {
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000621 continue;
622 }
623 r.fLeft = rand.nextUScalar1() * 300;
624 r.fTop = rand.nextUScalar1() * 300;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000625 r.fRight = r.fLeft + 2 * size;
626 r.fBottom = r.fTop + 2 * size;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000627
628 SkPath temp;
629
630 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
631
632 REPORTER_ASSERT(reporter, temp.isConvex());
633 }
634}
635
robertphillips@google.com158618e2012-10-23 16:56:56 +0000636// Chrome will sometimes create a 0 radius round rect. The degenerate
637// quads prevent the path from being converted to a rect
638// Note: PathBench::ArbRoundRectBench performs almost exactly
639// the same test (but with drawing)
640static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000641 SkRandom rand;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000642 SkRect r;
643
644 for (int i = 0; i < 5000; ++i) {
645
646 SkScalar size = rand.nextUScalar1() * 30;
647 if (size < SK_Scalar1) {
648 continue;
649 }
650 r.fLeft = rand.nextUScalar1() * 300;
651 r.fTop = rand.nextUScalar1() * 300;
652 r.fRight = r.fLeft + 2 * size;
653 r.fBottom = r.fTop + 2 * size;
654
655 SkPath temp;
656
657 make_arb_round_rect(&temp, r, 0, 0);
658
robertphillips@google.com158618e2012-10-23 16:56:56 +0000659 SkRect result;
660 REPORTER_ASSERT(reporter, temp.isRect(&result));
661 REPORTER_ASSERT(reporter, r == result);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000662 }
663}
664
reed@google.com0bb18bb2012-07-26 15:20:36 +0000665static void test_rect_isfinite(skiatest::Reporter* reporter) {
666 const SkScalar inf = SK_ScalarInfinity;
caryclark@google.com7abfa492013-04-12 11:59:41 +0000667 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000668 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000669
reed@google.com0bb18bb2012-07-26 15:20:36 +0000670 SkRect r;
671 r.setEmpty();
672 REPORTER_ASSERT(reporter, r.isFinite());
caryclark@google.com7abfa492013-04-12 11:59:41 +0000673 r.set(0, 0, inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000674 REPORTER_ASSERT(reporter, !r.isFinite());
675 r.set(0, 0, nan, 0);
676 REPORTER_ASSERT(reporter, !r.isFinite());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000677
reed@google.com0bb18bb2012-07-26 15:20:36 +0000678 SkPoint pts[] = {
679 { 0, 0 },
680 { SK_Scalar1, 0 },
681 { 0, SK_Scalar1 },
682 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000683
reed@google.com0bb18bb2012-07-26 15:20:36 +0000684 bool isFine = r.setBoundsCheck(pts, 3);
685 REPORTER_ASSERT(reporter, isFine);
686 REPORTER_ASSERT(reporter, !r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000687
reed@google.com0bb18bb2012-07-26 15:20:36 +0000688 pts[1].set(inf, 0);
689 isFine = r.setBoundsCheck(pts, 3);
690 REPORTER_ASSERT(reporter, !isFine);
691 REPORTER_ASSERT(reporter, r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000692
reed@google.com0bb18bb2012-07-26 15:20:36 +0000693 pts[1].set(nan, 0);
694 isFine = r.setBoundsCheck(pts, 3);
695 REPORTER_ASSERT(reporter, !isFine);
696 REPORTER_ASSERT(reporter, r.isEmpty());
697}
698
699static void test_path_isfinite(skiatest::Reporter* reporter) {
700 const SkScalar inf = SK_ScalarInfinity;
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000701 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000702 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000703
reed@google.com0bb18bb2012-07-26 15:20:36 +0000704 SkPath path;
705 REPORTER_ASSERT(reporter, path.isFinite());
706
707 path.reset();
708 REPORTER_ASSERT(reporter, path.isFinite());
709
710 path.reset();
711 path.moveTo(SK_Scalar1, 0);
712 REPORTER_ASSERT(reporter, path.isFinite());
713
714 path.reset();
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000715 path.moveTo(inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000716 REPORTER_ASSERT(reporter, !path.isFinite());
717
718 path.reset();
719 path.moveTo(nan, 0);
720 REPORTER_ASSERT(reporter, !path.isFinite());
721}
722
723static void test_isfinite(skiatest::Reporter* reporter) {
724 test_rect_isfinite(reporter);
725 test_path_isfinite(reporter);
726}
727
reed@google.com744faba2012-05-29 19:54:52 +0000728// assert that we always
729// start with a moveTo
730// only have 1 moveTo
731// only have Lines after that
732// end with a single close
733// only have (at most) 1 close
734//
735static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000736 const SkPoint srcPts[], bool expectClose) {
reed@google.com744faba2012-05-29 19:54:52 +0000737 SkPath::RawIter iter(path);
738 SkPoint pts[4];
reed@google.com744faba2012-05-29 19:54:52 +0000739
740 bool firstTime = true;
741 bool foundClose = false;
742 for (;;) {
743 switch (iter.next(pts)) {
744 case SkPath::kMove_Verb:
745 REPORTER_ASSERT(reporter, firstTime);
746 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
747 srcPts++;
748 firstTime = false;
749 break;
750 case SkPath::kLine_Verb:
751 REPORTER_ASSERT(reporter, !firstTime);
752 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
753 srcPts++;
754 break;
755 case SkPath::kQuad_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000756 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb");
reed@google.com744faba2012-05-29 19:54:52 +0000757 break;
reed@google.com277c3f82013-05-31 15:17:50 +0000758 case SkPath::kConic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000759 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb");
reed@google.com277c3f82013-05-31 15:17:50 +0000760 break;
reed@google.com744faba2012-05-29 19:54:52 +0000761 case SkPath::kCubic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000762 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb");
reed@google.com744faba2012-05-29 19:54:52 +0000763 break;
764 case SkPath::kClose_Verb:
765 REPORTER_ASSERT(reporter, !firstTime);
766 REPORTER_ASSERT(reporter, !foundClose);
767 REPORTER_ASSERT(reporter, expectClose);
768 foundClose = true;
769 break;
770 case SkPath::kDone_Verb:
771 goto DONE;
772 }
773 }
774DONE:
775 REPORTER_ASSERT(reporter, foundClose == expectClose);
776}
777
778static void test_addPoly(skiatest::Reporter* reporter) {
779 SkPoint pts[32];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000780 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000781
reed@google.com744faba2012-05-29 19:54:52 +0000782 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
783 pts[i].fX = rand.nextSScalar1();
784 pts[i].fY = rand.nextSScalar1();
785 }
786
787 for (int doClose = 0; doClose <= 1; ++doClose) {
788 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
789 SkPath path;
790 path.addPoly(pts, count, SkToBool(doClose));
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000791 test_poly(reporter, path, pts, SkToBool(doClose));
reed@google.com744faba2012-05-29 19:54:52 +0000792 }
793 }
794}
795
reed@google.com8b06f1a2012-05-29 12:03:46 +0000796static void test_strokerec(skiatest::Reporter* reporter) {
797 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
798 REPORTER_ASSERT(reporter, rec.isFillStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000799
reed@google.com8b06f1a2012-05-29 12:03:46 +0000800 rec.setHairlineStyle();
801 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000802
reed@google.com8b06f1a2012-05-29 12:03:46 +0000803 rec.setStrokeStyle(SK_Scalar1, false);
804 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000805
reed@google.com8b06f1a2012-05-29 12:03:46 +0000806 rec.setStrokeStyle(SK_Scalar1, true);
807 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000808
reed@google.com8b06f1a2012-05-29 12:03:46 +0000809 rec.setStrokeStyle(0, false);
810 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000811
reed@google.com8b06f1a2012-05-29 12:03:46 +0000812 rec.setStrokeStyle(0, true);
813 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
814}
815
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000816// Set this for paths that don't have a consistent direction such as a bowtie.
817// (cheapComputeDirection is not expected to catch these.)
818static const SkPath::Direction kDontCheckDir = static_cast<SkPath::Direction>(-1);
819
820static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
821 SkPath::Direction expected) {
822 if (expected == kDontCheckDir) {
823 return;
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000824 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000825 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
826
827 SkPath::Direction dir;
828 if (copy.cheapComputeDirection(&dir)) {
829 REPORTER_ASSERT(reporter, dir == expected);
830 } else {
831 REPORTER_ASSERT(reporter, SkPath::kUnknown_Direction == expected);
832 }
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000833}
834
reed@google.com3e71a882012-01-10 18:44:37 +0000835static void test_direction(skiatest::Reporter* reporter) {
836 size_t i;
837 SkPath path;
838 REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL));
839 REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCW_Direction));
840 REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCCW_Direction));
reed@google.coma8a3b3d2012-11-26 18:16:27 +0000841 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kUnknown_Direction));
reed@google.com3e71a882012-01-10 18:44:37 +0000842
843 static const char* gDegen[] = {
844 "M 10 10",
845 "M 10 10 M 20 20",
846 "M 10 10 L 20 20",
847 "M 10 10 L 10 10 L 10 10",
848 "M 10 10 Q 10 10 10 10",
849 "M 10 10 C 10 10 10 10 10 10",
850 };
851 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
852 path.reset();
853 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
854 REPORTER_ASSERT(reporter, valid);
855 REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL));
856 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000857
reed@google.com3e71a882012-01-10 18:44:37 +0000858 static const char* gCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000859 "M 10 10 L 10 10 Q 20 10 20 20",
reed@google.com3e71a882012-01-10 18:44:37 +0000860 "M 10 10 C 20 10 20 20 20 20",
reed@google.comd4146662012-01-31 15:42:29 +0000861 "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 +0000862 // rect with top two corners replaced by cubics with identical middle
863 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000864 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
865 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000866 };
867 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
868 path.reset();
869 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
870 REPORTER_ASSERT(reporter, valid);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000871 check_direction(reporter, path, SkPath::kCW_Direction);
reed@google.com3e71a882012-01-10 18:44:37 +0000872 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000873
reed@google.com3e71a882012-01-10 18:44:37 +0000874 static const char* gCCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000875 "M 10 10 L 10 10 Q 20 10 20 -20",
reed@google.com3e71a882012-01-10 18:44:37 +0000876 "M 10 10 C 20 10 20 -20 20 -20",
reed@google.comd4146662012-01-31 15:42:29 +0000877 "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 +0000878 // rect with top two corners replaced by cubics with identical middle
879 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000880 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
881 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000882 };
883 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
884 path.reset();
885 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
886 REPORTER_ASSERT(reporter, valid);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000887 check_direction(reporter, path, SkPath::kCCW_Direction);
reed@google.com3e71a882012-01-10 18:44:37 +0000888 }
reed@google.comac8543f2012-01-30 20:51:25 +0000889
890 // Test two donuts, each wound a different direction. Only the outer contour
891 // determines the cheap direction
892 path.reset();
893 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
894 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000895 check_direction(reporter, path, SkPath::kCW_Direction);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000896
reed@google.comac8543f2012-01-30 20:51:25 +0000897 path.reset();
898 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
899 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000900 check_direction(reporter, path, SkPath::kCCW_Direction);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000901
902 // triangle with one point really far from the origin.
903 path.reset();
904 // the first point is roughly 1.05e10, 1.05e10
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000905 path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
bsalomon@google.com53aab782012-02-23 14:54:49 +0000906 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
907 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000908 check_direction(reporter, path, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000909
910 path.reset();
911 path.conicTo(20, 0, 20, 20, 0.5f);
912 path.close();
913 check_direction(reporter, path, SkPath::kCW_Direction);
914
915 path.reset();
916 path.lineTo(1, 1e7f);
917 path.lineTo(1e7f, 2e7f);
918 path.close();
919 REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity());
920 check_direction(reporter, path, SkPath::kCCW_Direction);
reed@google.com3e71a882012-01-10 18:44:37 +0000921}
922
reed@google.comffdb0182011-11-14 19:29:14 +0000923static void add_rect(SkPath* path, const SkRect& r) {
924 path->moveTo(r.fLeft, r.fTop);
925 path->lineTo(r.fRight, r.fTop);
926 path->lineTo(r.fRight, r.fBottom);
927 path->lineTo(r.fLeft, r.fBottom);
928 path->close();
929}
930
931static void test_bounds(skiatest::Reporter* reporter) {
932 static const SkRect rects[] = {
reed@google.com3563c9e2011-11-14 19:34:57 +0000933 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
934 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
935 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
936 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
reed@google.comffdb0182011-11-14 19:29:14 +0000937 };
938
939 SkPath path0, path1;
940 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
941 path0.addRect(rects[i]);
942 add_rect(&path1, rects[i]);
943 }
944
945 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
946}
947
reed@google.com55b5f4b2011-09-07 12:23:41 +0000948static void stroke_cubic(const SkPoint pts[4]) {
949 SkPath path;
950 path.moveTo(pts[0]);
951 path.cubicTo(pts[1], pts[2], pts[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000952
reed@google.com55b5f4b2011-09-07 12:23:41 +0000953 SkPaint paint;
954 paint.setStyle(SkPaint::kStroke_Style);
955 paint.setStrokeWidth(SK_Scalar1 * 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000956
reed@google.com55b5f4b2011-09-07 12:23:41 +0000957 SkPath fill;
958 paint.getFillPath(path, &fill);
959}
960
961// just ensure this can run w/o any SkASSERTS firing in the debug build
962// we used to assert due to differences in how we determine a degenerate vector
963// but that was fixed with the introduction of SkPoint::CanNormalize
964static void stroke_tiny_cubic() {
965 SkPoint p0[] = {
966 { 372.0f, 92.0f },
967 { 372.0f, 92.0f },
968 { 372.0f, 92.0f },
969 { 372.0f, 92.0f },
970 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000971
reed@google.com55b5f4b2011-09-07 12:23:41 +0000972 stroke_cubic(p0);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000973
reed@google.com55b5f4b2011-09-07 12:23:41 +0000974 SkPoint p1[] = {
975 { 372.0f, 92.0f },
976 { 372.0007f, 92.000755f },
977 { 371.99927f, 92.003922f },
978 { 371.99826f, 92.003899f },
979 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000980
reed@google.com55b5f4b2011-09-07 12:23:41 +0000981 stroke_cubic(p1);
982}
983
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000984static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
985 for (int i = 0; i < 2; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +0000986 SkPath::Iter iter(path, SkToBool(i));
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000987 SkPoint mv;
988 SkPoint pts[4];
989 SkPath::Verb v;
990 int nMT = 0;
991 int nCL = 0;
tomhudson@google.com221db3c2011-07-28 21:10:29 +0000992 mv.set(0, 0);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000993 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
994 switch (v) {
995 case SkPath::kMove_Verb:
996 mv = pts[0];
997 ++nMT;
998 break;
999 case SkPath::kClose_Verb:
1000 REPORTER_ASSERT(reporter, mv == pts[0]);
1001 ++nCL;
1002 break;
1003 default:
1004 break;
1005 }
1006 }
1007 // if we force a close on the interator we should have a close
1008 // for every moveTo
1009 REPORTER_ASSERT(reporter, !i || nMT == nCL);
1010 }
1011}
1012
1013static void test_close(skiatest::Reporter* reporter) {
1014 SkPath closePt;
1015 closePt.moveTo(0, 0);
1016 closePt.close();
1017 check_close(reporter, closePt);
1018
1019 SkPath openPt;
1020 openPt.moveTo(0, 0);
1021 check_close(reporter, openPt);
1022
1023 SkPath empty;
1024 check_close(reporter, empty);
1025 empty.close();
1026 check_close(reporter, empty);
1027
1028 SkPath rect;
1029 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1030 check_close(reporter, rect);
1031 rect.close();
1032 check_close(reporter, rect);
1033
1034 SkPath quad;
1035 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1036 check_close(reporter, quad);
1037 quad.close();
1038 check_close(reporter, quad);
1039
1040 SkPath cubic;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001041 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001042 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
1043 check_close(reporter, cubic);
1044 cubic.close();
1045 check_close(reporter, cubic);
1046
1047 SkPath line;
1048 line.moveTo(SK_Scalar1, SK_Scalar1);
1049 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
1050 check_close(reporter, line);
1051 line.close();
1052 check_close(reporter, line);
1053
1054 SkPath rect2;
1055 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1056 rect2.close();
1057 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1058 check_close(reporter, rect2);
1059 rect2.close();
1060 check_close(reporter, rect2);
1061
1062 SkPath oval3;
1063 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
1064 oval3.close();
1065 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
1066 check_close(reporter, oval3);
1067 oval3.close();
1068 check_close(reporter, oval3);
1069
1070 SkPath moves;
1071 moves.moveTo(SK_Scalar1, SK_Scalar1);
1072 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
1073 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
1074 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
1075 check_close(reporter, moves);
reed@google.com55b5f4b2011-09-07 12:23:41 +00001076
1077 stroke_tiny_cubic();
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001078}
1079
reed@google.com7c424812011-05-15 04:38:34 +00001080static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
1081 SkPath::Convexity expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001082 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1083 SkPath::Convexity c = copy.getConvexity();
reed@google.com7c424812011-05-15 04:38:34 +00001084 REPORTER_ASSERT(reporter, c == expected);
1085}
1086
1087static void test_convexity2(skiatest::Reporter* reporter) {
1088 SkPath pt;
1089 pt.moveTo(0, 0);
1090 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +00001091 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001092 check_direction(reporter, pt, SkPath::kUnknown_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001093
reed@google.com7c424812011-05-15 04:38:34 +00001094 SkPath line;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001095 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
1096 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001097 line.close();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001098 check_convexity(reporter, line, SkPath::kConvex_Convexity);
1099 check_direction(reporter, line, SkPath::kUnknown_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001100
reed@google.com7c424812011-05-15 04:38:34 +00001101 SkPath triLeft;
1102 triLeft.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001103 triLeft.lineTo(SK_Scalar1, 0);
1104 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001105 triLeft.close();
1106 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001107 check_direction(reporter, triLeft, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001108
reed@google.com7c424812011-05-15 04:38:34 +00001109 SkPath triRight;
1110 triRight.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001111 triRight.lineTo(-SK_Scalar1, 0);
1112 triRight.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001113 triRight.close();
1114 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001115 check_direction(reporter, triRight, SkPath::kCCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001116
reed@google.com7c424812011-05-15 04:38:34 +00001117 SkPath square;
1118 square.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001119 square.lineTo(SK_Scalar1, 0);
1120 square.lineTo(SK_Scalar1, SK_Scalar1);
1121 square.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001122 square.close();
1123 check_convexity(reporter, square, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001124 check_direction(reporter, square, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001125
reed@google.com7c424812011-05-15 04:38:34 +00001126 SkPath redundantSquare;
1127 redundantSquare.moveTo(0, 0);
1128 redundantSquare.lineTo(0, 0);
1129 redundantSquare.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001130 redundantSquare.lineTo(SK_Scalar1, 0);
1131 redundantSquare.lineTo(SK_Scalar1, 0);
1132 redundantSquare.lineTo(SK_Scalar1, 0);
1133 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1134 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1135 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1136 redundantSquare.lineTo(0, SK_Scalar1);
1137 redundantSquare.lineTo(0, SK_Scalar1);
1138 redundantSquare.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001139 redundantSquare.close();
1140 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001141 check_direction(reporter, redundantSquare, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001142
reed@google.com7c424812011-05-15 04:38:34 +00001143 SkPath bowTie;
1144 bowTie.moveTo(0, 0);
1145 bowTie.lineTo(0, 0);
1146 bowTie.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001147 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1148 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1149 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1150 bowTie.lineTo(SK_Scalar1, 0);
1151 bowTie.lineTo(SK_Scalar1, 0);
1152 bowTie.lineTo(SK_Scalar1, 0);
1153 bowTie.lineTo(0, SK_Scalar1);
1154 bowTie.lineTo(0, SK_Scalar1);
1155 bowTie.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001156 bowTie.close();
1157 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001158 check_direction(reporter, bowTie, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001159
reed@google.com7c424812011-05-15 04:38:34 +00001160 SkPath spiral;
1161 spiral.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001162 spiral.lineTo(100*SK_Scalar1, 0);
1163 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1164 spiral.lineTo(0, 100*SK_Scalar1);
1165 spiral.lineTo(0, 50*SK_Scalar1);
1166 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1167 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001168 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +00001169 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001170 check_direction(reporter, spiral, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001171
reed@google.com7c424812011-05-15 04:38:34 +00001172 SkPath dent;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001173 dent.moveTo(0, 0);
1174 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1175 dent.lineTo(0, 100*SK_Scalar1);
1176 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1177 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001178 dent.close();
1179 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001180 check_direction(reporter, dent, SkPath::kCW_Direction);
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001181
1182 // http://skbug.com/2235
1183 SkPath strokedSin;
1184 for (int i = 0; i < 2000; i++) {
1185 SkScalar x = SkIntToScalar(i) / 2;
1186 SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3;
1187 if (0 == i) {
1188 strokedSin.moveTo(x, y);
1189 } else {
1190 strokedSin.lineTo(x, y);
1191 }
1192 }
1193 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1194 stroke.setStrokeStyle(2 * SK_Scalar1);
1195 stroke.applyToPath(&strokedSin, strokedSin);
1196 check_convexity(reporter, strokedSin, SkPath::kConcave_Convexity);
1197 check_direction(reporter, strokedSin, kDontCheckDir);
robertphillipsc506e302014-09-16 09:43:31 -07001198
1199 // http://crbug.com/412640
1200 SkPath degenerateConcave;
1201 degenerateConcave.moveTo(148.67912f, 191.875f);
1202 degenerateConcave.lineTo(470.37695f, 7.5f);
1203 degenerateConcave.lineTo(148.67912f, 191.875f);
1204 degenerateConcave.lineTo(41.446522f, 376.25f);
1205 degenerateConcave.lineTo(-55.971577f, 460.0f);
1206 degenerateConcave.lineTo(41.446522f, 376.25f);
1207 check_convexity(reporter, degenerateConcave, SkPath::kConcave_Convexity);
1208 check_direction(reporter, degenerateConcave, SkPath::kUnknown_Direction);
reed@google.com7c424812011-05-15 04:38:34 +00001209}
1210
reed@android.com6b82d1a2009-06-03 02:35:01 +00001211static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1212 const SkRect& bounds) {
1213 REPORTER_ASSERT(reporter, p.isConvex());
1214 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +00001215
reed@android.com6b82d1a2009-06-03 02:35:01 +00001216 SkPath p2(p);
1217 REPORTER_ASSERT(reporter, p2.isConvex());
1218 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1219
1220 SkPath other;
1221 other.swap(p2);
1222 REPORTER_ASSERT(reporter, other.isConvex());
1223 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1224}
1225
reed@google.com04863fa2011-05-15 04:08:24 +00001226static void setFromString(SkPath* path, const char str[]) {
1227 bool first = true;
1228 while (str) {
1229 SkScalar x, y;
1230 str = SkParse::FindScalar(str, &x);
1231 if (NULL == str) {
1232 break;
1233 }
1234 str = SkParse::FindScalar(str, &y);
1235 SkASSERT(str);
1236 if (first) {
1237 path->moveTo(x, y);
1238 first = false;
1239 } else {
1240 path->lineTo(x, y);
1241 }
1242 }
1243}
1244
1245static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +00001246 SkPath path;
1247
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001248 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001249 path.addCircle(0, 0, SkIntToScalar(10));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001250 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001251 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001252 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1253
reed@google.com04863fa2011-05-15 04:08:24 +00001254 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001255 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001256 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.com3e71a882012-01-10 18:44:37 +00001257 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCCW_Direction));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001258
reed@google.com04863fa2011-05-15 04:08:24 +00001259 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001260 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001261 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.com3e71a882012-01-10 18:44:37 +00001262 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCW_Direction));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001263
reed@google.com04863fa2011-05-15 04:08:24 +00001264 static const struct {
1265 const char* fPathStr;
1266 SkPath::Convexity fExpectedConvexity;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001267 SkPath::Direction fExpectedDirection;
reed@google.com04863fa2011-05-15 04:08:24 +00001268 } gRec[] = {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001269 { "", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1270 { "0 0", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1271 { "0 0 10 10", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1272 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPath::kUnknown_Direction },
1273 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPath::kCW_Direction },
1274 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPath::kCCW_Direction },
1275 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
1276 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPath::kCW_Direction },
reed@google.com04863fa2011-05-15 04:08:24 +00001277 };
1278
1279 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1280 SkPath path;
1281 setFromString(&path, gRec[i].fPathStr);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001282 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1283 check_direction(reporter, path, gRec[i].fExpectedDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001284 // check after setting the initial convex and direction
1285 if (kDontCheckDir != gRec[i].fExpectedDirection) {
1286 SkPath copy(path);
1287 SkPath::Direction dir;
1288 bool foundDir = copy.cheapComputeDirection(&dir);
1289 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPath::kUnknown_Direction)
1290 ^ foundDir);
1291 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1292 check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
1293 }
1294 REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
1295 check_direction(reporter, path, gRec[i].fExpectedDirection);
reed@google.com04863fa2011-05-15 04:08:24 +00001296 }
1297}
1298
reed@google.com7e6c4d12012-05-10 14:05:43 +00001299static void test_isLine(skiatest::Reporter* reporter) {
1300 SkPath path;
1301 SkPoint pts[2];
1302 const SkScalar value = SkIntToScalar(5);
1303
1304 REPORTER_ASSERT(reporter, !path.isLine(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001305
reed@google.com7e6c4d12012-05-10 14:05:43 +00001306 // set some non-zero values
1307 pts[0].set(value, value);
1308 pts[1].set(value, value);
1309 REPORTER_ASSERT(reporter, !path.isLine(pts));
1310 // check that pts was untouched
1311 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1312 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1313
1314 const SkScalar moveX = SkIntToScalar(1);
1315 const SkScalar moveY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001316 REPORTER_ASSERT(reporter, value != moveX && value != moveY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001317
1318 path.moveTo(moveX, moveY);
1319 REPORTER_ASSERT(reporter, !path.isLine(NULL));
1320 REPORTER_ASSERT(reporter, !path.isLine(pts));
1321 // check that pts was untouched
1322 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1323 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1324
1325 const SkScalar lineX = SkIntToScalar(2);
1326 const SkScalar lineY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001327 REPORTER_ASSERT(reporter, value != lineX && value != lineY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001328
1329 path.lineTo(lineX, lineY);
1330 REPORTER_ASSERT(reporter, path.isLine(NULL));
1331
1332 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1333 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1334 REPORTER_ASSERT(reporter, path.isLine(pts));
1335 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1336 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1337
1338 path.lineTo(0, 0); // too many points/verbs
1339 REPORTER_ASSERT(reporter, !path.isLine(NULL));
1340 REPORTER_ASSERT(reporter, !path.isLine(pts));
1341 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1342 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001343
1344 path.reset();
1345 path.quadTo(1, 1, 2, 2);
1346 REPORTER_ASSERT(reporter, !path.isLine(NULL));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001347}
1348
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001349static void test_conservativelyContains(skiatest::Reporter* reporter) {
1350 SkPath path;
1351
1352 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1353 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1354
1355 // A circle that bounds kBaseRect (with a significant amount of slop)
1356 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001357 circleR = SkScalarMul(circleR, 1.75f) / 2;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001358 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1359
1360 // round-rect radii
1361 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
skia.committer@gmail.comcec8de62012-11-14 02:01:22 +00001362
caryclark@google.com56f233a2012-11-19 13:06:06 +00001363 static const struct SUPPRESS_VISIBILITY_WARNING {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001364 SkRect fQueryRect;
1365 bool fInRect;
1366 bool fInCircle;
1367 bool fInRR;
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001368 bool fInCubicRR;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001369 } kQueries[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001370 {kBaseRect, true, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001371
1372 // rect well inside of kBaseRect
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001373 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1374 kBaseRect.fTop + 0.25f*kBaseRect.height(),
1375 kBaseRect.fRight - 0.25f*kBaseRect.width(),
1376 kBaseRect.fBottom - 0.25f*kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001377 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001378
1379 // rects with edges off by one from kBaseRect's edges
1380 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1381 kBaseRect.width(), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001382 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001383 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1384 kBaseRect.width() + 1, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001385 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001386 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1387 kBaseRect.width() + 1, kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001388 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001389 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1390 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001391 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001392 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1393 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001394 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001395 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1396 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001397 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001398 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1399 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001400 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001401
1402 // zero-w/h rects at each corner of kBaseRect
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001403 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1404 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1405 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1406 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001407
1408 // far away rect
1409 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1410 SkIntToScalar(10), SkIntToScalar(10)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001411 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001412
1413 // very large rect containing kBaseRect
1414 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1415 kBaseRect.fTop - 5 * kBaseRect.height(),
1416 11 * kBaseRect.width(), 11 * kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001417 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001418
1419 // skinny rect that spans same y-range as kBaseRect
1420 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1421 SkIntToScalar(1), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001422 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001423
1424 // short rect that spans same x-range as kBaseRect
1425 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001426 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001427
1428 // skinny rect that spans slightly larger y-range than kBaseRect
1429 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1430 SkIntToScalar(1), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001431 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001432
1433 // short rect that spans slightly larger x-range than kBaseRect
1434 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1435 kBaseRect.width() + 1, SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001436 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001437 };
1438
1439 for (int inv = 0; inv < 4; ++inv) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001440 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001441 SkRect qRect = kQueries[q].fQueryRect;
1442 if (inv & 0x1) {
1443 SkTSwap(qRect.fLeft, qRect.fRight);
1444 }
1445 if (inv & 0x2) {
1446 SkTSwap(qRect.fTop, qRect.fBottom);
1447 }
1448 for (int d = 0; d < 2; ++d) {
1449 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1450 path.reset();
1451 path.addRect(kBaseRect, dir);
1452 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1453 path.conservativelyContainsRect(qRect));
1454
1455 path.reset();
1456 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1457 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1458 path.conservativelyContainsRect(qRect));
1459
1460 path.reset();
1461 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1462 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1463 path.conservativelyContainsRect(qRect));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001464
1465 path.reset();
1466 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1467 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1468 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1469 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1470 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1471 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1472 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1473 path.close();
1474 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1475 path.conservativelyContainsRect(qRect));
1476
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001477 }
1478 // Slightly non-convex shape, shouldn't contain any rects.
1479 path.reset();
1480 path.moveTo(0, 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001481 path.lineTo(SkIntToScalar(50), 0.05f);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001482 path.lineTo(SkIntToScalar(100), 0);
1483 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1484 path.lineTo(0, SkIntToScalar(100));
1485 path.close();
1486 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1487 }
1488 }
1489
1490 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1491 path.reset();
1492 path.moveTo(0, 0);
1493 path.lineTo(SkIntToScalar(100), 0);
1494 path.lineTo(0, SkIntToScalar(100));
1495
1496 // inside, on along top edge
1497 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1498 SkIntToScalar(10),
1499 SkIntToScalar(10))));
1500 // above
1501 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1502 SkRect::MakeXYWH(SkIntToScalar(50),
1503 SkIntToScalar(-10),
1504 SkIntToScalar(10),
1505 SkIntToScalar(10))));
1506 // to the left
1507 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1508 SkIntToScalar(5),
1509 SkIntToScalar(5),
1510 SkIntToScalar(5))));
1511
1512 // outside the diagonal edge
1513 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1514 SkIntToScalar(200),
1515 SkIntToScalar(20),
1516 SkIntToScalar(5))));
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001517
bsalomonb17c1292014-08-28 14:04:55 -07001518
1519 // Test that multiple move commands do not cause asserts.
1520
1521 // At the time of writing, this would not modify cached convexity. This caused an assert while
1522 // checking conservative containment again. http://skbug.com/1460
1523 path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
1524#if 0
1525 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1526 SkIntToScalar(10),
1527 SkIntToScalar(10))));
1528#endif
1529
1530 // Same as above path and first test but with an extra moveTo.
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001531 path.reset();
1532 path.moveTo(100, 100);
1533 path.moveTo(0, 0);
1534 path.lineTo(SkIntToScalar(100), 0);
1535 path.lineTo(0, SkIntToScalar(100));
1536
1537 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1538 SkIntToScalar(10),
1539 SkIntToScalar(10))));
1540
bsalomonb17c1292014-08-28 14:04:55 -07001541 // Test that multiple move commands do not cause asserts and that the function
1542 // is not confused by the multiple moves.
1543 path.reset();
1544 path.moveTo(0, 0);
1545 path.lineTo(SkIntToScalar(100), 0);
1546 path.lineTo(0, SkIntToScalar(100));
1547 path.moveTo(0, SkIntToScalar(200));
1548 path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
1549 path.lineTo(0, SkIntToScalar(300));
1550
1551 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1552 SkRect::MakeXYWH(SkIntToScalar(50), 0,
1553 SkIntToScalar(10),
1554 SkIntToScalar(10))));
1555
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001556 path.reset();
1557 path.lineTo(100, 100);
1558 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001559}
1560
reed@google.comf32322b2013-10-16 15:14:04 +00001561static void test_isRect_open_close(skiatest::Reporter* reporter) {
1562 SkPath path;
1563 bool isClosed;
1564
1565 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
reed@google.comf32322b2013-10-16 15:14:04 +00001566 path.close();
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001567
reed@google.comf32322b2013-10-16 15:14:04 +00001568 REPORTER_ASSERT(reporter, path.isRect(NULL, NULL));
1569 REPORTER_ASSERT(reporter, path.isRect(&isClosed, NULL));
1570 REPORTER_ASSERT(reporter, isClosed);
commit-bot@chromium.org7e90e8d2014-02-11 01:38:30 +00001571 REPORTER_ASSERT(reporter, SkPath::kStroke_PathAsRect == path.asRect(NULL));
reed@google.comf32322b2013-10-16 15:14:04 +00001572}
1573
caryclark@google.comf1316942011-07-26 19:54:45 +00001574// Simple isRect test is inline TestPath, below.
1575// test_isRect provides more extensive testing.
1576static void test_isRect(skiatest::Reporter* reporter) {
reed@google.comf32322b2013-10-16 15:14:04 +00001577 test_isRect_open_close(reporter);
1578
caryclark@google.comf1316942011-07-26 19:54:45 +00001579 // passing tests (all moveTo / lineTo...
1580 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1581 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1582 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1583 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1584 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1585 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1586 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1587 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1588 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001589 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1590 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}};
caryclark@google.comf1316942011-07-26 19:54:45 +00001591 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1592 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1593 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
caryclark@google.combfe90372012-11-21 13:56:20 +00001594 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
rmistry@google.comd6176b02012-08-23 18:14:13 +00001595
caryclark@google.comf1316942011-07-26 19:54:45 +00001596 // failing tests
1597 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1598 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1599 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1600 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1601 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1602 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1603 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1604 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
caryclark@google.combfe90372012-11-21 13:56:20 +00001605 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
1606 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
1607 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
rmistry@google.comd6176b02012-08-23 18:14:13 +00001608
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001609 // no close, but we should detect them as fillably the same as a rect
1610 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1611 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
1612 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
1613
1614 // like c2, but we double-back on ourselves
1615 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
1616 // like c2, but we overshoot the start point
1617 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
1618 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
caryclark@google.comf1316942011-07-26 19:54:45 +00001619
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001620 struct IsRectTest {
1621 SkPoint *fPoints;
1622 size_t fPointCount;
1623 bool fClose;
1624 bool fIsRect;
1625 } tests[] = {
1626 { r1, SK_ARRAY_COUNT(r1), true, true },
1627 { r2, SK_ARRAY_COUNT(r2), true, true },
1628 { r3, SK_ARRAY_COUNT(r3), true, true },
1629 { r4, SK_ARRAY_COUNT(r4), true, true },
1630 { r5, SK_ARRAY_COUNT(r5), true, true },
1631 { r6, SK_ARRAY_COUNT(r6), true, true },
1632 { r7, SK_ARRAY_COUNT(r7), true, true },
1633 { r8, SK_ARRAY_COUNT(r8), true, true },
1634 { r9, SK_ARRAY_COUNT(r9), true, true },
1635 { ra, SK_ARRAY_COUNT(ra), true, true },
1636 { rb, SK_ARRAY_COUNT(rb), true, true },
1637 { rc, SK_ARRAY_COUNT(rc), true, true },
1638 { rd, SK_ARRAY_COUNT(rd), true, true },
1639 { re, SK_ARRAY_COUNT(re), true, true },
1640 { rf, SK_ARRAY_COUNT(rf), true, true },
1641
1642 { f1, SK_ARRAY_COUNT(f1), true, false },
1643 { f2, SK_ARRAY_COUNT(f2), true, false },
1644 { f3, SK_ARRAY_COUNT(f3), true, false },
1645 { f4, SK_ARRAY_COUNT(f4), true, false },
1646 { f5, SK_ARRAY_COUNT(f5), true, false },
1647 { f6, SK_ARRAY_COUNT(f6), true, false },
1648 { f7, SK_ARRAY_COUNT(f7), true, false },
1649 { f8, SK_ARRAY_COUNT(f8), true, false },
1650 { f9, SK_ARRAY_COUNT(f9), true, false },
1651 { fa, SK_ARRAY_COUNT(fa), true, false },
1652 { fb, SK_ARRAY_COUNT(fb), true, false },
1653
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001654 { c1, SK_ARRAY_COUNT(c1), false, true },
1655 { c2, SK_ARRAY_COUNT(c2), false, true },
1656 { c3, SK_ARRAY_COUNT(c3), false, true },
1657
1658 { d1, SK_ARRAY_COUNT(d1), false, false },
1659 { d2, SK_ARRAY_COUNT(d2), false, false },
1660 { d3, SK_ARRAY_COUNT(d3), false, false },
caryclark@google.comf1316942011-07-26 19:54:45 +00001661 };
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001662
1663 const size_t testCount = SK_ARRAY_COUNT(tests);
caryclark@google.comf1316942011-07-26 19:54:45 +00001664 size_t index;
1665 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1666 SkPath path;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001667 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1668 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1669 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.comf1316942011-07-26 19:54:45 +00001670 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001671 if (tests[testIndex].fClose) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001672 path.close();
1673 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001674 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(NULL));
1675 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(NULL, NULL));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001676
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001677 if (tests[testIndex].fIsRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001678 SkRect computed, expected;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001679 expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001680 REPORTER_ASSERT(reporter, path.isRect(&computed));
1681 REPORTER_ASSERT(reporter, expected == computed);
skia.committer@gmail.com1c9c0d32012-11-22 02:02:41 +00001682
caryclark@google.comf68154a2012-11-21 15:18:06 +00001683 bool isClosed;
1684 SkPath::Direction direction, cheapDirection;
1685 REPORTER_ASSERT(reporter, path.cheapComputeDirection(&cheapDirection));
robertphillips@google.com8fd16032013-06-25 15:39:58 +00001686 REPORTER_ASSERT(reporter, path.isRect(&isClosed, &direction));
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001687 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001688 REPORTER_ASSERT(reporter, direction == cheapDirection);
commit-bot@chromium.orgc2abd542014-01-25 16:54:31 +00001689 direction = (SkPath::Direction) -1;
commit-bot@chromium.org7e90e8d2014-02-11 01:38:30 +00001690 if (!tests[testIndex].fClose) {
commit-bot@chromium.orgc2abd542014-01-25 16:54:31 +00001691 REPORTER_ASSERT(reporter, SkPath::kFill_PathAsRect == path.asRect());
1692 REPORTER_ASSERT(reporter, SkPath::kFill_PathAsRect == path.asRect(&direction));
1693 } else {
1694 REPORTER_ASSERT(reporter, SkPath::kStroke_PathAsRect == path.asRect());
1695 REPORTER_ASSERT(reporter, SkPath::kStroke_PathAsRect == path.asRect(&direction));
1696 }
1697 REPORTER_ASSERT(reporter, direction == cheapDirection);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001698 } else {
1699 SkRect computed;
1700 computed.set(123, 456, 789, 1011);
1701 REPORTER_ASSERT(reporter, !path.isRect(&computed));
1702 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
1703 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
1704
1705 bool isClosed = (bool) -1;
1706 SkPath::Direction direction = (SkPath::Direction) -1;
robertphillips@google.com8fd16032013-06-25 15:39:58 +00001707 REPORTER_ASSERT(reporter, !path.isRect(&isClosed, &direction));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001708 REPORTER_ASSERT(reporter, isClosed == (bool) -1);
1709 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
commit-bot@chromium.orgc2abd542014-01-25 16:54:31 +00001710 REPORTER_ASSERT(reporter, SkPath::kNone_PathAsRect == path.asRect());
1711 REPORTER_ASSERT(reporter, SkPath::kNone_PathAsRect == path.asRect(&direction));
1712 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001713 }
caryclark@google.comf1316942011-07-26 19:54:45 +00001714 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001715
caryclark@google.comf1316942011-07-26 19:54:45 +00001716 // fail, close then line
1717 SkPath path1;
1718 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001719 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001720 path1.lineTo(r1[index].fX, r1[index].fY);
1721 }
1722 path1.close();
1723 path1.lineTo(1, 0);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001724 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001725
caryclark@google.comf1316942011-07-26 19:54:45 +00001726 // fail, move in the middle
1727 path1.reset();
1728 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001729 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001730 if (index == 2) {
1731 path1.moveTo(1, .5f);
1732 }
1733 path1.lineTo(r1[index].fX, r1[index].fY);
1734 }
1735 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001736 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00001737
1738 // fail, move on the edge
1739 path1.reset();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001740 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001741 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
1742 path1.lineTo(r1[index].fX, r1[index].fY);
1743 }
1744 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001745 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001746
caryclark@google.comf1316942011-07-26 19:54:45 +00001747 // fail, quad
1748 path1.reset();
1749 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001750 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001751 if (index == 2) {
1752 path1.quadTo(1, .5f, 1, .5f);
1753 }
1754 path1.lineTo(r1[index].fX, r1[index].fY);
1755 }
1756 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001757 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001758
caryclark@google.comf1316942011-07-26 19:54:45 +00001759 // fail, cubic
1760 path1.reset();
1761 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001762 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001763 if (index == 2) {
1764 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
1765 }
1766 path1.lineTo(r1[index].fX, r1[index].fY);
1767 }
1768 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001769 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00001770}
1771
caryclark@google.com56f233a2012-11-19 13:06:06 +00001772static void test_isNestedRects(skiatest::Reporter* reporter) {
1773 // passing tests (all moveTo / lineTo...
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001774 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001775 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1776 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1777 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001778 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001779 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1780 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1781 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1782 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001783 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
1784 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}}; // CW
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001785 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
1786 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
1787 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001788
1789 // failing tests
1790 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1791 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1792 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1793 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1794 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1795 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1796 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1797 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
1798
1799 // failing, no close
1800 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
1801 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
1802
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001803 struct IsNestedRectTest {
1804 SkPoint *fPoints;
1805 size_t fPointCount;
1806 SkPath::Direction fDirection;
1807 bool fClose;
1808 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
1809 } tests[] = {
1810 { r1, SK_ARRAY_COUNT(r1), SkPath::kCW_Direction , true, true },
1811 { r2, SK_ARRAY_COUNT(r2), SkPath::kCW_Direction , true, true },
1812 { r3, SK_ARRAY_COUNT(r3), SkPath::kCW_Direction , true, true },
1813 { r4, SK_ARRAY_COUNT(r4), SkPath::kCW_Direction , true, true },
1814 { r5, SK_ARRAY_COUNT(r5), SkPath::kCCW_Direction, true, true },
1815 { r6, SK_ARRAY_COUNT(r6), SkPath::kCCW_Direction, true, true },
1816 { r7, SK_ARRAY_COUNT(r7), SkPath::kCCW_Direction, true, true },
1817 { r8, SK_ARRAY_COUNT(r8), SkPath::kCCW_Direction, true, true },
1818 { r9, SK_ARRAY_COUNT(r9), SkPath::kCCW_Direction, true, true },
1819 { ra, SK_ARRAY_COUNT(ra), SkPath::kCCW_Direction, true, true },
1820 { rb, SK_ARRAY_COUNT(rb), SkPath::kCW_Direction, true, true },
1821 { rc, SK_ARRAY_COUNT(rc), SkPath::kCW_Direction, true, true },
1822 { rd, SK_ARRAY_COUNT(rd), SkPath::kCCW_Direction, true, true },
1823 { re, SK_ARRAY_COUNT(re), SkPath::kCW_Direction, true, true },
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001824
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001825 { f1, SK_ARRAY_COUNT(f1), SkPath::kUnknown_Direction, true, false },
1826 { f2, SK_ARRAY_COUNT(f2), SkPath::kUnknown_Direction, true, false },
1827 { f3, SK_ARRAY_COUNT(f3), SkPath::kUnknown_Direction, true, false },
1828 { f4, SK_ARRAY_COUNT(f4), SkPath::kUnknown_Direction, true, false },
1829 { f5, SK_ARRAY_COUNT(f5), SkPath::kUnknown_Direction, true, false },
1830 { f6, SK_ARRAY_COUNT(f6), SkPath::kUnknown_Direction, true, false },
1831 { f7, SK_ARRAY_COUNT(f7), SkPath::kUnknown_Direction, true, false },
1832 { f8, SK_ARRAY_COUNT(f8), SkPath::kUnknown_Direction, true, false },
1833
1834 { c1, SK_ARRAY_COUNT(c1), SkPath::kUnknown_Direction, false, false },
1835 { c2, SK_ARRAY_COUNT(c2), SkPath::kUnknown_Direction, false, false },
1836 };
1837
1838 const size_t testCount = SK_ARRAY_COUNT(tests);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001839 size_t index;
1840 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001841 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1842 SkPath path;
1843 if (rectFirst) {
1844 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1845 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001846 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1847 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1848 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001849 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001850 if (tests[testIndex].fClose) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001851 path.close();
1852 }
1853 if (!rectFirst) {
1854 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1855 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001856 REPORTER_ASSERT(reporter, tests[testIndex].fIsNestedRect == path.isNestedRects(NULL));
1857 if (tests[testIndex].fIsNestedRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001858 SkRect expected[2], computed[2];
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001859 SkPath::Direction expectedDirs[2], computedDirs[2];
caryclark@google.com56f233a2012-11-19 13:06:06 +00001860 SkRect testBounds;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001861 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001862 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
1863 expected[1] = testBounds;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001864 if (rectFirst) {
1865 expectedDirs[0] = SkPath::kCW_Direction;
1866 } else {
1867 expectedDirs[0] = SkPath::kCCW_Direction;
1868 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001869 expectedDirs[1] = tests[testIndex].fDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001870 REPORTER_ASSERT(reporter, path.isNestedRects(computed, computedDirs));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001871 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
1872 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001873 REPORTER_ASSERT(reporter, expectedDirs[0] == computedDirs[0]);
1874 REPORTER_ASSERT(reporter, expectedDirs[1] == computedDirs[1]);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001875 }
caryclark@google.com56f233a2012-11-19 13:06:06 +00001876 }
1877
1878 // fail, close then line
1879 SkPath path1;
1880 if (rectFirst) {
1881 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1882 }
1883 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001884 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001885 path1.lineTo(r1[index].fX, r1[index].fY);
1886 }
1887 path1.close();
1888 path1.lineTo(1, 0);
1889 if (!rectFirst) {
1890 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1891 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001892 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001893
1894 // fail, move in the middle
1895 path1.reset();
1896 if (rectFirst) {
1897 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1898 }
1899 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001900 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001901 if (index == 2) {
1902 path1.moveTo(1, .5f);
1903 }
1904 path1.lineTo(r1[index].fX, r1[index].fY);
1905 }
1906 path1.close();
1907 if (!rectFirst) {
1908 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1909 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001910 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001911
1912 // fail, move on the edge
1913 path1.reset();
1914 if (rectFirst) {
1915 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1916 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001917 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001918 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
1919 path1.lineTo(r1[index].fX, r1[index].fY);
1920 }
1921 path1.close();
1922 if (!rectFirst) {
1923 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1924 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001925 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001926
1927 // fail, quad
1928 path1.reset();
1929 if (rectFirst) {
1930 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1931 }
1932 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001933 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001934 if (index == 2) {
1935 path1.quadTo(1, .5f, 1, .5f);
1936 }
1937 path1.lineTo(r1[index].fX, r1[index].fY);
1938 }
1939 path1.close();
1940 if (!rectFirst) {
1941 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1942 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001943 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001944
1945 // fail, cubic
1946 path1.reset();
1947 if (rectFirst) {
1948 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1949 }
1950 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001951 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001952 if (index == 2) {
1953 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
1954 }
1955 path1.lineTo(r1[index].fX, r1[index].fY);
1956 }
1957 path1.close();
1958 if (!rectFirst) {
1959 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1960 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001961 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
skia.committer@gmail.com34587162012-11-20 02:01:23 +00001962
caryclark@google.com56f233a2012-11-19 13:06:06 +00001963 // fail, not nested
1964 path1.reset();
1965 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
1966 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001967 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001968 }
caryclark@google.combfe90372012-11-21 13:56:20 +00001969
1970 // pass, stroke rect
1971 SkPath src, dst;
1972 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
1973 SkPaint strokePaint;
1974 strokePaint.setStyle(SkPaint::kStroke_Style);
1975 strokePaint.setStrokeWidth(2);
1976 strokePaint.getFillPath(src, &dst);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001977 REPORTER_ASSERT(reporter, dst.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001978}
1979
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001980static void write_and_read_back(skiatest::Reporter* reporter,
1981 const SkPath& p) {
commit-bot@chromium.org19382422014-01-14 20:51:26 +00001982 SkWriter32 writer;
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001983 writer.writePath(p);
reed@google.com44699382013-10-31 17:28:30 +00001984 size_t size = writer.bytesWritten();
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001985 SkAutoMalloc storage(size);
1986 writer.flatten(storage.get());
1987 SkReader32 reader(storage.get(), size);
1988
1989 SkPath readBack;
1990 REPORTER_ASSERT(reporter, readBack != p);
1991 reader.readPath(&readBack);
1992 REPORTER_ASSERT(reporter, readBack == p);
1993
rmistry@google.comd6176b02012-08-23 18:14:13 +00001994 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001995 p.getConvexityOrUnknown());
1996
1997 REPORTER_ASSERT(reporter, readBack.isOval(NULL) == p.isOval(NULL));
1998
1999 const SkRect& origBounds = p.getBounds();
2000 const SkRect& readBackBounds = readBack.getBounds();
2001
2002 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
2003}
2004
reed@google.com53effc52011-09-21 19:05:12 +00002005static void test_flattening(skiatest::Reporter* reporter) {
2006 SkPath p;
2007
2008 static const SkPoint pts[] = {
2009 { 0, 0 },
2010 { SkIntToScalar(10), SkIntToScalar(10) },
2011 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2012 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2013 };
2014 p.moveTo(pts[0]);
2015 p.lineTo(pts[1]);
2016 p.quadTo(pts[2], pts[3]);
2017 p.cubicTo(pts[4], pts[5], pts[6]);
2018
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002019 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002020
2021 // create a buffer that should be much larger than the path so we don't
2022 // kill our stack if writer goes too far.
2023 char buffer[1024];
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002024 size_t size1 = p.writeToMemory(NULL);
2025 size_t size2 = p.writeToMemory(buffer);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002026 REPORTER_ASSERT(reporter, size1 == size2);
2027
2028 SkPath p2;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002029 size_t size3 = p2.readFromMemory(buffer, 1024);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002030 REPORTER_ASSERT(reporter, size1 == size3);
2031 REPORTER_ASSERT(reporter, p == p2);
2032
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002033 size3 = p2.readFromMemory(buffer, 0);
2034 REPORTER_ASSERT(reporter, !size3);
2035
2036 SkPath tooShort;
2037 size3 = tooShort.readFromMemory(buffer, size1 - 1);
2038 REPORTER_ASSERT(reporter, tooShort.isEmpty());
2039
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002040 char buffer2[1024];
2041 size3 = p2.writeToMemory(buffer2);
2042 REPORTER_ASSERT(reporter, size1 == size3);
2043 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002044
2045 // test persistence of the oval flag & convexity
2046 {
2047 SkPath oval;
2048 SkRect rect = SkRect::MakeWH(10, 10);
2049 oval.addOval(rect);
2050
2051 write_and_read_back(reporter, oval);
2052 }
reed@google.com53effc52011-09-21 19:05:12 +00002053}
2054
2055static void test_transform(skiatest::Reporter* reporter) {
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002056 SkPath p;
rmistry@google.comd6176b02012-08-23 18:14:13 +00002057
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002058#define CONIC_PERSPECTIVE_BUG_FIXED 0
reed@google.com53effc52011-09-21 19:05:12 +00002059 static const SkPoint pts[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002060 { 0, 0 }, // move
2061 { SkIntToScalar(10), SkIntToScalar(10) }, // line
2062 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
2063 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
2064#if CONIC_PERSPECTIVE_BUG_FIXED
2065 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
2066#endif
reed@google.com53effc52011-09-21 19:05:12 +00002067 };
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002068 const int kPtCount = SK_ARRAY_COUNT(pts);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002069
reed@google.com53effc52011-09-21 19:05:12 +00002070 p.moveTo(pts[0]);
2071 p.lineTo(pts[1]);
2072 p.quadTo(pts[2], pts[3]);
2073 p.cubicTo(pts[4], pts[5], pts[6]);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002074#if CONIC_PERSPECTIVE_BUG_FIXED
2075 p.conicTo(pts[4], pts[5], 0.5f);
2076#endif
2077 p.close();
rmistry@google.comd6176b02012-08-23 18:14:13 +00002078
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002079 {
2080 SkMatrix matrix;
2081 matrix.reset();
2082 SkPath p1;
2083 p.transform(matrix, &p1);
2084 REPORTER_ASSERT(reporter, p == p1);
reed@google.com53effc52011-09-21 19:05:12 +00002085 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002086
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002087
2088 {
2089 SkMatrix matrix;
2090 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2091
2092 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
2093
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002094 p.transform(matrix, &p1);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002095 SkPoint pts1[kPtCount];
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002096 int count = p1.getPoints(pts1, kPtCount);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002097 REPORTER_ASSERT(reporter, kPtCount == count);
2098 for (int i = 0; i < count; ++i) {
2099 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2100 REPORTER_ASSERT(reporter, newPt == pts1[i]);
2101 }
2102 }
2103
2104 {
2105 SkMatrix matrix;
2106 matrix.reset();
2107 matrix.setPerspX(SkScalarToPersp(4));
2108
2109 SkPath p1;
2110 p1.moveTo(SkPoint::Make(0, 0));
2111
2112 p.transform(matrix, &p1);
2113 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
2114 p1.transform(matrix, NULL);
2115 SkRect pBounds = p.getBounds();
2116 SkRect p1Bounds = p1.getBounds();
2117 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2118 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2119 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2120 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2121 }
2122
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002123 p.reset();
2124 p.addCircle(0, 0, 1, SkPath::kCW_Direction);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002125
2126 {
2127 SkMatrix matrix;
2128 matrix.reset();
2129 SkPath p1;
2130 p1.moveTo(SkPoint::Make(0, 0));
2131
2132 p.transform(matrix, &p1);
2133 REPORTER_ASSERT(reporter, p1.cheapIsDirection(SkPath::kCW_Direction));
2134 }
2135
2136
2137 {
2138 SkMatrix matrix;
2139 matrix.reset();
2140 matrix.setScaleX(-1);
2141 SkPath p1;
2142 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2143
2144 p.transform(matrix, &p1);
2145 REPORTER_ASSERT(reporter, p1.cheapIsDirection(SkPath::kCCW_Direction));
2146 }
2147
2148 {
2149 SkMatrix matrix;
2150 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2151 SkPath p1;
2152 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2153
2154 p.transform(matrix, &p1);
2155 REPORTER_ASSERT(reporter, p1.cheapIsDirection(SkPath::kUnknown_Direction));
2156 }
reed@google.com53effc52011-09-21 19:05:12 +00002157}
2158
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002159static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002160 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002161 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002162
caryclark@google.com56f233a2012-11-19 13:06:06 +00002163 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002164 const char* testPath;
2165 const size_t numResultPts;
2166 const SkRect resultBound;
2167 const SkPath::Verb* resultVerbs;
2168 const size_t numResultVerbs;
2169 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002170
schenney@chromium.org7e963602012-06-13 17:05:43 +00002171 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2172 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2173 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2174 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2175 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2176 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2177 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2178 static const SkPath::Verb resultVerbs8[] = {
2179 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2180 };
2181 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2182 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2183 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2184 static const SkPath::Verb resultVerbs12[] = {
2185 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2186 };
2187 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2188 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2189 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2190 static const SkPath::Verb resultVerbs16[] = {
2191 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2192 };
2193 static const struct zeroPathTestData gZeroLengthTests[] = {
2194 { "M 1 1", 1, {0, 0, 0, 0}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002195 { "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 +00002196 { "M 1 1 z", 1, {0, 0, 0, 0}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002197 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2198 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2199 { "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) },
2200 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2201 { "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) },
2202 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2203 { "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) },
2204 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2205 { "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) },
2206 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2207 { "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 +00002208 SK_ARRAY_COUNT(resultVerbs14)
2209 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002210 { "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) },
2211 { "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 +00002212 SK_ARRAY_COUNT(resultVerbs16)
2213 }
2214 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002215
schenney@chromium.org7e963602012-06-13 17:05:43 +00002216 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2217 p.reset();
2218 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2219 REPORTER_ASSERT(reporter, valid);
2220 REPORTER_ASSERT(reporter, !p.isEmpty());
2221 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2222 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2223 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2224 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2225 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2226 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002227 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002228}
2229
2230struct SegmentInfo {
2231 SkPath fPath;
2232 int fPointCount;
2233};
2234
reed@google.com10296cc2011-09-21 12:29:05 +00002235#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2236
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002237static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00002238 SkPath p, p2;
2239
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002240 p.moveTo(0, 0);
2241 p.quadTo(100, 100, 200, 200);
2242 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2243 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002244 p2 = p;
2245 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002246 p.cubicTo(100, 100, 200, 200, 300, 300);
2247 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2248 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002249 p2 = p;
2250 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2251
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002252 p.reset();
2253 p.moveTo(0, 0);
2254 p.cubicTo(100, 100, 200, 200, 300, 300);
2255 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00002256 p2 = p;
2257 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00002258
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002259 REPORTER_ASSERT(reporter, !p.isEmpty());
2260}
2261
2262static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002263 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002264 SkPoint pts[4];
2265
2266 // Test an iterator with no path
2267 SkPath::Iter noPathIter;
2268 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002269
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002270 // Test that setting an empty path works
2271 noPathIter.setPath(p, false);
2272 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002273
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002274 // Test that close path makes no difference for an empty path
2275 noPathIter.setPath(p, true);
2276 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002277
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002278 // Test an iterator with an initial empty path
2279 SkPath::Iter iter(p, false);
2280 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2281
2282 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00002283 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002284 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2285
rmistry@google.comd6176b02012-08-23 18:14:13 +00002286
schenney@chromium.org7e963602012-06-13 17:05:43 +00002287 struct iterTestData {
2288 const char* testPath;
2289 const bool forceClose;
2290 const bool consumeDegenerates;
2291 const size_t* numResultPtsPerVerb;
2292 const SkPoint* resultPts;
2293 const SkPath::Verb* resultVerbs;
2294 const size_t numResultVerbs;
2295 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002296
schenney@chromium.org7e963602012-06-13 17:05:43 +00002297 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2298 static const SkPath::Verb resultVerbs2[] = {
2299 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2300 };
2301 static const SkPath::Verb resultVerbs3[] = {
2302 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2303 };
2304 static const SkPath::Verb resultVerbs4[] = {
2305 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2306 };
2307 static const SkPath::Verb resultVerbs5[] = {
2308 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2309 };
2310 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00002311 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2312 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2313 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2314 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002315 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002316 static const SkPoint resultPts2[] = {
2317 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2318 };
2319 static const SkPoint resultPts3[] = {
2320 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2321 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2322 };
2323 static const SkPoint resultPts4[] = {
2324 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2325 };
2326 static const SkPoint resultPts5[] = {
2327 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2328 };
2329 static const struct iterTestData gIterTests[] = {
2330 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002331 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2332 { "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 +00002333 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2334 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2335 { "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) },
2336 { "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 +00002337 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2338 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2339 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2340 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2341 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2342 { "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 +00002343 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002344
schenney@chromium.org7e963602012-06-13 17:05:43 +00002345 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2346 p.reset();
2347 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2348 REPORTER_ASSERT(reporter, valid);
2349 iter.setPath(p, gIterTests[i].forceClose);
2350 int j = 0, l = 0;
2351 do {
2352 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2353 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2354 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2355 }
2356 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2357 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2358 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002359
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002360 p.reset();
2361 iter.setPath(p, false);
2362 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2363 p.lineTo(1, 1);
2364 p.close();
2365 iter.setPath(p, false);
2366 REPORTER_ASSERT(reporter, iter.isClosedContour());
2367 p.reset();
2368 iter.setPath(p, true);
2369 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2370 p.lineTo(1, 1);
2371 iter.setPath(p, true);
2372 REPORTER_ASSERT(reporter, iter.isClosedContour());
2373 p.moveTo(0, 0);
2374 p.lineTo(2, 2);
2375 iter.setPath(p, false);
2376 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2377
2378 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
2379 // check to see if the result is correct.
2380 for (int setNaN = 0; setNaN < 4; ++setNaN) {
2381 p.reset();
2382 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
2383 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
2384 iter.setPath(p, true);
2385 iter.next(pts, false);
2386 iter.next(pts, false);
2387 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
2388 }
2389
2390 p.reset();
2391 p.quadTo(0, 0, 0, 0);
2392 iter.setPath(p, false);
2393 iter.next(pts, false);
2394 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false));
2395 iter.setPath(p, false);
2396 iter.next(pts, false);
2397 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2398
2399 p.reset();
2400 p.conicTo(0, 0, 0, 0, 0.5f);
2401 iter.setPath(p, false);
2402 iter.next(pts, false);
2403 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
2404 iter.setPath(p, false);
2405 iter.next(pts, false);
2406 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2407
2408 p.reset();
2409 p.cubicTo(0, 0, 0, 0, 0, 0);
2410 iter.setPath(p, false);
2411 iter.next(pts, false);
2412 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2413 iter.setPath(p, false);
2414 iter.next(pts, false);
2415 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2416
2417 p.moveTo(1, 1); // add a trailing moveto
2418 iter.setPath(p, false);
2419 iter.next(pts, false);
2420 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2421 iter.setPath(p, false);
2422 iter.next(pts, false);
2423 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2424
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002425 // The GM degeneratesegments.cpp test is more extensive
2426}
2427
2428static void test_raw_iter(skiatest::Reporter* reporter) {
2429 SkPath p;
2430 SkPoint pts[4];
2431
2432 // Test an iterator with no path
2433 SkPath::RawIter noPathIter;
2434 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2435 // Test that setting an empty path works
2436 noPathIter.setPath(p);
2437 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00002438
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002439 // Test an iterator with an initial empty path
2440 SkPath::RawIter iter(p);
2441 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2442
2443 // Test that a move-only path returns the move.
2444 p.moveTo(SK_Scalar1, 0);
2445 iter.setPath(p);
2446 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2447 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2448 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2449 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2450
2451 // No matter how many moves we add, we should get them all back
2452 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2453 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2454 iter.setPath(p);
2455 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2456 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2457 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2458 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2459 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2460 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2461 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2462 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2463 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2464 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2465
2466 // Initial close is never ever stored
2467 p.reset();
2468 p.close();
2469 iter.setPath(p);
2470 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2471
2472 // Move/close sequences
2473 p.reset();
2474 p.close(); // Not stored, no purpose
2475 p.moveTo(SK_Scalar1, 0);
2476 p.close();
2477 p.close(); // Not stored, no purpose
2478 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2479 p.close();
2480 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2481 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
2482 p.close();
2483 iter.setPath(p);
2484 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2485 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2486 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2487 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2488 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2489 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2490 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2491 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2492 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2493 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2494 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2495 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2496 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2497 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2498 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2499 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2500 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2501 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2502 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2503 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2504 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2505 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2506
2507 // Generate random paths and verify
2508 SkPoint randomPts[25];
2509 for (int i = 0; i < 5; ++i) {
2510 for (int j = 0; j < 5; ++j) {
2511 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
2512 }
2513 }
2514
2515 // Max of 10 segments, max 3 points per segment
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002516 SkRandom rand(9876543);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002517 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00002518 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002519 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00002520
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002521 for (int i = 0; i < 500; ++i) {
2522 p.reset();
2523 bool lastWasClose = true;
2524 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00002525 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002526 int numPoints = 0;
2527 int numVerbs = (rand.nextU() >> 16) % 10;
2528 int numIterVerbs = 0;
2529 for (int j = 0; j < numVerbs; ++j) {
2530 do {
2531 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
2532 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002533 switch (nextVerb) {
2534 case SkPath::kMove_Verb:
2535 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2536 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00002537 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002538 numPoints += 1;
2539 lastWasClose = false;
2540 haveMoveTo = true;
2541 break;
2542 case SkPath::kLine_Verb:
2543 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002544 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002545 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2546 haveMoveTo = true;
2547 }
2548 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2549 p.lineTo(expectedPts[numPoints]);
2550 numPoints += 1;
2551 lastWasClose = false;
2552 break;
2553 case SkPath::kQuad_Verb:
2554 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002555 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002556 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2557 haveMoveTo = true;
2558 }
2559 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2560 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2561 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
2562 numPoints += 2;
2563 lastWasClose = false;
2564 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002565 case SkPath::kConic_Verb:
2566 if (!haveMoveTo) {
2567 expectedPts[numPoints++] = lastMoveToPt;
2568 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2569 haveMoveTo = true;
2570 }
2571 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2572 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2573 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2574 rand.nextUScalar1() * 4);
2575 numPoints += 2;
2576 lastWasClose = false;
2577 break;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002578 case SkPath::kCubic_Verb:
2579 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002580 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002581 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2582 haveMoveTo = true;
2583 }
2584 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2585 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2586 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
2587 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2588 expectedPts[numPoints + 2]);
2589 numPoints += 3;
2590 lastWasClose = false;
2591 break;
2592 case SkPath::kClose_Verb:
2593 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00002594 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002595 lastWasClose = true;
2596 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002597 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002598 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002599 }
2600 expectedVerbs[numIterVerbs++] = nextVerb;
2601 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00002602
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002603 iter.setPath(p);
2604 numVerbs = numIterVerbs;
2605 numIterVerbs = 0;
2606 int numIterPts = 0;
2607 SkPoint lastMoveTo;
2608 SkPoint lastPt;
2609 lastMoveTo.set(0, 0);
2610 lastPt.set(0, 0);
2611 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
2612 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
2613 numIterVerbs++;
2614 switch (nextVerb) {
2615 case SkPath::kMove_Verb:
2616 REPORTER_ASSERT(reporter, numIterPts < numPoints);
2617 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
2618 lastPt = lastMoveTo = pts[0];
2619 numIterPts += 1;
2620 break;
2621 case SkPath::kLine_Verb:
2622 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
2623 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2624 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2625 lastPt = pts[1];
2626 numIterPts += 1;
2627 break;
2628 case SkPath::kQuad_Verb:
reed@google.com277c3f82013-05-31 15:17:50 +00002629 case SkPath::kConic_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002630 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
2631 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2632 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2633 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2634 lastPt = pts[2];
2635 numIterPts += 2;
2636 break;
2637 case SkPath::kCubic_Verb:
2638 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
2639 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2640 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2641 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2642 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
2643 lastPt = pts[3];
2644 numIterPts += 3;
2645 break;
2646 case SkPath::kClose_Verb:
2647 REPORTER_ASSERT(reporter, pts[0] == lastMoveTo);
2648 lastPt = lastMoveTo;
2649 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002650 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002651 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002652 }
2653 }
2654 REPORTER_ASSERT(reporter, numIterPts == numPoints);
2655 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
2656 }
2657}
2658
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002659static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002660 const SkPath& path,
2661 bool expectedCircle,
2662 SkPath::Direction expectedDir) {
robertphillips@google.com466310d2013-12-03 16:43:54 +00002663 SkRect rect = SkRect::MakeEmpty();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002664 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
2665 REPORTER_ASSERT(reporter, path.cheapIsDirection(expectedDir));
skia.committer@gmail.comfbb0ed92012-11-13 21:46:06 +00002666
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002667 if (expectedCircle) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002668 REPORTER_ASSERT(reporter, rect.height() == rect.width());
2669 }
2670}
2671
2672static void test_circle_skew(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002673 const SkPath& path,
2674 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002675 SkPath tmp;
2676
2677 SkMatrix m;
2678 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
2679 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002680 // this matrix reverses the direction.
2681 if (SkPath::kCCW_Direction == dir) {
2682 dir = SkPath::kCW_Direction;
2683 } else {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002684 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002685 dir = SkPath::kCCW_Direction;
2686 }
2687 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002688}
2689
2690static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002691 const SkPath& path,
2692 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002693 SkPath tmp;
2694
2695 // translate at small offset
2696 SkMatrix m;
2697 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
2698 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002699 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002700
2701 tmp.reset();
2702 m.reset();
2703
2704 // translate at a relatively big offset
2705 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
2706 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002707 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002708}
2709
2710static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002711 const SkPath& path,
2712 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002713 for (int angle = 0; angle < 360; ++angle) {
2714 SkPath tmp;
2715 SkMatrix m;
2716 m.setRotate(SkIntToScalar(angle));
2717 path.transform(m, &tmp);
2718
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002719 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002720 // degrees is not an oval anymore, this can be improved. we made this
2721 // for the simplicity of our implementation.
2722 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002723 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002724 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002725 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002726 }
2727 }
2728}
2729
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002730static void test_circle_mirror_x(skiatest::Reporter* reporter,
2731 const SkPath& path,
2732 SkPath::Direction dir) {
2733 SkPath tmp;
2734 SkMatrix m;
2735 m.reset();
2736 m.setScaleX(-SK_Scalar1);
2737 path.transform(m, &tmp);
2738
2739 if (SkPath::kCW_Direction == dir) {
2740 dir = SkPath::kCCW_Direction;
2741 } else {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002742 REPORTER_ASSERT(reporter, SkPath::kCCW_Direction == dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002743 dir = SkPath::kCW_Direction;
2744 }
2745
2746 check_for_circle(reporter, tmp, true, dir);
2747}
2748
2749static void test_circle_mirror_y(skiatest::Reporter* reporter,
2750 const SkPath& path,
2751 SkPath::Direction dir) {
2752 SkPath tmp;
2753 SkMatrix m;
2754 m.reset();
2755 m.setScaleY(-SK_Scalar1);
2756 path.transform(m, &tmp);
2757
2758 if (SkPath::kCW_Direction == dir) {
2759 dir = SkPath::kCCW_Direction;
2760 } else {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002761 REPORTER_ASSERT(reporter, SkPath::kCCW_Direction == dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002762 dir = SkPath::kCW_Direction;
2763 }
2764
2765 check_for_circle(reporter, tmp, true, dir);
2766}
2767
2768static void test_circle_mirror_xy(skiatest::Reporter* reporter,
2769 const SkPath& path,
2770 SkPath::Direction dir) {
2771 SkPath tmp;
2772 SkMatrix m;
2773 m.reset();
2774 m.setScaleX(-SK_Scalar1);
2775 m.setScaleY(-SK_Scalar1);
2776 path.transform(m, &tmp);
2777
2778 check_for_circle(reporter, tmp, true, dir);
2779}
2780
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002781static void test_circle_with_direction(skiatest::Reporter* reporter,
2782 SkPath::Direction dir) {
2783 SkPath path;
2784
2785 // circle at origin
2786 path.addCircle(0, 0, SkIntToScalar(20), dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002787 check_for_circle(reporter, path, true, dir);
2788 test_circle_rotate(reporter, path, dir);
2789 test_circle_translate(reporter, path, dir);
2790 test_circle_skew(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002791
2792 // circle at an offset at (10, 10)
2793 path.reset();
2794 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
2795 SkIntToScalar(20), dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002796 check_for_circle(reporter, path, true, dir);
2797 test_circle_rotate(reporter, path, dir);
2798 test_circle_translate(reporter, path, dir);
2799 test_circle_skew(reporter, path, dir);
2800 test_circle_mirror_x(reporter, path, dir);
2801 test_circle_mirror_y(reporter, path, dir);
2802 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002803}
2804
2805static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
2806 SkPath path;
2807 SkPath circle;
2808 SkPath rect;
2809 SkPath empty;
2810
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002811 static const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
2812 static const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
2813
2814 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002815 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
2816 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
2817
2818 SkMatrix translate;
2819 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
2820
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00002821 // Although all the path concatenation related operations leave
2822 // the path a circle, most mark it as a non-circle for simplicity
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002823
2824 // empty + circle (translate)
2825 path = empty;
2826 path.addPath(circle, translate);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002827 check_for_circle(reporter, path, false, kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002828
2829 // circle + empty (translate)
2830 path = circle;
2831 path.addPath(empty, translate);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00002832 check_for_circle(reporter, path, true, kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002833
2834 // test reverseAddPath
2835 path = circle;
2836 path.reverseAddPath(rect);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002837 check_for_circle(reporter, path, false, kCircleDirOpposite);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002838}
2839
2840static void test_circle(skiatest::Reporter* reporter) {
2841 test_circle_with_direction(reporter, SkPath::kCW_Direction);
2842 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
2843
2844 // multiple addCircle()
2845 SkPath path;
2846 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2847 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002848 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002849
2850 // some extra lineTo() would make isOval() fail
2851 path.reset();
2852 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2853 path.lineTo(0, 0);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002854 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002855
2856 // not back to the original point
2857 path.reset();
2858 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2859 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002860 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002861
2862 test_circle_with_add_paths(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002863
2864 // test negative radius
2865 path.reset();
2866 path.addCircle(0, 0, -1, SkPath::kCW_Direction);
2867 REPORTER_ASSERT(reporter, path.isEmpty());
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002868}
2869
2870static void test_oval(skiatest::Reporter* reporter) {
2871 SkRect rect;
2872 SkMatrix m;
2873 SkPath path;
2874
2875 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
2876 path.addOval(rect);
2877
2878 REPORTER_ASSERT(reporter, path.isOval(NULL));
2879
2880 m.setRotate(SkIntToScalar(90));
2881 SkPath tmp;
2882 path.transform(m, &tmp);
2883 // an oval rotated 90 degrees is still an oval.
2884 REPORTER_ASSERT(reporter, tmp.isOval(NULL));
2885
2886 m.reset();
2887 m.setRotate(SkIntToScalar(30));
2888 tmp.reset();
2889 path.transform(m, &tmp);
2890 // an oval rotated 30 degrees is not an oval anymore.
2891 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2892
2893 // since empty path being transformed.
2894 path.reset();
2895 tmp.reset();
2896 m.reset();
2897 path.transform(m, &tmp);
2898 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2899
2900 // empty path is not an oval
2901 tmp.reset();
2902 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2903
2904 // only has moveTo()s
2905 tmp.reset();
2906 tmp.moveTo(0, 0);
2907 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
2908 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2909
2910 // mimic WebKit's calling convention,
2911 // call moveTo() first and then call addOval()
2912 path.reset();
2913 path.moveTo(0, 0);
2914 path.addOval(rect);
2915 REPORTER_ASSERT(reporter, path.isOval(NULL));
2916
2917 // copy path
2918 path.reset();
2919 tmp.reset();
2920 tmp.addOval(rect);
2921 path = tmp;
2922 REPORTER_ASSERT(reporter, path.isOval(NULL));
2923}
2924
bungeman@google.coma5809a32013-06-21 15:13:34 +00002925static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
2926 SkPath empty;
reed@android.com80e39a72009-04-02 16:59:40 +00002927
reed@android.com3abec1d2009-03-02 05:36:20 +00002928 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002929 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002930 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00002931 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00002932 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00002933 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
2934 REPORTER_ASSERT(reporter, !p.isInverseFillType());
bungeman@google.coma5809a32013-06-21 15:13:34 +00002935 REPORTER_ASSERT(reporter, p == empty);
2936 REPORTER_ASSERT(reporter, !(p != empty));
2937}
2938
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002939static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
2940 SkPath::Direction dir) {
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002941 REPORTER_ASSERT(reporter, path->isConvex());
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002942 REPORTER_ASSERT(reporter, path->cheapIsDirection(dir));
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002943 path->setConvexity(SkPath::kUnknown_Convexity);
2944 REPORTER_ASSERT(reporter, path->isConvex());
2945 path->reset();
2946}
2947
2948static void test_rrect(skiatest::Reporter* reporter) {
2949 SkPath p;
2950 SkRRect rr;
2951 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
2952 SkRect r = {10, 20, 30, 40};
2953 rr.setRectRadii(r, radii);
2954 p.addRRect(rr);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002955 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002956 p.addRRect(rr, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002957 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002958 p.addRoundRect(r, &radii[0].fX);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002959 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002960 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002961 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002962 p.addRoundRect(r, radii[1].fX, radii[1].fY);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002963 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002964 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002965 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
2966 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
2967 SkVector save = radii[i];
2968 radii[i].set(0, 0);
2969 rr.setRectRadii(r, radii);
2970 p.addRRect(rr);
2971 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2972 radii[i] = save;
2973 }
2974 p.addRoundRect(r, 0, 0);
2975 SkRect returnedRect;
2976 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
2977 REPORTER_ASSERT(reporter, returnedRect == r);
2978 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2979 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
2980 rr.setRectRadii(r, zeroRadii);
2981 p.addRRect(rr);
2982 bool closed;
2983 SkPath::Direction dir;
2984 REPORTER_ASSERT(reporter, p.isRect(&closed, &dir));
2985 REPORTER_ASSERT(reporter, closed);
2986 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
2987 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2988 p.addRRect(rr, SkPath::kCW_Direction);
2989 p.addRRect(rr, SkPath::kCW_Direction);
2990 REPORTER_ASSERT(reporter, !p.isConvex());
2991 p.reset();
2992 p.addRRect(rr, SkPath::kCCW_Direction);
2993 p.addRRect(rr, SkPath::kCCW_Direction);
2994 REPORTER_ASSERT(reporter, !p.isConvex());
2995 p.reset();
2996 SkRect emptyR = {10, 20, 10, 30};
2997 rr.setRectRadii(emptyR, radii);
2998 p.addRRect(rr);
2999 REPORTER_ASSERT(reporter, p.isEmpty());
3000 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
3001 rr.setRectRadii(largeR, radii);
3002 p.addRRect(rr);
3003 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3004 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
3005 rr.setRectRadii(infR, radii);
3006 p.addRRect(rr);
3007 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3008 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
3009 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
3010 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003011}
3012
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003013static void test_arc(skiatest::Reporter* reporter) {
3014 SkPath p;
3015 SkRect emptyOval = {10, 20, 30, 20};
3016 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3017 p.addArc(emptyOval, 1, 2);
3018 REPORTER_ASSERT(reporter, p.isEmpty());
3019 p.reset();
3020 SkRect oval = {10, 20, 30, 40};
3021 p.addArc(oval, 1, 0);
3022 REPORTER_ASSERT(reporter, p.isEmpty());
3023 p.reset();
3024 SkPath cwOval;
3025 cwOval.addOval(oval);
3026 p.addArc(oval, 1, 360);
3027 REPORTER_ASSERT(reporter, p == cwOval);
3028 p.reset();
3029 SkPath ccwOval;
3030 ccwOval.addOval(oval, SkPath::kCCW_Direction);
3031 p.addArc(oval, 1, -360);
3032 REPORTER_ASSERT(reporter, p == ccwOval);
3033 p.reset();
3034 p.addArc(oval, 1, 180);
3035 REPORTER_ASSERT(reporter, p.isConvex());
3036 REPORTER_ASSERT(reporter, p.cheapIsDirection(SkPath::kCW_Direction));
3037 p.setConvexity(SkPath::kUnknown_Convexity);
3038 REPORTER_ASSERT(reporter, p.isConvex());
3039}
3040
3041static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3042 SkScalar x0, SkScalar y0) {
3043 SkPoint pts[4];
3044 SkPath::Verb v = iter->next(pts);
3045 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
3046 REPORTER_ASSERT(reporter, pts[0].fX == x0);
3047 REPORTER_ASSERT(reporter, pts[0].fY == y0);
3048}
3049
3050static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3051 SkScalar x1, SkScalar y1) {
3052 SkPoint pts[4];
3053 SkPath::Verb v = iter->next(pts);
3054 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
3055 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3056 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3057}
3058
3059static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3060 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3061 SkPoint pts[4];
3062 SkPath::Verb v = iter->next(pts);
3063 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
3064 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3065 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3066 REPORTER_ASSERT(reporter, pts[2].fX == x2);
3067 REPORTER_ASSERT(reporter, pts[2].fY == y2);
3068}
3069
3070static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3071 SkPoint pts[4];
3072 SkPath::Verb v = iter->next(pts);
3073 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
3074}
3075
3076static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3077 check_done(reporter, p, iter);
3078 p->reset();
3079}
3080
3081static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3082 SkScalar x0, SkScalar y0) {
3083 SkPath::RawIter iter(*p);
3084 check_move(reporter, &iter, x0, y0);
3085 check_done_and_reset(reporter, p, &iter);
3086}
3087
3088static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3089 SkScalar x1, SkScalar y1) {
3090 SkPath::RawIter iter(*p);
3091 check_move(reporter, &iter, 0, 0);
3092 check_line(reporter, &iter, x1, y1);
3093 check_done_and_reset(reporter, p, &iter);
3094}
3095
3096static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3097 SkScalar x1, SkScalar y1) {
3098 SkPath::RawIter iter(*p);
3099 check_move(reporter, &iter, 0, 0);
3100 check_line(reporter, &iter, x1, y1);
3101 check_done(reporter, p, &iter);
3102}
3103
3104static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3105 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3106 SkPath::RawIter iter(*p);
3107 check_move(reporter, &iter, 0, 0);
3108 check_line(reporter, &iter, x1, y1);
3109 check_line(reporter, &iter, x2, y2);
3110 check_done_and_reset(reporter, p, &iter);
3111}
3112
3113static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3114 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3115 SkPath::RawIter iter(*p);
3116 check_move(reporter, &iter, 0, 0);
3117 check_quad(reporter, &iter, x1, y1, x2, y2);
3118 check_done_and_reset(reporter, p, &iter);
3119}
3120
3121static void test_arcTo(skiatest::Reporter* reporter) {
3122 SkPath p;
3123 p.arcTo(0, 0, 1, 2, 1);
3124 check_path_is_line_and_reset(reporter, &p, 0, 0);
3125 p.arcTo(1, 2, 1, 2, 1);
3126 check_path_is_line_and_reset(reporter, &p, 1, 2);
3127 p.arcTo(1, 2, 3, 4, 0);
3128 check_path_is_line_and_reset(reporter, &p, 1, 2);
3129 p.arcTo(1, 2, 0, 0, 1);
3130 check_path_is_line_and_reset(reporter, &p, 1, 2);
3131 p.arcTo(1, 0, 1, 1, 1);
3132 SkPoint pt;
3133 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3134 p.reset();
3135 p.arcTo(1, 0, 1, -1, 1);
3136 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3137 p.reset();
3138 SkRect oval = {1, 2, 3, 4};
3139 p.arcTo(oval, 0, 0, true);
3140 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3141 p.arcTo(oval, 0, 0, false);
3142 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3143 p.arcTo(oval, 360, 0, true);
3144 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3145 p.arcTo(oval, 360, 0, false);
3146 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3147 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003148 p.arcTo(oval, 0, sweep, false);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003149 REPORTER_ASSERT(reporter, p.getBounds() == oval);
3150 sweep += delta;
3151 delta /= 2;
3152 }
3153 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003154 p.arcTo(oval, 0, sweep, false);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003155 REPORTER_ASSERT(reporter, p.getBounds() == oval);
3156 sweep -= delta;
3157 delta /= 2;
3158 }
3159 SkRect noOvalWidth = {1, 2, 0, 3};
3160 p.reset();
3161 p.arcTo(noOvalWidth, 0, 360, false);
3162 REPORTER_ASSERT(reporter, p.isEmpty());
3163
3164 SkRect noOvalHeight = {1, 2, 3, 1};
3165 p.reset();
3166 p.arcTo(noOvalHeight, 0, 360, false);
3167 REPORTER_ASSERT(reporter, p.isEmpty());
3168}
3169
3170static void test_addPath(skiatest::Reporter* reporter) {
3171 SkPath p, q;
3172 p.lineTo(1, 2);
3173 q.moveTo(4, 4);
3174 q.lineTo(7, 8);
3175 q.conicTo(8, 7, 6, 5, 0.5f);
3176 q.quadTo(6, 7, 8, 6);
3177 q.cubicTo(5, 6, 7, 8, 7, 5);
3178 q.close();
3179 p.addPath(q, -4, -4);
3180 SkRect expected = {0, 0, 4, 4};
3181 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3182 p.reset();
3183 p.reverseAddPath(q);
3184 SkRect reverseExpected = {4, 4, 8, 8};
3185 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3186}
3187
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003188static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3189 SkPath p, q;
3190 if (explicitMoveTo) {
3191 p.moveTo(1, 1);
3192 }
3193 p.lineTo(1, 2);
3194 if (explicitMoveTo) {
3195 q.moveTo(2, 1);
3196 }
3197 q.lineTo(2, 2);
3198 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3199 uint8_t verbs[4];
3200 int verbcount = p.getVerbs(verbs, 4);
3201 REPORTER_ASSERT(reporter, verbcount == 4);
3202 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3203 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3204 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3205 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3206}
3207
3208static void test_extendClosedPath(skiatest::Reporter* reporter) {
3209 SkPath p, q;
3210 p.moveTo(1, 1);
3211 p.lineTo(1, 2);
3212 p.lineTo(2, 2);
3213 p.close();
3214 q.moveTo(2, 1);
3215 q.lineTo(2, 3);
3216 p.addPath(q, SkPath::kExtend_AddPathMode);
3217 uint8_t verbs[7];
3218 int verbcount = p.getVerbs(verbs, 7);
3219 REPORTER_ASSERT(reporter, verbcount == 7);
3220 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3221 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3222 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3223 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3224 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3225 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3226 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3227
3228 SkPoint pt;
3229 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3230 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3231 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3232}
3233
3234static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3235 SkPath p, q, r;
3236 // case 1: dst is empty
3237 p.moveTo(2, 1);
3238 p.lineTo(2, 3);
3239 q.addPath(p, mode);
3240 REPORTER_ASSERT(reporter, q == p);
3241 // case 2: src is empty
3242 p.addPath(r, mode);
3243 REPORTER_ASSERT(reporter, q == p);
3244 // case 3: src and dst are empty
3245 q.reset();
3246 q.addPath(r, mode);
3247 REPORTER_ASSERT(reporter, q.isEmpty());
skia.committer@gmail.com877c4492014-02-12 03:02:04 +00003248}
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003249
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003250static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3251 SkPath p;
3252 p.conicTo(1, 2, 3, 4, -1);
3253 check_path_is_line_and_reset(reporter, &p, 3, 4);
3254 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3255 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
3256 p.conicTo(1, 2, 3, 4, 1);
3257 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
3258}
3259
3260static void test_get_point(skiatest::Reporter* reporter) {
3261 SkPath p;
3262 SkPoint pt = p.getPoint(0);
3263 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
3264 REPORTER_ASSERT(reporter, !p.getLastPt(NULL));
3265 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
3266 p.setLastPt(10, 10);
3267 pt = p.getPoint(0);
3268 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
3269 REPORTER_ASSERT(reporter, p.getLastPt(NULL));
3270 p.rMoveTo(10, 10);
3271 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
3272}
3273
3274static void test_contains(skiatest::Reporter* reporter) {
3275 SkPath p;
3276 p.setFillType(SkPath::kInverseWinding_FillType);
3277 REPORTER_ASSERT(reporter, p.contains(0, 0));
3278 p.setFillType(SkPath::kWinding_FillType);
3279 REPORTER_ASSERT(reporter, !p.contains(0, 0));
3280 p.moveTo(4, 4);
3281 p.lineTo(6, 8);
3282 p.lineTo(8, 4);
3283 // test quick reject
3284 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3285 REPORTER_ASSERT(reporter, !p.contains(0, 4));
3286 REPORTER_ASSERT(reporter, !p.contains(4, 10));
3287 REPORTER_ASSERT(reporter, !p.contains(10, 4));
3288 // test various crossings in x
3289 REPORTER_ASSERT(reporter, !p.contains(5, 7));
3290 REPORTER_ASSERT(reporter, p.contains(6, 7));
3291 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3292 p.reset();
3293 p.moveTo(4, 4);
3294 p.lineTo(8, 6);
3295 p.lineTo(4, 8);
3296 // test various crossings in y
3297 REPORTER_ASSERT(reporter, !p.contains(7, 5));
3298 REPORTER_ASSERT(reporter, p.contains(7, 6));
3299 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3300 // test quads
3301 p.reset();
3302 p.moveTo(4, 4);
3303 p.quadTo(6, 6, 8, 8);
3304 p.quadTo(6, 8, 4, 8);
3305 p.quadTo(4, 6, 4, 4);
3306 REPORTER_ASSERT(reporter, p.contains(5, 6));
3307 REPORTER_ASSERT(reporter, !p.contains(6, 5));
3308
3309 p.reset();
3310 p.moveTo(6, 6);
3311 p.quadTo(8, 8, 6, 8);
3312 p.quadTo(4, 8, 4, 6);
3313 p.quadTo(4, 4, 6, 6);
3314 REPORTER_ASSERT(reporter, p.contains(5, 6));
3315 REPORTER_ASSERT(reporter, !p.contains(6, 5));
3316
3317#define CONIC_CONTAINS_BUG_FIXED 0
3318#if CONIC_CONTAINS_BUG_FIXED
3319 p.reset();
3320 p.moveTo(4, 4);
3321 p.conicTo(6, 6, 8, 8, 0.5f);
3322 p.conicTo(6, 8, 4, 8, 0.5f);
3323 p.conicTo(4, 6, 4, 4, 0.5f);
3324 REPORTER_ASSERT(reporter, p.contains(5, 6));
3325 REPORTER_ASSERT(reporter, !p.contains(6, 5));
3326#endif
3327
3328 // test cubics
3329 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
3330 for (int i = 0; i < 3; ++i) {
3331 p.reset();
3332 p.setFillType(SkPath::kEvenOdd_FillType);
3333 p.moveTo(pts[i].fX, pts[i].fY);
3334 p.cubicTo(pts[i + 1].fX, pts[i + 1].fY, pts[i + 2].fX, pts[i + 2].fY, pts[i + 3].fX, pts[i + 3].fY);
3335 p.cubicTo(pts[i + 4].fX, pts[i + 4].fY, pts[i + 5].fX, pts[i + 5].fY, pts[i + 6].fX, pts[i + 6].fY);
3336 p.close();
3337 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
3338 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
3339 }
3340}
3341
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003342class PathRefTest_Private {
3343public:
3344 static void TestPathRef(skiatest::Reporter* reporter) {
3345 static const int kRepeatCnt = 10;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003346
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003347 SkAutoTUnref<SkPathRef> pathRef(SkNEW(SkPathRef));
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003348
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003349 SkPathRef::Editor ed(&pathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003350
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003351 {
3352 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
3353 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3354 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
3355 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
3356 for (int i = 0; i < kRepeatCnt; ++i) {
3357 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
3358 }
3359 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003360 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003361
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003362 {
3363 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
3364 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3365 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
3366 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
3367 for (int i = 0; i < kRepeatCnt; ++i) {
3368 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
3369 }
3370 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003371 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003372
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003373 {
3374 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
3375 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3376 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
3377 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
3378 for (int i = 0; i < kRepeatCnt; ++i) {
3379 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
3380 }
3381 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003382 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003383
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003384 {
3385 SkScalar* weights = NULL;
3386 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
3387 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3388 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
3389 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
3390 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
bsalomon49f085d2014-09-05 13:34:00 -07003391 REPORTER_ASSERT(reporter, weights);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003392 for (int i = 0; i < kRepeatCnt; ++i) {
3393 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
3394 }
3395 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003396 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003397
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003398 {
3399 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
3400 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3401 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
3402 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
3403 for (int i = 0; i < kRepeatCnt; ++i) {
3404 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
3405 }
3406 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003407 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003408 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003409};
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003410
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003411static void test_operatorEqual(skiatest::Reporter* reporter) {
3412 SkPath a;
3413 SkPath b;
3414 REPORTER_ASSERT(reporter, a == a);
3415 REPORTER_ASSERT(reporter, a == b);
3416 a.setFillType(SkPath::kInverseWinding_FillType);
3417 REPORTER_ASSERT(reporter, a != b);
3418 a.reset();
3419 REPORTER_ASSERT(reporter, a == b);
3420 a.lineTo(1, 1);
3421 REPORTER_ASSERT(reporter, a != b);
3422 a.reset();
3423 REPORTER_ASSERT(reporter, a == b);
3424 a.lineTo(1, 1);
3425 b.lineTo(1, 2);
3426 REPORTER_ASSERT(reporter, a != b);
3427 a.reset();
3428 a.lineTo(1, 2);
3429 REPORTER_ASSERT(reporter, a == b);
3430}
3431
caryclark66a5d8b2014-06-24 08:30:15 -07003432static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force,
caryclarke9562592014-09-15 09:26:09 -07003433 bool dumpAsHex, const char* str) {
caryclark66a5d8b2014-06-24 08:30:15 -07003434 SkDynamicMemoryWStream wStream;
caryclarke9562592014-09-15 09:26:09 -07003435 path.dump(&wStream, force, dumpAsHex);
caryclark66a5d8b2014-06-24 08:30:15 -07003436 SkAutoDataUnref data(wStream.copyToData());
3437 REPORTER_ASSERT(reporter, data->size() == strlen(str));
3438 REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
3439}
3440
3441static void test_dump(skiatest::Reporter* reporter) {
3442 SkPath p;
caryclarke9562592014-09-15 09:26:09 -07003443 compare_dump(reporter, p, false, false, "");
3444 compare_dump(reporter, p, true, false, "");
caryclark66a5d8b2014-06-24 08:30:15 -07003445 p.moveTo(1, 2);
3446 p.lineTo(3, 4);
caryclarke9562592014-09-15 09:26:09 -07003447 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3448 "path.lineTo(3, 4);\n");
3449 compare_dump(reporter, p, true, false, "path.moveTo(1, 2);\n"
3450 "path.lineTo(3, 4);\n"
3451 "path.lineTo(1, 2);\n"
3452 "path.close();\n");
caryclark66a5d8b2014-06-24 08:30:15 -07003453 p.reset();
3454 p.moveTo(1, 2);
3455 p.quadTo(3, 4, 5, 6);
caryclarke9562592014-09-15 09:26:09 -07003456 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3457 "path.quadTo(3, 4, 5, 6);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07003458 p.reset();
3459 p.moveTo(1, 2);
3460 p.conicTo(3, 4, 5, 6, 0.5f);
caryclarke9562592014-09-15 09:26:09 -07003461 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3462 "path.conicTo(3, 4, 5, 6, 0.5f);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07003463 p.reset();
3464 p.moveTo(1, 2);
3465 p.cubicTo(3, 4, 5, 6, 7, 8);
caryclarke9562592014-09-15 09:26:09 -07003466 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3467 "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
3468 p.reset();
3469 p.moveTo(1, 2);
3470 p.lineTo(3, 4);
3471 compare_dump(reporter, p, false, true, "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));\n"
3472 "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));\n");
3473 p.reset();
3474 p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
3475 p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
3476 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3477 "path.lineTo(3, 4);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07003478}
3479
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003480class PathTest_Private {
3481public:
3482 static void TestPathTo(skiatest::Reporter* reporter) {
3483 SkPath p, q;
3484 p.lineTo(4, 4);
3485 p.reversePathTo(q);
3486 check_path_is_line(reporter, &p, 4, 4);
3487 q.moveTo(-4, -4);
3488 p.reversePathTo(q);
3489 check_path_is_line(reporter, &p, 4, 4);
3490 q.lineTo(7, 8);
3491 q.conicTo(8, 7, 6, 5, 0.5f);
3492 q.quadTo(6, 7, 8, 6);
3493 q.cubicTo(5, 6, 7, 8, 7, 5);
3494 q.close();
3495 p.reversePathTo(q);
3496 SkRect reverseExpected = {-4, -4, 8, 8};
3497 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3498 }
3499};
3500
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00003501DEF_TEST(Paths, reporter) {
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +00003502 test_path_crbug364224();
3503
bungeman@google.coma5809a32013-06-21 15:13:34 +00003504 SkTSize<SkScalar>::Make(3,4);
3505
3506 SkPath p, empty;
3507 SkRect bounds, bounds2;
3508 test_empty(reporter, p);
reed@android.com3abec1d2009-03-02 05:36:20 +00003509
reed@android.comd252db02009-04-01 18:31:44 +00003510 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00003511
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003512 // this triggers a code path in SkPath::operator= which is otherwise unexercised
3513 SkPath& self = p;
3514 p = self;
3515
3516 // this triggers a code path in SkPath::swap which is otherwise unexercised
3517 p.swap(self);
3518
reed@android.com3abec1d2009-03-02 05:36:20 +00003519 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00003520
reed@android.com6b82d1a2009-06-03 02:35:01 +00003521 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
3522 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00003523 // we have quads or cubics
3524 REPORTER_ASSERT(reporter, p.getSegmentMasks() & kCurveSegmentMask);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003525 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00003526
reed@android.com6b82d1a2009-06-03 02:35:01 +00003527 p.reset();
bungeman@google.coma5809a32013-06-21 15:13:34 +00003528 test_empty(reporter, p);
reed@google.com10296cc2011-09-21 12:29:05 +00003529
reed@android.com6b82d1a2009-06-03 02:35:01 +00003530 p.addOval(bounds);
3531 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003532 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00003533
bungeman@google.coma5809a32013-06-21 15:13:34 +00003534 p.rewind();
3535 test_empty(reporter, p);
3536
reed@android.com3abec1d2009-03-02 05:36:20 +00003537 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00003538 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00003539 // we have only lines
3540 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003541 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00003542
bungeman@google.coma5809a32013-06-21 15:13:34 +00003543 REPORTER_ASSERT(reporter, p != empty);
3544 REPORTER_ASSERT(reporter, !(p == empty));
reed@android.com3abec1d2009-03-02 05:36:20 +00003545
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003546 // do getPoints and getVerbs return the right result
3547 REPORTER_ASSERT(reporter, p.getPoints(NULL, 0) == 4);
3548 REPORTER_ASSERT(reporter, p.getVerbs(NULL, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00003549 SkPoint pts[4];
3550 int count = p.getPoints(pts, 4);
3551 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003552 uint8_t verbs[6];
3553 verbs[5] = 0xff;
3554 p.getVerbs(verbs, 5);
3555 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
3556 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
3557 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
3558 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
3559 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
3560 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00003561 bounds2.set(pts, 4);
3562 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00003563
reed@android.com3abec1d2009-03-02 05:36:20 +00003564 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
3565 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00003566 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00003567
reed@android.com3abec1d2009-03-02 05:36:20 +00003568 REPORTER_ASSERT(reporter, p.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00003569 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00003570 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
3571 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00003572
reed@android.com3abec1d2009-03-02 05:36:20 +00003573 // now force p to not be a rect
3574 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
3575 p.addRect(bounds);
3576 REPORTER_ASSERT(reporter, !p.isRect(NULL));
reed@android.com3abec1d2009-03-02 05:36:20 +00003577
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003578 test_operatorEqual(reporter);
reed@google.com7e6c4d12012-05-10 14:05:43 +00003579 test_isLine(reporter);
3580 test_isRect(reporter);
caryclark@google.com56f233a2012-11-19 13:06:06 +00003581 test_isNestedRects(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003582 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00003583 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00003584 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00003585 test_convexity2(reporter);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00003586 test_conservativelyContains(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00003587 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003588 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00003589 test_flattening(reporter);
3590 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00003591 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003592 test_iter(reporter);
3593 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003594 test_circle(reporter);
3595 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00003596 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00003597 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00003598 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00003599 test_isfinite_after_transform(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00003600 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00003601 test_arb_zero_rad_round_rect_is_rect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003602 test_addrect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00003603 test_addrect_isfinite(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00003604 test_tricky_cubic();
3605 test_clipped_cubic();
3606 test_crbug_170666();
reed@google.com7a90daf2013-04-10 18:44:00 +00003607 test_bad_cubic_crbug229478();
reed@google.com3eff3592013-05-08 21:08:21 +00003608 test_bad_cubic_crbug234190();
mtklein@google.com9c9d4a72013-08-07 19:17:53 +00003609 test_android_specific_behavior(reporter);
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +00003610 test_gen_id(reporter);
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +00003611 test_path_close_issue1474(reporter);
reed@google.comb58ba892013-10-15 15:44:35 +00003612 test_path_to_region(reporter);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003613 test_rrect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003614 test_arc(reporter);
3615 test_arcTo(reporter);
3616 test_addPath(reporter);
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003617 test_addPathMode(reporter, false, false);
3618 test_addPathMode(reporter, true, false);
3619 test_addPathMode(reporter, false, true);
3620 test_addPathMode(reporter, true, true);
3621 test_extendClosedPath(reporter);
3622 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
3623 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003624 test_conicTo_special_case(reporter);
3625 test_get_point(reporter);
3626 test_contains(reporter);
3627 PathTest_Private::TestPathTo(reporter);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003628 PathRefTest_Private::TestPathRef(reporter);
caryclark66a5d8b2014-06-24 08:30:15 -07003629 test_dump(reporter);
piotaixrfac4e0e2014-08-26 11:59:04 -07003630 test_path_crbugskia2820(reporter);
reed@android.com3abec1d2009-03-02 05:36:20 +00003631}