OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org> | 2 * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org> |
3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org> | 3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org> |
4 * | 4 * |
5 * This library is free software; you can redistribute it and/or | 5 * This library is free software; you can redistribute it and/or |
6 * modify it under the terms of the GNU Library General Public | 6 * modify it under the terms of the GNU Library General Public |
7 * License as published by the Free Software Foundation; either | 7 * License as published by the Free Software Foundation; either |
8 * version 2 of the License, or (at your option) any later version. | 8 * version 2 of the License, or (at your option) any later version. |
9 * | 9 * |
10 * This library is distributed in the hope that it will be useful, | 10 * This library is distributed in the hope that it will be useful, |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 * Library General Public License for more details. | 13 * Library General Public License for more details. |
14 * | 14 * |
15 * You should have received a copy of the GNU Library General Public License | 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 | 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, | 17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
18 * Boston, MA 02110-1301, USA. | 18 * Boston, MA 02110-1301, USA. |
19 */ | 19 */ |
20 | 20 |
21 #include "config.h" | 21 #include "config.h" |
22 #include "core/svg/SVGLengthList.h" | 22 #include "core/svg/SVGLengthList.h" |
23 | 23 |
24 #include "bindings/v8/ExceptionState.h" | 24 #include "bindings/v8/ExceptionStatePlaceholder.h" |
| 25 #include "core/svg/SVGAnimationElement.h" |
25 #include "core/svg/SVGParserUtilities.h" | 26 #include "core/svg/SVGParserUtilities.h" |
26 #include "wtf/text/StringBuilder.h" | 27 #include "wtf/text/StringBuilder.h" |
27 | 28 |
28 namespace WebCore { | 29 namespace WebCore { |
29 | 30 |
30 template<typename CharType> | 31 inline PassRefPtr<SVGLengthList> toSVGLengthList(PassRefPtr<NewSVGPropertyBase>
passBase) |
31 void SVGLengthList::parseInternal(const CharType*& ptr, const CharType* end, SVG
LengthMode mode) | |
32 { | 32 { |
33 TrackExceptionState exceptionState; | 33 RefPtr<NewSVGPropertyBase> base = passBase; |
| 34 ASSERT(base->type() == SVGLengthList::classType()); |
| 35 return static_pointer_cast<SVGLengthList>(base.release()); |
| 36 } |
34 | 37 |
| 38 SVGLengthList::SVGLengthList(SVGLengthMode mode) |
| 39 : m_mode(mode) |
| 40 { |
| 41 } |
| 42 |
| 43 SVGLengthList::~SVGLengthList() |
| 44 { |
| 45 } |
| 46 |
| 47 PassRefPtr<SVGLengthList> SVGLengthList::clone() |
| 48 { |
| 49 RefPtr<SVGLengthList> ret = SVGLengthList::create(m_mode); |
| 50 ret->deepCopy(this); |
| 51 return ret.release(); |
| 52 } |
| 53 |
| 54 PassRefPtr<NewSVGPropertyBase> SVGLengthList::cloneForAnimation(const String& va
lue) const |
| 55 { |
| 56 RefPtr<SVGLengthList> ret = SVGLengthList::create(m_mode); |
| 57 ret->setValueAsString(value, IGNORE_EXCEPTION); |
| 58 return ret.release(); |
| 59 } |
| 60 |
| 61 String SVGLengthList::valueAsString() const |
| 62 { |
| 63 StringBuilder builder; |
| 64 |
| 65 Vector<RefPtr<SVGLength> >::const_iterator it = m_values.begin(); |
| 66 Vector<RefPtr<SVGLength> >::const_iterator itEnd = m_values.end(); |
| 67 if (it != itEnd) { |
| 68 builder.append((*it++)->valueAsString()); |
| 69 |
| 70 for (; it != itEnd; ++it) { |
| 71 builder.append(' '); |
| 72 builder.append((*it)->valueAsString()); |
| 73 } |
| 74 } |
| 75 |
| 76 return builder.toString(); |
| 77 } |
| 78 |
| 79 template <typename CharType> |
| 80 void SVGLengthList::parseInternal(const CharType*& ptr, const CharType* end, Exc
eptionState& exceptionState) |
| 81 { |
| 82 m_values.clear(); |
35 while (ptr < end) { | 83 while (ptr < end) { |
36 const CharType* start = ptr; | 84 const CharType* start = ptr; |
37 while (ptr < end && *ptr != ',' && !isSVGSpace(*ptr)) | 85 while (ptr < end && *ptr != ',' && !isSVGSpace(*ptr)) |
38 ptr++; | 86 ptr++; |
39 if (ptr == start) | 87 if (ptr == start) |
40 break; | 88 break; |
41 | 89 |
42 SVGLength length(mode); | 90 RefPtr<SVGLength> length = SVGLength::create(m_mode); |
43 String valueString(start, ptr - start); | 91 String valueString(start, ptr - start); |
44 if (valueString.isEmpty()) | 92 if (valueString.isEmpty()) |
45 return; | 93 return; |
46 length.setValueAsString(valueString, exceptionState); | 94 length->setValueAsString(valueString, exceptionState); |
47 if (exceptionState.hadException()) | 95 if (exceptionState.hadException()) |
48 return; | 96 return; |
49 append(length); | 97 m_values.append(length); |
50 skipOptionalSVGSpacesOrDelimiter(ptr, end); | 98 skipOptionalSVGSpacesOrDelimiter(ptr, end); |
51 } | 99 } |
52 } | 100 } |
53 | 101 |
54 void SVGLengthList::parse(const String& value, SVGLengthMode mode) | 102 void SVGLengthList::setValueAsString(const String& value, ExceptionState& except
ionState) |
55 { | 103 { |
56 clear(); | |
57 if (value.isEmpty()) | 104 if (value.isEmpty()) |
58 return; | 105 return; |
59 if (value.is8Bit()) { | 106 if (value.is8Bit()) { |
60 const LChar* ptr = value.characters8(); | 107 const LChar* ptr = value.characters8(); |
61 const LChar* end = ptr + value.length(); | 108 const LChar* end = ptr + value.length(); |
62 parseInternal(ptr, end, mode); | 109 parseInternal(ptr, end, exceptionState); |
63 } else { | 110 } else { |
64 const UChar* ptr = value.characters16(); | 111 const UChar* ptr = value.characters16(); |
65 const UChar* end = ptr + value.length(); | 112 const UChar* end = ptr + value.length(); |
66 parseInternal(ptr, end, mode); | 113 parseInternal(ptr, end, exceptionState); |
67 } | 114 } |
68 } | 115 } |
69 | 116 |
70 String SVGLengthList::valueAsString() const | 117 void SVGLengthList::add(PassRefPtr<NewSVGPropertyBase> other, SVGElement* contex
tElement) |
71 { | 118 { |
72 StringBuilder builder; | 119 RefPtr<SVGLengthList> otherList = toSVGLengthList(other); |
73 | 120 |
74 unsigned size = this->size(); | 121 if (m_values.size() != otherList->m_values.size()) |
75 for (unsigned i = 0; i < size; ++i) { | 122 return; |
76 if (i > 0) | |
77 builder.append(' '); | |
78 | 123 |
79 builder.append(at(i).valueAsString()); | 124 SVGLengthContext lengthContext(contextElement); |
| 125 for (size_t i = 0; i < m_values.size(); ++i) |
| 126 m_values[i]->setValue(m_values[i]->value(lengthContext) + otherList->m_v
alues[i]->value(lengthContext), lengthContext, ASSERT_NO_EXCEPTION); |
| 127 } |
| 128 |
| 129 bool SVGLengthList::adjustFromToListValues(PassRefPtr<SVGLengthList> passFromLis
t, PassRefPtr<SVGLengthList> passToList, float percentage, bool isToAnimation, b
ool resizeAnimatedListIfNeeded) |
| 130 { |
| 131 RefPtr<SVGLengthList> fromList = passFromList; |
| 132 RefPtr<SVGLengthList> toList = passToList; |
| 133 |
| 134 // If no 'to' value is given, nothing to animate. |
| 135 size_t toListSize = toList->m_values.size(); |
| 136 if (!toListSize) |
| 137 return false; |
| 138 |
| 139 // If the 'from' value is given and it's length doesn't match the 'to' value
list length, fallback to a discrete animation. |
| 140 size_t fromListSize = fromList->m_values.size(); |
| 141 if (fromListSize != toListSize && fromListSize) { |
| 142 if (percentage < 0.5) { |
| 143 if (!isToAnimation) |
| 144 deepCopy(fromList); |
| 145 } else { |
| 146 deepCopy(toList); |
| 147 } |
| 148 |
| 149 return false; |
80 } | 150 } |
81 | 151 |
82 return builder.toString(); | 152 ASSERT(!fromListSize || fromListSize == toListSize); |
| 153 if (resizeAnimatedListIfNeeded && m_values.size() < toListSize) { |
| 154 size_t paddingCount = toListSize - m_values.size(); |
| 155 for (size_t i = 0; i < paddingCount; ++i) |
| 156 m_values.append(SVGLength::create(m_mode)); |
| 157 } |
| 158 |
| 159 return true; |
83 } | 160 } |
84 | 161 |
| 162 void SVGLengthList::calculateAnimatedValue(SVGAnimationElement* animationElement
, float percentage, unsigned repeatCount, PassRefPtr<NewSVGPropertyBase> fromVal
ue, PassRefPtr<NewSVGPropertyBase> toValue, PassRefPtr<NewSVGPropertyBase> toAtE
ndOfDurationValue, SVGElement* contextElement) |
| 163 { |
| 164 RefPtr<SVGLengthList> fromList = toSVGLengthList(fromValue); |
| 165 RefPtr<SVGLengthList> toList = toSVGLengthList(toValue); |
| 166 RefPtr<SVGLengthList> toAtEndOfDurationList = toSVGLengthList(toAtEndOfDurat
ionValue); |
| 167 |
| 168 SVGLengthContext lengthContext(contextElement); |
| 169 ASSERT(m_mode == SVGLength::lengthModeForAnimatedLengthAttribute(animationEl
ement->attributeName())); |
| 170 |
| 171 size_t fromLengthListSize = fromList->m_values.size(); |
| 172 size_t toLengthListSize = toList->m_values.size(); |
| 173 size_t toAtEndOfDurationListSize = toAtEndOfDurationList->m_values.size(); |
| 174 |
| 175 if (!adjustFromToListValues(fromList, toList, percentage, animationElement->
animationMode() == ToAnimation, true)) |
| 176 return; |
| 177 |
| 178 for (size_t i = 0; i < toLengthListSize; ++i) { |
| 179 float animatedNumber = m_values[i]->value(lengthContext); |
| 180 SVGLengthType unitType = toList->m_values[i]->unitType(); |
| 181 float effectiveFrom = 0; |
| 182 if (fromLengthListSize) { |
| 183 if (percentage < 0.5) |
| 184 unitType = fromList->m_values[i]->unitType(); |
| 185 effectiveFrom = fromList->m_values[i]->value(lengthContext); |
| 186 } |
| 187 float effectiveTo = toList->m_values[i]->value(lengthContext); |
| 188 float effectiveToAtEnd = i < toAtEndOfDurationListSize ? toAtEndOfDurati
onList->m_values[i]->value(lengthContext) : 0; |
| 189 |
| 190 animationElement->animateAdditiveNumber(percentage, repeatCount, effecti
veFrom, effectiveTo, effectiveToAtEnd, animatedNumber); |
| 191 m_values[i]->setUnitType(unitType); |
| 192 m_values[i]->setValue(animatedNumber, lengthContext, ASSERT_NO_EXCEPTION
); |
| 193 } |
85 } | 194 } |
| 195 |
| 196 float SVGLengthList::calculateDistance(PassRefPtr<NewSVGPropertyBase> to, SVGEle
ment*) |
| 197 { |
| 198 // FIXME: Distance calculation is not possible for SVGLengthList right now.
We need the distance for every single value. |
| 199 return -1; |
| 200 } |
| 201 } |
OLD | NEW |