]>
git.saurik.com Git - iphone-api.git/blob - WebCore/Length.h
2 Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
24 #include <wtf/Assertions.h>
25 #include <wtf/MathExtras.h>
31 const int undefinedLength
= -1;
32 const int percentScaleFactor
= 128;
34 enum LengthType
{ Auto
, Relative
, Percent
, Fixed
, Static
, Intrinsic
, MinIntrinsic
};
47 Length(int v
, LengthType t
, bool q
= false)
48 : m_value((v
* 16) | (q
<< 3) | t
) // FIXME: Doesn't work if the passed-in value is very large!
53 Length(double v
, LengthType t
, bool q
= false)
54 : m_value(static_cast<int>(v
* percentScaleFactor
) * 16 | (q
<< 3) | t
)
59 bool operator==(const Length
& o
) const { return m_value
== o
.m_value
; }
60 bool operator!=(const Length
& o
) const { return m_value
!= o
.m_value
; }
63 ASSERT(type() != Percent
);
67 int rawValue() const { return (m_value
& ~0xF) / 16; }
69 double percent() const
71 ASSERT(type() == Percent
);
72 return static_cast<double>(rawValue()) / percentScaleFactor
;
75 LengthType
type() const { return static_cast<LengthType
>(m_value
& 7); }
76 bool quirk() const { return (m_value
>> 3) & 1; }
78 void setValue(LengthType t
, int value
)
81 setRawValue(t
, value
);
84 void setRawValue(LengthType t
, int value
) { m_value
= value
* 16 | (m_value
& 0x8) | t
; }
86 void setValue(int value
)
88 ASSERT(!value
|| type() != Percent
);
92 void setRawValue(int value
) { m_value
= value
* 16 | (m_value
& 0xF); }
94 void setValue(LengthType t
, double value
)
97 m_value
= static_cast<int>(value
* percentScaleFactor
) * 16 | (m_value
& 0x8) | t
;
100 void setValue(double value
)
102 ASSERT(type() == Percent
);
103 m_value
= static_cast<int>(value
* percentScaleFactor
) * 16 | (m_value
& 0xF);
106 // note: works only for certain types, returns undefinedLength otherwise
107 int calcValue(int maxValue
, bool roundPercentages
= false) const
113 if (roundPercentages
)
114 return static_cast<int>(round(maxValue
* percent() / 100.0));
115 return maxValue
* rawValue() / (100 * percentScaleFactor
);
119 return undefinedLength
;
123 int calcMinValue(int maxValue
, bool roundPercentages
= false) const
129 if (roundPercentages
)
130 return static_cast<int>(round(maxValue
* percent() / 100.0));
131 return maxValue
* rawValue() / (100 * percentScaleFactor
);
138 float calcFloatValue(int maxValue
) const
142 return static_cast<float>(value());
144 return static_cast<float>(maxValue
* percent() / 100.0);
146 return static_cast<float>(maxValue
);
148 return static_cast<float>(undefinedLength
);
152 bool isUndefined() const { return rawValue() == undefinedLength
; }
153 bool isZero() const { return !(m_value
& ~0xF); }
154 bool isPositive() const { return rawValue() > 0; }
155 bool isNegative() const { return rawValue() < 0; }
157 bool isAuto() const { return type() == Auto
; }
158 bool isRelative() const { return type() == Relative
; }
159 bool isPercent() const { return type() == Percent
; }
160 bool isFixed() const { return type() == Fixed
; }
161 bool isStatic() const { return type() == Static
; }
162 bool isIntrinsicOrAuto() const { return type() == Auto
|| type() == MinIntrinsic
|| type() == Intrinsic
; }
164 Length
blend(const Length
& from
, double progress
) const
166 // Blend two lengths to produce a new length that is in between them. Used for animation.
167 if (!from
.isZero() && !isZero() && from
.type() != type())
170 if (from
.isZero() && isZero())
173 LengthType resultType
= type();
175 resultType
= from
.type();
177 if (resultType
== Percent
) {
178 double fromPercent
= from
.isZero() ? 0. : from
.percent();
179 double toPercent
= isZero() ? 0. : percent();
180 return Length(fromPercent
+ (toPercent
- fromPercent
) * progress
, Percent
);
183 int fromValue
= from
.isZero() ? 0 : from
.value();
184 int toValue
= isZero() ? 0 : value();
185 return Length(int(fromValue
+ (toValue
- fromValue
) * progress
), resultType
);
192 Length
* newCoordsArray(const String
&, int& len
);
193 Length
* newLengthArray(const String
&, int& len
);
195 } // namespace WebCore