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/LayoutPoint.h"
36 #include "platform/geometry/LayoutRectOutsets.h"
37 #include "wtf/Vector.h"
45 class PLATFORM_EXPORT LayoutRect
{
48 LayoutRect(const LayoutPoint
& location
, const LayoutSize
& size
)
49 : m_location(location
), m_size(size
) { }
50 LayoutRect(LayoutUnit x
, LayoutUnit y
, LayoutUnit width
, LayoutUnit height
)
51 : m_location(LayoutPoint(x
, y
)), m_size(LayoutSize(width
, height
)) { }
52 LayoutRect(const FloatPoint
& location
, const FloatSize
& size
)
53 : m_location(location
), m_size(size
) { }
54 LayoutRect(const DoublePoint
& location
, const DoubleSize
& size
)
55 : m_location(location
), m_size(size
) { }
56 LayoutRect(const IntPoint
& location
, const IntSize
& size
)
57 : m_location(location
), m_size(size
) { }
58 explicit LayoutRect(const IntRect
& rect
) : m_location(rect
.location()), m_size(rect
.size()) { }
60 explicit LayoutRect(const FloatRect
&); // don't do this implicitly since it's lossy
61 explicit LayoutRect(const DoubleRect
&); // don't do this implicitly since it's lossy
63 LayoutPoint
location() const { return m_location
; }
64 LayoutSize
size() const { return m_size
; }
66 IntPoint
pixelSnappedLocation() const { return roundedIntPoint(m_location
); }
67 IntSize
pixelSnappedSize() const { return IntSize(snapSizeToPixel(m_size
.width(), m_location
.x()), snapSizeToPixel(m_size
.height(), m_location
.y())); }
69 void setLocation(const LayoutPoint
& location
) { m_location
= location
; }
70 void setSize(const LayoutSize
& size
) { m_size
= size
; }
72 ALWAYS_INLINE LayoutUnit
x() const { return m_location
.x(); }
73 ALWAYS_INLINE LayoutUnit
y() const { return m_location
.y(); }
74 ALWAYS_INLINE LayoutUnit
maxX() const { return x() + width(); }
75 ALWAYS_INLINE LayoutUnit
maxY() const { return y() + height(); }
76 LayoutUnit
width() const { return m_size
.width(); }
77 LayoutUnit
height() const { return m_size
.height(); }
79 int pixelSnappedX() const { return x().round(); }
80 int pixelSnappedY() const { return y().round(); }
81 int pixelSnappedWidth() const { return snapSizeToPixel(width(), x()); }
82 int pixelSnappedHeight() const { return snapSizeToPixel(height(), y()); }
83 int pixelSnappedMaxX() const { return (m_location
.x() + m_size
.width()).round(); }
84 int pixelSnappedMaxY() const { return (m_location
.y() + m_size
.height()).round(); }
86 void setX(LayoutUnit x
) { m_location
.setX(x
); }
87 void setY(LayoutUnit y
) { m_location
.setY(y
); }
88 void setWidth(LayoutUnit width
) { m_size
.setWidth(width
); }
89 void setHeight(LayoutUnit height
) { m_size
.setHeight(height
); }
91 ALWAYS_INLINE
bool isEmpty() const { return m_size
.isEmpty(); }
93 // NOTE: The result is rounded to integer values, and thus may be not the exact
95 LayoutPoint
center() const { return LayoutPoint(x() + width() / 2, y() + height() / 2); }
97 void move(const LayoutSize
& size
) { m_location
+= size
; }
98 void move(const IntSize
& size
) { m_location
.move(size
.width(), size
.height()); }
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 LayoutRectOutsets
& 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 expandEdges(LayoutUnit top
, LayoutUnit right
, LayoutUnit bottom
, LayoutUnit left
)
111 m_location
.move(-left
, -top
);
112 m_size
.expand(left
+ right
, top
+ bottom
);
114 void contract(const LayoutSize
& size
) { m_size
-= size
; }
115 void contract(LayoutUnit dw
, LayoutUnit dh
) { m_size
.expand(-dw
, -dh
); }
116 void contractEdges(LayoutUnit top
, LayoutUnit right
, LayoutUnit bottom
, LayoutUnit left
)
118 m_location
.move(left
, top
);
119 m_size
.shrink(left
+ right
, top
+ bottom
);
122 void shiftXEdgeTo(LayoutUnit edge
)
124 LayoutUnit delta
= edge
- x();
126 setWidth(std::max
<LayoutUnit
>(0, width() - delta
));
128 void shiftMaxXEdgeTo(LayoutUnit edge
)
130 LayoutUnit delta
= edge
- maxX();
131 setWidth(std::max
<LayoutUnit
>(0, width() + delta
));
133 void shiftYEdgeTo(LayoutUnit edge
)
135 LayoutUnit delta
= edge
- y();
137 setHeight(std::max
<LayoutUnit
>(0, height() - delta
));
139 void shiftMaxYEdgeTo(LayoutUnit edge
)
141 LayoutUnit delta
= edge
- maxY();
142 setHeight(std::max
<LayoutUnit
>(0, height() + delta
));
145 LayoutPoint
minXMinYCorner() const { return m_location
; } // typically topLeft
146 LayoutPoint
maxXMinYCorner() const { return LayoutPoint(m_location
.x() + m_size
.width(), m_location
.y()); } // typically topRight
147 LayoutPoint
minXMaxYCorner() const { return LayoutPoint(m_location
.x(), m_location
.y() + m_size
.height()); } // typically bottomLeft
148 LayoutPoint
maxXMaxYCorner() const { return LayoutPoint(m_location
.x() + m_size
.width(), m_location
.y() + m_size
.height()); } // typically bottomRight
150 bool intersects(const LayoutRect
&) const;
151 bool contains(const LayoutRect
&) const;
153 // This checks to see if the rect contains x,y in the traditional sense.
154 // Equivalent to checking if the rect contains a 1x1 rect below and to the right of (px,py).
155 bool contains(LayoutUnit px
, LayoutUnit py
) const
156 { return px
>= x() && px
< maxX() && py
>= y() && py
< maxY(); }
157 bool contains(const LayoutPoint
& point
) const { return contains(point
.x(), point
.y()); }
159 void intersect(const LayoutRect
&);
160 void unite(const LayoutRect
&);
161 void uniteIfNonZero(const LayoutRect
&);
163 // Besides non-empty rects, this method also unites empty rects (as points or line segments).
164 // For example, union of (100, 100, 0x0) and (200, 200, 50x0) is (100, 100, 150x100).
165 void uniteEvenIfEmpty(const LayoutRect
&);
167 void inflateX(LayoutUnit dx
)
169 m_location
.setX(m_location
.x() - dx
);
170 m_size
.setWidth(m_size
.width() + dx
+ dx
);
172 void inflateY(LayoutUnit dy
)
174 m_location
.setY(m_location
.y() - dy
);
175 m_size
.setHeight(m_size
.height() + dy
+ dy
);
177 void inflate(LayoutUnit d
) { inflateX(d
); inflateY(d
); }
179 void scale(float xAxisScale
, float yAxisScale
);
181 LayoutRect
transposedRect() const { return LayoutRect(m_location
.transposedPoint(), m_size
.transposedSize()); }
183 static LayoutRect
infiniteRect()
185 // Return a rect that is slightly smaller than the true max rect to allow pixelSnapping to round up to the nearest IntRect without overflowing.
186 // FIXME(crbug.com/440143): Remove this hack.
187 static LayoutRect
infiniteRect(LayoutUnit::nearlyMin() / 2, LayoutUnit::nearlyMin() / 2, LayoutUnit::nearlyMax(), LayoutUnit::nearlyMax());
191 static IntRect
infiniteIntRect()
193 // Due to saturated arithemetic this value is not the same as LayoutRect(IntRect(INT_MIN/2, INT_MIN/2, INT_MAX/2, INT_MAX/2)).
194 static IntRect
infiniteIntRect(infiniteRect());
195 return infiniteIntRect
;
199 // Prints the rect to the screen.
200 void show(bool showRawValue
= false) const;
204 LayoutPoint m_location
;
208 inline LayoutRect
intersection(const LayoutRect
& a
, const LayoutRect
& b
)
215 inline LayoutRect
unionRect(const LayoutRect
& a
, const LayoutRect
& b
)
222 PLATFORM_EXPORT LayoutRect
unionRect(const Vector
<LayoutRect
>&);
224 inline LayoutRect
unionRectEvenIfEmpty(const LayoutRect
& a
, const LayoutRect
& b
)
227 c
.uniteEvenIfEmpty(b
);
231 PLATFORM_EXPORT LayoutRect
unionRectEvenIfEmpty(const Vector
<LayoutRect
>&);
233 ALWAYS_INLINE
bool operator==(const LayoutRect
& a
, const LayoutRect
& b
)
235 return a
.location() == b
.location() && a
.size() == b
.size();
238 inline bool operator!=(const LayoutRect
& a
, const LayoutRect
& b
)
240 return a
.location() != b
.location() || a
.size() != b
.size();
243 inline IntRect
pixelSnappedIntRect(const LayoutRect
& rect
)
245 return IntRect(roundedIntPoint(rect
.location()), IntSize(
246 snapSizeToPixel(rect
.width(), rect
.x()),
247 snapSizeToPixel(rect
.height(), rect
.y())));
250 PLATFORM_EXPORT IntRect
enclosingIntRect(const LayoutRect
&);
251 PLATFORM_EXPORT LayoutRect
enclosingLayoutRect(const FloatRect
&);
253 inline IntRect
pixelSnappedIntRect(LayoutUnit left
, LayoutUnit top
, LayoutUnit width
, LayoutUnit height
)
255 return IntRect(left
.round(), top
.round(), snapSizeToPixel(width
, left
), snapSizeToPixel(height
, top
));
258 inline IntRect
pixelSnappedIntRectFromEdges(LayoutUnit left
, LayoutUnit top
, LayoutUnit right
, LayoutUnit bottom
)
260 return IntRect(left
.round(), top
.round(), snapSizeToPixel(right
- left
, left
), snapSizeToPixel(bottom
- top
, top
));
263 inline IntRect
pixelSnappedIntRect(LayoutPoint location
, LayoutSize size
)
265 return IntRect(roundedIntPoint(location
), pixelSnappedIntSize(size
, location
));
268 // Redeclared here to avoid ODR issues.
269 // See platform/testing/GeometryPrinters.h.
270 void PrintTo(const LayoutRect
&, std::ostream
*);
274 #endif // LayoutRect_h