Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Side by Side Diff: ui/gfx/geometry/rect.h

Issue 2384063007: Fix scrollbar overflow with ScaleToEnclosingRectSafe (Closed)
Patch Set: With comment Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « cc/layers/painted_scrollbar_layer.cc ('k') | ui/gfx/geometry/rect_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 GFX_EXPORT Rect SubtractRects(const Rect& a, const Rect& b); 247 GFX_EXPORT Rect SubtractRects(const Rect& a, const Rect& b);
248 248
249 // Constructs a rectangle with |p1| and |p2| as opposite corners. 249 // Constructs a rectangle with |p1| and |p2| as opposite corners.
250 // 250 //
251 // This could also be thought of as "the smallest rect that contains both 251 // This could also be thought of as "the smallest rect that contains both
252 // points", except that we consider points on the right/bottom edges of the 252 // points", except that we consider points on the right/bottom edges of the
253 // rect to be outside the rect. So technically one or both points will not be 253 // rect to be outside the rect. So technically one or both points will not be
254 // contained within the rect, because they will appear on one of these edges. 254 // contained within the rect, because they will appear on one of these edges.
255 GFX_EXPORT Rect BoundingRect(const Point& p1, const Point& p2); 255 GFX_EXPORT Rect BoundingRect(const Point& p1, const Point& p2);
256 256
257 // Scales the rect and returns the enclosing rect. Use this only the inputs are
258 // known to not overflow. Use ScaleToEnclosingRectSafe if the inputs are
259 // unknown and need to use saturated math.
257 inline Rect ScaleToEnclosingRect(const Rect& rect, 260 inline Rect ScaleToEnclosingRect(const Rect& rect,
258 float x_scale, 261 float x_scale,
259 float y_scale) { 262 float y_scale) {
260 if (x_scale == 1.f && y_scale == 1.f) 263 if (x_scale == 1.f && y_scale == 1.f)
261 return rect; 264 return rect;
262 // These next functions cast instead of using e.g. ToFlooredInt() because we 265 // These next functions cast instead of using e.g. ToFlooredInt() because we
263 // haven't checked to ensure that the clamping behavior of the helper 266 // haven't checked to ensure that the clamping behavior of the helper
264 // functions doesn't degrade performance, and callers shouldn't be passing 267 // functions doesn't degrade performance, and callers shouldn't be passing
265 // values that cause overflow anyway. 268 // values that cause overflow anyway.
266 DCHECK(base::IsValueInRangeForNumericType<int>( 269 DCHECK(base::IsValueInRangeForNumericType<int>(
(...skipping 10 matching lines...) Expand all
277 x : static_cast<int>(std::ceil(rect.right() * x_scale)); 280 x : static_cast<int>(std::ceil(rect.right() * x_scale));
278 int b = rect.height() == 0 ? 281 int b = rect.height() == 0 ?
279 y : static_cast<int>(std::ceil(rect.bottom() * y_scale)); 282 y : static_cast<int>(std::ceil(rect.bottom() * y_scale));
280 return Rect(x, y, r - x, b - y); 283 return Rect(x, y, r - x, b - y);
281 } 284 }
282 285
283 inline Rect ScaleToEnclosingRect(const Rect& rect, float scale) { 286 inline Rect ScaleToEnclosingRect(const Rect& rect, float scale) {
284 return ScaleToEnclosingRect(rect, scale, scale); 287 return ScaleToEnclosingRect(rect, scale, scale);
285 } 288 }
286 289
290 // ScaleToEnclosingRect but clamping instead of asserting if the resulting rect
291 // would overflow.
292 inline Rect ScaleToEnclosingRectSafe(const Rect& rect,
293 float x_scale,
294 float y_scale) {
295 if (x_scale == 1.f && y_scale == 1.f)
296 return rect;
297 int x = base::saturated_cast<int>(std::floor(rect.x() * x_scale));
298 int y = base::saturated_cast<int>(std::floor(rect.y() * y_scale));
299 int w = base::saturated_cast<int>(std::ceil(rect.width() * x_scale));
300 int h = base::saturated_cast<int>(std::ceil(rect.height() * y_scale));
301 return Rect(x, y, w, h);
302 }
303
304 inline Rect ScaleToEnclosingRectSafe(const Rect& rect, float scale) {
305 return ScaleToEnclosingRectSafe(rect, scale, scale);
306 }
307
287 inline Rect ScaleToEnclosedRect(const Rect& rect, 308 inline Rect ScaleToEnclosedRect(const Rect& rect,
288 float x_scale, 309 float x_scale,
289 float y_scale) { 310 float y_scale) {
290 if (x_scale == 1.f && y_scale == 1.f) 311 if (x_scale == 1.f && y_scale == 1.f)
291 return rect; 312 return rect;
292 DCHECK(base::IsValueInRangeForNumericType<int>( 313 DCHECK(base::IsValueInRangeForNumericType<int>(
293 std::ceil(rect.x() * x_scale))); 314 std::ceil(rect.x() * x_scale)));
294 DCHECK(base::IsValueInRangeForNumericType<int>( 315 DCHECK(base::IsValueInRangeForNumericType<int>(
295 std::ceil(rect.y() * y_scale))); 316 std::ceil(rect.y() * y_scale)));
296 DCHECK(base::IsValueInRangeForNumericType<int>( 317 DCHECK(base::IsValueInRangeForNumericType<int>(
(...skipping 14 matching lines...) Expand all
311 } 332 }
312 333
313 // This is declared here for use in gtest-based unit tests but is defined in 334 // 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 335 // 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. 336 // test. This should not be used in production code - call ToString() instead.
316 void PrintTo(const Rect& rect, ::std::ostream* os); 337 void PrintTo(const Rect& rect, ::std::ostream* os);
317 338
318 } // namespace gfx 339 } // namespace gfx
319 340
320 #endif // UI_GFX_GEOMETRY_RECT_H_ 341 #endif // UI_GFX_GEOMETRY_RECT_H_
OLDNEW
« no previous file with comments | « cc/layers/painted_scrollbar_layer.cc ('k') | ui/gfx/geometry/rect_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698