| 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-2019 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 | #pragma once |
| 26 | |
| 27 | #include "SVGExternalResourcesRequired.h" |
| 28 | #include "SVGSMILElement.h" |
| 29 | #include "SVGTests.h" |
| 30 | #include "UnitBezier.h" |
| 31 | |
| 32 | namespace WebCore { |
| 33 | |
| 34 | class ConditionEventListener; |
| 35 | class TimeContainer; |
| 36 | |
| 37 | // If we have 'currentColor' or 'inherit' as animation value, we need to grab |
| 38 | // the value during the animation since the value can be animated itself. |
| 39 | enum AnimatedPropertyValueType { RegularPropertyValue, CurrentColorValue, InheritValue }; |
| 40 | |
| 41 | class SVGAnimationElement : public SVGSMILElement, public SVGExternalResourcesRequired, public SVGTests { |
| 42 | WTF_MAKE_ISO_ALLOCATED(SVGAnimationElement); |
| 43 | public: |
| 44 | float getStartTime() const; |
| 45 | float getCurrentTime() const; |
| 46 | float getSimpleDuration() const; |
| 47 | |
| 48 | void beginElement(); |
| 49 | void beginElementAt(float offset); |
| 50 | void endElement(); |
| 51 | void endElementAt(float offset); |
| 52 | |
| 53 | static bool isTargetAttributeCSSProperty(SVGElement*, const QualifiedName&); |
| 54 | |
| 55 | bool isAdditive() const override; |
| 56 | bool isAccumulated() const; |
| 57 | AnimationMode animationMode() const { return m_animationMode; } |
| 58 | CalcMode calcMode() const { return m_calcMode; } |
| 59 | |
| 60 | AnimatedPropertyValueType fromPropertyValueType() const { return m_fromPropertyValueType; } |
| 61 | AnimatedPropertyValueType toPropertyValueType() const { return m_toPropertyValueType; } |
| 62 | |
| 63 | void animateAdditiveNumber(float percentage, unsigned repeatCount, float fromNumber, float toNumber, float toAtEndOfDurationNumber, float& animatedNumber) |
| 64 | { |
| 65 | float number; |
| 66 | if (calcMode() == CalcMode::Discrete) |
| 67 | number = percentage < 0.5 ? fromNumber : toNumber; |
| 68 | else |
| 69 | number = (toNumber - fromNumber) * percentage + fromNumber; |
| 70 | |
| 71 | if (isAccumulated() && repeatCount) |
| 72 | number += toAtEndOfDurationNumber * repeatCount; |
| 73 | |
| 74 | if (isAdditive() && animationMode() != AnimationMode::To) |
| 75 | animatedNumber += number; |
| 76 | else |
| 77 | animatedNumber = number; |
| 78 | } |
| 79 | |
| 80 | enum class AttributeType : uint8_t { CSS, XML, Auto }; |
| 81 | AttributeType attributeType() const { return m_attributeType; } |
| 82 | |
| 83 | void computeCSSPropertyValue(SVGElement*, CSSPropertyID, String& value); |
| 84 | virtual void determinePropertyValueTypes(const String& from, const String& to); |
| 85 | |
| 86 | protected: |
| 87 | SVGAnimationElement(const QualifiedName&, Document&); |
| 88 | |
| 89 | using PropertyRegistry = SVGPropertyOwnerRegistry<SVGAnimationElement, SVGElement, SVGExternalResourcesRequired, SVGTests>; |
| 90 | const SVGPropertyRegistry& propertyRegistry() const override { return m_propertyRegistry; } |
| 91 | |
| 92 | virtual void resetAnimation(); |
| 93 | |
| 94 | static bool isSupportedAttribute(const QualifiedName&); |
| 95 | void parseAttribute(const QualifiedName&, const AtomicString&) override; |
| 96 | void svgAttributeChanged(const QualifiedName&) override; |
| 97 | |
| 98 | String toValue() const; |
| 99 | String byValue() const; |
| 100 | String fromValue() const; |
| 101 | |
| 102 | String targetAttributeBaseValue(); |
| 103 | |
| 104 | // from SVGSMILElement |
| 105 | void startedActiveInterval() override; |
| 106 | void updateAnimation(float percent, unsigned repeat, SVGSMILElement* resultElement) override; |
| 107 | |
| 108 | AnimatedPropertyValueType m_fromPropertyValueType { RegularPropertyValue }; |
| 109 | AnimatedPropertyValueType m_toPropertyValueType { RegularPropertyValue }; |
| 110 | |
| 111 | void setAttributeName(const QualifiedName&) override { } |
| 112 | |
| 113 | virtual void updateAnimationMode(); |
| 114 | void setAnimationMode(AnimationMode animationMode) { m_animationMode = animationMode; } |
| 115 | void setCalcMode(CalcMode calcMode) { m_calcMode = calcMode; } |
| 116 | |
| 117 | private: |
| 118 | void animationAttributeChanged() override; |
| 119 | void setAttributeType(const AtomicString&); |
| 120 | |
| 121 | virtual bool calculateToAtEndOfDurationValue(const String& toAtEndOfDurationString) = 0; |
| 122 | virtual bool calculateFromAndToValues(const String& fromString, const String& toString) = 0; |
| 123 | virtual bool calculateFromAndByValues(const String& fromString, const String& byString) = 0; |
| 124 | virtual void calculateAnimatedValue(float percent, unsigned repeatCount, SVGSMILElement* resultElement) = 0; |
| 125 | virtual Optional<float> calculateDistance(const String& /*fromString*/, const String& /*toString*/) = 0; |
| 126 | |
| 127 | void currentValuesForValuesAnimation(float percent, float& effectivePercent, String& from, String& to); |
| 128 | void calculateKeyTimesForCalcModePaced(); |
| 129 | float calculatePercentFromKeyPoints(float percent) const; |
| 130 | void currentValuesFromKeyPoints(float percent, float& effectivePercent, String& from, String& to) const; |
| 131 | float calculatePercentForSpline(float percent, unsigned splineIndex) const; |
| 132 | float calculatePercentForFromTo(float percent) const; |
| 133 | unsigned calculateKeyTimesIndex(float percent) const; |
| 134 | |
| 135 | void setCalcMode(const AtomicString&); |
| 136 | |
| 137 | bool m_animationValid { false }; |
| 138 | |
| 139 | AttributeType m_attributeType { AttributeType::Auto }; |
| 140 | Vector<String> m_values; |
| 141 | Vector<float> m_keyTimes; |
| 142 | Vector<float> m_keyPoints; |
| 143 | Vector<UnitBezier> m_keySplines; |
| 144 | String m_lastValuesAnimationFrom; |
| 145 | String m_lastValuesAnimationTo; |
| 146 | CalcMode m_calcMode { CalcMode::Linear }; |
| 147 | AnimationMode m_animationMode { AnimationMode::None }; |
| 148 | PropertyRegistry m_propertyRegistry { *this }; |
| 149 | }; |
| 150 | |
| 151 | } // namespace WebCore |
| 152 | |