| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org> | 2 * Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org> |
| 3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org> | 3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org> |
| 4 * | 4 * |
| 5 * This library is free software; you can redistribute it and/or | 5 * This library is free software; you can redistribute it and/or |
| 6 * modify it under the terms of the GNU Library General Public | 6 * modify it under the terms of the GNU Library General Public |
| 7 * License as published by the Free Software Foundation; either | 7 * License as published by the Free Software Foundation; either |
| 8 * version 2 of the License, or (at your option) any later version. | 8 * version 2 of the License, or (at your option) any later version. |
| 9 * | 9 * |
| 10 * This library is distributed in the hope that it will be useful, | 10 * This library is distributed in the hope that it will be useful, |
| 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 * Library General Public License for more details. | 13 * Library General Public License for more details. |
| 14 * | 14 * |
| 15 * You should have received a copy of the GNU Library General Public License | 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 | 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, | 17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 18 * Boston, MA 02110-1301, USA. | 18 * Boston, MA 02110-1301, USA. |
| 19 */ | 19 */ |
| 20 | 20 |
| 21 #ifndef SVGLength_h | 21 #ifndef SVGLength_h |
| 22 #define SVGLength_h | 22 #define SVGLength_h |
| 23 | 23 |
| 24 #include "bindings/v8/ExceptionState.h" | 24 #include "bindings/v8/ExceptionMessages.h" |
| 25 #include "bindings/v8/ExceptionStatePlaceholder.h" |
| 26 #include "bindings/v8/ScriptWrappable.h" |
| 27 #include "core/dom/ExceptionCode.h" |
| 25 #include "core/svg/SVGLengthContext.h" | 28 #include "core/svg/SVGLengthContext.h" |
| 26 #include "core/svg/SVGParsingError.h" | 29 #include "core/svg/SVGParsingError.h" |
| 27 #include "core/svg/properties/SVGPropertyTraits.h" | 30 #include "core/svg/properties/NewSVGProperty.h" |
| 28 #include "platform/animation/AnimationUtilities.h" | |
| 29 | 31 |
| 30 namespace WebCore { | 32 namespace WebCore { |
| 31 | 33 |
| 34 /* use SVGLength.h decls */ |
| 32 class CSSPrimitiveValue; | 35 class CSSPrimitiveValue; |
| 33 class ExceptionState; | 36 class ExceptionState; |
| 34 class QualifiedName; | 37 class QualifiedName; |
| 35 | 38 |
| 36 enum SVGLengthNegativeValuesMode { | 39 enum SVGLengthNegativeValuesMode { |
| 37 AllowNegativeLengths, | 40 AllowNegativeLengths, |
| 38 ForbidNegativeLengths | 41 ForbidNegativeLengths |
| 39 }; | 42 }; |
| 40 | 43 |
| 41 class SVGLength { | 44 class SVGLengthTearOff; |
| 42 WTF_MAKE_FAST_ALLOCATED; | 45 |
| 46 class SVGLength : public NewSVGPropertyBase { |
| 43 public: | 47 public: |
| 44 // Forward declare these enums in the w3c naming scheme, for IDL generation | 48 typedef SVGLengthTearOff TearOffType; |
| 45 enum { | |
| 46 SVG_LENGTHTYPE_UNKNOWN = LengthTypeUnknown, | |
| 47 SVG_LENGTHTYPE_NUMBER = LengthTypeNumber, | |
| 48 SVG_LENGTHTYPE_PERCENTAGE = LengthTypePercentage, | |
| 49 SVG_LENGTHTYPE_EMS = LengthTypeEMS, | |
| 50 SVG_LENGTHTYPE_EXS = LengthTypeEXS, | |
| 51 SVG_LENGTHTYPE_PX = LengthTypePX, | |
| 52 SVG_LENGTHTYPE_CM = LengthTypeCM, | |
| 53 SVG_LENGTHTYPE_MM = LengthTypeMM, | |
| 54 SVG_LENGTHTYPE_IN = LengthTypeIN, | |
| 55 SVG_LENGTHTYPE_PT = LengthTypePT, | |
| 56 SVG_LENGTHTYPE_PC = LengthTypePC | |
| 57 }; | |
| 58 | 49 |
| 59 SVGLength(SVGLengthMode = LengthModeOther, const String& valueAsString = Str
ing()); | 50 static PassRefPtr<SVGLength> create(SVGLengthMode mode = LengthModeOther) |
| 60 SVGLength(const SVGLengthContext&, float, SVGLengthMode = LengthModeOther, S
VGLengthType = LengthTypeNumber); | 51 { |
| 61 SVGLength(const SVGLength&); | 52 return adoptRef(new SVGLength(mode)); |
| 53 } |
| 62 | 54 |
| 63 SVGLengthType unitType() const; | 55 PassRefPtr<SVGLength> clone() const; |
| 64 SVGLengthMode unitMode() const; | 56 virtual PassRefPtr<NewSVGPropertyBase> cloneForAnimation(const String&) cons
t OVERRIDE; |
| 57 |
| 58 SVGLengthType unitType() const { return static_cast<SVGLengthType>(m_unitTyp
e); } |
| 59 void setUnitType(SVGLengthType); |
| 60 SVGLengthMode unitMode() const { return static_cast<SVGLengthMode>(m_unitMod
e); } |
| 65 | 61 |
| 66 bool operator==(const SVGLength&) const; | 62 bool operator==(const SVGLength&) const; |
| 67 bool operator!=(const SVGLength&) const; | 63 bool operator!=(const SVGLength& other) const { return !operator==(other); } |
| 68 | 64 |
| 69 static SVGLength construct(SVGLengthMode, const String&, SVGParsingError&, S
VGLengthNegativeValuesMode = AllowNegativeLengths); | 65 float value(const SVGLengthContext& context) const |
| 70 | 66 { |
| 71 float value(const SVGLengthContext&) const; | 67 return value(context, IGNORE_EXCEPTION); |
| 68 } |
| 72 float value(const SVGLengthContext&, ExceptionState&) const; | 69 float value(const SVGLengthContext&, ExceptionState&) const; |
| 73 void setValue(float, const SVGLengthContext&, ExceptionState&); | 70 void setValue(float, const SVGLengthContext&, ExceptionState&); |
| 74 void setValue(const SVGLengthContext&, float, SVGLengthMode, SVGLengthType,
ExceptionState&); | |
| 75 | 71 |
| 76 float valueInSpecifiedUnits() const { return m_valueInSpecifiedUnits; } | 72 float valueInSpecifiedUnits() const { return m_valueInSpecifiedUnits; } |
| 77 void setValueInSpecifiedUnits(float value) { m_valueInSpecifiedUnits = value
; } | 73 void setValueInSpecifiedUnits(float value) { m_valueInSpecifiedUnits = value
; } |
| 78 | 74 |
| 79 float valueAsPercentage() const; | 75 float valueAsPercentage() const; |
| 80 | 76 |
| 81 String valueAsString() const; | 77 virtual String valueAsString() const OVERRIDE; |
| 82 void setValueAsString(const String&, ExceptionState&); | 78 void setValueAsString(const String&, ExceptionState&); |
| 83 void setValueAsString(const String&, SVGLengthMode, ExceptionState&); | |
| 84 | 79 |
| 85 void newValueSpecifiedUnits(unsigned short, float valueInSpecifiedUnits, Exc
eptionState&); | 80 void newValueSpecifiedUnits(SVGLengthType, float valueInSpecifiedUnits); |
| 86 void convertToSpecifiedUnits(unsigned short, const SVGLengthContext&, Except
ionState&); | 81 void convertToSpecifiedUnits(SVGLengthType, const SVGLengthContext&, Excepti
onState&); |
| 87 | 82 |
| 88 // Helper functions | 83 // Helper functions |
| 89 inline bool isRelative() const | 84 inline bool isRelative() const |
| 90 { | 85 { |
| 91 SVGLengthType type = unitType(); | 86 return m_unitType == LengthTypePercentage |
| 92 return type == LengthTypePercentage || type == LengthTypeEMS || type ==
LengthTypeEXS; | 87 || m_unitType == LengthTypeEMS |
| 88 || m_unitType == LengthTypeEXS; |
| 93 } | 89 } |
| 94 | 90 |
| 95 bool isZero() const | 91 bool isZero() const |
| 96 { | 92 { |
| 97 return !m_valueInSpecifiedUnits; | 93 return !m_valueInSpecifiedUnits; |
| 98 } | 94 } |
| 99 | 95 |
| 100 static SVGLength fromCSSPrimitiveValue(CSSPrimitiveValue*); | 96 static PassRefPtr<SVGLength> fromCSSPrimitiveValue(CSSPrimitiveValue*); |
| 101 static PassRefPtr<CSSPrimitiveValue> toCSSPrimitiveValue(const SVGLength&); | 97 static PassRefPtr<CSSPrimitiveValue> toCSSPrimitiveValue(PassRefPtr<SVGLengt
h>); |
| 102 static SVGLengthMode lengthModeForAnimatedLengthAttribute(const QualifiedNam
e&); | 98 static SVGLengthMode lengthModeForAnimatedLengthAttribute(const QualifiedNam
e&); |
| 103 | 99 |
| 104 SVGLength blend(const SVGLength& from, float progress) const | 100 PassRefPtr<SVGLength> blend(PassRefPtr<SVGLength> from, float progress) cons
t; |
| 105 { | |
| 106 SVGLengthType toType = unitType(); | |
| 107 SVGLengthType fromType = from.unitType(); | |
| 108 if ((from.isZero() && isZero()) | |
| 109 || fromType == LengthTypeUnknown | |
| 110 || toType == LengthTypeUnknown | |
| 111 || (!from.isZero() && fromType != LengthTypePercentage && toType ==
LengthTypePercentage) | |
| 112 || (!isZero() && fromType == LengthTypePercentage && toType != Lengt
hTypePercentage) | |
| 113 || (!from.isZero() && !isZero() && (fromType == LengthTypeEMS || fro
mType == LengthTypeEXS) && fromType != toType)) | |
| 114 return *this; | |
| 115 | 101 |
| 116 SVGLength length; | 102 virtual void add(PassRefPtr<NewSVGPropertyBase>, SVGElement*) OVERRIDE; |
| 117 TrackExceptionState exceptionState; | 103 virtual void calculateAnimatedValue(SVGAnimationElement*, float percentage,
unsigned repeatCount, PassRefPtr<NewSVGPropertyBase> from, PassRefPtr<NewSVGProp
ertyBase> to, PassRefPtr<NewSVGPropertyBase> toAtEndOfDurationValue, SVGElement*
contextElement) OVERRIDE; |
| 104 virtual float calculateDistance(PassRefPtr<NewSVGPropertyBase> to, SVGElemen
t* contextElement) OVERRIDE; |
| 118 | 105 |
| 119 if (fromType == LengthTypePercentage || toType == LengthTypePercentage)
{ | 106 static AnimatedPropertyType classType() { return AnimatedLength; } |
| 120 float fromPercent = from.valueAsPercentage() * 100; | |
| 121 float toPercent = valueAsPercentage() * 100; | |
| 122 length.newValueSpecifiedUnits(LengthTypePercentage, WebCore::blend(f
romPercent, toPercent, progress), exceptionState); | |
| 123 if (exceptionState.hadException()) | |
| 124 return SVGLength(); | |
| 125 return length; | |
| 126 } | |
| 127 | |
| 128 if (fromType == toType || from.isZero() || isZero() || fromType == Lengt
hTypeEMS || fromType == LengthTypeEXS) { | |
| 129 float fromValue = from.valueInSpecifiedUnits(); | |
| 130 float toValue = valueInSpecifiedUnits(); | |
| 131 if (isZero()) | |
| 132 length.newValueSpecifiedUnits(fromType, WebCore::blend(fromValue
, toValue, progress), exceptionState); | |
| 133 else | |
| 134 length.newValueSpecifiedUnits(toType, WebCore::blend(fromValue,
toValue, progress), exceptionState); | |
| 135 if (exceptionState.hadException()) | |
| 136 return SVGLength(); | |
| 137 return length; | |
| 138 } | |
| 139 | |
| 140 ASSERT(!isRelative()); | |
| 141 ASSERT(!from.isRelative()); | |
| 142 | |
| 143 SVGLengthContext nonRelativeLengthContext(0); | |
| 144 float fromValueInUserUnits = nonRelativeLengthContext.convertValueToUser
Units(from.valueInSpecifiedUnits(), from.unitMode(), fromType, exceptionState); | |
| 145 if (exceptionState.hadException()) | |
| 146 return SVGLength(); | |
| 147 | |
| 148 float fromValue = nonRelativeLengthContext.convertValueFromUserUnits(fro
mValueInUserUnits, unitMode(), toType, exceptionState); | |
| 149 if (exceptionState.hadException()) | |
| 150 return SVGLength(); | |
| 151 | |
| 152 float toValue = valueInSpecifiedUnits(); | |
| 153 length.newValueSpecifiedUnits(toType, WebCore::blend(fromValue, toValue,
progress), exceptionState); | |
| 154 | |
| 155 if (exceptionState.hadException()) | |
| 156 return SVGLength(); | |
| 157 return length; | |
| 158 } | |
| 159 | 107 |
| 160 private: | 108 private: |
| 109 SVGLength(SVGLengthMode); |
| 110 SVGLength(const SVGLength&); |
| 111 |
| 161 float m_valueInSpecifiedUnits; | 112 float m_valueInSpecifiedUnits; |
| 162 unsigned int m_unit; | 113 unsigned m_unitMode : 2; |
| 114 unsigned m_unitType : 4; |
| 163 }; | 115 }; |
| 164 | 116 |
| 165 template<> | 117 inline PassRefPtr<SVGLength> toSVGLength(PassRefPtr<NewSVGPropertyBase> passBase
) |
| 166 struct SVGPropertyTraits<SVGLength> { | 118 { |
| 167 static SVGLength initialValue() { return SVGLength(); } | 119 RefPtr<NewSVGPropertyBase> base = passBase; |
| 168 static String toString(const SVGLength& type) { return type.valueAsString();
} | 120 ASSERT(base->type() == SVGLength::classType()); |
| 169 }; | 121 return static_pointer_cast<SVGLength>(base.release()); |
| 170 | 122 } |
| 171 | 123 |
| 172 } // namespace WebCore | 124 } // namespace WebCore |
| 173 | 125 |
| 174 #endif // SVGLength_h | 126 #endif // SVGLength_h |
| OLD | NEW |