initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / FloatRect.cpp
1 /*
2  * Copyright (C) 2003, 2006, 2007 Apple Inc.  All rights reserved.
3  * Copyright (C) 2005 Nokia.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28 #include "FloatRect.h"
29
30 #include "FloatConversion.h"
31 #include "IntRect.h"
32 #include <algorithm>
33 #include <math.h>
34 #include <wtf/MathExtras.h>
35
36 using std::max;
37 using std::min;
38
39 namespace WebCore {
40
41 FloatRect::FloatRect(const IntRect& r) : m_location(r.location()), m_size(r.size())
42 {
43 }
44
45 FloatRect FloatRect::narrowPrecision(double x, double y, double width, double height)
46 {
47     return FloatRect(narrowPrecisionToFloat(x), narrowPrecisionToFloat(y), narrowPrecisionToFloat(width), narrowPrecisionToFloat(height));
48 }
49
50 bool FloatRect::isExpressibleAsIntRect() const
51 {
52     return isWithinIntRange(x()) && isWithinIntRange(y())
53         && isWithinIntRange(width()) && isWithinIntRange(height())
54         && isWithinIntRange(maxX()) && isWithinIntRange(maxY());
55 }
56
57 bool FloatRect::intersects(const FloatRect& other) const
58 {
59     // Checking emptiness handles negative widths as well as zero.
60     return !isEmpty() && !other.isEmpty()
61         && x() < other.maxX() && other.x() < maxX()
62         && y() < other.maxY() && other.y() < maxY();
63 }
64
65 bool FloatRect::contains(const FloatRect& other) const
66 {
67     return x() <= other.x() && maxX() >= other.maxX()
68         && y() <= other.y() && maxY() >= other.maxY();
69 }
70
71 void FloatRect::intersect(const FloatRect& other)
72 {
73     float l = max(x(), other.x());
74     float t = max(y(), other.y());
75     float r = min(maxX(), other.maxX());
76     float b = min(maxY(), other.maxY());
77
78     // Return a clean empty rectangle for non-intersecting cases.
79     if (l >= r || t >= b) {
80         l = 0;
81         t = 0;
82         r = 0;
83         b = 0;
84     }
85
86     setLocationAndSizeFromEdges(l, t, r, b);
87 }
88
89 void FloatRect::unite(const FloatRect& other)
90 {
91     // Handle empty special cases first.
92     if (other.isEmpty())
93         return;
94     if (isEmpty()) {
95         *this = other;
96         return;
97     }
98
99     float l = min(x(), other.x());
100     float t = min(y(), other.y());
101     float r = max(maxX(), other.maxX());
102     float b = max(maxY(), other.maxY());
103
104     setLocationAndSizeFromEdges(l, t, r, b);
105 }
106
107 void FloatRect::uniteIfNonZero(const FloatRect& other)
108 {
109     // Handle empty special cases first.
110     if (!other.width() && !other.height())
111         return;
112     if (!width() && !height()) {
113         *this = other;
114         return;
115     }
116
117     float left = min(x(), other.x());
118     float top = min(y(), other.y());
119     float right = max(maxX(), other.maxX());
120     float bottom = max(maxY(), other.maxY());
121
122     setLocationAndSizeFromEdges(left, top, right, bottom);
123 }
124
125 void FloatRect::scale(float sx, float sy)
126 {
127     m_location.setX(x() * sx);
128     m_location.setY(y() * sy);
129     m_size.setWidth(width() * sx);
130     m_size.setHeight(height() * sy);
131 }
132
133 void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1)
134 {
135     float left = min(p0.x(), p1.x());
136     float top = min(p0.y(), p1.y());
137     float right = max(p0.x(), p1.x());
138     float bottom = max(p0.y(), p1.y());
139
140     setLocationAndSizeFromEdges(left, top, right, bottom);
141 }
142
143 namespace {
144 // Helpers for 3- and 4-way max and min.
145
146 template <typename T>
147 T min3(const T& v1, const T& v2, const T& v3)
148 {
149     return min(min(v1, v2), v3);
150 }
151
152 template <typename T>
153 T max3(const T& v1, const T& v2, const T& v3)
154 {
155     return max(max(v1, v2), v3);
156 }
157
158 template <typename T>
159 T min4(const T& v1, const T& v2, const T& v3, const T& v4)
160 {
161     return min(min(v1, v2), min(v3, v4));
162 }
163
164 template <typename T>
165 T max4(const T& v1, const T& v2, const T& v3, const T& v4)
166 {
167     return max(max(v1, v2), max(v3, v4));
168 }
169
170 } // anonymous namespace
171
172 void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const FloatPoint& p2)
173 {
174     float left = min3(p0.x(), p1.x(), p2.x());
175     float top = min3(p0.y(), p1.y(), p2.y());
176     float right = max3(p0.x(), p1.x(), p2.x());
177     float bottom = max3(p0.y(), p1.y(), p2.y());
178
179     setLocationAndSizeFromEdges(left, top, right, bottom);
180 }
181
182 void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& p3)
183 {
184     float left = min4(p0.x(), p1.x(), p2.x(), p3.x());
185     float top = min4(p0.y(), p1.y(), p2.y(), p3.y());
186     float right = max4(p0.x(), p1.x(), p2.x(), p3.x());
187     float bottom = max4(p0.y(), p1.y(), p2.y(), p3.y());
188
189     setLocationAndSizeFromEdges(left, top, right, bottom);
190 }
191
192 IntRect enclosingIntRect(const FloatRect& rect)
193 {
194     float left = floorf(rect.x());
195     float top = floorf(rect.y());
196     float width = ceilf(rect.maxX()) - left;
197     float height = ceilf(rect.maxY()) - top;
198     
199     return IntRect(clampToInteger(left), clampToInteger(top), 
200                    clampToInteger(width), clampToInteger(height));
201 }
202
203 FloatRect mapRect(const FloatRect& r, const FloatRect& srcRect, const FloatRect& destRect)
204 {
205     if (srcRect.width() == 0 || srcRect.height() == 0)
206         return FloatRect();
207
208     float widthScale = destRect.width() / srcRect.width();
209     float heightScale = destRect.height() / srcRect.height();
210     return FloatRect(destRect.x() + (r.x() - srcRect.x()) * widthScale,
211                      destRect.y() + (r.y() - srcRect.y()) * heightScale,
212                      r.width() * widthScale, r.height() * heightScale);
213 }
214
215 }