blob: e1fbff28b6470bbc3ba6b5ccb754c0bda15de7b6 [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
reed@google.comcc8be772013-10-15 15:35:29 +000023static void make_path0(SkPath* path) {
24 // from * https://code.google.com/p/skia/issues/detail?id=1706
25
26 path->moveTo(146.939f, 1012.84f);
27 path->lineTo(181.747f, 1009.18f);
28 path->lineTo(182.165f, 1013.16f);
29 path->lineTo(147.357f, 1016.82f);
30 path->lineTo(146.939f, 1012.84f);
31 path->close();
32}
33
34static void make_path1(SkPath* path) {
35 path->addRect(SkRect::MakeXYWH(10, 10, 10, 1));
36}
37
38typedef void (*PathProc)(SkPath*);
39
40/*
41 * Regression test: we used to crash (overwrite internal storage) during
42 * construction of the region when the path was INVERSE. That is now fixed,
43 * so test these regions (which used to assert/crash).
44 *
45 * https://code.google.com/p/skia/issues/detail?id=1706
46 */
47static void test_path_to_region(skiatest::Reporter* reporter) {
48 PathProc procs[] = {
49 make_path0,
50 make_path1,
51 };
skia.committer@gmail.com47262912013-10-16 07:02:24 +000052
reed@google.comcc8be772013-10-15 15:35:29 +000053 SkRegion clip;
54 clip.setRect(0, 0, 1255, 1925);
skia.committer@gmail.com47262912013-10-16 07:02:24 +000055
reed@google.comcc8be772013-10-15 15:35:29 +000056 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
57 SkPath path;
58 procs[i](&path);
skia.committer@gmail.com47262912013-10-16 07:02:24 +000059
reed@google.comcc8be772013-10-15 15:35:29 +000060 SkRegion rgn;
61 rgn.setPath(path, clip);
62 path.toggleInverseFillType();
63 rgn.setPath(path, clip);
64 }
65}
66
caryclark@google.com56f233a2012-11-19 13:06:06 +000067#if defined(WIN32)
68 #define SUPPRESS_VISIBILITY_WARNING
69#else
70 #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
71#endif
72
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +000073static void test_path_close_issue1474(skiatest::Reporter* reporter) {
74 // This test checks that r{Line,Quad,Conic,Cubic}To following a close()
75 // are relative to the point we close to, not relative to the point we close from.
76 SkPath path;
77 SkPoint last;
78
79 // Test rLineTo().
80 path.rLineTo(0, 100);
81 path.rLineTo(100, 0);
82 path.close(); // Returns us back to 0,0.
83 path.rLineTo(50, 50); // This should go to 50,50.
84
85 path.getLastPt(&last);
86 REPORTER_ASSERT(reporter, 50 == last.fX);
87 REPORTER_ASSERT(reporter, 50 == last.fY);
88
89 // Test rQuadTo().
90 path.rewind();
91 path.rLineTo(0, 100);
92 path.rLineTo(100, 0);
93 path.close();
94 path.rQuadTo(50, 50, 75, 75);
95
96 path.getLastPt(&last);
97 REPORTER_ASSERT(reporter, 75 == last.fX);
98 REPORTER_ASSERT(reporter, 75 == last.fY);
99
100 // Test rConicTo().
101 path.rewind();
102 path.rLineTo(0, 100);
103 path.rLineTo(100, 0);
104 path.close();
105 path.rConicTo(50, 50, 85, 85, 2);
106
107 path.getLastPt(&last);
108 REPORTER_ASSERT(reporter, 85 == last.fX);
109 REPORTER_ASSERT(reporter, 85 == last.fY);
110
111 // Test rCubicTo().
112 path.rewind();
113 path.rLineTo(0, 100);
114 path.rLineTo(100, 0);
115 path.close();
116 path.rCubicTo(50, 50, 85, 85, 95, 95);
117
118 path.getLastPt(&last);
119 REPORTER_ASSERT(reporter, 95 == last.fX);
120 REPORTER_ASSERT(reporter, 95 == last.fY);
121}
122
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000123static void test_android_specific_behavior(skiatest::Reporter* reporter) {
124#ifdef SK_BUILD_FOR_ANDROID
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000125 // Make sure we treat fGenerationID and fSourcePath correctly for each of
126 // copy, assign, rewind, reset, and swap.
127 SkPath original, source, anotherSource;
128 original.setSourcePath(&source);
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000129 original.moveTo(0, 0);
130 original.lineTo(1, 1);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000131 REPORTER_ASSERT(reporter, original.getSourcePath() == &source);
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000132
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000133 uint32_t copyID, assignID;
134
135 // Test copy constructor. Copy generation ID, copy source path.
136 SkPath copy(original);
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000137 REPORTER_ASSERT(reporter, copy.getGenerationID() == original.getGenerationID());
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000138 REPORTER_ASSERT(reporter, copy.getSourcePath() == original.getSourcePath());
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000139
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000140 // Test assigment operator. Change generation ID, copy source path.
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000141 SkPath assign;
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000142 assignID = assign.getGenerationID();
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000143 assign = original;
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000144 REPORTER_ASSERT(reporter, assign.getGenerationID() != assignID);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000145 REPORTER_ASSERT(reporter, assign.getSourcePath() == original.getSourcePath());
146
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000147 // Test rewind. Change generation ID, don't touch source path.
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000148 copyID = copy.getGenerationID();
149 copy.rewind();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000150 REPORTER_ASSERT(reporter, copy.getGenerationID() != copyID);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000151 REPORTER_ASSERT(reporter, copy.getSourcePath() == original.getSourcePath());
152
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000153 // Test reset. Change generation ID, don't touch source path.
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000154 assignID = assign.getGenerationID();
155 assign.reset();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000156 REPORTER_ASSERT(reporter, assign.getGenerationID() != assignID);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000157 REPORTER_ASSERT(reporter, assign.getSourcePath() == original.getSourcePath());
158
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000159 // Test swap. Swap the generation IDs, swap source paths.
160 copy.reset();
161 copy.moveTo(2, 2);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000162 copy.setSourcePath(&anotherSource);
163 copyID = copy.getGenerationID();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000164 assign.moveTo(3, 3);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000165 assignID = assign.getGenerationID();
166 copy.swap(assign);
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000167 REPORTER_ASSERT(reporter, copy.getGenerationID() != copyID);
168 REPORTER_ASSERT(reporter, assign.getGenerationID() != assignID);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000169 REPORTER_ASSERT(reporter, copy.getSourcePath() == original.getSourcePath());
170 REPORTER_ASSERT(reporter, assign.getSourcePath() == &anotherSource);
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000171#endif
172}
173
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000174static void test_gen_id(skiatest::Reporter* reporter) {
175 SkPath a, b;
176 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
177
178 a.moveTo(0, 0);
179 const uint32_t z = a.getGenerationID();
180 REPORTER_ASSERT(reporter, z != b.getGenerationID());
181
182 a.reset();
183 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
184
185 a.moveTo(1, 1);
186 const uint32_t y = a.getGenerationID();
187 REPORTER_ASSERT(reporter, z != y);
188
189 b.moveTo(2, 2);
190 const uint32_t x = b.getGenerationID();
191 REPORTER_ASSERT(reporter, x != y && x != z);
192
193 a.swap(b);
194 REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x);
195
196 b = a;
197 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
198
199 SkPath c(a);
200 REPORTER_ASSERT(reporter, c.getGenerationID() == x);
201
202 c.lineTo(3, 3);
203 const uint32_t w = c.getGenerationID();
204 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
205 REPORTER_ASSERT(reporter, a.getGenerationID() == x);
206 REPORTER_ASSERT(reporter, w != x);
207
208#ifdef SK_BUILD_FOR_ANDROID
209 static bool kExpectGenIDToIgnoreFill = false;
210#else
211 static bool kExpectGenIDToIgnoreFill = true;
212#endif
213
214 c.toggleInverseFillType();
215 const uint32_t v = c.getGenerationID();
216 REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill);
217
218 c.rewind();
219 REPORTER_ASSERT(reporter, v != c.getGenerationID());
220}
221
reed@google.com3eff3592013-05-08 21:08:21 +0000222// This used to assert in the debug build, as the edges did not all line-up.
223static void test_bad_cubic_crbug234190() {
224 SkPath path;
225 path.moveTo(13.8509f, 3.16858f);
226 path.cubicTo(-2.35893e+08f, -4.21044e+08f,
227 -2.38991e+08f, -4.26573e+08f,
228 -2.41016e+08f, -4.30188e+08f);
229
230 SkPaint paint;
231 paint.setAntiAlias(true);
reed@google.comd28ba802013-09-20 19:33:52 +0000232 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(84, 88));
reed@google.com3eff3592013-05-08 21:08:21 +0000233 surface->getCanvas()->drawPath(path, paint);
234}
235
reed@google.com7a90daf2013-04-10 18:44:00 +0000236static void test_bad_cubic_crbug229478() {
237 const SkPoint pts[] = {
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000238 { 4595.91064f, -11596.9873f },
239 { 4597.2168f, -11595.9414f },
240 { 4598.52344f, -11594.8955f },
241 { 4599.83008f, -11593.8496f },
reed@google.com7a90daf2013-04-10 18:44:00 +0000242 };
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000243
reed@google.com7a90daf2013-04-10 18:44:00 +0000244 SkPath path;
245 path.moveTo(pts[0]);
246 path.cubicTo(pts[1], pts[2], pts[3]);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000247
reed@google.com7a90daf2013-04-10 18:44:00 +0000248 SkPaint paint;
249 paint.setStyle(SkPaint::kStroke_Style);
250 paint.setStrokeWidth(20);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000251
reed@google.com7a90daf2013-04-10 18:44:00 +0000252 SkPath dst;
253 // Before the fix, this would infinite-recurse, and run out of stack
254 // because we would keep trying to subdivide a degenerate cubic segment.
255 paint.getFillPath(path, &dst, NULL);
256}
257
reed@google.com64d62952013-01-18 17:49:28 +0000258static void build_path_170666(SkPath& path) {
259 path.moveTo(17.9459f, 21.6344f);
260 path.lineTo(139.545f, -47.8105f);
261 path.lineTo(139.545f, -47.8105f);
262 path.lineTo(131.07f, -47.3888f);
263 path.lineTo(131.07f, -47.3888f);
264 path.lineTo(122.586f, -46.9532f);
265 path.lineTo(122.586f, -46.9532f);
266 path.lineTo(18076.6f, 31390.9f);
267 path.lineTo(18076.6f, 31390.9f);
268 path.lineTo(18085.1f, 31390.5f);
269 path.lineTo(18085.1f, 31390.5f);
270 path.lineTo(18076.6f, 31390.9f);
271 path.lineTo(18076.6f, 31390.9f);
272 path.lineTo(17955, 31460.3f);
273 path.lineTo(17955, 31460.3f);
274 path.lineTo(17963.5f, 31459.9f);
275 path.lineTo(17963.5f, 31459.9f);
276 path.lineTo(17971.9f, 31459.5f);
277 path.lineTo(17971.9f, 31459.5f);
278 path.lineTo(17.9551f, 21.6205f);
279 path.lineTo(17.9551f, 21.6205f);
280 path.lineTo(9.47091f, 22.0561f);
281 path.lineTo(9.47091f, 22.0561f);
282 path.lineTo(17.9459f, 21.6344f);
283 path.lineTo(17.9459f, 21.6344f);
284 path.close();path.moveTo(0.995934f, 22.4779f);
285 path.lineTo(0.986725f, 22.4918f);
286 path.lineTo(0.986725f, 22.4918f);
287 path.lineTo(17955, 31460.4f);
288 path.lineTo(17955, 31460.4f);
289 path.lineTo(17971.9f, 31459.5f);
290 path.lineTo(17971.9f, 31459.5f);
291 path.lineTo(18093.6f, 31390.1f);
292 path.lineTo(18093.6f, 31390.1f);
293 path.lineTo(18093.6f, 31390);
294 path.lineTo(18093.6f, 31390);
295 path.lineTo(139.555f, -47.8244f);
296 path.lineTo(139.555f, -47.8244f);
297 path.lineTo(122.595f, -46.9671f);
298 path.lineTo(122.595f, -46.9671f);
299 path.lineTo(0.995934f, 22.4779f);
300 path.lineTo(0.995934f, 22.4779f);
301 path.close();
302 path.moveTo(5.43941f, 25.5223f);
303 path.lineTo(798267, -28871.1f);
304 path.lineTo(798267, -28871.1f);
305 path.lineTo(3.12512e+06f, -113102);
306 path.lineTo(3.12512e+06f, -113102);
307 path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813);
308 path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f);
309 path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f);
310 path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f);
311 path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f);
312 path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f);
313 path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f);
314 path.lineTo(2.78271e+08f, -1.00733e+07f);
315 path.lineTo(2.78271e+08f, -1.00733e+07f);
316 path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f);
317 path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f);
318 path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f);
319 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
320 path.lineTo(2.77473e+08f, -1.00444e+07f);
321 path.lineTo(2.77473e+08f, -1.00444e+07f);
322 path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f);
323 path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f);
324 path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f);
325 path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f);
326 path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f);
327 path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814);
328 path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103);
329 path.lineTo(798284, -28872);
330 path.lineTo(798284, -28872);
331 path.lineTo(22.4044f, 24.6677f);
332 path.lineTo(22.4044f, 24.6677f);
333 path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f);
334 path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f);
335 path.close();
336}
337
338static void build_path_simple_170666(SkPath& path) {
339 path.moveTo(126.677f, 24.1591f);
340 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
341}
342
343// This used to assert in the SK_DEBUG build, as the clip step would fail with
344// too-few interations in our cubic-line intersection code. That code now runs
345// 24 interations (instead of 16).
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000346static void test_crbug_170666() {
reed@google.com64d62952013-01-18 17:49:28 +0000347 SkPath path;
348 SkPaint paint;
349 paint.setAntiAlias(true);
350
reed@google.comd28ba802013-09-20 19:33:52 +0000351 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(1000, 1000));
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000352
reed@google.com64d62952013-01-18 17:49:28 +0000353 build_path_simple_170666(path);
354 surface->getCanvas()->drawPath(path, paint);
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000355
reed@google.com64d62952013-01-18 17:49:28 +0000356 build_path_170666(path);
357 surface->getCanvas()->drawPath(path, paint);
358}
359
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000360static void test_addrect(skiatest::Reporter* reporter) {
361 SkPath path;
362 path.lineTo(0, 0);
363 path.addRect(SkRect::MakeWH(50, 100));
364 REPORTER_ASSERT(reporter, path.isRect(NULL));
365
366 path.reset();
367 path.lineTo(FLT_EPSILON, FLT_EPSILON);
368 path.addRect(SkRect::MakeWH(50, 100));
369 REPORTER_ASSERT(reporter, !path.isRect(NULL));
370
371 path.reset();
372 path.quadTo(0, 0, 0, 0);
373 path.addRect(SkRect::MakeWH(50, 100));
374 REPORTER_ASSERT(reporter, !path.isRect(NULL));
375
376 path.reset();
377 path.conicTo(0, 0, 0, 0, 0.5f);
378 path.addRect(SkRect::MakeWH(50, 100));
379 REPORTER_ASSERT(reporter, !path.isRect(NULL));
380
381 path.reset();
382 path.cubicTo(0, 0, 0, 0, 0, 0);
383 path.addRect(SkRect::MakeWH(50, 100));
384 REPORTER_ASSERT(reporter, !path.isRect(NULL));
385}
386
reed@google.coma8790de2012-10-24 21:04:04 +0000387// Make sure we stay non-finite once we get there (unless we reset or rewind).
388static void test_addrect_isfinite(skiatest::Reporter* reporter) {
389 SkPath path;
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000390
reed@google.coma8790de2012-10-24 21:04:04 +0000391 path.addRect(SkRect::MakeWH(50, 100));
392 REPORTER_ASSERT(reporter, path.isFinite());
393
394 path.moveTo(0, 0);
395 path.lineTo(SK_ScalarInfinity, 42);
396 REPORTER_ASSERT(reporter, !path.isFinite());
397
398 path.addRect(SkRect::MakeWH(50, 100));
399 REPORTER_ASSERT(reporter, !path.isFinite());
400
401 path.reset();
402 REPORTER_ASSERT(reporter, path.isFinite());
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000403
reed@google.coma8790de2012-10-24 21:04:04 +0000404 path.addRect(SkRect::MakeWH(50, 100));
405 REPORTER_ASSERT(reporter, path.isFinite());
406}
407
reed@google.com848148e2013-01-15 15:51:59 +0000408static void build_big_path(SkPath* path, bool reducedCase) {
409 if (reducedCase) {
410 path->moveTo(577330, 1971.72f);
411 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
412 } else {
413 path->moveTo(60.1631f, 7.70567f);
414 path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
415 path->lineTo(577379, 1977.77f);
416 path->quadTo(577364, 1979.57f, 577325, 1980.26f);
417 path->quadTo(577286, 1980.95f, 577245, 1980.13f);
418 path->quadTo(577205, 1979.3f, 577187, 1977.45f);
419 path->quadTo(577168, 1975.6f, 577183, 1973.8f);
420 path->quadTo(577198, 1972, 577238, 1971.31f);
421 path->quadTo(577277, 1970.62f, 577317, 1971.45f);
422 path->quadTo(577330, 1971.72f, 577341, 1972.11f);
423 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
424 path->moveTo(306.718f, -32.912f);
425 path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
426 }
427}
428
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000429static void test_clipped_cubic() {
reed@google.comd28ba802013-09-20 19:33:52 +0000430 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(640, 480));
reed@google.com848148e2013-01-15 15:51:59 +0000431
432 // This path used to assert, because our cubic-chopping code incorrectly
433 // moved control points after the chop. This test should be run in SK_DEBUG
434 // mode to ensure that we no long assert.
435 SkPath path;
436 for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
437 build_big_path(&path, SkToBool(doReducedCase));
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +0000438
reed@google.com848148e2013-01-15 15:51:59 +0000439 SkPaint paint;
440 for (int doAA = 0; doAA <= 1; ++doAA) {
441 paint.setAntiAlias(SkToBool(doAA));
442 surface->getCanvas()->drawPath(path, paint);
443 }
444 }
445}
446
reed@google.com8cae8352012-09-14 15:18:41 +0000447// Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
448// which triggered an assert, from a tricky cubic. This test replicates that
449// example, so we can ensure that we handle it (in SkEdge.cpp), and don't
450// assert in the SK_DEBUG build.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000451static void test_tricky_cubic() {
reed@google.com8cae8352012-09-14 15:18:41 +0000452 const SkPoint pts[] = {
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +0000453 { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
454 { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
455 { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
456 { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
reed@google.com8cae8352012-09-14 15:18:41 +0000457 };
458
459 SkPath path;
460 path.moveTo(pts[0]);
461 path.cubicTo(pts[1], pts[2], pts[3]);
462
463 SkPaint paint;
464 paint.setAntiAlias(true);
465
reed@google.comd28ba802013-09-20 19:33:52 +0000466 SkSurface* surface = SkSurface::NewRasterPMColor(19, 130);
reed@google.com8cae8352012-09-14 15:18:41 +0000467 surface->getCanvas()->drawPath(path, paint);
468 surface->unref();
469}
reed@android.com3abec1d2009-03-02 05:36:20 +0000470
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000471// Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
472//
473static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
474 SkPath path;
475 path.quadTo(157, 366, 286, 208);
476 path.arcTo(37, 442, 315, 163, 957494590897113.0f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000477
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000478 SkMatrix matrix;
479 matrix.setScale(1000*1000, 1000*1000);
480
481 // Be sure that path::transform correctly updates isFinite and the bounds
482 // if the transformation overflows. The previous bug was that isFinite was
483 // set to true in this case, but the bounds were not set to empty (which
484 // they should be).
485 while (path.isFinite()) {
486 REPORTER_ASSERT(reporter, path.getBounds().isFinite());
487 REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
488 path.transform(matrix);
489 }
490 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
491
492 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
493 path.transform(matrix);
494 // we need to still be non-finite
495 REPORTER_ASSERT(reporter, !path.isFinite());
496 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
497}
498
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000499static void add_corner_arc(SkPath* path, const SkRect& rect,
500 SkScalar xIn, SkScalar yIn,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000501 int startAngle)
502{
503
504 SkScalar rx = SkMinScalar(rect.width(), xIn);
505 SkScalar ry = SkMinScalar(rect.height(), yIn);
506
507 SkRect arcRect;
508 arcRect.set(-rx, -ry, rx, ry);
509 switch (startAngle) {
510 case 0:
511 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
512 break;
513 case 90:
514 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
515 break;
516 case 180:
517 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
518 break;
519 case 270:
520 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
521 break;
522 default:
523 break;
524 }
525
526 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
527}
528
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000529static void make_arb_round_rect(SkPath* path, const SkRect& r,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000530 SkScalar xCorner, SkScalar yCorner) {
531 // we are lazy here and use the same x & y for each corner
532 add_corner_arc(path, r, xCorner, yCorner, 270);
533 add_corner_arc(path, r, xCorner, yCorner, 0);
534 add_corner_arc(path, r, xCorner, yCorner, 90);
535 add_corner_arc(path, r, xCorner, yCorner, 180);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000536 path->close();
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000537}
538
539// Chrome creates its own round rects with each corner possibly being different.
540// Performance will suffer if they are not convex.
541// Note: PathBench::ArbRoundRectBench performs almost exactly
542// the same test (but with drawing)
543static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000544 SkRandom rand;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000545 SkRect r;
546
547 for (int i = 0; i < 5000; ++i) {
548
robertphillips@google.com158618e2012-10-23 16:56:56 +0000549 SkScalar size = rand.nextUScalar1() * 30;
550 if (size < SK_Scalar1) {
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000551 continue;
552 }
553 r.fLeft = rand.nextUScalar1() * 300;
554 r.fTop = rand.nextUScalar1() * 300;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000555 r.fRight = r.fLeft + 2 * size;
556 r.fBottom = r.fTop + 2 * size;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000557
558 SkPath temp;
559
560 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
561
562 REPORTER_ASSERT(reporter, temp.isConvex());
563 }
564}
565
robertphillips@google.com158618e2012-10-23 16:56:56 +0000566// Chrome will sometimes create a 0 radius round rect. The degenerate
567// quads prevent the path from being converted to a rect
568// Note: PathBench::ArbRoundRectBench performs almost exactly
569// the same test (but with drawing)
570static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000571 SkRandom rand;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000572 SkRect r;
573
574 for (int i = 0; i < 5000; ++i) {
575
576 SkScalar size = rand.nextUScalar1() * 30;
577 if (size < SK_Scalar1) {
578 continue;
579 }
580 r.fLeft = rand.nextUScalar1() * 300;
581 r.fTop = rand.nextUScalar1() * 300;
582 r.fRight = r.fLeft + 2 * size;
583 r.fBottom = r.fTop + 2 * size;
584
585 SkPath temp;
586
587 make_arb_round_rect(&temp, r, 0, 0);
588
robertphillips@google.com158618e2012-10-23 16:56:56 +0000589 SkRect result;
590 REPORTER_ASSERT(reporter, temp.isRect(&result));
591 REPORTER_ASSERT(reporter, r == result);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000592 }
593}
594
reed@google.com0bb18bb2012-07-26 15:20:36 +0000595static void test_rect_isfinite(skiatest::Reporter* reporter) {
596 const SkScalar inf = SK_ScalarInfinity;
caryclark@google.com7abfa492013-04-12 11:59:41 +0000597 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000598 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000599
reed@google.com0bb18bb2012-07-26 15:20:36 +0000600 SkRect r;
601 r.setEmpty();
602 REPORTER_ASSERT(reporter, r.isFinite());
caryclark@google.com7abfa492013-04-12 11:59:41 +0000603 r.set(0, 0, inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000604 REPORTER_ASSERT(reporter, !r.isFinite());
605 r.set(0, 0, nan, 0);
606 REPORTER_ASSERT(reporter, !r.isFinite());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000607
reed@google.com0bb18bb2012-07-26 15:20:36 +0000608 SkPoint pts[] = {
609 { 0, 0 },
610 { SK_Scalar1, 0 },
611 { 0, SK_Scalar1 },
612 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000613
reed@google.com0bb18bb2012-07-26 15:20:36 +0000614 bool isFine = r.setBoundsCheck(pts, 3);
615 REPORTER_ASSERT(reporter, isFine);
616 REPORTER_ASSERT(reporter, !r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000617
reed@google.com0bb18bb2012-07-26 15:20:36 +0000618 pts[1].set(inf, 0);
619 isFine = r.setBoundsCheck(pts, 3);
620 REPORTER_ASSERT(reporter, !isFine);
621 REPORTER_ASSERT(reporter, r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000622
reed@google.com0bb18bb2012-07-26 15:20:36 +0000623 pts[1].set(nan, 0);
624 isFine = r.setBoundsCheck(pts, 3);
625 REPORTER_ASSERT(reporter, !isFine);
626 REPORTER_ASSERT(reporter, r.isEmpty());
627}
628
629static void test_path_isfinite(skiatest::Reporter* reporter) {
630 const SkScalar inf = SK_ScalarInfinity;
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000631 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000632 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000633
reed@google.com0bb18bb2012-07-26 15:20:36 +0000634 SkPath path;
635 REPORTER_ASSERT(reporter, path.isFinite());
636
637 path.reset();
638 REPORTER_ASSERT(reporter, path.isFinite());
639
640 path.reset();
641 path.moveTo(SK_Scalar1, 0);
642 REPORTER_ASSERT(reporter, path.isFinite());
643
644 path.reset();
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000645 path.moveTo(inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000646 REPORTER_ASSERT(reporter, !path.isFinite());
647
648 path.reset();
649 path.moveTo(nan, 0);
650 REPORTER_ASSERT(reporter, !path.isFinite());
651}
652
653static void test_isfinite(skiatest::Reporter* reporter) {
654 test_rect_isfinite(reporter);
655 test_path_isfinite(reporter);
656}
657
reed@google.com744faba2012-05-29 19:54:52 +0000658// assert that we always
659// start with a moveTo
660// only have 1 moveTo
661// only have Lines after that
662// end with a single close
663// only have (at most) 1 close
664//
665static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000666 const SkPoint srcPts[], bool expectClose) {
reed@google.com744faba2012-05-29 19:54:52 +0000667 SkPath::RawIter iter(path);
668 SkPoint pts[4];
reed@google.com744faba2012-05-29 19:54:52 +0000669
670 bool firstTime = true;
671 bool foundClose = false;
672 for (;;) {
673 switch (iter.next(pts)) {
674 case SkPath::kMove_Verb:
675 REPORTER_ASSERT(reporter, firstTime);
676 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
677 srcPts++;
678 firstTime = false;
679 break;
680 case SkPath::kLine_Verb:
681 REPORTER_ASSERT(reporter, !firstTime);
682 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
683 srcPts++;
684 break;
685 case SkPath::kQuad_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000686 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb");
reed@google.com744faba2012-05-29 19:54:52 +0000687 break;
reed@google.com277c3f82013-05-31 15:17:50 +0000688 case SkPath::kConic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000689 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb");
reed@google.com277c3f82013-05-31 15:17:50 +0000690 break;
reed@google.com744faba2012-05-29 19:54:52 +0000691 case SkPath::kCubic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000692 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb");
reed@google.com744faba2012-05-29 19:54:52 +0000693 break;
694 case SkPath::kClose_Verb:
695 REPORTER_ASSERT(reporter, !firstTime);
696 REPORTER_ASSERT(reporter, !foundClose);
697 REPORTER_ASSERT(reporter, expectClose);
698 foundClose = true;
699 break;
700 case SkPath::kDone_Verb:
701 goto DONE;
702 }
703 }
704DONE:
705 REPORTER_ASSERT(reporter, foundClose == expectClose);
706}
707
708static void test_addPoly(skiatest::Reporter* reporter) {
709 SkPoint pts[32];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000710 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000711
reed@google.com744faba2012-05-29 19:54:52 +0000712 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
713 pts[i].fX = rand.nextSScalar1();
714 pts[i].fY = rand.nextSScalar1();
715 }
716
717 for (int doClose = 0; doClose <= 1; ++doClose) {
718 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
719 SkPath path;
720 path.addPoly(pts, count, SkToBool(doClose));
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000721 test_poly(reporter, path, pts, SkToBool(doClose));
reed@google.com744faba2012-05-29 19:54:52 +0000722 }
723 }
724}
725
reed@google.com8b06f1a2012-05-29 12:03:46 +0000726static void test_strokerec(skiatest::Reporter* reporter) {
727 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
728 REPORTER_ASSERT(reporter, rec.isFillStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000729
reed@google.com8b06f1a2012-05-29 12:03:46 +0000730 rec.setHairlineStyle();
731 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000732
reed@google.com8b06f1a2012-05-29 12:03:46 +0000733 rec.setStrokeStyle(SK_Scalar1, false);
734 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000735
reed@google.com8b06f1a2012-05-29 12:03:46 +0000736 rec.setStrokeStyle(SK_Scalar1, true);
737 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000738
reed@google.com8b06f1a2012-05-29 12:03:46 +0000739 rec.setStrokeStyle(0, false);
740 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000741
reed@google.com8b06f1a2012-05-29 12:03:46 +0000742 rec.setStrokeStyle(0, true);
743 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
744}
745
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000746// Set this for paths that don't have a consistent direction such as a bowtie.
747// (cheapComputeDirection is not expected to catch these.)
748static const SkPath::Direction kDontCheckDir = static_cast<SkPath::Direction>(-1);
749
750static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
751 SkPath::Direction expected) {
752 if (expected == kDontCheckDir) {
753 return;
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000754 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000755 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
756
757 SkPath::Direction dir;
758 if (copy.cheapComputeDirection(&dir)) {
759 REPORTER_ASSERT(reporter, dir == expected);
760 } else {
761 REPORTER_ASSERT(reporter, SkPath::kUnknown_Direction == expected);
762 }
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000763}
764
reed@google.com3e71a882012-01-10 18:44:37 +0000765static void test_direction(skiatest::Reporter* reporter) {
766 size_t i;
767 SkPath path;
768 REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL));
769 REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCW_Direction));
770 REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCCW_Direction));
reed@google.coma8a3b3d2012-11-26 18:16:27 +0000771 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kUnknown_Direction));
reed@google.com3e71a882012-01-10 18:44:37 +0000772
773 static const char* gDegen[] = {
774 "M 10 10",
775 "M 10 10 M 20 20",
776 "M 10 10 L 20 20",
777 "M 10 10 L 10 10 L 10 10",
778 "M 10 10 Q 10 10 10 10",
779 "M 10 10 C 10 10 10 10 10 10",
780 };
781 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
782 path.reset();
783 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
784 REPORTER_ASSERT(reporter, valid);
785 REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL));
786 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000787
reed@google.com3e71a882012-01-10 18:44:37 +0000788 static const char* gCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000789 "M 10 10 L 10 10 Q 20 10 20 20",
reed@google.com3e71a882012-01-10 18:44:37 +0000790 "M 10 10 C 20 10 20 20 20 20",
reed@google.comd4146662012-01-31 15:42:29 +0000791 "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 +0000792 // rect with top two corners replaced by cubics with identical middle
793 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000794 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
795 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000796 };
797 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
798 path.reset();
799 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
800 REPORTER_ASSERT(reporter, valid);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000801 check_direction(reporter, path, SkPath::kCW_Direction);
reed@google.com3e71a882012-01-10 18:44:37 +0000802 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000803
reed@google.com3e71a882012-01-10 18:44:37 +0000804 static const char* gCCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000805 "M 10 10 L 10 10 Q 20 10 20 -20",
reed@google.com3e71a882012-01-10 18:44:37 +0000806 "M 10 10 C 20 10 20 -20 20 -20",
reed@google.comd4146662012-01-31 15:42:29 +0000807 "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 +0000808 // rect with top two corners replaced by cubics with identical middle
809 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000810 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
811 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000812 };
813 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
814 path.reset();
815 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
816 REPORTER_ASSERT(reporter, valid);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000817 check_direction(reporter, path, SkPath::kCCW_Direction);
reed@google.com3e71a882012-01-10 18:44:37 +0000818 }
reed@google.comac8543f2012-01-30 20:51:25 +0000819
820 // Test two donuts, each wound a different direction. Only the outer contour
821 // determines the cheap direction
822 path.reset();
823 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
824 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000825 check_direction(reporter, path, SkPath::kCW_Direction);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000826
reed@google.comac8543f2012-01-30 20:51:25 +0000827 path.reset();
828 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
829 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000830 check_direction(reporter, path, SkPath::kCCW_Direction);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000831
832 // triangle with one point really far from the origin.
833 path.reset();
834 // the first point is roughly 1.05e10, 1.05e10
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000835 path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
bsalomon@google.com53aab782012-02-23 14:54:49 +0000836 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
837 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000838 check_direction(reporter, path, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000839
840 path.reset();
841 path.conicTo(20, 0, 20, 20, 0.5f);
842 path.close();
843 check_direction(reporter, path, SkPath::kCW_Direction);
844
845 path.reset();
846 path.lineTo(1, 1e7f);
847 path.lineTo(1e7f, 2e7f);
848 path.close();
849 REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity());
850 check_direction(reporter, path, SkPath::kCCW_Direction);
reed@google.com3e71a882012-01-10 18:44:37 +0000851}
852
reed@google.comffdb0182011-11-14 19:29:14 +0000853static void add_rect(SkPath* path, const SkRect& r) {
854 path->moveTo(r.fLeft, r.fTop);
855 path->lineTo(r.fRight, r.fTop);
856 path->lineTo(r.fRight, r.fBottom);
857 path->lineTo(r.fLeft, r.fBottom);
858 path->close();
859}
860
861static void test_bounds(skiatest::Reporter* reporter) {
862 static const SkRect rects[] = {
reed@google.com3563c9e2011-11-14 19:34:57 +0000863 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
864 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
865 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
866 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
reed@google.comffdb0182011-11-14 19:29:14 +0000867 };
868
869 SkPath path0, path1;
870 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
871 path0.addRect(rects[i]);
872 add_rect(&path1, rects[i]);
873 }
874
875 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
876}
877
reed@google.com55b5f4b2011-09-07 12:23:41 +0000878static void stroke_cubic(const SkPoint pts[4]) {
879 SkPath path;
880 path.moveTo(pts[0]);
881 path.cubicTo(pts[1], pts[2], pts[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000882
reed@google.com55b5f4b2011-09-07 12:23:41 +0000883 SkPaint paint;
884 paint.setStyle(SkPaint::kStroke_Style);
885 paint.setStrokeWidth(SK_Scalar1 * 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000886
reed@google.com55b5f4b2011-09-07 12:23:41 +0000887 SkPath fill;
888 paint.getFillPath(path, &fill);
889}
890
891// just ensure this can run w/o any SkASSERTS firing in the debug build
892// we used to assert due to differences in how we determine a degenerate vector
893// but that was fixed with the introduction of SkPoint::CanNormalize
894static void stroke_tiny_cubic() {
895 SkPoint p0[] = {
896 { 372.0f, 92.0f },
897 { 372.0f, 92.0f },
898 { 372.0f, 92.0f },
899 { 372.0f, 92.0f },
900 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000901
reed@google.com55b5f4b2011-09-07 12:23:41 +0000902 stroke_cubic(p0);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000903
reed@google.com55b5f4b2011-09-07 12:23:41 +0000904 SkPoint p1[] = {
905 { 372.0f, 92.0f },
906 { 372.0007f, 92.000755f },
907 { 371.99927f, 92.003922f },
908 { 371.99826f, 92.003899f },
909 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000910
reed@google.com55b5f4b2011-09-07 12:23:41 +0000911 stroke_cubic(p1);
912}
913
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000914static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
915 for (int i = 0; i < 2; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +0000916 SkPath::Iter iter(path, SkToBool(i));
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000917 SkPoint mv;
918 SkPoint pts[4];
919 SkPath::Verb v;
920 int nMT = 0;
921 int nCL = 0;
tomhudson@google.com221db3c2011-07-28 21:10:29 +0000922 mv.set(0, 0);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000923 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
924 switch (v) {
925 case SkPath::kMove_Verb:
926 mv = pts[0];
927 ++nMT;
928 break;
929 case SkPath::kClose_Verb:
930 REPORTER_ASSERT(reporter, mv == pts[0]);
931 ++nCL;
932 break;
933 default:
934 break;
935 }
936 }
937 // if we force a close on the interator we should have a close
938 // for every moveTo
939 REPORTER_ASSERT(reporter, !i || nMT == nCL);
940 }
941}
942
943static void test_close(skiatest::Reporter* reporter) {
944 SkPath closePt;
945 closePt.moveTo(0, 0);
946 closePt.close();
947 check_close(reporter, closePt);
948
949 SkPath openPt;
950 openPt.moveTo(0, 0);
951 check_close(reporter, openPt);
952
953 SkPath empty;
954 check_close(reporter, empty);
955 empty.close();
956 check_close(reporter, empty);
957
958 SkPath rect;
959 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
960 check_close(reporter, rect);
961 rect.close();
962 check_close(reporter, rect);
963
964 SkPath quad;
965 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
966 check_close(reporter, quad);
967 quad.close();
968 check_close(reporter, quad);
969
970 SkPath cubic;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000971 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000972 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
973 check_close(reporter, cubic);
974 cubic.close();
975 check_close(reporter, cubic);
976
977 SkPath line;
978 line.moveTo(SK_Scalar1, SK_Scalar1);
979 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
980 check_close(reporter, line);
981 line.close();
982 check_close(reporter, line);
983
984 SkPath rect2;
985 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
986 rect2.close();
987 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
988 check_close(reporter, rect2);
989 rect2.close();
990 check_close(reporter, rect2);
991
992 SkPath oval3;
993 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
994 oval3.close();
995 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
996 check_close(reporter, oval3);
997 oval3.close();
998 check_close(reporter, oval3);
999
1000 SkPath moves;
1001 moves.moveTo(SK_Scalar1, SK_Scalar1);
1002 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
1003 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
1004 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
1005 check_close(reporter, moves);
reed@google.com55b5f4b2011-09-07 12:23:41 +00001006
1007 stroke_tiny_cubic();
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001008}
1009
reed@google.com7c424812011-05-15 04:38:34 +00001010static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
1011 SkPath::Convexity expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001012 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1013 SkPath::Convexity c = copy.getConvexity();
reed@google.com7c424812011-05-15 04:38:34 +00001014 REPORTER_ASSERT(reporter, c == expected);
1015}
1016
1017static void test_convexity2(skiatest::Reporter* reporter) {
1018 SkPath pt;
1019 pt.moveTo(0, 0);
1020 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +00001021 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001022 check_direction(reporter, pt, SkPath::kUnknown_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001023
reed@google.com7c424812011-05-15 04:38:34 +00001024 SkPath line;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001025 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
1026 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001027 line.close();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001028 check_convexity(reporter, line, SkPath::kConvex_Convexity);
1029 check_direction(reporter, line, SkPath::kUnknown_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001030
reed@google.com7c424812011-05-15 04:38:34 +00001031 SkPath triLeft;
1032 triLeft.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001033 triLeft.lineTo(SK_Scalar1, 0);
1034 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001035 triLeft.close();
1036 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001037 check_direction(reporter, triLeft, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001038
reed@google.com7c424812011-05-15 04:38:34 +00001039 SkPath triRight;
1040 triRight.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001041 triRight.lineTo(-SK_Scalar1, 0);
1042 triRight.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001043 triRight.close();
1044 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001045 check_direction(reporter, triRight, SkPath::kCCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001046
reed@google.com7c424812011-05-15 04:38:34 +00001047 SkPath square;
1048 square.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001049 square.lineTo(SK_Scalar1, 0);
1050 square.lineTo(SK_Scalar1, SK_Scalar1);
1051 square.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001052 square.close();
1053 check_convexity(reporter, square, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001054 check_direction(reporter, square, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001055
reed@google.com7c424812011-05-15 04:38:34 +00001056 SkPath redundantSquare;
1057 redundantSquare.moveTo(0, 0);
1058 redundantSquare.lineTo(0, 0);
1059 redundantSquare.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001060 redundantSquare.lineTo(SK_Scalar1, 0);
1061 redundantSquare.lineTo(SK_Scalar1, 0);
1062 redundantSquare.lineTo(SK_Scalar1, 0);
1063 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1064 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1065 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1066 redundantSquare.lineTo(0, SK_Scalar1);
1067 redundantSquare.lineTo(0, SK_Scalar1);
1068 redundantSquare.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001069 redundantSquare.close();
1070 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001071 check_direction(reporter, redundantSquare, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001072
reed@google.com7c424812011-05-15 04:38:34 +00001073 SkPath bowTie;
1074 bowTie.moveTo(0, 0);
1075 bowTie.lineTo(0, 0);
1076 bowTie.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001077 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1078 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1079 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1080 bowTie.lineTo(SK_Scalar1, 0);
1081 bowTie.lineTo(SK_Scalar1, 0);
1082 bowTie.lineTo(SK_Scalar1, 0);
1083 bowTie.lineTo(0, SK_Scalar1);
1084 bowTie.lineTo(0, SK_Scalar1);
1085 bowTie.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001086 bowTie.close();
1087 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001088 check_direction(reporter, bowTie, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001089
reed@google.com7c424812011-05-15 04:38:34 +00001090 SkPath spiral;
1091 spiral.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001092 spiral.lineTo(100*SK_Scalar1, 0);
1093 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1094 spiral.lineTo(0, 100*SK_Scalar1);
1095 spiral.lineTo(0, 50*SK_Scalar1);
1096 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1097 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001098 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +00001099 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001100 check_direction(reporter, spiral, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001101
reed@google.com7c424812011-05-15 04:38:34 +00001102 SkPath dent;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001103 dent.moveTo(0, 0);
1104 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1105 dent.lineTo(0, 100*SK_Scalar1);
1106 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1107 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001108 dent.close();
1109 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001110 check_direction(reporter, dent, SkPath::kCW_Direction);
reed@google.com7c424812011-05-15 04:38:34 +00001111}
1112
reed@android.com6b82d1a2009-06-03 02:35:01 +00001113static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1114 const SkRect& bounds) {
1115 REPORTER_ASSERT(reporter, p.isConvex());
1116 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +00001117
reed@android.com6b82d1a2009-06-03 02:35:01 +00001118 SkPath p2(p);
1119 REPORTER_ASSERT(reporter, p2.isConvex());
1120 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1121
1122 SkPath other;
1123 other.swap(p2);
1124 REPORTER_ASSERT(reporter, other.isConvex());
1125 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1126}
1127
reed@google.com04863fa2011-05-15 04:08:24 +00001128static void setFromString(SkPath* path, const char str[]) {
1129 bool first = true;
1130 while (str) {
1131 SkScalar x, y;
1132 str = SkParse::FindScalar(str, &x);
1133 if (NULL == str) {
1134 break;
1135 }
1136 str = SkParse::FindScalar(str, &y);
1137 SkASSERT(str);
1138 if (first) {
1139 path->moveTo(x, y);
1140 first = false;
1141 } else {
1142 path->lineTo(x, y);
1143 }
1144 }
1145}
1146
1147static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +00001148 SkPath path;
1149
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001150 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001151 path.addCircle(0, 0, SkIntToScalar(10));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001152 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001153 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001154 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1155
reed@google.com04863fa2011-05-15 04:08:24 +00001156 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001157 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001158 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.com3e71a882012-01-10 18:44:37 +00001159 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCCW_Direction));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001160
reed@google.com04863fa2011-05-15 04:08:24 +00001161 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001162 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001163 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.com3e71a882012-01-10 18:44:37 +00001164 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCW_Direction));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001165
reed@google.com04863fa2011-05-15 04:08:24 +00001166 static const struct {
1167 const char* fPathStr;
1168 SkPath::Convexity fExpectedConvexity;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001169 SkPath::Direction fExpectedDirection;
reed@google.com04863fa2011-05-15 04:08:24 +00001170 } gRec[] = {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001171 { "", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1172 { "0 0", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1173 { "0 0 10 10", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1174 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPath::kUnknown_Direction },
1175 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPath::kCW_Direction },
1176 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPath::kCCW_Direction },
1177 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
1178 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPath::kCW_Direction },
reed@google.com04863fa2011-05-15 04:08:24 +00001179 };
1180
1181 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1182 SkPath path;
1183 setFromString(&path, gRec[i].fPathStr);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001184 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1185 check_direction(reporter, path, gRec[i].fExpectedDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001186 // check after setting the initial convex and direction
1187 if (kDontCheckDir != gRec[i].fExpectedDirection) {
1188 SkPath copy(path);
1189 SkPath::Direction dir;
1190 bool foundDir = copy.cheapComputeDirection(&dir);
1191 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPath::kUnknown_Direction)
1192 ^ foundDir);
1193 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1194 check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
1195 }
1196 REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
1197 check_direction(reporter, path, gRec[i].fExpectedDirection);
reed@google.com04863fa2011-05-15 04:08:24 +00001198 }
1199}
1200
reed@google.com7e6c4d12012-05-10 14:05:43 +00001201static void test_isLine(skiatest::Reporter* reporter) {
1202 SkPath path;
1203 SkPoint pts[2];
1204 const SkScalar value = SkIntToScalar(5);
1205
1206 REPORTER_ASSERT(reporter, !path.isLine(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001207
reed@google.com7e6c4d12012-05-10 14:05:43 +00001208 // set some non-zero values
1209 pts[0].set(value, value);
1210 pts[1].set(value, value);
1211 REPORTER_ASSERT(reporter, !path.isLine(pts));
1212 // check that pts was untouched
1213 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1214 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1215
1216 const SkScalar moveX = SkIntToScalar(1);
1217 const SkScalar moveY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001218 REPORTER_ASSERT(reporter, value != moveX && value != moveY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001219
1220 path.moveTo(moveX, moveY);
1221 REPORTER_ASSERT(reporter, !path.isLine(NULL));
1222 REPORTER_ASSERT(reporter, !path.isLine(pts));
1223 // check that pts was untouched
1224 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1225 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1226
1227 const SkScalar lineX = SkIntToScalar(2);
1228 const SkScalar lineY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001229 REPORTER_ASSERT(reporter, value != lineX && value != lineY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001230
1231 path.lineTo(lineX, lineY);
1232 REPORTER_ASSERT(reporter, path.isLine(NULL));
1233
1234 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1235 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1236 REPORTER_ASSERT(reporter, path.isLine(pts));
1237 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1238 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1239
1240 path.lineTo(0, 0); // too many points/verbs
1241 REPORTER_ASSERT(reporter, !path.isLine(NULL));
1242 REPORTER_ASSERT(reporter, !path.isLine(pts));
1243 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1244 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001245
1246 path.reset();
1247 path.quadTo(1, 1, 2, 2);
1248 REPORTER_ASSERT(reporter, !path.isLine(NULL));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001249}
1250
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001251static void test_conservativelyContains(skiatest::Reporter* reporter) {
1252 SkPath path;
1253
1254 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1255 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1256
1257 // A circle that bounds kBaseRect (with a significant amount of slop)
1258 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001259 circleR = SkScalarMul(circleR, 1.75f) / 2;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001260 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1261
1262 // round-rect radii
1263 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
skia.committer@gmail.comcec8de62012-11-14 02:01:22 +00001264
caryclark@google.com56f233a2012-11-19 13:06:06 +00001265 static const struct SUPPRESS_VISIBILITY_WARNING {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001266 SkRect fQueryRect;
1267 bool fInRect;
1268 bool fInCircle;
1269 bool fInRR;
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001270 bool fInCubicRR;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001271 } kQueries[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001272 {kBaseRect, true, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001273
1274 // rect well inside of kBaseRect
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001275 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1276 kBaseRect.fTop + 0.25f*kBaseRect.height(),
1277 kBaseRect.fRight - 0.25f*kBaseRect.width(),
1278 kBaseRect.fBottom - 0.25f*kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001279 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001280
1281 // rects with edges off by one from kBaseRect's edges
1282 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1283 kBaseRect.width(), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001284 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001285 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1286 kBaseRect.width() + 1, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001287 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001288 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1289 kBaseRect.width() + 1, kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001290 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001291 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1292 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001293 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001294 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1295 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001296 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001297 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1298 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001299 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001300 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1301 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001302 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001303
1304 // zero-w/h rects at each corner of kBaseRect
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001305 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1306 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1307 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1308 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001309
1310 // far away rect
1311 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1312 SkIntToScalar(10), SkIntToScalar(10)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001313 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001314
1315 // very large rect containing kBaseRect
1316 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1317 kBaseRect.fTop - 5 * kBaseRect.height(),
1318 11 * kBaseRect.width(), 11 * kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001319 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001320
1321 // skinny rect that spans same y-range as kBaseRect
1322 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1323 SkIntToScalar(1), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001324 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001325
1326 // short rect that spans same x-range as kBaseRect
1327 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001328 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001329
1330 // skinny rect that spans slightly larger y-range than kBaseRect
1331 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1332 SkIntToScalar(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
1335 // short rect that spans slightly larger x-range than kBaseRect
1336 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1337 kBaseRect.width() + 1, SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001338 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001339 };
1340
1341 for (int inv = 0; inv < 4; ++inv) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001342 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001343 SkRect qRect = kQueries[q].fQueryRect;
1344 if (inv & 0x1) {
1345 SkTSwap(qRect.fLeft, qRect.fRight);
1346 }
1347 if (inv & 0x2) {
1348 SkTSwap(qRect.fTop, qRect.fBottom);
1349 }
1350 for (int d = 0; d < 2; ++d) {
1351 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1352 path.reset();
1353 path.addRect(kBaseRect, dir);
1354 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1355 path.conservativelyContainsRect(qRect));
1356
1357 path.reset();
1358 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1359 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1360 path.conservativelyContainsRect(qRect));
1361
1362 path.reset();
1363 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1364 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1365 path.conservativelyContainsRect(qRect));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001366
1367 path.reset();
1368 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1369 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1370 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1371 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1372 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1373 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1374 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1375 path.close();
1376 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1377 path.conservativelyContainsRect(qRect));
1378
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001379 }
1380 // Slightly non-convex shape, shouldn't contain any rects.
1381 path.reset();
1382 path.moveTo(0, 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001383 path.lineTo(SkIntToScalar(50), 0.05f);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001384 path.lineTo(SkIntToScalar(100), 0);
1385 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1386 path.lineTo(0, SkIntToScalar(100));
1387 path.close();
1388 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1389 }
1390 }
1391
1392 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1393 path.reset();
1394 path.moveTo(0, 0);
1395 path.lineTo(SkIntToScalar(100), 0);
1396 path.lineTo(0, SkIntToScalar(100));
1397
1398 // inside, on along top edge
1399 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1400 SkIntToScalar(10),
1401 SkIntToScalar(10))));
1402 // above
1403 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1404 SkRect::MakeXYWH(SkIntToScalar(50),
1405 SkIntToScalar(-10),
1406 SkIntToScalar(10),
1407 SkIntToScalar(10))));
1408 // to the left
1409 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1410 SkIntToScalar(5),
1411 SkIntToScalar(5),
1412 SkIntToScalar(5))));
1413
1414 // outside the diagonal edge
1415 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1416 SkIntToScalar(200),
1417 SkIntToScalar(20),
1418 SkIntToScalar(5))));
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001419
1420 // same as above path and first test but with an extra moveTo.
1421 path.reset();
1422 path.moveTo(100, 100);
1423 path.moveTo(0, 0);
1424 path.lineTo(SkIntToScalar(100), 0);
1425 path.lineTo(0, SkIntToScalar(100));
1426
1427 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1428 SkIntToScalar(10),
1429 SkIntToScalar(10))));
1430
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001431 path.reset();
1432 path.lineTo(100, 100);
1433 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001434}
1435
reed@google.comf32322b2013-10-16 15:14:04 +00001436static void test_isRect_open_close(skiatest::Reporter* reporter) {
1437 SkPath path;
1438 bool isClosed;
1439
1440 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
reed@google.comf32322b2013-10-16 15:14:04 +00001441 path.close();
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001442
reed@google.comf32322b2013-10-16 15:14:04 +00001443 REPORTER_ASSERT(reporter, path.isRect(NULL, NULL));
1444 REPORTER_ASSERT(reporter, path.isRect(&isClosed, NULL));
1445 REPORTER_ASSERT(reporter, isClosed);
commit-bot@chromium.org7e90e8d2014-02-11 01:38:30 +00001446 REPORTER_ASSERT(reporter, SkPath::kStroke_PathAsRect == path.asRect(NULL));
reed@google.comf32322b2013-10-16 15:14:04 +00001447}
1448
caryclark@google.comf1316942011-07-26 19:54:45 +00001449// Simple isRect test is inline TestPath, below.
1450// test_isRect provides more extensive testing.
1451static void test_isRect(skiatest::Reporter* reporter) {
reed@google.comf32322b2013-10-16 15:14:04 +00001452 test_isRect_open_close(reporter);
1453
caryclark@google.comf1316942011-07-26 19:54:45 +00001454 // passing tests (all moveTo / lineTo...
1455 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1456 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1457 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1458 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1459 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1460 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1461 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1462 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1463 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001464 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1465 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 +00001466 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1467 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1468 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
caryclark@google.combfe90372012-11-21 13:56:20 +00001469 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
rmistry@google.comd6176b02012-08-23 18:14:13 +00001470
caryclark@google.comf1316942011-07-26 19:54:45 +00001471 // failing tests
1472 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1473 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1474 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1475 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1476 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1477 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1478 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1479 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
caryclark@google.combfe90372012-11-21 13:56:20 +00001480 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
1481 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
1482 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
rmistry@google.comd6176b02012-08-23 18:14:13 +00001483
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001484 // no close, but we should detect them as fillably the same as a rect
1485 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1486 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
1487 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
1488
1489 // like c2, but we double-back on ourselves
1490 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
1491 // like c2, but we overshoot the start point
1492 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
1493 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
caryclark@google.comf1316942011-07-26 19:54:45 +00001494
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001495 struct IsRectTest {
1496 SkPoint *fPoints;
1497 size_t fPointCount;
1498 bool fClose;
1499 bool fIsRect;
1500 } tests[] = {
1501 { r1, SK_ARRAY_COUNT(r1), true, true },
1502 { r2, SK_ARRAY_COUNT(r2), true, true },
1503 { r3, SK_ARRAY_COUNT(r3), true, true },
1504 { r4, SK_ARRAY_COUNT(r4), true, true },
1505 { r5, SK_ARRAY_COUNT(r5), true, true },
1506 { r6, SK_ARRAY_COUNT(r6), true, true },
1507 { r7, SK_ARRAY_COUNT(r7), true, true },
1508 { r8, SK_ARRAY_COUNT(r8), true, true },
1509 { r9, SK_ARRAY_COUNT(r9), true, true },
1510 { ra, SK_ARRAY_COUNT(ra), true, true },
1511 { rb, SK_ARRAY_COUNT(rb), true, true },
1512 { rc, SK_ARRAY_COUNT(rc), true, true },
1513 { rd, SK_ARRAY_COUNT(rd), true, true },
1514 { re, SK_ARRAY_COUNT(re), true, true },
1515 { rf, SK_ARRAY_COUNT(rf), true, true },
1516
1517 { f1, SK_ARRAY_COUNT(f1), true, false },
1518 { f2, SK_ARRAY_COUNT(f2), true, false },
1519 { f3, SK_ARRAY_COUNT(f3), true, false },
1520 { f4, SK_ARRAY_COUNT(f4), true, false },
1521 { f5, SK_ARRAY_COUNT(f5), true, false },
1522 { f6, SK_ARRAY_COUNT(f6), true, false },
1523 { f7, SK_ARRAY_COUNT(f7), true, false },
1524 { f8, SK_ARRAY_COUNT(f8), true, false },
1525 { f9, SK_ARRAY_COUNT(f9), true, false },
1526 { fa, SK_ARRAY_COUNT(fa), true, false },
1527 { fb, SK_ARRAY_COUNT(fb), true, false },
1528
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001529 { c1, SK_ARRAY_COUNT(c1), false, true },
1530 { c2, SK_ARRAY_COUNT(c2), false, true },
1531 { c3, SK_ARRAY_COUNT(c3), false, true },
1532
1533 { d1, SK_ARRAY_COUNT(d1), false, false },
1534 { d2, SK_ARRAY_COUNT(d2), false, false },
1535 { d3, SK_ARRAY_COUNT(d3), false, false },
caryclark@google.comf1316942011-07-26 19:54:45 +00001536 };
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001537
1538 const size_t testCount = SK_ARRAY_COUNT(tests);
caryclark@google.comf1316942011-07-26 19:54:45 +00001539 size_t index;
1540 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1541 SkPath path;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001542 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1543 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1544 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.comf1316942011-07-26 19:54:45 +00001545 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001546 if (tests[testIndex].fClose) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001547 path.close();
1548 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001549 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(NULL));
1550 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(NULL, NULL));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001551
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001552 if (tests[testIndex].fIsRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001553 SkRect computed, expected;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001554 expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001555 REPORTER_ASSERT(reporter, path.isRect(&computed));
1556 REPORTER_ASSERT(reporter, expected == computed);
skia.committer@gmail.com1c9c0d32012-11-22 02:02:41 +00001557
caryclark@google.comf68154a2012-11-21 15:18:06 +00001558 bool isClosed;
1559 SkPath::Direction direction, cheapDirection;
1560 REPORTER_ASSERT(reporter, path.cheapComputeDirection(&cheapDirection));
robertphillips@google.com8fd16032013-06-25 15:39:58 +00001561 REPORTER_ASSERT(reporter, path.isRect(&isClosed, &direction));
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001562 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001563 REPORTER_ASSERT(reporter, direction == cheapDirection);
commit-bot@chromium.orgc2abd542014-01-25 16:54:31 +00001564 direction = (SkPath::Direction) -1;
commit-bot@chromium.org7e90e8d2014-02-11 01:38:30 +00001565 if (!tests[testIndex].fClose) {
commit-bot@chromium.orgc2abd542014-01-25 16:54:31 +00001566 REPORTER_ASSERT(reporter, SkPath::kFill_PathAsRect == path.asRect());
1567 REPORTER_ASSERT(reporter, SkPath::kFill_PathAsRect == path.asRect(&direction));
1568 } else {
1569 REPORTER_ASSERT(reporter, SkPath::kStroke_PathAsRect == path.asRect());
1570 REPORTER_ASSERT(reporter, SkPath::kStroke_PathAsRect == path.asRect(&direction));
1571 }
1572 REPORTER_ASSERT(reporter, direction == cheapDirection);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001573 } else {
1574 SkRect computed;
1575 computed.set(123, 456, 789, 1011);
1576 REPORTER_ASSERT(reporter, !path.isRect(&computed));
1577 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
1578 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
1579
1580 bool isClosed = (bool) -1;
1581 SkPath::Direction direction = (SkPath::Direction) -1;
robertphillips@google.com8fd16032013-06-25 15:39:58 +00001582 REPORTER_ASSERT(reporter, !path.isRect(&isClosed, &direction));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001583 REPORTER_ASSERT(reporter, isClosed == (bool) -1);
1584 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
commit-bot@chromium.orgc2abd542014-01-25 16:54:31 +00001585 REPORTER_ASSERT(reporter, SkPath::kNone_PathAsRect == path.asRect());
1586 REPORTER_ASSERT(reporter, SkPath::kNone_PathAsRect == path.asRect(&direction));
1587 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001588 }
caryclark@google.comf1316942011-07-26 19:54:45 +00001589 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001590
caryclark@google.comf1316942011-07-26 19:54:45 +00001591 // fail, close then line
1592 SkPath path1;
1593 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001594 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001595 path1.lineTo(r1[index].fX, r1[index].fY);
1596 }
1597 path1.close();
1598 path1.lineTo(1, 0);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001599 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001600
caryclark@google.comf1316942011-07-26 19:54:45 +00001601 // fail, move in the middle
1602 path1.reset();
1603 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001604 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001605 if (index == 2) {
1606 path1.moveTo(1, .5f);
1607 }
1608 path1.lineTo(r1[index].fX, r1[index].fY);
1609 }
1610 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001611 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00001612
1613 // fail, move on the edge
1614 path1.reset();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001615 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001616 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
1617 path1.lineTo(r1[index].fX, r1[index].fY);
1618 }
1619 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001620 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001621
caryclark@google.comf1316942011-07-26 19:54:45 +00001622 // fail, quad
1623 path1.reset();
1624 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001625 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001626 if (index == 2) {
1627 path1.quadTo(1, .5f, 1, .5f);
1628 }
1629 path1.lineTo(r1[index].fX, r1[index].fY);
1630 }
1631 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001632 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001633
caryclark@google.comf1316942011-07-26 19:54:45 +00001634 // fail, cubic
1635 path1.reset();
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 if (index == 2) {
1639 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
1640 }
1641 path1.lineTo(r1[index].fX, r1[index].fY);
1642 }
1643 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001644 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00001645}
1646
caryclark@google.com56f233a2012-11-19 13:06:06 +00001647static void test_isNestedRects(skiatest::Reporter* reporter) {
1648 // passing tests (all moveTo / lineTo...
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001649 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001650 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1651 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1652 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001653 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001654 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1655 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1656 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1657 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001658 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
1659 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 +00001660 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
1661 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
1662 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001663
1664 // failing tests
1665 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1666 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1667 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1668 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1669 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1670 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1671 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1672 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
1673
1674 // failing, no close
1675 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
1676 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
1677
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001678 struct IsNestedRectTest {
1679 SkPoint *fPoints;
1680 size_t fPointCount;
1681 SkPath::Direction fDirection;
1682 bool fClose;
1683 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
1684 } tests[] = {
1685 { r1, SK_ARRAY_COUNT(r1), SkPath::kCW_Direction , true, true },
1686 { r2, SK_ARRAY_COUNT(r2), SkPath::kCW_Direction , true, true },
1687 { r3, SK_ARRAY_COUNT(r3), SkPath::kCW_Direction , true, true },
1688 { r4, SK_ARRAY_COUNT(r4), SkPath::kCW_Direction , true, true },
1689 { r5, SK_ARRAY_COUNT(r5), SkPath::kCCW_Direction, true, true },
1690 { r6, SK_ARRAY_COUNT(r6), SkPath::kCCW_Direction, true, true },
1691 { r7, SK_ARRAY_COUNT(r7), SkPath::kCCW_Direction, true, true },
1692 { r8, SK_ARRAY_COUNT(r8), SkPath::kCCW_Direction, true, true },
1693 { r9, SK_ARRAY_COUNT(r9), SkPath::kCCW_Direction, true, true },
1694 { ra, SK_ARRAY_COUNT(ra), SkPath::kCCW_Direction, true, true },
1695 { rb, SK_ARRAY_COUNT(rb), SkPath::kCW_Direction, true, true },
1696 { rc, SK_ARRAY_COUNT(rc), SkPath::kCW_Direction, true, true },
1697 { rd, SK_ARRAY_COUNT(rd), SkPath::kCCW_Direction, true, true },
1698 { re, SK_ARRAY_COUNT(re), SkPath::kCW_Direction, true, true },
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001699
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001700 { f1, SK_ARRAY_COUNT(f1), SkPath::kUnknown_Direction, true, false },
1701 { f2, SK_ARRAY_COUNT(f2), SkPath::kUnknown_Direction, true, false },
1702 { f3, SK_ARRAY_COUNT(f3), SkPath::kUnknown_Direction, true, false },
1703 { f4, SK_ARRAY_COUNT(f4), SkPath::kUnknown_Direction, true, false },
1704 { f5, SK_ARRAY_COUNT(f5), SkPath::kUnknown_Direction, true, false },
1705 { f6, SK_ARRAY_COUNT(f6), SkPath::kUnknown_Direction, true, false },
1706 { f7, SK_ARRAY_COUNT(f7), SkPath::kUnknown_Direction, true, false },
1707 { f8, SK_ARRAY_COUNT(f8), SkPath::kUnknown_Direction, true, false },
1708
1709 { c1, SK_ARRAY_COUNT(c1), SkPath::kUnknown_Direction, false, false },
1710 { c2, SK_ARRAY_COUNT(c2), SkPath::kUnknown_Direction, false, false },
1711 };
1712
1713 const size_t testCount = SK_ARRAY_COUNT(tests);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001714 size_t index;
1715 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001716 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1717 SkPath path;
1718 if (rectFirst) {
1719 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1720 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001721 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1722 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1723 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001724 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001725 if (tests[testIndex].fClose) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001726 path.close();
1727 }
1728 if (!rectFirst) {
1729 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1730 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001731 REPORTER_ASSERT(reporter, tests[testIndex].fIsNestedRect == path.isNestedRects(NULL));
1732 if (tests[testIndex].fIsNestedRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001733 SkRect expected[2], computed[2];
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001734 SkPath::Direction expectedDirs[2], computedDirs[2];
caryclark@google.com56f233a2012-11-19 13:06:06 +00001735 SkRect testBounds;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001736 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001737 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
1738 expected[1] = testBounds;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001739 if (rectFirst) {
1740 expectedDirs[0] = SkPath::kCW_Direction;
1741 } else {
1742 expectedDirs[0] = SkPath::kCCW_Direction;
1743 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001744 expectedDirs[1] = tests[testIndex].fDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001745 REPORTER_ASSERT(reporter, path.isNestedRects(computed, computedDirs));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001746 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
1747 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001748 REPORTER_ASSERT(reporter, expectedDirs[0] == computedDirs[0]);
1749 REPORTER_ASSERT(reporter, expectedDirs[1] == computedDirs[1]);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001750 }
caryclark@google.com56f233a2012-11-19 13:06:06 +00001751 }
1752
1753 // fail, close then line
1754 SkPath path1;
1755 if (rectFirst) {
1756 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1757 }
1758 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001759 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001760 path1.lineTo(r1[index].fX, r1[index].fY);
1761 }
1762 path1.close();
1763 path1.lineTo(1, 0);
1764 if (!rectFirst) {
1765 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1766 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001767 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001768
1769 // fail, move in the middle
1770 path1.reset();
1771 if (rectFirst) {
1772 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1773 }
1774 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001775 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001776 if (index == 2) {
1777 path1.moveTo(1, .5f);
1778 }
1779 path1.lineTo(r1[index].fX, r1[index].fY);
1780 }
1781 path1.close();
1782 if (!rectFirst) {
1783 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1784 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001785 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001786
1787 // fail, move on the edge
1788 path1.reset();
1789 if (rectFirst) {
1790 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1791 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001792 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001793 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
1794 path1.lineTo(r1[index].fX, r1[index].fY);
1795 }
1796 path1.close();
1797 if (!rectFirst) {
1798 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1799 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001800 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001801
1802 // fail, quad
1803 path1.reset();
1804 if (rectFirst) {
1805 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1806 }
1807 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001808 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001809 if (index == 2) {
1810 path1.quadTo(1, .5f, 1, .5f);
1811 }
1812 path1.lineTo(r1[index].fX, r1[index].fY);
1813 }
1814 path1.close();
1815 if (!rectFirst) {
1816 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1817 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001818 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001819
1820 // fail, cubic
1821 path1.reset();
1822 if (rectFirst) {
1823 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1824 }
1825 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001826 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001827 if (index == 2) {
1828 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
1829 }
1830 path1.lineTo(r1[index].fX, r1[index].fY);
1831 }
1832 path1.close();
1833 if (!rectFirst) {
1834 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1835 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001836 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
skia.committer@gmail.com34587162012-11-20 02:01:23 +00001837
caryclark@google.com56f233a2012-11-19 13:06:06 +00001838 // fail, not nested
1839 path1.reset();
1840 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
1841 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001842 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001843 }
caryclark@google.combfe90372012-11-21 13:56:20 +00001844
1845 // pass, stroke rect
1846 SkPath src, dst;
1847 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
1848 SkPaint strokePaint;
1849 strokePaint.setStyle(SkPaint::kStroke_Style);
1850 strokePaint.setStrokeWidth(2);
1851 strokePaint.getFillPath(src, &dst);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001852 REPORTER_ASSERT(reporter, dst.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001853}
1854
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001855static void write_and_read_back(skiatest::Reporter* reporter,
1856 const SkPath& p) {
commit-bot@chromium.org19382422014-01-14 20:51:26 +00001857 SkWriter32 writer;
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001858 writer.writePath(p);
reed@google.com44699382013-10-31 17:28:30 +00001859 size_t size = writer.bytesWritten();
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001860 SkAutoMalloc storage(size);
1861 writer.flatten(storage.get());
1862 SkReader32 reader(storage.get(), size);
1863
1864 SkPath readBack;
1865 REPORTER_ASSERT(reporter, readBack != p);
1866 reader.readPath(&readBack);
1867 REPORTER_ASSERT(reporter, readBack == p);
1868
rmistry@google.comd6176b02012-08-23 18:14:13 +00001869 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001870 p.getConvexityOrUnknown());
1871
1872 REPORTER_ASSERT(reporter, readBack.isOval(NULL) == p.isOval(NULL));
1873
1874 const SkRect& origBounds = p.getBounds();
1875 const SkRect& readBackBounds = readBack.getBounds();
1876
1877 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
1878}
1879
reed@google.com53effc52011-09-21 19:05:12 +00001880static void test_flattening(skiatest::Reporter* reporter) {
1881 SkPath p;
1882
1883 static const SkPoint pts[] = {
1884 { 0, 0 },
1885 { SkIntToScalar(10), SkIntToScalar(10) },
1886 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
1887 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
1888 };
1889 p.moveTo(pts[0]);
1890 p.lineTo(pts[1]);
1891 p.quadTo(pts[2], pts[3]);
1892 p.cubicTo(pts[4], pts[5], pts[6]);
1893
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001894 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001895
1896 // create a buffer that should be much larger than the path so we don't
1897 // kill our stack if writer goes too far.
1898 char buffer[1024];
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00001899 size_t size1 = p.writeToMemory(NULL);
1900 size_t size2 = p.writeToMemory(buffer);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001901 REPORTER_ASSERT(reporter, size1 == size2);
1902
1903 SkPath p2;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00001904 size_t size3 = p2.readFromMemory(buffer, 1024);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001905 REPORTER_ASSERT(reporter, size1 == size3);
1906 REPORTER_ASSERT(reporter, p == p2);
1907
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001908 size3 = p2.readFromMemory(buffer, 0);
1909 REPORTER_ASSERT(reporter, !size3);
1910
1911 SkPath tooShort;
1912 size3 = tooShort.readFromMemory(buffer, size1 - 1);
1913 REPORTER_ASSERT(reporter, tooShort.isEmpty());
1914
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001915 char buffer2[1024];
1916 size3 = p2.writeToMemory(buffer2);
1917 REPORTER_ASSERT(reporter, size1 == size3);
1918 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001919
1920 // test persistence of the oval flag & convexity
1921 {
1922 SkPath oval;
1923 SkRect rect = SkRect::MakeWH(10, 10);
1924 oval.addOval(rect);
1925
1926 write_and_read_back(reporter, oval);
1927 }
reed@google.com53effc52011-09-21 19:05:12 +00001928}
1929
1930static void test_transform(skiatest::Reporter* reporter) {
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00001931 SkPath p;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001932
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001933#define CONIC_PERSPECTIVE_BUG_FIXED 0
reed@google.com53effc52011-09-21 19:05:12 +00001934 static const SkPoint pts[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001935 { 0, 0 }, // move
1936 { SkIntToScalar(10), SkIntToScalar(10) }, // line
1937 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
1938 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
1939#if CONIC_PERSPECTIVE_BUG_FIXED
1940 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
1941#endif
reed@google.com53effc52011-09-21 19:05:12 +00001942 };
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001943 const int kPtCount = SK_ARRAY_COUNT(pts);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00001944
reed@google.com53effc52011-09-21 19:05:12 +00001945 p.moveTo(pts[0]);
1946 p.lineTo(pts[1]);
1947 p.quadTo(pts[2], pts[3]);
1948 p.cubicTo(pts[4], pts[5], pts[6]);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001949#if CONIC_PERSPECTIVE_BUG_FIXED
1950 p.conicTo(pts[4], pts[5], 0.5f);
1951#endif
1952 p.close();
rmistry@google.comd6176b02012-08-23 18:14:13 +00001953
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00001954 {
1955 SkMatrix matrix;
1956 matrix.reset();
1957 SkPath p1;
1958 p.transform(matrix, &p1);
1959 REPORTER_ASSERT(reporter, p == p1);
reed@google.com53effc52011-09-21 19:05:12 +00001960 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001961
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00001962
1963 {
1964 SkMatrix matrix;
1965 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
1966
1967 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
1968
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00001969 p.transform(matrix, &p1);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00001970 SkPoint pts1[kPtCount];
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00001971 int count = p1.getPoints(pts1, kPtCount);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00001972 REPORTER_ASSERT(reporter, kPtCount == count);
1973 for (int i = 0; i < count; ++i) {
1974 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
1975 REPORTER_ASSERT(reporter, newPt == pts1[i]);
1976 }
1977 }
1978
1979 {
1980 SkMatrix matrix;
1981 matrix.reset();
1982 matrix.setPerspX(SkScalarToPersp(4));
1983
1984 SkPath p1;
1985 p1.moveTo(SkPoint::Make(0, 0));
1986
1987 p.transform(matrix, &p1);
1988 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
1989 p1.transform(matrix, NULL);
1990 SkRect pBounds = p.getBounds();
1991 SkRect p1Bounds = p1.getBounds();
1992 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
1993 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
1994 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
1995 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
1996 }
1997
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001998 p.reset();
1999 p.addCircle(0, 0, 1, SkPath::kCW_Direction);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002000
2001 {
2002 SkMatrix matrix;
2003 matrix.reset();
2004 SkPath p1;
2005 p1.moveTo(SkPoint::Make(0, 0));
2006
2007 p.transform(matrix, &p1);
2008 REPORTER_ASSERT(reporter, p1.cheapIsDirection(SkPath::kCW_Direction));
2009 }
2010
2011
2012 {
2013 SkMatrix matrix;
2014 matrix.reset();
2015 matrix.setScaleX(-1);
2016 SkPath p1;
2017 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2018
2019 p.transform(matrix, &p1);
2020 REPORTER_ASSERT(reporter, p1.cheapIsDirection(SkPath::kCCW_Direction));
2021 }
2022
2023 {
2024 SkMatrix matrix;
2025 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2026 SkPath p1;
2027 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2028
2029 p.transform(matrix, &p1);
2030 REPORTER_ASSERT(reporter, p1.cheapIsDirection(SkPath::kUnknown_Direction));
2031 }
reed@google.com53effc52011-09-21 19:05:12 +00002032}
2033
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002034static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002035 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002036 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002037
caryclark@google.com56f233a2012-11-19 13:06:06 +00002038 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002039 const char* testPath;
2040 const size_t numResultPts;
2041 const SkRect resultBound;
2042 const SkPath::Verb* resultVerbs;
2043 const size_t numResultVerbs;
2044 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002045
schenney@chromium.org7e963602012-06-13 17:05:43 +00002046 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2047 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2048 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2049 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2050 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2051 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2052 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2053 static const SkPath::Verb resultVerbs8[] = {
2054 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2055 };
2056 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2057 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2058 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2059 static const SkPath::Verb resultVerbs12[] = {
2060 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2061 };
2062 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2063 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2064 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2065 static const SkPath::Verb resultVerbs16[] = {
2066 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2067 };
2068 static const struct zeroPathTestData gZeroLengthTests[] = {
2069 { "M 1 1", 1, {0, 0, 0, 0}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002070 { "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 +00002071 { "M 1 1 z", 1, {0, 0, 0, 0}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002072 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2073 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2074 { "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) },
2075 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2076 { "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) },
2077 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2078 { "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) },
2079 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2080 { "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) },
2081 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2082 { "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 +00002083 SK_ARRAY_COUNT(resultVerbs14)
2084 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002085 { "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) },
2086 { "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 +00002087 SK_ARRAY_COUNT(resultVerbs16)
2088 }
2089 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002090
schenney@chromium.org7e963602012-06-13 17:05:43 +00002091 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2092 p.reset();
2093 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2094 REPORTER_ASSERT(reporter, valid);
2095 REPORTER_ASSERT(reporter, !p.isEmpty());
2096 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2097 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2098 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2099 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2100 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2101 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002102 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002103}
2104
2105struct SegmentInfo {
2106 SkPath fPath;
2107 int fPointCount;
2108};
2109
reed@google.com10296cc2011-09-21 12:29:05 +00002110#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2111
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002112static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00002113 SkPath p, p2;
2114
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002115 p.moveTo(0, 0);
2116 p.quadTo(100, 100, 200, 200);
2117 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2118 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002119 p2 = p;
2120 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002121 p.cubicTo(100, 100, 200, 200, 300, 300);
2122 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2123 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002124 p2 = p;
2125 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2126
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002127 p.reset();
2128 p.moveTo(0, 0);
2129 p.cubicTo(100, 100, 200, 200, 300, 300);
2130 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00002131 p2 = p;
2132 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00002133
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002134 REPORTER_ASSERT(reporter, !p.isEmpty());
2135}
2136
2137static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002138 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002139 SkPoint pts[4];
2140
2141 // Test an iterator with no path
2142 SkPath::Iter noPathIter;
2143 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002144
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002145 // Test that setting an empty path works
2146 noPathIter.setPath(p, false);
2147 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002148
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002149 // Test that close path makes no difference for an empty path
2150 noPathIter.setPath(p, true);
2151 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002152
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002153 // Test an iterator with an initial empty path
2154 SkPath::Iter iter(p, false);
2155 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2156
2157 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00002158 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002159 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2160
rmistry@google.comd6176b02012-08-23 18:14:13 +00002161
schenney@chromium.org7e963602012-06-13 17:05:43 +00002162 struct iterTestData {
2163 const char* testPath;
2164 const bool forceClose;
2165 const bool consumeDegenerates;
2166 const size_t* numResultPtsPerVerb;
2167 const SkPoint* resultPts;
2168 const SkPath::Verb* resultVerbs;
2169 const size_t numResultVerbs;
2170 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002171
schenney@chromium.org7e963602012-06-13 17:05:43 +00002172 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2173 static const SkPath::Verb resultVerbs2[] = {
2174 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2175 };
2176 static const SkPath::Verb resultVerbs3[] = {
2177 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2178 };
2179 static const SkPath::Verb resultVerbs4[] = {
2180 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2181 };
2182 static const SkPath::Verb resultVerbs5[] = {
2183 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2184 };
2185 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00002186 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2187 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2188 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2189 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002190 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002191 static const SkPoint resultPts2[] = {
2192 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2193 };
2194 static const SkPoint resultPts3[] = {
2195 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2196 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2197 };
2198 static const SkPoint resultPts4[] = {
2199 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2200 };
2201 static const SkPoint resultPts5[] = {
2202 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2203 };
2204 static const struct iterTestData gIterTests[] = {
2205 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002206 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2207 { "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 +00002208 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2209 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2210 { "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) },
2211 { "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 +00002212 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2213 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2214 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2215 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2216 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2217 { "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 +00002218 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002219
schenney@chromium.org7e963602012-06-13 17:05:43 +00002220 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2221 p.reset();
2222 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2223 REPORTER_ASSERT(reporter, valid);
2224 iter.setPath(p, gIterTests[i].forceClose);
2225 int j = 0, l = 0;
2226 do {
2227 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2228 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2229 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2230 }
2231 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2232 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2233 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002234
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002235 p.reset();
2236 iter.setPath(p, false);
2237 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2238 p.lineTo(1, 1);
2239 p.close();
2240 iter.setPath(p, false);
2241 REPORTER_ASSERT(reporter, iter.isClosedContour());
2242 p.reset();
2243 iter.setPath(p, true);
2244 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2245 p.lineTo(1, 1);
2246 iter.setPath(p, true);
2247 REPORTER_ASSERT(reporter, iter.isClosedContour());
2248 p.moveTo(0, 0);
2249 p.lineTo(2, 2);
2250 iter.setPath(p, false);
2251 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2252
2253 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
2254 // check to see if the result is correct.
2255 for (int setNaN = 0; setNaN < 4; ++setNaN) {
2256 p.reset();
2257 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
2258 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
2259 iter.setPath(p, true);
2260 iter.next(pts, false);
2261 iter.next(pts, false);
2262 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
2263 }
2264
2265 p.reset();
2266 p.quadTo(0, 0, 0, 0);
2267 iter.setPath(p, false);
2268 iter.next(pts, false);
2269 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false));
2270 iter.setPath(p, false);
2271 iter.next(pts, false);
2272 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2273
2274 p.reset();
2275 p.conicTo(0, 0, 0, 0, 0.5f);
2276 iter.setPath(p, false);
2277 iter.next(pts, false);
2278 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
2279 iter.setPath(p, false);
2280 iter.next(pts, false);
2281 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2282
2283 p.reset();
2284 p.cubicTo(0, 0, 0, 0, 0, 0);
2285 iter.setPath(p, false);
2286 iter.next(pts, false);
2287 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2288 iter.setPath(p, false);
2289 iter.next(pts, false);
2290 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2291
2292 p.moveTo(1, 1); // add a trailing moveto
2293 iter.setPath(p, false);
2294 iter.next(pts, false);
2295 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2296 iter.setPath(p, false);
2297 iter.next(pts, false);
2298 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2299
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002300 // The GM degeneratesegments.cpp test is more extensive
2301}
2302
2303static void test_raw_iter(skiatest::Reporter* reporter) {
2304 SkPath p;
2305 SkPoint pts[4];
2306
2307 // Test an iterator with no path
2308 SkPath::RawIter noPathIter;
2309 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2310 // Test that setting an empty path works
2311 noPathIter.setPath(p);
2312 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00002313
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002314 // Test an iterator with an initial empty path
2315 SkPath::RawIter iter(p);
2316 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2317
2318 // Test that a move-only path returns the move.
2319 p.moveTo(SK_Scalar1, 0);
2320 iter.setPath(p);
2321 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2322 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2323 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2324 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2325
2326 // No matter how many moves we add, we should get them all back
2327 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2328 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2329 iter.setPath(p);
2330 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2331 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2332 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2333 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2334 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2335 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2336 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2337 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2338 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2339 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2340
2341 // Initial close is never ever stored
2342 p.reset();
2343 p.close();
2344 iter.setPath(p);
2345 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2346
2347 // Move/close sequences
2348 p.reset();
2349 p.close(); // Not stored, no purpose
2350 p.moveTo(SK_Scalar1, 0);
2351 p.close();
2352 p.close(); // Not stored, no purpose
2353 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2354 p.close();
2355 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2356 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
2357 p.close();
2358 iter.setPath(p);
2359 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2360 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2361 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2362 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2363 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2364 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2365 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2366 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2367 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2368 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2369 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2370 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2371 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2372 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2373 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2374 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2375 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2376 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2377 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2378 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2379 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2380 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2381
2382 // Generate random paths and verify
2383 SkPoint randomPts[25];
2384 for (int i = 0; i < 5; ++i) {
2385 for (int j = 0; j < 5; ++j) {
2386 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
2387 }
2388 }
2389
2390 // Max of 10 segments, max 3 points per segment
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002391 SkRandom rand(9876543);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002392 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00002393 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002394 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00002395
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002396 for (int i = 0; i < 500; ++i) {
2397 p.reset();
2398 bool lastWasClose = true;
2399 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00002400 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002401 int numPoints = 0;
2402 int numVerbs = (rand.nextU() >> 16) % 10;
2403 int numIterVerbs = 0;
2404 for (int j = 0; j < numVerbs; ++j) {
2405 do {
2406 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
2407 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002408 switch (nextVerb) {
2409 case SkPath::kMove_Verb:
2410 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2411 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00002412 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002413 numPoints += 1;
2414 lastWasClose = false;
2415 haveMoveTo = true;
2416 break;
2417 case SkPath::kLine_Verb:
2418 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002419 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002420 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2421 haveMoveTo = true;
2422 }
2423 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2424 p.lineTo(expectedPts[numPoints]);
2425 numPoints += 1;
2426 lastWasClose = false;
2427 break;
2428 case SkPath::kQuad_Verb:
2429 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002430 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002431 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2432 haveMoveTo = true;
2433 }
2434 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2435 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2436 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
2437 numPoints += 2;
2438 lastWasClose = false;
2439 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002440 case SkPath::kConic_Verb:
2441 if (!haveMoveTo) {
2442 expectedPts[numPoints++] = lastMoveToPt;
2443 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2444 haveMoveTo = true;
2445 }
2446 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2447 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2448 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2449 rand.nextUScalar1() * 4);
2450 numPoints += 2;
2451 lastWasClose = false;
2452 break;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002453 case SkPath::kCubic_Verb:
2454 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002455 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002456 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2457 haveMoveTo = true;
2458 }
2459 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2460 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2461 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
2462 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2463 expectedPts[numPoints + 2]);
2464 numPoints += 3;
2465 lastWasClose = false;
2466 break;
2467 case SkPath::kClose_Verb:
2468 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00002469 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002470 lastWasClose = true;
2471 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002472 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002473 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002474 }
2475 expectedVerbs[numIterVerbs++] = nextVerb;
2476 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00002477
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002478 iter.setPath(p);
2479 numVerbs = numIterVerbs;
2480 numIterVerbs = 0;
2481 int numIterPts = 0;
2482 SkPoint lastMoveTo;
2483 SkPoint lastPt;
2484 lastMoveTo.set(0, 0);
2485 lastPt.set(0, 0);
2486 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
2487 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
2488 numIterVerbs++;
2489 switch (nextVerb) {
2490 case SkPath::kMove_Verb:
2491 REPORTER_ASSERT(reporter, numIterPts < numPoints);
2492 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
2493 lastPt = lastMoveTo = pts[0];
2494 numIterPts += 1;
2495 break;
2496 case SkPath::kLine_Verb:
2497 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
2498 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2499 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2500 lastPt = pts[1];
2501 numIterPts += 1;
2502 break;
2503 case SkPath::kQuad_Verb:
reed@google.com277c3f82013-05-31 15:17:50 +00002504 case SkPath::kConic_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002505 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
2506 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2507 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2508 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2509 lastPt = pts[2];
2510 numIterPts += 2;
2511 break;
2512 case SkPath::kCubic_Verb:
2513 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
2514 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2515 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2516 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2517 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
2518 lastPt = pts[3];
2519 numIterPts += 3;
2520 break;
2521 case SkPath::kClose_Verb:
2522 REPORTER_ASSERT(reporter, pts[0] == lastMoveTo);
2523 lastPt = lastMoveTo;
2524 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002525 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002526 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002527 }
2528 }
2529 REPORTER_ASSERT(reporter, numIterPts == numPoints);
2530 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
2531 }
2532}
2533
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002534static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002535 const SkPath& path,
2536 bool expectedCircle,
2537 SkPath::Direction expectedDir) {
robertphillips@google.com466310d2013-12-03 16:43:54 +00002538 SkRect rect = SkRect::MakeEmpty();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002539 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
2540 REPORTER_ASSERT(reporter, path.cheapIsDirection(expectedDir));
skia.committer@gmail.comfbb0ed92012-11-13 21:46:06 +00002541
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002542 if (expectedCircle) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002543 REPORTER_ASSERT(reporter, rect.height() == rect.width());
2544 }
2545}
2546
2547static void test_circle_skew(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002548 const SkPath& path,
2549 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002550 SkPath tmp;
2551
2552 SkMatrix m;
2553 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
2554 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002555 // this matrix reverses the direction.
2556 if (SkPath::kCCW_Direction == dir) {
2557 dir = SkPath::kCW_Direction;
2558 } else {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002559 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002560 dir = SkPath::kCCW_Direction;
2561 }
2562 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002563}
2564
2565static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002566 const SkPath& path,
2567 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002568 SkPath tmp;
2569
2570 // translate at small offset
2571 SkMatrix m;
2572 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
2573 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002574 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002575
2576 tmp.reset();
2577 m.reset();
2578
2579 // translate at a relatively big offset
2580 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
2581 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002582 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002583}
2584
2585static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002586 const SkPath& path,
2587 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002588 for (int angle = 0; angle < 360; ++angle) {
2589 SkPath tmp;
2590 SkMatrix m;
2591 m.setRotate(SkIntToScalar(angle));
2592 path.transform(m, &tmp);
2593
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002594 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002595 // degrees is not an oval anymore, this can be improved. we made this
2596 // for the simplicity of our implementation.
2597 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002598 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002599 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002600 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002601 }
2602 }
2603}
2604
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002605static void test_circle_mirror_x(skiatest::Reporter* reporter,
2606 const SkPath& path,
2607 SkPath::Direction dir) {
2608 SkPath tmp;
2609 SkMatrix m;
2610 m.reset();
2611 m.setScaleX(-SK_Scalar1);
2612 path.transform(m, &tmp);
2613
2614 if (SkPath::kCW_Direction == dir) {
2615 dir = SkPath::kCCW_Direction;
2616 } else {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002617 REPORTER_ASSERT(reporter, SkPath::kCCW_Direction == dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002618 dir = SkPath::kCW_Direction;
2619 }
2620
2621 check_for_circle(reporter, tmp, true, dir);
2622}
2623
2624static void test_circle_mirror_y(skiatest::Reporter* reporter,
2625 const SkPath& path,
2626 SkPath::Direction dir) {
2627 SkPath tmp;
2628 SkMatrix m;
2629 m.reset();
2630 m.setScaleY(-SK_Scalar1);
2631 path.transform(m, &tmp);
2632
2633 if (SkPath::kCW_Direction == dir) {
2634 dir = SkPath::kCCW_Direction;
2635 } else {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002636 REPORTER_ASSERT(reporter, SkPath::kCCW_Direction == dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002637 dir = SkPath::kCW_Direction;
2638 }
2639
2640 check_for_circle(reporter, tmp, true, dir);
2641}
2642
2643static void test_circle_mirror_xy(skiatest::Reporter* reporter,
2644 const SkPath& path,
2645 SkPath::Direction dir) {
2646 SkPath tmp;
2647 SkMatrix m;
2648 m.reset();
2649 m.setScaleX(-SK_Scalar1);
2650 m.setScaleY(-SK_Scalar1);
2651 path.transform(m, &tmp);
2652
2653 check_for_circle(reporter, tmp, true, dir);
2654}
2655
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002656static void test_circle_with_direction(skiatest::Reporter* reporter,
2657 SkPath::Direction dir) {
2658 SkPath path;
2659
2660 // circle at origin
2661 path.addCircle(0, 0, SkIntToScalar(20), dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002662 check_for_circle(reporter, path, true, dir);
2663 test_circle_rotate(reporter, path, dir);
2664 test_circle_translate(reporter, path, dir);
2665 test_circle_skew(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002666
2667 // circle at an offset at (10, 10)
2668 path.reset();
2669 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
2670 SkIntToScalar(20), dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002671 check_for_circle(reporter, path, true, dir);
2672 test_circle_rotate(reporter, path, dir);
2673 test_circle_translate(reporter, path, dir);
2674 test_circle_skew(reporter, path, dir);
2675 test_circle_mirror_x(reporter, path, dir);
2676 test_circle_mirror_y(reporter, path, dir);
2677 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002678}
2679
2680static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
2681 SkPath path;
2682 SkPath circle;
2683 SkPath rect;
2684 SkPath empty;
2685
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002686 static const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
2687 static const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
2688
2689 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002690 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
2691 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
2692
2693 SkMatrix translate;
2694 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
2695
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00002696 // Although all the path concatenation related operations leave
2697 // the path a circle, most mark it as a non-circle for simplicity
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002698
2699 // empty + circle (translate)
2700 path = empty;
2701 path.addPath(circle, translate);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002702 check_for_circle(reporter, path, false, kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002703
2704 // circle + empty (translate)
2705 path = circle;
2706 path.addPath(empty, translate);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00002707 check_for_circle(reporter, path, true, kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002708
2709 // test reverseAddPath
2710 path = circle;
2711 path.reverseAddPath(rect);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002712 check_for_circle(reporter, path, false, kCircleDirOpposite);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002713}
2714
2715static void test_circle(skiatest::Reporter* reporter) {
2716 test_circle_with_direction(reporter, SkPath::kCW_Direction);
2717 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
2718
2719 // multiple addCircle()
2720 SkPath path;
2721 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2722 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002723 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002724
2725 // some extra lineTo() would make isOval() fail
2726 path.reset();
2727 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2728 path.lineTo(0, 0);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002729 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002730
2731 // not back to the original point
2732 path.reset();
2733 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2734 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002735 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002736
2737 test_circle_with_add_paths(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002738
2739 // test negative radius
2740 path.reset();
2741 path.addCircle(0, 0, -1, SkPath::kCW_Direction);
2742 REPORTER_ASSERT(reporter, path.isEmpty());
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002743}
2744
2745static void test_oval(skiatest::Reporter* reporter) {
2746 SkRect rect;
2747 SkMatrix m;
2748 SkPath path;
2749
2750 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
2751 path.addOval(rect);
2752
2753 REPORTER_ASSERT(reporter, path.isOval(NULL));
2754
2755 m.setRotate(SkIntToScalar(90));
2756 SkPath tmp;
2757 path.transform(m, &tmp);
2758 // an oval rotated 90 degrees is still an oval.
2759 REPORTER_ASSERT(reporter, tmp.isOval(NULL));
2760
2761 m.reset();
2762 m.setRotate(SkIntToScalar(30));
2763 tmp.reset();
2764 path.transform(m, &tmp);
2765 // an oval rotated 30 degrees is not an oval anymore.
2766 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2767
2768 // since empty path being transformed.
2769 path.reset();
2770 tmp.reset();
2771 m.reset();
2772 path.transform(m, &tmp);
2773 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2774
2775 // empty path is not an oval
2776 tmp.reset();
2777 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2778
2779 // only has moveTo()s
2780 tmp.reset();
2781 tmp.moveTo(0, 0);
2782 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
2783 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2784
2785 // mimic WebKit's calling convention,
2786 // call moveTo() first and then call addOval()
2787 path.reset();
2788 path.moveTo(0, 0);
2789 path.addOval(rect);
2790 REPORTER_ASSERT(reporter, path.isOval(NULL));
2791
2792 // copy path
2793 path.reset();
2794 tmp.reset();
2795 tmp.addOval(rect);
2796 path = tmp;
2797 REPORTER_ASSERT(reporter, path.isOval(NULL));
2798}
2799
bungeman@google.coma5809a32013-06-21 15:13:34 +00002800static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
2801 SkPath empty;
reed@android.com80e39a72009-04-02 16:59:40 +00002802
reed@android.com3abec1d2009-03-02 05:36:20 +00002803 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002804 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002805 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00002806 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00002807 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00002808 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
2809 REPORTER_ASSERT(reporter, !p.isInverseFillType());
bungeman@google.coma5809a32013-06-21 15:13:34 +00002810 REPORTER_ASSERT(reporter, p == empty);
2811 REPORTER_ASSERT(reporter, !(p != empty));
2812}
2813
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002814static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
2815 SkPath::Direction dir) {
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002816 REPORTER_ASSERT(reporter, path->isConvex());
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002817 REPORTER_ASSERT(reporter, path->cheapIsDirection(dir));
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002818 path->setConvexity(SkPath::kUnknown_Convexity);
2819 REPORTER_ASSERT(reporter, path->isConvex());
2820 path->reset();
2821}
2822
2823static void test_rrect(skiatest::Reporter* reporter) {
2824 SkPath p;
2825 SkRRect rr;
2826 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
2827 SkRect r = {10, 20, 30, 40};
2828 rr.setRectRadii(r, radii);
2829 p.addRRect(rr);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002830 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002831 p.addRRect(rr, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002832 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002833 p.addRoundRect(r, &radii[0].fX);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002834 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002835 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002836 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002837 p.addRoundRect(r, radii[1].fX, radii[1].fY);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002838 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002839 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002840 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
2841 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
2842 SkVector save = radii[i];
2843 radii[i].set(0, 0);
2844 rr.setRectRadii(r, radii);
2845 p.addRRect(rr);
2846 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2847 radii[i] = save;
2848 }
2849 p.addRoundRect(r, 0, 0);
2850 SkRect returnedRect;
2851 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
2852 REPORTER_ASSERT(reporter, returnedRect == r);
2853 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2854 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
2855 rr.setRectRadii(r, zeroRadii);
2856 p.addRRect(rr);
2857 bool closed;
2858 SkPath::Direction dir;
2859 REPORTER_ASSERT(reporter, p.isRect(&closed, &dir));
2860 REPORTER_ASSERT(reporter, closed);
2861 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
2862 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2863 p.addRRect(rr, SkPath::kCW_Direction);
2864 p.addRRect(rr, SkPath::kCW_Direction);
2865 REPORTER_ASSERT(reporter, !p.isConvex());
2866 p.reset();
2867 p.addRRect(rr, SkPath::kCCW_Direction);
2868 p.addRRect(rr, SkPath::kCCW_Direction);
2869 REPORTER_ASSERT(reporter, !p.isConvex());
2870 p.reset();
2871 SkRect emptyR = {10, 20, 10, 30};
2872 rr.setRectRadii(emptyR, radii);
2873 p.addRRect(rr);
2874 REPORTER_ASSERT(reporter, p.isEmpty());
2875 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
2876 rr.setRectRadii(largeR, radii);
2877 p.addRRect(rr);
2878 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2879 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
2880 rr.setRectRadii(infR, radii);
2881 p.addRRect(rr);
2882 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2883 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
2884 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
2885 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002886}
2887
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002888static void test_arc(skiatest::Reporter* reporter) {
2889 SkPath p;
2890 SkRect emptyOval = {10, 20, 30, 20};
2891 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
2892 p.addArc(emptyOval, 1, 2);
2893 REPORTER_ASSERT(reporter, p.isEmpty());
2894 p.reset();
2895 SkRect oval = {10, 20, 30, 40};
2896 p.addArc(oval, 1, 0);
2897 REPORTER_ASSERT(reporter, p.isEmpty());
2898 p.reset();
2899 SkPath cwOval;
2900 cwOval.addOval(oval);
2901 p.addArc(oval, 1, 360);
2902 REPORTER_ASSERT(reporter, p == cwOval);
2903 p.reset();
2904 SkPath ccwOval;
2905 ccwOval.addOval(oval, SkPath::kCCW_Direction);
2906 p.addArc(oval, 1, -360);
2907 REPORTER_ASSERT(reporter, p == ccwOval);
2908 p.reset();
2909 p.addArc(oval, 1, 180);
2910 REPORTER_ASSERT(reporter, p.isConvex());
2911 REPORTER_ASSERT(reporter, p.cheapIsDirection(SkPath::kCW_Direction));
2912 p.setConvexity(SkPath::kUnknown_Convexity);
2913 REPORTER_ASSERT(reporter, p.isConvex());
2914}
2915
2916static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
2917 SkScalar x0, SkScalar y0) {
2918 SkPoint pts[4];
2919 SkPath::Verb v = iter->next(pts);
2920 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
2921 REPORTER_ASSERT(reporter, pts[0].fX == x0);
2922 REPORTER_ASSERT(reporter, pts[0].fY == y0);
2923}
2924
2925static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
2926 SkScalar x1, SkScalar y1) {
2927 SkPoint pts[4];
2928 SkPath::Verb v = iter->next(pts);
2929 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
2930 REPORTER_ASSERT(reporter, pts[1].fX == x1);
2931 REPORTER_ASSERT(reporter, pts[1].fY == y1);
2932}
2933
2934static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
2935 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
2936 SkPoint pts[4];
2937 SkPath::Verb v = iter->next(pts);
2938 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
2939 REPORTER_ASSERT(reporter, pts[1].fX == x1);
2940 REPORTER_ASSERT(reporter, pts[1].fY == y1);
2941 REPORTER_ASSERT(reporter, pts[2].fX == x2);
2942 REPORTER_ASSERT(reporter, pts[2].fY == y2);
2943}
2944
2945static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
2946 SkPoint pts[4];
2947 SkPath::Verb v = iter->next(pts);
2948 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
2949}
2950
2951static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
2952 check_done(reporter, p, iter);
2953 p->reset();
2954}
2955
2956static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
2957 SkScalar x0, SkScalar y0) {
2958 SkPath::RawIter iter(*p);
2959 check_move(reporter, &iter, x0, y0);
2960 check_done_and_reset(reporter, p, &iter);
2961}
2962
2963static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
2964 SkScalar x1, SkScalar y1) {
2965 SkPath::RawIter iter(*p);
2966 check_move(reporter, &iter, 0, 0);
2967 check_line(reporter, &iter, x1, y1);
2968 check_done_and_reset(reporter, p, &iter);
2969}
2970
2971static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
2972 SkScalar x1, SkScalar y1) {
2973 SkPath::RawIter iter(*p);
2974 check_move(reporter, &iter, 0, 0);
2975 check_line(reporter, &iter, x1, y1);
2976 check_done(reporter, p, &iter);
2977}
2978
2979static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
2980 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
2981 SkPath::RawIter iter(*p);
2982 check_move(reporter, &iter, 0, 0);
2983 check_line(reporter, &iter, x1, y1);
2984 check_line(reporter, &iter, x2, y2);
2985 check_done_and_reset(reporter, p, &iter);
2986}
2987
2988static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
2989 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
2990 SkPath::RawIter iter(*p);
2991 check_move(reporter, &iter, 0, 0);
2992 check_quad(reporter, &iter, x1, y1, x2, y2);
2993 check_done_and_reset(reporter, p, &iter);
2994}
2995
2996static void test_arcTo(skiatest::Reporter* reporter) {
2997 SkPath p;
2998 p.arcTo(0, 0, 1, 2, 1);
2999 check_path_is_line_and_reset(reporter, &p, 0, 0);
3000 p.arcTo(1, 2, 1, 2, 1);
3001 check_path_is_line_and_reset(reporter, &p, 1, 2);
3002 p.arcTo(1, 2, 3, 4, 0);
3003 check_path_is_line_and_reset(reporter, &p, 1, 2);
3004 p.arcTo(1, 2, 0, 0, 1);
3005 check_path_is_line_and_reset(reporter, &p, 1, 2);
3006 p.arcTo(1, 0, 1, 1, 1);
3007 SkPoint pt;
3008 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3009 p.reset();
3010 p.arcTo(1, 0, 1, -1, 1);
3011 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3012 p.reset();
3013 SkRect oval = {1, 2, 3, 4};
3014 p.arcTo(oval, 0, 0, true);
3015 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3016 p.arcTo(oval, 0, 0, false);
3017 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3018 p.arcTo(oval, 360, 0, true);
3019 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3020 p.arcTo(oval, 360, 0, false);
3021 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3022 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003023 p.arcTo(oval, 0, sweep, false);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003024 REPORTER_ASSERT(reporter, p.getBounds() == oval);
3025 sweep += delta;
3026 delta /= 2;
3027 }
3028 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003029 p.arcTo(oval, 0, sweep, false);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003030 REPORTER_ASSERT(reporter, p.getBounds() == oval);
3031 sweep -= delta;
3032 delta /= 2;
3033 }
3034 SkRect noOvalWidth = {1, 2, 0, 3};
3035 p.reset();
3036 p.arcTo(noOvalWidth, 0, 360, false);
3037 REPORTER_ASSERT(reporter, p.isEmpty());
3038
3039 SkRect noOvalHeight = {1, 2, 3, 1};
3040 p.reset();
3041 p.arcTo(noOvalHeight, 0, 360, false);
3042 REPORTER_ASSERT(reporter, p.isEmpty());
3043}
3044
3045static void test_addPath(skiatest::Reporter* reporter) {
3046 SkPath p, q;
3047 p.lineTo(1, 2);
3048 q.moveTo(4, 4);
3049 q.lineTo(7, 8);
3050 q.conicTo(8, 7, 6, 5, 0.5f);
3051 q.quadTo(6, 7, 8, 6);
3052 q.cubicTo(5, 6, 7, 8, 7, 5);
3053 q.close();
3054 p.addPath(q, -4, -4);
3055 SkRect expected = {0, 0, 4, 4};
3056 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3057 p.reset();
3058 p.reverseAddPath(q);
3059 SkRect reverseExpected = {4, 4, 8, 8};
3060 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3061}
3062
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003063static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3064 SkPath p, q;
3065 if (explicitMoveTo) {
3066 p.moveTo(1, 1);
3067 }
3068 p.lineTo(1, 2);
3069 if (explicitMoveTo) {
3070 q.moveTo(2, 1);
3071 }
3072 q.lineTo(2, 2);
3073 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3074 uint8_t verbs[4];
3075 int verbcount = p.getVerbs(verbs, 4);
3076 REPORTER_ASSERT(reporter, verbcount == 4);
3077 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3078 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3079 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3080 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3081}
3082
3083static void test_extendClosedPath(skiatest::Reporter* reporter) {
3084 SkPath p, q;
3085 p.moveTo(1, 1);
3086 p.lineTo(1, 2);
3087 p.lineTo(2, 2);
3088 p.close();
3089 q.moveTo(2, 1);
3090 q.lineTo(2, 3);
3091 p.addPath(q, SkPath::kExtend_AddPathMode);
3092 uint8_t verbs[7];
3093 int verbcount = p.getVerbs(verbs, 7);
3094 REPORTER_ASSERT(reporter, verbcount == 7);
3095 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3096 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3097 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3098 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3099 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3100 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3101 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3102
3103 SkPoint pt;
3104 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3105 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3106 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3107}
3108
3109static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3110 SkPath p, q, r;
3111 // case 1: dst is empty
3112 p.moveTo(2, 1);
3113 p.lineTo(2, 3);
3114 q.addPath(p, mode);
3115 REPORTER_ASSERT(reporter, q == p);
3116 // case 2: src is empty
3117 p.addPath(r, mode);
3118 REPORTER_ASSERT(reporter, q == p);
3119 // case 3: src and dst are empty
3120 q.reset();
3121 q.addPath(r, mode);
3122 REPORTER_ASSERT(reporter, q.isEmpty());
skia.committer@gmail.com877c4492014-02-12 03:02:04 +00003123}
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003124
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003125static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3126 SkPath p;
3127 p.conicTo(1, 2, 3, 4, -1);
3128 check_path_is_line_and_reset(reporter, &p, 3, 4);
3129 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3130 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
3131 p.conicTo(1, 2, 3, 4, 1);
3132 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
3133}
3134
3135static void test_get_point(skiatest::Reporter* reporter) {
3136 SkPath p;
3137 SkPoint pt = p.getPoint(0);
3138 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
3139 REPORTER_ASSERT(reporter, !p.getLastPt(NULL));
3140 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
3141 p.setLastPt(10, 10);
3142 pt = p.getPoint(0);
3143 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
3144 REPORTER_ASSERT(reporter, p.getLastPt(NULL));
3145 p.rMoveTo(10, 10);
3146 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
3147}
3148
3149static void test_contains(skiatest::Reporter* reporter) {
3150 SkPath p;
3151 p.setFillType(SkPath::kInverseWinding_FillType);
3152 REPORTER_ASSERT(reporter, p.contains(0, 0));
3153 p.setFillType(SkPath::kWinding_FillType);
3154 REPORTER_ASSERT(reporter, !p.contains(0, 0));
3155 p.moveTo(4, 4);
3156 p.lineTo(6, 8);
3157 p.lineTo(8, 4);
3158 // test quick reject
3159 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3160 REPORTER_ASSERT(reporter, !p.contains(0, 4));
3161 REPORTER_ASSERT(reporter, !p.contains(4, 10));
3162 REPORTER_ASSERT(reporter, !p.contains(10, 4));
3163 // test various crossings in x
3164 REPORTER_ASSERT(reporter, !p.contains(5, 7));
3165 REPORTER_ASSERT(reporter, p.contains(6, 7));
3166 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3167 p.reset();
3168 p.moveTo(4, 4);
3169 p.lineTo(8, 6);
3170 p.lineTo(4, 8);
3171 // test various crossings in y
3172 REPORTER_ASSERT(reporter, !p.contains(7, 5));
3173 REPORTER_ASSERT(reporter, p.contains(7, 6));
3174 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3175 // test quads
3176 p.reset();
3177 p.moveTo(4, 4);
3178 p.quadTo(6, 6, 8, 8);
3179 p.quadTo(6, 8, 4, 8);
3180 p.quadTo(4, 6, 4, 4);
3181 REPORTER_ASSERT(reporter, p.contains(5, 6));
3182 REPORTER_ASSERT(reporter, !p.contains(6, 5));
3183
3184 p.reset();
3185 p.moveTo(6, 6);
3186 p.quadTo(8, 8, 6, 8);
3187 p.quadTo(4, 8, 4, 6);
3188 p.quadTo(4, 4, 6, 6);
3189 REPORTER_ASSERT(reporter, p.contains(5, 6));
3190 REPORTER_ASSERT(reporter, !p.contains(6, 5));
3191
3192#define CONIC_CONTAINS_BUG_FIXED 0
3193#if CONIC_CONTAINS_BUG_FIXED
3194 p.reset();
3195 p.moveTo(4, 4);
3196 p.conicTo(6, 6, 8, 8, 0.5f);
3197 p.conicTo(6, 8, 4, 8, 0.5f);
3198 p.conicTo(4, 6, 4, 4, 0.5f);
3199 REPORTER_ASSERT(reporter, p.contains(5, 6));
3200 REPORTER_ASSERT(reporter, !p.contains(6, 5));
3201#endif
3202
3203 // test cubics
3204 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
3205 for (int i = 0; i < 3; ++i) {
3206 p.reset();
3207 p.setFillType(SkPath::kEvenOdd_FillType);
3208 p.moveTo(pts[i].fX, pts[i].fY);
3209 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);
3210 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);
3211 p.close();
3212 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
3213 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
3214 }
3215}
3216
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003217class PathRefTest_Private {
3218public:
3219 static void TestPathRef(skiatest::Reporter* reporter) {
3220 static const int kRepeatCnt = 10;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003221
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003222 SkAutoTUnref<SkPathRef> pathRef(SkNEW(SkPathRef));
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003223
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003224 SkPathRef::Editor ed(&pathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003225
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003226 {
3227 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
3228 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3229 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
3230 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
3231 for (int i = 0; i < kRepeatCnt; ++i) {
3232 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
3233 }
3234 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003235 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003236
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003237 {
3238 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
3239 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3240 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
3241 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
3242 for (int i = 0; i < kRepeatCnt; ++i) {
3243 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
3244 }
3245 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003246 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003247
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003248 {
3249 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
3250 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3251 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
3252 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
3253 for (int i = 0; i < kRepeatCnt; ++i) {
3254 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
3255 }
3256 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003257 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003258
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003259 {
3260 SkScalar* weights = NULL;
3261 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
3262 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3263 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
3264 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
3265 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
3266 REPORTER_ASSERT(reporter, NULL != weights);
3267 for (int i = 0; i < kRepeatCnt; ++i) {
3268 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
3269 }
3270 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003271 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003272
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003273 {
3274 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
3275 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3276 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
3277 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
3278 for (int i = 0; i < kRepeatCnt; ++i) {
3279 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
3280 }
3281 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003282 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003283 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003284};
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003285
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003286static void test_operatorEqual(skiatest::Reporter* reporter) {
3287 SkPath a;
3288 SkPath b;
3289 REPORTER_ASSERT(reporter, a == a);
3290 REPORTER_ASSERT(reporter, a == b);
3291 a.setFillType(SkPath::kInverseWinding_FillType);
3292 REPORTER_ASSERT(reporter, a != b);
3293 a.reset();
3294 REPORTER_ASSERT(reporter, a == b);
3295 a.lineTo(1, 1);
3296 REPORTER_ASSERT(reporter, a != b);
3297 a.reset();
3298 REPORTER_ASSERT(reporter, a == b);
3299 a.lineTo(1, 1);
3300 b.lineTo(1, 2);
3301 REPORTER_ASSERT(reporter, a != b);
3302 a.reset();
3303 a.lineTo(1, 2);
3304 REPORTER_ASSERT(reporter, a == b);
3305}
3306
3307class PathTest_Private {
3308public:
3309 static void TestPathTo(skiatest::Reporter* reporter) {
3310 SkPath p, q;
3311 p.lineTo(4, 4);
3312 p.reversePathTo(q);
3313 check_path_is_line(reporter, &p, 4, 4);
3314 q.moveTo(-4, -4);
3315 p.reversePathTo(q);
3316 check_path_is_line(reporter, &p, 4, 4);
3317 q.lineTo(7, 8);
3318 q.conicTo(8, 7, 6, 5, 0.5f);
3319 q.quadTo(6, 7, 8, 6);
3320 q.cubicTo(5, 6, 7, 8, 7, 5);
3321 q.close();
3322 p.reversePathTo(q);
3323 SkRect reverseExpected = {-4, -4, 8, 8};
3324 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3325 }
3326};
3327
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00003328DEF_TEST(Paths, reporter) {
bungeman@google.coma5809a32013-06-21 15:13:34 +00003329 SkTSize<SkScalar>::Make(3,4);
3330
3331 SkPath p, empty;
3332 SkRect bounds, bounds2;
3333 test_empty(reporter, p);
reed@android.com3abec1d2009-03-02 05:36:20 +00003334
reed@android.comd252db02009-04-01 18:31:44 +00003335 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00003336
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003337 // this triggers a code path in SkPath::operator= which is otherwise unexercised
3338 SkPath& self = p;
3339 p = self;
3340
3341 // this triggers a code path in SkPath::swap which is otherwise unexercised
3342 p.swap(self);
3343
reed@android.com3abec1d2009-03-02 05:36:20 +00003344 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00003345
reed@android.com6b82d1a2009-06-03 02:35:01 +00003346 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
3347 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00003348 // we have quads or cubics
3349 REPORTER_ASSERT(reporter, p.getSegmentMasks() & kCurveSegmentMask);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003350 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00003351
reed@android.com6b82d1a2009-06-03 02:35:01 +00003352 p.reset();
bungeman@google.coma5809a32013-06-21 15:13:34 +00003353 test_empty(reporter, p);
reed@google.com10296cc2011-09-21 12:29:05 +00003354
reed@android.com6b82d1a2009-06-03 02:35:01 +00003355 p.addOval(bounds);
3356 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003357 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00003358
bungeman@google.coma5809a32013-06-21 15:13:34 +00003359 p.rewind();
3360 test_empty(reporter, p);
3361
reed@android.com3abec1d2009-03-02 05:36:20 +00003362 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00003363 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00003364 // we have only lines
3365 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003366 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00003367
bungeman@google.coma5809a32013-06-21 15:13:34 +00003368 REPORTER_ASSERT(reporter, p != empty);
3369 REPORTER_ASSERT(reporter, !(p == empty));
reed@android.com3abec1d2009-03-02 05:36:20 +00003370
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003371 // do getPoints and getVerbs return the right result
3372 REPORTER_ASSERT(reporter, p.getPoints(NULL, 0) == 4);
3373 REPORTER_ASSERT(reporter, p.getVerbs(NULL, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00003374 SkPoint pts[4];
3375 int count = p.getPoints(pts, 4);
3376 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003377 uint8_t verbs[6];
3378 verbs[5] = 0xff;
3379 p.getVerbs(verbs, 5);
3380 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
3381 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
3382 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
3383 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
3384 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
3385 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00003386 bounds2.set(pts, 4);
3387 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00003388
reed@android.com3abec1d2009-03-02 05:36:20 +00003389 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
3390 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00003391 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00003392
reed@android.com3abec1d2009-03-02 05:36:20 +00003393 REPORTER_ASSERT(reporter, p.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00003394 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00003395 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
3396 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00003397
reed@android.com3abec1d2009-03-02 05:36:20 +00003398 // now force p to not be a rect
3399 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
3400 p.addRect(bounds);
3401 REPORTER_ASSERT(reporter, !p.isRect(NULL));
reed@android.com3abec1d2009-03-02 05:36:20 +00003402
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003403 test_operatorEqual(reporter);
reed@google.com7e6c4d12012-05-10 14:05:43 +00003404 test_isLine(reporter);
3405 test_isRect(reporter);
caryclark@google.com56f233a2012-11-19 13:06:06 +00003406 test_isNestedRects(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003407 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00003408 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00003409 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00003410 test_convexity2(reporter);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00003411 test_conservativelyContains(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00003412 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003413 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00003414 test_flattening(reporter);
3415 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00003416 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003417 test_iter(reporter);
3418 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003419 test_circle(reporter);
3420 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00003421 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00003422 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00003423 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00003424 test_isfinite_after_transform(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00003425 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00003426 test_arb_zero_rad_round_rect_is_rect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003427 test_addrect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00003428 test_addrect_isfinite(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00003429 test_tricky_cubic();
3430 test_clipped_cubic();
3431 test_crbug_170666();
reed@google.com7a90daf2013-04-10 18:44:00 +00003432 test_bad_cubic_crbug229478();
reed@google.com3eff3592013-05-08 21:08:21 +00003433 test_bad_cubic_crbug234190();
mtklein@google.com9c9d4a72013-08-07 19:17:53 +00003434 test_android_specific_behavior(reporter);
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +00003435 test_gen_id(reporter);
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +00003436 test_path_close_issue1474(reporter);
reed@google.comb58ba892013-10-15 15:44:35 +00003437 test_path_to_region(reporter);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003438 test_rrect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003439 test_arc(reporter);
3440 test_arcTo(reporter);
3441 test_addPath(reporter);
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003442 test_addPathMode(reporter, false, false);
3443 test_addPathMode(reporter, true, false);
3444 test_addPathMode(reporter, false, true);
3445 test_addPathMode(reporter, true, true);
3446 test_extendClosedPath(reporter);
3447 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
3448 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003449 test_conicTo_special_case(reporter);
3450 test_get_point(reporter);
3451 test_contains(reporter);
3452 PathTest_Private::TestPathTo(reporter);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003453 PathRefTest_Private::TestPathRef(reporter);
reed@android.com3abec1d2009-03-02 05:36:20 +00003454}