OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org> | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org> | |
4 * | 3 * |
5 * This library is free software; you can redistribute it and/or | 4 * Redistribution and use in source and binary forms, with or without |
6 * modify it under the terms of the GNU Library General Public | 5 * modification, are permitted provided that the following conditions are |
7 * License as published by the Free Software Foundation; either | 6 * met: |
8 * version 2 of the License, or (at your option) any later version. | |
9 * | 7 * |
10 * This library is distributed in the hope that it will be useful, | 8 * * Redistributions of source code must retain the above copyright |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 * notice, this list of conditions and the following disclaimer. |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 10 * * Redistributions in binary form must reproduce the above |
13 * Library General Public License for more details. | 11 * copyright notice, this list of conditions and the following disclaimer |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
14 * | 17 * |
15 * You should have received a copy of the GNU Library General Public License | 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
16 * along with this library; see the file COPYING.LIB. If not, write to | 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
18 * Boston, MA 02110-1301, USA. | 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
19 */ | 29 */ |
20 | 30 |
21 #include "config.h" | 31 #include "config.h" |
22 #include "core/svg/SVGLengthList.h" | 32 #include "core/svg/SVGLengthList.h" |
23 | 33 |
24 #include "bindings/v8/ExceptionState.h" | 34 #include "bindings/v8/ExceptionStatePlaceholder.h" |
35 #include "core/svg/SVGAnimationElement.h" | |
25 #include "core/svg/SVGParserUtilities.h" | 36 #include "core/svg/SVGParserUtilities.h" |
26 #include "wtf/text/StringBuilder.h" | 37 #include "wtf/text/StringBuilder.h" |
27 | 38 |
28 namespace WebCore { | 39 namespace WebCore { |
29 | 40 |
30 template<typename CharType> | 41 inline SVGLengthList* toSVGLengthList(NewSVGPropertyBase* base) |
31 void SVGLengthList::parseInternal(const CharType*& ptr, const CharType* end, SVG LengthMode mode) | |
32 { | 42 { |
33 TrackExceptionState exceptionState; | 43 // FIXME: add type checking |
44 return static_cast<SVGLengthList*>(base); | |
45 } | |
34 | 46 |
47 SVGLengthList::SVGLengthList(SVGLengthMode mode) | |
48 : m_mode(mode) | |
49 { | |
50 } | |
51 | |
52 SVGLengthList::~SVGLengthList() | |
53 { | |
54 } | |
55 | |
56 PassRefPtr<SVGLengthList> SVGLengthList::clone() const | |
57 { | |
58 RefPtr<SVGLengthList> ret = SVGLengthList::create(m_mode); | |
59 ret->deepCopy(*this); | |
60 return ret.release(); | |
61 } | |
62 | |
63 PassRefPtr<NewSVGPropertyBase> SVGLengthList::cloneForAnimation(const String& va lue) const | |
64 { | |
65 RefPtr<SVGLengthList> ret = SVGLengthList::create(m_mode); | |
66 ret->setValueAsString(value, IGNORE_EXCEPTION); | |
67 return ret.release(); | |
68 } | |
69 | |
70 String SVGLengthList::valueAsString() const | |
71 { | |
72 StringBuilder builder; | |
73 | |
74 unsigned size = m_values.size(); | |
pdr.
2014/01/07 06:42:53
Using an iterator here will be faster due to bound
kouhei (in TOK)
2014/01/08 08:12:07
Done.
| |
75 for (unsigned i = 0; i < size; ++i) { | |
76 if (i > 0) | |
77 builder.append(' '); | |
78 | |
79 builder.append(m_values[i]->valueAsString()); | |
80 } | |
81 | |
82 return builder.toString(); | |
83 } | |
84 | |
85 template <typename CharType> | |
86 void SVGLengthList::parseInternal(const CharType*& ptr, const CharType* end, Exc eptionState& exceptionState) | |
87 { | |
88 m_values.clear(); | |
35 while (ptr < end) { | 89 while (ptr < end) { |
36 const CharType* start = ptr; | 90 const CharType* start = ptr; |
37 while (ptr < end && *ptr != ',' && !isSVGSpace(*ptr)) | 91 while (ptr < end && *ptr != ',' && !isSVGSpace(*ptr)) |
38 ptr++; | 92 ptr++; |
39 if (ptr == start) | 93 if (ptr == start) |
40 break; | 94 break; |
41 | 95 |
42 SVGLength length(mode); | 96 RefPtr<SVGLength> length = SVGLength::create(m_mode); |
43 String valueString(start, ptr - start); | 97 String valueString(start, ptr - start); |
44 if (valueString.isEmpty()) | 98 if (valueString.isEmpty()) |
45 return; | 99 return; |
46 length.setValueAsString(valueString, exceptionState); | 100 length->setValueAsString(valueString, exceptionState); |
47 if (exceptionState.hadException()) | 101 if (exceptionState.hadException()) |
48 return; | 102 return; |
49 append(length); | 103 m_values.append(length); |
50 skipOptionalSVGSpacesOrDelimiter(ptr, end); | 104 skipOptionalSVGSpacesOrDelimiter(ptr, end); |
51 } | 105 } |
52 } | 106 } |
53 | 107 |
54 void SVGLengthList::parse(const String& value, SVGLengthMode mode) | 108 void SVGLengthList::setValueAsString(const String& value, ExceptionState& except ionState) |
55 { | 109 { |
56 clear(); | |
57 if (value.isEmpty()) | 110 if (value.isEmpty()) |
58 return; | 111 return; |
59 if (value.is8Bit()) { | 112 if (value.is8Bit()) { |
60 const LChar* ptr = value.characters8(); | 113 const LChar* ptr = value.characters8(); |
61 const LChar* end = ptr + value.length(); | 114 const LChar* end = ptr + value.length(); |
62 parseInternal(ptr, end, mode); | 115 parseInternal(ptr, end, exceptionState); |
63 } else { | 116 } else { |
64 const UChar* ptr = value.characters16(); | 117 const UChar* ptr = value.characters16(); |
65 const UChar* end = ptr + value.length(); | 118 const UChar* end = ptr + value.length(); |
66 parseInternal(ptr, end, mode); | 119 parseInternal(ptr, end, exceptionState); |
67 } | 120 } |
68 } | 121 } |
69 | 122 |
70 String SVGLengthList::valueAsString() const | 123 void SVGLengthList::add(PassRefPtr<NewSVGPropertyBase> other, SVGElement* contex tElement) |
71 { | 124 { |
72 StringBuilder builder; | 125 SVGLengthList* otherList = toSVGLengthList(other.get()); |
73 | 126 |
74 unsigned size = this->size(); | 127 if (m_values.size() != otherList->m_values.size()) |
75 for (unsigned i = 0; i < size; ++i) { | 128 return; |
76 if (i > 0) | |
77 builder.append(' '); | |
78 | 129 |
79 builder.append(at(i).valueAsString()); | 130 SVGLengthContext lengthContext(contextElement); |
131 for (size_t i = 0; i < m_values.size(); ++i) | |
132 m_values[i]->setValue(m_values[i]->value(lengthContext) + otherList->m_v alues[i]->value(lengthContext), lengthContext, ASSERT_NO_EXCEPTION); | |
133 } | |
134 | |
135 bool SVGLengthList::adjustFromToListValues(const SVGLengthList* fromList, const SVGLengthList* toList, float percentage, bool isToAnimation, bool resizeAnimated ListIfNeeded) | |
136 { | |
137 // If no 'to' value is given, nothing to animate. | |
138 size_t toListSize = toList->m_values.size(); | |
139 if (!toListSize) | |
140 return false; | |
141 | |
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->m_values.size(); | |
144 if (fromListSize != toListSize && fromListSize) { | |
145 if (percentage < 0.5) { | |
146 if (!isToAnimation) | |
147 deepCopy(*fromList); | |
148 } else { | |
149 deepCopy(*toList); | |
150 } | |
151 | |
152 return false; | |
80 } | 153 } |
81 | 154 |
82 return builder.toString(); | 155 ASSERT(!fromListSize || fromListSize == toListSize); |
156 if (resizeAnimatedListIfNeeded && m_values.size() < toListSize) { | |
157 size_t paddingCount = toListSize - m_values.size(); | |
158 for (size_t i = 0; i < paddingCount; ++i) | |
159 m_values.append(SVGLength::create(m_mode)); | |
160 } | |
161 | |
162 return true; | |
83 } | 163 } |
84 | 164 |
165 void SVGLengthList::calculateAnimatedValue(SVGAnimationElement* animationElement , float percentage, unsigned repeatCount, PassRefPtr<NewSVGPropertyBase> fromVal ue, PassRefPtr<NewSVGPropertyBase> toValue, PassRefPtr<NewSVGPropertyBase> toAtE ndOfDurationValue, SVGElement* contextElement) | |
166 { | |
167 SVGLengthContext lengthContext(contextElement); | |
168 ASSERT(m_mode == SVGLength::lengthModeForAnimatedLengthAttribute(animationEl ement->attributeName())); | |
169 const SVGLengthList* fromList = toSVGLengthList(fromValue.get()); | |
170 const SVGLengthList* toList = toSVGLengthList(toValue.get()); | |
171 const SVGLengthList* toAtEndOfDurationList = toSVGLengthList(toAtEndOfDurati onValue.get()); | |
172 | |
173 size_t fromLengthListSize = fromList->m_values.size(); | |
174 size_t toLengthListSize = toList->m_values.size(); | |
175 size_t toAtEndOfDurationListSize = toAtEndOfDurationList->m_values.size(); | |
176 | |
177 if (!adjustFromToListValues(fromList, toList, percentage, animationElement-> animationMode() == ToAnimation, true)) | |
178 return; | |
179 | |
180 for (size_t i = 0; i < toLengthListSize; ++i) { | |
181 float animatedNumber = m_values[i]->value(lengthContext); | |
182 SVGLengthType unitType = toList->m_values[i]->unitType(); | |
183 float effectiveFrom = 0; | |
184 if (fromLengthListSize) { | |
185 if (percentage < 0.5) | |
186 unitType = fromList->m_values[i]->unitType(); | |
187 effectiveFrom = fromList->m_values[i]->value(lengthContext); | |
188 } | |
189 float effectiveTo = toList->m_values[i]->value(lengthContext); | |
190 float effectiveToAtEnd = i < toAtEndOfDurationListSize ? toAtEndOfDurati onList->m_values[i]->value(lengthContext) : 0; | |
191 | |
192 animationElement->animateAdditiveNumber(percentage, repeatCount, effecti veFrom, effectiveTo, effectiveToAtEnd, animatedNumber); | |
193 m_values[i]->setUnitType(unitType); | |
194 m_values[i]->setValue(animatedNumber, lengthContext, ASSERT_NO_EXCEPTION ); | |
195 } | |
85 } | 196 } |
197 | |
198 float SVGLengthList::calculateDistance(PassRefPtr<NewSVGPropertyBase> to, SVGEle ment*) | |
199 { | |
200 // FIXME: Distance calculation is not possible for SVGLengthList right now. We need the distance for every single value. | |
201 return -1; | |
202 } | |
203 } | |
OLD | NEW |