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 | |
| 21 | static void test_strokerect(skiatest::Reporter* reporter) { |
| 22 | const SkScalar width = SkIntToScalar(10); |
| 23 | SkPaint paint; |
| 24 | |
| 25 | paint.setStyle(SkPaint::kStroke_Style); |
| 26 | paint.setStrokeWidth(width); |
| 27 | |
| 28 | SkRect r = { 0, 0, SkIntToScalar(200), SkIntToScalar(100) }; |
| 29 | |
| 30 | SkRect outer(r); |
| 31 | outer.outset(width/2, width/2); |
| 32 | |
| 33 | static const SkPaint::Join joins[] = { |
| 34 | SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join |
| 35 | }; |
| 36 | |
| 37 | for (size_t i = 0; i < SK_ARRAY_COUNT(joins); ++i) { |
| 38 | paint.setStrokeJoin(joins[i]); |
| 39 | |
| 40 | SkPath path, fillPath; |
| 41 | path.addRect(r); |
| 42 | paint.getFillPath(path, &fillPath); |
| 43 | |
| 44 | REPORTER_ASSERT(reporter, equal(outer, fillPath.getBounds())); |
skia.committer@gmail.com | b0a327e | 2012-11-21 02:02:25 +0000 | [diff] [blame] | 45 | |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 46 | bool isMiter = SkPaint::kMiter_Join == joins[i]; |
| 47 | SkRect nested[2]; |
| 48 | REPORTER_ASSERT(reporter, fillPath.isNestedRects(nested) == isMiter); |
| 49 | if (isMiter) { |
| 50 | SkRect inner(r); |
| 51 | inner.inset(width/2, width/2); |
| 52 | REPORTER_ASSERT(reporter, equal(nested[0], outer)); |
| 53 | REPORTER_ASSERT(reporter, equal(nested[1], inner)); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 58 | DEF_TEST(Stroke, reporter) { |
reed@google.com | 603dbed | 2012-11-20 19:00:28 +0000 | [diff] [blame] | 59 | test_strokerect(reporter); |
| 60 | } |