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