blob: 3de10cb10642696b504ee605f6299520ee2edbd4 [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"
reed@google.com8cae8352012-09-14 15:18:41 +000018#include "SkSurface.h"
mtklein@google.com9c9d4a72013-08-07 19:17:53 +000019#include "SkTypes.h"
20#include "SkWriter32.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000021#include "Test.h"
reed@google.com8cae8352012-09-14 15:18:41 +000022
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +000023static void make_path_crbug364224(SkPath* path) {
24 path->reset();
25 path->moveTo(3.747501373f, 2.724499941f);
26 path->lineTo(3.747501373f, 3.75f);
27 path->cubicTo(3.747501373f, 3.88774991f, 3.635501385f, 4.0f, 3.497501373f, 4.0f);
28 path->lineTo(0.7475013733f, 4.0f);
29 path->cubicTo(0.6095013618f, 4.0f, 0.4975013733f, 3.88774991f, 0.4975013733f, 3.75f);
30 path->lineTo(0.4975013733f, 1.0f);
31 path->cubicTo(0.4975013733f, 0.8622499704f, 0.6095013618f, 0.75f, 0.7475013733f,0.75f);
32 path->lineTo(3.497501373f, 0.75f);
33 path->cubicTo(3.50275135f, 0.75f, 3.5070014f, 0.7527500391f, 3.513001442f, 0.753000021f);
34 path->lineTo(3.715001345f, 0.5512499809f);
35 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
36 path->lineTo(0.7475013733f, 0.4999999702f);
37 path->cubicTo(0.4715013802f, 0.4999999702f, 0.2475013733f, 0.7239999771f, 0.2475013733f, 1.0f);
38 path->lineTo(0.2475013733f, 3.75f);
39 path->cubicTo(0.2475013733f, 4.026000023f, 0.4715013504f, 4.25f, 0.7475013733f, 4.25f);
40 path->lineTo(3.497501373f, 4.25f);
41 path->cubicTo(3.773501396f, 4.25f, 3.997501373f, 4.026000023f, 3.997501373f, 3.75f);
42 path->lineTo(3.997501373f, 2.474750042f);
43 path->lineTo(3.747501373f, 2.724499941f);
44 path->close();
45}
46
47static void make_path_crbug364224_simplified(SkPath* path) {
48 path->moveTo(3.747501373f, 2.724499941f);
49 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
50 path->close();
51}
52
53static void test_path_crbug364224() {
54 SkPath path;
55 SkPaint paint;
56 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(84, 88));
57 SkCanvas* canvas = surface->getCanvas();
58
59 make_path_crbug364224_simplified(&path);
60 canvas->drawPath(path, paint);
61
62 make_path_crbug364224(&path);
63 canvas->drawPath(path, paint);
64}
65
reed@google.comcc8be772013-10-15 15:35:29 +000066static void make_path0(SkPath* path) {
67 // from * https://code.google.com/p/skia/issues/detail?id=1706
68
69 path->moveTo(146.939f, 1012.84f);
70 path->lineTo(181.747f, 1009.18f);
71 path->lineTo(182.165f, 1013.16f);
72 path->lineTo(147.357f, 1016.82f);
73 path->lineTo(146.939f, 1012.84f);
74 path->close();
75}
76
77static void make_path1(SkPath* path) {
78 path->addRect(SkRect::MakeXYWH(10, 10, 10, 1));
79}
80
81typedef void (*PathProc)(SkPath*);
82
83/*
84 * Regression test: we used to crash (overwrite internal storage) during
85 * construction of the region when the path was INVERSE. That is now fixed,
86 * so test these regions (which used to assert/crash).
87 *
88 * https://code.google.com/p/skia/issues/detail?id=1706
89 */
90static void test_path_to_region(skiatest::Reporter* reporter) {
91 PathProc procs[] = {
92 make_path0,
93 make_path1,
94 };
skia.committer@gmail.com47262912013-10-16 07:02:24 +000095
reed@google.comcc8be772013-10-15 15:35:29 +000096 SkRegion clip;
97 clip.setRect(0, 0, 1255, 1925);
skia.committer@gmail.com47262912013-10-16 07:02:24 +000098
reed@google.comcc8be772013-10-15 15:35:29 +000099 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
100 SkPath path;
101 procs[i](&path);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000102
reed@google.comcc8be772013-10-15 15:35:29 +0000103 SkRegion rgn;
104 rgn.setPath(path, clip);
105 path.toggleInverseFillType();
106 rgn.setPath(path, clip);
107 }
108}
109
caryclark@google.com56f233a2012-11-19 13:06:06 +0000110#if defined(WIN32)
111 #define SUPPRESS_VISIBILITY_WARNING
112#else
113 #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
114#endif
115
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +0000116static void test_path_close_issue1474(skiatest::Reporter* reporter) {
117 // This test checks that r{Line,Quad,Conic,Cubic}To following a close()
118 // are relative to the point we close to, not relative to the point we close from.
119 SkPath path;
120 SkPoint last;
121
122 // Test rLineTo().
123 path.rLineTo(0, 100);
124 path.rLineTo(100, 0);
125 path.close(); // Returns us back to 0,0.
126 path.rLineTo(50, 50); // This should go to 50,50.
127
128 path.getLastPt(&last);
129 REPORTER_ASSERT(reporter, 50 == last.fX);
130 REPORTER_ASSERT(reporter, 50 == last.fY);
131
132 // Test rQuadTo().
133 path.rewind();
134 path.rLineTo(0, 100);
135 path.rLineTo(100, 0);
136 path.close();
137 path.rQuadTo(50, 50, 75, 75);
138
139 path.getLastPt(&last);
140 REPORTER_ASSERT(reporter, 75 == last.fX);
141 REPORTER_ASSERT(reporter, 75 == last.fY);
142
143 // Test rConicTo().
144 path.rewind();
145 path.rLineTo(0, 100);
146 path.rLineTo(100, 0);
147 path.close();
148 path.rConicTo(50, 50, 85, 85, 2);
149
150 path.getLastPt(&last);
151 REPORTER_ASSERT(reporter, 85 == last.fX);
152 REPORTER_ASSERT(reporter, 85 == last.fY);
153
154 // Test rCubicTo().
155 path.rewind();
156 path.rLineTo(0, 100);
157 path.rLineTo(100, 0);
158 path.close();
159 path.rCubicTo(50, 50, 85, 85, 95, 95);
160
161 path.getLastPt(&last);
162 REPORTER_ASSERT(reporter, 95 == last.fX);
163 REPORTER_ASSERT(reporter, 95 == last.fY);
164}
165
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000166static void test_android_specific_behavior(skiatest::Reporter* reporter) {
167#ifdef SK_BUILD_FOR_ANDROID
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000168 // Make sure we treat fGenerationID and fSourcePath correctly for each of
169 // copy, assign, rewind, reset, and swap.
170 SkPath original, source, anotherSource;
171 original.setSourcePath(&source);
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000172 original.moveTo(0, 0);
173 original.lineTo(1, 1);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000174 REPORTER_ASSERT(reporter, original.getSourcePath() == &source);
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000175
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000176 uint32_t copyID, assignID;
177
178 // Test copy constructor. Copy generation ID, copy source path.
179 SkPath copy(original);
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000180 REPORTER_ASSERT(reporter, copy.getGenerationID() == original.getGenerationID());
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000181 REPORTER_ASSERT(reporter, copy.getSourcePath() == original.getSourcePath());
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000182
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000183 // Test assigment operator. Change generation ID, copy source path.
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000184 SkPath assign;
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000185 assignID = assign.getGenerationID();
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000186 assign = original;
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000187 REPORTER_ASSERT(reporter, assign.getGenerationID() != assignID);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000188 REPORTER_ASSERT(reporter, assign.getSourcePath() == original.getSourcePath());
189
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000190 // Test rewind. Change generation ID, don't touch source path.
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000191 copyID = copy.getGenerationID();
192 copy.rewind();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000193 REPORTER_ASSERT(reporter, copy.getGenerationID() != copyID);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000194 REPORTER_ASSERT(reporter, copy.getSourcePath() == original.getSourcePath());
195
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000196 // Test reset. Change generation ID, don't touch source path.
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000197 assignID = assign.getGenerationID();
198 assign.reset();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000199 REPORTER_ASSERT(reporter, assign.getGenerationID() != assignID);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000200 REPORTER_ASSERT(reporter, assign.getSourcePath() == original.getSourcePath());
201
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000202 // Test swap. Swap the generation IDs, swap source paths.
203 copy.reset();
204 copy.moveTo(2, 2);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000205 copy.setSourcePath(&anotherSource);
206 copyID = copy.getGenerationID();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000207 assign.moveTo(3, 3);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000208 assignID = assign.getGenerationID();
209 copy.swap(assign);
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000210 REPORTER_ASSERT(reporter, copy.getGenerationID() != copyID);
211 REPORTER_ASSERT(reporter, assign.getGenerationID() != assignID);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000212 REPORTER_ASSERT(reporter, copy.getSourcePath() == original.getSourcePath());
213 REPORTER_ASSERT(reporter, assign.getSourcePath() == &anotherSource);
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000214#endif
215}
216
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000217static void test_gen_id(skiatest::Reporter* reporter) {
218 SkPath a, b;
219 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
220
221 a.moveTo(0, 0);
222 const uint32_t z = a.getGenerationID();
223 REPORTER_ASSERT(reporter, z != b.getGenerationID());
224
225 a.reset();
226 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
227
228 a.moveTo(1, 1);
229 const uint32_t y = a.getGenerationID();
230 REPORTER_ASSERT(reporter, z != y);
231
232 b.moveTo(2, 2);
233 const uint32_t x = b.getGenerationID();
234 REPORTER_ASSERT(reporter, x != y && x != z);
235
236 a.swap(b);
237 REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x);
238
239 b = a;
240 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
241
242 SkPath c(a);
243 REPORTER_ASSERT(reporter, c.getGenerationID() == x);
244
245 c.lineTo(3, 3);
246 const uint32_t w = c.getGenerationID();
247 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
248 REPORTER_ASSERT(reporter, a.getGenerationID() == x);
249 REPORTER_ASSERT(reporter, w != x);
250
251#ifdef SK_BUILD_FOR_ANDROID
252 static bool kExpectGenIDToIgnoreFill = false;
253#else
254 static bool kExpectGenIDToIgnoreFill = true;
255#endif
256
257 c.toggleInverseFillType();
258 const uint32_t v = c.getGenerationID();
259 REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill);
260
261 c.rewind();
262 REPORTER_ASSERT(reporter, v != c.getGenerationID());
263}
264
reed@google.com3eff3592013-05-08 21:08:21 +0000265// This used to assert in the debug build, as the edges did not all line-up.
266static void test_bad_cubic_crbug234190() {
267 SkPath path;
268 path.moveTo(13.8509f, 3.16858f);
269 path.cubicTo(-2.35893e+08f, -4.21044e+08f,
270 -2.38991e+08f, -4.26573e+08f,
271 -2.41016e+08f, -4.30188e+08f);
272
273 SkPaint paint;
274 paint.setAntiAlias(true);
reed@google.comd28ba802013-09-20 19:33:52 +0000275 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(84, 88));
reed@google.com3eff3592013-05-08 21:08:21 +0000276 surface->getCanvas()->drawPath(path, paint);
277}
278
reed@google.com7a90daf2013-04-10 18:44:00 +0000279static void test_bad_cubic_crbug229478() {
280 const SkPoint pts[] = {
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000281 { 4595.91064f, -11596.9873f },
282 { 4597.2168f, -11595.9414f },
283 { 4598.52344f, -11594.8955f },
284 { 4599.83008f, -11593.8496f },
reed@google.com7a90daf2013-04-10 18:44:00 +0000285 };
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000286
reed@google.com7a90daf2013-04-10 18:44:00 +0000287 SkPath path;
288 path.moveTo(pts[0]);
289 path.cubicTo(pts[1], pts[2], pts[3]);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000290
reed@google.com7a90daf2013-04-10 18:44:00 +0000291 SkPaint paint;
292 paint.setStyle(SkPaint::kStroke_Style);
293 paint.setStrokeWidth(20);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000294
reed@google.com7a90daf2013-04-10 18:44:00 +0000295 SkPath dst;
296 // Before the fix, this would infinite-recurse, and run out of stack
297 // because we would keep trying to subdivide a degenerate cubic segment.
298 paint.getFillPath(path, &dst, NULL);
299}
300
reed@google.com64d62952013-01-18 17:49:28 +0000301static void build_path_170666(SkPath& path) {
302 path.moveTo(17.9459f, 21.6344f);
303 path.lineTo(139.545f, -47.8105f);
304 path.lineTo(139.545f, -47.8105f);
305 path.lineTo(131.07f, -47.3888f);
306 path.lineTo(131.07f, -47.3888f);
307 path.lineTo(122.586f, -46.9532f);
308 path.lineTo(122.586f, -46.9532f);
309 path.lineTo(18076.6f, 31390.9f);
310 path.lineTo(18076.6f, 31390.9f);
311 path.lineTo(18085.1f, 31390.5f);
312 path.lineTo(18085.1f, 31390.5f);
313 path.lineTo(18076.6f, 31390.9f);
314 path.lineTo(18076.6f, 31390.9f);
315 path.lineTo(17955, 31460.3f);
316 path.lineTo(17955, 31460.3f);
317 path.lineTo(17963.5f, 31459.9f);
318 path.lineTo(17963.5f, 31459.9f);
319 path.lineTo(17971.9f, 31459.5f);
320 path.lineTo(17971.9f, 31459.5f);
321 path.lineTo(17.9551f, 21.6205f);
322 path.lineTo(17.9551f, 21.6205f);
323 path.lineTo(9.47091f, 22.0561f);
324 path.lineTo(9.47091f, 22.0561f);
325 path.lineTo(17.9459f, 21.6344f);
326 path.lineTo(17.9459f, 21.6344f);
327 path.close();path.moveTo(0.995934f, 22.4779f);
328 path.lineTo(0.986725f, 22.4918f);
329 path.lineTo(0.986725f, 22.4918f);
330 path.lineTo(17955, 31460.4f);
331 path.lineTo(17955, 31460.4f);
332 path.lineTo(17971.9f, 31459.5f);
333 path.lineTo(17971.9f, 31459.5f);
334 path.lineTo(18093.6f, 31390.1f);
335 path.lineTo(18093.6f, 31390.1f);
336 path.lineTo(18093.6f, 31390);
337 path.lineTo(18093.6f, 31390);
338 path.lineTo(139.555f, -47.8244f);
339 path.lineTo(139.555f, -47.8244f);
340 path.lineTo(122.595f, -46.9671f);
341 path.lineTo(122.595f, -46.9671f);
342 path.lineTo(0.995934f, 22.4779f);
343 path.lineTo(0.995934f, 22.4779f);
344 path.close();
345 path.moveTo(5.43941f, 25.5223f);
346 path.lineTo(798267, -28871.1f);
347 path.lineTo(798267, -28871.1f);
348 path.lineTo(3.12512e+06f, -113102);
349 path.lineTo(3.12512e+06f, -113102);
350 path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813);
351 path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f);
352 path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f);
353 path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f);
354 path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f);
355 path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f);
356 path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f);
357 path.lineTo(2.78271e+08f, -1.00733e+07f);
358 path.lineTo(2.78271e+08f, -1.00733e+07f);
359 path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f);
360 path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f);
361 path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f);
362 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
363 path.lineTo(2.77473e+08f, -1.00444e+07f);
364 path.lineTo(2.77473e+08f, -1.00444e+07f);
365 path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f);
366 path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f);
367 path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f);
368 path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f);
369 path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f);
370 path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814);
371 path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103);
372 path.lineTo(798284, -28872);
373 path.lineTo(798284, -28872);
374 path.lineTo(22.4044f, 24.6677f);
375 path.lineTo(22.4044f, 24.6677f);
376 path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f);
377 path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f);
378 path.close();
379}
380
381static void build_path_simple_170666(SkPath& path) {
382 path.moveTo(126.677f, 24.1591f);
383 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
384}
385
386// This used to assert in the SK_DEBUG build, as the clip step would fail with
387// too-few interations in our cubic-line intersection code. That code now runs
388// 24 interations (instead of 16).
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000389static void test_crbug_170666() {
reed@google.com64d62952013-01-18 17:49:28 +0000390 SkPath path;
391 SkPaint paint;
392 paint.setAntiAlias(true);
393
reed@google.comd28ba802013-09-20 19:33:52 +0000394 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(1000, 1000));
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000395
reed@google.com64d62952013-01-18 17:49:28 +0000396 build_path_simple_170666(path);
397 surface->getCanvas()->drawPath(path, paint);
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000398
reed@google.com64d62952013-01-18 17:49:28 +0000399 build_path_170666(path);
400 surface->getCanvas()->drawPath(path, paint);
401}
402
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000403static void test_addrect(skiatest::Reporter* reporter) {
404 SkPath path;
405 path.lineTo(0, 0);
406 path.addRect(SkRect::MakeWH(50, 100));
407 REPORTER_ASSERT(reporter, path.isRect(NULL));
408
409 path.reset();
410 path.lineTo(FLT_EPSILON, FLT_EPSILON);
411 path.addRect(SkRect::MakeWH(50, 100));
412 REPORTER_ASSERT(reporter, !path.isRect(NULL));
413
414 path.reset();
415 path.quadTo(0, 0, 0, 0);
416 path.addRect(SkRect::MakeWH(50, 100));
417 REPORTER_ASSERT(reporter, !path.isRect(NULL));
418
419 path.reset();
420 path.conicTo(0, 0, 0, 0, 0.5f);
421 path.addRect(SkRect::MakeWH(50, 100));
422 REPORTER_ASSERT(reporter, !path.isRect(NULL));
423
424 path.reset();
425 path.cubicTo(0, 0, 0, 0, 0, 0);
426 path.addRect(SkRect::MakeWH(50, 100));
427 REPORTER_ASSERT(reporter, !path.isRect(NULL));
428}
429
reed@google.coma8790de2012-10-24 21:04:04 +0000430// Make sure we stay non-finite once we get there (unless we reset or rewind).
431static void test_addrect_isfinite(skiatest::Reporter* reporter) {
432 SkPath path;
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000433
reed@google.coma8790de2012-10-24 21:04:04 +0000434 path.addRect(SkRect::MakeWH(50, 100));
435 REPORTER_ASSERT(reporter, path.isFinite());
436
437 path.moveTo(0, 0);
438 path.lineTo(SK_ScalarInfinity, 42);
439 REPORTER_ASSERT(reporter, !path.isFinite());
440
441 path.addRect(SkRect::MakeWH(50, 100));
442 REPORTER_ASSERT(reporter, !path.isFinite());
443
444 path.reset();
445 REPORTER_ASSERT(reporter, path.isFinite());
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000446
reed@google.coma8790de2012-10-24 21:04:04 +0000447 path.addRect(SkRect::MakeWH(50, 100));
448 REPORTER_ASSERT(reporter, path.isFinite());
449}
450
reed@google.com848148e2013-01-15 15:51:59 +0000451static void build_big_path(SkPath* path, bool reducedCase) {
452 if (reducedCase) {
453 path->moveTo(577330, 1971.72f);
454 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
455 } else {
456 path->moveTo(60.1631f, 7.70567f);
457 path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
458 path->lineTo(577379, 1977.77f);
459 path->quadTo(577364, 1979.57f, 577325, 1980.26f);
460 path->quadTo(577286, 1980.95f, 577245, 1980.13f);
461 path->quadTo(577205, 1979.3f, 577187, 1977.45f);
462 path->quadTo(577168, 1975.6f, 577183, 1973.8f);
463 path->quadTo(577198, 1972, 577238, 1971.31f);
464 path->quadTo(577277, 1970.62f, 577317, 1971.45f);
465 path->quadTo(577330, 1971.72f, 577341, 1972.11f);
466 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
467 path->moveTo(306.718f, -32.912f);
468 path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
469 }
470}
471
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000472static void test_clipped_cubic() {
reed@google.comd28ba802013-09-20 19:33:52 +0000473 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(640, 480));
reed@google.com848148e2013-01-15 15:51:59 +0000474
475 // This path used to assert, because our cubic-chopping code incorrectly
476 // moved control points after the chop. This test should be run in SK_DEBUG
477 // mode to ensure that we no long assert.
478 SkPath path;
479 for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
480 build_big_path(&path, SkToBool(doReducedCase));
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +0000481
reed@google.com848148e2013-01-15 15:51:59 +0000482 SkPaint paint;
483 for (int doAA = 0; doAA <= 1; ++doAA) {
484 paint.setAntiAlias(SkToBool(doAA));
485 surface->getCanvas()->drawPath(path, paint);
486 }
487 }
488}
489
reed@google.com8cae8352012-09-14 15:18:41 +0000490// Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
491// which triggered an assert, from a tricky cubic. This test replicates that
492// example, so we can ensure that we handle it (in SkEdge.cpp), and don't
493// assert in the SK_DEBUG build.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000494static void test_tricky_cubic() {
reed@google.com8cae8352012-09-14 15:18:41 +0000495 const SkPoint pts[] = {
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +0000496 { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
497 { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
498 { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
499 { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
reed@google.com8cae8352012-09-14 15:18:41 +0000500 };
501
502 SkPath path;
503 path.moveTo(pts[0]);
504 path.cubicTo(pts[1], pts[2], pts[3]);
505
506 SkPaint paint;
507 paint.setAntiAlias(true);
508
reed@google.comd28ba802013-09-20 19:33:52 +0000509 SkSurface* surface = SkSurface::NewRasterPMColor(19, 130);
reed@google.com8cae8352012-09-14 15:18:41 +0000510 surface->getCanvas()->drawPath(path, paint);
511 surface->unref();
512}
reed@android.com3abec1d2009-03-02 05:36:20 +0000513
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000514// Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
515//
516static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
517 SkPath path;
518 path.quadTo(157, 366, 286, 208);
519 path.arcTo(37, 442, 315, 163, 957494590897113.0f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000520
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000521 SkMatrix matrix;
522 matrix.setScale(1000*1000, 1000*1000);
523
524 // Be sure that path::transform correctly updates isFinite and the bounds
525 // if the transformation overflows. The previous bug was that isFinite was
526 // set to true in this case, but the bounds were not set to empty (which
527 // they should be).
528 while (path.isFinite()) {
529 REPORTER_ASSERT(reporter, path.getBounds().isFinite());
530 REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
531 path.transform(matrix);
532 }
533 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
534
535 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
536 path.transform(matrix);
537 // we need to still be non-finite
538 REPORTER_ASSERT(reporter, !path.isFinite());
539 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
540}
541
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000542static void add_corner_arc(SkPath* path, const SkRect& rect,
543 SkScalar xIn, SkScalar yIn,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000544 int startAngle)
545{
546
547 SkScalar rx = SkMinScalar(rect.width(), xIn);
548 SkScalar ry = SkMinScalar(rect.height(), yIn);
549
550 SkRect arcRect;
551 arcRect.set(-rx, -ry, rx, ry);
552 switch (startAngle) {
553 case 0:
554 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
555 break;
556 case 90:
557 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
558 break;
559 case 180:
560 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
561 break;
562 case 270:
563 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
564 break;
565 default:
566 break;
567 }
568
569 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
570}
571
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000572static void make_arb_round_rect(SkPath* path, const SkRect& r,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000573 SkScalar xCorner, SkScalar yCorner) {
574 // we are lazy here and use the same x & y for each corner
575 add_corner_arc(path, r, xCorner, yCorner, 270);
576 add_corner_arc(path, r, xCorner, yCorner, 0);
577 add_corner_arc(path, r, xCorner, yCorner, 90);
578 add_corner_arc(path, r, xCorner, yCorner, 180);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000579 path->close();
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000580}
581
582// Chrome creates its own round rects with each corner possibly being different.
583// Performance will suffer if they are not convex.
584// Note: PathBench::ArbRoundRectBench performs almost exactly
585// the same test (but with drawing)
586static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000587 SkRandom rand;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000588 SkRect r;
589
590 for (int i = 0; i < 5000; ++i) {
591
robertphillips@google.com158618e2012-10-23 16:56:56 +0000592 SkScalar size = rand.nextUScalar1() * 30;
593 if (size < SK_Scalar1) {
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000594 continue;
595 }
596 r.fLeft = rand.nextUScalar1() * 300;
597 r.fTop = rand.nextUScalar1() * 300;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000598 r.fRight = r.fLeft + 2 * size;
599 r.fBottom = r.fTop + 2 * size;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000600
601 SkPath temp;
602
603 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
604
605 REPORTER_ASSERT(reporter, temp.isConvex());
606 }
607}
608
robertphillips@google.com158618e2012-10-23 16:56:56 +0000609// Chrome will sometimes create a 0 radius round rect. The degenerate
610// quads prevent the path from being converted to a rect
611// Note: PathBench::ArbRoundRectBench performs almost exactly
612// the same test (but with drawing)
613static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000614 SkRandom rand;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000615 SkRect r;
616
617 for (int i = 0; i < 5000; ++i) {
618
619 SkScalar size = rand.nextUScalar1() * 30;
620 if (size < SK_Scalar1) {
621 continue;
622 }
623 r.fLeft = rand.nextUScalar1() * 300;
624 r.fTop = rand.nextUScalar1() * 300;
625 r.fRight = r.fLeft + 2 * size;
626 r.fBottom = r.fTop + 2 * size;
627
628 SkPath temp;
629
630 make_arb_round_rect(&temp, r, 0, 0);
631
robertphillips@google.com158618e2012-10-23 16:56:56 +0000632 SkRect result;
633 REPORTER_ASSERT(reporter, temp.isRect(&result));
634 REPORTER_ASSERT(reporter, r == result);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000635 }
636}
637
reed@google.com0bb18bb2012-07-26 15:20:36 +0000638static void test_rect_isfinite(skiatest::Reporter* reporter) {
639 const SkScalar inf = SK_ScalarInfinity;
caryclark@google.com7abfa492013-04-12 11:59:41 +0000640 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000641 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000642
reed@google.com0bb18bb2012-07-26 15:20:36 +0000643 SkRect r;
644 r.setEmpty();
645 REPORTER_ASSERT(reporter, r.isFinite());
caryclark@google.com7abfa492013-04-12 11:59:41 +0000646 r.set(0, 0, inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000647 REPORTER_ASSERT(reporter, !r.isFinite());
648 r.set(0, 0, nan, 0);
649 REPORTER_ASSERT(reporter, !r.isFinite());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000650
reed@google.com0bb18bb2012-07-26 15:20:36 +0000651 SkPoint pts[] = {
652 { 0, 0 },
653 { SK_Scalar1, 0 },
654 { 0, SK_Scalar1 },
655 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000656
reed@google.com0bb18bb2012-07-26 15:20:36 +0000657 bool isFine = r.setBoundsCheck(pts, 3);
658 REPORTER_ASSERT(reporter, isFine);
659 REPORTER_ASSERT(reporter, !r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000660
reed@google.com0bb18bb2012-07-26 15:20:36 +0000661 pts[1].set(inf, 0);
662 isFine = r.setBoundsCheck(pts, 3);
663 REPORTER_ASSERT(reporter, !isFine);
664 REPORTER_ASSERT(reporter, r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000665
reed@google.com0bb18bb2012-07-26 15:20:36 +0000666 pts[1].set(nan, 0);
667 isFine = r.setBoundsCheck(pts, 3);
668 REPORTER_ASSERT(reporter, !isFine);
669 REPORTER_ASSERT(reporter, r.isEmpty());
670}
671
672static void test_path_isfinite(skiatest::Reporter* reporter) {
673 const SkScalar inf = SK_ScalarInfinity;
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000674 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000675 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000676
reed@google.com0bb18bb2012-07-26 15:20:36 +0000677 SkPath path;
678 REPORTER_ASSERT(reporter, path.isFinite());
679
680 path.reset();
681 REPORTER_ASSERT(reporter, path.isFinite());
682
683 path.reset();
684 path.moveTo(SK_Scalar1, 0);
685 REPORTER_ASSERT(reporter, path.isFinite());
686
687 path.reset();
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000688 path.moveTo(inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000689 REPORTER_ASSERT(reporter, !path.isFinite());
690
691 path.reset();
692 path.moveTo(nan, 0);
693 REPORTER_ASSERT(reporter, !path.isFinite());
694}
695
696static void test_isfinite(skiatest::Reporter* reporter) {
697 test_rect_isfinite(reporter);
698 test_path_isfinite(reporter);
699}
700
reed@google.com744faba2012-05-29 19:54:52 +0000701// assert that we always
702// start with a moveTo
703// only have 1 moveTo
704// only have Lines after that
705// end with a single close
706// only have (at most) 1 close
707//
708static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000709 const SkPoint srcPts[], bool expectClose) {
reed@google.com744faba2012-05-29 19:54:52 +0000710 SkPath::RawIter iter(path);
711 SkPoint pts[4];
reed@google.com744faba2012-05-29 19:54:52 +0000712
713 bool firstTime = true;
714 bool foundClose = false;
715 for (;;) {
716 switch (iter.next(pts)) {
717 case SkPath::kMove_Verb:
718 REPORTER_ASSERT(reporter, firstTime);
719 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
720 srcPts++;
721 firstTime = false;
722 break;
723 case SkPath::kLine_Verb:
724 REPORTER_ASSERT(reporter, !firstTime);
725 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
726 srcPts++;
727 break;
728 case SkPath::kQuad_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000729 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb");
reed@google.com744faba2012-05-29 19:54:52 +0000730 break;
reed@google.com277c3f82013-05-31 15:17:50 +0000731 case SkPath::kConic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000732 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb");
reed@google.com277c3f82013-05-31 15:17:50 +0000733 break;
reed@google.com744faba2012-05-29 19:54:52 +0000734 case SkPath::kCubic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000735 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb");
reed@google.com744faba2012-05-29 19:54:52 +0000736 break;
737 case SkPath::kClose_Verb:
738 REPORTER_ASSERT(reporter, !firstTime);
739 REPORTER_ASSERT(reporter, !foundClose);
740 REPORTER_ASSERT(reporter, expectClose);
741 foundClose = true;
742 break;
743 case SkPath::kDone_Verb:
744 goto DONE;
745 }
746 }
747DONE:
748 REPORTER_ASSERT(reporter, foundClose == expectClose);
749}
750
751static void test_addPoly(skiatest::Reporter* reporter) {
752 SkPoint pts[32];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000753 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000754
reed@google.com744faba2012-05-29 19:54:52 +0000755 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
756 pts[i].fX = rand.nextSScalar1();
757 pts[i].fY = rand.nextSScalar1();
758 }
759
760 for (int doClose = 0; doClose <= 1; ++doClose) {
761 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
762 SkPath path;
763 path.addPoly(pts, count, SkToBool(doClose));
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000764 test_poly(reporter, path, pts, SkToBool(doClose));
reed@google.com744faba2012-05-29 19:54:52 +0000765 }
766 }
767}
768
reed@google.com8b06f1a2012-05-29 12:03:46 +0000769static void test_strokerec(skiatest::Reporter* reporter) {
770 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
771 REPORTER_ASSERT(reporter, rec.isFillStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000772
reed@google.com8b06f1a2012-05-29 12:03:46 +0000773 rec.setHairlineStyle();
774 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000775
reed@google.com8b06f1a2012-05-29 12:03:46 +0000776 rec.setStrokeStyle(SK_Scalar1, false);
777 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000778
reed@google.com8b06f1a2012-05-29 12:03:46 +0000779 rec.setStrokeStyle(SK_Scalar1, true);
780 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000781
reed@google.com8b06f1a2012-05-29 12:03:46 +0000782 rec.setStrokeStyle(0, false);
783 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000784
reed@google.com8b06f1a2012-05-29 12:03:46 +0000785 rec.setStrokeStyle(0, true);
786 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
787}
788
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000789// Set this for paths that don't have a consistent direction such as a bowtie.
790// (cheapComputeDirection is not expected to catch these.)
791static const SkPath::Direction kDontCheckDir = static_cast<SkPath::Direction>(-1);
792
793static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
794 SkPath::Direction expected) {
795 if (expected == kDontCheckDir) {
796 return;
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000797 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000798 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
799
800 SkPath::Direction dir;
801 if (copy.cheapComputeDirection(&dir)) {
802 REPORTER_ASSERT(reporter, dir == expected);
803 } else {
804 REPORTER_ASSERT(reporter, SkPath::kUnknown_Direction == expected);
805 }
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000806}
807
reed@google.com3e71a882012-01-10 18:44:37 +0000808static void test_direction(skiatest::Reporter* reporter) {
809 size_t i;
810 SkPath path;
811 REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL));
812 REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCW_Direction));
813 REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCCW_Direction));
reed@google.coma8a3b3d2012-11-26 18:16:27 +0000814 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kUnknown_Direction));
reed@google.com3e71a882012-01-10 18:44:37 +0000815
816 static const char* gDegen[] = {
817 "M 10 10",
818 "M 10 10 M 20 20",
819 "M 10 10 L 20 20",
820 "M 10 10 L 10 10 L 10 10",
821 "M 10 10 Q 10 10 10 10",
822 "M 10 10 C 10 10 10 10 10 10",
823 };
824 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
825 path.reset();
826 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
827 REPORTER_ASSERT(reporter, valid);
828 REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL));
829 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000830
reed@google.com3e71a882012-01-10 18:44:37 +0000831 static const char* gCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000832 "M 10 10 L 10 10 Q 20 10 20 20",
reed@google.com3e71a882012-01-10 18:44:37 +0000833 "M 10 10 C 20 10 20 20 20 20",
reed@google.comd4146662012-01-31 15:42:29 +0000834 "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 +0000835 // rect with top two corners replaced by cubics with identical middle
836 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000837 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
838 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000839 };
840 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
841 path.reset();
842 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
843 REPORTER_ASSERT(reporter, valid);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000844 check_direction(reporter, path, SkPath::kCW_Direction);
reed@google.com3e71a882012-01-10 18:44:37 +0000845 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000846
reed@google.com3e71a882012-01-10 18:44:37 +0000847 static const char* gCCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000848 "M 10 10 L 10 10 Q 20 10 20 -20",
reed@google.com3e71a882012-01-10 18:44:37 +0000849 "M 10 10 C 20 10 20 -20 20 -20",
reed@google.comd4146662012-01-31 15:42:29 +0000850 "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 +0000851 // rect with top two corners replaced by cubics with identical middle
852 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000853 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
854 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000855 };
856 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
857 path.reset();
858 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
859 REPORTER_ASSERT(reporter, valid);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000860 check_direction(reporter, path, SkPath::kCCW_Direction);
reed@google.com3e71a882012-01-10 18:44:37 +0000861 }
reed@google.comac8543f2012-01-30 20:51:25 +0000862
863 // Test two donuts, each wound a different direction. Only the outer contour
864 // determines the cheap direction
865 path.reset();
866 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
867 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000868 check_direction(reporter, path, SkPath::kCW_Direction);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000869
reed@google.comac8543f2012-01-30 20:51:25 +0000870 path.reset();
871 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
872 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000873 check_direction(reporter, path, SkPath::kCCW_Direction);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000874
875 // triangle with one point really far from the origin.
876 path.reset();
877 // the first point is roughly 1.05e10, 1.05e10
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000878 path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
bsalomon@google.com53aab782012-02-23 14:54:49 +0000879 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
880 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000881 check_direction(reporter, path, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000882
883 path.reset();
884 path.conicTo(20, 0, 20, 20, 0.5f);
885 path.close();
886 check_direction(reporter, path, SkPath::kCW_Direction);
887
888 path.reset();
889 path.lineTo(1, 1e7f);
890 path.lineTo(1e7f, 2e7f);
891 path.close();
892 REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity());
893 check_direction(reporter, path, SkPath::kCCW_Direction);
reed@google.com3e71a882012-01-10 18:44:37 +0000894}
895
reed@google.comffdb0182011-11-14 19:29:14 +0000896static void add_rect(SkPath* path, const SkRect& r) {
897 path->moveTo(r.fLeft, r.fTop);
898 path->lineTo(r.fRight, r.fTop);
899 path->lineTo(r.fRight, r.fBottom);
900 path->lineTo(r.fLeft, r.fBottom);
901 path->close();
902}
903
904static void test_bounds(skiatest::Reporter* reporter) {
905 static const SkRect rects[] = {
reed@google.com3563c9e2011-11-14 19:34:57 +0000906 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
907 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
908 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
909 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
reed@google.comffdb0182011-11-14 19:29:14 +0000910 };
911
912 SkPath path0, path1;
913 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
914 path0.addRect(rects[i]);
915 add_rect(&path1, rects[i]);
916 }
917
918 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
919}
920
reed@google.com55b5f4b2011-09-07 12:23:41 +0000921static void stroke_cubic(const SkPoint pts[4]) {
922 SkPath path;
923 path.moveTo(pts[0]);
924 path.cubicTo(pts[1], pts[2], pts[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000925
reed@google.com55b5f4b2011-09-07 12:23:41 +0000926 SkPaint paint;
927 paint.setStyle(SkPaint::kStroke_Style);
928 paint.setStrokeWidth(SK_Scalar1 * 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000929
reed@google.com55b5f4b2011-09-07 12:23:41 +0000930 SkPath fill;
931 paint.getFillPath(path, &fill);
932}
933
934// just ensure this can run w/o any SkASSERTS firing in the debug build
935// we used to assert due to differences in how we determine a degenerate vector
936// but that was fixed with the introduction of SkPoint::CanNormalize
937static void stroke_tiny_cubic() {
938 SkPoint p0[] = {
939 { 372.0f, 92.0f },
940 { 372.0f, 92.0f },
941 { 372.0f, 92.0f },
942 { 372.0f, 92.0f },
943 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000944
reed@google.com55b5f4b2011-09-07 12:23:41 +0000945 stroke_cubic(p0);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000946
reed@google.com55b5f4b2011-09-07 12:23:41 +0000947 SkPoint p1[] = {
948 { 372.0f, 92.0f },
949 { 372.0007f, 92.000755f },
950 { 371.99927f, 92.003922f },
951 { 371.99826f, 92.003899f },
952 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000953
reed@google.com55b5f4b2011-09-07 12:23:41 +0000954 stroke_cubic(p1);
955}
956
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000957static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
958 for (int i = 0; i < 2; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +0000959 SkPath::Iter iter(path, SkToBool(i));
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000960 SkPoint mv;
961 SkPoint pts[4];
962 SkPath::Verb v;
963 int nMT = 0;
964 int nCL = 0;
tomhudson@google.com221db3c2011-07-28 21:10:29 +0000965 mv.set(0, 0);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000966 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
967 switch (v) {
968 case SkPath::kMove_Verb:
969 mv = pts[0];
970 ++nMT;
971 break;
972 case SkPath::kClose_Verb:
973 REPORTER_ASSERT(reporter, mv == pts[0]);
974 ++nCL;
975 break;
976 default:
977 break;
978 }
979 }
980 // if we force a close on the interator we should have a close
981 // for every moveTo
982 REPORTER_ASSERT(reporter, !i || nMT == nCL);
983 }
984}
985
986static void test_close(skiatest::Reporter* reporter) {
987 SkPath closePt;
988 closePt.moveTo(0, 0);
989 closePt.close();
990 check_close(reporter, closePt);
991
992 SkPath openPt;
993 openPt.moveTo(0, 0);
994 check_close(reporter, openPt);
995
996 SkPath empty;
997 check_close(reporter, empty);
998 empty.close();
999 check_close(reporter, empty);
1000
1001 SkPath rect;
1002 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1003 check_close(reporter, rect);
1004 rect.close();
1005 check_close(reporter, rect);
1006
1007 SkPath quad;
1008 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1009 check_close(reporter, quad);
1010 quad.close();
1011 check_close(reporter, quad);
1012
1013 SkPath cubic;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001014 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001015 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
1016 check_close(reporter, cubic);
1017 cubic.close();
1018 check_close(reporter, cubic);
1019
1020 SkPath line;
1021 line.moveTo(SK_Scalar1, SK_Scalar1);
1022 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
1023 check_close(reporter, line);
1024 line.close();
1025 check_close(reporter, line);
1026
1027 SkPath rect2;
1028 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1029 rect2.close();
1030 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1031 check_close(reporter, rect2);
1032 rect2.close();
1033 check_close(reporter, rect2);
1034
1035 SkPath oval3;
1036 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
1037 oval3.close();
1038 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
1039 check_close(reporter, oval3);
1040 oval3.close();
1041 check_close(reporter, oval3);
1042
1043 SkPath moves;
1044 moves.moveTo(SK_Scalar1, SK_Scalar1);
1045 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
1046 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
1047 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
1048 check_close(reporter, moves);
reed@google.com55b5f4b2011-09-07 12:23:41 +00001049
1050 stroke_tiny_cubic();
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001051}
1052
reed@google.com7c424812011-05-15 04:38:34 +00001053static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
1054 SkPath::Convexity expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001055 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1056 SkPath::Convexity c = copy.getConvexity();
reed@google.com7c424812011-05-15 04:38:34 +00001057 REPORTER_ASSERT(reporter, c == expected);
1058}
1059
1060static void test_convexity2(skiatest::Reporter* reporter) {
1061 SkPath pt;
1062 pt.moveTo(0, 0);
1063 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +00001064 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001065 check_direction(reporter, pt, SkPath::kUnknown_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001066
reed@google.com7c424812011-05-15 04:38:34 +00001067 SkPath line;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001068 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
1069 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001070 line.close();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001071 check_convexity(reporter, line, SkPath::kConvex_Convexity);
1072 check_direction(reporter, line, SkPath::kUnknown_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001073
reed@google.com7c424812011-05-15 04:38:34 +00001074 SkPath triLeft;
1075 triLeft.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001076 triLeft.lineTo(SK_Scalar1, 0);
1077 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001078 triLeft.close();
1079 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001080 check_direction(reporter, triLeft, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001081
reed@google.com7c424812011-05-15 04:38:34 +00001082 SkPath triRight;
1083 triRight.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001084 triRight.lineTo(-SK_Scalar1, 0);
1085 triRight.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001086 triRight.close();
1087 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001088 check_direction(reporter, triRight, SkPath::kCCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001089
reed@google.com7c424812011-05-15 04:38:34 +00001090 SkPath square;
1091 square.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001092 square.lineTo(SK_Scalar1, 0);
1093 square.lineTo(SK_Scalar1, SK_Scalar1);
1094 square.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001095 square.close();
1096 check_convexity(reporter, square, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001097 check_direction(reporter, square, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001098
reed@google.com7c424812011-05-15 04:38:34 +00001099 SkPath redundantSquare;
1100 redundantSquare.moveTo(0, 0);
1101 redundantSquare.lineTo(0, 0);
1102 redundantSquare.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001103 redundantSquare.lineTo(SK_Scalar1, 0);
1104 redundantSquare.lineTo(SK_Scalar1, 0);
1105 redundantSquare.lineTo(SK_Scalar1, 0);
1106 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1107 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1108 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1109 redundantSquare.lineTo(0, SK_Scalar1);
1110 redundantSquare.lineTo(0, SK_Scalar1);
1111 redundantSquare.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001112 redundantSquare.close();
1113 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001114 check_direction(reporter, redundantSquare, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001115
reed@google.com7c424812011-05-15 04:38:34 +00001116 SkPath bowTie;
1117 bowTie.moveTo(0, 0);
1118 bowTie.lineTo(0, 0);
1119 bowTie.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001120 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1121 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1122 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1123 bowTie.lineTo(SK_Scalar1, 0);
1124 bowTie.lineTo(SK_Scalar1, 0);
1125 bowTie.lineTo(SK_Scalar1, 0);
1126 bowTie.lineTo(0, SK_Scalar1);
1127 bowTie.lineTo(0, SK_Scalar1);
1128 bowTie.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001129 bowTie.close();
1130 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001131 check_direction(reporter, bowTie, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001132
reed@google.com7c424812011-05-15 04:38:34 +00001133 SkPath spiral;
1134 spiral.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001135 spiral.lineTo(100*SK_Scalar1, 0);
1136 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1137 spiral.lineTo(0, 100*SK_Scalar1);
1138 spiral.lineTo(0, 50*SK_Scalar1);
1139 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1140 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001141 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +00001142 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001143 check_direction(reporter, spiral, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001144
reed@google.com7c424812011-05-15 04:38:34 +00001145 SkPath dent;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001146 dent.moveTo(0, 0);
1147 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1148 dent.lineTo(0, 100*SK_Scalar1);
1149 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1150 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001151 dent.close();
1152 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001153 check_direction(reporter, dent, SkPath::kCW_Direction);
reed@google.com7c424812011-05-15 04:38:34 +00001154}
1155
reed@android.com6b82d1a2009-06-03 02:35:01 +00001156static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1157 const SkRect& bounds) {
1158 REPORTER_ASSERT(reporter, p.isConvex());
1159 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +00001160
reed@android.com6b82d1a2009-06-03 02:35:01 +00001161 SkPath p2(p);
1162 REPORTER_ASSERT(reporter, p2.isConvex());
1163 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1164
1165 SkPath other;
1166 other.swap(p2);
1167 REPORTER_ASSERT(reporter, other.isConvex());
1168 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1169}
1170
reed@google.com04863fa2011-05-15 04:08:24 +00001171static void setFromString(SkPath* path, const char str[]) {
1172 bool first = true;
1173 while (str) {
1174 SkScalar x, y;
1175 str = SkParse::FindScalar(str, &x);
1176 if (NULL == str) {
1177 break;
1178 }
1179 str = SkParse::FindScalar(str, &y);
1180 SkASSERT(str);
1181 if (first) {
1182 path->moveTo(x, y);
1183 first = false;
1184 } else {
1185 path->lineTo(x, y);
1186 }
1187 }
1188}
1189
1190static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +00001191 SkPath path;
1192
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001193 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001194 path.addCircle(0, 0, SkIntToScalar(10));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001195 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001196 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001197 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1198
reed@google.com04863fa2011-05-15 04:08:24 +00001199 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001200 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001201 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.com3e71a882012-01-10 18:44:37 +00001202 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCCW_Direction));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001203
reed@google.com04863fa2011-05-15 04:08:24 +00001204 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001205 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001206 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.com3e71a882012-01-10 18:44:37 +00001207 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCW_Direction));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001208
reed@google.com04863fa2011-05-15 04:08:24 +00001209 static const struct {
1210 const char* fPathStr;
1211 SkPath::Convexity fExpectedConvexity;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001212 SkPath::Direction fExpectedDirection;
reed@google.com04863fa2011-05-15 04:08:24 +00001213 } gRec[] = {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001214 { "", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1215 { "0 0", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1216 { "0 0 10 10", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1217 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPath::kUnknown_Direction },
1218 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPath::kCW_Direction },
1219 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPath::kCCW_Direction },
1220 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
1221 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPath::kCW_Direction },
reed@google.com04863fa2011-05-15 04:08:24 +00001222 };
1223
1224 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1225 SkPath path;
1226 setFromString(&path, gRec[i].fPathStr);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001227 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1228 check_direction(reporter, path, gRec[i].fExpectedDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001229 // check after setting the initial convex and direction
1230 if (kDontCheckDir != gRec[i].fExpectedDirection) {
1231 SkPath copy(path);
1232 SkPath::Direction dir;
1233 bool foundDir = copy.cheapComputeDirection(&dir);
1234 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPath::kUnknown_Direction)
1235 ^ foundDir);
1236 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1237 check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
1238 }
1239 REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
1240 check_direction(reporter, path, gRec[i].fExpectedDirection);
reed@google.com04863fa2011-05-15 04:08:24 +00001241 }
1242}
1243
reed@google.com7e6c4d12012-05-10 14:05:43 +00001244static void test_isLine(skiatest::Reporter* reporter) {
1245 SkPath path;
1246 SkPoint pts[2];
1247 const SkScalar value = SkIntToScalar(5);
1248
1249 REPORTER_ASSERT(reporter, !path.isLine(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001250
reed@google.com7e6c4d12012-05-10 14:05:43 +00001251 // set some non-zero values
1252 pts[0].set(value, value);
1253 pts[1].set(value, value);
1254 REPORTER_ASSERT(reporter, !path.isLine(pts));
1255 // check that pts was untouched
1256 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1257 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1258
1259 const SkScalar moveX = SkIntToScalar(1);
1260 const SkScalar moveY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001261 REPORTER_ASSERT(reporter, value != moveX && value != moveY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001262
1263 path.moveTo(moveX, moveY);
1264 REPORTER_ASSERT(reporter, !path.isLine(NULL));
1265 REPORTER_ASSERT(reporter, !path.isLine(pts));
1266 // check that pts was untouched
1267 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1268 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1269
1270 const SkScalar lineX = SkIntToScalar(2);
1271 const SkScalar lineY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001272 REPORTER_ASSERT(reporter, value != lineX && value != lineY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001273
1274 path.lineTo(lineX, lineY);
1275 REPORTER_ASSERT(reporter, path.isLine(NULL));
1276
1277 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1278 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1279 REPORTER_ASSERT(reporter, path.isLine(pts));
1280 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1281 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1282
1283 path.lineTo(0, 0); // too many points/verbs
1284 REPORTER_ASSERT(reporter, !path.isLine(NULL));
1285 REPORTER_ASSERT(reporter, !path.isLine(pts));
1286 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1287 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001288
1289 path.reset();
1290 path.quadTo(1, 1, 2, 2);
1291 REPORTER_ASSERT(reporter, !path.isLine(NULL));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001292}
1293
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001294static void test_conservativelyContains(skiatest::Reporter* reporter) {
1295 SkPath path;
1296
1297 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1298 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1299
1300 // A circle that bounds kBaseRect (with a significant amount of slop)
1301 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001302 circleR = SkScalarMul(circleR, 1.75f) / 2;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001303 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1304
1305 // round-rect radii
1306 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
skia.committer@gmail.comcec8de62012-11-14 02:01:22 +00001307
caryclark@google.com56f233a2012-11-19 13:06:06 +00001308 static const struct SUPPRESS_VISIBILITY_WARNING {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001309 SkRect fQueryRect;
1310 bool fInRect;
1311 bool fInCircle;
1312 bool fInRR;
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001313 bool fInCubicRR;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001314 } kQueries[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001315 {kBaseRect, true, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001316
1317 // rect well inside of kBaseRect
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001318 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1319 kBaseRect.fTop + 0.25f*kBaseRect.height(),
1320 kBaseRect.fRight - 0.25f*kBaseRect.width(),
1321 kBaseRect.fBottom - 0.25f*kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001322 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001323
1324 // rects with edges off by one from kBaseRect's edges
1325 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1326 kBaseRect.width(), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001327 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001328 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1329 kBaseRect.width() + 1, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001330 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001331 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1332 kBaseRect.width() + 1, kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001333 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001334 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1335 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001336 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001337 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1338 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001339 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001340 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1341 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001342 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001343 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1344 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001345 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001346
1347 // zero-w/h rects at each corner of kBaseRect
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001348 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1349 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1350 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1351 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001352
1353 // far away rect
1354 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1355 SkIntToScalar(10), SkIntToScalar(10)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001356 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001357
1358 // very large rect containing kBaseRect
1359 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1360 kBaseRect.fTop - 5 * kBaseRect.height(),
1361 11 * kBaseRect.width(), 11 * kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001362 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001363
1364 // skinny rect that spans same y-range as kBaseRect
1365 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1366 SkIntToScalar(1), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001367 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001368
1369 // short rect that spans same x-range as kBaseRect
1370 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001371 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001372
1373 // skinny rect that spans slightly larger y-range than kBaseRect
1374 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1375 SkIntToScalar(1), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001376 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001377
1378 // short rect that spans slightly larger x-range than kBaseRect
1379 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1380 kBaseRect.width() + 1, SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001381 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001382 };
1383
1384 for (int inv = 0; inv < 4; ++inv) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001385 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001386 SkRect qRect = kQueries[q].fQueryRect;
1387 if (inv & 0x1) {
1388 SkTSwap(qRect.fLeft, qRect.fRight);
1389 }
1390 if (inv & 0x2) {
1391 SkTSwap(qRect.fTop, qRect.fBottom);
1392 }
1393 for (int d = 0; d < 2; ++d) {
1394 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1395 path.reset();
1396 path.addRect(kBaseRect, dir);
1397 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1398 path.conservativelyContainsRect(qRect));
1399
1400 path.reset();
1401 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1402 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1403 path.conservativelyContainsRect(qRect));
1404
1405 path.reset();
1406 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1407 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1408 path.conservativelyContainsRect(qRect));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001409
1410 path.reset();
1411 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1412 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1413 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1414 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1415 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1416 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1417 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1418 path.close();
1419 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1420 path.conservativelyContainsRect(qRect));
1421
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001422 }
1423 // Slightly non-convex shape, shouldn't contain any rects.
1424 path.reset();
1425 path.moveTo(0, 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001426 path.lineTo(SkIntToScalar(50), 0.05f);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001427 path.lineTo(SkIntToScalar(100), 0);
1428 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1429 path.lineTo(0, SkIntToScalar(100));
1430 path.close();
1431 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1432 }
1433 }
1434
1435 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1436 path.reset();
1437 path.moveTo(0, 0);
1438 path.lineTo(SkIntToScalar(100), 0);
1439 path.lineTo(0, SkIntToScalar(100));
1440
1441 // inside, on along top edge
1442 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1443 SkIntToScalar(10),
1444 SkIntToScalar(10))));
1445 // above
1446 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1447 SkRect::MakeXYWH(SkIntToScalar(50),
1448 SkIntToScalar(-10),
1449 SkIntToScalar(10),
1450 SkIntToScalar(10))));
1451 // to the left
1452 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1453 SkIntToScalar(5),
1454 SkIntToScalar(5),
1455 SkIntToScalar(5))));
1456
1457 // outside the diagonal edge
1458 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1459 SkIntToScalar(200),
1460 SkIntToScalar(20),
1461 SkIntToScalar(5))));
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001462
1463 // same as above path and first test but with an extra moveTo.
1464 path.reset();
1465 path.moveTo(100, 100);
1466 path.moveTo(0, 0);
1467 path.lineTo(SkIntToScalar(100), 0);
1468 path.lineTo(0, SkIntToScalar(100));
1469
1470 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1471 SkIntToScalar(10),
1472 SkIntToScalar(10))));
1473
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001474 path.reset();
1475 path.lineTo(100, 100);
1476 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001477}
1478
reed@google.comf32322b2013-10-16 15:14:04 +00001479static void test_isRect_open_close(skiatest::Reporter* reporter) {
1480 SkPath path;
1481 bool isClosed;
1482
1483 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
reed@google.comf32322b2013-10-16 15:14:04 +00001484 path.close();
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001485
reed@google.comf32322b2013-10-16 15:14:04 +00001486 REPORTER_ASSERT(reporter, path.isRect(NULL, NULL));
1487 REPORTER_ASSERT(reporter, path.isRect(&isClosed, NULL));
1488 REPORTER_ASSERT(reporter, isClosed);
commit-bot@chromium.org7e90e8d2014-02-11 01:38:30 +00001489 REPORTER_ASSERT(reporter, SkPath::kStroke_PathAsRect == path.asRect(NULL));
reed@google.comf32322b2013-10-16 15:14:04 +00001490}
1491
caryclark@google.comf1316942011-07-26 19:54:45 +00001492// Simple isRect test is inline TestPath, below.
1493// test_isRect provides more extensive testing.
1494static void test_isRect(skiatest::Reporter* reporter) {
reed@google.comf32322b2013-10-16 15:14:04 +00001495 test_isRect_open_close(reporter);
1496
caryclark@google.comf1316942011-07-26 19:54:45 +00001497 // passing tests (all moveTo / lineTo...
1498 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1499 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1500 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1501 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1502 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1503 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1504 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1505 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1506 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001507 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1508 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 +00001509 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1510 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1511 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
caryclark@google.combfe90372012-11-21 13:56:20 +00001512 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
rmistry@google.comd6176b02012-08-23 18:14:13 +00001513
caryclark@google.comf1316942011-07-26 19:54:45 +00001514 // failing tests
1515 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1516 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1517 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1518 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1519 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1520 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1521 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1522 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
caryclark@google.combfe90372012-11-21 13:56:20 +00001523 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
1524 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
1525 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
rmistry@google.comd6176b02012-08-23 18:14:13 +00001526
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001527 // no close, but we should detect them as fillably the same as a rect
1528 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1529 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
1530 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
1531
1532 // like c2, but we double-back on ourselves
1533 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
1534 // like c2, but we overshoot the start point
1535 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
1536 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
caryclark@google.comf1316942011-07-26 19:54:45 +00001537
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001538 struct IsRectTest {
1539 SkPoint *fPoints;
1540 size_t fPointCount;
1541 bool fClose;
1542 bool fIsRect;
1543 } tests[] = {
1544 { r1, SK_ARRAY_COUNT(r1), true, true },
1545 { r2, SK_ARRAY_COUNT(r2), true, true },
1546 { r3, SK_ARRAY_COUNT(r3), true, true },
1547 { r4, SK_ARRAY_COUNT(r4), true, true },
1548 { r5, SK_ARRAY_COUNT(r5), true, true },
1549 { r6, SK_ARRAY_COUNT(r6), true, true },
1550 { r7, SK_ARRAY_COUNT(r7), true, true },
1551 { r8, SK_ARRAY_COUNT(r8), true, true },
1552 { r9, SK_ARRAY_COUNT(r9), true, true },
1553 { ra, SK_ARRAY_COUNT(ra), true, true },
1554 { rb, SK_ARRAY_COUNT(rb), true, true },
1555 { rc, SK_ARRAY_COUNT(rc), true, true },
1556 { rd, SK_ARRAY_COUNT(rd), true, true },
1557 { re, SK_ARRAY_COUNT(re), true, true },
1558 { rf, SK_ARRAY_COUNT(rf), true, true },
1559
1560 { f1, SK_ARRAY_COUNT(f1), true, false },
1561 { f2, SK_ARRAY_COUNT(f2), true, false },
1562 { f3, SK_ARRAY_COUNT(f3), true, false },
1563 { f4, SK_ARRAY_COUNT(f4), true, false },
1564 { f5, SK_ARRAY_COUNT(f5), true, false },
1565 { f6, SK_ARRAY_COUNT(f6), true, false },
1566 { f7, SK_ARRAY_COUNT(f7), true, false },
1567 { f8, SK_ARRAY_COUNT(f8), true, false },
1568 { f9, SK_ARRAY_COUNT(f9), true, false },
1569 { fa, SK_ARRAY_COUNT(fa), true, false },
1570 { fb, SK_ARRAY_COUNT(fb), true, false },
1571
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001572 { c1, SK_ARRAY_COUNT(c1), false, true },
1573 { c2, SK_ARRAY_COUNT(c2), false, true },
1574 { c3, SK_ARRAY_COUNT(c3), false, true },
1575
1576 { d1, SK_ARRAY_COUNT(d1), false, false },
1577 { d2, SK_ARRAY_COUNT(d2), false, false },
1578 { d3, SK_ARRAY_COUNT(d3), false, false },
caryclark@google.comf1316942011-07-26 19:54:45 +00001579 };
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001580
1581 const size_t testCount = SK_ARRAY_COUNT(tests);
caryclark@google.comf1316942011-07-26 19:54:45 +00001582 size_t index;
1583 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1584 SkPath path;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001585 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1586 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1587 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.comf1316942011-07-26 19:54:45 +00001588 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001589 if (tests[testIndex].fClose) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001590 path.close();
1591 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001592 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(NULL));
1593 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(NULL, NULL));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001594
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001595 if (tests[testIndex].fIsRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001596 SkRect computed, expected;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001597 expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001598 REPORTER_ASSERT(reporter, path.isRect(&computed));
1599 REPORTER_ASSERT(reporter, expected == computed);
skia.committer@gmail.com1c9c0d32012-11-22 02:02:41 +00001600
caryclark@google.comf68154a2012-11-21 15:18:06 +00001601 bool isClosed;
1602 SkPath::Direction direction, cheapDirection;
1603 REPORTER_ASSERT(reporter, path.cheapComputeDirection(&cheapDirection));
robertphillips@google.com8fd16032013-06-25 15:39:58 +00001604 REPORTER_ASSERT(reporter, path.isRect(&isClosed, &direction));
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001605 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001606 REPORTER_ASSERT(reporter, direction == cheapDirection);
commit-bot@chromium.orgc2abd542014-01-25 16:54:31 +00001607 direction = (SkPath::Direction) -1;
commit-bot@chromium.org7e90e8d2014-02-11 01:38:30 +00001608 if (!tests[testIndex].fClose) {
commit-bot@chromium.orgc2abd542014-01-25 16:54:31 +00001609 REPORTER_ASSERT(reporter, SkPath::kFill_PathAsRect == path.asRect());
1610 REPORTER_ASSERT(reporter, SkPath::kFill_PathAsRect == path.asRect(&direction));
1611 } else {
1612 REPORTER_ASSERT(reporter, SkPath::kStroke_PathAsRect == path.asRect());
1613 REPORTER_ASSERT(reporter, SkPath::kStroke_PathAsRect == path.asRect(&direction));
1614 }
1615 REPORTER_ASSERT(reporter, direction == cheapDirection);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001616 } else {
1617 SkRect computed;
1618 computed.set(123, 456, 789, 1011);
1619 REPORTER_ASSERT(reporter, !path.isRect(&computed));
1620 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
1621 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
1622
1623 bool isClosed = (bool) -1;
1624 SkPath::Direction direction = (SkPath::Direction) -1;
robertphillips@google.com8fd16032013-06-25 15:39:58 +00001625 REPORTER_ASSERT(reporter, !path.isRect(&isClosed, &direction));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001626 REPORTER_ASSERT(reporter, isClosed == (bool) -1);
1627 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
commit-bot@chromium.orgc2abd542014-01-25 16:54:31 +00001628 REPORTER_ASSERT(reporter, SkPath::kNone_PathAsRect == path.asRect());
1629 REPORTER_ASSERT(reporter, SkPath::kNone_PathAsRect == path.asRect(&direction));
1630 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001631 }
caryclark@google.comf1316942011-07-26 19:54:45 +00001632 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001633
caryclark@google.comf1316942011-07-26 19:54:45 +00001634 // fail, close then line
1635 SkPath path1;
1636 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001637 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001638 path1.lineTo(r1[index].fX, r1[index].fY);
1639 }
1640 path1.close();
1641 path1.lineTo(1, 0);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001642 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001643
caryclark@google.comf1316942011-07-26 19:54:45 +00001644 // fail, move in the middle
1645 path1.reset();
1646 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001647 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001648 if (index == 2) {
1649 path1.moveTo(1, .5f);
1650 }
1651 path1.lineTo(r1[index].fX, r1[index].fY);
1652 }
1653 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001654 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00001655
1656 // fail, move on the edge
1657 path1.reset();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001658 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001659 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
1660 path1.lineTo(r1[index].fX, r1[index].fY);
1661 }
1662 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001663 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001664
caryclark@google.comf1316942011-07-26 19:54:45 +00001665 // fail, quad
1666 path1.reset();
1667 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001668 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001669 if (index == 2) {
1670 path1.quadTo(1, .5f, 1, .5f);
1671 }
1672 path1.lineTo(r1[index].fX, r1[index].fY);
1673 }
1674 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001675 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001676
caryclark@google.comf1316942011-07-26 19:54:45 +00001677 // fail, cubic
1678 path1.reset();
1679 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001680 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001681 if (index == 2) {
1682 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
1683 }
1684 path1.lineTo(r1[index].fX, r1[index].fY);
1685 }
1686 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001687 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00001688}
1689
caryclark@google.com56f233a2012-11-19 13:06:06 +00001690static void test_isNestedRects(skiatest::Reporter* reporter) {
1691 // passing tests (all moveTo / lineTo...
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001692 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001693 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1694 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1695 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001696 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001697 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1698 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1699 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1700 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001701 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
1702 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 +00001703 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
1704 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
1705 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001706
1707 // failing tests
1708 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1709 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1710 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1711 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1712 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1713 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1714 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1715 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
1716
1717 // failing, no close
1718 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
1719 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
1720
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001721 struct IsNestedRectTest {
1722 SkPoint *fPoints;
1723 size_t fPointCount;
1724 SkPath::Direction fDirection;
1725 bool fClose;
1726 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
1727 } tests[] = {
1728 { r1, SK_ARRAY_COUNT(r1), SkPath::kCW_Direction , true, true },
1729 { r2, SK_ARRAY_COUNT(r2), SkPath::kCW_Direction , true, true },
1730 { r3, SK_ARRAY_COUNT(r3), SkPath::kCW_Direction , true, true },
1731 { r4, SK_ARRAY_COUNT(r4), SkPath::kCW_Direction , true, true },
1732 { r5, SK_ARRAY_COUNT(r5), SkPath::kCCW_Direction, true, true },
1733 { r6, SK_ARRAY_COUNT(r6), SkPath::kCCW_Direction, true, true },
1734 { r7, SK_ARRAY_COUNT(r7), SkPath::kCCW_Direction, true, true },
1735 { r8, SK_ARRAY_COUNT(r8), SkPath::kCCW_Direction, true, true },
1736 { r9, SK_ARRAY_COUNT(r9), SkPath::kCCW_Direction, true, true },
1737 { ra, SK_ARRAY_COUNT(ra), SkPath::kCCW_Direction, true, true },
1738 { rb, SK_ARRAY_COUNT(rb), SkPath::kCW_Direction, true, true },
1739 { rc, SK_ARRAY_COUNT(rc), SkPath::kCW_Direction, true, true },
1740 { rd, SK_ARRAY_COUNT(rd), SkPath::kCCW_Direction, true, true },
1741 { re, SK_ARRAY_COUNT(re), SkPath::kCW_Direction, true, true },
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001742
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001743 { f1, SK_ARRAY_COUNT(f1), SkPath::kUnknown_Direction, true, false },
1744 { f2, SK_ARRAY_COUNT(f2), SkPath::kUnknown_Direction, true, false },
1745 { f3, SK_ARRAY_COUNT(f3), SkPath::kUnknown_Direction, true, false },
1746 { f4, SK_ARRAY_COUNT(f4), SkPath::kUnknown_Direction, true, false },
1747 { f5, SK_ARRAY_COUNT(f5), SkPath::kUnknown_Direction, true, false },
1748 { f6, SK_ARRAY_COUNT(f6), SkPath::kUnknown_Direction, true, false },
1749 { f7, SK_ARRAY_COUNT(f7), SkPath::kUnknown_Direction, true, false },
1750 { f8, SK_ARRAY_COUNT(f8), SkPath::kUnknown_Direction, true, false },
1751
1752 { c1, SK_ARRAY_COUNT(c1), SkPath::kUnknown_Direction, false, false },
1753 { c2, SK_ARRAY_COUNT(c2), SkPath::kUnknown_Direction, false, false },
1754 };
1755
1756 const size_t testCount = SK_ARRAY_COUNT(tests);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001757 size_t index;
1758 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001759 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1760 SkPath path;
1761 if (rectFirst) {
1762 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1763 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001764 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1765 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1766 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001767 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001768 if (tests[testIndex].fClose) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001769 path.close();
1770 }
1771 if (!rectFirst) {
1772 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1773 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001774 REPORTER_ASSERT(reporter, tests[testIndex].fIsNestedRect == path.isNestedRects(NULL));
1775 if (tests[testIndex].fIsNestedRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001776 SkRect expected[2], computed[2];
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001777 SkPath::Direction expectedDirs[2], computedDirs[2];
caryclark@google.com56f233a2012-11-19 13:06:06 +00001778 SkRect testBounds;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001779 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001780 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
1781 expected[1] = testBounds;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001782 if (rectFirst) {
1783 expectedDirs[0] = SkPath::kCW_Direction;
1784 } else {
1785 expectedDirs[0] = SkPath::kCCW_Direction;
1786 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001787 expectedDirs[1] = tests[testIndex].fDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001788 REPORTER_ASSERT(reporter, path.isNestedRects(computed, computedDirs));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001789 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
1790 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001791 REPORTER_ASSERT(reporter, expectedDirs[0] == computedDirs[0]);
1792 REPORTER_ASSERT(reporter, expectedDirs[1] == computedDirs[1]);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001793 }
caryclark@google.com56f233a2012-11-19 13:06:06 +00001794 }
1795
1796 // fail, close then line
1797 SkPath path1;
1798 if (rectFirst) {
1799 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1800 }
1801 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001802 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001803 path1.lineTo(r1[index].fX, r1[index].fY);
1804 }
1805 path1.close();
1806 path1.lineTo(1, 0);
1807 if (!rectFirst) {
1808 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1809 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001810 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001811
1812 // fail, move in the middle
1813 path1.reset();
1814 if (rectFirst) {
1815 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1816 }
1817 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001818 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001819 if (index == 2) {
1820 path1.moveTo(1, .5f);
1821 }
1822 path1.lineTo(r1[index].fX, r1[index].fY);
1823 }
1824 path1.close();
1825 if (!rectFirst) {
1826 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1827 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001828 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001829
1830 // fail, move on the edge
1831 path1.reset();
1832 if (rectFirst) {
1833 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1834 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001835 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001836 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
1837 path1.lineTo(r1[index].fX, r1[index].fY);
1838 }
1839 path1.close();
1840 if (!rectFirst) {
1841 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1842 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001843 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001844
1845 // fail, quad
1846 path1.reset();
1847 if (rectFirst) {
1848 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1849 }
1850 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001851 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001852 if (index == 2) {
1853 path1.quadTo(1, .5f, 1, .5f);
1854 }
1855 path1.lineTo(r1[index].fX, r1[index].fY);
1856 }
1857 path1.close();
1858 if (!rectFirst) {
1859 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1860 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001861 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001862
1863 // fail, cubic
1864 path1.reset();
1865 if (rectFirst) {
1866 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1867 }
1868 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001869 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001870 if (index == 2) {
1871 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
1872 }
1873 path1.lineTo(r1[index].fX, r1[index].fY);
1874 }
1875 path1.close();
1876 if (!rectFirst) {
1877 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1878 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001879 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
skia.committer@gmail.com34587162012-11-20 02:01:23 +00001880
caryclark@google.com56f233a2012-11-19 13:06:06 +00001881 // fail, not nested
1882 path1.reset();
1883 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
1884 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001885 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001886 }
caryclark@google.combfe90372012-11-21 13:56:20 +00001887
1888 // pass, stroke rect
1889 SkPath src, dst;
1890 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
1891 SkPaint strokePaint;
1892 strokePaint.setStyle(SkPaint::kStroke_Style);
1893 strokePaint.setStrokeWidth(2);
1894 strokePaint.getFillPath(src, &dst);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001895 REPORTER_ASSERT(reporter, dst.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001896}
1897
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001898static void write_and_read_back(skiatest::Reporter* reporter,
1899 const SkPath& p) {
commit-bot@chromium.org19382422014-01-14 20:51:26 +00001900 SkWriter32 writer;
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001901 writer.writePath(p);
reed@google.com44699382013-10-31 17:28:30 +00001902 size_t size = writer.bytesWritten();
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001903 SkAutoMalloc storage(size);
1904 writer.flatten(storage.get());
1905 SkReader32 reader(storage.get(), size);
1906
1907 SkPath readBack;
1908 REPORTER_ASSERT(reporter, readBack != p);
1909 reader.readPath(&readBack);
1910 REPORTER_ASSERT(reporter, readBack == p);
1911
rmistry@google.comd6176b02012-08-23 18:14:13 +00001912 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001913 p.getConvexityOrUnknown());
1914
1915 REPORTER_ASSERT(reporter, readBack.isOval(NULL) == p.isOval(NULL));
1916
1917 const SkRect& origBounds = p.getBounds();
1918 const SkRect& readBackBounds = readBack.getBounds();
1919
1920 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
1921}
1922
reed@google.com53effc52011-09-21 19:05:12 +00001923static void test_flattening(skiatest::Reporter* reporter) {
1924 SkPath p;
1925
1926 static const SkPoint pts[] = {
1927 { 0, 0 },
1928 { SkIntToScalar(10), SkIntToScalar(10) },
1929 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
1930 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
1931 };
1932 p.moveTo(pts[0]);
1933 p.lineTo(pts[1]);
1934 p.quadTo(pts[2], pts[3]);
1935 p.cubicTo(pts[4], pts[5], pts[6]);
1936
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001937 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001938
1939 // create a buffer that should be much larger than the path so we don't
1940 // kill our stack if writer goes too far.
1941 char buffer[1024];
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00001942 size_t size1 = p.writeToMemory(NULL);
1943 size_t size2 = p.writeToMemory(buffer);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001944 REPORTER_ASSERT(reporter, size1 == size2);
1945
1946 SkPath p2;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00001947 size_t size3 = p2.readFromMemory(buffer, 1024);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001948 REPORTER_ASSERT(reporter, size1 == size3);
1949 REPORTER_ASSERT(reporter, p == p2);
1950
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001951 size3 = p2.readFromMemory(buffer, 0);
1952 REPORTER_ASSERT(reporter, !size3);
1953
1954 SkPath tooShort;
1955 size3 = tooShort.readFromMemory(buffer, size1 - 1);
1956 REPORTER_ASSERT(reporter, tooShort.isEmpty());
1957
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001958 char buffer2[1024];
1959 size3 = p2.writeToMemory(buffer2);
1960 REPORTER_ASSERT(reporter, size1 == size3);
1961 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001962
1963 // test persistence of the oval flag & convexity
1964 {
1965 SkPath oval;
1966 SkRect rect = SkRect::MakeWH(10, 10);
1967 oval.addOval(rect);
1968
1969 write_and_read_back(reporter, oval);
1970 }
reed@google.com53effc52011-09-21 19:05:12 +00001971}
1972
1973static void test_transform(skiatest::Reporter* reporter) {
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00001974 SkPath p;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001975
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001976#define CONIC_PERSPECTIVE_BUG_FIXED 0
reed@google.com53effc52011-09-21 19:05:12 +00001977 static const SkPoint pts[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001978 { 0, 0 }, // move
1979 { SkIntToScalar(10), SkIntToScalar(10) }, // line
1980 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
1981 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
1982#if CONIC_PERSPECTIVE_BUG_FIXED
1983 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
1984#endif
reed@google.com53effc52011-09-21 19:05:12 +00001985 };
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001986 const int kPtCount = SK_ARRAY_COUNT(pts);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00001987
reed@google.com53effc52011-09-21 19:05:12 +00001988 p.moveTo(pts[0]);
1989 p.lineTo(pts[1]);
1990 p.quadTo(pts[2], pts[3]);
1991 p.cubicTo(pts[4], pts[5], pts[6]);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001992#if CONIC_PERSPECTIVE_BUG_FIXED
1993 p.conicTo(pts[4], pts[5], 0.5f);
1994#endif
1995 p.close();
rmistry@google.comd6176b02012-08-23 18:14:13 +00001996
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00001997 {
1998 SkMatrix matrix;
1999 matrix.reset();
2000 SkPath p1;
2001 p.transform(matrix, &p1);
2002 REPORTER_ASSERT(reporter, p == p1);
reed@google.com53effc52011-09-21 19:05:12 +00002003 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002004
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002005
2006 {
2007 SkMatrix matrix;
2008 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2009
2010 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
2011
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002012 p.transform(matrix, &p1);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002013 SkPoint pts1[kPtCount];
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002014 int count = p1.getPoints(pts1, kPtCount);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002015 REPORTER_ASSERT(reporter, kPtCount == count);
2016 for (int i = 0; i < count; ++i) {
2017 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2018 REPORTER_ASSERT(reporter, newPt == pts1[i]);
2019 }
2020 }
2021
2022 {
2023 SkMatrix matrix;
2024 matrix.reset();
2025 matrix.setPerspX(SkScalarToPersp(4));
2026
2027 SkPath p1;
2028 p1.moveTo(SkPoint::Make(0, 0));
2029
2030 p.transform(matrix, &p1);
2031 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
2032 p1.transform(matrix, NULL);
2033 SkRect pBounds = p.getBounds();
2034 SkRect p1Bounds = p1.getBounds();
2035 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2036 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2037 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2038 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2039 }
2040
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002041 p.reset();
2042 p.addCircle(0, 0, 1, SkPath::kCW_Direction);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002043
2044 {
2045 SkMatrix matrix;
2046 matrix.reset();
2047 SkPath p1;
2048 p1.moveTo(SkPoint::Make(0, 0));
2049
2050 p.transform(matrix, &p1);
2051 REPORTER_ASSERT(reporter, p1.cheapIsDirection(SkPath::kCW_Direction));
2052 }
2053
2054
2055 {
2056 SkMatrix matrix;
2057 matrix.reset();
2058 matrix.setScaleX(-1);
2059 SkPath p1;
2060 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2061
2062 p.transform(matrix, &p1);
2063 REPORTER_ASSERT(reporter, p1.cheapIsDirection(SkPath::kCCW_Direction));
2064 }
2065
2066 {
2067 SkMatrix matrix;
2068 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2069 SkPath p1;
2070 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2071
2072 p.transform(matrix, &p1);
2073 REPORTER_ASSERT(reporter, p1.cheapIsDirection(SkPath::kUnknown_Direction));
2074 }
reed@google.com53effc52011-09-21 19:05:12 +00002075}
2076
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002077static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002078 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002079 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002080
caryclark@google.com56f233a2012-11-19 13:06:06 +00002081 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002082 const char* testPath;
2083 const size_t numResultPts;
2084 const SkRect resultBound;
2085 const SkPath::Verb* resultVerbs;
2086 const size_t numResultVerbs;
2087 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002088
schenney@chromium.org7e963602012-06-13 17:05:43 +00002089 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2090 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2091 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2092 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2093 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2094 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2095 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2096 static const SkPath::Verb resultVerbs8[] = {
2097 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2098 };
2099 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2100 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2101 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2102 static const SkPath::Verb resultVerbs12[] = {
2103 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2104 };
2105 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2106 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2107 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2108 static const SkPath::Verb resultVerbs16[] = {
2109 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2110 };
2111 static const struct zeroPathTestData gZeroLengthTests[] = {
2112 { "M 1 1", 1, {0, 0, 0, 0}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002113 { "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 +00002114 { "M 1 1 z", 1, {0, 0, 0, 0}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002115 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2116 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2117 { "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) },
2118 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2119 { "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) },
2120 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2121 { "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) },
2122 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2123 { "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) },
2124 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2125 { "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 +00002126 SK_ARRAY_COUNT(resultVerbs14)
2127 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002128 { "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) },
2129 { "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 +00002130 SK_ARRAY_COUNT(resultVerbs16)
2131 }
2132 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002133
schenney@chromium.org7e963602012-06-13 17:05:43 +00002134 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2135 p.reset();
2136 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2137 REPORTER_ASSERT(reporter, valid);
2138 REPORTER_ASSERT(reporter, !p.isEmpty());
2139 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2140 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2141 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2142 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2143 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2144 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002145 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002146}
2147
2148struct SegmentInfo {
2149 SkPath fPath;
2150 int fPointCount;
2151};
2152
reed@google.com10296cc2011-09-21 12:29:05 +00002153#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2154
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002155static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00002156 SkPath p, p2;
2157
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002158 p.moveTo(0, 0);
2159 p.quadTo(100, 100, 200, 200);
2160 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2161 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002162 p2 = p;
2163 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002164 p.cubicTo(100, 100, 200, 200, 300, 300);
2165 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2166 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002167 p2 = p;
2168 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2169
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002170 p.reset();
2171 p.moveTo(0, 0);
2172 p.cubicTo(100, 100, 200, 200, 300, 300);
2173 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00002174 p2 = p;
2175 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00002176
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002177 REPORTER_ASSERT(reporter, !p.isEmpty());
2178}
2179
2180static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002181 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002182 SkPoint pts[4];
2183
2184 // Test an iterator with no path
2185 SkPath::Iter noPathIter;
2186 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002187
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002188 // Test that setting an empty path works
2189 noPathIter.setPath(p, false);
2190 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002191
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002192 // Test that close path makes no difference for an empty path
2193 noPathIter.setPath(p, true);
2194 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002195
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002196 // Test an iterator with an initial empty path
2197 SkPath::Iter iter(p, false);
2198 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2199
2200 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00002201 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002202 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2203
rmistry@google.comd6176b02012-08-23 18:14:13 +00002204
schenney@chromium.org7e963602012-06-13 17:05:43 +00002205 struct iterTestData {
2206 const char* testPath;
2207 const bool forceClose;
2208 const bool consumeDegenerates;
2209 const size_t* numResultPtsPerVerb;
2210 const SkPoint* resultPts;
2211 const SkPath::Verb* resultVerbs;
2212 const size_t numResultVerbs;
2213 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002214
schenney@chromium.org7e963602012-06-13 17:05:43 +00002215 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2216 static const SkPath::Verb resultVerbs2[] = {
2217 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2218 };
2219 static const SkPath::Verb resultVerbs3[] = {
2220 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2221 };
2222 static const SkPath::Verb resultVerbs4[] = {
2223 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2224 };
2225 static const SkPath::Verb resultVerbs5[] = {
2226 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2227 };
2228 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00002229 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2230 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2231 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2232 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002233 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002234 static const SkPoint resultPts2[] = {
2235 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2236 };
2237 static const SkPoint resultPts3[] = {
2238 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2239 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2240 };
2241 static const SkPoint resultPts4[] = {
2242 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2243 };
2244 static const SkPoint resultPts5[] = {
2245 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2246 };
2247 static const struct iterTestData gIterTests[] = {
2248 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002249 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2250 { "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 +00002251 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2252 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2253 { "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) },
2254 { "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 +00002255 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2256 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2257 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2258 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2259 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2260 { "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 +00002261 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002262
schenney@chromium.org7e963602012-06-13 17:05:43 +00002263 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2264 p.reset();
2265 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2266 REPORTER_ASSERT(reporter, valid);
2267 iter.setPath(p, gIterTests[i].forceClose);
2268 int j = 0, l = 0;
2269 do {
2270 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2271 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2272 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2273 }
2274 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2275 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2276 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002277
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002278 p.reset();
2279 iter.setPath(p, false);
2280 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2281 p.lineTo(1, 1);
2282 p.close();
2283 iter.setPath(p, false);
2284 REPORTER_ASSERT(reporter, iter.isClosedContour());
2285 p.reset();
2286 iter.setPath(p, true);
2287 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2288 p.lineTo(1, 1);
2289 iter.setPath(p, true);
2290 REPORTER_ASSERT(reporter, iter.isClosedContour());
2291 p.moveTo(0, 0);
2292 p.lineTo(2, 2);
2293 iter.setPath(p, false);
2294 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2295
2296 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
2297 // check to see if the result is correct.
2298 for (int setNaN = 0; setNaN < 4; ++setNaN) {
2299 p.reset();
2300 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
2301 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
2302 iter.setPath(p, true);
2303 iter.next(pts, false);
2304 iter.next(pts, false);
2305 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
2306 }
2307
2308 p.reset();
2309 p.quadTo(0, 0, 0, 0);
2310 iter.setPath(p, false);
2311 iter.next(pts, false);
2312 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false));
2313 iter.setPath(p, false);
2314 iter.next(pts, false);
2315 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2316
2317 p.reset();
2318 p.conicTo(0, 0, 0, 0, 0.5f);
2319 iter.setPath(p, false);
2320 iter.next(pts, false);
2321 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
2322 iter.setPath(p, false);
2323 iter.next(pts, false);
2324 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2325
2326 p.reset();
2327 p.cubicTo(0, 0, 0, 0, 0, 0);
2328 iter.setPath(p, false);
2329 iter.next(pts, false);
2330 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2331 iter.setPath(p, false);
2332 iter.next(pts, false);
2333 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2334
2335 p.moveTo(1, 1); // add a trailing moveto
2336 iter.setPath(p, false);
2337 iter.next(pts, false);
2338 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2339 iter.setPath(p, false);
2340 iter.next(pts, false);
2341 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2342
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002343 // The GM degeneratesegments.cpp test is more extensive
2344}
2345
2346static void test_raw_iter(skiatest::Reporter* reporter) {
2347 SkPath p;
2348 SkPoint pts[4];
2349
2350 // Test an iterator with no path
2351 SkPath::RawIter noPathIter;
2352 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2353 // Test that setting an empty path works
2354 noPathIter.setPath(p);
2355 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00002356
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002357 // Test an iterator with an initial empty path
2358 SkPath::RawIter iter(p);
2359 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2360
2361 // Test that a move-only path returns the move.
2362 p.moveTo(SK_Scalar1, 0);
2363 iter.setPath(p);
2364 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2365 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2366 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2367 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2368
2369 // No matter how many moves we add, we should get them all back
2370 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2371 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2372 iter.setPath(p);
2373 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2374 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2375 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2376 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2377 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2378 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2379 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2380 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2381 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2382 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2383
2384 // Initial close is never ever stored
2385 p.reset();
2386 p.close();
2387 iter.setPath(p);
2388 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2389
2390 // Move/close sequences
2391 p.reset();
2392 p.close(); // Not stored, no purpose
2393 p.moveTo(SK_Scalar1, 0);
2394 p.close();
2395 p.close(); // Not stored, no purpose
2396 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2397 p.close();
2398 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2399 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
2400 p.close();
2401 iter.setPath(p);
2402 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2403 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2404 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2405 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2406 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2407 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2408 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2409 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2410 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2411 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2412 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2413 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2414 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2415 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2416 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2417 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2418 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2419 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2420 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2421 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2422 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2423 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2424
2425 // Generate random paths and verify
2426 SkPoint randomPts[25];
2427 for (int i = 0; i < 5; ++i) {
2428 for (int j = 0; j < 5; ++j) {
2429 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
2430 }
2431 }
2432
2433 // Max of 10 segments, max 3 points per segment
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002434 SkRandom rand(9876543);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002435 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00002436 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002437 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00002438
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002439 for (int i = 0; i < 500; ++i) {
2440 p.reset();
2441 bool lastWasClose = true;
2442 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00002443 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002444 int numPoints = 0;
2445 int numVerbs = (rand.nextU() >> 16) % 10;
2446 int numIterVerbs = 0;
2447 for (int j = 0; j < numVerbs; ++j) {
2448 do {
2449 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
2450 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002451 switch (nextVerb) {
2452 case SkPath::kMove_Verb:
2453 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2454 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00002455 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002456 numPoints += 1;
2457 lastWasClose = false;
2458 haveMoveTo = true;
2459 break;
2460 case SkPath::kLine_Verb:
2461 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002462 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002463 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2464 haveMoveTo = true;
2465 }
2466 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2467 p.lineTo(expectedPts[numPoints]);
2468 numPoints += 1;
2469 lastWasClose = false;
2470 break;
2471 case SkPath::kQuad_Verb:
2472 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002473 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002474 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2475 haveMoveTo = true;
2476 }
2477 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2478 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2479 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
2480 numPoints += 2;
2481 lastWasClose = false;
2482 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002483 case SkPath::kConic_Verb:
2484 if (!haveMoveTo) {
2485 expectedPts[numPoints++] = lastMoveToPt;
2486 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2487 haveMoveTo = true;
2488 }
2489 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2490 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2491 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2492 rand.nextUScalar1() * 4);
2493 numPoints += 2;
2494 lastWasClose = false;
2495 break;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002496 case SkPath::kCubic_Verb:
2497 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002498 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002499 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2500 haveMoveTo = true;
2501 }
2502 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2503 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2504 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
2505 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2506 expectedPts[numPoints + 2]);
2507 numPoints += 3;
2508 lastWasClose = false;
2509 break;
2510 case SkPath::kClose_Verb:
2511 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00002512 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002513 lastWasClose = true;
2514 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002515 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002516 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002517 }
2518 expectedVerbs[numIterVerbs++] = nextVerb;
2519 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00002520
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002521 iter.setPath(p);
2522 numVerbs = numIterVerbs;
2523 numIterVerbs = 0;
2524 int numIterPts = 0;
2525 SkPoint lastMoveTo;
2526 SkPoint lastPt;
2527 lastMoveTo.set(0, 0);
2528 lastPt.set(0, 0);
2529 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
2530 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
2531 numIterVerbs++;
2532 switch (nextVerb) {
2533 case SkPath::kMove_Verb:
2534 REPORTER_ASSERT(reporter, numIterPts < numPoints);
2535 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
2536 lastPt = lastMoveTo = pts[0];
2537 numIterPts += 1;
2538 break;
2539 case SkPath::kLine_Verb:
2540 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
2541 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2542 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2543 lastPt = pts[1];
2544 numIterPts += 1;
2545 break;
2546 case SkPath::kQuad_Verb:
reed@google.com277c3f82013-05-31 15:17:50 +00002547 case SkPath::kConic_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002548 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
2549 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2550 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2551 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2552 lastPt = pts[2];
2553 numIterPts += 2;
2554 break;
2555 case SkPath::kCubic_Verb:
2556 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
2557 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2558 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2559 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2560 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
2561 lastPt = pts[3];
2562 numIterPts += 3;
2563 break;
2564 case SkPath::kClose_Verb:
2565 REPORTER_ASSERT(reporter, pts[0] == lastMoveTo);
2566 lastPt = lastMoveTo;
2567 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002568 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002569 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002570 }
2571 }
2572 REPORTER_ASSERT(reporter, numIterPts == numPoints);
2573 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
2574 }
2575}
2576
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002577static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002578 const SkPath& path,
2579 bool expectedCircle,
2580 SkPath::Direction expectedDir) {
robertphillips@google.com466310d2013-12-03 16:43:54 +00002581 SkRect rect = SkRect::MakeEmpty();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002582 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
2583 REPORTER_ASSERT(reporter, path.cheapIsDirection(expectedDir));
skia.committer@gmail.comfbb0ed92012-11-13 21:46:06 +00002584
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002585 if (expectedCircle) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002586 REPORTER_ASSERT(reporter, rect.height() == rect.width());
2587 }
2588}
2589
2590static void test_circle_skew(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002591 const SkPath& path,
2592 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002593 SkPath tmp;
2594
2595 SkMatrix m;
2596 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
2597 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002598 // this matrix reverses the direction.
2599 if (SkPath::kCCW_Direction == dir) {
2600 dir = SkPath::kCW_Direction;
2601 } else {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002602 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002603 dir = SkPath::kCCW_Direction;
2604 }
2605 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002606}
2607
2608static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002609 const SkPath& path,
2610 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002611 SkPath tmp;
2612
2613 // translate at small offset
2614 SkMatrix m;
2615 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
2616 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002617 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002618
2619 tmp.reset();
2620 m.reset();
2621
2622 // translate at a relatively big offset
2623 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
2624 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002625 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002626}
2627
2628static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002629 const SkPath& path,
2630 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002631 for (int angle = 0; angle < 360; ++angle) {
2632 SkPath tmp;
2633 SkMatrix m;
2634 m.setRotate(SkIntToScalar(angle));
2635 path.transform(m, &tmp);
2636
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002637 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002638 // degrees is not an oval anymore, this can be improved. we made this
2639 // for the simplicity of our implementation.
2640 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002641 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002642 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002643 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002644 }
2645 }
2646}
2647
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002648static void test_circle_mirror_x(skiatest::Reporter* reporter,
2649 const SkPath& path,
2650 SkPath::Direction dir) {
2651 SkPath tmp;
2652 SkMatrix m;
2653 m.reset();
2654 m.setScaleX(-SK_Scalar1);
2655 path.transform(m, &tmp);
2656
2657 if (SkPath::kCW_Direction == dir) {
2658 dir = SkPath::kCCW_Direction;
2659 } else {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002660 REPORTER_ASSERT(reporter, SkPath::kCCW_Direction == dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002661 dir = SkPath::kCW_Direction;
2662 }
2663
2664 check_for_circle(reporter, tmp, true, dir);
2665}
2666
2667static void test_circle_mirror_y(skiatest::Reporter* reporter,
2668 const SkPath& path,
2669 SkPath::Direction dir) {
2670 SkPath tmp;
2671 SkMatrix m;
2672 m.reset();
2673 m.setScaleY(-SK_Scalar1);
2674 path.transform(m, &tmp);
2675
2676 if (SkPath::kCW_Direction == dir) {
2677 dir = SkPath::kCCW_Direction;
2678 } else {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002679 REPORTER_ASSERT(reporter, SkPath::kCCW_Direction == dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002680 dir = SkPath::kCW_Direction;
2681 }
2682
2683 check_for_circle(reporter, tmp, true, dir);
2684}
2685
2686static void test_circle_mirror_xy(skiatest::Reporter* reporter,
2687 const SkPath& path,
2688 SkPath::Direction dir) {
2689 SkPath tmp;
2690 SkMatrix m;
2691 m.reset();
2692 m.setScaleX(-SK_Scalar1);
2693 m.setScaleY(-SK_Scalar1);
2694 path.transform(m, &tmp);
2695
2696 check_for_circle(reporter, tmp, true, dir);
2697}
2698
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002699static void test_circle_with_direction(skiatest::Reporter* reporter,
2700 SkPath::Direction dir) {
2701 SkPath path;
2702
2703 // circle at origin
2704 path.addCircle(0, 0, SkIntToScalar(20), dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002705 check_for_circle(reporter, path, true, dir);
2706 test_circle_rotate(reporter, path, dir);
2707 test_circle_translate(reporter, path, dir);
2708 test_circle_skew(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002709
2710 // circle at an offset at (10, 10)
2711 path.reset();
2712 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
2713 SkIntToScalar(20), dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002714 check_for_circle(reporter, path, true, dir);
2715 test_circle_rotate(reporter, path, dir);
2716 test_circle_translate(reporter, path, dir);
2717 test_circle_skew(reporter, path, dir);
2718 test_circle_mirror_x(reporter, path, dir);
2719 test_circle_mirror_y(reporter, path, dir);
2720 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002721}
2722
2723static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
2724 SkPath path;
2725 SkPath circle;
2726 SkPath rect;
2727 SkPath empty;
2728
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002729 static const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
2730 static const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
2731
2732 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002733 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
2734 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
2735
2736 SkMatrix translate;
2737 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
2738
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00002739 // Although all the path concatenation related operations leave
2740 // the path a circle, most mark it as a non-circle for simplicity
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002741
2742 // empty + circle (translate)
2743 path = empty;
2744 path.addPath(circle, translate);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002745 check_for_circle(reporter, path, false, kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002746
2747 // circle + empty (translate)
2748 path = circle;
2749 path.addPath(empty, translate);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00002750 check_for_circle(reporter, path, true, kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002751
2752 // test reverseAddPath
2753 path = circle;
2754 path.reverseAddPath(rect);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002755 check_for_circle(reporter, path, false, kCircleDirOpposite);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002756}
2757
2758static void test_circle(skiatest::Reporter* reporter) {
2759 test_circle_with_direction(reporter, SkPath::kCW_Direction);
2760 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
2761
2762 // multiple addCircle()
2763 SkPath path;
2764 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2765 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002766 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002767
2768 // some extra lineTo() would make isOval() fail
2769 path.reset();
2770 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2771 path.lineTo(0, 0);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002772 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002773
2774 // not back to the original point
2775 path.reset();
2776 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2777 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002778 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002779
2780 test_circle_with_add_paths(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002781
2782 // test negative radius
2783 path.reset();
2784 path.addCircle(0, 0, -1, SkPath::kCW_Direction);
2785 REPORTER_ASSERT(reporter, path.isEmpty());
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002786}
2787
2788static void test_oval(skiatest::Reporter* reporter) {
2789 SkRect rect;
2790 SkMatrix m;
2791 SkPath path;
2792
2793 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
2794 path.addOval(rect);
2795
2796 REPORTER_ASSERT(reporter, path.isOval(NULL));
2797
2798 m.setRotate(SkIntToScalar(90));
2799 SkPath tmp;
2800 path.transform(m, &tmp);
2801 // an oval rotated 90 degrees is still an oval.
2802 REPORTER_ASSERT(reporter, tmp.isOval(NULL));
2803
2804 m.reset();
2805 m.setRotate(SkIntToScalar(30));
2806 tmp.reset();
2807 path.transform(m, &tmp);
2808 // an oval rotated 30 degrees is not an oval anymore.
2809 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2810
2811 // since empty path being transformed.
2812 path.reset();
2813 tmp.reset();
2814 m.reset();
2815 path.transform(m, &tmp);
2816 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2817
2818 // empty path is not an oval
2819 tmp.reset();
2820 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2821
2822 // only has moveTo()s
2823 tmp.reset();
2824 tmp.moveTo(0, 0);
2825 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
2826 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2827
2828 // mimic WebKit's calling convention,
2829 // call moveTo() first and then call addOval()
2830 path.reset();
2831 path.moveTo(0, 0);
2832 path.addOval(rect);
2833 REPORTER_ASSERT(reporter, path.isOval(NULL));
2834
2835 // copy path
2836 path.reset();
2837 tmp.reset();
2838 tmp.addOval(rect);
2839 path = tmp;
2840 REPORTER_ASSERT(reporter, path.isOval(NULL));
2841}
2842
bungeman@google.coma5809a32013-06-21 15:13:34 +00002843static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
2844 SkPath empty;
reed@android.com80e39a72009-04-02 16:59:40 +00002845
reed@android.com3abec1d2009-03-02 05:36:20 +00002846 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002847 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002848 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00002849 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00002850 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00002851 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
2852 REPORTER_ASSERT(reporter, !p.isInverseFillType());
bungeman@google.coma5809a32013-06-21 15:13:34 +00002853 REPORTER_ASSERT(reporter, p == empty);
2854 REPORTER_ASSERT(reporter, !(p != empty));
2855}
2856
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002857static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
2858 SkPath::Direction dir) {
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002859 REPORTER_ASSERT(reporter, path->isConvex());
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002860 REPORTER_ASSERT(reporter, path->cheapIsDirection(dir));
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002861 path->setConvexity(SkPath::kUnknown_Convexity);
2862 REPORTER_ASSERT(reporter, path->isConvex());
2863 path->reset();
2864}
2865
2866static void test_rrect(skiatest::Reporter* reporter) {
2867 SkPath p;
2868 SkRRect rr;
2869 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
2870 SkRect r = {10, 20, 30, 40};
2871 rr.setRectRadii(r, radii);
2872 p.addRRect(rr);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002873 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002874 p.addRRect(rr, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002875 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002876 p.addRoundRect(r, &radii[0].fX);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002877 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002878 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002879 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002880 p.addRoundRect(r, radii[1].fX, radii[1].fY);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002881 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002882 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002883 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
2884 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
2885 SkVector save = radii[i];
2886 radii[i].set(0, 0);
2887 rr.setRectRadii(r, radii);
2888 p.addRRect(rr);
2889 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2890 radii[i] = save;
2891 }
2892 p.addRoundRect(r, 0, 0);
2893 SkRect returnedRect;
2894 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
2895 REPORTER_ASSERT(reporter, returnedRect == r);
2896 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2897 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
2898 rr.setRectRadii(r, zeroRadii);
2899 p.addRRect(rr);
2900 bool closed;
2901 SkPath::Direction dir;
2902 REPORTER_ASSERT(reporter, p.isRect(&closed, &dir));
2903 REPORTER_ASSERT(reporter, closed);
2904 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
2905 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2906 p.addRRect(rr, SkPath::kCW_Direction);
2907 p.addRRect(rr, SkPath::kCW_Direction);
2908 REPORTER_ASSERT(reporter, !p.isConvex());
2909 p.reset();
2910 p.addRRect(rr, SkPath::kCCW_Direction);
2911 p.addRRect(rr, SkPath::kCCW_Direction);
2912 REPORTER_ASSERT(reporter, !p.isConvex());
2913 p.reset();
2914 SkRect emptyR = {10, 20, 10, 30};
2915 rr.setRectRadii(emptyR, radii);
2916 p.addRRect(rr);
2917 REPORTER_ASSERT(reporter, p.isEmpty());
2918 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
2919 rr.setRectRadii(largeR, radii);
2920 p.addRRect(rr);
2921 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2922 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
2923 rr.setRectRadii(infR, radii);
2924 p.addRRect(rr);
2925 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2926 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
2927 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
2928 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002929}
2930
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002931static void test_arc(skiatest::Reporter* reporter) {
2932 SkPath p;
2933 SkRect emptyOval = {10, 20, 30, 20};
2934 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
2935 p.addArc(emptyOval, 1, 2);
2936 REPORTER_ASSERT(reporter, p.isEmpty());
2937 p.reset();
2938 SkRect oval = {10, 20, 30, 40};
2939 p.addArc(oval, 1, 0);
2940 REPORTER_ASSERT(reporter, p.isEmpty());
2941 p.reset();
2942 SkPath cwOval;
2943 cwOval.addOval(oval);
2944 p.addArc(oval, 1, 360);
2945 REPORTER_ASSERT(reporter, p == cwOval);
2946 p.reset();
2947 SkPath ccwOval;
2948 ccwOval.addOval(oval, SkPath::kCCW_Direction);
2949 p.addArc(oval, 1, -360);
2950 REPORTER_ASSERT(reporter, p == ccwOval);
2951 p.reset();
2952 p.addArc(oval, 1, 180);
2953 REPORTER_ASSERT(reporter, p.isConvex());
2954 REPORTER_ASSERT(reporter, p.cheapIsDirection(SkPath::kCW_Direction));
2955 p.setConvexity(SkPath::kUnknown_Convexity);
2956 REPORTER_ASSERT(reporter, p.isConvex());
2957}
2958
2959static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
2960 SkScalar x0, SkScalar y0) {
2961 SkPoint pts[4];
2962 SkPath::Verb v = iter->next(pts);
2963 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
2964 REPORTER_ASSERT(reporter, pts[0].fX == x0);
2965 REPORTER_ASSERT(reporter, pts[0].fY == y0);
2966}
2967
2968static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
2969 SkScalar x1, SkScalar y1) {
2970 SkPoint pts[4];
2971 SkPath::Verb v = iter->next(pts);
2972 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
2973 REPORTER_ASSERT(reporter, pts[1].fX == x1);
2974 REPORTER_ASSERT(reporter, pts[1].fY == y1);
2975}
2976
2977static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
2978 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
2979 SkPoint pts[4];
2980 SkPath::Verb v = iter->next(pts);
2981 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
2982 REPORTER_ASSERT(reporter, pts[1].fX == x1);
2983 REPORTER_ASSERT(reporter, pts[1].fY == y1);
2984 REPORTER_ASSERT(reporter, pts[2].fX == x2);
2985 REPORTER_ASSERT(reporter, pts[2].fY == y2);
2986}
2987
2988static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
2989 SkPoint pts[4];
2990 SkPath::Verb v = iter->next(pts);
2991 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
2992}
2993
2994static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
2995 check_done(reporter, p, iter);
2996 p->reset();
2997}
2998
2999static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3000 SkScalar x0, SkScalar y0) {
3001 SkPath::RawIter iter(*p);
3002 check_move(reporter, &iter, x0, y0);
3003 check_done_and_reset(reporter, p, &iter);
3004}
3005
3006static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3007 SkScalar x1, SkScalar y1) {
3008 SkPath::RawIter iter(*p);
3009 check_move(reporter, &iter, 0, 0);
3010 check_line(reporter, &iter, x1, y1);
3011 check_done_and_reset(reporter, p, &iter);
3012}
3013
3014static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3015 SkScalar x1, SkScalar y1) {
3016 SkPath::RawIter iter(*p);
3017 check_move(reporter, &iter, 0, 0);
3018 check_line(reporter, &iter, x1, y1);
3019 check_done(reporter, p, &iter);
3020}
3021
3022static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3023 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3024 SkPath::RawIter iter(*p);
3025 check_move(reporter, &iter, 0, 0);
3026 check_line(reporter, &iter, x1, y1);
3027 check_line(reporter, &iter, x2, y2);
3028 check_done_and_reset(reporter, p, &iter);
3029}
3030
3031static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3032 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3033 SkPath::RawIter iter(*p);
3034 check_move(reporter, &iter, 0, 0);
3035 check_quad(reporter, &iter, x1, y1, x2, y2);
3036 check_done_and_reset(reporter, p, &iter);
3037}
3038
3039static void test_arcTo(skiatest::Reporter* reporter) {
3040 SkPath p;
3041 p.arcTo(0, 0, 1, 2, 1);
3042 check_path_is_line_and_reset(reporter, &p, 0, 0);
3043 p.arcTo(1, 2, 1, 2, 1);
3044 check_path_is_line_and_reset(reporter, &p, 1, 2);
3045 p.arcTo(1, 2, 3, 4, 0);
3046 check_path_is_line_and_reset(reporter, &p, 1, 2);
3047 p.arcTo(1, 2, 0, 0, 1);
3048 check_path_is_line_and_reset(reporter, &p, 1, 2);
3049 p.arcTo(1, 0, 1, 1, 1);
3050 SkPoint pt;
3051 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3052 p.reset();
3053 p.arcTo(1, 0, 1, -1, 1);
3054 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3055 p.reset();
3056 SkRect oval = {1, 2, 3, 4};
3057 p.arcTo(oval, 0, 0, true);
3058 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3059 p.arcTo(oval, 0, 0, false);
3060 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3061 p.arcTo(oval, 360, 0, true);
3062 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3063 p.arcTo(oval, 360, 0, false);
3064 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3065 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003066 p.arcTo(oval, 0, sweep, false);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003067 REPORTER_ASSERT(reporter, p.getBounds() == oval);
3068 sweep += delta;
3069 delta /= 2;
3070 }
3071 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003072 p.arcTo(oval, 0, sweep, false);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003073 REPORTER_ASSERT(reporter, p.getBounds() == oval);
3074 sweep -= delta;
3075 delta /= 2;
3076 }
3077 SkRect noOvalWidth = {1, 2, 0, 3};
3078 p.reset();
3079 p.arcTo(noOvalWidth, 0, 360, false);
3080 REPORTER_ASSERT(reporter, p.isEmpty());
3081
3082 SkRect noOvalHeight = {1, 2, 3, 1};
3083 p.reset();
3084 p.arcTo(noOvalHeight, 0, 360, false);
3085 REPORTER_ASSERT(reporter, p.isEmpty());
3086}
3087
3088static void test_addPath(skiatest::Reporter* reporter) {
3089 SkPath p, q;
3090 p.lineTo(1, 2);
3091 q.moveTo(4, 4);
3092 q.lineTo(7, 8);
3093 q.conicTo(8, 7, 6, 5, 0.5f);
3094 q.quadTo(6, 7, 8, 6);
3095 q.cubicTo(5, 6, 7, 8, 7, 5);
3096 q.close();
3097 p.addPath(q, -4, -4);
3098 SkRect expected = {0, 0, 4, 4};
3099 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3100 p.reset();
3101 p.reverseAddPath(q);
3102 SkRect reverseExpected = {4, 4, 8, 8};
3103 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3104}
3105
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003106static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3107 SkPath p, q;
3108 if (explicitMoveTo) {
3109 p.moveTo(1, 1);
3110 }
3111 p.lineTo(1, 2);
3112 if (explicitMoveTo) {
3113 q.moveTo(2, 1);
3114 }
3115 q.lineTo(2, 2);
3116 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3117 uint8_t verbs[4];
3118 int verbcount = p.getVerbs(verbs, 4);
3119 REPORTER_ASSERT(reporter, verbcount == 4);
3120 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3121 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3122 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3123 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3124}
3125
3126static void test_extendClosedPath(skiatest::Reporter* reporter) {
3127 SkPath p, q;
3128 p.moveTo(1, 1);
3129 p.lineTo(1, 2);
3130 p.lineTo(2, 2);
3131 p.close();
3132 q.moveTo(2, 1);
3133 q.lineTo(2, 3);
3134 p.addPath(q, SkPath::kExtend_AddPathMode);
3135 uint8_t verbs[7];
3136 int verbcount = p.getVerbs(verbs, 7);
3137 REPORTER_ASSERT(reporter, verbcount == 7);
3138 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3139 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3140 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3141 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3142 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3143 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3144 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3145
3146 SkPoint pt;
3147 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3148 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3149 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3150}
3151
3152static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3153 SkPath p, q, r;
3154 // case 1: dst is empty
3155 p.moveTo(2, 1);
3156 p.lineTo(2, 3);
3157 q.addPath(p, mode);
3158 REPORTER_ASSERT(reporter, q == p);
3159 // case 2: src is empty
3160 p.addPath(r, mode);
3161 REPORTER_ASSERT(reporter, q == p);
3162 // case 3: src and dst are empty
3163 q.reset();
3164 q.addPath(r, mode);
3165 REPORTER_ASSERT(reporter, q.isEmpty());
skia.committer@gmail.com877c4492014-02-12 03:02:04 +00003166}
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003167
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003168static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3169 SkPath p;
3170 p.conicTo(1, 2, 3, 4, -1);
3171 check_path_is_line_and_reset(reporter, &p, 3, 4);
3172 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3173 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
3174 p.conicTo(1, 2, 3, 4, 1);
3175 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
3176}
3177
3178static void test_get_point(skiatest::Reporter* reporter) {
3179 SkPath p;
3180 SkPoint pt = p.getPoint(0);
3181 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
3182 REPORTER_ASSERT(reporter, !p.getLastPt(NULL));
3183 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
3184 p.setLastPt(10, 10);
3185 pt = p.getPoint(0);
3186 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
3187 REPORTER_ASSERT(reporter, p.getLastPt(NULL));
3188 p.rMoveTo(10, 10);
3189 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
3190}
3191
3192static void test_contains(skiatest::Reporter* reporter) {
3193 SkPath p;
3194 p.setFillType(SkPath::kInverseWinding_FillType);
3195 REPORTER_ASSERT(reporter, p.contains(0, 0));
3196 p.setFillType(SkPath::kWinding_FillType);
3197 REPORTER_ASSERT(reporter, !p.contains(0, 0));
3198 p.moveTo(4, 4);
3199 p.lineTo(6, 8);
3200 p.lineTo(8, 4);
3201 // test quick reject
3202 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3203 REPORTER_ASSERT(reporter, !p.contains(0, 4));
3204 REPORTER_ASSERT(reporter, !p.contains(4, 10));
3205 REPORTER_ASSERT(reporter, !p.contains(10, 4));
3206 // test various crossings in x
3207 REPORTER_ASSERT(reporter, !p.contains(5, 7));
3208 REPORTER_ASSERT(reporter, p.contains(6, 7));
3209 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3210 p.reset();
3211 p.moveTo(4, 4);
3212 p.lineTo(8, 6);
3213 p.lineTo(4, 8);
3214 // test various crossings in y
3215 REPORTER_ASSERT(reporter, !p.contains(7, 5));
3216 REPORTER_ASSERT(reporter, p.contains(7, 6));
3217 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3218 // test quads
3219 p.reset();
3220 p.moveTo(4, 4);
3221 p.quadTo(6, 6, 8, 8);
3222 p.quadTo(6, 8, 4, 8);
3223 p.quadTo(4, 6, 4, 4);
3224 REPORTER_ASSERT(reporter, p.contains(5, 6));
3225 REPORTER_ASSERT(reporter, !p.contains(6, 5));
3226
3227 p.reset();
3228 p.moveTo(6, 6);
3229 p.quadTo(8, 8, 6, 8);
3230 p.quadTo(4, 8, 4, 6);
3231 p.quadTo(4, 4, 6, 6);
3232 REPORTER_ASSERT(reporter, p.contains(5, 6));
3233 REPORTER_ASSERT(reporter, !p.contains(6, 5));
3234
3235#define CONIC_CONTAINS_BUG_FIXED 0
3236#if CONIC_CONTAINS_BUG_FIXED
3237 p.reset();
3238 p.moveTo(4, 4);
3239 p.conicTo(6, 6, 8, 8, 0.5f);
3240 p.conicTo(6, 8, 4, 8, 0.5f);
3241 p.conicTo(4, 6, 4, 4, 0.5f);
3242 REPORTER_ASSERT(reporter, p.contains(5, 6));
3243 REPORTER_ASSERT(reporter, !p.contains(6, 5));
3244#endif
3245
3246 // test cubics
3247 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
3248 for (int i = 0; i < 3; ++i) {
3249 p.reset();
3250 p.setFillType(SkPath::kEvenOdd_FillType);
3251 p.moveTo(pts[i].fX, pts[i].fY);
3252 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);
3253 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);
3254 p.close();
3255 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
3256 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
3257 }
3258}
3259
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003260class PathRefTest_Private {
3261public:
3262 static void TestPathRef(skiatest::Reporter* reporter) {
3263 static const int kRepeatCnt = 10;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003264
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003265 SkAutoTUnref<SkPathRef> pathRef(SkNEW(SkPathRef));
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003266
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003267 SkPathRef::Editor ed(&pathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003268
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003269 {
3270 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
3271 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3272 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
3273 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
3274 for (int i = 0; i < kRepeatCnt; ++i) {
3275 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
3276 }
3277 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003278 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003279
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003280 {
3281 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
3282 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3283 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
3284 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
3285 for (int i = 0; i < kRepeatCnt; ++i) {
3286 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
3287 }
3288 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003289 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003290
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003291 {
3292 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
3293 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3294 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
3295 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
3296 for (int i = 0; i < kRepeatCnt; ++i) {
3297 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
3298 }
3299 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003300 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003301
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003302 {
3303 SkScalar* weights = NULL;
3304 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
3305 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3306 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
3307 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
3308 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
3309 REPORTER_ASSERT(reporter, NULL != weights);
3310 for (int i = 0; i < kRepeatCnt; ++i) {
3311 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
3312 }
3313 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003314 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003315
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003316 {
3317 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
3318 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3319 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
3320 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
3321 for (int i = 0; i < kRepeatCnt; ++i) {
3322 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
3323 }
3324 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003325 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003326 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003327};
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003328
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003329static void test_operatorEqual(skiatest::Reporter* reporter) {
3330 SkPath a;
3331 SkPath b;
3332 REPORTER_ASSERT(reporter, a == a);
3333 REPORTER_ASSERT(reporter, a == b);
3334 a.setFillType(SkPath::kInverseWinding_FillType);
3335 REPORTER_ASSERT(reporter, a != b);
3336 a.reset();
3337 REPORTER_ASSERT(reporter, a == b);
3338 a.lineTo(1, 1);
3339 REPORTER_ASSERT(reporter, a != b);
3340 a.reset();
3341 REPORTER_ASSERT(reporter, a == b);
3342 a.lineTo(1, 1);
3343 b.lineTo(1, 2);
3344 REPORTER_ASSERT(reporter, a != b);
3345 a.reset();
3346 a.lineTo(1, 2);
3347 REPORTER_ASSERT(reporter, a == b);
3348}
3349
3350class PathTest_Private {
3351public:
3352 static void TestPathTo(skiatest::Reporter* reporter) {
3353 SkPath p, q;
3354 p.lineTo(4, 4);
3355 p.reversePathTo(q);
3356 check_path_is_line(reporter, &p, 4, 4);
3357 q.moveTo(-4, -4);
3358 p.reversePathTo(q);
3359 check_path_is_line(reporter, &p, 4, 4);
3360 q.lineTo(7, 8);
3361 q.conicTo(8, 7, 6, 5, 0.5f);
3362 q.quadTo(6, 7, 8, 6);
3363 q.cubicTo(5, 6, 7, 8, 7, 5);
3364 q.close();
3365 p.reversePathTo(q);
3366 SkRect reverseExpected = {-4, -4, 8, 8};
3367 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3368 }
3369};
3370
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00003371DEF_TEST(Paths, reporter) {
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +00003372 test_path_crbug364224();
3373
bungeman@google.coma5809a32013-06-21 15:13:34 +00003374 SkTSize<SkScalar>::Make(3,4);
3375
3376 SkPath p, empty;
3377 SkRect bounds, bounds2;
3378 test_empty(reporter, p);
reed@android.com3abec1d2009-03-02 05:36:20 +00003379
reed@android.comd252db02009-04-01 18:31:44 +00003380 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00003381
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003382 // this triggers a code path in SkPath::operator= which is otherwise unexercised
3383 SkPath& self = p;
3384 p = self;
3385
3386 // this triggers a code path in SkPath::swap which is otherwise unexercised
3387 p.swap(self);
3388
reed@android.com3abec1d2009-03-02 05:36:20 +00003389 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00003390
reed@android.com6b82d1a2009-06-03 02:35:01 +00003391 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
3392 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00003393 // we have quads or cubics
3394 REPORTER_ASSERT(reporter, p.getSegmentMasks() & kCurveSegmentMask);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003395 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00003396
reed@android.com6b82d1a2009-06-03 02:35:01 +00003397 p.reset();
bungeman@google.coma5809a32013-06-21 15:13:34 +00003398 test_empty(reporter, p);
reed@google.com10296cc2011-09-21 12:29:05 +00003399
reed@android.com6b82d1a2009-06-03 02:35:01 +00003400 p.addOval(bounds);
3401 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003402 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00003403
bungeman@google.coma5809a32013-06-21 15:13:34 +00003404 p.rewind();
3405 test_empty(reporter, p);
3406
reed@android.com3abec1d2009-03-02 05:36:20 +00003407 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00003408 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00003409 // we have only lines
3410 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003411 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00003412
bungeman@google.coma5809a32013-06-21 15:13:34 +00003413 REPORTER_ASSERT(reporter, p != empty);
3414 REPORTER_ASSERT(reporter, !(p == empty));
reed@android.com3abec1d2009-03-02 05:36:20 +00003415
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003416 // do getPoints and getVerbs return the right result
3417 REPORTER_ASSERT(reporter, p.getPoints(NULL, 0) == 4);
3418 REPORTER_ASSERT(reporter, p.getVerbs(NULL, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00003419 SkPoint pts[4];
3420 int count = p.getPoints(pts, 4);
3421 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003422 uint8_t verbs[6];
3423 verbs[5] = 0xff;
3424 p.getVerbs(verbs, 5);
3425 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
3426 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
3427 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
3428 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
3429 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
3430 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00003431 bounds2.set(pts, 4);
3432 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00003433
reed@android.com3abec1d2009-03-02 05:36:20 +00003434 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
3435 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00003436 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00003437
reed@android.com3abec1d2009-03-02 05:36:20 +00003438 REPORTER_ASSERT(reporter, p.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00003439 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00003440 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
3441 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00003442
reed@android.com3abec1d2009-03-02 05:36:20 +00003443 // now force p to not be a rect
3444 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
3445 p.addRect(bounds);
3446 REPORTER_ASSERT(reporter, !p.isRect(NULL));
reed@android.com3abec1d2009-03-02 05:36:20 +00003447
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003448 test_operatorEqual(reporter);
reed@google.com7e6c4d12012-05-10 14:05:43 +00003449 test_isLine(reporter);
3450 test_isRect(reporter);
caryclark@google.com56f233a2012-11-19 13:06:06 +00003451 test_isNestedRects(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003452 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00003453 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00003454 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00003455 test_convexity2(reporter);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00003456 test_conservativelyContains(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00003457 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003458 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00003459 test_flattening(reporter);
3460 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00003461 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003462 test_iter(reporter);
3463 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003464 test_circle(reporter);
3465 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00003466 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00003467 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00003468 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00003469 test_isfinite_after_transform(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00003470 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00003471 test_arb_zero_rad_round_rect_is_rect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003472 test_addrect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00003473 test_addrect_isfinite(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00003474 test_tricky_cubic();
3475 test_clipped_cubic();
3476 test_crbug_170666();
reed@google.com7a90daf2013-04-10 18:44:00 +00003477 test_bad_cubic_crbug229478();
reed@google.com3eff3592013-05-08 21:08:21 +00003478 test_bad_cubic_crbug234190();
mtklein@google.com9c9d4a72013-08-07 19:17:53 +00003479 test_android_specific_behavior(reporter);
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +00003480 test_gen_id(reporter);
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +00003481 test_path_close_issue1474(reporter);
reed@google.comb58ba892013-10-15 15:44:35 +00003482 test_path_to_region(reporter);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003483 test_rrect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003484 test_arc(reporter);
3485 test_arcTo(reporter);
3486 test_addPath(reporter);
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003487 test_addPathMode(reporter, false, false);
3488 test_addPathMode(reporter, true, false);
3489 test_addPathMode(reporter, false, true);
3490 test_addPathMode(reporter, true, true);
3491 test_extendClosedPath(reporter);
3492 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
3493 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003494 test_conicTo_special_case(reporter);
3495 test_get_point(reporter);
3496 test_contains(reporter);
3497 PathTest_Private::TestPathTo(reporter);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003498 PathRefTest_Private::TestPathRef(reporter);
reed@android.com3abec1d2009-03-02 05:36:20 +00003499}