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.
31 #ifndef FractionalLayoutRect_h
32 #define FractionalLayoutRect_h
34 #include "FractionalLayoutBoxExtent.h"
35 #include "FractionalLayoutPoint.h"
37 #include <wtf/Vector.h>
51 class FractionalLayoutRect {
53 FractionalLayoutRect() { }
54 FractionalLayoutRect(const FractionalLayoutPoint& location, const FractionalLayoutSize& size)
55 : m_location(location), m_size(size) { }
56 FractionalLayoutRect(FractionalLayoutUnit x, FractionalLayoutUnit y, FractionalLayoutUnit width, FractionalLayoutUnit height)
57 : m_location(FractionalLayoutPoint(x, y)), m_size(FractionalLayoutSize(width, height)) { }
58 FractionalLayoutRect(const FloatPoint& location, const FloatSize& size)
59 : m_location(location), m_size(size) { }
60 FractionalLayoutRect(const IntRect& rect) : m_location(rect.location()), m_size(rect.size()) { }
62 explicit FractionalLayoutRect(const FloatRect&); // don't do this implicitly since it's lossy
64 FractionalLayoutPoint location() const { return m_location; }
65 FractionalLayoutSize 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 FractionalLayoutPoint& location) { m_location = location; }
71 void setSize(const FractionalLayoutSize& size) { m_size = size; }
73 FractionalLayoutUnit x() const { return m_location.x(); }
74 FractionalLayoutUnit y() const { return m_location.y(); }
75 FractionalLayoutUnit maxX() const { return x() + width(); }
76 FractionalLayoutUnit maxY() const { return y() + height(); }
77 FractionalLayoutUnit width() const { return m_size.width(); }
78 FractionalLayoutUnit 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(FractionalLayoutUnit x) { m_location.setX(x); }
88 void setY(FractionalLayoutUnit y) { m_location.setY(y); }
89 void setWidth(FractionalLayoutUnit width) { m_size.setWidth(width); }
90 void setHeight(FractionalLayoutUnit 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 FractionalLayoutPoint center() const { return FractionalLayoutPoint(x() + width() / 2, y() + height() / 2); }
98 void move(const FractionalLayoutSize& size) { m_location += size; }
99 void moveBy(const FractionalLayoutPoint& offset) { m_location.move(offset.x(), offset.y()); }
100 void move(FractionalLayoutUnit dx, FractionalLayoutUnit dy) { m_location.move(dx, dy); }
102 void expand(const FractionalLayoutSize& size) { m_size += size; }
103 void expand(const FractionalLayoutBoxExtent& box)
105 m_location.move(-box.left(), -box.top());
106 m_size.expand(box.left() + box.right(), box.top() + box.bottom());
108 void expand(FractionalLayoutUnit dw, FractionalLayoutUnit dh) { m_size.expand(dw, dh); }
109 void contract(const FractionalLayoutSize& size) { m_size -= size; }
110 void contract(FractionalLayoutUnit dw, FractionalLayoutUnit dh) { m_size.expand(-dw, -dh); }
112 void shiftXEdgeTo(FractionalLayoutUnit edge)
114 FractionalLayoutUnit delta = edge - x();
116 setWidth(std::max<FractionalLayoutUnit>(0, width() - delta));
118 void shiftMaxXEdgeTo(FractionalLayoutUnit edge)
120 FractionalLayoutUnit delta = edge - maxX();
121 setWidth(std::max<FractionalLayoutUnit>(0, width() + delta));
123 void shiftYEdgeTo(FractionalLayoutUnit edge)
125 FractionalLayoutUnit delta = edge - y();
127 setHeight(std::max<FractionalLayoutUnit>(0, height() - delta));
129 void shiftMaxYEdgeTo(FractionalLayoutUnit edge)
131 FractionalLayoutUnit delta = edge - maxY();
132 setHeight(std::max<FractionalLayoutUnit>(0, height() + delta));
135 FractionalLayoutPoint minXMinYCorner() const { return m_location; } // typically topLeft
136 FractionalLayoutPoint maxXMinYCorner() const { return FractionalLayoutPoint(m_location.x() + m_size.width(), m_location.y()); } // typically topRight
137 FractionalLayoutPoint minXMaxYCorner() const { return FractionalLayoutPoint(m_location.x(), m_location.y() + m_size.height()); } // typically bottomLeft
138 FractionalLayoutPoint maxXMaxYCorner() const { return FractionalLayoutPoint(m_location.x() + m_size.width(), m_location.y() + m_size.height()); } // typically bottomRight
140 bool intersects(const FractionalLayoutRect&) const;
141 bool contains(const FractionalLayoutRect&) 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(FractionalLayoutUnit px, FractionalLayoutUnit py) const
146 { return px >= x() && px < maxX() && py >= y() && py < maxY(); }
147 bool contains(const FractionalLayoutPoint& point) const { return contains(point.x(), point.y()); }
149 void intersect(const FractionalLayoutRect&);
150 void unite(const FractionalLayoutRect&);
151 void uniteIfNonZero(const FractionalLayoutRect&);
153 void inflateX(FractionalLayoutUnit dx)
155 m_location.setX(m_location.x() - dx);
156 m_size.setWidth(m_size.width() + dx + dx);
158 void inflateY(FractionalLayoutUnit dy)
160 m_location.setY(m_location.y() - dy);
161 m_size.setHeight(m_size.height() + dy + dy);
163 void inflate(FractionalLayoutUnit d) { inflateX(d); inflateY(d); }
166 FractionalLayoutRect transposedRect() const { return FractionalLayoutRect(m_location.transposedPoint(), m_size.transposedSize()); }
168 static FractionalLayoutRect infiniteRect() {return FractionalLayoutRect(FractionalLayoutUnit::min() / 2, FractionalLayoutUnit::min() / 2, FractionalLayoutUnit::max(), FractionalLayoutUnit::max()); }
171 explicit FractionalLayoutRect(const QRect&);
172 explicit FractionalLayoutRect(const QRectF&);
173 operator QRectF() const;
177 FractionalLayoutPoint m_location;
178 FractionalLayoutSize m_size;
181 inline FractionalLayoutRect intersection(const FractionalLayoutRect& a, const FractionalLayoutRect& b)
183 FractionalLayoutRect c = a;
188 inline FractionalLayoutRect unionRect(const FractionalLayoutRect& a, const FractionalLayoutRect& b)
190 FractionalLayoutRect c = a;
195 FractionalLayoutRect unionRect(const Vector<FractionalLayoutRect>&);
197 inline bool operator==(const FractionalLayoutRect& a, const FractionalLayoutRect& b)
199 return a.location() == b.location() && a.size() == b.size();
202 inline bool operator!=(const FractionalLayoutRect& a, const FractionalLayoutRect& b)
204 return a.location() != b.location() || a.size() != b.size();
207 inline IntRect pixelSnappedIntRect(const FractionalLayoutRect& rect)
209 #if ENABLE(SUBPIXEL_LAYOUT)
210 IntPoint roundedLocation = roundedIntPoint(rect.location());
211 return IntRect(roundedLocation, IntSize((rect.x() + rect.width()).round() - roundedLocation.x(),
212 (rect.y() + rect.height()).round() - roundedLocation.y()));
214 return IntRect(rect);
218 IntRect enclosingIntRect(const FractionalLayoutRect&);
219 FractionalLayoutRect enclosingFractionalLayoutRect(const FloatRect&);
221 } // namespace WebCore
223 #endif // FractionalLayoutRect_h