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 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 333 x : static_cast<int>(std::floor(rect.right() * x_scale)); | 333 x : static_cast<int>(std::floor(rect.right() * x_scale)); |
| 334 int b = rect.height() == 0 ? | 334 int b = rect.height() == 0 ? |
| 335 y : static_cast<int>(std::floor(rect.bottom() * y_scale)); | 335 y : static_cast<int>(std::floor(rect.bottom() * y_scale)); |
| 336 return Rect(x, y, r - x, b - y); | 336 return Rect(x, y, r - x, b - y); |
| 337 } | 337 } |
| 338 | 338 |
| 339 inline Rect ScaleToEnclosedRect(const Rect& rect, float scale) { | 339 inline Rect ScaleToEnclosedRect(const Rect& rect, float scale) { |
| 340 return ScaleToEnclosedRect(rect, scale, scale); | 340 return ScaleToEnclosedRect(rect, scale, scale); |
| 341 } | 341 } |
| 342 | 342 |
| 343 // Scales |rect| by scaling its four corner points. If the corner points lie on | |
| 344 // non-integral coordinate after scaling, their values are rounded to the | |
| 345 // nearest integer. | |
| 346 // This scheme for scaling is important to keep consistency during layout. | |
| 347 // 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
| |
| 348 // have the same relative positioning after scaling if this scheme is used. | |
| 349 inline Rect ScaleToRoundedRect(const Rect& rect, float x_scale, float y_scale) { | |
| 350 if (x_scale == 1.f && y_scale == 1.f) | |
| 351 return rect; | |
| 352 | |
| 353 DCHECK( | |
| 354 base::IsValueInRangeForNumericType<int>(std::round(rect.x() * x_scale))); | |
| 355 DCHECK( | |
| 356 base::IsValueInRangeForNumericType<int>(std::round(rect.y() * y_scale))); | |
| 357 DCHECK(base::IsValueInRangeForNumericType<int>( | |
| 358 std::round(rect.right() * x_scale))); | |
| 359 DCHECK(base::IsValueInRangeForNumericType<int>( | |
| 360 std::round(rect.bottom() * y_scale))); | |
| 361 | |
| 362 int x = static_cast<int>(std::round(rect.x() * x_scale)); | |
| 363 int y = static_cast<int>(std::round(rect.y() * y_scale)); | |
| 364 int r = rect.width() == 0 | |
| 365 ? x | |
| 366 : static_cast<int>(std::round(rect.right() * x_scale)); | |
| 367 int b = rect.height() == 0 | |
| 368 ? y | |
| 369 : static_cast<int>(std::round(rect.bottom() * y_scale)); | |
| 370 | |
| 371 return Rect(x, y, r - x, b - y); | |
| 372 } | |
| 373 | |
| 374 inline Rect ScaleToRoundedRect(const Rect& rect, float scale) { | |
| 375 return ScaleToRoundedRect(rect, scale, scale); | |
| 376 } | |
| 377 | |
| 343 // This is declared here for use in gtest-based unit tests but is defined in | 378 // This is declared here for use in gtest-based unit tests but is defined in |
| 344 // the //ui/gfx:test_support target. Depend on that to use this in your unit | 379 // the //ui/gfx:test_support target. Depend on that to use this in your unit |
| 345 // test. This should not be used in production code - call ToString() instead. | 380 // test. This should not be used in production code - call ToString() instead. |
| 346 void PrintTo(const Rect& rect, ::std::ostream* os); | 381 void PrintTo(const Rect& rect, ::std::ostream* os); |
| 347 | 382 |
| 348 } // namespace gfx | 383 } // namespace gfx |
| 349 | 384 |
| 350 #endif // UI_GFX_GEOMETRY_RECT_H_ | 385 #endif // UI_GFX_GEOMETRY_RECT_H_ |
| OLD | NEW |