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 #ifndef LayoutRect_h
32 #define LayoutRect_h
33
34 #include "platform/geometry/IntRect.h"
35 #include "platform/geometry/LayoutBoxExtent.h"
36 #include "platform/geometry/LayoutPoint.h"
37 #include "wtf/Vector.h"
38
39 namespace WebCore {
40
41 class FloatRect;
42
43 class PLATFORM_EXPORT LayoutRect {
44 public:
LayoutRect()45 LayoutRect() { }
LayoutRect(const LayoutPoint & location,const LayoutSize & size)46 LayoutRect(const LayoutPoint& location, const LayoutSize& size)
47 : m_location(location), m_size(size) { }
LayoutRect(LayoutUnit x,LayoutUnit y,LayoutUnit width,LayoutUnit height)48 LayoutRect(LayoutUnit x, LayoutUnit y, LayoutUnit width, LayoutUnit height)
49 : m_location(LayoutPoint(x, y)), m_size(LayoutSize(width, height)) { }
LayoutRect(const FloatPoint & location,const FloatSize & size)50 LayoutRect(const FloatPoint& location, const FloatSize& size)
51 : m_location(location), m_size(size) { }
LayoutRect(const IntRect & rect)52 LayoutRect(const IntRect& rect) : m_location(rect.location()), m_size(rect.size()) { }
53
54 explicit LayoutRect(const FloatRect&); // don't do this implicitly since it's lossy
55
location()56 LayoutPoint location() const { return m_location; }
size()57 LayoutSize size() const { return m_size; }
58
pixelSnappedLocation()59 IntPoint pixelSnappedLocation() const { return roundedIntPoint(m_location); }
pixelSnappedSize()60 IntSize pixelSnappedSize() const { return IntSize(snapSizeToPixel(m_size.width(), m_location.x()), snapSizeToPixel(m_size.height(), m_location.y())); }
61
setLocation(const LayoutPoint & location)62 void setLocation(const LayoutPoint& location) { m_location = location; }
setSize(const LayoutSize & size)63 void setSize(const LayoutSize& size) { m_size = size; }
64
x()65 LayoutUnit x() const { return m_location.x(); }
y()66 LayoutUnit y() const { return m_location.y(); }
maxX()67 LayoutUnit maxX() const { return x() + width(); }
maxY()68 LayoutUnit maxY() const { return y() + height(); }
width()69 LayoutUnit width() const { return m_size.width(); }
height()70 LayoutUnit height() const { return m_size.height(); }
71
pixelSnappedX()72 int pixelSnappedX() const { return x().round(); }
pixelSnappedY()73 int pixelSnappedY() const { return y().round(); }
pixelSnappedWidth()74 int pixelSnappedWidth() const { return snapSizeToPixel(width(), x()); }
pixelSnappedHeight()75 int pixelSnappedHeight() const { return snapSizeToPixel(height(), y()); }
pixelSnappedMaxX()76 int pixelSnappedMaxX() const { return (m_location.x() + m_size.width()).round(); }
pixelSnappedMaxY()77 int pixelSnappedMaxY() const { return (m_location.y() + m_size.height()).round(); }
78
setX(LayoutUnit x)79 void setX(LayoutUnit x) { m_location.setX(x); }
setY(LayoutUnit y)80 void setY(LayoutUnit y) { m_location.setY(y); }
setWidth(LayoutUnit width)81 void setWidth(LayoutUnit width) { m_size.setWidth(width); }
setHeight(LayoutUnit height)82 void setHeight(LayoutUnit height) { m_size.setHeight(height); }
83
isEmpty()84 bool isEmpty() const { return m_size.isEmpty(); }
85
86 // NOTE: The result is rounded to integer values, and thus may be not the exact
87 // center point.
center()88 LayoutPoint center() const { return LayoutPoint(x() + width() / 2, y() + height() / 2); }
89
move(const LayoutSize & size)90 void move(const LayoutSize& size) { m_location += size; }
moveBy(const LayoutPoint & offset)91 void moveBy(const LayoutPoint& offset) { m_location.move(offset.x(), offset.y()); }
move(LayoutUnit dx,LayoutUnit dy)92 void move(LayoutUnit dx, LayoutUnit dy) { m_location.move(dx, dy); }
93
expand(const LayoutSize & size)94 void expand(const LayoutSize& size) { m_size += size; }
expand(const LayoutBoxExtent & box)95 void expand(const LayoutBoxExtent& box)
96 {
97 m_location.move(-box.left(), -box.top());
98 m_size.expand(box.left() + box.right(), box.top() + box.bottom());
99 }
expand(LayoutUnit dw,LayoutUnit dh)100 void expand(LayoutUnit dw, LayoutUnit dh) { m_size.expand(dw, dh); }
contract(const LayoutSize & size)101 void contract(const LayoutSize& size) { m_size -= size; }
contract(const LayoutBoxExtent & box)102 void contract(const LayoutBoxExtent& box)
103 {
104 m_location.move(box.left(), box.top());
105 m_size.shrink(box.left() + box.right(), box.top() + box.bottom());
106 }
contract(LayoutUnit dw,LayoutUnit dh)107 void contract(LayoutUnit dw, LayoutUnit dh) { m_size.expand(-dw, -dh); }
108
shiftXEdgeTo(LayoutUnit edge)109 void shiftXEdgeTo(LayoutUnit edge)
110 {
111 LayoutUnit delta = edge - x();
112 setX(edge);
113 setWidth(std::max<LayoutUnit>(0, width() - delta));
114 }
shiftMaxXEdgeTo(LayoutUnit edge)115 void shiftMaxXEdgeTo(LayoutUnit edge)
116 {
117 LayoutUnit delta = edge - maxX();
118 setWidth(std::max<LayoutUnit>(0, width() + delta));
119 }
shiftYEdgeTo(LayoutUnit edge)120 void shiftYEdgeTo(LayoutUnit edge)
121 {
122 LayoutUnit delta = edge - y();
123 setY(edge);
124 setHeight(std::max<LayoutUnit>(0, height() - delta));
125 }
shiftMaxYEdgeTo(LayoutUnit edge)126 void shiftMaxYEdgeTo(LayoutUnit edge)
127 {
128 LayoutUnit delta = edge - maxY();
129 setHeight(std::max<LayoutUnit>(0, height() + delta));
130 }
131
minXMinYCorner()132 LayoutPoint minXMinYCorner() const { return m_location; } // typically topLeft
maxXMinYCorner()133 LayoutPoint maxXMinYCorner() const { return LayoutPoint(m_location.x() + m_size.width(), m_location.y()); } // typically topRight
minXMaxYCorner()134 LayoutPoint minXMaxYCorner() const { return LayoutPoint(m_location.x(), m_location.y() + m_size.height()); } // typically bottomLeft
maxXMaxYCorner()135 LayoutPoint maxXMaxYCorner() const { return LayoutPoint(m_location.x() + m_size.width(), m_location.y() + m_size.height()); } // typically bottomRight
136
137 bool intersects(const LayoutRect&) const;
138 bool contains(const LayoutRect&) const;
139
140 // This checks to see if the rect contains x,y in the traditional sense.
141 // Equivalent to checking if the rect contains a 1x1 rect below and to the right of (px,py).
contains(LayoutUnit px,LayoutUnit py)142 bool contains(LayoutUnit px, LayoutUnit py) const
143 { return px >= x() && px < maxX() && py >= y() && py < maxY(); }
contains(const LayoutPoint & point)144 bool contains(const LayoutPoint& point) const { return contains(point.x(), point.y()); }
145
146 void intersect(const LayoutRect&);
147 void unite(const LayoutRect&);
148 void uniteIfNonZero(const LayoutRect&);
149
inflateX(LayoutUnit dx)150 void inflateX(LayoutUnit dx)
151 {
152 m_location.setX(m_location.x() - dx);
153 m_size.setWidth(m_size.width() + dx + dx);
154 }
inflateY(LayoutUnit dy)155 void inflateY(LayoutUnit dy)
156 {
157 m_location.setY(m_location.y() - dy);
158 m_size.setHeight(m_size.height() + dy + dy);
159 }
inflate(LayoutUnit d)160 void inflate(LayoutUnit d) { inflateX(d); inflateY(d); }
161 void scale(float s);
162 void scale(float xAxisScale, float yAxisScale);
163
transposedRect()164 LayoutRect transposedRect() const { return LayoutRect(m_location.transposedPoint(), m_size.transposedSize()); }
165
infiniteRect()166 static LayoutRect infiniteRect()
167 {
168 // Return a rect that is slightly smaller than the true max rect to allow pixelSnapping to round up to the nearest IntRect without overflowing.
169 return LayoutRect(LayoutUnit::nearlyMin() / 2, LayoutUnit::nearlyMin() / 2, LayoutUnit::nearlyMax(), LayoutUnit::nearlyMax());
170 }
171
172 private:
173 LayoutPoint m_location;
174 LayoutSize m_size;
175 };
176
intersection(const LayoutRect & a,const LayoutRect & b)177 inline LayoutRect intersection(const LayoutRect& a, const LayoutRect& b)
178 {
179 LayoutRect c = a;
180 c.intersect(b);
181 return c;
182 }
183
unionRect(const LayoutRect & a,const LayoutRect & b)184 inline LayoutRect unionRect(const LayoutRect& a, const LayoutRect& b)
185 {
186 LayoutRect c = a;
187 c.unite(b);
188 return c;
189 }
190
191 PLATFORM_EXPORT LayoutRect unionRect(const Vector<LayoutRect>&);
192
193 inline bool operator==(const LayoutRect& a, const LayoutRect& b)
194 {
195 return a.location() == b.location() && a.size() == b.size();
196 }
197
198 inline bool operator!=(const LayoutRect& a, const LayoutRect& b)
199 {
200 return a.location() != b.location() || a.size() != b.size();
201 }
202
pixelSnappedIntRect(const LayoutRect & rect)203 inline IntRect pixelSnappedIntRect(const LayoutRect& rect)
204 {
205 return IntRect(roundedIntPoint(rect.location()), IntSize(
206 snapSizeToPixel(rect.width(), rect.x()),
207 snapSizeToPixel(rect.height(), rect.y())));
208 }
209
210 PLATFORM_EXPORT IntRect enclosingIntRect(const LayoutRect&);
211 PLATFORM_EXPORT LayoutRect enclosingLayoutRect(const FloatRect&);
212
pixelSnappedIntRect(LayoutUnit left,LayoutUnit top,LayoutUnit width,LayoutUnit height)213 inline IntRect pixelSnappedIntRect(LayoutUnit left, LayoutUnit top, LayoutUnit width, LayoutUnit height)
214 {
215 return IntRect(left.round(), top.round(), snapSizeToPixel(width, left), snapSizeToPixel(height, top));
216 }
217
pixelSnappedIntRectFromEdges(LayoutUnit left,LayoutUnit top,LayoutUnit right,LayoutUnit bottom)218 inline IntRect pixelSnappedIntRectFromEdges(LayoutUnit left, LayoutUnit top, LayoutUnit right, LayoutUnit bottom)
219 {
220 return IntRect(left.round(), top.round(), snapSizeToPixel(right - left, left), snapSizeToPixel(bottom - top, top));
221 }
222
pixelSnappedIntRect(LayoutPoint location,LayoutSize size)223 inline IntRect pixelSnappedIntRect(LayoutPoint location, LayoutSize size)
224 {
225 return IntRect(roundedIntPoint(location), pixelSnappedIntSize(size, location));
226 }
227
228 } // namespace WebCore
229
230 #endif // LayoutRect_h
231