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

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: SkIntToMScalar 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
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"
22 #include "ui/gfx/geometry/safe_integer_conversions.h"
danakj 2014/11/03 20:35:26 unused?
Peter Kasting 2014/11/03 22:19:55 Oops, thanks.
21 #include "ui/gfx/geometry/size.h" 23 #include "ui/gfx/geometry/size.h"
22 #include "ui/gfx/geometry/vector2d.h" 24 #include "ui/gfx/geometry/vector2d.h"
23 25
24 #if defined(OS_WIN) 26 #if defined(OS_WIN)
25 typedef struct tagRECT RECT; 27 typedef struct tagRECT RECT;
26 #elif defined(OS_IOS) 28 #elif defined(OS_IOS)
27 #include <CoreGraphics/CoreGraphics.h> 29 #include <CoreGraphics/CoreGraphics.h>
28 #elif defined(OS_MACOSX) 30 #elif defined(OS_MACOSX)
29 #include <ApplicationServices/ApplicationServices.h> 31 #include <ApplicationServices/ApplicationServices.h>
30 #endif 32 #endif
(...skipping 21 matching lines...) Expand all
52 54
53 #if defined(OS_WIN) 55 #if defined(OS_WIN)
54 // Construct an equivalent Win32 RECT object. 56 // Construct an equivalent Win32 RECT object.
55 RECT ToRECT() const; 57 RECT ToRECT() const;
56 #elif defined(OS_MACOSX) 58 #elif defined(OS_MACOSX)
57 // Construct an equivalent CoreGraphics object. 59 // Construct an equivalent CoreGraphics object.
58 CGRect ToCGRect() const; 60 CGRect ToCGRect() const;
59 #endif 61 #endif
60 62
61 operator RectF() const { 63 operator RectF() const {
62 return RectF(origin().x(), origin().y(), size().width(), size().height()); 64 return RectF(static_cast<float>(x()), static_cast<float>(y()),
65 static_cast<float>(width()), static_cast<float>(height()));
63 } 66 }
64 67
65 int x() const { return origin_.x(); } 68 int x() const { return origin_.x(); }
66 void set_x(int x) { origin_.set_x(x); } 69 void set_x(int x) { origin_.set_x(x); }
67 70
68 int y() const { return origin_.y(); } 71 int y() const { return origin_.y(); }
69 void set_y(int y) { origin_.set_y(y); } 72 void set_y(int y) { origin_.set_y(y); }
70 73
71 int width() const { return size_.width(); } 74 int width() const { return size_.width(); }
72 void set_width(int width) { size_.set_width(width); } 75 void set_width(int width) { size_.set_width(width); }
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 // 216 //
214 // This could also be thought of as "the smallest rect that contains both 217 // 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 218 // 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 219 // 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. 220 // contained within the rect, because they will appear on one of these edges.
218 GFX_EXPORT Rect BoundingRect(const Point& p1, const Point& p2); 221 GFX_EXPORT Rect BoundingRect(const Point& p1, const Point& p2);
219 222
220 inline Rect ScaleToEnclosingRect(const Rect& rect, 223 inline Rect ScaleToEnclosingRect(const Rect& rect,
221 float x_scale, 224 float x_scale,
222 float y_scale) { 225 float y_scale) {
223 int x = std::floor(rect.x() * x_scale); 226 // These next functions cast instead of using e.g. ToFlooredInt() because we
224 int y = std::floor(rect.y() * y_scale); 227 // 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); 228 // functions doesn't degrade performance, and callers shouldn't be passing
226 int b = rect.height() == 0 ? y : std::ceil(rect.bottom() * y_scale); 229 // values that cause overflow anyway.
230 float scaled_x = std::floor(rect.x() * x_scale);
231 DCHECK(base::IsValueInRangeForNumericType<int>(scaled_x));
danakj 2014/11/03 20:35:26 nit: it might be easier to read if you just moved
Peter Kasting 2014/11/03 22:19:55 Done.
232 int x = static_cast<int>(scaled_x);
233 float scaled_y = std::floor(rect.y() * y_scale);
234 DCHECK(base::IsValueInRangeForNumericType<int>(scaled_y));
235 int y = static_cast<int>(scaled_y);
236 int r, b;
237 if (rect.width() == 0) {
238 r = x;
239 } else {
240 float scaled_r = std::ceil(rect.right() * x_scale);
241 DCHECK(base::IsValueInRangeForNumericType<int>(scaled_r));
242 r = static_cast<int>(scaled_r);
243 }
244 if (rect.height() == 0) {
245 b = y;
246 } else {
247 float scaled_b = std::ceil(rect.bottom() * y_scale);
248 DCHECK(base::IsValueInRangeForNumericType<int>(scaled_b));
249 b = static_cast<int>(scaled_b);
250 }
227 return Rect(x, y, r - x, b - y); 251 return Rect(x, y, r - x, b - y);
228 } 252 }
229 253
230 inline Rect ScaleToEnclosingRect(const Rect& rect, float scale) { 254 inline Rect ScaleToEnclosingRect(const Rect& rect, float scale) {
231 return ScaleToEnclosingRect(rect, scale, scale); 255 return ScaleToEnclosingRect(rect, scale, scale);
232 } 256 }
233 257
234 inline Rect ScaleToEnclosedRect(const Rect& rect, 258 inline Rect ScaleToEnclosedRect(const Rect& rect,
235 float x_scale, 259 float x_scale,
236 float y_scale) { 260 float y_scale) {
237 int x = std::ceil(rect.x() * x_scale); 261 float scaled_x = std::ceil(rect.x() * x_scale);
238 int y = std::ceil(rect.y() * y_scale); 262 DCHECK(base::IsValueInRangeForNumericType<int>(scaled_x));
239 int r = rect.width() == 0 ? x : std::floor(rect.right() * x_scale); 263 int x = static_cast<int>(scaled_x);
240 int b = rect.height() == 0 ? y : std::floor(rect.bottom() * y_scale); 264 float scaled_y = std::ceil(rect.y() * y_scale);
265 DCHECK(base::IsValueInRangeForNumericType<int>(scaled_y));
266 int y = static_cast<int>(scaled_y);
267 int r, b;
268 if (rect.width() == 0) {
269 r = x;
270 } else {
271 float scaled_r = std::floor(rect.right() * x_scale);
272 DCHECK(base::IsValueInRangeForNumericType<int>(scaled_r));
273 r = static_cast<int>(scaled_r);
274 }
275 if (rect.height() == 0) {
276 b = y;
277 } else {
278 float scaled_b = std::floor(rect.bottom() * y_scale);
279 DCHECK(base::IsValueInRangeForNumericType<int>(scaled_b));
280 b = static_cast<int>(scaled_b);
281 }
241 return Rect(x, y, r - x, b - y); 282 return Rect(x, y, r - x, b - y);
242 } 283 }
243 284
244 inline Rect ScaleToEnclosedRect(const Rect& rect, float scale) { 285 inline Rect ScaleToEnclosedRect(const Rect& rect, float scale) {
245 return ScaleToEnclosedRect(rect, scale, scale); 286 return ScaleToEnclosedRect(rect, scale, scale);
246 } 287 }
247 288
248 // This is declared here for use in gtest-based unit tests but is defined in 289 // 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. 290 // 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. 291 // This should not be used in production code - call ToString() instead.
251 void PrintTo(const Rect& rect, ::std::ostream* os); 292 void PrintTo(const Rect& rect, ::std::ostream* os);
252 293
253 } // namespace gfx 294 } // namespace gfx
254 295
255 #endif // UI_GFX_GEOMETRY_RECT_H_ 296 #endif // UI_GFX_GEOMETRY_RECT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698