blob: d879ea6e33e4523e67e296c12fcacd9f6800c739 [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"
reed@android.com60bc6d52010-02-11 11:09:39 +000017#include "SkSize.h"
reed@google.com8cae8352012-09-14 15:18:41 +000018#include "SkSurface.h"
mtklein@google.com9c9d4a72013-08-07 19:17:53 +000019#include "SkTypes.h"
20#include "SkWriter32.h"
reed@google.com8cae8352012-09-14 15:18:41 +000021
reed@google.comcc8be772013-10-15 15:35:29 +000022static void make_path0(SkPath* path) {
23 // from * https://code.google.com/p/skia/issues/detail?id=1706
24
25 path->moveTo(146.939f, 1012.84f);
26 path->lineTo(181.747f, 1009.18f);
27 path->lineTo(182.165f, 1013.16f);
28 path->lineTo(147.357f, 1016.82f);
29 path->lineTo(146.939f, 1012.84f);
30 path->close();
31}
32
33static void make_path1(SkPath* path) {
34 path->addRect(SkRect::MakeXYWH(10, 10, 10, 1));
35}
36
37typedef void (*PathProc)(SkPath*);
38
39/*
40 * Regression test: we used to crash (overwrite internal storage) during
41 * construction of the region when the path was INVERSE. That is now fixed,
42 * so test these regions (which used to assert/crash).
43 *
44 * https://code.google.com/p/skia/issues/detail?id=1706
45 */
46static void test_path_to_region(skiatest::Reporter* reporter) {
47 PathProc procs[] = {
48 make_path0,
49 make_path1,
50 };
skia.committer@gmail.com47262912013-10-16 07:02:24 +000051
reed@google.comcc8be772013-10-15 15:35:29 +000052 SkRegion clip;
53 clip.setRect(0, 0, 1255, 1925);
skia.committer@gmail.com47262912013-10-16 07:02:24 +000054
reed@google.comcc8be772013-10-15 15:35:29 +000055 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
56 SkPath path;
57 procs[i](&path);
skia.committer@gmail.com47262912013-10-16 07:02:24 +000058
reed@google.comcc8be772013-10-15 15:35:29 +000059 SkRegion rgn;
60 rgn.setPath(path, clip);
61 path.toggleInverseFillType();
62 rgn.setPath(path, clip);
63 }
64}
65
caryclark@google.com56f233a2012-11-19 13:06:06 +000066#if defined(WIN32)
67 #define SUPPRESS_VISIBILITY_WARNING
68#else
69 #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
70#endif
71
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +000072static void test_path_close_issue1474(skiatest::Reporter* reporter) {
73 // This test checks that r{Line,Quad,Conic,Cubic}To following a close()
74 // are relative to the point we close to, not relative to the point we close from.
75 SkPath path;
76 SkPoint last;
77
78 // Test rLineTo().
79 path.rLineTo(0, 100);
80 path.rLineTo(100, 0);
81 path.close(); // Returns us back to 0,0.
82 path.rLineTo(50, 50); // This should go to 50,50.
83
84 path.getLastPt(&last);
85 REPORTER_ASSERT(reporter, 50 == last.fX);
86 REPORTER_ASSERT(reporter, 50 == last.fY);
87
88 // Test rQuadTo().
89 path.rewind();
90 path.rLineTo(0, 100);
91 path.rLineTo(100, 0);
92 path.close();
93 path.rQuadTo(50, 50, 75, 75);
94
95 path.getLastPt(&last);
96 REPORTER_ASSERT(reporter, 75 == last.fX);
97 REPORTER_ASSERT(reporter, 75 == last.fY);
98
99 // Test rConicTo().
100 path.rewind();
101 path.rLineTo(0, 100);
102 path.rLineTo(100, 0);
103 path.close();
104 path.rConicTo(50, 50, 85, 85, 2);
105
106 path.getLastPt(&last);
107 REPORTER_ASSERT(reporter, 85 == last.fX);
108 REPORTER_ASSERT(reporter, 85 == last.fY);
109
110 // Test rCubicTo().
111 path.rewind();
112 path.rLineTo(0, 100);
113 path.rLineTo(100, 0);
114 path.close();
115 path.rCubicTo(50, 50, 85, 85, 95, 95);
116
117 path.getLastPt(&last);
118 REPORTER_ASSERT(reporter, 95 == last.fX);
119 REPORTER_ASSERT(reporter, 95 == last.fY);
120}
121
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000122static void test_android_specific_behavior(skiatest::Reporter* reporter) {
123#ifdef SK_BUILD_FOR_ANDROID
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000124 // Make sure we treat fGenerationID and fSourcePath correctly for each of
125 // copy, assign, rewind, reset, and swap.
126 SkPath original, source, anotherSource;
127 original.setSourcePath(&source);
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000128 original.moveTo(0, 0);
129 original.lineTo(1, 1);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000130 REPORTER_ASSERT(reporter, original.getSourcePath() == &source);
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000131
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000132 uint32_t copyID, assignID;
133
134 // Test copy constructor. Copy generation ID, copy source path.
135 SkPath copy(original);
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000136 REPORTER_ASSERT(reporter, copy.getGenerationID() == original.getGenerationID());
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000137 REPORTER_ASSERT(reporter, copy.getSourcePath() == original.getSourcePath());
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000138
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000139 // Test assigment operator. Change generation ID, copy source path.
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000140 SkPath assign;
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000141 assignID = assign.getGenerationID();
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000142 assign = original;
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000143 REPORTER_ASSERT(reporter, assign.getGenerationID() != assignID);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000144 REPORTER_ASSERT(reporter, assign.getSourcePath() == original.getSourcePath());
145
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000146 // Test rewind. Change generation ID, don't touch source path.
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000147 copyID = copy.getGenerationID();
148 copy.rewind();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000149 REPORTER_ASSERT(reporter, copy.getGenerationID() != copyID);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000150 REPORTER_ASSERT(reporter, copy.getSourcePath() == original.getSourcePath());
151
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000152 // Test reset. Change generation ID, don't touch source path.
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000153 assignID = assign.getGenerationID();
154 assign.reset();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000155 REPORTER_ASSERT(reporter, assign.getGenerationID() != assignID);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000156 REPORTER_ASSERT(reporter, assign.getSourcePath() == original.getSourcePath());
157
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000158 // Test swap. Swap the generation IDs, swap source paths.
159 copy.reset();
160 copy.moveTo(2, 2);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000161 copy.setSourcePath(&anotherSource);
162 copyID = copy.getGenerationID();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000163 assign.moveTo(3, 3);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000164 assignID = assign.getGenerationID();
165 copy.swap(assign);
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000166 REPORTER_ASSERT(reporter, copy.getGenerationID() != copyID);
167 REPORTER_ASSERT(reporter, assign.getGenerationID() != assignID);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000168 REPORTER_ASSERT(reporter, copy.getSourcePath() == original.getSourcePath());
169 REPORTER_ASSERT(reporter, assign.getSourcePath() == &anotherSource);
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000170#endif
171}
172
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000173static void test_gen_id(skiatest::Reporter* reporter) {
174 SkPath a, b;
175 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
176
177 a.moveTo(0, 0);
178 const uint32_t z = a.getGenerationID();
179 REPORTER_ASSERT(reporter, z != b.getGenerationID());
180
181 a.reset();
182 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
183
184 a.moveTo(1, 1);
185 const uint32_t y = a.getGenerationID();
186 REPORTER_ASSERT(reporter, z != y);
187
188 b.moveTo(2, 2);
189 const uint32_t x = b.getGenerationID();
190 REPORTER_ASSERT(reporter, x != y && x != z);
191
192 a.swap(b);
193 REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x);
194
195 b = a;
196 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
197
198 SkPath c(a);
199 REPORTER_ASSERT(reporter, c.getGenerationID() == x);
200
201 c.lineTo(3, 3);
202 const uint32_t w = c.getGenerationID();
203 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
204 REPORTER_ASSERT(reporter, a.getGenerationID() == x);
205 REPORTER_ASSERT(reporter, w != x);
206
207#ifdef SK_BUILD_FOR_ANDROID
208 static bool kExpectGenIDToIgnoreFill = false;
209#else
210 static bool kExpectGenIDToIgnoreFill = true;
211#endif
212
213 c.toggleInverseFillType();
214 const uint32_t v = c.getGenerationID();
215 REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill);
216
217 c.rewind();
218 REPORTER_ASSERT(reporter, v != c.getGenerationID());
219}
220
reed@google.com3eff3592013-05-08 21:08:21 +0000221// This used to assert in the debug build, as the edges did not all line-up.
222static void test_bad_cubic_crbug234190() {
223 SkPath path;
224 path.moveTo(13.8509f, 3.16858f);
225 path.cubicTo(-2.35893e+08f, -4.21044e+08f,
226 -2.38991e+08f, -4.26573e+08f,
227 -2.41016e+08f, -4.30188e+08f);
228
229 SkPaint paint;
230 paint.setAntiAlias(true);
reed@google.comd28ba802013-09-20 19:33:52 +0000231 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(84, 88));
reed@google.com3eff3592013-05-08 21:08:21 +0000232 surface->getCanvas()->drawPath(path, paint);
233}
234
reed@google.com7a90daf2013-04-10 18:44:00 +0000235static void test_bad_cubic_crbug229478() {
236 const SkPoint pts[] = {
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000237 { 4595.91064f, -11596.9873f },
238 { 4597.2168f, -11595.9414f },
239 { 4598.52344f, -11594.8955f },
240 { 4599.83008f, -11593.8496f },
reed@google.com7a90daf2013-04-10 18:44:00 +0000241 };
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000242
reed@google.com7a90daf2013-04-10 18:44:00 +0000243 SkPath path;
244 path.moveTo(pts[0]);
245 path.cubicTo(pts[1], pts[2], pts[3]);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000246
reed@google.com7a90daf2013-04-10 18:44:00 +0000247 SkPaint paint;
248 paint.setStyle(SkPaint::kStroke_Style);
249 paint.setStrokeWidth(20);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000250
reed@google.com7a90daf2013-04-10 18:44:00 +0000251 SkPath dst;
252 // Before the fix, this would infinite-recurse, and run out of stack
253 // because we would keep trying to subdivide a degenerate cubic segment.
254 paint.getFillPath(path, &dst, NULL);
255}
256
reed@google.com64d62952013-01-18 17:49:28 +0000257static void build_path_170666(SkPath& path) {
258 path.moveTo(17.9459f, 21.6344f);
259 path.lineTo(139.545f, -47.8105f);
260 path.lineTo(139.545f, -47.8105f);
261 path.lineTo(131.07f, -47.3888f);
262 path.lineTo(131.07f, -47.3888f);
263 path.lineTo(122.586f, -46.9532f);
264 path.lineTo(122.586f, -46.9532f);
265 path.lineTo(18076.6f, 31390.9f);
266 path.lineTo(18076.6f, 31390.9f);
267 path.lineTo(18085.1f, 31390.5f);
268 path.lineTo(18085.1f, 31390.5f);
269 path.lineTo(18076.6f, 31390.9f);
270 path.lineTo(18076.6f, 31390.9f);
271 path.lineTo(17955, 31460.3f);
272 path.lineTo(17955, 31460.3f);
273 path.lineTo(17963.5f, 31459.9f);
274 path.lineTo(17963.5f, 31459.9f);
275 path.lineTo(17971.9f, 31459.5f);
276 path.lineTo(17971.9f, 31459.5f);
277 path.lineTo(17.9551f, 21.6205f);
278 path.lineTo(17.9551f, 21.6205f);
279 path.lineTo(9.47091f, 22.0561f);
280 path.lineTo(9.47091f, 22.0561f);
281 path.lineTo(17.9459f, 21.6344f);
282 path.lineTo(17.9459f, 21.6344f);
283 path.close();path.moveTo(0.995934f, 22.4779f);
284 path.lineTo(0.986725f, 22.4918f);
285 path.lineTo(0.986725f, 22.4918f);
286 path.lineTo(17955, 31460.4f);
287 path.lineTo(17955, 31460.4f);
288 path.lineTo(17971.9f, 31459.5f);
289 path.lineTo(17971.9f, 31459.5f);
290 path.lineTo(18093.6f, 31390.1f);
291 path.lineTo(18093.6f, 31390.1f);
292 path.lineTo(18093.6f, 31390);
293 path.lineTo(18093.6f, 31390);
294 path.lineTo(139.555f, -47.8244f);
295 path.lineTo(139.555f, -47.8244f);
296 path.lineTo(122.595f, -46.9671f);
297 path.lineTo(122.595f, -46.9671f);
298 path.lineTo(0.995934f, 22.4779f);
299 path.lineTo(0.995934f, 22.4779f);
300 path.close();
301 path.moveTo(5.43941f, 25.5223f);
302 path.lineTo(798267, -28871.1f);
303 path.lineTo(798267, -28871.1f);
304 path.lineTo(3.12512e+06f, -113102);
305 path.lineTo(3.12512e+06f, -113102);
306 path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813);
307 path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f);
308 path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f);
309 path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f);
310 path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f);
311 path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f);
312 path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f);
313 path.lineTo(2.78271e+08f, -1.00733e+07f);
314 path.lineTo(2.78271e+08f, -1.00733e+07f);
315 path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f);
316 path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f);
317 path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f);
318 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
319 path.lineTo(2.77473e+08f, -1.00444e+07f);
320 path.lineTo(2.77473e+08f, -1.00444e+07f);
321 path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f);
322 path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f);
323 path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f);
324 path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f);
325 path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f);
326 path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814);
327 path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103);
328 path.lineTo(798284, -28872);
329 path.lineTo(798284, -28872);
330 path.lineTo(22.4044f, 24.6677f);
331 path.lineTo(22.4044f, 24.6677f);
332 path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f);
333 path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f);
334 path.close();
335}
336
337static void build_path_simple_170666(SkPath& path) {
338 path.moveTo(126.677f, 24.1591f);
339 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
340}
341
342// This used to assert in the SK_DEBUG build, as the clip step would fail with
343// too-few interations in our cubic-line intersection code. That code now runs
344// 24 interations (instead of 16).
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000345static void test_crbug_170666() {
reed@google.com64d62952013-01-18 17:49:28 +0000346 SkPath path;
347 SkPaint paint;
348 paint.setAntiAlias(true);
349
reed@google.comd28ba802013-09-20 19:33:52 +0000350 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(1000, 1000));
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000351
reed@google.com64d62952013-01-18 17:49:28 +0000352 build_path_simple_170666(path);
353 surface->getCanvas()->drawPath(path, paint);
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000354
reed@google.com64d62952013-01-18 17:49:28 +0000355 build_path_170666(path);
356 surface->getCanvas()->drawPath(path, paint);
357}
358
reed@google.coma8790de2012-10-24 21:04:04 +0000359// Make sure we stay non-finite once we get there (unless we reset or rewind).
360static void test_addrect_isfinite(skiatest::Reporter* reporter) {
361 SkPath path;
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000362
reed@google.coma8790de2012-10-24 21:04:04 +0000363 path.addRect(SkRect::MakeWH(50, 100));
364 REPORTER_ASSERT(reporter, path.isFinite());
365
366 path.moveTo(0, 0);
367 path.lineTo(SK_ScalarInfinity, 42);
368 REPORTER_ASSERT(reporter, !path.isFinite());
369
370 path.addRect(SkRect::MakeWH(50, 100));
371 REPORTER_ASSERT(reporter, !path.isFinite());
372
373 path.reset();
374 REPORTER_ASSERT(reporter, path.isFinite());
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000375
reed@google.coma8790de2012-10-24 21:04:04 +0000376 path.addRect(SkRect::MakeWH(50, 100));
377 REPORTER_ASSERT(reporter, path.isFinite());
378}
379
reed@google.com848148e2013-01-15 15:51:59 +0000380static void build_big_path(SkPath* path, bool reducedCase) {
381 if (reducedCase) {
382 path->moveTo(577330, 1971.72f);
383 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
384 } else {
385 path->moveTo(60.1631f, 7.70567f);
386 path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
387 path->lineTo(577379, 1977.77f);
388 path->quadTo(577364, 1979.57f, 577325, 1980.26f);
389 path->quadTo(577286, 1980.95f, 577245, 1980.13f);
390 path->quadTo(577205, 1979.3f, 577187, 1977.45f);
391 path->quadTo(577168, 1975.6f, 577183, 1973.8f);
392 path->quadTo(577198, 1972, 577238, 1971.31f);
393 path->quadTo(577277, 1970.62f, 577317, 1971.45f);
394 path->quadTo(577330, 1971.72f, 577341, 1972.11f);
395 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
396 path->moveTo(306.718f, -32.912f);
397 path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
398 }
399}
400
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000401static void test_clipped_cubic() {
reed@google.comd28ba802013-09-20 19:33:52 +0000402 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(640, 480));
reed@google.com848148e2013-01-15 15:51:59 +0000403
404 // This path used to assert, because our cubic-chopping code incorrectly
405 // moved control points after the chop. This test should be run in SK_DEBUG
406 // mode to ensure that we no long assert.
407 SkPath path;
408 for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
409 build_big_path(&path, SkToBool(doReducedCase));
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +0000410
reed@google.com848148e2013-01-15 15:51:59 +0000411 SkPaint paint;
412 for (int doAA = 0; doAA <= 1; ++doAA) {
413 paint.setAntiAlias(SkToBool(doAA));
414 surface->getCanvas()->drawPath(path, paint);
415 }
416 }
417}
418
reed@google.com8cae8352012-09-14 15:18:41 +0000419// Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
420// which triggered an assert, from a tricky cubic. This test replicates that
421// example, so we can ensure that we handle it (in SkEdge.cpp), and don't
422// assert in the SK_DEBUG build.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000423static void test_tricky_cubic() {
reed@google.com8cae8352012-09-14 15:18:41 +0000424 const SkPoint pts[] = {
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +0000425 { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
426 { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
427 { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
428 { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
reed@google.com8cae8352012-09-14 15:18:41 +0000429 };
430
431 SkPath path;
432 path.moveTo(pts[0]);
433 path.cubicTo(pts[1], pts[2], pts[3]);
434
435 SkPaint paint;
436 paint.setAntiAlias(true);
437
reed@google.comd28ba802013-09-20 19:33:52 +0000438 SkSurface* surface = SkSurface::NewRasterPMColor(19, 130);
reed@google.com8cae8352012-09-14 15:18:41 +0000439 surface->getCanvas()->drawPath(path, paint);
440 surface->unref();
441}
reed@android.com3abec1d2009-03-02 05:36:20 +0000442
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000443// Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
444//
445static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
446 SkPath path;
447 path.quadTo(157, 366, 286, 208);
448 path.arcTo(37, 442, 315, 163, 957494590897113.0f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000449
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000450 SkMatrix matrix;
451 matrix.setScale(1000*1000, 1000*1000);
452
453 // Be sure that path::transform correctly updates isFinite and the bounds
454 // if the transformation overflows. The previous bug was that isFinite was
455 // set to true in this case, but the bounds were not set to empty (which
456 // they should be).
457 while (path.isFinite()) {
458 REPORTER_ASSERT(reporter, path.getBounds().isFinite());
459 REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
460 path.transform(matrix);
461 }
462 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
463
464 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
465 path.transform(matrix);
466 // we need to still be non-finite
467 REPORTER_ASSERT(reporter, !path.isFinite());
468 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
469}
470
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000471static void add_corner_arc(SkPath* path, const SkRect& rect,
472 SkScalar xIn, SkScalar yIn,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000473 int startAngle)
474{
475
476 SkScalar rx = SkMinScalar(rect.width(), xIn);
477 SkScalar ry = SkMinScalar(rect.height(), yIn);
478
479 SkRect arcRect;
480 arcRect.set(-rx, -ry, rx, ry);
481 switch (startAngle) {
482 case 0:
483 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
484 break;
485 case 90:
486 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
487 break;
488 case 180:
489 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
490 break;
491 case 270:
492 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
493 break;
494 default:
495 break;
496 }
497
498 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
499}
500
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000501static void make_arb_round_rect(SkPath* path, const SkRect& r,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000502 SkScalar xCorner, SkScalar yCorner) {
503 // we are lazy here and use the same x & y for each corner
504 add_corner_arc(path, r, xCorner, yCorner, 270);
505 add_corner_arc(path, r, xCorner, yCorner, 0);
506 add_corner_arc(path, r, xCorner, yCorner, 90);
507 add_corner_arc(path, r, xCorner, yCorner, 180);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000508 path->close();
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000509}
510
511// Chrome creates its own round rects with each corner possibly being different.
512// Performance will suffer if they are not convex.
513// Note: PathBench::ArbRoundRectBench performs almost exactly
514// the same test (but with drawing)
515static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000516 SkRandom rand;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000517 SkRect r;
518
519 for (int i = 0; i < 5000; ++i) {
520
robertphillips@google.com158618e2012-10-23 16:56:56 +0000521 SkScalar size = rand.nextUScalar1() * 30;
522 if (size < SK_Scalar1) {
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000523 continue;
524 }
525 r.fLeft = rand.nextUScalar1() * 300;
526 r.fTop = rand.nextUScalar1() * 300;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000527 r.fRight = r.fLeft + 2 * size;
528 r.fBottom = r.fTop + 2 * size;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000529
530 SkPath temp;
531
532 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
533
534 REPORTER_ASSERT(reporter, temp.isConvex());
535 }
536}
537
robertphillips@google.com158618e2012-10-23 16:56:56 +0000538// Chrome will sometimes create a 0 radius round rect. The degenerate
539// quads prevent the path from being converted to a rect
540// Note: PathBench::ArbRoundRectBench performs almost exactly
541// the same test (but with drawing)
542static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000543 SkRandom rand;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000544 SkRect r;
545
546 for (int i = 0; i < 5000; ++i) {
547
548 SkScalar size = rand.nextUScalar1() * 30;
549 if (size < SK_Scalar1) {
550 continue;
551 }
552 r.fLeft = rand.nextUScalar1() * 300;
553 r.fTop = rand.nextUScalar1() * 300;
554 r.fRight = r.fLeft + 2 * size;
555 r.fBottom = r.fTop + 2 * size;
556
557 SkPath temp;
558
559 make_arb_round_rect(&temp, r, 0, 0);
560
robertphillips@google.com158618e2012-10-23 16:56:56 +0000561 SkRect result;
562 REPORTER_ASSERT(reporter, temp.isRect(&result));
563 REPORTER_ASSERT(reporter, r == result);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000564 }
565}
566
reed@google.com0bb18bb2012-07-26 15:20:36 +0000567static void test_rect_isfinite(skiatest::Reporter* reporter) {
568 const SkScalar inf = SK_ScalarInfinity;
caryclark@google.com7abfa492013-04-12 11:59:41 +0000569 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000570 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000571
reed@google.com0bb18bb2012-07-26 15:20:36 +0000572 SkRect r;
573 r.setEmpty();
574 REPORTER_ASSERT(reporter, r.isFinite());
caryclark@google.com7abfa492013-04-12 11:59:41 +0000575 r.set(0, 0, inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000576 REPORTER_ASSERT(reporter, !r.isFinite());
577 r.set(0, 0, nan, 0);
578 REPORTER_ASSERT(reporter, !r.isFinite());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000579
reed@google.com0bb18bb2012-07-26 15:20:36 +0000580 SkPoint pts[] = {
581 { 0, 0 },
582 { SK_Scalar1, 0 },
583 { 0, SK_Scalar1 },
584 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000585
reed@google.com0bb18bb2012-07-26 15:20:36 +0000586 bool isFine = r.setBoundsCheck(pts, 3);
587 REPORTER_ASSERT(reporter, isFine);
588 REPORTER_ASSERT(reporter, !r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000589
reed@google.com0bb18bb2012-07-26 15:20:36 +0000590 pts[1].set(inf, 0);
591 isFine = r.setBoundsCheck(pts, 3);
592 REPORTER_ASSERT(reporter, !isFine);
593 REPORTER_ASSERT(reporter, r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000594
reed@google.com0bb18bb2012-07-26 15:20:36 +0000595 pts[1].set(nan, 0);
596 isFine = r.setBoundsCheck(pts, 3);
597 REPORTER_ASSERT(reporter, !isFine);
598 REPORTER_ASSERT(reporter, r.isEmpty());
599}
600
601static void test_path_isfinite(skiatest::Reporter* reporter) {
602 const SkScalar inf = SK_ScalarInfinity;
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000603 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000604 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000605
reed@google.com0bb18bb2012-07-26 15:20:36 +0000606 SkPath path;
607 REPORTER_ASSERT(reporter, path.isFinite());
608
609 path.reset();
610 REPORTER_ASSERT(reporter, path.isFinite());
611
612 path.reset();
613 path.moveTo(SK_Scalar1, 0);
614 REPORTER_ASSERT(reporter, path.isFinite());
615
616 path.reset();
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000617 path.moveTo(inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000618 REPORTER_ASSERT(reporter, !path.isFinite());
619
620 path.reset();
621 path.moveTo(nan, 0);
622 REPORTER_ASSERT(reporter, !path.isFinite());
623}
624
625static void test_isfinite(skiatest::Reporter* reporter) {
626 test_rect_isfinite(reporter);
627 test_path_isfinite(reporter);
628}
629
reed@google.com744faba2012-05-29 19:54:52 +0000630// assert that we always
631// start with a moveTo
632// only have 1 moveTo
633// only have Lines after that
634// end with a single close
635// only have (at most) 1 close
636//
637static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000638 const SkPoint srcPts[], bool expectClose) {
reed@google.com744faba2012-05-29 19:54:52 +0000639 SkPath::RawIter iter(path);
640 SkPoint pts[4];
reed@google.com744faba2012-05-29 19:54:52 +0000641
642 bool firstTime = true;
643 bool foundClose = false;
644 for (;;) {
645 switch (iter.next(pts)) {
646 case SkPath::kMove_Verb:
647 REPORTER_ASSERT(reporter, firstTime);
648 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
649 srcPts++;
650 firstTime = false;
651 break;
652 case SkPath::kLine_Verb:
653 REPORTER_ASSERT(reporter, !firstTime);
654 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
655 srcPts++;
656 break;
657 case SkPath::kQuad_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000658 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb");
reed@google.com744faba2012-05-29 19:54:52 +0000659 break;
reed@google.com277c3f82013-05-31 15:17:50 +0000660 case SkPath::kConic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000661 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb");
reed@google.com277c3f82013-05-31 15:17:50 +0000662 break;
reed@google.com744faba2012-05-29 19:54:52 +0000663 case SkPath::kCubic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000664 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb");
reed@google.com744faba2012-05-29 19:54:52 +0000665 break;
666 case SkPath::kClose_Verb:
667 REPORTER_ASSERT(reporter, !firstTime);
668 REPORTER_ASSERT(reporter, !foundClose);
669 REPORTER_ASSERT(reporter, expectClose);
670 foundClose = true;
671 break;
672 case SkPath::kDone_Verb:
673 goto DONE;
674 }
675 }
676DONE:
677 REPORTER_ASSERT(reporter, foundClose == expectClose);
678}
679
680static void test_addPoly(skiatest::Reporter* reporter) {
681 SkPoint pts[32];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000682 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000683
reed@google.com744faba2012-05-29 19:54:52 +0000684 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
685 pts[i].fX = rand.nextSScalar1();
686 pts[i].fY = rand.nextSScalar1();
687 }
688
689 for (int doClose = 0; doClose <= 1; ++doClose) {
690 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
691 SkPath path;
692 path.addPoly(pts, count, SkToBool(doClose));
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000693 test_poly(reporter, path, pts, SkToBool(doClose));
reed@google.com744faba2012-05-29 19:54:52 +0000694 }
695 }
696}
697
reed@google.com8b06f1a2012-05-29 12:03:46 +0000698static void test_strokerec(skiatest::Reporter* reporter) {
699 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
700 REPORTER_ASSERT(reporter, rec.isFillStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000701
reed@google.com8b06f1a2012-05-29 12:03:46 +0000702 rec.setHairlineStyle();
703 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000704
reed@google.com8b06f1a2012-05-29 12:03:46 +0000705 rec.setStrokeStyle(SK_Scalar1, false);
706 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000707
reed@google.com8b06f1a2012-05-29 12:03:46 +0000708 rec.setStrokeStyle(SK_Scalar1, true);
709 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000710
reed@google.com8b06f1a2012-05-29 12:03:46 +0000711 rec.setStrokeStyle(0, false);
712 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000713
reed@google.com8b06f1a2012-05-29 12:03:46 +0000714 rec.setStrokeStyle(0, true);
715 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
716}
717
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000718// Set this for paths that don't have a consistent direction such as a bowtie.
719// (cheapComputeDirection is not expected to catch these.)
720static const SkPath::Direction kDontCheckDir = static_cast<SkPath::Direction>(-1);
721
722static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
723 SkPath::Direction expected) {
724 if (expected == kDontCheckDir) {
725 return;
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000726 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000727 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
728
729 SkPath::Direction dir;
730 if (copy.cheapComputeDirection(&dir)) {
731 REPORTER_ASSERT(reporter, dir == expected);
732 } else {
733 REPORTER_ASSERT(reporter, SkPath::kUnknown_Direction == expected);
734 }
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000735}
736
reed@google.com3e71a882012-01-10 18:44:37 +0000737static void test_direction(skiatest::Reporter* reporter) {
738 size_t i;
739 SkPath path;
740 REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL));
741 REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCW_Direction));
742 REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCCW_Direction));
reed@google.coma8a3b3d2012-11-26 18:16:27 +0000743 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kUnknown_Direction));
reed@google.com3e71a882012-01-10 18:44:37 +0000744
745 static const char* gDegen[] = {
746 "M 10 10",
747 "M 10 10 M 20 20",
748 "M 10 10 L 20 20",
749 "M 10 10 L 10 10 L 10 10",
750 "M 10 10 Q 10 10 10 10",
751 "M 10 10 C 10 10 10 10 10 10",
752 };
753 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
754 path.reset();
755 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
756 REPORTER_ASSERT(reporter, valid);
757 REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL));
758 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000759
reed@google.com3e71a882012-01-10 18:44:37 +0000760 static const char* gCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000761 "M 10 10 L 10 10 Q 20 10 20 20",
reed@google.com3e71a882012-01-10 18:44:37 +0000762 "M 10 10 C 20 10 20 20 20 20",
reed@google.comd4146662012-01-31 15:42:29 +0000763 "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 +0000764 // rect with top two corners replaced by cubics with identical middle
765 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000766 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
767 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000768 };
769 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
770 path.reset();
771 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
772 REPORTER_ASSERT(reporter, valid);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000773 check_direction(reporter, path, SkPath::kCW_Direction);
reed@google.com3e71a882012-01-10 18:44:37 +0000774 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000775
reed@google.com3e71a882012-01-10 18:44:37 +0000776 static const char* gCCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000777 "M 10 10 L 10 10 Q 20 10 20 -20",
reed@google.com3e71a882012-01-10 18:44:37 +0000778 "M 10 10 C 20 10 20 -20 20 -20",
reed@google.comd4146662012-01-31 15:42:29 +0000779 "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 +0000780 // rect with top two corners replaced by cubics with identical middle
781 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000782 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
783 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000784 };
785 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
786 path.reset();
787 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
788 REPORTER_ASSERT(reporter, valid);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000789 check_direction(reporter, path, SkPath::kCCW_Direction);
reed@google.com3e71a882012-01-10 18:44:37 +0000790 }
reed@google.comac8543f2012-01-30 20:51:25 +0000791
792 // Test two donuts, each wound a different direction. Only the outer contour
793 // determines the cheap direction
794 path.reset();
795 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
796 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000797 check_direction(reporter, path, SkPath::kCW_Direction);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000798
reed@google.comac8543f2012-01-30 20:51:25 +0000799 path.reset();
800 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
801 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000802 check_direction(reporter, path, SkPath::kCCW_Direction);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000803
bsalomon@google.com6843ac42012-02-17 13:49:03 +0000804#ifdef SK_SCALAR_IS_FLOAT
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000805 // triangle with one point really far from the origin.
806 path.reset();
807 // the first point is roughly 1.05e10, 1.05e10
bsalomon@google.com53aab782012-02-23 14:54:49 +0000808 path.moveTo(SkFloatToScalar(SkBits2Float(0x501c7652)), SkFloatToScalar(SkBits2Float(0x501c7652)));
809 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
810 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000811 check_direction(reporter, path, SkPath::kCCW_Direction);
bsalomon@google.com53aab782012-02-23 14:54:49 +0000812#endif
reed@google.com3e71a882012-01-10 18:44:37 +0000813}
814
reed@google.comffdb0182011-11-14 19:29:14 +0000815static void add_rect(SkPath* path, const SkRect& r) {
816 path->moveTo(r.fLeft, r.fTop);
817 path->lineTo(r.fRight, r.fTop);
818 path->lineTo(r.fRight, r.fBottom);
819 path->lineTo(r.fLeft, r.fBottom);
820 path->close();
821}
822
823static void test_bounds(skiatest::Reporter* reporter) {
824 static const SkRect rects[] = {
reed@google.com3563c9e2011-11-14 19:34:57 +0000825 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
826 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
827 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
828 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
reed@google.comffdb0182011-11-14 19:29:14 +0000829 };
830
831 SkPath path0, path1;
832 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
833 path0.addRect(rects[i]);
834 add_rect(&path1, rects[i]);
835 }
836
837 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
838}
839
reed@google.com55b5f4b2011-09-07 12:23:41 +0000840static void stroke_cubic(const SkPoint pts[4]) {
841 SkPath path;
842 path.moveTo(pts[0]);
843 path.cubicTo(pts[1], pts[2], pts[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000844
reed@google.com55b5f4b2011-09-07 12:23:41 +0000845 SkPaint paint;
846 paint.setStyle(SkPaint::kStroke_Style);
847 paint.setStrokeWidth(SK_Scalar1 * 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000848
reed@google.com55b5f4b2011-09-07 12:23:41 +0000849 SkPath fill;
850 paint.getFillPath(path, &fill);
851}
852
853// just ensure this can run w/o any SkASSERTS firing in the debug build
854// we used to assert due to differences in how we determine a degenerate vector
855// but that was fixed with the introduction of SkPoint::CanNormalize
856static void stroke_tiny_cubic() {
857 SkPoint p0[] = {
858 { 372.0f, 92.0f },
859 { 372.0f, 92.0f },
860 { 372.0f, 92.0f },
861 { 372.0f, 92.0f },
862 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000863
reed@google.com55b5f4b2011-09-07 12:23:41 +0000864 stroke_cubic(p0);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000865
reed@google.com55b5f4b2011-09-07 12:23:41 +0000866 SkPoint p1[] = {
867 { 372.0f, 92.0f },
868 { 372.0007f, 92.000755f },
869 { 371.99927f, 92.003922f },
870 { 371.99826f, 92.003899f },
871 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000872
reed@google.com55b5f4b2011-09-07 12:23:41 +0000873 stroke_cubic(p1);
874}
875
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000876static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
877 for (int i = 0; i < 2; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +0000878 SkPath::Iter iter(path, SkToBool(i));
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000879 SkPoint mv;
880 SkPoint pts[4];
881 SkPath::Verb v;
882 int nMT = 0;
883 int nCL = 0;
tomhudson@google.com221db3c2011-07-28 21:10:29 +0000884 mv.set(0, 0);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000885 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
886 switch (v) {
887 case SkPath::kMove_Verb:
888 mv = pts[0];
889 ++nMT;
890 break;
891 case SkPath::kClose_Verb:
892 REPORTER_ASSERT(reporter, mv == pts[0]);
893 ++nCL;
894 break;
895 default:
896 break;
897 }
898 }
899 // if we force a close on the interator we should have a close
900 // for every moveTo
901 REPORTER_ASSERT(reporter, !i || nMT == nCL);
902 }
903}
904
905static void test_close(skiatest::Reporter* reporter) {
906 SkPath closePt;
907 closePt.moveTo(0, 0);
908 closePt.close();
909 check_close(reporter, closePt);
910
911 SkPath openPt;
912 openPt.moveTo(0, 0);
913 check_close(reporter, openPt);
914
915 SkPath empty;
916 check_close(reporter, empty);
917 empty.close();
918 check_close(reporter, empty);
919
920 SkPath rect;
921 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
922 check_close(reporter, rect);
923 rect.close();
924 check_close(reporter, rect);
925
926 SkPath quad;
927 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
928 check_close(reporter, quad);
929 quad.close();
930 check_close(reporter, quad);
931
932 SkPath cubic;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000933 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000934 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
935 check_close(reporter, cubic);
936 cubic.close();
937 check_close(reporter, cubic);
938
939 SkPath line;
940 line.moveTo(SK_Scalar1, SK_Scalar1);
941 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
942 check_close(reporter, line);
943 line.close();
944 check_close(reporter, line);
945
946 SkPath rect2;
947 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
948 rect2.close();
949 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
950 check_close(reporter, rect2);
951 rect2.close();
952 check_close(reporter, rect2);
953
954 SkPath oval3;
955 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
956 oval3.close();
957 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
958 check_close(reporter, oval3);
959 oval3.close();
960 check_close(reporter, oval3);
961
962 SkPath moves;
963 moves.moveTo(SK_Scalar1, SK_Scalar1);
964 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
965 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
966 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
967 check_close(reporter, moves);
reed@google.com55b5f4b2011-09-07 12:23:41 +0000968
969 stroke_tiny_cubic();
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000970}
971
reed@google.com7c424812011-05-15 04:38:34 +0000972static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
973 SkPath::Convexity expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000974 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
975 SkPath::Convexity c = copy.getConvexity();
reed@google.com7c424812011-05-15 04:38:34 +0000976 REPORTER_ASSERT(reporter, c == expected);
977}
978
979static void test_convexity2(skiatest::Reporter* reporter) {
980 SkPath pt;
981 pt.moveTo(0, 0);
982 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +0000983 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000984 check_direction(reporter, pt, SkPath::kUnknown_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000985
reed@google.com7c424812011-05-15 04:38:34 +0000986 SkPath line;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000987 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
988 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000989 line.close();
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000990 check_convexity(reporter, line, SkPath::kConvex_Convexity);
991 check_direction(reporter, line, SkPath::kUnknown_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000992
reed@google.com7c424812011-05-15 04:38:34 +0000993 SkPath triLeft;
994 triLeft.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000995 triLeft.lineTo(SK_Scalar1, 0);
996 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000997 triLeft.close();
998 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000999 check_direction(reporter, triLeft, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001000
reed@google.com7c424812011-05-15 04:38:34 +00001001 SkPath triRight;
1002 triRight.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001003 triRight.lineTo(-SK_Scalar1, 0);
1004 triRight.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001005 triRight.close();
1006 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001007 check_direction(reporter, triRight, SkPath::kCCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001008
reed@google.com7c424812011-05-15 04:38:34 +00001009 SkPath square;
1010 square.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001011 square.lineTo(SK_Scalar1, 0);
1012 square.lineTo(SK_Scalar1, SK_Scalar1);
1013 square.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001014 square.close();
1015 check_convexity(reporter, square, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001016 check_direction(reporter, square, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001017
reed@google.com7c424812011-05-15 04:38:34 +00001018 SkPath redundantSquare;
1019 redundantSquare.moveTo(0, 0);
1020 redundantSquare.lineTo(0, 0);
1021 redundantSquare.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001022 redundantSquare.lineTo(SK_Scalar1, 0);
1023 redundantSquare.lineTo(SK_Scalar1, 0);
1024 redundantSquare.lineTo(SK_Scalar1, 0);
1025 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1026 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1027 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1028 redundantSquare.lineTo(0, SK_Scalar1);
1029 redundantSquare.lineTo(0, SK_Scalar1);
1030 redundantSquare.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001031 redundantSquare.close();
1032 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001033 check_direction(reporter, redundantSquare, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001034
reed@google.com7c424812011-05-15 04:38:34 +00001035 SkPath bowTie;
1036 bowTie.moveTo(0, 0);
1037 bowTie.lineTo(0, 0);
1038 bowTie.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001039 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1040 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1041 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1042 bowTie.lineTo(SK_Scalar1, 0);
1043 bowTie.lineTo(SK_Scalar1, 0);
1044 bowTie.lineTo(SK_Scalar1, 0);
1045 bowTie.lineTo(0, SK_Scalar1);
1046 bowTie.lineTo(0, SK_Scalar1);
1047 bowTie.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001048 bowTie.close();
1049 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001050 check_direction(reporter, bowTie, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001051
reed@google.com7c424812011-05-15 04:38:34 +00001052 SkPath spiral;
1053 spiral.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001054 spiral.lineTo(100*SK_Scalar1, 0);
1055 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1056 spiral.lineTo(0, 100*SK_Scalar1);
1057 spiral.lineTo(0, 50*SK_Scalar1);
1058 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1059 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001060 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +00001061 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001062 check_direction(reporter, spiral, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001063
reed@google.com7c424812011-05-15 04:38:34 +00001064 SkPath dent;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001065 dent.moveTo(0, 0);
1066 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1067 dent.lineTo(0, 100*SK_Scalar1);
1068 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1069 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001070 dent.close();
1071 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001072 check_direction(reporter, dent, SkPath::kCW_Direction);
reed@google.com7c424812011-05-15 04:38:34 +00001073}
1074
reed@android.com6b82d1a2009-06-03 02:35:01 +00001075static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1076 const SkRect& bounds) {
1077 REPORTER_ASSERT(reporter, p.isConvex());
1078 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +00001079
reed@android.com6b82d1a2009-06-03 02:35:01 +00001080 SkPath p2(p);
1081 REPORTER_ASSERT(reporter, p2.isConvex());
1082 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1083
1084 SkPath other;
1085 other.swap(p2);
1086 REPORTER_ASSERT(reporter, other.isConvex());
1087 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1088}
1089
reed@google.com04863fa2011-05-15 04:08:24 +00001090static void setFromString(SkPath* path, const char str[]) {
1091 bool first = true;
1092 while (str) {
1093 SkScalar x, y;
1094 str = SkParse::FindScalar(str, &x);
1095 if (NULL == str) {
1096 break;
1097 }
1098 str = SkParse::FindScalar(str, &y);
1099 SkASSERT(str);
1100 if (first) {
1101 path->moveTo(x, y);
1102 first = false;
1103 } else {
1104 path->lineTo(x, y);
1105 }
1106 }
1107}
1108
1109static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +00001110 SkPath path;
1111
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001112 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001113 path.addCircle(0, 0, SkIntToScalar(10));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001114 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001115 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001116 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1117
reed@google.com04863fa2011-05-15 04:08:24 +00001118 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001119 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001120 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.com3e71a882012-01-10 18:44:37 +00001121 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCCW_Direction));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001122
reed@google.com04863fa2011-05-15 04:08:24 +00001123 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001124 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001125 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.com3e71a882012-01-10 18:44:37 +00001126 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCW_Direction));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001127
reed@google.com04863fa2011-05-15 04:08:24 +00001128 static const struct {
1129 const char* fPathStr;
1130 SkPath::Convexity fExpectedConvexity;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001131 SkPath::Direction fExpectedDirection;
reed@google.com04863fa2011-05-15 04:08:24 +00001132 } gRec[] = {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001133 { "", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1134 { "0 0", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1135 { "0 0 10 10", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1136 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPath::kUnknown_Direction },
1137 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPath::kCW_Direction },
1138 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPath::kCCW_Direction },
1139 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
1140 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPath::kCW_Direction },
reed@google.com04863fa2011-05-15 04:08:24 +00001141 };
1142
1143 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1144 SkPath path;
1145 setFromString(&path, gRec[i].fPathStr);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001146 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1147 check_direction(reporter, path, gRec[i].fExpectedDirection);
reed@google.com04863fa2011-05-15 04:08:24 +00001148 }
1149}
1150
reed@google.com7e6c4d12012-05-10 14:05:43 +00001151static void test_isLine(skiatest::Reporter* reporter) {
1152 SkPath path;
1153 SkPoint pts[2];
1154 const SkScalar value = SkIntToScalar(5);
1155
1156 REPORTER_ASSERT(reporter, !path.isLine(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001157
reed@google.com7e6c4d12012-05-10 14:05:43 +00001158 // set some non-zero values
1159 pts[0].set(value, value);
1160 pts[1].set(value, value);
1161 REPORTER_ASSERT(reporter, !path.isLine(pts));
1162 // check that pts was untouched
1163 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1164 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1165
1166 const SkScalar moveX = SkIntToScalar(1);
1167 const SkScalar moveY = SkIntToScalar(2);
1168 SkASSERT(value != moveX && value != moveY);
1169
1170 path.moveTo(moveX, moveY);
1171 REPORTER_ASSERT(reporter, !path.isLine(NULL));
1172 REPORTER_ASSERT(reporter, !path.isLine(pts));
1173 // check that pts was untouched
1174 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1175 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1176
1177 const SkScalar lineX = SkIntToScalar(2);
1178 const SkScalar lineY = SkIntToScalar(2);
1179 SkASSERT(value != lineX && value != lineY);
1180
1181 path.lineTo(lineX, lineY);
1182 REPORTER_ASSERT(reporter, path.isLine(NULL));
1183
1184 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1185 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1186 REPORTER_ASSERT(reporter, path.isLine(pts));
1187 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1188 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1189
1190 path.lineTo(0, 0); // too many points/verbs
1191 REPORTER_ASSERT(reporter, !path.isLine(NULL));
1192 REPORTER_ASSERT(reporter, !path.isLine(pts));
1193 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1194 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1195}
1196
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001197static void test_conservativelyContains(skiatest::Reporter* reporter) {
1198 SkPath path;
1199
1200 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1201 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1202
1203 // A circle that bounds kBaseRect (with a significant amount of slop)
1204 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
1205 circleR = SkScalarMul(circleR, SkFloatToScalar(1.75f)) / 2;
1206 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1207
1208 // round-rect radii
1209 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
skia.committer@gmail.comcec8de62012-11-14 02:01:22 +00001210
caryclark@google.com56f233a2012-11-19 13:06:06 +00001211 static const struct SUPPRESS_VISIBILITY_WARNING {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001212 SkRect fQueryRect;
1213 bool fInRect;
1214 bool fInCircle;
1215 bool fInRR;
1216 } kQueries[] = {
1217 {kBaseRect, true, true, false},
1218
1219 // rect well inside of kBaseRect
1220 {SkRect::MakeLTRB(kBaseRect.fLeft + SkFloatToScalar(0.25f)*kBaseRect.width(),
1221 kBaseRect.fTop + SkFloatToScalar(0.25f)*kBaseRect.height(),
1222 kBaseRect.fRight - SkFloatToScalar(0.25f)*kBaseRect.width(),
1223 kBaseRect.fBottom - SkFloatToScalar(0.25f)*kBaseRect.height()),
1224 true, true, true},
1225
1226 // rects with edges off by one from kBaseRect's edges
1227 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1228 kBaseRect.width(), kBaseRect.height() + 1),
1229 false, true, false},
1230 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1231 kBaseRect.width() + 1, kBaseRect.height()),
1232 false, true, false},
1233 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1234 kBaseRect.width() + 1, kBaseRect.height() + 1),
1235 false, true, false},
1236 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1237 kBaseRect.width(), kBaseRect.height()),
1238 false, true, false},
1239 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1240 kBaseRect.width(), kBaseRect.height()),
1241 false, true, false},
1242 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1243 kBaseRect.width() + 2, kBaseRect.height()),
1244 false, true, false},
1245 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1246 kBaseRect.width() + 2, kBaseRect.height()),
1247 false, true, false},
1248
1249 // zero-w/h rects at each corner of kBaseRect
1250 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false},
1251 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false},
1252 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false},
1253 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false},
1254
1255 // far away rect
1256 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1257 SkIntToScalar(10), SkIntToScalar(10)),
1258 false, false, false},
1259
1260 // very large rect containing kBaseRect
1261 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1262 kBaseRect.fTop - 5 * kBaseRect.height(),
1263 11 * kBaseRect.width(), 11 * kBaseRect.height()),
1264 false, false, false},
1265
1266 // skinny rect that spans same y-range as kBaseRect
1267 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1268 SkIntToScalar(1), kBaseRect.height()),
1269 true, true, true},
1270
1271 // short rect that spans same x-range as kBaseRect
1272 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
1273 true, true, true},
1274
1275 // skinny rect that spans slightly larger y-range than kBaseRect
1276 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1277 SkIntToScalar(1), kBaseRect.height() + 1),
1278 false, true, false},
1279
1280 // short rect that spans slightly larger x-range than kBaseRect
1281 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1282 kBaseRect.width() + 1, SkScalar(1)),
1283 false, true, false},
1284 };
1285
1286 for (int inv = 0; inv < 4; ++inv) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001287 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001288 SkRect qRect = kQueries[q].fQueryRect;
1289 if (inv & 0x1) {
1290 SkTSwap(qRect.fLeft, qRect.fRight);
1291 }
1292 if (inv & 0x2) {
1293 SkTSwap(qRect.fTop, qRect.fBottom);
1294 }
1295 for (int d = 0; d < 2; ++d) {
1296 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1297 path.reset();
1298 path.addRect(kBaseRect, dir);
1299 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1300 path.conservativelyContainsRect(qRect));
1301
1302 path.reset();
1303 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1304 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1305 path.conservativelyContainsRect(qRect));
1306
1307 path.reset();
1308 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1309 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1310 path.conservativelyContainsRect(qRect));
1311 }
1312 // Slightly non-convex shape, shouldn't contain any rects.
1313 path.reset();
1314 path.moveTo(0, 0);
1315 path.lineTo(SkIntToScalar(50), SkFloatToScalar(0.05f));
1316 path.lineTo(SkIntToScalar(100), 0);
1317 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1318 path.lineTo(0, SkIntToScalar(100));
1319 path.close();
1320 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1321 }
1322 }
1323
1324 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1325 path.reset();
1326 path.moveTo(0, 0);
1327 path.lineTo(SkIntToScalar(100), 0);
1328 path.lineTo(0, SkIntToScalar(100));
1329
1330 // inside, on along top edge
1331 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1332 SkIntToScalar(10),
1333 SkIntToScalar(10))));
1334 // above
1335 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1336 SkRect::MakeXYWH(SkIntToScalar(50),
1337 SkIntToScalar(-10),
1338 SkIntToScalar(10),
1339 SkIntToScalar(10))));
1340 // to the left
1341 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1342 SkIntToScalar(5),
1343 SkIntToScalar(5),
1344 SkIntToScalar(5))));
1345
1346 // outside the diagonal edge
1347 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1348 SkIntToScalar(200),
1349 SkIntToScalar(20),
1350 SkIntToScalar(5))));
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001351
1352 // same as above path and first test but with an extra moveTo.
1353 path.reset();
1354 path.moveTo(100, 100);
1355 path.moveTo(0, 0);
1356 path.lineTo(SkIntToScalar(100), 0);
1357 path.lineTo(0, SkIntToScalar(100));
1358
1359 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1360 SkIntToScalar(10),
1361 SkIntToScalar(10))));
1362
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001363}
1364
reed@google.comf32322b2013-10-16 15:14:04 +00001365static void test_isRect_open_close(skiatest::Reporter* reporter) {
1366 SkPath path;
1367 bool isClosed;
1368
1369 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
1370
1371 if (false) {
1372 // I think these should pass, but isRect() doesn't behave
1373 // this way... yet
1374 REPORTER_ASSERT(reporter, path.isRect(NULL, NULL));
1375 REPORTER_ASSERT(reporter, path.isRect(&isClosed, NULL));
1376 REPORTER_ASSERT(reporter, !isClosed);
1377 }
1378
1379 path.close();
1380 REPORTER_ASSERT(reporter, path.isRect(NULL, NULL));
1381 REPORTER_ASSERT(reporter, path.isRect(&isClosed, NULL));
1382 REPORTER_ASSERT(reporter, isClosed);
1383}
1384
caryclark@google.comf1316942011-07-26 19:54:45 +00001385// Simple isRect test is inline TestPath, below.
1386// test_isRect provides more extensive testing.
1387static void test_isRect(skiatest::Reporter* reporter) {
reed@google.comf32322b2013-10-16 15:14:04 +00001388 test_isRect_open_close(reporter);
1389
caryclark@google.comf1316942011-07-26 19:54:45 +00001390 // passing tests (all moveTo / lineTo...
1391 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1392 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1393 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1394 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1395 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1396 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1397 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1398 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1399 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001400 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1401 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 +00001402 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1403 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1404 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
caryclark@google.combfe90372012-11-21 13:56:20 +00001405 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
rmistry@google.comd6176b02012-08-23 18:14:13 +00001406
caryclark@google.comf1316942011-07-26 19:54:45 +00001407 // failing tests
1408 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1409 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1410 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1411 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1412 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1413 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1414 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1415 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
caryclark@google.combfe90372012-11-21 13:56:20 +00001416 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
1417 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
1418 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
rmistry@google.comd6176b02012-08-23 18:14:13 +00001419
caryclark@google.comf1316942011-07-26 19:54:45 +00001420 // failing, no close
1421 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
1422 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
1423
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001424 struct IsRectTest {
1425 SkPoint *fPoints;
1426 size_t fPointCount;
1427 bool fClose;
1428 bool fIsRect;
1429 } tests[] = {
1430 { r1, SK_ARRAY_COUNT(r1), true, true },
1431 { r2, SK_ARRAY_COUNT(r2), true, true },
1432 { r3, SK_ARRAY_COUNT(r3), true, true },
1433 { r4, SK_ARRAY_COUNT(r4), true, true },
1434 { r5, SK_ARRAY_COUNT(r5), true, true },
1435 { r6, SK_ARRAY_COUNT(r6), true, true },
1436 { r7, SK_ARRAY_COUNT(r7), true, true },
1437 { r8, SK_ARRAY_COUNT(r8), true, true },
1438 { r9, SK_ARRAY_COUNT(r9), true, true },
1439 { ra, SK_ARRAY_COUNT(ra), true, true },
1440 { rb, SK_ARRAY_COUNT(rb), true, true },
1441 { rc, SK_ARRAY_COUNT(rc), true, true },
1442 { rd, SK_ARRAY_COUNT(rd), true, true },
1443 { re, SK_ARRAY_COUNT(re), true, true },
1444 { rf, SK_ARRAY_COUNT(rf), true, true },
1445
1446 { f1, SK_ARRAY_COUNT(f1), true, false },
1447 { f2, SK_ARRAY_COUNT(f2), true, false },
1448 { f3, SK_ARRAY_COUNT(f3), true, false },
1449 { f4, SK_ARRAY_COUNT(f4), true, false },
1450 { f5, SK_ARRAY_COUNT(f5), true, false },
1451 { f6, SK_ARRAY_COUNT(f6), true, false },
1452 { f7, SK_ARRAY_COUNT(f7), true, false },
1453 { f8, SK_ARRAY_COUNT(f8), true, false },
1454 { f9, SK_ARRAY_COUNT(f9), true, false },
1455 { fa, SK_ARRAY_COUNT(fa), true, false },
1456 { fb, SK_ARRAY_COUNT(fb), true, false },
1457
1458 { c1, SK_ARRAY_COUNT(c1), false, false },
1459 { c2, SK_ARRAY_COUNT(c2), false, false },
caryclark@google.comf1316942011-07-26 19:54:45 +00001460 };
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001461
1462 const size_t testCount = SK_ARRAY_COUNT(tests);
caryclark@google.comf1316942011-07-26 19:54:45 +00001463 size_t index;
1464 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1465 SkPath path;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001466 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1467 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1468 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.comf1316942011-07-26 19:54:45 +00001469 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001470 if (tests[testIndex].fClose) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001471 path.close();
1472 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001473 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(NULL));
1474 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(NULL, NULL));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001475
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001476 if (tests[testIndex].fIsRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001477 SkRect computed, expected;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001478 expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001479 REPORTER_ASSERT(reporter, path.isRect(&computed));
1480 REPORTER_ASSERT(reporter, expected == computed);
skia.committer@gmail.com1c9c0d32012-11-22 02:02:41 +00001481
caryclark@google.comf68154a2012-11-21 15:18:06 +00001482 bool isClosed;
1483 SkPath::Direction direction, cheapDirection;
1484 REPORTER_ASSERT(reporter, path.cheapComputeDirection(&cheapDirection));
robertphillips@google.com8fd16032013-06-25 15:39:58 +00001485 REPORTER_ASSERT(reporter, path.isRect(&isClosed, &direction));
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001486 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001487 REPORTER_ASSERT(reporter, direction == cheapDirection);
1488 } else {
1489 SkRect computed;
1490 computed.set(123, 456, 789, 1011);
1491 REPORTER_ASSERT(reporter, !path.isRect(&computed));
1492 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
1493 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
1494
1495 bool isClosed = (bool) -1;
1496 SkPath::Direction direction = (SkPath::Direction) -1;
robertphillips@google.com8fd16032013-06-25 15:39:58 +00001497 REPORTER_ASSERT(reporter, !path.isRect(&isClosed, &direction));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001498 REPORTER_ASSERT(reporter, isClosed == (bool) -1);
1499 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001500 }
caryclark@google.comf1316942011-07-26 19:54:45 +00001501 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001502
caryclark@google.comf1316942011-07-26 19:54:45 +00001503 // fail, close then line
1504 SkPath path1;
1505 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001506 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001507 path1.lineTo(r1[index].fX, r1[index].fY);
1508 }
1509 path1.close();
1510 path1.lineTo(1, 0);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001511 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001512
caryclark@google.comf1316942011-07-26 19:54:45 +00001513 // fail, move in the middle
1514 path1.reset();
1515 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001516 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001517 if (index == 2) {
1518 path1.moveTo(1, .5f);
1519 }
1520 path1.lineTo(r1[index].fX, r1[index].fY);
1521 }
1522 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001523 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00001524
1525 // fail, move on the edge
1526 path1.reset();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001527 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001528 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
1529 path1.lineTo(r1[index].fX, r1[index].fY);
1530 }
1531 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001532 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001533
caryclark@google.comf1316942011-07-26 19:54:45 +00001534 // fail, quad
1535 path1.reset();
1536 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001537 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001538 if (index == 2) {
1539 path1.quadTo(1, .5f, 1, .5f);
1540 }
1541 path1.lineTo(r1[index].fX, r1[index].fY);
1542 }
1543 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001544 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001545
caryclark@google.comf1316942011-07-26 19:54:45 +00001546 // fail, cubic
1547 path1.reset();
1548 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001549 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001550 if (index == 2) {
1551 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
1552 }
1553 path1.lineTo(r1[index].fX, r1[index].fY);
1554 }
1555 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001556 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00001557}
1558
caryclark@google.com56f233a2012-11-19 13:06:06 +00001559static void test_isNestedRects(skiatest::Reporter* reporter) {
1560 // passing tests (all moveTo / lineTo...
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001561 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001562 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1563 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1564 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001565 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001566 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1567 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1568 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1569 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001570 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
1571 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 +00001572 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
1573 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
1574 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001575
1576 // failing tests
1577 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1578 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1579 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1580 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1581 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1582 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1583 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1584 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
1585
1586 // failing, no close
1587 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
1588 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
1589
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001590 struct IsNestedRectTest {
1591 SkPoint *fPoints;
1592 size_t fPointCount;
1593 SkPath::Direction fDirection;
1594 bool fClose;
1595 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
1596 } tests[] = {
1597 { r1, SK_ARRAY_COUNT(r1), SkPath::kCW_Direction , true, true },
1598 { r2, SK_ARRAY_COUNT(r2), SkPath::kCW_Direction , true, true },
1599 { r3, SK_ARRAY_COUNT(r3), SkPath::kCW_Direction , true, true },
1600 { r4, SK_ARRAY_COUNT(r4), SkPath::kCW_Direction , true, true },
1601 { r5, SK_ARRAY_COUNT(r5), SkPath::kCCW_Direction, true, true },
1602 { r6, SK_ARRAY_COUNT(r6), SkPath::kCCW_Direction, true, true },
1603 { r7, SK_ARRAY_COUNT(r7), SkPath::kCCW_Direction, true, true },
1604 { r8, SK_ARRAY_COUNT(r8), SkPath::kCCW_Direction, true, true },
1605 { r9, SK_ARRAY_COUNT(r9), SkPath::kCCW_Direction, true, true },
1606 { ra, SK_ARRAY_COUNT(ra), SkPath::kCCW_Direction, true, true },
1607 { rb, SK_ARRAY_COUNT(rb), SkPath::kCW_Direction, true, true },
1608 { rc, SK_ARRAY_COUNT(rc), SkPath::kCW_Direction, true, true },
1609 { rd, SK_ARRAY_COUNT(rd), SkPath::kCCW_Direction, true, true },
1610 { re, SK_ARRAY_COUNT(re), SkPath::kCW_Direction, true, true },
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001611
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001612 { f1, SK_ARRAY_COUNT(f1), SkPath::kUnknown_Direction, true, false },
1613 { f2, SK_ARRAY_COUNT(f2), SkPath::kUnknown_Direction, true, false },
1614 { f3, SK_ARRAY_COUNT(f3), SkPath::kUnknown_Direction, true, false },
1615 { f4, SK_ARRAY_COUNT(f4), SkPath::kUnknown_Direction, true, false },
1616 { f5, SK_ARRAY_COUNT(f5), SkPath::kUnknown_Direction, true, false },
1617 { f6, SK_ARRAY_COUNT(f6), SkPath::kUnknown_Direction, true, false },
1618 { f7, SK_ARRAY_COUNT(f7), SkPath::kUnknown_Direction, true, false },
1619 { f8, SK_ARRAY_COUNT(f8), SkPath::kUnknown_Direction, true, false },
1620
1621 { c1, SK_ARRAY_COUNT(c1), SkPath::kUnknown_Direction, false, false },
1622 { c2, SK_ARRAY_COUNT(c2), SkPath::kUnknown_Direction, false, false },
1623 };
1624
1625 const size_t testCount = SK_ARRAY_COUNT(tests);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001626 size_t index;
1627 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001628 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1629 SkPath path;
1630 if (rectFirst) {
1631 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1632 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001633 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1634 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1635 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001636 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001637 if (tests[testIndex].fClose) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001638 path.close();
1639 }
1640 if (!rectFirst) {
1641 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1642 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001643 REPORTER_ASSERT(reporter, tests[testIndex].fIsNestedRect == path.isNestedRects(NULL));
1644 if (tests[testIndex].fIsNestedRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001645 SkRect expected[2], computed[2];
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001646 SkPath::Direction expectedDirs[2], computedDirs[2];
caryclark@google.com56f233a2012-11-19 13:06:06 +00001647 SkRect testBounds;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001648 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001649 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
1650 expected[1] = testBounds;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001651 if (rectFirst) {
1652 expectedDirs[0] = SkPath::kCW_Direction;
1653 } else {
1654 expectedDirs[0] = SkPath::kCCW_Direction;
1655 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001656 expectedDirs[1] = tests[testIndex].fDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001657 REPORTER_ASSERT(reporter, path.isNestedRects(computed, computedDirs));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001658 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
1659 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001660 REPORTER_ASSERT(reporter, expectedDirs[0] == computedDirs[0]);
1661 REPORTER_ASSERT(reporter, expectedDirs[1] == computedDirs[1]);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001662 }
caryclark@google.com56f233a2012-11-19 13:06:06 +00001663 }
1664
1665 // fail, close then line
1666 SkPath path1;
1667 if (rectFirst) {
1668 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1669 }
1670 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001671 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001672 path1.lineTo(r1[index].fX, r1[index].fY);
1673 }
1674 path1.close();
1675 path1.lineTo(1, 0);
1676 if (!rectFirst) {
1677 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1678 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001679 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001680
1681 // fail, move in the middle
1682 path1.reset();
1683 if (rectFirst) {
1684 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1685 }
1686 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001687 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001688 if (index == 2) {
1689 path1.moveTo(1, .5f);
1690 }
1691 path1.lineTo(r1[index].fX, r1[index].fY);
1692 }
1693 path1.close();
1694 if (!rectFirst) {
1695 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1696 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001697 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001698
1699 // fail, move on the edge
1700 path1.reset();
1701 if (rectFirst) {
1702 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1703 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001704 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001705 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
1706 path1.lineTo(r1[index].fX, r1[index].fY);
1707 }
1708 path1.close();
1709 if (!rectFirst) {
1710 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1711 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001712 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001713
1714 // fail, quad
1715 path1.reset();
1716 if (rectFirst) {
1717 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1718 }
1719 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001720 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001721 if (index == 2) {
1722 path1.quadTo(1, .5f, 1, .5f);
1723 }
1724 path1.lineTo(r1[index].fX, r1[index].fY);
1725 }
1726 path1.close();
1727 if (!rectFirst) {
1728 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1729 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001730 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001731
1732 // fail, cubic
1733 path1.reset();
1734 if (rectFirst) {
1735 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1736 }
1737 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001738 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001739 if (index == 2) {
1740 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
1741 }
1742 path1.lineTo(r1[index].fX, r1[index].fY);
1743 }
1744 path1.close();
1745 if (!rectFirst) {
1746 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1747 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001748 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
skia.committer@gmail.com34587162012-11-20 02:01:23 +00001749
caryclark@google.com56f233a2012-11-19 13:06:06 +00001750 // fail, not nested
1751 path1.reset();
1752 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
1753 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001754 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001755 }
caryclark@google.combfe90372012-11-21 13:56:20 +00001756
1757 // pass, stroke rect
1758 SkPath src, dst;
1759 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
1760 SkPaint strokePaint;
1761 strokePaint.setStyle(SkPaint::kStroke_Style);
1762 strokePaint.setStrokeWidth(2);
1763 strokePaint.getFillPath(src, &dst);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001764 REPORTER_ASSERT(reporter, dst.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001765}
1766
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001767static void write_and_read_back(skiatest::Reporter* reporter,
1768 const SkPath& p) {
1769 SkWriter32 writer(100);
1770 writer.writePath(p);
reed@google.com081560e2013-10-31 16:24:08 +00001771 size_t size = writer.bytesWritten();
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001772 SkAutoMalloc storage(size);
1773 writer.flatten(storage.get());
1774 SkReader32 reader(storage.get(), size);
1775
1776 SkPath readBack;
1777 REPORTER_ASSERT(reporter, readBack != p);
1778 reader.readPath(&readBack);
1779 REPORTER_ASSERT(reporter, readBack == p);
1780
rmistry@google.comd6176b02012-08-23 18:14:13 +00001781 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001782 p.getConvexityOrUnknown());
1783
1784 REPORTER_ASSERT(reporter, readBack.isOval(NULL) == p.isOval(NULL));
1785
1786 const SkRect& origBounds = p.getBounds();
1787 const SkRect& readBackBounds = readBack.getBounds();
1788
1789 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
1790}
1791
reed@google.com53effc52011-09-21 19:05:12 +00001792static void test_flattening(skiatest::Reporter* reporter) {
1793 SkPath p;
1794
1795 static const SkPoint pts[] = {
1796 { 0, 0 },
1797 { SkIntToScalar(10), SkIntToScalar(10) },
1798 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
1799 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
1800 };
1801 p.moveTo(pts[0]);
1802 p.lineTo(pts[1]);
1803 p.quadTo(pts[2], pts[3]);
1804 p.cubicTo(pts[4], pts[5], pts[6]);
1805
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001806 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001807
1808 // create a buffer that should be much larger than the path so we don't
1809 // kill our stack if writer goes too far.
1810 char buffer[1024];
1811 uint32_t size1 = p.writeToMemory(NULL);
1812 uint32_t size2 = p.writeToMemory(buffer);
1813 REPORTER_ASSERT(reporter, size1 == size2);
1814
1815 SkPath p2;
1816 uint32_t size3 = p2.readFromMemory(buffer);
1817 REPORTER_ASSERT(reporter, size1 == size3);
1818 REPORTER_ASSERT(reporter, p == p2);
1819
1820 char buffer2[1024];
1821 size3 = p2.writeToMemory(buffer2);
1822 REPORTER_ASSERT(reporter, size1 == size3);
1823 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001824
1825 // test persistence of the oval flag & convexity
1826 {
1827 SkPath oval;
1828 SkRect rect = SkRect::MakeWH(10, 10);
1829 oval.addOval(rect);
1830
1831 write_and_read_back(reporter, oval);
1832 }
reed@google.com53effc52011-09-21 19:05:12 +00001833}
1834
1835static void test_transform(skiatest::Reporter* reporter) {
1836 SkPath p, p1;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001837
reed@google.com53effc52011-09-21 19:05:12 +00001838 static const SkPoint pts[] = {
1839 { 0, 0 },
1840 { SkIntToScalar(10), SkIntToScalar(10) },
1841 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
1842 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
1843 };
1844 p.moveTo(pts[0]);
1845 p.lineTo(pts[1]);
1846 p.quadTo(pts[2], pts[3]);
1847 p.cubicTo(pts[4], pts[5], pts[6]);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001848
reed@google.com53effc52011-09-21 19:05:12 +00001849 SkMatrix matrix;
1850 matrix.reset();
1851 p.transform(matrix, &p1);
1852 REPORTER_ASSERT(reporter, p == p1);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001853
reed@google.com53effc52011-09-21 19:05:12 +00001854 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
1855 p.transform(matrix, &p1);
1856 SkPoint pts1[7];
1857 int count = p1.getPoints(pts1, 7);
1858 REPORTER_ASSERT(reporter, 7 == count);
1859 for (int i = 0; i < count; ++i) {
1860 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
1861 REPORTER_ASSERT(reporter, newPt == pts1[i]);
1862 }
1863}
1864
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001865static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001866 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00001867 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001868
caryclark@google.com56f233a2012-11-19 13:06:06 +00001869 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
schenney@chromium.org7e963602012-06-13 17:05:43 +00001870 const char* testPath;
1871 const size_t numResultPts;
1872 const SkRect resultBound;
1873 const SkPath::Verb* resultVerbs;
1874 const size_t numResultVerbs;
1875 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001876
schenney@chromium.org7e963602012-06-13 17:05:43 +00001877 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
1878 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
1879 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
1880 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
1881 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
1882 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
1883 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
1884 static const SkPath::Verb resultVerbs8[] = {
1885 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
1886 };
1887 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
1888 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
1889 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
1890 static const SkPath::Verb resultVerbs12[] = {
1891 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
1892 };
1893 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
1894 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
1895 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
1896 static const SkPath::Verb resultVerbs16[] = {
1897 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
1898 };
1899 static const struct zeroPathTestData gZeroLengthTests[] = {
1900 { "M 1 1", 1, {0, 0, 0, 0}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001901 { "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 +00001902 { "M 1 1 z", 1, {0, 0, 0, 0}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001903 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
1904 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
1905 { "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) },
1906 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
1907 { "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) },
1908 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
1909 { "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) },
1910 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
1911 { "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) },
1912 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
1913 { "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 +00001914 SK_ARRAY_COUNT(resultVerbs14)
1915 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001916 { "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) },
1917 { "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 +00001918 SK_ARRAY_COUNT(resultVerbs16)
1919 }
1920 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001921
schenney@chromium.org7e963602012-06-13 17:05:43 +00001922 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
1923 p.reset();
1924 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
1925 REPORTER_ASSERT(reporter, valid);
1926 REPORTER_ASSERT(reporter, !p.isEmpty());
1927 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
1928 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
1929 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
1930 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
1931 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
1932 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00001933 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001934}
1935
1936struct SegmentInfo {
1937 SkPath fPath;
1938 int fPointCount;
1939};
1940
reed@google.com10296cc2011-09-21 12:29:05 +00001941#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
1942
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001943static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00001944 SkPath p, p2;
1945
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001946 p.moveTo(0, 0);
1947 p.quadTo(100, 100, 200, 200);
1948 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
1949 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00001950 p2 = p;
1951 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001952 p.cubicTo(100, 100, 200, 200, 300, 300);
1953 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
1954 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00001955 p2 = p;
1956 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
1957
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001958 p.reset();
1959 p.moveTo(0, 0);
1960 p.cubicTo(100, 100, 200, 200, 300, 300);
1961 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00001962 p2 = p;
1963 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001964
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001965 REPORTER_ASSERT(reporter, !p.isEmpty());
1966}
1967
1968static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00001969 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001970 SkPoint pts[4];
1971
1972 // Test an iterator with no path
1973 SkPath::Iter noPathIter;
1974 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00001975
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001976 // Test that setting an empty path works
1977 noPathIter.setPath(p, false);
1978 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00001979
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001980 // Test that close path makes no difference for an empty path
1981 noPathIter.setPath(p, true);
1982 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00001983
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001984 // Test an iterator with an initial empty path
1985 SkPath::Iter iter(p, false);
1986 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1987
1988 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00001989 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001990 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1991
rmistry@google.comd6176b02012-08-23 18:14:13 +00001992
schenney@chromium.org7e963602012-06-13 17:05:43 +00001993 struct iterTestData {
1994 const char* testPath;
1995 const bool forceClose;
1996 const bool consumeDegenerates;
1997 const size_t* numResultPtsPerVerb;
1998 const SkPoint* resultPts;
1999 const SkPath::Verb* resultVerbs;
2000 const size_t numResultVerbs;
2001 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002002
schenney@chromium.org7e963602012-06-13 17:05:43 +00002003 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2004 static const SkPath::Verb resultVerbs2[] = {
2005 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2006 };
2007 static const SkPath::Verb resultVerbs3[] = {
2008 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2009 };
2010 static const SkPath::Verb resultVerbs4[] = {
2011 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2012 };
2013 static const SkPath::Verb resultVerbs5[] = {
2014 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2015 };
2016 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00002017 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2018 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2019 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2020 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002021 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002022 static const SkPoint resultPts2[] = {
2023 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2024 };
2025 static const SkPoint resultPts3[] = {
2026 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2027 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2028 };
2029 static const SkPoint resultPts4[] = {
2030 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2031 };
2032 static const SkPoint resultPts5[] = {
2033 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2034 };
2035 static const struct iterTestData gIterTests[] = {
2036 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002037 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2038 { "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 +00002039 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2040 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2041 { "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) },
2042 { "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 +00002043 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2044 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2045 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2046 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2047 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2048 { "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 +00002049 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002050
schenney@chromium.org7e963602012-06-13 17:05:43 +00002051 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2052 p.reset();
2053 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2054 REPORTER_ASSERT(reporter, valid);
2055 iter.setPath(p, gIterTests[i].forceClose);
2056 int j = 0, l = 0;
2057 do {
2058 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2059 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2060 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2061 }
2062 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2063 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2064 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002065
2066 // The GM degeneratesegments.cpp test is more extensive
2067}
2068
2069static void test_raw_iter(skiatest::Reporter* reporter) {
2070 SkPath p;
2071 SkPoint pts[4];
2072
2073 // Test an iterator with no path
2074 SkPath::RawIter noPathIter;
2075 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2076 // Test that setting an empty path works
2077 noPathIter.setPath(p);
2078 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00002079
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002080 // Test an iterator with an initial empty path
2081 SkPath::RawIter iter(p);
2082 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2083
2084 // Test that a move-only path returns the move.
2085 p.moveTo(SK_Scalar1, 0);
2086 iter.setPath(p);
2087 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2088 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2089 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2090 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2091
2092 // No matter how many moves we add, we should get them all back
2093 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2094 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2095 iter.setPath(p);
2096 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2097 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2098 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2099 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2100 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2101 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2102 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2103 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2104 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2105 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2106
2107 // Initial close is never ever stored
2108 p.reset();
2109 p.close();
2110 iter.setPath(p);
2111 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2112
2113 // Move/close sequences
2114 p.reset();
2115 p.close(); // Not stored, no purpose
2116 p.moveTo(SK_Scalar1, 0);
2117 p.close();
2118 p.close(); // Not stored, no purpose
2119 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2120 p.close();
2121 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2122 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
2123 p.close();
2124 iter.setPath(p);
2125 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2126 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2127 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2128 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2129 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2130 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2131 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2132 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2133 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2134 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2135 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2136 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2137 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2138 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2139 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2140 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2141 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2142 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2143 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2144 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2145 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2146 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2147
2148 // Generate random paths and verify
2149 SkPoint randomPts[25];
2150 for (int i = 0; i < 5; ++i) {
2151 for (int j = 0; j < 5; ++j) {
2152 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
2153 }
2154 }
2155
2156 // Max of 10 segments, max 3 points per segment
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002157 SkRandom rand(9876543);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002158 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00002159 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002160 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00002161
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002162 for (int i = 0; i < 500; ++i) {
2163 p.reset();
2164 bool lastWasClose = true;
2165 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00002166 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002167 int numPoints = 0;
2168 int numVerbs = (rand.nextU() >> 16) % 10;
2169 int numIterVerbs = 0;
2170 for (int j = 0; j < numVerbs; ++j) {
2171 do {
2172 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
2173 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002174 switch (nextVerb) {
2175 case SkPath::kMove_Verb:
2176 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2177 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00002178 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002179 numPoints += 1;
2180 lastWasClose = false;
2181 haveMoveTo = true;
2182 break;
2183 case SkPath::kLine_Verb:
2184 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002185 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002186 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2187 haveMoveTo = true;
2188 }
2189 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2190 p.lineTo(expectedPts[numPoints]);
2191 numPoints += 1;
2192 lastWasClose = false;
2193 break;
2194 case SkPath::kQuad_Verb:
2195 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002196 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002197 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2198 haveMoveTo = true;
2199 }
2200 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2201 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2202 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
2203 numPoints += 2;
2204 lastWasClose = false;
2205 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002206 case SkPath::kConic_Verb:
2207 if (!haveMoveTo) {
2208 expectedPts[numPoints++] = lastMoveToPt;
2209 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2210 haveMoveTo = true;
2211 }
2212 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2213 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2214 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2215 rand.nextUScalar1() * 4);
2216 numPoints += 2;
2217 lastWasClose = false;
2218 break;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002219 case SkPath::kCubic_Verb:
2220 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002221 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002222 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2223 haveMoveTo = true;
2224 }
2225 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2226 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2227 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
2228 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2229 expectedPts[numPoints + 2]);
2230 numPoints += 3;
2231 lastWasClose = false;
2232 break;
2233 case SkPath::kClose_Verb:
2234 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00002235 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002236 lastWasClose = true;
2237 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002238 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002239 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002240 }
2241 expectedVerbs[numIterVerbs++] = nextVerb;
2242 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00002243
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002244 iter.setPath(p);
2245 numVerbs = numIterVerbs;
2246 numIterVerbs = 0;
2247 int numIterPts = 0;
2248 SkPoint lastMoveTo;
2249 SkPoint lastPt;
2250 lastMoveTo.set(0, 0);
2251 lastPt.set(0, 0);
2252 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
2253 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
2254 numIterVerbs++;
2255 switch (nextVerb) {
2256 case SkPath::kMove_Verb:
2257 REPORTER_ASSERT(reporter, numIterPts < numPoints);
2258 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
2259 lastPt = lastMoveTo = pts[0];
2260 numIterPts += 1;
2261 break;
2262 case SkPath::kLine_Verb:
2263 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
2264 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2265 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2266 lastPt = pts[1];
2267 numIterPts += 1;
2268 break;
2269 case SkPath::kQuad_Verb:
reed@google.com277c3f82013-05-31 15:17:50 +00002270 case SkPath::kConic_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002271 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
2272 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2273 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2274 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2275 lastPt = pts[2];
2276 numIterPts += 2;
2277 break;
2278 case SkPath::kCubic_Verb:
2279 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
2280 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2281 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2282 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2283 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
2284 lastPt = pts[3];
2285 numIterPts += 3;
2286 break;
2287 case SkPath::kClose_Verb:
2288 REPORTER_ASSERT(reporter, pts[0] == lastMoveTo);
2289 lastPt = lastMoveTo;
2290 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002291 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002292 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002293 }
2294 }
2295 REPORTER_ASSERT(reporter, numIterPts == numPoints);
2296 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
2297 }
2298}
2299
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002300static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002301 const SkPath& path,
2302 bool expectedCircle,
2303 SkPath::Direction expectedDir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002304 SkRect rect;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002305 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
2306 REPORTER_ASSERT(reporter, path.cheapIsDirection(expectedDir));
skia.committer@gmail.comfbb0ed92012-11-13 21:46:06 +00002307
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002308 if (expectedCircle) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002309 REPORTER_ASSERT(reporter, rect.height() == rect.width());
2310 }
2311}
2312
2313static void test_circle_skew(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002314 const SkPath& path,
2315 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002316 SkPath tmp;
2317
2318 SkMatrix m;
2319 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
2320 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002321 // this matrix reverses the direction.
2322 if (SkPath::kCCW_Direction == dir) {
2323 dir = SkPath::kCW_Direction;
2324 } else {
2325 SkASSERT(SkPath::kCW_Direction == dir);
2326 dir = SkPath::kCCW_Direction;
2327 }
2328 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002329}
2330
2331static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002332 const SkPath& path,
2333 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002334 SkPath tmp;
2335
2336 // translate at small offset
2337 SkMatrix m;
2338 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
2339 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002340 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002341
2342 tmp.reset();
2343 m.reset();
2344
2345 // translate at a relatively big offset
2346 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
2347 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002348 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002349}
2350
2351static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002352 const SkPath& path,
2353 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002354 for (int angle = 0; angle < 360; ++angle) {
2355 SkPath tmp;
2356 SkMatrix m;
2357 m.setRotate(SkIntToScalar(angle));
2358 path.transform(m, &tmp);
2359
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002360 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002361 // degrees is not an oval anymore, this can be improved. we made this
2362 // for the simplicity of our implementation.
2363 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002364 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002365 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002366 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002367 }
2368 }
2369}
2370
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002371static void test_circle_mirror_x(skiatest::Reporter* reporter,
2372 const SkPath& path,
2373 SkPath::Direction dir) {
2374 SkPath tmp;
2375 SkMatrix m;
2376 m.reset();
2377 m.setScaleX(-SK_Scalar1);
2378 path.transform(m, &tmp);
2379
2380 if (SkPath::kCW_Direction == dir) {
2381 dir = SkPath::kCCW_Direction;
2382 } else {
2383 SkASSERT(SkPath::kCCW_Direction == dir);
2384 dir = SkPath::kCW_Direction;
2385 }
2386
2387 check_for_circle(reporter, tmp, true, dir);
2388}
2389
2390static void test_circle_mirror_y(skiatest::Reporter* reporter,
2391 const SkPath& path,
2392 SkPath::Direction dir) {
2393 SkPath tmp;
2394 SkMatrix m;
2395 m.reset();
2396 m.setScaleY(-SK_Scalar1);
2397 path.transform(m, &tmp);
2398
2399 if (SkPath::kCW_Direction == dir) {
2400 dir = SkPath::kCCW_Direction;
2401 } else {
2402 SkASSERT(SkPath::kCCW_Direction == dir);
2403 dir = SkPath::kCW_Direction;
2404 }
2405
2406 check_for_circle(reporter, tmp, true, dir);
2407}
2408
2409static void test_circle_mirror_xy(skiatest::Reporter* reporter,
2410 const SkPath& path,
2411 SkPath::Direction dir) {
2412 SkPath tmp;
2413 SkMatrix m;
2414 m.reset();
2415 m.setScaleX(-SK_Scalar1);
2416 m.setScaleY(-SK_Scalar1);
2417 path.transform(m, &tmp);
2418
2419 check_for_circle(reporter, tmp, true, dir);
2420}
2421
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002422static void test_circle_with_direction(skiatest::Reporter* reporter,
2423 SkPath::Direction dir) {
2424 SkPath path;
2425
2426 // circle at origin
2427 path.addCircle(0, 0, SkIntToScalar(20), dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002428 check_for_circle(reporter, path, true, dir);
2429 test_circle_rotate(reporter, path, dir);
2430 test_circle_translate(reporter, path, dir);
2431 test_circle_skew(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002432
2433 // circle at an offset at (10, 10)
2434 path.reset();
2435 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
2436 SkIntToScalar(20), dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002437 check_for_circle(reporter, path, true, dir);
2438 test_circle_rotate(reporter, path, dir);
2439 test_circle_translate(reporter, path, dir);
2440 test_circle_skew(reporter, path, dir);
2441 test_circle_mirror_x(reporter, path, dir);
2442 test_circle_mirror_y(reporter, path, dir);
2443 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002444}
2445
2446static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
2447 SkPath path;
2448 SkPath circle;
2449 SkPath rect;
2450 SkPath empty;
2451
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002452 static const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
2453 static const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
2454
2455 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002456 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
2457 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
2458
2459 SkMatrix translate;
2460 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
2461
2462 // For simplicity, all the path concatenation related operations
2463 // would mark it non-circle, though in theory it's still a circle.
2464
2465 // empty + circle (translate)
2466 path = empty;
2467 path.addPath(circle, translate);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002468 check_for_circle(reporter, path, false, kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002469
2470 // circle + empty (translate)
2471 path = circle;
2472 path.addPath(empty, translate);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002473 check_for_circle(reporter, path, false, kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002474
2475 // test reverseAddPath
2476 path = circle;
2477 path.reverseAddPath(rect);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002478 check_for_circle(reporter, path, false, kCircleDirOpposite);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002479}
2480
2481static void test_circle(skiatest::Reporter* reporter) {
2482 test_circle_with_direction(reporter, SkPath::kCW_Direction);
2483 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
2484
2485 // multiple addCircle()
2486 SkPath path;
2487 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2488 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002489 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002490
2491 // some extra lineTo() would make isOval() fail
2492 path.reset();
2493 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2494 path.lineTo(0, 0);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002495 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002496
2497 // not back to the original point
2498 path.reset();
2499 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2500 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002501 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002502
2503 test_circle_with_add_paths(reporter);
2504}
2505
2506static void test_oval(skiatest::Reporter* reporter) {
2507 SkRect rect;
2508 SkMatrix m;
2509 SkPath path;
2510
2511 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
2512 path.addOval(rect);
2513
2514 REPORTER_ASSERT(reporter, path.isOval(NULL));
2515
2516 m.setRotate(SkIntToScalar(90));
2517 SkPath tmp;
2518 path.transform(m, &tmp);
2519 // an oval rotated 90 degrees is still an oval.
2520 REPORTER_ASSERT(reporter, tmp.isOval(NULL));
2521
2522 m.reset();
2523 m.setRotate(SkIntToScalar(30));
2524 tmp.reset();
2525 path.transform(m, &tmp);
2526 // an oval rotated 30 degrees is not an oval anymore.
2527 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2528
2529 // since empty path being transformed.
2530 path.reset();
2531 tmp.reset();
2532 m.reset();
2533 path.transform(m, &tmp);
2534 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2535
2536 // empty path is not an oval
2537 tmp.reset();
2538 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2539
2540 // only has moveTo()s
2541 tmp.reset();
2542 tmp.moveTo(0, 0);
2543 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
2544 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2545
2546 // mimic WebKit's calling convention,
2547 // call moveTo() first and then call addOval()
2548 path.reset();
2549 path.moveTo(0, 0);
2550 path.addOval(rect);
2551 REPORTER_ASSERT(reporter, path.isOval(NULL));
2552
2553 // copy path
2554 path.reset();
2555 tmp.reset();
2556 tmp.addOval(rect);
2557 path = tmp;
2558 REPORTER_ASSERT(reporter, path.isOval(NULL));
2559}
2560
bungeman@google.coma5809a32013-06-21 15:13:34 +00002561static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
2562 SkPath empty;
reed@android.com80e39a72009-04-02 16:59:40 +00002563
reed@android.com3abec1d2009-03-02 05:36:20 +00002564 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002565 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002566 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00002567 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00002568 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00002569 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
2570 REPORTER_ASSERT(reporter, !p.isInverseFillType());
bungeman@google.coma5809a32013-06-21 15:13:34 +00002571 REPORTER_ASSERT(reporter, p == empty);
2572 REPORTER_ASSERT(reporter, !(p != empty));
2573}
2574
2575static void TestPath(skiatest::Reporter* reporter) {
2576 SkTSize<SkScalar>::Make(3,4);
2577
2578 SkPath p, empty;
2579 SkRect bounds, bounds2;
2580 test_empty(reporter, p);
reed@android.com3abec1d2009-03-02 05:36:20 +00002581
reed@android.comd252db02009-04-01 18:31:44 +00002582 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00002583
reed@android.com3abec1d2009-03-02 05:36:20 +00002584 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00002585
reed@android.com6b82d1a2009-06-03 02:35:01 +00002586 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
2587 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00002588 // we have quads or cubics
2589 REPORTER_ASSERT(reporter, p.getSegmentMasks() & kCurveSegmentMask);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002590 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00002591
reed@android.com6b82d1a2009-06-03 02:35:01 +00002592 p.reset();
bungeman@google.coma5809a32013-06-21 15:13:34 +00002593 test_empty(reporter, p);
reed@google.com10296cc2011-09-21 12:29:05 +00002594
reed@android.com6b82d1a2009-06-03 02:35:01 +00002595 p.addOval(bounds);
2596 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002597 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00002598
bungeman@google.coma5809a32013-06-21 15:13:34 +00002599 p.rewind();
2600 test_empty(reporter, p);
2601
reed@android.com3abec1d2009-03-02 05:36:20 +00002602 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00002603 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00002604 // we have only lines
2605 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002606 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00002607
bungeman@google.coma5809a32013-06-21 15:13:34 +00002608 REPORTER_ASSERT(reporter, p != empty);
2609 REPORTER_ASSERT(reporter, !(p == empty));
reed@android.com3abec1d2009-03-02 05:36:20 +00002610
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002611 // do getPoints and getVerbs return the right result
2612 REPORTER_ASSERT(reporter, p.getPoints(NULL, 0) == 4);
2613 REPORTER_ASSERT(reporter, p.getVerbs(NULL, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00002614 SkPoint pts[4];
2615 int count = p.getPoints(pts, 4);
2616 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002617 uint8_t verbs[6];
2618 verbs[5] = 0xff;
2619 p.getVerbs(verbs, 5);
2620 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
2621 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
2622 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
2623 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
2624 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
2625 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00002626 bounds2.set(pts, 4);
2627 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00002628
reed@android.com3abec1d2009-03-02 05:36:20 +00002629 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
2630 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00002631 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00002632
reed@android.com3abec1d2009-03-02 05:36:20 +00002633 REPORTER_ASSERT(reporter, p.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00002634 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00002635 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
2636 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00002637
reed@android.com3abec1d2009-03-02 05:36:20 +00002638 // now force p to not be a rect
2639 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
2640 p.addRect(bounds);
2641 REPORTER_ASSERT(reporter, !p.isRect(NULL));
reed@android.com3abec1d2009-03-02 05:36:20 +00002642
reed@google.com7e6c4d12012-05-10 14:05:43 +00002643 test_isLine(reporter);
2644 test_isRect(reporter);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002645 test_isNestedRects(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002646 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00002647 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00002648 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00002649 test_convexity2(reporter);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00002650 test_conservativelyContains(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00002651 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002652 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00002653 test_flattening(reporter);
2654 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00002655 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002656 test_iter(reporter);
2657 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002658 test_circle(reporter);
2659 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00002660 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00002661 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00002662 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00002663 test_isfinite_after_transform(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00002664 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00002665 test_arb_zero_rad_round_rect_is_rect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00002666 test_addrect_isfinite(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00002667 test_tricky_cubic();
2668 test_clipped_cubic();
2669 test_crbug_170666();
reed@google.com7a90daf2013-04-10 18:44:00 +00002670 test_bad_cubic_crbug229478();
reed@google.com3eff3592013-05-08 21:08:21 +00002671 test_bad_cubic_crbug234190();
mtklein@google.com9c9d4a72013-08-07 19:17:53 +00002672 test_android_specific_behavior(reporter);
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +00002673 test_gen_id(reporter);
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +00002674 test_path_close_issue1474(reporter);
reed@google.comb58ba892013-10-15 15:44:35 +00002675 test_path_to_region(reporter);
reed@android.com3abec1d2009-03-02 05:36:20 +00002676}
2677
2678#include "TestClassDef.h"
2679DEFINE_TESTCLASS("Path", PathTestClass, TestPath)