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