From 752e9bf892abdf1ee588ba87c857d0783a017b27 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Fri, 1 Dec 2017 20:09:21 +0000 Subject: Add CFX_FloatRect::ToRoundedFxRect(). Unlike ToFxRect(), which floors the LBRT values. Also fix some nits. Change-Id: I0680eebb09031807a14402ca30b13558ea5b56d9 Reviewed-on: https://pdfium-review.googlesource.com/20213 Reviewed-by: Ryan Harrison Commit-Queue: Ryan Harrison --- core/fxcrt/fx_coordinates.cpp | 60 +++++++++++++++++++++++++++---------------- core/fxcrt/fx_coordinates.h | 17 ++++-------- 2 files changed, 43 insertions(+), 34 deletions(-) diff --git a/core/fxcrt/fx_coordinates.cpp b/core/fxcrt/fx_coordinates.cpp index fe62d01ebe..561f25d4a6 100644 --- a/core/fxcrt/fx_coordinates.cpp +++ b/core/fxcrt/fx_coordinates.cpp @@ -47,10 +47,28 @@ void FX_RECT::Intersect(const FX_RECT& src) { } CFX_FloatRect::CFX_FloatRect(const FX_RECT& rect) { - left = static_cast(rect.left); - top = static_cast(rect.bottom); - right = static_cast(rect.right); - bottom = static_cast(rect.top); + left = rect.left; + top = rect.bottom; + right = rect.right; + bottom = rect.top; +} + +// static +CFX_FloatRect CFX_FloatRect::GetBBox(const CFX_PointF* pPoints, int nPoints) { + if (nPoints == 0) + return CFX_FloatRect(); + + float min_x = pPoints->x; + float max_x = pPoints->x; + float min_y = pPoints->y; + float max_y = pPoints->y; + for (int i = 1; i < nPoints; i++) { + min_x = std::min(min_x, pPoints[i].x); + max_x = std::max(max_x, pPoints[i].x); + min_y = std::min(min_y, pPoints[i].y); + max_y = std::max(max_y, pPoints[i].y); + } + return CFX_FloatRect(min_x, min_y, max_x, max_y); } void CFX_FloatRect::Normalize() { @@ -60,6 +78,13 @@ void CFX_FloatRect::Normalize() { std::swap(top, bottom); } +void CFX_FloatRect::Reset() { + left = 0.0f; + right = 0.0f; + bottom = 0.0f; + top = 0.0f; +} + void CFX_FloatRect::Intersect(const CFX_FloatRect& other_rect) { Normalize(); CFX_FloatRect other = other_rect; @@ -68,9 +93,8 @@ void CFX_FloatRect::Intersect(const CFX_FloatRect& other_rect) { bottom = std::max(bottom, other.bottom); right = std::min(right, other.right); top = std::min(top, other.top); - if (left > right || bottom > top) { - left = bottom = right = top = 0; - } + if (left > right || bottom > top) + Reset(); } void CFX_FloatRect::Union(const CFX_FloatRect& other_rect) { @@ -168,22 +192,14 @@ void CFX_FloatRect::ScaleFromCenterPoint(float fScale) { top = center_y + fHalfHeight * fScale; } -// static -CFX_FloatRect CFX_FloatRect::GetBBox(const CFX_PointF* pPoints, int nPoints) { - if (nPoints == 0) - return CFX_FloatRect(); +FX_RECT CFX_FloatRect::ToFxRect() const { + return FX_RECT(static_cast(left), static_cast(top), + static_cast(right), static_cast(bottom)); +} - float min_x = pPoints->x; - float max_x = pPoints->x; - float min_y = pPoints->y; - float max_y = pPoints->y; - for (int i = 1; i < nPoints; i++) { - min_x = std::min(min_x, pPoints[i].x); - max_x = std::max(max_x, pPoints[i].x); - min_y = std::min(min_y, pPoints[i].y); - max_y = std::max(max_y, pPoints[i].y); - } - return CFX_FloatRect(min_x, min_y, max_x, max_y); +FX_RECT CFX_FloatRect::ToRoundedFxRect() const { + return FX_RECT(FXSYS_round(left), FXSYS_round(top), FXSYS_round(right), + FXSYS_round(bottom)); } CFX_Matrix CFX_Matrix::GetInverse() const { diff --git a/core/fxcrt/fx_coordinates.h b/core/fxcrt/fx_coordinates.h index 5da86102f0..1767e33f0c 100644 --- a/core/fxcrt/fx_coordinates.h +++ b/core/fxcrt/fx_coordinates.h @@ -233,14 +233,11 @@ class CFX_FloatRect { explicit CFX_FloatRect(const FX_RECT& rect); + static CFX_FloatRect GetBBox(const CFX_PointF* pPoints, int nPoints); + void Normalize(); - void Reset() { - left = 0.0f; - right = 0.0f; - bottom = 0.0f; - top = 0.0f; - } + void Reset(); bool IsEmpty() const { return left >= right || bottom >= top; } @@ -332,12 +329,8 @@ class CFX_FloatRect { void Scale(float fScale); void ScaleFromCenterPoint(float fScale); - static CFX_FloatRect GetBBox(const CFX_PointF* pPoints, int nPoints); - - FX_RECT ToFxRect() const { - return FX_RECT(static_cast(left), static_cast(top), - static_cast(right), static_cast(bottom)); - } + FX_RECT ToFxRect() const; + FX_RECT ToRoundedFxRect() const; float left; float bottom; -- cgit v1.2.3