From 967af6fabb527e58d0b166b2db1f9c44d9450402 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Fri, 1 Dec 2017 20:05:23 +0000 Subject: Add CFX_FloatRect::Scale(). Unlike ScaleFromCenterPoint(), Scale() just scales the LBRT values. Also add unit tests for CFX_FloatRect::Normalize(). Change-Id: Iebf48fa9adcf47adff3255b157d3e3056f3687fc Reviewed-on: https://pdfium-review.googlesource.com/20212 Reviewed-by: Ryan Harrison Commit-Queue: Ryan Harrison --- core/fxcrt/fx_coordinates.cpp | 7 ++++ core/fxcrt/fx_coordinates.h | 1 + core/fxcrt/fx_coordinates_unittest.cpp | 75 ++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) diff --git a/core/fxcrt/fx_coordinates.cpp b/core/fxcrt/fx_coordinates.cpp index 136ae72527..fe62d01ebe 100644 --- a/core/fxcrt/fx_coordinates.cpp +++ b/core/fxcrt/fx_coordinates.cpp @@ -148,6 +148,13 @@ void CFX_FloatRect::UpdateRect(const CFX_PointF& point) { top = std::max(top, point.y); } +void CFX_FloatRect::Scale(float fScale) { + left *= fScale; + bottom *= fScale; + right *= fScale; + top *= fScale; +} + void CFX_FloatRect::ScaleFromCenterPoint(float fScale) { float fHalfWidth = (right - left) / 2.0f; float fHalfHeight = (top - bottom) / 2.0f; diff --git a/core/fxcrt/fx_coordinates.h b/core/fxcrt/fx_coordinates.h index 0d3d123a59..5da86102f0 100644 --- a/core/fxcrt/fx_coordinates.h +++ b/core/fxcrt/fx_coordinates.h @@ -329,6 +329,7 @@ class CFX_FloatRect { bottom += f; } + void Scale(float fScale); void ScaleFromCenterPoint(float fScale); static CFX_FloatRect GetBBox(const CFX_PointF* pPoints, int nPoints); diff --git a/core/fxcrt/fx_coordinates_unittest.cpp b/core/fxcrt/fx_coordinates_unittest.cpp index 23d99e46d1..5be3aa83f9 100644 --- a/core/fxcrt/fx_coordinates_unittest.cpp +++ b/core/fxcrt/fx_coordinates_unittest.cpp @@ -64,6 +64,81 @@ TEST(CFX_FloatRect, GetBBox) { EXPECT_FLOAT_EQ(6.3f, rect.top); } +TEST(CFX_FloatRect, Normalize) { + CFX_FloatRect rect; + rect.Normalize(); + EXPECT_FLOAT_EQ(0.0f, rect.left); + EXPECT_FLOAT_EQ(0.0f, rect.bottom); + EXPECT_FLOAT_EQ(0.0f, rect.right); + EXPECT_FLOAT_EQ(0.0f, rect.top); + + rect = CFX_FloatRect(-1.0f, -3.0f, 4.5f, 3.2f); + rect.Normalize(); + EXPECT_FLOAT_EQ(-1.0f, rect.left); + EXPECT_FLOAT_EQ(-3.0f, rect.bottom); + EXPECT_FLOAT_EQ(4.5f, rect.right); + EXPECT_FLOAT_EQ(3.2f, rect.top); + rect.Scale(-1.0f); + rect.Normalize(); + EXPECT_FLOAT_EQ(-4.5f, rect.left); + EXPECT_FLOAT_EQ(-3.2f, rect.bottom); + EXPECT_FLOAT_EQ(1.0f, rect.right); + EXPECT_FLOAT_EQ(3.0f, rect.top); +} + +TEST(CFX_FloatRect, Scale) { + CFX_FloatRect rect(-1.0f, -3.0f, 4.5f, 3.2f); + rect.Scale(1.0f); + EXPECT_FLOAT_EQ(-1.0f, rect.left); + EXPECT_FLOAT_EQ(-3.0f, rect.bottom); + EXPECT_FLOAT_EQ(4.5f, rect.right); + EXPECT_FLOAT_EQ(3.2f, rect.top); + rect.Scale(0.5f); + EXPECT_FLOAT_EQ(-0.5, rect.left); + EXPECT_FLOAT_EQ(-1.5, rect.bottom); + EXPECT_FLOAT_EQ(2.25f, rect.right); + EXPECT_FLOAT_EQ(1.6f, rect.top); + rect.Scale(2.0f); + EXPECT_FLOAT_EQ(-1.0f, rect.left); + EXPECT_FLOAT_EQ(-3.0f, rect.bottom); + EXPECT_FLOAT_EQ(4.5f, rect.right); + EXPECT_FLOAT_EQ(3.2f, rect.top); + rect.Scale(-1.0f); + EXPECT_FLOAT_EQ(1.0f, rect.left); + EXPECT_FLOAT_EQ(3.0f, rect.bottom); + EXPECT_FLOAT_EQ(-4.5f, rect.right); + EXPECT_FLOAT_EQ(-3.2f, rect.top); + rect.Scale(0.0f); + EXPECT_FLOAT_EQ(0.0f, rect.left); + EXPECT_FLOAT_EQ(0.0f, rect.bottom); + EXPECT_FLOAT_EQ(0.0f, rect.right); + EXPECT_FLOAT_EQ(0.0f, rect.top); +} + +TEST(CFX_FloatRect, ScaleEmpty) { + CFX_FloatRect rect; + rect.Scale(1.0f); + EXPECT_FLOAT_EQ(0.0f, rect.left); + EXPECT_FLOAT_EQ(0.0f, rect.bottom); + EXPECT_FLOAT_EQ(0.0f, rect.right); + EXPECT_FLOAT_EQ(0.0f, rect.top); + rect.Scale(0.5f); + EXPECT_FLOAT_EQ(0.0f, rect.left); + EXPECT_FLOAT_EQ(0.0f, rect.bottom); + EXPECT_FLOAT_EQ(0.0f, rect.right); + EXPECT_FLOAT_EQ(0.0f, rect.top); + rect.Scale(2.0f); + EXPECT_FLOAT_EQ(0.0f, rect.left); + EXPECT_FLOAT_EQ(0.0f, rect.bottom); + EXPECT_FLOAT_EQ(0.0f, rect.right); + EXPECT_FLOAT_EQ(0.0f, rect.top); + rect.Scale(0.0f); + EXPECT_FLOAT_EQ(0.0f, rect.left); + EXPECT_FLOAT_EQ(0.0f, rect.bottom); + EXPECT_FLOAT_EQ(0.0f, rect.right); + EXPECT_FLOAT_EQ(0.0f, rect.top); +} + TEST(CFX_FloatRect, ScaleFromCenterPoint) { CFX_FloatRect rect(-1.0f, -3.0f, 4.5f, 3.2f); rect.ScaleFromCenterPoint(1.0f); -- cgit v1.2.3