• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2012, Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30 
31 #include "config.h"
32 #include "platform/geometry/LayoutRect.h"
33 
34 #include "platform/LayoutUnit.h"
35 #include "platform/geometry/FloatRect.h"
36 #include <algorithm>
37 
38 namespace WebCore {
39 
LayoutRect(const FloatRect & r)40 LayoutRect::LayoutRect(const FloatRect& r)
41     : m_location(LayoutPoint(r.location()))
42     , m_size(LayoutSize(r.size()))
43 {
44 }
45 
intersects(const LayoutRect & other) const46 bool LayoutRect::intersects(const LayoutRect& other) const
47 {
48     // Checking emptiness handles negative widths as well as zero.
49     return !isEmpty() && !other.isEmpty()
50         && x() < other.maxX() && other.x() < maxX()
51         && y() < other.maxY() && other.y() < maxY();
52 }
53 
contains(const LayoutRect & other) const54 bool LayoutRect::contains(const LayoutRect& other) const
55 {
56     return x() <= other.x() && maxX() >= other.maxX()
57         && y() <= other.y() && maxY() >= other.maxY();
58 }
59 
intersect(const LayoutRect & other)60 void LayoutRect::intersect(const LayoutRect& other)
61 {
62     LayoutPoint newLocation(std::max(x(), other.x()), std::max(y(), other.y()));
63     LayoutPoint newMaxPoint(std::min(maxX(), other.maxX()), std::min(maxY(), other.maxY()));
64 
65     // Return a clean empty rectangle for non-intersecting cases.
66     if (newLocation.x() >= newMaxPoint.x() || newLocation.y() >= newMaxPoint.y()) {
67         newLocation = LayoutPoint(0, 0);
68         newMaxPoint = LayoutPoint(0, 0);
69     }
70 
71     m_location = newLocation;
72     m_size = newMaxPoint - newLocation;
73 }
74 
unite(const LayoutRect & other)75 void LayoutRect::unite(const LayoutRect& other)
76 {
77     // Handle empty special cases first.
78     if (other.isEmpty())
79         return;
80     if (isEmpty()) {
81         *this = other;
82         return;
83     }
84 
85     LayoutPoint newLocation(std::min(x(), other.x()), std::min(y(), other.y()));
86     LayoutPoint newMaxPoint(std::max(maxX(), other.maxX()), std::max(maxY(), other.maxY()));
87 
88     m_location = newLocation;
89     m_size = newMaxPoint - newLocation;
90 }
91 
uniteIfNonZero(const LayoutRect & other)92 void LayoutRect::uniteIfNonZero(const LayoutRect& other)
93 {
94     // Handle empty special cases first.
95     if (!other.width() && !other.height())
96         return;
97     if (!width() && !height()) {
98         *this = other;
99         return;
100     }
101 
102     LayoutPoint newLocation(std::min(x(), other.x()), std::min(y(), other.y()));
103     LayoutPoint newMaxPoint(std::max(maxX(), other.maxX()), std::max(maxY(), other.maxY()));
104 
105     m_location = newLocation;
106     m_size = newMaxPoint - newLocation;
107 }
108 
scale(float s)109 void LayoutRect::scale(float s)
110 {
111     m_location.scale(s, s);
112     m_size.scale(s);
113 }
114 
scale(float xAxisScale,float yAxisScale)115 void LayoutRect::scale(float xAxisScale, float yAxisScale)
116 {
117     m_location.scale(xAxisScale, yAxisScale);
118     m_size.scale(xAxisScale, yAxisScale);
119 }
120 
unionRect(const Vector<LayoutRect> & rects)121 LayoutRect unionRect(const Vector<LayoutRect>& rects)
122 {
123     LayoutRect result;
124 
125     size_t count = rects.size();
126     for (size_t i = 0; i < count; ++i)
127         result.unite(rects[i]);
128 
129     return result;
130 }
131 
enclosingIntRect(const LayoutRect & rect)132 IntRect enclosingIntRect(const LayoutRect& rect)
133 {
134     IntPoint location = flooredIntPoint(rect.minXMinYCorner());
135     IntPoint maxPoint = ceiledIntPoint(rect.maxXMaxYCorner());
136 
137     return IntRect(location, maxPoint - location);
138 }
139 
enclosingLayoutRect(const FloatRect & rect)140 LayoutRect enclosingLayoutRect(const FloatRect& rect)
141 {
142     LayoutPoint location = flooredLayoutPoint(rect.minXMinYCorner());
143     LayoutPoint maxPoint = ceiledLayoutPoint(rect.maxXMaxYCorner());
144     return LayoutRect(location, maxPoint - location);
145 }
146 
147 } // namespace WebCore
148