~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/platform/graphics/LayoutRect.h

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 LayoutRect_h
 
32
#define LayoutRect_h
 
33
 
 
34
#include "IntRect.h"
 
35
#include "LayoutBoxExtent.h"
 
36
#include "LayoutPoint.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 LayoutRect {
 
52
public:
 
53
    LayoutRect() { }
 
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()) { }
 
61
 
 
62
    explicit LayoutRect(const FloatRect&); // don't do this implicitly since it's lossy
 
63
        
 
64
    LayoutPoint location() const { return m_location; }
 
65
    LayoutSize 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 LayoutPoint& location) { m_location = location; }
 
71
    void setSize(const LayoutSize& size) { m_size = size; }
 
72
 
 
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(); }
 
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(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); }
 
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
    LayoutPoint center() const { return LayoutPoint(x() + width() / 2, y() + height() / 2); }
 
97
 
 
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); } 
 
101
 
 
102
    void expand(const LayoutSize& size) { m_size += size; }
 
103
    void expand(const LayoutBoxExtent& 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(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); }
 
111
 
 
112
    void shiftXEdgeTo(LayoutUnit edge)
 
113
    {
 
114
        LayoutUnit delta = edge - x();
 
115
        setX(edge);
 
116
        setWidth(std::max<LayoutUnit>(0, width() - delta));
 
117
    }
 
118
    void shiftMaxXEdgeTo(LayoutUnit edge)
 
119
    {
 
120
        LayoutUnit delta = edge - maxX();
 
121
        setWidth(std::max<LayoutUnit>(0, width() + delta));
 
122
    }
 
123
    void shiftYEdgeTo(LayoutUnit edge)
 
124
    {
 
125
        LayoutUnit delta = edge - y();
 
126
        setY(edge);
 
127
        setHeight(std::max<LayoutUnit>(0, height() - delta));
 
128
    }
 
129
    void shiftMaxYEdgeTo(LayoutUnit edge)
 
130
    {
 
131
        LayoutUnit delta = edge - maxY();
 
132
        setHeight(std::max<LayoutUnit>(0, height() + delta));
 
133
    }
 
134
 
 
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
 
139
    
 
140
    bool intersects(const LayoutRect&) const;
 
141
    bool contains(const LayoutRect&) 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(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()); }
 
148
 
 
149
    void intersect(const LayoutRect&);
 
150
    void unite(const LayoutRect&);
 
151
    void uniteIfNonZero(const LayoutRect&);
 
152
 
 
153
    void inflateX(LayoutUnit dx)
 
154
    {
 
155
        m_location.setX(m_location.x() - dx);
 
156
        m_size.setWidth(m_size.width() + dx + dx);
 
157
    }
 
158
    void inflateY(LayoutUnit dy)
 
159
    {
 
160
        m_location.setY(m_location.y() - dy);
 
161
        m_size.setHeight(m_size.height() + dy + dy);
 
162
    }
 
163
    void inflate(LayoutUnit d) { inflateX(d); inflateY(d); }
 
164
    void scale(float s);
 
165
 
 
166
    LayoutRect transposedRect() const { return LayoutRect(m_location.transposedPoint(), m_size.transposedSize()); }
 
167
 
 
168
    static LayoutRect infiniteRect()
 
169
    {
 
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());
 
172
    }
 
173
 
 
174
#if PLATFORM(QT)
 
175
    explicit LayoutRect(const QRect&);
 
176
    explicit LayoutRect(const QRectF&);
 
177
    operator QRectF() const;
 
178
#endif
 
179
 
 
180
private:
 
181
    LayoutPoint m_location;
 
182
    LayoutSize m_size;
 
183
};
 
184
 
 
185
inline LayoutRect intersection(const LayoutRect& a, const LayoutRect& b)
 
186
{
 
187
    LayoutRect c = a;
 
188
    c.intersect(b);
 
189
    return c;
 
190
}
 
191
 
 
192
inline LayoutRect unionRect(const LayoutRect& a, const LayoutRect& b)
 
193
{
 
194
    LayoutRect c = a;
 
195
    c.unite(b);
 
196
    return c;
 
197
}
 
198
 
 
199
LayoutRect unionRect(const Vector<LayoutRect>&);
 
200
 
 
201
inline bool operator==(const LayoutRect& a, const LayoutRect& b)
 
202
{
 
203
    return a.location() == b.location() && a.size() == b.size();
 
204
}
 
205
 
 
206
inline bool operator!=(const LayoutRect& a, const LayoutRect& b)
 
207
{
 
208
    return a.location() != b.location() || a.size() != b.size();
 
209
}
 
210
 
 
211
inline IntRect pixelSnappedIntRect(const LayoutRect& rect)
 
212
{
 
213
#if ENABLE(SUBPIXEL_LAYOUT)
 
214
    return IntRect(roundedIntPoint(rect.location()), IntSize(snapSizeToPixel(rect.width(), rect.x()),
 
215
                                                             snapSizeToPixel(rect.height(), rect.y())));
 
216
 
 
217
#else
 
218
    return IntRect(rect);
 
219
#endif
 
220
}
 
221
 
 
222
IntRect enclosingIntRect(const LayoutRect&);
 
223
LayoutRect enclosingLayoutRect(const FloatRect&);
 
224
 
 
225
 
 
226
inline IntRect pixelSnappedIntRect(LayoutUnit left, LayoutUnit top, LayoutUnit width, LayoutUnit height)
 
227
{
 
228
    return IntRect(left.round(), top.round(), snapSizeToPixel(width, left), snapSizeToPixel(height, top));
 
229
}
 
230
 
 
231
inline IntRect pixelSnappedIntRectFromEdges(LayoutUnit left, LayoutUnit top, LayoutUnit right, LayoutUnit bottom)
 
232
{
 
233
    return IntRect(left.round(), top.round(), snapSizeToPixel(right - left, left), snapSizeToPixel(bottom - top, top));
 
234
}
 
235
 
 
236
inline IntRect pixelSnappedIntRect(LayoutPoint location, LayoutSize size)
 
237
{
 
238
    return IntRect(roundedIntPoint(location), pixelSnappedIntSize(size, location));
 
239
}
 
240
 
 
241
} // namespace WebCore
 
242
 
 
243
#endif // LayoutRect_h