Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004, 2005 Nikolas Zimmermann <zimmermann@kde.org> |
| 3 | * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org> |
| 4 | * Copyright (C) 2007 Eric Seidel <eric@webkit.org> |
| 5 | * Copyright (C) 2008 Apple Inc. All rights reserved. |
| 6 | * Copyright (C) 2008 Cameron McCormack <cam@mcc.id.au> |
| 7 | * Copyright (C) Research In Motion Limited 2011. All rights reserved. |
| 8 | * |
| 9 | * This library is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU Library General Public |
| 11 | * License as published by the Free Software Foundation; either |
| 12 | * version 2 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This library is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * Library General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Library General Public License |
| 20 | * along with this library; see the file COPYING.LIB. If not, write to |
| 21 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 22 | * Boston, MA 02110-1301, USA. |
| 23 | */ |
| 24 | |
| 25 | #ifndef SVGAnimationElement_h |
| 26 | #define SVGAnimationElement_h |
| 27 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 28 | #include "core/platform/graphics/UnitBezier.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 29 | #include "core/svg/SVGAnimatedBoolean.h" |
| 30 | #include "core/svg/SVGExternalResourcesRequired.h" |
| 31 | #include "core/svg/SVGStringList.h" |
| 32 | #include "core/svg/SVGTests.h" |
| 33 | #include "core/svg/animation/SMILTime.h" |
| 34 | #include "core/svg/animation/SVGSMILElement.h" |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | enum AnimationMode { |
| 39 | NoAnimation, |
| 40 | FromToAnimation, |
| 41 | FromByAnimation, |
| 42 | ToAnimation, |
| 43 | ByAnimation, |
| 44 | ValuesAnimation, |
| 45 | PathAnimation // Used by AnimateMotion. |
| 46 | }; |
| 47 | |
| 48 | // If we have 'currentColor' or 'inherit' as animation value, we need to grab |
| 49 | // the value during the animation since the value can be animated itself. |
| 50 | enum AnimatedPropertyValueType { |
| 51 | RegularPropertyValue, |
| 52 | CurrentColorValue, |
| 53 | InheritValue |
| 54 | }; |
| 55 | |
| 56 | enum CalcMode { |
| 57 | CalcModeDiscrete, |
| 58 | CalcModeLinear, |
| 59 | CalcModePaced, |
| 60 | CalcModeSpline |
| 61 | }; |
| 62 | |
| 63 | class ConditionEventListener; |
| 64 | class TimeContainer; |
| 65 | class SVGAnimatedType; |
| 66 | |
| 67 | class SVGAnimationElement : public SVGSMILElement, |
| 68 | public SVGTests, |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame^] | 69 | public SVGExternalResourcesRequired { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 70 | public: |
| 71 | // SVGAnimationElement |
| 72 | float getStartTime() const; |
| 73 | float getCurrentTime() const; |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 74 | float getSimpleDuration() const; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 75 | |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame^] | 76 | void beginElement(); |
| 77 | void beginElementAt(float offset); |
| 78 | void endElement(); |
| 79 | void endElementAt(float offset); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 80 | |
| 81 | static bool isTargetAttributeCSSProperty(SVGElement*, const QualifiedName&); |
| 82 | |
| 83 | virtual bool isAdditive() const; |
| 84 | bool isAccumulated() const; |
| 85 | AnimationMode animationMode() const { return m_animationMode; } |
| 86 | CalcMode calcMode() const { return m_calcMode; } |
| 87 | |
| 88 | enum ShouldApplyAnimation { |
| 89 | DontApplyAnimation, |
| 90 | ApplyCSSAnimation, |
| 91 | ApplyXMLAnimation |
| 92 | }; |
| 93 | |
| 94 | ShouldApplyAnimation shouldApplyAnimation(SVGElement* targetElement, const QualifiedName& attributeName); |
| 95 | |
| 96 | AnimatedPropertyValueType fromPropertyValueType() const { return m_fromPropertyValueType; } |
| 97 | AnimatedPropertyValueType toPropertyValueType() const { return m_toPropertyValueType; } |
| 98 | |
| 99 | template<typename AnimatedType> |
| 100 | void adjustForInheritance(AnimatedType (*parseTypeFromString)(SVGAnimationElement*, const String&), |
| 101 | AnimatedPropertyValueType valueType, AnimatedType& animatedType, SVGElement* contextElement) |
| 102 | { |
| 103 | if (valueType != InheritValue) |
| 104 | return; |
| 105 | // Replace 'inherit' by its computed property value. |
| 106 | ASSERT(parseTypeFromString); |
| 107 | String typeString; |
| 108 | adjustForInheritance(contextElement, attributeName(), typeString); |
| 109 | animatedType = (*parseTypeFromString)(this, typeString); |
| 110 | } |
| 111 | |
| 112 | template<typename AnimatedType> |
| 113 | bool adjustFromToListValues(const AnimatedType& fromList, const AnimatedType& toList, AnimatedType& animatedList, float percentage, bool resizeAnimatedListIfNeeded = true) |
| 114 | { |
| 115 | // If no 'to' value is given, nothing to animate. |
| 116 | unsigned toListSize = toList.size(); |
| 117 | if (!toListSize) |
| 118 | return false; |
| 119 | |
| 120 | // If the 'from' value is given and it's length doesn't match the 'to' value list length, fallback to a discrete animation. |
| 121 | unsigned fromListSize = fromList.size(); |
| 122 | if (fromListSize != toListSize && fromListSize) { |
| 123 | if (percentage < 0.5) { |
| 124 | if (animationMode() != ToAnimation) |
| 125 | animatedList = AnimatedType(fromList); |
| 126 | } else |
| 127 | animatedList = AnimatedType(toList); |
| 128 | |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | ASSERT(!fromListSize || fromListSize == toListSize); |
| 133 | if (resizeAnimatedListIfNeeded && animatedList.size() < toListSize) |
| 134 | animatedList.resize(toListSize); |
| 135 | |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | template<typename AnimatedType> |
| 140 | void animateDiscreteType(float percentage, const AnimatedType& fromType, const AnimatedType& toType, AnimatedType& animatedType) |
| 141 | { |
| 142 | if ((animationMode() == FromToAnimation && percentage > 0.5) || animationMode() == ToAnimation || percentage == 1) { |
| 143 | animatedType = AnimatedType(toType); |
| 144 | return; |
| 145 | } |
| 146 | animatedType = AnimatedType(fromType); |
| 147 | } |
| 148 | |
| 149 | void animateAdditiveNumber(float percentage, unsigned repeatCount, float fromNumber, float toNumber, float toAtEndOfDurationNumber, float& animatedNumber) |
| 150 | { |
| 151 | float number; |
| 152 | if (calcMode() == CalcModeDiscrete) |
| 153 | number = percentage < 0.5 ? fromNumber : toNumber; |
| 154 | else |
| 155 | number = (toNumber - fromNumber) * percentage + fromNumber; |
| 156 | |
| 157 | if (isAccumulated() && repeatCount) |
| 158 | number += toAtEndOfDurationNumber * repeatCount; |
| 159 | |
| 160 | if (isAdditive() && animationMode() != ToAnimation) |
| 161 | animatedNumber += number; |
| 162 | else |
| 163 | animatedNumber = number; |
| 164 | } |
| 165 | |
| 166 | protected: |
| 167 | SVGAnimationElement(const QualifiedName&, Document*); |
| 168 | |
| 169 | void computeCSSPropertyValue(SVGElement*, CSSPropertyID, String& value); |
| 170 | virtual void determinePropertyValueTypes(const String& from, const String& to); |
| 171 | |
| 172 | bool isSupportedAttribute(const QualifiedName&); |
| 173 | virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE; |
| 174 | virtual void svgAttributeChanged(const QualifiedName&) OVERRIDE; |
| 175 | |
| 176 | enum AttributeType { |
| 177 | AttributeTypeCSS, |
| 178 | AttributeTypeXML, |
| 179 | AttributeTypeAuto |
| 180 | }; |
| 181 | AttributeType attributeType() const { return m_attributeType; } |
| 182 | |
| 183 | String toValue() const; |
| 184 | String byValue() const; |
| 185 | String fromValue() const; |
| 186 | |
| 187 | String targetAttributeBaseValue(); |
| 188 | |
| 189 | // from SVGSMILElement |
| 190 | virtual void startedActiveInterval() OVERRIDE; |
| 191 | virtual void updateAnimation(float percent, unsigned repeat, SVGSMILElement* resultElement) OVERRIDE; |
| 192 | |
| 193 | AnimatedPropertyValueType m_fromPropertyValueType; |
| 194 | AnimatedPropertyValueType m_toPropertyValueType; |
| 195 | |
| 196 | virtual void setTargetElement(SVGElement*) OVERRIDE; |
| 197 | virtual void setAttributeName(const QualifiedName&) OVERRIDE; |
| 198 | bool hasInvalidCSSAttributeType() const { return m_hasInvalidCSSAttributeType; } |
| 199 | |
| 200 | virtual void updateAnimationMode(); |
| 201 | void setAnimationMode(AnimationMode animationMode) { m_animationMode = animationMode; } |
| 202 | void setCalcMode(CalcMode calcMode) { m_calcMode = calcMode; } |
| 203 | |
| 204 | private: |
| 205 | virtual void animationAttributeChanged() OVERRIDE; |
| 206 | void setAttributeType(const AtomicString&); |
| 207 | |
| 208 | void checkInvalidCSSAttributeType(SVGElement*); |
| 209 | |
| 210 | virtual bool calculateToAtEndOfDurationValue(const String& toAtEndOfDurationString) = 0; |
| 211 | virtual bool calculateFromAndToValues(const String& fromString, const String& toString) = 0; |
| 212 | virtual bool calculateFromAndByValues(const String& fromString, const String& byString) = 0; |
| 213 | virtual void calculateAnimatedValue(float percent, unsigned repeatCount, SVGSMILElement* resultElement) = 0; |
| 214 | virtual float calculateDistance(const String& /*fromString*/, const String& /*toString*/) { return -1.f; } |
| 215 | |
| 216 | void currentValuesForValuesAnimation(float percent, float& effectivePercent, String& from, String& to); |
| 217 | void calculateKeyTimesForCalcModePaced(); |
| 218 | float calculatePercentFromKeyPoints(float percent) const; |
| 219 | void currentValuesFromKeyPoints(float percent, float& effectivePercent, String& from, String& to) const; |
| 220 | float calculatePercentForSpline(float percent, unsigned splineIndex) const; |
| 221 | float calculatePercentForFromTo(float percent) const; |
| 222 | unsigned calculateKeyTimesIndex(float percent) const; |
| 223 | |
| 224 | void applyAnimatedValue(ShouldApplyAnimation, SVGElement* targetElement, const QualifiedName& attributeName, SVGAnimatedType*); |
| 225 | void adjustForInheritance(SVGElement* targetElement, const QualifiedName& attributeName, String&); |
| 226 | |
| 227 | BEGIN_DECLARE_ANIMATED_PROPERTIES(SVGAnimationElement) |
| 228 | DECLARE_ANIMATED_BOOLEAN(ExternalResourcesRequired, externalResourcesRequired) |
| 229 | END_DECLARE_ANIMATED_PROPERTIES |
| 230 | |
| 231 | // SVGTests |
| 232 | virtual void synchronizeRequiredFeatures() { SVGTests::synchronizeRequiredFeatures(this); } |
| 233 | virtual void synchronizeRequiredExtensions() { SVGTests::synchronizeRequiredExtensions(this); } |
| 234 | virtual void synchronizeSystemLanguage() { SVGTests::synchronizeSystemLanguage(this); } |
| 235 | |
| 236 | void setCalcMode(const AtomicString&); |
| 237 | |
| 238 | bool m_animationValid; |
| 239 | |
| 240 | AttributeType m_attributeType; |
| 241 | Vector<String> m_values; |
| 242 | Vector<float> m_keyTimes; |
| 243 | Vector<float> m_keyPoints; |
| 244 | Vector<UnitBezier> m_keySplines; |
| 245 | String m_lastValuesAnimationFrom; |
| 246 | String m_lastValuesAnimationTo; |
| 247 | bool m_hasInvalidCSSAttributeType; |
| 248 | CalcMode m_calcMode; |
| 249 | AnimationMode m_animationMode; |
| 250 | }; |
| 251 | |
| 252 | } // namespace WebCore |
| 253 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 254 | #endif // SVGAnimationElement_h |