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.
34 #include "platform/LayoutUnit.h"
35 #include "platform/geometry/DoubleSize.h"
36 #include "platform/geometry/FloatSize.h"
37 #include "platform/geometry/IntSize.h"
49 LayoutSize(const IntSize& size) : m_width(size.width()), m_height(size.height()) { }
50 LayoutSize(LayoutUnit width, LayoutUnit height) : m_width(width), m_height(height) { }
52 explicit LayoutSize(const FloatSize& size) : m_width(size.width()), m_height(size.height()) { }
53 explicit LayoutSize(const DoubleSize& size) : m_width(size.width()), m_height(size.height()) { }
55 LayoutUnit width() const { return m_width; }
56 LayoutUnit height() const { return m_height; }
58 void setWidth(LayoutUnit width) { m_width = width; }
59 void setHeight(LayoutUnit height) { m_height = height; }
61 bool isEmpty() const { return m_width.rawValue() <= 0 || m_height.rawValue() <= 0; }
62 bool isZero() const { return !m_width && !m_height; }
64 float aspectRatio() const { return m_width.toFloat() / m_height.toFloat(); }
66 void expand(LayoutUnit width, LayoutUnit height)
72 void shrink(LayoutUnit width, LayoutUnit height)
78 void scale(float scale)
84 void scale(float widthScale, float heightScale)
86 m_width *= widthScale;
87 m_height *= heightScale;
90 LayoutSize expandedTo(const LayoutSize& other) const
92 return LayoutSize(m_width > other.m_width ? m_width : other.m_width,
93 m_height > other.m_height ? m_height : other.m_height);
96 LayoutSize shrunkTo(const LayoutSize& other) const
98 return LayoutSize(m_width < other.m_width ? m_width : other.m_width,
99 m_height < other.m_height ? m_height : other.m_height);
102 void clampNegativeToZero()
104 *this = expandedTo(LayoutSize());
107 void clampToMinimumSize(const LayoutSize& minimumSize)
109 if (m_width < minimumSize.width())
110 m_width = minimumSize.width();
111 if (m_height < minimumSize.height())
112 m_height = minimumSize.height();
115 LayoutSize transposedSize() const
117 return LayoutSize(m_height, m_width);
120 LayoutSize fitToAspectRatio(const LayoutSize& aspectRatio, AspectRatioFit fit) const
122 float heightScale = height().toFloat() / aspectRatio.height().toFloat();
123 float widthScale = width().toFloat() / aspectRatio.width().toFloat();
124 if ((widthScale > heightScale) != (fit == AspectRatioFitGrow))
125 return LayoutSize(height() * aspectRatio.width() / aspectRatio.height(), height());
126 return LayoutSize(width(), width() * aspectRatio.height() / aspectRatio.width());
129 LayoutSize fraction() const
131 return LayoutSize(m_width.fraction(), m_height.fraction());
135 LayoutUnit m_width, m_height;
138 inline LayoutSize& operator+=(LayoutSize& a, const LayoutSize& b)
140 a.setWidth(a.width() + b.width());
141 a.setHeight(a.height() + b.height());
145 inline LayoutSize& operator-=(LayoutSize& a, const LayoutSize& b)
147 a.setWidth(a.width() - b.width());
148 a.setHeight(a.height() - b.height());
152 inline LayoutSize operator+(const LayoutSize& a, const LayoutSize& b)
154 return LayoutSize(a.width() + b.width(), a.height() + b.height());
157 inline LayoutSize operator-(const LayoutSize& a, const LayoutSize& b)
159 return LayoutSize(a.width() - b.width(), a.height() - b.height());
162 inline LayoutSize operator-(const LayoutSize& size)
164 return LayoutSize(-size.width(), -size.height());
167 inline bool operator==(const LayoutSize& a, const LayoutSize& b)
169 return a.width() == b.width() && a.height() == b.height();
172 inline bool operator!=(const LayoutSize& a, const LayoutSize& b)
174 return a.width() != b.width() || a.height() != b.height();
177 inline IntSize flooredIntSize(const LayoutSize& s)
179 return IntSize(s.width().floor(), s.height().floor());
182 inline IntSize roundedIntSize(const LayoutSize& s)
184 return IntSize(s.width().round(), s.height().round());
187 inline LayoutSize roundedLayoutSize(const FloatSize& s)
189 return LayoutSize(s);
194 #endif // LayoutSize_h