2 * Copyright (c) 2012, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "platform/geometry/LayoutRect.h"
34 #include "platform/LayoutUnit.h"
35 #include "platform/geometry/FloatRect.h"
41 LayoutRect::LayoutRect(const FloatRect& r)
42 : m_location(LayoutPoint(r.location()))
43 , m_size(LayoutSize(r.size()))
47 bool LayoutRect::intersects(const LayoutRect& other) const
49 // Checking emptiness handles negative widths as well as zero.
50 return !isEmpty() && !other.isEmpty()
51 && x() < other.maxX() && other.x() < maxX()
52 && y() < other.maxY() && other.y() < maxY();
55 bool LayoutRect::contains(const LayoutRect& other) const
57 return x() <= other.x() && maxX() >= other.maxX()
58 && y() <= other.y() && maxY() >= other.maxY();
61 void LayoutRect::intersect(const LayoutRect& other)
63 LayoutPoint newLocation(std::max(x(), other.x()), std::max(y(), other.y()));
64 LayoutPoint newMaxPoint(std::min(maxX(), other.maxX()), std::min(maxY(), other.maxY()));
66 // Return a clean empty rectangle for non-intersecting cases.
67 if (newLocation.x() >= newMaxPoint.x() || newLocation.y() >= newMaxPoint.y()) {
68 newLocation = LayoutPoint(0, 0);
69 newMaxPoint = LayoutPoint(0, 0);
72 m_location = newLocation;
73 m_size = newMaxPoint - newLocation;
76 void LayoutRect::unite(const LayoutRect& other)
78 // Handle empty special cases first.
86 LayoutPoint newLocation(std::min(x(), other.x()), std::min(y(), other.y()));
87 LayoutPoint newMaxPoint(std::max(maxX(), other.maxX()), std::max(maxY(), other.maxY()));
89 m_location = newLocation;
90 m_size = newMaxPoint - newLocation;
93 void LayoutRect::uniteIfNonZero(const LayoutRect& other)
95 // Handle empty special cases first.
96 if (!other.width() && !other.height())
98 if (!width() && !height()) {
103 LayoutPoint newLocation(std::min(x(), other.x()), std::min(y(), other.y()));
104 LayoutPoint newMaxPoint(std::max(maxX(), other.maxX()), std::max(maxY(), other.maxY()));
106 m_location = newLocation;
107 m_size = newMaxPoint - newLocation;
110 void LayoutRect::scale(float s)
112 m_location.scale(s, s);
116 void LayoutRect::scale(float xAxisScale, float yAxisScale)
118 m_location.scale(xAxisScale, yAxisScale);
119 m_size.scale(xAxisScale, yAxisScale);
123 void LayoutRect::show(bool showRawValue) const
126 printf("Rect (in raw layout units): [x=%d y=%d maxX=%d maxY=%d]\n", x().rawValue(), y().rawValue(), maxX().rawValue(), maxY().rawValue());
128 printf("Rect (in pixels): [x=%lf y=%lf maxX=%lf maxY=%lf]\n", x().toDouble(), y().toDouble(), maxX().toDouble(), maxY().toDouble());
132 LayoutRect unionRect(const Vector<LayoutRect>& rects)
136 size_t count = rects.size();
137 for (size_t i = 0; i < count; ++i)
138 result.unite(rects[i]);
143 IntRect enclosingIntRect(const LayoutRect& rect)
145 IntPoint location = flooredIntPoint(rect.minXMinYCorner());
146 IntPoint maxPoint = ceiledIntPoint(rect.maxXMaxYCorner());
148 return IntRect(location, maxPoint - location);
151 LayoutRect enclosingLayoutRect(const FloatRect& rect)
153 LayoutPoint location = flooredLayoutPoint(rect.minXMinYCorner());
154 LayoutPoint maxPoint = ceiledLayoutPoint(rect.maxXMaxYCorner());
155 return LayoutRect(location, maxPoint - location);