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.
35
#include "LayoutBoxExtent.h"
36
#include "LayoutPoint.h"
37
#include <wtf/Vector.h>
54
LayoutRect(const LayoutPoint& location, const LayoutSize& size)
55
: m_location(location), m_size(size) { }
56
LayoutRect(LayoutUnit x, LayoutUnit y, LayoutUnit width, LayoutUnit height)
57
: m_location(LayoutPoint(x, y)), m_size(LayoutSize(width, height)) { }
58
LayoutRect(const FloatPoint& location, const FloatSize& size)
59
: m_location(location), m_size(size) { }
60
LayoutRect(const IntRect& rect) : m_location(rect.location()), m_size(rect.size()) { }
62
explicit LayoutRect(const FloatRect&); // don't do this implicitly since it's lossy
64
LayoutPoint location() const { return m_location; }
65
LayoutSize size() const { return m_size; }
67
IntPoint pixelSnappedLocation() const { return roundedIntPoint(m_location); }
68
IntSize pixelSnappedSize() const { return IntSize(snapSizeToPixel(m_size.width(), m_location.x()), snapSizeToPixel(m_size.height(), m_location.y())); }
70
void setLocation(const LayoutPoint& location) { m_location = location; }
71
void setSize(const LayoutSize& size) { m_size = size; }
73
LayoutUnit x() const { return m_location.x(); }
74
LayoutUnit y() const { return m_location.y(); }
75
LayoutUnit maxX() const { return x() + width(); }
76
LayoutUnit maxY() const { return y() + height(); }
77
LayoutUnit width() const { return m_size.width(); }
78
LayoutUnit height() const { return m_size.height(); }
80
int pixelSnappedX() const { return x().round(); }
81
int pixelSnappedY() const { return y().round(); }
82
int pixelSnappedWidth() const { return snapSizeToPixel(width(), x()); }
83
int pixelSnappedHeight() const { return snapSizeToPixel(height(), y()); }
84
int pixelSnappedMaxX() const { return (m_location.x() + m_size.width()).round(); }
85
int pixelSnappedMaxY() const { return (m_location.y() + m_size.height()).round(); }
87
void setX(LayoutUnit x) { m_location.setX(x); }
88
void setY(LayoutUnit y) { m_location.setY(y); }
89
void setWidth(LayoutUnit width) { m_size.setWidth(width); }
90
void setHeight(LayoutUnit height) { m_size.setHeight(height); }
92
bool isEmpty() const { return m_size.isEmpty(); }
94
// NOTE: The result is rounded to integer values, and thus may be not the exact
96
LayoutPoint center() const { return LayoutPoint(x() + width() / 2, y() + height() / 2); }
98
void move(const LayoutSize& size) { m_location += size; }
99
void moveBy(const LayoutPoint& offset) { m_location.move(offset.x(), offset.y()); }
100
void move(LayoutUnit dx, LayoutUnit dy) { m_location.move(dx, dy); }
102
void expand(const LayoutSize& size) { m_size += size; }
103
void expand(const LayoutBoxExtent& box)
105
m_location.move(-box.left(), -box.top());
106
m_size.expand(box.left() + box.right(), box.top() + box.bottom());
108
void expand(LayoutUnit dw, LayoutUnit dh) { m_size.expand(dw, dh); }
109
void contract(const LayoutSize& size) { m_size -= size; }
110
void contract(LayoutUnit dw, LayoutUnit dh) { m_size.expand(-dw, -dh); }
112
void shiftXEdgeTo(LayoutUnit edge)
114
LayoutUnit delta = edge - x();
116
setWidth(std::max<LayoutUnit>(0, width() - delta));
118
void shiftMaxXEdgeTo(LayoutUnit edge)
120
LayoutUnit delta = edge - maxX();
121
setWidth(std::max<LayoutUnit>(0, width() + delta));
123
void shiftYEdgeTo(LayoutUnit edge)
125
LayoutUnit delta = edge - y();
127
setHeight(std::max<LayoutUnit>(0, height() - delta));
129
void shiftMaxYEdgeTo(LayoutUnit edge)
131
LayoutUnit delta = edge - maxY();
132
setHeight(std::max<LayoutUnit>(0, height() + delta));
135
LayoutPoint minXMinYCorner() const { return m_location; } // typically topLeft
136
LayoutPoint maxXMinYCorner() const { return LayoutPoint(m_location.x() + m_size.width(), m_location.y()); } // typically topRight
137
LayoutPoint minXMaxYCorner() const { return LayoutPoint(m_location.x(), m_location.y() + m_size.height()); } // typically bottomLeft
138
LayoutPoint maxXMaxYCorner() const { return LayoutPoint(m_location.x() + m_size.width(), m_location.y() + m_size.height()); } // typically bottomRight
140
bool intersects(const LayoutRect&) const;
141
bool contains(const LayoutRect&) const;
143
// This checks to see if the rect contains x,y in the traditional sense.
144
// Equivalent to checking if the rect contains a 1x1 rect below and to the right of (px,py).
145
bool contains(LayoutUnit px, LayoutUnit py) const
146
{ return px >= x() && px < maxX() && py >= y() && py < maxY(); }
147
bool contains(const LayoutPoint& point) const { return contains(point.x(), point.y()); }
149
void intersect(const LayoutRect&);
150
void unite(const LayoutRect&);
151
void uniteIfNonZero(const LayoutRect&);
153
void inflateX(LayoutUnit dx)
155
m_location.setX(m_location.x() - dx);
156
m_size.setWidth(m_size.width() + dx + dx);
158
void inflateY(LayoutUnit dy)
160
m_location.setY(m_location.y() - dy);
161
m_size.setHeight(m_size.height() + dy + dy);
163
void inflate(LayoutUnit d) { inflateX(d); inflateY(d); }
166
LayoutRect transposedRect() const { return LayoutRect(m_location.transposedPoint(), m_size.transposedSize()); }
168
static LayoutRect infiniteRect()
170
// Return a rect that is slightly smaller than the true max rect to allow pixelSnapping to round up to the nearest IntRect without overflowing.
171
return LayoutRect(LayoutUnit::nearlyMin() / 2, LayoutUnit::nearlyMin() / 2, LayoutUnit::nearlyMax(), LayoutUnit::nearlyMax());
175
explicit LayoutRect(const QRect&);
176
explicit LayoutRect(const QRectF&);
177
operator QRectF() const;
181
LayoutPoint m_location;
185
inline LayoutRect intersection(const LayoutRect& a, const LayoutRect& b)
192
inline LayoutRect unionRect(const LayoutRect& a, const LayoutRect& b)
199
LayoutRect unionRect(const Vector<LayoutRect>&);
201
inline bool operator==(const LayoutRect& a, const LayoutRect& b)
203
return a.location() == b.location() && a.size() == b.size();
206
inline bool operator!=(const LayoutRect& a, const LayoutRect& b)
208
return a.location() != b.location() || a.size() != b.size();
211
inline IntRect pixelSnappedIntRect(const LayoutRect& rect)
213
#if ENABLE(SUBPIXEL_LAYOUT)
214
return IntRect(roundedIntPoint(rect.location()), IntSize(snapSizeToPixel(rect.width(), rect.x()),
215
snapSizeToPixel(rect.height(), rect.y())));
218
return IntRect(rect);
222
IntRect enclosingIntRect(const LayoutRect&);
223
LayoutRect enclosingLayoutRect(const FloatRect&);
226
inline IntRect pixelSnappedIntRect(LayoutUnit left, LayoutUnit top, LayoutUnit width, LayoutUnit height)
228
return IntRect(left.round(), top.round(), snapSizeToPixel(width, left), snapSizeToPixel(height, top));
231
inline IntRect pixelSnappedIntRectFromEdges(LayoutUnit left, LayoutUnit top, LayoutUnit right, LayoutUnit bottom)
233
return IntRect(left.round(), top.round(), snapSizeToPixel(right - left, left), snapSizeToPixel(bottom - top, top));
236
inline IntRect pixelSnappedIntRect(LayoutPoint location, LayoutSize size)
238
return IntRect(roundedIntPoint(location), pixelSnappedIntSize(size, location));
241
} // namespace WebCore
243
#endif // LayoutRect_h