Chromium Code Reviews| Index: ui/gfx/geometry/rect_conversions.cc |
| diff --git a/ui/gfx/geometry/rect_conversions.cc b/ui/gfx/geometry/rect_conversions.cc |
| index b4ef3d44ec9c4e434b93ae64531cde2f6197014e..5bba79ea4f139e6507d0c44f7e197c210a0beb4f 100644 |
| --- a/ui/gfx/geometry/rect_conversions.cc |
| +++ b/ui/gfx/geometry/rect_conversions.cc |
| @@ -15,32 +15,60 @@ namespace gfx { |
| Rect ToEnclosingRect(const RectF& rect) { |
| int min_x = ToFlooredInt(rect.x()); |
| int min_y = ToFlooredInt(rect.y()); |
| - float max_x = rect.right(); |
| - float max_y = rect.bottom(); |
| - int width = |
| - rect.width() == 0 |
| - ? 0 |
| - : std::max( |
| - ToCeiledInt(static_cast<double>(ToCeiledInt(max_x)) - min_x), |
| - 0); |
| - int height = |
| - rect.height() == 0 |
| - ? 0 |
| - : std::max( |
| - ToCeiledInt(static_cast<double>(ToCeiledInt(max_y)) - min_y), |
| - 0); |
| + int max_x = ToCeiledInt(rect.right()); |
| + int max_y = ToCeiledInt(rect.bottom()); |
| + int width = base::SaturatedSubtraction(max_x, min_x); |
| + int height = base::SaturatedSubtraction(max_y, min_y); |
|
Peter Mayo
2017/03/14 18:35:59
move into set from range in header
|
| + |
| + // clamping may move one side, choose to move the bigger by recomputing it. |
| + // The computation of right is implicit, so this happens vacuosly. |
| + // If Right is small, move left to width away. |
| + // If Left is big(negative), move them both to keep the center. |
| + if (width >= std::numeric_limits<int>::max() / 2) { |
| + if (base::SaturatedAbsolute(max_x) < width / 2) |
| + min_x = max_x - width; |
| + else if (base::SaturatedAbsolute(min_x) > width / 2) |
| + min_x = min_x + width / 4; |
| + } |
| + |
| + // Same computation, other axis. |
| + if (height >= std::numeric_limits<int>::max() / 2) { |
| + if (base::SaturatedAbsolute(max_y) < height / 2) |
| + min_y = max_y - height; |
| + else if (base::SaturatedAbsolute(min_y) > height / 2) |
| + min_y = min_y + height / 4; |
| + } |
| + |
| return Rect(min_x, min_y, width, height); |
| } |
| Rect ToEnclosedRect(const RectF& rect) { |
| int min_x = ToCeiledInt(rect.x()); |
| int min_y = ToCeiledInt(rect.y()); |
| - float max_x = rect.right(); |
| - float max_y = rect.bottom(); |
| - int width = std::max( |
| - ToFlooredInt(static_cast<float>(ToFlooredInt(max_x)) - min_x), 0); |
| - int height = std::max( |
| - ToFlooredInt(static_cast<float>(ToFlooredInt(max_y)) - min_y), 0); |
| + int max_x = ToFlooredInt(rect.right()); |
| + int max_y = ToFlooredInt(rect.bottom()); |
| + int width = base::SaturatedSubtraction(max_x, min_x); |
| + int height = base::SaturatedSubtraction(max_y, min_y); |
| + |
| + // clamping may move one side, choose to move the bigger by recomputing it. |
| + // The computation of right is implicit, so this happens vacuosly. |
| + // If Right is small, move left to width away. |
| + // If Left is big(negative), move them both to keep the center. |
| + if (width >= std::numeric_limits<int>::max() / 2) { |
| + if (base::SaturatedAbsolute(max_x) < width / 2) |
| + min_x = max_x - width; |
| + else if (base::SaturatedAbsolute(min_x) > width / 2) |
| + min_x = min_x + width / 4; |
| + } |
| + |
| + // Same computation, other axis. |
| + if (height >= std::numeric_limits<int>::max() / 2) { |
| + if (base::SaturatedAbsolute(max_y) < height / 2) |
| + min_y = max_y - height; |
| + else if (base::SaturatedAbsolute(min_y) > height / 2) |
| + min_y = min_y + height / 4; |
| + } |
| + |
| return Rect(min_x, min_y, width, height); |
| } |