Update broken references to image assets
[chromium-blink-merge.git] / ui / gfx / geometry / rect.h
blob4ce85a0cfef9a25bbba39d9cb840d076a5f2eab4
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_MACOSX)
28 typedef struct CGRect CGRect;
29 #endif
31 namespace gfx {
33 class Insets;
35 class GFX_EXPORT Rect {
36 public:
37 Rect() {}
38 Rect(int width, int height) : size_(width, height) {}
39 Rect(int x, int y, int width, int height)
40 : origin_(x, y), size_(width, height) {}
41 explicit Rect(const Size& size) : size_(size) {}
42 Rect(const Point& origin, const Size& size) : origin_(origin), size_(size) {}
44 #if defined(OS_WIN)
45 explicit Rect(const RECT& r);
46 #elif defined(OS_MACOSX)
47 explicit Rect(const CGRect& r);
48 #endif
50 ~Rect() {}
52 #if defined(OS_WIN)
53 // Construct an equivalent Win32 RECT object.
54 RECT ToRECT() const;
55 #elif defined(OS_MACOSX)
56 // Construct an equivalent CoreGraphics object.
57 CGRect ToCGRect() const;
58 #endif
60 operator RectF() const {
61 return RectF(static_cast<float>(x()), static_cast<float>(y()),
62 static_cast<float>(width()), static_cast<float>(height()));
65 int x() const { return origin_.x(); }
66 void set_x(int x) { origin_.set_x(x); }
68 int y() const { return origin_.y(); }
69 void set_y(int y) { origin_.set_y(y); }
71 int width() const { return size_.width(); }
72 void set_width(int width) { size_.set_width(width); }
74 int height() const { return size_.height(); }
75 void set_height(int height) { size_.set_height(height); }
77 const Point& origin() const { return origin_; }
78 void set_origin(const Point& origin) { origin_ = origin; }
80 const Size& size() const { return size_; }
81 void set_size(const Size& size) { size_ = size; }
83 int right() const { return x() + width(); }
84 int bottom() const { return y() + height(); }
86 Point top_right() const { return Point(right(), y()); }
87 Point bottom_left() const { return Point(x(), bottom()); }
88 Point bottom_right() const { return Point(right(), bottom()); }
90 Vector2d OffsetFromOrigin() const { return Vector2d(x(), y()); }
92 void SetRect(int x, int y, int width, int height) {
93 origin_.SetPoint(x, y);
94 size_.SetSize(width, height);
97 // Shrink the rectangle by a horizontal and vertical distance on all sides.
98 void Inset(int horizontal, int vertical) {
99 Inset(horizontal, vertical, horizontal, vertical);
102 // Shrink the rectangle by the given insets.
103 void Inset(const Insets& insets);
105 // Shrink the rectangle by the specified amount on each side.
106 void Inset(int left, int top, int right, int bottom);
108 // Move the rectangle by a horizontal and vertical distance.
109 void Offset(int horizontal, int vertical);
110 void Offset(const Vector2d& distance) { Offset(distance.x(), distance.y()); }
111 void operator+=(const Vector2d& offset);
112 void operator-=(const Vector2d& offset);
114 Insets InsetsFrom(const Rect& inner) const;
116 // Returns true if the area of the rectangle is zero.
117 bool IsEmpty() const { return size_.IsEmpty(); }
119 // A rect is less than another rect if its origin is less than
120 // the other rect's origin. If the origins are equal, then the
121 // shortest rect is less than the other. If the origin and the
122 // height are equal, then the narrowest rect is less than.
123 // This comparison is required to use Rects in sets, or sorted
124 // vectors.
125 bool operator<(const Rect& other) const;
127 // Returns true if the point identified by point_x and point_y falls inside
128 // this rectangle. The point (x, y) is inside the rectangle, but the
129 // point (x + width, y + height) is not.
130 bool Contains(int point_x, int point_y) const;
132 // Returns true if the specified point is contained by this rectangle.
133 bool Contains(const Point& point) const {
134 return Contains(point.x(), point.y());
137 // Returns true if this rectangle contains the specified rectangle.
138 bool Contains(const Rect& rect) const;
140 // Returns true if this rectangle intersects the specified rectangle.
141 // An empty rectangle doesn't intersect any rectangle.
142 bool Intersects(const Rect& rect) const;
144 // Computes the intersection of this rectangle with the given rectangle.
145 void Intersect(const Rect& rect);
147 // Computes the union of this rectangle with the given rectangle. The union
148 // is the smallest rectangle containing both rectangles.
149 void Union(const Rect& rect);
151 // Computes the rectangle resulting from subtracting |rect| from |*this|,
152 // i.e. the bounding rect of |Region(*this) - Region(rect)|.
153 void Subtract(const Rect& rect);
155 // Fits as much of the receiving rectangle into the supplied rectangle as
156 // possible, becoming the result. For example, if the receiver had
157 // a x-location of 2 and a width of 4, and the supplied rectangle had
158 // an x-location of 0 with a width of 5, the returned rectangle would have
159 // an x-location of 1 with a width of 4.
160 void AdjustToFit(const Rect& rect);
162 // Returns the center of this rectangle.
163 Point CenterPoint() const;
165 // Becomes a rectangle that has the same center point but with a size capped
166 // at given |size|.
167 void ClampToCenteredSize(const Size& size);
169 // Splits |this| in two halves, |left_half| and |right_half|.
170 void SplitVertically(Rect* left_half, Rect* right_half) const;
172 // Returns true if this rectangle shares an entire edge (i.e., same width or
173 // same height) with the given rectangle, and the rectangles do not overlap.
174 bool SharesEdgeWith(const Rect& rect) const;
176 // Returns the manhattan distance from the rect to the point. If the point is
177 // inside the rect, returns 0.
178 int ManhattanDistanceToPoint(const Point& point) const;
180 // Returns the manhattan distance between the contents of this rect and the
181 // contents of the given rect. That is, if the intersection of the two rects
182 // is non-empty then the function returns 0. If the rects share a side, it
183 // returns the smallest non-zero value appropriate for int.
184 int ManhattanInternalDistance(const Rect& rect) const;
186 std::string ToString() const;
188 private:
189 gfx::Point origin_;
190 gfx::Size size_;
193 inline bool operator==(const Rect& lhs, const Rect& rhs) {
194 return lhs.origin() == rhs.origin() && lhs.size() == rhs.size();
197 inline bool operator!=(const Rect& lhs, const Rect& rhs) {
198 return !(lhs == rhs);
201 GFX_EXPORT Rect operator+(const Rect& lhs, const Vector2d& rhs);
202 GFX_EXPORT Rect operator-(const Rect& lhs, const Vector2d& rhs);
204 inline Rect operator+(const Vector2d& lhs, const Rect& rhs) {
205 return rhs + lhs;
208 GFX_EXPORT Rect IntersectRects(const Rect& a, const Rect& b);
209 GFX_EXPORT Rect UnionRects(const Rect& a, const Rect& b);
210 GFX_EXPORT Rect SubtractRects(const Rect& a, const Rect& b);
212 // Constructs a rectangle with |p1| and |p2| as opposite corners.
214 // 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
216 // 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.
218 GFX_EXPORT Rect BoundingRect(const Point& p1, const Point& p2);
220 inline Rect ScaleToEnclosingRect(const Rect& rect,
221 float x_scale,
222 float y_scale) {
223 if (x_scale == 1.f && y_scale == 1.f)
224 return rect;
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 if (x_scale == 1.f && y_scale == 1.f)
254 return rect;
255 DCHECK(base::IsValueInRangeForNumericType<int>(
256 std::ceil(rect.x() * x_scale)));
257 DCHECK(base::IsValueInRangeForNumericType<int>(
258 std::ceil(rect.y() * y_scale)));
259 DCHECK(base::IsValueInRangeForNumericType<int>(
260 std::floor(rect.right() * x_scale)));
261 DCHECK(base::IsValueInRangeForNumericType<int>(
262 std::floor(rect.bottom() * y_scale)));
263 int x = static_cast<int>(std::ceil(rect.x() * x_scale));
264 int y = static_cast<int>(std::ceil(rect.y() * y_scale));
265 int r = rect.width() == 0 ?
266 x : static_cast<int>(std::floor(rect.right() * x_scale));
267 int b = rect.height() == 0 ?
268 y : static_cast<int>(std::floor(rect.bottom() * y_scale));
269 return Rect(x, y, r - x, b - y);
272 inline Rect ScaleToEnclosedRect(const Rect& rect, float scale) {
273 return ScaleToEnclosedRect(rect, scale, scale);
276 // This is declared here for use in gtest-based unit tests but is defined in
277 // the gfx_test_support target. Depend on that to use this in your unit test.
278 // This should not be used in production code - call ToString() instead.
279 void PrintTo(const Rect& rect, ::std::ostream* os);
281 } // namespace gfx
283 #endif // UI_GFX_GEOMETRY_RECT_H_