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

Unified Diff: ui/gfx/geometry/rect.h

Issue 649203003: Type conversion fixes, ui/gfx/ edition. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Review comments Created 6 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/gfx/geometry/point.h ('k') | ui/gfx/geometry/safe_integer_conversions.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/geometry/rect.h
diff --git a/ui/gfx/geometry/rect.h b/ui/gfx/geometry/rect.h
index fed202d2bd30b769f91ee041c70224f618495010..b2e84fe16e1e344605fe00012951b83bdf5e2956 100644
--- a/ui/gfx/geometry/rect.h
+++ b/ui/gfx/geometry/rect.h
@@ -16,6 +16,7 @@
#include <iosfwd>
#include <string>
+#include "base/numerics/safe_conversions.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/geometry/size.h"
@@ -59,7 +60,8 @@ class GFX_EXPORT Rect {
#endif
operator RectF() const {
- return RectF(origin().x(), origin().y(), size().width(), size().height());
+ return RectF(static_cast<float>(x()), static_cast<float>(y()),
+ static_cast<float>(width()), static_cast<float>(height()));
}
int x() const { return origin_.x(); }
@@ -220,10 +222,24 @@ GFX_EXPORT Rect BoundingRect(const Point& p1, const Point& p2);
inline Rect ScaleToEnclosingRect(const Rect& rect,
float x_scale,
float y_scale) {
- int x = std::floor(rect.x() * x_scale);
- int y = std::floor(rect.y() * y_scale);
- int r = rect.width() == 0 ? x : std::ceil(rect.right() * x_scale);
- int b = rect.height() == 0 ? y : std::ceil(rect.bottom() * y_scale);
+ // These next functions cast instead of using e.g. ToFlooredInt() because we
+ // haven't checked to ensure that the clamping behavior of the helper
+ // functions doesn't degrade performance, and callers shouldn't be passing
+ // values that cause overflow anyway.
+ DCHECK(base::IsValueInRangeForNumericType<int>(
+ std::floor(rect.x() * x_scale)));
+ DCHECK(base::IsValueInRangeForNumericType<int>(
+ std::floor(rect.y() * y_scale)));
+ DCHECK(base::IsValueInRangeForNumericType<int>(
+ std::ceil(rect.right() * x_scale)));
+ DCHECK(base::IsValueInRangeForNumericType<int>(
+ std::ceil(rect.bottom() * y_scale)));
+ int x = static_cast<int>(std::floor(rect.x() * x_scale));
+ int y = static_cast<int>(std::floor(rect.y() * y_scale));
+ int r = rect.width() == 0 ?
+ x : static_cast<int>(std::ceil(rect.right() * x_scale));
+ int b = rect.height() == 0 ?
+ y : static_cast<int>(std::ceil(rect.bottom() * y_scale));
return Rect(x, y, r - x, b - y);
}
@@ -234,10 +250,20 @@ inline Rect ScaleToEnclosingRect(const Rect& rect, float scale) {
inline Rect ScaleToEnclosedRect(const Rect& rect,
float x_scale,
float y_scale) {
- int x = std::ceil(rect.x() * x_scale);
- int y = std::ceil(rect.y() * y_scale);
- int r = rect.width() == 0 ? x : std::floor(rect.right() * x_scale);
- int b = rect.height() == 0 ? y : std::floor(rect.bottom() * y_scale);
+ DCHECK(base::IsValueInRangeForNumericType<int>(
+ std::ceil(rect.x() * x_scale)));
+ DCHECK(base::IsValueInRangeForNumericType<int>(
+ std::ceil(rect.y() * y_scale)));
+ DCHECK(base::IsValueInRangeForNumericType<int>(
+ std::floor(rect.right() * x_scale)));
+ DCHECK(base::IsValueInRangeForNumericType<int>(
+ std::floor(rect.bottom() * y_scale)));
+ int x = static_cast<int>(std::ceil(rect.x() * x_scale));
+ int y = static_cast<int>(std::ceil(rect.y() * y_scale));
+ int r = rect.width() == 0 ?
+ x : static_cast<int>(std::floor(rect.right() * x_scale));
+ int b = rect.height() == 0 ?
+ y : static_cast<int>(std::floor(rect.bottom() * y_scale));
return Rect(x, y, r - x, b - y);
}
« no previous file with comments | « ui/gfx/geometry/point.h ('k') | ui/gfx/geometry/safe_integer_conversions.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698