12#ifndef UI_GFX_GEOMETRY_RECT_H_
13#define UI_GFX_GEOMETRY_RECT_H_
27typedef struct tagRECT RECT;
28#elif defined(OS_APPLE)
29typedef struct CGRect CGRect;
38 constexpr Rect() =
default;
41 : origin_(
x,
y), size_(GetClampedValue(
x,
width), GetClampedValue(
y,
height)) {}
42 constexpr explicit Rect(
const Size& size) : size_(size) {}
45 size_(GetClampedValue(origin.
x(), size.
width()),
46 GetClampedValue(origin.
y(), size.
height())) {}
49 explicit Rect(
const RECT& r);
50#elif defined(OS_APPLE)
51 explicit Rect(
const CGRect& r);
57#elif defined(OS_APPLE)
59 CGRect ToCGRect()
const;
62 constexpr int x()
const {
return origin_.x(); }
66 size_.set_width(GetClampedValue(
x,
width()));
69 constexpr int y()
const {
return origin_.y(); }
73 size_.set_height(GetClampedValue(
y,
height()));
76 constexpr int width()
const {
return size_.width(); }
79 constexpr int height()
const {
return size_.height(); }
90 constexpr const Size&
size()
const {
return size_; }
92 set_width(size.width());
93 set_height(size.height());
111 origin_.SetPoint(
x,
y);
120 void SetByBounds(
int left,
int top,
int right,
int bottom);
123 void Inset(
int horizontal,
int vertical) {
Inset(horizontal, vertical, horizontal, vertical); }
126 void Inset(
const Insets& insets);
129 void Inset(
int left,
int top,
int right,
int bottom);
132 void Offset(
int horizontal,
int vertical);
140 bool IsEmpty()
const {
return size_.IsEmpty(); }
153 bool Contains(
int point_x,
int point_y)
const;
159 bool Contains(
const Rect& rect)
const;
163 bool Intersects(
const Rect& rect)
const;
166 void Intersect(
const Rect& rect);
174 void Subtract(
const Rect& rect);
181 void AdjustToFit(
const Rect& rect);
184 Point CenterPoint()
const;
188 void ClampToCenteredSize(
const Size& size);
194 void SplitVertically(
Rect* left_half,
Rect* right_half)
const;
198 bool SharesEdgeWith(
const Rect& rect)
const;
202 int ManhattanDistanceToPoint(
const Point& point)
const;
208 int ManhattanInternalDistance(
const Rect& rect)
const;
210 std::string ToString()
const;
212 bool ApproximatelyEqual(
const Rect& rect,
int tolerance)
const;
219 static constexpr bool AddWouldOverflow(
int a,
int b) {
226 return a > 0 &&
b > 0 &&
227 static_cast<unsigned>(std::numeric_limits<int>::max() -
a) <
static_cast<unsigned>(
b);
234 static constexpr int GetClampedValue(
int origin,
int size) {
235 return AddWouldOverflow(origin, size) ? std::numeric_limits<int>::max() - origin : size;
244 return !(lhs == rhs);
270 if (x_scale == 1.f && y_scale == 1.f)
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));
296 if (x_scale == 1.f && y_scale == 1.f)
310 if (x_scale == 1.f && y_scale == 1.f)
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));
334 if (x_scale == 1.f && y_scale == 1.f)
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)));
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));
357void PrintTo(
const Rect& rect, ::std::ostream* os);
static void Union(SkRegion *rgn, const SkIRect &rect)
static bool left(const SkPoint &p0, const SkPoint &p1)
static bool right(const SkPoint &p0, const SkPoint &p1)
constexpr int height() const
void set_width(int width)
void set_origin(const Point &origin)
bool Contains(const Point &point) const
constexpr Rect(int width, int height)
constexpr int right() const
void Offset(const Vector2d &distance)
constexpr const Point & origin() const
constexpr int bottom() const
void set_size(const Size &size)
void set_height(int height)
constexpr Point left_center() const
constexpr Rect(int x, int y, int width, int height)
constexpr Point bottom_right() const
constexpr const Size & size() const
constexpr Rect(const Point &origin, const Size &size)
constexpr Point top_right() const
constexpr Point bottom_center() const
constexpr Point bottom_left() const
constexpr Point right_center() const
void Inset(int horizontal, int vertical)
void SetRect(int x, int y, int width, int height)
Vector2d OffsetFromOrigin() const
constexpr Point top_center() const
constexpr Rect(const Size &size)
constexpr int width() const
static bool operator<(const SkPlainTextEditor::Editor::TextPosition &u, const SkPlainTextEditor::Editor::TextPosition &v)
Dst ClampFloor(Src value)
Rect BoundingRect(const Point &p1, const Point &p2)
Rect IntersectRects(const Rect &a, const Rect &b)
Insets operator+(Insets lhs, const Insets &rhs)
Rect UnionRects(const Rect &a, const Rect &b)
Rect SubtractRects(const Rect &a, const Rect &b)
Rect ScaleToEnclosedRect(const Rect &rect, float x_scale, float y_scale)
void PrintTo(const Point &point, ::std::ostream *os)
Insets operator-(Insets lhs, const Insets &rhs)
Rect ScaleToRoundedRect(const Rect &rect, float x_scale, float y_scale)
bool operator==(const Point &lhs, const Point &rhs)
Rect ScaleToEnclosingRectSafe(const Rect &rect, float x_scale, float y_scale)
Rect ScaleToEnclosingRect(const Rect &rect, float x_scale, float y_scale)
bool operator!=(const Point &lhs, const Point &rhs)
#define BASE_DCHECK(condition)