[CherryPick] flex-grow should be 1 when flex:auto
[framework/web/webkit-efl.git] / Source / WebCore / css / LengthFunctions.cpp
1 /*
2     Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3     Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
4     Copyright (C) 2011 Rik Cabanier (cabanier@adobe.com)
5     Copyright (C) 2011 Adobe Systems Incorporated. All rights reserved.
6     Copyright (C) 2012 Motorola Mobility, Inc. All rights reserved.
7
8     This library is free software; you can redistribute it and/or
9     modify it under the terms of the GNU Library General Public
10     License as published by the Free Software Foundation; either
11     version 2 of the License, or (at your option) any later version.
12
13     This library is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16     Library General Public License for more details.
17
18     You should have received a copy of the GNU Library General Public License
19     along with this library; see the file COPYING.LIB.  If not, write to
20     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21     Boston, MA 02110-1301, USA.
22 */
23
24 #include "config.h"
25 #include "LengthFunctions.h"
26
27 #include "Length.h"
28 #include "RenderView.h"
29
30 namespace WebCore {
31
32 int minimumIntValueForLength(const Length& length, LayoutUnit maximumValue, RenderView* renderView, bool roundPercentages)
33 {
34     return static_cast<int>(minimumValueForLength(length, maximumValue, renderView, roundPercentages));
35 }
36
37 int intValueForLength(const Length& length, LayoutUnit maximumValue, RenderView* renderView, bool roundPercentages)
38 {
39     return static_cast<int>(valueForLength(length, maximumValue, renderView, roundPercentages));
40 }
41
42 LayoutUnit minimumValueForLength(const Length& length, LayoutUnit maximumValue, RenderView* renderView, bool roundPercentages)
43 {
44     switch (length.type()) {
45     case Fixed:
46         return length.value();
47     case Percent:
48         if (roundPercentages)
49             return static_cast<LayoutUnit>(round(maximumValue * length.percent() / 100.0f));
50         // Don't remove the extra cast to float. It is needed for rounding on 32-bit Intel machines that use the FPU stack.
51         return static_cast<LayoutUnit>(static_cast<float>(maximumValue * length.percent() / 100.0f));
52     case Calculated:
53         return length.nonNanCalculatedValue(maximumValue);
54     case ViewportPercentageWidth:
55         if (renderView)
56             return static_cast<LayoutUnit>(renderView->viewportSize().width() * length.viewportPercentageLength() / 100.0f);
57         return ZERO_LAYOUT_UNIT;
58     case ViewportPercentageHeight:
59         if (renderView)
60             return static_cast<LayoutUnit>(renderView->viewportSize().height() * length.viewportPercentageLength() / 100.0f);
61         return ZERO_LAYOUT_UNIT;
62     case ViewportPercentageMin:
63         if (renderView) {
64             IntSize viewportSize = renderView->viewportSize();
65             return static_cast<LayoutUnit>(std::min(viewportSize.width(), viewportSize.height()) * length.viewportPercentageLength() / 100.0f);
66         }
67         return ZERO_LAYOUT_UNIT;
68     case FillAvailable:
69     case Auto:
70         return ZERO_LAYOUT_UNIT;
71     case Relative:
72     case Intrinsic:
73     case MinIntrinsic:
74     case MinContent:
75     case MaxContent:
76     case FitContent:
77     case Undefined:
78         ASSERT_NOT_REACHED();
79         return ZERO_LAYOUT_UNIT;
80     }
81     ASSERT_NOT_REACHED();
82     return ZERO_LAYOUT_UNIT;
83 }
84
85 LayoutUnit valueForLength(const Length& length, LayoutUnit maximumValue, RenderView* renderView, bool roundPercentages)
86 {
87     switch (length.type()) {
88     case Fixed:
89     case Percent:
90     case Calculated:
91     case ViewportPercentageWidth:
92     case ViewportPercentageHeight:
93     case ViewportPercentageMin:
94         return minimumValueForLength(length, maximumValue, renderView, roundPercentages);
95     case FillAvailable:
96     case Auto:
97         return maximumValue;
98     case Relative:
99     case Intrinsic:
100     case MinIntrinsic:
101     case MinContent:
102     case MaxContent:
103     case FitContent:
104     case Undefined:
105         ASSERT_NOT_REACHED();
106         return ZERO_LAYOUT_UNIT;
107     }
108     ASSERT_NOT_REACHED();
109     return ZERO_LAYOUT_UNIT;
110 }
111
112 // FIXME: when subpixel layout is supported this copy of floatValueForLength() can be removed. See bug 71143.
113 float floatValueForLength(const Length& length, LayoutUnit maximumValue, RenderView* renderView)
114 {
115     switch (length.type()) {
116     case Fixed:
117         return length.getFloatValue();
118     case Percent:
119         return static_cast<float>(maximumValue * length.percent() / 100.0f);
120     case FillAvailable:
121     case Auto:
122         return static_cast<float>(maximumValue);
123     case Calculated:
124         return length.nonNanCalculatedValue(maximumValue);                
125     case ViewportPercentageWidth:
126         if (renderView)
127             return static_cast<int>(renderView->viewportSize().width() * length.viewportPercentageLength() / 100.0f);
128         return 0;
129     case ViewportPercentageHeight:
130         if (renderView)
131             return static_cast<int>(renderView->viewportSize().height() * length.viewportPercentageLength() / 100.0f);
132         return 0;
133     case ViewportPercentageMin:
134         if (renderView) {
135             IntSize viewportSize = renderView->viewportSize();
136             return static_cast<int>(std::min(viewportSize.width(), viewportSize.height()) * length.viewportPercentageLength() / 100.0f);
137         }
138         return 0;
139     case Relative:
140     case Intrinsic:
141     case MinIntrinsic:
142     case MinContent:
143     case MaxContent:
144     case FitContent:
145     case Undefined:
146         ASSERT_NOT_REACHED();
147         return 0;
148     }
149     ASSERT_NOT_REACHED();
150     return 0;
151 }
152
153 float floatValueForLength(const Length& length, float maximumValue, RenderView* renderView)
154 {
155     switch (length.type()) {
156     case Fixed:
157         return length.getFloatValue();
158     case Percent:
159         return static_cast<float>(maximumValue * length.percent() / 100.0f);
160     case FillAvailable:
161     case Auto:
162         return static_cast<float>(maximumValue);
163     case Calculated:
164         return length.nonNanCalculatedValue(maximumValue);
165     case ViewportPercentageWidth:
166         if (renderView)
167             return static_cast<int>(renderView->viewportSize().width() * length.viewportPercentageLength() / 100.0f);
168         return 0;
169     case ViewportPercentageHeight:
170         if (renderView)
171             return static_cast<int>(renderView->viewportSize().height() * length.viewportPercentageLength() / 100.0f);
172         return 0;
173     case ViewportPercentageMin:
174         if (renderView) {
175             IntSize viewportSize = renderView->viewportSize();
176             return static_cast<int>(std::min(viewportSize.width(), viewportSize.height()) * length.viewportPercentageLength() / 100.0f);
177         }
178         return 0;
179     case Relative:
180     case Intrinsic:
181     case MinIntrinsic:
182     case MinContent:
183     case MaxContent:
184     case FitContent:
185     case Undefined:
186         ASSERT_NOT_REACHED();
187         return 0;
188     }
189     ASSERT_NOT_REACHED();
190     return 0;
191 }
192
193 } // namespace WebCore