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 "ui/gfx/geometry/point.h" | 19 #include "ui/gfx/geometry/point.h" |
20 #include "ui/gfx/geometry/rect_f.h" | 20 #include "ui/gfx/geometry/rect_f.h" |
21 #include "ui/gfx/geometry/safe_integer_conversions.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) |
29 #include <ApplicationServices/ApplicationServices.h> | 30 #include <ApplicationServices/ApplicationServices.h> |
30 #endif | 31 #endif |
(...skipping 21 matching lines...) Expand all Loading... | |
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 Loading... | |
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 int x = ToFlooredInt(rect.x() * x_scale); |
danakj
2014/10/23 15:35:44
i had written these purposefully as static_casts i
Peter Kasting
2014/10/23 17:29:45
Is that a performance concern? I originally added
danakj
2014/10/23 17:43:23
I think we should cast unless we can show there is
| |
224 int y = std::floor(rect.y() * y_scale); | 226 int y = ToFlooredInt(rect.y() * y_scale); |
225 int r = rect.width() == 0 ? x : std::ceil(rect.right() * x_scale); | 227 int r = rect.width() == 0 ? x : ToCeiledInt(rect.right() * x_scale); |
226 int b = rect.height() == 0 ? y : std::ceil(rect.bottom() * y_scale); | 228 int b = rect.height() == 0 ? y : ToCeiledInt(rect.bottom() * y_scale); |
227 return Rect(x, y, r - x, b - y); | 229 return Rect(x, y, r - x, b - y); |
228 } | 230 } |
229 | 231 |
230 inline Rect ScaleToEnclosingRect(const Rect& rect, float scale) { | 232 inline Rect ScaleToEnclosingRect(const Rect& rect, float scale) { |
231 return ScaleToEnclosingRect(rect, scale, scale); | 233 return ScaleToEnclosingRect(rect, scale, scale); |
232 } | 234 } |
233 | 235 |
234 inline Rect ScaleToEnclosedRect(const Rect& rect, | 236 inline Rect ScaleToEnclosedRect(const Rect& rect, |
235 float x_scale, | 237 float x_scale, |
236 float y_scale) { | 238 float y_scale) { |
237 int x = std::ceil(rect.x() * x_scale); | 239 int x = ToCeiledInt(rect.x() * x_scale); |
238 int y = std::ceil(rect.y() * y_scale); | 240 int y = ToCeiledInt(rect.y() * y_scale); |
239 int r = rect.width() == 0 ? x : std::floor(rect.right() * x_scale); | 241 int r = rect.width() == 0 ? x : ToFlooredInt(rect.right() * x_scale); |
240 int b = rect.height() == 0 ? y : std::floor(rect.bottom() * y_scale); | 242 int b = rect.height() == 0 ? y : ToFlooredInt(rect.bottom() * y_scale); |
241 return Rect(x, y, r - x, b - y); | 243 return Rect(x, y, r - x, b - y); |
242 } | 244 } |
243 | 245 |
244 inline Rect ScaleToEnclosedRect(const Rect& rect, float scale) { | 246 inline Rect ScaleToEnclosedRect(const Rect& rect, float scale) { |
245 return ScaleToEnclosedRect(rect, scale, scale); | 247 return ScaleToEnclosedRect(rect, scale, scale); |
246 } | 248 } |
247 | 249 |
248 // This is declared here for use in gtest-based unit tests but is defined in | 250 // 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. | 251 // 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. | 252 // This should not be used in production code - call ToString() instead. |
251 void PrintTo(const Rect& rect, ::std::ostream* os); | 253 void PrintTo(const Rect& rect, ::std::ostream* os); |
252 | 254 |
253 } // namespace gfx | 255 } // namespace gfx |
254 | 256 |
255 #endif // UI_GFX_GEOMETRY_RECT_H_ | 257 #endif // UI_GFX_GEOMETRY_RECT_H_ |
OLD | NEW |