OLD | NEW |
| (Empty) |
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 "core/platform/graphics/FloatSize.h" | |
35 #include "core/platform/graphics/IntSize.h" | |
36 #include "platform/LayoutUnit.h" | |
37 | |
38 namespace WebCore { | |
39 | |
40 class LayoutPoint; | |
41 | |
42 enum AspectRatioFit { | |
43 AspectRatioFitShrink, | |
44 AspectRatioFitGrow | |
45 }; | |
46 | |
47 class LayoutSize { | |
48 public: | |
49 LayoutSize() { } | |
50 LayoutSize(const IntSize& size) : m_width(size.width()), m_height(size.heigh
t()) { } | |
51 LayoutSize(LayoutUnit width, LayoutUnit height) : m_width(width), m_height(h
eight) { } | |
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.rawValue() <= 0 || m_height.rawValue()
<= 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 LayoutSize fitToAspectRatio(const LayoutSize& aspectRatio, AspectRatioFit fi
t) const | |
121 { | |
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.heigh
t(), height()); | |
126 return LayoutSize(width(), width() * aspectRatio.height() / aspectRatio.
width()); | |
127 } | |
128 | |
129 private: | |
130 LayoutUnit m_width, m_height; | |
131 }; | |
132 | |
133 inline LayoutSize& operator+=(LayoutSize& a, const LayoutSize& b) | |
134 { | |
135 a.setWidth(a.width() + b.width()); | |
136 a.setHeight(a.height() + b.height()); | |
137 return a; | |
138 } | |
139 | |
140 inline LayoutSize& operator-=(LayoutSize& a, const LayoutSize& b) | |
141 { | |
142 a.setWidth(a.width() - b.width()); | |
143 a.setHeight(a.height() - b.height()); | |
144 return a; | |
145 } | |
146 | |
147 inline LayoutSize operator+(const LayoutSize& a, const LayoutSize& b) | |
148 { | |
149 return LayoutSize(a.width() + b.width(), a.height() + b.height()); | |
150 } | |
151 | |
152 inline LayoutSize operator-(const LayoutSize& a, const LayoutSize& b) | |
153 { | |
154 return LayoutSize(a.width() - b.width(), a.height() - b.height()); | |
155 } | |
156 | |
157 inline LayoutSize operator-(const LayoutSize& size) | |
158 { | |
159 return LayoutSize(-size.width(), -size.height()); | |
160 } | |
161 | |
162 inline bool operator==(const LayoutSize& a, const LayoutSize& b) | |
163 { | |
164 return a.width() == b.width() && a.height() == b.height(); | |
165 } | |
166 | |
167 inline bool operator!=(const LayoutSize& a, const LayoutSize& b) | |
168 { | |
169 return a.width() != b.width() || a.height() != b.height(); | |
170 } | |
171 | |
172 inline IntSize flooredIntSize(const LayoutSize& s) | |
173 { | |
174 return IntSize(s.width().floor(), s.height().floor()); | |
175 } | |
176 | |
177 inline IntSize roundedIntSize(const LayoutSize& s) | |
178 { | |
179 return IntSize(s.width().round(), s.height().round()); | |
180 } | |
181 | |
182 inline LayoutSize roundedLayoutSize(const FloatSize& s) | |
183 { | |
184 return LayoutSize(s); | |
185 } | |
186 | |
187 } // namespace WebCore | |
188 | |
189 #endif // LayoutSize_h | |
OLD | NEW |