Flutter Engine
The Flutter Engine
rect_f.h
Go to the documentation of this file.
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
12#include "point_f.h"
13#include "rect.h"
14#include "size_f.h"
15#include "vector2d_f.h"
16
17#if defined(OS_APPLE)
18typedef struct CGRect CGRect;
19#endif
20
21namespace gfx {
22
23class InsetsF;
24
25// A floating version of gfx::Rect.
27 public:
28 constexpr RectF() = default;
29 constexpr RectF(float width, float height) : size_(width, height) {}
30 constexpr RectF(float x, float y, float width, float height)
31 : origin_(x, y), size_(width, height) {}
32 constexpr explicit RectF(const SizeF& size) : size_(size) {}
33 constexpr RectF(const PointF& origin, const SizeF& size) : origin_(origin), size_(size) {}
34
35 constexpr explicit RectF(const Rect& r)
36 : RectF(static_cast<float>(r.x()),
37 static_cast<float>(r.y()),
38 static_cast<float>(r.width()),
39 static_cast<float>(r.height())) {}
40
41#if defined(OS_APPLE)
42 explicit RectF(const CGRect& r);
43 // Construct an equivalent CoreGraphics object.
44 CGRect ToCGRect() const;
45#endif
46
47 constexpr float x() const { return origin_.x(); }
48 void set_x(float x) { origin_.set_x(x); }
49
50 constexpr float y() const { return origin_.y(); }
51 void set_y(float y) { origin_.set_y(y); }
52
53 constexpr float width() const { return size_.width(); }
54 void set_width(float width) { size_.set_width(width); }
55
56 constexpr float height() const { return size_.height(); }
57 void set_height(float height) { size_.set_height(height); }
58
59 constexpr const PointF& origin() const { return origin_; }
60 void set_origin(const PointF& origin) { origin_ = origin; }
61
62 constexpr const SizeF& size() const { return size_; }
63 void set_size(const SizeF& size) { size_ = size; }
64
65 constexpr float right() const { return x() + width(); }
66 constexpr float bottom() const { return y() + height(); }
67
68 constexpr PointF top_right() const { return PointF(right(), y()); }
69 constexpr PointF bottom_left() const { return PointF(x(), bottom()); }
70 constexpr PointF bottom_right() const { return PointF(right(), bottom()); }
71
72 constexpr PointF left_center() const { return PointF(x(), y() + height() / 2); }
73 constexpr PointF top_center() const { return PointF(x() + width() / 2, y()); }
74 constexpr PointF right_center() const { return PointF(right(), y() + height() / 2); }
75 constexpr PointF bottom_center() const { return PointF(x() + width() / 2, bottom()); }
76
77 Vector2dF OffsetFromOrigin() const { return Vector2dF(x(), y()); }
78
79 void SetRect(float x, float y, float width, float height) {
80 origin_.SetPoint(x, y);
81 size_.SetSize(width, height);
82 }
83
84 // Shrink the rectangle by a horizontal and vertical distance on all sides.
85 void Inset(float horizontal, float vertical) {
86 Inset(horizontal, vertical, horizontal, vertical);
87 }
88
89 // Shrink the rectangle by the given insets.
90 void Inset(const InsetsF& insets);
91
92 // Shrink the rectangle by the specified amount on each side.
93 void Inset(float left, float top, float right, float bottom);
94
95 // Move the rectangle by a horizontal and vertical distance.
96 void Offset(float horizontal, float vertical);
97 void Offset(const Vector2dF& distance) { Offset(distance.x(), distance.y()); }
98 void operator+=(const Vector2dF& offset);
99 void operator-=(const Vector2dF& offset);
100
101 InsetsF InsetsFrom(const RectF& inner) const;
102
103 // Returns true if the area of the rectangle is zero.
104 bool IsEmpty() const { return size_.IsEmpty(); }
105
106 // A rect is less than another rect if its origin is less than
107 // the other rect's origin. If the origins are equal, then the
108 // shortest rect is less than the other. If the origin and the
109 // height are equal, then the narrowest rect is less than.
110 // This comparison is required to use Rects in sets, or sorted
111 // vectors.
112 bool operator<(const RectF& other) const;
113
114 // Returns true if the point identified by point_x and point_y falls inside
115 // this rectangle. The point (x, y) is inside the rectangle, but the
116 // point (x + width, y + height) is not.
117 bool Contains(float point_x, float point_y) const;
118
119 // Returns true if the specified point is contained by this rectangle.
120 bool Contains(const PointF& point) const { return Contains(point.x(), point.y()); }
121
122 // Returns true if this rectangle contains the specified rectangle.
123 bool Contains(const RectF& rect) const;
124
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;
128
129 // Computes the intersection of this rectangle with the given rectangle.
130 void Intersect(const RectF& rect);
131
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);
135
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);
139
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);
146
147 // Returns the center of this rectangle.
148 PointF CenterPoint() const;
149
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);
153
154 // Transpose x and y axis.
155 void Transpose();
156
157 // Splits |this| in two halves, |left_half| and |right_half|.
158 void SplitVertically(RectF* left_half, RectF* right_half) const;
159
160 // Returns true if this rectangle shares an entire edge (i.e., same width or
161 // same height) with the given rectangle, and the rectangles do not overlap.
162 bool SharesEdgeWith(const RectF& rect) const;
163
164 // Returns the manhattan distance from the rect to the point. If the point is
165 // inside the rect, returns 0.
166 float ManhattanDistanceToPoint(const PointF& point) const;
167
168 // Returns the manhattan distance between the contents of this rect and the
169 // contents of the given rect. That is, if the intersection of the two rects
170 // is non-empty then the function returns 0. If the rects share a side, it
171 // returns the smallest non-zero value appropriate for float.
172 float ManhattanInternalDistance(const RectF& rect) const;
173
174 // Scales the rectangle by |scale|.
175 void Scale(float scale) { Scale(scale, scale); }
176
177 void Scale(float x_scale, float y_scale) {
178 set_origin(ScalePoint(origin(), x_scale, y_scale));
179 set_size(ScaleSize(size(), x_scale, y_scale));
180 }
181
182 // This method reports if the RectF can be safely converted to an integer
183 // Rect. When it is false, some dimension of the RectF is outside the bounds
184 // of what an integer can represent, and converting it to a Rect will require
185 // clamping.
186 bool IsExpressibleAsRect() const;
187
188 std::string ToString() const;
189
190 private:
191 PointF origin_;
192 SizeF size_;
193};
194
195inline bool operator==(const RectF& lhs, const RectF& rhs) {
196 return lhs.origin() == rhs.origin() && lhs.size() == rhs.size();
197}
198
199inline bool operator!=(const RectF& lhs, const RectF& rhs) {
200 return !(lhs == rhs);
201}
202
203inline RectF operator+(const RectF& lhs, const Vector2dF& rhs) {
204 return RectF(lhs.x() + rhs.x(), lhs.y() + rhs.y(), lhs.width(), lhs.height());
205}
206
207inline RectF operator-(const RectF& lhs, const Vector2dF& rhs) {
208 return RectF(lhs.x() - rhs.x(), lhs.y() - rhs.y(), lhs.width(), lhs.height());
209}
210
211inline RectF operator+(const Vector2dF& lhs, const RectF& rhs) {
212 return rhs + lhs;
213}
214
215GFX_EXPORT RectF IntersectRects(const RectF& a, const RectF& b);
216GFX_EXPORT RectF UnionRects(const RectF& a, const RectF& b);
217GFX_EXPORT RectF SubtractRects(const RectF& a, const RectF& b);
218
219inline RectF ScaleRect(const RectF& r, float x_scale, float y_scale) {
220 return RectF(r.x() * x_scale, r.y() * y_scale, r.width() * x_scale, r.height() * y_scale);
221}
222
223inline 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.
233GFX_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.
238void PrintTo(const RectF& rect, ::std::ostream* os);
239
240} // namespace gfx
241
242#endif // UI_GFX_GEOMETRY_RECT_F_H_
static void Union(SkRegion *rgn, const SkIRect &rect)
Definition: RegionTest.cpp:27
constexpr float x() const
Definition: point_f.h:27
constexpr float y() const
Definition: point_f.h:28
constexpr RectF(const SizeF &size)
Definition: rect_f.h:32
constexpr PointF top_right() const
Definition: rect_f.h:68
bool IsEmpty() const
Definition: rect_f.h:104
constexpr PointF top_center() const
Definition: rect_f.h:73
void set_y(float y)
Definition: rect_f.h:51
constexpr PointF bottom_left() const
Definition: rect_f.h:69
Vector2dF OffsetFromOrigin() const
Definition: rect_f.h:77
constexpr const PointF & origin() const
Definition: rect_f.h:59
constexpr RectF()=default
void set_size(const SizeF &size)
Definition: rect_f.h:63
constexpr float y() const
Definition: rect_f.h:50
void set_origin(const PointF &origin)
Definition: rect_f.h:60
void Scale(float scale)
Definition: rect_f.h:175
constexpr float width() const
Definition: rect_f.h:53
constexpr float height() const
Definition: rect_f.h:56
constexpr RectF(const Rect &r)
Definition: rect_f.h:35
constexpr float right() const
Definition: rect_f.h:65
constexpr RectF(const PointF &origin, const SizeF &size)
Definition: rect_f.h:33
constexpr const SizeF & size() const
Definition: rect_f.h:62
constexpr PointF left_center() const
Definition: rect_f.h:72
void set_width(float width)
Definition: rect_f.h:54
constexpr PointF bottom_right() const
Definition: rect_f.h:70
constexpr RectF(float x, float y, float width, float height)
Definition: rect_f.h:30
constexpr float bottom() const
Definition: rect_f.h:66
void set_height(float height)
Definition: rect_f.h:57
constexpr PointF right_center() const
Definition: rect_f.h:74
void Inset(float horizontal, float vertical)
Definition: rect_f.h:85
constexpr RectF(float width, float height)
Definition: rect_f.h:29
void Offset(const Vector2dF &distance)
Definition: rect_f.h:97
bool Contains(const PointF &point) const
Definition: rect_f.h:120
void SetRect(float x, float y, float width, float height)
Definition: rect_f.h:79
constexpr float x() const
Definition: rect_f.h:47
constexpr PointF bottom_center() const
Definition: rect_f.h:75
void set_x(float x)
Definition: rect_f.h:48
void Scale(float x_scale, float y_scale)
Definition: rect_f.h:177
Definition: rect.h:36
constexpr float y() const
Definition: vector2d_f.h:29
constexpr float x() const
Definition: vector2d_f.h:26
static bool b
struct MyStruct a[10]
#define GFX_EXPORT
Definition: gfx_export.h:26
double y
double x
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
bool Contains(const Container &container, const Value &value)
Definition: insets.cc:10
Rect BoundingRect(const Point &p1, const Point &p2)
Definition: rect.cc:336
Rect IntersectRects(const Rect &a, const Rect &b)
Definition: rect.cc:318
Insets operator+(Insets lhs, const Insets &rhs)
Definition: insets.h:175
Rect UnionRects(const Rect &a, const Rect &b)
Definition: rect.cc:324
Rect SubtractRects(const Rect &a, const Rect &b)
Definition: rect.cc:330
void PrintTo(const Point &point, ::std::ostream *os)
Definition: gfx_util.cc:74
RectF ScaleRect(const RectF &r, float x_scale, float y_scale)
Definition: rect_f.h:219
PointF ScalePoint(const PointF &p, float x_scale, float y_scale)
Definition: point_f.cc:25
SizeF ScaleSize(const SizeF &s, float x_scale, float y_scale)
Definition: size_f.cc:33
Insets operator-(Insets lhs, const Insets &rhs)
Definition: insets.h:180
bool operator==(const Point &lhs, const Point &rhs)
Definition: point.h:96
constexpr size_t size(const T(&array)[N]) noexcept
bool operator!=(const Point &lhs, const Point &rhs)
Definition: point.h:100
static std::string ToString(CompilerBackend::Type type)
Definition: reflector.cc:559
constexpr bool operator<(const EnumType &lhs, const Mask< EnumType > &rhs)
Definition: mask.h:188
SINT Vec< N, T > & operator-=(Vec< N, T > &x, const Vec< N, T > &y)
Definition: SkVx.h:451
SINT Vec< N, T > & operator+=(Vec< N, T > &x, const Vec< N, T > &y)
Definition: SkVx.h:450
int32_t height
int32_t width
const Scalar scale
SeparatedVector2 offset