2 * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2005 Nokia. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 #include "FloatPoint.h"
31 #include <wtf/Vector.h>
33 #if USE(CG) || USE(SKIA_ON_MAC_CHROMIUM)
34 typedef struct CGRect CGRect;
37 #if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN))
38 #ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
39 typedef struct CGRect NSRect;
41 typedef struct _NSRect NSRect;
51 #if PLATFORM(WX) && USE(WXGC)
55 #if PLATFORM(BLACKBERRY)
56 namespace BlackBerry {
68 typedef struct _cairo_rectangle cairo_rectangle_t;
77 class FractionalLayoutRect;
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&);
96 static FloatRect narrowPrecision(double x, double y, double width, double height);
98 FloatPoint location() const { return m_location; }
99 FloatSize size() const { return m_size; }
101 void setLocation(const FloatPoint& location) { m_location = location; }
102 void setSize(const FloatSize& size) { m_size = size; }
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(); }
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); }
116 bool isEmpty() const { return m_size.isEmpty(); }
117 bool isZero() const { return m_size.isZero(); }
118 bool isExpressibleAsIntRect() const;
120 FloatPoint center() const { return FloatPoint(x() + width() / 2, y() + height() / 2); }
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); }
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); }
131 void shiftXEdgeTo(float edge)
133 float delta = edge - x();
135 setWidth(std::max(0.0f, width() - delta));
137 void shiftMaxXEdgeTo(float edge)
139 float delta = edge - maxX();
140 setWidth(std::max(0.0f, width() + delta));
142 void shiftYEdgeTo(float edge)
144 float delta = edge - y();
146 setHeight(std::max(0.0f, height() - delta));
148 void shiftMaxYEdgeTo(float edge)
150 float delta = edge - maxY();
151 setHeight(std::max(0.0f, height() + delta));
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
159 bool intersects(const FloatRect&) const;
160 bool contains(const FloatRect&) const;
161 bool contains(const FloatPoint&, ContainsMode = InsideOrOnStroke) const;
163 void intersect(const FloatRect&);
164 void unite(const FloatRect&);
165 void uniteEvenIfEmpty(const FloatRect&);
166 void uniteIfNonZero(const FloatRect&);
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(); }
173 void inflateX(float dx) {
174 m_location.setX(m_location.x() - dx);
175 m_size.setWidth(m_size.width() + dx + dx);
177 void inflateY(float dy) {
178 m_location.setY(m_location.y() - dy);
179 m_size.setHeight(m_size.height() + dy + dy);
181 void inflate(float d) { inflateX(d); inflateY(d); }
182 void scale(float s) { scale(s, s); }
183 void scale(float sx, float sy);
185 FloatRect transposedRect() const { return FloatRect(m_location.transposedPoint(), m_size.transposedSize()); }
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);
192 #if PLATFORM(BLACKBERRY)
193 FloatRect(const BlackBerry::Platform::FloatRect&);
194 operator BlackBerry::Platform::FloatRect() const;
197 #if USE(CG) || USE(SKIA_ON_MAC_CHROMIUM)
198 FloatRect(const CGRect&);
199 operator CGRect() const;
202 #if (PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)) \
203 || (PLATFORM(CHROMIUM) && OS(DARWIN))
204 FloatRect(const NSRect&);
205 operator NSRect() const;
209 FloatRect(const QRectF&);
210 operator QRectF() const;
211 FloatRect normalized() const;
214 #if PLATFORM(WX) && USE(WXGC)
215 FloatRect(const wxRect2DDouble&);
216 operator wxRect2DDouble() const;
220 FloatRect(const SkRect&);
221 operator SkRect() const;
225 operator VGRect() const;
229 FloatRect(const cairo_rectangle_t&);
230 operator cairo_rectangle_t() const;
234 FloatPoint m_location;
237 void setLocationAndSizeFromEdges(float left, float top, float right, float bottom)
239 m_location.set(left, top);
240 m_size.setWidth(right - left);
241 m_size.setHeight(bottom - top);
245 inline FloatRect intersection(const FloatRect& a, const FloatRect& b)
252 inline FloatRect unionRect(const FloatRect& a, const FloatRect& b)
259 FloatRect unionRect(const Vector<FloatRect>&);
261 inline FloatRect& operator+=(FloatRect& a, const FloatRect& b)
263 a.move(b.x(), b.y());
264 a.setWidth(a.width() + b.width());
265 a.setHeight(a.height() + b.height());
269 inline FloatRect operator+(const FloatRect& a, const FloatRect& b)
276 inline bool operator==(const FloatRect& a, const FloatRect& b)
278 return a.location() == b.location() && a.size() == b.size();
281 inline bool operator!=(const FloatRect& a, const FloatRect& b)
283 return a.location() != b.location() || a.size() != b.size();
286 IntRect enclosingIntRect(const FloatRect&);
288 // Returns a valid IntRect contained within the given FloatRect.
289 IntRect enclosedIntRect(const FloatRect&);
291 IntRect roundedIntRect(const FloatRect&);
293 // Map rect r from srcRect to an equivalent rect in destRect.
294 FloatRect mapRect(const FloatRect& r, const FloatRect& srcRect, const FloatRect& destRect);