Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / geometry / IntRect.h
1 /*
2  * Copyright (C) 2003, 2006, 2009 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef IntRect_h
27 #define IntRect_h
28
29 #include "platform/geometry/IntPoint.h"
30 #include "wtf/FastAllocBase.h"
31 #include "wtf/Vector.h"
32
33 #if OS(MACOSX)
34 typedef struct CGRect CGRect;
35
36 #ifdef __OBJC__
37 #import <Foundation/Foundation.h>
38 #endif
39 #endif
40
41 struct SkRect;
42 struct SkIRect;
43
44 namespace WebCore {
45
46 class FloatRect;
47 class LayoutRect;
48
49 class PLATFORM_EXPORT IntRect {
50     WTF_MAKE_FAST_ALLOCATED;
51 public:
52     IntRect() { }
53     IntRect(const IntPoint& location, const IntSize& size)
54         : m_location(location), m_size(size) { }
55     IntRect(int x, int y, int width, int height)
56         : m_location(IntPoint(x, y)), m_size(IntSize(width, height)) { }
57
58     explicit IntRect(const FloatRect&); // don't do this implicitly since it's lossy
59     explicit IntRect(const LayoutRect&); // don't do this implicitly since it's lossy
60
61     IntPoint location() const { return m_location; }
62     IntSize size() const { return m_size; }
63
64     void setLocation(const IntPoint& location) { m_location = location; }
65     void setSize(const IntSize& size) { m_size = size; }
66
67     int x() const { return m_location.x(); }
68     int y() const { return m_location.y(); }
69     int maxX() const { return x() + width(); }
70     int maxY() const { return y() + height(); }
71     int width() const { return m_size.width(); }
72     int height() const { return m_size.height(); }
73
74     void setX(int x) { m_location.setX(x); }
75     void setY(int y) { m_location.setY(y); }
76     void setWidth(int width) { m_size.setWidth(width); }
77     void setHeight(int height) { m_size.setHeight(height); }
78
79     bool isEmpty() const { return m_size.isEmpty(); }
80
81     // NOTE: The result is rounded to integer values, and thus may be not the exact
82     // center point.
83     IntPoint center() const { return IntPoint(x() + width() / 2, y() + height() / 2); }
84
85     void move(const IntSize& size) { m_location += size; }
86     void moveBy(const IntPoint& offset) { m_location.move(offset.x(), offset.y()); }
87     void move(int dx, int dy) { m_location.move(dx, dy); }
88
89     void expand(const IntSize& size) { m_size += size; }
90     void expand(int dw, int dh) { m_size.expand(dw, dh); }
91     void contract(const IntSize& size) { m_size -= size; }
92     void contract(int dw, int dh) { m_size.expand(-dw, -dh); }
93
94     void shiftXEdgeTo(int edge)
95     {
96         int delta = edge - x();
97         setX(edge);
98         setWidth(std::max(0, width() - delta));
99     }
100     void shiftMaxXEdgeTo(int edge)
101     {
102         int delta = edge - maxX();
103         setWidth(std::max(0, width() + delta));
104     }
105     void shiftYEdgeTo(int edge)
106     {
107         int delta = edge - y();
108         setY(edge);
109         setHeight(std::max(0, height() - delta));
110     }
111     void shiftMaxYEdgeTo(int edge)
112     {
113         int delta = edge - maxY();
114         setHeight(std::max(0, height() + delta));
115     }
116
117     IntPoint minXMinYCorner() const { return m_location; } // typically topLeft
118     IntPoint maxXMinYCorner() const { return IntPoint(m_location.x() + m_size.width(), m_location.y()); } // typically topRight
119     IntPoint minXMaxYCorner() const { return IntPoint(m_location.x(), m_location.y() + m_size.height()); } // typically bottomLeft
120     IntPoint maxXMaxYCorner() const { return IntPoint(m_location.x() + m_size.width(), m_location.y() + m_size.height()); } // typically bottomRight
121
122     bool intersects(const IntRect&) const;
123     bool contains(const IntRect&) const;
124
125     // This checks to see if the rect contains x,y in the traditional sense.
126     // Equivalent to checking if the rect contains a 1x1 rect below and to the right of (px,py).
127     bool contains(int px, int py) const
128         { return px >= x() && px < maxX() && py >= y() && py < maxY(); }
129     bool contains(const IntPoint& point) const { return contains(point.x(), point.y()); }
130
131     void intersect(const IntRect&);
132     void unite(const IntRect&);
133     void uniteIfNonZero(const IntRect&);
134
135     void inflateX(int dx)
136     {
137         m_location.setX(m_location.x() - dx);
138         m_size.setWidth(m_size.width() + dx + dx);
139     }
140     void inflateY(int dy)
141     {
142         m_location.setY(m_location.y() - dy);
143         m_size.setHeight(m_size.height() + dy + dy);
144     }
145     void inflate(int d) { inflateX(d); inflateY(d); }
146     void scale(float s);
147
148     IntSize differenceToPoint(const IntPoint&) const;
149     int distanceSquaredToPoint(const IntPoint& p) const { return differenceToPoint(p).diagonalLengthSquared(); }
150
151     IntRect transposedRect() const { return IntRect(m_location.transposedPoint(), m_size.transposedSize()); }
152
153 #if OS(MACOSX)
154     operator CGRect() const;
155 #if defined(__OBJC__) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
156     operator NSRect() const;
157 #endif
158 #endif
159
160     operator SkRect() const;
161     operator SkIRect() const;
162
163 #ifndef NDEBUG
164     // Prints the rect to the screen.
165     void show() const;
166 #endif
167
168 private:
169     IntPoint m_location;
170     IntSize m_size;
171 };
172
173 inline IntRect intersection(const IntRect& a, const IntRect& b)
174 {
175     IntRect c = a;
176     c.intersect(b);
177     return c;
178 }
179
180 inline IntRect unionRect(const IntRect& a, const IntRect& b)
181 {
182     IntRect c = a;
183     c.unite(b);
184     return c;
185 }
186
187 PLATFORM_EXPORT IntRect unionRect(const Vector<IntRect>&);
188
189 inline bool operator==(const IntRect& a, const IntRect& b)
190 {
191     return a.location() == b.location() && a.size() == b.size();
192 }
193
194 inline bool operator!=(const IntRect& a, const IntRect& b)
195 {
196     return a.location() != b.location() || a.size() != b.size();
197 }
198
199 #if OS(MACOSX)
200 PLATFORM_EXPORT IntRect enclosingIntRect(const CGRect&);
201 #if defined(__OBJC__) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
202 PLATFORM_EXPORT IntRect enclosingIntRect(const NSRect&);
203 #endif
204 #endif
205
206 } // namespace WebCore
207
208 #endif // IntRect_h