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 LayoutSize_h
32#define LayoutSize_h
33
34#include "FloatSize.h"
35#include "IntSize.h"
36#include "LayoutUnit.h"
37
38namespace WebCore {
39
40class LayoutPoint;
41
42enum AspectRatioFit {
43    AspectRatioFitShrink,
44    AspectRatioFitGrow
45};
46
47class LayoutSize {
48public:
49    LayoutSize() : m_width(0), m_height(0) { }
50    LayoutSize(const IntSize& size) : m_width(size.width()), m_height(size.height()) { }
51    LayoutSize(LayoutUnit width, LayoutUnit height) : m_width(width), m_height(height) { }
52
53    explicit LayoutSize(const FloatSize& size) : m_width(size.width()), m_height(size.height()) { }
54
55    LayoutUnit width() const { return m_width; }
56    LayoutUnit height() const { return m_height; }
57
58    void setWidth(LayoutUnit width) { m_width = width; }
59    void setHeight(LayoutUnit height) { m_height = height; }
60
61    bool isEmpty() const { return m_width <= 0 || m_height <= 0; }
62    bool isZero() const { return !m_width && !m_height; }
63
64    float aspectRatio() const { return static_cast<float>(m_width) / static_cast<float>(m_height); }
65
66    void expand(LayoutUnit width, LayoutUnit height)
67    {
68        m_width += width;
69        m_height += height;
70    }
71
72    void shrink(LayoutUnit width, LayoutUnit height)
73    {
74        m_width -= width;
75        m_height -= height;
76    }
77
78    void scale(float scale)
79    {
80        m_width *= scale;
81        m_height *= scale;
82    }
83
84    void scale(float widthScale, float heightScale)
85    {
86        m_width *= widthScale;
87        m_height *= heightScale;
88    }
89
90    LayoutSize expandedTo(const LayoutSize& other) const
91    {
92        return LayoutSize(m_width > other.m_width ? m_width : other.m_width,
93            m_height > other.m_height ? m_height : other.m_height);
94    }
95
96    LayoutSize shrunkTo(const LayoutSize& other) const
97    {
98        return LayoutSize(m_width < other.m_width ? m_width : other.m_width,
99            m_height < other.m_height ? m_height : other.m_height);
100    }
101
102    void clampNegativeToZero()
103    {
104        *this = expandedTo(LayoutSize());
105    }
106
107    void clampToMinimumSize(const LayoutSize& minimumSize)
108    {
109        if (m_width < minimumSize.width())
110            m_width = minimumSize.width();
111        if (m_height < minimumSize.height())
112            m_height = minimumSize.height();
113    }
114
115    LayoutSize transposedSize() const
116    {
117        return LayoutSize(m_height, m_width);
118    }
119
120    operator FloatSize() const { return FloatSize(m_width, m_height); }
121
122    LayoutSize fitToAspectRatio(const LayoutSize& aspectRatio, AspectRatioFit fit) const
123    {
124        float heightScale = height().toFloat() / aspectRatio.height().toFloat();
125        float widthScale = width().toFloat() / aspectRatio.width().toFloat();
126
127        if ((widthScale > heightScale) != (fit == AspectRatioFitGrow))
128            return LayoutSize(height() * aspectRatio.width() / aspectRatio.height(), height());
129
130        return LayoutSize(width(), width() * aspectRatio.height() / aspectRatio.width());
131    }
132
133private:
134    LayoutUnit m_width, m_height;
135};
136
137inline LayoutSize& operator+=(LayoutSize& a, const LayoutSize& b)
138{
139    a.setWidth(a.width() + b.width());
140    a.setHeight(a.height() + b.height());
141    return a;
142}
143
144inline LayoutSize& operator-=(LayoutSize& a, const LayoutSize& b)
145{
146    a.setWidth(a.width() - b.width());
147    a.setHeight(a.height() - b.height());
148    return a;
149}
150
151inline LayoutSize operator+(const LayoutSize& a, const LayoutSize& b)
152{
153    return LayoutSize(a.width() + b.width(), a.height() + b.height());
154}
155
156inline LayoutSize operator-(const LayoutSize& a, const LayoutSize& b)
157{
158    return LayoutSize(a.width() - b.width(), a.height() - b.height());
159}
160
161inline LayoutSize operator-(const LayoutSize& size)
162{
163    return LayoutSize(-size.width(), -size.height());
164}
165
166inline bool operator==(const LayoutSize& a, const LayoutSize& b)
167{
168    return a.width() == b.width() && a.height() == b.height();
169}
170
171inline bool operator!=(const LayoutSize& a, const LayoutSize& b)
172{
173    return a.width() != b.width() || a.height() != b.height();
174}
175
176inline IntSize flooredIntSize(const LayoutSize& s)
177{
178    return IntSize(s.width().floor(), s.height().floor());
179}
180
181inline IntSize roundedIntSize(const LayoutSize& s)
182{
183    return IntSize(s.width().round(), s.height().round());
184}
185
186inline LayoutSize roundedLayoutSize(const FloatSize& s)
187{
188#if ENABLE(SUBPIXEL_LAYOUT)
189    return LayoutSize(s);
190#else
191    return roundedIntSize(s);
192#endif
193}
194
195inline FloatSize flooredForPainting(const LayoutSize& size, float pixelSnappingFactor)
196{
197#if ENABLE(SUBPIXEL_LAYOUT)
198    return FloatSize(floorToDevicePixel(size.width(), pixelSnappingFactor), floorToDevicePixel(size.height(), pixelSnappingFactor));
199#else
200    UNUSED_PARAM(pixelSnappingFactor);
201    return FloatSize(size);
202#endif
203}
204
205} // namespace WebCore
206
207#endif // LayoutSize_h
208