• 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 #ifndef FloatRect_h
28 #define FloatRect_h
29 
30 #include "FloatPoint.h"
31 
32 #if USE(CG) || USE(SKIA_ON_MAC_CHROME)
33 typedef struct CGRect CGRect;
34 #endif
35 
36 #if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN))
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(HAIKU)
55 class BRect;
56 #endif
57 
58 #if USE(SKIA)
59 struct SkRect;
60 #endif
61 
62 #if USE(CAIRO)
63 typedef struct _cairo_rectangle cairo_rectangle_t;
64 #endif
65 
66 namespace WebCore {
67 
68 #if PLATFORM(OPENVG)
69 class VGRect;
70 #endif
71 
72 class IntRect;
73 
74 class FloatRect {
75 public:
FloatRect()76     FloatRect() { }
FloatRect(const FloatPoint & location,const FloatSize & size)77     FloatRect(const FloatPoint& location, const FloatSize& size)
78         : m_location(location), m_size(size) { }
FloatRect(float x,float y,float width,float height)79     FloatRect(float x, float y, float width, float height)
80         : m_location(FloatPoint(x, y)), m_size(FloatSize(width, height)) { }
81     FloatRect(const IntRect&);
82 
83     static FloatRect narrowPrecision(double x, double y, double width, double height);
84 
location()85     FloatPoint location() const { return m_location; }
size()86     FloatSize size() const { return m_size; }
87 
setLocation(const FloatPoint & location)88     void setLocation(const FloatPoint& location) { m_location = location; }
setSize(const FloatSize & size)89     void setSize(const FloatSize& size) { m_size = size; }
90 
x()91     float x() const { return m_location.x(); }
y()92     float y() const { return m_location.y(); }
maxX()93     float maxX() const { return x() + width(); }
maxY()94     float maxY() const { return y() + height(); }
width()95     float width() const { return m_size.width(); }
height()96     float height() const { return m_size.height(); }
97 
setX(float x)98     void setX(float x) { m_location.setX(x); }
setY(float y)99     void setY(float y) { m_location.setY(y); }
setWidth(float width)100     void setWidth(float width) { m_size.setWidth(width); }
setHeight(float height)101     void setHeight(float height) { m_size.setHeight(height); }
102 
isEmpty()103     bool isEmpty() const { return m_size.isEmpty(); }
104 
center()105     FloatPoint center() const { return FloatPoint(x() + width() / 2, y() + height() / 2); }
106 
move(const FloatSize & delta)107     void move(const FloatSize& delta) { m_location += delta; }
move(float dx,float dy)108     void move(float dx, float dy) { m_location.move(dx, dy); }
109 
110     bool intersects(const FloatRect&) const;
111     bool contains(const FloatRect&) const;
112 
113     void intersect(const FloatRect&);
114     void unite(const FloatRect&);
115     void uniteIfNonZero(const FloatRect&);
116 
117     // Note, this doesn't match what IntRect::contains(IntPoint&) does; the int version
118     // is really checking for containment of 1x1 rect, but that doesn't make sense with floats.
contains(float px,float py)119     bool contains(float px, float py) const
120         { return px >= x() && px <= maxX() && py >= y() && py <= maxY(); }
contains(const FloatPoint & point)121     bool contains(const FloatPoint& point) const { return contains(point.x(), point.y()); }
122 
inflateX(float dx)123     void inflateX(float dx) {
124         m_location.setX(m_location.x() - dx);
125         m_size.setWidth(m_size.width() + dx + dx);
126     }
inflateY(float dy)127     void inflateY(float dy) {
128         m_location.setY(m_location.y() - dy);
129         m_size.setHeight(m_size.height() + dy + dy);
130     }
inflate(float d)131     void inflate(float d) { inflateX(d); inflateY(d); }
scale(float s)132     void scale(float s) { scale(s, s); }
133     void scale(float sx, float sy);
134 
135     // Re-initializes this rectangle to fit the sets of passed points.
136     void fitToPoints(const FloatPoint& p0, const FloatPoint& p1);
137     void fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const FloatPoint& p2);
138     void fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& p3);
139 
140 #if USE(CG) || USE(SKIA_ON_MAC_CHROME)
141     FloatRect(const CGRect&);
142     operator CGRect() const;
143 #endif
144 
145 #if (PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)) \
146         || (PLATFORM(CHROMIUM) && OS(DARWIN))
147     FloatRect(const NSRect&);
148     operator NSRect() const;
149 #endif
150 
151 #if PLATFORM(QT)
152     FloatRect(const QRectF&);
153     operator QRectF() const;
154     FloatRect normalized() const;
155 #endif
156 
157 #if PLATFORM(WX) && USE(WXGC)
158     FloatRect(const wxRect2DDouble&);
159     operator wxRect2DDouble() const;
160 #endif
161 
162 #if PLATFORM(HAIKU)
163     FloatRect(const BRect&);
164     operator BRect() const;
165 #endif
166 
167 #if USE(SKIA)
168     FloatRect(const SkRect&);
169     operator SkRect() const;
170 #endif
171 
172 #if PLATFORM(OPENVG)
173     operator VGRect() const;
174 #endif
175 
176 #if USE(CAIRO)
177     FloatRect(const cairo_rectangle_t&);
178     operator cairo_rectangle_t() const;
179 #endif
180 
181 private:
182     FloatPoint m_location;
183     FloatSize m_size;
184 
setLocationAndSizeFromEdges(float left,float top,float right,float bottom)185     void setLocationAndSizeFromEdges(float left, float top, float right, float bottom)
186     {
187         m_location.set(left, top);
188         m_size.setWidth(right - left);
189         m_size.setHeight(bottom - top);
190     }
191 };
192 
intersection(const FloatRect & a,const FloatRect & b)193 inline FloatRect intersection(const FloatRect& a, const FloatRect& b)
194 {
195     FloatRect c = a;
196     c.intersect(b);
197     return c;
198 }
199 
unionRect(const FloatRect & a,const FloatRect & b)200 inline FloatRect unionRect(const FloatRect& a, const FloatRect& b)
201 {
202     FloatRect c = a;
203     c.unite(b);
204     return c;
205 }
206 
207 
208 inline bool operator==(const FloatRect& a, const FloatRect& b)
209 {
210     return a.location() == b.location() && a.size() == b.size();
211 }
212 
213 inline bool operator!=(const FloatRect& a, const FloatRect& b)
214 {
215     return a.location() != b.location() || a.size() != b.size();
216 }
217 
218 IntRect enclosingIntRect(const FloatRect&);
219 
220 // Map rect r from srcRect to an equivalent rect in destRect.
221 FloatRect mapRect(const FloatRect& r, const FloatRect& srcRect, const FloatRect& destRect);
222 
223 }
224 
225 #endif
226