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 "LayoutSize.h" |
29 | #include "LengthSize.h" |
30 | |
31 | namespace WebCore { |
32 | |
33 | int intValueForLength(const Length& length, LayoutUnit maximumValue) |
34 | { |
35 | return static_cast<int>(valueForLength(length, maximumValue)); |
36 | } |
37 | |
38 | LayoutUnit valueForLength(const Length& length, LayoutUnit maximumValue) |
39 | { |
40 | switch (length.type()) { |
41 | case Fixed: |
42 | case Percent: |
43 | case Calculated: |
44 | return minimumValueForLength(length, maximumValue); |
45 | case FillAvailable: |
46 | case Auto: |
47 | return maximumValue; |
48 | case Relative: |
49 | case Intrinsic: |
50 | case MinIntrinsic: |
51 | case MinContent: |
52 | case MaxContent: |
53 | case FitContent: |
54 | case Undefined: |
55 | ASSERT_NOT_REACHED(); |
56 | return 0; |
57 | } |
58 | ASSERT_NOT_REACHED(); |
59 | return 0; |
60 | } |
61 | |
62 | LayoutSize sizeForLengthSize(const LengthSize& length, const LayoutSize& maximumValue) |
63 | { |
64 | return { valueForLength(length.width, maximumValue.width()), valueForLength(length.height, maximumValue.height()) }; |
65 | } |
66 | |
67 | // FIXME: when subpixel layout is supported this copy of floatValueForLength() can be removed. See bug 71143. |
68 | float floatValueForLength(const Length& length, LayoutUnit maximumValue) |
69 | { |
70 | switch (length.type()) { |
71 | case Fixed: |
72 | return length.value(); |
73 | case Percent: |
74 | return static_cast<float>(maximumValue * length.percent() / 100.0f); |
75 | case FillAvailable: |
76 | case Auto: |
77 | return static_cast<float>(maximumValue); |
78 | case Calculated: |
79 | return length.nonNanCalculatedValue(maximumValue); |
80 | case Relative: |
81 | case Intrinsic: |
82 | case MinIntrinsic: |
83 | case MinContent: |
84 | case MaxContent: |
85 | case FitContent: |
86 | case Undefined: |
87 | ASSERT_NOT_REACHED(); |
88 | return 0; |
89 | } |
90 | ASSERT_NOT_REACHED(); |
91 | return 0; |
92 | } |
93 | |
94 | float floatValueForLength(const Length& length, float maximumValue) |
95 | { |
96 | switch (length.type()) { |
97 | case Fixed: |
98 | return length.value(); |
99 | case Percent: |
100 | return static_cast<float>(maximumValue * length.percent() / 100.0f); |
101 | case FillAvailable: |
102 | case Auto: |
103 | return static_cast<float>(maximumValue); |
104 | case Calculated: |
105 | return length.nonNanCalculatedValue(maximumValue); |
106 | case Relative: |
107 | case Intrinsic: |
108 | case MinIntrinsic: |
109 | case MinContent: |
110 | case MaxContent: |
111 | case FitContent: |
112 | case Undefined: |
113 | ASSERT_NOT_REACHED(); |
114 | return 0; |
115 | } |
116 | ASSERT_NOT_REACHED(); |
117 | return 0; |
118 | } |
119 | |
120 | FloatSize floatSizeForLengthSize(const LengthSize& lengthSize, const FloatSize& boxSize) |
121 | { |
122 | return { floatValueForLength(lengthSize.width, boxSize.width()), floatValueForLength(lengthSize.height, boxSize.height()) }; |
123 | } |
124 | |
125 | } // namespace WebCore |
126 | |