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) || defined(OS_IOS)
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 GEOMETRY_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) || defined(OS_IOS)
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 
left_center()73   constexpr PointF left_center() const {
74     return PointF(x(), y() + height() / 2);
75   }
top_center()76   constexpr PointF top_center() const { return PointF(x() + width() / 2, y()); }
right_center()77   constexpr PointF right_center() const {
78     return PointF(right(), y() + height() / 2);
79   }
bottom_center()80   constexpr PointF bottom_center() const {
81     return PointF(x() + width() / 2, bottom());
82   }
83 
OffsetFromOrigin()84   Vector2dF OffsetFromOrigin() const { return Vector2dF(x(), y()); }
85 
SetRect(float x,float y,float width,float height)86   void SetRect(float x, float y, float width, float height) {
87     origin_.SetPoint(x, y);
88     size_.SetSize(width, height);
89   }
90 
91   // Shrink the rectangle by a horizontal and vertical distance on all sides.
Inset(float horizontal,float vertical)92   void Inset(float horizontal, float vertical) {
93     Inset(horizontal, vertical, horizontal, vertical);
94   }
95 
96   // Shrink the rectangle by the given insets.
97   void Inset(const InsetsF& insets);
98 
99   // Shrink the rectangle by the specified amount on each side.
100   void Inset(float left, float top, float right, float bottom);
101 
102   // Move the rectangle by a horizontal and vertical distance.
103   void Offset(float horizontal, float vertical);
Offset(const Vector2dF & distance)104   void Offset(const Vector2dF& distance) { Offset(distance.x(), distance.y()); }
105   void operator+=(const Vector2dF& offset);
106   void operator-=(const Vector2dF& offset);
107 
108   InsetsF InsetsFrom(const RectF& inner) const;
109 
110   // Returns true if the area of the rectangle is zero.
IsEmpty()111   bool IsEmpty() const { return size_.IsEmpty(); }
112 
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 RectF& other) const;
120 
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(float point_x, float point_y) const;
125 
126   // Returns true if the specified point is contained by this rectangle.
Contains(const PointF & point)127   bool Contains(const PointF& point) const {
128     return Contains(point.x(), point.y());
129   }
130 
131   // Returns true if this rectangle contains the specified rectangle.
132   bool Contains(const RectF& rect) const;
133 
134   // Returns true if this rectangle intersects the specified rectangle.
135   // An empty rectangle doesn't intersect any rectangle.
136   bool Intersects(const RectF& rect) const;
137 
138   // Computes the intersection of this rectangle with the given rectangle.
139   void Intersect(const RectF& rect);
140 
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 RectF& rect);
144 
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 RectF& rect);
148 
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 RectF& rect);
155 
156   // Returns the center of this rectangle.
157   PointF CenterPoint() const;
158 
159   // Becomes a rectangle that has the same center point but with a size capped
160   // at given |size|.
161   void ClampToCenteredSize(const SizeF& size);
162 
163   // Transpose x and y axis.
164   void Transpose();
165 
166   // Splits |this| in two halves, |left_half| and |right_half|.
167   void SplitVertically(RectF* left_half, RectF* right_half) const;
168 
169   // Returns true if this rectangle shares an entire edge (i.e., same width or
170   // same height) with the given rectangle, and the rectangles do not overlap.
171   bool SharesEdgeWith(const RectF& rect) const;
172 
173   // Returns the manhattan distance from the rect to the point. If the point is
174   // inside the rect, returns 0.
175   float ManhattanDistanceToPoint(const PointF& point) const;
176 
177   // Returns the manhattan distance between the contents of this rect and the
178   // contents of the given rect. That is, if the intersection of the two rects
179   // is non-empty then the function returns 0. If the rects share a side, it
180   // returns the smallest non-zero value appropriate for float.
181   float ManhattanInternalDistance(const RectF& rect) const;
182 
183   // Scales the rectangle by |scale|.
Scale(float scale)184   void Scale(float scale) {
185     Scale(scale, scale);
186   }
187 
Scale(float x_scale,float y_scale)188   void Scale(float x_scale, float y_scale) {
189     set_origin(ScalePoint(origin(), x_scale, y_scale));
190     set_size(ScaleSize(size(), x_scale, y_scale));
191   }
192 
193   // This method reports if the RectF can be safely converted to an integer
194   // Rect. When it is false, some dimension of the RectF is outside the bounds
195   // of what an integer can represent, and converting it to a Rect will require
196   // clamping.
197   bool IsExpressibleAsRect() const;
198 
199   std::string ToString() const;
200 
201  private:
202   PointF origin_;
203   SizeF size_;
204 };
205 
206 inline bool operator==(const RectF& lhs, const RectF& rhs) {
207   return lhs.origin() == rhs.origin() && lhs.size() == rhs.size();
208 }
209 
210 inline bool operator!=(const RectF& lhs, const RectF& rhs) {
211   return !(lhs == rhs);
212 }
213 
214 inline RectF operator+(const RectF& lhs, const Vector2dF& rhs) {
215   return RectF(lhs.x() + rhs.x(), lhs.y() + rhs.y(),
216       lhs.width(), lhs.height());
217 }
218 
219 inline RectF operator-(const RectF& lhs, const Vector2dF& rhs) {
220   return RectF(lhs.x() - rhs.x(), lhs.y() - rhs.y(),
221       lhs.width(), lhs.height());
222 }
223 
224 inline RectF operator+(const Vector2dF& lhs, const RectF& rhs) {
225   return rhs + lhs;
226 }
227 
228 GEOMETRY_EXPORT RectF IntersectRects(const RectF& a, const RectF& b);
229 GEOMETRY_EXPORT RectF UnionRects(const RectF& a, const RectF& b);
230 GEOMETRY_EXPORT RectF SubtractRects(const RectF& a, const RectF& b);
231 
ScaleRect(const RectF & r,float x_scale,float y_scale)232 inline RectF ScaleRect(const RectF& r, float x_scale, float y_scale) {
233   return RectF(r.x() * x_scale, r.y() * y_scale,
234        r.width() * x_scale, r.height() * y_scale);
235 }
236 
ScaleRect(const RectF & r,float scale)237 inline RectF ScaleRect(const RectF& r, float scale) {
238   return ScaleRect(r, scale, scale);
239 }
240 
241 // Constructs a rectangle with |p1| and |p2| as opposite corners.
242 //
243 // This could also be thought of as "the smallest rect that contains both
244 // points", except that we consider points on the right/bottom edges of the
245 // rect to be outside the rect.  So technically one or both points will not be
246 // contained within the rect, because they will appear on one of these edges.
247 GEOMETRY_EXPORT RectF BoundingRect(const PointF& p1, const PointF& p2);
248 
249 // This is declared here for use in gtest-based unit tests but is defined in
250 // the //ui/gfx:test_support target. Depend on that to use this in your unit
251 // test. This should not be used in production code - call ToString() instead.
252 void PrintTo(const RectF& rect, ::std::ostream* os);
253 
254 }  // namespace gfx
255 
256 #endif  // UI_GFX_GEOMETRY_RECT_F_H_
257