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 #include "wtf/VectorTraits.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 struct SkRect;
43 struct SkIRect;
44
45 namespace blink {
46
47 class FloatRect;
48 class LayoutRect;
49
50 class PLATFORM_EXPORT IntRect {
51 WTF_MAKE_FAST_ALLOCATED;
52 public:
IntRect()53 IntRect() { }
IntRect(const IntPoint & location,const IntSize & size)54 IntRect(const IntPoint& location, const IntSize& size)
55 : m_location(location), m_size(size) { }
IntRect(int x,int y,int width,int height)56 IntRect(int x, int y, int width, int height)
57 : m_location(IntPoint(x, y)), m_size(IntSize(width, height)) { }
58
59 explicit IntRect(const FloatRect&); // don't do this implicitly since it's lossy
60 explicit IntRect(const LayoutRect&); // don't do this implicitly since it's lossy
61
location()62 IntPoint location() const { return m_location; }
size()63 IntSize size() const { return m_size; }
64
setLocation(const IntPoint & location)65 void setLocation(const IntPoint& location) { m_location = location; }
setSize(const IntSize & size)66 void setSize(const IntSize& size) { m_size = size; }
67
x()68 int x() const { return m_location.x(); }
y()69 int y() const { return m_location.y(); }
maxX()70 int maxX() const { return x() + width(); }
maxY()71 int maxY() const { return y() + height(); }
width()72 int width() const { return m_size.width(); }
height()73 int height() const { return m_size.height(); }
74
setX(int x)75 void setX(int x) { m_location.setX(x); }
setY(int y)76 void setY(int y) { m_location.setY(y); }
setWidth(int width)77 void setWidth(int width) { m_size.setWidth(width); }
setHeight(int height)78 void setHeight(int height) { m_size.setHeight(height); }
79
isEmpty()80 bool isEmpty() const { return m_size.isEmpty(); }
81
82 // NOTE: The result is rounded to integer values, and thus may be not the exact
83 // center point.
center()84 IntPoint center() const { return IntPoint(x() + width() / 2, y() + height() / 2); }
85
move(const IntSize & size)86 void move(const IntSize& size) { m_location += size; }
moveBy(const IntPoint & offset)87 void moveBy(const IntPoint& offset) { m_location.move(offset.x(), offset.y()); }
move(int dx,int dy)88 void move(int dx, int dy) { m_location.move(dx, dy); }
89
expand(const IntSize & size)90 void expand(const IntSize& size) { m_size += size; }
expand(int dw,int dh)91 void expand(int dw, int dh) { m_size.expand(dw, dh); }
contract(const IntSize & size)92 void contract(const IntSize& size) { m_size -= size; }
contract(int dw,int dh)93 void contract(int dw, int dh) { m_size.expand(-dw, -dh); }
94
shiftXEdgeTo(int edge)95 void shiftXEdgeTo(int edge)
96 {
97 int delta = edge - x();
98 setX(edge);
99 setWidth(std::max(0, width() - delta));
100 }
shiftMaxXEdgeTo(int edge)101 void shiftMaxXEdgeTo(int edge)
102 {
103 int delta = edge - maxX();
104 setWidth(std::max(0, width() + delta));
105 }
shiftYEdgeTo(int edge)106 void shiftYEdgeTo(int edge)
107 {
108 int delta = edge - y();
109 setY(edge);
110 setHeight(std::max(0, height() - delta));
111 }
shiftMaxYEdgeTo(int edge)112 void shiftMaxYEdgeTo(int edge)
113 {
114 int delta = edge - maxY();
115 setHeight(std::max(0, height() + delta));
116 }
117
minXMinYCorner()118 IntPoint minXMinYCorner() const { return m_location; } // typically topLeft
maxXMinYCorner()119 IntPoint maxXMinYCorner() const { return IntPoint(m_location.x() + m_size.width(), m_location.y()); } // typically topRight
minXMaxYCorner()120 IntPoint minXMaxYCorner() const { return IntPoint(m_location.x(), m_location.y() + m_size.height()); } // typically bottomLeft
maxXMaxYCorner()121 IntPoint maxXMaxYCorner() const { return IntPoint(m_location.x() + m_size.width(), m_location.y() + m_size.height()); } // typically bottomRight
122
123 bool intersects(const IntRect&) const;
124 bool contains(const IntRect&) const;
125
126 // This checks to see if the rect contains x,y in the traditional sense.
127 // Equivalent to checking if the rect contains a 1x1 rect below and to the right of (px,py).
contains(int px,int py)128 bool contains(int px, int py) const
129 { return px >= x() && px < maxX() && py >= y() && py < maxY(); }
contains(const IntPoint & point)130 bool contains(const IntPoint& point) const { return contains(point.x(), point.y()); }
131
132 void intersect(const IntRect&);
133 void unite(const IntRect&);
134 void uniteIfNonZero(const IntRect&);
135
inflateX(int dx)136 void inflateX(int dx)
137 {
138 m_location.setX(m_location.x() - dx);
139 m_size.setWidth(m_size.width() + dx + dx);
140 }
inflateY(int dy)141 void inflateY(int dy)
142 {
143 m_location.setY(m_location.y() - dy);
144 m_size.setHeight(m_size.height() + dy + dy);
145 }
inflate(int d)146 void inflate(int d) { inflateX(d); inflateY(d); }
147 void scale(float s);
148
149 IntSize differenceToPoint(const IntPoint&) const;
distanceSquaredToPoint(const IntPoint & p)150 int distanceSquaredToPoint(const IntPoint& p) const { return differenceToPoint(p).diagonalLengthSquared(); }
151
transposedRect()152 IntRect transposedRect() const { return IntRect(m_location.transposedPoint(), m_size.transposedSize()); }
153
154 #if OS(MACOSX)
155 operator CGRect() const;
156 #if defined(__OBJC__) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
157 operator NSRect() const;
158 #endif
159 #endif
160
161 operator SkRect() const;
162 operator SkIRect() const;
163
164 #ifndef NDEBUG
165 // Prints the rect to the screen.
166 void show() const;
167 #endif
168
169 private:
170 IntPoint m_location;
171 IntSize m_size;
172 };
173
intersection(const IntRect & a,const IntRect & b)174 inline IntRect intersection(const IntRect& a, const IntRect& b)
175 {
176 IntRect c = a;
177 c.intersect(b);
178 return c;
179 }
180
unionRect(const IntRect & a,const IntRect & b)181 inline IntRect unionRect(const IntRect& a, const IntRect& b)
182 {
183 IntRect c = a;
184 c.unite(b);
185 return c;
186 }
187
188 PLATFORM_EXPORT IntRect unionRect(const Vector<IntRect>&);
189
190 inline bool operator==(const IntRect& a, const IntRect& b)
191 {
192 return a.location() == b.location() && a.size() == b.size();
193 }
194
195 inline bool operator!=(const IntRect& a, const IntRect& b)
196 {
197 return a.location() != b.location() || a.size() != b.size();
198 }
199
200 #if OS(MACOSX)
201 PLATFORM_EXPORT IntRect enclosingIntRect(const CGRect&);
202 #if defined(__OBJC__) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
203 PLATFORM_EXPORT IntRect enclosingIntRect(const NSRect&);
204 #endif
205 #endif
206
207 } // namespace blink
208
209 WTF_ALLOW_MOVE_INIT_AND_COMPARE_WITH_MEM_FUNCTIONS(blink::IntRect);
210
211 #endif // IntRect_h
212