• 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 "platform/geometry/FloatPoint.h"
31 #include "third_party/skia/include/core/SkRect.h"
32 #include "wtf/Vector.h"
33 
34 #if OS(MACOSX)
35 typedef struct CGRect CGRect;
36 
37 #ifdef __OBJC__
38 #import <Foundation/Foundation.h>
39 #endif
40 #endif
41 
42 namespace WebCore {
43 
44 class LayoutRect;
45 class IntRect;
46 class IntPoint;
47 
48 class PLATFORM_EXPORT FloatRect {
49 public:
50     enum ContainsMode {
51         InsideOrOnStroke,
52         InsideButNotOnStroke
53     };
54 
FloatRect()55     FloatRect() { }
FloatRect(const FloatPoint & location,const FloatSize & size)56     FloatRect(const FloatPoint& location, const FloatSize& size)
57         : m_location(location), m_size(size) { }
FloatRect(float x,float y,float width,float height)58     FloatRect(float x, float y, float width, float height)
59         : m_location(FloatPoint(x, y)), m_size(FloatSize(width, height)) { }
60     FloatRect(const IntRect&);
61     FloatRect(const LayoutRect&);
62     FloatRect(const SkRect&);
63 
64     static FloatRect narrowPrecision(double x, double y, double width, double height);
65 
location()66     FloatPoint location() const { return m_location; }
size()67     FloatSize size() const { return m_size; }
68 
setLocation(const FloatPoint & location)69     void setLocation(const FloatPoint& location) { m_location = location; }
setSize(const FloatSize & size)70     void setSize(const FloatSize& size) { m_size = size; }
71 
x()72     float x() const { return m_location.x(); }
y()73     float y() const { return m_location.y(); }
maxX()74     float maxX() const { return x() + width(); }
maxY()75     float maxY() const { return y() + height(); }
width()76     float width() const { return m_size.width(); }
height()77     float height() const { return m_size.height(); }
78 
setX(float x)79     void setX(float x) { m_location.setX(x); }
setY(float y)80     void setY(float y) { m_location.setY(y); }
setWidth(float width)81     void setWidth(float width) { m_size.setWidth(width); }
setHeight(float height)82     void setHeight(float height) { m_size.setHeight(height); }
83 
isEmpty()84     bool isEmpty() const { return m_size.isEmpty(); }
isZero()85     bool isZero() const { return m_size.isZero(); }
86     bool isExpressibleAsIntRect() const;
87 
center()88     FloatPoint center() const { return FloatPoint(x() + width() / 2, y() + height() / 2); }
89 
move(const FloatSize & delta)90     void move(const FloatSize& delta) { m_location += delta; }
moveBy(const FloatPoint & delta)91     void moveBy(const FloatPoint& delta) { m_location.move(delta.x(), delta.y()); }
move(float dx,float dy)92     void move(float dx, float dy) { m_location.move(dx, dy); }
93 
expand(const FloatSize & size)94     void expand(const FloatSize& size) { m_size += size; }
expand(float dw,float dh)95     void expand(float dw, float dh) { m_size.expand(dw, dh); }
contract(const FloatSize & size)96     void contract(const FloatSize& size) { m_size -= size; }
contract(float dw,float dh)97     void contract(float dw, float dh) { m_size.expand(-dw, -dh); }
98 
shiftXEdgeTo(float edge)99     void shiftXEdgeTo(float edge)
100     {
101         float delta = edge - x();
102         setX(edge);
103         setWidth(std::max(0.0f, width() - delta));
104     }
shiftMaxXEdgeTo(float edge)105     void shiftMaxXEdgeTo(float edge)
106     {
107         float delta = edge - maxX();
108         setWidth(std::max(0.0f, width() + delta));
109     }
shiftYEdgeTo(float edge)110     void shiftYEdgeTo(float edge)
111     {
112         float delta = edge - y();
113         setY(edge);
114         setHeight(std::max(0.0f, height() - delta));
115     }
shiftMaxYEdgeTo(float edge)116     void shiftMaxYEdgeTo(float edge)
117     {
118         float delta = edge - maxY();
119         setHeight(std::max(0.0f, height() + delta));
120     }
121 
minXMinYCorner()122     FloatPoint minXMinYCorner() const { return m_location; } // typically topLeft
maxXMinYCorner()123     FloatPoint maxXMinYCorner() const { return FloatPoint(m_location.x() + m_size.width(), m_location.y()); } // typically topRight
minXMaxYCorner()124     FloatPoint minXMaxYCorner() const { return FloatPoint(m_location.x(), m_location.y() + m_size.height()); } // typically bottomLeft
maxXMaxYCorner()125     FloatPoint maxXMaxYCorner() const { return FloatPoint(m_location.x() + m_size.width(), m_location.y() + m_size.height()); } // typically bottomRight
126 
127     bool intersects(const FloatRect&) const;
128     bool contains(const FloatRect&) const;
129     bool contains(const FloatPoint&, ContainsMode = InsideOrOnStroke) const;
130 
131     void intersect(const FloatRect&);
132     void unite(const FloatRect&);
133     void uniteEvenIfEmpty(const FloatRect&);
134     void uniteIfNonZero(const FloatRect&);
135     void extend(const FloatPoint&);
136 
137     // Note, this doesn't match what IntRect::contains(IntPoint&) does; the int version
138     // is really checking for containment of 1x1 rect, but that doesn't make sense with floats.
contains(float px,float py)139     bool contains(float px, float py) const
140     {
141         return px >= x() && px <= maxX() && py >= y() && py <= maxY();
142     }
143 
inflateX(float dx)144     void inflateX(float dx)
145     {
146         m_location.setX(m_location.x() - dx);
147         m_size.setWidth(m_size.width() + dx + dx);
148     }
inflateY(float dy)149     void inflateY(float dy)
150     {
151         m_location.setY(m_location.y() - dy);
152         m_size.setHeight(m_size.height() + dy + dy);
153     }
inflate(float d)154     void inflate(float d) { inflateX(d); inflateY(d); }
scale(float s)155     void scale(float s) { scale(s, s); }
156     void scale(float sx, float sy);
157 
transposedRect()158     FloatRect transposedRect() const { return FloatRect(m_location.transposedPoint(), m_size.transposedSize()); }
159 
160     // Re-initializes this rectangle to fit the sets of passed points.
161     void fitToPoints(const FloatPoint& p0, const FloatPoint& p1);
162     void fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const FloatPoint& p2);
163     void fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& p3);
164 
165 #if OS(MACOSX)
166     FloatRect(const CGRect&);
167     operator CGRect() const;
168 #if defined(__OBJC__) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
169     FloatRect(const NSRect&);
170     operator NSRect() const;
171 #endif
172 #endif
173 
SkRect()174     operator SkRect() const { return SkRect::MakeXYWH(x(), y(), width(), height()); }
175 
176 private:
177     FloatPoint m_location;
178     FloatSize m_size;
179 
setLocationAndSizeFromEdges(float left,float top,float right,float bottom)180     void setLocationAndSizeFromEdges(float left, float top, float right, float bottom)
181     {
182         m_location.set(left, top);
183         m_size.setWidth(right - left);
184         m_size.setHeight(bottom - top);
185     }
186 };
187 
intersection(const FloatRect & a,const FloatRect & b)188 inline FloatRect intersection(const FloatRect& a, const FloatRect& b)
189 {
190     FloatRect c = a;
191     c.intersect(b);
192     return c;
193 }
194 
unionRect(const FloatRect & a,const FloatRect & b)195 inline FloatRect unionRect(const FloatRect& a, const FloatRect& b)
196 {
197     FloatRect c = a;
198     c.unite(b);
199     return c;
200 }
201 
202 FloatRect unionRect(const Vector<FloatRect>&);
203 
204 inline FloatRect& operator+=(FloatRect& a, const FloatRect& b)
205 {
206     a.move(b.x(), b.y());
207     a.setWidth(a.width() + b.width());
208     a.setHeight(a.height() + b.height());
209     return a;
210 }
211 
212 inline FloatRect operator+(const FloatRect& a, const FloatRect& b)
213 {
214     FloatRect c = a;
215     c += b;
216     return c;
217 }
218 
219 inline bool operator==(const FloatRect& a, const FloatRect& b)
220 {
221     return a.location() == b.location() && a.size() == b.size();
222 }
223 
224 inline bool operator!=(const FloatRect& a, const FloatRect& b)
225 {
226     return a.location() != b.location() || a.size() != b.size();
227 }
228 
229 PLATFORM_EXPORT IntRect enclosingIntRect(const FloatRect&);
230 
231 // Returns a valid IntRect contained within the given FloatRect.
232 PLATFORM_EXPORT IntRect enclosedIntRect(const FloatRect&);
233 
234 PLATFORM_EXPORT IntRect roundedIntRect(const FloatRect&);
235 
236 // Map supplied rect from srcRect to an equivalent rect in destRect.
237 PLATFORM_EXPORT FloatRect mapRect(const FloatRect&, const FloatRect& srcRect, const FloatRect& destRect);
238 
239 }
240 
241 #endif
242