1/*
2 * Copyright (c) 2012, Google 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 are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#pragma once
32
33#include "FloatRect.h"
34#include "IntRect.h"
35#include "LayoutPoint.h"
36#include "LengthBox.h"
37#include <wtf/Forward.h>
38
39namespace WTF {
40class TextStream;
41}
42
43namespace WebCore {
44
45class LayoutRect {
46public:
47 LayoutRect() { }
48 LayoutRect(const LayoutPoint& location, const LayoutSize& size)
49 : m_location(location), m_size(size) { }
50 template<typename T1, typename T2, typename U1, typename U2>
51 LayoutRect(T1 x, T2 y, U1 width, U2 height)
52 : m_location(LayoutPoint(x, y)), m_size(LayoutSize(width, height)) { }
53 LayoutRect(const LayoutPoint& topLeft, const LayoutPoint& bottomRight)
54 : m_location(topLeft), m_size(LayoutSize(bottomRight.x() - topLeft.x(), bottomRight.y() - topLeft.y())) { }
55 LayoutRect(const FloatPoint& location, const FloatSize& size)
56 : m_location(location), m_size(size) { }
57 LayoutRect(const IntRect& rect) : m_location(rect.location()), m_size(rect.size()) { }
58
59 WEBCORE_EXPORT explicit LayoutRect(const FloatRect&); // don't do this implicitly since it's lossy
60
61 LayoutPoint location() const { return m_location; }
62 LayoutSize size() const { return m_size; }
63
64 void setLocation(const LayoutPoint& location) { m_location = location; }
65 void setSize(const LayoutSize& size) { m_size = size; }
66
67 LayoutUnit x() const { return m_location.x(); }
68 LayoutUnit y() const { return m_location.y(); }
69 LayoutUnit maxX() const { return x() + width(); }
70 LayoutUnit maxY() const { return y() + height(); }
71 LayoutUnit width() const { return m_size.width(); }
72 LayoutUnit height() const { return m_size.height(); }
73
74 template<typename T> void setX(T x) { m_location.setX(x); }
75 template<typename T> void setY(T y) { m_location.setY(y); }
76 template<typename T> void setWidth(T width) { m_size.setWidth(width); }
77 template<typename T> void setHeight(T 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 LayoutPoint center() const { return LayoutPoint(x() + width() / 2, y() + height() / 2); }
84
85 void move(const LayoutSize& size) { m_location += size; }
86 void moveBy(const LayoutPoint& offset) { m_location.move(offset.x(), offset.y()); }
87 template<typename T, typename U> void move(T dx, U dy) { m_location.move(dx, dy); }
88
89 void expand(const LayoutSize& size) { m_size += size; }
90 void expand(const LayoutBoxExtent& box)
91 {
92 m_location.move(-box.left(), -box.top());
93 m_size.expand(box.left() + box.right(), box.top() + box.bottom());
94 }
95 template<typename T, typename U> void expand(T dw, U dh) { m_size.expand(dw, dh); }
96 void contract(const LayoutSize& size) { m_size -= size; }
97 void contract(const LayoutBoxExtent& box)
98 {
99 m_location.move(box.left(), box.top());
100 m_size.shrink(box.left() + box.right(), box.top() + box.bottom());
101 }
102 template<typename T, typename U> void contract(T dw, U dh) { m_size.expand(-dw, -dh); }
103
104 void shiftXEdgeTo(LayoutUnit edge)
105 {
106 LayoutUnit delta = edge - x();
107 setX(edge);
108 setWidth(std::max<LayoutUnit>(0, width() - delta));
109 }
110 void shiftMaxXEdgeTo(LayoutUnit edge)
111 {
112 LayoutUnit delta = edge - maxX();
113 setWidth(std::max<LayoutUnit>(0, width() + delta));
114 }
115 void shiftYEdgeTo(LayoutUnit edge)
116 {
117 LayoutUnit delta = edge - y();
118 setY(edge);
119 setHeight(std::max<LayoutUnit>(0, height() - delta));
120 }
121 void shiftMaxYEdgeTo(LayoutUnit edge)
122 {
123 LayoutUnit delta = edge - maxY();
124 setHeight(std::max<LayoutUnit>(0, height() + delta));
125 }
126
127 template<typename T> void shiftXEdgeTo(T edge) { shiftXEdgeTo(LayoutUnit(edge)); }
128 template<typename T> void shiftMaxXEdgeTo(T edge) { shiftMaxXEdgeTo(LayoutUnit(edge)); }
129 template<typename T> void shiftYEdgeTo(T edge) { shiftYEdgeTo(LayoutUnit(edge)); }
130 template<typename T> void shiftMaxYEdgeTo(T edge) { shiftMaxYEdgeTo(LayoutUnit(edge)); }
131
132 LayoutPoint minXMinYCorner() const { return m_location; } // typically topLeft
133 LayoutPoint maxXMinYCorner() const { return LayoutPoint(m_location.x() + m_size.width(), m_location.y()); } // typically topRight
134 LayoutPoint minXMaxYCorner() const { return LayoutPoint(m_location.x(), m_location.y() + m_size.height()); } // typically bottomLeft
135 LayoutPoint maxXMaxYCorner() const { return LayoutPoint(m_location.x() + m_size.width(), m_location.y() + m_size.height()); } // typically bottomRight
136 bool isMaxXMaxYRepresentable() const
137 {
138 FloatRect rect = *this;
139 float maxX = rect.maxX();
140 float maxY = rect.maxY();
141 return maxX > LayoutUnit::nearlyMin() && maxX < LayoutUnit::nearlyMax() && maxY > LayoutUnit::nearlyMin() && maxY < LayoutUnit::nearlyMax();
142 }
143
144 bool intersects(const LayoutRect&) const;
145 WEBCORE_EXPORT bool contains(const LayoutRect&) const;
146
147 // This checks to see if the rect contains x,y in the traditional sense.
148 // Equivalent to checking if the rect contains a 1x1 rect below and to the right of (px,py).
149 bool contains(LayoutUnit px, LayoutUnit py) const
150 { return px >= x() && px < maxX() && py >= y() && py < maxY(); }
151 bool contains(const LayoutPoint& point) const { return contains(point.x(), point.y()); }
152
153 void intersect(const LayoutRect&);
154 bool edgeInclusiveIntersect(const LayoutRect&);
155 WEBCORE_EXPORT void unite(const LayoutRect&);
156 void uniteIfNonZero(const LayoutRect&);
157 bool checkedUnite(const LayoutRect&);
158
159 void inflateX(LayoutUnit dx)
160 {
161 m_location.setX(m_location.x() - dx);
162 m_size.setWidth(m_size.width() + dx + dx);
163 }
164 void inflateY(LayoutUnit dy)
165 {
166 m_location.setY(m_location.y() - dy);
167 m_size.setHeight(m_size.height() + dy + dy);
168 }
169 void inflate(LayoutSize size) { inflateX(size.width()); inflateY(size.height()); }
170 template<typename T> void inflateX(T dx) { inflateX(LayoutUnit(dx)); }
171 template<typename T> void inflateY(T dy) { inflateY(LayoutUnit(dy)); }
172 template<typename T> void inflate(T d) { inflateX(d); inflateY(d); }
173
174 WEBCORE_EXPORT void scale(float);
175 void scale(float xScale, float yScale);
176
177 LayoutRect transposedRect() const { return LayoutRect(m_location.transposedPoint(), m_size.transposedSize()); }
178 bool isInfinite() const;
179
180 static LayoutRect infiniteRect()
181 {
182 // Return a rect that is slightly smaller than the true max rect to allow pixelSnapping to round up to the nearest IntRect without overflowing.
183 return LayoutRect(LayoutUnit::nearlyMin() / 2, LayoutUnit::nearlyMin() / 2, LayoutUnit::nearlyMax(), LayoutUnit::nearlyMax());
184 }
185
186 operator FloatRect() const { return FloatRect(m_location, m_size); }
187
188private:
189 LayoutPoint m_location;
190 LayoutSize m_size;
191};
192
193inline LayoutRect intersection(const LayoutRect& a, const LayoutRect& b)
194{
195 LayoutRect c = a;
196 c.intersect(b);
197 return c;
198}
199
200inline LayoutRect unionRect(const LayoutRect& a, const LayoutRect& b)
201{
202 LayoutRect c = a;
203 c.unite(b);
204 return c;
205}
206
207LayoutRect unionRect(const Vector<LayoutRect>&);
208
209inline bool operator==(const LayoutRect& a, const LayoutRect& b)
210{
211 return a.location() == b.location() && a.size() == b.size();
212}
213
214inline bool operator!=(const LayoutRect& a, const LayoutRect& b)
215{
216 return a.location() != b.location() || a.size() != b.size();
217}
218
219inline bool LayoutRect::isInfinite() const
220{
221 return *this == LayoutRect::infiniteRect();
222}
223
224// Integral snapping functions.
225inline IntRect snappedIntRect(const LayoutRect& rect)
226{
227 return IntRect(roundedIntPoint(rect.location()), snappedIntSize(rect.size(), rect.location()));
228}
229
230inline IntRect snappedIntRect(LayoutUnit left, LayoutUnit top, LayoutUnit width, LayoutUnit height)
231{
232 return IntRect(IntPoint(left.round(), top.round()), snappedIntSize(LayoutSize(width, height), LayoutPoint(left, top)));
233}
234
235inline IntRect snappedIntRect(LayoutPoint location, LayoutSize size)
236{
237 return IntRect(roundedIntPoint(location), snappedIntSize(size, location));
238}
239
240WEBCORE_EXPORT IntRect enclosingIntRect(const LayoutRect&);
241WEBCORE_EXPORT LayoutRect enclosingLayoutRect(const FloatRect&);
242
243// Device pixel snapping functions.
244inline FloatRect snapRectToDevicePixels(const LayoutRect& rect, float pixelSnappingFactor)
245{
246 return FloatRect(FloatPoint(roundToDevicePixel(rect.x(), pixelSnappingFactor), roundToDevicePixel(rect.y(), pixelSnappingFactor)), snapSizeToDevicePixel(rect.size(), rect.location(), pixelSnappingFactor));
247}
248
249inline FloatRect snapRectToDevicePixels(LayoutUnit x, LayoutUnit y, LayoutUnit width, LayoutUnit height, float pixelSnappingFactor)
250{
251 return snapRectToDevicePixels(LayoutRect(x, y, width, height), pixelSnappingFactor);
252}
253
254// FIXME: This needs to take vertical centering into account too.
255inline FloatRect snapRectToDevicePixelsWithWritingDirection(const LayoutRect& rect, float deviceScaleFactor, bool ltr)
256{
257 if (!ltr) {
258 FloatPoint snappedTopRight = roundPointToDevicePixels(rect.maxXMinYCorner(), deviceScaleFactor, ltr);
259 FloatSize snappedSize = snapSizeToDevicePixel(rect.size(), rect.maxXMinYCorner(), deviceScaleFactor);
260 return FloatRect(snappedTopRight.x() - snappedSize.width(), snappedTopRight.y(), snappedSize.width(), snappedSize.height());
261 }
262 return snapRectToDevicePixels(rect, deviceScaleFactor);
263}
264
265FloatRect encloseRectToDevicePixels(const LayoutRect&, float pixelSnappingFactor);
266
267WEBCORE_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, const LayoutRect&);
268
269} // namespace WebCore
270
271