Revert "[Hotword] Implement IsHotwordHardwareAvailable() using device types."
[chromium-blink-merge.git] / ui / gfx / geometry / rect.h
blobb2e84fe16e1e344605fe00012951b83bdf5e2956
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
3 // found in the LICENSE file.
5 // Defines a simple integer rectangle class. The containment semantics
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.
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
10 // in the operations (such as Contains()) to complain in this case.
12 #ifndef UI_GFX_GEOMETRY_RECT_H_
13 #define UI_GFX_GEOMETRY_RECT_H_
15 #include <cmath>
16 #include <iosfwd>
17 #include <string>
19 #include "base/numerics/safe_conversions.h"
20 #include "ui/gfx/geometry/point.h"
21 #include "ui/gfx/geometry/rect_f.h"
22 #include "ui/gfx/geometry/size.h"
23 #include "ui/gfx/geometry/vector2d.h"
25 #if defined(OS_WIN)
26 typedef struct tagRECT RECT;
27 #elif defined(OS_IOS)
28 #include <CoreGraphics/CoreGraphics.h>
29 #elif defined(OS_MACOSX)
30 #include <ApplicationServices/ApplicationServices.h>
31 #endif
33 namespace gfx {
35 class Insets;
37 class GFX_EXPORT Rect {
38 public:
39 Rect() {}
40 Rect(int width, int height) : size_(width, height) {}
41 Rect(int x, int y, int width, int height)
42 : origin_(x, y), size_(width, height) {}
43 explicit Rect(const Size& size) : size_(size) {}
44 Rect(const Point& origin, const Size& size) : origin_(origin), size_(size) {}
46 #if defined(OS_WIN)
47 explicit Rect(const RECT& r);
48 #elif defined(OS_MACOSX)
49 explicit Rect(const CGRect& r);
50 #endif
52 ~Rect() {}
54 #if defined(OS_WIN)
55 // Construct an equivalent Win32 RECT object.
56 RECT ToRECT() const;
57 #elif defined(OS_MACOSX)
58 // Construct an equivalent CoreGraphics object.
59 CGRect ToCGRect() const;
60 #endif
62 operator RectF() const {
63 return RectF(static_cast<float>(x()), static_cast<float>(y()),
64 static_cast<float>(width()), static_cast<float>(height()));
67 int x() const { return origin_.x(); }
68 void set_x(int x) { origin_.set_x(x); }
70 int y() const { return origin_.y(); }
71 void set_y(int y) { origin_.set_y(y); }
73 int width() const { return size_.width(); }
74 void set_width(int width) { size_.set_width(width); }
76 int height() const { return size_.height(); }
77 void set_height(int height) { size_.set_height(height); }
79 const Point& origin() const { return origin_; }
80 void set_origin(const Point& origin) { origin_ = origin; }
82 const Size& size() const { return size_; }
83 void set_size(const Size& size) { size_ = size; }
85 int right() const { return x() + width(); }
86 int bottom() const { return y() + height(); }
88 Point top_right() const { return Point(right(), y()); }
89 Point bottom_left() const { return Point(x(), bottom()); }
90 Point bottom_right() const { return Point(right(), bottom()); }
92 Vector2d OffsetFromOrigin() const { return Vector2d(x(), y()); }
94 void SetRect(int x, int y, int width, int height) {
95 origin_.SetPoint(x, y);
96 size_.SetSize(width, height);
99 // Shrink the rectangle by a horizontal and vertical distance on all sides.
100 void Inset(int horizontal, int vertical) {
101 Inset(horizontal, vertical, horizontal, vertical);
104 // Shrink the rectangle by the given insets.
105 void Inset(const Insets& insets);
107 // Shrink the rectangle by the specified amount on each side.
108 void Inset(int left, int top, int right, int bottom);
110 // Move the rectangle by a horizontal and vertical distance.
111 void Offset(int horizontal, int vertical);
112 void Offset(const Vector2d& distance) { Offset(distance.x(), distance.y()); }
113 void operator+=(const Vector2d& offset);
114 void operator-=(const Vector2d& offset);
116 Insets InsetsFrom(const Rect& inner) const;
118 // Returns true if the area of the rectangle is zero.
119 bool IsEmpty() const { return size_.IsEmpty(); }
121 // A rect is less than another rect if its origin is less than
122 // the other rect's origin. If the origins are equal, then the
123 // shortest rect is less than the other. If the origin and the
124 // height are equal, then the narrowest rect is less than.
125 // This comparison is required to use Rects in sets, or sorted
126 // vectors.
127 bool operator<(const Rect& other) const;
129 // Returns true if the point identified by point_x and point_y falls inside
130 // this rectangle. The point (x, y) is inside the rectangle, but the
131 // point (x + width, y + height) is not.
132 bool Contains(int point_x, int point_y) const;
134 // Returns true if the specified point is contained by this rectangle.
135 bool Contains(const Point& point) const {
136 return Contains(point.x(), point.y());
139 // Returns true if this rectangle contains the specified rectangle.
140 bool Contains(const Rect& rect) const;
142 // Returns true if this rectangle intersects the specified rectangle.
143 // An empty rectangle doesn't intersect any rectangle.
144 bool Intersects(const Rect& rect) const;
146 // Computes the intersection of this rectangle with the given rectangle.
147 void Intersect(const Rect& rect);
149 // Computes the union of this rectangle with the given rectangle. The union
150 // is the smallest rectangle containing both rectangles.
151 void Union(const Rect& rect);
153 // Computes the rectangle resulting from subtracting |rect| from |*this|,
154 // i.e. the bounding rect of |Region(*this) - Region(rect)|.
155 void Subtract(const Rect& rect);
157 // Fits as much of the receiving rectangle into the supplied rectangle as
158 // possible, becoming the result. For example, if the receiver had
159 // a x-location of 2 and a width of 4, and the supplied rectangle had
160 // an x-location of 0 with a width of 5, the returned rectangle would have
161 // an x-location of 1 with a width of 4.
162 void AdjustToFit(const Rect& rect);
164 // Returns the center of this rectangle.
165 Point CenterPoint() const;
167 // Becomes a rectangle that has the same center point but with a size capped
168 // at given |size|.
169 void ClampToCenteredSize(const Size& size);
171 // Splits |this| in two halves, |left_half| and |right_half|.
172 void SplitVertically(Rect* left_half, Rect* right_half) const;
174 // Returns true if this rectangle shares an entire edge (i.e., same width or
175 // same height) with the given rectangle, and the rectangles do not overlap.
176 bool SharesEdgeWith(const Rect& rect) const;
178 // Returns the manhattan distance from the rect to the point. If the point is
179 // inside the rect, returns 0.
180 int ManhattanDistanceToPoint(const Point& point) const;
182 // Returns the manhattan distance between the contents of this rect and the
183 // contents of the given rect. That is, if the intersection of the two rects
184 // is non-empty then the function returns 0. If the rects share a side, it
185 // returns the smallest non-zero value appropriate for int.
186 int ManhattanInternalDistance(const Rect& rect) const;
188 std::string ToString() const;
190 private:
191 gfx::Point origin_;
192 gfx::Size size_;
195 inline bool operator==(const Rect& lhs, const Rect& rhs) {
196 return lhs.origin() == rhs.origin() && lhs.size() == rhs.size();
199 inline bool operator!=(const Rect& lhs, const Rect& rhs) {
200 return !(lhs == rhs);
203 GFX_EXPORT Rect operator+(const Rect& lhs, const Vector2d& rhs);
204 GFX_EXPORT Rect operator-(const Rect& lhs, const Vector2d& rhs);
206 inline Rect operator+(const Vector2d& lhs, const Rect& rhs) {
207 return rhs + lhs;
210 GFX_EXPORT Rect IntersectRects(const Rect& a, const Rect& b);
211 GFX_EXPORT Rect UnionRects(const Rect& a, const Rect& b);
212 GFX_EXPORT Rect SubtractRects(const Rect& a, const Rect& b);
214 // Constructs a rectangle with |p1| and |p2| as opposite corners.
216 // This could also be thought of as "the smallest rect that contains both
217 // points", except that we consider points on the right/bottom edges of the
218 // rect to be outside the rect. So technically one or both points will not be
219 // contained within the rect, because they will appear on one of these edges.
220 GFX_EXPORT Rect BoundingRect(const Point& p1, const Point& p2);
222 inline Rect ScaleToEnclosingRect(const Rect& rect,
223 float x_scale,
224 float y_scale) {
225 // These next functions cast instead of using e.g. ToFlooredInt() because we
226 // haven't checked to ensure that the clamping behavior of the helper
227 // functions doesn't degrade performance, and callers shouldn't be passing
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));
243 return Rect(x, y, r - x, b - y);
246 inline Rect ScaleToEnclosingRect(const Rect& rect, float scale) {
247 return ScaleToEnclosingRect(rect, scale, scale);
250 inline Rect ScaleToEnclosedRect(const Rect& rect,
251 float x_scale,
252 float y_scale) {
253 DCHECK(base::IsValueInRangeForNumericType<int>(
254 std::ceil(rect.x() * x_scale)));
255 DCHECK(base::IsValueInRangeForNumericType<int>(
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));
267 return Rect(x, y, r - x, b - y);
270 inline Rect ScaleToEnclosedRect(const Rect& rect, float scale) {
271 return ScaleToEnclosedRect(rect, scale, scale);
274 // This is declared here for use in gtest-based unit tests but is defined in
275 // the gfx_test_support target. Depend on that to use this in your unit test.
276 // This should not be used in production code - call ToString() instead.
277 void PrintTo(const Rect& rect, ::std::ostream* os);
279 } // namespace gfx
281 #endif // UI_GFX_GEOMETRY_RECT_H_