Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Defines a simple integer rectangle class. The containment semantics | 5 // Defines a simple integer rectangle class. The containment semantics |
| 6 // are array-like; that is, the coordinate (x, y) is considered to be | 6 // are array-like; that is, the coordinate (x, y) is considered to be |
| 7 // contained by the rectangle, but the coordinate (x + width, y) is not. | 7 // contained by the rectangle, but the coordinate (x + width, y) is not. |
| 8 // The class will happily let you create malformed rectangles (that is, | 8 // The class will happily let you create malformed rectangles (that is, |
| 9 // rectangles with negative width and/or height), but there will be assertions | 9 // rectangles with negative width and/or height), but there will be assertions |
| 10 // in the operations (such as Contains()) to complain in this case. | 10 // in the operations (such as Contains()) to complain in this case. |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 277 x : static_cast<int>(std::ceil(rect.right() * x_scale)); | 277 x : static_cast<int>(std::ceil(rect.right() * x_scale)); |
| 278 int b = rect.height() == 0 ? | 278 int b = rect.height() == 0 ? |
| 279 y : static_cast<int>(std::ceil(rect.bottom() * y_scale)); | 279 y : static_cast<int>(std::ceil(rect.bottom() * y_scale)); |
| 280 return Rect(x, y, r - x, b - y); | 280 return Rect(x, y, r - x, b - y); |
| 281 } | 281 } |
| 282 | 282 |
| 283 inline Rect ScaleToEnclosingRect(const Rect& rect, float scale) { | 283 inline Rect ScaleToEnclosingRect(const Rect& rect, float scale) { |
| 284 return ScaleToEnclosingRect(rect, scale, scale); | 284 return ScaleToEnclosingRect(rect, scale, scale); |
| 285 } | 285 } |
| 286 | 286 |
| 287 // ScaleToEnclosingRect but clamping instead of asserting if the resulting rect | |
| 288 // would overflow. | |
| 289 inline Rect ScaleToEnclosingRectSafe(const Rect& rect, | |
| 290 float x_scale, | |
| 291 float y_scale) { | |
| 292 if (x_scale == 1.f && y_scale == 1.f) | |
| 293 return rect; | |
| 294 int x = base::saturated_cast<int>(std::floor(rect.x() * x_scale)); | |
| 295 int y = base::saturated_cast<int>(std::floor(rect.y() * y_scale)); | |
| 296 int w = base::saturated_cast<int>(std::ceil(rect.width() * x_scale)); | |
| 297 int h = base::saturated_cast<int>(std::ceil(rect.height() * y_scale)); | |
| 298 return Rect(x, y, w, h); | |
| 299 } | |
| 300 | |
| 301 inline Rect ScaleToEnclosingRectSafe(const Rect& rect, float scale) { | |
| 302 return ScaleToEnclosingRectSafe(rect, scale, scale); | |
| 303 } | |
| 304 | |
| 287 inline Rect ScaleToEnclosedRect(const Rect& rect, | 305 inline Rect ScaleToEnclosedRect(const Rect& rect, |
|
danakj
2016/10/04 21:32:51
Leave a comment here to use ScaleToEnclosingRectSa
| |
| 288 float x_scale, | 306 float x_scale, |
| 289 float y_scale) { | 307 float y_scale) { |
| 290 if (x_scale == 1.f && y_scale == 1.f) | 308 if (x_scale == 1.f && y_scale == 1.f) |
| 291 return rect; | 309 return rect; |
| 292 DCHECK(base::IsValueInRangeForNumericType<int>( | 310 DCHECK(base::IsValueInRangeForNumericType<int>( |
| 293 std::ceil(rect.x() * x_scale))); | 311 std::ceil(rect.x() * x_scale))); |
| 294 DCHECK(base::IsValueInRangeForNumericType<int>( | 312 DCHECK(base::IsValueInRangeForNumericType<int>( |
| 295 std::ceil(rect.y() * y_scale))); | 313 std::ceil(rect.y() * y_scale))); |
| 296 DCHECK(base::IsValueInRangeForNumericType<int>( | 314 DCHECK(base::IsValueInRangeForNumericType<int>( |
| 297 std::floor(rect.right() * x_scale))); | 315 std::floor(rect.right() * x_scale))); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 311 } | 329 } |
| 312 | 330 |
| 313 // This is declared here for use in gtest-based unit tests but is defined in | 331 // This is declared here for use in gtest-based unit tests but is defined in |
| 314 // the //ui/gfx:test_support target. Depend on that to use this in your unit | 332 // the //ui/gfx:test_support target. Depend on that to use this in your unit |
| 315 // test. This should not be used in production code - call ToString() instead. | 333 // test. This should not be used in production code - call ToString() instead. |
| 316 void PrintTo(const Rect& rect, ::std::ostream* os); | 334 void PrintTo(const Rect& rect, ::std::ostream* os); |
| 317 | 335 |
| 318 } // namespace gfx | 336 } // namespace gfx |
| 319 | 337 |
| 320 #endif // UI_GFX_GEOMETRY_RECT_H_ | 338 #endif // UI_GFX_GEOMETRY_RECT_H_ |
| OLD | NEW |