Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / ui / gfx / geometry / rect.h
blob9e0508d59eb8b6f009a8b35378df8a7df9b7149a
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/size.h"
22 #include "ui/gfx/geometry/vector2d.h"
24 #if defined(OS_WIN)
25 typedef struct tagRECT RECT;
26 #elif defined(OS_MACOSX)
27 typedef struct CGRect CGRect;
28 #endif
30 namespace gfx {
32 class Insets;
34 class GFX_EXPORT Rect {
35 public:
36 Rect() {}
37 Rect(int width, int height) : size_(width, height) {}
38 Rect(int x, int y, int width, int height)
39 : origin_(x, y), size_(width, height) {}
40 explicit Rect(const Size& size) : size_(size) {}
41 Rect(const Point& origin, const Size& size) : origin_(origin), size_(size) {}
43 #if defined(OS_WIN)
44 explicit Rect(const RECT& r);
45 #elif defined(OS_MACOSX)
46 explicit Rect(const CGRect& r);
47 #endif
49 ~Rect() {}
51 #if defined(OS_WIN)
52 // Construct an equivalent Win32 RECT object.
53 RECT ToRECT() const;
54 #elif defined(OS_MACOSX)
55 // Construct an equivalent CoreGraphics object.
56 CGRect ToCGRect() const;
57 #endif
59 int x() const { return origin_.x(); }
60 void set_x(int x) { origin_.set_x(x); }
62 int y() const { return origin_.y(); }
63 void set_y(int y) { origin_.set_y(y); }
65 int width() const { return size_.width(); }
66 void set_width(int width) { size_.set_width(width); }
68 int height() const { return size_.height(); }
69 void set_height(int height) { size_.set_height(height); }
71 const Point& origin() const { return origin_; }
72 void set_origin(const Point& origin) { origin_ = origin; }
74 const Size& size() const { return size_; }
75 void set_size(const Size& size) { size_ = size; }
77 int right() const { return x() + width(); }
78 int bottom() const { return y() + height(); }
80 Point top_right() const { return Point(right(), y()); }
81 Point bottom_left() const { return Point(x(), bottom()); }
82 Point bottom_right() const { return Point(right(), bottom()); }
84 Vector2d OffsetFromOrigin() const { return Vector2d(x(), y()); }
86 void SetRect(int x, int y, int width, int height) {
87 origin_.SetPoint(x, y);
88 size_.SetSize(width, height);
91 // Shrink the rectangle by a horizontal and vertical distance on all sides.
92 void Inset(int horizontal, int vertical) {
93 Inset(horizontal, vertical, horizontal, vertical);
96 // Shrink the rectangle by the given insets.
97 void Inset(const Insets& insets);
99 // Shrink the rectangle by the specified amount on each side.
100 void Inset(int left, int top, int right, int bottom);
102 // Move the rectangle by a horizontal and vertical distance.
103 void Offset(int horizontal, int vertical);
104 void Offset(const Vector2d& distance) { Offset(distance.x(), distance.y()); }
105 void operator+=(const Vector2d& offset);
106 void operator-=(const Vector2d& offset);
108 Insets InsetsFrom(const Rect& inner) const;
110 // Returns true if the area of the rectangle is zero.
111 bool IsEmpty() const { return size_.IsEmpty(); }
113 // A rect is less than another rect if its origin is less than
114 // the other rect's origin. If the origins are equal, then the
115 // shortest rect is less than the other. If the origin and the
116 // height are equal, then the narrowest rect is less than.
117 // This comparison is required to use Rects in sets, or sorted
118 // vectors.
119 bool operator<(const Rect& other) const;
121 // Returns true if the point identified by point_x and point_y falls inside
122 // this rectangle. The point (x, y) is inside the rectangle, but the
123 // point (x + width, y + height) is not.
124 bool Contains(int point_x, int point_y) const;
126 // Returns true if the specified point is contained by this rectangle.
127 bool Contains(const Point& point) const {
128 return Contains(point.x(), point.y());
131 // Returns true if this rectangle contains the specified rectangle.
132 bool Contains(const Rect& rect) const;
134 // Returns true if this rectangle intersects the specified rectangle.
135 // An empty rectangle doesn't intersect any rectangle.
136 bool Intersects(const Rect& rect) const;
138 // Computes the intersection of this rectangle with the given rectangle.
139 void Intersect(const Rect& rect);
141 // Computes the union of this rectangle with the given rectangle. The union
142 // is the smallest rectangle containing both rectangles.
143 void Union(const Rect& rect);
145 // Computes the rectangle resulting from subtracting |rect| from |*this|,
146 // i.e. the bounding rect of |Region(*this) - Region(rect)|.
147 void Subtract(const Rect& rect);
149 // Fits as much of the receiving rectangle into the supplied rectangle as
150 // possible, becoming the result. For example, if the receiver had
151 // a x-location of 2 and a width of 4, and the supplied rectangle had
152 // an x-location of 0 with a width of 5, the returned rectangle would have
153 // an x-location of 1 with a width of 4.
154 void AdjustToFit(const Rect& rect);
156 // Returns the center of this rectangle.
157 Point CenterPoint() const;
159 // Becomes a rectangle that has the same center point but with a size capped
160 // at given |size|.
161 void ClampToCenteredSize(const Size& size);
163 // Splits |this| in two halves, |left_half| and |right_half|.
164 void SplitVertically(Rect* left_half, Rect* right_half) const;
166 // Returns true if this rectangle shares an entire edge (i.e., same width or
167 // same height) with the given rectangle, and the rectangles do not overlap.
168 bool SharesEdgeWith(const Rect& rect) const;
170 // Returns the manhattan distance from the rect to the point. If the point is
171 // inside the rect, returns 0.
172 int ManhattanDistanceToPoint(const Point& point) const;
174 // Returns the manhattan distance between the contents of this rect and the
175 // contents of the given rect. That is, if the intersection of the two rects
176 // is non-empty then the function returns 0. If the rects share a side, it
177 // returns the smallest non-zero value appropriate for int.
178 int ManhattanInternalDistance(const Rect& rect) const;
180 std::string ToString() const;
182 private:
183 gfx::Point origin_;
184 gfx::Size size_;
187 inline bool operator==(const Rect& lhs, const Rect& rhs) {
188 return lhs.origin() == rhs.origin() && lhs.size() == rhs.size();
191 inline bool operator!=(const Rect& lhs, const Rect& rhs) {
192 return !(lhs == rhs);
195 GFX_EXPORT Rect operator+(const Rect& lhs, const Vector2d& rhs);
196 GFX_EXPORT Rect operator-(const Rect& lhs, const Vector2d& rhs);
198 inline Rect operator+(const Vector2d& lhs, const Rect& rhs) {
199 return rhs + lhs;
202 GFX_EXPORT Rect IntersectRects(const Rect& a, const Rect& b);
203 GFX_EXPORT Rect UnionRects(const Rect& a, const Rect& b);
204 GFX_EXPORT Rect SubtractRects(const Rect& a, const Rect& b);
206 // Constructs a rectangle with |p1| and |p2| as opposite corners.
208 // This could also be thought of as "the smallest rect that contains both
209 // points", except that we consider points on the right/bottom edges of the
210 // rect to be outside the rect. So technically one or both points will not be
211 // contained within the rect, because they will appear on one of these edges.
212 GFX_EXPORT Rect BoundingRect(const Point& p1, const Point& p2);
214 inline Rect ScaleToEnclosingRect(const Rect& rect,
215 float x_scale,
216 float y_scale) {
217 if (x_scale == 1.f && y_scale == 1.f)
218 return rect;
219 // These next functions cast instead of using e.g. ToFlooredInt() because we
220 // haven't checked to ensure that the clamping behavior of the helper
221 // functions doesn't degrade performance, and callers shouldn't be passing
222 // values that cause overflow anyway.
223 DCHECK(base::IsValueInRangeForNumericType<int>(
224 std::floor(rect.x() * x_scale)));
225 DCHECK(base::IsValueInRangeForNumericType<int>(
226 std::floor(rect.y() * y_scale)));
227 DCHECK(base::IsValueInRangeForNumericType<int>(
228 std::ceil(rect.right() * x_scale)));
229 DCHECK(base::IsValueInRangeForNumericType<int>(
230 std::ceil(rect.bottom() * y_scale)));
231 int x = static_cast<int>(std::floor(rect.x() * x_scale));
232 int y = static_cast<int>(std::floor(rect.y() * y_scale));
233 int r = rect.width() == 0 ?
234 x : static_cast<int>(std::ceil(rect.right() * x_scale));
235 int b = rect.height() == 0 ?
236 y : static_cast<int>(std::ceil(rect.bottom() * y_scale));
237 return Rect(x, y, r - x, b - y);
240 inline Rect ScaleToEnclosingRect(const Rect& rect, float scale) {
241 return ScaleToEnclosingRect(rect, scale, scale);
244 inline Rect ScaleToEnclosedRect(const Rect& rect,
245 float x_scale,
246 float y_scale) {
247 if (x_scale == 1.f && y_scale == 1.f)
248 return rect;
249 DCHECK(base::IsValueInRangeForNumericType<int>(
250 std::ceil(rect.x() * x_scale)));
251 DCHECK(base::IsValueInRangeForNumericType<int>(
252 std::ceil(rect.y() * y_scale)));
253 DCHECK(base::IsValueInRangeForNumericType<int>(
254 std::floor(rect.right() * x_scale)));
255 DCHECK(base::IsValueInRangeForNumericType<int>(
256 std::floor(rect.bottom() * y_scale)));
257 int x = static_cast<int>(std::ceil(rect.x() * x_scale));
258 int y = static_cast<int>(std::ceil(rect.y() * y_scale));
259 int r = rect.width() == 0 ?
260 x : static_cast<int>(std::floor(rect.right() * x_scale));
261 int b = rect.height() == 0 ?
262 y : static_cast<int>(std::floor(rect.bottom() * y_scale));
263 return Rect(x, y, r - x, b - y);
266 inline Rect ScaleToEnclosedRect(const Rect& rect, float scale) {
267 return ScaleToEnclosedRect(rect, scale, scale);
270 // This is declared here for use in gtest-based unit tests but is defined in
271 // the gfx_test_support target. Depend on that to use this in your unit test.
272 // This should not be used in production code - call ToString() instead.
273 void PrintTo(const Rect& rect, ::std::ostream* os);
275 } // namespace gfx
277 #endif // UI_GFX_GEOMETRY_RECT_H_