Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(156)

Side by Side Diff: ui/gfx/geometry/rect.cc

Issue 2499783002: Move SaturatedArithmetic from Blink to base (Closed)
Patch Set: Revert flag addition Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gfx/geometry/rect.h ('k') | ui/gfx/geometry/safe_integer_conversions.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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>
11 #elif defined(OS_IOS) 11 #elif defined(OS_IOS)
12 #include <CoreGraphics/CoreGraphics.h> 12 #include <CoreGraphics/CoreGraphics.h>
13 #elif defined(OS_MACOSX) 13 #elif defined(OS_MACOSX)
14 #include <ApplicationServices/ApplicationServices.h> 14 #include <ApplicationServices/ApplicationServices.h>
15 #endif 15 #endif
16 16
17 #include "base/logging.h" 17 #include "base/logging.h"
18 #include "base/numerics/saturated_arithmetic.h"
18 #include "base/strings/stringprintf.h" 19 #include "base/strings/stringprintf.h"
19 #include "build/build_config.h" 20 #include "build/build_config.h"
20 #include "ui/gfx/geometry/insets.h" 21 #include "ui/gfx/geometry/insets.h"
21 22
22 namespace gfx { 23 namespace gfx {
23 24
24 #if defined(OS_WIN) 25 #if defined(OS_WIN)
25 Rect::Rect(const RECT& r) 26 Rect::Rect(const RECT& r)
26 : origin_(r.left, r.top), 27 : origin_(r.left, r.top),
27 size_(std::abs(r.right - r.left), std::abs(r.bottom - r.top)) { 28 size_(std::abs(r.right - r.left), std::abs(r.bottom - r.top)) {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 namespace gfx { 61 namespace gfx {
61 62
62 void Rect::Inset(const Insets& insets) { 63 void Rect::Inset(const Insets& insets) {
63 Inset(insets.left(), insets.top(), insets.right(), insets.bottom()); 64 Inset(insets.left(), insets.top(), insets.right(), insets.bottom());
64 } 65 }
65 66
66 void Rect::Inset(int left, int top, int right, int bottom) { 67 void Rect::Inset(int left, int top, int right, int bottom) {
67 origin_ += Vector2d(left, top); 68 origin_ += Vector2d(left, top);
68 // left+right might overflow/underflow, but width() - (left+right) might 69 // left+right might overflow/underflow, but width() - (left+right) might
69 // overflow as well. 70 // overflow as well.
70 set_width(SafeSubtract(width(), SafeAdd(left, right))); 71 set_width(base::SaturatedSubtraction(width(),
71 set_height(SafeSubtract(height(), SafeAdd(top, bottom))); 72 base::SaturatedAddition(left, right)));
73 set_height(base::SaturatedSubtraction(height(),
74 base::SaturatedAddition(top, bottom)));
72 } 75 }
73 76
74 void Rect::Offset(int horizontal, int vertical) { 77 void Rect::Offset(int horizontal, int vertical) {
75 origin_ += Vector2d(horizontal, vertical); 78 origin_ += Vector2d(horizontal, vertical);
76 // Ensure that width and height remain valid. 79 // Ensure that width and height remain valid.
77 set_width(width()); 80 set_width(width());
78 set_height(height()); 81 set_height(height());
79 } 82 }
80 83
81 void Rect::operator+=(const Vector2d& offset) { 84 void Rect::operator+=(const Vector2d& offset) {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 } 150 }
148 if (rect.IsEmpty()) 151 if (rect.IsEmpty())
149 return; 152 return;
150 153
151 int rx = std::min(x(), rect.x()); 154 int rx = std::min(x(), rect.x());
152 int ry = std::min(y(), rect.y()); 155 int ry = std::min(y(), rect.y());
153 int rr = std::max(right(), rect.right()); 156 int rr = std::max(right(), rect.right());
154 int rb = std::max(bottom(), rect.bottom()); 157 int rb = std::max(bottom(), rect.bottom());
155 158
156 // Subtracting to get width/height might overflow integers, so clamp them. 159 // Subtracting to get width/height might overflow integers, so clamp them.
157 SetRect(rx, ry, GetClampedWidthFromExtents(rx, rr), 160 SetRect(rx, ry, base::SaturatedSubtraction(rr, rx),
158 GetClampedWidthFromExtents(ry, rb)); 161 base::SaturatedSubtraction(rb, ry));
159 } 162 }
160 163
161 void Rect::Subtract(const Rect& rect) { 164 void Rect::Subtract(const Rect& rect) {
162 if (!Intersects(rect)) 165 if (!Intersects(rect))
163 return; 166 return;
164 if (rect.Contains(*this)) { 167 if (rect.Contains(*this)) {
165 SetRect(0, 0, 0, 0); 168 SetRect(0, 0, 0, 0);
166 return; 169 return;
167 } 170 }
168 171
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 293
291 Rect BoundingRect(const Point& p1, const Point& p2) { 294 Rect BoundingRect(const Point& p1, const Point& p2) {
292 int rx = std::min(p1.x(), p2.x()); 295 int rx = std::min(p1.x(), p2.x());
293 int ry = std::min(p1.y(), p2.y()); 296 int ry = std::min(p1.y(), p2.y());
294 int rr = std::max(p1.x(), p2.x()); 297 int rr = std::max(p1.x(), p2.x());
295 int rb = std::max(p1.y(), p2.y()); 298 int rb = std::max(p1.y(), p2.y());
296 return Rect(rx, ry, rr - rx, rb - ry); 299 return Rect(rx, ry, rr - rx, rb - ry);
297 } 300 }
298 301
299 } // namespace gfx 302 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/geometry/rect.h ('k') | ui/gfx/geometry/safe_integer_conversions.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698