blob: c47b2c4858d6f9ff3f2351c4a2db25fe484ca8aa [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);
130 REPORTER_ASSERT(reporter, original.getGenerationID() > 0);
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000131 REPORTER_ASSERT(reporter, original.getSourcePath() == &source);
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000132
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000133 uint32_t copyID, assignID;
134
135 // Test copy constructor. Copy generation ID, copy source path.
136 SkPath copy(original);
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000137 REPORTER_ASSERT(reporter, copy.getGenerationID() == original.getGenerationID());
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000138 REPORTER_ASSERT(reporter, copy.getSourcePath() == original.getSourcePath());
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000139
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000140 // Test assigment operator. Increment generation ID, copy source path.
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000141 SkPath assign;
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000142 assignID = assign.getGenerationID();
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000143 assign = original;
mtklein@google.comcb8b0ee2013-08-15 21:14:51 +0000144 REPORTER_ASSERT(reporter, assign.getGenerationID() > assignID);
145 REPORTER_ASSERT(reporter, assign.getSourcePath() == original.getSourcePath());
146
147 // Test rewind. Increment generation ID, don't touch source path.
148 copyID = copy.getGenerationID();
149 copy.rewind();
150 REPORTER_ASSERT(reporter, copy.getGenerationID() > copyID);
151 REPORTER_ASSERT(reporter, copy.getSourcePath() == original.getSourcePath());
152
153 // Test reset. Increment generation ID, don't touch source path.
154 assignID = assign.getGenerationID();
155 assign.reset();
156 REPORTER_ASSERT(reporter, assign.getGenerationID() > assignID);
157 REPORTER_ASSERT(reporter, assign.getSourcePath() == original.getSourcePath());
158
159 // Test swap. Increment both generation IDs, swap source paths.
160 copy.setSourcePath(&anotherSource);
161 copyID = copy.getGenerationID();
162 assignID = assign.getGenerationID();
163 copy.swap(assign);
164 REPORTER_ASSERT(reporter, copy.getGenerationID() > copyID);
165 REPORTER_ASSERT(reporter, assign.getGenerationID() > assignID);
166 REPORTER_ASSERT(reporter, copy.getSourcePath() == original.getSourcePath());
167 REPORTER_ASSERT(reporter, assign.getSourcePath() == &anotherSource);
mtklein@google.com9c9d4a72013-08-07 19:17:53 +0000168#endif
169}
170
reed@google.com3eff3592013-05-08 21:08:21 +0000171// This used to assert in the debug build, as the edges did not all line-up.
172static void test_bad_cubic_crbug234190() {
173 SkPath path;
174 path.moveTo(13.8509f, 3.16858f);
175 path.cubicTo(-2.35893e+08f, -4.21044e+08f,
176 -2.38991e+08f, -4.26573e+08f,
177 -2.41016e+08f, -4.30188e+08f);
178
179 SkPaint paint;
180 paint.setAntiAlias(true);
reed@google.comd28ba802013-09-20 19:33:52 +0000181 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(84, 88));
reed@google.com3eff3592013-05-08 21:08:21 +0000182 surface->getCanvas()->drawPath(path, paint);
183}
184
reed@google.com7a90daf2013-04-10 18:44:00 +0000185static void test_bad_cubic_crbug229478() {
186 const SkPoint pts[] = {
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000187 { 4595.91064f, -11596.9873f },
188 { 4597.2168f, -11595.9414f },
189 { 4598.52344f, -11594.8955f },
190 { 4599.83008f, -11593.8496f },
reed@google.com7a90daf2013-04-10 18:44:00 +0000191 };
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000192
reed@google.com7a90daf2013-04-10 18:44:00 +0000193 SkPath path;
194 path.moveTo(pts[0]);
195 path.cubicTo(pts[1], pts[2], pts[3]);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000196
reed@google.com7a90daf2013-04-10 18:44:00 +0000197 SkPaint paint;
198 paint.setStyle(SkPaint::kStroke_Style);
199 paint.setStrokeWidth(20);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000200
reed@google.com7a90daf2013-04-10 18:44:00 +0000201 SkPath dst;
202 // Before the fix, this would infinite-recurse, and run out of stack
203 // because we would keep trying to subdivide a degenerate cubic segment.
204 paint.getFillPath(path, &dst, NULL);
205}
206
reed@google.com64d62952013-01-18 17:49:28 +0000207static void build_path_170666(SkPath& path) {
208 path.moveTo(17.9459f, 21.6344f);
209 path.lineTo(139.545f, -47.8105f);
210 path.lineTo(139.545f, -47.8105f);
211 path.lineTo(131.07f, -47.3888f);
212 path.lineTo(131.07f, -47.3888f);
213 path.lineTo(122.586f, -46.9532f);
214 path.lineTo(122.586f, -46.9532f);
215 path.lineTo(18076.6f, 31390.9f);
216 path.lineTo(18076.6f, 31390.9f);
217 path.lineTo(18085.1f, 31390.5f);
218 path.lineTo(18085.1f, 31390.5f);
219 path.lineTo(18076.6f, 31390.9f);
220 path.lineTo(18076.6f, 31390.9f);
221 path.lineTo(17955, 31460.3f);
222 path.lineTo(17955, 31460.3f);
223 path.lineTo(17963.5f, 31459.9f);
224 path.lineTo(17963.5f, 31459.9f);
225 path.lineTo(17971.9f, 31459.5f);
226 path.lineTo(17971.9f, 31459.5f);
227 path.lineTo(17.9551f, 21.6205f);
228 path.lineTo(17.9551f, 21.6205f);
229 path.lineTo(9.47091f, 22.0561f);
230 path.lineTo(9.47091f, 22.0561f);
231 path.lineTo(17.9459f, 21.6344f);
232 path.lineTo(17.9459f, 21.6344f);
233 path.close();path.moveTo(0.995934f, 22.4779f);
234 path.lineTo(0.986725f, 22.4918f);
235 path.lineTo(0.986725f, 22.4918f);
236 path.lineTo(17955, 31460.4f);
237 path.lineTo(17955, 31460.4f);
238 path.lineTo(17971.9f, 31459.5f);
239 path.lineTo(17971.9f, 31459.5f);
240 path.lineTo(18093.6f, 31390.1f);
241 path.lineTo(18093.6f, 31390.1f);
242 path.lineTo(18093.6f, 31390);
243 path.lineTo(18093.6f, 31390);
244 path.lineTo(139.555f, -47.8244f);
245 path.lineTo(139.555f, -47.8244f);
246 path.lineTo(122.595f, -46.9671f);
247 path.lineTo(122.595f, -46.9671f);
248 path.lineTo(0.995934f, 22.4779f);
249 path.lineTo(0.995934f, 22.4779f);
250 path.close();
251 path.moveTo(5.43941f, 25.5223f);
252 path.lineTo(798267, -28871.1f);
253 path.lineTo(798267, -28871.1f);
254 path.lineTo(3.12512e+06f, -113102);
255 path.lineTo(3.12512e+06f, -113102);
256 path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813);
257 path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f);
258 path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f);
259 path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f);
260 path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f);
261 path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f);
262 path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f);
263 path.lineTo(2.78271e+08f, -1.00733e+07f);
264 path.lineTo(2.78271e+08f, -1.00733e+07f);
265 path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f);
266 path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f);
267 path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f);
268 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
269 path.lineTo(2.77473e+08f, -1.00444e+07f);
270 path.lineTo(2.77473e+08f, -1.00444e+07f);
271 path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f);
272 path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f);
273 path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f);
274 path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f);
275 path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f);
276 path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814);
277 path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103);
278 path.lineTo(798284, -28872);
279 path.lineTo(798284, -28872);
280 path.lineTo(22.4044f, 24.6677f);
281 path.lineTo(22.4044f, 24.6677f);
282 path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f);
283 path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f);
284 path.close();
285}
286
287static void build_path_simple_170666(SkPath& path) {
288 path.moveTo(126.677f, 24.1591f);
289 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
290}
291
292// This used to assert in the SK_DEBUG build, as the clip step would fail with
293// too-few interations in our cubic-line intersection code. That code now runs
294// 24 interations (instead of 16).
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000295static void test_crbug_170666() {
reed@google.com64d62952013-01-18 17:49:28 +0000296 SkPath path;
297 SkPaint paint;
298 paint.setAntiAlias(true);
299
reed@google.comd28ba802013-09-20 19:33:52 +0000300 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(1000, 1000));
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000301
reed@google.com64d62952013-01-18 17:49:28 +0000302 build_path_simple_170666(path);
303 surface->getCanvas()->drawPath(path, paint);
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000304
reed@google.com64d62952013-01-18 17:49:28 +0000305 build_path_170666(path);
306 surface->getCanvas()->drawPath(path, paint);
307}
308
reed@google.coma8790de2012-10-24 21:04:04 +0000309// Make sure we stay non-finite once we get there (unless we reset or rewind).
310static void test_addrect_isfinite(skiatest::Reporter* reporter) {
311 SkPath path;
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000312
reed@google.coma8790de2012-10-24 21:04:04 +0000313 path.addRect(SkRect::MakeWH(50, 100));
314 REPORTER_ASSERT(reporter, path.isFinite());
315
316 path.moveTo(0, 0);
317 path.lineTo(SK_ScalarInfinity, 42);
318 REPORTER_ASSERT(reporter, !path.isFinite());
319
320 path.addRect(SkRect::MakeWH(50, 100));
321 REPORTER_ASSERT(reporter, !path.isFinite());
322
323 path.reset();
324 REPORTER_ASSERT(reporter, path.isFinite());
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000325
reed@google.coma8790de2012-10-24 21:04:04 +0000326 path.addRect(SkRect::MakeWH(50, 100));
327 REPORTER_ASSERT(reporter, path.isFinite());
328}
329
reed@google.com848148e2013-01-15 15:51:59 +0000330static void build_big_path(SkPath* path, bool reducedCase) {
331 if (reducedCase) {
332 path->moveTo(577330, 1971.72f);
333 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
334 } else {
335 path->moveTo(60.1631f, 7.70567f);
336 path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
337 path->lineTo(577379, 1977.77f);
338 path->quadTo(577364, 1979.57f, 577325, 1980.26f);
339 path->quadTo(577286, 1980.95f, 577245, 1980.13f);
340 path->quadTo(577205, 1979.3f, 577187, 1977.45f);
341 path->quadTo(577168, 1975.6f, 577183, 1973.8f);
342 path->quadTo(577198, 1972, 577238, 1971.31f);
343 path->quadTo(577277, 1970.62f, 577317, 1971.45f);
344 path->quadTo(577330, 1971.72f, 577341, 1972.11f);
345 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
346 path->moveTo(306.718f, -32.912f);
347 path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
348 }
349}
350
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000351static void test_clipped_cubic() {
reed@google.comd28ba802013-09-20 19:33:52 +0000352 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(640, 480));
reed@google.com848148e2013-01-15 15:51:59 +0000353
354 // This path used to assert, because our cubic-chopping code incorrectly
355 // moved control points after the chop. This test should be run in SK_DEBUG
356 // mode to ensure that we no long assert.
357 SkPath path;
358 for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
359 build_big_path(&path, SkToBool(doReducedCase));
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +0000360
reed@google.com848148e2013-01-15 15:51:59 +0000361 SkPaint paint;
362 for (int doAA = 0; doAA <= 1; ++doAA) {
363 paint.setAntiAlias(SkToBool(doAA));
364 surface->getCanvas()->drawPath(path, paint);
365 }
366 }
367}
368
reed@google.com8cae8352012-09-14 15:18:41 +0000369// Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
370// which triggered an assert, from a tricky cubic. This test replicates that
371// example, so we can ensure that we handle it (in SkEdge.cpp), and don't
372// assert in the SK_DEBUG build.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000373static void test_tricky_cubic() {
reed@google.com8cae8352012-09-14 15:18:41 +0000374 const SkPoint pts[] = {
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +0000375 { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
376 { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
377 { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
378 { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
reed@google.com8cae8352012-09-14 15:18:41 +0000379 };
380
381 SkPath path;
382 path.moveTo(pts[0]);
383 path.cubicTo(pts[1], pts[2], pts[3]);
384
385 SkPaint paint;
386 paint.setAntiAlias(true);
387
reed@google.comd28ba802013-09-20 19:33:52 +0000388 SkSurface* surface = SkSurface::NewRasterPMColor(19, 130);
reed@google.com8cae8352012-09-14 15:18:41 +0000389 surface->getCanvas()->drawPath(path, paint);
390 surface->unref();
391}
reed@android.com3abec1d2009-03-02 05:36:20 +0000392
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000393// Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
394//
395static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
396 SkPath path;
397 path.quadTo(157, 366, 286, 208);
398 path.arcTo(37, 442, 315, 163, 957494590897113.0f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000399
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000400 SkMatrix matrix;
401 matrix.setScale(1000*1000, 1000*1000);
402
403 // Be sure that path::transform correctly updates isFinite and the bounds
404 // if the transformation overflows. The previous bug was that isFinite was
405 // set to true in this case, but the bounds were not set to empty (which
406 // they should be).
407 while (path.isFinite()) {
408 REPORTER_ASSERT(reporter, path.getBounds().isFinite());
409 REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
410 path.transform(matrix);
411 }
412 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
413
414 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
415 path.transform(matrix);
416 // we need to still be non-finite
417 REPORTER_ASSERT(reporter, !path.isFinite());
418 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
419}
420
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000421static void add_corner_arc(SkPath* path, const SkRect& rect,
422 SkScalar xIn, SkScalar yIn,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000423 int startAngle)
424{
425
426 SkScalar rx = SkMinScalar(rect.width(), xIn);
427 SkScalar ry = SkMinScalar(rect.height(), yIn);
428
429 SkRect arcRect;
430 arcRect.set(-rx, -ry, rx, ry);
431 switch (startAngle) {
432 case 0:
433 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
434 break;
435 case 90:
436 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
437 break;
438 case 180:
439 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
440 break;
441 case 270:
442 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
443 break;
444 default:
445 break;
446 }
447
448 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
449}
450
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000451static void make_arb_round_rect(SkPath* path, const SkRect& r,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000452 SkScalar xCorner, SkScalar yCorner) {
453 // we are lazy here and use the same x & y for each corner
454 add_corner_arc(path, r, xCorner, yCorner, 270);
455 add_corner_arc(path, r, xCorner, yCorner, 0);
456 add_corner_arc(path, r, xCorner, yCorner, 90);
457 add_corner_arc(path, r, xCorner, yCorner, 180);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000458 path->close();
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000459}
460
461// Chrome creates its own round rects with each corner possibly being different.
462// Performance will suffer if they are not convex.
463// Note: PathBench::ArbRoundRectBench performs almost exactly
464// the same test (but with drawing)
465static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000466 SkRandom rand;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000467 SkRect r;
468
469 for (int i = 0; i < 5000; ++i) {
470
robertphillips@google.com158618e2012-10-23 16:56:56 +0000471 SkScalar size = rand.nextUScalar1() * 30;
472 if (size < SK_Scalar1) {
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000473 continue;
474 }
475 r.fLeft = rand.nextUScalar1() * 300;
476 r.fTop = rand.nextUScalar1() * 300;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000477 r.fRight = r.fLeft + 2 * size;
478 r.fBottom = r.fTop + 2 * size;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000479
480 SkPath temp;
481
482 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
483
484 REPORTER_ASSERT(reporter, temp.isConvex());
485 }
486}
487
robertphillips@google.com158618e2012-10-23 16:56:56 +0000488// Chrome will sometimes create a 0 radius round rect. The degenerate
489// quads prevent the path from being converted to a rect
490// Note: PathBench::ArbRoundRectBench performs almost exactly
491// the same test (but with drawing)
492static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000493 SkRandom rand;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000494 SkRect r;
495
496 for (int i = 0; i < 5000; ++i) {
497
498 SkScalar size = rand.nextUScalar1() * 30;
499 if (size < SK_Scalar1) {
500 continue;
501 }
502 r.fLeft = rand.nextUScalar1() * 300;
503 r.fTop = rand.nextUScalar1() * 300;
504 r.fRight = r.fLeft + 2 * size;
505 r.fBottom = r.fTop + 2 * size;
506
507 SkPath temp;
508
509 make_arb_round_rect(&temp, r, 0, 0);
510
robertphillips@google.com158618e2012-10-23 16:56:56 +0000511 SkRect result;
512 REPORTER_ASSERT(reporter, temp.isRect(&result));
513 REPORTER_ASSERT(reporter, r == result);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000514 }
515}
516
reed@google.com0bb18bb2012-07-26 15:20:36 +0000517static void test_rect_isfinite(skiatest::Reporter* reporter) {
518 const SkScalar inf = SK_ScalarInfinity;
caryclark@google.com7abfa492013-04-12 11:59:41 +0000519 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000520 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000521
reed@google.com0bb18bb2012-07-26 15:20:36 +0000522 SkRect r;
523 r.setEmpty();
524 REPORTER_ASSERT(reporter, r.isFinite());
caryclark@google.com7abfa492013-04-12 11:59:41 +0000525 r.set(0, 0, inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000526 REPORTER_ASSERT(reporter, !r.isFinite());
527 r.set(0, 0, nan, 0);
528 REPORTER_ASSERT(reporter, !r.isFinite());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000529
reed@google.com0bb18bb2012-07-26 15:20:36 +0000530 SkPoint pts[] = {
531 { 0, 0 },
532 { SK_Scalar1, 0 },
533 { 0, SK_Scalar1 },
534 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000535
reed@google.com0bb18bb2012-07-26 15:20:36 +0000536 bool isFine = r.setBoundsCheck(pts, 3);
537 REPORTER_ASSERT(reporter, isFine);
538 REPORTER_ASSERT(reporter, !r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000539
reed@google.com0bb18bb2012-07-26 15:20:36 +0000540 pts[1].set(inf, 0);
541 isFine = r.setBoundsCheck(pts, 3);
542 REPORTER_ASSERT(reporter, !isFine);
543 REPORTER_ASSERT(reporter, r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000544
reed@google.com0bb18bb2012-07-26 15:20:36 +0000545 pts[1].set(nan, 0);
546 isFine = r.setBoundsCheck(pts, 3);
547 REPORTER_ASSERT(reporter, !isFine);
548 REPORTER_ASSERT(reporter, r.isEmpty());
549}
550
551static void test_path_isfinite(skiatest::Reporter* reporter) {
552 const SkScalar inf = SK_ScalarInfinity;
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000553 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000554 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000555
reed@google.com0bb18bb2012-07-26 15:20:36 +0000556 SkPath path;
557 REPORTER_ASSERT(reporter, path.isFinite());
558
559 path.reset();
560 REPORTER_ASSERT(reporter, path.isFinite());
561
562 path.reset();
563 path.moveTo(SK_Scalar1, 0);
564 REPORTER_ASSERT(reporter, path.isFinite());
565
566 path.reset();
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000567 path.moveTo(inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000568 REPORTER_ASSERT(reporter, !path.isFinite());
569
570 path.reset();
571 path.moveTo(nan, 0);
572 REPORTER_ASSERT(reporter, !path.isFinite());
573}
574
575static void test_isfinite(skiatest::Reporter* reporter) {
576 test_rect_isfinite(reporter);
577 test_path_isfinite(reporter);
578}
579
reed@google.com744faba2012-05-29 19:54:52 +0000580// assert that we always
581// start with a moveTo
582// only have 1 moveTo
583// only have Lines after that
584// end with a single close
585// only have (at most) 1 close
586//
587static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000588 const SkPoint srcPts[], bool expectClose) {
reed@google.com744faba2012-05-29 19:54:52 +0000589 SkPath::RawIter iter(path);
590 SkPoint pts[4];
reed@google.com744faba2012-05-29 19:54:52 +0000591
592 bool firstTime = true;
593 bool foundClose = false;
594 for (;;) {
595 switch (iter.next(pts)) {
596 case SkPath::kMove_Verb:
597 REPORTER_ASSERT(reporter, firstTime);
598 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
599 srcPts++;
600 firstTime = false;
601 break;
602 case SkPath::kLine_Verb:
603 REPORTER_ASSERT(reporter, !firstTime);
604 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
605 srcPts++;
606 break;
607 case SkPath::kQuad_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000608 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb");
reed@google.com744faba2012-05-29 19:54:52 +0000609 break;
reed@google.com277c3f82013-05-31 15:17:50 +0000610 case SkPath::kConic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000611 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb");
reed@google.com277c3f82013-05-31 15:17:50 +0000612 break;
reed@google.com744faba2012-05-29 19:54:52 +0000613 case SkPath::kCubic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000614 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb");
reed@google.com744faba2012-05-29 19:54:52 +0000615 break;
616 case SkPath::kClose_Verb:
617 REPORTER_ASSERT(reporter, !firstTime);
618 REPORTER_ASSERT(reporter, !foundClose);
619 REPORTER_ASSERT(reporter, expectClose);
620 foundClose = true;
621 break;
622 case SkPath::kDone_Verb:
623 goto DONE;
624 }
625 }
626DONE:
627 REPORTER_ASSERT(reporter, foundClose == expectClose);
628}
629
630static void test_addPoly(skiatest::Reporter* reporter) {
631 SkPoint pts[32];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000632 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000633
reed@google.com744faba2012-05-29 19:54:52 +0000634 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
635 pts[i].fX = rand.nextSScalar1();
636 pts[i].fY = rand.nextSScalar1();
637 }
638
639 for (int doClose = 0; doClose <= 1; ++doClose) {
640 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
641 SkPath path;
642 path.addPoly(pts, count, SkToBool(doClose));
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000643 test_poly(reporter, path, pts, SkToBool(doClose));
reed@google.com744faba2012-05-29 19:54:52 +0000644 }
645 }
646}
647
reed@google.com8b06f1a2012-05-29 12:03:46 +0000648static void test_strokerec(skiatest::Reporter* reporter) {
649 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
650 REPORTER_ASSERT(reporter, rec.isFillStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000651
reed@google.com8b06f1a2012-05-29 12:03:46 +0000652 rec.setHairlineStyle();
653 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000654
reed@google.com8b06f1a2012-05-29 12:03:46 +0000655 rec.setStrokeStyle(SK_Scalar1, false);
656 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000657
reed@google.com8b06f1a2012-05-29 12:03:46 +0000658 rec.setStrokeStyle(SK_Scalar1, true);
659 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000660
reed@google.com8b06f1a2012-05-29 12:03:46 +0000661 rec.setStrokeStyle(0, false);
662 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000663
reed@google.com8b06f1a2012-05-29 12:03:46 +0000664 rec.setStrokeStyle(0, true);
665 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
666}
667
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000668// Set this for paths that don't have a consistent direction such as a bowtie.
669// (cheapComputeDirection is not expected to catch these.)
670static const SkPath::Direction kDontCheckDir = static_cast<SkPath::Direction>(-1);
671
672static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
673 SkPath::Direction expected) {
674 if (expected == kDontCheckDir) {
675 return;
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000676 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000677 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
678
679 SkPath::Direction dir;
680 if (copy.cheapComputeDirection(&dir)) {
681 REPORTER_ASSERT(reporter, dir == expected);
682 } else {
683 REPORTER_ASSERT(reporter, SkPath::kUnknown_Direction == expected);
684 }
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000685}
686
reed@google.com3e71a882012-01-10 18:44:37 +0000687static void test_direction(skiatest::Reporter* reporter) {
688 size_t i;
689 SkPath path;
690 REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL));
691 REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCW_Direction));
692 REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCCW_Direction));
reed@google.coma8a3b3d2012-11-26 18:16:27 +0000693 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kUnknown_Direction));
reed@google.com3e71a882012-01-10 18:44:37 +0000694
695 static const char* gDegen[] = {
696 "M 10 10",
697 "M 10 10 M 20 20",
698 "M 10 10 L 20 20",
699 "M 10 10 L 10 10 L 10 10",
700 "M 10 10 Q 10 10 10 10",
701 "M 10 10 C 10 10 10 10 10 10",
702 };
703 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
704 path.reset();
705 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
706 REPORTER_ASSERT(reporter, valid);
707 REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL));
708 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000709
reed@google.com3e71a882012-01-10 18:44:37 +0000710 static const char* gCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000711 "M 10 10 L 10 10 Q 20 10 20 20",
reed@google.com3e71a882012-01-10 18:44:37 +0000712 "M 10 10 C 20 10 20 20 20 20",
reed@google.comd4146662012-01-31 15:42:29 +0000713 "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 +0000714 // rect with top two corners replaced by cubics with identical middle
715 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000716 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
717 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000718 };
719 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
720 path.reset();
721 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
722 REPORTER_ASSERT(reporter, valid);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000723 check_direction(reporter, path, SkPath::kCW_Direction);
reed@google.com3e71a882012-01-10 18:44:37 +0000724 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000725
reed@google.com3e71a882012-01-10 18:44:37 +0000726 static const char* gCCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000727 "M 10 10 L 10 10 Q 20 10 20 -20",
reed@google.com3e71a882012-01-10 18:44:37 +0000728 "M 10 10 C 20 10 20 -20 20 -20",
reed@google.comd4146662012-01-31 15:42:29 +0000729 "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 +0000730 // rect with top two corners replaced by cubics with identical middle
731 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000732 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
733 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000734 };
735 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
736 path.reset();
737 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
738 REPORTER_ASSERT(reporter, valid);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000739 check_direction(reporter, path, SkPath::kCCW_Direction);
reed@google.com3e71a882012-01-10 18:44:37 +0000740 }
reed@google.comac8543f2012-01-30 20:51:25 +0000741
742 // Test two donuts, each wound a different direction. Only the outer contour
743 // determines the cheap direction
744 path.reset();
745 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
746 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000747 check_direction(reporter, path, SkPath::kCW_Direction);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000748
reed@google.comac8543f2012-01-30 20:51:25 +0000749 path.reset();
750 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
751 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000752 check_direction(reporter, path, SkPath::kCCW_Direction);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000753
bsalomon@google.com6843ac42012-02-17 13:49:03 +0000754#ifdef SK_SCALAR_IS_FLOAT
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000755 // triangle with one point really far from the origin.
756 path.reset();
757 // the first point is roughly 1.05e10, 1.05e10
bsalomon@google.com53aab782012-02-23 14:54:49 +0000758 path.moveTo(SkFloatToScalar(SkBits2Float(0x501c7652)), SkFloatToScalar(SkBits2Float(0x501c7652)));
759 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
760 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000761 check_direction(reporter, path, SkPath::kCCW_Direction);
bsalomon@google.com53aab782012-02-23 14:54:49 +0000762#endif
reed@google.com3e71a882012-01-10 18:44:37 +0000763}
764
reed@google.comffdb0182011-11-14 19:29:14 +0000765static void add_rect(SkPath* path, const SkRect& r) {
766 path->moveTo(r.fLeft, r.fTop);
767 path->lineTo(r.fRight, r.fTop);
768 path->lineTo(r.fRight, r.fBottom);
769 path->lineTo(r.fLeft, r.fBottom);
770 path->close();
771}
772
773static void test_bounds(skiatest::Reporter* reporter) {
774 static const SkRect rects[] = {
reed@google.com3563c9e2011-11-14 19:34:57 +0000775 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
776 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
777 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
778 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
reed@google.comffdb0182011-11-14 19:29:14 +0000779 };
780
781 SkPath path0, path1;
782 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
783 path0.addRect(rects[i]);
784 add_rect(&path1, rects[i]);
785 }
786
787 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
788}
789
reed@google.com55b5f4b2011-09-07 12:23:41 +0000790static void stroke_cubic(const SkPoint pts[4]) {
791 SkPath path;
792 path.moveTo(pts[0]);
793 path.cubicTo(pts[1], pts[2], pts[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000794
reed@google.com55b5f4b2011-09-07 12:23:41 +0000795 SkPaint paint;
796 paint.setStyle(SkPaint::kStroke_Style);
797 paint.setStrokeWidth(SK_Scalar1 * 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000798
reed@google.com55b5f4b2011-09-07 12:23:41 +0000799 SkPath fill;
800 paint.getFillPath(path, &fill);
801}
802
803// just ensure this can run w/o any SkASSERTS firing in the debug build
804// we used to assert due to differences in how we determine a degenerate vector
805// but that was fixed with the introduction of SkPoint::CanNormalize
806static void stroke_tiny_cubic() {
807 SkPoint p0[] = {
808 { 372.0f, 92.0f },
809 { 372.0f, 92.0f },
810 { 372.0f, 92.0f },
811 { 372.0f, 92.0f },
812 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000813
reed@google.com55b5f4b2011-09-07 12:23:41 +0000814 stroke_cubic(p0);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000815
reed@google.com55b5f4b2011-09-07 12:23:41 +0000816 SkPoint p1[] = {
817 { 372.0f, 92.0f },
818 { 372.0007f, 92.000755f },
819 { 371.99927f, 92.003922f },
820 { 371.99826f, 92.003899f },
821 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000822
reed@google.com55b5f4b2011-09-07 12:23:41 +0000823 stroke_cubic(p1);
824}
825
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000826static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
827 for (int i = 0; i < 2; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +0000828 SkPath::Iter iter(path, SkToBool(i));
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000829 SkPoint mv;
830 SkPoint pts[4];
831 SkPath::Verb v;
832 int nMT = 0;
833 int nCL = 0;
tomhudson@google.com221db3c2011-07-28 21:10:29 +0000834 mv.set(0, 0);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000835 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
836 switch (v) {
837 case SkPath::kMove_Verb:
838 mv = pts[0];
839 ++nMT;
840 break;
841 case SkPath::kClose_Verb:
842 REPORTER_ASSERT(reporter, mv == pts[0]);
843 ++nCL;
844 break;
845 default:
846 break;
847 }
848 }
849 // if we force a close on the interator we should have a close
850 // for every moveTo
851 REPORTER_ASSERT(reporter, !i || nMT == nCL);
852 }
853}
854
855static void test_close(skiatest::Reporter* reporter) {
856 SkPath closePt;
857 closePt.moveTo(0, 0);
858 closePt.close();
859 check_close(reporter, closePt);
860
861 SkPath openPt;
862 openPt.moveTo(0, 0);
863 check_close(reporter, openPt);
864
865 SkPath empty;
866 check_close(reporter, empty);
867 empty.close();
868 check_close(reporter, empty);
869
870 SkPath rect;
871 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
872 check_close(reporter, rect);
873 rect.close();
874 check_close(reporter, rect);
875
876 SkPath quad;
877 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
878 check_close(reporter, quad);
879 quad.close();
880 check_close(reporter, quad);
881
882 SkPath cubic;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000883 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000884 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
885 check_close(reporter, cubic);
886 cubic.close();
887 check_close(reporter, cubic);
888
889 SkPath line;
890 line.moveTo(SK_Scalar1, SK_Scalar1);
891 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
892 check_close(reporter, line);
893 line.close();
894 check_close(reporter, line);
895
896 SkPath rect2;
897 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
898 rect2.close();
899 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
900 check_close(reporter, rect2);
901 rect2.close();
902 check_close(reporter, rect2);
903
904 SkPath oval3;
905 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
906 oval3.close();
907 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
908 check_close(reporter, oval3);
909 oval3.close();
910 check_close(reporter, oval3);
911
912 SkPath moves;
913 moves.moveTo(SK_Scalar1, SK_Scalar1);
914 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
915 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
916 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
917 check_close(reporter, moves);
reed@google.com55b5f4b2011-09-07 12:23:41 +0000918
919 stroke_tiny_cubic();
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +0000920}
921
reed@google.com7c424812011-05-15 04:38:34 +0000922static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
923 SkPath::Convexity expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000924 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
925 SkPath::Convexity c = copy.getConvexity();
reed@google.com7c424812011-05-15 04:38:34 +0000926 REPORTER_ASSERT(reporter, c == expected);
927}
928
929static void test_convexity2(skiatest::Reporter* reporter) {
930 SkPath pt;
931 pt.moveTo(0, 0);
932 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +0000933 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000934 check_direction(reporter, pt, SkPath::kUnknown_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000935
reed@google.com7c424812011-05-15 04:38:34 +0000936 SkPath line;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000937 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
938 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000939 line.close();
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000940 check_convexity(reporter, line, SkPath::kConvex_Convexity);
941 check_direction(reporter, line, SkPath::kUnknown_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000942
reed@google.com7c424812011-05-15 04:38:34 +0000943 SkPath triLeft;
944 triLeft.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000945 triLeft.lineTo(SK_Scalar1, 0);
946 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000947 triLeft.close();
948 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000949 check_direction(reporter, triLeft, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000950
reed@google.com7c424812011-05-15 04:38:34 +0000951 SkPath triRight;
952 triRight.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000953 triRight.lineTo(-SK_Scalar1, 0);
954 triRight.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000955 triRight.close();
956 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000957 check_direction(reporter, triRight, SkPath::kCCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000958
reed@google.com7c424812011-05-15 04:38:34 +0000959 SkPath square;
960 square.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000961 square.lineTo(SK_Scalar1, 0);
962 square.lineTo(SK_Scalar1, SK_Scalar1);
963 square.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000964 square.close();
965 check_convexity(reporter, square, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000966 check_direction(reporter, square, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000967
reed@google.com7c424812011-05-15 04:38:34 +0000968 SkPath redundantSquare;
969 redundantSquare.moveTo(0, 0);
970 redundantSquare.lineTo(0, 0);
971 redundantSquare.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000972 redundantSquare.lineTo(SK_Scalar1, 0);
973 redundantSquare.lineTo(SK_Scalar1, 0);
974 redundantSquare.lineTo(SK_Scalar1, 0);
975 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
976 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
977 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
978 redundantSquare.lineTo(0, SK_Scalar1);
979 redundantSquare.lineTo(0, SK_Scalar1);
980 redundantSquare.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000981 redundantSquare.close();
982 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000983 check_direction(reporter, redundantSquare, SkPath::kCW_Direction);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000984
reed@google.com7c424812011-05-15 04:38:34 +0000985 SkPath bowTie;
986 bowTie.moveTo(0, 0);
987 bowTie.lineTo(0, 0);
988 bowTie.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +0000989 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
990 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
991 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
992 bowTie.lineTo(SK_Scalar1, 0);
993 bowTie.lineTo(SK_Scalar1, 0);
994 bowTie.lineTo(SK_Scalar1, 0);
995 bowTie.lineTo(0, SK_Scalar1);
996 bowTie.lineTo(0, SK_Scalar1);
997 bowTie.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +0000998 bowTie.close();
999 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001000 check_direction(reporter, bowTie, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001001
reed@google.com7c424812011-05-15 04:38:34 +00001002 SkPath spiral;
1003 spiral.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001004 spiral.lineTo(100*SK_Scalar1, 0);
1005 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1006 spiral.lineTo(0, 100*SK_Scalar1);
1007 spiral.lineTo(0, 50*SK_Scalar1);
1008 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1009 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001010 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +00001011 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001012 check_direction(reporter, spiral, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001013
reed@google.com7c424812011-05-15 04:38:34 +00001014 SkPath dent;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001015 dent.moveTo(0, 0);
1016 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1017 dent.lineTo(0, 100*SK_Scalar1);
1018 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1019 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001020 dent.close();
1021 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001022 check_direction(reporter, dent, SkPath::kCW_Direction);
reed@google.com7c424812011-05-15 04:38:34 +00001023}
1024
reed@android.com6b82d1a2009-06-03 02:35:01 +00001025static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1026 const SkRect& bounds) {
1027 REPORTER_ASSERT(reporter, p.isConvex());
1028 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +00001029
reed@android.com6b82d1a2009-06-03 02:35:01 +00001030 SkPath p2(p);
1031 REPORTER_ASSERT(reporter, p2.isConvex());
1032 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1033
1034 SkPath other;
1035 other.swap(p2);
1036 REPORTER_ASSERT(reporter, other.isConvex());
1037 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1038}
1039
reed@google.com04863fa2011-05-15 04:08:24 +00001040static void setFromString(SkPath* path, const char str[]) {
1041 bool first = true;
1042 while (str) {
1043 SkScalar x, y;
1044 str = SkParse::FindScalar(str, &x);
1045 if (NULL == str) {
1046 break;
1047 }
1048 str = SkParse::FindScalar(str, &y);
1049 SkASSERT(str);
1050 if (first) {
1051 path->moveTo(x, y);
1052 first = false;
1053 } else {
1054 path->lineTo(x, y);
1055 }
1056 }
1057}
1058
1059static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +00001060 SkPath path;
1061
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001062 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001063 path.addCircle(0, 0, SkIntToScalar(10));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001064 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001065 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001066 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1067
reed@google.com04863fa2011-05-15 04:08:24 +00001068 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001069 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001070 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.com3e71a882012-01-10 18:44:37 +00001071 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCCW_Direction));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001072
reed@google.com04863fa2011-05-15 04:08:24 +00001073 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001074 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001075 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.com3e71a882012-01-10 18:44:37 +00001076 REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCW_Direction));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001077
reed@google.com04863fa2011-05-15 04:08:24 +00001078 static const struct {
1079 const char* fPathStr;
1080 SkPath::Convexity fExpectedConvexity;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001081 SkPath::Direction fExpectedDirection;
reed@google.com04863fa2011-05-15 04:08:24 +00001082 } gRec[] = {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001083 { "", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1084 { "0 0", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1085 { "0 0 10 10", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction },
1086 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPath::kUnknown_Direction },
1087 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPath::kCW_Direction },
1088 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPath::kCCW_Direction },
1089 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
1090 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPath::kCW_Direction },
reed@google.com04863fa2011-05-15 04:08:24 +00001091 };
1092
1093 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1094 SkPath path;
1095 setFromString(&path, gRec[i].fPathStr);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001096 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1097 check_direction(reporter, path, gRec[i].fExpectedDirection);
reed@google.com04863fa2011-05-15 04:08:24 +00001098 }
1099}
1100
reed@google.com7e6c4d12012-05-10 14:05:43 +00001101static void test_isLine(skiatest::Reporter* reporter) {
1102 SkPath path;
1103 SkPoint pts[2];
1104 const SkScalar value = SkIntToScalar(5);
1105
1106 REPORTER_ASSERT(reporter, !path.isLine(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001107
reed@google.com7e6c4d12012-05-10 14:05:43 +00001108 // set some non-zero values
1109 pts[0].set(value, value);
1110 pts[1].set(value, value);
1111 REPORTER_ASSERT(reporter, !path.isLine(pts));
1112 // check that pts was untouched
1113 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1114 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1115
1116 const SkScalar moveX = SkIntToScalar(1);
1117 const SkScalar moveY = SkIntToScalar(2);
1118 SkASSERT(value != moveX && value != moveY);
1119
1120 path.moveTo(moveX, moveY);
1121 REPORTER_ASSERT(reporter, !path.isLine(NULL));
1122 REPORTER_ASSERT(reporter, !path.isLine(pts));
1123 // check that pts was untouched
1124 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1125 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1126
1127 const SkScalar lineX = SkIntToScalar(2);
1128 const SkScalar lineY = SkIntToScalar(2);
1129 SkASSERT(value != lineX && value != lineY);
1130
1131 path.lineTo(lineX, lineY);
1132 REPORTER_ASSERT(reporter, path.isLine(NULL));
1133
1134 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1135 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1136 REPORTER_ASSERT(reporter, path.isLine(pts));
1137 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1138 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1139
1140 path.lineTo(0, 0); // too many points/verbs
1141 REPORTER_ASSERT(reporter, !path.isLine(NULL));
1142 REPORTER_ASSERT(reporter, !path.isLine(pts));
1143 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1144 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1145}
1146
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001147static void test_conservativelyContains(skiatest::Reporter* reporter) {
1148 SkPath path;
1149
1150 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1151 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1152
1153 // A circle that bounds kBaseRect (with a significant amount of slop)
1154 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
1155 circleR = SkScalarMul(circleR, SkFloatToScalar(1.75f)) / 2;
1156 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1157
1158 // round-rect radii
1159 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
skia.committer@gmail.comcec8de62012-11-14 02:01:22 +00001160
caryclark@google.com56f233a2012-11-19 13:06:06 +00001161 static const struct SUPPRESS_VISIBILITY_WARNING {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001162 SkRect fQueryRect;
1163 bool fInRect;
1164 bool fInCircle;
1165 bool fInRR;
1166 } kQueries[] = {
1167 {kBaseRect, true, true, false},
1168
1169 // rect well inside of kBaseRect
1170 {SkRect::MakeLTRB(kBaseRect.fLeft + SkFloatToScalar(0.25f)*kBaseRect.width(),
1171 kBaseRect.fTop + SkFloatToScalar(0.25f)*kBaseRect.height(),
1172 kBaseRect.fRight - SkFloatToScalar(0.25f)*kBaseRect.width(),
1173 kBaseRect.fBottom - SkFloatToScalar(0.25f)*kBaseRect.height()),
1174 true, true, true},
1175
1176 // rects with edges off by one from kBaseRect's edges
1177 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1178 kBaseRect.width(), kBaseRect.height() + 1),
1179 false, true, false},
1180 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1181 kBaseRect.width() + 1, kBaseRect.height()),
1182 false, true, false},
1183 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1184 kBaseRect.width() + 1, kBaseRect.height() + 1),
1185 false, true, false},
1186 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1187 kBaseRect.width(), kBaseRect.height()),
1188 false, true, false},
1189 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1190 kBaseRect.width(), kBaseRect.height()),
1191 false, true, false},
1192 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1193 kBaseRect.width() + 2, kBaseRect.height()),
1194 false, true, false},
1195 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1196 kBaseRect.width() + 2, kBaseRect.height()),
1197 false, true, false},
1198
1199 // zero-w/h rects at each corner of kBaseRect
1200 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false},
1201 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false},
1202 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false},
1203 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false},
1204
1205 // far away rect
1206 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1207 SkIntToScalar(10), SkIntToScalar(10)),
1208 false, false, false},
1209
1210 // very large rect containing kBaseRect
1211 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1212 kBaseRect.fTop - 5 * kBaseRect.height(),
1213 11 * kBaseRect.width(), 11 * kBaseRect.height()),
1214 false, false, false},
1215
1216 // skinny rect that spans same y-range as kBaseRect
1217 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1218 SkIntToScalar(1), kBaseRect.height()),
1219 true, true, true},
1220
1221 // short rect that spans same x-range as kBaseRect
1222 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
1223 true, true, true},
1224
1225 // skinny rect that spans slightly larger y-range than kBaseRect
1226 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1227 SkIntToScalar(1), kBaseRect.height() + 1),
1228 false, true, false},
1229
1230 // short rect that spans slightly larger x-range than kBaseRect
1231 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1232 kBaseRect.width() + 1, SkScalar(1)),
1233 false, true, false},
1234 };
1235
1236 for (int inv = 0; inv < 4; ++inv) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001237 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001238 SkRect qRect = kQueries[q].fQueryRect;
1239 if (inv & 0x1) {
1240 SkTSwap(qRect.fLeft, qRect.fRight);
1241 }
1242 if (inv & 0x2) {
1243 SkTSwap(qRect.fTop, qRect.fBottom);
1244 }
1245 for (int d = 0; d < 2; ++d) {
1246 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1247 path.reset();
1248 path.addRect(kBaseRect, dir);
1249 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1250 path.conservativelyContainsRect(qRect));
1251
1252 path.reset();
1253 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1254 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1255 path.conservativelyContainsRect(qRect));
1256
1257 path.reset();
1258 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1259 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1260 path.conservativelyContainsRect(qRect));
1261 }
1262 // Slightly non-convex shape, shouldn't contain any rects.
1263 path.reset();
1264 path.moveTo(0, 0);
1265 path.lineTo(SkIntToScalar(50), SkFloatToScalar(0.05f));
1266 path.lineTo(SkIntToScalar(100), 0);
1267 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1268 path.lineTo(0, SkIntToScalar(100));
1269 path.close();
1270 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1271 }
1272 }
1273
1274 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1275 path.reset();
1276 path.moveTo(0, 0);
1277 path.lineTo(SkIntToScalar(100), 0);
1278 path.lineTo(0, SkIntToScalar(100));
1279
1280 // inside, on along top edge
1281 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1282 SkIntToScalar(10),
1283 SkIntToScalar(10))));
1284 // above
1285 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1286 SkRect::MakeXYWH(SkIntToScalar(50),
1287 SkIntToScalar(-10),
1288 SkIntToScalar(10),
1289 SkIntToScalar(10))));
1290 // to the left
1291 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1292 SkIntToScalar(5),
1293 SkIntToScalar(5),
1294 SkIntToScalar(5))));
1295
1296 // outside the diagonal edge
1297 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1298 SkIntToScalar(200),
1299 SkIntToScalar(20),
1300 SkIntToScalar(5))));
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001301
1302 // same as above path and first test but with an extra moveTo.
1303 path.reset();
1304 path.moveTo(100, 100);
1305 path.moveTo(0, 0);
1306 path.lineTo(SkIntToScalar(100), 0);
1307 path.lineTo(0, SkIntToScalar(100));
1308
1309 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1310 SkIntToScalar(10),
1311 SkIntToScalar(10))));
1312
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001313}
1314
reed@google.comf32322b2013-10-16 15:14:04 +00001315static void test_isRect_open_close(skiatest::Reporter* reporter) {
1316 SkPath path;
1317 bool isClosed;
1318
1319 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
1320
1321 if (false) {
1322 // I think these should pass, but isRect() doesn't behave
1323 // this way... yet
1324 REPORTER_ASSERT(reporter, path.isRect(NULL, NULL));
1325 REPORTER_ASSERT(reporter, path.isRect(&isClosed, NULL));
1326 REPORTER_ASSERT(reporter, !isClosed);
1327 }
1328
1329 path.close();
1330 REPORTER_ASSERT(reporter, path.isRect(NULL, NULL));
1331 REPORTER_ASSERT(reporter, path.isRect(&isClosed, NULL));
1332 REPORTER_ASSERT(reporter, isClosed);
1333}
1334
caryclark@google.comf1316942011-07-26 19:54:45 +00001335// Simple isRect test is inline TestPath, below.
1336// test_isRect provides more extensive testing.
1337static void test_isRect(skiatest::Reporter* reporter) {
reed@google.comf32322b2013-10-16 15:14:04 +00001338 test_isRect_open_close(reporter);
1339
caryclark@google.comf1316942011-07-26 19:54:45 +00001340 // passing tests (all moveTo / lineTo...
1341 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1342 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1343 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1344 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1345 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1346 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1347 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1348 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1349 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001350 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1351 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 +00001352 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1353 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1354 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
caryclark@google.combfe90372012-11-21 13:56:20 +00001355 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
rmistry@google.comd6176b02012-08-23 18:14:13 +00001356
caryclark@google.comf1316942011-07-26 19:54:45 +00001357 // failing tests
1358 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1359 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1360 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1361 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1362 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1363 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1364 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1365 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
caryclark@google.combfe90372012-11-21 13:56:20 +00001366 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
1367 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
1368 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
rmistry@google.comd6176b02012-08-23 18:14:13 +00001369
caryclark@google.comf1316942011-07-26 19:54:45 +00001370 // failing, no close
1371 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
1372 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
1373
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001374 struct IsRectTest {
1375 SkPoint *fPoints;
1376 size_t fPointCount;
1377 bool fClose;
1378 bool fIsRect;
1379 } tests[] = {
1380 { r1, SK_ARRAY_COUNT(r1), true, true },
1381 { r2, SK_ARRAY_COUNT(r2), true, true },
1382 { r3, SK_ARRAY_COUNT(r3), true, true },
1383 { r4, SK_ARRAY_COUNT(r4), true, true },
1384 { r5, SK_ARRAY_COUNT(r5), true, true },
1385 { r6, SK_ARRAY_COUNT(r6), true, true },
1386 { r7, SK_ARRAY_COUNT(r7), true, true },
1387 { r8, SK_ARRAY_COUNT(r8), true, true },
1388 { r9, SK_ARRAY_COUNT(r9), true, true },
1389 { ra, SK_ARRAY_COUNT(ra), true, true },
1390 { rb, SK_ARRAY_COUNT(rb), true, true },
1391 { rc, SK_ARRAY_COUNT(rc), true, true },
1392 { rd, SK_ARRAY_COUNT(rd), true, true },
1393 { re, SK_ARRAY_COUNT(re), true, true },
1394 { rf, SK_ARRAY_COUNT(rf), true, true },
1395
1396 { f1, SK_ARRAY_COUNT(f1), true, false },
1397 { f2, SK_ARRAY_COUNT(f2), true, false },
1398 { f3, SK_ARRAY_COUNT(f3), true, false },
1399 { f4, SK_ARRAY_COUNT(f4), true, false },
1400 { f5, SK_ARRAY_COUNT(f5), true, false },
1401 { f6, SK_ARRAY_COUNT(f6), true, false },
1402 { f7, SK_ARRAY_COUNT(f7), true, false },
1403 { f8, SK_ARRAY_COUNT(f8), true, false },
1404 { f9, SK_ARRAY_COUNT(f9), true, false },
1405 { fa, SK_ARRAY_COUNT(fa), true, false },
1406 { fb, SK_ARRAY_COUNT(fb), true, false },
1407
1408 { c1, SK_ARRAY_COUNT(c1), false, false },
1409 { c2, SK_ARRAY_COUNT(c2), false, false },
caryclark@google.comf1316942011-07-26 19:54:45 +00001410 };
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001411
1412 const size_t testCount = SK_ARRAY_COUNT(tests);
caryclark@google.comf1316942011-07-26 19:54:45 +00001413 size_t index;
1414 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1415 SkPath path;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001416 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1417 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1418 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.comf1316942011-07-26 19:54:45 +00001419 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001420 if (tests[testIndex].fClose) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001421 path.close();
1422 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001423 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(NULL));
1424 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(NULL, NULL));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001425
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001426 if (tests[testIndex].fIsRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001427 SkRect computed, expected;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001428 expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001429 REPORTER_ASSERT(reporter, path.isRect(&computed));
1430 REPORTER_ASSERT(reporter, expected == computed);
skia.committer@gmail.com1c9c0d32012-11-22 02:02:41 +00001431
caryclark@google.comf68154a2012-11-21 15:18:06 +00001432 bool isClosed;
1433 SkPath::Direction direction, cheapDirection;
1434 REPORTER_ASSERT(reporter, path.cheapComputeDirection(&cheapDirection));
robertphillips@google.com8fd16032013-06-25 15:39:58 +00001435 REPORTER_ASSERT(reporter, path.isRect(&isClosed, &direction));
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001436 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001437 REPORTER_ASSERT(reporter, direction == cheapDirection);
1438 } else {
1439 SkRect computed;
1440 computed.set(123, 456, 789, 1011);
1441 REPORTER_ASSERT(reporter, !path.isRect(&computed));
1442 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
1443 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
1444
1445 bool isClosed = (bool) -1;
1446 SkPath::Direction direction = (SkPath::Direction) -1;
robertphillips@google.com8fd16032013-06-25 15:39:58 +00001447 REPORTER_ASSERT(reporter, !path.isRect(&isClosed, &direction));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001448 REPORTER_ASSERT(reporter, isClosed == (bool) -1);
1449 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001450 }
caryclark@google.comf1316942011-07-26 19:54:45 +00001451 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001452
caryclark@google.comf1316942011-07-26 19:54:45 +00001453 // fail, close then line
1454 SkPath path1;
1455 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001456 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001457 path1.lineTo(r1[index].fX, r1[index].fY);
1458 }
1459 path1.close();
1460 path1.lineTo(1, 0);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001461 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001462
caryclark@google.comf1316942011-07-26 19:54:45 +00001463 // fail, move in the middle
1464 path1.reset();
1465 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001466 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001467 if (index == 2) {
1468 path1.moveTo(1, .5f);
1469 }
1470 path1.lineTo(r1[index].fX, r1[index].fY);
1471 }
1472 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001473 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00001474
1475 // fail, move on the edge
1476 path1.reset();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001477 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001478 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
1479 path1.lineTo(r1[index].fX, r1[index].fY);
1480 }
1481 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001482 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001483
caryclark@google.comf1316942011-07-26 19:54:45 +00001484 // fail, quad
1485 path1.reset();
1486 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001487 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001488 if (index == 2) {
1489 path1.quadTo(1, .5f, 1, .5f);
1490 }
1491 path1.lineTo(r1[index].fX, r1[index].fY);
1492 }
1493 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001494 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001495
caryclark@google.comf1316942011-07-26 19:54:45 +00001496 // fail, cubic
1497 path1.reset();
1498 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001499 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001500 if (index == 2) {
1501 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
1502 }
1503 path1.lineTo(r1[index].fX, r1[index].fY);
1504 }
1505 path1.close();
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001506 REPORTER_ASSERT(reporter, !path1.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00001507}
1508
caryclark@google.com56f233a2012-11-19 13:06:06 +00001509static void test_isNestedRects(skiatest::Reporter* reporter) {
1510 // passing tests (all moveTo / lineTo...
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001511 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001512 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1513 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1514 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001515 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001516 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1517 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1518 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1519 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001520 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
1521 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 +00001522 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
1523 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
1524 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001525
1526 // failing tests
1527 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1528 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1529 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1530 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1531 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1532 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1533 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1534 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
1535
1536 // failing, no close
1537 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
1538 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
1539
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001540 struct IsNestedRectTest {
1541 SkPoint *fPoints;
1542 size_t fPointCount;
1543 SkPath::Direction fDirection;
1544 bool fClose;
1545 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
1546 } tests[] = {
1547 { r1, SK_ARRAY_COUNT(r1), SkPath::kCW_Direction , true, true },
1548 { r2, SK_ARRAY_COUNT(r2), SkPath::kCW_Direction , true, true },
1549 { r3, SK_ARRAY_COUNT(r3), SkPath::kCW_Direction , true, true },
1550 { r4, SK_ARRAY_COUNT(r4), SkPath::kCW_Direction , true, true },
1551 { r5, SK_ARRAY_COUNT(r5), SkPath::kCCW_Direction, true, true },
1552 { r6, SK_ARRAY_COUNT(r6), SkPath::kCCW_Direction, true, true },
1553 { r7, SK_ARRAY_COUNT(r7), SkPath::kCCW_Direction, true, true },
1554 { r8, SK_ARRAY_COUNT(r8), SkPath::kCCW_Direction, true, true },
1555 { r9, SK_ARRAY_COUNT(r9), SkPath::kCCW_Direction, true, true },
1556 { ra, SK_ARRAY_COUNT(ra), SkPath::kCCW_Direction, true, true },
1557 { rb, SK_ARRAY_COUNT(rb), SkPath::kCW_Direction, true, true },
1558 { rc, SK_ARRAY_COUNT(rc), SkPath::kCW_Direction, true, true },
1559 { rd, SK_ARRAY_COUNT(rd), SkPath::kCCW_Direction, true, true },
1560 { re, SK_ARRAY_COUNT(re), SkPath::kCW_Direction, true, true },
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001561
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001562 { f1, SK_ARRAY_COUNT(f1), SkPath::kUnknown_Direction, true, false },
1563 { f2, SK_ARRAY_COUNT(f2), SkPath::kUnknown_Direction, true, false },
1564 { f3, SK_ARRAY_COUNT(f3), SkPath::kUnknown_Direction, true, false },
1565 { f4, SK_ARRAY_COUNT(f4), SkPath::kUnknown_Direction, true, false },
1566 { f5, SK_ARRAY_COUNT(f5), SkPath::kUnknown_Direction, true, false },
1567 { f6, SK_ARRAY_COUNT(f6), SkPath::kUnknown_Direction, true, false },
1568 { f7, SK_ARRAY_COUNT(f7), SkPath::kUnknown_Direction, true, false },
1569 { f8, SK_ARRAY_COUNT(f8), SkPath::kUnknown_Direction, true, false },
1570
1571 { c1, SK_ARRAY_COUNT(c1), SkPath::kUnknown_Direction, false, false },
1572 { c2, SK_ARRAY_COUNT(c2), SkPath::kUnknown_Direction, false, false },
1573 };
1574
1575 const size_t testCount = SK_ARRAY_COUNT(tests);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001576 size_t index;
1577 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001578 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1579 SkPath path;
1580 if (rectFirst) {
1581 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1582 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001583 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1584 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1585 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001586 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001587 if (tests[testIndex].fClose) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001588 path.close();
1589 }
1590 if (!rectFirst) {
1591 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1592 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001593 REPORTER_ASSERT(reporter, tests[testIndex].fIsNestedRect == path.isNestedRects(NULL));
1594 if (tests[testIndex].fIsNestedRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001595 SkRect expected[2], computed[2];
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001596 SkPath::Direction expectedDirs[2], computedDirs[2];
caryclark@google.com56f233a2012-11-19 13:06:06 +00001597 SkRect testBounds;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001598 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001599 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
1600 expected[1] = testBounds;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001601 if (rectFirst) {
1602 expectedDirs[0] = SkPath::kCW_Direction;
1603 } else {
1604 expectedDirs[0] = SkPath::kCCW_Direction;
1605 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001606 expectedDirs[1] = tests[testIndex].fDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001607 REPORTER_ASSERT(reporter, path.isNestedRects(computed, computedDirs));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001608 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
1609 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001610 REPORTER_ASSERT(reporter, expectedDirs[0] == computedDirs[0]);
1611 REPORTER_ASSERT(reporter, expectedDirs[1] == computedDirs[1]);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001612 }
caryclark@google.com56f233a2012-11-19 13:06:06 +00001613 }
1614
1615 // fail, close then line
1616 SkPath path1;
1617 if (rectFirst) {
1618 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1619 }
1620 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001621 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001622 path1.lineTo(r1[index].fX, r1[index].fY);
1623 }
1624 path1.close();
1625 path1.lineTo(1, 0);
1626 if (!rectFirst) {
1627 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1628 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001629 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001630
1631 // fail, move in the middle
1632 path1.reset();
1633 if (rectFirst) {
1634 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1635 }
1636 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001637 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001638 if (index == 2) {
1639 path1.moveTo(1, .5f);
1640 }
1641 path1.lineTo(r1[index].fX, r1[index].fY);
1642 }
1643 path1.close();
1644 if (!rectFirst) {
1645 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1646 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001647 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001648
1649 // fail, move on the edge
1650 path1.reset();
1651 if (rectFirst) {
1652 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1653 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001654 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001655 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
1656 path1.lineTo(r1[index].fX, r1[index].fY);
1657 }
1658 path1.close();
1659 if (!rectFirst) {
1660 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1661 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001662 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001663
1664 // fail, quad
1665 path1.reset();
1666 if (rectFirst) {
1667 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1668 }
1669 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001670 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001671 if (index == 2) {
1672 path1.quadTo(1, .5f, 1, .5f);
1673 }
1674 path1.lineTo(r1[index].fX, r1[index].fY);
1675 }
1676 path1.close();
1677 if (!rectFirst) {
1678 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1679 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001680 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001681
1682 // fail, cubic
1683 path1.reset();
1684 if (rectFirst) {
1685 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
1686 }
1687 path1.moveTo(r1[0].fX, r1[0].fY);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001688 for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001689 if (index == 2) {
1690 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
1691 }
1692 path1.lineTo(r1[index].fX, r1[index].fY);
1693 }
1694 path1.close();
1695 if (!rectFirst) {
1696 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
1697 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001698 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
skia.committer@gmail.com34587162012-11-20 02:01:23 +00001699
caryclark@google.com56f233a2012-11-19 13:06:06 +00001700 // fail, not nested
1701 path1.reset();
1702 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
1703 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001704 REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001705 }
caryclark@google.combfe90372012-11-21 13:56:20 +00001706
1707 // pass, stroke rect
1708 SkPath src, dst;
1709 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
1710 SkPaint strokePaint;
1711 strokePaint.setStyle(SkPaint::kStroke_Style);
1712 strokePaint.setStrokeWidth(2);
1713 strokePaint.getFillPath(src, &dst);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001714 REPORTER_ASSERT(reporter, dst.isNestedRects(NULL));
caryclark@google.com56f233a2012-11-19 13:06:06 +00001715}
1716
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001717static void write_and_read_back(skiatest::Reporter* reporter,
1718 const SkPath& p) {
1719 SkWriter32 writer(100);
1720 writer.writePath(p);
1721 size_t size = writer.size();
1722 SkAutoMalloc storage(size);
1723 writer.flatten(storage.get());
1724 SkReader32 reader(storage.get(), size);
1725
1726 SkPath readBack;
1727 REPORTER_ASSERT(reporter, readBack != p);
1728 reader.readPath(&readBack);
1729 REPORTER_ASSERT(reporter, readBack == p);
1730
rmistry@google.comd6176b02012-08-23 18:14:13 +00001731 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001732 p.getConvexityOrUnknown());
1733
1734 REPORTER_ASSERT(reporter, readBack.isOval(NULL) == p.isOval(NULL));
1735
1736 const SkRect& origBounds = p.getBounds();
1737 const SkRect& readBackBounds = readBack.getBounds();
1738
1739 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
1740}
1741
reed@google.com53effc52011-09-21 19:05:12 +00001742static void test_flattening(skiatest::Reporter* reporter) {
1743 SkPath p;
1744
1745 static const SkPoint pts[] = {
1746 { 0, 0 },
1747 { SkIntToScalar(10), SkIntToScalar(10) },
1748 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
1749 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
1750 };
1751 p.moveTo(pts[0]);
1752 p.lineTo(pts[1]);
1753 p.quadTo(pts[2], pts[3]);
1754 p.cubicTo(pts[4], pts[5], pts[6]);
1755
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001756 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00001757
1758 // create a buffer that should be much larger than the path so we don't
1759 // kill our stack if writer goes too far.
1760 char buffer[1024];
1761 uint32_t size1 = p.writeToMemory(NULL);
1762 uint32_t size2 = p.writeToMemory(buffer);
1763 REPORTER_ASSERT(reporter, size1 == size2);
1764
1765 SkPath p2;
1766 uint32_t size3 = p2.readFromMemory(buffer);
1767 REPORTER_ASSERT(reporter, size1 == size3);
1768 REPORTER_ASSERT(reporter, p == p2);
1769
1770 char buffer2[1024];
1771 size3 = p2.writeToMemory(buffer2);
1772 REPORTER_ASSERT(reporter, size1 == size3);
1773 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00001774
1775 // test persistence of the oval flag & convexity
1776 {
1777 SkPath oval;
1778 SkRect rect = SkRect::MakeWH(10, 10);
1779 oval.addOval(rect);
1780
1781 write_and_read_back(reporter, oval);
1782 }
reed@google.com53effc52011-09-21 19:05:12 +00001783}
1784
1785static void test_transform(skiatest::Reporter* reporter) {
1786 SkPath p, p1;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001787
reed@google.com53effc52011-09-21 19:05:12 +00001788 static const SkPoint pts[] = {
1789 { 0, 0 },
1790 { SkIntToScalar(10), SkIntToScalar(10) },
1791 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
1792 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
1793 };
1794 p.moveTo(pts[0]);
1795 p.lineTo(pts[1]);
1796 p.quadTo(pts[2], pts[3]);
1797 p.cubicTo(pts[4], pts[5], pts[6]);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001798
reed@google.com53effc52011-09-21 19:05:12 +00001799 SkMatrix matrix;
1800 matrix.reset();
1801 p.transform(matrix, &p1);
1802 REPORTER_ASSERT(reporter, p == p1);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001803
reed@google.com53effc52011-09-21 19:05:12 +00001804 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
1805 p.transform(matrix, &p1);
1806 SkPoint pts1[7];
1807 int count = p1.getPoints(pts1, 7);
1808 REPORTER_ASSERT(reporter, 7 == count);
1809 for (int i = 0; i < count; ++i) {
1810 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
1811 REPORTER_ASSERT(reporter, newPt == pts1[i]);
1812 }
1813}
1814
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001815static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001816 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00001817 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001818
caryclark@google.com56f233a2012-11-19 13:06:06 +00001819 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
schenney@chromium.org7e963602012-06-13 17:05:43 +00001820 const char* testPath;
1821 const size_t numResultPts;
1822 const SkRect resultBound;
1823 const SkPath::Verb* resultVerbs;
1824 const size_t numResultVerbs;
1825 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001826
schenney@chromium.org7e963602012-06-13 17:05:43 +00001827 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
1828 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
1829 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
1830 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
1831 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
1832 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
1833 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
1834 static const SkPath::Verb resultVerbs8[] = {
1835 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
1836 };
1837 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
1838 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
1839 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
1840 static const SkPath::Verb resultVerbs12[] = {
1841 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
1842 };
1843 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
1844 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
1845 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
1846 static const SkPath::Verb resultVerbs16[] = {
1847 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
1848 };
1849 static const struct zeroPathTestData gZeroLengthTests[] = {
1850 { "M 1 1", 1, {0, 0, 0, 0}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001851 { "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 +00001852 { "M 1 1 z", 1, {0, 0, 0, 0}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001853 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
1854 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
1855 { "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) },
1856 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
1857 { "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) },
1858 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
1859 { "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) },
1860 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
1861 { "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) },
1862 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
1863 { "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 +00001864 SK_ARRAY_COUNT(resultVerbs14)
1865 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001866 { "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) },
1867 { "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 +00001868 SK_ARRAY_COUNT(resultVerbs16)
1869 }
1870 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001871
schenney@chromium.org7e963602012-06-13 17:05:43 +00001872 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
1873 p.reset();
1874 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
1875 REPORTER_ASSERT(reporter, valid);
1876 REPORTER_ASSERT(reporter, !p.isEmpty());
1877 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
1878 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
1879 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
1880 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
1881 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
1882 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00001883 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00001884}
1885
1886struct SegmentInfo {
1887 SkPath fPath;
1888 int fPointCount;
1889};
1890
reed@google.com10296cc2011-09-21 12:29:05 +00001891#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
1892
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001893static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00001894 SkPath p, p2;
1895
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001896 p.moveTo(0, 0);
1897 p.quadTo(100, 100, 200, 200);
1898 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
1899 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00001900 p2 = p;
1901 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001902 p.cubicTo(100, 100, 200, 200, 300, 300);
1903 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
1904 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00001905 p2 = p;
1906 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
1907
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001908 p.reset();
1909 p.moveTo(0, 0);
1910 p.cubicTo(100, 100, 200, 200, 300, 300);
1911 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00001912 p2 = p;
1913 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001914
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001915 REPORTER_ASSERT(reporter, !p.isEmpty());
1916}
1917
1918static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00001919 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001920 SkPoint pts[4];
1921
1922 // Test an iterator with no path
1923 SkPath::Iter noPathIter;
1924 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00001925
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001926 // Test that setting an empty path works
1927 noPathIter.setPath(p, false);
1928 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00001929
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001930 // Test that close path makes no difference for an empty path
1931 noPathIter.setPath(p, true);
1932 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00001933
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001934 // Test an iterator with an initial empty path
1935 SkPath::Iter iter(p, false);
1936 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1937
1938 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00001939 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001940 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
1941
rmistry@google.comd6176b02012-08-23 18:14:13 +00001942
schenney@chromium.org7e963602012-06-13 17:05:43 +00001943 struct iterTestData {
1944 const char* testPath;
1945 const bool forceClose;
1946 const bool consumeDegenerates;
1947 const size_t* numResultPtsPerVerb;
1948 const SkPoint* resultPts;
1949 const SkPath::Verb* resultVerbs;
1950 const size_t numResultVerbs;
1951 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00001952
schenney@chromium.org7e963602012-06-13 17:05:43 +00001953 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
1954 static const SkPath::Verb resultVerbs2[] = {
1955 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
1956 };
1957 static const SkPath::Verb resultVerbs3[] = {
1958 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
1959 };
1960 static const SkPath::Verb resultVerbs4[] = {
1961 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
1962 };
1963 static const SkPath::Verb resultVerbs5[] = {
1964 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
1965 };
1966 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00001967 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
1968 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
1969 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
1970 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001971 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00001972 static const SkPoint resultPts2[] = {
1973 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
1974 };
1975 static const SkPoint resultPts3[] = {
1976 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
1977 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
1978 };
1979 static const SkPoint resultPts4[] = {
1980 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
1981 };
1982 static const SkPoint resultPts5[] = {
1983 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
1984 };
1985 static const struct iterTestData gIterTests[] = {
1986 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00001987 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1988 { "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 +00001989 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1990 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1991 { "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) },
1992 { "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 +00001993 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
1994 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
1995 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1996 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
1997 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
1998 { "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 +00001999 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002000
schenney@chromium.org7e963602012-06-13 17:05:43 +00002001 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2002 p.reset();
2003 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2004 REPORTER_ASSERT(reporter, valid);
2005 iter.setPath(p, gIterTests[i].forceClose);
2006 int j = 0, l = 0;
2007 do {
2008 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2009 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2010 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2011 }
2012 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2013 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2014 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002015
2016 // The GM degeneratesegments.cpp test is more extensive
2017}
2018
2019static void test_raw_iter(skiatest::Reporter* reporter) {
2020 SkPath p;
2021 SkPoint pts[4];
2022
2023 // Test an iterator with no path
2024 SkPath::RawIter noPathIter;
2025 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2026 // Test that setting an empty path works
2027 noPathIter.setPath(p);
2028 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00002029
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002030 // Test an iterator with an initial empty path
2031 SkPath::RawIter iter(p);
2032 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2033
2034 // Test that a move-only path returns the move.
2035 p.moveTo(SK_Scalar1, 0);
2036 iter.setPath(p);
2037 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2038 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2039 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2040 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2041
2042 // No matter how many moves we add, we should get them all back
2043 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2044 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2045 iter.setPath(p);
2046 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2047 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2048 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2049 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2050 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2051 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2052 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2053 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2054 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2055 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2056
2057 // Initial close is never ever stored
2058 p.reset();
2059 p.close();
2060 iter.setPath(p);
2061 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2062
2063 // Move/close sequences
2064 p.reset();
2065 p.close(); // Not stored, no purpose
2066 p.moveTo(SK_Scalar1, 0);
2067 p.close();
2068 p.close(); // Not stored, no purpose
2069 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2070 p.close();
2071 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2072 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
2073 p.close();
2074 iter.setPath(p);
2075 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2076 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2077 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2078 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2079 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2080 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2081 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2082 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2083 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2084 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2085 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2086 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2087 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2088 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2089 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2090 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2091 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2092 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2093 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2094 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2095 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2096 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2097
2098 // Generate random paths and verify
2099 SkPoint randomPts[25];
2100 for (int i = 0; i < 5; ++i) {
2101 for (int j = 0; j < 5; ++j) {
2102 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
2103 }
2104 }
2105
2106 // Max of 10 segments, max 3 points per segment
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002107 SkRandom rand(9876543);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002108 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00002109 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002110 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00002111
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002112 for (int i = 0; i < 500; ++i) {
2113 p.reset();
2114 bool lastWasClose = true;
2115 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00002116 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002117 int numPoints = 0;
2118 int numVerbs = (rand.nextU() >> 16) % 10;
2119 int numIterVerbs = 0;
2120 for (int j = 0; j < numVerbs; ++j) {
2121 do {
2122 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
2123 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002124 switch (nextVerb) {
2125 case SkPath::kMove_Verb:
2126 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2127 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00002128 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002129 numPoints += 1;
2130 lastWasClose = false;
2131 haveMoveTo = true;
2132 break;
2133 case SkPath::kLine_Verb:
2134 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002135 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002136 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2137 haveMoveTo = true;
2138 }
2139 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2140 p.lineTo(expectedPts[numPoints]);
2141 numPoints += 1;
2142 lastWasClose = false;
2143 break;
2144 case SkPath::kQuad_Verb:
2145 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002146 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002147 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2148 haveMoveTo = true;
2149 }
2150 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2151 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2152 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
2153 numPoints += 2;
2154 lastWasClose = false;
2155 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002156 case SkPath::kConic_Verb:
2157 if (!haveMoveTo) {
2158 expectedPts[numPoints++] = lastMoveToPt;
2159 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2160 haveMoveTo = true;
2161 }
2162 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2163 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2164 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2165 rand.nextUScalar1() * 4);
2166 numPoints += 2;
2167 lastWasClose = false;
2168 break;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002169 case SkPath::kCubic_Verb:
2170 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002171 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002172 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2173 haveMoveTo = true;
2174 }
2175 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2176 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2177 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
2178 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2179 expectedPts[numPoints + 2]);
2180 numPoints += 3;
2181 lastWasClose = false;
2182 break;
2183 case SkPath::kClose_Verb:
2184 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00002185 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002186 lastWasClose = true;
2187 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002188 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002189 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002190 }
2191 expectedVerbs[numIterVerbs++] = nextVerb;
2192 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00002193
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002194 iter.setPath(p);
2195 numVerbs = numIterVerbs;
2196 numIterVerbs = 0;
2197 int numIterPts = 0;
2198 SkPoint lastMoveTo;
2199 SkPoint lastPt;
2200 lastMoveTo.set(0, 0);
2201 lastPt.set(0, 0);
2202 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
2203 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
2204 numIterVerbs++;
2205 switch (nextVerb) {
2206 case SkPath::kMove_Verb:
2207 REPORTER_ASSERT(reporter, numIterPts < numPoints);
2208 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
2209 lastPt = lastMoveTo = pts[0];
2210 numIterPts += 1;
2211 break;
2212 case SkPath::kLine_Verb:
2213 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
2214 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2215 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2216 lastPt = pts[1];
2217 numIterPts += 1;
2218 break;
2219 case SkPath::kQuad_Verb:
reed@google.com277c3f82013-05-31 15:17:50 +00002220 case SkPath::kConic_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002221 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
2222 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2223 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2224 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2225 lastPt = pts[2];
2226 numIterPts += 2;
2227 break;
2228 case SkPath::kCubic_Verb:
2229 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
2230 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2231 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2232 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2233 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
2234 lastPt = pts[3];
2235 numIterPts += 3;
2236 break;
2237 case SkPath::kClose_Verb:
2238 REPORTER_ASSERT(reporter, pts[0] == lastMoveTo);
2239 lastPt = lastMoveTo;
2240 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002241 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002242 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002243 }
2244 }
2245 REPORTER_ASSERT(reporter, numIterPts == numPoints);
2246 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
2247 }
2248}
2249
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002250static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002251 const SkPath& path,
2252 bool expectedCircle,
2253 SkPath::Direction expectedDir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002254 SkRect rect;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002255 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
2256 REPORTER_ASSERT(reporter, path.cheapIsDirection(expectedDir));
skia.committer@gmail.comfbb0ed92012-11-13 21:46:06 +00002257
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002258 if (expectedCircle) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002259 REPORTER_ASSERT(reporter, rect.height() == rect.width());
2260 }
2261}
2262
2263static void test_circle_skew(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002264 const SkPath& path,
2265 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002266 SkPath tmp;
2267
2268 SkMatrix m;
2269 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
2270 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002271 // this matrix reverses the direction.
2272 if (SkPath::kCCW_Direction == dir) {
2273 dir = SkPath::kCW_Direction;
2274 } else {
2275 SkASSERT(SkPath::kCW_Direction == dir);
2276 dir = SkPath::kCCW_Direction;
2277 }
2278 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002279}
2280
2281static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002282 const SkPath& path,
2283 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002284 SkPath tmp;
2285
2286 // translate at small offset
2287 SkMatrix m;
2288 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
2289 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002290 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002291
2292 tmp.reset();
2293 m.reset();
2294
2295 // translate at a relatively big offset
2296 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
2297 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002298 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002299}
2300
2301static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002302 const SkPath& path,
2303 SkPath::Direction dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002304 for (int angle = 0; angle < 360; ++angle) {
2305 SkPath tmp;
2306 SkMatrix m;
2307 m.setRotate(SkIntToScalar(angle));
2308 path.transform(m, &tmp);
2309
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002310 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002311 // degrees is not an oval anymore, this can be improved. we made this
2312 // for the simplicity of our implementation.
2313 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002314 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002315 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002316 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002317 }
2318 }
2319}
2320
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002321static void test_circle_mirror_x(skiatest::Reporter* reporter,
2322 const SkPath& path,
2323 SkPath::Direction dir) {
2324 SkPath tmp;
2325 SkMatrix m;
2326 m.reset();
2327 m.setScaleX(-SK_Scalar1);
2328 path.transform(m, &tmp);
2329
2330 if (SkPath::kCW_Direction == dir) {
2331 dir = SkPath::kCCW_Direction;
2332 } else {
2333 SkASSERT(SkPath::kCCW_Direction == dir);
2334 dir = SkPath::kCW_Direction;
2335 }
2336
2337 check_for_circle(reporter, tmp, true, dir);
2338}
2339
2340static void test_circle_mirror_y(skiatest::Reporter* reporter,
2341 const SkPath& path,
2342 SkPath::Direction dir) {
2343 SkPath tmp;
2344 SkMatrix m;
2345 m.reset();
2346 m.setScaleY(-SK_Scalar1);
2347 path.transform(m, &tmp);
2348
2349 if (SkPath::kCW_Direction == dir) {
2350 dir = SkPath::kCCW_Direction;
2351 } else {
2352 SkASSERT(SkPath::kCCW_Direction == dir);
2353 dir = SkPath::kCW_Direction;
2354 }
2355
2356 check_for_circle(reporter, tmp, true, dir);
2357}
2358
2359static void test_circle_mirror_xy(skiatest::Reporter* reporter,
2360 const SkPath& path,
2361 SkPath::Direction dir) {
2362 SkPath tmp;
2363 SkMatrix m;
2364 m.reset();
2365 m.setScaleX(-SK_Scalar1);
2366 m.setScaleY(-SK_Scalar1);
2367 path.transform(m, &tmp);
2368
2369 check_for_circle(reporter, tmp, true, dir);
2370}
2371
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002372static void test_circle_with_direction(skiatest::Reporter* reporter,
2373 SkPath::Direction dir) {
2374 SkPath path;
2375
2376 // circle at origin
2377 path.addCircle(0, 0, SkIntToScalar(20), dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002378 check_for_circle(reporter, path, true, dir);
2379 test_circle_rotate(reporter, path, dir);
2380 test_circle_translate(reporter, path, dir);
2381 test_circle_skew(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002382
2383 // circle at an offset at (10, 10)
2384 path.reset();
2385 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
2386 SkIntToScalar(20), dir);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002387 check_for_circle(reporter, path, true, dir);
2388 test_circle_rotate(reporter, path, dir);
2389 test_circle_translate(reporter, path, dir);
2390 test_circle_skew(reporter, path, dir);
2391 test_circle_mirror_x(reporter, path, dir);
2392 test_circle_mirror_y(reporter, path, dir);
2393 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002394}
2395
2396static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
2397 SkPath path;
2398 SkPath circle;
2399 SkPath rect;
2400 SkPath empty;
2401
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002402 static const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
2403 static const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
2404
2405 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002406 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
2407 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
2408
2409 SkMatrix translate;
2410 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
2411
2412 // For simplicity, all the path concatenation related operations
2413 // would mark it non-circle, though in theory it's still a circle.
2414
2415 // empty + circle (translate)
2416 path = empty;
2417 path.addPath(circle, translate);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002418 check_for_circle(reporter, path, false, kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002419
2420 // circle + empty (translate)
2421 path = circle;
2422 path.addPath(empty, translate);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002423 check_for_circle(reporter, path, false, kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002424
2425 // test reverseAddPath
2426 path = circle;
2427 path.reverseAddPath(rect);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002428 check_for_circle(reporter, path, false, kCircleDirOpposite);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002429}
2430
2431static void test_circle(skiatest::Reporter* reporter) {
2432 test_circle_with_direction(reporter, SkPath::kCW_Direction);
2433 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
2434
2435 // multiple addCircle()
2436 SkPath path;
2437 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2438 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002439 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002440
2441 // some extra lineTo() would make isOval() fail
2442 path.reset();
2443 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2444 path.lineTo(0, 0);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002445 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002446
2447 // not back to the original point
2448 path.reset();
2449 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
2450 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002451 check_for_circle(reporter, path, false, SkPath::kCW_Direction);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002452
2453 test_circle_with_add_paths(reporter);
2454}
2455
2456static void test_oval(skiatest::Reporter* reporter) {
2457 SkRect rect;
2458 SkMatrix m;
2459 SkPath path;
2460
2461 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
2462 path.addOval(rect);
2463
2464 REPORTER_ASSERT(reporter, path.isOval(NULL));
2465
2466 m.setRotate(SkIntToScalar(90));
2467 SkPath tmp;
2468 path.transform(m, &tmp);
2469 // an oval rotated 90 degrees is still an oval.
2470 REPORTER_ASSERT(reporter, tmp.isOval(NULL));
2471
2472 m.reset();
2473 m.setRotate(SkIntToScalar(30));
2474 tmp.reset();
2475 path.transform(m, &tmp);
2476 // an oval rotated 30 degrees is not an oval anymore.
2477 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2478
2479 // since empty path being transformed.
2480 path.reset();
2481 tmp.reset();
2482 m.reset();
2483 path.transform(m, &tmp);
2484 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2485
2486 // empty path is not an oval
2487 tmp.reset();
2488 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2489
2490 // only has moveTo()s
2491 tmp.reset();
2492 tmp.moveTo(0, 0);
2493 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
2494 REPORTER_ASSERT(reporter, !tmp.isOval(NULL));
2495
2496 // mimic WebKit's calling convention,
2497 // call moveTo() first and then call addOval()
2498 path.reset();
2499 path.moveTo(0, 0);
2500 path.addOval(rect);
2501 REPORTER_ASSERT(reporter, path.isOval(NULL));
2502
2503 // copy path
2504 path.reset();
2505 tmp.reset();
2506 tmp.addOval(rect);
2507 path = tmp;
2508 REPORTER_ASSERT(reporter, path.isOval(NULL));
2509}
2510
bungeman@google.coma5809a32013-06-21 15:13:34 +00002511static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
2512 SkPath empty;
reed@android.com80e39a72009-04-02 16:59:40 +00002513
reed@android.com3abec1d2009-03-02 05:36:20 +00002514 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002515 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002516 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00002517 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00002518 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00002519 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
2520 REPORTER_ASSERT(reporter, !p.isInverseFillType());
bungeman@google.coma5809a32013-06-21 15:13:34 +00002521 REPORTER_ASSERT(reporter, p == empty);
2522 REPORTER_ASSERT(reporter, !(p != empty));
2523}
2524
2525static void TestPath(skiatest::Reporter* reporter) {
2526 SkTSize<SkScalar>::Make(3,4);
2527
2528 SkPath p, empty;
2529 SkRect bounds, bounds2;
2530 test_empty(reporter, p);
reed@android.com3abec1d2009-03-02 05:36:20 +00002531
reed@android.comd252db02009-04-01 18:31:44 +00002532 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00002533
reed@android.com3abec1d2009-03-02 05:36:20 +00002534 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00002535
reed@android.com6b82d1a2009-06-03 02:35:01 +00002536 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
2537 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00002538 // we have quads or cubics
2539 REPORTER_ASSERT(reporter, p.getSegmentMasks() & kCurveSegmentMask);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002540 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00002541
reed@android.com6b82d1a2009-06-03 02:35:01 +00002542 p.reset();
bungeman@google.coma5809a32013-06-21 15:13:34 +00002543 test_empty(reporter, p);
reed@google.com10296cc2011-09-21 12:29:05 +00002544
reed@android.com6b82d1a2009-06-03 02:35:01 +00002545 p.addOval(bounds);
2546 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002547 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00002548
bungeman@google.coma5809a32013-06-21 15:13:34 +00002549 p.rewind();
2550 test_empty(reporter, p);
2551
reed@android.com3abec1d2009-03-02 05:36:20 +00002552 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00002553 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00002554 // we have only lines
2555 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002556 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00002557
bungeman@google.coma5809a32013-06-21 15:13:34 +00002558 REPORTER_ASSERT(reporter, p != empty);
2559 REPORTER_ASSERT(reporter, !(p == empty));
reed@android.com3abec1d2009-03-02 05:36:20 +00002560
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002561 // do getPoints and getVerbs return the right result
2562 REPORTER_ASSERT(reporter, p.getPoints(NULL, 0) == 4);
2563 REPORTER_ASSERT(reporter, p.getVerbs(NULL, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00002564 SkPoint pts[4];
2565 int count = p.getPoints(pts, 4);
2566 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002567 uint8_t verbs[6];
2568 verbs[5] = 0xff;
2569 p.getVerbs(verbs, 5);
2570 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
2571 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
2572 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
2573 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
2574 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
2575 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00002576 bounds2.set(pts, 4);
2577 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00002578
reed@android.com3abec1d2009-03-02 05:36:20 +00002579 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
2580 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00002581 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00002582
reed@android.com3abec1d2009-03-02 05:36:20 +00002583 REPORTER_ASSERT(reporter, p.isRect(NULL));
caryclark@google.comf1316942011-07-26 19:54:45 +00002584 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00002585 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
2586 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00002587
reed@android.com3abec1d2009-03-02 05:36:20 +00002588 // now force p to not be a rect
2589 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
2590 p.addRect(bounds);
2591 REPORTER_ASSERT(reporter, !p.isRect(NULL));
reed@android.com3abec1d2009-03-02 05:36:20 +00002592
reed@google.com7e6c4d12012-05-10 14:05:43 +00002593 test_isLine(reporter);
2594 test_isRect(reporter);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002595 test_isNestedRects(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002596 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00002597 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00002598 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00002599 test_convexity2(reporter);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00002600 test_conservativelyContains(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00002601 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002602 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00002603 test_flattening(reporter);
2604 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00002605 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002606 test_iter(reporter);
2607 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002608 test_circle(reporter);
2609 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00002610 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00002611 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00002612 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00002613 test_isfinite_after_transform(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00002614 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00002615 test_arb_zero_rad_round_rect_is_rect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00002616 test_addrect_isfinite(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00002617 test_tricky_cubic();
2618 test_clipped_cubic();
2619 test_crbug_170666();
reed@google.com7a90daf2013-04-10 18:44:00 +00002620 test_bad_cubic_crbug229478();
reed@google.com3eff3592013-05-08 21:08:21 +00002621 test_bad_cubic_crbug234190();
mtklein@google.com9c9d4a72013-08-07 19:17:53 +00002622 test_android_specific_behavior(reporter);
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +00002623 test_path_close_issue1474(reporter);
reed@google.comb58ba892013-10-15 15:44:35 +00002624 test_path_to_region(reporter);
reed@android.com3abec1d2009-03-02 05:36:20 +00002625}
2626
2627#include "TestClassDef.h"
2628DEFINE_TESTCLASS("Path", PathTestClass, TestPath)