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