Flutter Engine
The Flutter Engine
rect.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// Defines a simple integer rectangle class. The containment semantics
6// are array-like; that is, the coordinate (x, y) is considered to be
7// contained by the rectangle, but the coordinate (x + width, y) is not.
8// The class will happily let you create malformed rectangles (that is,
9// rectangles with negative width and/or height), but there will be assertions
10// in the operations (such as Contains()) to complain in this case.
11
12#ifndef UI_GFX_GEOMETRY_RECT_H_
13#define UI_GFX_GEOMETRY_RECT_H_
14
15#include <cmath>
16#include <iosfwd>
17#include <string>
18
20#include "base/logging.h"
22#include "point.h"
23#include "size.h"
24#include "vector2d.h"
25
26#if defined(OS_WIN)
27typedef struct tagRECT RECT;
28#elif defined(OS_APPLE)
29typedef struct CGRect CGRect;
30#endif
31
32namespace gfx {
33
34class Insets;
35
37 public:
38 constexpr Rect() = default;
39 constexpr Rect(int width, int height) : size_(width, height) {}
40 constexpr Rect(int x, int y, int width, int height)
41 : origin_(x, y), size_(GetClampedValue(x, width), GetClampedValue(y, height)) {}
42 constexpr explicit Rect(const Size& size) : size_(size) {}
43 constexpr Rect(const Point& origin, const Size& size)
44 : origin_(origin),
45 size_(GetClampedValue(origin.x(), size.width()),
46 GetClampedValue(origin.y(), size.height())) {}
47
48#if defined(OS_WIN)
49 explicit Rect(const RECT& r);
50#elif defined(OS_APPLE)
51 explicit Rect(const CGRect& r);
52#endif
53
54#if defined(OS_WIN)
55 // Construct an equivalent Win32 RECT object.
56 RECT ToRECT() const;
57#elif defined(OS_APPLE)
58 // Construct an equivalent CoreGraphics object.
59 CGRect ToCGRect() const;
60#endif
61
62 constexpr int x() const { return origin_.x(); }
63 // Sets the X position while preserving the width.
64 void set_x(int x) {
65 origin_.set_x(x);
66 size_.set_width(GetClampedValue(x, width()));
67 }
68
69 constexpr int y() const { return origin_.y(); }
70 // Sets the Y position while preserving the height.
71 void set_y(int y) {
72 origin_.set_y(y);
73 size_.set_height(GetClampedValue(y, height()));
74 }
75
76 constexpr int width() const { return size_.width(); }
77 void set_width(int width) { size_.set_width(GetClampedValue(x(), width)); }
78
79 constexpr int height() const { return size_.height(); }
80 void set_height(int height) { size_.set_height(GetClampedValue(y(), height)); }
81
82 constexpr const Point& origin() const { return origin_; }
83 void set_origin(const Point& origin) {
84 origin_ = origin;
85 // Ensure that width and height remain valid.
86 set_width(width());
87 set_height(height());
88 }
89
90 constexpr const Size& size() const { return size_; }
91 void set_size(const Size& size) {
92 set_width(size.width());
93 set_height(size.height());
94 }
95
96 constexpr int right() const { return x() + width(); }
97 constexpr int bottom() const { return y() + height(); }
98
99 constexpr Point top_right() const { return Point(right(), y()); }
100 constexpr Point bottom_left() const { return Point(x(), bottom()); }
101 constexpr Point bottom_right() const { return Point(right(), bottom()); }
102
103 constexpr Point left_center() const { return Point(x(), y() + height() / 2); }
104 constexpr Point top_center() const { return Point(x() + width() / 2, y()); }
105 constexpr Point right_center() const { return Point(right(), y() + height() / 2); }
106 constexpr Point bottom_center() const { return Point(x() + width() / 2, bottom()); }
107
108 Vector2d OffsetFromOrigin() const { return Vector2d(x(), y()); }
109
110 void SetRect(int x, int y, int width, int height) {
111 origin_.SetPoint(x, y);
112 // Ensure that width and height remain valid.
113 set_width(width);
114 set_height(height);
115 }
116
117 // Use in place of SetRect() when you know the edges of the rectangle instead
118 // of the dimensions, rather than trying to determine the width/height
119 // yourself. This safely handles cases where the width/height would overflow.
120 void SetByBounds(int left, int top, int right, int bottom);
121
122 // Shrink the rectangle by a horizontal and vertical distance on all sides.
123 void Inset(int horizontal, int vertical) { Inset(horizontal, vertical, horizontal, vertical); }
124
125 // Shrink the rectangle by the given insets.
126 void Inset(const Insets& insets);
127
128 // Shrink the rectangle by the specified amount on each side.
129 void Inset(int left, int top, int right, int bottom);
130
131 // Move the rectangle by a horizontal and vertical distance.
132 void Offset(int horizontal, int vertical);
133 void Offset(const Vector2d& distance) { Offset(distance.x(), distance.y()); }
134 void operator+=(const Vector2d& offset);
135 void operator-=(const Vector2d& offset);
136
137 Insets InsetsFrom(const Rect& inner) const;
138
139 // Returns true if the area of the rectangle is zero.
140 bool IsEmpty() const { return size_.IsEmpty(); }
141
142 // A rect is less than another rect if its origin is less than
143 // the other rect's origin. If the origins are equal, then the
144 // shortest rect is less than the other. If the origin and the
145 // height are equal, then the narrowest rect is less than.
146 // This comparison is required to use Rects in sets, or sorted
147 // vectors.
148 bool operator<(const Rect& other) const;
149
150 // Returns true if the point identified by point_x and point_y falls inside
151 // this rectangle. The point (x, y) is inside the rectangle, but the
152 // point (x + width, y + height) is not.
153 bool Contains(int point_x, int point_y) const;
154
155 // Returns true if the specified point is contained by this rectangle.
156 bool Contains(const Point& point) const { return Contains(point.x(), point.y()); }
157
158 // Returns true if this rectangle contains the specified rectangle.
159 bool Contains(const Rect& rect) const;
160
161 // Returns true if this rectangle intersects the specified rectangle.
162 // An empty rectangle doesn't intersect any rectangle.
163 bool Intersects(const Rect& rect) const;
164
165 // Computes the intersection of this rectangle with the given rectangle.
166 void Intersect(const Rect& rect);
167
168 // Computes the union of this rectangle with the given rectangle. The union
169 // is the smallest rectangle containing both rectangles.
170 void Union(const Rect& rect);
171
172 // Computes the rectangle resulting from subtracting |rect| from |*this|,
173 // i.e. the bounding rect of |Region(*this) - Region(rect)|.
174 void Subtract(const Rect& rect);
175
176 // Fits as much of the receiving rectangle into the supplied rectangle as
177 // possible, becoming the result. For example, if the receiver had
178 // a x-location of 2 and a width of 4, and the supplied rectangle had
179 // an x-location of 0 with a width of 5, the returned rectangle would have
180 // an x-location of 1 with a width of 4.
181 void AdjustToFit(const Rect& rect);
182
183 // Returns the center of this rectangle.
184 Point CenterPoint() const;
185
186 // Becomes a rectangle that has the same center point but with a size capped
187 // at given |size|.
188 void ClampToCenteredSize(const Size& size);
189
190 // Transpose x and y axis.
191 void Transpose();
192
193 // Splits |this| in two halves, |left_half| and |right_half|.
194 void SplitVertically(Rect* left_half, Rect* right_half) const;
195
196 // Returns true if this rectangle shares an entire edge (i.e., same width or
197 // same height) with the given rectangle, and the rectangles do not overlap.
198 bool SharesEdgeWith(const Rect& rect) const;
199
200 // Returns the manhattan distance from the rect to the point. If the point is
201 // inside the rect, returns 0.
202 int ManhattanDistanceToPoint(const Point& point) const;
203
204 // Returns the manhattan distance between the contents of this rect and the
205 // contents of the given rect. That is, if the intersection of the two rects
206 // is non-empty then the function returns 0. If the rects share a side, it
207 // returns the smallest non-zero value appropriate for int.
208 int ManhattanInternalDistance(const Rect& rect) const;
209
210 std::string ToString() const;
211
212 bool ApproximatelyEqual(const Rect& rect, int tolerance) const;
213
214 private:
215 gfx::Point origin_;
216 gfx::Size size_;
217
218 // Returns true iff a+b would overflow max int.
219 static constexpr bool AddWouldOverflow(int a, int b) {
220 // In this function, GCC tries to make optimizations that would only work if
221 // max - a wouldn't overflow but it isn't smart enough to notice that a > 0.
222 // So cast everything to unsigned to avoid this. As it is guaranteed that
223 // max - a and b are both already positive, the cast is a noop.
224 //
225 // This is intended to be: a > 0 && max - a < b
226 return a > 0 && b > 0 &&
227 static_cast<unsigned>(std::numeric_limits<int>::max() - a) < static_cast<unsigned>(b);
228 }
229
230 // Clamp the size to avoid integer overflow in bottom() and right().
231 // This returns the width given an origin and a width.
232 // TODO(enne): this should probably use base::ClampAdd, but that
233 // function is not a constexpr.
234 static constexpr int GetClampedValue(int origin, int size) {
235 return AddWouldOverflow(origin, size) ? std::numeric_limits<int>::max() - origin : size;
236 }
237};
238
239inline bool operator==(const Rect& lhs, const Rect& rhs) {
240 return lhs.origin() == rhs.origin() && lhs.size() == rhs.size();
241}
242
243inline bool operator!=(const Rect& lhs, const Rect& rhs) {
244 return !(lhs == rhs);
245}
246
247GFX_EXPORT Rect operator+(const Rect& lhs, const Vector2d& rhs);
248GFX_EXPORT Rect operator-(const Rect& lhs, const Vector2d& rhs);
249
250inline Rect operator+(const Vector2d& lhs, const Rect& rhs) {
251 return rhs + lhs;
252}
253
254GFX_EXPORT Rect IntersectRects(const Rect& a, const Rect& b);
255GFX_EXPORT Rect UnionRects(const Rect& a, const Rect& b);
256GFX_EXPORT Rect SubtractRects(const Rect& a, const Rect& b);
257
258// Constructs a rectangle with |p1| and |p2| as opposite corners.
259//
260// This could also be thought of as "the smallest rect that contains both
261// points", except that we consider points on the right/bottom edges of the
262// rect to be outside the rect. So technically one or both points will not be
263// contained within the rect, because they will appear on one of these edges.
264GFX_EXPORT Rect BoundingRect(const Point& p1, const Point& p2);
265
266// Scales the rect and returns the enclosing rect. Use this only the inputs are
267// known to not overflow. Use ScaleToEnclosingRectSafe if the inputs are
268// unknown and need to use saturated math.
269inline Rect ScaleToEnclosingRect(const Rect& rect, float x_scale, float y_scale) {
270 if (x_scale == 1.f && y_scale == 1.f)
271 return rect;
272 // These next functions cast instead of using e.g. base::ClampFloor() because
273 // we haven't checked to ensure that the clamping behavior of the helper
274 // functions doesn't degrade performance, and callers shouldn't be passing
275 // values that cause overflow anyway.
276 BASE_DCHECK(base::IsValueInRangeForNumericType<int>(std::floor(rect.x() * x_scale)));
277 BASE_DCHECK(base::IsValueInRangeForNumericType<int>(std::floor(rect.y() * y_scale)));
278 BASE_DCHECK(base::IsValueInRangeForNumericType<int>(std::ceil(rect.right() * x_scale)));
279 BASE_DCHECK(base::IsValueInRangeForNumericType<int>(std::ceil(rect.bottom() * y_scale)));
280 int x = static_cast<int>(std::floor(rect.x() * x_scale));
281 int y = static_cast<int>(std::floor(rect.y() * y_scale));
282 int r = rect.width() == 0 ? x : static_cast<int>(std::ceil(rect.right() * x_scale));
283 int b = rect.height() == 0 ? y : static_cast<int>(std::ceil(rect.bottom() * y_scale));
284 return Rect(x, y, r - x, b - y);
285}
286
287inline Rect ScaleToEnclosingRect(const Rect& rect, float scale) {
289}
290
291// ScaleToEnclosingRect but clamping instead of asserting if the resulting rect
292// would overflow.
293// TODO(pkasting): Attempt to switch ScaleTo...Rect() to this construction and
294// check performance.
295inline Rect ScaleToEnclosingRectSafe(const Rect& rect, float x_scale, float y_scale) {
296 if (x_scale == 1.f && y_scale == 1.f)
297 return rect;
298 int x = base::ClampFloor(rect.x() * x_scale);
299 int y = base::ClampFloor(rect.y() * y_scale);
300 int w = base::ClampCeil(rect.width() * x_scale);
301 int h = base::ClampCeil(rect.height() * y_scale);
302 return Rect(x, y, w, h);
303}
304
307}
308
309inline Rect ScaleToEnclosedRect(const Rect& rect, float x_scale, float y_scale) {
310 if (x_scale == 1.f && y_scale == 1.f)
311 return rect;
312 BASE_DCHECK(base::IsValueInRangeForNumericType<int>(std::ceil(rect.x() * x_scale)));
313 BASE_DCHECK(base::IsValueInRangeForNumericType<int>(std::ceil(rect.y() * y_scale)));
314 BASE_DCHECK(base::IsValueInRangeForNumericType<int>(std::floor(rect.right() * x_scale)));
315 BASE_DCHECK(base::IsValueInRangeForNumericType<int>(std::floor(rect.bottom() * y_scale)));
316 int x = static_cast<int>(std::ceil(rect.x() * x_scale));
317 int y = static_cast<int>(std::ceil(rect.y() * y_scale));
318 int r = rect.width() == 0 ? x : static_cast<int>(std::floor(rect.right() * x_scale));
319 int b = rect.height() == 0 ? y : static_cast<int>(std::floor(rect.bottom() * y_scale));
320 return Rect(x, y, r - x, b - y);
321}
322
323inline Rect ScaleToEnclosedRect(const Rect& rect, float scale) {
325}
326
327// Scales |rect| by scaling its four corner points. If the corner points lie on
328// non-integral coordinate after scaling, their values are rounded to the
329// nearest integer.
330// This is helpful during layout when relative positions of multiple gfx::Rect
331// in a given coordinate space needs to be same after scaling as it was before
332// scaling. ie. this gives a lossless relative positioning of rects.
333inline Rect ScaleToRoundedRect(const Rect& rect, float x_scale, float y_scale) {
334 if (x_scale == 1.f && y_scale == 1.f)
335 return rect;
336
337 BASE_DCHECK(base::IsValueInRangeForNumericType<int>(std::round(rect.x() * x_scale)));
338 BASE_DCHECK(base::IsValueInRangeForNumericType<int>(std::round(rect.y() * y_scale)));
339 BASE_DCHECK(base::IsValueInRangeForNumericType<int>(std::round(rect.right() * x_scale)));
340 BASE_DCHECK(base::IsValueInRangeForNumericType<int>(std::round(rect.bottom() * y_scale)));
341
342 int x = static_cast<int>(std::round(rect.x() * x_scale));
343 int y = static_cast<int>(std::round(rect.y() * y_scale));
344 int r = rect.width() == 0 ? x : static_cast<int>(std::round(rect.right() * x_scale));
345 int b = rect.height() == 0 ? y : static_cast<int>(std::round(rect.bottom() * y_scale));
346
347 return Rect(x, y, r - x, b - y);
348}
349
350inline Rect ScaleToRoundedRect(const Rect& rect, float scale) {
352}
353
354// This is declared here for use in gtest-based unit tests but is defined in
355// the //ui/gfx:test_support target. Depend on that to use this in your unit
356// test. This should not be used in production code - call ToString() instead.
357void PrintTo(const Rect& rect, ::std::ostream* os);
358
359} // namespace gfx
360
361#endif // UI_GFX_GEOMETRY_RECT_H_
static void round(SkPoint *p)
static void Union(SkRegion *rgn, const SkIRect &rect)
Definition: RegionTest.cpp:27
constexpr int y() const
Definition: point.h:49
constexpr int x() const
Definition: point.h:48
Definition: rect.h:36
constexpr int height() const
Definition: rect.h:79
void set_width(int width)
Definition: rect.h:77
void set_origin(const Point &origin)
Definition: rect.h:83
void set_x(int x)
Definition: rect.h:64
bool Contains(const Point &point) const
Definition: rect.h:156
constexpr Rect(int width, int height)
Definition: rect.h:39
constexpr int right() const
Definition: rect.h:96
void Offset(const Vector2d &distance)
Definition: rect.h:133
constexpr const Point & origin() const
Definition: rect.h:82
constexpr int bottom() const
Definition: rect.h:97
void set_size(const Size &size)
Definition: rect.h:91
void set_height(int height)
Definition: rect.h:80
constexpr Point left_center() const
Definition: rect.h:103
constexpr Rect(int x, int y, int width, int height)
Definition: rect.h:40
constexpr Point bottom_right() const
Definition: rect.h:101
constexpr int y() const
Definition: rect.h:69
constexpr const Size & size() const
Definition: rect.h:90
constexpr Rect(const Point &origin, const Size &size)
Definition: rect.h:43
void set_y(int y)
Definition: rect.h:71
constexpr Point top_right() const
Definition: rect.h:99
constexpr Point bottom_center() const
Definition: rect.h:106
constexpr Point bottom_left() const
Definition: rect.h:100
constexpr Point right_center() const
Definition: rect.h:105
void Inset(int horizontal, int vertical)
Definition: rect.h:123
void SetRect(int x, int y, int width, int height)
Definition: rect.h:110
Vector2d OffsetFromOrigin() const
Definition: rect.h:108
constexpr Point top_center() const
Definition: rect.h:104
bool IsEmpty() const
Definition: rect.h:140
constexpr int x() const
Definition: rect.h:62
constexpr Rect()=default
constexpr Rect(const Size &size)
Definition: rect.h:42
constexpr int width() const
Definition: rect.h:76
Definition: size.h:26
static bool b
struct MyStruct a[10]
#define GFX_EXPORT
Definition: gfx_export.h:26
static float max(float r, float g, float b)
Definition: hsl.cpp:49
double y
double x
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
Dst ClampFloor(Src value)
Dst ClampCeil(Src value)
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
Rect ScaleToEnclosedRect(const Rect &rect, float x_scale, float y_scale)
Definition: rect.h:309
void PrintTo(const Point &point, ::std::ostream *os)
Definition: gfx_util.cc:74
Insets operator-(Insets lhs, const Insets &rhs)
Definition: insets.h:180
Rect ScaleToRoundedRect(const Rect &rect, float x_scale, float y_scale)
Definition: rect.h:333
bool operator==(const Point &lhs, const Point &rhs)
Definition: point.h:96
Rect ScaleToEnclosingRectSafe(const Rect &rect, float x_scale, float y_scale)
Definition: rect.h:295
Rect ScaleToEnclosingRect(const Rect &rect, float x_scale, float y_scale)
Definition: rect.h:269
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
TRect< Scalar > Rect
Definition: rect.h:769
TPoint< Scalar > Point
Definition: point.h:322
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
SIN Vec< N, float > floor(const Vec< N, float > &x)
Definition: SkVx.h:703
SIN Vec< N, float > ceil(const Vec< N, float > &x)
Definition: SkVx.h:702
SkScalar w
SkScalar h
int32_t height
int32_t width
const Scalar scale
SeparatedVector2 offset
#define BASE_DCHECK(condition)
Definition: logging.h:63