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