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