blob: 57587c4a35f9258a74abbb67e24794667c3539cd [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);
reed@google.com7c424812011-05-15 04:38:34 +00001198}
1199
reed@android.com6b82d1a2009-06-03 02:35:01 +00001200static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1201 const SkRect& bounds) {
1202 REPORTER_ASSERT(reporter, p.isConvex());
1203 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +00001204
reed@android.com6b82d1a2009-06-03 02:35:01 +00001205 SkPath p2(p);
1206 REPORTER_ASSERT(reporter, p2.isConvex());
1207 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1208
1209 SkPath other;
1210 other.swap(p2);
1211 REPORTER_ASSERT(reporter, other.isConvex());
1212 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1213}
1214
reed@google.com04863fa2011-05-15 04:08:24 +00001215static void setFromString(SkPath* path, const char str[]) {
1216 bool first = true;
1217 while (str) {
1218 SkScalar x, y;
1219 str = SkParse::FindScalar(str, &x);
1220 if (NULL == str) {
1221 break;
1222 }
1223 str = SkParse::FindScalar(str, &y);
1224 SkASSERT(str);
1225 if (first) {
1226 path->moveTo(x, y);
1227 first = false;
1228 } else {
1229 path->lineTo(x, y);
1230 }
1231 }
1232}
1233
1234static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +00001235 SkPath path;
1236
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001237 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001238 path.addCircle(0, 0, SkIntToScalar(10));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001239 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001240 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001241 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1242
reed@google.com04863fa2011-05-15 04:08:24 +00001243 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001244 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001245 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.com3e71a882012-01-10 18:44:37 +00001246 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCCW_Direction));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001247
reed@google.com04863fa2011-05-15 04:08:24 +00001248 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001249 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001250 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.com3e71a882012-01-10 18:44:37 +00001251 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCW_Direction));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001252
reed@google.com04863fa2011-05-15 04:08:24 +00001253 static const struct {
1254 const char* fPathStr;
1255 SkPath::Convexity fExpectedConvexity;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001256 SkPath::Direction fExpectedDirection;
reed@google.com04863fa2011-05-15 04:08:24 +00001257 } gRec[] = {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001258 { "", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1259 { "0 0", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1260 { "0 0 10 10", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1261 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPath::kUnknown_Direction },
1262 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPath::kCW_Direction },
1263 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPath::kCCW_Direction },
1264 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
1265 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPath::kCW_Direction },
reed@google.com04863fa2011-05-15 04:08:24 +00001266 };
1267
1268 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1269 SkPath path;
1270 setFromString(&path, gRec[i].fPathStr);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001271 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1272 check_direction(reporter, path, gRec[i].fExpectedDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001273 // check after setting the initial convex and direction
1274 if (kDontCheckDir != gRec[i].fExpectedDirection) {
1275 SkPath copy(path);
1276 SkPath::Direction dir;
1277 bool foundDir = copy.cheapComputeDirection(&dir);
1278 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPath::kUnknown_Direction)
1279 ^ foundDir);
1280 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1281 check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
1282 }
1283 REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
1284 check_direction(reporter, path, gRec[i].fExpectedDirection);
reed@google.com04863fa2011-05-15 04:08:24 +00001285 }
1286}
1287
reed@google.com7e6c4d12012-05-10 14:05:43 +00001288static void test_isLine(skiatest::Reporter* reporter) {
1289 SkPath path;
1290 SkPoint pts[2];
1291 const SkScalar value = SkIntToScalar(5);
1292
1293 REPORTER_ASSERT(reporter, !path.isLine(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001294
reed@google.com7e6c4d12012-05-10 14:05:43 +00001295 // set some non-zero values
1296 pts[0].set(value, value);
1297 pts[1].set(value, value);
1298 REPORTER_ASSERT(reporter, !path.isLine(pts));
1299 // check that pts was untouched
1300 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1301 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1302
1303 const SkScalar moveX = SkIntToScalar(1);
1304 const SkScalar moveY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001305 REPORTER_ASSERT(reporter, value != moveX && value != moveY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001306
1307 path.moveTo(moveX, moveY);
1308 REPORTER_ASSERT(reporter, !path.isLine(NULL));
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 lineX = SkIntToScalar(2);
1315 const SkScalar lineY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001316 REPORTER_ASSERT(reporter, value != lineX && value != lineY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001317
1318 path.lineTo(lineX, lineY);
1319 REPORTER_ASSERT(reporter, path.isLine(NULL));
1320
1321 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1322 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1323 REPORTER_ASSERT(reporter, path.isLine(pts));
1324 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1325 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1326
1327 path.lineTo(0, 0); // too many points/verbs
1328 REPORTER_ASSERT(reporter, !path.isLine(NULL));
1329 REPORTER_ASSERT(reporter, !path.isLine(pts));
1330 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1331 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001332
1333 path.reset();
1334 path.quadTo(1, 1, 2, 2);
1335 REPORTER_ASSERT(reporter, !path.isLine(NULL));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001336}
1337
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001338static void test_conservativelyContains(skiatest::Reporter* reporter) {
1339 SkPath path;
1340
1341 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1342 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1343
1344 // A circle that bounds kBaseRect (with a significant amount of slop)
1345 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001346 circleR = SkScalarMul(circleR, 1.75f) / 2;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001347 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1348
1349 // round-rect radii
1350 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
skia.committer@gmail.comcec8de62012-11-14 02:01:22 +00001351
caryclark@google.com56f233a2012-11-19 13:06:06 +00001352 static const struct SUPPRESS_VISIBILITY_WARNING {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001353 SkRect fQueryRect;
1354 bool fInRect;
1355 bool fInCircle;
1356 bool fInRR;
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001357 bool fInCubicRR;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001358 } kQueries[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001359 {kBaseRect, true, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001360
1361 // rect well inside of kBaseRect
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001362 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1363 kBaseRect.fTop + 0.25f*kBaseRect.height(),
1364 kBaseRect.fRight - 0.25f*kBaseRect.width(),
1365 kBaseRect.fBottom - 0.25f*kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001366 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001367
1368 // rects with edges off by one from kBaseRect's edges
1369 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1370 kBaseRect.width(), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001371 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001372 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1373 kBaseRect.width() + 1, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001374 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001375 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1376 kBaseRect.width() + 1, kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001377 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001378 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1379 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001380 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001381 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1382 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001383 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001384 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1385 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001386 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001387 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1388 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001389 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001390
1391 // zero-w/h rects at each corner of kBaseRect
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001392 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1393 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1394 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1395 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001396
1397 // far away rect
1398 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1399 SkIntToScalar(10), SkIntToScalar(10)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001400 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001401
1402 // very large rect containing kBaseRect
1403 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1404 kBaseRect.fTop - 5 * kBaseRect.height(),
1405 11 * kBaseRect.width(), 11 * kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001406 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001407
1408 // skinny rect that spans same y-range as kBaseRect
1409 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1410 SkIntToScalar(1), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001411 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001412
1413 // short rect that spans same x-range as kBaseRect
1414 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001415 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001416
1417 // skinny rect that spans slightly larger y-range than kBaseRect
1418 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1419 SkIntToScalar(1), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001420 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001421
1422 // short rect that spans slightly larger x-range than kBaseRect
1423 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1424 kBaseRect.width() + 1, SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001425 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001426 };
1427
1428 for (int inv = 0; inv < 4; ++inv) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001429 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001430 SkRect qRect = kQueries[q].fQueryRect;
1431 if (inv & 0x1) {
1432 SkTSwap(qRect.fLeft, qRect.fRight);
1433 }
1434 if (inv & 0x2) {
1435 SkTSwap(qRect.fTop, qRect.fBottom);
1436 }
1437 for (int d = 0; d < 2; ++d) {
1438 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1439 path.reset();
1440 path.addRect(kBaseRect, dir);
1441 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1442 path.conservativelyContainsRect(qRect));
1443
1444 path.reset();
1445 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1446 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1447 path.conservativelyContainsRect(qRect));
1448
1449 path.reset();
1450 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1451 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1452 path.conservativelyContainsRect(qRect));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001453
1454 path.reset();
1455 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1456 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1457 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1458 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1459 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1460 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1461 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1462 path.close();
1463 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1464 path.conservativelyContainsRect(qRect));
1465
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001466 }
1467 // Slightly non-convex shape, shouldn't contain any rects.
1468 path.reset();
1469 path.moveTo(0, 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001470 path.lineTo(SkIntToScalar(50), 0.05f);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001471 path.lineTo(SkIntToScalar(100), 0);
1472 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1473 path.lineTo(0, SkIntToScalar(100));
1474 path.close();
1475 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1476 }
1477 }
1478
1479 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1480 path.reset();
1481 path.moveTo(0, 0);
1482 path.lineTo(SkIntToScalar(100), 0);
1483 path.lineTo(0, SkIntToScalar(100));
1484
1485 // inside, on along top edge
1486 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1487 SkIntToScalar(10),
1488 SkIntToScalar(10))));
1489 // above
1490 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1491 SkRect::MakeXYWH(SkIntToScalar(50),
1492 SkIntToScalar(-10),
1493 SkIntToScalar(10),
1494 SkIntToScalar(10))));
1495 // to the left
1496 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1497 SkIntToScalar(5),
1498 SkIntToScalar(5),
1499 SkIntToScalar(5))));
1500
1501 // outside the diagonal edge
1502 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1503 SkIntToScalar(200),
1504 SkIntToScalar(20),
1505 SkIntToScalar(5))));
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001506
bsalomonb17c1292014-08-28 14:04:55 -07001507
1508 // Test that multiple move commands do not cause asserts.
1509
1510 // At the time of writing, this would not modify cached convexity. This caused an assert while
1511 // checking conservative containment again. http://skbug.com/1460
1512 path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
1513#if 0
1514 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1515 SkIntToScalar(10),
1516 SkIntToScalar(10))));
1517#endif
1518
1519 // Same as above path and first test but with an extra moveTo.
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001520 path.reset();
1521 path.moveTo(100, 100);
1522 path.moveTo(0, 0);
1523 path.lineTo(SkIntToScalar(100), 0);
1524 path.lineTo(0, SkIntToScalar(100));
1525
1526 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1527 SkIntToScalar(10),
1528 SkIntToScalar(10))));
1529
bsalomonb17c1292014-08-28 14:04:55 -07001530 // Test that multiple move commands do not cause asserts and that the function
1531 // is not confused by the multiple moves.
1532 path.reset();
1533 path.moveTo(0, 0);
1534 path.lineTo(SkIntToScalar(100), 0);
1535 path.lineTo(0, SkIntToScalar(100));
1536 path.moveTo(0, SkIntToScalar(200));
1537 path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
1538 path.lineTo(0, SkIntToScalar(300));
1539
1540 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1541 SkRect::MakeXYWH(SkIntToScalar(50), 0,
1542 SkIntToScalar(10),
1543 SkIntToScalar(10))));
1544
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001545 path.reset();
1546 path.lineTo(100, 100);
1547 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001548}
1549
reed@google.comf32322b2013-10-16 15:14:04 +00001550static void test_isRect_open_close(skiatest::Reporter* reporter) {
1551 SkPath path;
1552 bool isClosed;
1553
1554 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
reed@google.comf32322b2013-10-16 15:14:04 +00001555 path.close();
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001556
reed@google.comf32322b2013-10-16 15:14:04 +00001557 REPORTER_ASSERT(reporter, path.isRect(NULL, NULL));
1558 REPORTER_ASSERT(reporter, path.isRect(&isClosed, NULL));
1559 REPORTER_ASSERT(reporter, isClosed);
commit-bot@chromium.org7e90e8d2014-02-11 01:38:30 +00001560 REPORTER_ASSERT(reporter, SkPath::kStroke_PathAsRect == path.asRect(NULL));
reed@google.comf32322b2013-10-16 15:14:04 +00001561}
1562
caryclark@google.comf1316942011-07-26 19:54:45 +00001563// Simple isRect test is inline TestPath, below.
1564// test_isRect provides more extensive testing.
1565static void test_isRect(skiatest::Reporter* reporter) {
reed@google.comf32322b2013-10-16 15:14:04 +00001566 test_isRect_open_close(reporter);
1567
caryclark@google.comf1316942011-07-26 19:54:45 +00001568 // passing tests (all moveTo / lineTo...
1569 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1570 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1571 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1572 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1573 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1574 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1575 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1576 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1577 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001578 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1579 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 +00001580 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1581 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1582 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
caryclark@google.combfe90372012-11-21 13:56:20 +00001583 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
rmistry@google.comd6176b02012-08-23 18:14:13 +00001584
caryclark@google.comf1316942011-07-26 19:54:45 +00001585 // failing tests
1586 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1587 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1588 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1589 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1590 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1591 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1592 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1593 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
caryclark@google.combfe90372012-11-21 13:56:20 +00001594 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
1595 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
1596 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
rmistry@google.comd6176b02012-08-23 18:14:13 +00001597
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001598 // no close, but we should detect them as fillably the same as a rect
1599 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1600 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
1601 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
1602
1603 // like c2, but we double-back on ourselves
1604 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
1605 // like c2, but we overshoot the start point
1606 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
1607 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
caryclark@google.comf1316942011-07-26 19:54:45 +00001608
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001609 struct IsRectTest {
1610 SkPoint *fPoints;
1611 size_t fPointCount;
1612 bool fClose;
1613 bool fIsRect;
1614 } tests[] = {
1615 { r1, SK_ARRAY_COUNT(r1), true, true },
1616 { r2, SK_ARRAY_COUNT(r2), true, true },
1617 { r3, SK_ARRAY_COUNT(r3), true, true },
1618 { r4, SK_ARRAY_COUNT(r4), true, true },
1619 { r5, SK_ARRAY_COUNT(r5), true, true },
1620 { r6, SK_ARRAY_COUNT(r6), true, true },
1621 { r7, SK_ARRAY_COUNT(r7), true, true },
1622 { r8, SK_ARRAY_COUNT(r8), true, true },
1623 { r9, SK_ARRAY_COUNT(r9), true, true },
1624 { ra, SK_ARRAY_COUNT(ra), true, true },
1625 { rb, SK_ARRAY_COUNT(rb), true, true },
1626 { rc, SK_ARRAY_COUNT(rc), true, true },
1627 { rd, SK_ARRAY_COUNT(rd), true, true },
1628 { re, SK_ARRAY_COUNT(re), true, true },
1629 { rf, SK_ARRAY_COUNT(rf), true, true },
1630
1631 { f1, SK_ARRAY_COUNT(f1), true, false },
1632 { f2, SK_ARRAY_COUNT(f2), true, false },
1633 { f3, SK_ARRAY_COUNT(f3), true, false },
1634 { f4, SK_ARRAY_COUNT(f4), true, false },
1635 { f5, SK_ARRAY_COUNT(f5), true, false },
1636 { f6, SK_ARRAY_COUNT(f6), true, false },
1637 { f7, SK_ARRAY_COUNT(f7), true, false },
1638 { f8, SK_ARRAY_COUNT(f8), true, false },
1639 { f9, SK_ARRAY_COUNT(f9), true, false },
1640 { fa, SK_ARRAY_COUNT(fa), true, false },
1641 { fb, SK_ARRAY_COUNT(fb), true, false },
1642
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001643 { c1, SK_ARRAY_COUNT(c1), false, true },
1644 { c2, SK_ARRAY_COUNT(c2), false, true },
1645 { c3, SK_ARRAY_COUNT(c3), false, true },
1646
1647 { d1, SK_ARRAY_COUNT(d1), false, false },
1648 { d2, SK_ARRAY_COUNT(d2), false, false },
1649 { d3, SK_ARRAY_COUNT(d3), false, false },
caryclark@google.comf1316942011-07-26 19:54:45 +00001650 };
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001651
1652 const size_t testCount = SK_ARRAY_COUNT(tests);
caryclark@google.comf1316942011-07-26 19:54:45 +00001653 size_t index;
1654 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1655 SkPath path;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001656 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1657 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1658 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.comf1316942011-07-26 19:54:45 +00001659 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001660 if (tests[testIndex].fClose) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001661 path.close();
1662 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001663 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(NULL));
1664 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(NULL, NULL));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001665
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001666 if (tests[testIndex].fIsRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001667 SkRect computed, expected;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001668 expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001669 REPORTER_ASSERT(reporter, path.isRect(&computed));
1670 REPORTER_ASSERT(reporter, expected == computed);
skia.committer@gmail.com1c9c0d32012-11-22 02:02:41 +00001671
caryclark@google.comf68154a2012-11-21 15:18:06 +00001672 bool isClosed;
1673 SkPath::Direction direction, cheapDirection;
1674 REPORTER_ASSERT(reporter, path.cheapComputeDirection(&cheapDirection));
robertphillips@google.com8fd16032013-06-25 15:39:58 +00001675 REPORTER_ASSERT(reporter, path.isRect(&isClosed, &direction));
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001676 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001677 REPORTER_ASSERT(reporter, direction == cheapDirection);
commit-bot@chromium.orgc2abd542014-01-25 16:54:31 +00001678 direction = (SkPath::Direction) -1;
commit-bot@chromium.org7e90e8d2014-02-11 01:38:30 +00001679 if (!tests[testIndex].fClose) {
commit-bot@chromium.orgc2abd542014-01-25 16:54:31 +00001680 REPORTER_ASSERT(reporter, SkPath::kFill_PathAsRect == path.asRect());
1681 REPORTER_ASSERT(reporter, SkPath::kFill_PathAsRect == path.asRect(&direction));
1682 } else {
1683 REPORTER_ASSERT(reporter, SkPath::kStroke_PathAsRect == path.asRect());
1684 REPORTER_ASSERT(reporter, SkPath::kStroke_PathAsRect == path.asRect(&direction));
1685 }
1686 REPORTER_ASSERT(reporter, direction == cheapDirection);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001687 } else {
1688 SkRect computed;
1689 computed.set(123, 456, 789, 1011);
1690 REPORTER_ASSERT(reporter, !path.isRect(&computed));
1691 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
1692 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
1693
1694 bool isClosed = (bool) -1;
1695 SkPath::Direction direction = (SkPath::Direction) -1;
robertphillips@google.com8fd16032013-06-25 15:39:58 +00001696 REPORTER_ASSERT(reporter, !path.isRect(&isClosed, &direction));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001697 REPORTER_ASSERT(reporter, isClosed == (bool) -1);
1698 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
commit-bot@chromium.orgc2abd542014-01-25 16:54:31 +00001699 REPORTER_ASSERT(reporter, SkPath::kNone_PathAsRect == path.asRect());
1700 REPORTER_ASSERT(reporter, SkPath::kNone_PathAsRect == path.asRect(&direction));
1701 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001702 }
caryclark@google.comf1316942011-07-26 19:54:45 +00001703 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001704
caryclark@google.comf1316942011-07-26 19:54:45 +00001705 // fail, close then line
1706 SkPath path1;
1707 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001708 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001709 path1.lineTo(r1[index].fX, r1[index].fY);
1710 }
1711 path1.close();
1712 path1.lineTo(1, 0);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001713 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001714
caryclark@google.comf1316942011-07-26 19:54:45 +00001715 // fail, move in the middle
1716 path1.reset();
1717 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001718 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001719 if (index == 2) {
1720 path1.moveTo(1, .5f);
1721 }
1722 path1.lineTo(r1[index].fX, r1[index].fY);
1723 }
1724 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001725 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00001726
1727 // fail, move on the edge
1728 path1.reset();
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 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
1731 path1.lineTo(r1[index].fX, r1[index].fY);
1732 }
1733 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001734 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001735
caryclark@google.comf1316942011-07-26 19:54:45 +00001736 // fail, quad
1737 path1.reset();
1738 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001739 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001740 if (index == 2) {
1741 path1.quadTo(1, .5f, 1, .5f);
1742 }
1743 path1.lineTo(r1[index].fX, r1[index].fY);
1744 }
1745 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001746 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001747
caryclark@google.comf1316942011-07-26 19:54:45 +00001748 // fail, cubic
1749 path1.reset();
1750 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001751 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001752 if (index == 2) {
1753 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
1754 }
1755 path1.lineTo(r1[index].fX, r1[index].fY);
1756 }
1757 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001758 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00001759}
1760
caryclark@google.com56f233a2012-11-19 13:06:06 +00001761static void test_isNestedRects(skiatest::Reporter* reporter) {
1762 // passing tests (all moveTo / lineTo...
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001763 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001764 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1765 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1766 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001767 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001768 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1769 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1770 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1771 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001772 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
1773 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 +00001774 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
1775 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
1776 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001777
1778 // failing tests
1779 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1780 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1781 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1782 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1783 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1784 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1785 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1786 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
1787
1788 // failing, no close
1789 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
1790 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
1791
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001792 struct IsNestedRectTest {
1793 SkPoint *fPoints;
1794 size_t fPointCount;
1795 SkPath::Direction fDirection;
1796 bool fClose;
1797 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
1798 } tests[] = {
1799 { r1, SK_ARRAY_COUNT(r1), SkPath::kCW_Direction , true, true },
1800 { r2, SK_ARRAY_COUNT(r2), SkPath::kCW_Direction , true, true },
1801 { r3, SK_ARRAY_COUNT(r3), SkPath::kCW_Direction , true, true },
1802 { r4, SK_ARRAY_COUNT(r4), SkPath::kCW_Direction , true, true },
1803 { r5, SK_ARRAY_COUNT(r5), SkPath::kCCW_Direction, true, true },
1804 { r6, SK_ARRAY_COUNT(r6), SkPath::kCCW_Direction, true, true },
1805 { r7, SK_ARRAY_COUNT(r7), SkPath::kCCW_Direction, true, true },
1806 { r8, SK_ARRAY_COUNT(r8), SkPath::kCCW_Direction, true, true },
1807 { r9, SK_ARRAY_COUNT(r9), SkPath::kCCW_Direction, true, true },
1808 { ra, SK_ARRAY_COUNT(ra), SkPath::kCCW_Direction, true, true },
1809 { rb, SK_ARRAY_COUNT(rb), SkPath::kCW_Direction, true, true },
1810 { rc, SK_ARRAY_COUNT(rc), SkPath::kCW_Direction, true, true },
1811 { rd, SK_ARRAY_COUNT(rd), SkPath::kCCW_Direction, true, true },
1812 { re, SK_ARRAY_COUNT(re), SkPath::kCW_Direction, true, true },
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001813
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001814 { f1, SK_ARRAY_COUNT(f1), SkPath::kUnknown_Direction, true, false },
1815 { f2, SK_ARRAY_COUNT(f2), SkPath::kUnknown_Direction, true, false },
1816 { f3, SK_ARRAY_COUNT(f3), SkPath::kUnknown_Direction, true, false },
1817 { f4, SK_ARRAY_COUNT(f4), SkPath::kUnknown_Direction, true, false },
1818 { f5, SK_ARRAY_COUNT(f5), SkPath::kUnknown_Direction, true, false },
1819 { f6, SK_ARRAY_COUNT(f6), SkPath::kUnknown_Direction, true, false },
1820 { f7, SK_ARRAY_COUNT(f7), SkPath::kUnknown_Direction, true, false },
1821 { f8, SK_ARRAY_COUNT(f8), SkPath::kUnknown_Direction, true, false },
1822
1823 { c1, SK_ARRAY_COUNT(c1), SkPath::kUnknown_Direction, false, false },
1824 { c2, SK_ARRAY_COUNT(c2), SkPath::kUnknown_Direction, false, false },
1825 };
1826
1827 const size_t testCount = SK_ARRAY_COUNT(tests);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001828 size_t index;
1829 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001830 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1831 SkPath path;
1832 if (rectFirst) {
1833 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1834 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001835 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1836 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1837 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001838 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001839 if (tests[testIndex].fClose) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001840 path.close();
1841 }
1842 if (!rectFirst) {
1843 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1844 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001845 REPORTER_ASSERT(reporter, tests[testIndex].fIsNestedRect == path.isNestedRects(NULL));
1846 if (tests[testIndex].fIsNestedRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001847 SkRect expected[2], computed[2];
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001848 SkPath::Direction expectedDirs[2], computedDirs[2];
caryclark@google.com56f233a2012-11-19 13:06:06 +00001849 SkRect testBounds;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001850 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001851 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
1852 expected[1] = testBounds;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001853 if (rectFirst) {
1854 expectedDirs[0] = SkPath::kCW_Direction;
1855 } else {
1856 expectedDirs[0] = SkPath::kCCW_Direction;
1857 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001858 expectedDirs[1] = tests[testIndex].fDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001859 REPORTER_ASSERT(reporter, path.isNestedRects(computed, computedDirs));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001860 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
1861 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001862 REPORTER_ASSERT(reporter, expectedDirs[0] == computedDirs[0]);
1863 REPORTER_ASSERT(reporter, expectedDirs[1] == computedDirs[1]);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001864 }
caryclark@google.com56f233a2012-11-19 13:06:06 +00001865 }
1866
1867 // fail, close then line
1868 SkPath path1;
1869 if (rectFirst) {
1870 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1871 }
1872 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001873 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001874 path1.lineTo(r1[index].fX, r1[index].fY);
1875 }
1876 path1.close();
1877 path1.lineTo(1, 0);
1878 if (!rectFirst) {
1879 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1880 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001881 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001882
1883 // fail, move in the middle
1884 path1.reset();
1885 if (rectFirst) {
1886 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1887 }
1888 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001889 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001890 if (index == 2) {
1891 path1.moveTo(1, .5f);
1892 }
1893 path1.lineTo(r1[index].fX, r1[index].fY);
1894 }
1895 path1.close();
1896 if (!rectFirst) {
1897 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1898 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001899 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001900
1901 // fail, move on the edge
1902 path1.reset();
1903 if (rectFirst) {
1904 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1905 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001906 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001907 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
1908 path1.lineTo(r1[index].fX, r1[index].fY);
1909 }
1910 path1.close();
1911 if (!rectFirst) {
1912 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1913 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001914 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001915
1916 // fail, quad
1917 path1.reset();
1918 if (rectFirst) {
1919 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1920 }
1921 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001922 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001923 if (index == 2) {
1924 path1.quadTo(1, .5f, 1, .5f);
1925 }
1926 path1.lineTo(r1[index].fX, r1[index].fY);
1927 }
1928 path1.close();
1929 if (!rectFirst) {
1930 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1931 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001932 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001933
1934 // fail, cubic
1935 path1.reset();
1936 if (rectFirst) {
1937 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1938 }
1939 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001940 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001941 if (index == 2) {
1942 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
1943 }
1944 path1.lineTo(r1[index].fX, r1[index].fY);
1945 }
1946 path1.close();
1947 if (!rectFirst) {
1948 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1949 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001950 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
skia.committer@gmail.com34587162012-11-20 02:01:23 +00001951
caryclark@google.com56f233a2012-11-19 13:06:06 +00001952 // fail, not nested
1953 path1.reset();
1954 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
1955 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001956 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001957 }
caryclark@google.combfe90372012-11-21 13:56:20 +00001958
1959 // pass, stroke rect
1960 SkPath src, dst;
1961 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
1962 SkPaint strokePaint;
1963 strokePaint.setStyle(SkPaint::kStroke_Style);
1964 strokePaint.setStrokeWidth(2);
1965 strokePaint.getFillPath(src, &dst);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001966 REPORTER_ASSERT(reporter, dst.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001967}
1968
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001969static void write_and_read_back(skiatest::Reporter* reporter,
1970 const SkPath& p) {
commit-bot@chromium.org19382422014-01-14 20:51:26 +00001971 SkWriter32 writer;
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001972 writer.writePath(p);
reed@google.com44699382013-10-31 17:28:30 +00001973 size_t size = writer.bytesWritten();
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001974 SkAutoMalloc storage(size);
1975 writer.flatten(storage.get());
1976 SkReader32 reader(storage.get(), size);
1977
1978 SkPath readBack;
1979 REPORTER_ASSERT(reporter, readBack != p);
1980 reader.readPath(&readBack);
1981 REPORTER_ASSERT(reporter, readBack == p);
1982
rmistry@google.comd6176b02012-08-23 18:14:13 +00001983 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001984 p.getConvexityOrUnknown());
1985
1986 REPORTER_ASSERT(reporter, readBack.isOval(NULL) == p.isOval(NULL));
1987
1988 const SkRect& origBounds = p.getBounds();
1989 const SkRect& readBackBounds = readBack.getBounds();
1990
1991 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
1992}
1993
reed@google.com53effc52011-09-21 19:05:12 +00001994static void test_flattening(skiatest::Reporter* reporter) {
1995 SkPath p;
1996
1997 static const SkPoint pts[] = {
1998 { 0, 0 },
1999 { SkIntToScalar(10), SkIntToScalar(10) },
2000 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2001 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2002 };
2003 p.moveTo(pts[0]);
2004 p.lineTo(pts[1]);
2005 p.quadTo(pts[2], pts[3]);
2006 p.cubicTo(pts[4], pts[5], pts[6]);
2007
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002008 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002009
2010 // create a buffer that should be much larger than the path so we don't
2011 // kill our stack if writer goes too far.
2012 char buffer[1024];
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002013 size_t size1 = p.writeToMemory(NULL);
2014 size_t size2 = p.writeToMemory(buffer);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002015 REPORTER_ASSERT(reporter, size1 == size2);
2016
2017 SkPath p2;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002018 size_t size3 = p2.readFromMemory(buffer, 1024);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002019 REPORTER_ASSERT(reporter, size1 == size3);
2020 REPORTER_ASSERT(reporter, p == p2);
2021
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002022 size3 = p2.readFromMemory(buffer, 0);
2023 REPORTER_ASSERT(reporter, !size3);
2024
2025 SkPath tooShort;
2026 size3 = tooShort.readFromMemory(buffer, size1 - 1);
2027 REPORTER_ASSERT(reporter, tooShort.isEmpty());
2028
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002029 char buffer2[1024];
2030 size3 = p2.writeToMemory(buffer2);
2031 REPORTER_ASSERT(reporter, size1 == size3);
2032 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002033
2034 // test persistence of the oval flag & convexity
2035 {
2036 SkPath oval;
2037 SkRect rect = SkRect::MakeWH(10, 10);
2038 oval.addOval(rect);
2039
2040 write_and_read_back(reporter, oval);
2041 }
reed@google.com53effc52011-09-21 19:05:12 +00002042}
2043
2044static void test_transform(skiatest::Reporter* reporter) {
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002045 SkPath p;
rmistry@google.comd6176b02012-08-23 18:14:13 +00002046
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002047#define CONIC_PERSPECTIVE_BUG_FIXED 0
reed@google.com53effc52011-09-21 19:05:12 +00002048 static const SkPoint pts[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002049 { 0, 0 }, // move
2050 { SkIntToScalar(10), SkIntToScalar(10) }, // line
2051 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
2052 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
2053#if CONIC_PERSPECTIVE_BUG_FIXED
2054 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
2055#endif
reed@google.com53effc52011-09-21 19:05:12 +00002056 };
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002057 const int kPtCount = SK_ARRAY_COUNT(pts);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002058
reed@google.com53effc52011-09-21 19:05:12 +00002059 p.moveTo(pts[0]);
2060 p.lineTo(pts[1]);
2061 p.quadTo(pts[2], pts[3]);
2062 p.cubicTo(pts[4], pts[5], pts[6]);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002063#if CONIC_PERSPECTIVE_BUG_FIXED
2064 p.conicTo(pts[4], pts[5], 0.5f);
2065#endif
2066 p.close();
rmistry@google.comd6176b02012-08-23 18:14:13 +00002067
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002068 {
2069 SkMatrix matrix;
2070 matrix.reset();
2071 SkPath p1;
2072 p.transform(matrix, &p1);
2073 REPORTER_ASSERT(reporter, p == p1);
reed@google.com53effc52011-09-21 19:05:12 +00002074 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002075
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002076
2077 {
2078 SkMatrix matrix;
2079 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2080
2081 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
2082
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002083 p.transform(matrix, &p1);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002084 SkPoint pts1[kPtCount];
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002085 int count = p1.getPoints(pts1, kPtCount);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002086 REPORTER_ASSERT(reporter, kPtCount == count);
2087 for (int i = 0; i < count; ++i) {
2088 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2089 REPORTER_ASSERT(reporter, newPt == pts1[i]);
2090 }
2091 }
2092
2093 {
2094 SkMatrix matrix;
2095 matrix.reset();
2096 matrix.setPerspX(SkScalarToPersp(4));
2097
2098 SkPath p1;
2099 p1.moveTo(SkPoint::Make(0, 0));
2100
2101 p.transform(matrix, &p1);
2102 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
2103 p1.transform(matrix, NULL);
2104 SkRect pBounds = p.getBounds();
2105 SkRect p1Bounds = p1.getBounds();
2106 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2107 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2108 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2109 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2110 }
2111
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002112 p.reset();
2113 p.addCircle(0, 0, 1, SkPath::kCW_Direction);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002114
2115 {
2116 SkMatrix matrix;
2117 matrix.reset();
2118 SkPath p1;
2119 p1.moveTo(SkPoint::Make(0, 0));
2120
2121 p.transform(matrix, &p1);
2122 REPORTER_ASSERT(reporter, p1.cheapIsDirection(SkPath::kCW_Direction));
2123 }
2124
2125
2126 {
2127 SkMatrix matrix;
2128 matrix.reset();
2129 matrix.setScaleX(-1);
2130 SkPath p1;
2131 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2132
2133 p.transform(matrix, &p1);
2134 REPORTER_ASSERT(reporter, p1.cheapIsDirection(SkPath::kCCW_Direction));
2135 }
2136
2137 {
2138 SkMatrix matrix;
2139 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2140 SkPath p1;
2141 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2142
2143 p.transform(matrix, &p1);
2144 REPORTER_ASSERT(reporter, p1.cheapIsDirection(SkPath::kUnknown_Direction));
2145 }
reed@google.com53effc52011-09-21 19:05:12 +00002146}
2147
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002148static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002149 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002150 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002151
caryclark@google.com56f233a2012-11-19 13:06:06 +00002152 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002153 const char* testPath;
2154 const size_t numResultPts;
2155 const SkRect resultBound;
2156 const SkPath::Verb* resultVerbs;
2157 const size_t numResultVerbs;
2158 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002159
schenney@chromium.org7e963602012-06-13 17:05:43 +00002160 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2161 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2162 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2163 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2164 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2165 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2166 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2167 static const SkPath::Verb resultVerbs8[] = {
2168 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2169 };
2170 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2171 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2172 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2173 static const SkPath::Verb resultVerbs12[] = {
2174 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2175 };
2176 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2177 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2178 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2179 static const SkPath::Verb resultVerbs16[] = {
2180 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2181 };
2182 static const struct zeroPathTestData gZeroLengthTests[] = {
2183 { "M 1 1", 1, {0, 0, 0, 0}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002184 { "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 +00002185 { "M 1 1 z", 1, {0, 0, 0, 0}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002186 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2187 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2188 { "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) },
2189 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2190 { "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) },
2191 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2192 { "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) },
2193 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2194 { "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) },
2195 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2196 { "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 +00002197 SK_ARRAY_COUNT(resultVerbs14)
2198 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002199 { "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) },
2200 { "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 +00002201 SK_ARRAY_COUNT(resultVerbs16)
2202 }
2203 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002204
schenney@chromium.org7e963602012-06-13 17:05:43 +00002205 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2206 p.reset();
2207 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2208 REPORTER_ASSERT(reporter, valid);
2209 REPORTER_ASSERT(reporter, !p.isEmpty());
2210 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2211 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2212 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2213 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2214 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2215 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002216 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002217}
2218
2219struct SegmentInfo {
2220 SkPath fPath;
2221 int fPointCount;
2222};
2223
reed@google.com10296cc2011-09-21 12:29:05 +00002224#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2225
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002226static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00002227 SkPath p, p2;
2228
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002229 p.moveTo(0, 0);
2230 p.quadTo(100, 100, 200, 200);
2231 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2232 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002233 p2 = p;
2234 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002235 p.cubicTo(100, 100, 200, 200, 300, 300);
2236 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2237 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002238 p2 = p;
2239 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2240
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002241 p.reset();
2242 p.moveTo(0, 0);
2243 p.cubicTo(100, 100, 200, 200, 300, 300);
2244 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00002245 p2 = p;
2246 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00002247
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002248 REPORTER_ASSERT(reporter, !p.isEmpty());
2249}
2250
2251static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002252 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002253 SkPoint pts[4];
2254
2255 // Test an iterator with no path
2256 SkPath::Iter noPathIter;
2257 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002258
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002259 // Test that setting an empty path works
2260 noPathIter.setPath(p, false);
2261 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002262
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002263 // Test that close path makes no difference for an empty path
2264 noPathIter.setPath(p, true);
2265 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002266
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002267 // Test an iterator with an initial empty path
2268 SkPath::Iter iter(p, false);
2269 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2270
2271 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00002272 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002273 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2274
rmistry@google.comd6176b02012-08-23 18:14:13 +00002275
schenney@chromium.org7e963602012-06-13 17:05:43 +00002276 struct iterTestData {
2277 const char* testPath;
2278 const bool forceClose;
2279 const bool consumeDegenerates;
2280 const size_t* numResultPtsPerVerb;
2281 const SkPoint* resultPts;
2282 const SkPath::Verb* resultVerbs;
2283 const size_t numResultVerbs;
2284 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002285
schenney@chromium.org7e963602012-06-13 17:05:43 +00002286 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2287 static const SkPath::Verb resultVerbs2[] = {
2288 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2289 };
2290 static const SkPath::Verb resultVerbs3[] = {
2291 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2292 };
2293 static const SkPath::Verb resultVerbs4[] = {
2294 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2295 };
2296 static const SkPath::Verb resultVerbs5[] = {
2297 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2298 };
2299 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00002300 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2301 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2302 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2303 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002304 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002305 static const SkPoint resultPts2[] = {
2306 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2307 };
2308 static const SkPoint resultPts3[] = {
2309 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2310 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2311 };
2312 static const SkPoint resultPts4[] = {
2313 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2314 };
2315 static const SkPoint resultPts5[] = {
2316 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2317 };
2318 static const struct iterTestData gIterTests[] = {
2319 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002320 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2321 { "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 +00002322 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2323 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2324 { "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) },
2325 { "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 +00002326 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2327 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2328 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2329 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2330 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2331 { "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 +00002332 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002333
schenney@chromium.org7e963602012-06-13 17:05:43 +00002334 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2335 p.reset();
2336 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2337 REPORTER_ASSERT(reporter, valid);
2338 iter.setPath(p, gIterTests[i].forceClose);
2339 int j = 0, l = 0;
2340 do {
2341 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2342 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2343 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2344 }
2345 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2346 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2347 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002348
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002349 p.reset();
2350 iter.setPath(p, false);
2351 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2352 p.lineTo(1, 1);
2353 p.close();
2354 iter.setPath(p, false);
2355 REPORTER_ASSERT(reporter, iter.isClosedContour());
2356 p.reset();
2357 iter.setPath(p, true);
2358 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2359 p.lineTo(1, 1);
2360 iter.setPath(p, true);
2361 REPORTER_ASSERT(reporter, iter.isClosedContour());
2362 p.moveTo(0, 0);
2363 p.lineTo(2, 2);
2364 iter.setPath(p, false);
2365 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2366
2367 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
2368 // check to see if the result is correct.
2369 for (int setNaN = 0; setNaN < 4; ++setNaN) {
2370 p.reset();
2371 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
2372 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
2373 iter.setPath(p, true);
2374 iter.next(pts, false);
2375 iter.next(pts, false);
2376 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
2377 }
2378
2379 p.reset();
2380 p.quadTo(0, 0, 0, 0);
2381 iter.setPath(p, false);
2382 iter.next(pts, false);
2383 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false));
2384 iter.setPath(p, false);
2385 iter.next(pts, false);
2386 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2387
2388 p.reset();
2389 p.conicTo(0, 0, 0, 0, 0.5f);
2390 iter.setPath(p, false);
2391 iter.next(pts, false);
2392 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
2393 iter.setPath(p, false);
2394 iter.next(pts, false);
2395 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2396
2397 p.reset();
2398 p.cubicTo(0, 0, 0, 0, 0, 0);
2399 iter.setPath(p, false);
2400 iter.next(pts, false);
2401 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2402 iter.setPath(p, false);
2403 iter.next(pts, false);
2404 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2405
2406 p.moveTo(1, 1); // add a trailing moveto
2407 iter.setPath(p, false);
2408 iter.next(pts, false);
2409 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2410 iter.setPath(p, false);
2411 iter.next(pts, false);
2412 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2413
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002414 // The GM degeneratesegments.cpp test is more extensive
2415}
2416
2417static void test_raw_iter(skiatest::Reporter* reporter) {
2418 SkPath p;
2419 SkPoint pts[4];
2420
2421 // Test an iterator with no path
2422 SkPath::RawIter noPathIter;
2423 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2424 // Test that setting an empty path works
2425 noPathIter.setPath(p);
2426 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00002427
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002428 // Test an iterator with an initial empty path
2429 SkPath::RawIter iter(p);
2430 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2431
2432 // Test that a move-only path returns the move.
2433 p.moveTo(SK_Scalar1, 0);
2434 iter.setPath(p);
2435 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2436 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2437 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2438 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2439
2440 // No matter how many moves we add, we should get them all back
2441 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2442 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2443 iter.setPath(p);
2444 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2445 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2446 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2447 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2448 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2449 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2450 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2451 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2452 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2453 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2454
2455 // Initial close is never ever stored
2456 p.reset();
2457 p.close();
2458 iter.setPath(p);
2459 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2460
2461 // Move/close sequences
2462 p.reset();
2463 p.close(); // Not stored, no purpose
2464 p.moveTo(SK_Scalar1, 0);
2465 p.close();
2466 p.close(); // Not stored, no purpose
2467 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2468 p.close();
2469 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2470 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
2471 p.close();
2472 iter.setPath(p);
2473 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2474 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2475 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2476 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2477 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2478 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2479 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2480 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2481 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2482 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2483 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2484 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2485 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2486 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2487 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2488 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2489 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2490 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2491 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2492 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2493 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2494 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2495
2496 // Generate random paths and verify
2497 SkPoint randomPts[25];
2498 for (int i = 0; i < 5; ++i) {
2499 for (int j = 0; j < 5; ++j) {
2500 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
2501 }
2502 }
2503
2504 // Max of 10 segments, max 3 points per segment
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002505 SkRandom rand(9876543);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002506 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00002507 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002508 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00002509
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002510 for (int i = 0; i < 500; ++i) {
2511 p.reset();
2512 bool lastWasClose = true;
2513 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00002514 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002515 int numPoints = 0;
2516 int numVerbs = (rand.nextU() >> 16) % 10;
2517 int numIterVerbs = 0;
2518 for (int j = 0; j < numVerbs; ++j) {
2519 do {
2520 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
2521 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002522 switch (nextVerb) {
2523 case SkPath::kMove_Verb:
2524 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2525 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00002526 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002527 numPoints += 1;
2528 lastWasClose = false;
2529 haveMoveTo = true;
2530 break;
2531 case SkPath::kLine_Verb:
2532 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002533 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002534 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2535 haveMoveTo = true;
2536 }
2537 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2538 p.lineTo(expectedPts[numPoints]);
2539 numPoints += 1;
2540 lastWasClose = false;
2541 break;
2542 case SkPath::kQuad_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 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2550 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
2551 numPoints += 2;
2552 lastWasClose = false;
2553 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002554 case SkPath::kConic_Verb:
2555 if (!haveMoveTo) {
2556 expectedPts[numPoints++] = lastMoveToPt;
2557 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2558 haveMoveTo = true;
2559 }
2560 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2561 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2562 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2563 rand.nextUScalar1() * 4);
2564 numPoints += 2;
2565 lastWasClose = false;
2566 break;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002567 case SkPath::kCubic_Verb:
2568 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002569 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002570 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2571 haveMoveTo = true;
2572 }
2573 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2574 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2575 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
2576 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2577 expectedPts[numPoints + 2]);
2578 numPoints += 3;
2579 lastWasClose = false;
2580 break;
2581 case SkPath::kClose_Verb:
2582 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00002583 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002584 lastWasClose = true;
2585 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002586 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002587 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002588 }
2589 expectedVerbs[numIterVerbs++] = nextVerb;
2590 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00002591
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002592 iter.setPath(p);
2593 numVerbs = numIterVerbs;
2594 numIterVerbs = 0;
2595 int numIterPts = 0;
2596 SkPoint lastMoveTo;
2597 SkPoint lastPt;
2598 lastMoveTo.set(0, 0);
2599 lastPt.set(0, 0);
2600 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
2601 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
2602 numIterVerbs++;
2603 switch (nextVerb) {
2604 case SkPath::kMove_Verb:
2605 REPORTER_ASSERT(reporter, numIterPts < numPoints);
2606 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
2607 lastPt = lastMoveTo = pts[0];
2608 numIterPts += 1;
2609 break;
2610 case SkPath::kLine_Verb:
2611 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
2612 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2613 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2614 lastPt = pts[1];
2615 numIterPts += 1;
2616 break;
2617 case SkPath::kQuad_Verb:
reed@google.com277c3f82013-05-31 15:17:50 +00002618 case SkPath::kConic_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002619 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
2620 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2621 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2622 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2623 lastPt = pts[2];
2624 numIterPts += 2;
2625 break;
2626 case SkPath::kCubic_Verb:
2627 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
2628 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2629 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2630 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2631 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
2632 lastPt = pts[3];
2633 numIterPts += 3;
2634 break;
2635 case SkPath::kClose_Verb:
2636 REPORTER_ASSERT(reporter, pts[0] == lastMoveTo);
2637 lastPt = lastMoveTo;
2638 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002639 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002640 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002641 }
2642 }
2643 REPORTER_ASSERT(reporter, numIterPts == numPoints);
2644 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
2645 }
2646}
2647
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002648static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002649 const SkPath& path,
2650 bool expectedCircle,
2651 SkPath::Direction expectedDir) {
robertphillips@google.com466310d2013-12-03 16:43:54 +00002652 SkRect rect = SkRect::MakeEmpty();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002653 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
2654 REPORTER_ASSERT(reporter, path.cheapIsDirection(expectedDir));
skia.committer@gmail.comfbb0ed92012-11-13 21:46:06 +00002655
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002656 if (expectedCircle) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002657 REPORTER_ASSERT(reporter, rect.height() == rect.width());
2658 }
2659}
2660
2661static void test_circle_skew(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002662 const SkPath& path,
2663 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002664 SkPath tmp;
2665
2666 SkMatrix m;
2667 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
2668 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002669 // this matrix reverses the direction.
2670 if (SkPath::kCCW_Direction == dir) {
2671 dir = SkPath::kCW_Direction;
2672 } else {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002673 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002674 dir = SkPath::kCCW_Direction;
2675 }
2676 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002677}
2678
2679static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002680 const SkPath& path,
2681 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002682 SkPath tmp;
2683
2684 // translate at small offset
2685 SkMatrix m;
2686 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
2687 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002688 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002689
2690 tmp.reset();
2691 m.reset();
2692
2693 // translate at a relatively big offset
2694 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
2695 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002696 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002697}
2698
2699static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002700 const SkPath& path,
2701 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002702 for (int angle = 0; angle < 360; ++angle) {
2703 SkPath tmp;
2704 SkMatrix m;
2705 m.setRotate(SkIntToScalar(angle));
2706 path.transform(m, &tmp);
2707
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002708 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002709 // degrees is not an oval anymore, this can be improved. we made this
2710 // for the simplicity of our implementation.
2711 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002712 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002713 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002714 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002715 }
2716 }
2717}
2718
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002719static void test_circle_mirror_x(skiatest::Reporter* reporter,
2720 const SkPath& path,
2721 SkPath::Direction dir) {
2722 SkPath tmp;
2723 SkMatrix m;
2724 m.reset();
2725 m.setScaleX(-SK_Scalar1);
2726 path.transform(m, &tmp);
2727
2728 if (SkPath::kCW_Direction == dir) {
2729 dir = SkPath::kCCW_Direction;
2730 } else {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002731 REPORTER_ASSERT(reporter, SkPath::kCCW_Direction == dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002732 dir = SkPath::kCW_Direction;
2733 }
2734
2735 check_for_circle(reporter, tmp, true, dir);
2736}
2737
2738static void test_circle_mirror_y(skiatest::Reporter* reporter,
2739 const SkPath& path,
2740 SkPath::Direction dir) {
2741 SkPath tmp;
2742 SkMatrix m;
2743 m.reset();
2744 m.setScaleY(-SK_Scalar1);
2745 path.transform(m, &tmp);
2746
2747 if (SkPath::kCW_Direction == dir) {
2748 dir = SkPath::kCCW_Direction;
2749 } else {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002750 REPORTER_ASSERT(reporter, SkPath::kCCW_Direction == dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002751 dir = SkPath::kCW_Direction;
2752 }
2753
2754 check_for_circle(reporter, tmp, true, dir);
2755}
2756
2757static void test_circle_mirror_xy(skiatest::Reporter* reporter,
2758 const SkPath& path,
2759 SkPath::Direction dir) {
2760 SkPath tmp;
2761 SkMatrix m;
2762 m.reset();
2763 m.setScaleX(-SK_Scalar1);
2764 m.setScaleY(-SK_Scalar1);
2765 path.transform(m, &tmp);
2766
2767 check_for_circle(reporter, tmp, true, dir);
2768}
2769
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002770static void test_circle_with_direction(skiatest::Reporter* reporter,
2771 SkPath::Direction dir) {
2772 SkPath path;
2773
2774 // circle at origin
2775 path.addCircle(0, 0, SkIntToScalar(20), dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002776 check_for_circle(reporter, path, true, dir);
2777 test_circle_rotate(reporter, path, dir);
2778 test_circle_translate(reporter, path, dir);
2779 test_circle_skew(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002780
2781 // circle at an offset at (10, 10)
2782 path.reset();
2783 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
2784 SkIntToScalar(20), dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002785 check_for_circle(reporter, path, true, dir);
2786 test_circle_rotate(reporter, path, dir);
2787 test_circle_translate(reporter, path, dir);
2788 test_circle_skew(reporter, path, dir);
2789 test_circle_mirror_x(reporter, path, dir);
2790 test_circle_mirror_y(reporter, path, dir);
2791 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002792}
2793
2794static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
2795 SkPath path;
2796 SkPath circle;
2797 SkPath rect;
2798 SkPath empty;
2799
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002800 static const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
2801 static const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
2802
2803 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002804 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
2805 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
2806
2807 SkMatrix translate;
2808 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
2809
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00002810 // Although all the path concatenation related operations leave
2811 // the path a circle, most mark it as a non-circle for simplicity
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002812
2813 // empty + circle (translate)
2814 path = empty;
2815 path.addPath(circle, translate);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002816 check_for_circle(reporter, path, false, kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002817
2818 // circle + empty (translate)
2819 path = circle;
2820 path.addPath(empty, translate);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00002821 check_for_circle(reporter, path, true, kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002822
2823 // test reverseAddPath
2824 path = circle;
2825 path.reverseAddPath(rect);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002826 check_for_circle(reporter, path, false, kCircleDirOpposite);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002827}
2828
2829static void test_circle(skiatest::Reporter* reporter) {
2830 test_circle_with_direction(reporter, SkPath::kCW_Direction);
2831 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
2832
2833 // multiple addCircle()
2834 SkPath path;
2835 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2836 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002837 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002838
2839 // some extra lineTo() would make isOval() fail
2840 path.reset();
2841 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2842 path.lineTo(0, 0);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002843 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002844
2845 // not back to the original point
2846 path.reset();
2847 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2848 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002849 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002850
2851 test_circle_with_add_paths(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002852
2853 // test negative radius
2854 path.reset();
2855 path.addCircle(0, 0, -1, SkPath::kCW_Direction);
2856 REPORTER_ASSERT(reporter, path.isEmpty());
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002857}
2858
2859static void test_oval(skiatest::Reporter* reporter) {
2860 SkRect rect;
2861 SkMatrix m;
2862 SkPath path;
2863
2864 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
2865 path.addOval(rect);
2866
2867 REPORTER_ASSERT(reporter, path.isOval(NULL));
2868
2869 m.setRotate(SkIntToScalar(90));
2870 SkPath tmp;
2871 path.transform(m, &tmp);
2872 // an oval rotated 90 degrees is still an oval.
2873 REPORTER_ASSERT(reporter, tmp.isOval(NULL));
2874
2875 m.reset();
2876 m.setRotate(SkIntToScalar(30));
2877 tmp.reset();
2878 path.transform(m, &tmp);
2879 // an oval rotated 30 degrees is not an oval anymore.
2880 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2881
2882 // since empty path being transformed.
2883 path.reset();
2884 tmp.reset();
2885 m.reset();
2886 path.transform(m, &tmp);
2887 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2888
2889 // empty path is not an oval
2890 tmp.reset();
2891 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2892
2893 // only has moveTo()s
2894 tmp.reset();
2895 tmp.moveTo(0, 0);
2896 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
2897 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2898
2899 // mimic WebKit's calling convention,
2900 // call moveTo() first and then call addOval()
2901 path.reset();
2902 path.moveTo(0, 0);
2903 path.addOval(rect);
2904 REPORTER_ASSERT(reporter, path.isOval(NULL));
2905
2906 // copy path
2907 path.reset();
2908 tmp.reset();
2909 tmp.addOval(rect);
2910 path = tmp;
2911 REPORTER_ASSERT(reporter, path.isOval(NULL));
2912}
2913
bungeman@google.coma5809a32013-06-21 15:13:34 +00002914static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
2915 SkPath empty;
reed@android.com80e39a72009-04-02 16:59:40 +00002916
reed@android.com3abec1d2009-03-02 05:36:20 +00002917 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002918 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002919 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00002920 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00002921 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00002922 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
2923 REPORTER_ASSERT(reporter, !p.isInverseFillType());
bungeman@google.coma5809a32013-06-21 15:13:34 +00002924 REPORTER_ASSERT(reporter, p == empty);
2925 REPORTER_ASSERT(reporter, !(p != empty));
2926}
2927
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002928static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
2929 SkPath::Direction dir) {
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002930 REPORTER_ASSERT(reporter, path->isConvex());
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002931 REPORTER_ASSERT(reporter, path->cheapIsDirection(dir));
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002932 path->setConvexity(SkPath::kUnknown_Convexity);
2933 REPORTER_ASSERT(reporter, path->isConvex());
2934 path->reset();
2935}
2936
2937static void test_rrect(skiatest::Reporter* reporter) {
2938 SkPath p;
2939 SkRRect rr;
2940 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
2941 SkRect r = {10, 20, 30, 40};
2942 rr.setRectRadii(r, radii);
2943 p.addRRect(rr);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002944 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002945 p.addRRect(rr, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002946 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002947 p.addRoundRect(r, &radii[0].fX);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002948 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002949 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002950 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002951 p.addRoundRect(r, radii[1].fX, radii[1].fY);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002952 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002953 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002954 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
2955 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
2956 SkVector save = radii[i];
2957 radii[i].set(0, 0);
2958 rr.setRectRadii(r, radii);
2959 p.addRRect(rr);
2960 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2961 radii[i] = save;
2962 }
2963 p.addRoundRect(r, 0, 0);
2964 SkRect returnedRect;
2965 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
2966 REPORTER_ASSERT(reporter, returnedRect == r);
2967 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2968 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
2969 rr.setRectRadii(r, zeroRadii);
2970 p.addRRect(rr);
2971 bool closed;
2972 SkPath::Direction dir;
2973 REPORTER_ASSERT(reporter, p.isRect(&closed, &dir));
2974 REPORTER_ASSERT(reporter, closed);
2975 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
2976 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2977 p.addRRect(rr, SkPath::kCW_Direction);
2978 p.addRRect(rr, SkPath::kCW_Direction);
2979 REPORTER_ASSERT(reporter, !p.isConvex());
2980 p.reset();
2981 p.addRRect(rr, SkPath::kCCW_Direction);
2982 p.addRRect(rr, SkPath::kCCW_Direction);
2983 REPORTER_ASSERT(reporter, !p.isConvex());
2984 p.reset();
2985 SkRect emptyR = {10, 20, 10, 30};
2986 rr.setRectRadii(emptyR, radii);
2987 p.addRRect(rr);
2988 REPORTER_ASSERT(reporter, p.isEmpty());
2989 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
2990 rr.setRectRadii(largeR, radii);
2991 p.addRRect(rr);
2992 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2993 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
2994 rr.setRectRadii(infR, radii);
2995 p.addRRect(rr);
2996 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2997 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
2998 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
2999 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003000}
3001
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003002static void test_arc(skiatest::Reporter* reporter) {
3003 SkPath p;
3004 SkRect emptyOval = {10, 20, 30, 20};
3005 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3006 p.addArc(emptyOval, 1, 2);
3007 REPORTER_ASSERT(reporter, p.isEmpty());
3008 p.reset();
3009 SkRect oval = {10, 20, 30, 40};
3010 p.addArc(oval, 1, 0);
3011 REPORTER_ASSERT(reporter, p.isEmpty());
3012 p.reset();
3013 SkPath cwOval;
3014 cwOval.addOval(oval);
3015 p.addArc(oval, 1, 360);
3016 REPORTER_ASSERT(reporter, p == cwOval);
3017 p.reset();
3018 SkPath ccwOval;
3019 ccwOval.addOval(oval, SkPath::kCCW_Direction);
3020 p.addArc(oval, 1, -360);
3021 REPORTER_ASSERT(reporter, p == ccwOval);
3022 p.reset();
3023 p.addArc(oval, 1, 180);
3024 REPORTER_ASSERT(reporter, p.isConvex());
3025 REPORTER_ASSERT(reporter, p.cheapIsDirection(SkPath::kCW_Direction));
3026 p.setConvexity(SkPath::kUnknown_Convexity);
3027 REPORTER_ASSERT(reporter, p.isConvex());
3028}
3029
3030static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3031 SkScalar x0, SkScalar y0) {
3032 SkPoint pts[4];
3033 SkPath::Verb v = iter->next(pts);
3034 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
3035 REPORTER_ASSERT(reporter, pts[0].fX == x0);
3036 REPORTER_ASSERT(reporter, pts[0].fY == y0);
3037}
3038
3039static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3040 SkScalar x1, SkScalar y1) {
3041 SkPoint pts[4];
3042 SkPath::Verb v = iter->next(pts);
3043 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
3044 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3045 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3046}
3047
3048static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3049 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3050 SkPoint pts[4];
3051 SkPath::Verb v = iter->next(pts);
3052 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
3053 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3054 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3055 REPORTER_ASSERT(reporter, pts[2].fX == x2);
3056 REPORTER_ASSERT(reporter, pts[2].fY == y2);
3057}
3058
3059static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3060 SkPoint pts[4];
3061 SkPath::Verb v = iter->next(pts);
3062 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
3063}
3064
3065static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3066 check_done(reporter, p, iter);
3067 p->reset();
3068}
3069
3070static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3071 SkScalar x0, SkScalar y0) {
3072 SkPath::RawIter iter(*p);
3073 check_move(reporter, &iter, x0, y0);
3074 check_done_and_reset(reporter, p, &iter);
3075}
3076
3077static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3078 SkScalar x1, SkScalar y1) {
3079 SkPath::RawIter iter(*p);
3080 check_move(reporter, &iter, 0, 0);
3081 check_line(reporter, &iter, x1, y1);
3082 check_done_and_reset(reporter, p, &iter);
3083}
3084
3085static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3086 SkScalar x1, SkScalar y1) {
3087 SkPath::RawIter iter(*p);
3088 check_move(reporter, &iter, 0, 0);
3089 check_line(reporter, &iter, x1, y1);
3090 check_done(reporter, p, &iter);
3091}
3092
3093static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3094 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3095 SkPath::RawIter iter(*p);
3096 check_move(reporter, &iter, 0, 0);
3097 check_line(reporter, &iter, x1, y1);
3098 check_line(reporter, &iter, x2, y2);
3099 check_done_and_reset(reporter, p, &iter);
3100}
3101
3102static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3103 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3104 SkPath::RawIter iter(*p);
3105 check_move(reporter, &iter, 0, 0);
3106 check_quad(reporter, &iter, x1, y1, x2, y2);
3107 check_done_and_reset(reporter, p, &iter);
3108}
3109
3110static void test_arcTo(skiatest::Reporter* reporter) {
3111 SkPath p;
3112 p.arcTo(0, 0, 1, 2, 1);
3113 check_path_is_line_and_reset(reporter, &p, 0, 0);
3114 p.arcTo(1, 2, 1, 2, 1);
3115 check_path_is_line_and_reset(reporter, &p, 1, 2);
3116 p.arcTo(1, 2, 3, 4, 0);
3117 check_path_is_line_and_reset(reporter, &p, 1, 2);
3118 p.arcTo(1, 2, 0, 0, 1);
3119 check_path_is_line_and_reset(reporter, &p, 1, 2);
3120 p.arcTo(1, 0, 1, 1, 1);
3121 SkPoint pt;
3122 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3123 p.reset();
3124 p.arcTo(1, 0, 1, -1, 1);
3125 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3126 p.reset();
3127 SkRect oval = {1, 2, 3, 4};
3128 p.arcTo(oval, 0, 0, true);
3129 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3130 p.arcTo(oval, 0, 0, false);
3131 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3132 p.arcTo(oval, 360, 0, true);
3133 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3134 p.arcTo(oval, 360, 0, false);
3135 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3136 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003137 p.arcTo(oval, 0, sweep, false);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003138 REPORTER_ASSERT(reporter, p.getBounds() == oval);
3139 sweep += delta;
3140 delta /= 2;
3141 }
3142 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003143 p.arcTo(oval, 0, sweep, false);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003144 REPORTER_ASSERT(reporter, p.getBounds() == oval);
3145 sweep -= delta;
3146 delta /= 2;
3147 }
3148 SkRect noOvalWidth = {1, 2, 0, 3};
3149 p.reset();
3150 p.arcTo(noOvalWidth, 0, 360, false);
3151 REPORTER_ASSERT(reporter, p.isEmpty());
3152
3153 SkRect noOvalHeight = {1, 2, 3, 1};
3154 p.reset();
3155 p.arcTo(noOvalHeight, 0, 360, false);
3156 REPORTER_ASSERT(reporter, p.isEmpty());
3157}
3158
3159static void test_addPath(skiatest::Reporter* reporter) {
3160 SkPath p, q;
3161 p.lineTo(1, 2);
3162 q.moveTo(4, 4);
3163 q.lineTo(7, 8);
3164 q.conicTo(8, 7, 6, 5, 0.5f);
3165 q.quadTo(6, 7, 8, 6);
3166 q.cubicTo(5, 6, 7, 8, 7, 5);
3167 q.close();
3168 p.addPath(q, -4, -4);
3169 SkRect expected = {0, 0, 4, 4};
3170 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3171 p.reset();
3172 p.reverseAddPath(q);
3173 SkRect reverseExpected = {4, 4, 8, 8};
3174 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3175}
3176
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003177static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3178 SkPath p, q;
3179 if (explicitMoveTo) {
3180 p.moveTo(1, 1);
3181 }
3182 p.lineTo(1, 2);
3183 if (explicitMoveTo) {
3184 q.moveTo(2, 1);
3185 }
3186 q.lineTo(2, 2);
3187 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3188 uint8_t verbs[4];
3189 int verbcount = p.getVerbs(verbs, 4);
3190 REPORTER_ASSERT(reporter, verbcount == 4);
3191 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3192 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3193 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3194 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3195}
3196
3197static void test_extendClosedPath(skiatest::Reporter* reporter) {
3198 SkPath p, q;
3199 p.moveTo(1, 1);
3200 p.lineTo(1, 2);
3201 p.lineTo(2, 2);
3202 p.close();
3203 q.moveTo(2, 1);
3204 q.lineTo(2, 3);
3205 p.addPath(q, SkPath::kExtend_AddPathMode);
3206 uint8_t verbs[7];
3207 int verbcount = p.getVerbs(verbs, 7);
3208 REPORTER_ASSERT(reporter, verbcount == 7);
3209 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3210 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3211 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3212 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3213 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3214 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3215 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3216
3217 SkPoint pt;
3218 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3219 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3220 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3221}
3222
3223static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3224 SkPath p, q, r;
3225 // case 1: dst is empty
3226 p.moveTo(2, 1);
3227 p.lineTo(2, 3);
3228 q.addPath(p, mode);
3229 REPORTER_ASSERT(reporter, q == p);
3230 // case 2: src is empty
3231 p.addPath(r, mode);
3232 REPORTER_ASSERT(reporter, q == p);
3233 // case 3: src and dst are empty
3234 q.reset();
3235 q.addPath(r, mode);
3236 REPORTER_ASSERT(reporter, q.isEmpty());
skia.committer@gmail.com877c4492014-02-12 03:02:04 +00003237}
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003238
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003239static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3240 SkPath p;
3241 p.conicTo(1, 2, 3, 4, -1);
3242 check_path_is_line_and_reset(reporter, &p, 3, 4);
3243 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3244 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
3245 p.conicTo(1, 2, 3, 4, 1);
3246 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
3247}
3248
3249static void test_get_point(skiatest::Reporter* reporter) {
3250 SkPath p;
3251 SkPoint pt = p.getPoint(0);
3252 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
3253 REPORTER_ASSERT(reporter, !p.getLastPt(NULL));
3254 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
3255 p.setLastPt(10, 10);
3256 pt = p.getPoint(0);
3257 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
3258 REPORTER_ASSERT(reporter, p.getLastPt(NULL));
3259 p.rMoveTo(10, 10);
3260 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
3261}
3262
3263static void test_contains(skiatest::Reporter* reporter) {
3264 SkPath p;
3265 p.setFillType(SkPath::kInverseWinding_FillType);
3266 REPORTER_ASSERT(reporter, p.contains(0, 0));
3267 p.setFillType(SkPath::kWinding_FillType);
3268 REPORTER_ASSERT(reporter, !p.contains(0, 0));
3269 p.moveTo(4, 4);
3270 p.lineTo(6, 8);
3271 p.lineTo(8, 4);
3272 // test quick reject
3273 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3274 REPORTER_ASSERT(reporter, !p.contains(0, 4));
3275 REPORTER_ASSERT(reporter, !p.contains(4, 10));
3276 REPORTER_ASSERT(reporter, !p.contains(10, 4));
3277 // test various crossings in x
3278 REPORTER_ASSERT(reporter, !p.contains(5, 7));
3279 REPORTER_ASSERT(reporter, p.contains(6, 7));
3280 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3281 p.reset();
3282 p.moveTo(4, 4);
3283 p.lineTo(8, 6);
3284 p.lineTo(4, 8);
3285 // test various crossings in y
3286 REPORTER_ASSERT(reporter, !p.contains(7, 5));
3287 REPORTER_ASSERT(reporter, p.contains(7, 6));
3288 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3289 // test quads
3290 p.reset();
3291 p.moveTo(4, 4);
3292 p.quadTo(6, 6, 8, 8);
3293 p.quadTo(6, 8, 4, 8);
3294 p.quadTo(4, 6, 4, 4);
3295 REPORTER_ASSERT(reporter, p.contains(5, 6));
3296 REPORTER_ASSERT(reporter, !p.contains(6, 5));
3297
3298 p.reset();
3299 p.moveTo(6, 6);
3300 p.quadTo(8, 8, 6, 8);
3301 p.quadTo(4, 8, 4, 6);
3302 p.quadTo(4, 4, 6, 6);
3303 REPORTER_ASSERT(reporter, p.contains(5, 6));
3304 REPORTER_ASSERT(reporter, !p.contains(6, 5));
3305
3306#define CONIC_CONTAINS_BUG_FIXED 0
3307#if CONIC_CONTAINS_BUG_FIXED
3308 p.reset();
3309 p.moveTo(4, 4);
3310 p.conicTo(6, 6, 8, 8, 0.5f);
3311 p.conicTo(6, 8, 4, 8, 0.5f);
3312 p.conicTo(4, 6, 4, 4, 0.5f);
3313 REPORTER_ASSERT(reporter, p.contains(5, 6));
3314 REPORTER_ASSERT(reporter, !p.contains(6, 5));
3315#endif
3316
3317 // test cubics
3318 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
3319 for (int i = 0; i < 3; ++i) {
3320 p.reset();
3321 p.setFillType(SkPath::kEvenOdd_FillType);
3322 p.moveTo(pts[i].fX, pts[i].fY);
3323 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);
3324 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);
3325 p.close();
3326 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
3327 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
3328 }
3329}
3330
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003331class PathRefTest_Private {
3332public:
3333 static void TestPathRef(skiatest::Reporter* reporter) {
3334 static const int kRepeatCnt = 10;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003335
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003336 SkAutoTUnref<SkPathRef> pathRef(SkNEW(SkPathRef));
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003337
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003338 SkPathRef::Editor ed(&pathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003339
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003340 {
3341 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
3342 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3343 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
3344 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
3345 for (int i = 0; i < kRepeatCnt; ++i) {
3346 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
3347 }
3348 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003349 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003350
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003351 {
3352 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
3353 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3354 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
3355 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
3356 for (int i = 0; i < kRepeatCnt; ++i) {
3357 REPORTER_ASSERT(reporter, SkPath::kLine_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::kQuad_Verb, kRepeatCnt);
3364 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3365 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
3366 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
3367 for (int i = 0; i < kRepeatCnt; ++i) {
3368 REPORTER_ASSERT(reporter, SkPath::kQuad_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 SkScalar* weights = NULL;
3375 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
3376 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3377 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
3378 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
3379 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
3380 REPORTER_ASSERT(reporter, NULL != weights);
3381 for (int i = 0; i < kRepeatCnt; ++i) {
3382 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
3383 }
3384 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003385 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003386
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003387 {
3388 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
3389 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3390 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
3391 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
3392 for (int i = 0; i < kRepeatCnt; ++i) {
3393 REPORTER_ASSERT(reporter, SkPath::kCubic_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};
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003399
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003400static void test_operatorEqual(skiatest::Reporter* reporter) {
3401 SkPath a;
3402 SkPath b;
3403 REPORTER_ASSERT(reporter, a == a);
3404 REPORTER_ASSERT(reporter, a == b);
3405 a.setFillType(SkPath::kInverseWinding_FillType);
3406 REPORTER_ASSERT(reporter, a != b);
3407 a.reset();
3408 REPORTER_ASSERT(reporter, a == b);
3409 a.lineTo(1, 1);
3410 REPORTER_ASSERT(reporter, a != b);
3411 a.reset();
3412 REPORTER_ASSERT(reporter, a == b);
3413 a.lineTo(1, 1);
3414 b.lineTo(1, 2);
3415 REPORTER_ASSERT(reporter, a != b);
3416 a.reset();
3417 a.lineTo(1, 2);
3418 REPORTER_ASSERT(reporter, a == b);
3419}
3420
caryclark66a5d8b2014-06-24 08:30:15 -07003421static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force,
3422 const char* str) {
3423 SkDynamicMemoryWStream wStream;
3424 path.dump(&wStream, force);
3425 SkAutoDataUnref data(wStream.copyToData());
3426 REPORTER_ASSERT(reporter, data->size() == strlen(str));
3427 REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
3428}
3429
3430static void test_dump(skiatest::Reporter* reporter) {
3431 SkPath p;
3432 compare_dump(reporter, p, false, "");
3433 compare_dump(reporter, p, true, "");
3434 p.moveTo(1, 2);
3435 p.lineTo(3, 4);
3436 compare_dump(reporter, p, false, "path.moveTo(1, 2);\n"
3437 "path.lineTo(3, 4);\n");
3438 compare_dump(reporter, p, true, "path.moveTo(1, 2);\n"
3439 "path.lineTo(3, 4);\n"
3440 "path.lineTo(1, 2);\n"
3441 "path.close();\n");
3442 p.reset();
3443 p.moveTo(1, 2);
3444 p.quadTo(3, 4, 5, 6);
3445 compare_dump(reporter, p, false, "path.moveTo(1, 2);\n"
3446 "path.quadTo(3, 4, 5, 6);\n");
3447 p.reset();
3448 p.moveTo(1, 2);
3449 p.conicTo(3, 4, 5, 6, 0.5f);
3450 compare_dump(reporter, p, false, "path.moveTo(1, 2);\n"
3451 "path.conicTo(3, 4, 5, 6, 0.5f);\n");
3452 p.reset();
3453 p.moveTo(1, 2);
3454 p.cubicTo(3, 4, 5, 6, 7, 8);
3455 compare_dump(reporter, p, false, "path.moveTo(1, 2);\n"
3456 "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
3457}
3458
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003459class PathTest_Private {
3460public:
3461 static void TestPathTo(skiatest::Reporter* reporter) {
3462 SkPath p, q;
3463 p.lineTo(4, 4);
3464 p.reversePathTo(q);
3465 check_path_is_line(reporter, &p, 4, 4);
3466 q.moveTo(-4, -4);
3467 p.reversePathTo(q);
3468 check_path_is_line(reporter, &p, 4, 4);
3469 q.lineTo(7, 8);
3470 q.conicTo(8, 7, 6, 5, 0.5f);
3471 q.quadTo(6, 7, 8, 6);
3472 q.cubicTo(5, 6, 7, 8, 7, 5);
3473 q.close();
3474 p.reversePathTo(q);
3475 SkRect reverseExpected = {-4, -4, 8, 8};
3476 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3477 }
3478};
3479
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00003480DEF_TEST(Paths, reporter) {
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +00003481 test_path_crbug364224();
3482
bungeman@google.coma5809a32013-06-21 15:13:34 +00003483 SkTSize<SkScalar>::Make(3,4);
3484
3485 SkPath p, empty;
3486 SkRect bounds, bounds2;
3487 test_empty(reporter, p);
reed@android.com3abec1d2009-03-02 05:36:20 +00003488
reed@android.comd252db02009-04-01 18:31:44 +00003489 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00003490
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003491 // this triggers a code path in SkPath::operator= which is otherwise unexercised
3492 SkPath& self = p;
3493 p = self;
3494
3495 // this triggers a code path in SkPath::swap which is otherwise unexercised
3496 p.swap(self);
3497
reed@android.com3abec1d2009-03-02 05:36:20 +00003498 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00003499
reed@android.com6b82d1a2009-06-03 02:35:01 +00003500 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
3501 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00003502 // we have quads or cubics
3503 REPORTER_ASSERT(reporter, p.getSegmentMasks() & kCurveSegmentMask);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003504 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00003505
reed@android.com6b82d1a2009-06-03 02:35:01 +00003506 p.reset();
bungeman@google.coma5809a32013-06-21 15:13:34 +00003507 test_empty(reporter, p);
reed@google.com10296cc2011-09-21 12:29:05 +00003508
reed@android.com6b82d1a2009-06-03 02:35:01 +00003509 p.addOval(bounds);
3510 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003511 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00003512
bungeman@google.coma5809a32013-06-21 15:13:34 +00003513 p.rewind();
3514 test_empty(reporter, p);
3515
reed@android.com3abec1d2009-03-02 05:36:20 +00003516 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00003517 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00003518 // we have only lines
3519 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003520 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00003521
bungeman@google.coma5809a32013-06-21 15:13:34 +00003522 REPORTER_ASSERT(reporter, p != empty);
3523 REPORTER_ASSERT(reporter, !(p == empty));
reed@android.com3abec1d2009-03-02 05:36:20 +00003524
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003525 // do getPoints and getVerbs return the right result
3526 REPORTER_ASSERT(reporter, p.getPoints(NULL, 0) == 4);
3527 REPORTER_ASSERT(reporter, p.getVerbs(NULL, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00003528 SkPoint pts[4];
3529 int count = p.getPoints(pts, 4);
3530 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003531 uint8_t verbs[6];
3532 verbs[5] = 0xff;
3533 p.getVerbs(verbs, 5);
3534 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
3535 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
3536 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
3537 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
3538 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
3539 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00003540 bounds2.set(pts, 4);
3541 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00003542
reed@android.com3abec1d2009-03-02 05:36:20 +00003543 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
3544 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00003545 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00003546
reed@android.com3abec1d2009-03-02 05:36:20 +00003547 REPORTER_ASSERT(reporter, p.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00003548 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00003549 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
3550 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00003551
reed@android.com3abec1d2009-03-02 05:36:20 +00003552 // now force p to not be a rect
3553 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
3554 p.addRect(bounds);
3555 REPORTER_ASSERT(reporter, !p.isRect(NULL));
reed@android.com3abec1d2009-03-02 05:36:20 +00003556
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003557 test_operatorEqual(reporter);
reed@google.com7e6c4d12012-05-10 14:05:43 +00003558 test_isLine(reporter);
3559 test_isRect(reporter);
caryclark@google.com56f233a2012-11-19 13:06:06 +00003560 test_isNestedRects(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003561 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00003562 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00003563 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00003564 test_convexity2(reporter);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00003565 test_conservativelyContains(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00003566 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003567 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00003568 test_flattening(reporter);
3569 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00003570 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003571 test_iter(reporter);
3572 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003573 test_circle(reporter);
3574 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00003575 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00003576 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00003577 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00003578 test_isfinite_after_transform(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00003579 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00003580 test_arb_zero_rad_round_rect_is_rect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003581 test_addrect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00003582 test_addrect_isfinite(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00003583 test_tricky_cubic();
3584 test_clipped_cubic();
3585 test_crbug_170666();
reed@google.com7a90daf2013-04-10 18:44:00 +00003586 test_bad_cubic_crbug229478();
reed@google.com3eff3592013-05-08 21:08:21 +00003587 test_bad_cubic_crbug234190();
mtklein@google.com9c9d4a72013-08-07 19:17:53 +00003588 test_android_specific_behavior(reporter);
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +00003589 test_gen_id(reporter);
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +00003590 test_path_close_issue1474(reporter);
reed@google.comb58ba892013-10-15 15:44:35 +00003591 test_path_to_region(reporter);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003592 test_rrect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003593 test_arc(reporter);
3594 test_arcTo(reporter);
3595 test_addPath(reporter);
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003596 test_addPathMode(reporter, false, false);
3597 test_addPathMode(reporter, true, false);
3598 test_addPathMode(reporter, false, true);
3599 test_addPathMode(reporter, true, true);
3600 test_extendClosedPath(reporter);
3601 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
3602 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003603 test_conicTo_special_case(reporter);
3604 test_get_point(reporter);
3605 test_contains(reporter);
3606 PathTest_Private::TestPathTo(reporter);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003607 PathRefTest_Private::TestPathRef(reporter);
caryclark66a5d8b2014-06-24 08:30:15 -07003608 test_dump(reporter);
piotaixrfac4e0e2014-08-26 11:59:04 -07003609 test_path_crbugskia2820(reporter);
reed@android.com3abec1d2009-03-02 05:36:20 +00003610}