• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
35 using std::max;
36 using std::min;
37 
38 namespace WebCore {
39 
FloatRect(const IntRect & r)40 FloatRect::FloatRect(const IntRect& r) : m_location(r.location()), m_size(r.size())
41 {
42 }
43 
narrowPrecision(double x,double y,double width,double height)44 FloatRect FloatRect::narrowPrecision(double x, double y, double width, double height)
45 {
46     return FloatRect(narrowPrecisionToFloat(x), narrowPrecisionToFloat(y), narrowPrecisionToFloat(width), narrowPrecisionToFloat(height));
47 }
48 
intersects(const FloatRect & other) const49 bool FloatRect::intersects(const FloatRect& other) const
50 {
51     // Checking emptiness handles negative widths as well as zero.
52     return !isEmpty() && !other.isEmpty()
53         && x() < other.right() && other.x() < right()
54         && y() < other.bottom() && other.y() < bottom();
55 }
56 
contains(const FloatRect & other) const57 bool FloatRect::contains(const FloatRect& other) const
58 {
59     return x() <= other.x() && right() >= other.right()
60         && y() <= other.y() && bottom() >= other.bottom();
61 }
62 
intersect(const FloatRect & other)63 void FloatRect::intersect(const FloatRect& other)
64 {
65     float l = max(x(), other.x());
66     float t = max(y(), other.y());
67     float r = min(right(), other.right());
68     float b = min(bottom(), other.bottom());
69 
70     // Return a clean empty rectangle for non-intersecting cases.
71     if (l >= r || t >= b) {
72         l = 0;
73         t = 0;
74         r = 0;
75         b = 0;
76     }
77 
78     m_location.setX(l);
79     m_location.setY(t);
80     m_size.setWidth(r - l);
81     m_size.setHeight(b - t);
82 }
83 
unite(const FloatRect & other)84 void FloatRect::unite(const FloatRect& other)
85 {
86     // Handle empty special cases first.
87     if (other.isEmpty())
88         return;
89     if (isEmpty()) {
90         *this = other;
91         return;
92     }
93 
94     float l = min(x(), other.x());
95     float t = min(y(), other.y());
96     float r = max(right(), other.right());
97     float b = max(bottom(), other.bottom());
98 
99     m_location.setX(l);
100     m_location.setY(t);
101     m_size.setWidth(r - l);
102     m_size.setHeight(b - t);
103 }
104 
scale(float sx,float sy)105 void FloatRect::scale(float sx, float sy)
106 {
107     m_location.setX(x() * sx);
108     m_location.setY(y() * sy);
109     m_size.setWidth(width() * sx);
110     m_size.setHeight(height() * sy);
111 }
112 
enclosingIntRect(const FloatRect & rect)113 IntRect enclosingIntRect(const FloatRect& rect)
114 {
115     int l = static_cast<int>(floorf(rect.x()));
116     int t = static_cast<int>(floorf(rect.y()));
117     int r = static_cast<int>(ceilf(rect.right()));
118     int b = static_cast<int>(ceilf(rect.bottom()));
119     return IntRect(l, t, r - l, b - t);
120 }
121 
mapRect(const FloatRect & r,const FloatRect & srcRect,const FloatRect & destRect)122 FloatRect mapRect(const FloatRect& r, const FloatRect& srcRect, const FloatRect& destRect)
123 {
124     if (srcRect.width() == 0 || srcRect.height() == 0)
125         return FloatRect();
126 
127     float widthScale = destRect.width() / srcRect.width();
128     float heightScale = destRect.height() / srcRect.height();
129     return FloatRect(destRect.x() + (r.x() - srcRect.x()) * widthScale,
130                      destRect.y() + (r.y() - srcRect.y()) * heightScale,
131                      r.width() * widthScale, r.height() * heightScale);
132 }
133 
134 }
135