2 * Copyright (C) 2004, 2005, 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.
22 #include "core/svg/SVGLengthList.h"
24 #include "bindings/v8/ExceptionStatePlaceholder.h"
25 #include "core/svg/SVGAnimationElement.h"
26 #include "core/svg/SVGParserUtilities.h"
27 #include "wtf/text/StringBuilder.h"
31 inline PassRefPtr<SVGLengthList> toSVGLengthList(PassRefPtr<SVGPropertyBase> passBase)
33 RefPtr<SVGPropertyBase> base = passBase;
34 ASSERT(base->type() == SVGLengthList::classType());
35 return static_pointer_cast<SVGLengthList>(base.release());
38 SVGLengthList::SVGLengthList(SVGLengthMode mode)
43 SVGLengthList::~SVGLengthList()
47 PassRefPtr<SVGLengthList> SVGLengthList::clone()
49 RefPtr<SVGLengthList> ret = SVGLengthList::create(m_mode);
54 PassRefPtr<SVGPropertyBase> SVGLengthList::cloneForAnimation(const String& value) const
56 RefPtr<SVGLengthList> ret = SVGLengthList::create(m_mode);
57 ret->setValueAsString(value, IGNORE_EXCEPTION);
61 String SVGLengthList::valueAsString() const
63 StringBuilder builder;
65 ConstIterator it = begin();
66 ConstIterator itEnd = end();
68 builder.append(it->valueAsString());
71 for (; it != itEnd; ++it) {
73 builder.append(it->valueAsString());
77 return builder.toString();
80 template <typename CharType>
81 void SVGLengthList::parseInternal(const CharType*& ptr, const CharType* end, ExceptionState& exceptionState)
85 const CharType* start = ptr;
86 while (ptr < end && *ptr != ',' && !isSVGSpace(*ptr))
91 RefPtr<SVGLength> length = SVGLength::create(m_mode);
92 String valueString(start, ptr - start);
93 if (valueString.isEmpty())
95 length->setValueAsString(valueString, exceptionState);
96 if (exceptionState.hadException())
99 skipOptionalSVGSpacesOrDelimiter(ptr, end);
103 void SVGLengthList::setValueAsString(const String& value, ExceptionState& exceptionState)
105 if (value.isEmpty()) {
109 if (value.is8Bit()) {
110 const LChar* ptr = value.characters8();
111 const LChar* end = ptr + value.length();
112 parseInternal(ptr, end, exceptionState);
114 const UChar* ptr = value.characters16();
115 const UChar* end = ptr + value.length();
116 parseInternal(ptr, end, exceptionState);
120 void SVGLengthList::add(PassRefPtr<SVGPropertyBase> other, SVGElement* contextElement)
122 RefPtr<SVGLengthList> otherList = toSVGLengthList(other);
124 if (length() != otherList->length())
127 SVGLengthContext lengthContext(contextElement);
128 for (size_t i = 0; i < length(); ++i)
129 at(i)->setValue(at(i)->value(lengthContext) + otherList->at(i)->value(lengthContext), lengthContext, ASSERT_NO_EXCEPTION);
132 bool SVGLengthList::adjustFromToListValues(PassRefPtr<SVGLengthList> passFromList, PassRefPtr<SVGLengthList> passToList, float percentage, bool isToAnimation, bool resizeAnimatedListIfNeeded)
134 RefPtr<SVGLengthList> fromList = passFromList;
135 RefPtr<SVGLengthList> toList = passToList;
137 // If no 'to' value is given, nothing to animate.
138 size_t toListSize = toList->length();
142 // If the 'from' value is given and it's length doesn't match the 'to' value list length, fallback to a discrete animation.
143 size_t fromListSize = fromList->length();
144 if (fromListSize != toListSize && fromListSize) {
145 if (percentage < 0.5) {
155 ASSERT(!fromListSize || fromListSize == toListSize);
156 if (resizeAnimatedListIfNeeded && length() < toListSize) {
157 size_t paddingCount = toListSize - length();
158 for (size_t i = 0; i < paddingCount; ++i)
159 append(SVGLength::create(m_mode));
165 void SVGLengthList::calculateAnimatedValue(SVGAnimationElement* animationElement, float percentage, unsigned repeatCount, PassRefPtr<SVGPropertyBase> fromValue, PassRefPtr<SVGPropertyBase> toValue, PassRefPtr<SVGPropertyBase> toAtEndOfDurationValue, SVGElement* contextElement)
167 RefPtr<SVGLengthList> fromList = toSVGLengthList(fromValue);
168 RefPtr<SVGLengthList> toList = toSVGLengthList(toValue);
169 RefPtr<SVGLengthList> toAtEndOfDurationList = toSVGLengthList(toAtEndOfDurationValue);
171 SVGLengthContext lengthContext(contextElement);
172 ASSERT(m_mode == SVGLength::lengthModeForAnimatedLengthAttribute(animationElement->attributeName()));
174 size_t fromLengthListSize = fromList->length();
175 size_t toLengthListSize = toList->length();
176 size_t toAtEndOfDurationListSize = toAtEndOfDurationList->length();
178 if (!adjustFromToListValues(fromList, toList, percentage, animationElement->animationMode() == ToAnimation, true))
181 for (size_t i = 0; i < toLengthListSize; ++i) {
182 float animatedNumber = at(i)->value(lengthContext);
183 SVGLengthType unitType = toList->at(i)->unitType();
184 float effectiveFrom = 0;
185 if (fromLengthListSize) {
186 if (percentage < 0.5)
187 unitType = fromList->at(i)->unitType();
188 effectiveFrom = fromList->at(i)->value(lengthContext);
190 float effectiveTo = toList->at(i)->value(lengthContext);
191 float effectiveToAtEnd = i < toAtEndOfDurationListSize ? toAtEndOfDurationList->at(i)->value(lengthContext) : 0;
193 animationElement->animateAdditiveNumber(percentage, repeatCount, effectiveFrom, effectiveTo, effectiveToAtEnd, animatedNumber);
194 at(i)->setUnitType(unitType);
195 at(i)->setValue(animatedNumber, lengthContext, ASSERT_NO_EXCEPTION);
199 float SVGLengthList::calculateDistance(PassRefPtr<SVGPropertyBase> to, SVGElement*)
201 // FIXME: Distance calculation is not possible for SVGLengthList right now. We need the distance for every single value.