| 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. |
| 11 | 11 |
| 12 #ifndef UI_GFX_GEOMETRY_RECT_H_ | 12 #ifndef UI_GFX_GEOMETRY_RECT_H_ |
| 13 #define UI_GFX_GEOMETRY_RECT_H_ | 13 #define UI_GFX_GEOMETRY_RECT_H_ |
| 14 | 14 |
| 15 #include <cmath> | 15 #include <cmath> |
| 16 #include <iosfwd> | 16 #include <iosfwd> |
| 17 #include <string> | 17 #include <string> |
| 18 | 18 |
| 19 #include "base/numerics/safe_conversions.h" | |
| 20 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 21 #include "ui/gfx/geometry/point.h" | 20 #include "ui/gfx/geometry/point.h" |
| 21 #include "ui/gfx/geometry/safe_integer_conversions.h" |
| 22 #include "ui/gfx/geometry/size.h" | 22 #include "ui/gfx/geometry/size.h" |
| 23 #include "ui/gfx/geometry/vector2d.h" | 23 #include "ui/gfx/geometry/vector2d.h" |
| 24 | 24 |
| 25 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
| 26 typedef struct tagRECT RECT; | 26 typedef struct tagRECT RECT; |
| 27 #elif defined(OS_MACOSX) | 27 #elif defined(OS_MACOSX) |
| 28 typedef struct CGRect CGRect; | 28 typedef struct CGRect CGRect; |
| 29 #endif | 29 #endif |
| 30 | 30 |
| 31 namespace gfx { | 31 namespace gfx { |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 | 200 |
| 201 std::string ToString() const; | 201 std::string ToString() const; |
| 202 | 202 |
| 203 bool ApproximatelyEqual(const Rect& rect, int tolerance) const; | 203 bool ApproximatelyEqual(const Rect& rect, int tolerance) const; |
| 204 | 204 |
| 205 private: | 205 private: |
| 206 gfx::Point origin_; | 206 gfx::Point origin_; |
| 207 gfx::Size size_; | 207 gfx::Size size_; |
| 208 | 208 |
| 209 // Clamp the size to avoid integer overflow in bottom() and right(). | 209 // Clamp the size to avoid integer overflow in bottom() and right(). |
| 210 // There are three conditions to determine whether there is a potential | 210 // This returns the width given an origin and a width. |
| 211 // overflow: | |
| 212 // 1) Origin > 0: if the origin is a negative value, origin + size will | |
| 213 // definitely be less than int_max. | |
| 214 // 2) size > 0: if size <= 0, it will be clamped to 0 making x + 0 valid for | |
| 215 // all x. | |
| 216 // 3) We cast the values to unsigned int because the compiler can optimize | |
| 217 // this check away entirely but it is not smart enough to know that it | |
| 218 // won't overflow. It can't overflow since origin is positive ensured by | |
| 219 // part 1). If size > int_max - origin it will overflow when added to | |
| 220 // origin. | |
| 221 static constexpr int GetClampedValue(int origin, int size) { | 211 static constexpr int GetClampedValue(int origin, int size) { |
| 222 return origin > 0 && size > 0 && | 212 return AddWouldOverflow(origin, size) |
| 223 static_cast<unsigned>(std::numeric_limits<int>::max() - | |
| 224 origin) < static_cast<unsigned>(size) | |
| 225 ? std::numeric_limits<int>::max() - origin | 213 ? std::numeric_limits<int>::max() - origin |
| 226 : size; | 214 : size; |
| 227 } | 215 } |
| 216 |
| 217 // Returns a clamped width given a right and a left, assuming right > left. |
| 218 static constexpr int GetClampedWidthFromExtents(int left, int right) { |
| 219 return SubtractWouldOverflow(right, left) ? std::numeric_limits<int>::max() |
| 220 : right - left; |
| 221 } |
| 228 }; | 222 }; |
| 229 | 223 |
| 230 inline bool operator==(const Rect& lhs, const Rect& rhs) { | 224 inline bool operator==(const Rect& lhs, const Rect& rhs) { |
| 231 return lhs.origin() == rhs.origin() && lhs.size() == rhs.size(); | 225 return lhs.origin() == rhs.origin() && lhs.size() == rhs.size(); |
| 232 } | 226 } |
| 233 | 227 |
| 234 inline bool operator!=(const Rect& lhs, const Rect& rhs) { | 228 inline bool operator!=(const Rect& lhs, const Rect& rhs) { |
| 235 return !(lhs == rhs); | 229 return !(lhs == rhs); |
| 236 } | 230 } |
| 237 | 231 |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 } | 326 } |
| 333 | 327 |
| 334 // This is declared here for use in gtest-based unit tests but is defined in | 328 // This is declared here for use in gtest-based unit tests but is defined in |
| 335 // the //ui/gfx:test_support target. Depend on that to use this in your unit | 329 // the //ui/gfx:test_support target. Depend on that to use this in your unit |
| 336 // test. This should not be used in production code - call ToString() instead. | 330 // test. This should not be used in production code - call ToString() instead. |
| 337 void PrintTo(const Rect& rect, ::std::ostream* os); | 331 void PrintTo(const Rect& rect, ::std::ostream* os); |
| 338 | 332 |
| 339 } // namespace gfx | 333 } // namespace gfx |
| 340 | 334 |
| 341 #endif // UI_GFX_GEOMETRY_RECT_H_ | 335 #endif // UI_GFX_GEOMETRY_RECT_H_ |
| OLD | NEW |