|
1 /* |
|
2 This file is part of the KDE libraries |
|
3 |
|
4 Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
|
5 Copyright (C) 2006 Apple Computer, Inc. |
|
6 |
|
7 This library is free software; you can redistribute it and/or |
|
8 modify it under the terms of the GNU Library General Public |
|
9 License as published by the Free Software Foundation; either |
|
10 version 2 of the License, or (at your option) any later version. |
|
11 |
|
12 This library is distributed in the hope that it will be useful, |
|
13 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
15 Library General Public License for more details. |
|
16 |
|
17 You should have received a copy of the GNU Library General Public License |
|
18 along with this library; see the file COPYING.LIB. If not, write to |
|
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
20 Boston, MA 02110-1301, USA. |
|
21 */ |
|
22 |
|
23 #ifndef Length_h |
|
24 #define Length_h |
|
25 |
|
26 #include <wtf/Assertions.h> |
|
27 |
|
28 namespace WebCore { |
|
29 |
|
30 const int undefinedLength = -1; |
|
31 const int percentScaleFactor = 128; |
|
32 |
|
33 enum LengthType { Auto, Relative, Percent, Fixed, Static, Intrinsic, MinIntrinsic }; |
|
34 |
|
35 struct Length { |
|
36 Length() |
|
37 : m_value(0) |
|
38 { |
|
39 } |
|
40 |
|
41 Length(LengthType t) |
|
42 : m_value(t) |
|
43 { |
|
44 } |
|
45 |
|
46 Length(int v, LengthType t, bool q = false) |
|
47 : m_value((v * 16) | (q << 3) | t) // FIXME: Doesn't work if the passed-in value is very large! |
|
48 { |
|
49 ASSERT(t != Percent); |
|
50 } |
|
51 |
|
52 Length(double v, LengthType t, bool q = false) |
|
53 : m_value(static_cast<int>(v * percentScaleFactor) * 16 | (q << 3) | t) |
|
54 { |
|
55 ASSERT(t == Percent); |
|
56 } |
|
57 |
|
58 bool operator==(const Length& o) const { return m_value == o.m_value; } |
|
59 bool operator!=(const Length& o) const { return m_value != o.m_value; } |
|
60 |
|
61 int value() const { |
|
62 ASSERT(type() != Percent); |
|
63 return rawValue(); |
|
64 } |
|
65 |
|
66 int rawValue() const { return (m_value & ~0xF) / 16; } |
|
67 |
|
68 double percent() const |
|
69 { |
|
70 ASSERT(type() == Percent); |
|
71 return static_cast<double>(rawValue()) / percentScaleFactor; |
|
72 } |
|
73 |
|
74 LengthType type() const { return static_cast<LengthType>(m_value & 7); } |
|
75 bool quirk() const { return (m_value >> 3) & 1; } |
|
76 |
|
77 void setValue(LengthType t, int value) |
|
78 { |
|
79 ASSERT(t != Percent); |
|
80 setRawValue(t, value); |
|
81 } |
|
82 |
|
83 void setRawValue(LengthType t, int value) { m_value = value * 16 | (m_value & 0x8) | t; } |
|
84 |
|
85 void setValue(int value) |
|
86 { |
|
87 ASSERT(!value || type() != Percent); |
|
88 setRawValue(value); |
|
89 } |
|
90 |
|
91 void setRawValue(int value) { m_value = value * 16 | (m_value & 0xF); } |
|
92 |
|
93 void setValue(LengthType t, double value) |
|
94 { |
|
95 ASSERT(t == Percent); |
|
96 m_value = static_cast<int>(value * percentScaleFactor) * 16 | (m_value & 0x8) | t; |
|
97 } |
|
98 |
|
99 void setValue(double value) |
|
100 { |
|
101 ASSERT(type() == Percent); |
|
102 m_value = static_cast<int>(value * percentScaleFactor) * 16 | (m_value & 0xF); |
|
103 } |
|
104 |
|
105 // note: works only for certain types, returns undefinedLength otherwise |
|
106 int calcValue(int maxValue) const |
|
107 { |
|
108 switch (type()) { |
|
109 case Fixed: |
|
110 return value(); |
|
111 case Percent: |
|
112 return maxValue * rawValue() / (100 * percentScaleFactor); |
|
113 case Auto: |
|
114 return maxValue; |
|
115 default: |
|
116 return undefinedLength; |
|
117 } |
|
118 } |
|
119 |
|
120 int calcMinValue(int maxValue) const |
|
121 { |
|
122 switch (type()) { |
|
123 case Fixed: |
|
124 return value(); |
|
125 case Percent: |
|
126 return maxValue * rawValue() / (100 * percentScaleFactor); |
|
127 case Auto: |
|
128 default: |
|
129 return 0; |
|
130 } |
|
131 } |
|
132 |
|
133 bool isUndefined() const { return rawValue() == undefinedLength; } |
|
134 bool isZero() const { return !(m_value & ~0xF); } |
|
135 bool isPositive() const { return rawValue() > 0; } |
|
136 bool isNegative() const { return rawValue() < 0; } |
|
137 |
|
138 bool isAuto() const { return type() == Auto; } |
|
139 bool isRelative() const { return type() == Relative; } |
|
140 bool isPercent() const { return type() == Percent; } |
|
141 bool isFixed() const { return type() == Fixed; } |
|
142 bool isStatic() const { return type() == Static; } |
|
143 bool isIntrinsicOrAuto() const { return type() == Auto || type() == MinIntrinsic || type() == Intrinsic; } |
|
144 |
|
145 private: |
|
146 int m_value; |
|
147 }; |
|
148 |
|
149 } // namespace WebCore |
|
150 |
|
151 #endif // Length_h |