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

Side by Side 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 unified diff | 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 »
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.
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"
19 #include "ui/gfx/geometry/point.h" 20 #include "ui/gfx/geometry/point.h"
20 #include "ui/gfx/geometry/rect_f.h" 21 #include "ui/gfx/geometry/rect_f.h"
21 #include "ui/gfx/geometry/size.h" 22 #include "ui/gfx/geometry/size.h"
22 #include "ui/gfx/geometry/vector2d.h" 23 #include "ui/gfx/geometry/vector2d.h"
23 24
24 #if defined(OS_WIN) 25 #if defined(OS_WIN)
25 typedef struct tagRECT RECT; 26 typedef struct tagRECT RECT;
26 #elif defined(OS_IOS) 27 #elif defined(OS_IOS)
27 #include <CoreGraphics/CoreGraphics.h> 28 #include <CoreGraphics/CoreGraphics.h>
28 #elif defined(OS_MACOSX) 29 #elif defined(OS_MACOSX)
(...skipping 23 matching lines...) Expand all
52 53
53 #if defined(OS_WIN) 54 #if defined(OS_WIN)
54 // Construct an equivalent Win32 RECT object. 55 // Construct an equivalent Win32 RECT object.
55 RECT ToRECT() const; 56 RECT ToRECT() const;
56 #elif defined(OS_MACOSX) 57 #elif defined(OS_MACOSX)
57 // Construct an equivalent CoreGraphics object. 58 // Construct an equivalent CoreGraphics object.
58 CGRect ToCGRect() const; 59 CGRect ToCGRect() const;
59 #endif 60 #endif
60 61
61 operator RectF() const { 62 operator RectF() const {
62 return RectF(origin().x(), origin().y(), size().width(), size().height()); 63 return RectF(static_cast<float>(x()), static_cast<float>(y()),
64 static_cast<float>(width()), static_cast<float>(height()));
63 } 65 }
64 66
65 int x() const { return origin_.x(); } 67 int x() const { return origin_.x(); }
66 void set_x(int x) { origin_.set_x(x); } 68 void set_x(int x) { origin_.set_x(x); }
67 69
68 int y() const { return origin_.y(); } 70 int y() const { return origin_.y(); }
69 void set_y(int y) { origin_.set_y(y); } 71 void set_y(int y) { origin_.set_y(y); }
70 72
71 int width() const { return size_.width(); } 73 int width() const { return size_.width(); }
72 void set_width(int width) { size_.set_width(width); } 74 void set_width(int width) { size_.set_width(width); }
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 // 215 //
214 // This could also be thought of as "the smallest rect that contains both 216 // This could also be thought of as "the smallest rect that contains both
215 // points", except that we consider points on the right/bottom edges of the 217 // points", except that we consider points on the right/bottom edges of the
216 // rect to be outside the rect. So technically one or both points will not be 218 // rect to be outside the rect. So technically one or both points will not be
217 // contained within the rect, because they will appear on one of these edges. 219 // contained within the rect, because they will appear on one of these edges.
218 GFX_EXPORT Rect BoundingRect(const Point& p1, const Point& p2); 220 GFX_EXPORT Rect BoundingRect(const Point& p1, const Point& p2);
219 221
220 inline Rect ScaleToEnclosingRect(const Rect& rect, 222 inline Rect ScaleToEnclosingRect(const Rect& rect,
221 float x_scale, 223 float x_scale,
222 float y_scale) { 224 float y_scale) {
223 int x = std::floor(rect.x() * x_scale); 225 // These next functions cast instead of using e.g. ToFlooredInt() because we
224 int y = std::floor(rect.y() * y_scale); 226 // haven't checked to ensure that the clamping behavior of the helper
225 int r = rect.width() == 0 ? x : std::ceil(rect.right() * x_scale); 227 // functions doesn't degrade performance, and callers shouldn't be passing
226 int b = rect.height() == 0 ? y : std::ceil(rect.bottom() * y_scale); 228 // values that cause overflow anyway.
229 DCHECK(base::IsValueInRangeForNumericType<int>(
230 std::floor(rect.x() * x_scale)));
231 DCHECK(base::IsValueInRangeForNumericType<int>(
232 std::floor(rect.y() * y_scale)));
233 DCHECK(base::IsValueInRangeForNumericType<int>(
234 std::ceil(rect.right() * x_scale)));
235 DCHECK(base::IsValueInRangeForNumericType<int>(
236 std::ceil(rect.bottom() * y_scale)));
237 int x = static_cast<int>(std::floor(rect.x() * x_scale));
238 int y = static_cast<int>(std::floor(rect.y() * y_scale));
239 int r = rect.width() == 0 ?
240 x : static_cast<int>(std::ceil(rect.right() * x_scale));
241 int b = rect.height() == 0 ?
242 y : static_cast<int>(std::ceil(rect.bottom() * y_scale));
227 return Rect(x, y, r - x, b - y); 243 return Rect(x, y, r - x, b - y);
228 } 244 }
229 245
230 inline Rect ScaleToEnclosingRect(const Rect& rect, float scale) { 246 inline Rect ScaleToEnclosingRect(const Rect& rect, float scale) {
231 return ScaleToEnclosingRect(rect, scale, scale); 247 return ScaleToEnclosingRect(rect, scale, scale);
232 } 248 }
233 249
234 inline Rect ScaleToEnclosedRect(const Rect& rect, 250 inline Rect ScaleToEnclosedRect(const Rect& rect,
235 float x_scale, 251 float x_scale,
236 float y_scale) { 252 float y_scale) {
237 int x = std::ceil(rect.x() * x_scale); 253 DCHECK(base::IsValueInRangeForNumericType<int>(
238 int y = std::ceil(rect.y() * y_scale); 254 std::ceil(rect.x() * x_scale)));
239 int r = rect.width() == 0 ? x : std::floor(rect.right() * x_scale); 255 DCHECK(base::IsValueInRangeForNumericType<int>(
240 int b = rect.height() == 0 ? y : std::floor(rect.bottom() * y_scale); 256 std::ceil(rect.y() * y_scale)));
257 DCHECK(base::IsValueInRangeForNumericType<int>(
258 std::floor(rect.right() * x_scale)));
259 DCHECK(base::IsValueInRangeForNumericType<int>(
260 std::floor(rect.bottom() * y_scale)));
261 int x = static_cast<int>(std::ceil(rect.x() * x_scale));
262 int y = static_cast<int>(std::ceil(rect.y() * y_scale));
263 int r = rect.width() == 0 ?
264 x : static_cast<int>(std::floor(rect.right() * x_scale));
265 int b = rect.height() == 0 ?
266 y : static_cast<int>(std::floor(rect.bottom() * y_scale));
241 return Rect(x, y, r - x, b - y); 267 return Rect(x, y, r - x, b - y);
242 } 268 }
243 269
244 inline Rect ScaleToEnclosedRect(const Rect& rect, float scale) { 270 inline Rect ScaleToEnclosedRect(const Rect& rect, float scale) {
245 return ScaleToEnclosedRect(rect, scale, scale); 271 return ScaleToEnclosedRect(rect, scale, scale);
246 } 272 }
247 273
248 // This is declared here for use in gtest-based unit tests but is defined in 274 // This is declared here for use in gtest-based unit tests but is defined in
249 // the gfx_test_support target. Depend on that to use this in your unit test. 275 // the gfx_test_support target. Depend on that to use this in your unit test.
250 // This should not be used in production code - call ToString() instead. 276 // This should not be used in production code - call ToString() instead.
251 void PrintTo(const Rect& rect, ::std::ostream* os); 277 void PrintTo(const Rect& rect, ::std::ostream* os);
252 278
253 } // namespace gfx 279 } // namespace gfx
254 280
255 #endif // UI_GFX_GEOMETRY_RECT_H_ 281 #endif // UI_GFX_GEOMETRY_RECT_H_
OLDNEW
« 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