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/core/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 != ',' && !isHTMLSpace<CharType>(*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(PassRefPtrWillBeRawPtr<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 PassRefPtr<SVGLength> SVGLengthList::createPaddingItem() const
134 return SVGLength::create(m_mode);
137 void SVGLengthList::calculateAnimatedValue(SVGAnimationElement* animationElement, float percentage, unsigned repeatCount, PassRefPtr<SVGPropertyBase> fromValue, PassRefPtr<SVGPropertyBase> toValue, PassRefPtr<SVGPropertyBase> toAtEndOfDurationValue, SVGElement* contextElement)
139 RefPtr<SVGLengthList> fromList = toSVGLengthList(fromValue);
140 RefPtr<SVGLengthList> toList = toSVGLengthList(toValue);
141 RefPtr<SVGLengthList> toAtEndOfDurationList = toSVGLengthList(toAtEndOfDurationValue);
143 SVGLengthContext lengthContext(contextElement);
144 ASSERT(m_mode == SVGLength::lengthModeForAnimatedLengthAttribute(animationElement->attributeName()));
146 size_t fromLengthListSize = fromList->length();
147 size_t toLengthListSize = toList->length();
148 size_t toAtEndOfDurationListSize = toAtEndOfDurationList->length();
150 if (!adjustFromToListValues(fromList, toList, percentage, animationElement->animationMode()))
153 for (size_t i = 0; i < toLengthListSize; ++i) {
154 float animatedNumber = at(i)->value(lengthContext);
155 SVGLengthType unitType = toList->at(i)->unitType();
156 float effectiveFrom = 0;
157 if (fromLengthListSize) {
158 if (percentage < 0.5)
159 unitType = fromList->at(i)->unitType();
160 effectiveFrom = fromList->at(i)->value(lengthContext);
162 float effectiveTo = toList->at(i)->value(lengthContext);
163 float effectiveToAtEnd = i < toAtEndOfDurationListSize ? toAtEndOfDurationList->at(i)->value(lengthContext) : 0;
165 animationElement->animateAdditiveNumber(percentage, repeatCount, effectiveFrom, effectiveTo, effectiveToAtEnd, animatedNumber);
166 at(i)->setUnitType(unitType);
167 at(i)->setValue(animatedNumber, lengthContext, ASSERT_NO_EXCEPTION);
171 float SVGLengthList::calculateDistance(PassRefPtr<SVGPropertyBase> to, SVGElement*)
173 // FIXME: Distance calculation is not possible for SVGLengthList right now. We need the distance for every single value.