tagging audio streams and changing audio sink to pulseaudio
[profile/ivi/webkit-efl.git] / Source / WebCore / platform / graphics / FloatRect.h
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 #include <wtf/Vector.h>
32
33 #if USE(CG) || USE(SKIA_ON_MAC_CHROMIUM)
34 typedef struct CGRect CGRect;
35 #endif
36
37 #if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN))
38 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
39 typedef struct CGRect NSRect;
40 #else
41 typedef struct _NSRect NSRect;
42 #endif
43 #endif
44
45 #if PLATFORM(QT)
46 QT_BEGIN_NAMESPACE
47 class QRectF;
48 QT_END_NAMESPACE
49 #endif
50
51 #if PLATFORM(WX) && USE(WXGC)
52 class wxRect2DDouble;
53 #endif
54
55 #if PLATFORM(BLACKBERRY)
56 namespace BlackBerry {
57 namespace Platform {
58 class FloatRect;
59 }
60 }
61 #endif
62
63 #if USE(SKIA)
64 struct SkRect;
65 #endif
66
67 #if USE(CAIRO)
68 typedef struct _cairo_rectangle cairo_rectangle_t;
69 #endif
70
71 namespace WebCore {
72
73 #if PLATFORM(OPENVG)
74 class VGRect;
75 #endif
76
77 class FractionalLayoutRect;
78 class IntRect;
79 class IntPoint;
80
81 class FloatRect {
82 public:
83     enum ContainsMode {
84         InsideOrOnStroke,
85         InsideButNotOnStroke
86     };
87
88     FloatRect() { }
89     FloatRect(const FloatPoint& location, const FloatSize& size)
90         : m_location(location), m_size(size) { }
91     FloatRect(float x, float y, float width, float height)
92         : m_location(FloatPoint(x, y)), m_size(FloatSize(width, height)) { }
93     FloatRect(const IntRect&);
94     FloatRect(const FractionalLayoutRect&);
95
96     static FloatRect narrowPrecision(double x, double y, double width, double height);
97
98     FloatPoint location() const { return m_location; }
99     FloatSize size() const { return m_size; }
100
101     void setLocation(const FloatPoint& location) { m_location = location; }
102     void setSize(const FloatSize& size) { m_size = size; }
103
104     float x() const { return m_location.x(); }
105     float y() const { return m_location.y(); }
106     float maxX() const { return x() + width(); }
107     float maxY() const { return y() + height(); }
108     float width() const { return m_size.width(); }
109     float height() const { return m_size.height(); }
110
111     void setX(float x) { m_location.setX(x); }
112     void setY(float y) { m_location.setY(y); }
113     void setWidth(float width) { m_size.setWidth(width); }
114     void setHeight(float height) { m_size.setHeight(height); }
115
116     bool isEmpty() const { return m_size.isEmpty(); }
117     bool isZero() const { return m_size.isZero(); }
118     bool isExpressibleAsIntRect() const;
119
120     FloatPoint center() const { return FloatPoint(x() + width() / 2, y() + height() / 2); }
121
122     void move(const FloatSize& delta) { m_location += delta; } 
123     void moveBy(const FloatPoint& delta) { m_location.move(delta.x(), delta.y()); }
124     void move(float dx, float dy) { m_location.move(dx, dy); }
125
126     void expand(const FloatSize& size) { m_size += size; }
127     void expand(float dw, float dh) { m_size.expand(dw, dh); }
128     void contract(const FloatSize& size) { m_size -= size; }
129     void contract(float dw, float dh) { m_size.expand(-dw, -dh); }
130
131     void shiftXEdgeTo(float edge)
132     {
133         float delta = edge - x();
134         setX(edge);
135         setWidth(std::max(0.0f, width() - delta));
136     }
137     void shiftMaxXEdgeTo(float edge)
138     {
139         float delta = edge - maxX();
140         setWidth(std::max(0.0f, width() + delta));
141     }
142     void shiftYEdgeTo(float edge)
143     {
144         float delta = edge - y();
145         setY(edge);
146         setHeight(std::max(0.0f, height() - delta));
147     }
148     void shiftMaxYEdgeTo(float edge)
149     {
150         float delta = edge - maxY();
151         setHeight(std::max(0.0f, height() + delta));
152     }
153
154     FloatPoint minXMinYCorner() const { return m_location; } // typically topLeft
155     FloatPoint maxXMinYCorner() const { return FloatPoint(m_location.x() + m_size.width(), m_location.y()); } // typically topRight
156     FloatPoint minXMaxYCorner() const { return FloatPoint(m_location.x(), m_location.y() + m_size.height()); } // typically bottomLeft
157     FloatPoint maxXMaxYCorner() const { return FloatPoint(m_location.x() + m_size.width(), m_location.y() + m_size.height()); } // typically bottomRight
158
159     bool intersects(const FloatRect&) const;
160     bool contains(const FloatRect&) const;
161     bool contains(const FloatPoint&, ContainsMode = InsideOrOnStroke) const;
162
163     void intersect(const FloatRect&);
164     void unite(const FloatRect&);
165     void uniteEvenIfEmpty(const FloatRect&);
166     void uniteIfNonZero(const FloatRect&);
167
168     // Note, this doesn't match what IntRect::contains(IntPoint&) does; the int version
169     // is really checking for containment of 1x1 rect, but that doesn't make sense with floats.
170     bool contains(float px, float py) const
171         { return px >= x() && px <= maxX() && py >= y() && py <= maxY(); }
172
173     void inflateX(float dx) {
174         m_location.setX(m_location.x() - dx);
175         m_size.setWidth(m_size.width() + dx + dx);
176     }
177     void inflateY(float dy) {
178         m_location.setY(m_location.y() - dy);
179         m_size.setHeight(m_size.height() + dy + dy);
180     }
181     void inflate(float d) { inflateX(d); inflateY(d); }
182     void scale(float s) { scale(s, s); }
183     void scale(float sx, float sy);
184
185     FloatRect transposedRect() const { return FloatRect(m_location.transposedPoint(), m_size.transposedSize()); }
186
187     // Re-initializes this rectangle to fit the sets of passed points.
188     void fitToPoints(const FloatPoint& p0, const FloatPoint& p1);
189     void fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const FloatPoint& p2);
190     void fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& p3);
191
192 #if PLATFORM(BLACKBERRY)
193     FloatRect(const BlackBerry::Platform::FloatRect&);
194     operator BlackBerry::Platform::FloatRect() const;
195 #endif
196
197 #if USE(CG) || USE(SKIA_ON_MAC_CHROMIUM)
198     FloatRect(const CGRect&);
199     operator CGRect() const;
200 #endif
201
202 #if (PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)) \
203         || (PLATFORM(CHROMIUM) && OS(DARWIN))
204     FloatRect(const NSRect&);
205     operator NSRect() const;
206 #endif
207
208 #if PLATFORM(QT)
209     FloatRect(const QRectF&);
210     operator QRectF() const;
211     FloatRect normalized() const;
212 #endif
213
214 #if PLATFORM(WX) && USE(WXGC)
215     FloatRect(const wxRect2DDouble&);
216     operator wxRect2DDouble() const;
217 #endif
218
219 #if USE(SKIA)
220     FloatRect(const SkRect&);
221     operator SkRect() const;
222 #endif
223
224 #if PLATFORM(OPENVG)
225     operator VGRect() const;
226 #endif
227
228 #if USE(CAIRO)
229     FloatRect(const cairo_rectangle_t&);
230     operator cairo_rectangle_t() const;
231 #endif
232
233 private:
234     FloatPoint m_location;
235     FloatSize m_size;
236
237     void setLocationAndSizeFromEdges(float left, float top, float right, float bottom)
238     {
239         m_location.set(left, top);
240         m_size.setWidth(right - left);
241         m_size.setHeight(bottom - top);
242     }
243 };
244
245 inline FloatRect intersection(const FloatRect& a, const FloatRect& b)
246 {
247     FloatRect c = a;
248     c.intersect(b);
249     return c;
250 }
251
252 inline FloatRect unionRect(const FloatRect& a, const FloatRect& b)
253 {
254     FloatRect c = a;
255     c.unite(b);
256     return c;
257 }
258
259 FloatRect unionRect(const Vector<FloatRect>&);
260
261 inline FloatRect& operator+=(FloatRect& a, const FloatRect& b)
262 {
263     a.move(b.x(), b.y());
264     a.setWidth(a.width() + b.width());
265     a.setHeight(a.height() + b.height());
266     return a;
267 }
268
269 inline FloatRect operator+(const FloatRect& a, const FloatRect& b)
270 {
271     FloatRect c = a;
272     c += b;
273     return c;
274 }
275
276 inline bool operator==(const FloatRect& a, const FloatRect& b)
277 {
278     return a.location() == b.location() && a.size() == b.size();
279 }
280
281 inline bool operator!=(const FloatRect& a, const FloatRect& b)
282 {
283     return a.location() != b.location() || a.size() != b.size();
284 }
285
286 IntRect enclosingIntRect(const FloatRect&);
287
288 // Returns a valid IntRect contained within the given FloatRect.
289 IntRect enclosedIntRect(const FloatRect&);
290
291 IntRect roundedIntRect(const FloatRect&);
292
293 // Map rect r from srcRect to an equivalent rect in destRect.
294 FloatRect mapRect(const FloatRect& r, const FloatRect& srcRect, const FloatRect& destRect);
295
296 }
297
298 #endif