epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 8 | #include "Test.h" |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 10 | #include "SkPaint.h" |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 11 | #include "SkPath.h" |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 12 | #include "SkParse.h" |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 13 | #include "SkParsePath.h" |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 14 | #include "SkPathEffect.h" |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 15 | #include "SkRandom.h" |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 16 | #include "SkReader32.h" |
reed@android.com | 60bc6d5 | 2010-02-11 11:09:39 +0000 | [diff] [blame] | 17 | #include "SkSize.h" |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 18 | #include "SkWriter32.h" |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 19 | #include "SkSurface.h" |
| 20 | |
| 21 | static SkSurface* new_surface(int w, int h) { |
| 22 | SkImage::Info info = { |
| 23 | w, h, SkImage::kPMColor_ColorType, SkImage::kPremul_AlphaType |
| 24 | }; |
| 25 | return SkSurface::NewRaster(info, NULL); |
| 26 | } |
| 27 | |
| 28 | // Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/ |
| 29 | // which triggered an assert, from a tricky cubic. This test replicates that |
| 30 | // example, so we can ensure that we handle it (in SkEdge.cpp), and don't |
| 31 | // assert in the SK_DEBUG build. |
| 32 | static void test_tricky_cubic(skiatest::Reporter* reporter) { |
| 33 | const SkPoint pts[] = { |
skia.committer@gmail.com | 055c7c2 | 2012-09-15 02:01:41 +0000 | [diff] [blame] | 34 | { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) }, |
| 35 | { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) }, |
| 36 | { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) }, |
| 37 | { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) }, |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | SkPath path; |
| 41 | path.moveTo(pts[0]); |
| 42 | path.cubicTo(pts[1], pts[2], pts[3]); |
| 43 | |
| 44 | SkPaint paint; |
| 45 | paint.setAntiAlias(true); |
| 46 | |
| 47 | SkSurface* surface = new_surface(19, 130); |
| 48 | surface->getCanvas()->drawPath(path, paint); |
| 49 | surface->unref(); |
| 50 | } |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 51 | |
tomhudson@google.com | ed02c4d | 2012-08-10 14:10:45 +0000 | [diff] [blame] | 52 | // Inspired by http://code.google.com/p/chromium/issues/detail?id=141651 |
| 53 | // |
| 54 | static void test_isfinite_after_transform(skiatest::Reporter* reporter) { |
| 55 | SkPath path; |
| 56 | path.quadTo(157, 366, 286, 208); |
| 57 | path.arcTo(37, 442, 315, 163, 957494590897113.0f); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 58 | |
tomhudson@google.com | ed02c4d | 2012-08-10 14:10:45 +0000 | [diff] [blame] | 59 | SkMatrix matrix; |
| 60 | matrix.setScale(1000*1000, 1000*1000); |
| 61 | |
| 62 | // Be sure that path::transform correctly updates isFinite and the bounds |
| 63 | // if the transformation overflows. The previous bug was that isFinite was |
| 64 | // set to true in this case, but the bounds were not set to empty (which |
| 65 | // they should be). |
| 66 | while (path.isFinite()) { |
| 67 | REPORTER_ASSERT(reporter, path.getBounds().isFinite()); |
| 68 | REPORTER_ASSERT(reporter, !path.getBounds().isEmpty()); |
| 69 | path.transform(matrix); |
| 70 | } |
| 71 | REPORTER_ASSERT(reporter, path.getBounds().isEmpty()); |
| 72 | |
| 73 | matrix.setTranslate(SK_Scalar1, SK_Scalar1); |
| 74 | path.transform(matrix); |
| 75 | // we need to still be non-finite |
| 76 | REPORTER_ASSERT(reporter, !path.isFinite()); |
| 77 | REPORTER_ASSERT(reporter, path.getBounds().isEmpty()); |
| 78 | } |
| 79 | |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 80 | static void add_corner_arc(SkPath* path, const SkRect& rect, |
| 81 | SkScalar xIn, SkScalar yIn, |
| 82 | int startAngle) |
| 83 | { |
| 84 | |
| 85 | SkScalar rx = SkMinScalar(rect.width(), xIn); |
| 86 | SkScalar ry = SkMinScalar(rect.height(), yIn); |
| 87 | |
| 88 | SkRect arcRect; |
| 89 | arcRect.set(-rx, -ry, rx, ry); |
| 90 | switch (startAngle) { |
| 91 | case 0: |
| 92 | arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom); |
| 93 | break; |
| 94 | case 90: |
| 95 | arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom); |
| 96 | break; |
| 97 | case 180: |
| 98 | arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop); |
| 99 | break; |
| 100 | case 270: |
| 101 | arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop); |
| 102 | break; |
| 103 | default: |
| 104 | break; |
| 105 | } |
| 106 | |
| 107 | path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false); |
| 108 | } |
| 109 | |
| 110 | static void make_arb_round_rect(SkPath* path, const SkRect& r, |
| 111 | SkScalar xCorner, SkScalar yCorner) { |
| 112 | // we are lazy here and use the same x & y for each corner |
| 113 | add_corner_arc(path, r, xCorner, yCorner, 270); |
| 114 | add_corner_arc(path, r, xCorner, yCorner, 0); |
| 115 | add_corner_arc(path, r, xCorner, yCorner, 90); |
| 116 | add_corner_arc(path, r, xCorner, yCorner, 180); |
| 117 | } |
| 118 | |
| 119 | // Chrome creates its own round rects with each corner possibly being different. |
| 120 | // Performance will suffer if they are not convex. |
| 121 | // Note: PathBench::ArbRoundRectBench performs almost exactly |
| 122 | // the same test (but with drawing) |
| 123 | static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) { |
| 124 | SkRandom rand; |
| 125 | SkRect r; |
| 126 | |
| 127 | for (int i = 0; i < 5000; ++i) { |
| 128 | |
| 129 | SkScalar radius = rand.nextUScalar1() * 30; |
| 130 | if (radius < SK_Scalar1) { |
| 131 | continue; |
| 132 | } |
| 133 | r.fLeft = rand.nextUScalar1() * 300; |
| 134 | r.fTop = rand.nextUScalar1() * 300; |
| 135 | r.fRight = r.fLeft + 2 * radius; |
| 136 | r.fBottom = r.fTop + 2 * radius; |
| 137 | |
| 138 | SkPath temp; |
| 139 | |
| 140 | make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15); |
| 141 | |
robertphillips@google.com | c7a37c7 | 2012-10-19 01:26:18 +0000 | [diff] [blame^] | 142 | #ifdef SK_REDEFINE_ROOT2OVER2_TO_MAKE_ARCTOS_CONVEX |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 143 | REPORTER_ASSERT(reporter, temp.isConvex()); |
robertphillips@google.com | c7a37c7 | 2012-10-19 01:26:18 +0000 | [diff] [blame^] | 144 | #endif |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 148 | static void test_rect_isfinite(skiatest::Reporter* reporter) { |
| 149 | const SkScalar inf = SK_ScalarInfinity; |
| 150 | const SkScalar nan = SK_ScalarNaN; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 151 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 152 | SkRect r; |
| 153 | r.setEmpty(); |
| 154 | REPORTER_ASSERT(reporter, r.isFinite()); |
| 155 | r.set(0, 0, inf, -inf); |
| 156 | REPORTER_ASSERT(reporter, !r.isFinite()); |
| 157 | r.set(0, 0, nan, 0); |
| 158 | REPORTER_ASSERT(reporter, !r.isFinite()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 159 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 160 | SkPoint pts[] = { |
| 161 | { 0, 0 }, |
| 162 | { SK_Scalar1, 0 }, |
| 163 | { 0, SK_Scalar1 }, |
| 164 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 165 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 166 | bool isFine = r.setBoundsCheck(pts, 3); |
| 167 | REPORTER_ASSERT(reporter, isFine); |
| 168 | REPORTER_ASSERT(reporter, !r.isEmpty()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 169 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 170 | pts[1].set(inf, 0); |
| 171 | isFine = r.setBoundsCheck(pts, 3); |
| 172 | REPORTER_ASSERT(reporter, !isFine); |
| 173 | REPORTER_ASSERT(reporter, r.isEmpty()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 174 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 175 | pts[1].set(nan, 0); |
| 176 | isFine = r.setBoundsCheck(pts, 3); |
| 177 | REPORTER_ASSERT(reporter, !isFine); |
| 178 | REPORTER_ASSERT(reporter, r.isEmpty()); |
| 179 | } |
| 180 | |
| 181 | static void test_path_isfinite(skiatest::Reporter* reporter) { |
| 182 | const SkScalar inf = SK_ScalarInfinity; |
| 183 | const SkScalar nan = SK_ScalarNaN; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 184 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 185 | SkPath path; |
| 186 | REPORTER_ASSERT(reporter, path.isFinite()); |
| 187 | |
| 188 | path.reset(); |
| 189 | REPORTER_ASSERT(reporter, path.isFinite()); |
| 190 | |
| 191 | path.reset(); |
| 192 | path.moveTo(SK_Scalar1, 0); |
| 193 | REPORTER_ASSERT(reporter, path.isFinite()); |
| 194 | |
| 195 | path.reset(); |
| 196 | path.moveTo(inf, -inf); |
| 197 | REPORTER_ASSERT(reporter, !path.isFinite()); |
| 198 | |
| 199 | path.reset(); |
| 200 | path.moveTo(nan, 0); |
| 201 | REPORTER_ASSERT(reporter, !path.isFinite()); |
| 202 | } |
| 203 | |
| 204 | static void test_isfinite(skiatest::Reporter* reporter) { |
| 205 | test_rect_isfinite(reporter); |
| 206 | test_path_isfinite(reporter); |
| 207 | } |
| 208 | |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 209 | // assert that we always |
| 210 | // start with a moveTo |
| 211 | // only have 1 moveTo |
| 212 | // only have Lines after that |
| 213 | // end with a single close |
| 214 | // only have (at most) 1 close |
| 215 | // |
| 216 | static void test_poly(skiatest::Reporter* reporter, const SkPath& path, |
| 217 | const SkPoint srcPts[], int count, bool expectClose) { |
| 218 | SkPath::RawIter iter(path); |
| 219 | SkPoint pts[4]; |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 220 | |
| 221 | bool firstTime = true; |
| 222 | bool foundClose = false; |
| 223 | for (;;) { |
| 224 | switch (iter.next(pts)) { |
| 225 | case SkPath::kMove_Verb: |
| 226 | REPORTER_ASSERT(reporter, firstTime); |
| 227 | REPORTER_ASSERT(reporter, pts[0] == srcPts[0]); |
| 228 | srcPts++; |
| 229 | firstTime = false; |
| 230 | break; |
| 231 | case SkPath::kLine_Verb: |
| 232 | REPORTER_ASSERT(reporter, !firstTime); |
| 233 | REPORTER_ASSERT(reporter, pts[1] == srcPts[0]); |
| 234 | srcPts++; |
| 235 | break; |
| 236 | case SkPath::kQuad_Verb: |
| 237 | REPORTER_ASSERT(reporter, !"unexpected quad verb"); |
| 238 | break; |
| 239 | case SkPath::kCubic_Verb: |
| 240 | REPORTER_ASSERT(reporter, !"unexpected cubic verb"); |
| 241 | break; |
| 242 | case SkPath::kClose_Verb: |
| 243 | REPORTER_ASSERT(reporter, !firstTime); |
| 244 | REPORTER_ASSERT(reporter, !foundClose); |
| 245 | REPORTER_ASSERT(reporter, expectClose); |
| 246 | foundClose = true; |
| 247 | break; |
| 248 | case SkPath::kDone_Verb: |
| 249 | goto DONE; |
| 250 | } |
| 251 | } |
| 252 | DONE: |
| 253 | REPORTER_ASSERT(reporter, foundClose == expectClose); |
| 254 | } |
| 255 | |
| 256 | static void test_addPoly(skiatest::Reporter* reporter) { |
| 257 | SkPoint pts[32]; |
| 258 | SkRandom rand; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 259 | |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 260 | for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) { |
| 261 | pts[i].fX = rand.nextSScalar1(); |
| 262 | pts[i].fY = rand.nextSScalar1(); |
| 263 | } |
| 264 | |
| 265 | for (int doClose = 0; doClose <= 1; ++doClose) { |
| 266 | for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) { |
| 267 | SkPath path; |
| 268 | path.addPoly(pts, count, SkToBool(doClose)); |
| 269 | test_poly(reporter, path, pts, count, SkToBool(doClose)); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 274 | static void test_strokerec(skiatest::Reporter* reporter) { |
| 275 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
| 276 | REPORTER_ASSERT(reporter, rec.isFillStyle()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 277 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 278 | rec.setHairlineStyle(); |
| 279 | REPORTER_ASSERT(reporter, rec.isHairlineStyle()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 280 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 281 | rec.setStrokeStyle(SK_Scalar1, false); |
| 282 | REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 283 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 284 | rec.setStrokeStyle(SK_Scalar1, true); |
| 285 | REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 286 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 287 | rec.setStrokeStyle(0, false); |
| 288 | REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 289 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 290 | rec.setStrokeStyle(0, true); |
| 291 | REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle()); |
| 292 | } |
| 293 | |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 294 | /** |
| 295 | * cheapIsDirection can take a shortcut when a path is marked convex. |
| 296 | * This function ensures that we always test cheapIsDirection when the path |
| 297 | * is flagged with unknown convexity status. |
| 298 | */ |
| 299 | static void check_direction(SkPath* path, |
| 300 | SkPath::Direction expectedDir, |
| 301 | skiatest::Reporter* reporter) { |
| 302 | if (SkPath::kConvex_Convexity == path->getConvexity()) { |
| 303 | REPORTER_ASSERT(reporter, path->cheapIsDirection(expectedDir)); |
| 304 | path->setConvexity(SkPath::kUnknown_Convexity); |
| 305 | } |
| 306 | REPORTER_ASSERT(reporter, path->cheapIsDirection(expectedDir)); |
| 307 | } |
| 308 | |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 309 | static void test_direction(skiatest::Reporter* reporter) { |
| 310 | size_t i; |
| 311 | SkPath path; |
| 312 | REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL)); |
| 313 | REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCW_Direction)); |
| 314 | REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCCW_Direction)); |
| 315 | |
| 316 | static const char* gDegen[] = { |
| 317 | "M 10 10", |
| 318 | "M 10 10 M 20 20", |
| 319 | "M 10 10 L 20 20", |
| 320 | "M 10 10 L 10 10 L 10 10", |
| 321 | "M 10 10 Q 10 10 10 10", |
| 322 | "M 10 10 C 10 10 10 10 10 10", |
| 323 | }; |
| 324 | for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) { |
| 325 | path.reset(); |
| 326 | bool valid = SkParsePath::FromSVGString(gDegen[i], &path); |
| 327 | REPORTER_ASSERT(reporter, valid); |
| 328 | REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL)); |
| 329 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 330 | |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 331 | static const char* gCW[] = { |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 332 | "M 10 10 L 10 10 Q 20 10 20 20", |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 333 | "M 10 10 C 20 10 20 20 20 20", |
reed@google.com | d414666 | 2012-01-31 15:42:29 +0000 | [diff] [blame] | 334 | "M 20 10 Q 20 20 30 20 L 10 20", // test double-back at y-max |
bsalomon@google.com | 4eefe61 | 2012-07-10 18:28:12 +0000 | [diff] [blame] | 335 | // rect with top two corners replaced by cubics with identical middle |
| 336 | // control points |
| 337 | "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10" |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 338 | }; |
| 339 | for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) { |
| 340 | path.reset(); |
| 341 | bool valid = SkParsePath::FromSVGString(gCW[i], &path); |
| 342 | REPORTER_ASSERT(reporter, valid); |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 343 | check_direction(&path, SkPath::kCW_Direction, reporter); |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 344 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 345 | |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 346 | static const char* gCCW[] = { |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 347 | "M 10 10 L 10 10 Q 20 10 20 -20", |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 348 | "M 10 10 C 20 10 20 -20 20 -20", |
reed@google.com | d414666 | 2012-01-31 15:42:29 +0000 | [diff] [blame] | 349 | "M 20 10 Q 20 20 10 20 L 30 20", // test double-back at y-max |
bsalomon@google.com | 4eefe61 | 2012-07-10 18:28:12 +0000 | [diff] [blame] | 350 | // rect with top two corners replaced by cubics with identical middle |
| 351 | // control points |
| 352 | "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10" |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 353 | }; |
| 354 | for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) { |
| 355 | path.reset(); |
| 356 | bool valid = SkParsePath::FromSVGString(gCCW[i], &path); |
| 357 | REPORTER_ASSERT(reporter, valid); |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 358 | check_direction(&path, SkPath::kCCW_Direction, reporter); |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 359 | } |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 360 | |
| 361 | // Test two donuts, each wound a different direction. Only the outer contour |
| 362 | // determines the cheap direction |
| 363 | path.reset(); |
| 364 | path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction); |
| 365 | path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction); |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 366 | check_direction(&path, SkPath::kCW_Direction, reporter); |
| 367 | |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 368 | path.reset(); |
| 369 | path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction); |
| 370 | path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction); |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 371 | check_direction(&path, SkPath::kCCW_Direction, reporter); |
| 372 | |
bsalomon@google.com | 6843ac4 | 2012-02-17 13:49:03 +0000 | [diff] [blame] | 373 | #ifdef SK_SCALAR_IS_FLOAT |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 374 | // triangle with one point really far from the origin. |
| 375 | path.reset(); |
| 376 | // the first point is roughly 1.05e10, 1.05e10 |
bsalomon@google.com | 53aab78 | 2012-02-23 14:54:49 +0000 | [diff] [blame] | 377 | path.moveTo(SkFloatToScalar(SkBits2Float(0x501c7652)), SkFloatToScalar(SkBits2Float(0x501c7652))); |
| 378 | path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1); |
| 379 | path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1); |
| 380 | check_direction(&path, SkPath::kCCW_Direction, reporter); |
| 381 | #endif |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 382 | } |
| 383 | |
reed@google.com | ffdb018 | 2011-11-14 19:29:14 +0000 | [diff] [blame] | 384 | static void add_rect(SkPath* path, const SkRect& r) { |
| 385 | path->moveTo(r.fLeft, r.fTop); |
| 386 | path->lineTo(r.fRight, r.fTop); |
| 387 | path->lineTo(r.fRight, r.fBottom); |
| 388 | path->lineTo(r.fLeft, r.fBottom); |
| 389 | path->close(); |
| 390 | } |
| 391 | |
| 392 | static void test_bounds(skiatest::Reporter* reporter) { |
| 393 | static const SkRect rects[] = { |
reed@google.com | 3563c9e | 2011-11-14 19:34:57 +0000 | [diff] [blame] | 394 | { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) }, |
| 395 | { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) }, |
| 396 | { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) }, |
| 397 | { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) }, |
reed@google.com | ffdb018 | 2011-11-14 19:29:14 +0000 | [diff] [blame] | 398 | }; |
| 399 | |
| 400 | SkPath path0, path1; |
| 401 | for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) { |
| 402 | path0.addRect(rects[i]); |
| 403 | add_rect(&path1, rects[i]); |
| 404 | } |
| 405 | |
| 406 | REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds()); |
| 407 | } |
| 408 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 409 | static void stroke_cubic(const SkPoint pts[4]) { |
| 410 | SkPath path; |
| 411 | path.moveTo(pts[0]); |
| 412 | path.cubicTo(pts[1], pts[2], pts[3]); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 413 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 414 | SkPaint paint; |
| 415 | paint.setStyle(SkPaint::kStroke_Style); |
| 416 | paint.setStrokeWidth(SK_Scalar1 * 2); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 417 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 418 | SkPath fill; |
| 419 | paint.getFillPath(path, &fill); |
| 420 | } |
| 421 | |
| 422 | // just ensure this can run w/o any SkASSERTS firing in the debug build |
| 423 | // we used to assert due to differences in how we determine a degenerate vector |
| 424 | // but that was fixed with the introduction of SkPoint::CanNormalize |
| 425 | static void stroke_tiny_cubic() { |
| 426 | SkPoint p0[] = { |
| 427 | { 372.0f, 92.0f }, |
| 428 | { 372.0f, 92.0f }, |
| 429 | { 372.0f, 92.0f }, |
| 430 | { 372.0f, 92.0f }, |
| 431 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 432 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 433 | stroke_cubic(p0); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 434 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 435 | SkPoint p1[] = { |
| 436 | { 372.0f, 92.0f }, |
| 437 | { 372.0007f, 92.000755f }, |
| 438 | { 371.99927f, 92.003922f }, |
| 439 | { 371.99826f, 92.003899f }, |
| 440 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 441 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 442 | stroke_cubic(p1); |
| 443 | } |
| 444 | |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 445 | static void check_close(skiatest::Reporter* reporter, const SkPath& path) { |
| 446 | for (int i = 0; i < 2; ++i) { |
robertphillips@google.com | 09042b8 | 2012-04-06 20:01:46 +0000 | [diff] [blame] | 447 | SkPath::Iter iter(path, SkToBool(i)); |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 448 | SkPoint mv; |
| 449 | SkPoint pts[4]; |
| 450 | SkPath::Verb v; |
| 451 | int nMT = 0; |
| 452 | int nCL = 0; |
tomhudson@google.com | 221db3c | 2011-07-28 21:10:29 +0000 | [diff] [blame] | 453 | mv.set(0, 0); |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 454 | while (SkPath::kDone_Verb != (v = iter.next(pts))) { |
| 455 | switch (v) { |
| 456 | case SkPath::kMove_Verb: |
| 457 | mv = pts[0]; |
| 458 | ++nMT; |
| 459 | break; |
| 460 | case SkPath::kClose_Verb: |
| 461 | REPORTER_ASSERT(reporter, mv == pts[0]); |
| 462 | ++nCL; |
| 463 | break; |
| 464 | default: |
| 465 | break; |
| 466 | } |
| 467 | } |
| 468 | // if we force a close on the interator we should have a close |
| 469 | // for every moveTo |
| 470 | REPORTER_ASSERT(reporter, !i || nMT == nCL); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | static void test_close(skiatest::Reporter* reporter) { |
| 475 | SkPath closePt; |
| 476 | closePt.moveTo(0, 0); |
| 477 | closePt.close(); |
| 478 | check_close(reporter, closePt); |
| 479 | |
| 480 | SkPath openPt; |
| 481 | openPt.moveTo(0, 0); |
| 482 | check_close(reporter, openPt); |
| 483 | |
| 484 | SkPath empty; |
| 485 | check_close(reporter, empty); |
| 486 | empty.close(); |
| 487 | check_close(reporter, empty); |
| 488 | |
| 489 | SkPath rect; |
| 490 | rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1); |
| 491 | check_close(reporter, rect); |
| 492 | rect.close(); |
| 493 | check_close(reporter, rect); |
| 494 | |
| 495 | SkPath quad; |
| 496 | quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1); |
| 497 | check_close(reporter, quad); |
| 498 | quad.close(); |
| 499 | check_close(reporter, quad); |
| 500 | |
| 501 | SkPath cubic; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 502 | quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 503 | 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1); |
| 504 | check_close(reporter, cubic); |
| 505 | cubic.close(); |
| 506 | check_close(reporter, cubic); |
| 507 | |
| 508 | SkPath line; |
| 509 | line.moveTo(SK_Scalar1, SK_Scalar1); |
| 510 | line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1); |
| 511 | check_close(reporter, line); |
| 512 | line.close(); |
| 513 | check_close(reporter, line); |
| 514 | |
| 515 | SkPath rect2; |
| 516 | rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1); |
| 517 | rect2.close(); |
| 518 | rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1); |
| 519 | check_close(reporter, rect2); |
| 520 | rect2.close(); |
| 521 | check_close(reporter, rect2); |
| 522 | |
| 523 | SkPath oval3; |
| 524 | oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100)); |
| 525 | oval3.close(); |
| 526 | oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200)); |
| 527 | check_close(reporter, oval3); |
| 528 | oval3.close(); |
| 529 | check_close(reporter, oval3); |
| 530 | |
| 531 | SkPath moves; |
| 532 | moves.moveTo(SK_Scalar1, SK_Scalar1); |
| 533 | moves.moveTo(5 * SK_Scalar1, SK_Scalar1); |
| 534 | moves.moveTo(SK_Scalar1, 10 * SK_Scalar1); |
| 535 | moves.moveTo(10 *SK_Scalar1, SK_Scalar1); |
| 536 | check_close(reporter, moves); |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 537 | |
| 538 | stroke_tiny_cubic(); |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 539 | } |
| 540 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 541 | static void check_convexity(skiatest::Reporter* reporter, const SkPath& path, |
| 542 | SkPath::Convexity expected) { |
| 543 | SkPath::Convexity c = SkPath::ComputeConvexity(path); |
| 544 | REPORTER_ASSERT(reporter, c == expected); |
| 545 | } |
| 546 | |
| 547 | static void test_convexity2(skiatest::Reporter* reporter) { |
| 548 | SkPath pt; |
| 549 | pt.moveTo(0, 0); |
| 550 | pt.close(); |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 551 | check_convexity(reporter, pt, SkPath::kConvex_Convexity); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 552 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 553 | SkPath line; |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 554 | line.moveTo(12*SK_Scalar1, 20*SK_Scalar1); |
| 555 | line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 556 | line.close(); |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 557 | check_convexity(reporter, pt, SkPath::kConvex_Convexity); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 558 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 559 | SkPath triLeft; |
| 560 | triLeft.moveTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 561 | triLeft.lineTo(SK_Scalar1, 0); |
| 562 | triLeft.lineTo(SK_Scalar1, SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 563 | triLeft.close(); |
| 564 | check_convexity(reporter, triLeft, SkPath::kConvex_Convexity); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 565 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 566 | SkPath triRight; |
| 567 | triRight.moveTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 568 | triRight.lineTo(-SK_Scalar1, 0); |
| 569 | triRight.lineTo(SK_Scalar1, SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 570 | triRight.close(); |
| 571 | check_convexity(reporter, triRight, SkPath::kConvex_Convexity); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 572 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 573 | SkPath square; |
| 574 | square.moveTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 575 | square.lineTo(SK_Scalar1, 0); |
| 576 | square.lineTo(SK_Scalar1, SK_Scalar1); |
| 577 | square.lineTo(0, SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 578 | square.close(); |
| 579 | check_convexity(reporter, square, SkPath::kConvex_Convexity); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 580 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 581 | SkPath redundantSquare; |
| 582 | redundantSquare.moveTo(0, 0); |
| 583 | redundantSquare.lineTo(0, 0); |
| 584 | redundantSquare.lineTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 585 | redundantSquare.lineTo(SK_Scalar1, 0); |
| 586 | redundantSquare.lineTo(SK_Scalar1, 0); |
| 587 | redundantSquare.lineTo(SK_Scalar1, 0); |
| 588 | redundantSquare.lineTo(SK_Scalar1, SK_Scalar1); |
| 589 | redundantSquare.lineTo(SK_Scalar1, SK_Scalar1); |
| 590 | redundantSquare.lineTo(SK_Scalar1, SK_Scalar1); |
| 591 | redundantSquare.lineTo(0, SK_Scalar1); |
| 592 | redundantSquare.lineTo(0, SK_Scalar1); |
| 593 | redundantSquare.lineTo(0, SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 594 | redundantSquare.close(); |
| 595 | check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 596 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 597 | SkPath bowTie; |
| 598 | bowTie.moveTo(0, 0); |
| 599 | bowTie.lineTo(0, 0); |
| 600 | bowTie.lineTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 601 | bowTie.lineTo(SK_Scalar1, SK_Scalar1); |
| 602 | bowTie.lineTo(SK_Scalar1, SK_Scalar1); |
| 603 | bowTie.lineTo(SK_Scalar1, SK_Scalar1); |
| 604 | bowTie.lineTo(SK_Scalar1, 0); |
| 605 | bowTie.lineTo(SK_Scalar1, 0); |
| 606 | bowTie.lineTo(SK_Scalar1, 0); |
| 607 | bowTie.lineTo(0, SK_Scalar1); |
| 608 | bowTie.lineTo(0, SK_Scalar1); |
| 609 | bowTie.lineTo(0, SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 610 | bowTie.close(); |
| 611 | check_convexity(reporter, bowTie, SkPath::kConcave_Convexity); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 612 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 613 | SkPath spiral; |
| 614 | spiral.moveTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 615 | spiral.lineTo(100*SK_Scalar1, 0); |
| 616 | spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1); |
| 617 | spiral.lineTo(0, 100*SK_Scalar1); |
| 618 | spiral.lineTo(0, 50*SK_Scalar1); |
| 619 | spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1); |
| 620 | spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 621 | spiral.close(); |
reed@google.com | 85b6e39 | 2011-05-15 20:25:17 +0000 | [diff] [blame] | 622 | check_convexity(reporter, spiral, SkPath::kConcave_Convexity); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 623 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 624 | SkPath dent; |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 625 | dent.moveTo(0, 0); |
| 626 | dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1); |
| 627 | dent.lineTo(0, 100*SK_Scalar1); |
| 628 | dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1); |
| 629 | dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 630 | dent.close(); |
| 631 | check_convexity(reporter, dent, SkPath::kConcave_Convexity); |
| 632 | } |
| 633 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 634 | static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p, |
| 635 | const SkRect& bounds) { |
| 636 | REPORTER_ASSERT(reporter, p.isConvex()); |
| 637 | REPORTER_ASSERT(reporter, p.getBounds() == bounds); |
reed@google.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 638 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 639 | SkPath p2(p); |
| 640 | REPORTER_ASSERT(reporter, p2.isConvex()); |
| 641 | REPORTER_ASSERT(reporter, p2.getBounds() == bounds); |
| 642 | |
| 643 | SkPath other; |
| 644 | other.swap(p2); |
| 645 | REPORTER_ASSERT(reporter, other.isConvex()); |
| 646 | REPORTER_ASSERT(reporter, other.getBounds() == bounds); |
| 647 | } |
| 648 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 649 | static void setFromString(SkPath* path, const char str[]) { |
| 650 | bool first = true; |
| 651 | while (str) { |
| 652 | SkScalar x, y; |
| 653 | str = SkParse::FindScalar(str, &x); |
| 654 | if (NULL == str) { |
| 655 | break; |
| 656 | } |
| 657 | str = SkParse::FindScalar(str, &y); |
| 658 | SkASSERT(str); |
| 659 | if (first) { |
| 660 | path->moveTo(x, y); |
| 661 | first = false; |
| 662 | } else { |
| 663 | path->lineTo(x, y); |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | static void test_convexity(skiatest::Reporter* reporter) { |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 669 | static const SkPath::Convexity C = SkPath::kConcave_Convexity; |
| 670 | static const SkPath::Convexity V = SkPath::kConvex_Convexity; |
| 671 | |
| 672 | SkPath path; |
| 673 | |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 674 | REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path)); |
reed@google.com | e354397 | 2012-01-10 18:59:22 +0000 | [diff] [blame] | 675 | path.addCircle(0, 0, SkIntToScalar(10)); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 676 | REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path)); |
reed@google.com | e354397 | 2012-01-10 18:59:22 +0000 | [diff] [blame] | 677 | path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 678 | REPORTER_ASSERT(reporter, C == SkPath::ComputeConvexity(path)); |
| 679 | path.reset(); |
reed@google.com | e354397 | 2012-01-10 18:59:22 +0000 | [diff] [blame] | 680 | path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 681 | REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path)); |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 682 | REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCCW_Direction)); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 683 | path.reset(); |
reed@google.com | e354397 | 2012-01-10 18:59:22 +0000 | [diff] [blame] | 684 | path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 685 | REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path)); |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 686 | REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCW_Direction)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 687 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 688 | static const struct { |
| 689 | const char* fPathStr; |
| 690 | SkPath::Convexity fExpectedConvexity; |
| 691 | } gRec[] = { |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 692 | { "", SkPath::kConvex_Convexity }, |
| 693 | { "0 0", SkPath::kConvex_Convexity }, |
| 694 | { "0 0 10 10", SkPath::kConvex_Convexity }, |
reed@google.com | 85b6e39 | 2011-05-15 20:25:17 +0000 | [diff] [blame] | 695 | { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity }, |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 696 | { "0 0 10 10 10 20", SkPath::kConvex_Convexity }, |
| 697 | { "0 0 10 10 10 0", SkPath::kConvex_Convexity }, |
| 698 | { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity }, |
| 699 | { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity }, |
| 700 | }; |
| 701 | |
| 702 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
| 703 | SkPath path; |
| 704 | setFromString(&path, gRec[i].fPathStr); |
| 705 | SkPath::Convexity c = SkPath::ComputeConvexity(path); |
| 706 | REPORTER_ASSERT(reporter, c == gRec[i].fExpectedConvexity); |
| 707 | } |
| 708 | } |
| 709 | |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 710 | static void test_isLine(skiatest::Reporter* reporter) { |
| 711 | SkPath path; |
| 712 | SkPoint pts[2]; |
| 713 | const SkScalar value = SkIntToScalar(5); |
| 714 | |
| 715 | REPORTER_ASSERT(reporter, !path.isLine(NULL)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 716 | |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 717 | // set some non-zero values |
| 718 | pts[0].set(value, value); |
| 719 | pts[1].set(value, value); |
| 720 | REPORTER_ASSERT(reporter, !path.isLine(pts)); |
| 721 | // check that pts was untouched |
| 722 | REPORTER_ASSERT(reporter, pts[0].equals(value, value)); |
| 723 | REPORTER_ASSERT(reporter, pts[1].equals(value, value)); |
| 724 | |
| 725 | const SkScalar moveX = SkIntToScalar(1); |
| 726 | const SkScalar moveY = SkIntToScalar(2); |
| 727 | SkASSERT(value != moveX && value != moveY); |
| 728 | |
| 729 | path.moveTo(moveX, moveY); |
| 730 | REPORTER_ASSERT(reporter, !path.isLine(NULL)); |
| 731 | REPORTER_ASSERT(reporter, !path.isLine(pts)); |
| 732 | // check that pts was untouched |
| 733 | REPORTER_ASSERT(reporter, pts[0].equals(value, value)); |
| 734 | REPORTER_ASSERT(reporter, pts[1].equals(value, value)); |
| 735 | |
| 736 | const SkScalar lineX = SkIntToScalar(2); |
| 737 | const SkScalar lineY = SkIntToScalar(2); |
| 738 | SkASSERT(value != lineX && value != lineY); |
| 739 | |
| 740 | path.lineTo(lineX, lineY); |
| 741 | REPORTER_ASSERT(reporter, path.isLine(NULL)); |
| 742 | |
| 743 | REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY)); |
| 744 | REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY)); |
| 745 | REPORTER_ASSERT(reporter, path.isLine(pts)); |
| 746 | REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY)); |
| 747 | REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY)); |
| 748 | |
| 749 | path.lineTo(0, 0); // too many points/verbs |
| 750 | REPORTER_ASSERT(reporter, !path.isLine(NULL)); |
| 751 | REPORTER_ASSERT(reporter, !path.isLine(pts)); |
| 752 | REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY)); |
| 753 | REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY)); |
| 754 | } |
| 755 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 756 | // Simple isRect test is inline TestPath, below. |
| 757 | // test_isRect provides more extensive testing. |
| 758 | static void test_isRect(skiatest::Reporter* reporter) { |
| 759 | // passing tests (all moveTo / lineTo... |
| 760 | SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; |
| 761 | SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}}; |
| 762 | SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}}; |
| 763 | SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}}; |
| 764 | SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; |
| 765 | SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}}; |
| 766 | SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}}; |
| 767 | SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}}; |
| 768 | SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}}; |
| 769 | SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, |
| 770 | {1, 0}, {.5f, 0}}; |
| 771 | SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, |
| 772 | {0, 1}, {0, .5f}}; |
| 773 | SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; |
| 774 | SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; |
| 775 | SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 776 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 777 | // failing tests |
| 778 | SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points |
| 779 | SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal |
| 780 | SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps |
| 781 | SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up |
| 782 | SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots |
| 783 | SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots |
| 784 | SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots |
| 785 | SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L' |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 786 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 787 | // failing, no close |
| 788 | SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match |
| 789 | SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto |
| 790 | |
| 791 | size_t testLen[] = { |
| 792 | sizeof(r1), sizeof(r2), sizeof(r3), sizeof(r4), sizeof(r5), sizeof(r6), |
| 793 | sizeof(r7), sizeof(r8), sizeof(r9), sizeof(ra), sizeof(rb), sizeof(rc), |
| 794 | sizeof(rd), sizeof(re), |
| 795 | sizeof(f1), sizeof(f2), sizeof(f3), sizeof(f4), sizeof(f5), sizeof(f6), |
| 796 | sizeof(f7), sizeof(f8), |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 797 | sizeof(c1), sizeof(c2) |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 798 | }; |
| 799 | SkPoint* tests[] = { |
| 800 | r1, r2, r3, r4, r5, r6, r7, r8, r9, ra, rb, rc, rd, re, |
| 801 | f1, f2, f3, f4, f5, f6, f7, f8, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 802 | c1, c2 |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 803 | }; |
| 804 | SkPoint* lastPass = re; |
| 805 | SkPoint* lastClose = f8; |
| 806 | bool fail = false; |
| 807 | bool close = true; |
| 808 | const size_t testCount = sizeof(tests) / sizeof(tests[0]); |
| 809 | size_t index; |
| 810 | for (size_t testIndex = 0; testIndex < testCount; ++testIndex) { |
| 811 | SkPath path; |
| 812 | path.moveTo(tests[testIndex][0].fX, tests[testIndex][0].fY); |
| 813 | for (index = 1; index < testLen[testIndex] / sizeof(SkPoint); ++index) { |
| 814 | path.lineTo(tests[testIndex][index].fX, tests[testIndex][index].fY); |
| 815 | } |
| 816 | if (close) { |
| 817 | path.close(); |
| 818 | } |
| 819 | REPORTER_ASSERT(reporter, fail ^ path.isRect(0)); |
| 820 | if (tests[testIndex] == lastPass) { |
| 821 | fail = true; |
| 822 | } |
| 823 | if (tests[testIndex] == lastClose) { |
| 824 | close = false; |
| 825 | } |
| 826 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 827 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 828 | // fail, close then line |
| 829 | SkPath path1; |
| 830 | path1.moveTo(r1[0].fX, r1[0].fY); |
| 831 | for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) { |
| 832 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 833 | } |
| 834 | path1.close(); |
| 835 | path1.lineTo(1, 0); |
| 836 | REPORTER_ASSERT(reporter, fail ^ path1.isRect(0)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 837 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 838 | // fail, move in the middle |
| 839 | path1.reset(); |
| 840 | path1.moveTo(r1[0].fX, r1[0].fY); |
| 841 | for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) { |
| 842 | if (index == 2) { |
| 843 | path1.moveTo(1, .5f); |
| 844 | } |
| 845 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 846 | } |
| 847 | path1.close(); |
| 848 | REPORTER_ASSERT(reporter, fail ^ path1.isRect(0)); |
| 849 | |
| 850 | // fail, move on the edge |
| 851 | path1.reset(); |
| 852 | for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) { |
| 853 | path1.moveTo(r1[index - 1].fX, r1[index - 1].fY); |
| 854 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 855 | } |
| 856 | path1.close(); |
| 857 | REPORTER_ASSERT(reporter, fail ^ path1.isRect(0)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 858 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 859 | // fail, quad |
| 860 | path1.reset(); |
| 861 | path1.moveTo(r1[0].fX, r1[0].fY); |
| 862 | for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) { |
| 863 | if (index == 2) { |
| 864 | path1.quadTo(1, .5f, 1, .5f); |
| 865 | } |
| 866 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 867 | } |
| 868 | path1.close(); |
| 869 | REPORTER_ASSERT(reporter, fail ^ path1.isRect(0)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 870 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 871 | // fail, cubic |
| 872 | path1.reset(); |
| 873 | path1.moveTo(r1[0].fX, r1[0].fY); |
| 874 | for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) { |
| 875 | if (index == 2) { |
| 876 | path1.cubicTo(1, .5f, 1, .5f, 1, .5f); |
| 877 | } |
| 878 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 879 | } |
| 880 | path1.close(); |
| 881 | REPORTER_ASSERT(reporter, fail ^ path1.isRect(0)); |
| 882 | } |
| 883 | |
robertphillips@google.com | 2972bb5 | 2012-08-07 17:32:51 +0000 | [diff] [blame] | 884 | static void write_and_read_back(skiatest::Reporter* reporter, |
| 885 | const SkPath& p) { |
| 886 | SkWriter32 writer(100); |
| 887 | writer.writePath(p); |
| 888 | size_t size = writer.size(); |
| 889 | SkAutoMalloc storage(size); |
| 890 | writer.flatten(storage.get()); |
| 891 | SkReader32 reader(storage.get(), size); |
| 892 | |
| 893 | SkPath readBack; |
| 894 | REPORTER_ASSERT(reporter, readBack != p); |
| 895 | reader.readPath(&readBack); |
| 896 | REPORTER_ASSERT(reporter, readBack == p); |
| 897 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 898 | REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() == |
robertphillips@google.com | 2972bb5 | 2012-08-07 17:32:51 +0000 | [diff] [blame] | 899 | p.getConvexityOrUnknown()); |
| 900 | |
| 901 | REPORTER_ASSERT(reporter, readBack.isOval(NULL) == p.isOval(NULL)); |
| 902 | |
| 903 | const SkRect& origBounds = p.getBounds(); |
| 904 | const SkRect& readBackBounds = readBack.getBounds(); |
| 905 | |
| 906 | REPORTER_ASSERT(reporter, origBounds == readBackBounds); |
| 907 | } |
| 908 | |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 909 | static void test_flattening(skiatest::Reporter* reporter) { |
| 910 | SkPath p; |
| 911 | |
| 912 | static const SkPoint pts[] = { |
| 913 | { 0, 0 }, |
| 914 | { SkIntToScalar(10), SkIntToScalar(10) }, |
| 915 | { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, |
| 916 | { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) } |
| 917 | }; |
| 918 | p.moveTo(pts[0]); |
| 919 | p.lineTo(pts[1]); |
| 920 | p.quadTo(pts[2], pts[3]); |
| 921 | p.cubicTo(pts[4], pts[5], pts[6]); |
| 922 | |
robertphillips@google.com | 2972bb5 | 2012-08-07 17:32:51 +0000 | [diff] [blame] | 923 | write_and_read_back(reporter, p); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 924 | |
| 925 | // create a buffer that should be much larger than the path so we don't |
| 926 | // kill our stack if writer goes too far. |
| 927 | char buffer[1024]; |
| 928 | uint32_t size1 = p.writeToMemory(NULL); |
| 929 | uint32_t size2 = p.writeToMemory(buffer); |
| 930 | REPORTER_ASSERT(reporter, size1 == size2); |
| 931 | |
| 932 | SkPath p2; |
| 933 | uint32_t size3 = p2.readFromMemory(buffer); |
| 934 | REPORTER_ASSERT(reporter, size1 == size3); |
| 935 | REPORTER_ASSERT(reporter, p == p2); |
| 936 | |
| 937 | char buffer2[1024]; |
| 938 | size3 = p2.writeToMemory(buffer2); |
| 939 | REPORTER_ASSERT(reporter, size1 == size3); |
| 940 | REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0); |
robertphillips@google.com | 2972bb5 | 2012-08-07 17:32:51 +0000 | [diff] [blame] | 941 | |
| 942 | // test persistence of the oval flag & convexity |
| 943 | { |
| 944 | SkPath oval; |
| 945 | SkRect rect = SkRect::MakeWH(10, 10); |
| 946 | oval.addOval(rect); |
| 947 | |
| 948 | write_and_read_back(reporter, oval); |
| 949 | } |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | static void test_transform(skiatest::Reporter* reporter) { |
| 953 | SkPath p, p1; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 954 | |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 955 | static const SkPoint pts[] = { |
| 956 | { 0, 0 }, |
| 957 | { SkIntToScalar(10), SkIntToScalar(10) }, |
| 958 | { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, |
| 959 | { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) } |
| 960 | }; |
| 961 | p.moveTo(pts[0]); |
| 962 | p.lineTo(pts[1]); |
| 963 | p.quadTo(pts[2], pts[3]); |
| 964 | p.cubicTo(pts[4], pts[5], pts[6]); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 965 | |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 966 | SkMatrix matrix; |
| 967 | matrix.reset(); |
| 968 | p.transform(matrix, &p1); |
| 969 | REPORTER_ASSERT(reporter, p == p1); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 970 | |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 971 | matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3); |
| 972 | p.transform(matrix, &p1); |
| 973 | SkPoint pts1[7]; |
| 974 | int count = p1.getPoints(pts1, 7); |
| 975 | REPORTER_ASSERT(reporter, 7 == count); |
| 976 | for (int i = 0; i < count; ++i) { |
| 977 | SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3); |
| 978 | REPORTER_ASSERT(reporter, newPt == pts1[i]); |
| 979 | } |
| 980 | } |
| 981 | |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 982 | static void test_zero_length_paths(skiatest::Reporter* reporter) { |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 983 | SkPath p; |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 984 | uint8_t verbs[32]; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 985 | |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 986 | struct zeroPathTestData { |
| 987 | const char* testPath; |
| 988 | const size_t numResultPts; |
| 989 | const SkRect resultBound; |
| 990 | const SkPath::Verb* resultVerbs; |
| 991 | const size_t numResultVerbs; |
| 992 | }; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 993 | |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 994 | static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb }; |
| 995 | static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb }; |
| 996 | static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb }; |
| 997 | static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb }; |
| 998 | static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb }; |
| 999 | static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb }; |
| 1000 | static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb }; |
| 1001 | static const SkPath::Verb resultVerbs8[] = { |
| 1002 | SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb |
| 1003 | }; |
| 1004 | static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb }; |
| 1005 | static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb }; |
| 1006 | static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb }; |
| 1007 | static const SkPath::Verb resultVerbs12[] = { |
| 1008 | SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb |
| 1009 | }; |
| 1010 | static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb }; |
| 1011 | static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb }; |
| 1012 | static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb }; |
| 1013 | static const SkPath::Verb resultVerbs16[] = { |
| 1014 | SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb |
| 1015 | }; |
| 1016 | static const struct zeroPathTestData gZeroLengthTests[] = { |
| 1017 | { "M 1 1", 1, {0, 0, 0, 0}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
schenney@chromium.org | aaf1688 | 2012-06-13 17:41:00 +0000 | [diff] [blame] | 1018 | { "M 1 1 M 2 1", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) }, |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1019 | { "M 1 1 z", 1, {0, 0, 0, 0}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) }, |
schenney@chromium.org | aaf1688 | 2012-06-13 17:41:00 +0000 | [diff] [blame] | 1020 | { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) }, |
| 1021 | { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) }, |
| 1022 | { "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) }, |
| 1023 | { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) }, |
| 1024 | { "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) }, |
| 1025 | { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) }, |
| 1026 | { "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) }, |
| 1027 | { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) }, |
| 1028 | { "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) }, |
| 1029 | { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) }, |
| 1030 | { "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.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1031 | SK_ARRAY_COUNT(resultVerbs14) |
| 1032 | }, |
schenney@chromium.org | aaf1688 | 2012-06-13 17:41:00 +0000 | [diff] [blame] | 1033 | { "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) }, |
| 1034 | { "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.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1035 | SK_ARRAY_COUNT(resultVerbs16) |
| 1036 | } |
| 1037 | }; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1038 | |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1039 | for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) { |
| 1040 | p.reset(); |
| 1041 | bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p); |
| 1042 | REPORTER_ASSERT(reporter, valid); |
| 1043 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
| 1044 | REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints()); |
| 1045 | REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds()); |
| 1046 | REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs))); |
| 1047 | for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) { |
| 1048 | REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]); |
| 1049 | } |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 1050 | } |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | struct SegmentInfo { |
| 1054 | SkPath fPath; |
| 1055 | int fPointCount; |
| 1056 | }; |
| 1057 | |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 1058 | #define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask) |
| 1059 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1060 | static void test_segment_masks(skiatest::Reporter* reporter) { |
reed@google.com | eef938c | 2012-08-01 20:01:49 +0000 | [diff] [blame] | 1061 | SkPath p, p2; |
| 1062 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1063 | p.moveTo(0, 0); |
| 1064 | p.quadTo(100, 100, 200, 200); |
| 1065 | REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks()); |
| 1066 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
reed@google.com | eef938c | 2012-08-01 20:01:49 +0000 | [diff] [blame] | 1067 | p2 = p; |
| 1068 | REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks()); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1069 | p.cubicTo(100, 100, 200, 200, 300, 300); |
| 1070 | REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks()); |
| 1071 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
reed@google.com | eef938c | 2012-08-01 20:01:49 +0000 | [diff] [blame] | 1072 | p2 = p; |
| 1073 | REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks()); |
| 1074 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1075 | p.reset(); |
| 1076 | p.moveTo(0, 0); |
| 1077 | p.cubicTo(100, 100, 200, 200, 300, 300); |
| 1078 | REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks()); |
reed@google.com | eef938c | 2012-08-01 20:01:49 +0000 | [diff] [blame] | 1079 | p2 = p; |
| 1080 | REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1081 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1082 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
| 1083 | } |
| 1084 | |
| 1085 | static void test_iter(skiatest::Reporter* reporter) { |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1086 | SkPath p; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1087 | SkPoint pts[4]; |
| 1088 | |
| 1089 | // Test an iterator with no path |
| 1090 | SkPath::Iter noPathIter; |
| 1091 | REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1092 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1093 | // Test that setting an empty path works |
| 1094 | noPathIter.setPath(p, false); |
| 1095 | REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1096 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1097 | // Test that close path makes no difference for an empty path |
| 1098 | noPathIter.setPath(p, true); |
| 1099 | REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1100 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1101 | // Test an iterator with an initial empty path |
| 1102 | SkPath::Iter iter(p, false); |
| 1103 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 1104 | |
| 1105 | // Test that close path makes no difference |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1106 | iter.setPath(p, true); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1107 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 1108 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1109 | |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1110 | struct iterTestData { |
| 1111 | const char* testPath; |
| 1112 | const bool forceClose; |
| 1113 | const bool consumeDegenerates; |
| 1114 | const size_t* numResultPtsPerVerb; |
| 1115 | const SkPoint* resultPts; |
| 1116 | const SkPath::Verb* resultVerbs; |
| 1117 | const size_t numResultVerbs; |
| 1118 | }; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1119 | |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1120 | static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb }; |
| 1121 | static const SkPath::Verb resultVerbs2[] = { |
| 1122 | SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb |
| 1123 | }; |
| 1124 | static const SkPath::Verb resultVerbs3[] = { |
| 1125 | SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb |
| 1126 | }; |
| 1127 | static const SkPath::Verb resultVerbs4[] = { |
| 1128 | SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb |
| 1129 | }; |
| 1130 | static const SkPath::Verb resultVerbs5[] = { |
| 1131 | SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb |
| 1132 | }; |
| 1133 | static const size_t resultPtsSizes1[] = { 0 }; |
schenney@chromium.org | fedd09b | 2012-06-13 18:29:20 +0000 | [diff] [blame] | 1134 | static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 }; |
| 1135 | static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 }; |
| 1136 | static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 }; |
| 1137 | static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 }; |
schenney@chromium.org | aaf1688 | 2012-06-13 17:41:00 +0000 | [diff] [blame] | 1138 | static const SkPoint* resultPts1 = 0; |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1139 | static const SkPoint resultPts2[] = { |
| 1140 | { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 } |
| 1141 | }; |
| 1142 | static const SkPoint resultPts3[] = { |
| 1143 | { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }, |
| 1144 | { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 } |
| 1145 | }; |
| 1146 | static const SkPoint resultPts4[] = { |
| 1147 | { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 } |
| 1148 | }; |
| 1149 | static const SkPoint resultPts5[] = { |
| 1150 | { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 } |
| 1151 | }; |
| 1152 | static const struct iterTestData gIterTests[] = { |
| 1153 | { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
schenney@chromium.org | aaf1688 | 2012-06-13 17:41:00 +0000 | [diff] [blame] | 1154 | { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
| 1155 | { "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.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1156 | { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
| 1157 | { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
| 1158 | { "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) }, |
| 1159 | { "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.org | aaf1688 | 2012-06-13 17:41:00 +0000 | [diff] [blame] | 1160 | { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) }, |
| 1161 | { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) }, |
| 1162 | { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
| 1163 | { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
| 1164 | { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) }, |
| 1165 | { "M 1 0 L 1 0 M 0 0 z", true, false, resultPtsSizes5, resultPts5, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) } |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1166 | }; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1167 | |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1168 | for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) { |
| 1169 | p.reset(); |
| 1170 | bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p); |
| 1171 | REPORTER_ASSERT(reporter, valid); |
| 1172 | iter.setPath(p, gIterTests[i].forceClose); |
| 1173 | int j = 0, l = 0; |
| 1174 | do { |
| 1175 | REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]); |
| 1176 | for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) { |
| 1177 | REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]); |
| 1178 | } |
| 1179 | } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb); |
| 1180 | REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs); |
| 1181 | } |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1182 | |
| 1183 | // The GM degeneratesegments.cpp test is more extensive |
| 1184 | } |
| 1185 | |
| 1186 | static void test_raw_iter(skiatest::Reporter* reporter) { |
| 1187 | SkPath p; |
| 1188 | SkPoint pts[4]; |
| 1189 | |
| 1190 | // Test an iterator with no path |
| 1191 | SkPath::RawIter noPathIter; |
| 1192 | REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); |
| 1193 | // Test that setting an empty path works |
| 1194 | noPathIter.setPath(p); |
| 1195 | REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1196 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1197 | // Test an iterator with an initial empty path |
| 1198 | SkPath::RawIter iter(p); |
| 1199 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 1200 | |
| 1201 | // Test that a move-only path returns the move. |
| 1202 | p.moveTo(SK_Scalar1, 0); |
| 1203 | iter.setPath(p); |
| 1204 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 1205 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1); |
| 1206 | REPORTER_ASSERT(reporter, pts[0].fY == 0); |
| 1207 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 1208 | |
| 1209 | // No matter how many moves we add, we should get them all back |
| 1210 | p.moveTo(SK_Scalar1*2, SK_Scalar1); |
| 1211 | p.moveTo(SK_Scalar1*3, SK_Scalar1*2); |
| 1212 | iter.setPath(p); |
| 1213 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 1214 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1); |
| 1215 | REPORTER_ASSERT(reporter, pts[0].fY == 0); |
| 1216 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 1217 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2); |
| 1218 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1); |
| 1219 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 1220 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3); |
| 1221 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2); |
| 1222 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 1223 | |
| 1224 | // Initial close is never ever stored |
| 1225 | p.reset(); |
| 1226 | p.close(); |
| 1227 | iter.setPath(p); |
| 1228 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 1229 | |
| 1230 | // Move/close sequences |
| 1231 | p.reset(); |
| 1232 | p.close(); // Not stored, no purpose |
| 1233 | p.moveTo(SK_Scalar1, 0); |
| 1234 | p.close(); |
| 1235 | p.close(); // Not stored, no purpose |
| 1236 | p.moveTo(SK_Scalar1*2, SK_Scalar1); |
| 1237 | p.close(); |
| 1238 | p.moveTo(SK_Scalar1*3, SK_Scalar1*2); |
| 1239 | p.moveTo(SK_Scalar1*4, SK_Scalar1*3); |
| 1240 | p.close(); |
| 1241 | iter.setPath(p); |
| 1242 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 1243 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1); |
| 1244 | REPORTER_ASSERT(reporter, pts[0].fY == 0); |
| 1245 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb); |
| 1246 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1); |
| 1247 | REPORTER_ASSERT(reporter, pts[0].fY == 0); |
| 1248 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 1249 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2); |
| 1250 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1); |
| 1251 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb); |
| 1252 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2); |
| 1253 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1); |
| 1254 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 1255 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3); |
| 1256 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2); |
| 1257 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 1258 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4); |
| 1259 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3); |
| 1260 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb); |
| 1261 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4); |
| 1262 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3); |
| 1263 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 1264 | |
| 1265 | // Generate random paths and verify |
| 1266 | SkPoint randomPts[25]; |
| 1267 | for (int i = 0; i < 5; ++i) { |
| 1268 | for (int j = 0; j < 5; ++j) { |
| 1269 | randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j); |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | // Max of 10 segments, max 3 points per segment |
| 1274 | SkRandom rand(9876543); |
| 1275 | SkPoint expectedPts[31]; // May have leading moveTo |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 1276 | SkPath::Verb expectedVerbs[22]; // May have leading moveTo |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1277 | SkPath::Verb nextVerb; |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 1278 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1279 | for (int i = 0; i < 500; ++i) { |
| 1280 | p.reset(); |
| 1281 | bool lastWasClose = true; |
| 1282 | bool haveMoveTo = false; |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 1283 | SkPoint lastMoveToPt = { 0, 0 }; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1284 | int numPoints = 0; |
| 1285 | int numVerbs = (rand.nextU() >> 16) % 10; |
| 1286 | int numIterVerbs = 0; |
| 1287 | for (int j = 0; j < numVerbs; ++j) { |
| 1288 | do { |
| 1289 | nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb); |
| 1290 | } while (lastWasClose && nextVerb == SkPath::kClose_Verb); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1291 | switch (nextVerb) { |
| 1292 | case SkPath::kMove_Verb: |
| 1293 | expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; |
| 1294 | p.moveTo(expectedPts[numPoints]); |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 1295 | lastMoveToPt = expectedPts[numPoints]; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1296 | numPoints += 1; |
| 1297 | lastWasClose = false; |
| 1298 | haveMoveTo = true; |
| 1299 | break; |
| 1300 | case SkPath::kLine_Verb: |
| 1301 | if (!haveMoveTo) { |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 1302 | expectedPts[numPoints++] = lastMoveToPt; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1303 | expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb; |
| 1304 | haveMoveTo = true; |
| 1305 | } |
| 1306 | expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; |
| 1307 | p.lineTo(expectedPts[numPoints]); |
| 1308 | numPoints += 1; |
| 1309 | lastWasClose = false; |
| 1310 | break; |
| 1311 | case SkPath::kQuad_Verb: |
| 1312 | if (!haveMoveTo) { |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 1313 | expectedPts[numPoints++] = lastMoveToPt; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1314 | expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb; |
| 1315 | haveMoveTo = true; |
| 1316 | } |
| 1317 | expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; |
| 1318 | expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25]; |
| 1319 | p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]); |
| 1320 | numPoints += 2; |
| 1321 | lastWasClose = false; |
| 1322 | break; |
| 1323 | case SkPath::kCubic_Verb: |
| 1324 | if (!haveMoveTo) { |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 1325 | expectedPts[numPoints++] = lastMoveToPt; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1326 | expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb; |
| 1327 | haveMoveTo = true; |
| 1328 | } |
| 1329 | expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; |
| 1330 | expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25]; |
| 1331 | expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25]; |
| 1332 | p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1], |
| 1333 | expectedPts[numPoints + 2]); |
| 1334 | numPoints += 3; |
| 1335 | lastWasClose = false; |
| 1336 | break; |
| 1337 | case SkPath::kClose_Verb: |
| 1338 | p.close(); |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 1339 | haveMoveTo = false; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1340 | lastWasClose = true; |
| 1341 | break; |
| 1342 | default:; |
| 1343 | } |
| 1344 | expectedVerbs[numIterVerbs++] = nextVerb; |
| 1345 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1346 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1347 | iter.setPath(p); |
| 1348 | numVerbs = numIterVerbs; |
| 1349 | numIterVerbs = 0; |
| 1350 | int numIterPts = 0; |
| 1351 | SkPoint lastMoveTo; |
| 1352 | SkPoint lastPt; |
| 1353 | lastMoveTo.set(0, 0); |
| 1354 | lastPt.set(0, 0); |
| 1355 | while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 1356 | REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]); |
| 1357 | numIterVerbs++; |
| 1358 | switch (nextVerb) { |
| 1359 | case SkPath::kMove_Verb: |
| 1360 | REPORTER_ASSERT(reporter, numIterPts < numPoints); |
| 1361 | REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]); |
| 1362 | lastPt = lastMoveTo = pts[0]; |
| 1363 | numIterPts += 1; |
| 1364 | break; |
| 1365 | case SkPath::kLine_Verb: |
| 1366 | REPORTER_ASSERT(reporter, numIterPts < numPoints + 1); |
| 1367 | REPORTER_ASSERT(reporter, pts[0] == lastPt); |
| 1368 | REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]); |
| 1369 | lastPt = pts[1]; |
| 1370 | numIterPts += 1; |
| 1371 | break; |
| 1372 | case SkPath::kQuad_Verb: |
| 1373 | REPORTER_ASSERT(reporter, numIterPts < numPoints + 2); |
| 1374 | REPORTER_ASSERT(reporter, pts[0] == lastPt); |
| 1375 | REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]); |
| 1376 | REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]); |
| 1377 | lastPt = pts[2]; |
| 1378 | numIterPts += 2; |
| 1379 | break; |
| 1380 | case SkPath::kCubic_Verb: |
| 1381 | REPORTER_ASSERT(reporter, numIterPts < numPoints + 3); |
| 1382 | REPORTER_ASSERT(reporter, pts[0] == lastPt); |
| 1383 | REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]); |
| 1384 | REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]); |
| 1385 | REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]); |
| 1386 | lastPt = pts[3]; |
| 1387 | numIterPts += 3; |
| 1388 | break; |
| 1389 | case SkPath::kClose_Verb: |
| 1390 | REPORTER_ASSERT(reporter, pts[0] == lastMoveTo); |
| 1391 | lastPt = lastMoveTo; |
| 1392 | break; |
| 1393 | default:; |
| 1394 | } |
| 1395 | } |
| 1396 | REPORTER_ASSERT(reporter, numIterPts == numPoints); |
| 1397 | REPORTER_ASSERT(reporter, numIterVerbs == numVerbs); |
| 1398 | } |
| 1399 | } |
| 1400 | |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1401 | static void check_for_circle(skiatest::Reporter* reporter, |
| 1402 | const SkPath& path, bool expected) { |
| 1403 | SkRect rect; |
| 1404 | REPORTER_ASSERT(reporter, path.isOval(&rect) == expected); |
| 1405 | if (expected) { |
| 1406 | REPORTER_ASSERT(reporter, rect.height() == rect.width()); |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | static void test_circle_skew(skiatest::Reporter* reporter, |
| 1411 | const SkPath& path) { |
| 1412 | SkPath tmp; |
| 1413 | |
| 1414 | SkMatrix m; |
| 1415 | m.setSkew(SkIntToScalar(3), SkIntToScalar(5)); |
| 1416 | path.transform(m, &tmp); |
| 1417 | check_for_circle(reporter, tmp, false); |
| 1418 | } |
| 1419 | |
| 1420 | static void test_circle_translate(skiatest::Reporter* reporter, |
| 1421 | const SkPath& path) { |
| 1422 | SkPath tmp; |
| 1423 | |
| 1424 | // translate at small offset |
| 1425 | SkMatrix m; |
| 1426 | m.setTranslate(SkIntToScalar(15), SkIntToScalar(15)); |
| 1427 | path.transform(m, &tmp); |
| 1428 | check_for_circle(reporter, tmp, true); |
| 1429 | |
| 1430 | tmp.reset(); |
| 1431 | m.reset(); |
| 1432 | |
| 1433 | // translate at a relatively big offset |
| 1434 | m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000)); |
| 1435 | path.transform(m, &tmp); |
| 1436 | check_for_circle(reporter, tmp, true); |
| 1437 | } |
| 1438 | |
| 1439 | static void test_circle_rotate(skiatest::Reporter* reporter, |
| 1440 | const SkPath& path) { |
| 1441 | for (int angle = 0; angle < 360; ++angle) { |
| 1442 | SkPath tmp; |
| 1443 | SkMatrix m; |
| 1444 | m.setRotate(SkIntToScalar(angle)); |
| 1445 | path.transform(m, &tmp); |
| 1446 | |
| 1447 | // TODO: a rotated circle whose rotated angle is not a mutiple of 90 |
| 1448 | // degrees is not an oval anymore, this can be improved. we made this |
| 1449 | // for the simplicity of our implementation. |
| 1450 | if (angle % 90 == 0) { |
| 1451 | check_for_circle(reporter, tmp, true); |
| 1452 | } else { |
| 1453 | check_for_circle(reporter, tmp, false); |
| 1454 | } |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | static void test_circle_with_direction(skiatest::Reporter* reporter, |
| 1459 | SkPath::Direction dir) { |
| 1460 | SkPath path; |
| 1461 | |
| 1462 | // circle at origin |
| 1463 | path.addCircle(0, 0, SkIntToScalar(20), dir); |
| 1464 | check_for_circle(reporter, path, true); |
| 1465 | test_circle_rotate(reporter, path); |
| 1466 | test_circle_translate(reporter, path); |
| 1467 | test_circle_skew(reporter, path); |
| 1468 | |
| 1469 | // circle at an offset at (10, 10) |
| 1470 | path.reset(); |
| 1471 | path.addCircle(SkIntToScalar(10), SkIntToScalar(10), |
| 1472 | SkIntToScalar(20), dir); |
| 1473 | check_for_circle(reporter, path, true); |
| 1474 | test_circle_rotate(reporter, path); |
| 1475 | test_circle_translate(reporter, path); |
| 1476 | test_circle_skew(reporter, path); |
| 1477 | } |
| 1478 | |
| 1479 | static void test_circle_with_add_paths(skiatest::Reporter* reporter) { |
| 1480 | SkPath path; |
| 1481 | SkPath circle; |
| 1482 | SkPath rect; |
| 1483 | SkPath empty; |
| 1484 | |
| 1485 | circle.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction); |
| 1486 | rect.addRect(SkIntToScalar(5), SkIntToScalar(5), |
| 1487 | SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction); |
| 1488 | |
| 1489 | SkMatrix translate; |
| 1490 | translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12)); |
| 1491 | |
| 1492 | // For simplicity, all the path concatenation related operations |
| 1493 | // would mark it non-circle, though in theory it's still a circle. |
| 1494 | |
| 1495 | // empty + circle (translate) |
| 1496 | path = empty; |
| 1497 | path.addPath(circle, translate); |
| 1498 | check_for_circle(reporter, path, false); |
| 1499 | |
| 1500 | // circle + empty (translate) |
| 1501 | path = circle; |
| 1502 | path.addPath(empty, translate); |
| 1503 | check_for_circle(reporter, path, false); |
| 1504 | |
| 1505 | // test reverseAddPath |
| 1506 | path = circle; |
| 1507 | path.reverseAddPath(rect); |
| 1508 | check_for_circle(reporter, path, false); |
| 1509 | } |
| 1510 | |
| 1511 | static void test_circle(skiatest::Reporter* reporter) { |
| 1512 | test_circle_with_direction(reporter, SkPath::kCW_Direction); |
| 1513 | test_circle_with_direction(reporter, SkPath::kCCW_Direction); |
| 1514 | |
| 1515 | // multiple addCircle() |
| 1516 | SkPath path; |
| 1517 | path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction); |
| 1518 | path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction); |
| 1519 | check_for_circle(reporter, path, false); |
| 1520 | |
| 1521 | // some extra lineTo() would make isOval() fail |
| 1522 | path.reset(); |
| 1523 | path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction); |
| 1524 | path.lineTo(0, 0); |
| 1525 | check_for_circle(reporter, path, false); |
| 1526 | |
| 1527 | // not back to the original point |
| 1528 | path.reset(); |
| 1529 | path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction); |
| 1530 | path.setLastPt(SkIntToScalar(5), SkIntToScalar(5)); |
| 1531 | check_for_circle(reporter, path, false); |
| 1532 | |
| 1533 | test_circle_with_add_paths(reporter); |
| 1534 | } |
| 1535 | |
| 1536 | static void test_oval(skiatest::Reporter* reporter) { |
| 1537 | SkRect rect; |
| 1538 | SkMatrix m; |
| 1539 | SkPath path; |
| 1540 | |
| 1541 | rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50)); |
| 1542 | path.addOval(rect); |
| 1543 | |
| 1544 | REPORTER_ASSERT(reporter, path.isOval(NULL)); |
| 1545 | |
| 1546 | m.setRotate(SkIntToScalar(90)); |
| 1547 | SkPath tmp; |
| 1548 | path.transform(m, &tmp); |
| 1549 | // an oval rotated 90 degrees is still an oval. |
| 1550 | REPORTER_ASSERT(reporter, tmp.isOval(NULL)); |
| 1551 | |
| 1552 | m.reset(); |
| 1553 | m.setRotate(SkIntToScalar(30)); |
| 1554 | tmp.reset(); |
| 1555 | path.transform(m, &tmp); |
| 1556 | // an oval rotated 30 degrees is not an oval anymore. |
| 1557 | REPORTER_ASSERT(reporter, !tmp.isOval(NULL)); |
| 1558 | |
| 1559 | // since empty path being transformed. |
| 1560 | path.reset(); |
| 1561 | tmp.reset(); |
| 1562 | m.reset(); |
| 1563 | path.transform(m, &tmp); |
| 1564 | REPORTER_ASSERT(reporter, !tmp.isOval(NULL)); |
| 1565 | |
| 1566 | // empty path is not an oval |
| 1567 | tmp.reset(); |
| 1568 | REPORTER_ASSERT(reporter, !tmp.isOval(NULL)); |
| 1569 | |
| 1570 | // only has moveTo()s |
| 1571 | tmp.reset(); |
| 1572 | tmp.moveTo(0, 0); |
| 1573 | tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10)); |
| 1574 | REPORTER_ASSERT(reporter, !tmp.isOval(NULL)); |
| 1575 | |
| 1576 | // mimic WebKit's calling convention, |
| 1577 | // call moveTo() first and then call addOval() |
| 1578 | path.reset(); |
| 1579 | path.moveTo(0, 0); |
| 1580 | path.addOval(rect); |
| 1581 | REPORTER_ASSERT(reporter, path.isOval(NULL)); |
| 1582 | |
| 1583 | // copy path |
| 1584 | path.reset(); |
| 1585 | tmp.reset(); |
| 1586 | tmp.addOval(rect); |
| 1587 | path = tmp; |
| 1588 | REPORTER_ASSERT(reporter, path.isOval(NULL)); |
| 1589 | } |
| 1590 | |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 1591 | static void TestPath(skiatest::Reporter* reporter) { |
reed@android.com | 60bc6d5 | 2010-02-11 11:09:39 +0000 | [diff] [blame] | 1592 | SkTSize<SkScalar>::Make(3,4); |
| 1593 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 1594 | SkPath p, p2; |
| 1595 | SkRect bounds, bounds2; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 1596 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 1597 | REPORTER_ASSERT(reporter, p.isEmpty()); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1598 | REPORTER_ASSERT(reporter, 0 == p.countPoints()); |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 1599 | REPORTER_ASSERT(reporter, 0 == p.countVerbs()); |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 1600 | REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks()); |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 1601 | REPORTER_ASSERT(reporter, p.isConvex()); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 1602 | REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType); |
| 1603 | REPORTER_ASSERT(reporter, !p.isInverseFillType()); |
| 1604 | REPORTER_ASSERT(reporter, p == p2); |
| 1605 | REPORTER_ASSERT(reporter, !(p != p2)); |
| 1606 | |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 1607 | REPORTER_ASSERT(reporter, p.getBounds().isEmpty()); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 1608 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 1609 | bounds.set(0, 0, SK_Scalar1, SK_Scalar1); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 1610 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 1611 | p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1); |
| 1612 | check_convex_bounds(reporter, p, bounds); |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 1613 | // we have quads or cubics |
| 1614 | REPORTER_ASSERT(reporter, p.getSegmentMasks() & kCurveSegmentMask); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1615 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
reed@google.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 1616 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 1617 | p.reset(); |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 1618 | REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks()); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1619 | REPORTER_ASSERT(reporter, p.isEmpty()); |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 1620 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 1621 | p.addOval(bounds); |
| 1622 | check_convex_bounds(reporter, p, bounds); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1623 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
reed@google.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 1624 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 1625 | p.reset(); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 1626 | p.addRect(bounds); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 1627 | check_convex_bounds(reporter, p, bounds); |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 1628 | // we have only lines |
| 1629 | REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks()); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1630 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 1631 | |
| 1632 | REPORTER_ASSERT(reporter, p != p2); |
| 1633 | REPORTER_ASSERT(reporter, !(p == p2)); |
| 1634 | |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 1635 | // do getPoints and getVerbs return the right result |
| 1636 | REPORTER_ASSERT(reporter, p.getPoints(NULL, 0) == 4); |
| 1637 | REPORTER_ASSERT(reporter, p.getVerbs(NULL, 0) == 5); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 1638 | SkPoint pts[4]; |
| 1639 | int count = p.getPoints(pts, 4); |
| 1640 | REPORTER_ASSERT(reporter, count == 4); |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 1641 | uint8_t verbs[6]; |
| 1642 | verbs[5] = 0xff; |
| 1643 | p.getVerbs(verbs, 5); |
| 1644 | REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]); |
| 1645 | REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]); |
| 1646 | REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]); |
| 1647 | REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]); |
| 1648 | REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]); |
| 1649 | REPORTER_ASSERT(reporter, 0xff == verbs[5]); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 1650 | bounds2.set(pts, 4); |
| 1651 | REPORTER_ASSERT(reporter, bounds == bounds2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 1652 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 1653 | bounds.offset(SK_Scalar1*3, SK_Scalar1*4); |
| 1654 | p.offset(SK_Scalar1*3, SK_Scalar1*4); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 1655 | REPORTER_ASSERT(reporter, bounds == p.getBounds()); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 1656 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 1657 | REPORTER_ASSERT(reporter, p.isRect(NULL)); |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1658 | bounds2.setEmpty(); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 1659 | REPORTER_ASSERT(reporter, p.isRect(&bounds2)); |
| 1660 | REPORTER_ASSERT(reporter, bounds == bounds2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 1661 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 1662 | // now force p to not be a rect |
| 1663 | bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2); |
| 1664 | p.addRect(bounds); |
| 1665 | REPORTER_ASSERT(reporter, !p.isRect(NULL)); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 1666 | |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 1667 | test_isLine(reporter); |
| 1668 | test_isRect(reporter); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1669 | test_zero_length_paths(reporter); |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 1670 | test_direction(reporter); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1671 | test_convexity(reporter); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1672 | test_convexity2(reporter); |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 1673 | test_close(reporter); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1674 | test_segment_masks(reporter); |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 1675 | test_flattening(reporter); |
| 1676 | test_transform(reporter); |
reed@google.com | 3563c9e | 2011-11-14 19:34:57 +0000 | [diff] [blame] | 1677 | test_bounds(reporter); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1678 | test_iter(reporter); |
| 1679 | test_raw_iter(reporter); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 1680 | test_circle(reporter); |
| 1681 | test_oval(reporter); |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 1682 | test_strokerec(reporter); |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 1683 | test_addPoly(reporter); |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 1684 | test_isfinite(reporter); |
tomhudson@google.com | ed02c4d | 2012-08-10 14:10:45 +0000 | [diff] [blame] | 1685 | test_isfinite_after_transform(reporter); |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 1686 | test_tricky_cubic(reporter); |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 1687 | test_arb_round_rect_is_convex(reporter); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
| 1690 | #include "TestClassDef.h" |
| 1691 | DEFINE_TESTCLASS("Path", PathTestClass, TestPath) |