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 "base/numerics/saturated_arithmetic.h" |
12 #include "build/build_config.h" | 13 #include "build/build_config.h" |
13 #include "ui/gfx/geometry/safe_integer_conversions.h" | |
14 #include "ui/gfx/geometry/vector2d.h" | 14 #include "ui/gfx/geometry/vector2d.h" |
15 #include "ui/gfx/gfx_export.h" | 15 #include "ui/gfx/gfx_export.h" |
16 | 16 |
17 #if defined(OS_WIN) | 17 #if defined(OS_WIN) |
18 typedef unsigned long DWORD; | 18 typedef unsigned long DWORD; |
19 typedef struct tagPOINT POINT; | 19 typedef struct tagPOINT POINT; |
20 #elif defined(OS_MACOSX) | 20 #elif defined(OS_MACOSX) |
21 typedef struct CGPoint CGPoint; | 21 typedef struct CGPoint CGPoint; |
22 #endif | 22 #endif |
23 | 23 |
(...skipping 25 matching lines...) Expand all Loading... |
49 constexpr int y() const { return y_; } | 49 constexpr int y() const { return y_; } |
50 void set_x(int x) { x_ = x; } | 50 void set_x(int x) { x_ = x; } |
51 void set_y(int y) { y_ = y; } | 51 void set_y(int y) { y_ = y; } |
52 | 52 |
53 void SetPoint(int x, int y) { | 53 void SetPoint(int x, int y) { |
54 x_ = x; | 54 x_ = x; |
55 y_ = y; | 55 y_ = y; |
56 } | 56 } |
57 | 57 |
58 void Offset(int delta_x, int delta_y) { | 58 void Offset(int delta_x, int delta_y) { |
59 x_ = SafeAdd(x_, delta_x); | 59 x_ = base::SaturatedAddition(x_, delta_x); |
60 y_ = SafeAdd(y_, delta_y); | 60 y_ = base::SaturatedAddition(y_, delta_y); |
61 } | 61 } |
62 | 62 |
63 void operator+=(const Vector2d& vector) { | 63 void operator+=(const Vector2d& vector) { |
64 x_ = SafeAdd(x_, vector.x()); | 64 x_ = base::SaturatedAddition(x_, vector.x()); |
65 y_ = SafeAdd(y_, vector.y()); | 65 y_ = base::SaturatedAddition(y_, vector.y()); |
66 } | 66 } |
67 | 67 |
68 void operator-=(const Vector2d& vector) { | 68 void operator-=(const Vector2d& vector) { |
69 x_ = SafeSubtract(x_, vector.x()); | 69 x_ = base::SaturatedSubtraction(x_, vector.x()); |
70 y_ = SafeSubtract(y_, vector.y()); | 70 y_ = base::SaturatedSubtraction(y_, vector.y()); |
71 } | 71 } |
72 | 72 |
73 void SetToMin(const Point& other); | 73 void SetToMin(const Point& other); |
74 void SetToMax(const Point& other); | 74 void SetToMax(const Point& other); |
75 | 75 |
76 bool IsOrigin() const { return x_ == 0 && y_ == 0; } | 76 bool IsOrigin() const { return x_ == 0 && y_ == 0; } |
77 | 77 |
78 Vector2d OffsetFromOrigin() const { return Vector2d(x_, y_); } | 78 Vector2d OffsetFromOrigin() const { return Vector2d(x_, y_); } |
79 | 79 |
80 // 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... |
109 return result; | 109 return result; |
110 } | 110 } |
111 | 111 |
112 inline Point operator-(const Point& lhs, const Vector2d& rhs) { | 112 inline Point operator-(const Point& lhs, const Vector2d& rhs) { |
113 Point result(lhs); | 113 Point result(lhs); |
114 result -= rhs; | 114 result -= rhs; |
115 return result; | 115 return result; |
116 } | 116 } |
117 | 117 |
118 inline Vector2d operator-(const Point& lhs, const Point& rhs) { | 118 inline Vector2d operator-(const Point& lhs, const Point& rhs) { |
119 return Vector2d(SafeSubtract(lhs.x(), rhs.x()), | 119 return Vector2d(base::SaturatedSubtraction(lhs.x(), rhs.x()), |
120 SafeSubtract(lhs.y(), rhs.y())); | 120 base::SaturatedSubtraction(lhs.y(), rhs.y())); |
121 } | 121 } |
122 | 122 |
123 inline Point PointAtOffsetFromOrigin(const Vector2d& offset_from_origin) { | 123 inline Point PointAtOffsetFromOrigin(const Vector2d& offset_from_origin) { |
124 return Point(offset_from_origin.x(), offset_from_origin.y()); | 124 return Point(offset_from_origin.x(), offset_from_origin.y()); |
125 } | 125 } |
126 | 126 |
127 // 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 |
128 // 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 |
129 // 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. |
130 void PrintTo(const Point& point, ::std::ostream* os); | 130 void PrintTo(const Point& point, ::std::ostream* os); |
131 | 131 |
132 // 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. |
133 GFX_EXPORT Point ScaleToCeiledPoint(const Point& point, | 133 GFX_EXPORT Point ScaleToCeiledPoint(const Point& point, |
134 float x_scale, | 134 float x_scale, |
135 float y_scale); | 135 float y_scale); |
136 GFX_EXPORT Point ScaleToCeiledPoint(const Point& point, float x_scale); | 136 GFX_EXPORT Point ScaleToCeiledPoint(const Point& point, float x_scale); |
137 GFX_EXPORT Point ScaleToFlooredPoint(const Point& point, | 137 GFX_EXPORT Point ScaleToFlooredPoint(const Point& point, |
138 float x_scale, | 138 float x_scale, |
139 float y_scale); | 139 float y_scale); |
140 GFX_EXPORT Point ScaleToFlooredPoint(const Point& point, float x_scale); | 140 GFX_EXPORT Point ScaleToFlooredPoint(const Point& point, float x_scale); |
141 GFX_EXPORT Point ScaleToRoundedPoint(const Point& point, | 141 GFX_EXPORT Point ScaleToRoundedPoint(const Point& point, |
142 float x_scale, | 142 float x_scale, |
143 float y_scale); | 143 float y_scale); |
144 GFX_EXPORT Point ScaleToRoundedPoint(const Point& point, float x_scale); | 144 GFX_EXPORT Point ScaleToRoundedPoint(const Point& point, float x_scale); |
145 | 145 |
146 } // namespace gfx | 146 } // namespace gfx |
147 | 147 |
148 #endif // UI_GFX_GEOMETRY_POINT_H_ | 148 #endif // UI_GFX_GEOMETRY_POINT_H_ |
OLD | NEW |