1/* 2 * Copyright (C) 2014 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 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. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 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. 29 */ 30 31#include "config.h" 32#include "core/svg/SVGNumber.h" 33 34#include "core/svg/SVGAnimationElement.h" 35#include "core/svg/SVGParserUtilities.h" 36 37namespace WebCore { 38 39SVGNumber::SVGNumber(float value) 40 : SVGPropertyBase(classType()) 41 , m_value(value) 42{ 43} 44 45PassRefPtr<SVGNumber> SVGNumber::clone() const 46{ 47 return create(m_value); 48} 49 50PassRefPtr<SVGPropertyBase> SVGNumber::cloneForAnimation(const String& value) const 51{ 52 RefPtr<SVGNumber> svgNumber = create(); 53 svgNumber->setValueAsString(value, IGNORE_EXCEPTION); 54 return svgNumber.release(); 55} 56 57String SVGNumber::valueAsString() const 58{ 59 return String::number(m_value); 60} 61 62template<typename CharType> 63bool SVGNumber::parse(const CharType*& ptr, const CharType* end) 64{ 65 if (!parseNumber(ptr, end, m_value, AllowLeadingAndTrailingWhitespace)) { 66 m_value = 0; 67 return false; 68 } 69 70 if (ptr != end) { 71 m_value = 0; 72 return false; 73 } 74 75 return true; 76} 77 78void SVGNumber::setValueAsString(const String& string, ExceptionState& exceptionState) 79{ 80 if (string.isEmpty()) { 81 m_value = 0; 82 return; 83 } 84 85 bool valid = false; 86 if (string.is8Bit()) { 87 const LChar* ptr = string.characters8(); 88 const LChar* end = ptr + string.length(); 89 valid = parse(ptr, end); 90 } else { 91 const UChar* ptr = string.characters16(); 92 const UChar* end = ptr + string.length(); 93 valid = parse(ptr, end); 94 } 95 96 if (!valid) { 97 exceptionState.throwDOMException(SyntaxError, "The value provided ('" + string + "') is invalid."); 98 m_value = 0; 99 } 100} 101 102void SVGNumber::add(PassRefPtrWillBeRawPtr<SVGPropertyBase> other, SVGElement*) 103{ 104 setValue(m_value + toSVGNumber(other)->value()); 105} 106 107void SVGNumber::calculateAnimatedValue(SVGAnimationElement* animationElement, float percentage, unsigned repeatCount, PassRefPtr<SVGPropertyBase> from, PassRefPtr<SVGPropertyBase> to, PassRefPtr<SVGPropertyBase> toAtEndOfDuration, SVGElement*) 108{ 109 ASSERT(animationElement); 110 111 RefPtr<SVGNumber> fromNumber = toSVGNumber(from); 112 RefPtr<SVGNumber> toNumber = toSVGNumber(to); 113 RefPtr<SVGNumber> toAtEndOfDurationNumber = toSVGNumber(toAtEndOfDuration); 114 115 animationElement->animateAdditiveNumber(percentage, repeatCount, fromNumber->value(), toNumber->value(), toAtEndOfDurationNumber->value(), m_value); 116} 117 118float SVGNumber::calculateDistance(PassRefPtr<SVGPropertyBase> other, SVGElement*) 119{ 120 return fabsf(m_value - toSVGNumber(other)->value()); 121} 122 123PassRefPtr<SVGNumber> SVGNumberAcceptPercentage::clone() const 124{ 125 return create(m_value); 126} 127 128void SVGNumberAcceptPercentage::setValueAsString(const String& string, ExceptionState& exceptionState) 129{ 130 bool valid = parseNumberOrPercentage(string, m_value); 131 132 if (!valid) { 133 exceptionState.throwDOMException(SyntaxError, "The value provided ('" + string + "') is invalid."); 134 m_value = 0; 135 } 136} 137 138SVGNumberAcceptPercentage::SVGNumberAcceptPercentage(float value) 139 : SVGNumber(value) 140{ 141} 142 143} 144