From 12ec6760afd92b63d185854008a55762fe39f866 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Mon, 4 Dec 2017 21:24:15 +0000 Subject: Simplify some CFX_FloatRect methods. Also add a comment about the various CFX_FloatRect to FX_RECT conversion methods. Change-Id: Ia9984797dc513cdc487fe9972b32c216c9f99ec1 Reviewed-on: https://pdfium-review.googlesource.com/20217 Commit-Queue: Lei Zhang Reviewed-by: Henrique Nakashima --- core/fxcrt/fx_coordinates.cpp | 29 +++++++++++++---------------- core/fxcrt/fx_coordinates.h | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 16 deletions(-) (limited to 'core') diff --git a/core/fxcrt/fx_coordinates.cpp b/core/fxcrt/fx_coordinates.cpp index 561f25d4a6..733425ef26 100644 --- a/core/fxcrt/fx_coordinates.cpp +++ b/core/fxcrt/fx_coordinates.cpp @@ -17,10 +17,10 @@ void MatchFloatRange(float f1, float f2, int* i1, int* i2) { int length = static_cast(ceil(f2 - f1)); int i1_1 = static_cast(floor(f1)); int i1_2 = static_cast(ceil(f1)); - float error1 = f1 - i1_1 + (float)fabs(f2 - i1_1 - length); - float error2 = i1_2 - f1 + (float)fabs(f2 - i1_2 - length); + float error1 = f1 - i1_1 + fabsf(f2 - i1_1 - length); + float error2 = i1_2 - f1 + fabsf(f2 - i1_2 - length); - *i1 = (error1 > error2) ? i1_2 : i1_1; + *i1 = error1 > error2 ? i1_2 : i1_1; *i2 = *i1 + length; } @@ -108,32 +108,29 @@ void CFX_FloatRect::Union(const CFX_FloatRect& other_rect) { } FX_RECT CFX_FloatRect::GetOuterRect() const { - CFX_FloatRect rect1 = *this; FX_RECT rect; - rect.left = static_cast(floor(rect1.left)); - rect.bottom = static_cast(ceil(rect1.top)); - rect.right = static_cast(ceil(rect1.right)); - rect.top = static_cast(floor(rect1.bottom)); + rect.left = static_cast(floor(left)); + rect.bottom = static_cast(ceil(top)); + rect.right = static_cast(ceil(right)); + rect.top = static_cast(floor(bottom)); rect.Normalize(); return rect; } FX_RECT CFX_FloatRect::GetInnerRect() const { - CFX_FloatRect rect1 = *this; FX_RECT rect; - rect.left = static_cast(ceil(rect1.left)); - rect.bottom = static_cast(floor(rect1.top)); - rect.right = static_cast(floor(rect1.right)); - rect.top = static_cast(ceil(rect1.bottom)); + rect.left = static_cast(ceil(left)); + rect.bottom = static_cast(floor(top)); + rect.right = static_cast(floor(right)); + rect.top = static_cast(ceil(bottom)); rect.Normalize(); return rect; } FX_RECT CFX_FloatRect::GetClosestRect() const { - CFX_FloatRect rect1 = *this; FX_RECT rect; - MatchFloatRange(rect1.left, rect1.right, &rect.left, &rect.right); - MatchFloatRange(rect1.bottom, rect1.top, &rect.top, &rect.bottom); + MatchFloatRange(left, right, &rect.left, &rect.right); + MatchFloatRange(bottom, top, &rect.top, &rect.bottom); rect.Normalize(); return rect; } diff --git a/core/fxcrt/fx_coordinates.h b/core/fxcrt/fx_coordinates.h index 1767e33f0c..55d28cd66f 100644 --- a/core/fxcrt/fx_coordinates.h +++ b/core/fxcrt/fx_coordinates.h @@ -247,9 +247,20 @@ class CFX_FloatRect { void Intersect(const CFX_FloatRect& other_rect); void Union(const CFX_FloatRect& other_rect); + // These may be better at rounding than ToFxRect() and friends. + // + // Returned rect has bounds rounded up/down such that it is contained in the + // original. FX_RECT GetInnerRect() const; + + // Returned rect has bounds rounded up/down such that the original is + // contained in it. FX_RECT GetOuterRect() const; + + // Returned rect has bounds rounded up/down such that the dimensions are + // rounded up and the sum of the error in the bounds is minimized. FX_RECT GetClosestRect() const; + CFX_FloatRect GetCenterSquare() const; void InitRect(const CFX_PointF& point) { @@ -329,7 +340,15 @@ class CFX_FloatRect { void Scale(float fScale); void ScaleFromCenterPoint(float fScale); + // GetInnerRect() and friends may be better at rounding than these methods. + // Unlike the methods above, these two blindly floor / round the LBRT values. + // Doing so may introduce rounding errors that are visible to users as + // off-by-one pixels/lines. + // + // Floors LBRT values. FX_RECT ToFxRect() const; + + // Rounds LBRT values. FX_RECT ToRoundedFxRect() const; float left; -- cgit v1.2.3