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)
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
25 #include <wtf/Assertions.h>
26 #include <wtf/FastAllocBase.h>
27 #include <wtf/Forward.h>
28 #include <wtf/MathExtras.h>
29 #include <wtf/PassOwnArrayPtr.h>
33 const int undefinedLength = -1;
34 const int intMaxForLength = 0x7ffffff; // max value for a 28-bit int
35 const int intMinForLength = (-0x7ffffff - 1); // min value for a 28-bit int
37 enum LengthType { Auto, Relative, Percent, Fixed, Intrinsic, MinIntrinsic };
40 WTF_MAKE_FAST_ALLOCATED;
43 : m_intValue(0), m_quirk(false), m_type(Auto), m_isFloat(false)
48 : m_intValue(0), m_quirk(false), m_type(t), m_isFloat(false)
52 Length(int v, LengthType t, bool q = false)
53 : m_intValue(v), m_quirk(q), m_type(t), m_isFloat(false)
57 Length(float v, LengthType t, bool q = false)
58 : m_floatValue(v), m_quirk(q), m_type(t), m_isFloat(true)
62 Length(double v, LengthType t, bool q = false)
63 : m_quirk(q), m_type(t), m_isFloat(true)
65 m_floatValue = static_cast<float>(v);
68 bool operator==(const Length& o) const { return (getFloatValue() == o.getFloatValue()) && (m_type == o.m_type) && (m_quirk == o.m_quirk); }
69 bool operator!=(const Length& o) const { return (getFloatValue() != o.getFloatValue()) || (m_type != o.m_type) || (m_quirk != o.m_quirk); }
71 const Length& operator*=(float v)
74 m_floatValue = static_cast<float>(m_floatValue * v);
76 m_intValue = static_cast<int>(m_intValue * v);
87 ASSERT(type() == Percent);
88 return getFloatValue();
91 LengthType type() const { return static_cast<LengthType>(m_type); }
92 bool quirk() const { return m_quirk; }
94 void setQuirk(bool quirk)
99 void setValue(LengthType t, int value)
106 void setValue(int value)
108 setValue(Fixed, value);
111 void setValue(LengthType t, float value)
114 m_floatValue = value;
118 void setValue(float value)
120 *this = Length(value, Fixed);
123 // note: works only for certain types, returns undefinedLength otherwise
124 int calcValue(int maxValue, bool roundPercentages = false) const
129 return calcMinValue(maxValue, roundPercentages);
133 return undefinedLength;
137 int calcMinValue(int maxValue, bool roundPercentages = false) const
143 if (roundPercentages)
144 return static_cast<int>(round(maxValue * percent() / 100.0f));
145 // Don't remove the extra cast to float. It is needed for rounding on 32-bit Intel machines that use the FPU stack.
146 return static_cast<int>(static_cast<float>(maxValue * percent() / 100.0f));
153 float calcFloatValue(int maxValue) const
157 return getFloatValue();
159 return static_cast<float>(maxValue * percent() / 100.0f);
161 return static_cast<float>(maxValue);
163 return static_cast<float>(undefinedLength);
167 bool isUndefined() const { return value() == undefinedLength; }
170 return m_isFloat ? !m_floatValue : !m_intValue;
173 bool isPositive() const { return getFloatValue() > 0; }
174 bool isNegative() const { return getFloatValue() < 0; }
176 bool isAuto() const { return type() == Auto; }
177 bool isRelative() const { return type() == Relative; }
178 bool isPercent() const { return type() == Percent; }
179 bool isFixed() const { return type() == Fixed; }
180 bool isIntrinsicOrAuto() const { return type() == Auto || type() == MinIntrinsic || type() == Intrinsic; }
181 bool isSpecified() const { return type() == Fixed || type() == Percent; }
183 Length blend(const Length& from, float progress) const
185 // Blend two lengths to produce a new length that is in between them. Used for animation.
186 if (!from.isZero() && !isZero() && from.type() != type())
189 if (from.isZero() && isZero())
192 LengthType resultType = type();
194 resultType = from.type();
196 if (resultType == Percent) {
197 float fromPercent = from.isZero() ? 0 : from.percent();
198 float toPercent = isZero() ? 0 : percent();
199 return Length(fromPercent + (toPercent - fromPercent) * progress, Percent);
202 float fromValue = from.isZero() ? 0 : from.value();
203 float toValue = isZero() ? 0 : value();
204 return Length(fromValue + (toValue - fromValue) * progress, resultType);
208 int getIntValue() const
210 return m_isFloat ? static_cast<int>(m_floatValue) : m_intValue;
213 float getFloatValue() const
215 return m_isFloat ? m_floatValue : m_intValue;
223 unsigned char m_type;
227 PassOwnArrayPtr<Length> newCoordsArray(const String&, int& len);
228 PassOwnArrayPtr<Length> newLengthArray(const String&, int& len);
230 } // namespace WebCore