Chromium Code Reviews| Index: Source/core/svg/SVGLengthList.cpp |
| diff --git a/Source/core/svg/SVGLengthList.cpp b/Source/core/svg/SVGLengthList.cpp |
| index 1518d2c924a541cdfceba3370acb439b96b88560..bd5a278f5704e82e3d155091bd3e055aca5e61a0 100644 |
| --- a/Source/core/svg/SVGLengthList.cpp |
| +++ b/Source/core/svg/SVGLengthList.cpp |
| @@ -1,37 +1,91 @@ |
| /* |
| - * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org> |
| - * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org> |
| + * Copyright (C) 2013 Google Inc. 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. |
| + * Redistribution and use in source and binary forms, with or without |
| + * modification, are permitted provided that the following conditions are |
| + * met: |
| * |
| - * 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. |
| + * * Redistributions of source code must retain the above copyright |
| + * notice, this list of conditions and the following disclaimer. |
| + * * Redistributions in binary form must reproduce the above |
| + * copyright notice, this list of conditions and the following disclaimer |
| + * in the documentation and/or other materials provided with the |
| + * distribution. |
| + * * Neither the name of Google Inc. nor the names of its |
| + * contributors may be used to endorse or promote products derived from |
| + * this software without specific prior written permission. |
| * |
| - * 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. |
| + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| */ |
| #include "config.h" |
| #include "core/svg/SVGLengthList.h" |
| -#include "bindings/v8/ExceptionState.h" |
| +#include "bindings/v8/ExceptionStatePlaceholder.h" |
| +#include "core/svg/SVGAnimationElement.h" |
| #include "core/svg/SVGParserUtilities.h" |
| #include "wtf/text/StringBuilder.h" |
| namespace WebCore { |
| -template<typename CharType> |
| -void SVGLengthList::parseInternal(const CharType*& ptr, const CharType* end, SVGLengthMode mode) |
| +inline SVGLengthList* toSVGLengthList(NewSVGPropertyBase* base) |
| { |
| - TrackExceptionState exceptionState; |
| + // FIXME: add type checking |
| + return static_cast<SVGLengthList*>(base); |
| +} |
| + |
| +SVGLengthList::SVGLengthList(SVGLengthMode mode) |
| +: m_mode(mode) |
| +{ |
| +} |
| + |
| +SVGLengthList::~SVGLengthList() |
| +{ |
| +} |
| + |
| +PassRefPtr<SVGLengthList> SVGLengthList::clone() const |
| +{ |
| + RefPtr<SVGLengthList> ret = SVGLengthList::create(m_mode); |
| + ret->deepCopy(*this); |
| + return ret.release(); |
| +} |
| + |
| +PassRefPtr<NewSVGPropertyBase> SVGLengthList::cloneForAnimation(const String& value) const |
| +{ |
| + RefPtr<SVGLengthList> ret = SVGLengthList::create(m_mode); |
| + ret->setValueAsString(value, IGNORE_EXCEPTION); |
| + return ret.release(); |
| +} |
| + |
| +String SVGLengthList::valueAsString() const |
| +{ |
| + StringBuilder builder; |
| + |
| + 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.
|
| + for (unsigned i = 0; i < size; ++i) { |
| + if (i > 0) |
| + builder.append(' '); |
| + |
| + builder.append(m_values[i]->valueAsString()); |
| + } |
| + |
| + return builder.toString(); |
| +} |
| +template <typename CharType> |
| +void SVGLengthList::parseInternal(const CharType*& ptr, const CharType* end, ExceptionState& exceptionState) |
| +{ |
| + m_values.clear(); |
| while (ptr < end) { |
| const CharType* start = ptr; |
| while (ptr < end && *ptr != ',' && !isSVGSpace(*ptr)) |
| @@ -39,47 +93,111 @@ void SVGLengthList::parseInternal(const CharType*& ptr, const CharType* end, SVG |
| if (ptr == start) |
| break; |
| - SVGLength length(mode); |
| + RefPtr<SVGLength> length = SVGLength::create(m_mode); |
| String valueString(start, ptr - start); |
| if (valueString.isEmpty()) |
| return; |
| - length.setValueAsString(valueString, exceptionState); |
| + length->setValueAsString(valueString, exceptionState); |
| if (exceptionState.hadException()) |
| return; |
| - append(length); |
| + m_values.append(length); |
| skipOptionalSVGSpacesOrDelimiter(ptr, end); |
| } |
| } |
| -void SVGLengthList::parse(const String& value, SVGLengthMode mode) |
| +void SVGLengthList::setValueAsString(const String& value, ExceptionState& exceptionState) |
| { |
| - clear(); |
| if (value.isEmpty()) |
| return; |
| if (value.is8Bit()) { |
| const LChar* ptr = value.characters8(); |
| const LChar* end = ptr + value.length(); |
| - parseInternal(ptr, end, mode); |
| + parseInternal(ptr, end, exceptionState); |
| } else { |
| const UChar* ptr = value.characters16(); |
| const UChar* end = ptr + value.length(); |
| - parseInternal(ptr, end, mode); |
| + parseInternal(ptr, end, exceptionState); |
| } |
| } |
| -String SVGLengthList::valueAsString() const |
| +void SVGLengthList::add(PassRefPtr<NewSVGPropertyBase> other, SVGElement* contextElement) |
| { |
| - StringBuilder builder; |
| + SVGLengthList* otherList = toSVGLengthList(other.get()); |
| - unsigned size = this->size(); |
| - for (unsigned i = 0; i < size; ++i) { |
| - if (i > 0) |
| - builder.append(' '); |
| + if (m_values.size() != otherList->m_values.size()) |
| + return; |
| - builder.append(at(i).valueAsString()); |
| + SVGLengthContext lengthContext(contextElement); |
| + for (size_t i = 0; i < m_values.size(); ++i) |
| + m_values[i]->setValue(m_values[i]->value(lengthContext) + otherList->m_values[i]->value(lengthContext), lengthContext, ASSERT_NO_EXCEPTION); |
| +} |
| + |
| +bool SVGLengthList::adjustFromToListValues(const SVGLengthList* fromList, const SVGLengthList* toList, float percentage, bool isToAnimation, bool resizeAnimatedListIfNeeded) |
| +{ |
| + // If no 'to' value is given, nothing to animate. |
| + size_t toListSize = toList->m_values.size(); |
| + if (!toListSize) |
| + return false; |
| + |
| + // If the 'from' value is given and it's length doesn't match the 'to' value list length, fallback to a discrete animation. |
| + size_t fromListSize = fromList->m_values.size(); |
| + if (fromListSize != toListSize && fromListSize) { |
| + if (percentage < 0.5) { |
| + if (!isToAnimation) |
| + deepCopy(*fromList); |
| + } else { |
| + deepCopy(*toList); |
| + } |
| + |
| + return false; |
| } |
| - return builder.toString(); |
| + ASSERT(!fromListSize || fromListSize == toListSize); |
| + if (resizeAnimatedListIfNeeded && m_values.size() < toListSize) { |
| + size_t paddingCount = toListSize - m_values.size(); |
| + for (size_t i = 0; i < paddingCount; ++i) |
| + m_values.append(SVGLength::create(m_mode)); |
| + } |
| + |
| + return true; |
| } |
| +void SVGLengthList::calculateAnimatedValue(SVGAnimationElement* animationElement, float percentage, unsigned repeatCount, PassRefPtr<NewSVGPropertyBase> fromValue, PassRefPtr<NewSVGPropertyBase> toValue, PassRefPtr<NewSVGPropertyBase> toAtEndOfDurationValue, SVGElement* contextElement) |
| +{ |
| + SVGLengthContext lengthContext(contextElement); |
| + ASSERT(m_mode == SVGLength::lengthModeForAnimatedLengthAttribute(animationElement->attributeName())); |
| + const SVGLengthList* fromList = toSVGLengthList(fromValue.get()); |
| + const SVGLengthList* toList = toSVGLengthList(toValue.get()); |
| + const SVGLengthList* toAtEndOfDurationList = toSVGLengthList(toAtEndOfDurationValue.get()); |
| + |
| + size_t fromLengthListSize = fromList->m_values.size(); |
| + size_t toLengthListSize = toList->m_values.size(); |
| + size_t toAtEndOfDurationListSize = toAtEndOfDurationList->m_values.size(); |
| + |
| + if (!adjustFromToListValues(fromList, toList, percentage, animationElement->animationMode() == ToAnimation, true)) |
| + return; |
| + |
| + for (size_t i = 0; i < toLengthListSize; ++i) { |
| + float animatedNumber = m_values[i]->value(lengthContext); |
| + SVGLengthType unitType = toList->m_values[i]->unitType(); |
| + float effectiveFrom = 0; |
| + if (fromLengthListSize) { |
| + if (percentage < 0.5) |
| + unitType = fromList->m_values[i]->unitType(); |
| + effectiveFrom = fromList->m_values[i]->value(lengthContext); |
| + } |
| + float effectiveTo = toList->m_values[i]->value(lengthContext); |
| + float effectiveToAtEnd = i < toAtEndOfDurationListSize ? toAtEndOfDurationList->m_values[i]->value(lengthContext) : 0; |
| + |
| + animationElement->animateAdditiveNumber(percentage, repeatCount, effectiveFrom, effectiveTo, effectiveToAtEnd, animatedNumber); |
| + m_values[i]->setUnitType(unitType); |
| + m_values[i]->setValue(animatedNumber, lengthContext, ASSERT_NO_EXCEPTION); |
| + } |
| +} |
| + |
| +float SVGLengthList::calculateDistance(PassRefPtr<NewSVGPropertyBase> to, SVGElement*) |
| +{ |
| + // FIXME: Distance calculation is not possible for SVGLengthList right now. We need the distance for every single value. |
| + return -1; |
| +} |
| } |