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 #include "ui/gfx/geometry/rect.h"
6
7 #include <algorithm>
8
9 #if defined(OS_WIN)
10 #include <windows.h>
11 #elif defined(OS_IOS)
12 #include <CoreGraphics/CoreGraphics.h>
13 #elif defined(OS_MACOSX)
14 #include <ApplicationServices/ApplicationServices.h>
15 #endif
16
17 #include "base/logging.h"
18 #include "base/numerics/clamped_math.h"
19 #include "base/strings/stringprintf.h"
20 #include "build/build_config.h"
21 #include "ui/gfx/geometry/insets.h"
22
23 namespace gfx {
24
25 #if defined(OS_WIN)
Rect(const RECT & r)26 Rect::Rect(const RECT& r)
27 : origin_(r.left, r.top),
28 size_(std::abs(r.right - r.left), std::abs(r.bottom - r.top)) {
29 }
30 #elif defined(OS_MACOSX)
31 Rect::Rect(const CGRect& r)
32 : origin_(r.origin.x, r.origin.y), size_(r.size.width, r.size.height) {
33 }
34 #endif
35
36 #if defined(OS_WIN)
ToRECT() const37 RECT Rect::ToRECT() const {
38 RECT r;
39 r.left = x();
40 r.right = right();
41 r.top = y();
42 r.bottom = bottom();
43 return r;
44 }
45 #elif defined(OS_MACOSX)
ToCGRect() const46 CGRect Rect::ToCGRect() const {
47 return CGRectMake(x(), y(), width(), height());
48 }
49 #endif
50
AdjustAlongAxis(int dst_origin,int dst_size,int * origin,int * size)51 void AdjustAlongAxis(int dst_origin, int dst_size, int* origin, int* size) {
52 *size = std::min(dst_size, *size);
53 if (*origin < dst_origin)
54 *origin = dst_origin;
55 else
56 *origin = std::min(dst_origin + dst_size, *origin + *size) - *size;
57 }
58
59 } // namespace
60
61 namespace gfx {
62
63 // This is the per-axis heuristic for picking the most useful origin and
64 // width/height to represent the input range.
SaturatedClampRange(int min,int max,int * origin,int * span)65 static void SaturatedClampRange(int min, int max, int* origin, int* span) {
66 if (max < min) {
67 *span = 0;
68 *origin = min;
69 return;
70 }
71
72 int effective_span = base::ClampSub(max, min);
73 int span_loss = base::ClampSub(max, min + effective_span);
74
75 // If the desired width is within the limits of ints, we can just
76 // use the simple computations to represent the range precisely.
77 if (span_loss == 0) {
78 *span = effective_span;
79 *origin = min;
80 return;
81 }
82
83 // Now we have to approximate. If one of min or max is close enough
84 // to zero we choose to represent that one precisely. The other side is
85 // probably practically "infinite", so we move it.
86 constexpr unsigned kMaxDimension = std::numeric_limits<int>::max() / 2;
87 if (base::SafeUnsignedAbs(max) < kMaxDimension) {
88 // Maintain origin + span == max.
89 *span = effective_span;
90 *origin = max - effective_span;
91 } else if (base::SafeUnsignedAbs(min) < kMaxDimension) {
92 // Maintain origin == min.
93 *span = effective_span;
94 *origin = min;
95 } else {
96 // Both are big, so keep the center.
97 *span = effective_span;
98 *origin = min + span_loss / 2;
99 }
100 }
101
SetByBounds(int left,int top,int right,int bottom)102 void Rect::SetByBounds(int left, int top, int right, int bottom) {
103 int x, y;
104 int width, height;
105 SaturatedClampRange(left, right, &x, &width);
106 SaturatedClampRange(top, bottom, &y, &height);
107 origin_.SetPoint(x, y);
108 size_.SetSize(width, height);
109 }
110
Inset(const Insets & insets)111 void Rect::Inset(const Insets& insets) {
112 Inset(insets.left(), insets.top(), insets.right(), insets.bottom());
113 }
114
Inset(int left,int top,int right,int bottom)115 void Rect::Inset(int left, int top, int right, int bottom) {
116 origin_ += Vector2d(left, top);
117 // left+right might overflow/underflow, but width() - (left+right) might
118 // overflow as well.
119 set_width(base::ClampSub(width(), base::ClampAdd(left, right)));
120 set_height(base::ClampSub(height(), base::ClampAdd(top, bottom)));
121 }
122
Offset(int horizontal,int vertical)123 void Rect::Offset(int horizontal, int vertical) {
124 origin_ += Vector2d(horizontal, vertical);
125 // Ensure that width and height remain valid.
126 set_width(width());
127 set_height(height());
128 }
129
operator +=(const Vector2d & offset)130 void Rect::operator+=(const Vector2d& offset) {
131 origin_ += offset;
132 // Ensure that width and height remain valid.
133 set_width(width());
134 set_height(height());
135 }
136
operator -=(const Vector2d & offset)137 void Rect::operator-=(const Vector2d& offset) {
138 origin_ -= offset;
139 }
140
InsetsFrom(const Rect & inner) const141 Insets Rect::InsetsFrom(const Rect& inner) const {
142 return Insets(inner.y() - y(),
143 inner.x() - x(),
144 bottom() - inner.bottom(),
145 right() - inner.right());
146 }
147
operator <(const Rect & other) const148 bool Rect::operator<(const Rect& other) const {
149 if (origin_ == other.origin_) {
150 if (width() == other.width()) {
151 return height() < other.height();
152 } else {
153 return width() < other.width();
154 }
155 } else {
156 return origin_ < other.origin_;
157 }
158 }
159
Contains(int point_x,int point_y) const160 bool Rect::Contains(int point_x, int point_y) const {
161 return (point_x >= x()) && (point_x < right()) && (point_y >= y()) &&
162 (point_y < bottom());
163 }
164
Contains(const Rect & rect) const165 bool Rect::Contains(const Rect& rect) const {
166 return (rect.x() >= x() && rect.right() <= right() && rect.y() >= y() &&
167 rect.bottom() <= bottom());
168 }
169
Intersects(const Rect & rect) const170 bool Rect::Intersects(const Rect& rect) const {
171 return !(IsEmpty() || rect.IsEmpty() || rect.x() >= right() ||
172 rect.right() <= x() || rect.y() >= bottom() || rect.bottom() <= y());
173 }
174
Intersect(const Rect & rect)175 void Rect::Intersect(const Rect& rect) {
176 if (IsEmpty() || rect.IsEmpty()) {
177 SetRect(0, 0, 0, 0); // Throws away empty position.
178 return;
179 }
180
181 int left = std::max(x(), rect.x());
182 int top = std::max(y(), rect.y());
183 int new_right = std::min(right(), rect.right());
184 int new_bottom = std::min(bottom(), rect.bottom());
185
186 if (left >= new_right || top >= new_bottom) {
187 SetRect(0, 0, 0, 0); // Throws away empty position.
188 return;
189 }
190
191 SetByBounds(left, top, new_right, new_bottom);
192 }
193
Union(const Rect & rect)194 void Rect::Union(const Rect& rect) {
195 if (IsEmpty()) {
196 *this = rect;
197 return;
198 }
199 if (rect.IsEmpty())
200 return;
201
202 SetByBounds(std::min(x(), rect.x()), std::min(y(), rect.y()),
203 std::max(right(), rect.right()),
204 std::max(bottom(), rect.bottom()));
205 }
206
Subtract(const Rect & rect)207 void Rect::Subtract(const Rect& rect) {
208 if (!Intersects(rect))
209 return;
210 if (rect.Contains(*this)) {
211 SetRect(0, 0, 0, 0);
212 return;
213 }
214
215 int rx = x();
216 int ry = y();
217 int rr = right();
218 int rb = bottom();
219
220 if (rect.y() <= y() && rect.bottom() >= bottom()) {
221 // complete intersection in the y-direction
222 if (rect.x() <= x()) {
223 rx = rect.right();
224 } else if (rect.right() >= right()) {
225 rr = rect.x();
226 }
227 } else if (rect.x() <= x() && rect.right() >= right()) {
228 // complete intersection in the x-direction
229 if (rect.y() <= y()) {
230 ry = rect.bottom();
231 } else if (rect.bottom() >= bottom()) {
232 rb = rect.y();
233 }
234 }
235 SetByBounds(rx, ry, rr, rb);
236 }
237
AdjustToFit(const Rect & rect)238 void Rect::AdjustToFit(const Rect& rect) {
239 int new_x = x();
240 int new_y = y();
241 int new_width = width();
242 int new_height = height();
243 AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width);
244 AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height);
245 SetRect(new_x, new_y, new_width, new_height);
246 }
247
CenterPoint() const248 Point Rect::CenterPoint() const {
249 return Point(x() + width() / 2, y() + height() / 2);
250 }
251
ClampToCenteredSize(const Size & size)252 void Rect::ClampToCenteredSize(const Size& size) {
253 int new_width = std::min(width(), size.width());
254 int new_height = std::min(height(), size.height());
255 int new_x = x() + (width() - new_width) / 2;
256 int new_y = y() + (height() - new_height) / 2;
257 SetRect(new_x, new_y, new_width, new_height);
258 }
259
SplitVertically(Rect * left_half,Rect * right_half) const260 void Rect::SplitVertically(Rect* left_half, Rect* right_half) const {
261 DCHECK(left_half);
262 DCHECK(right_half);
263
264 left_half->SetRect(x(), y(), width() / 2, height());
265 right_half->SetRect(
266 left_half->right(), y(), width() - left_half->width(), height());
267 }
268
SharesEdgeWith(const Rect & rect) const269 bool Rect::SharesEdgeWith(const Rect& rect) const {
270 return (y() == rect.y() && height() == rect.height() &&
271 (x() == rect.right() || right() == rect.x())) ||
272 (x() == rect.x() && width() == rect.width() &&
273 (y() == rect.bottom() || bottom() == rect.y()));
274 }
275
ManhattanDistanceToPoint(const Point & point) const276 int Rect::ManhattanDistanceToPoint(const Point& point) const {
277 int x_distance =
278 std::max<int>(0, std::max(x() - point.x(), point.x() - right()));
279 int y_distance =
280 std::max<int>(0, std::max(y() - point.y(), point.y() - bottom()));
281
282 return x_distance + y_distance;
283 }
284
ManhattanInternalDistance(const Rect & rect) const285 int Rect::ManhattanInternalDistance(const Rect& rect) const {
286 Rect c(*this);
287 c.Union(rect);
288
289 int x = std::max(0, c.width() - width() - rect.width() + 1);
290 int y = std::max(0, c.height() - height() - rect.height() + 1);
291 return x + y;
292 }
293
ToString() const294 std::string Rect::ToString() const {
295 return base::StringPrintf("%s %s",
296 origin().ToString().c_str(),
297 size().ToString().c_str());
298 }
299
ApproximatelyEqual(const Rect & rect,int tolerance) const300 bool Rect::ApproximatelyEqual(const Rect& rect, int tolerance) const {
301 return std::abs(x() - rect.x()) <= tolerance &&
302 std::abs(y() - rect.y()) <= tolerance &&
303 std::abs(right() - rect.right()) <= tolerance &&
304 std::abs(bottom() - rect.bottom()) <= tolerance;
305 }
306
operator +(const Rect & lhs,const Vector2d & rhs)307 Rect operator+(const Rect& lhs, const Vector2d& rhs) {
308 Rect result(lhs);
309 result += rhs;
310 return result;
311 }
312
operator -(const Rect & lhs,const Vector2d & rhs)313 Rect operator-(const Rect& lhs, const Vector2d& rhs) {
314 Rect result(lhs);
315 result -= rhs;
316 return result;
317 }
318
IntersectRects(const Rect & a,const Rect & b)319 Rect IntersectRects(const Rect& a, const Rect& b) {
320 Rect result = a;
321 result.Intersect(b);
322 return result;
323 }
324
UnionRects(const Rect & a,const Rect & b)325 Rect UnionRects(const Rect& a, const Rect& b) {
326 Rect result = a;
327 result.Union(b);
328 return result;
329 }
330
SubtractRects(const Rect & a,const Rect & b)331 Rect SubtractRects(const Rect& a, const Rect& b) {
332 Rect result = a;
333 result.Subtract(b);
334 return result;
335 }
336
BoundingRect(const Point & p1,const Point & p2)337 Rect BoundingRect(const Point& p1, const Point& p2) {
338 Rect result;
339 result.SetByBounds(std::min(p1.x(), p2.x()), std::min(p1.y(), p2.y()),
340 std::max(p1.x(), p2.x()), std::max(p1.y(), p2.y()));
341 return result;
342 }
343
344 } // namespace gfx
345