diff options
author | 2012-12-23 23:11:21 +0000 | |
---|---|---|
committer | 2012-12-23 23:11:21 +0000 | |
commit | bcbef579d02e255b9a29b5db2d6804f4bfc76d1c (patch) | |
tree | ffad137edc5dda0c6a6a18d59bd387530a8b8f68 | |
parent | 15ed90f6ee939055ce6e2e1ace0292e7c3d9cbfc (diff) |
add SkRRect::inset(), which mimics stroking
git-svn-id: http://skia.googlecode.com/svn/trunk@6935 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r-- | include/core/SkRRect.h | 20 | ||||
-rw-r--r-- | src/core/SkRRect.cpp | 14 | ||||
-rw-r--r-- | tests/RoundRectTest.cpp | 22 |
3 files changed, 50 insertions, 6 deletions
diff --git a/include/core/SkRRect.h b/include/core/SkRRect.h index b09d27ac20..d6187f415a 100644 --- a/include/core/SkRRect.h +++ b/include/core/SkRRect.h @@ -220,18 +220,34 @@ public: */ bool contains(SkScalar x, SkScalar y) const; -#if 0 + /** + * Call inset on the bounds, and adjust the radii to reflect what happens + * in stroking: If the corner is sharp (no curvature), leave it alone, + * otherwise we grow/shrink the radii by the amount of the inset. If a + * given radius becomes negative, it is pinned to 0. + * + * It is valid for dst == this. + */ void inset(SkScalar dx, SkScalar dy, SkRRect* dst) const; + void inset(SkScalar dx, SkScalar dy) { this->inset(dx, dy, this); } + + /** + * Call outset on the bounds, and adjust the radii to reflect what happens + * in stroking: If the corner is sharp (no curvature), leave it alone, + * otherwise we grow/shrink the radii by the amount of the inset. If a + * given radius becomes negative, it is pinned to 0. + * + * It is valid for dst == this. + */ void outset(SkScalar dx, SkScalar dy, SkRRect* dst) const { this->inset(-dx, -dy, dst); } void outset(SkScalar dx, SkScalar dy) { this->inset(-dx, -dy, this); } -#endif SkDEBUGCODE(void validate() const;) diff --git a/src/core/SkRRect.cpp b/src/core/SkRRect.cpp index 0d137ec953..f27400c7c0 100644 --- a/src/core/SkRRect.cpp +++ b/src/core/SkRRect.cpp @@ -228,10 +228,10 @@ void SkRRect::computeType() const { } /////////////////////////////////////////////////////////////////////////////// -#if 0 -void SkRRect::inset(SkScalar dx, SkScalar dy, SkRRect* dst) const { +void SkRRect::inset(SkScalar dx, SkScalar dy, SkRRect* dst) const { SkRect r = fRect; + r.inset(dx, dy); if (r.isEmpty()) { dst->setEmpty(); @@ -239,12 +239,18 @@ void SkRRect::inset(SkScalar dx, SkScalar dy, SkRRect* dst) const { } SkVector radii[4]; + memcpy(radii, fRadii, sizeof(radii)); for (int i = 0; i < 4; ++i) { - radii[i].set(fRadii[i].fX - dx, fRadii[i].fY - dy); + if (radii[i].fX) { + radii[i].fX -= dx; + } + if (radii[i].fY) { + radii[i].fY -= dy; + } } dst->setRectRadii(r, radii); } -#endif + /////////////////////////////////////////////////////////////////////////////// uint32_t SkRRect::writeToMemory(void* buffer) const { diff --git a/tests/RoundRectTest.cpp b/tests/RoundRectTest.cpp index e88ed8ad8b..7a2c66e4cf 100644 --- a/tests/RoundRectTest.cpp +++ b/tests/RoundRectTest.cpp @@ -11,6 +11,27 @@ static const SkScalar kWidth = 100.0f; static const SkScalar kHeight = 100.0f; +static void test_inset(skiatest::Reporter* reporter) { + SkRRect rr, rr2; + SkRect r = { 0, 0, 100, 100 }; + + rr.setRect(r); + rr.inset(-20, -20, &rr2); + REPORTER_ASSERT(reporter, rr2.isRect()); + + rr.inset(20, 20, &rr2); + REPORTER_ASSERT(reporter, rr2.isRect()); + + rr.inset(r.width()/2, r.height()/2, &rr2); + REPORTER_ASSERT(reporter, rr2.isEmpty()); + + rr.setRectXY(r, 20, 20); + rr.inset(19, 19, &rr2); + REPORTER_ASSERT(reporter, rr2.isSimple()); + rr.inset(20, 20, &rr2); + REPORTER_ASSERT(reporter, rr2.isRect()); +} + // Test out the basic API entry points static void test_round_rect_basic(skiatest::Reporter* reporter) { // Test out initialization methods @@ -302,6 +323,7 @@ static void TestRoundRect(skiatest::Reporter* reporter) { test_round_rect_ovals(reporter); test_round_rect_general(reporter); test_round_rect_iffy_parameters(reporter); + test_inset(reporter); } #include "TestClassDef.h" |