• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #include "ui/gfx/geometry/rect_f.h"
6 
7 #include <algorithm>
8 #include <limits>
9 
10 #include "base/logging.h"
11 #include "base/strings/stringprintf.h"
12 #include "build/build_config.h"
13 #include "ui/gfx/geometry/insets_f.h"
14 #include "ui/gfx/geometry/safe_integer_conversions.h"
15 
16 #if defined(OS_IOS)
17 #include <CoreGraphics/CoreGraphics.h>
18 #elif defined(OS_MACOSX)
19 #include <ApplicationServices/ApplicationServices.h>
20 #endif
21 
22 namespace gfx {
23 
AdjustAlongAxis(float dst_origin,float dst_size,float * origin,float * size)24 static void AdjustAlongAxis(float dst_origin,
25                             float dst_size,
26                             float* origin,
27                             float* size) {
28   *size = std::min(dst_size, *size);
29   if (*origin < dst_origin)
30     *origin = dst_origin;
31   else
32     *origin = std::min(dst_origin + dst_size, *origin + *size) - *size;
33 }
34 
35 #if defined(OS_MACOSX)
RectF(const CGRect & r)36 RectF::RectF(const CGRect& r)
37     : origin_(r.origin.x, r.origin.y), size_(r.size.width, r.size.height) {
38 }
39 
ToCGRect() const40 CGRect RectF::ToCGRect() const {
41   return CGRectMake(x(), y(), width(), height());
42 }
43 #endif
44 
Inset(const InsetsF & insets)45 void RectF::Inset(const InsetsF& insets) {
46   Inset(insets.left(), insets.top(), insets.right(), insets.bottom());
47 }
48 
Inset(float left,float top,float right,float bottom)49 void RectF::Inset(float left, float top, float right, float bottom) {
50   origin_ += Vector2dF(left, top);
51   set_width(std::max(width() - left - right, 0.0f));
52   set_height(std::max(height() - top - bottom, 0.0f));
53 }
54 
Offset(float horizontal,float vertical)55 void RectF::Offset(float horizontal, float vertical) {
56   origin_ += Vector2dF(horizontal, vertical);
57 }
58 
operator +=(const Vector2dF & offset)59 void RectF::operator+=(const Vector2dF& offset) {
60   origin_ += offset;
61 }
62 
operator -=(const Vector2dF & offset)63 void RectF::operator-=(const Vector2dF& offset) {
64   origin_ -= offset;
65 }
66 
InsetsFrom(const RectF & inner) const67 InsetsF RectF::InsetsFrom(const RectF& inner) const {
68   return InsetsF(inner.y() - y(),
69                  inner.x() - x(),
70                  bottom() - inner.bottom(),
71                  right() - inner.right());
72 }
73 
operator <(const RectF & other) const74 bool RectF::operator<(const RectF& other) const {
75   if (origin_ != other.origin_)
76     return origin_ < other.origin_;
77 
78   if (width() == other.width())
79     return height() < other.height();
80   return width() < other.width();
81 }
82 
Contains(float point_x,float point_y) const83 bool RectF::Contains(float point_x, float point_y) const {
84   return point_x >= x() && point_x < right() && point_y >= y() &&
85          point_y < bottom();
86 }
87 
Contains(const RectF & rect) const88 bool RectF::Contains(const RectF& rect) const {
89   return rect.x() >= x() && rect.right() <= right() && rect.y() >= y() &&
90          rect.bottom() <= bottom();
91 }
92 
Intersects(const RectF & rect) const93 bool RectF::Intersects(const RectF& rect) const {
94   return !IsEmpty() && !rect.IsEmpty() && rect.x() < right() &&
95          rect.right() > x() && rect.y() < bottom() && rect.bottom() > y();
96 }
97 
Intersect(const RectF & rect)98 void RectF::Intersect(const RectF& rect) {
99   if (IsEmpty() || rect.IsEmpty()) {
100     SetRect(0, 0, 0, 0);
101     return;
102   }
103 
104   float rx = std::max(x(), rect.x());
105   float ry = std::max(y(), rect.y());
106   float rr = std::min(right(), rect.right());
107   float rb = std::min(bottom(), rect.bottom());
108 
109   if (rx >= rr || ry >= rb) {
110     SetRect(0, 0, 0, 0);
111     return;
112   }
113 
114   SetRect(rx, ry, rr - rx, rb - ry);
115 }
116 
Union(const RectF & rect)117 void RectF::Union(const RectF& rect) {
118   if (IsEmpty()) {
119     *this = rect;
120     return;
121   }
122   if (rect.IsEmpty())
123     return;
124 
125   float rx = std::min(x(), rect.x());
126   float ry = std::min(y(), rect.y());
127   float rr = std::max(right(), rect.right());
128   float rb = std::max(bottom(), rect.bottom());
129 
130   SetRect(rx, ry, rr - rx, rb - ry);
131 }
132 
Subtract(const RectF & rect)133 void RectF::Subtract(const RectF& rect) {
134   if (!Intersects(rect))
135     return;
136   if (rect.Contains(*this)) {
137     SetRect(0, 0, 0, 0);
138     return;
139   }
140 
141   float rx = x();
142   float ry = y();
143   float rr = right();
144   float rb = bottom();
145 
146   if (rect.y() <= y() && rect.bottom() >= bottom()) {
147     // complete intersection in the y-direction
148     if (rect.x() <= x()) {
149       rx = rect.right();
150     } else if (rect.right() >= right()) {
151       rr = rect.x();
152     }
153   } else if (rect.x() <= x() && rect.right() >= right()) {
154     // complete intersection in the x-direction
155     if (rect.y() <= y()) {
156       ry = rect.bottom();
157     } else if (rect.bottom() >= bottom()) {
158       rb = rect.y();
159     }
160   }
161   SetRect(rx, ry, rr - rx, rb - ry);
162 }
163 
AdjustToFit(const RectF & rect)164 void RectF::AdjustToFit(const RectF& rect) {
165   float new_x = x();
166   float new_y = y();
167   float new_width = width();
168   float new_height = height();
169   AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width);
170   AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height);
171   SetRect(new_x, new_y, new_width, new_height);
172 }
173 
CenterPoint() const174 PointF RectF::CenterPoint() const {
175   return PointF(x() + width() / 2, y() + height() / 2);
176 }
177 
ClampToCenteredSize(const SizeF & size)178 void RectF::ClampToCenteredSize(const SizeF& size) {
179   float new_width = std::min(width(), size.width());
180   float new_height = std::min(height(), size.height());
181   float new_x = x() + (width() - new_width) / 2;
182   float new_y = y() + (height() - new_height) / 2;
183   SetRect(new_x, new_y, new_width, new_height);
184 }
185 
SplitVertically(RectF * left_half,RectF * right_half) const186 void RectF::SplitVertically(RectF* left_half, RectF* right_half) const {
187   DCHECK(left_half);
188   DCHECK(right_half);
189 
190   left_half->SetRect(x(), y(), width() / 2, height());
191   right_half->SetRect(
192       left_half->right(), y(), width() - left_half->width(), height());
193 }
194 
SharesEdgeWith(const RectF & rect) const195 bool RectF::SharesEdgeWith(const RectF& rect) const {
196   return (y() == rect.y() && height() == rect.height() &&
197           (x() == rect.right() || right() == rect.x())) ||
198          (x() == rect.x() && width() == rect.width() &&
199           (y() == rect.bottom() || bottom() == rect.y()));
200 }
201 
ManhattanDistanceToPoint(const PointF & point) const202 float RectF::ManhattanDistanceToPoint(const PointF& point) const {
203   float x_distance =
204       std::max<float>(0, std::max(x() - point.x(), point.x() - right()));
205   float y_distance =
206       std::max<float>(0, std::max(y() - point.y(), point.y() - bottom()));
207 
208   return x_distance + y_distance;
209 }
210 
ManhattanInternalDistance(const RectF & rect) const211 float RectF::ManhattanInternalDistance(const RectF& rect) const {
212   RectF c(*this);
213   c.Union(rect);
214 
215   static constexpr float kEpsilon = std::numeric_limits<float>::epsilon();
216   float x = std::max(0.f, c.width() - width() - rect.width() + kEpsilon);
217   float y = std::max(0.f, c.height() - height() - rect.height() + kEpsilon);
218   return x + y;
219 }
220 
IsExpressibleAsRect() const221 bool RectF::IsExpressibleAsRect() const {
222   return IsExpressibleAsInt(x()) && IsExpressibleAsInt(y()) &&
223       IsExpressibleAsInt(width()) && IsExpressibleAsInt(height()) &&
224       IsExpressibleAsInt(right()) && IsExpressibleAsInt(bottom());
225 }
226 
ToString() const227 std::string RectF::ToString() const {
228   return base::StringPrintf("%s %s",
229                             origin().ToString().c_str(),
230                             size().ToString().c_str());
231 }
232 
IntersectRects(const RectF & a,const RectF & b)233 RectF IntersectRects(const RectF& a, const RectF& b) {
234   RectF result = a;
235   result.Intersect(b);
236   return result;
237 }
238 
UnionRects(const RectF & a,const RectF & b)239 RectF UnionRects(const RectF& a, const RectF& b) {
240   RectF result = a;
241   result.Union(b);
242   return result;
243 }
244 
SubtractRects(const RectF & a,const RectF & b)245 RectF SubtractRects(const RectF& a, const RectF& b) {
246   RectF result = a;
247   result.Subtract(b);
248   return result;
249 }
250 
BoundingRect(const PointF & p1,const PointF & p2)251 RectF BoundingRect(const PointF& p1, const PointF& p2) {
252   float rx = std::min(p1.x(), p2.x());
253   float ry = std::min(p1.y(), p2.y());
254   float rr = std::max(p1.x(), p2.x());
255   float rb = std::max(p1.y(), p2.y());
256   return RectF(rx, ry, rr - rx, rb - ry);
257 }
258 
259 }  // namespace gfx
260