blob: 06ecff6281956be37b4b39d1aeba1c0ad4b3f36f [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@android.com3abec1d2009-03-02 05:36:20 +00008#include "Test.h"
reed@google.com8cae8352012-09-14 15:18:41 +00009#include "SkCanvas.h"
reed@google.com55b5f4b2011-09-07 12:23:41 +000010#include "SkPaint.h"
reed@android.com3abec1d2009-03-02 05:36:20 +000011#include "SkPath.h"
reed@google.com04863fa2011-05-15 04:08:24 +000012#include "SkParse.h"
reed@google.com3e71a882012-01-10 18:44:37 +000013#include "SkParsePath.h"
reed@google.com8b06f1a2012-05-29 12:03:46 +000014#include "SkPathEffect.h"
schenney@chromium.org6630d8d2012-01-04 21:05:51 +000015#include "SkRandom.h"
reed@google.com53effc52011-09-21 19:05:12 +000016#include "SkReader32.h"
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +000017#include "SkRRect.h"
reed@android.com60bc6d52010-02-11 11:09:39 +000018#include "SkSize.h"
reed@google.com8cae8352012-09-14 15:18:41 +000019#include "SkSurface.h"
mtklein@google.com9c9d4a72013-08-07 19:17:53 +000020#include "SkTypes.h"
21#include "SkWriter32.h"
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
bsalomon@google.com6843ac42012-02-17 13:49:03 +0000832#ifdef SK_SCALAR_IS_FLOAT
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000833 // triangle with one point really far from the origin.
834 path.reset();
835 // the first point is roughly 1.05e10, 1.05e10
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000836 path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
bsalomon@google.com53aab782012-02-23 14:54:49 +0000837 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
838 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000839 check_direction(reporter, path, SkPath::kCCW_Direction);
bsalomon@google.com53aab782012-02-23 14:54:49 +0000840#endif
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000841
842 path.reset();
843 path.conicTo(20, 0, 20, 20, 0.5f);
844 path.close();
845 check_direction(reporter, path, SkPath::kCW_Direction);
846
847 path.reset();
848 path.lineTo(1, 1e7f);
849 path.lineTo(1e7f, 2e7f);
850 path.close();
851 REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity());
852 check_direction(reporter, path, SkPath::kCCW_Direction);
reed@google.com3e71a882012-01-10 18:44:37 +0000853}
854
reed@google.comffdb0182011-11-14 19:29:14 +0000855static void add_rect(SkPath* path, const SkRect& r) {
856 path->moveTo(r.fLeft, r.fTop);
857 path->lineTo(r.fRight, r.fTop);
858 path->lineTo(r.fRight, r.fBottom);
859 path->lineTo(r.fLeft, r.fBottom);
860 path->close();
861}
862
863static void test_bounds(skiatest::Reporter* reporter) {
864 static const SkRect rects[] = {
reed@google.com3563c9e2011-11-14 19:34:57 +0000865 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
866 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
867 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
868 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
reed@google.comffdb0182011-11-14 19:29:14 +0000869 };
870
871 SkPath path0, path1;
872 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
873 path0.addRect(rects[i]);
874 add_rect(&path1, rects[i]);
875 }
876
877 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
878}
879
reed@google.com55b5f4b2011-09-07 12:23:41 +0000880static void stroke_cubic(const SkPoint pts[4]) {
881 SkPath path;
882 path.moveTo(pts[0]);
883 path.cubicTo(pts[1], pts[2], pts[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000884
reed@google.com55b5f4b2011-09-07 12:23:41 +0000885 SkPaint paint;
886 paint.setStyle(SkPaint::kStroke_Style);
887 paint.setStrokeWidth(SK_Scalar1 * 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000888
reed@google.com55b5f4b2011-09-07 12:23:41 +0000889 SkPath fill;
890 paint.getFillPath(path, &fill);
891}
892
893// just ensure this can run w/o any SkASSERTS firing in the debug build
894// we used to assert due to differences in how we determine a degenerate vector
895// but that was fixed with the introduction of SkPoint::CanNormalize
896static void stroke_tiny_cubic() {
897 SkPoint p0[] = {
898 { 372.0f, 92.0f },
899 { 372.0f, 92.0f },
900 { 372.0f, 92.0f },
901 { 372.0f, 92.0f },
902 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000903
reed@google.com55b5f4b2011-09-07 12:23:41 +0000904 stroke_cubic(p0);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000905
reed@google.com55b5f4b2011-09-07 12:23:41 +0000906 SkPoint p1[] = {
907 { 372.0f, 92.0f },
908 { 372.0007f, 92.000755f },
909 { 371.99927f, 92.003922f },
910 { 371.99826f, 92.003899f },
911 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000912
reed@google.com55b5f4b2011-09-07 12:23:41 +0000913 stroke_cubic(p1);
914}
915
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000916static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
917 for (int i = 0; i < 2; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +0000918 SkPath::Iter iter(path, SkToBool(i));
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000919 SkPoint mv;
920 SkPoint pts[4];
921 SkPath::Verb v;
922 int nMT = 0;
923 int nCL = 0;
tomhudson@google.com221db3c2011-07-28 21:10:29 +0000924 mv.set(0, 0);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000925 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
926 switch (v) {
927 case SkPath::kMove_Verb:
928 mv = pts[0];
929 ++nMT;
930 break;
931 case SkPath::kClose_Verb:
932 REPORTER_ASSERT(reporter, mv == pts[0]);
933 ++nCL;
934 break;
935 default:
936 break;
937 }
938 }
939 // if we force a close on the interator we should have a close
940 // for every moveTo
941 REPORTER_ASSERT(reporter, !i || nMT == nCL);
942 }
943}
944
945static void test_close(skiatest::Reporter* reporter) {
946 SkPath closePt;
947 closePt.moveTo(0, 0);
948 closePt.close();
949 check_close(reporter, closePt);
950
951 SkPath openPt;
952 openPt.moveTo(0, 0);
953 check_close(reporter, openPt);
954
955 SkPath empty;
956 check_close(reporter, empty);
957 empty.close();
958 check_close(reporter, empty);
959
960 SkPath rect;
961 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
962 check_close(reporter, rect);
963 rect.close();
964 check_close(reporter, rect);
965
966 SkPath quad;
967 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
968 check_close(reporter, quad);
969 quad.close();
970 check_close(reporter, quad);
971
972 SkPath cubic;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000973 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000974 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
975 check_close(reporter, cubic);
976 cubic.close();
977 check_close(reporter, cubic);
978
979 SkPath line;
980 line.moveTo(SK_Scalar1, SK_Scalar1);
981 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
982 check_close(reporter, line);
983 line.close();
984 check_close(reporter, line);
985
986 SkPath rect2;
987 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
988 rect2.close();
989 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
990 check_close(reporter, rect2);
991 rect2.close();
992 check_close(reporter, rect2);
993
994 SkPath oval3;
995 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
996 oval3.close();
997 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
998 check_close(reporter, oval3);
999 oval3.close();
1000 check_close(reporter, oval3);
1001
1002 SkPath moves;
1003 moves.moveTo(SK_Scalar1, SK_Scalar1);
1004 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
1005 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
1006 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
1007 check_close(reporter, moves);
reed@google.com55b5f4b2011-09-07 12:23:41 +00001008
1009 stroke_tiny_cubic();
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001010}
1011
reed@google.com7c424812011-05-15 04:38:34 +00001012static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
1013 SkPath::Convexity expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001014 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1015 SkPath::Convexity c = copy.getConvexity();
reed@google.com7c424812011-05-15 04:38:34 +00001016 REPORTER_ASSERT(reporter, c == expected);
1017}
1018
1019static void test_convexity2(skiatest::Reporter* reporter) {
1020 SkPath pt;
1021 pt.moveTo(0, 0);
1022 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +00001023 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001024 check_direction(reporter, pt, SkPath::kUnknown_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001025
reed@google.com7c424812011-05-15 04:38:34 +00001026 SkPath line;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001027 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
1028 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001029 line.close();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001030 check_convexity(reporter, line, SkPath::kConvex_Convexity);
1031 check_direction(reporter, line, SkPath::kUnknown_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001032
reed@google.com7c424812011-05-15 04:38:34 +00001033 SkPath triLeft;
1034 triLeft.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001035 triLeft.lineTo(SK_Scalar1, 0);
1036 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001037 triLeft.close();
1038 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001039 check_direction(reporter, triLeft, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001040
reed@google.com7c424812011-05-15 04:38:34 +00001041 SkPath triRight;
1042 triRight.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001043 triRight.lineTo(-SK_Scalar1, 0);
1044 triRight.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001045 triRight.close();
1046 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001047 check_direction(reporter, triRight, SkPath::kCCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001048
reed@google.com7c424812011-05-15 04:38:34 +00001049 SkPath square;
1050 square.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001051 square.lineTo(SK_Scalar1, 0);
1052 square.lineTo(SK_Scalar1, SK_Scalar1);
1053 square.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001054 square.close();
1055 check_convexity(reporter, square, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001056 check_direction(reporter, square, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001057
reed@google.com7c424812011-05-15 04:38:34 +00001058 SkPath redundantSquare;
1059 redundantSquare.moveTo(0, 0);
1060 redundantSquare.lineTo(0, 0);
1061 redundantSquare.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001062 redundantSquare.lineTo(SK_Scalar1, 0);
1063 redundantSquare.lineTo(SK_Scalar1, 0);
1064 redundantSquare.lineTo(SK_Scalar1, 0);
1065 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1066 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1067 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1068 redundantSquare.lineTo(0, SK_Scalar1);
1069 redundantSquare.lineTo(0, SK_Scalar1);
1070 redundantSquare.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001071 redundantSquare.close();
1072 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001073 check_direction(reporter, redundantSquare, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001074
reed@google.com7c424812011-05-15 04:38:34 +00001075 SkPath bowTie;
1076 bowTie.moveTo(0, 0);
1077 bowTie.lineTo(0, 0);
1078 bowTie.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001079 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1080 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1081 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1082 bowTie.lineTo(SK_Scalar1, 0);
1083 bowTie.lineTo(SK_Scalar1, 0);
1084 bowTie.lineTo(SK_Scalar1, 0);
1085 bowTie.lineTo(0, SK_Scalar1);
1086 bowTie.lineTo(0, SK_Scalar1);
1087 bowTie.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001088 bowTie.close();
1089 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001090 check_direction(reporter, bowTie, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001091
reed@google.com7c424812011-05-15 04:38:34 +00001092 SkPath spiral;
1093 spiral.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001094 spiral.lineTo(100*SK_Scalar1, 0);
1095 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1096 spiral.lineTo(0, 100*SK_Scalar1);
1097 spiral.lineTo(0, 50*SK_Scalar1);
1098 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1099 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001100 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +00001101 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001102 check_direction(reporter, spiral, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001103
reed@google.com7c424812011-05-15 04:38:34 +00001104 SkPath dent;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001105 dent.moveTo(0, 0);
1106 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1107 dent.lineTo(0, 100*SK_Scalar1);
1108 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1109 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001110 dent.close();
1111 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001112 check_direction(reporter, dent, SkPath::kCW_Direction);
reed@google.com7c424812011-05-15 04:38:34 +00001113}
1114
reed@android.com6b82d1a2009-06-03 02:35:01 +00001115static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1116 const SkRect& bounds) {
1117 REPORTER_ASSERT(reporter, p.isConvex());
1118 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +00001119
reed@android.com6b82d1a2009-06-03 02:35:01 +00001120 SkPath p2(p);
1121 REPORTER_ASSERT(reporter, p2.isConvex());
1122 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1123
1124 SkPath other;
1125 other.swap(p2);
1126 REPORTER_ASSERT(reporter, other.isConvex());
1127 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1128}
1129
reed@google.com04863fa2011-05-15 04:08:24 +00001130static void setFromString(SkPath* path, const char str[]) {
1131 bool first = true;
1132 while (str) {
1133 SkScalar x, y;
1134 str = SkParse::FindScalar(str, &x);
1135 if (NULL == str) {
1136 break;
1137 }
1138 str = SkParse::FindScalar(str, &y);
1139 SkASSERT(str);
1140 if (first) {
1141 path->moveTo(x, y);
1142 first = false;
1143 } else {
1144 path->lineTo(x, y);
1145 }
1146 }
1147}
1148
1149static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +00001150 SkPath path;
1151
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));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001154 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001155 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001156 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1157
reed@google.com04863fa2011-05-15 04:08:24 +00001158 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001159 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001160 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.com3e71a882012-01-10 18:44:37 +00001161 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCCW_Direction));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001162
reed@google.com04863fa2011-05-15 04:08:24 +00001163 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001164 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001165 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.com3e71a882012-01-10 18:44:37 +00001166 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCW_Direction));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001167
reed@google.com04863fa2011-05-15 04:08:24 +00001168 static const struct {
1169 const char* fPathStr;
1170 SkPath::Convexity fExpectedConvexity;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001171 SkPath::Direction fExpectedDirection;
reed@google.com04863fa2011-05-15 04:08:24 +00001172 } gRec[] = {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001173 { "", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1174 { "0 0", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1175 { "0 0 10 10", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1176 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPath::kUnknown_Direction },
1177 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPath::kCW_Direction },
1178 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPath::kCCW_Direction },
1179 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
1180 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPath::kCW_Direction },
reed@google.com04863fa2011-05-15 04:08:24 +00001181 };
1182
1183 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1184 SkPath path;
1185 setFromString(&path, gRec[i].fPathStr);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001186 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1187 check_direction(reporter, path, gRec[i].fExpectedDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001188 // check after setting the initial convex and direction
1189 if (kDontCheckDir != gRec[i].fExpectedDirection) {
1190 SkPath copy(path);
1191 SkPath::Direction dir;
1192 bool foundDir = copy.cheapComputeDirection(&dir);
1193 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPath::kUnknown_Direction)
1194 ^ foundDir);
1195 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1196 check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
1197 }
1198 REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
1199 check_direction(reporter, path, gRec[i].fExpectedDirection);
reed@google.com04863fa2011-05-15 04:08:24 +00001200 }
1201}
1202
reed@google.com7e6c4d12012-05-10 14:05:43 +00001203static void test_isLine(skiatest::Reporter* reporter) {
1204 SkPath path;
1205 SkPoint pts[2];
1206 const SkScalar value = SkIntToScalar(5);
1207
1208 REPORTER_ASSERT(reporter, !path.isLine(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001209
reed@google.com7e6c4d12012-05-10 14:05:43 +00001210 // set some non-zero values
1211 pts[0].set(value, value);
1212 pts[1].set(value, value);
1213 REPORTER_ASSERT(reporter, !path.isLine(pts));
1214 // check that pts was untouched
1215 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1216 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1217
1218 const SkScalar moveX = SkIntToScalar(1);
1219 const SkScalar moveY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001220 REPORTER_ASSERT(reporter, value != moveX && value != moveY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001221
1222 path.moveTo(moveX, moveY);
1223 REPORTER_ASSERT(reporter, !path.isLine(NULL));
1224 REPORTER_ASSERT(reporter, !path.isLine(pts));
1225 // check that pts was untouched
1226 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1227 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1228
1229 const SkScalar lineX = SkIntToScalar(2);
1230 const SkScalar lineY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001231 REPORTER_ASSERT(reporter, value != lineX && value != lineY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001232
1233 path.lineTo(lineX, lineY);
1234 REPORTER_ASSERT(reporter, path.isLine(NULL));
1235
1236 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1237 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1238 REPORTER_ASSERT(reporter, path.isLine(pts));
1239 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1240 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1241
1242 path.lineTo(0, 0); // too many points/verbs
1243 REPORTER_ASSERT(reporter, !path.isLine(NULL));
1244 REPORTER_ASSERT(reporter, !path.isLine(pts));
1245 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1246 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001247
1248 path.reset();
1249 path.quadTo(1, 1, 2, 2);
1250 REPORTER_ASSERT(reporter, !path.isLine(NULL));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001251}
1252
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001253static void test_conservativelyContains(skiatest::Reporter* reporter) {
1254 SkPath path;
1255
1256 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1257 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1258
1259 // A circle that bounds kBaseRect (with a significant amount of slop)
1260 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001261 circleR = SkScalarMul(circleR, 1.75f) / 2;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001262 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1263
1264 // round-rect radii
1265 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
skia.committer@gmail.comcec8de62012-11-14 02:01:22 +00001266
caryclark@google.com56f233a2012-11-19 13:06:06 +00001267 static const struct SUPPRESS_VISIBILITY_WARNING {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001268 SkRect fQueryRect;
1269 bool fInRect;
1270 bool fInCircle;
1271 bool fInRR;
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001272 bool fInCubicRR;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001273 } kQueries[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001274 {kBaseRect, true, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001275
1276 // rect well inside of kBaseRect
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001277 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1278 kBaseRect.fTop + 0.25f*kBaseRect.height(),
1279 kBaseRect.fRight - 0.25f*kBaseRect.width(),
1280 kBaseRect.fBottom - 0.25f*kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001281 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001282
1283 // rects with edges off by one from kBaseRect's edges
1284 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1285 kBaseRect.width(), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001286 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001287 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1288 kBaseRect.width() + 1, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001289 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001290 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1291 kBaseRect.width() + 1, kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001292 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001293 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1294 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001295 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001296 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1297 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001298 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001299 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1300 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001301 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001302 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1303 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001304 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001305
1306 // zero-w/h rects at each corner of kBaseRect
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001307 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1308 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1309 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1310 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001311
1312 // far away rect
1313 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1314 SkIntToScalar(10), SkIntToScalar(10)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001315 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001316
1317 // very large rect containing kBaseRect
1318 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1319 kBaseRect.fTop - 5 * kBaseRect.height(),
1320 11 * kBaseRect.width(), 11 * kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001321 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001322
1323 // skinny rect that spans same y-range as kBaseRect
1324 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1325 SkIntToScalar(1), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001326 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001327
1328 // short rect that spans same x-range as kBaseRect
1329 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001330 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001331
1332 // skinny rect that spans slightly larger y-range than kBaseRect
1333 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1334 SkIntToScalar(1), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001335 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001336
1337 // short rect that spans slightly larger x-range than kBaseRect
1338 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1339 kBaseRect.width() + 1, SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001340 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001341 };
1342
1343 for (int inv = 0; inv < 4; ++inv) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001344 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001345 SkRect qRect = kQueries[q].fQueryRect;
1346 if (inv & 0x1) {
1347 SkTSwap(qRect.fLeft, qRect.fRight);
1348 }
1349 if (inv & 0x2) {
1350 SkTSwap(qRect.fTop, qRect.fBottom);
1351 }
1352 for (int d = 0; d < 2; ++d) {
1353 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1354 path.reset();
1355 path.addRect(kBaseRect, dir);
1356 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1357 path.conservativelyContainsRect(qRect));
1358
1359 path.reset();
1360 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1361 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1362 path.conservativelyContainsRect(qRect));
1363
1364 path.reset();
1365 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1366 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1367 path.conservativelyContainsRect(qRect));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001368
1369 path.reset();
1370 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1371 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1372 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1373 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1374 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1375 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1376 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1377 path.close();
1378 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1379 path.conservativelyContainsRect(qRect));
1380
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001381 }
1382 // Slightly non-convex shape, shouldn't contain any rects.
1383 path.reset();
1384 path.moveTo(0, 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001385 path.lineTo(SkIntToScalar(50), 0.05f);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001386 path.lineTo(SkIntToScalar(100), 0);
1387 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1388 path.lineTo(0, SkIntToScalar(100));
1389 path.close();
1390 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1391 }
1392 }
1393
1394 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1395 path.reset();
1396 path.moveTo(0, 0);
1397 path.lineTo(SkIntToScalar(100), 0);
1398 path.lineTo(0, SkIntToScalar(100));
1399
1400 // inside, on along top edge
1401 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1402 SkIntToScalar(10),
1403 SkIntToScalar(10))));
1404 // above
1405 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1406 SkRect::MakeXYWH(SkIntToScalar(50),
1407 SkIntToScalar(-10),
1408 SkIntToScalar(10),
1409 SkIntToScalar(10))));
1410 // to the left
1411 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1412 SkIntToScalar(5),
1413 SkIntToScalar(5),
1414 SkIntToScalar(5))));
1415
1416 // outside the diagonal edge
1417 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1418 SkIntToScalar(200),
1419 SkIntToScalar(20),
1420 SkIntToScalar(5))));
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001421
1422 // same as above path and first test but with an extra moveTo.
1423 path.reset();
1424 path.moveTo(100, 100);
1425 path.moveTo(0, 0);
1426 path.lineTo(SkIntToScalar(100), 0);
1427 path.lineTo(0, SkIntToScalar(100));
1428
1429 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1430 SkIntToScalar(10),
1431 SkIntToScalar(10))));
1432
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001433 path.reset();
1434 path.lineTo(100, 100);
1435 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001436}
1437
reed@google.comf32322b2013-10-16 15:14:04 +00001438static void test_isRect_open_close(skiatest::Reporter* reporter) {
1439 SkPath path;
1440 bool isClosed;
1441
1442 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
1443
1444 if (false) {
1445 // I think these should pass, but isRect() doesn't behave
1446 // this way... yet
1447 REPORTER_ASSERT(reporter, path.isRect(NULL, NULL));
1448 REPORTER_ASSERT(reporter, path.isRect(&isClosed, NULL));
1449 REPORTER_ASSERT(reporter, !isClosed);
1450 }
1451
1452 path.close();
1453 REPORTER_ASSERT(reporter, path.isRect(NULL, NULL));
1454 REPORTER_ASSERT(reporter, path.isRect(&isClosed, NULL));
1455 REPORTER_ASSERT(reporter, isClosed);
1456}
1457
caryclark@google.comf1316942011-07-26 19:54:45 +00001458// Simple isRect test is inline TestPath, below.
1459// test_isRect provides more extensive testing.
1460static void test_isRect(skiatest::Reporter* reporter) {
reed@google.comf32322b2013-10-16 15:14:04 +00001461 test_isRect_open_close(reporter);
1462
caryclark@google.comf1316942011-07-26 19:54:45 +00001463 // passing tests (all moveTo / lineTo...
1464 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1465 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1466 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1467 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1468 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1469 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1470 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1471 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1472 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001473 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1474 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 +00001475 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1476 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1477 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
caryclark@google.combfe90372012-11-21 13:56:20 +00001478 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
rmistry@google.comd6176b02012-08-23 18:14:13 +00001479
caryclark@google.comf1316942011-07-26 19:54:45 +00001480 // failing tests
1481 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1482 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1483 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1484 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1485 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1486 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1487 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1488 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
caryclark@google.combfe90372012-11-21 13:56:20 +00001489 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
1490 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
1491 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
rmistry@google.comd6176b02012-08-23 18:14:13 +00001492
caryclark@google.comf1316942011-07-26 19:54:45 +00001493 // failing, no close
1494 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
1495 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
1496
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001497 struct IsRectTest {
1498 SkPoint *fPoints;
1499 size_t fPointCount;
1500 bool fClose;
1501 bool fIsRect;
1502 } tests[] = {
1503 { r1, SK_ARRAY_COUNT(r1), true, true },
1504 { r2, SK_ARRAY_COUNT(r2), true, true },
1505 { r3, SK_ARRAY_COUNT(r3), true, true },
1506 { r4, SK_ARRAY_COUNT(r4), true, true },
1507 { r5, SK_ARRAY_COUNT(r5), true, true },
1508 { r6, SK_ARRAY_COUNT(r6), true, true },
1509 { r7, SK_ARRAY_COUNT(r7), true, true },
1510 { r8, SK_ARRAY_COUNT(r8), true, true },
1511 { r9, SK_ARRAY_COUNT(r9), true, true },
1512 { ra, SK_ARRAY_COUNT(ra), true, true },
1513 { rb, SK_ARRAY_COUNT(rb), true, true },
1514 { rc, SK_ARRAY_COUNT(rc), true, true },
1515 { rd, SK_ARRAY_COUNT(rd), true, true },
1516 { re, SK_ARRAY_COUNT(re), true, true },
1517 { rf, SK_ARRAY_COUNT(rf), true, true },
1518
1519 { f1, SK_ARRAY_COUNT(f1), true, false },
1520 { f2, SK_ARRAY_COUNT(f2), true, false },
1521 { f3, SK_ARRAY_COUNT(f3), true, false },
1522 { f4, SK_ARRAY_COUNT(f4), true, false },
1523 { f5, SK_ARRAY_COUNT(f5), true, false },
1524 { f6, SK_ARRAY_COUNT(f6), true, false },
1525 { f7, SK_ARRAY_COUNT(f7), true, false },
1526 { f8, SK_ARRAY_COUNT(f8), true, false },
1527 { f9, SK_ARRAY_COUNT(f9), true, false },
1528 { fa, SK_ARRAY_COUNT(fa), true, false },
1529 { fb, SK_ARRAY_COUNT(fb), true, false },
1530
1531 { c1, SK_ARRAY_COUNT(c1), false, false },
1532 { c2, SK_ARRAY_COUNT(c2), false, false },
caryclark@google.comf1316942011-07-26 19:54:45 +00001533 };
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001534
1535 const size_t testCount = SK_ARRAY_COUNT(tests);
caryclark@google.comf1316942011-07-26 19:54:45 +00001536 size_t index;
1537 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1538 SkPath path;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001539 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1540 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1541 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.comf1316942011-07-26 19:54:45 +00001542 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001543 if (tests[testIndex].fClose) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001544 path.close();
1545 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001546 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(NULL));
1547 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(NULL, NULL));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001548
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001549 if (tests[testIndex].fIsRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001550 SkRect computed, expected;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001551 expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001552 REPORTER_ASSERT(reporter, path.isRect(&computed));
1553 REPORTER_ASSERT(reporter, expected == computed);
skia.committer@gmail.com1c9c0d32012-11-22 02:02:41 +00001554
caryclark@google.comf68154a2012-11-21 15:18:06 +00001555 bool isClosed;
1556 SkPath::Direction direction, cheapDirection;
1557 REPORTER_ASSERT(reporter, path.cheapComputeDirection(&cheapDirection));
robertphillips@google.com8fd16032013-06-25 15:39:58 +00001558 REPORTER_ASSERT(reporter, path.isRect(&isClosed, &direction));
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001559 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001560 REPORTER_ASSERT(reporter, direction == cheapDirection);
1561 } else {
1562 SkRect computed;
1563 computed.set(123, 456, 789, 1011);
1564 REPORTER_ASSERT(reporter, !path.isRect(&computed));
1565 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
1566 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
1567
1568 bool isClosed = (bool) -1;
1569 SkPath::Direction direction = (SkPath::Direction) -1;
robertphillips@google.com8fd16032013-06-25 15:39:58 +00001570 REPORTER_ASSERT(reporter, !path.isRect(&isClosed, &direction));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001571 REPORTER_ASSERT(reporter, isClosed == (bool) -1);
1572 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001573 }
caryclark@google.comf1316942011-07-26 19:54:45 +00001574 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001575
caryclark@google.comf1316942011-07-26 19:54:45 +00001576 // fail, close then line
1577 SkPath path1;
1578 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001579 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001580 path1.lineTo(r1[index].fX, r1[index].fY);
1581 }
1582 path1.close();
1583 path1.lineTo(1, 0);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001584 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001585
caryclark@google.comf1316942011-07-26 19:54:45 +00001586 // fail, move in the middle
1587 path1.reset();
1588 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001589 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001590 if (index == 2) {
1591 path1.moveTo(1, .5f);
1592 }
1593 path1.lineTo(r1[index].fX, r1[index].fY);
1594 }
1595 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001596 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00001597
1598 // fail, move on the edge
1599 path1.reset();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001600 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001601 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
1602 path1.lineTo(r1[index].fX, r1[index].fY);
1603 }
1604 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001605 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001606
caryclark@google.comf1316942011-07-26 19:54:45 +00001607 // fail, quad
1608 path1.reset();
1609 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001610 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001611 if (index == 2) {
1612 path1.quadTo(1, .5f, 1, .5f);
1613 }
1614 path1.lineTo(r1[index].fX, r1[index].fY);
1615 }
1616 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001617 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001618
caryclark@google.comf1316942011-07-26 19:54:45 +00001619 // fail, cubic
1620 path1.reset();
1621 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001622 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001623 if (index == 2) {
1624 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
1625 }
1626 path1.lineTo(r1[index].fX, r1[index].fY);
1627 }
1628 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001629 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00001630}
1631
caryclark@google.com56f233a2012-11-19 13:06:06 +00001632static void test_isNestedRects(skiatest::Reporter* reporter) {
1633 // passing tests (all moveTo / lineTo...
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001634 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001635 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1636 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1637 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001638 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001639 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1640 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1641 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1642 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001643 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
1644 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 +00001645 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
1646 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
1647 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001648
1649 // failing tests
1650 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1651 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1652 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1653 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1654 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1655 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1656 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1657 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
1658
1659 // failing, no close
1660 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
1661 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
1662
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001663 struct IsNestedRectTest {
1664 SkPoint *fPoints;
1665 size_t fPointCount;
1666 SkPath::Direction fDirection;
1667 bool fClose;
1668 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
1669 } tests[] = {
1670 { r1, SK_ARRAY_COUNT(r1), SkPath::kCW_Direction , true, true },
1671 { r2, SK_ARRAY_COUNT(r2), SkPath::kCW_Direction , true, true },
1672 { r3, SK_ARRAY_COUNT(r3), SkPath::kCW_Direction , true, true },
1673 { r4, SK_ARRAY_COUNT(r4), SkPath::kCW_Direction , true, true },
1674 { r5, SK_ARRAY_COUNT(r5), SkPath::kCCW_Direction, true, true },
1675 { r6, SK_ARRAY_COUNT(r6), SkPath::kCCW_Direction, true, true },
1676 { r7, SK_ARRAY_COUNT(r7), SkPath::kCCW_Direction, true, true },
1677 { r8, SK_ARRAY_COUNT(r8), SkPath::kCCW_Direction, true, true },
1678 { r9, SK_ARRAY_COUNT(r9), SkPath::kCCW_Direction, true, true },
1679 { ra, SK_ARRAY_COUNT(ra), SkPath::kCCW_Direction, true, true },
1680 { rb, SK_ARRAY_COUNT(rb), SkPath::kCW_Direction, true, true },
1681 { rc, SK_ARRAY_COUNT(rc), SkPath::kCW_Direction, true, true },
1682 { rd, SK_ARRAY_COUNT(rd), SkPath::kCCW_Direction, true, true },
1683 { re, SK_ARRAY_COUNT(re), SkPath::kCW_Direction, true, true },
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001684
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001685 { f1, SK_ARRAY_COUNT(f1), SkPath::kUnknown_Direction, true, false },
1686 { f2, SK_ARRAY_COUNT(f2), SkPath::kUnknown_Direction, true, false },
1687 { f3, SK_ARRAY_COUNT(f3), SkPath::kUnknown_Direction, true, false },
1688 { f4, SK_ARRAY_COUNT(f4), SkPath::kUnknown_Direction, true, false },
1689 { f5, SK_ARRAY_COUNT(f5), SkPath::kUnknown_Direction, true, false },
1690 { f6, SK_ARRAY_COUNT(f6), SkPath::kUnknown_Direction, true, false },
1691 { f7, SK_ARRAY_COUNT(f7), SkPath::kUnknown_Direction, true, false },
1692 { f8, SK_ARRAY_COUNT(f8), SkPath::kUnknown_Direction, true, false },
1693
1694 { c1, SK_ARRAY_COUNT(c1), SkPath::kUnknown_Direction, false, false },
1695 { c2, SK_ARRAY_COUNT(c2), SkPath::kUnknown_Direction, false, false },
1696 };
1697
1698 const size_t testCount = SK_ARRAY_COUNT(tests);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001699 size_t index;
1700 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001701 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1702 SkPath path;
1703 if (rectFirst) {
1704 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1705 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001706 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1707 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1708 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001709 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001710 if (tests[testIndex].fClose) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001711 path.close();
1712 }
1713 if (!rectFirst) {
1714 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1715 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001716 REPORTER_ASSERT(reporter, tests[testIndex].fIsNestedRect == path.isNestedRects(NULL));
1717 if (tests[testIndex].fIsNestedRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001718 SkRect expected[2], computed[2];
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001719 SkPath::Direction expectedDirs[2], computedDirs[2];
caryclark@google.com56f233a2012-11-19 13:06:06 +00001720 SkRect testBounds;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001721 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001722 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
1723 expected[1] = testBounds;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001724 if (rectFirst) {
1725 expectedDirs[0] = SkPath::kCW_Direction;
1726 } else {
1727 expectedDirs[0] = SkPath::kCCW_Direction;
1728 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001729 expectedDirs[1] = tests[testIndex].fDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001730 REPORTER_ASSERT(reporter, path.isNestedRects(computed, computedDirs));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001731 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
1732 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001733 REPORTER_ASSERT(reporter, expectedDirs[0] == computedDirs[0]);
1734 REPORTER_ASSERT(reporter, expectedDirs[1] == computedDirs[1]);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001735 }
caryclark@google.com56f233a2012-11-19 13:06:06 +00001736 }
1737
1738 // fail, close then line
1739 SkPath path1;
1740 if (rectFirst) {
1741 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1742 }
1743 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001744 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001745 path1.lineTo(r1[index].fX, r1[index].fY);
1746 }
1747 path1.close();
1748 path1.lineTo(1, 0);
1749 if (!rectFirst) {
1750 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1751 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001752 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001753
1754 // fail, move in the middle
1755 path1.reset();
1756 if (rectFirst) {
1757 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1758 }
1759 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001760 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001761 if (index == 2) {
1762 path1.moveTo(1, .5f);
1763 }
1764 path1.lineTo(r1[index].fX, r1[index].fY);
1765 }
1766 path1.close();
1767 if (!rectFirst) {
1768 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1769 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001770 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001771
1772 // fail, move on the edge
1773 path1.reset();
1774 if (rectFirst) {
1775 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1776 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001777 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001778 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
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, quad
1788 path1.reset();
1789 if (rectFirst) {
1790 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1791 }
1792 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001793 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001794 if (index == 2) {
1795 path1.quadTo(1, .5f, 1, .5f);
1796 }
1797 path1.lineTo(r1[index].fX, r1[index].fY);
1798 }
1799 path1.close();
1800 if (!rectFirst) {
1801 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1802 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001803 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001804
1805 // fail, cubic
1806 path1.reset();
1807 if (rectFirst) {
1808 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1809 }
1810 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001811 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001812 if (index == 2) {
1813 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
1814 }
1815 path1.lineTo(r1[index].fX, r1[index].fY);
1816 }
1817 path1.close();
1818 if (!rectFirst) {
1819 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1820 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001821 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
skia.committer@gmail.com34587162012-11-20 02:01:23 +00001822
caryclark@google.com56f233a2012-11-19 13:06:06 +00001823 // fail, not nested
1824 path1.reset();
1825 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
1826 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001827 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001828 }
caryclark@google.combfe90372012-11-21 13:56:20 +00001829
1830 // pass, stroke rect
1831 SkPath src, dst;
1832 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
1833 SkPaint strokePaint;
1834 strokePaint.setStyle(SkPaint::kStroke_Style);
1835 strokePaint.setStrokeWidth(2);
1836 strokePaint.getFillPath(src, &dst);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001837 REPORTER_ASSERT(reporter, dst.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001838}
1839
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001840static void write_and_read_back(skiatest::Reporter* reporter,
1841 const SkPath& p) {
1842 SkWriter32 writer(100);
1843 writer.writePath(p);
reed@google.com44699382013-10-31 17:28:30 +00001844 size_t size = writer.bytesWritten();
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001845 SkAutoMalloc storage(size);
1846 writer.flatten(storage.get());
1847 SkReader32 reader(storage.get(), size);
1848
1849 SkPath readBack;
1850 REPORTER_ASSERT(reporter, readBack != p);
1851 reader.readPath(&readBack);
1852 REPORTER_ASSERT(reporter, readBack == p);
1853
rmistry@google.comd6176b02012-08-23 18:14:13 +00001854 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001855 p.getConvexityOrUnknown());
1856
1857 REPORTER_ASSERT(reporter, readBack.isOval(NULL) == p.isOval(NULL));
1858
1859 const SkRect& origBounds = p.getBounds();
1860 const SkRect& readBackBounds = readBack.getBounds();
1861
1862 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
1863}
1864
reed@google.com53effc52011-09-21 19:05:12 +00001865static void test_flattening(skiatest::Reporter* reporter) {
1866 SkPath p;
1867
1868 static const SkPoint pts[] = {
1869 { 0, 0 },
1870 { SkIntToScalar(10), SkIntToScalar(10) },
1871 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
1872 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
1873 };
1874 p.moveTo(pts[0]);
1875 p.lineTo(pts[1]);
1876 p.quadTo(pts[2], pts[3]);
1877 p.cubicTo(pts[4], pts[5], pts[6]);
1878
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001879 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001880
1881 // create a buffer that should be much larger than the path so we don't
1882 // kill our stack if writer goes too far.
1883 char buffer[1024];
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00001884 size_t size1 = p.writeToMemory(NULL);
1885 size_t size2 = p.writeToMemory(buffer);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001886 REPORTER_ASSERT(reporter, size1 == size2);
1887
1888 SkPath p2;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00001889 size_t size3 = p2.readFromMemory(buffer, 1024);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001890 REPORTER_ASSERT(reporter, size1 == size3);
1891 REPORTER_ASSERT(reporter, p == p2);
1892
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001893 size3 = p2.readFromMemory(buffer, 0);
1894 REPORTER_ASSERT(reporter, !size3);
1895
1896 SkPath tooShort;
1897 size3 = tooShort.readFromMemory(buffer, size1 - 1);
1898 REPORTER_ASSERT(reporter, tooShort.isEmpty());
1899
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001900 char buffer2[1024];
1901 size3 = p2.writeToMemory(buffer2);
1902 REPORTER_ASSERT(reporter, size1 == size3);
1903 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001904
1905 // test persistence of the oval flag & convexity
1906 {
1907 SkPath oval;
1908 SkRect rect = SkRect::MakeWH(10, 10);
1909 oval.addOval(rect);
1910
1911 write_and_read_back(reporter, oval);
1912 }
reed@google.com53effc52011-09-21 19:05:12 +00001913}
1914
1915static void test_transform(skiatest::Reporter* reporter) {
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00001916 SkPath p;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001917
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001918#define CONIC_PERSPECTIVE_BUG_FIXED 0
reed@google.com53effc52011-09-21 19:05:12 +00001919 static const SkPoint pts[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001920 { 0, 0 }, // move
1921 { SkIntToScalar(10), SkIntToScalar(10) }, // line
1922 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
1923 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
1924#if CONIC_PERSPECTIVE_BUG_FIXED
1925 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
1926#endif
reed@google.com53effc52011-09-21 19:05:12 +00001927 };
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001928 const int kPtCount = SK_ARRAY_COUNT(pts);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00001929
reed@google.com53effc52011-09-21 19:05:12 +00001930 p.moveTo(pts[0]);
1931 p.lineTo(pts[1]);
1932 p.quadTo(pts[2], pts[3]);
1933 p.cubicTo(pts[4], pts[5], pts[6]);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001934#if CONIC_PERSPECTIVE_BUG_FIXED
1935 p.conicTo(pts[4], pts[5], 0.5f);
1936#endif
1937 p.close();
rmistry@google.comd6176b02012-08-23 18:14:13 +00001938
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00001939 {
1940 SkMatrix matrix;
1941 matrix.reset();
1942 SkPath p1;
1943 p.transform(matrix, &p1);
1944 REPORTER_ASSERT(reporter, p == p1);
reed@google.com53effc52011-09-21 19:05:12 +00001945 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001946
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00001947
1948 {
1949 SkMatrix matrix;
1950 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
1951
1952 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
1953
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00001954 p.transform(matrix, &p1);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00001955 SkPoint pts1[kPtCount];
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00001956 int count = p1.getPoints(pts1, kPtCount);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00001957 REPORTER_ASSERT(reporter, kPtCount == count);
1958 for (int i = 0; i < count; ++i) {
1959 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
1960 REPORTER_ASSERT(reporter, newPt == pts1[i]);
1961 }
1962 }
1963
1964 {
1965 SkMatrix matrix;
1966 matrix.reset();
1967 matrix.setPerspX(SkScalarToPersp(4));
1968
1969 SkPath p1;
1970 p1.moveTo(SkPoint::Make(0, 0));
1971
1972 p.transform(matrix, &p1);
1973 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
1974 p1.transform(matrix, NULL);
1975 SkRect pBounds = p.getBounds();
1976 SkRect p1Bounds = p1.getBounds();
1977 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
1978 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
1979 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
1980 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
1981 }
1982
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001983 p.reset();
1984 p.addCircle(0, 0, 1, SkPath::kCW_Direction);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00001985
1986 {
1987 SkMatrix matrix;
1988 matrix.reset();
1989 SkPath p1;
1990 p1.moveTo(SkPoint::Make(0, 0));
1991
1992 p.transform(matrix, &p1);
1993 REPORTER_ASSERT(reporter, p1.cheapIsDirection(SkPath::kCW_Direction));
1994 }
1995
1996
1997 {
1998 SkMatrix matrix;
1999 matrix.reset();
2000 matrix.setScaleX(-1);
2001 SkPath p1;
2002 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2003
2004 p.transform(matrix, &p1);
2005 REPORTER_ASSERT(reporter, p1.cheapIsDirection(SkPath::kCCW_Direction));
2006 }
2007
2008 {
2009 SkMatrix matrix;
2010 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2011 SkPath p1;
2012 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2013
2014 p.transform(matrix, &p1);
2015 REPORTER_ASSERT(reporter, p1.cheapIsDirection(SkPath::kUnknown_Direction));
2016 }
reed@google.com53effc52011-09-21 19:05:12 +00002017}
2018
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002019static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002020 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002021 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002022
caryclark@google.com56f233a2012-11-19 13:06:06 +00002023 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002024 const char* testPath;
2025 const size_t numResultPts;
2026 const SkRect resultBound;
2027 const SkPath::Verb* resultVerbs;
2028 const size_t numResultVerbs;
2029 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002030
schenney@chromium.org7e963602012-06-13 17:05:43 +00002031 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2032 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2033 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2034 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2035 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2036 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2037 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2038 static const SkPath::Verb resultVerbs8[] = {
2039 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2040 };
2041 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2042 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2043 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2044 static const SkPath::Verb resultVerbs12[] = {
2045 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2046 };
2047 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2048 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2049 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2050 static const SkPath::Verb resultVerbs16[] = {
2051 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2052 };
2053 static const struct zeroPathTestData gZeroLengthTests[] = {
2054 { "M 1 1", 1, {0, 0, 0, 0}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002055 { "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 +00002056 { "M 1 1 z", 1, {0, 0, 0, 0}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002057 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2058 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2059 { "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) },
2060 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2061 { "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) },
2062 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2063 { "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) },
2064 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2065 { "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) },
2066 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2067 { "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 +00002068 SK_ARRAY_COUNT(resultVerbs14)
2069 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002070 { "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) },
2071 { "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 +00002072 SK_ARRAY_COUNT(resultVerbs16)
2073 }
2074 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002075
schenney@chromium.org7e963602012-06-13 17:05:43 +00002076 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2077 p.reset();
2078 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2079 REPORTER_ASSERT(reporter, valid);
2080 REPORTER_ASSERT(reporter, !p.isEmpty());
2081 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2082 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2083 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2084 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2085 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2086 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002087 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002088}
2089
2090struct SegmentInfo {
2091 SkPath fPath;
2092 int fPointCount;
2093};
2094
reed@google.com10296cc2011-09-21 12:29:05 +00002095#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2096
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002097static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00002098 SkPath p, p2;
2099
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002100 p.moveTo(0, 0);
2101 p.quadTo(100, 100, 200, 200);
2102 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2103 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002104 p2 = p;
2105 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002106 p.cubicTo(100, 100, 200, 200, 300, 300);
2107 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2108 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002109 p2 = p;
2110 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2111
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002112 p.reset();
2113 p.moveTo(0, 0);
2114 p.cubicTo(100, 100, 200, 200, 300, 300);
2115 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00002116 p2 = p;
2117 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00002118
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002119 REPORTER_ASSERT(reporter, !p.isEmpty());
2120}
2121
2122static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002123 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002124 SkPoint pts[4];
2125
2126 // Test an iterator with no path
2127 SkPath::Iter noPathIter;
2128 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002129
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002130 // Test that setting an empty path works
2131 noPathIter.setPath(p, false);
2132 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002133
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002134 // Test that close path makes no difference for an empty path
2135 noPathIter.setPath(p, true);
2136 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002137
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002138 // Test an iterator with an initial empty path
2139 SkPath::Iter iter(p, false);
2140 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2141
2142 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00002143 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002144 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2145
rmistry@google.comd6176b02012-08-23 18:14:13 +00002146
schenney@chromium.org7e963602012-06-13 17:05:43 +00002147 struct iterTestData {
2148 const char* testPath;
2149 const bool forceClose;
2150 const bool consumeDegenerates;
2151 const size_t* numResultPtsPerVerb;
2152 const SkPoint* resultPts;
2153 const SkPath::Verb* resultVerbs;
2154 const size_t numResultVerbs;
2155 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002156
schenney@chromium.org7e963602012-06-13 17:05:43 +00002157 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2158 static const SkPath::Verb resultVerbs2[] = {
2159 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2160 };
2161 static const SkPath::Verb resultVerbs3[] = {
2162 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2163 };
2164 static const SkPath::Verb resultVerbs4[] = {
2165 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2166 };
2167 static const SkPath::Verb resultVerbs5[] = {
2168 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2169 };
2170 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00002171 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2172 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2173 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2174 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002175 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002176 static const SkPoint resultPts2[] = {
2177 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2178 };
2179 static const SkPoint resultPts3[] = {
2180 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2181 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2182 };
2183 static const SkPoint resultPts4[] = {
2184 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2185 };
2186 static const SkPoint resultPts5[] = {
2187 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2188 };
2189 static const struct iterTestData gIterTests[] = {
2190 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002191 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2192 { "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 +00002193 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2194 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2195 { "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) },
2196 { "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 +00002197 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2198 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2199 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2200 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2201 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2202 { "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 +00002203 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002204
schenney@chromium.org7e963602012-06-13 17:05:43 +00002205 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2206 p.reset();
2207 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2208 REPORTER_ASSERT(reporter, valid);
2209 iter.setPath(p, gIterTests[i].forceClose);
2210 int j = 0, l = 0;
2211 do {
2212 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2213 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2214 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2215 }
2216 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2217 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2218 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002219
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002220 p.reset();
2221 iter.setPath(p, false);
2222 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2223 p.lineTo(1, 1);
2224 p.close();
2225 iter.setPath(p, false);
2226 REPORTER_ASSERT(reporter, iter.isClosedContour());
2227 p.reset();
2228 iter.setPath(p, true);
2229 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2230 p.lineTo(1, 1);
2231 iter.setPath(p, true);
2232 REPORTER_ASSERT(reporter, iter.isClosedContour());
2233 p.moveTo(0, 0);
2234 p.lineTo(2, 2);
2235 iter.setPath(p, false);
2236 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2237
2238 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
2239 // check to see if the result is correct.
2240 for (int setNaN = 0; setNaN < 4; ++setNaN) {
2241 p.reset();
2242 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
2243 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
2244 iter.setPath(p, true);
2245 iter.next(pts, false);
2246 iter.next(pts, false);
2247 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
2248 }
2249
2250 p.reset();
2251 p.quadTo(0, 0, 0, 0);
2252 iter.setPath(p, false);
2253 iter.next(pts, false);
2254 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false));
2255 iter.setPath(p, false);
2256 iter.next(pts, false);
2257 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2258
2259 p.reset();
2260 p.conicTo(0, 0, 0, 0, 0.5f);
2261 iter.setPath(p, false);
2262 iter.next(pts, false);
2263 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
2264 iter.setPath(p, false);
2265 iter.next(pts, false);
2266 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2267
2268 p.reset();
2269 p.cubicTo(0, 0, 0, 0, 0, 0);
2270 iter.setPath(p, false);
2271 iter.next(pts, false);
2272 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2273 iter.setPath(p, false);
2274 iter.next(pts, false);
2275 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2276
2277 p.moveTo(1, 1); // add a trailing moveto
2278 iter.setPath(p, false);
2279 iter.next(pts, false);
2280 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2281 iter.setPath(p, false);
2282 iter.next(pts, false);
2283 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2284
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002285 // The GM degeneratesegments.cpp test is more extensive
2286}
2287
2288static void test_raw_iter(skiatest::Reporter* reporter) {
2289 SkPath p;
2290 SkPoint pts[4];
2291
2292 // Test an iterator with no path
2293 SkPath::RawIter noPathIter;
2294 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2295 // Test that setting an empty path works
2296 noPathIter.setPath(p);
2297 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00002298
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002299 // Test an iterator with an initial empty path
2300 SkPath::RawIter iter(p);
2301 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2302
2303 // Test that a move-only path returns the move.
2304 p.moveTo(SK_Scalar1, 0);
2305 iter.setPath(p);
2306 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2307 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2308 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2309 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2310
2311 // No matter how many moves we add, we should get them all back
2312 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2313 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2314 iter.setPath(p);
2315 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2316 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2317 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2318 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2319 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2320 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2321 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2322 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2323 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2324 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2325
2326 // Initial close is never ever stored
2327 p.reset();
2328 p.close();
2329 iter.setPath(p);
2330 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2331
2332 // Move/close sequences
2333 p.reset();
2334 p.close(); // Not stored, no purpose
2335 p.moveTo(SK_Scalar1, 0);
2336 p.close();
2337 p.close(); // Not stored, no purpose
2338 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2339 p.close();
2340 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2341 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
2342 p.close();
2343 iter.setPath(p);
2344 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2345 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2346 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2347 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2348 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2349 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2350 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2351 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2352 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2353 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2354 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2355 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2356 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2357 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2358 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2359 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2360 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2361 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2362 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2363 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2364 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2365 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2366
2367 // Generate random paths and verify
2368 SkPoint randomPts[25];
2369 for (int i = 0; i < 5; ++i) {
2370 for (int j = 0; j < 5; ++j) {
2371 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
2372 }
2373 }
2374
2375 // Max of 10 segments, max 3 points per segment
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002376 SkRandom rand(9876543);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002377 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00002378 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002379 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00002380
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002381 for (int i = 0; i < 500; ++i) {
2382 p.reset();
2383 bool lastWasClose = true;
2384 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00002385 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002386 int numPoints = 0;
2387 int numVerbs = (rand.nextU() >> 16) % 10;
2388 int numIterVerbs = 0;
2389 for (int j = 0; j < numVerbs; ++j) {
2390 do {
2391 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
2392 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002393 switch (nextVerb) {
2394 case SkPath::kMove_Verb:
2395 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2396 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00002397 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002398 numPoints += 1;
2399 lastWasClose = false;
2400 haveMoveTo = true;
2401 break;
2402 case SkPath::kLine_Verb:
2403 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002404 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002405 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2406 haveMoveTo = true;
2407 }
2408 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2409 p.lineTo(expectedPts[numPoints]);
2410 numPoints += 1;
2411 lastWasClose = false;
2412 break;
2413 case SkPath::kQuad_Verb:
2414 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002415 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002416 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2417 haveMoveTo = true;
2418 }
2419 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2420 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2421 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
2422 numPoints += 2;
2423 lastWasClose = false;
2424 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002425 case SkPath::kConic_Verb:
2426 if (!haveMoveTo) {
2427 expectedPts[numPoints++] = lastMoveToPt;
2428 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2429 haveMoveTo = true;
2430 }
2431 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2432 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2433 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2434 rand.nextUScalar1() * 4);
2435 numPoints += 2;
2436 lastWasClose = false;
2437 break;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002438 case SkPath::kCubic_Verb:
2439 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002440 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002441 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2442 haveMoveTo = true;
2443 }
2444 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2445 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2446 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
2447 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2448 expectedPts[numPoints + 2]);
2449 numPoints += 3;
2450 lastWasClose = false;
2451 break;
2452 case SkPath::kClose_Verb:
2453 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00002454 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002455 lastWasClose = true;
2456 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002457 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002458 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002459 }
2460 expectedVerbs[numIterVerbs++] = nextVerb;
2461 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00002462
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002463 iter.setPath(p);
2464 numVerbs = numIterVerbs;
2465 numIterVerbs = 0;
2466 int numIterPts = 0;
2467 SkPoint lastMoveTo;
2468 SkPoint lastPt;
2469 lastMoveTo.set(0, 0);
2470 lastPt.set(0, 0);
2471 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
2472 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
2473 numIterVerbs++;
2474 switch (nextVerb) {
2475 case SkPath::kMove_Verb:
2476 REPORTER_ASSERT(reporter, numIterPts < numPoints);
2477 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
2478 lastPt = lastMoveTo = pts[0];
2479 numIterPts += 1;
2480 break;
2481 case SkPath::kLine_Verb:
2482 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
2483 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2484 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2485 lastPt = pts[1];
2486 numIterPts += 1;
2487 break;
2488 case SkPath::kQuad_Verb:
reed@google.com277c3f82013-05-31 15:17:50 +00002489 case SkPath::kConic_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002490 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
2491 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2492 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2493 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2494 lastPt = pts[2];
2495 numIterPts += 2;
2496 break;
2497 case SkPath::kCubic_Verb:
2498 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
2499 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2500 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2501 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2502 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
2503 lastPt = pts[3];
2504 numIterPts += 3;
2505 break;
2506 case SkPath::kClose_Verb:
2507 REPORTER_ASSERT(reporter, pts[0] == lastMoveTo);
2508 lastPt = lastMoveTo;
2509 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002510 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002511 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002512 }
2513 }
2514 REPORTER_ASSERT(reporter, numIterPts == numPoints);
2515 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
2516 }
2517}
2518
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002519static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002520 const SkPath& path,
2521 bool expectedCircle,
2522 SkPath::Direction expectedDir) {
robertphillips@google.com466310d2013-12-03 16:43:54 +00002523 SkRect rect = SkRect::MakeEmpty();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002524 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
2525 REPORTER_ASSERT(reporter, path.cheapIsDirection(expectedDir));
skia.committer@gmail.comfbb0ed92012-11-13 21:46:06 +00002526
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002527 if (expectedCircle) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002528 REPORTER_ASSERT(reporter, rect.height() == rect.width());
2529 }
2530}
2531
2532static void test_circle_skew(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002533 const SkPath& path,
2534 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002535 SkPath tmp;
2536
2537 SkMatrix m;
2538 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
2539 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002540 // this matrix reverses the direction.
2541 if (SkPath::kCCW_Direction == dir) {
2542 dir = SkPath::kCW_Direction;
2543 } else {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002544 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002545 dir = SkPath::kCCW_Direction;
2546 }
2547 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002548}
2549
2550static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002551 const SkPath& path,
2552 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002553 SkPath tmp;
2554
2555 // translate at small offset
2556 SkMatrix m;
2557 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
2558 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002559 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002560
2561 tmp.reset();
2562 m.reset();
2563
2564 // translate at a relatively big offset
2565 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
2566 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002567 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002568}
2569
2570static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002571 const SkPath& path,
2572 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002573 for (int angle = 0; angle < 360; ++angle) {
2574 SkPath tmp;
2575 SkMatrix m;
2576 m.setRotate(SkIntToScalar(angle));
2577 path.transform(m, &tmp);
2578
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002579 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002580 // degrees is not an oval anymore, this can be improved. we made this
2581 // for the simplicity of our implementation.
2582 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002583 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002584 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002585 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002586 }
2587 }
2588}
2589
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002590static void test_circle_mirror_x(skiatest::Reporter* reporter,
2591 const SkPath& path,
2592 SkPath::Direction dir) {
2593 SkPath tmp;
2594 SkMatrix m;
2595 m.reset();
2596 m.setScaleX(-SK_Scalar1);
2597 path.transform(m, &tmp);
2598
2599 if (SkPath::kCW_Direction == dir) {
2600 dir = SkPath::kCCW_Direction;
2601 } else {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002602 REPORTER_ASSERT(reporter, SkPath::kCCW_Direction == dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002603 dir = SkPath::kCW_Direction;
2604 }
2605
2606 check_for_circle(reporter, tmp, true, dir);
2607}
2608
2609static void test_circle_mirror_y(skiatest::Reporter* reporter,
2610 const SkPath& path,
2611 SkPath::Direction dir) {
2612 SkPath tmp;
2613 SkMatrix m;
2614 m.reset();
2615 m.setScaleY(-SK_Scalar1);
2616 path.transform(m, &tmp);
2617
2618 if (SkPath::kCW_Direction == dir) {
2619 dir = SkPath::kCCW_Direction;
2620 } else {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002621 REPORTER_ASSERT(reporter, SkPath::kCCW_Direction == dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002622 dir = SkPath::kCW_Direction;
2623 }
2624
2625 check_for_circle(reporter, tmp, true, dir);
2626}
2627
2628static void test_circle_mirror_xy(skiatest::Reporter* reporter,
2629 const SkPath& path,
2630 SkPath::Direction dir) {
2631 SkPath tmp;
2632 SkMatrix m;
2633 m.reset();
2634 m.setScaleX(-SK_Scalar1);
2635 m.setScaleY(-SK_Scalar1);
2636 path.transform(m, &tmp);
2637
2638 check_for_circle(reporter, tmp, true, dir);
2639}
2640
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002641static void test_circle_with_direction(skiatest::Reporter* reporter,
2642 SkPath::Direction dir) {
2643 SkPath path;
2644
2645 // circle at origin
2646 path.addCircle(0, 0, SkIntToScalar(20), dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002647 check_for_circle(reporter, path, true, dir);
2648 test_circle_rotate(reporter, path, dir);
2649 test_circle_translate(reporter, path, dir);
2650 test_circle_skew(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002651
2652 // circle at an offset at (10, 10)
2653 path.reset();
2654 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
2655 SkIntToScalar(20), dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002656 check_for_circle(reporter, path, true, dir);
2657 test_circle_rotate(reporter, path, dir);
2658 test_circle_translate(reporter, path, dir);
2659 test_circle_skew(reporter, path, dir);
2660 test_circle_mirror_x(reporter, path, dir);
2661 test_circle_mirror_y(reporter, path, dir);
2662 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002663}
2664
2665static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
2666 SkPath path;
2667 SkPath circle;
2668 SkPath rect;
2669 SkPath empty;
2670
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002671 static const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
2672 static const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
2673
2674 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002675 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
2676 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
2677
2678 SkMatrix translate;
2679 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
2680
2681 // For simplicity, all the path concatenation related operations
2682 // would mark it non-circle, though in theory it's still a circle.
2683
2684 // empty + circle (translate)
2685 path = empty;
2686 path.addPath(circle, translate);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002687 check_for_circle(reporter, path, false, kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002688
2689 // circle + empty (translate)
2690 path = circle;
2691 path.addPath(empty, translate);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002692 check_for_circle(reporter, path, false, kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002693
2694 // test reverseAddPath
2695 path = circle;
2696 path.reverseAddPath(rect);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002697 check_for_circle(reporter, path, false, kCircleDirOpposite);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002698}
2699
2700static void test_circle(skiatest::Reporter* reporter) {
2701 test_circle_with_direction(reporter, SkPath::kCW_Direction);
2702 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
2703
2704 // multiple addCircle()
2705 SkPath path;
2706 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2707 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002708 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002709
2710 // some extra lineTo() would make isOval() fail
2711 path.reset();
2712 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2713 path.lineTo(0, 0);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002714 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002715
2716 // not back to the original point
2717 path.reset();
2718 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2719 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002720 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002721
2722 test_circle_with_add_paths(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002723
2724 // test negative radius
2725 path.reset();
2726 path.addCircle(0, 0, -1, SkPath::kCW_Direction);
2727 REPORTER_ASSERT(reporter, path.isEmpty());
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002728}
2729
2730static void test_oval(skiatest::Reporter* reporter) {
2731 SkRect rect;
2732 SkMatrix m;
2733 SkPath path;
2734
2735 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
2736 path.addOval(rect);
2737
2738 REPORTER_ASSERT(reporter, path.isOval(NULL));
2739
2740 m.setRotate(SkIntToScalar(90));
2741 SkPath tmp;
2742 path.transform(m, &tmp);
2743 // an oval rotated 90 degrees is still an oval.
2744 REPORTER_ASSERT(reporter, tmp.isOval(NULL));
2745
2746 m.reset();
2747 m.setRotate(SkIntToScalar(30));
2748 tmp.reset();
2749 path.transform(m, &tmp);
2750 // an oval rotated 30 degrees is not an oval anymore.
2751 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2752
2753 // since empty path being transformed.
2754 path.reset();
2755 tmp.reset();
2756 m.reset();
2757 path.transform(m, &tmp);
2758 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2759
2760 // empty path is not an oval
2761 tmp.reset();
2762 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2763
2764 // only has moveTo()s
2765 tmp.reset();
2766 tmp.moveTo(0, 0);
2767 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
2768 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2769
2770 // mimic WebKit's calling convention,
2771 // call moveTo() first and then call addOval()
2772 path.reset();
2773 path.moveTo(0, 0);
2774 path.addOval(rect);
2775 REPORTER_ASSERT(reporter, path.isOval(NULL));
2776
2777 // copy path
2778 path.reset();
2779 tmp.reset();
2780 tmp.addOval(rect);
2781 path = tmp;
2782 REPORTER_ASSERT(reporter, path.isOval(NULL));
2783}
2784
bungeman@google.coma5809a32013-06-21 15:13:34 +00002785static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
2786 SkPath empty;
reed@android.com80e39a72009-04-02 16:59:40 +00002787
reed@android.com3abec1d2009-03-02 05:36:20 +00002788 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002789 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002790 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00002791 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00002792 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00002793 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
2794 REPORTER_ASSERT(reporter, !p.isInverseFillType());
bungeman@google.coma5809a32013-06-21 15:13:34 +00002795 REPORTER_ASSERT(reporter, p == empty);
2796 REPORTER_ASSERT(reporter, !(p != empty));
2797}
2798
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002799static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
2800 SkPath::Direction dir) {
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002801 REPORTER_ASSERT(reporter, path->isConvex());
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002802 REPORTER_ASSERT(reporter, path->cheapIsDirection(dir));
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002803 path->setConvexity(SkPath::kUnknown_Convexity);
2804 REPORTER_ASSERT(reporter, path->isConvex());
2805 path->reset();
2806}
2807
2808static void test_rrect(skiatest::Reporter* reporter) {
2809 SkPath p;
2810 SkRRect rr;
2811 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
2812 SkRect r = {10, 20, 30, 40};
2813 rr.setRectRadii(r, radii);
2814 p.addRRect(rr);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002815 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002816 p.addRRect(rr, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002817 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002818 p.addRoundRect(r, &radii[0].fX);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002819 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002820 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002821 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002822 p.addRoundRect(r, radii[1].fX, radii[1].fY);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002823 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002824 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002825 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
2826 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
2827 SkVector save = radii[i];
2828 radii[i].set(0, 0);
2829 rr.setRectRadii(r, radii);
2830 p.addRRect(rr);
2831 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2832 radii[i] = save;
2833 }
2834 p.addRoundRect(r, 0, 0);
2835 SkRect returnedRect;
2836 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
2837 REPORTER_ASSERT(reporter, returnedRect == r);
2838 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2839 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
2840 rr.setRectRadii(r, zeroRadii);
2841 p.addRRect(rr);
2842 bool closed;
2843 SkPath::Direction dir;
2844 REPORTER_ASSERT(reporter, p.isRect(&closed, &dir));
2845 REPORTER_ASSERT(reporter, closed);
2846 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
2847 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2848 p.addRRect(rr, SkPath::kCW_Direction);
2849 p.addRRect(rr, SkPath::kCW_Direction);
2850 REPORTER_ASSERT(reporter, !p.isConvex());
2851 p.reset();
2852 p.addRRect(rr, SkPath::kCCW_Direction);
2853 p.addRRect(rr, SkPath::kCCW_Direction);
2854 REPORTER_ASSERT(reporter, !p.isConvex());
2855 p.reset();
2856 SkRect emptyR = {10, 20, 10, 30};
2857 rr.setRectRadii(emptyR, radii);
2858 p.addRRect(rr);
2859 REPORTER_ASSERT(reporter, p.isEmpty());
2860 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
2861 rr.setRectRadii(largeR, radii);
2862 p.addRRect(rr);
2863 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2864 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
2865 rr.setRectRadii(infR, radii);
2866 p.addRRect(rr);
2867 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
2868 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
2869 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
2870 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002871}
2872
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002873static void test_arc(skiatest::Reporter* reporter) {
2874 SkPath p;
2875 SkRect emptyOval = {10, 20, 30, 20};
2876 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
2877 p.addArc(emptyOval, 1, 2);
2878 REPORTER_ASSERT(reporter, p.isEmpty());
2879 p.reset();
2880 SkRect oval = {10, 20, 30, 40};
2881 p.addArc(oval, 1, 0);
2882 REPORTER_ASSERT(reporter, p.isEmpty());
2883 p.reset();
2884 SkPath cwOval;
2885 cwOval.addOval(oval);
2886 p.addArc(oval, 1, 360);
2887 REPORTER_ASSERT(reporter, p == cwOval);
2888 p.reset();
2889 SkPath ccwOval;
2890 ccwOval.addOval(oval, SkPath::kCCW_Direction);
2891 p.addArc(oval, 1, -360);
2892 REPORTER_ASSERT(reporter, p == ccwOval);
2893 p.reset();
2894 p.addArc(oval, 1, 180);
2895 REPORTER_ASSERT(reporter, p.isConvex());
2896 REPORTER_ASSERT(reporter, p.cheapIsDirection(SkPath::kCW_Direction));
2897 p.setConvexity(SkPath::kUnknown_Convexity);
2898 REPORTER_ASSERT(reporter, p.isConvex());
2899}
2900
2901static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
2902 SkScalar x0, SkScalar y0) {
2903 SkPoint pts[4];
2904 SkPath::Verb v = iter->next(pts);
2905 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
2906 REPORTER_ASSERT(reporter, pts[0].fX == x0);
2907 REPORTER_ASSERT(reporter, pts[0].fY == y0);
2908}
2909
2910static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
2911 SkScalar x1, SkScalar y1) {
2912 SkPoint pts[4];
2913 SkPath::Verb v = iter->next(pts);
2914 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
2915 REPORTER_ASSERT(reporter, pts[1].fX == x1);
2916 REPORTER_ASSERT(reporter, pts[1].fY == y1);
2917}
2918
2919static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
2920 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
2921 SkPoint pts[4];
2922 SkPath::Verb v = iter->next(pts);
2923 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
2924 REPORTER_ASSERT(reporter, pts[1].fX == x1);
2925 REPORTER_ASSERT(reporter, pts[1].fY == y1);
2926 REPORTER_ASSERT(reporter, pts[2].fX == x2);
2927 REPORTER_ASSERT(reporter, pts[2].fY == y2);
2928}
2929
2930static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
2931 SkPoint pts[4];
2932 SkPath::Verb v = iter->next(pts);
2933 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
2934}
2935
2936static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
2937 check_done(reporter, p, iter);
2938 p->reset();
2939}
2940
2941static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
2942 SkScalar x0, SkScalar y0) {
2943 SkPath::RawIter iter(*p);
2944 check_move(reporter, &iter, x0, y0);
2945 check_done_and_reset(reporter, p, &iter);
2946}
2947
2948static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
2949 SkScalar x1, SkScalar y1) {
2950 SkPath::RawIter iter(*p);
2951 check_move(reporter, &iter, 0, 0);
2952 check_line(reporter, &iter, x1, y1);
2953 check_done_and_reset(reporter, p, &iter);
2954}
2955
2956static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
2957 SkScalar x1, SkScalar y1) {
2958 SkPath::RawIter iter(*p);
2959 check_move(reporter, &iter, 0, 0);
2960 check_line(reporter, &iter, x1, y1);
2961 check_done(reporter, p, &iter);
2962}
2963
2964static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
2965 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
2966 SkPath::RawIter iter(*p);
2967 check_move(reporter, &iter, 0, 0);
2968 check_line(reporter, &iter, x1, y1);
2969 check_line(reporter, &iter, x2, y2);
2970 check_done_and_reset(reporter, p, &iter);
2971}
2972
2973static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
2974 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
2975 SkPath::RawIter iter(*p);
2976 check_move(reporter, &iter, 0, 0);
2977 check_quad(reporter, &iter, x1, y1, x2, y2);
2978 check_done_and_reset(reporter, p, &iter);
2979}
2980
2981static void test_arcTo(skiatest::Reporter* reporter) {
2982 SkPath p;
2983 p.arcTo(0, 0, 1, 2, 1);
2984 check_path_is_line_and_reset(reporter, &p, 0, 0);
2985 p.arcTo(1, 2, 1, 2, 1);
2986 check_path_is_line_and_reset(reporter, &p, 1, 2);
2987 p.arcTo(1, 2, 3, 4, 0);
2988 check_path_is_line_and_reset(reporter, &p, 1, 2);
2989 p.arcTo(1, 2, 0, 0, 1);
2990 check_path_is_line_and_reset(reporter, &p, 1, 2);
2991 p.arcTo(1, 0, 1, 1, 1);
2992 SkPoint pt;
2993 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
2994 p.reset();
2995 p.arcTo(1, 0, 1, -1, 1);
2996 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
2997 p.reset();
2998 SkRect oval = {1, 2, 3, 4};
2999 p.arcTo(oval, 0, 0, true);
3000 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3001 p.arcTo(oval, 0, 0, false);
3002 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3003 p.arcTo(oval, 360, 0, true);
3004 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3005 p.arcTo(oval, 360, 0, false);
3006 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3007 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003008 p.arcTo(oval, 0, sweep, false);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003009 REPORTER_ASSERT(reporter, p.getBounds() == oval);
3010 sweep += delta;
3011 delta /= 2;
3012 }
3013 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003014 p.arcTo(oval, 0, sweep, false);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003015 REPORTER_ASSERT(reporter, p.getBounds() == oval);
3016 sweep -= delta;
3017 delta /= 2;
3018 }
3019 SkRect noOvalWidth = {1, 2, 0, 3};
3020 p.reset();
3021 p.arcTo(noOvalWidth, 0, 360, false);
3022 REPORTER_ASSERT(reporter, p.isEmpty());
3023
3024 SkRect noOvalHeight = {1, 2, 3, 1};
3025 p.reset();
3026 p.arcTo(noOvalHeight, 0, 360, false);
3027 REPORTER_ASSERT(reporter, p.isEmpty());
3028}
3029
3030static void test_addPath(skiatest::Reporter* reporter) {
3031 SkPath p, q;
3032 p.lineTo(1, 2);
3033 q.moveTo(4, 4);
3034 q.lineTo(7, 8);
3035 q.conicTo(8, 7, 6, 5, 0.5f);
3036 q.quadTo(6, 7, 8, 6);
3037 q.cubicTo(5, 6, 7, 8, 7, 5);
3038 q.close();
3039 p.addPath(q, -4, -4);
3040 SkRect expected = {0, 0, 4, 4};
3041 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3042 p.reset();
3043 p.reverseAddPath(q);
3044 SkRect reverseExpected = {4, 4, 8, 8};
3045 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3046}
3047
3048static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3049 SkPath p;
3050 p.conicTo(1, 2, 3, 4, -1);
3051 check_path_is_line_and_reset(reporter, &p, 3, 4);
3052 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3053 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
3054 p.conicTo(1, 2, 3, 4, 1);
3055 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
3056}
3057
3058static void test_get_point(skiatest::Reporter* reporter) {
3059 SkPath p;
3060 SkPoint pt = p.getPoint(0);
3061 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
3062 REPORTER_ASSERT(reporter, !p.getLastPt(NULL));
3063 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
3064 p.setLastPt(10, 10);
3065 pt = p.getPoint(0);
3066 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
3067 REPORTER_ASSERT(reporter, p.getLastPt(NULL));
3068 p.rMoveTo(10, 10);
3069 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
3070}
3071
3072static void test_contains(skiatest::Reporter* reporter) {
3073 SkPath p;
3074 p.setFillType(SkPath::kInverseWinding_FillType);
3075 REPORTER_ASSERT(reporter, p.contains(0, 0));
3076 p.setFillType(SkPath::kWinding_FillType);
3077 REPORTER_ASSERT(reporter, !p.contains(0, 0));
3078 p.moveTo(4, 4);
3079 p.lineTo(6, 8);
3080 p.lineTo(8, 4);
3081 // test quick reject
3082 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3083 REPORTER_ASSERT(reporter, !p.contains(0, 4));
3084 REPORTER_ASSERT(reporter, !p.contains(4, 10));
3085 REPORTER_ASSERT(reporter, !p.contains(10, 4));
3086 // test various crossings in x
3087 REPORTER_ASSERT(reporter, !p.contains(5, 7));
3088 REPORTER_ASSERT(reporter, p.contains(6, 7));
3089 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3090 p.reset();
3091 p.moveTo(4, 4);
3092 p.lineTo(8, 6);
3093 p.lineTo(4, 8);
3094 // test various crossings in y
3095 REPORTER_ASSERT(reporter, !p.contains(7, 5));
3096 REPORTER_ASSERT(reporter, p.contains(7, 6));
3097 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3098 // test quads
3099 p.reset();
3100 p.moveTo(4, 4);
3101 p.quadTo(6, 6, 8, 8);
3102 p.quadTo(6, 8, 4, 8);
3103 p.quadTo(4, 6, 4, 4);
3104 REPORTER_ASSERT(reporter, p.contains(5, 6));
3105 REPORTER_ASSERT(reporter, !p.contains(6, 5));
3106
3107 p.reset();
3108 p.moveTo(6, 6);
3109 p.quadTo(8, 8, 6, 8);
3110 p.quadTo(4, 8, 4, 6);
3111 p.quadTo(4, 4, 6, 6);
3112 REPORTER_ASSERT(reporter, p.contains(5, 6));
3113 REPORTER_ASSERT(reporter, !p.contains(6, 5));
3114
3115#define CONIC_CONTAINS_BUG_FIXED 0
3116#if CONIC_CONTAINS_BUG_FIXED
3117 p.reset();
3118 p.moveTo(4, 4);
3119 p.conicTo(6, 6, 8, 8, 0.5f);
3120 p.conicTo(6, 8, 4, 8, 0.5f);
3121 p.conicTo(4, 6, 4, 4, 0.5f);
3122 REPORTER_ASSERT(reporter, p.contains(5, 6));
3123 REPORTER_ASSERT(reporter, !p.contains(6, 5));
3124#endif
3125
3126 // test cubics
3127 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
3128 for (int i = 0; i < 3; ++i) {
3129 p.reset();
3130 p.setFillType(SkPath::kEvenOdd_FillType);
3131 p.moveTo(pts[i].fX, pts[i].fY);
3132 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);
3133 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);
3134 p.close();
3135 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
3136 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
3137 }
3138}
3139
3140static void test_operatorEqual(skiatest::Reporter* reporter) {
3141 SkPath a;
3142 SkPath b;
3143 REPORTER_ASSERT(reporter, a == a);
3144 REPORTER_ASSERT(reporter, a == b);
3145 a.setFillType(SkPath::kInverseWinding_FillType);
3146 REPORTER_ASSERT(reporter, a != b);
3147 a.reset();
3148 REPORTER_ASSERT(reporter, a == b);
3149 a.lineTo(1, 1);
3150 REPORTER_ASSERT(reporter, a != b);
3151 a.reset();
3152 REPORTER_ASSERT(reporter, a == b);
3153 a.lineTo(1, 1);
3154 b.lineTo(1, 2);
3155 REPORTER_ASSERT(reporter, a != b);
3156 a.reset();
3157 a.lineTo(1, 2);
3158 REPORTER_ASSERT(reporter, a == b);
3159}
3160
3161class PathTest_Private {
3162public:
3163 static void TestPathTo(skiatest::Reporter* reporter) {
3164 SkPath p, q;
3165 p.lineTo(4, 4);
3166 p.reversePathTo(q);
3167 check_path_is_line(reporter, &p, 4, 4);
3168 q.moveTo(-4, -4);
3169 p.reversePathTo(q);
3170 check_path_is_line(reporter, &p, 4, 4);
3171 q.lineTo(7, 8);
3172 q.conicTo(8, 7, 6, 5, 0.5f);
3173 q.quadTo(6, 7, 8, 6);
3174 q.cubicTo(5, 6, 7, 8, 7, 5);
3175 q.close();
3176 p.reversePathTo(q);
3177 SkRect reverseExpected = {-4, -4, 8, 8};
3178 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3179 }
3180};
3181
bungeman@google.coma5809a32013-06-21 15:13:34 +00003182static void TestPath(skiatest::Reporter* reporter) {
3183 SkTSize<SkScalar>::Make(3,4);
3184
3185 SkPath p, empty;
3186 SkRect bounds, bounds2;
3187 test_empty(reporter, p);
reed@android.com3abec1d2009-03-02 05:36:20 +00003188
reed@android.comd252db02009-04-01 18:31:44 +00003189 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00003190
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003191 // this triggers a code path in SkPath::operator= which is otherwise unexercised
3192 SkPath& self = p;
3193 p = self;
3194
3195 // this triggers a code path in SkPath::swap which is otherwise unexercised
3196 p.swap(self);
3197
reed@android.com3abec1d2009-03-02 05:36:20 +00003198 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00003199
reed@android.com6b82d1a2009-06-03 02:35:01 +00003200 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
3201 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00003202 // we have quads or cubics
3203 REPORTER_ASSERT(reporter, p.getSegmentMasks() & kCurveSegmentMask);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003204 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00003205
reed@android.com6b82d1a2009-06-03 02:35:01 +00003206 p.reset();
bungeman@google.coma5809a32013-06-21 15:13:34 +00003207 test_empty(reporter, p);
reed@google.com10296cc2011-09-21 12:29:05 +00003208
reed@android.com6b82d1a2009-06-03 02:35:01 +00003209 p.addOval(bounds);
3210 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003211 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00003212
bungeman@google.coma5809a32013-06-21 15:13:34 +00003213 p.rewind();
3214 test_empty(reporter, p);
3215
reed@android.com3abec1d2009-03-02 05:36:20 +00003216 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00003217 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00003218 // we have only lines
3219 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003220 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00003221
bungeman@google.coma5809a32013-06-21 15:13:34 +00003222 REPORTER_ASSERT(reporter, p != empty);
3223 REPORTER_ASSERT(reporter, !(p == empty));
reed@android.com3abec1d2009-03-02 05:36:20 +00003224
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003225 // do getPoints and getVerbs return the right result
3226 REPORTER_ASSERT(reporter, p.getPoints(NULL, 0) == 4);
3227 REPORTER_ASSERT(reporter, p.getVerbs(NULL, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00003228 SkPoint pts[4];
3229 int count = p.getPoints(pts, 4);
3230 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003231 uint8_t verbs[6];
3232 verbs[5] = 0xff;
3233 p.getVerbs(verbs, 5);
3234 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
3235 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
3236 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
3237 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
3238 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
3239 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00003240 bounds2.set(pts, 4);
3241 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00003242
reed@android.com3abec1d2009-03-02 05:36:20 +00003243 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
3244 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00003245 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00003246
reed@android.com3abec1d2009-03-02 05:36:20 +00003247 REPORTER_ASSERT(reporter, p.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00003248 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00003249 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
3250 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00003251
reed@android.com3abec1d2009-03-02 05:36:20 +00003252 // now force p to not be a rect
3253 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
3254 p.addRect(bounds);
3255 REPORTER_ASSERT(reporter, !p.isRect(NULL));
reed@android.com3abec1d2009-03-02 05:36:20 +00003256
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003257 test_operatorEqual(reporter);
reed@google.com7e6c4d12012-05-10 14:05:43 +00003258 test_isLine(reporter);
3259 test_isRect(reporter);
caryclark@google.com56f233a2012-11-19 13:06:06 +00003260 test_isNestedRects(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003261 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00003262 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00003263 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00003264 test_convexity2(reporter);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00003265 test_conservativelyContains(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00003266 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003267 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00003268 test_flattening(reporter);
3269 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00003270 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003271 test_iter(reporter);
3272 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003273 test_circle(reporter);
3274 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00003275 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00003276 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00003277 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00003278 test_isfinite_after_transform(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00003279 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00003280 test_arb_zero_rad_round_rect_is_rect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003281 test_addrect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00003282 test_addrect_isfinite(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00003283 test_tricky_cubic();
3284 test_clipped_cubic();
3285 test_crbug_170666();
reed@google.com7a90daf2013-04-10 18:44:00 +00003286 test_bad_cubic_crbug229478();
reed@google.com3eff3592013-05-08 21:08:21 +00003287 test_bad_cubic_crbug234190();
mtklein@google.com9c9d4a72013-08-07 19:17:53 +00003288 test_android_specific_behavior(reporter);
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +00003289 test_gen_id(reporter);
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +00003290 test_path_close_issue1474(reporter);
reed@google.comb58ba892013-10-15 15:44:35 +00003291 test_path_to_region(reporter);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003292 test_rrect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003293 test_arc(reporter);
3294 test_arcTo(reporter);
3295 test_addPath(reporter);
3296 test_conicTo_special_case(reporter);
3297 test_get_point(reporter);
3298 test_contains(reporter);
3299 PathTest_Private::TestPathTo(reporter);
reed@android.com3abec1d2009-03-02 05:36:20 +00003300}
3301
3302#include "TestClassDef.h"
3303DEFINE_TESTCLASS("Path", PathTestClass, TestPath)