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 #ifndef FloatRect_h
28 #define FloatRect_h
29
30 #include "FloatPoint.h"
31
32 #if PLATFORM(CG)
33 typedef struct CGRect CGRect;
34 #endif
35
36 #if PLATFORM(MAC)
37 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
38 typedef struct CGRect NSRect;
39 #else
40 typedef struct _NSRect NSRect;
41 #endif
42 #endif
43
44 #if PLATFORM(QT)
45 QT_BEGIN_NAMESPACE
46 class QRectF;
47 QT_END_NAMESPACE
48 #endif
49
50 #if PLATFORM(WX) && USE(WXGC)
51 class wxRect2DDouble;
52 #endif
53
54 #if (PLATFORM(SKIA) || PLATFORM(SGL))
55 struct SkRect;
56 #endif
57
58 namespace WebCore {
59
60 class IntRect;
61
62 class FloatRect {
63 public:
FloatRect()64 FloatRect() { }
FloatRect(const FloatPoint & location,const FloatSize & size)65 FloatRect(const FloatPoint& location, const FloatSize& size)
66 : m_location(location), m_size(size) { }
FloatRect(float x,float y,float width,float height)67 FloatRect(float x, float y, float width, float height)
68 : m_location(FloatPoint(x, y)), m_size(FloatSize(width, height)) { }
69 FloatRect(const IntRect&);
70
71 static FloatRect narrowPrecision(double x, double y, double width, double height);
72
location()73 FloatPoint location() const { return m_location; }
size()74 FloatSize size() const { return m_size; }
75
setLocation(const FloatPoint & location)76 void setLocation(const FloatPoint& location) { m_location = location; }
setSize(const FloatSize & size)77 void setSize(const FloatSize& size) { m_size = size; }
78
x()79 float x() const { return m_location.x(); }
y()80 float y() const { return m_location.y(); }
width()81 float width() const { return m_size.width(); }
height()82 float height() const { return m_size.height(); }
83
setX(float x)84 void setX(float x) { m_location.setX(x); }
setY(float y)85 void setY(float y) { m_location.setY(y); }
setWidth(float width)86 void setWidth(float width) { m_size.setWidth(width); }
setHeight(float height)87 void setHeight(float height) { m_size.setHeight(height); }
88
isEmpty()89 bool isEmpty() const { return m_size.isEmpty(); }
90
right()91 float right() const { return x() + width(); }
bottom()92 float bottom() const { return y() + height(); }
93
move(const FloatSize & delta)94 void move(const FloatSize& delta) { m_location += delta; }
move(float dx,float dy)95 void move(float dx, float dy) { m_location.move(dx, dy); }
96
97 bool intersects(const FloatRect&) const;
98 bool contains(const FloatRect&) const;
99
100 void intersect(const FloatRect&);
101 void unite(const FloatRect&);
102
103 // Note, this doesn't match what IntRect::contains(IntPoint&) does; the int version
104 // is really checking for containment of 1x1 rect, but that doesn't make sense with floats.
contains(float px,float py)105 bool contains(float px, float py) const
106 { return px >= x() && px <= right() && py >= y() && py <= bottom(); }
contains(const FloatPoint & point)107 bool contains(const FloatPoint& point) const { return contains(point.x(), point.y()); }
108
109
inflateX(float dx)110 void inflateX(float dx) {
111 m_location.setX(m_location.x() - dx);
112 m_size.setWidth(m_size.width() + dx + dx);
113 }
inflateY(float dy)114 void inflateY(float dy) {
115 m_location.setY(m_location.y() - dy);
116 m_size.setHeight(m_size.height() + dy + dy);
117 }
inflate(float d)118 void inflate(float d) { inflateX(d); inflateY(d); }
119 void scale(float s);
120
121 #if PLATFORM(CG)
122 FloatRect(const CGRect&);
123 operator CGRect() const;
124 #endif
125
126 #if PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
127 FloatRect(const NSRect&);
128 operator NSRect() const;
129 #endif
130
131 #if PLATFORM(QT)
132 FloatRect(const QRectF&);
133 operator QRectF() const;
134 #endif
135
136 #if PLATFORM(WX) && USE(WXGC)
137 FloatRect(const wxRect2DDouble&);
138 operator wxRect2DDouble() const;
139 #endif
140
141 #if (PLATFORM(SKIA) || PLATFORM(SGL))
142 FloatRect(const SkRect&);
143 operator SkRect() const;
144 #endif
145
146 private:
147 FloatPoint m_location;
148 FloatSize m_size;
149 };
150
intersection(const FloatRect & a,const FloatRect & b)151 inline FloatRect intersection(const FloatRect& a, const FloatRect& b)
152 {
153 FloatRect c = a;
154 c.intersect(b);
155 return c;
156 }
157
unionRect(const FloatRect & a,const FloatRect & b)158 inline FloatRect unionRect(const FloatRect& a, const FloatRect& b)
159 {
160 FloatRect c = a;
161 c.unite(b);
162 return c;
163 }
164
165
166 inline bool operator==(const FloatRect& a, const FloatRect& b)
167 {
168 return a.location() == b.location() && a.size() == b.size();
169 }
170
171 inline bool operator!=(const FloatRect& a, const FloatRect& b)
172 {
173 return a.location() != b.location() || a.size() != b.size();
174 }
175
176 IntRect enclosingIntRect(const FloatRect&);
177
178 // Map rect r from srcRect to an equivalent rect in destRect.
179 FloatRect mapRect(const FloatRect& r, const FloatRect& srcRect, const FloatRect& destRect);
180
181 }
182
183 #endif
184