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.
4
5 #ifndef UI_GFX_GEOMETRY_RECT_F_H_
6 #define UI_GFX_GEOMETRY_RECT_F_H_
7
8 #include <iosfwd>
9 #include <string>
10
11 #include "build/build_config.h"
12 #include "ui/gfx/geometry/point_f.h"
13 #include "ui/gfx/geometry/rect.h"
14 #include "ui/gfx/geometry/size_f.h"
15 #include "ui/gfx/geometry/vector2d_f.h"
16
17 #if defined(OS_MACOSX)
18 typedef struct CGRect CGRect;
19 #endif
20
21 namespace gfx {
22
23 class InsetsF;
24
25 // A floating version of gfx::Rect.
26 class GFX_EXPORT RectF {
27 public:
28 constexpr RectF() = default;
RectF(float width,float height)29 constexpr RectF(float width, float height) : size_(width, height) {}
RectF(float x,float y,float width,float height)30 constexpr RectF(float x, float y, float width, float height)
31 : origin_(x, y), size_(width, height) {}
RectF(const SizeF & size)32 constexpr explicit RectF(const SizeF& size) : size_(size) {}
RectF(const PointF & origin,const SizeF & size)33 constexpr RectF(const PointF& origin, const SizeF& size)
34 : origin_(origin), size_(size) {}
35
RectF(const Rect & r)36 constexpr explicit RectF(const Rect& r)
37 : RectF(static_cast<float>(r.x()),
38 static_cast<float>(r.y()),
39 static_cast<float>(r.width()),
40 static_cast<float>(r.height())) {}
41
42 #if defined(OS_MACOSX)
43 explicit RectF(const CGRect& r);
44 // Construct an equivalent CoreGraphics object.
45 CGRect ToCGRect() const;
46 #endif
47
x()48 constexpr float x() const { return origin_.x(); }
set_x(float x)49 void set_x(float x) { origin_.set_x(x); }
50
y()51 constexpr float y() const { return origin_.y(); }
set_y(float y)52 void set_y(float y) { origin_.set_y(y); }
53
width()54 constexpr float width() const { return size_.width(); }
set_width(float width)55 void set_width(float width) { size_.set_width(width); }
56
height()57 constexpr float height() const { return size_.height(); }
set_height(float height)58 void set_height(float height) { size_.set_height(height); }
59
origin()60 constexpr const PointF& origin() const { return origin_; }
set_origin(const PointF & origin)61 void set_origin(const PointF& origin) { origin_ = origin; }
62
size()63 constexpr const SizeF& size() const { return size_; }
set_size(const SizeF & size)64 void set_size(const SizeF& size) { size_ = size; }
65
right()66 constexpr float right() const { return x() + width(); }
bottom()67 constexpr float bottom() const { return y() + height(); }
68
top_right()69 constexpr PointF top_right() const { return PointF(right(), y()); }
bottom_left()70 constexpr PointF bottom_left() const { return PointF(x(), bottom()); }
bottom_right()71 constexpr PointF bottom_right() const { return PointF(right(), bottom()); }
72
OffsetFromOrigin()73 Vector2dF OffsetFromOrigin() const { return Vector2dF(x(), y()); }
74
SetRect(float x,float y,float width,float height)75 void SetRect(float x, float y, float width, float height) {
76 origin_.SetPoint(x, y);
77 size_.SetSize(width, height);
78 }
79
80 // Shrink the rectangle by a horizontal and vertical distance on all sides.
Inset(float horizontal,float vertical)81 void Inset(float horizontal, float vertical) {
82 Inset(horizontal, vertical, horizontal, vertical);
83 }
84
85 // Shrink the rectangle by the given insets.
86 void Inset(const InsetsF& insets);
87
88 // Shrink the rectangle by the specified amount on each side.
89 void Inset(float left, float top, float right, float bottom);
90
91 // Move the rectangle by a horizontal and vertical distance.
92 void Offset(float horizontal, float vertical);
Offset(const Vector2dF & distance)93 void Offset(const Vector2dF& distance) { Offset(distance.x(), distance.y()); }
94 void operator+=(const Vector2dF& offset);
95 void operator-=(const Vector2dF& offset);
96
97 InsetsF InsetsFrom(const RectF& inner) const;
98
99 // Returns true if the area of the rectangle is zero.
IsEmpty()100 bool IsEmpty() const { return size_.IsEmpty(); }
101
102 // A rect is less than another rect if its origin is less than
103 // the other rect's origin. If the origins are equal, then the
104 // shortest rect is less than the other. If the origin and the
105 // height are equal, then the narrowest rect is less than.
106 // This comparison is required to use Rects in sets, or sorted
107 // vectors.
108 bool operator<(const RectF& other) const;
109
110 // Returns true if the point identified by point_x and point_y falls inside
111 // this rectangle. The point (x, y) is inside the rectangle, but the
112 // point (x + width, y + height) is not.
113 bool Contains(float point_x, float point_y) const;
114
115 // Returns true if the specified point is contained by this rectangle.
Contains(const PointF & point)116 bool Contains(const PointF& point) const {
117 return Contains(point.x(), point.y());
118 }
119
120 // Returns true if this rectangle contains the specified rectangle.
121 bool Contains(const RectF& rect) const;
122
123 // Returns true if this rectangle intersects the specified rectangle.
124 // An empty rectangle doesn't intersect any rectangle.
125 bool Intersects(const RectF& rect) const;
126
127 // Computes the intersection of this rectangle with the given rectangle.
128 void Intersect(const RectF& rect);
129
130 // Computes the union of this rectangle with the given rectangle. The union
131 // is the smallest rectangle containing both rectangles.
132 void Union(const RectF& rect);
133
134 // Computes the rectangle resulting from subtracting |rect| from |*this|,
135 // i.e. the bounding rect of |Region(*this) - Region(rect)|.
136 void Subtract(const RectF& rect);
137
138 // Fits as much of the receiving rectangle into the supplied rectangle as
139 // possible, becoming the result. For example, if the receiver had
140 // a x-location of 2 and a width of 4, and the supplied rectangle had
141 // an x-location of 0 with a width of 5, the returned rectangle would have
142 // an x-location of 1 with a width of 4.
143 void AdjustToFit(const RectF& rect);
144
145 // Returns the center of this rectangle.
146 PointF CenterPoint() const;
147
148 // Becomes a rectangle that has the same center point but with a size capped
149 // at given |size|.
150 void ClampToCenteredSize(const SizeF& size);
151
152 // Splits |this| in two halves, |left_half| and |right_half|.
153 void SplitVertically(RectF* left_half, RectF* right_half) const;
154
155 // Returns true if this rectangle shares an entire edge (i.e., same width or
156 // same height) with the given rectangle, and the rectangles do not overlap.
157 bool SharesEdgeWith(const RectF& rect) const;
158
159 // Returns the manhattan distance from the rect to the point. If the point is
160 // inside the rect, returns 0.
161 float ManhattanDistanceToPoint(const PointF& point) const;
162
163 // Returns the manhattan distance between the contents of this rect and the
164 // contents of the given rect. That is, if the intersection of the two rects
165 // is non-empty then the function returns 0. If the rects share a side, it
166 // returns the smallest non-zero value appropriate for float.
167 float ManhattanInternalDistance(const RectF& rect) const;
168
169 // Scales the rectangle by |scale|.
Scale(float scale)170 void Scale(float scale) {
171 Scale(scale, scale);
172 }
173
Scale(float x_scale,float y_scale)174 void Scale(float x_scale, float y_scale) {
175 set_origin(ScalePoint(origin(), x_scale, y_scale));
176 set_size(ScaleSize(size(), x_scale, y_scale));
177 }
178
179 // This method reports if the RectF can be safely converted to an integer
180 // Rect. When it is false, some dimension of the RectF is outside the bounds
181 // of what an integer can represent, and converting it to a Rect will require
182 // clamping.
183 bool IsExpressibleAsRect() const;
184
185 std::string ToString() const;
186
187 private:
188 PointF origin_;
189 SizeF size_;
190 };
191
192 inline bool operator==(const RectF& lhs, const RectF& rhs) {
193 return lhs.origin() == rhs.origin() && lhs.size() == rhs.size();
194 }
195
196 inline bool operator!=(const RectF& lhs, const RectF& rhs) {
197 return !(lhs == rhs);
198 }
199
200 inline RectF operator+(const RectF& lhs, const Vector2dF& rhs) {
201 return RectF(lhs.x() + rhs.x(), lhs.y() + rhs.y(),
202 lhs.width(), lhs.height());
203 }
204
205 inline RectF operator-(const RectF& lhs, const Vector2dF& rhs) {
206 return RectF(lhs.x() - rhs.x(), lhs.y() - rhs.y(),
207 lhs.width(), lhs.height());
208 }
209
210 inline RectF operator+(const Vector2dF& lhs, const RectF& rhs) {
211 return rhs + lhs;
212 }
213
214 GFX_EXPORT RectF IntersectRects(const RectF& a, const RectF& b);
215 GFX_EXPORT RectF UnionRects(const RectF& a, const RectF& b);
216 GFX_EXPORT RectF SubtractRects(const RectF& a, const RectF& b);
217
ScaleRect(const RectF & r,float x_scale,float y_scale)218 inline RectF ScaleRect(const RectF& r, float x_scale, float y_scale) {
219 return RectF(r.x() * x_scale, r.y() * y_scale,
220 r.width() * x_scale, r.height() * y_scale);
221 }
222
ScaleRect(const RectF & r,float scale)223 inline RectF ScaleRect(const RectF& r, float scale) {
224 return ScaleRect(r, scale, scale);
225 }
226
227 // Constructs a rectangle with |p1| and |p2| as opposite corners.
228 //
229 // This could also be thought of as "the smallest rect that contains both
230 // points", except that we consider points on the right/bottom edges of the
231 // rect to be outside the rect. So technically one or both points will not be
232 // contained within the rect, because they will appear on one of these edges.
233 GFX_EXPORT RectF BoundingRect(const PointF& p1, const PointF& p2);
234
235 // This is declared here for use in gtest-based unit tests but is defined in
236 // the //ui/gfx:test_support target. Depend on that to use this in your unit
237 // test. This should not be used in production code - call ToString() instead.
238 void PrintTo(const RectF& rect, ::std::ostream* os);
239
240 } // namespace gfx
241
242 #endif // UI_GFX_GEOMETRY_RECT_F_H_
243