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#pragma once
27
28#include "IntPoint.h"
29#include "LayoutUnit.h"
30
31#if USE(CG)
32typedef struct CGRect CGRect;
33#endif
34
35#if PLATFORM(MAC)
36#ifdef NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
37typedef struct CGRect NSRect;
38#else
39typedef struct _NSRect NSRect;
40#endif
41#endif
42
43#if PLATFORM(IOS_FAMILY)
44#ifndef NSRect
45#define NSRect CGRect
46#endif
47#endif
48
49#if PLATFORM(WIN)
50typedef struct tagRECT RECT;
51
52struct D2D_RECT_U;
53typedef D2D_RECT_U D2D1_RECT_U;
54
55struct D2D_RECT_F;
56typedef D2D_RECT_F D2D1_RECT_F;
57#endif
58
59#if USE(CAIRO)
60typedef struct _cairo_rectangle_int cairo_rectangle_int_t;
61#endif
62
63namespace WTF {
64class TextStream;
65}
66
67namespace WebCore {
68
69class FloatRect;
70class LayoutRect;
71
72class IntRect {
73 WTF_MAKE_FAST_ALLOCATED;
74public:
75 IntRect() { }
76 IntRect(const IntPoint& location, const IntSize& size)
77 : m_location(location), m_size(size) { }
78 IntRect(int x, int y, int width, int height)
79 : m_location(IntPoint(x, y)), m_size(IntSize(width, height)) { }
80
81 WEBCORE_EXPORT explicit IntRect(const FloatRect&); // don't do this implicitly since it's lossy
82 WEBCORE_EXPORT explicit IntRect(const LayoutRect&); // don't do this implicitly since it's lossy
83
84 IntPoint location() const { return m_location; }
85 IntSize size() const { return m_size; }
86
87 void setLocation(const IntPoint& location) { m_location = location; }
88 void setSize(const IntSize& size) { m_size = size; }
89
90 int x() const { return m_location.x(); }
91 int y() const { return m_location.y(); }
92 int maxX() const { return x() + width(); }
93 int maxY() const { return y() + height(); }
94 int width() const { return m_size.width(); }
95 int height() const { return m_size.height(); }
96
97 template <typename T = WTF::CrashOnOverflow>
98 Checked<unsigned, T> area() const { return m_size.area<T>(); }
99
100 void setX(int x) { m_location.setX(x); }
101 void setY(int y) { m_location.setY(y); }
102 void setWidth(int width) { m_size.setWidth(width); }
103 void setHeight(int height) { m_size.setHeight(height); }
104
105 bool isEmpty() const { return m_size.isEmpty(); }
106
107 // NOTE: The result is rounded to integer values, and thus may be not the exact
108 // center point.
109 IntPoint center() const { return IntPoint(x() + width() / 2, y() + height() / 2); }
110
111 void move(const IntSize& size) { m_location += size; }
112 void moveBy(const IntPoint& offset) { m_location.move(offset.x(), offset.y()); }
113 void move(int dx, int dy) { m_location.move(dx, dy); }
114
115 void expand(const IntSize& size) { m_size += size; }
116 void expand(int dw, int dh) { m_size.expand(dw, dh); }
117 void contract(const IntSize& size) { m_size -= size; }
118 void contract(int dw, int dh) { m_size.expand(-dw, -dh); }
119
120 void shiftXEdgeTo(int edge)
121 {
122 int delta = edge - x();
123 setX(edge);
124 setWidth(std::max(0, width() - delta));
125 }
126 void shiftMaxXEdgeTo(int edge)
127 {
128 int delta = edge - maxX();
129 setWidth(std::max(0, width() + delta));
130 }
131 void shiftYEdgeTo(int edge)
132 {
133 int delta = edge - y();
134 setY(edge);
135 setHeight(std::max(0, height() - delta));
136 }
137 void shiftMaxYEdgeTo(int edge)
138 {
139 int delta = edge - maxY();
140 setHeight(std::max(0, height() + delta));
141 }
142
143 IntPoint minXMinYCorner() const { return m_location; } // typically topLeft
144 IntPoint maxXMinYCorner() const { return IntPoint(m_location.x() + m_size.width(), m_location.y()); } // typically topRight
145 IntPoint minXMaxYCorner() const { return IntPoint(m_location.x(), m_location.y() + m_size.height()); } // typically bottomLeft
146 IntPoint maxXMaxYCorner() const { return IntPoint(m_location.x() + m_size.width(), m_location.y() + m_size.height()); } // typically bottomRight
147
148 WEBCORE_EXPORT bool intersects(const IntRect&) const;
149 WEBCORE_EXPORT bool contains(const IntRect&) const;
150
151 // This checks to see if the rect contains x,y in the traditional sense.
152 // Equivalent to checking if the rect contains a 1x1 rect below and to the right of (px,py).
153 bool contains(int px, int py) const
154 { return px >= x() && px < maxX() && py >= y() && py < maxY(); }
155 bool contains(const IntPoint& point) const { return contains(point.x(), point.y()); }
156
157 WEBCORE_EXPORT void intersect(const IntRect&);
158 WEBCORE_EXPORT void unite(const IntRect&);
159 void uniteIfNonZero(const IntRect&);
160
161 void inflateX(int dx)
162 {
163 m_location.setX(m_location.x() - dx);
164 m_size.setWidth(m_size.width() + dx + dx);
165 }
166 void inflateY(int dy)
167 {
168 m_location.setY(m_location.y() - dy);
169 m_size.setHeight(m_size.height() + dy + dy);
170 }
171 void inflate(int d) { inflateX(d); inflateY(d); }
172 void inflate(IntSize size) { inflateX(size.width()); inflateY(size.height()); }
173 WEBCORE_EXPORT void scale(float s);
174
175 IntSize differenceToPoint(const IntPoint&) const;
176 int distanceSquaredToPoint(const IntPoint& p) const { return differenceToPoint(p).diagonalLengthSquared(); }
177
178 IntRect transposedRect() const { return IntRect(m_location.transposedPoint(), m_size.transposedSize()); }
179
180#if PLATFORM(WIN)
181 IntRect(const RECT&);
182 operator RECT() const;
183 explicit IntRect(const D2D1_RECT_F&);
184 IntRect(const D2D1_RECT_U&);
185 operator D2D1_RECT_F() const;
186 operator D2D1_RECT_U() const;
187#endif
188
189#if USE(CAIRO)
190 IntRect(const cairo_rectangle_int_t&);
191 operator cairo_rectangle_int_t() const;
192#endif
193
194#if USE(CG)
195 WEBCORE_EXPORT operator CGRect() const;
196#endif
197
198#if PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
199 WEBCORE_EXPORT operator NSRect() const;
200#endif
201
202private:
203 IntPoint m_location;
204 IntSize m_size;
205};
206
207inline IntRect intersection(const IntRect& a, const IntRect& b)
208{
209 IntRect c = a;
210 c.intersect(b);
211 return c;
212}
213
214inline IntRect unionRect(const IntRect& a, const IntRect& b)
215{
216 IntRect c = a;
217 c.unite(b);
218 return c;
219}
220
221inline bool operator==(const IntRect& a, const IntRect& b)
222{
223 return a.location() == b.location() && a.size() == b.size();
224}
225
226inline bool operator!=(const IntRect& a, const IntRect& b)
227{
228 return a.location() != b.location() || a.size() != b.size();
229}
230
231inline IntRect& operator-=(IntRect& r, const IntPoint& offset)
232{
233 r.move(-offset.x(), -offset.y());
234 return r;
235}
236
237inline IntRect operator-(const IntRect& r, const IntPoint& offset)
238{
239 IntRect t = r;
240 return t -= offset;
241}
242
243#if USE(CG)
244WEBCORE_EXPORT IntRect enclosingIntRect(const CGRect&);
245#endif
246
247#if PLATFORM(MAC) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
248WEBCORE_EXPORT IntRect enclosingIntRect(const NSRect&);
249#endif
250
251WEBCORE_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, const IntRect&);
252
253} // namespace WebCore
254
255