Fix radii scaling bug in SkRRect::setNinePatch

BUG=skia:3466

Review URL: https://codereview.chromium.org/951323004
This commit is contained in:
robertphillips 2015-02-24 11:18:48 -08:00 committed by Commit bot
parent 1a2979af08
commit fe1b180bee
2 changed files with 36 additions and 18 deletions

View File

@ -69,7 +69,7 @@ void SkRRect::setNinePatch(const SkRect& rect, SkScalar leftRad, SkScalar topRad
scale = SkScalarDiv(rect.width(), leftRad + rightRad);
}
if (topRad + bottomRad > rect.height()) {
scale = SkMinScalar(scale, SkScalarDiv(rect.width(), leftRad + rightRad));
scale = SkMinScalar(scale, SkScalarDiv(rect.height(), topRad + bottomRad));
}
if (scale < SK_Scalar1) {

View File

@ -54,6 +54,32 @@ static void test_inset(skiatest::Reporter* reporter) {
REPORTER_ASSERT(reporter, rr2.isRect());
}
static void test_9patch_rrect(skiatest::Reporter* reporter,
const SkRect& rect,
SkScalar l, SkScalar t, SkScalar r, SkScalar b,
bool checkRadii) {
SkRRect rr;
rr.setNinePatch(rect, l, t, r, b);
REPORTER_ASSERT(reporter, SkRRect::kNinePatch_Type == rr.type());
REPORTER_ASSERT(reporter, rr.rect() == rect);
if (checkRadii) {
// This test doesn't hold if the radii will be rescaled by SkRRect
SkRect ninePatchRadii = { l, t, r, b };
SkPoint rquad[4];
ninePatchRadii.toQuad(rquad);
for (int i = 0; i < 4; ++i) {
REPORTER_ASSERT(reporter, rquad[i] == rr.radii((SkRRect::Corner) i));
}
}
SkRRect rr2; // construct the same RR using the most general set function
SkVector radii[4] = { { l, t }, { r, t }, { r, b }, { l, b } };
rr2.setRectRadii(rect, radii);
REPORTER_ASSERT(reporter, rr2 == rr && rr2.getType() == rr.getType());
}
// Test out the basic API entry points
static void test_round_rect_basic(skiatest::Reporter* reporter) {
// Test out initialization methods
@ -130,24 +156,17 @@ static void test_round_rect_basic(skiatest::Reporter* reporter) {
REPORTER_ASSERT(reporter, rr3_3 == rr3 && rr3_3.getType() == rr3.getType());
//----
SkRect ninePatchRadii = { 10, 9, 8, 7 };
test_9patch_rrect(reporter, rect, 10, 9, 8, 7, true);
SkRRect rr4;
rr4.setNinePatch(rect, ninePatchRadii.fLeft, ninePatchRadii.fTop, ninePatchRadii.fRight,
ninePatchRadii.fBottom);
{
// Test out the rrect from skia:3466
SkRect rect2 = SkRect::MakeLTRB(0.358211994f, 0.755430222f, 0.872866154f, 0.806214333f);
REPORTER_ASSERT(reporter, SkRRect::kNinePatch_Type == rr4.type());
REPORTER_ASSERT(reporter, rr4.rect() == rect);
SkPoint rquad[4];
ninePatchRadii.toQuad(rquad);
for (int i = 0; i < 4; ++i) {
REPORTER_ASSERT(reporter, rquad[i] == rr4.radii((SkRRect::Corner) i));
test_9patch_rrect(reporter,
rect2,
0.926942348f, 0.642850280f, 0.529063463f, 0.587844372f,
false);
}
SkRRect rr4_2; // construct the same RR using the most general set function
SkVector rr4_2_radii[4] = { { 10, 9 }, { 8, 9 }, {8, 7 }, { 10, 7 } };
rr4_2.setRectRadii(rect, rr4_2_radii);
REPORTER_ASSERT(reporter, rr4_2 == rr4 && rr4_2.getType() == rr4.getType());
//----
SkPoint radii2[4] = { { 0, 0 }, { 0, 0 }, { 50, 50 }, { 20, 50 } };
@ -164,8 +183,7 @@ static void test_round_rect_basic(skiatest::Reporter* reporter) {
// Test out == & !=
REPORTER_ASSERT(reporter, empty != rr3);
REPORTER_ASSERT(reporter, rr3 != rr4);
REPORTER_ASSERT(reporter, rr4 != rr5);
REPORTER_ASSERT(reporter, rr3 != rr5);
}
// Test out the cases when the RR degenerates to a rect