tagging audio streams and changing audio sink to pulseaudio
[profile/ivi/webkit-efl.git] / Source / WebCore / platform / graphics / FractionalLayoutRect.h
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 FractionalLayoutRect_h
32 #define FractionalLayoutRect_h
33
34 #include "FractionalLayoutBoxExtent.h"
35 #include "FractionalLayoutPoint.h"
36 #include "IntRect.h"
37 #include <wtf/Vector.h>
38
39 #if PLATFORM(QT)
40 #include <qglobal.h>
41 QT_BEGIN_NAMESPACE
42 class QRect;
43 class QRectF;
44 QT_END_NAMESPACE
45 #endif
46
47 namespace WebCore {
48
49 class FloatRect;
50
51 class FractionalLayoutRect {
52 public:
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()) { }
61
62     explicit FractionalLayoutRect(const FloatRect&); // don't do this implicitly since it's lossy
63         
64     FractionalLayoutPoint location() const { return m_location; }
65     FractionalLayoutSize size() const { return m_size; }
66
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())); }
69
70     void setLocation(const FractionalLayoutPoint& location) { m_location = location; }
71     void setSize(const FractionalLayoutSize& size) { m_size = size; }
72
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(); }
79
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(); }
86
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); }
91
92     bool isEmpty() const { return m_size.isEmpty(); }
93
94     // NOTE: The result is rounded to integer values, and thus may be not the exact
95     // center point.
96     FractionalLayoutPoint center() const { return FractionalLayoutPoint(x() + width() / 2, y() + height() / 2); }
97
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); } 
101
102     void expand(const FractionalLayoutSize& size) { m_size += size; }
103     void expand(const FractionalLayoutBoxExtent& box)
104     {
105         m_location.move(-box.left(), -box.top());
106         m_size.expand(box.left() + box.right(), box.top() + box.bottom());
107     }
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); }
111
112     void shiftXEdgeTo(FractionalLayoutUnit edge)
113     {
114         FractionalLayoutUnit delta = edge - x();
115         setX(edge);
116         setWidth(std::max<FractionalLayoutUnit>(0, width() - delta));
117     }
118     void shiftMaxXEdgeTo(FractionalLayoutUnit edge)
119     {
120         FractionalLayoutUnit delta = edge - maxX();
121         setWidth(std::max<FractionalLayoutUnit>(0, width() + delta));
122     }
123     void shiftYEdgeTo(FractionalLayoutUnit edge)
124     {
125         FractionalLayoutUnit delta = edge - y();
126         setY(edge);
127         setHeight(std::max<FractionalLayoutUnit>(0, height() - delta));
128     }
129     void shiftMaxYEdgeTo(FractionalLayoutUnit edge)
130     {
131         FractionalLayoutUnit delta = edge - maxY();
132         setHeight(std::max<FractionalLayoutUnit>(0, height() + delta));
133     }
134
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
139     
140     bool intersects(const FractionalLayoutRect&) const;
141     bool contains(const FractionalLayoutRect&) const;
142
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()); }
148
149     void intersect(const FractionalLayoutRect&);
150     void unite(const FractionalLayoutRect&);
151     void uniteIfNonZero(const FractionalLayoutRect&);
152
153     void inflateX(FractionalLayoutUnit dx)
154     {
155         m_location.setX(m_location.x() - dx);
156         m_size.setWidth(m_size.width() + dx + dx);
157     }
158     void inflateY(FractionalLayoutUnit dy)
159     {
160         m_location.setY(m_location.y() - dy);
161         m_size.setHeight(m_size.height() + dy + dy);
162     }
163     void inflate(FractionalLayoutUnit d) { inflateX(d); inflateY(d); }
164     void scale(float s);
165
166     FractionalLayoutRect transposedRect() const { return FractionalLayoutRect(m_location.transposedPoint(), m_size.transposedSize()); }
167
168     static FractionalLayoutRect infiniteRect() {return FractionalLayoutRect(FractionalLayoutUnit::min() / 2, FractionalLayoutUnit::min() / 2, FractionalLayoutUnit::max(), FractionalLayoutUnit::max()); }
169
170 #if PLATFORM(QT)
171     explicit FractionalLayoutRect(const QRect&);
172     explicit FractionalLayoutRect(const QRectF&);
173     operator QRectF() const;
174 #endif
175
176 private:
177     FractionalLayoutPoint m_location;
178     FractionalLayoutSize m_size;
179 };
180
181 inline FractionalLayoutRect intersection(const FractionalLayoutRect& a, const FractionalLayoutRect& b)
182 {
183     FractionalLayoutRect c = a;
184     c.intersect(b);
185     return c;
186 }
187
188 inline FractionalLayoutRect unionRect(const FractionalLayoutRect& a, const FractionalLayoutRect& b)
189 {
190     FractionalLayoutRect c = a;
191     c.unite(b);
192     return c;
193 }
194
195 FractionalLayoutRect unionRect(const Vector<FractionalLayoutRect>&);
196
197 inline bool operator==(const FractionalLayoutRect& a, const FractionalLayoutRect& b)
198 {
199     return a.location() == b.location() && a.size() == b.size();
200 }
201
202 inline bool operator!=(const FractionalLayoutRect& a, const FractionalLayoutRect& b)
203 {
204     return a.location() != b.location() || a.size() != b.size();
205 }
206
207 inline IntRect pixelSnappedIntRect(const FractionalLayoutRect& rect)
208 {
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()));
213 #else
214     return IntRect(rect);
215 #endif
216 }
217
218 IntRect enclosingIntRect(const FractionalLayoutRect&);
219 FractionalLayoutRect enclosingFractionalLayoutRect(const FloatRect&);
220
221 } // namespace WebCore
222
223 #endif // FractionalLayoutRect_h