• 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 #include <stdio.h>
38 
39 namespace WebCore {
40 
LayoutRect(const FloatRect & r)41 LayoutRect::LayoutRect(const FloatRect& r)
42     : m_location(LayoutPoint(r.location()))
43     , m_size(LayoutSize(r.size()))
44 {
45 }
46 
intersects(const LayoutRect & other) const47 bool LayoutRect::intersects(const LayoutRect& other) const
48 {
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();
53 }
54 
contains(const LayoutRect & other) const55 bool LayoutRect::contains(const LayoutRect& other) const
56 {
57     return x() <= other.x() && maxX() >= other.maxX()
58         && y() <= other.y() && maxY() >= other.maxY();
59 }
60 
intersect(const LayoutRect & other)61 void LayoutRect::intersect(const LayoutRect& other)
62 {
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()));
65 
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);
70     }
71 
72     m_location = newLocation;
73     m_size = newMaxPoint - newLocation;
74 }
75 
unite(const LayoutRect & other)76 void LayoutRect::unite(const LayoutRect& other)
77 {
78     // Handle empty special cases first.
79     if (other.isEmpty())
80         return;
81     if (isEmpty()) {
82         *this = other;
83         return;
84     }
85 
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()));
88 
89     m_location = newLocation;
90     m_size = newMaxPoint - newLocation;
91 }
92 
uniteIfNonZero(const LayoutRect & other)93 void LayoutRect::uniteIfNonZero(const LayoutRect& other)
94 {
95     // Handle empty special cases first.
96     if (!other.width() && !other.height())
97         return;
98     if (!width() && !height()) {
99         *this = other;
100         return;
101     }
102 
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()));
105 
106     m_location = newLocation;
107     m_size = newMaxPoint - newLocation;
108 }
109 
scale(float s)110 void LayoutRect::scale(float s)
111 {
112     m_location.scale(s, s);
113     m_size.scale(s);
114 }
115 
scale(float xAxisScale,float yAxisScale)116 void LayoutRect::scale(float xAxisScale, float yAxisScale)
117 {
118     m_location.scale(xAxisScale, yAxisScale);
119     m_size.scale(xAxisScale, yAxisScale);
120 }
121 
122 #ifndef NDEBUG
show(bool showRawValue) const123 void LayoutRect::show(bool showRawValue) const
124 {
125     if (showRawValue)
126         printf("Rect (in raw layout units): [x=%d y=%d maxX=%d maxY=%d]\n", x().rawValue(), y().rawValue(), maxX().rawValue(), maxY().rawValue());
127     else
128         printf("Rect (in pixels): [x=%lf y=%lf maxX=%lf maxY=%lf]\n", x().toDouble(), y().toDouble(), maxX().toDouble(), maxY().toDouble());
129 }
130 #endif
131 
unionRect(const Vector<LayoutRect> & rects)132 LayoutRect unionRect(const Vector<LayoutRect>& rects)
133 {
134     LayoutRect result;
135 
136     size_t count = rects.size();
137     for (size_t i = 0; i < count; ++i)
138         result.unite(rects[i]);
139 
140     return result;
141 }
142 
enclosingIntRect(const LayoutRect & rect)143 IntRect enclosingIntRect(const LayoutRect& rect)
144 {
145     IntPoint location = flooredIntPoint(rect.minXMinYCorner());
146     IntPoint maxPoint = ceiledIntPoint(rect.maxXMaxYCorner());
147 
148     return IntRect(location, maxPoint - location);
149 }
150 
enclosingLayoutRect(const FloatRect & rect)151 LayoutRect enclosingLayoutRect(const FloatRect& rect)
152 {
153     LayoutPoint location = flooredLayoutPoint(rect.minXMinYCorner());
154     LayoutPoint maxPoint = ceiledLayoutPoint(rect.maxXMaxYCorner());
155     return LayoutRect(location, maxPoint - location);
156 }
157 
158 } // namespace WebCore
159