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.
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.
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.
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.
25 #include "LengthFunctions.h"
28 #include "RenderView.h"
32 int minimumIntValueForLength(const Length& length, LayoutUnit maximumValue, RenderView* renderView, bool roundPercentages)
34 return static_cast<int>(minimumValueForLength(length, maximumValue, renderView, roundPercentages));
37 int intValueForLength(const Length& length, LayoutUnit maximumValue, RenderView* renderView, bool roundPercentages)
39 return static_cast<int>(valueForLength(length, maximumValue, renderView, roundPercentages));
42 LayoutUnit minimumValueForLength(const Length& length, LayoutUnit maximumValue, RenderView* renderView, bool roundPercentages)
44 switch (length.type()) {
46 return length.value();
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));
53 return length.nonNanCalculatedValue(maximumValue);
54 case ViewportPercentageWidth:
56 return static_cast<LayoutUnit>(renderView->viewportSize().width() * length.viewportPercentageLength() / 100.0f);
57 return ZERO_LAYOUT_UNIT;
58 case ViewportPercentageHeight:
60 return static_cast<LayoutUnit>(renderView->viewportSize().height() * length.viewportPercentageLength() / 100.0f);
61 return ZERO_LAYOUT_UNIT;
62 case ViewportPercentageMin:
64 IntSize viewportSize = renderView->viewportSize();
65 return static_cast<LayoutUnit>(std::min(viewportSize.width(), viewportSize.height()) * length.viewportPercentageLength() / 100.0f);
67 return ZERO_LAYOUT_UNIT;
70 return ZERO_LAYOUT_UNIT;
79 return ZERO_LAYOUT_UNIT;
82 return ZERO_LAYOUT_UNIT;
85 LayoutUnit valueForLength(const Length& length, LayoutUnit maximumValue, RenderView* renderView, bool roundPercentages)
87 switch (length.type()) {
91 case ViewportPercentageWidth:
92 case ViewportPercentageHeight:
93 case ViewportPercentageMin:
94 return minimumValueForLength(length, maximumValue, renderView, roundPercentages);
105 ASSERT_NOT_REACHED();
106 return ZERO_LAYOUT_UNIT;
108 ASSERT_NOT_REACHED();
109 return ZERO_LAYOUT_UNIT;
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)
115 switch (length.type()) {
117 return length.getFloatValue();
119 return static_cast<float>(maximumValue * length.percent() / 100.0f);
122 return static_cast<float>(maximumValue);
124 return length.nonNanCalculatedValue(maximumValue);
125 case ViewportPercentageWidth:
127 return static_cast<int>(renderView->viewportSize().width() * length.viewportPercentageLength() / 100.0f);
129 case ViewportPercentageHeight:
131 return static_cast<int>(renderView->viewportSize().height() * length.viewportPercentageLength() / 100.0f);
133 case ViewportPercentageMin:
135 IntSize viewportSize = renderView->viewportSize();
136 return static_cast<int>(std::min(viewportSize.width(), viewportSize.height()) * length.viewportPercentageLength() / 100.0f);
146 ASSERT_NOT_REACHED();
149 ASSERT_NOT_REACHED();
153 float floatValueForLength(const Length& length, float maximumValue, RenderView* renderView)
155 switch (length.type()) {
157 return length.getFloatValue();
159 return static_cast<float>(maximumValue * length.percent() / 100.0f);
162 return static_cast<float>(maximumValue);
164 return length.nonNanCalculatedValue(maximumValue);
165 case ViewportPercentageWidth:
167 return static_cast<int>(renderView->viewportSize().width() * length.viewportPercentageLength() / 100.0f);
169 case ViewportPercentageHeight:
171 return static_cast<int>(renderView->viewportSize().height() * length.viewportPercentageLength() / 100.0f);
173 case ViewportPercentageMin:
175 IntSize viewportSize = renderView->viewportSize();
176 return static_cast<int>(std::min(viewportSize.width(), viewportSize.height()) * length.viewportPercentageLength() / 100.0f);
186 ASSERT_NOT_REACHED();
189 ASSERT_NOT_REACHED();
193 } // namespace WebCore