Index: ui/gfx/geometry/rect.h |
diff --git a/ui/gfx/geometry/rect.h b/ui/gfx/geometry/rect.h |
index 17266fe03fb7c3f385df26746ff660e466087820..796199860bce087d04470dc5e30c60085d324b2c 100644 |
--- a/ui/gfx/geometry/rect.h |
+++ b/ui/gfx/geometry/rect.h |
@@ -284,6 +284,24 @@ inline Rect ScaleToEnclosingRect(const Rect& rect, float scale) { |
return ScaleToEnclosingRect(rect, scale, scale); |
} |
+// ScaleToEnclosingRect but clamping instead of asserting if the resulting rect |
+// would overflow. |
+inline Rect ScaleToEnclosingRectSafe(const Rect& rect, |
+ float x_scale, |
+ float y_scale) { |
+ if (x_scale == 1.f && y_scale == 1.f) |
+ return rect; |
+ int x = base::saturated_cast<int>(std::floor(rect.x() * x_scale)); |
+ int y = base::saturated_cast<int>(std::floor(rect.y() * y_scale)); |
+ int w = base::saturated_cast<int>(std::ceil(rect.width() * x_scale)); |
+ int h = base::saturated_cast<int>(std::ceil(rect.height() * y_scale)); |
+ return Rect(x, y, w, h); |
+} |
+ |
+inline Rect ScaleToEnclosingRectSafe(const Rect& rect, float scale) { |
+ return ScaleToEnclosingRectSafe(rect, scale, scale); |
+} |
+ |
inline Rect ScaleToEnclosedRect(const Rect& rect, |
danakj
2016/10/04 21:32:51
Leave a comment here to use ScaleToEnclosingRectSa
|
float x_scale, |
float y_scale) { |