Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / gfx / geometry / rect_f.cc
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
9 #include "base/logging.h"
10 #include "base/strings/stringprintf.h"
11 #include "ui/gfx/geometry/insets_f.h"
12 #include "ui/gfx/geometry/safe_integer_conversions.h"
13
14 namespace gfx {
15
16 static void AdjustAlongAxis(float dst_origin,
17                             float dst_size,
18                             float* origin,
19                             float* size) {
20   *size = std::min(dst_size, *size);
21   if (*origin < dst_origin)
22     *origin = dst_origin;
23   else
24     *origin = std::min(dst_origin + dst_size, *origin + *size) - *size;
25 }
26
27 void RectF::Inset(const InsetsF& insets) {
28   Inset(insets.left(), insets.top(), insets.right(), insets.bottom());
29 }
30
31 void RectF::Inset(float left, float top, float right, float bottom) {
32   origin_ += Vector2dF(left, top);
33   set_width(std::max(width() - left - right, static_cast<float>(0)));
34   set_height(std::max(height() - top - bottom, static_cast<float>(0)));
35 }
36
37 void RectF::Offset(float horizontal, float vertical) {
38   origin_ += Vector2dF(horizontal, vertical);
39 }
40
41 void RectF::operator+=(const Vector2dF& offset) {
42   origin_ += offset;
43 }
44
45 void RectF::operator-=(const Vector2dF& offset) {
46   origin_ -= offset;
47 }
48
49 InsetsF RectF::InsetsFrom(const RectF& inner) const {
50   return InsetsF(inner.y() - y(),
51                  inner.x() - x(),
52                  bottom() - inner.bottom(),
53                  right() - inner.right());
54 }
55
56 bool RectF::operator<(const RectF& other) const {
57   if (origin_ == other.origin_) {
58     if (width() == other.width()) {
59       return height() < other.height();
60     } else {
61       return width() < other.width();
62     }
63   } else {
64     return origin_ < other.origin_;
65   }
66 }
67
68 bool RectF::Contains(float point_x, float point_y) const {
69   return (point_x >= x()) && (point_x < right()) && (point_y >= y()) &&
70          (point_y < bottom());
71 }
72
73 bool RectF::Contains(const RectF& rect) const {
74   return (rect.x() >= x() && rect.right() <= right() && rect.y() >= y() &&
75           rect.bottom() <= bottom());
76 }
77
78 bool RectF::Intersects(const RectF& rect) const {
79   return !(IsEmpty() || rect.IsEmpty() || rect.x() >= right() ||
80            rect.right() <= x() || rect.y() >= bottom() || rect.bottom() <= y());
81 }
82
83 void RectF::Intersect(const RectF& rect) {
84   if (IsEmpty() || rect.IsEmpty()) {
85     SetRect(0, 0, 0, 0);
86     return;
87   }
88
89   float rx = std::max(x(), rect.x());
90   float ry = std::max(y(), rect.y());
91   float rr = std::min(right(), rect.right());
92   float rb = std::min(bottom(), rect.bottom());
93
94   if (rx >= rr || ry >= rb)
95     rx = ry = rr = rb = 0;  // non-intersecting
96
97   SetRect(rx, ry, rr - rx, rb - ry);
98 }
99
100 void RectF::Union(const RectF& rect) {
101   if (IsEmpty()) {
102     *this = rect;
103     return;
104   }
105   if (rect.IsEmpty())
106     return;
107
108   float rx = std::min(x(), rect.x());
109   float ry = std::min(y(), rect.y());
110   float rr = std::max(right(), rect.right());
111   float rb = std::max(bottom(), rect.bottom());
112
113   SetRect(rx, ry, rr - rx, rb - ry);
114 }
115
116 void RectF::Subtract(const RectF& rect) {
117   if (!Intersects(rect))
118     return;
119   if (rect.Contains(*static_cast<const RectF*>(this))) {
120     SetRect(0, 0, 0, 0);
121     return;
122   }
123
124   float rx = x();
125   float ry = y();
126   float rr = right();
127   float rb = bottom();
128
129   if (rect.y() <= y() && rect.bottom() >= bottom()) {
130     // complete intersection in the y-direction
131     if (rect.x() <= x()) {
132       rx = rect.right();
133     } else if (rect.right() >= right()) {
134       rr = rect.x();
135     }
136   } else if (rect.x() <= x() && rect.right() >= right()) {
137     // complete intersection in the x-direction
138     if (rect.y() <= y()) {
139       ry = rect.bottom();
140     } else if (rect.bottom() >= bottom()) {
141       rb = rect.y();
142     }
143   }
144   SetRect(rx, ry, rr - rx, rb - ry);
145 }
146
147 void RectF::AdjustToFit(const RectF& rect) {
148   float new_x = x();
149   float new_y = y();
150   float new_width = width();
151   float new_height = height();
152   AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width);
153   AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height);
154   SetRect(new_x, new_y, new_width, new_height);
155 }
156
157 PointF RectF::CenterPoint() const {
158   return PointF(x() + width() / 2, y() + height() / 2);
159 }
160
161 void RectF::ClampToCenteredSize(const SizeF& size) {
162   float new_width = std::min(width(), size.width());
163   float new_height = std::min(height(), size.height());
164   float new_x = x() + (width() - new_width) / 2;
165   float new_y = y() + (height() - new_height) / 2;
166   SetRect(new_x, new_y, new_width, new_height);
167 }
168
169 void RectF::SplitVertically(RectF* left_half, RectF* right_half) const {
170   DCHECK(left_half);
171   DCHECK(right_half);
172
173   left_half->SetRect(x(), y(), width() / 2, height());
174   right_half->SetRect(
175       left_half->right(), y(), width() - left_half->width(), height());
176 }
177
178 bool RectF::SharesEdgeWith(const RectF& rect) const {
179   return (y() == rect.y() && height() == rect.height() &&
180           (x() == rect.right() || right() == rect.x())) ||
181          (x() == rect.x() && width() == rect.width() &&
182           (y() == rect.bottom() || bottom() == rect.y()));
183 }
184
185 float RectF::ManhattanDistanceToPoint(const PointF& point) const {
186   float x_distance =
187       std::max<float>(0, std::max(x() - point.x(), point.x() - right()));
188   float y_distance =
189       std::max<float>(0, std::max(y() - point.y(), point.y() - bottom()));
190
191   return x_distance + y_distance;
192 }
193
194 float RectF::ManhattanInternalDistance(const RectF& rect) const {
195   RectF c(*this);
196   c.Union(rect);
197
198   static const float kEpsilon = std::numeric_limits<float>::is_integer
199                                     ? 1
200                                     : std::numeric_limits<float>::epsilon();
201
202   float x = std::max<float>(0, c.width() - width() - rect.width() + kEpsilon);
203   float y =
204       std::max<float>(0, c.height() - height() - rect.height() + kEpsilon);
205   return x + y;
206 }
207
208 bool RectF::IsExpressibleAsRect() const {
209   return IsExpressibleAsInt(x()) && IsExpressibleAsInt(y()) &&
210       IsExpressibleAsInt(width()) && IsExpressibleAsInt(height()) &&
211       IsExpressibleAsInt(right()) && IsExpressibleAsInt(bottom());
212 }
213
214 std::string RectF::ToString() const {
215   return base::StringPrintf("%s %s",
216                             origin().ToString().c_str(),
217                             size().ToString().c_str());
218 }
219
220 RectF IntersectRects(const RectF& a, const RectF& b) {
221   RectF result = a;
222   result.Intersect(b);
223   return result;
224 }
225
226 RectF UnionRects(const RectF& a, const RectF& b) {
227   RectF result = a;
228   result.Union(b);
229   return result;
230 }
231
232 RectF SubtractRects(const RectF& a, const RectF& b) {
233   RectF result = a;
234   result.Subtract(b);
235   return result;
236 }
237
238 RectF BoundingRect(const PointF& p1, const PointF& p2) {
239   float rx = std::min(p1.x(), p2.x());
240   float ry = std::min(p1.y(), p2.y());
241   float rr = std::max(p1.x(), p2.x());
242   float rb = std::max(p1.y(), p2.y());
243   return RectF(rx, ry, rr - rx, rb - ry);
244 }
245
246 }  // namespace gfx