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

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

Issue 2744423002: Handle large rects better. (Closed)
Patch Set: Rebased, Use HasHeight, Symmetric test 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
« no previous file with comments | « ui/gfx/geometry/rect.cc ('k') | ui/gfx/geometry/rect_unittest.cc » ('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_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.
Peter Mayo 2017/03/23 00:01:31 Remove comment - Not relevant any more.
17 int min_y = ToFlooredInt(rect.y()); 17 Rect ToEnclosingRect(const RectF& r) {
18 float max_x = rect.right(); 18 int left = ToFlooredInt(r.x());
19 float max_y = rect.bottom(); 19 int right = r.size().HasWidth() ? ToCeiledInt(r.right()) : left;
20 int width = rect.size().HasWidth() 20 int top = ToFlooredInt(r.y());
21 ? ToCeiledInt(static_cast<double>(ToCeiledInt(max_x)) - min_x) 21 int bottom = r.size().HasHeight() ? ToCeiledInt(r.bottom()) : top;
22 : 0; 22
23 int height = 23 Rect result;
24 rect.size().HasHeight() 24 result.SetByBounds(left, right, top, bottom);
25 ? ToCeiledInt(static_cast<double>(ToCeiledInt(max_y)) - min_y) 25 return result;
26 : 0;
27 return Rect(min_x, min_y, width, height);
28 } 26 }
29 27
30 Rect ToEnclosedRect(const RectF& rect) { 28 Rect ToEnclosedRect(const RectF& rect) {
31 int min_x = ToCeiledInt(rect.x()); 29 Rect result;
32 int min_y = ToCeiledInt(rect.y()); 30 result.SetByBounds(ToCeiledInt(rect.x()), ToFlooredInt(rect.right()),
33 float max_x = rect.right(); 31 ToCeiledInt(rect.y()), ToFlooredInt(rect.bottom()));
34 float max_y = rect.bottom(); 32 return result;
35 int width = std::max(
36 ToFlooredInt(static_cast<float>(ToFlooredInt(max_x)) - min_x), 0);
37 int height = std::max(
38 ToFlooredInt(static_cast<float>(ToFlooredInt(max_y)) - min_y), 0);
39 return Rect(min_x, min_y, width, height);
40 } 33 }
41 34
42 Rect ToNearestRect(const RectF& rect) { 35 Rect ToNearestRect(const RectF& rect) {
43 float float_min_x = rect.x(); 36 float float_min_x = rect.x();
44 float float_min_y = rect.y(); 37 float float_min_y = rect.y();
45 float float_max_x = rect.right(); 38 float float_max_x = rect.right();
46 float float_max_y = rect.bottom(); 39 float float_max_y = rect.bottom();
47 40
48 int min_x = ToRoundedInt(float_min_x); 41 int min_x = ToRoundedInt(float_min_x);
49 int min_y = ToRoundedInt(float_min_y); 42 int min_y = ToRoundedInt(float_min_y);
50 int max_x = ToRoundedInt(float_max_x); 43 int max_x = ToRoundedInt(float_max_x);
51 int max_y = ToRoundedInt(float_max_y); 44 int max_y = ToRoundedInt(float_max_y);
52 45
53 // If these DCHECKs fail, you're using the wrong method, consider using 46 // If these DCHECKs fail, you're using the wrong method, consider using
54 // ToEnclosingRect or ToEnclosedRect instead. 47 // ToEnclosingRect or ToEnclosedRect instead.
55 DCHECK(std::abs(min_x - float_min_x) < 0.01f); 48 DCHECK(std::abs(min_x - float_min_x) < 0.01f);
56 DCHECK(std::abs(min_y - float_min_y) < 0.01f); 49 DCHECK(std::abs(min_y - float_min_y) < 0.01f);
57 DCHECK(std::abs(max_x - float_max_x) < 0.01f); 50 DCHECK(std::abs(max_x - float_max_x) < 0.01f);
58 DCHECK(std::abs(max_y - float_max_y) < 0.01f); 51 DCHECK(std::abs(max_y - float_max_y) < 0.01f);
59 52
60 return Rect(min_x, min_y, max_x - min_x, max_y - min_y); 53 Rect result;
54 result.SetByBounds(min_x, max_x, min_y, max_y);
55
56 return result;
61 } 57 }
62 58
63 bool IsNearestRectWithinDistance(const gfx::RectF& rect, float distance) { 59 bool IsNearestRectWithinDistance(const gfx::RectF& rect, float distance) {
64 float float_min_x = rect.x(); 60 float float_min_x = rect.x();
65 float float_min_y = rect.y(); 61 float float_min_y = rect.y();
66 float float_max_x = rect.right(); 62 float float_max_x = rect.right();
67 float float_max_y = rect.bottom(); 63 float float_max_y = rect.bottom();
68 64
69 int min_x = ToRoundedInt(float_min_x); 65 int min_x = ToRoundedInt(float_min_x);
70 int min_y = ToRoundedInt(float_min_y); 66 int min_y = ToRoundedInt(float_min_y);
71 int max_x = ToRoundedInt(float_max_x); 67 int max_x = ToRoundedInt(float_max_x);
72 int max_y = ToRoundedInt(float_max_y); 68 int max_y = ToRoundedInt(float_max_y);
73 69
74 return 70 return
75 (std::abs(min_x - float_min_x) < distance) && 71 (std::abs(min_x - float_min_x) < distance) &&
76 (std::abs(min_y - float_min_y) < distance) && 72 (std::abs(min_y - float_min_y) < distance) &&
77 (std::abs(max_x - float_max_x) < distance) && 73 (std::abs(max_x - float_max_x) < distance) &&
78 (std::abs(max_y - float_max_y) < distance); 74 (std::abs(max_y - float_max_y) < distance);
79 } 75 }
80 76
81 Rect ToFlooredRectDeprecated(const RectF& rect) { 77 Rect ToFlooredRectDeprecated(const RectF& rect) {
82 return Rect(ToFlooredInt(rect.x()), 78 return Rect(ToFlooredInt(rect.x()),
83 ToFlooredInt(rect.y()), 79 ToFlooredInt(rect.y()),
84 ToFlooredInt(rect.width()), 80 ToFlooredInt(rect.width()),
85 ToFlooredInt(rect.height())); 81 ToFlooredInt(rect.height()));
Peter Mayo 2017/03/23 00:01:31 This should use the improved heuristic too I suppo
86 } 82 }
87 83
88 } // namespace gfx 84 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/geometry/rect.cc ('k') | ui/gfx/geometry/rect_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698