blob: 55be331ac180e24caa8d9ac07339614e38eeda3d [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 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkPathMeasure.h"
Mike Reedf1f1e7d2020-10-15 14:52:54 -04009#include "src/core/SkPathPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "tests/Test.h"
reed@android.com5e5adfd2009-03-07 03:39:23 +000011
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000012static void test_small_segment3() {
reed@google.comded44142012-04-27 20:22:07 +000013 SkPath path;
14 const SkPoint pts[] = {
15 { 0, 0 },
16 { 100000000000.0f, 100000000000.0f }, { 0, 0 }, { 10, 10 },
17 { 10, 10 }, { 0, 0 }, { 10, 10 }
18 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000019
reed@google.comded44142012-04-27 20:22:07 +000020 path.moveTo(pts[0]);
bsalomon@google.com2fc23592012-10-03 19:10:31 +000021 for (size_t i = 1; i < SK_ARRAY_COUNT(pts); i += 3) {
reed@google.comded44142012-04-27 20:22:07 +000022 path.cubicTo(pts[i], pts[i + 1], pts[i + 2]);
23 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000024
reed@google.comded44142012-04-27 20:22:07 +000025 SkPathMeasure meas(path, false);
26 meas.getLength();
reed@google.comded44142012-04-27 20:22:07 +000027}
28
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000029static void test_small_segment2() {
reed@google.comded44142012-04-27 20:22:07 +000030 SkPath path;
31 const SkPoint pts[] = {
32 { 0, 0 },
rmistry@google.comd6176b02012-08-23 18:14:13 +000033 { 100000000000.0f, 100000000000.0f }, { 0, 0 },
34 { 10, 10 }, { 0, 0 },
reed@google.comded44142012-04-27 20:22:07 +000035 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000036
reed@google.comded44142012-04-27 20:22:07 +000037 path.moveTo(pts[0]);
38 for (size_t i = 1; i < SK_ARRAY_COUNT(pts); i += 2) {
39 path.quadTo(pts[i], pts[i + 1]);
40 }
41 SkPathMeasure meas(path, false);
42 meas.getLength();
reed@google.comded44142012-04-27 20:22:07 +000043}
44
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000045static void test_small_segment() {
reed@google.comfab1ddd2012-04-20 15:10:32 +000046 SkPath path;
47 const SkPoint pts[] = {
48 { 100000, 100000},
49 // big jump between these points, makes a big segment
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000050 { 1.0005f, 0.9999f },
reed@google.comfab1ddd2012-04-20 15:10:32 +000051 // tiny (non-zero) jump between these points
robertphillips@google.comc6ce7502012-05-08 13:15:37 +000052 { SK_Scalar1, SK_Scalar1 },
reed@google.comfab1ddd2012-04-20 15:10:32 +000053 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000054
reed@google.comfab1ddd2012-04-20 15:10:32 +000055 path.moveTo(pts[0]);
56 for (size_t i = 1; i < SK_ARRAY_COUNT(pts); ++i) {
57 path.lineTo(pts[i]);
58 }
59 SkPathMeasure meas(path, false);
60
61 /* this would assert (before a fix) because we added a segment with
62 the same length as the prev segment, due to the follow (bad) pattern
63
64 d = distance(pts[0], pts[1]);
65 distance += d;
66 seg->fDistance = distance;
reed@google.comded44142012-04-27 20:22:07 +000067
reed@google.comfab1ddd2012-04-20 15:10:32 +000068 SkASSERT(d > 0); // TRUE
69 SkASSERT(seg->fDistance > prevSeg->fDistance); // FALSE
70
71 This 2nd assert failes because (distance += d) didn't affect distance
72 because distance >>> d.
73 */
74 meas.getLength();
reed@google.comfab1ddd2012-04-20 15:10:32 +000075}
76
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000077DEF_TEST(PathMeasure, reporter) {
reed@android.com5e5adfd2009-03-07 03:39:23 +000078 SkPath path;
79
80 path.moveTo(0, 0);
81 path.lineTo(SK_Scalar1, 0);
82 path.lineTo(SK_Scalar1, SK_Scalar1);
83 path.lineTo(0, SK_Scalar1);
84
85 SkPathMeasure meas(path, true);
86 SkScalar length = meas.getLength();
87 SkASSERT(length == SK_Scalar1*4);
88
89 path.reset();
90 path.moveTo(0, 0);
91 path.lineTo(SK_Scalar1*3, SK_Scalar1*4);
92 meas.setPath(&path, false);
93 length = meas.getLength();
94 REPORTER_ASSERT(reporter, length == SK_Scalar1*5);
95
96 path.reset();
97 path.addCircle(0, 0, SK_Scalar1);
98 meas.setPath(&path, true);
99 length = meas.getLength();
100// SkDebugf("circle arc-length = %g\n", length);
101
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000102 // Test the behavior following a close not followed by a move.
103 path.reset();
104 path.lineTo(SK_Scalar1, 0);
105 path.lineTo(SK_Scalar1, SK_Scalar1);
106 path.lineTo(0, SK_Scalar1);
107 path.close();
108 path.lineTo(-SK_Scalar1, 0);
109 meas.setPath(&path, false);
110 length = meas.getLength();
111 REPORTER_ASSERT(reporter, length == SK_Scalar1 * 4);
112 meas.nextContour();
113 length = meas.getLength();
114 REPORTER_ASSERT(reporter, length == SK_Scalar1);
115 SkPoint position;
116 SkVector tangent;
117 REPORTER_ASSERT(reporter, meas.getPosTan(SK_ScalarHalf, &position, &tangent));
118 REPORTER_ASSERT(reporter,
robertphillips@google.com6853e802012-04-16 15:50:18 +0000119 SkScalarNearlyEqual(position.fX,
120 -SK_ScalarHalf,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000121 0.0001f));
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000122 REPORTER_ASSERT(reporter, position.fY == 0);
123 REPORTER_ASSERT(reporter, tangent.fX == -SK_Scalar1);
124 REPORTER_ASSERT(reporter, tangent.fY == 0);
125
126 // Test degenerate paths
127 path.reset();
128 path.moveTo(0, 0);
129 path.lineTo(0, 0);
130 path.lineTo(SK_Scalar1, 0);
131 path.quadTo(SK_Scalar1, 0, SK_Scalar1, 0);
132 path.quadTo(SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1 * 2);
133 path.cubicTo(SK_Scalar1, SK_Scalar1 * 2,
134 SK_Scalar1, SK_Scalar1 * 2,
135 SK_Scalar1, SK_Scalar1 * 2);
136 path.cubicTo(SK_Scalar1*2, SK_Scalar1 * 2,
137 SK_Scalar1*3, SK_Scalar1 * 2,
138 SK_Scalar1*4, SK_Scalar1 * 2);
139 meas.setPath(&path, false);
140 length = meas.getLength();
141 REPORTER_ASSERT(reporter, length == SK_Scalar1 * 6);
142 REPORTER_ASSERT(reporter, meas.getPosTan(SK_ScalarHalf, &position, &tangent));
143 REPORTER_ASSERT(reporter,
robertphillips@google.com6853e802012-04-16 15:50:18 +0000144 SkScalarNearlyEqual(position.fX,
145 SK_ScalarHalf,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000146 0.0001f));
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000147 REPORTER_ASSERT(reporter, position.fY == 0);
148 REPORTER_ASSERT(reporter, tangent.fX == SK_Scalar1);
149 REPORTER_ASSERT(reporter, tangent.fY == 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000150 REPORTER_ASSERT(reporter, meas.getPosTan(2.5f, &position, &tangent));
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000151 REPORTER_ASSERT(reporter,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000152 SkScalarNearlyEqual(position.fX, SK_Scalar1, 0.0001f));
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000153 REPORTER_ASSERT(reporter,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000154 SkScalarNearlyEqual(position.fY, 1.5f));
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000155 REPORTER_ASSERT(reporter, tangent.fX == 0);
156 REPORTER_ASSERT(reporter, tangent.fY == SK_Scalar1);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000157 REPORTER_ASSERT(reporter, meas.getPosTan(4.5f, &position, &tangent));
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000158 REPORTER_ASSERT(reporter,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000159 SkScalarNearlyEqual(position.fX,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000160 2.5f,
161 0.0001f));
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000162 REPORTER_ASSERT(reporter,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000163 SkScalarNearlyEqual(position.fY,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000164 2.0f,
165 0.0001f));
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000166 REPORTER_ASSERT(reporter, tangent.fX == SK_Scalar1);
167 REPORTER_ASSERT(reporter, tangent.fY == 0);
168
169 path.reset();
170 path.moveTo(0, 0);
171 path.lineTo(SK_Scalar1, 0);
172 path.moveTo(SK_Scalar1, SK_Scalar1);
173 path.moveTo(SK_Scalar1 * 2, SK_Scalar1 * 2);
174 path.lineTo(SK_Scalar1, SK_Scalar1 * 2);
175 meas.setPath(&path, false);
176 length = meas.getLength();
177 REPORTER_ASSERT(reporter, length == SK_Scalar1);
178 REPORTER_ASSERT(reporter, meas.getPosTan(SK_ScalarHalf, &position, &tangent));
179 REPORTER_ASSERT(reporter,
robertphillips@google.com6853e802012-04-16 15:50:18 +0000180 SkScalarNearlyEqual(position.fX,
181 SK_ScalarHalf,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000182 0.0001f));
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000183 REPORTER_ASSERT(reporter, position.fY == 0);
184 REPORTER_ASSERT(reporter, tangent.fX == SK_Scalar1);
185 REPORTER_ASSERT(reporter, tangent.fY == 0);
186 meas.nextContour();
187 length = meas.getLength();
188 REPORTER_ASSERT(reporter, length == SK_Scalar1);
189 REPORTER_ASSERT(reporter, meas.getPosTan(SK_ScalarHalf, &position, &tangent));
190 REPORTER_ASSERT(reporter,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000191 SkScalarNearlyEqual(position.fX,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000192 1.5f,
193 0.0001f));
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000194 REPORTER_ASSERT(reporter,
robertphillips@google.com6853e802012-04-16 15:50:18 +0000195 SkScalarNearlyEqual(position.fY,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000196 2.0f,
197 0.0001f));
schenney@chromium.org510c6b12012-01-12 20:04:06 +0000198 REPORTER_ASSERT(reporter, tangent.fX == -SK_Scalar1);
199 REPORTER_ASSERT(reporter, tangent.fY == 0);
reed@google.comfab1ddd2012-04-20 15:10:32 +0000200
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000201 test_small_segment();
202 test_small_segment2();
203 test_small_segment3();
reed@android.com5e5adfd2009-03-07 03:39:23 +0000204}
caryclarkb6474dd2016-01-19 08:07:49 -0800205
206DEF_TEST(PathMeasureConic, reporter) {
207 SkPoint stdP, hiP, pts[] = {{0,0}, {100,0}, {100,0}};
208 SkPath p;
209 p.moveTo(0, 0);
210 p.conicTo(pts[1], pts[2], 1);
211 SkPathMeasure stdm(p, false);
212 REPORTER_ASSERT(reporter, stdm.getPosTan(20, &stdP, nullptr));
213 p.reset();
214 p.moveTo(0, 0);
215 p.conicTo(pts[1], pts[2], 10);
216 stdm.setPath(&p, false);
217 REPORTER_ASSERT(reporter, stdm.getPosTan(20, &hiP, nullptr));
218 REPORTER_ASSERT(reporter, 19.5f < stdP.fX && stdP.fX < 20.5f);
219 REPORTER_ASSERT(reporter, 19.5f < hiP.fX && hiP.fX < 20.5f);
220}
Mike Reed140ab362018-03-20 16:47:32 +0000221
222// Regression test for b/26425223
223DEF_TEST(PathMeasure_nextctr, reporter) {
224 SkPath path;
225 path.moveTo(0, 0); path.lineTo(100, 0);
226
227 SkPathMeasure meas(path, false);
228 // only expect 1 contour, even if we didn't explicitly call getLength() ourselves
229 REPORTER_ASSERT(reporter, !meas.nextContour());
230}
Mike Reedcc88f3a2019-02-06 11:40:27 -0500231
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500232#include "include/core/SkContourMeasure.h"
Mike Reedcc88f3a2019-02-06 11:40:27 -0500233
234static void test_90_degrees(sk_sp<SkContourMeasure> cm, SkScalar radius,
235 skiatest::Reporter* reporter) {
236 SkPoint pos;
237 SkVector tan;
238 SkScalar distance = cm->length() / 4;
239 bool success = cm->getPosTan(distance, &pos, &tan);
240
241 REPORTER_ASSERT(reporter, success);
242 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pos.fX, 0));
243 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pos.fY, radius));
244 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(tan.fX, -1));
245 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(tan.fY, 0));
246}
247
248static void test_empty_contours(skiatest::Reporter* reporter) {
249 SkPath path;
250
251 path.moveTo(0, 0).lineTo(100, 100).lineTo(200, 100);
252 path.moveTo(2, 2).moveTo(3, 3); // zero-length(s)
253 path.moveTo(4, 4).close().close().close(); // zero-length
254 path.moveTo(5, 5).lineTo(5, 5); // zero-length
255 path.moveTo(5, 5).lineTo(5, 5).close(); // zero-length
256 path.moveTo(5, 5).lineTo(5, 5).close().close(); // zero-length
257 path.moveTo(6, 6).lineTo(7, 7);
258 path.moveTo(10, 10); // zero-length
259
260 SkContourMeasureIter fact(path, false);
261
262 // given the above construction, we expect only 2 contours (the rest are "empty")
263
264 REPORTER_ASSERT(reporter, fact.next());
265 REPORTER_ASSERT(reporter, fact.next());
266 REPORTER_ASSERT(reporter, !fact.next());
267}
268
Mike Reed62ad46b2019-02-13 11:48:35 -0500269static void test_MLM_contours(skiatest::Reporter* reporter) {
270 SkPath path;
271
272 // This odd sequence (with a trailing moveTo) used to return a 2nd contour, which is
273 // wrong, since the contract for a measure is to only return non-zero length contours.
274 path.moveTo(10, 10).lineTo(20, 20).moveTo(30, 30);
275
276 for (bool forceClosed : {false, true}) {
277 SkContourMeasureIter fact(path, forceClosed);
278 REPORTER_ASSERT(reporter, fact.next());
279 REPORTER_ASSERT(reporter, !fact.next());
280 }
281}
282
Mike Reedc924a882020-10-13 21:22:47 -0400283static void test_shrink(skiatest::Reporter* reporter) {
284 SkPath path;
285 path.addRect({1, 2, 3, 4});
286 path.incReserve(100); // give shrinkToFit() something to do
287
288 SkContourMeasureIter iter(path, false);
289
290 // shrinks the allocation, possibly relocating the underlying arrays.
291 // The contouremasureiter needs to have safely copied path, to be unaffected by this
292 // change to "path".
Mike Reedf1f1e7d2020-10-15 14:52:54 -0400293 SkPathPriv::ShrinkToFit(&path);
Mike Reedc924a882020-10-13 21:22:47 -0400294
295 // Note, this failed (before the fix) on an ASAN build, which notices that we were
296 // using an internal iterator of the passed-in path, not our copy.
297 while (iter.next())
298 ;
299}
300
Mike Reedcc88f3a2019-02-06 11:40:27 -0500301DEF_TEST(contour_measure, reporter) {
302 SkPath path;
303 path.addCircle(0, 0, 100);
304 path.addCircle(0, 0, 10);
305
306 SkContourMeasureIter fact(path, false);
307 path.reset(); // we should not need the path avert we created the factory
308
309 auto cm0 = fact.next();
310 auto cm1 = fact.next();
311
312 REPORTER_ASSERT(reporter, cm0->isClosed());
313 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(cm0->length(), 200 * SK_ScalarPI, 1.5f));
314
315 test_90_degrees(cm0, 100, reporter);
316
317 REPORTER_ASSERT(reporter, cm1->isClosed());
318 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(cm1->length(), 20 * SK_ScalarPI, 0.5f));
319
320 test_90_degrees(cm1, 10, reporter);
321
322 auto cm2 = fact.next();
323 REPORTER_ASSERT(reporter, !cm2);
324
325 test_empty_contours(reporter);
Mike Reed62ad46b2019-02-13 11:48:35 -0500326 test_MLM_contours(reporter);
Mike Reedc924a882020-10-13 21:22:47 -0400327
328 test_shrink(reporter);
Mike Reedcc88f3a2019-02-06 11:40:27 -0500329}