Index: ui/gfx/geometry/rect.h |
diff --git a/ui/gfx/geometry/rect.h b/ui/gfx/geometry/rect.h |
index 4e914b8ca6555167b88c7e3446e2953fae622b35..9c261f56ff34dfa9a4446038fbb6a9cb0a2501ae 100644 |
--- a/ui/gfx/geometry/rect.h |
+++ b/ui/gfx/geometry/rect.h |
@@ -340,6 +340,41 @@ inline Rect ScaleToEnclosedRect(const Rect& rect, float scale) { |
return ScaleToEnclosedRect(rect, scale, scale); |
} |
+// Scales |rect| by scaling its four corner points. If the corner points lie on |
+// non-integral coordinate after scaling, their values are rounded to the |
+// nearest integer. |
+// This scheme for scaling is important to keep consistency during layout. |
+// All rectangles (or any point) within the same coordinate space will |
sky
2017/07/28 16:02:11
I'm not sure what this last sentence means. This s
malaykeshav
2017/07/31 18:54:29
The relative positions _isn't_ lossy, even though
sky
2017/08/01 15:10:23
At the end of the day you are rounding to ints, so
|
+// have the same relative positioning after scaling if this scheme is used. |
+inline Rect ScaleToRoundedRect(const Rect& rect, float x_scale, float y_scale) { |
+ if (x_scale == 1.f && y_scale == 1.f) |
+ return rect; |
+ |
+ DCHECK( |
+ base::IsValueInRangeForNumericType<int>(std::round(rect.x() * x_scale))); |
+ DCHECK( |
+ base::IsValueInRangeForNumericType<int>(std::round(rect.y() * y_scale))); |
+ DCHECK(base::IsValueInRangeForNumericType<int>( |
+ std::round(rect.right() * x_scale))); |
+ DCHECK(base::IsValueInRangeForNumericType<int>( |
+ std::round(rect.bottom() * y_scale))); |
+ |
+ int x = static_cast<int>(std::round(rect.x() * x_scale)); |
+ int y = static_cast<int>(std::round(rect.y() * y_scale)); |
+ int r = rect.width() == 0 |
+ ? x |
+ : static_cast<int>(std::round(rect.right() * x_scale)); |
+ int b = rect.height() == 0 |
+ ? y |
+ : static_cast<int>(std::round(rect.bottom() * y_scale)); |
+ |
+ return Rect(x, y, r - x, b - y); |
+} |
+ |
+inline Rect ScaleToRoundedRect(const Rect& rect, float scale) { |
+ return ScaleToRoundedRect(rect, scale, scale); |
+} |
+ |
// This is declared here for use in gtest-based unit tests but is defined in |
// the //ui/gfx:test_support target. Depend on that to use this in your unit |
// test. This should not be used in production code - call ToString() instead. |