OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/geometry/rect.h" | 5 #include "ui/gfx/geometry/rect.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
10 #include <windows.h> | 10 #include <windows.h> |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
53 if (*origin < dst_origin) | 53 if (*origin < dst_origin) |
54 *origin = dst_origin; | 54 *origin = dst_origin; |
55 else | 55 else |
56 *origin = std::min(dst_origin + dst_size, *origin + *size) - *size; | 56 *origin = std::min(dst_origin + dst_size, *origin + *size) - *size; |
57 } | 57 } |
58 | 58 |
59 } // namespace | 59 } // namespace |
60 | 60 |
61 namespace gfx { | 61 namespace gfx { |
62 | 62 |
63 // This is the per-axis heuristic for picking the cwmost useful origin and | |
64 // width/height to represent the input range. | |
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 span = base::SaturatedSubtraction(max, min); | |
73 | |
74 int span_loss = base::SaturatedSubtraction(max, min + span); | |
75 | |
76 // If the desired width is within the limits of ints, we can just | |
77 // use the simple computations to represent the range precisely. | |
78 if (span_loss == 0) { | |
Peter Mayo
2017/03/17 00:30:48
This condition neatly summarizes that we have to p
| |
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 make sure it is represented precisely. | |
85 // The other side is probably practically "infinite", so we move it. | |
86 if (base::SaturatedAbsolute(max) < std::numeric_limits<int>::max() / 2) { | |
87 origin = max - span; | |
88 } else if (base::SaturatedAbsolute(min) < | |
89 std::numeric_limits<int>::max() / 2) { | |
90 origin = min; | |
91 } else { | |
92 // both are big, so keep the center. | |
93 origin = min + span_loss / 2; | |
94 } | |
95 } | |
96 | |
97 void Rect::SetByBounds(int left, int right, int top, int bottom) { | |
98 int x, y; | |
99 int width, height; | |
100 SaturatedClampRange(left, right, x, width); | |
101 SaturatedClampRange(top, bottom, y, height); | |
102 origin_.SetPoint(x, y); | |
103 size_.SetSize(width, height); | |
104 } | |
105 | |
63 void Rect::Inset(const Insets& insets) { | 106 void Rect::Inset(const Insets& insets) { |
64 Inset(insets.left(), insets.top(), insets.right(), insets.bottom()); | 107 Inset(insets.left(), insets.top(), insets.right(), insets.bottom()); |
65 } | 108 } |
66 | 109 |
67 void Rect::Inset(int left, int top, int right, int bottom) { | 110 void Rect::Inset(int left, int top, int right, int bottom) { |
68 origin_ += Vector2d(left, top); | 111 origin_ += Vector2d(left, top); |
69 // left+right might overflow/underflow, but width() - (left+right) might | 112 // left+right might overflow/underflow, but width() - (left+right) might |
70 // overflow as well. | 113 // overflow as well. |
71 set_width(base::SaturatedSubtraction(width(), | 114 set_width(base::SaturatedSubtraction(width(), |
72 base::SaturatedAddition(left, right))); | 115 base::SaturatedAddition(left, right))); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
144 } | 187 } |
145 | 188 |
146 void Rect::Union(const Rect& rect) { | 189 void Rect::Union(const Rect& rect) { |
147 if (IsEmpty()) { | 190 if (IsEmpty()) { |
148 *this = rect; | 191 *this = rect; |
149 return; | 192 return; |
150 } | 193 } |
151 if (rect.IsEmpty()) | 194 if (rect.IsEmpty()) |
152 return; | 195 return; |
153 | 196 |
154 int rx = std::min(x(), rect.x()); | 197 SetByBounds(std::min(x(), rect.x()), std::max(right(), rect.right()), |
155 int ry = std::min(y(), rect.y()); | 198 std::min(y(), rect.y()), std::max(bottom(), rect.bottom())); |
156 int rr = std::max(right(), rect.right()); | |
157 int rb = std::max(bottom(), rect.bottom()); | |
158 | |
159 // Subtracting to get width/height might overflow integers, so clamp them. | |
160 SetRect(rx, ry, base::SaturatedSubtraction(rr, rx), | |
161 base::SaturatedSubtraction(rb, ry)); | |
162 } | 199 } |
163 | 200 |
164 void Rect::Subtract(const Rect& rect) { | 201 void Rect::Subtract(const Rect& rect) { |
165 if (!Intersects(rect)) | 202 if (!Intersects(rect)) |
166 return; | 203 return; |
167 if (rect.Contains(*this)) { | 204 if (rect.Contains(*this)) { |
168 SetRect(0, 0, 0, 0); | 205 SetRect(0, 0, 0, 0); |
169 return; | 206 return; |
170 } | 207 } |
171 | 208 |
(...skipping 10 matching lines...) Expand all Loading... | |
182 rr = rect.x(); | 219 rr = rect.x(); |
183 } | 220 } |
184 } else if (rect.x() <= x() && rect.right() >= right()) { | 221 } else if (rect.x() <= x() && rect.right() >= right()) { |
185 // complete intersection in the x-direction | 222 // complete intersection in the x-direction |
186 if (rect.y() <= y()) { | 223 if (rect.y() <= y()) { |
187 ry = rect.bottom(); | 224 ry = rect.bottom(); |
188 } else if (rect.bottom() >= bottom()) { | 225 } else if (rect.bottom() >= bottom()) { |
189 rb = rect.y(); | 226 rb = rect.y(); |
190 } | 227 } |
191 } | 228 } |
192 SetRect(rx, ry, rr - rx, rb - ry); | 229 SetByBounds(rx, rr, ry, rb); |
193 } | 230 } |
194 | 231 |
195 void Rect::AdjustToFit(const Rect& rect) { | 232 void Rect::AdjustToFit(const Rect& rect) { |
196 int new_x = x(); | 233 int new_x = x(); |
197 int new_y = y(); | 234 int new_y = y(); |
198 int new_width = width(); | 235 int new_width = width(); |
199 int new_height = height(); | 236 int new_height = height(); |
200 AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width); | 237 AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width); |
201 AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height); | 238 AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height); |
202 SetRect(new_x, new_y, new_width, new_height); | 239 SetRect(new_x, new_y, new_width, new_height); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
285 return result; | 322 return result; |
286 } | 323 } |
287 | 324 |
288 Rect SubtractRects(const Rect& a, const Rect& b) { | 325 Rect SubtractRects(const Rect& a, const Rect& b) { |
289 Rect result = a; | 326 Rect result = a; |
290 result.Subtract(b); | 327 result.Subtract(b); |
291 return result; | 328 return result; |
292 } | 329 } |
293 | 330 |
294 Rect BoundingRect(const Point& p1, const Point& p2) { | 331 Rect BoundingRect(const Point& p1, const Point& p2) { |
295 int rx = std::min(p1.x(), p2.x()); | 332 Rect result; |
296 int ry = std::min(p1.y(), p2.y()); | 333 result.SetByBounds(std::min(p1.x(), p2.x()), std::max(p1.x(), p2.x()), |
297 int rr = std::max(p1.x(), p2.x()); | 334 std::min(p1.y(), p2.y()), std::max(p1.y(), p2.y())); |
298 int rb = std::max(p1.y(), p2.y()); | 335 return result; |
299 return Rect(rx, ry, rr - rx, rb - ry); | |
300 } | 336 } |
301 | 337 |
302 } // namespace gfx | 338 } // namespace gfx |
OLD | NEW |