2 * Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
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.
25 #include "ExceptionCode.h"
26 #include "SVGParsingError.h"
27 #include "SVGPropertyTraits.h"
31 class CSSPrimitiveValue;
34 LengthTypeUnknown = 0,
36 LengthTypePercentage = 2,
53 enum SVGLengthNegativeValuesMode {
63 // Forward declare these enums in the w3c naming scheme, for IDL generation
65 SVG_LENGTHTYPE_UNKNOWN = LengthTypeUnknown,
66 SVG_LENGTHTYPE_NUMBER = LengthTypeNumber,
67 SVG_LENGTHTYPE_PERCENTAGE = LengthTypePercentage,
68 SVG_LENGTHTYPE_EMS = LengthTypeEMS,
69 SVG_LENGTHTYPE_EXS = LengthTypeEXS,
70 SVG_LENGTHTYPE_PX = LengthTypePX,
71 SVG_LENGTHTYPE_CM = LengthTypeCM,
72 SVG_LENGTHTYPE_MM = LengthTypeMM,
73 SVG_LENGTHTYPE_IN = LengthTypeIN,
74 SVG_LENGTHTYPE_PT = LengthTypePT,
75 SVG_LENGTHTYPE_PC = LengthTypePC
78 SVGLength(SVGLengthMode mode = LengthModeOther, const String& valueAsString = String());
79 SVGLength(const SVGElement*, float, SVGLengthMode mode = LengthModeOther, SVGLengthType type = LengthTypeNumber);
80 SVGLength(const SVGLength&);
82 SVGLengthType unitType() const;
84 bool operator==(const SVGLength&) const;
85 bool operator!=(const SVGLength&) const;
87 static SVGLength construct(SVGLengthMode, const String&, SVGParsingError&, SVGLengthNegativeValuesMode = AllowNegativeLengths);
89 float value(const SVGElement* context) const;
90 float value(const SVGElement* context, ExceptionCode&) const;
91 void setValue(float, const SVGElement* context, ExceptionCode&);
92 void setValue(const SVGElement* context, float, SVGLengthMode, SVGLengthType, ExceptionCode&);
94 float valueInSpecifiedUnits() const { return m_valueInSpecifiedUnits; }
95 void setValueInSpecifiedUnits(float value) { m_valueInSpecifiedUnits = value; }
97 float valueAsPercentage() const;
99 String valueAsString() const;
100 void setValueAsString(const String&, ExceptionCode&);
101 void setValueAsString(const String&, SVGLengthMode, ExceptionCode&);
103 void newValueSpecifiedUnits(unsigned short, float valueInSpecifiedUnits, ExceptionCode&);
104 void convertToSpecifiedUnits(unsigned short, const SVGElement* context, ExceptionCode&);
107 inline bool isRelative() const
109 SVGLengthType type = unitType();
110 return type == LengthTypePercentage || type == LengthTypeEMS || type == LengthTypeEXS;
115 return !m_valueInSpecifiedUnits;
118 static SVGLength fromCSSPrimitiveValue(CSSPrimitiveValue*);
119 static PassRefPtr<CSSPrimitiveValue> toCSSPrimitiveValue(const SVGLength&);
120 static SVGLengthMode lengthModeForAnimatedLengthAttribute(const QualifiedName&);
122 SVGLength blend(const SVGLength& from, float progress) const
124 SVGLengthType toType = unitType();
125 SVGLengthType fromType = from.unitType();
127 if ((from.isZero() && isZero())
128 || fromType == LengthTypeUnknown
129 || toType == LengthTypeUnknown
130 || (!from.isZero() && fromType != LengthTypePercentage && toType == LengthTypePercentage)
131 || (!isZero() && fromType == LengthTypePercentage && toType != LengthTypePercentage)
132 || (!from.isZero() && !isZero() && (fromType == LengthTypeEMS || fromType == LengthTypeEXS) && fromType != toType))
136 ExceptionCode ec = 0;
138 if (fromType == LengthTypePercentage || toType == LengthTypePercentage) {
139 float fromPercent = from.valueAsPercentage() * 100;
140 float toPercent = valueAsPercentage() * 100;
141 length.newValueSpecifiedUnits(LengthTypePercentage, fromPercent + (toPercent - fromPercent) * progress, ec);
147 if (fromType == toType || from.isZero() || isZero() || fromType == LengthTypeEMS || fromType == LengthTypeEXS) {
148 float fromValue = from.valueInSpecifiedUnits();
149 float toValue = valueInSpecifiedUnits();
151 length.newValueSpecifiedUnits(fromType, fromValue + (toValue - fromValue) * progress, ec);
153 length.newValueSpecifiedUnits(toType, fromValue + (toValue - fromValue) * progress, ec);
159 ASSERT(!isRelative());
160 ASSERT(!from.isRelative());
161 float fromValueInUserUnits = convertValueToUserUnits(from.valueInSpecifiedUnits(), fromType, 0, ec);
165 float fromValue = convertValueFromUserUnits(fromValueInUserUnits, toType, 0, ec);
169 float toValue = valueInSpecifiedUnits();
170 length.newValueSpecifiedUnits(toType, fromValue + (toValue - fromValue) * progress, ec);
178 bool determineViewport(const SVGElement* context, float& width, float& height) const;
180 float convertValueToUserUnits(float value, SVGLengthType fromUnit, const SVGElement* context, ExceptionCode&) const;
181 float convertValueFromUserUnits(float value, SVGLengthType toUnit, const SVGElement* context, ExceptionCode&) const;
183 float convertValueFromPercentageToUserUnits(float value, const SVGElement* context, ExceptionCode&) const;
184 float convertValueFromUserUnitsToPercentage(float value, const SVGElement* context, ExceptionCode&) const;
186 float convertValueFromUserUnitsToEMS(float value, const SVGElement* context, ExceptionCode&) const;
187 float convertValueFromEMSToUserUnits(float value, const SVGElement* context, ExceptionCode&) const;
189 float convertValueFromUserUnitsToEXS(float value, const SVGElement* context, ExceptionCode&) const;
190 float convertValueFromEXSToUserUnits(float value, const SVGElement* context, ExceptionCode&) const;
193 float m_valueInSpecifiedUnits;
198 struct SVGPropertyTraits<SVGLength> {
199 static SVGLength initialValue() { return SVGLength(); }
200 static String toString(const SVGLength& type) { return type.valueAsString(); }
204 } // namespace WebCore
206 #endif // ENABLE(SVG)
207 #endif // SVGLength_h