1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
/*
* Copyright (C) 2004, 2005 Nikolas Zimmermann <zimmermann@kde.org>
* Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
* Copyright (C) 2008-2021 Apple Inc. All rights reserved.
* Copyright (C) Research In Motion Limited 2011. All rights reserved.
* Copyright (C) 2014 Adobe Systems Incorporated. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "SVGAnimateElementBase.h"
#include "QualifiedName.h"
#include "SVGAttributeAnimator.h"
#include "SVGElement.h"
#include "SVGNames.h"
#include <wtf/TZoneMallocInlines.h>
namespace WebCore {
WTF_MAKE_TZONE_OR_ISO_ALLOCATED_IMPL(SVGAnimateElementBase);
SVGAnimateElementBase::SVGAnimateElementBase(const QualifiedName& tagName, Document& document)
: SVGAnimationElement(tagName, document)
{
ASSERT(hasTagName(SVGNames::animateTag)
|| hasTagName(SVGNames::setTag)
|| hasTagName(SVGNames::animateTransformTag));
}
SVGAttributeAnimator* SVGAnimateElementBase::animator() const
{
ASSERT(targetElement());
ASSERT(!hasInvalidCSSAttributeType());
if (!m_animator)
m_animator = protectedTargetElement()->createAnimator(attributeName(), animationMode(), calcMode(), isAccumulated(), isAdditive());
return m_animator.get();
}
bool SVGAnimateElementBase::hasValidAttributeType() const
{
if (!targetElement() || hasInvalidCSSAttributeType())
return false;
return protectedTargetElement()->isAnimatedAttribute(attributeName());
}
bool SVGAnimateElementBase::hasInvalidCSSAttributeType() const
{
if (!targetElement())
return false;
if (!m_hasInvalidCSSAttributeType)
m_hasInvalidCSSAttributeType = hasValidAttributeName() && attributeType() == AttributeType::CSS && !isTargetAttributeCSSProperty(protectedTargetElement().get(), attributeName());
return m_hasInvalidCSSAttributeType.value();
}
bool SVGAnimateElementBase::isDiscreteAnimator() const
{
if (!hasValidAttributeType())
return false;
RefPtr animator = this->animator();
return animator && animator->isDiscrete();
}
void SVGAnimateElementBase::setTargetElement(SVGElement* targetElement)
{
SVGAnimationElement::setTargetElement(targetElement);
resetAnimation();
}
void SVGAnimateElementBase::setAttributeName(const QualifiedName& attributeName)
{
SVGSMILElement::setAttributeName(attributeName);
resetAnimation();
}
void SVGAnimateElementBase::resetAnimation()
{
SVGAnimationElement::resetAnimation();
m_animator = nullptr;
m_hasInvalidCSSAttributeType = { };
}
bool SVGAnimateElementBase::setFromAndToValues(const String& fromString, const String& toString)
{
if (!targetElement())
return false;
if (RefPtr animator = this->animator()) {
animator->setFromAndToValues(*protectedTargetElement(), animateRangeString(fromString), animateRangeString(toString));
return true;
}
return false;
}
bool SVGAnimateElementBase::setFromAndByValues(const String& fromString, const String& byString)
{
if (!targetElement())
return false;
if (animationMode() == AnimationMode::By && (!isAdditive() || isDiscreteAnimator()))
return false;
if (animationMode() == AnimationMode::FromBy && isDiscreteAnimator())
return false;
if (RefPtr animator = this->animator()) {
animator->setFromAndByValues(*protectedTargetElement(), animateRangeString(fromString), animateRangeString(byString));
return true;
}
return false;
}
bool SVGAnimateElementBase::setToAtEndOfDurationValue(const String& toAtEndOfDurationString)
{
if (!targetElement() || toAtEndOfDurationString.isEmpty())
return false;
if (isDiscreteAnimator())
return true;
if (RefPtr animator = this->animator()) {
animator->setToAtEndOfDurationValue(animateRangeString(toAtEndOfDurationString));
return true;
}
return false;
}
void SVGAnimateElementBase::startAnimation()
{
if (!targetElement())
return;
if (RefPtr protectedAnimator = this->animator())
protectedAnimator->start(*protectedTargetElement());
}
void SVGAnimateElementBase::calculateAnimatedValue(float progress, unsigned repeatCount)
{
if (!targetElement())
return;
ASSERT(progress >= 0 && progress <= 1);
if (hasTagName(SVGNames::setTag))
progress = 1;
if (calcMode() == CalcMode::Discrete)
progress = progress < 0.5 ? 0 : 1;
if (RefPtr animator = this->animator())
animator->animate(*protectedTargetElement(), progress, repeatCount);
}
void SVGAnimateElementBase::applyResultsToTarget()
{
if (!targetElement())
return;
if (RefPtr animator = this->animator())
animator->apply(*protectedTargetElement());
}
void SVGAnimateElementBase::stopAnimation(SVGElement* targetElement)
{
if (!targetElement)
return;
if (RefPtr animator = this->animatorIfExists())
animator->stop(*targetElement);
}
std::optional<float> SVGAnimateElementBase::calculateDistance(const String& fromString, const String& toString)
{
// FIXME: A return value of float is not enough to support paced animations on lists.
if (!targetElement())
return { };
if (RefPtr animator = this->animator())
return animator->calculateDistance(*protectedTargetElement(), fromString, toString);
return { };
}
} // namespace WebCore
|