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 FractionalLayoutSize_h
32 #define FractionalLayoutSize_h
34 #include "FloatSize.h"
35 #include "FractionalLayoutUnit.h"
48 class FractionalLayoutPoint;
50 class FractionalLayoutSize {
52 FractionalLayoutSize() : m_width(0), m_height(0) { }
53 FractionalLayoutSize(const IntSize& size) : m_width(size.width()), m_height(size.height()) { }
54 FractionalLayoutSize(FractionalLayoutUnit width, FractionalLayoutUnit height) : m_width(width), m_height(height) { }
56 explicit FractionalLayoutSize(const FloatSize& size) : m_width(size.width()), m_height(size.height()) { }
58 FractionalLayoutUnit width() const { return m_width; }
59 FractionalLayoutUnit height() const { return m_height; }
61 void setWidth(FractionalLayoutUnit width) { m_width = width; }
62 void setHeight(FractionalLayoutUnit height) { m_height = height; }
64 bool isEmpty() const { return m_width <= 0 || m_height <= 0; }
65 bool isZero() const { return !m_width && !m_height; }
67 float aspectRatio() const { return static_cast<float>(m_width) / static_cast<float>(m_height); }
69 void expand(FractionalLayoutUnit width, FractionalLayoutUnit height)
75 void scale(float scale)
77 m_width = static_cast<int>(static_cast<float>(m_width) * scale);
78 m_height = static_cast<int>(static_cast<float>(m_height) * scale);
81 FractionalLayoutSize expandedTo(const FractionalLayoutSize& other) const
83 return FractionalLayoutSize(m_width > other.m_width ? m_width : other.m_width,
84 m_height > other.m_height ? m_height : other.m_height);
87 FractionalLayoutSize shrunkTo(const FractionalLayoutSize& other) const
89 return FractionalLayoutSize(m_width < other.m_width ? m_width : other.m_width,
90 m_height < other.m_height ? m_height : other.m_height);
93 void clampNegativeToZero()
95 *this = expandedTo(FractionalLayoutSize());
98 FractionalLayoutSize transposedSize() const
100 return FractionalLayoutSize(m_height, m_width);
104 explicit FractionalLayoutSize(const QSize&);
105 explicit FractionalLayoutSize(const QSizeF&);
106 operator QSizeF() const;
110 FractionalLayoutUnit m_width, m_height;
113 inline FractionalLayoutSize& operator+=(FractionalLayoutSize& a, const FractionalLayoutSize& b)
115 a.setWidth(a.width() + b.width());
116 a.setHeight(a.height() + b.height());
120 inline FractionalLayoutSize& operator-=(FractionalLayoutSize& a, const FractionalLayoutSize& b)
122 a.setWidth(a.width() - b.width());
123 a.setHeight(a.height() - b.height());
127 inline FractionalLayoutSize operator+(const FractionalLayoutSize& a, const FractionalLayoutSize& b)
129 return FractionalLayoutSize(a.width() + b.width(), a.height() + b.height());
132 inline FractionalLayoutSize operator-(const FractionalLayoutSize& a, const FractionalLayoutSize& b)
134 return FractionalLayoutSize(a.width() - b.width(), a.height() - b.height());
137 inline FractionalLayoutSize operator-(const FractionalLayoutSize& size)
139 return FractionalLayoutSize(-size.width(), -size.height());
142 inline bool operator==(const FractionalLayoutSize& a, const FractionalLayoutSize& b)
144 return a.width() == b.width() && a.height() == b.height();
147 inline bool operator!=(const FractionalLayoutSize& a, const FractionalLayoutSize& b)
149 return a.width() != b.width() || a.height() != b.height();
152 inline IntSize flooredIntSize(const FractionalLayoutSize& s)
154 return IntSize(s.width().toInt(), s.height().toInt());
157 inline IntSize roundedIntSize(const FractionalLayoutSize& s)
159 return IntSize(s.width().round(), s.height().round());
162 } // namespace WebCore
164 #endif // FractionalLayoutSize_h