2 * Copyright (c) 2012, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
34 #include "platform/geometry/IntRect.h"
35 #include "platform/geometry/LayoutBoxExtent.h"
36 #include "platform/geometry/LayoutPoint.h"
37 #include "wtf/Vector.h"
43 class PLATFORM_EXPORT LayoutRect {
46 LayoutRect(const LayoutPoint& location, const LayoutSize& size)
47 : m_location(location), m_size(size) { }
48 LayoutRect(LayoutUnit x, LayoutUnit y, LayoutUnit width, LayoutUnit height)
49 : m_location(LayoutPoint(x, y)), m_size(LayoutSize(width, height)) { }
50 LayoutRect(const FloatPoint& location, const FloatSize& size)
51 : m_location(location), m_size(size) { }
52 LayoutRect(const IntRect& rect) : m_location(rect.location()), m_size(rect.size()) { }
54 explicit LayoutRect(const FloatRect&); // don't do this implicitly since it's lossy
56 LayoutPoint location() const { return m_location; }
57 LayoutSize size() const { return m_size; }
59 IntPoint pixelSnappedLocation() const { return roundedIntPoint(m_location); }
60 IntSize pixelSnappedSize() const { return IntSize(snapSizeToPixel(m_size.width(), m_location.x()), snapSizeToPixel(m_size.height(), m_location.y())); }
62 void setLocation(const LayoutPoint& location) { m_location = location; }
63 void setSize(const LayoutSize& size) { m_size = size; }
65 LayoutUnit x() const { return m_location.x(); }
66 LayoutUnit y() const { return m_location.y(); }
67 LayoutUnit maxX() const { return x() + width(); }
68 LayoutUnit maxY() const { return y() + height(); }
69 LayoutUnit width() const { return m_size.width(); }
70 LayoutUnit height() const { return m_size.height(); }
72 int pixelSnappedX() const { return x().round(); }
73 int pixelSnappedY() const { return y().round(); }
74 int pixelSnappedWidth() const { return snapSizeToPixel(width(), x()); }
75 int pixelSnappedHeight() const { return snapSizeToPixel(height(), y()); }
76 int pixelSnappedMaxX() const { return (m_location.x() + m_size.width()).round(); }
77 int pixelSnappedMaxY() const { return (m_location.y() + m_size.height()).round(); }
79 void setX(LayoutUnit x) { m_location.setX(x); }
80 void setY(LayoutUnit y) { m_location.setY(y); }
81 void setWidth(LayoutUnit width) { m_size.setWidth(width); }
82 void setHeight(LayoutUnit height) { m_size.setHeight(height); }
84 bool isEmpty() const { return m_size.isEmpty(); }
86 // NOTE: The result is rounded to integer values, and thus may be not the exact
88 LayoutPoint center() const { return LayoutPoint(x() + width() / 2, y() + height() / 2); }
90 void move(const LayoutSize& size) { m_location += size; }
91 void moveBy(const LayoutPoint& offset) { m_location.move(offset.x(), offset.y()); }
92 void move(LayoutUnit dx, LayoutUnit dy) { m_location.move(dx, dy); }
94 void expand(const LayoutSize& size) { m_size += size; }
95 void expand(const LayoutBoxExtent& box)
97 m_location.move(-box.left(), -box.top());
98 m_size.expand(box.left() + box.right(), box.top() + box.bottom());
100 void expand(LayoutUnit dw, LayoutUnit dh) { m_size.expand(dw, dh); }
101 void contract(const LayoutSize& size) { m_size -= size; }
102 void contract(const LayoutBoxExtent& box)
104 m_location.move(box.left(), box.top());
105 m_size.shrink(box.left() + box.right(), box.top() + box.bottom());
107 void contract(LayoutUnit dw, LayoutUnit dh) { m_size.expand(-dw, -dh); }
109 void shiftXEdgeTo(LayoutUnit edge)
111 LayoutUnit delta = edge - x();
113 setWidth(std::max<LayoutUnit>(0, width() - delta));
115 void shiftMaxXEdgeTo(LayoutUnit edge)
117 LayoutUnit delta = edge - maxX();
118 setWidth(std::max<LayoutUnit>(0, width() + delta));
120 void shiftYEdgeTo(LayoutUnit edge)
122 LayoutUnit delta = edge - y();
124 setHeight(std::max<LayoutUnit>(0, height() - delta));
126 void shiftMaxYEdgeTo(LayoutUnit edge)
128 LayoutUnit delta = edge - maxY();
129 setHeight(std::max<LayoutUnit>(0, height() + delta));
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
137 bool intersects(const LayoutRect&) const;
138 bool contains(const LayoutRect&) const;
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).
142 bool contains(LayoutUnit px, LayoutUnit py) const
143 { return px >= x() && px < maxX() && py >= y() && py < maxY(); }
144 bool contains(const LayoutPoint& point) const { return contains(point.x(), point.y()); }
146 void intersect(const LayoutRect&);
147 void unite(const LayoutRect&);
148 void uniteIfNonZero(const LayoutRect&);
150 void inflateX(LayoutUnit dx)
152 m_location.setX(m_location.x() - dx);
153 m_size.setWidth(m_size.width() + dx + dx);
155 void inflateY(LayoutUnit dy)
157 m_location.setY(m_location.y() - dy);
158 m_size.setHeight(m_size.height() + dy + dy);
160 void inflate(LayoutUnit d) { inflateX(d); inflateY(d); }
162 void scale(float xAxisScale, float yAxisScale);
164 LayoutRect transposedRect() const { return LayoutRect(m_location.transposedPoint(), m_size.transposedSize()); }
166 static LayoutRect infiniteRect()
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());
173 LayoutPoint m_location;
177 inline LayoutRect intersection(const LayoutRect& a, const LayoutRect& b)
184 inline LayoutRect unionRect(const LayoutRect& a, const LayoutRect& b)
191 PLATFORM_EXPORT LayoutRect unionRect(const Vector<LayoutRect>&);
193 inline bool operator==(const LayoutRect& a, const LayoutRect& b)
195 return a.location() == b.location() && a.size() == b.size();
198 inline bool operator!=(const LayoutRect& a, const LayoutRect& b)
200 return a.location() != b.location() || a.size() != b.size();
203 inline IntRect pixelSnappedIntRect(const LayoutRect& rect)
205 return IntRect(roundedIntPoint(rect.location()), IntSize(
206 snapSizeToPixel(rect.width(), rect.x()),
207 snapSizeToPixel(rect.height(), rect.y())));
210 PLATFORM_EXPORT IntRect enclosingIntRect(const LayoutRect&);
211 PLATFORM_EXPORT LayoutRect enclosingLayoutRect(const FloatRect&);
213 inline IntRect pixelSnappedIntRect(LayoutUnit left, LayoutUnit top, LayoutUnit width, LayoutUnit height)
215 return IntRect(left.round(), top.round(), snapSizeToPixel(width, left), snapSizeToPixel(height, top));
218 inline IntRect pixelSnappedIntRectFromEdges(LayoutUnit left, LayoutUnit top, LayoutUnit right, LayoutUnit bottom)
220 return IntRect(left.round(), top.round(), snapSizeToPixel(right - left, left), snapSizeToPixel(bottom - top, top));
223 inline IntRect pixelSnappedIntRect(LayoutPoint location, LayoutSize size)
225 return IntRect(roundedIntPoint(location), pixelSnappedIntSize(size, location));
228 } // namespace WebCore
230 #endif // LayoutRect_h