Chromium Code Reviews| 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 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 } | 150 } |
| 151 if (rect.IsEmpty()) | 151 if (rect.IsEmpty()) |
| 152 return; | 152 return; |
| 153 | 153 |
| 154 int rx = std::min(x(), rect.x()); | 154 int rx = std::min(x(), rect.x()); |
| 155 int ry = std::min(y(), rect.y()); | 155 int ry = std::min(y(), rect.y()); |
| 156 int rr = std::max(right(), rect.right()); | 156 int rr = std::max(right(), rect.right()); |
| 157 int rb = std::max(bottom(), rect.bottom()); | 157 int rb = std::max(bottom(), rect.bottom()); |
| 158 | 158 |
| 159 // Subtracting to get width/height might overflow integers, so clamp them. | 159 // Subtracting to get width/height might overflow integers, so clamp them. |
| 160 SetRect(rx, ry, base::SaturatedSubtraction(rr, rx), | 160 int rdx = base::SaturatedSubtraction(rr, rx); |
| 161 base::SaturatedSubtraction(rb, ry)); | 161 int rdy = base::SaturatedSubtraction(rb, ry); |
| 162 | |
| 163 // clamping may move one side, choose to move the bigger by recomputing it. | |
| 164 // The computation of right is implicit, so this happens vacuosly. | |
| 165 // If Right is small, move left to width away. | |
| 166 // If Left is big(negative), move them both to keep the center. | |
| 167 if (rdx >= std::numeric_limits<int>::max() / 2) { | |
|
Peter Mayo
2017/03/14 18:35:59
Maybe just on equality.
| |
| 168 if (base::SaturatedAbsolute(rr) < rdx / 2) | |
| 169 rx = rr - rdx; | |
| 170 else if (base::SaturatedAbsolute(rx) > rdx / 2) | |
| 171 rx = rx + rdx / 4; | |
| 172 } | |
| 173 | |
| 174 // Same computation, other axis. | |
| 175 if (rdy >= std::numeric_limits<int>::max() / 2) { | |
| 176 if (base::SaturatedAbsolute(rb) < rdy / 2) | |
| 177 ry = rb - rdy; | |
| 178 else if (base::SaturatedAbsolute(ry) > rdy / 2) | |
| 179 ry = ry + rdy / 4; | |
| 180 } | |
| 181 | |
| 182 SetRect(rx, ry, rdx, rdy); | |
| 162 } | 183 } |
| 163 | 184 |
| 164 void Rect::Subtract(const Rect& rect) { | 185 void Rect::Subtract(const Rect& rect) { |
| 165 if (!Intersects(rect)) | 186 if (!Intersects(rect)) |
| 166 return; | 187 return; |
| 167 if (rect.Contains(*this)) { | 188 if (rect.Contains(*this)) { |
| 168 SetRect(0, 0, 0, 0); | 189 SetRect(0, 0, 0, 0); |
| 169 return; | 190 return; |
| 170 } | 191 } |
| 171 | 192 |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 293 | 314 |
| 294 Rect BoundingRect(const Point& p1, const Point& p2) { | 315 Rect BoundingRect(const Point& p1, const Point& p2) { |
| 295 int rx = std::min(p1.x(), p2.x()); | 316 int rx = std::min(p1.x(), p2.x()); |
| 296 int ry = std::min(p1.y(), p2.y()); | 317 int ry = std::min(p1.y(), p2.y()); |
| 297 int rr = std::max(p1.x(), p2.x()); | 318 int rr = std::max(p1.x(), p2.x()); |
| 298 int rb = std::max(p1.y(), p2.y()); | 319 int rb = std::max(p1.y(), p2.y()); |
| 299 return Rect(rx, ry, rr - rx, rb - ry); | 320 return Rect(rx, ry, rr - rx, rb - ry); |
| 300 } | 321 } |
| 301 | 322 |
| 302 } // namespace gfx | 323 } // namespace gfx |
| OLD | NEW |