blob: 2dbf5c695e99039d9ef9d05619011afa9d0a7549 [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
reed@google.coma8790de2012-10-24 21:04:04 +0000360// Make sure we stay non-finite once we get there (unless we reset or rewind).
361static void test_addrect_isfinite(skiatest::Reporter* reporter) {
362 SkPath path;
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000363
reed@google.coma8790de2012-10-24 21:04:04 +0000364 path.addRect(SkRect::MakeWH(50, 100));
365 REPORTER_ASSERT(reporter, path.isFinite());
366
367 path.moveTo(0, 0);
368 path.lineTo(SK_ScalarInfinity, 42);
369 REPORTER_ASSERT(reporter, !path.isFinite());
370
371 path.addRect(SkRect::MakeWH(50, 100));
372 REPORTER_ASSERT(reporter, !path.isFinite());
373
374 path.reset();
375 REPORTER_ASSERT(reporter, path.isFinite());
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000376
reed@google.coma8790de2012-10-24 21:04:04 +0000377 path.addRect(SkRect::MakeWH(50, 100));
378 REPORTER_ASSERT(reporter, path.isFinite());
379}
380
reed@google.com848148e2013-01-15 15:51:59 +0000381static void build_big_path(SkPath* path, bool reducedCase) {
382 if (reducedCase) {
383 path->moveTo(577330, 1971.72f);
384 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
385 } else {
386 path->moveTo(60.1631f, 7.70567f);
387 path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
388 path->lineTo(577379, 1977.77f);
389 path->quadTo(577364, 1979.57f, 577325, 1980.26f);
390 path->quadTo(577286, 1980.95f, 577245, 1980.13f);
391 path->quadTo(577205, 1979.3f, 577187, 1977.45f);
392 path->quadTo(577168, 1975.6f, 577183, 1973.8f);
393 path->quadTo(577198, 1972, 577238, 1971.31f);
394 path->quadTo(577277, 1970.62f, 577317, 1971.45f);
395 path->quadTo(577330, 1971.72f, 577341, 1972.11f);
396 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
397 path->moveTo(306.718f, -32.912f);
398 path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
399 }
400}
401
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000402static void test_clipped_cubic() {
reed@google.comd28ba802013-09-20 19:33:52 +0000403 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(640, 480));
reed@google.com848148e2013-01-15 15:51:59 +0000404
405 // This path used to assert, because our cubic-chopping code incorrectly
406 // moved control points after the chop. This test should be run in SK_DEBUG
407 // mode to ensure that we no long assert.
408 SkPath path;
409 for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
410 build_big_path(&path, SkToBool(doReducedCase));
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +0000411
reed@google.com848148e2013-01-15 15:51:59 +0000412 SkPaint paint;
413 for (int doAA = 0; doAA <= 1; ++doAA) {
414 paint.setAntiAlias(SkToBool(doAA));
415 surface->getCanvas()->drawPath(path, paint);
416 }
417 }
418}
419
reed@google.com8cae8352012-09-14 15:18:41 +0000420// Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
421// which triggered an assert, from a tricky cubic. This test replicates that
422// example, so we can ensure that we handle it (in SkEdge.cpp), and don't
423// assert in the SK_DEBUG build.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000424static void test_tricky_cubic() {
reed@google.com8cae8352012-09-14 15:18:41 +0000425 const SkPoint pts[] = {
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +0000426 { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
427 { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
428 { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
429 { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
reed@google.com8cae8352012-09-14 15:18:41 +0000430 };
431
432 SkPath path;
433 path.moveTo(pts[0]);
434 path.cubicTo(pts[1], pts[2], pts[3]);
435
436 SkPaint paint;
437 paint.setAntiAlias(true);
438
reed@google.comd28ba802013-09-20 19:33:52 +0000439 SkSurface* surface = SkSurface::NewRasterPMColor(19, 130);
reed@google.com8cae8352012-09-14 15:18:41 +0000440 surface->getCanvas()->drawPath(path, paint);
441 surface->unref();
442}
reed@android.com3abec1d2009-03-02 05:36:20 +0000443
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000444// Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
445//
446static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
447 SkPath path;
448 path.quadTo(157, 366, 286, 208);
449 path.arcTo(37, 442, 315, 163, 957494590897113.0f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000450
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000451 SkMatrix matrix;
452 matrix.setScale(1000*1000, 1000*1000);
453
454 // Be sure that path::transform correctly updates isFinite and the bounds
455 // if the transformation overflows. The previous bug was that isFinite was
456 // set to true in this case, but the bounds were not set to empty (which
457 // they should be).
458 while (path.isFinite()) {
459 REPORTER_ASSERT(reporter, path.getBounds().isFinite());
460 REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
461 path.transform(matrix);
462 }
463 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
464
465 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
466 path.transform(matrix);
467 // we need to still be non-finite
468 REPORTER_ASSERT(reporter, !path.isFinite());
469 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
470}
471
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000472static void add_corner_arc(SkPath* path, const SkRect& rect,
473 SkScalar xIn, SkScalar yIn,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000474 int startAngle)
475{
476
477 SkScalar rx = SkMinScalar(rect.width(), xIn);
478 SkScalar ry = SkMinScalar(rect.height(), yIn);
479
480 SkRect arcRect;
481 arcRect.set(-rx, -ry, rx, ry);
482 switch (startAngle) {
483 case 0:
484 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
485 break;
486 case 90:
487 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
488 break;
489 case 180:
490 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
491 break;
492 case 270:
493 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
494 break;
495 default:
496 break;
497 }
498
499 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
500}
501
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000502static void make_arb_round_rect(SkPath* path, const SkRect& r,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000503 SkScalar xCorner, SkScalar yCorner) {
504 // we are lazy here and use the same x & y for each corner
505 add_corner_arc(path, r, xCorner, yCorner, 270);
506 add_corner_arc(path, r, xCorner, yCorner, 0);
507 add_corner_arc(path, r, xCorner, yCorner, 90);
508 add_corner_arc(path, r, xCorner, yCorner, 180);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000509 path->close();
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000510}
511
512// Chrome creates its own round rects with each corner possibly being different.
513// Performance will suffer if they are not convex.
514// Note: PathBench::ArbRoundRectBench performs almost exactly
515// the same test (but with drawing)
516static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000517 SkRandom rand;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000518 SkRect r;
519
520 for (int i = 0; i < 5000; ++i) {
521
robertphillips@google.com158618e2012-10-23 16:56:56 +0000522 SkScalar size = rand.nextUScalar1() * 30;
523 if (size < SK_Scalar1) {
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000524 continue;
525 }
526 r.fLeft = rand.nextUScalar1() * 300;
527 r.fTop = rand.nextUScalar1() * 300;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000528 r.fRight = r.fLeft + 2 * size;
529 r.fBottom = r.fTop + 2 * size;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000530
531 SkPath temp;
532
533 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
534
535 REPORTER_ASSERT(reporter, temp.isConvex());
536 }
537}
538
robertphillips@google.com158618e2012-10-23 16:56:56 +0000539// Chrome will sometimes create a 0 radius round rect. The degenerate
540// quads prevent the path from being converted to a rect
541// Note: PathBench::ArbRoundRectBench performs almost exactly
542// the same test (but with drawing)
543static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000544 SkRandom rand;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000545 SkRect r;
546
547 for (int i = 0; i < 5000; ++i) {
548
549 SkScalar size = rand.nextUScalar1() * 30;
550 if (size < SK_Scalar1) {
551 continue;
552 }
553 r.fLeft = rand.nextUScalar1() * 300;
554 r.fTop = rand.nextUScalar1() * 300;
555 r.fRight = r.fLeft + 2 * size;
556 r.fBottom = r.fTop + 2 * size;
557
558 SkPath temp;
559
560 make_arb_round_rect(&temp, r, 0, 0);
561
robertphillips@google.com158618e2012-10-23 16:56:56 +0000562 SkRect result;
563 REPORTER_ASSERT(reporter, temp.isRect(&result));
564 REPORTER_ASSERT(reporter, r == result);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000565 }
566}
567
reed@google.com0bb18bb2012-07-26 15:20:36 +0000568static void test_rect_isfinite(skiatest::Reporter* reporter) {
569 const SkScalar inf = SK_ScalarInfinity;
caryclark@google.com7abfa492013-04-12 11:59:41 +0000570 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000571 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000572
reed@google.com0bb18bb2012-07-26 15:20:36 +0000573 SkRect r;
574 r.setEmpty();
575 REPORTER_ASSERT(reporter, r.isFinite());
caryclark@google.com7abfa492013-04-12 11:59:41 +0000576 r.set(0, 0, inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000577 REPORTER_ASSERT(reporter, !r.isFinite());
578 r.set(0, 0, nan, 0);
579 REPORTER_ASSERT(reporter, !r.isFinite());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000580
reed@google.com0bb18bb2012-07-26 15:20:36 +0000581 SkPoint pts[] = {
582 { 0, 0 },
583 { SK_Scalar1, 0 },
584 { 0, SK_Scalar1 },
585 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000586
reed@google.com0bb18bb2012-07-26 15:20:36 +0000587 bool isFine = r.setBoundsCheck(pts, 3);
588 REPORTER_ASSERT(reporter, isFine);
589 REPORTER_ASSERT(reporter, !r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000590
reed@google.com0bb18bb2012-07-26 15:20:36 +0000591 pts[1].set(inf, 0);
592 isFine = r.setBoundsCheck(pts, 3);
593 REPORTER_ASSERT(reporter, !isFine);
594 REPORTER_ASSERT(reporter, r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000595
reed@google.com0bb18bb2012-07-26 15:20:36 +0000596 pts[1].set(nan, 0);
597 isFine = r.setBoundsCheck(pts, 3);
598 REPORTER_ASSERT(reporter, !isFine);
599 REPORTER_ASSERT(reporter, r.isEmpty());
600}
601
602static void test_path_isfinite(skiatest::Reporter* reporter) {
603 const SkScalar inf = SK_ScalarInfinity;
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000604 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000605 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000606
reed@google.com0bb18bb2012-07-26 15:20:36 +0000607 SkPath path;
608 REPORTER_ASSERT(reporter, path.isFinite());
609
610 path.reset();
611 REPORTER_ASSERT(reporter, path.isFinite());
612
613 path.reset();
614 path.moveTo(SK_Scalar1, 0);
615 REPORTER_ASSERT(reporter, path.isFinite());
616
617 path.reset();
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000618 path.moveTo(inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000619 REPORTER_ASSERT(reporter, !path.isFinite());
620
621 path.reset();
622 path.moveTo(nan, 0);
623 REPORTER_ASSERT(reporter, !path.isFinite());
624}
625
626static void test_isfinite(skiatest::Reporter* reporter) {
627 test_rect_isfinite(reporter);
628 test_path_isfinite(reporter);
629}
630
reed@google.com744faba2012-05-29 19:54:52 +0000631// assert that we always
632// start with a moveTo
633// only have 1 moveTo
634// only have Lines after that
635// end with a single close
636// only have (at most) 1 close
637//
638static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000639 const SkPoint srcPts[], bool expectClose) {
reed@google.com744faba2012-05-29 19:54:52 +0000640 SkPath::RawIter iter(path);
641 SkPoint pts[4];
reed@google.com744faba2012-05-29 19:54:52 +0000642
643 bool firstTime = true;
644 bool foundClose = false;
645 for (;;) {
646 switch (iter.next(pts)) {
647 case SkPath::kMove_Verb:
648 REPORTER_ASSERT(reporter, firstTime);
649 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
650 srcPts++;
651 firstTime = false;
652 break;
653 case SkPath::kLine_Verb:
654 REPORTER_ASSERT(reporter, !firstTime);
655 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
656 srcPts++;
657 break;
658 case SkPath::kQuad_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000659 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb");
reed@google.com744faba2012-05-29 19:54:52 +0000660 break;
reed@google.com277c3f82013-05-31 15:17:50 +0000661 case SkPath::kConic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000662 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb");
reed@google.com277c3f82013-05-31 15:17:50 +0000663 break;
reed@google.com744faba2012-05-29 19:54:52 +0000664 case SkPath::kCubic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000665 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb");
reed@google.com744faba2012-05-29 19:54:52 +0000666 break;
667 case SkPath::kClose_Verb:
668 REPORTER_ASSERT(reporter, !firstTime);
669 REPORTER_ASSERT(reporter, !foundClose);
670 REPORTER_ASSERT(reporter, expectClose);
671 foundClose = true;
672 break;
673 case SkPath::kDone_Verb:
674 goto DONE;
675 }
676 }
677DONE:
678 REPORTER_ASSERT(reporter, foundClose == expectClose);
679}
680
681static void test_addPoly(skiatest::Reporter* reporter) {
682 SkPoint pts[32];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000683 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000684
reed@google.com744faba2012-05-29 19:54:52 +0000685 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
686 pts[i].fX = rand.nextSScalar1();
687 pts[i].fY = rand.nextSScalar1();
688 }
689
690 for (int doClose = 0; doClose <= 1; ++doClose) {
691 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
692 SkPath path;
693 path.addPoly(pts, count, SkToBool(doClose));
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000694 test_poly(reporter, path, pts, SkToBool(doClose));
reed@google.com744faba2012-05-29 19:54:52 +0000695 }
696 }
697}
698
reed@google.com8b06f1a2012-05-29 12:03:46 +0000699static void test_strokerec(skiatest::Reporter* reporter) {
700 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
701 REPORTER_ASSERT(reporter, rec.isFillStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000702
reed@google.com8b06f1a2012-05-29 12:03:46 +0000703 rec.setHairlineStyle();
704 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000705
reed@google.com8b06f1a2012-05-29 12:03:46 +0000706 rec.setStrokeStyle(SK_Scalar1, false);
707 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000708
reed@google.com8b06f1a2012-05-29 12:03:46 +0000709 rec.setStrokeStyle(SK_Scalar1, true);
710 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000711
reed@google.com8b06f1a2012-05-29 12:03:46 +0000712 rec.setStrokeStyle(0, false);
713 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000714
reed@google.com8b06f1a2012-05-29 12:03:46 +0000715 rec.setStrokeStyle(0, true);
716 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
717}
718
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000719// Set this for paths that don't have a consistent direction such as a bowtie.
720// (cheapComputeDirection is not expected to catch these.)
721static const SkPath::Direction kDontCheckDir = static_cast<SkPath::Direction>(-1);
722
723static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
724 SkPath::Direction expected) {
725 if (expected == kDontCheckDir) {
726 return;
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000727 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000728 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
729
730 SkPath::Direction dir;
731 if (copy.cheapComputeDirection(&dir)) {
732 REPORTER_ASSERT(reporter, dir == expected);
733 } else {
734 REPORTER_ASSERT(reporter, SkPath::kUnknown_Direction == expected);
735 }
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000736}
737
reed@google.com3e71a882012-01-10 18:44:37 +0000738static void test_direction(skiatest::Reporter* reporter) {
739 size_t i;
740 SkPath path;
741 REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL));
742 REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCW_Direction));
743 REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCCW_Direction));
reed@google.coma8a3b3d2012-11-26 18:16:27 +0000744 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kUnknown_Direction));
reed@google.com3e71a882012-01-10 18:44:37 +0000745
746 static const char* gDegen[] = {
747 "M 10 10",
748 "M 10 10 M 20 20",
749 "M 10 10 L 20 20",
750 "M 10 10 L 10 10 L 10 10",
751 "M 10 10 Q 10 10 10 10",
752 "M 10 10 C 10 10 10 10 10 10",
753 };
754 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
755 path.reset();
756 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
757 REPORTER_ASSERT(reporter, valid);
758 REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL));
759 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000760
reed@google.com3e71a882012-01-10 18:44:37 +0000761 static const char* gCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000762 "M 10 10 L 10 10 Q 20 10 20 20",
reed@google.com3e71a882012-01-10 18:44:37 +0000763 "M 10 10 C 20 10 20 20 20 20",
reed@google.comd4146662012-01-31 15:42:29 +0000764 "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 +0000765 // rect with top two corners replaced by cubics with identical middle
766 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000767 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
768 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000769 };
770 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
771 path.reset();
772 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
773 REPORTER_ASSERT(reporter, valid);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000774 check_direction(reporter, path, SkPath::kCW_Direction);
reed@google.com3e71a882012-01-10 18:44:37 +0000775 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000776
reed@google.com3e71a882012-01-10 18:44:37 +0000777 static const char* gCCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000778 "M 10 10 L 10 10 Q 20 10 20 -20",
reed@google.com3e71a882012-01-10 18:44:37 +0000779 "M 10 10 C 20 10 20 -20 20 -20",
reed@google.comd4146662012-01-31 15:42:29 +0000780 "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 +0000781 // rect with top two corners replaced by cubics with identical middle
782 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000783 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
784 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000785 };
786 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
787 path.reset();
788 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
789 REPORTER_ASSERT(reporter, valid);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000790 check_direction(reporter, path, SkPath::kCCW_Direction);
reed@google.com3e71a882012-01-10 18:44:37 +0000791 }
reed@google.comac8543f2012-01-30 20:51:25 +0000792
793 // Test two donuts, each wound a different direction. Only the outer contour
794 // determines the cheap direction
795 path.reset();
796 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
797 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000798 check_direction(reporter, path, SkPath::kCW_Direction);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000799
reed@google.comac8543f2012-01-30 20:51:25 +0000800 path.reset();
801 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
802 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000803 check_direction(reporter, path, SkPath::kCCW_Direction);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000804
bsalomon@google.com6843ac42012-02-17 13:49:03 +0000805#ifdef SK_SCALAR_IS_FLOAT
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000806 // triangle with one point really far from the origin.
807 path.reset();
808 // the first point is roughly 1.05e10, 1.05e10
bsalomon@google.com53aab782012-02-23 14:54:49 +0000809 path.moveTo(SkFloatToScalar(SkBits2Float(0x501c7652)), SkFloatToScalar(SkBits2Float(0x501c7652)));
810 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
811 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000812 check_direction(reporter, path, SkPath::kCCW_Direction);
bsalomon@google.com53aab782012-02-23 14:54:49 +0000813#endif
reed@google.com3e71a882012-01-10 18:44:37 +0000814}
815
reed@google.comffdb0182011-11-14 19:29:14 +0000816static void add_rect(SkPath* path, const SkRect& r) {
817 path->moveTo(r.fLeft, r.fTop);
818 path->lineTo(r.fRight, r.fTop);
819 path->lineTo(r.fRight, r.fBottom);
820 path->lineTo(r.fLeft, r.fBottom);
821 path->close();
822}
823
824static void test_bounds(skiatest::Reporter* reporter) {
825 static const SkRect rects[] = {
reed@google.com3563c9e2011-11-14 19:34:57 +0000826 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
827 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
828 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
829 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
reed@google.comffdb0182011-11-14 19:29:14 +0000830 };
831
832 SkPath path0, path1;
833 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
834 path0.addRect(rects[i]);
835 add_rect(&path1, rects[i]);
836 }
837
838 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
839}
840
reed@google.com55b5f4b2011-09-07 12:23:41 +0000841static void stroke_cubic(const SkPoint pts[4]) {
842 SkPath path;
843 path.moveTo(pts[0]);
844 path.cubicTo(pts[1], pts[2], pts[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000845
reed@google.com55b5f4b2011-09-07 12:23:41 +0000846 SkPaint paint;
847 paint.setStyle(SkPaint::kStroke_Style);
848 paint.setStrokeWidth(SK_Scalar1 * 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000849
reed@google.com55b5f4b2011-09-07 12:23:41 +0000850 SkPath fill;
851 paint.getFillPath(path, &fill);
852}
853
854// just ensure this can run w/o any SkASSERTS firing in the debug build
855// we used to assert due to differences in how we determine a degenerate vector
856// but that was fixed with the introduction of SkPoint::CanNormalize
857static void stroke_tiny_cubic() {
858 SkPoint p0[] = {
859 { 372.0f, 92.0f },
860 { 372.0f, 92.0f },
861 { 372.0f, 92.0f },
862 { 372.0f, 92.0f },
863 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000864
reed@google.com55b5f4b2011-09-07 12:23:41 +0000865 stroke_cubic(p0);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000866
reed@google.com55b5f4b2011-09-07 12:23:41 +0000867 SkPoint p1[] = {
868 { 372.0f, 92.0f },
869 { 372.0007f, 92.000755f },
870 { 371.99927f, 92.003922f },
871 { 371.99826f, 92.003899f },
872 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000873
reed@google.com55b5f4b2011-09-07 12:23:41 +0000874 stroke_cubic(p1);
875}
876
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000877static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
878 for (int i = 0; i < 2; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +0000879 SkPath::Iter iter(path, SkToBool(i));
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000880 SkPoint mv;
881 SkPoint pts[4];
882 SkPath::Verb v;
883 int nMT = 0;
884 int nCL = 0;
tomhudson@google.com221db3c2011-07-28 21:10:29 +0000885 mv.set(0, 0);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000886 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
887 switch (v) {
888 case SkPath::kMove_Verb:
889 mv = pts[0];
890 ++nMT;
891 break;
892 case SkPath::kClose_Verb:
893 REPORTER_ASSERT(reporter, mv == pts[0]);
894 ++nCL;
895 break;
896 default:
897 break;
898 }
899 }
900 // if we force a close on the interator we should have a close
901 // for every moveTo
902 REPORTER_ASSERT(reporter, !i || nMT == nCL);
903 }
904}
905
906static void test_close(skiatest::Reporter* reporter) {
907 SkPath closePt;
908 closePt.moveTo(0, 0);
909 closePt.close();
910 check_close(reporter, closePt);
911
912 SkPath openPt;
913 openPt.moveTo(0, 0);
914 check_close(reporter, openPt);
915
916 SkPath empty;
917 check_close(reporter, empty);
918 empty.close();
919 check_close(reporter, empty);
920
921 SkPath rect;
922 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
923 check_close(reporter, rect);
924 rect.close();
925 check_close(reporter, rect);
926
927 SkPath quad;
928 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
929 check_close(reporter, quad);
930 quad.close();
931 check_close(reporter, quad);
932
933 SkPath cubic;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000934 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000935 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
936 check_close(reporter, cubic);
937 cubic.close();
938 check_close(reporter, cubic);
939
940 SkPath line;
941 line.moveTo(SK_Scalar1, SK_Scalar1);
942 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
943 check_close(reporter, line);
944 line.close();
945 check_close(reporter, line);
946
947 SkPath rect2;
948 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
949 rect2.close();
950 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
951 check_close(reporter, rect2);
952 rect2.close();
953 check_close(reporter, rect2);
954
955 SkPath oval3;
956 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
957 oval3.close();
958 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
959 check_close(reporter, oval3);
960 oval3.close();
961 check_close(reporter, oval3);
962
963 SkPath moves;
964 moves.moveTo(SK_Scalar1, SK_Scalar1);
965 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
966 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
967 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
968 check_close(reporter, moves);
reed@google.com55b5f4b2011-09-07 12:23:41 +0000969
970 stroke_tiny_cubic();
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000971}
972
reed@google.com7c424812011-05-15 04:38:34 +0000973static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
974 SkPath::Convexity expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000975 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
976 SkPath::Convexity c = copy.getConvexity();
reed@google.com7c424812011-05-15 04:38:34 +0000977 REPORTER_ASSERT(reporter, c == expected);
978}
979
980static void test_convexity2(skiatest::Reporter* reporter) {
981 SkPath pt;
982 pt.moveTo(0, 0);
983 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +0000984 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000985 check_direction(reporter, pt, SkPath::kUnknown_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000986
reed@google.com7c424812011-05-15 04:38:34 +0000987 SkPath line;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000988 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
989 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000990 line.close();
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000991 check_convexity(reporter, line, SkPath::kConvex_Convexity);
992 check_direction(reporter, line, SkPath::kUnknown_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000993
reed@google.com7c424812011-05-15 04:38:34 +0000994 SkPath triLeft;
995 triLeft.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000996 triLeft.lineTo(SK_Scalar1, 0);
997 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000998 triLeft.close();
999 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001000 check_direction(reporter, triLeft, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001001
reed@google.com7c424812011-05-15 04:38:34 +00001002 SkPath triRight;
1003 triRight.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001004 triRight.lineTo(-SK_Scalar1, 0);
1005 triRight.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001006 triRight.close();
1007 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001008 check_direction(reporter, triRight, SkPath::kCCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001009
reed@google.com7c424812011-05-15 04:38:34 +00001010 SkPath square;
1011 square.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001012 square.lineTo(SK_Scalar1, 0);
1013 square.lineTo(SK_Scalar1, SK_Scalar1);
1014 square.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001015 square.close();
1016 check_convexity(reporter, square, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001017 check_direction(reporter, square, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001018
reed@google.com7c424812011-05-15 04:38:34 +00001019 SkPath redundantSquare;
1020 redundantSquare.moveTo(0, 0);
1021 redundantSquare.lineTo(0, 0);
1022 redundantSquare.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001023 redundantSquare.lineTo(SK_Scalar1, 0);
1024 redundantSquare.lineTo(SK_Scalar1, 0);
1025 redundantSquare.lineTo(SK_Scalar1, 0);
1026 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1027 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1028 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1029 redundantSquare.lineTo(0, SK_Scalar1);
1030 redundantSquare.lineTo(0, SK_Scalar1);
1031 redundantSquare.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001032 redundantSquare.close();
1033 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001034 check_direction(reporter, redundantSquare, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001035
reed@google.com7c424812011-05-15 04:38:34 +00001036 SkPath bowTie;
1037 bowTie.moveTo(0, 0);
1038 bowTie.lineTo(0, 0);
1039 bowTie.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001040 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1041 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1042 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1043 bowTie.lineTo(SK_Scalar1, 0);
1044 bowTie.lineTo(SK_Scalar1, 0);
1045 bowTie.lineTo(SK_Scalar1, 0);
1046 bowTie.lineTo(0, SK_Scalar1);
1047 bowTie.lineTo(0, SK_Scalar1);
1048 bowTie.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001049 bowTie.close();
1050 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001051 check_direction(reporter, bowTie, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001052
reed@google.com7c424812011-05-15 04:38:34 +00001053 SkPath spiral;
1054 spiral.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001055 spiral.lineTo(100*SK_Scalar1, 0);
1056 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1057 spiral.lineTo(0, 100*SK_Scalar1);
1058 spiral.lineTo(0, 50*SK_Scalar1);
1059 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1060 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001061 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +00001062 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001063 check_direction(reporter, spiral, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001064
reed@google.com7c424812011-05-15 04:38:34 +00001065 SkPath dent;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001066 dent.moveTo(0, 0);
1067 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1068 dent.lineTo(0, 100*SK_Scalar1);
1069 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1070 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001071 dent.close();
1072 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001073 check_direction(reporter, dent, SkPath::kCW_Direction);
reed@google.com7c424812011-05-15 04:38:34 +00001074}
1075
reed@android.com6b82d1a2009-06-03 02:35:01 +00001076static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1077 const SkRect& bounds) {
1078 REPORTER_ASSERT(reporter, p.isConvex());
1079 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +00001080
reed@android.com6b82d1a2009-06-03 02:35:01 +00001081 SkPath p2(p);
1082 REPORTER_ASSERT(reporter, p2.isConvex());
1083 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1084
1085 SkPath other;
1086 other.swap(p2);
1087 REPORTER_ASSERT(reporter, other.isConvex());
1088 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1089}
1090
reed@google.com04863fa2011-05-15 04:08:24 +00001091static void setFromString(SkPath* path, const char str[]) {
1092 bool first = true;
1093 while (str) {
1094 SkScalar x, y;
1095 str = SkParse::FindScalar(str, &x);
1096 if (NULL == str) {
1097 break;
1098 }
1099 str = SkParse::FindScalar(str, &y);
1100 SkASSERT(str);
1101 if (first) {
1102 path->moveTo(x, y);
1103 first = false;
1104 } else {
1105 path->lineTo(x, y);
1106 }
1107 }
1108}
1109
1110static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +00001111 SkPath path;
1112
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001113 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001114 path.addCircle(0, 0, SkIntToScalar(10));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001115 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001116 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001117 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1118
reed@google.com04863fa2011-05-15 04:08:24 +00001119 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001120 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001121 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.com3e71a882012-01-10 18:44:37 +00001122 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCCW_Direction));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001123
reed@google.com04863fa2011-05-15 04:08:24 +00001124 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001125 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001126 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.com3e71a882012-01-10 18:44:37 +00001127 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCW_Direction));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001128
reed@google.com04863fa2011-05-15 04:08:24 +00001129 static const struct {
1130 const char* fPathStr;
1131 SkPath::Convexity fExpectedConvexity;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001132 SkPath::Direction fExpectedDirection;
reed@google.com04863fa2011-05-15 04:08:24 +00001133 } gRec[] = {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001134 { "", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1135 { "0 0", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1136 { "0 0 10 10", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1137 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPath::kUnknown_Direction },
1138 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPath::kCW_Direction },
1139 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPath::kCCW_Direction },
1140 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
1141 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPath::kCW_Direction },
reed@google.com04863fa2011-05-15 04:08:24 +00001142 };
1143
1144 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1145 SkPath path;
1146 setFromString(&path, gRec[i].fPathStr);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001147 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1148 check_direction(reporter, path, gRec[i].fExpectedDirection);
reed@google.com04863fa2011-05-15 04:08:24 +00001149 }
1150}
1151
reed@google.com7e6c4d12012-05-10 14:05:43 +00001152static void test_isLine(skiatest::Reporter* reporter) {
1153 SkPath path;
1154 SkPoint pts[2];
1155 const SkScalar value = SkIntToScalar(5);
1156
1157 REPORTER_ASSERT(reporter, !path.isLine(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001158
reed@google.com7e6c4d12012-05-10 14:05:43 +00001159 // set some non-zero values
1160 pts[0].set(value, value);
1161 pts[1].set(value, value);
1162 REPORTER_ASSERT(reporter, !path.isLine(pts));
1163 // check that pts was untouched
1164 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1165 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1166
1167 const SkScalar moveX = SkIntToScalar(1);
1168 const SkScalar moveY = SkIntToScalar(2);
1169 SkASSERT(value != moveX && value != moveY);
1170
1171 path.moveTo(moveX, moveY);
1172 REPORTER_ASSERT(reporter, !path.isLine(NULL));
1173 REPORTER_ASSERT(reporter, !path.isLine(pts));
1174 // check that pts was untouched
1175 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1176 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1177
1178 const SkScalar lineX = SkIntToScalar(2);
1179 const SkScalar lineY = SkIntToScalar(2);
1180 SkASSERT(value != lineX && value != lineY);
1181
1182 path.lineTo(lineX, lineY);
1183 REPORTER_ASSERT(reporter, path.isLine(NULL));
1184
1185 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1186 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1187 REPORTER_ASSERT(reporter, path.isLine(pts));
1188 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1189 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1190
1191 path.lineTo(0, 0); // too many points/verbs
1192 REPORTER_ASSERT(reporter, !path.isLine(NULL));
1193 REPORTER_ASSERT(reporter, !path.isLine(pts));
1194 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1195 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1196}
1197
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001198static void test_conservativelyContains(skiatest::Reporter* reporter) {
1199 SkPath path;
1200
1201 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1202 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1203
1204 // A circle that bounds kBaseRect (with a significant amount of slop)
1205 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
1206 circleR = SkScalarMul(circleR, SkFloatToScalar(1.75f)) / 2;
1207 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1208
1209 // round-rect radii
1210 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
skia.committer@gmail.comcec8de62012-11-14 02:01:22 +00001211
caryclark@google.com56f233a2012-11-19 13:06:06 +00001212 static const struct SUPPRESS_VISIBILITY_WARNING {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001213 SkRect fQueryRect;
1214 bool fInRect;
1215 bool fInCircle;
1216 bool fInRR;
1217 } kQueries[] = {
1218 {kBaseRect, true, true, false},
1219
1220 // rect well inside of kBaseRect
1221 {SkRect::MakeLTRB(kBaseRect.fLeft + SkFloatToScalar(0.25f)*kBaseRect.width(),
1222 kBaseRect.fTop + SkFloatToScalar(0.25f)*kBaseRect.height(),
1223 kBaseRect.fRight - SkFloatToScalar(0.25f)*kBaseRect.width(),
1224 kBaseRect.fBottom - SkFloatToScalar(0.25f)*kBaseRect.height()),
1225 true, true, true},
1226
1227 // rects with edges off by one from kBaseRect's edges
1228 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1229 kBaseRect.width(), kBaseRect.height() + 1),
1230 false, true, false},
1231 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1232 kBaseRect.width() + 1, kBaseRect.height()),
1233 false, true, false},
1234 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1235 kBaseRect.width() + 1, kBaseRect.height() + 1),
1236 false, true, false},
1237 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1238 kBaseRect.width(), kBaseRect.height()),
1239 false, true, false},
1240 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1241 kBaseRect.width(), kBaseRect.height()),
1242 false, true, false},
1243 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1244 kBaseRect.width() + 2, kBaseRect.height()),
1245 false, true, false},
1246 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1247 kBaseRect.width() + 2, kBaseRect.height()),
1248 false, true, false},
1249
1250 // zero-w/h rects at each corner of kBaseRect
1251 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false},
1252 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false},
1253 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false},
1254 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false},
1255
1256 // far away rect
1257 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1258 SkIntToScalar(10), SkIntToScalar(10)),
1259 false, false, false},
1260
1261 // very large rect containing kBaseRect
1262 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1263 kBaseRect.fTop - 5 * kBaseRect.height(),
1264 11 * kBaseRect.width(), 11 * kBaseRect.height()),
1265 false, false, false},
1266
1267 // skinny rect that spans same y-range as kBaseRect
1268 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1269 SkIntToScalar(1), kBaseRect.height()),
1270 true, true, true},
1271
1272 // short rect that spans same x-range as kBaseRect
1273 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
1274 true, true, true},
1275
1276 // skinny rect that spans slightly larger y-range than kBaseRect
1277 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1278 SkIntToScalar(1), kBaseRect.height() + 1),
1279 false, true, false},
1280
1281 // short rect that spans slightly larger x-range than kBaseRect
1282 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1283 kBaseRect.width() + 1, SkScalar(1)),
1284 false, true, false},
1285 };
1286
1287 for (int inv = 0; inv < 4; ++inv) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001288 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001289 SkRect qRect = kQueries[q].fQueryRect;
1290 if (inv & 0x1) {
1291 SkTSwap(qRect.fLeft, qRect.fRight);
1292 }
1293 if (inv & 0x2) {
1294 SkTSwap(qRect.fTop, qRect.fBottom);
1295 }
1296 for (int d = 0; d < 2; ++d) {
1297 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1298 path.reset();
1299 path.addRect(kBaseRect, dir);
1300 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1301 path.conservativelyContainsRect(qRect));
1302
1303 path.reset();
1304 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1305 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1306 path.conservativelyContainsRect(qRect));
1307
1308 path.reset();
1309 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1310 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1311 path.conservativelyContainsRect(qRect));
1312 }
1313 // Slightly non-convex shape, shouldn't contain any rects.
1314 path.reset();
1315 path.moveTo(0, 0);
1316 path.lineTo(SkIntToScalar(50), SkFloatToScalar(0.05f));
1317 path.lineTo(SkIntToScalar(100), 0);
1318 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1319 path.lineTo(0, SkIntToScalar(100));
1320 path.close();
1321 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1322 }
1323 }
1324
1325 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1326 path.reset();
1327 path.moveTo(0, 0);
1328 path.lineTo(SkIntToScalar(100), 0);
1329 path.lineTo(0, SkIntToScalar(100));
1330
1331 // inside, on along top edge
1332 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1333 SkIntToScalar(10),
1334 SkIntToScalar(10))));
1335 // above
1336 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1337 SkRect::MakeXYWH(SkIntToScalar(50),
1338 SkIntToScalar(-10),
1339 SkIntToScalar(10),
1340 SkIntToScalar(10))));
1341 // to the left
1342 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1343 SkIntToScalar(5),
1344 SkIntToScalar(5),
1345 SkIntToScalar(5))));
1346
1347 // outside the diagonal edge
1348 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1349 SkIntToScalar(200),
1350 SkIntToScalar(20),
1351 SkIntToScalar(5))));
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001352
1353 // same as above path and first test but with an extra moveTo.
1354 path.reset();
1355 path.moveTo(100, 100);
1356 path.moveTo(0, 0);
1357 path.lineTo(SkIntToScalar(100), 0);
1358 path.lineTo(0, SkIntToScalar(100));
1359
1360 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1361 SkIntToScalar(10),
1362 SkIntToScalar(10))));
1363
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001364}
1365
reed@google.comf32322b2013-10-16 15:14:04 +00001366static void test_isRect_open_close(skiatest::Reporter* reporter) {
1367 SkPath path;
1368 bool isClosed;
1369
1370 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
1371
1372 if (false) {
1373 // I think these should pass, but isRect() doesn't behave
1374 // this way... yet
1375 REPORTER_ASSERT(reporter, path.isRect(NULL, NULL));
1376 REPORTER_ASSERT(reporter, path.isRect(&isClosed, NULL));
1377 REPORTER_ASSERT(reporter, !isClosed);
1378 }
1379
1380 path.close();
1381 REPORTER_ASSERT(reporter, path.isRect(NULL, NULL));
1382 REPORTER_ASSERT(reporter, path.isRect(&isClosed, NULL));
1383 REPORTER_ASSERT(reporter, isClosed);
1384}
1385
caryclark@google.comf1316942011-07-26 19:54:45 +00001386// Simple isRect test is inline TestPath, below.
1387// test_isRect provides more extensive testing.
1388static void test_isRect(skiatest::Reporter* reporter) {
reed@google.comf32322b2013-10-16 15:14:04 +00001389 test_isRect_open_close(reporter);
1390
caryclark@google.comf1316942011-07-26 19:54:45 +00001391 // passing tests (all moveTo / lineTo...
1392 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1393 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1394 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1395 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1396 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1397 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1398 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1399 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1400 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001401 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1402 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 +00001403 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1404 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1405 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
caryclark@google.combfe90372012-11-21 13:56:20 +00001406 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
rmistry@google.comd6176b02012-08-23 18:14:13 +00001407
caryclark@google.comf1316942011-07-26 19:54:45 +00001408 // failing tests
1409 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1410 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1411 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1412 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1413 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1414 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1415 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1416 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
caryclark@google.combfe90372012-11-21 13:56:20 +00001417 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
1418 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
1419 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
rmistry@google.comd6176b02012-08-23 18:14:13 +00001420
caryclark@google.comf1316942011-07-26 19:54:45 +00001421 // failing, no close
1422 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
1423 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
1424
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001425 struct IsRectTest {
1426 SkPoint *fPoints;
1427 size_t fPointCount;
1428 bool fClose;
1429 bool fIsRect;
1430 } tests[] = {
1431 { r1, SK_ARRAY_COUNT(r1), true, true },
1432 { r2, SK_ARRAY_COUNT(r2), true, true },
1433 { r3, SK_ARRAY_COUNT(r3), true, true },
1434 { r4, SK_ARRAY_COUNT(r4), true, true },
1435 { r5, SK_ARRAY_COUNT(r5), true, true },
1436 { r6, SK_ARRAY_COUNT(r6), true, true },
1437 { r7, SK_ARRAY_COUNT(r7), true, true },
1438 { r8, SK_ARRAY_COUNT(r8), true, true },
1439 { r9, SK_ARRAY_COUNT(r9), true, true },
1440 { ra, SK_ARRAY_COUNT(ra), true, true },
1441 { rb, SK_ARRAY_COUNT(rb), true, true },
1442 { rc, SK_ARRAY_COUNT(rc), true, true },
1443 { rd, SK_ARRAY_COUNT(rd), true, true },
1444 { re, SK_ARRAY_COUNT(re), true, true },
1445 { rf, SK_ARRAY_COUNT(rf), true, true },
1446
1447 { f1, SK_ARRAY_COUNT(f1), true, false },
1448 { f2, SK_ARRAY_COUNT(f2), true, false },
1449 { f3, SK_ARRAY_COUNT(f3), true, false },
1450 { f4, SK_ARRAY_COUNT(f4), true, false },
1451 { f5, SK_ARRAY_COUNT(f5), true, false },
1452 { f6, SK_ARRAY_COUNT(f6), true, false },
1453 { f7, SK_ARRAY_COUNT(f7), true, false },
1454 { f8, SK_ARRAY_COUNT(f8), true, false },
1455 { f9, SK_ARRAY_COUNT(f9), true, false },
1456 { fa, SK_ARRAY_COUNT(fa), true, false },
1457 { fb, SK_ARRAY_COUNT(fb), true, false },
1458
1459 { c1, SK_ARRAY_COUNT(c1), false, false },
1460 { c2, SK_ARRAY_COUNT(c2), false, false },
caryclark@google.comf1316942011-07-26 19:54:45 +00001461 };
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001462
1463 const size_t testCount = SK_ARRAY_COUNT(tests);
caryclark@google.comf1316942011-07-26 19:54:45 +00001464 size_t index;
1465 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1466 SkPath path;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001467 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1468 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1469 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.comf1316942011-07-26 19:54:45 +00001470 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001471 if (tests[testIndex].fClose) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001472 path.close();
1473 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001474 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(NULL));
1475 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(NULL, NULL));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001476
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001477 if (tests[testIndex].fIsRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001478 SkRect computed, expected;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001479 expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001480 REPORTER_ASSERT(reporter, path.isRect(&computed));
1481 REPORTER_ASSERT(reporter, expected == computed);
skia.committer@gmail.com1c9c0d32012-11-22 02:02:41 +00001482
caryclark@google.comf68154a2012-11-21 15:18:06 +00001483 bool isClosed;
1484 SkPath::Direction direction, cheapDirection;
1485 REPORTER_ASSERT(reporter, path.cheapComputeDirection(&cheapDirection));
robertphillips@google.com8fd16032013-06-25 15:39:58 +00001486 REPORTER_ASSERT(reporter, path.isRect(&isClosed, &direction));
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001487 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001488 REPORTER_ASSERT(reporter, direction == cheapDirection);
1489 } else {
1490 SkRect computed;
1491 computed.set(123, 456, 789, 1011);
1492 REPORTER_ASSERT(reporter, !path.isRect(&computed));
1493 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
1494 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
1495
1496 bool isClosed = (bool) -1;
1497 SkPath::Direction direction = (SkPath::Direction) -1;
robertphillips@google.com8fd16032013-06-25 15:39:58 +00001498 REPORTER_ASSERT(reporter, !path.isRect(&isClosed, &direction));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001499 REPORTER_ASSERT(reporter, isClosed == (bool) -1);
1500 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001501 }
caryclark@google.comf1316942011-07-26 19:54:45 +00001502 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001503
caryclark@google.comf1316942011-07-26 19:54:45 +00001504 // fail, close then line
1505 SkPath path1;
1506 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001507 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001508 path1.lineTo(r1[index].fX, r1[index].fY);
1509 }
1510 path1.close();
1511 path1.lineTo(1, 0);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001512 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001513
caryclark@google.comf1316942011-07-26 19:54:45 +00001514 // fail, move in the middle
1515 path1.reset();
1516 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001517 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001518 if (index == 2) {
1519 path1.moveTo(1, .5f);
1520 }
1521 path1.lineTo(r1[index].fX, r1[index].fY);
1522 }
1523 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001524 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00001525
1526 // fail, move on the edge
1527 path1.reset();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001528 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001529 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
1530 path1.lineTo(r1[index].fX, r1[index].fY);
1531 }
1532 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001533 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001534
caryclark@google.comf1316942011-07-26 19:54:45 +00001535 // fail, quad
1536 path1.reset();
1537 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001538 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001539 if (index == 2) {
1540 path1.quadTo(1, .5f, 1, .5f);
1541 }
1542 path1.lineTo(r1[index].fX, r1[index].fY);
1543 }
1544 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001545 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001546
caryclark@google.comf1316942011-07-26 19:54:45 +00001547 // fail, cubic
1548 path1.reset();
1549 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001550 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001551 if (index == 2) {
1552 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
1553 }
1554 path1.lineTo(r1[index].fX, r1[index].fY);
1555 }
1556 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001557 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00001558}
1559
caryclark@google.com56f233a2012-11-19 13:06:06 +00001560static void test_isNestedRects(skiatest::Reporter* reporter) {
1561 // passing tests (all moveTo / lineTo...
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001562 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001563 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1564 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1565 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001566 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001567 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1568 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1569 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1570 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001571 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
1572 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 +00001573 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
1574 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
1575 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001576
1577 // failing tests
1578 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1579 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1580 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1581 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1582 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1583 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1584 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1585 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
1586
1587 // failing, no close
1588 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
1589 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
1590
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001591 struct IsNestedRectTest {
1592 SkPoint *fPoints;
1593 size_t fPointCount;
1594 SkPath::Direction fDirection;
1595 bool fClose;
1596 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
1597 } tests[] = {
1598 { r1, SK_ARRAY_COUNT(r1), SkPath::kCW_Direction , true, true },
1599 { r2, SK_ARRAY_COUNT(r2), SkPath::kCW_Direction , true, true },
1600 { r3, SK_ARRAY_COUNT(r3), SkPath::kCW_Direction , true, true },
1601 { r4, SK_ARRAY_COUNT(r4), SkPath::kCW_Direction , true, true },
1602 { r5, SK_ARRAY_COUNT(r5), SkPath::kCCW_Direction, true, true },
1603 { r6, SK_ARRAY_COUNT(r6), SkPath::kCCW_Direction, true, true },
1604 { r7, SK_ARRAY_COUNT(r7), SkPath::kCCW_Direction, true, true },
1605 { r8, SK_ARRAY_COUNT(r8), SkPath::kCCW_Direction, true, true },
1606 { r9, SK_ARRAY_COUNT(r9), SkPath::kCCW_Direction, true, true },
1607 { ra, SK_ARRAY_COUNT(ra), SkPath::kCCW_Direction, true, true },
1608 { rb, SK_ARRAY_COUNT(rb), SkPath::kCW_Direction, true, true },
1609 { rc, SK_ARRAY_COUNT(rc), SkPath::kCW_Direction, true, true },
1610 { rd, SK_ARRAY_COUNT(rd), SkPath::kCCW_Direction, true, true },
1611 { re, SK_ARRAY_COUNT(re), SkPath::kCW_Direction, true, true },
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001612
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001613 { f1, SK_ARRAY_COUNT(f1), SkPath::kUnknown_Direction, true, false },
1614 { f2, SK_ARRAY_COUNT(f2), SkPath::kUnknown_Direction, true, false },
1615 { f3, SK_ARRAY_COUNT(f3), SkPath::kUnknown_Direction, true, false },
1616 { f4, SK_ARRAY_COUNT(f4), SkPath::kUnknown_Direction, true, false },
1617 { f5, SK_ARRAY_COUNT(f5), SkPath::kUnknown_Direction, true, false },
1618 { f6, SK_ARRAY_COUNT(f6), SkPath::kUnknown_Direction, true, false },
1619 { f7, SK_ARRAY_COUNT(f7), SkPath::kUnknown_Direction, true, false },
1620 { f8, SK_ARRAY_COUNT(f8), SkPath::kUnknown_Direction, true, false },
1621
1622 { c1, SK_ARRAY_COUNT(c1), SkPath::kUnknown_Direction, false, false },
1623 { c2, SK_ARRAY_COUNT(c2), SkPath::kUnknown_Direction, false, false },
1624 };
1625
1626 const size_t testCount = SK_ARRAY_COUNT(tests);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001627 size_t index;
1628 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001629 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1630 SkPath path;
1631 if (rectFirst) {
1632 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1633 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001634 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1635 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1636 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001637 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001638 if (tests[testIndex].fClose) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001639 path.close();
1640 }
1641 if (!rectFirst) {
1642 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1643 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001644 REPORTER_ASSERT(reporter, tests[testIndex].fIsNestedRect == path.isNestedRects(NULL));
1645 if (tests[testIndex].fIsNestedRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001646 SkRect expected[2], computed[2];
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001647 SkPath::Direction expectedDirs[2], computedDirs[2];
caryclark@google.com56f233a2012-11-19 13:06:06 +00001648 SkRect testBounds;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001649 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001650 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
1651 expected[1] = testBounds;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001652 if (rectFirst) {
1653 expectedDirs[0] = SkPath::kCW_Direction;
1654 } else {
1655 expectedDirs[0] = SkPath::kCCW_Direction;
1656 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001657 expectedDirs[1] = tests[testIndex].fDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001658 REPORTER_ASSERT(reporter, path.isNestedRects(computed, computedDirs));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001659 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
1660 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001661 REPORTER_ASSERT(reporter, expectedDirs[0] == computedDirs[0]);
1662 REPORTER_ASSERT(reporter, expectedDirs[1] == computedDirs[1]);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001663 }
caryclark@google.com56f233a2012-11-19 13:06:06 +00001664 }
1665
1666 // fail, close then line
1667 SkPath path1;
1668 if (rectFirst) {
1669 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1670 }
1671 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001672 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001673 path1.lineTo(r1[index].fX, r1[index].fY);
1674 }
1675 path1.close();
1676 path1.lineTo(1, 0);
1677 if (!rectFirst) {
1678 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1679 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001680 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001681
1682 // fail, move in the middle
1683 path1.reset();
1684 if (rectFirst) {
1685 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1686 }
1687 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001688 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001689 if (index == 2) {
1690 path1.moveTo(1, .5f);
1691 }
1692 path1.lineTo(r1[index].fX, r1[index].fY);
1693 }
1694 path1.close();
1695 if (!rectFirst) {
1696 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1697 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001698 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001699
1700 // fail, move on the edge
1701 path1.reset();
1702 if (rectFirst) {
1703 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1704 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001705 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001706 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
1707 path1.lineTo(r1[index].fX, r1[index].fY);
1708 }
1709 path1.close();
1710 if (!rectFirst) {
1711 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1712 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001713 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001714
1715 // fail, quad
1716 path1.reset();
1717 if (rectFirst) {
1718 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1719 }
1720 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001721 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001722 if (index == 2) {
1723 path1.quadTo(1, .5f, 1, .5f);
1724 }
1725 path1.lineTo(r1[index].fX, r1[index].fY);
1726 }
1727 path1.close();
1728 if (!rectFirst) {
1729 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1730 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001731 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001732
1733 // fail, cubic
1734 path1.reset();
1735 if (rectFirst) {
1736 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1737 }
1738 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001739 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001740 if (index == 2) {
1741 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
1742 }
1743 path1.lineTo(r1[index].fX, r1[index].fY);
1744 }
1745 path1.close();
1746 if (!rectFirst) {
1747 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1748 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001749 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
skia.committer@gmail.com34587162012-11-20 02:01:23 +00001750
caryclark@google.com56f233a2012-11-19 13:06:06 +00001751 // fail, not nested
1752 path1.reset();
1753 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
1754 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001755 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001756 }
caryclark@google.combfe90372012-11-21 13:56:20 +00001757
1758 // pass, stroke rect
1759 SkPath src, dst;
1760 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
1761 SkPaint strokePaint;
1762 strokePaint.setStyle(SkPaint::kStroke_Style);
1763 strokePaint.setStrokeWidth(2);
1764 strokePaint.getFillPath(src, &dst);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001765 REPORTER_ASSERT(reporter, dst.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001766}
1767
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001768static void write_and_read_back(skiatest::Reporter* reporter,
1769 const SkPath& p) {
1770 SkWriter32 writer(100);
1771 writer.writePath(p);
reed@google.com44699382013-10-31 17:28:30 +00001772 size_t size = writer.bytesWritten();
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001773 SkAutoMalloc storage(size);
1774 writer.flatten(storage.get());
1775 SkReader32 reader(storage.get(), size);
1776
1777 SkPath readBack;
1778 REPORTER_ASSERT(reporter, readBack != p);
1779 reader.readPath(&readBack);
1780 REPORTER_ASSERT(reporter, readBack == p);
1781
rmistry@google.comd6176b02012-08-23 18:14:13 +00001782 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001783 p.getConvexityOrUnknown());
1784
1785 REPORTER_ASSERT(reporter, readBack.isOval(NULL) == p.isOval(NULL));
1786
1787 const SkRect& origBounds = p.getBounds();
1788 const SkRect& readBackBounds = readBack.getBounds();
1789
1790 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
1791}
1792
reed@google.com53effc52011-09-21 19:05:12 +00001793static void test_flattening(skiatest::Reporter* reporter) {
1794 SkPath p;
1795
1796 static const SkPoint pts[] = {
1797 { 0, 0 },
1798 { SkIntToScalar(10), SkIntToScalar(10) },
1799 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
1800 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
1801 };
1802 p.moveTo(pts[0]);
1803 p.lineTo(pts[1]);
1804 p.quadTo(pts[2], pts[3]);
1805 p.cubicTo(pts[4], pts[5], pts[6]);
1806
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001807 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001808
1809 // create a buffer that should be much larger than the path so we don't
1810 // kill our stack if writer goes too far.
1811 char buffer[1024];
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00001812 size_t size1 = p.writeToMemory(NULL);
1813 size_t size2 = p.writeToMemory(buffer);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001814 REPORTER_ASSERT(reporter, size1 == size2);
1815
1816 SkPath p2;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00001817 size_t size3 = p2.readFromMemory(buffer, 1024);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001818 REPORTER_ASSERT(reporter, size1 == size3);
1819 REPORTER_ASSERT(reporter, p == p2);
1820
1821 char buffer2[1024];
1822 size3 = p2.writeToMemory(buffer2);
1823 REPORTER_ASSERT(reporter, size1 == size3);
1824 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001825
1826 // test persistence of the oval flag & convexity
1827 {
1828 SkPath oval;
1829 SkRect rect = SkRect::MakeWH(10, 10);
1830 oval.addOval(rect);
1831
1832 write_and_read_back(reporter, oval);
1833 }
reed@google.com53effc52011-09-21 19:05:12 +00001834}
1835
1836static void test_transform(skiatest::Reporter* reporter) {
1837 SkPath p, p1;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001838
reed@google.com53effc52011-09-21 19:05:12 +00001839 static const SkPoint pts[] = {
1840 { 0, 0 },
1841 { SkIntToScalar(10), SkIntToScalar(10) },
1842 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
1843 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
1844 };
1845 p.moveTo(pts[0]);
1846 p.lineTo(pts[1]);
1847 p.quadTo(pts[2], pts[3]);
1848 p.cubicTo(pts[4], pts[5], pts[6]);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001849
reed@google.com53effc52011-09-21 19:05:12 +00001850 SkMatrix matrix;
1851 matrix.reset();
1852 p.transform(matrix, &p1);
1853 REPORTER_ASSERT(reporter, p == p1);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001854
reed@google.com53effc52011-09-21 19:05:12 +00001855 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
1856 p.transform(matrix, &p1);
1857 SkPoint pts1[7];
1858 int count = p1.getPoints(pts1, 7);
1859 REPORTER_ASSERT(reporter, 7 == count);
1860 for (int i = 0; i < count; ++i) {
1861 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
1862 REPORTER_ASSERT(reporter, newPt == pts1[i]);
1863 }
1864}
1865
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001866static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001867 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00001868 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001869
caryclark@google.com56f233a2012-11-19 13:06:06 +00001870 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
schenney@chromium.org7e963602012-06-13 17:05:43 +00001871 const char* testPath;
1872 const size_t numResultPts;
1873 const SkRect resultBound;
1874 const SkPath::Verb* resultVerbs;
1875 const size_t numResultVerbs;
1876 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001877
schenney@chromium.org7e963602012-06-13 17:05:43 +00001878 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
1879 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
1880 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
1881 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
1882 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
1883 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
1884 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
1885 static const SkPath::Verb resultVerbs8[] = {
1886 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
1887 };
1888 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
1889 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
1890 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
1891 static const SkPath::Verb resultVerbs12[] = {
1892 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
1893 };
1894 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
1895 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
1896 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
1897 static const SkPath::Verb resultVerbs16[] = {
1898 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
1899 };
1900 static const struct zeroPathTestData gZeroLengthTests[] = {
1901 { "M 1 1", 1, {0, 0, 0, 0}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001902 { "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 +00001903 { "M 1 1 z", 1, {0, 0, 0, 0}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001904 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
1905 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
1906 { "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) },
1907 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
1908 { "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) },
1909 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
1910 { "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) },
1911 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
1912 { "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) },
1913 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
1914 { "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 +00001915 SK_ARRAY_COUNT(resultVerbs14)
1916 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001917 { "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) },
1918 { "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 +00001919 SK_ARRAY_COUNT(resultVerbs16)
1920 }
1921 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001922
schenney@chromium.org7e963602012-06-13 17:05:43 +00001923 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
1924 p.reset();
1925 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
1926 REPORTER_ASSERT(reporter, valid);
1927 REPORTER_ASSERT(reporter, !p.isEmpty());
1928 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
1929 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
1930 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
1931 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
1932 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
1933 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00001934 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001935}
1936
1937struct SegmentInfo {
1938 SkPath fPath;
1939 int fPointCount;
1940};
1941
reed@google.com10296cc2011-09-21 12:29:05 +00001942#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
1943
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001944static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00001945 SkPath p, p2;
1946
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001947 p.moveTo(0, 0);
1948 p.quadTo(100, 100, 200, 200);
1949 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
1950 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00001951 p2 = p;
1952 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001953 p.cubicTo(100, 100, 200, 200, 300, 300);
1954 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
1955 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00001956 p2 = p;
1957 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
1958
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001959 p.reset();
1960 p.moveTo(0, 0);
1961 p.cubicTo(100, 100, 200, 200, 300, 300);
1962 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00001963 p2 = p;
1964 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001965
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001966 REPORTER_ASSERT(reporter, !p.isEmpty());
1967}
1968
1969static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00001970 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001971 SkPoint pts[4];
1972
1973 // Test an iterator with no path
1974 SkPath::Iter noPathIter;
1975 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00001976
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001977 // Test that setting an empty path works
1978 noPathIter.setPath(p, false);
1979 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00001980
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001981 // Test that close path makes no difference for an empty path
1982 noPathIter.setPath(p, true);
1983 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00001984
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001985 // Test an iterator with an initial empty path
1986 SkPath::Iter iter(p, false);
1987 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1988
1989 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00001990 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001991 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1992
rmistry@google.comd6176b02012-08-23 18:14:13 +00001993
schenney@chromium.org7e963602012-06-13 17:05:43 +00001994 struct iterTestData {
1995 const char* testPath;
1996 const bool forceClose;
1997 const bool consumeDegenerates;
1998 const size_t* numResultPtsPerVerb;
1999 const SkPoint* resultPts;
2000 const SkPath::Verb* resultVerbs;
2001 const size_t numResultVerbs;
2002 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002003
schenney@chromium.org7e963602012-06-13 17:05:43 +00002004 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2005 static const SkPath::Verb resultVerbs2[] = {
2006 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2007 };
2008 static const SkPath::Verb resultVerbs3[] = {
2009 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2010 };
2011 static const SkPath::Verb resultVerbs4[] = {
2012 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2013 };
2014 static const SkPath::Verb resultVerbs5[] = {
2015 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2016 };
2017 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00002018 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2019 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2020 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2021 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002022 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002023 static const SkPoint resultPts2[] = {
2024 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2025 };
2026 static const SkPoint resultPts3[] = {
2027 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2028 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2029 };
2030 static const SkPoint resultPts4[] = {
2031 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2032 };
2033 static const SkPoint resultPts5[] = {
2034 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2035 };
2036 static const struct iterTestData gIterTests[] = {
2037 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002038 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2039 { "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 +00002040 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2041 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2042 { "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) },
2043 { "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 +00002044 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2045 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2046 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2047 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2048 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2049 { "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 +00002050 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002051
schenney@chromium.org7e963602012-06-13 17:05:43 +00002052 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2053 p.reset();
2054 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2055 REPORTER_ASSERT(reporter, valid);
2056 iter.setPath(p, gIterTests[i].forceClose);
2057 int j = 0, l = 0;
2058 do {
2059 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2060 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2061 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2062 }
2063 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2064 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2065 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002066
2067 // The GM degeneratesegments.cpp test is more extensive
2068}
2069
2070static void test_raw_iter(skiatest::Reporter* reporter) {
2071 SkPath p;
2072 SkPoint pts[4];
2073
2074 // Test an iterator with no path
2075 SkPath::RawIter noPathIter;
2076 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2077 // Test that setting an empty path works
2078 noPathIter.setPath(p);
2079 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00002080
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002081 // Test an iterator with an initial empty path
2082 SkPath::RawIter iter(p);
2083 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2084
2085 // Test that a move-only path returns the move.
2086 p.moveTo(SK_Scalar1, 0);
2087 iter.setPath(p);
2088 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2089 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2090 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2091 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2092
2093 // No matter how many moves we add, we should get them all back
2094 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2095 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2096 iter.setPath(p);
2097 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2098 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2099 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2100 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2101 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2102 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2103 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2104 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2105 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2106 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2107
2108 // Initial close is never ever stored
2109 p.reset();
2110 p.close();
2111 iter.setPath(p);
2112 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2113
2114 // Move/close sequences
2115 p.reset();
2116 p.close(); // Not stored, no purpose
2117 p.moveTo(SK_Scalar1, 0);
2118 p.close();
2119 p.close(); // Not stored, no purpose
2120 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2121 p.close();
2122 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2123 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
2124 p.close();
2125 iter.setPath(p);
2126 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2127 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2128 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2129 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2130 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2131 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2132 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2133 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2134 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2135 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2136 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2137 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2138 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2139 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2140 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2141 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2142 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2143 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2144 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2145 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2146 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2147 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2148
2149 // Generate random paths and verify
2150 SkPoint randomPts[25];
2151 for (int i = 0; i < 5; ++i) {
2152 for (int j = 0; j < 5; ++j) {
2153 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
2154 }
2155 }
2156
2157 // Max of 10 segments, max 3 points per segment
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002158 SkRandom rand(9876543);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002159 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00002160 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002161 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00002162
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002163 for (int i = 0; i < 500; ++i) {
2164 p.reset();
2165 bool lastWasClose = true;
2166 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00002167 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002168 int numPoints = 0;
2169 int numVerbs = (rand.nextU() >> 16) % 10;
2170 int numIterVerbs = 0;
2171 for (int j = 0; j < numVerbs; ++j) {
2172 do {
2173 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
2174 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002175 switch (nextVerb) {
2176 case SkPath::kMove_Verb:
2177 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2178 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00002179 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002180 numPoints += 1;
2181 lastWasClose = false;
2182 haveMoveTo = true;
2183 break;
2184 case SkPath::kLine_Verb:
2185 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002186 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002187 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2188 haveMoveTo = true;
2189 }
2190 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2191 p.lineTo(expectedPts[numPoints]);
2192 numPoints += 1;
2193 lastWasClose = false;
2194 break;
2195 case SkPath::kQuad_Verb:
2196 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002197 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002198 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2199 haveMoveTo = true;
2200 }
2201 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2202 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2203 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
2204 numPoints += 2;
2205 lastWasClose = false;
2206 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002207 case SkPath::kConic_Verb:
2208 if (!haveMoveTo) {
2209 expectedPts[numPoints++] = lastMoveToPt;
2210 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2211 haveMoveTo = true;
2212 }
2213 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2214 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2215 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2216 rand.nextUScalar1() * 4);
2217 numPoints += 2;
2218 lastWasClose = false;
2219 break;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002220 case SkPath::kCubic_Verb:
2221 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002222 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002223 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2224 haveMoveTo = true;
2225 }
2226 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2227 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2228 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
2229 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2230 expectedPts[numPoints + 2]);
2231 numPoints += 3;
2232 lastWasClose = false;
2233 break;
2234 case SkPath::kClose_Verb:
2235 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00002236 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002237 lastWasClose = true;
2238 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002239 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002240 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002241 }
2242 expectedVerbs[numIterVerbs++] = nextVerb;
2243 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00002244
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002245 iter.setPath(p);
2246 numVerbs = numIterVerbs;
2247 numIterVerbs = 0;
2248 int numIterPts = 0;
2249 SkPoint lastMoveTo;
2250 SkPoint lastPt;
2251 lastMoveTo.set(0, 0);
2252 lastPt.set(0, 0);
2253 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
2254 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
2255 numIterVerbs++;
2256 switch (nextVerb) {
2257 case SkPath::kMove_Verb:
2258 REPORTER_ASSERT(reporter, numIterPts < numPoints);
2259 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
2260 lastPt = lastMoveTo = pts[0];
2261 numIterPts += 1;
2262 break;
2263 case SkPath::kLine_Verb:
2264 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
2265 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2266 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2267 lastPt = pts[1];
2268 numIterPts += 1;
2269 break;
2270 case SkPath::kQuad_Verb:
reed@google.com277c3f82013-05-31 15:17:50 +00002271 case SkPath::kConic_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002272 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
2273 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2274 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2275 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2276 lastPt = pts[2];
2277 numIterPts += 2;
2278 break;
2279 case SkPath::kCubic_Verb:
2280 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
2281 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2282 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2283 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2284 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
2285 lastPt = pts[3];
2286 numIterPts += 3;
2287 break;
2288 case SkPath::kClose_Verb:
2289 REPORTER_ASSERT(reporter, pts[0] == lastMoveTo);
2290 lastPt = lastMoveTo;
2291 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002292 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002293 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002294 }
2295 }
2296 REPORTER_ASSERT(reporter, numIterPts == numPoints);
2297 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
2298 }
2299}
2300
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002301static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002302 const SkPath& path,
2303 bool expectedCircle,
2304 SkPath::Direction expectedDir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002305 SkRect rect;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002306 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
2307 REPORTER_ASSERT(reporter, path.cheapIsDirection(expectedDir));
skia.committer@gmail.comfbb0ed92012-11-13 21:46:06 +00002308
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002309 if (expectedCircle) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002310 REPORTER_ASSERT(reporter, rect.height() == rect.width());
2311 }
2312}
2313
2314static void test_circle_skew(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002315 const SkPath& path,
2316 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002317 SkPath tmp;
2318
2319 SkMatrix m;
2320 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
2321 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002322 // this matrix reverses the direction.
2323 if (SkPath::kCCW_Direction == dir) {
2324 dir = SkPath::kCW_Direction;
2325 } else {
2326 SkASSERT(SkPath::kCW_Direction == dir);
2327 dir = SkPath::kCCW_Direction;
2328 }
2329 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002330}
2331
2332static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002333 const SkPath& path,
2334 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002335 SkPath tmp;
2336
2337 // translate at small offset
2338 SkMatrix m;
2339 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
2340 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002341 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002342
2343 tmp.reset();
2344 m.reset();
2345
2346 // translate at a relatively big offset
2347 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
2348 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002349 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002350}
2351
2352static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002353 const SkPath& path,
2354 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002355 for (int angle = 0; angle < 360; ++angle) {
2356 SkPath tmp;
2357 SkMatrix m;
2358 m.setRotate(SkIntToScalar(angle));
2359 path.transform(m, &tmp);
2360
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002361 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002362 // degrees is not an oval anymore, this can be improved. we made this
2363 // for the simplicity of our implementation.
2364 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002365 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002366 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002367 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002368 }
2369 }
2370}
2371
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002372static void test_circle_mirror_x(skiatest::Reporter* reporter,
2373 const SkPath& path,
2374 SkPath::Direction dir) {
2375 SkPath tmp;
2376 SkMatrix m;
2377 m.reset();
2378 m.setScaleX(-SK_Scalar1);
2379 path.transform(m, &tmp);
2380
2381 if (SkPath::kCW_Direction == dir) {
2382 dir = SkPath::kCCW_Direction;
2383 } else {
2384 SkASSERT(SkPath::kCCW_Direction == dir);
2385 dir = SkPath::kCW_Direction;
2386 }
2387
2388 check_for_circle(reporter, tmp, true, dir);
2389}
2390
2391static void test_circle_mirror_y(skiatest::Reporter* reporter,
2392 const SkPath& path,
2393 SkPath::Direction dir) {
2394 SkPath tmp;
2395 SkMatrix m;
2396 m.reset();
2397 m.setScaleY(-SK_Scalar1);
2398 path.transform(m, &tmp);
2399
2400 if (SkPath::kCW_Direction == dir) {
2401 dir = SkPath::kCCW_Direction;
2402 } else {
2403 SkASSERT(SkPath::kCCW_Direction == dir);
2404 dir = SkPath::kCW_Direction;
2405 }
2406
2407 check_for_circle(reporter, tmp, true, dir);
2408}
2409
2410static void test_circle_mirror_xy(skiatest::Reporter* reporter,
2411 const SkPath& path,
2412 SkPath::Direction dir) {
2413 SkPath tmp;
2414 SkMatrix m;
2415 m.reset();
2416 m.setScaleX(-SK_Scalar1);
2417 m.setScaleY(-SK_Scalar1);
2418 path.transform(m, &tmp);
2419
2420 check_for_circle(reporter, tmp, true, dir);
2421}
2422
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002423static void test_circle_with_direction(skiatest::Reporter* reporter,
2424 SkPath::Direction dir) {
2425 SkPath path;
2426
2427 // circle at origin
2428 path.addCircle(0, 0, SkIntToScalar(20), dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002429 check_for_circle(reporter, path, true, dir);
2430 test_circle_rotate(reporter, path, dir);
2431 test_circle_translate(reporter, path, dir);
2432 test_circle_skew(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002433
2434 // circle at an offset at (10, 10)
2435 path.reset();
2436 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
2437 SkIntToScalar(20), dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002438 check_for_circle(reporter, path, true, dir);
2439 test_circle_rotate(reporter, path, dir);
2440 test_circle_translate(reporter, path, dir);
2441 test_circle_skew(reporter, path, dir);
2442 test_circle_mirror_x(reporter, path, dir);
2443 test_circle_mirror_y(reporter, path, dir);
2444 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002445}
2446
2447static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
2448 SkPath path;
2449 SkPath circle;
2450 SkPath rect;
2451 SkPath empty;
2452
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002453 static const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
2454 static const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
2455
2456 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002457 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
2458 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
2459
2460 SkMatrix translate;
2461 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
2462
2463 // For simplicity, all the path concatenation related operations
2464 // would mark it non-circle, though in theory it's still a circle.
2465
2466 // empty + circle (translate)
2467 path = empty;
2468 path.addPath(circle, translate);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002469 check_for_circle(reporter, path, false, kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002470
2471 // circle + empty (translate)
2472 path = circle;
2473 path.addPath(empty, translate);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002474 check_for_circle(reporter, path, false, kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002475
2476 // test reverseAddPath
2477 path = circle;
2478 path.reverseAddPath(rect);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002479 check_for_circle(reporter, path, false, kCircleDirOpposite);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002480}
2481
2482static void test_circle(skiatest::Reporter* reporter) {
2483 test_circle_with_direction(reporter, SkPath::kCW_Direction);
2484 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
2485
2486 // multiple addCircle()
2487 SkPath path;
2488 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2489 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002490 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002491
2492 // some extra lineTo() would make isOval() fail
2493 path.reset();
2494 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2495 path.lineTo(0, 0);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002496 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002497
2498 // not back to the original point
2499 path.reset();
2500 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2501 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002502 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002503
2504 test_circle_with_add_paths(reporter);
2505}
2506
2507static void test_oval(skiatest::Reporter* reporter) {
2508 SkRect rect;
2509 SkMatrix m;
2510 SkPath path;
2511
2512 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
2513 path.addOval(rect);
2514
2515 REPORTER_ASSERT(reporter, path.isOval(NULL));
2516
2517 m.setRotate(SkIntToScalar(90));
2518 SkPath tmp;
2519 path.transform(m, &tmp);
2520 // an oval rotated 90 degrees is still an oval.
2521 REPORTER_ASSERT(reporter, tmp.isOval(NULL));
2522
2523 m.reset();
2524 m.setRotate(SkIntToScalar(30));
2525 tmp.reset();
2526 path.transform(m, &tmp);
2527 // an oval rotated 30 degrees is not an oval anymore.
2528 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2529
2530 // since empty path being transformed.
2531 path.reset();
2532 tmp.reset();
2533 m.reset();
2534 path.transform(m, &tmp);
2535 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2536
2537 // empty path is not an oval
2538 tmp.reset();
2539 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2540
2541 // only has moveTo()s
2542 tmp.reset();
2543 tmp.moveTo(0, 0);
2544 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
2545 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2546
2547 // mimic WebKit's calling convention,
2548 // call moveTo() first and then call addOval()
2549 path.reset();
2550 path.moveTo(0, 0);
2551 path.addOval(rect);
2552 REPORTER_ASSERT(reporter, path.isOval(NULL));
2553
2554 // copy path
2555 path.reset();
2556 tmp.reset();
2557 tmp.addOval(rect);
2558 path = tmp;
2559 REPORTER_ASSERT(reporter, path.isOval(NULL));
2560}
2561
bungeman@google.coma5809a32013-06-21 15:13:34 +00002562static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
2563 SkPath empty;
reed@android.com80e39a72009-04-02 16:59:40 +00002564
reed@android.com3abec1d2009-03-02 05:36:20 +00002565 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002566 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002567 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00002568 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00002569 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00002570 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
2571 REPORTER_ASSERT(reporter, !p.isInverseFillType());
bungeman@google.coma5809a32013-06-21 15:13:34 +00002572 REPORTER_ASSERT(reporter, p == empty);
2573 REPORTER_ASSERT(reporter, !(p != empty));
2574}
2575
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002576static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path) {
2577 REPORTER_ASSERT(reporter, path->isConvex());
2578 path->setConvexity(SkPath::kUnknown_Convexity);
2579 REPORTER_ASSERT(reporter, path->isConvex());
2580 path->reset();
2581}
2582
2583static void test_rrect(skiatest::Reporter* reporter) {
2584 SkPath p;
2585 SkRRect rr;
2586 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
2587 SkRect r = {10, 20, 30, 40};
2588 rr.setRectRadii(r, radii);
2589 p.addRRect(rr);
2590 test_rrect_is_convex(reporter, &p);
2591 p.addRRect(rr, SkPath::kCCW_Direction);
2592 test_rrect_is_convex(reporter, &p);
2593 p.addRoundRect(r, &radii[0].fX);
2594 test_rrect_is_convex(reporter, &p);
2595 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
2596 test_rrect_is_convex(reporter, &p);
2597 p.addRoundRect(r, radii[1].fX, radii[1].fY);
2598 test_rrect_is_convex(reporter, &p);
2599 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
2600 test_rrect_is_convex(reporter, &p);
2601}
2602
bungeman@google.coma5809a32013-06-21 15:13:34 +00002603static void TestPath(skiatest::Reporter* reporter) {
2604 SkTSize<SkScalar>::Make(3,4);
2605
2606 SkPath p, empty;
2607 SkRect bounds, bounds2;
2608 test_empty(reporter, p);
reed@android.com3abec1d2009-03-02 05:36:20 +00002609
reed@android.comd252db02009-04-01 18:31:44 +00002610 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00002611
reed@android.com3abec1d2009-03-02 05:36:20 +00002612 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00002613
reed@android.com6b82d1a2009-06-03 02:35:01 +00002614 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
2615 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00002616 // we have quads or cubics
2617 REPORTER_ASSERT(reporter, p.getSegmentMasks() & kCurveSegmentMask);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002618 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00002619
reed@android.com6b82d1a2009-06-03 02:35:01 +00002620 p.reset();
bungeman@google.coma5809a32013-06-21 15:13:34 +00002621 test_empty(reporter, p);
reed@google.com10296cc2011-09-21 12:29:05 +00002622
reed@android.com6b82d1a2009-06-03 02:35:01 +00002623 p.addOval(bounds);
2624 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002625 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00002626
bungeman@google.coma5809a32013-06-21 15:13:34 +00002627 p.rewind();
2628 test_empty(reporter, p);
2629
reed@android.com3abec1d2009-03-02 05:36:20 +00002630 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00002631 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00002632 // we have only lines
2633 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002634 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00002635
bungeman@google.coma5809a32013-06-21 15:13:34 +00002636 REPORTER_ASSERT(reporter, p != empty);
2637 REPORTER_ASSERT(reporter, !(p == empty));
reed@android.com3abec1d2009-03-02 05:36:20 +00002638
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002639 // do getPoints and getVerbs return the right result
2640 REPORTER_ASSERT(reporter, p.getPoints(NULL, 0) == 4);
2641 REPORTER_ASSERT(reporter, p.getVerbs(NULL, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00002642 SkPoint pts[4];
2643 int count = p.getPoints(pts, 4);
2644 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002645 uint8_t verbs[6];
2646 verbs[5] = 0xff;
2647 p.getVerbs(verbs, 5);
2648 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
2649 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
2650 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
2651 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
2652 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
2653 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00002654 bounds2.set(pts, 4);
2655 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00002656
reed@android.com3abec1d2009-03-02 05:36:20 +00002657 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
2658 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00002659 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00002660
reed@android.com3abec1d2009-03-02 05:36:20 +00002661 REPORTER_ASSERT(reporter, p.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00002662 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00002663 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
2664 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00002665
reed@android.com3abec1d2009-03-02 05:36:20 +00002666 // now force p to not be a rect
2667 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
2668 p.addRect(bounds);
2669 REPORTER_ASSERT(reporter, !p.isRect(NULL));
reed@android.com3abec1d2009-03-02 05:36:20 +00002670
reed@google.com7e6c4d12012-05-10 14:05:43 +00002671 test_isLine(reporter);
2672 test_isRect(reporter);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002673 test_isNestedRects(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002674 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00002675 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00002676 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00002677 test_convexity2(reporter);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00002678 test_conservativelyContains(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00002679 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002680 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00002681 test_flattening(reporter);
2682 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00002683 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002684 test_iter(reporter);
2685 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002686 test_circle(reporter);
2687 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00002688 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00002689 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00002690 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00002691 test_isfinite_after_transform(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00002692 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00002693 test_arb_zero_rad_round_rect_is_rect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00002694 test_addrect_isfinite(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00002695 test_tricky_cubic();
2696 test_clipped_cubic();
2697 test_crbug_170666();
reed@google.com7a90daf2013-04-10 18:44:00 +00002698 test_bad_cubic_crbug229478();
reed@google.com3eff3592013-05-08 21:08:21 +00002699 test_bad_cubic_crbug234190();
mtklein@google.com9c9d4a72013-08-07 19:17:53 +00002700 test_android_specific_behavior(reporter);
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +00002701 test_gen_id(reporter);
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +00002702 test_path_close_issue1474(reporter);
reed@google.comb58ba892013-10-15 15:44:35 +00002703 test_path_to_region(reporter);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00002704 test_rrect(reporter);
reed@android.com3abec1d2009-03-02 05:36:20 +00002705}
2706
2707#include "TestClassDef.h"
2708DEFINE_TESTCLASS("Path", PathTestClass, TestPath)