reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 8 | #include "SkPaint.h" |
| 9 | #include "SkPath.h" |
| 10 | #include "SkRect.h" |
| 11 | #include "SkStroke.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 12 | #include "Test.h" |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 13 | |
| 14 | static bool equal(const SkRect& a, const SkRect& b) { |
| 15 | return SkScalarNearlyEqual(a.left(), b.left()) && |
| 16 | SkScalarNearlyEqual(a.top(), b.top()) && |
| 17 | SkScalarNearlyEqual(a.right(), b.right()) && |
| 18 | SkScalarNearlyEqual(a.bottom(), b.bottom()); |
| 19 | } |
| 20 | |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame^] | 21 | static void test_strokecubic(skiatest::Reporter* reporter) { |
| 22 | uint32_t hexCubicVals[] = { |
| 23 | 0x424c1086, 0x44bcf0cb, // fX=51.0161362 fY=1511.52478 |
| 24 | 0x424c107c, 0x44bcf0cb, // fX=51.0160980 fY=1511.52478 |
| 25 | 0x424c10c2, 0x44bcf0cb, // fX=51.0163651 fY=1511.52478 |
| 26 | 0x424c1119, 0x44bcf0ca, // fX=51.0166969 fY=1511.52466 |
| 27 | }; |
| 28 | SkPoint cubicVals[] = { |
| 29 | {51.0161362f, 1511.52478f }, |
| 30 | {51.0160980f, 1511.52478f }, |
| 31 | {51.0163651f, 1511.52478f }, |
| 32 | {51.0166969f, 1511.52466f }, |
| 33 | }; |
| 34 | SkPaint paint; |
| 35 | |
| 36 | paint.setStyle(SkPaint::kStroke_Style); |
| 37 | paint.setStrokeWidth(0.394537568f); |
| 38 | SkPath path, fillPath; |
| 39 | path.moveTo(cubicVals[0]); |
| 40 | path.cubicTo(cubicVals[1], cubicVals[2], cubicVals[3]); |
| 41 | paint.getFillPath(path, &fillPath); |
| 42 | path.reset(); |
| 43 | path.moveTo(SkBits2Float(hexCubicVals[0]), SkBits2Float(hexCubicVals[1])); |
| 44 | path.cubicTo(SkBits2Float(hexCubicVals[2]), SkBits2Float(hexCubicVals[3]), |
| 45 | SkBits2Float(hexCubicVals[4]), SkBits2Float(hexCubicVals[5]), |
| 46 | SkBits2Float(hexCubicVals[6]), SkBits2Float(hexCubicVals[7])); |
| 47 | paint.getFillPath(path, &fillPath); |
| 48 | } |
| 49 | |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 50 | static void test_strokerect(skiatest::Reporter* reporter) { |
| 51 | const SkScalar width = SkIntToScalar(10); |
| 52 | SkPaint paint; |
| 53 | |
| 54 | paint.setStyle(SkPaint::kStroke_Style); |
| 55 | paint.setStrokeWidth(width); |
| 56 | |
| 57 | SkRect r = { 0, 0, SkIntToScalar(200), SkIntToScalar(100) }; |
| 58 | |
| 59 | SkRect outer(r); |
| 60 | outer.outset(width/2, width/2); |
| 61 | |
| 62 | static const SkPaint::Join joins[] = { |
| 63 | SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join |
| 64 | }; |
| 65 | |
| 66 | for (size_t i = 0; i < SK_ARRAY_COUNT(joins); ++i) { |
| 67 | paint.setStrokeJoin(joins[i]); |
| 68 | |
| 69 | SkPath path, fillPath; |
| 70 | path.addRect(r); |
| 71 | paint.getFillPath(path, &fillPath); |
| 72 | |
| 73 | REPORTER_ASSERT(reporter, equal(outer, fillPath.getBounds())); |
skia.committer@gmail.com | b0a327e | 2012-11-21 02:02:25 +0000 | [diff] [blame] | 74 | |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 75 | bool isMiter = SkPaint::kMiter_Join == joins[i]; |
| 76 | SkRect nested[2]; |
| 77 | REPORTER_ASSERT(reporter, fillPath.isNestedRects(nested) == isMiter); |
| 78 | if (isMiter) { |
| 79 | SkRect inner(r); |
| 80 | inner.inset(width/2, width/2); |
| 81 | REPORTER_ASSERT(reporter, equal(nested[0], outer)); |
| 82 | REPORTER_ASSERT(reporter, equal(nested[1], inner)); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 87 | DEF_TEST(Stroke, reporter) { |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame^] | 88 | test_strokecubic(reporter); |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 89 | test_strokerect(reporter); |
| 90 | } |