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 #ifndef UI_GFX_GEOMETRY_POINT_H_ | 5 #ifndef UI_GFX_GEOMETRY_POINT_H_ |
6 #define UI_GFX_GEOMETRY_POINT_H_ | 6 #define UI_GFX_GEOMETRY_POINT_H_ |
7 | 7 |
8 #include <iosfwd> | 8 #include <iosfwd> |
9 #include <string> | 9 #include <string> |
10 #include <tuple> | 10 #include <tuple> |
11 | 11 |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "ui/gfx/geometry/safe_integer_conversions.h" |
13 #include "ui/gfx/geometry/vector2d.h" | 14 #include "ui/gfx/geometry/vector2d.h" |
14 #include "ui/gfx/gfx_export.h" | 15 #include "ui/gfx/gfx_export.h" |
15 | 16 |
16 #if defined(OS_WIN) | 17 #if defined(OS_WIN) |
17 typedef unsigned long DWORD; | 18 typedef unsigned long DWORD; |
18 typedef struct tagPOINT POINT; | 19 typedef struct tagPOINT POINT; |
19 #elif defined(OS_MACOSX) | 20 #elif defined(OS_MACOSX) |
20 typedef struct CGPoint CGPoint; | 21 typedef struct CGPoint CGPoint; |
21 #endif | 22 #endif |
22 | 23 |
(...skipping 25 matching lines...) Expand all Loading... |
48 constexpr int y() const { return y_; } | 49 constexpr int y() const { return y_; } |
49 void set_x(int x) { x_ = x; } | 50 void set_x(int x) { x_ = x; } |
50 void set_y(int y) { y_ = y; } | 51 void set_y(int y) { y_ = y; } |
51 | 52 |
52 void SetPoint(int x, int y) { | 53 void SetPoint(int x, int y) { |
53 x_ = x; | 54 x_ = x; |
54 y_ = y; | 55 y_ = y; |
55 } | 56 } |
56 | 57 |
57 void Offset(int delta_x, int delta_y) { | 58 void Offset(int delta_x, int delta_y) { |
58 x_ += delta_x; | 59 x_ = SafeAdd(x_, delta_x); |
59 y_ += delta_y; | 60 y_ = SafeAdd(y_, delta_y); |
60 } | 61 } |
61 | 62 |
62 void operator+=(const Vector2d& vector) { | 63 void operator+=(const Vector2d& vector) { |
63 x_ += vector.x(); | 64 x_ = SafeAdd(x_, vector.x()); |
64 y_ += vector.y(); | 65 y_ = SafeAdd(y_, vector.y()); |
65 } | 66 } |
66 | 67 |
67 void operator-=(const Vector2d& vector) { | 68 void operator-=(const Vector2d& vector) { |
68 x_ -= vector.x(); | 69 x_ = SafeSubtract(x_, vector.x()); |
69 y_ -= vector.y(); | 70 y_ = SafeSubtract(y_, vector.y()); |
70 } | 71 } |
71 | 72 |
72 void SetToMin(const Point& other); | 73 void SetToMin(const Point& other); |
73 void SetToMax(const Point& other); | 74 void SetToMax(const Point& other); |
74 | 75 |
75 bool IsOrigin() const { return x_ == 0 && y_ == 0; } | 76 bool IsOrigin() const { return x_ == 0 && y_ == 0; } |
76 | 77 |
77 Vector2d OffsetFromOrigin() const { return Vector2d(x_, y_); } | 78 Vector2d OffsetFromOrigin() const { return Vector2d(x_, y_); } |
78 | 79 |
79 // A point is less than another point if its y-value is closer | 80 // A point is less than another point if its y-value is closer |
(...skipping 28 matching lines...) Expand all Loading... |
108 return result; | 109 return result; |
109 } | 110 } |
110 | 111 |
111 inline Point operator-(const Point& lhs, const Vector2d& rhs) { | 112 inline Point operator-(const Point& lhs, const Vector2d& rhs) { |
112 Point result(lhs); | 113 Point result(lhs); |
113 result -= rhs; | 114 result -= rhs; |
114 return result; | 115 return result; |
115 } | 116 } |
116 | 117 |
117 inline Vector2d operator-(const Point& lhs, const Point& rhs) { | 118 inline Vector2d operator-(const Point& lhs, const Point& rhs) { |
118 return Vector2d(lhs.x() - rhs.x(), lhs.y() - rhs.y()); | 119 return Vector2d(SafeSubtract(lhs.x(), rhs.x()), |
| 120 SafeSubtract(lhs.y(), rhs.y())); |
119 } | 121 } |
120 | 122 |
121 inline Point PointAtOffsetFromOrigin(const Vector2d& offset_from_origin) { | 123 inline Point PointAtOffsetFromOrigin(const Vector2d& offset_from_origin) { |
122 return Point(offset_from_origin.x(), offset_from_origin.y()); | 124 return Point(offset_from_origin.x(), offset_from_origin.y()); |
123 } | 125 } |
124 | 126 |
125 // This is declared here for use in gtest-based unit tests but is defined in | 127 // This is declared here for use in gtest-based unit tests but is defined in |
126 // the //ui/gfx:test_support target. Depend on that to use this in your unit | 128 // the //ui/gfx:test_support target. Depend on that to use this in your unit |
127 // test. This should not be used in production code - call ToString() instead. | 129 // test. This should not be used in production code - call ToString() instead. |
128 void PrintTo(const Point& point, ::std::ostream* os); | 130 void PrintTo(const Point& point, ::std::ostream* os); |
129 | 131 |
130 // Helper methods to scale a gfx::Point to a new gfx::Point. | 132 // Helper methods to scale a gfx::Point to a new gfx::Point. |
131 GFX_EXPORT Point ScaleToCeiledPoint(const Point& point, | 133 GFX_EXPORT Point ScaleToCeiledPoint(const Point& point, |
132 float x_scale, | 134 float x_scale, |
133 float y_scale); | 135 float y_scale); |
134 GFX_EXPORT Point ScaleToCeiledPoint(const Point& point, float x_scale); | 136 GFX_EXPORT Point ScaleToCeiledPoint(const Point& point, float x_scale); |
135 GFX_EXPORT Point ScaleToFlooredPoint(const Point& point, | 137 GFX_EXPORT Point ScaleToFlooredPoint(const Point& point, |
136 float x_scale, | 138 float x_scale, |
137 float y_scale); | 139 float y_scale); |
138 GFX_EXPORT Point ScaleToFlooredPoint(const Point& point, float x_scale); | 140 GFX_EXPORT Point ScaleToFlooredPoint(const Point& point, float x_scale); |
139 GFX_EXPORT Point ScaleToRoundedPoint(const Point& point, | 141 GFX_EXPORT Point ScaleToRoundedPoint(const Point& point, |
140 float x_scale, | 142 float x_scale, |
141 float y_scale); | 143 float y_scale); |
142 GFX_EXPORT Point ScaleToRoundedPoint(const Point& point, float x_scale); | 144 GFX_EXPORT Point ScaleToRoundedPoint(const Point& point, float x_scale); |
143 | 145 |
144 } // namespace gfx | 146 } // namespace gfx |
145 | 147 |
146 #endif // UI_GFX_GEOMETRY_POINT_H_ | 148 #endif // UI_GFX_GEOMETRY_POINT_H_ |
OLD | NEW |