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