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 "FloatSize.h"
28#include "LayoutUnit.h"
29#include "LengthSize.h"
30
31namespace WebCore {
32
33int minimumIntValueForLength(const Length& length, LayoutUnit maximumValue, bool roundPercentages)
34{
35    return static_cast<int>(minimumValueForLength(length, maximumValue, roundPercentages));
36}
37
38int intValueForLength(const Length& length, LayoutUnit maximumValue)
39{
40    return static_cast<int>(valueForLength(length, maximumValue));
41}
42
43LayoutUnit minimumValueForLength(const Length& length, LayoutUnit maximumValue, bool roundPercentages)
44{
45    switch (length.type()) {
46    case Fixed:
47        return length.value();
48    case Percent:
49        if (roundPercentages)
50            return LayoutUnit(round(maximumValue * length.percent() / 100.0f));
51        // Don't remove the extra cast to float. It is needed for rounding on 32-bit Intel machines that use the FPU stack.
52        return LayoutUnit(static_cast<float>(maximumValue * length.percent() / 100.0f));
53    case Calculated:
54        return length.nonNanCalculatedValue(maximumValue);
55    case FillAvailable:
56    case Auto:
57        return 0;
58    case Relative:
59    case Intrinsic:
60    case MinIntrinsic:
61    case MinContent:
62    case MaxContent:
63    case FitContent:
64    case Undefined:
65        ASSERT_NOT_REACHED();
66        return 0;
67    }
68    ASSERT_NOT_REACHED();
69    return 0;
70}
71
72LayoutUnit valueForLength(const Length& length, LayoutUnit maximumValue)
73{
74    switch (length.type()) {
75    case Fixed:
76    case Percent:
77    case Calculated:
78        return minimumValueForLength(length, maximumValue);
79    case FillAvailable:
80    case Auto:
81        return maximumValue;
82    case Relative:
83    case Intrinsic:
84    case MinIntrinsic:
85    case MinContent:
86    case MaxContent:
87    case FitContent:
88    case Undefined:
89        ASSERT_NOT_REACHED();
90        return 0;
91    }
92    ASSERT_NOT_REACHED();
93    return 0;
94}
95
96// FIXME: when subpixel layout is supported this copy of floatValueForLength() can be removed. See bug 71143.
97float floatValueForLength(const Length& length, LayoutUnit maximumValue)
98{
99    switch (length.type()) {
100    case Fixed:
101        return length.value();
102    case Percent:
103        return static_cast<float>(maximumValue * length.percent() / 100.0f);
104    case FillAvailable:
105    case Auto:
106        return static_cast<float>(maximumValue);
107    case Calculated:
108        return length.nonNanCalculatedValue(maximumValue);
109    case Relative:
110    case Intrinsic:
111    case MinIntrinsic:
112    case MinContent:
113    case MaxContent:
114    case FitContent:
115    case Undefined:
116        ASSERT_NOT_REACHED();
117        return 0;
118    }
119    ASSERT_NOT_REACHED();
120    return 0;
121}
122
123float floatValueForLength(const Length& length, float maximumValue)
124{
125    switch (length.type()) {
126    case Fixed:
127        return length.value();
128    case Percent:
129        return static_cast<float>(maximumValue * length.percent() / 100.0f);
130    case FillAvailable:
131    case Auto:
132        return static_cast<float>(maximumValue);
133    case Calculated:
134        return length.nonNanCalculatedValue(maximumValue);
135    case Relative:
136    case Intrinsic:
137    case MinIntrinsic:
138    case MinContent:
139    case MaxContent:
140    case FitContent:
141    case Undefined:
142        ASSERT_NOT_REACHED();
143        return 0;
144    }
145    ASSERT_NOT_REACHED();
146    return 0;
147}
148
149FloatSize floatSizeForLengthSize(const LengthSize& lengthSize, const FloatSize& boxSize)
150{
151    return FloatSize(floatValueForLength(lengthSize.width(), boxSize.width()), floatValueForLength(lengthSize.height(), boxSize.height()));
152}
153
154} // namespace WebCore
155