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

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

Issue 2744423002: Handle large rects better. (Closed)
Patch Set: Followup fixes Created 3 years, 9 months 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
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_conversions.h" 5 #include "ui/gfx/geometry/rect_conversions.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "ui/gfx/geometry/safe_integer_conversions.h" 11 #include "ui/gfx/geometry/safe_integer_conversions.h"
12 12
13 namespace gfx { 13 namespace gfx {
14 14
15 Rect ToEnclosingRect(const RectF& rect) { 15 // This is the minimum size of a float rect dimension for use to include it
16 int min_x = ToFlooredInt(rect.x()); 16 // in the enclosing int rect.
17 int min_y = ToFlooredInt(rect.y()); 17 static const float kEmptyDimension = 0.1f;
18 float max_x = rect.right(); 18 Rect ToEnclosingRect(const RectF& r) {
19 float max_y = rect.bottom(); 19 int left = ToFlooredInt(r.x());
20 int width = 20 int right = r.width() < kEmptyDimension ? left : ToCeiledInt(r.right());
21 rect.width() == 0 21 int top = ToFlooredInt(r.y());
22 ? 0 22 int bottom = r.height() < kEmptyDimension ? top : ToCeiledInt(r.bottom());
23 : std::max( 23
24 ToCeiledInt(static_cast<double>(ToCeiledInt(max_x)) - min_x), 24 Rect result;
25 0); 25 result.SetByBounds(left, right, top, bottom);
26 int height = 26 return result;
27 rect.height() == 0
28 ? 0
29 : std::max(
30 ToCeiledInt(static_cast<double>(ToCeiledInt(max_y)) - min_y),
31 0);
32 return Rect(min_x, min_y, width, height);
33 } 27 }
34 28
35 Rect ToEnclosedRect(const RectF& rect) { 29 Rect ToEnclosedRect(const RectF& rect) {
36 int min_x = ToCeiledInt(rect.x()); 30 Rect result;
37 int min_y = ToCeiledInt(rect.y()); 31 result.SetByBounds(ToCeiledInt(rect.x()), ToFlooredInt(rect.right()),
38 float max_x = rect.right(); 32 ToCeiledInt(rect.y()), ToFlooredInt(rect.bottom()));
39 float max_y = rect.bottom(); 33 return result;
40 int width = std::max(
41 ToFlooredInt(static_cast<float>(ToFlooredInt(max_x)) - min_x), 0);
42 int height = std::max(
43 ToFlooredInt(static_cast<float>(ToFlooredInt(max_y)) - min_y), 0);
44 return Rect(min_x, min_y, width, height);
45 } 34 }
46 35
47 Rect ToNearestRect(const RectF& rect) { 36 Rect ToNearestRect(const RectF& rect) {
48 float float_min_x = rect.x(); 37 float float_min_x = rect.x();
49 float float_min_y = rect.y(); 38 float float_min_y = rect.y();
50 float float_max_x = rect.right(); 39 float float_max_x = rect.right();
51 float float_max_y = rect.bottom(); 40 float float_max_y = rect.bottom();
52 41
53 int min_x = ToRoundedInt(float_min_x); 42 int min_x = ToRoundedInt(float_min_x);
54 int min_y = ToRoundedInt(float_min_y); 43 int min_y = ToRoundedInt(float_min_y);
55 int max_x = ToRoundedInt(float_max_x); 44 int max_x = ToRoundedInt(float_max_x);
56 int max_y = ToRoundedInt(float_max_y); 45 int max_y = ToRoundedInt(float_max_y);
57 46
58 // If these DCHECKs fail, you're using the wrong method, consider using 47 // If these DCHECKs fail, you're using the wrong method, consider using
59 // ToEnclosingRect or ToEnclosedRect instead. 48 // ToEnclosingRect or ToEnclosedRect instead.
60 DCHECK(std::abs(min_x - float_min_x) < 0.01f); 49 DCHECK(std::abs(min_x - float_min_x) < 0.01f);
61 DCHECK(std::abs(min_y - float_min_y) < 0.01f); 50 DCHECK(std::abs(min_y - float_min_y) < 0.01f);
62 DCHECK(std::abs(max_x - float_max_x) < 0.01f); 51 DCHECK(std::abs(max_x - float_max_x) < 0.01f);
63 DCHECK(std::abs(max_y - float_max_y) < 0.01f); 52 DCHECK(std::abs(max_y - float_max_y) < 0.01f);
64 53
65 return Rect(min_x, min_y, max_x - min_x, max_y - min_y); 54 Rect result;
55 result.SetByBounds(min_x, max_x, min_y, max_y);
56
57 return result;
66 } 58 }
67 59
68 bool IsNearestRectWithinDistance(const gfx::RectF& rect, float distance) { 60 bool IsNearestRectWithinDistance(const gfx::RectF& rect, float distance) {
69 float float_min_x = rect.x(); 61 float float_min_x = rect.x();
70 float float_min_y = rect.y(); 62 float float_min_y = rect.y();
71 float float_max_x = rect.right(); 63 float float_max_x = rect.right();
72 float float_max_y = rect.bottom(); 64 float float_max_y = rect.bottom();
73 65
74 int min_x = ToRoundedInt(float_min_x); 66 int min_x = ToRoundedInt(float_min_x);
75 int min_y = ToRoundedInt(float_min_y); 67 int min_y = ToRoundedInt(float_min_y);
76 int max_x = ToRoundedInt(float_max_x); 68 int max_x = ToRoundedInt(float_max_x);
77 int max_y = ToRoundedInt(float_max_y); 69 int max_y = ToRoundedInt(float_max_y);
78 70
79 return 71 return
80 (std::abs(min_x - float_min_x) < distance) && 72 (std::abs(min_x - float_min_x) < distance) &&
81 (std::abs(min_y - float_min_y) < distance) && 73 (std::abs(min_y - float_min_y) < distance) &&
82 (std::abs(max_x - float_max_x) < distance) && 74 (std::abs(max_x - float_max_x) < distance) &&
83 (std::abs(max_y - float_max_y) < distance); 75 (std::abs(max_y - float_max_y) < distance);
84 } 76 }
85 77
86 Rect ToFlooredRectDeprecated(const RectF& rect) { 78 Rect ToFlooredRectDeprecated(const RectF& rect) {
87 return Rect(ToFlooredInt(rect.x()), 79 return Rect(ToFlooredInt(rect.x()),
88 ToFlooredInt(rect.y()), 80 ToFlooredInt(rect.y()),
89 ToFlooredInt(rect.width()), 81 ToFlooredInt(rect.width()),
90 ToFlooredInt(rect.height())); 82 ToFlooredInt(rect.height()));
91 } 83 }
92 84
93 } // namespace gfx 85 } // namespace gfx
94 86
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698