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