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 "FloatRect.h"
28 
29 #include "FloatConversion.h"
30 #include <algorithm>
31 
32 using std::max;
33 using std::min;
34 
35 namespace WebCore
36 {
37 
FloatRect(const IntRect & r)38 FloatRect::FloatRect(const IntRect &r) : m_location(r.location()), m_size(r.size())
39 {
40 }
41 
narrowPrecision(double x,double y,double width,double height)42 FloatRect FloatRect::narrowPrecision(double x, double y, double width, double height)
43 {
44     return FloatRect(narrowPrecisionToFloat(x), narrowPrecisionToFloat(y), narrowPrecisionToFloat(width), narrowPrecisionToFloat(height));
45 }
46 
intersects(const FloatRect & other) const47 bool FloatRect::intersects(const FloatRect &other) const
48 {
49     // Checking emptiness handles negative widths as well as zero.
50     return !isEmpty() && !other.isEmpty()
51            && x() < other.right() && other.x() < right()
52            && y() < other.bottom() && other.y() < bottom();
53 }
54 
contains(const FloatRect & other) const55 bool FloatRect::contains(const FloatRect &other) const
56 {
57     return x() <= other.x() && right() >= other.right()
58            && y() <= other.y() && bottom() >= other.bottom();
59 }
60 
intersect(const FloatRect & other)61 void FloatRect::intersect(const FloatRect &other)
62 {
63     float l = max(x(), other.x());
64     float t = max(y(), other.y());
65     float r = min(right(), other.right());
66     float b = min(bottom(), other.bottom());
67 
68     // Return a clean empty rectangle for non-intersecting cases.
69     if (l >= r || t >= b) {
70         l = 0;
71         t = 0;
72         r = 0;
73         b = 0;
74     }
75 
76     m_location.setX(l);
77     m_location.setY(t);
78     m_size.setWidth(r - l);
79     m_size.setHeight(b - t);
80 }
81 
unite(const FloatRect & other)82 void FloatRect::unite(const FloatRect &other)
83 {
84     // Handle empty special cases first.
85     if (other.isEmpty()) {
86         return;
87     }
88     if (isEmpty()) {
89         *this = other;
90         return;
91     }
92 
93     float l = min(x(), other.x());
94     float t = min(y(), other.y());
95     float r = max(right(), other.right());
96     float b = max(bottom(), other.bottom());
97 
98     m_location.setX(l);
99     m_location.setY(t);
100     m_size.setWidth(r - l);
101     m_size.setHeight(b - t);
102 }
103 
scale(float s)104 void FloatRect::scale(float s)
105 {
106     m_location.setX(x() * s);
107     m_location.setY(y() * s);
108     m_size.setWidth(width() * s);
109     m_size.setHeight(height() * s);
110 }
111 
enclosingIntRect(const FloatRect & rect)112 IntRect enclosingIntRect(const FloatRect &rect)
113 {
114     int l = static_cast<int>(rect.x());
115     int t = static_cast<int>(rect.y());
116     // FIXME: These two need to be a "ceiling" operation, not rounding.
117     // We changed them to do "+ 0.5f" to compile on Win32 where there's
118     // no ceilf, but they should be changed back to "ceiling" at some point
119     // and we should provide an implementation of ceilf for Win32.
120     int r = static_cast<int>(rect.right() + 0.5f);
121     int b = static_cast<int>(rect.bottom() + 0.5f);
122     return IntRect(l, t, r - l, b - t);
123 }
124 
125 }
126