Home | History | Annotate | Download | only in svg
      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/SVGNumberOptionalNumber.h"
     33 
     34 #include "core/svg/SVGAnimationElement.h"
     35 #include "core/svg/SVGParserUtilities.h"
     36 
     37 namespace WebCore {
     38 
     39 SVGNumberOptionalNumber::SVGNumberOptionalNumber(PassRefPtr<SVGNumber> firstNumber, PassRefPtr<SVGNumber> secondNumber)
     40     : SVGPropertyBase(classType())
     41     , m_firstNumber(firstNumber)
     42     , m_secondNumber(secondNumber)
     43 {
     44 }
     45 
     46 PassRefPtr<SVGNumberOptionalNumber> SVGNumberOptionalNumber::clone() const
     47 {
     48     return SVGNumberOptionalNumber::create(m_firstNumber->clone(), m_secondNumber->clone());
     49 }
     50 
     51 PassRefPtr<SVGPropertyBase> SVGNumberOptionalNumber::cloneForAnimation(const String& value) const
     52 {
     53     float x, y;
     54     if (!parseNumberOptionalNumber(value, x, y)) {
     55         x = y = 0;
     56     }
     57 
     58     return SVGNumberOptionalNumber::create(SVGNumber::create(x), SVGNumber::create(y));
     59 }
     60 
     61 String SVGNumberOptionalNumber::valueAsString() const
     62 {
     63     if (m_firstNumber->value() == m_secondNumber->value()) {
     64         return String::number(m_firstNumber->value());
     65     }
     66 
     67     return String::number(m_firstNumber->value()) + " " + String::number(m_secondNumber->value());
     68 }
     69 
     70 void SVGNumberOptionalNumber::setValueAsString(const String& value, ExceptionState& exceptionState)
     71 {
     72     float x, y;
     73     if (!parseNumberOptionalNumber(value, x, y)) {
     74         exceptionState.throwDOMException(SyntaxError, "The value provided ('" + value + "') is invalid.");
     75         x = y = 0;
     76     }
     77 
     78     m_firstNumber->setValue(x);
     79     m_secondNumber->setValue(y);
     80 }
     81 
     82 void SVGNumberOptionalNumber::add(PassRefPtrWillBeRawPtr<SVGPropertyBase> other, SVGElement*)
     83 {
     84     RefPtr<SVGNumberOptionalNumber> otherNumberOptionalNumber = toSVGNumberOptionalNumber(other);
     85 
     86     m_firstNumber->setValue(m_firstNumber->value() + otherNumberOptionalNumber->m_firstNumber->value());
     87     m_secondNumber->setValue(m_secondNumber->value() + otherNumberOptionalNumber->m_secondNumber->value());
     88 }
     89 
     90 void SVGNumberOptionalNumber::calculateAnimatedValue(SVGAnimationElement* animationElement, float percentage, unsigned repeatCount, PassRefPtr<SVGPropertyBase> from, PassRefPtr<SVGPropertyBase> to, PassRefPtr<SVGPropertyBase> toAtEndOfDuration, SVGElement*)
     91 {
     92     ASSERT(animationElement);
     93 
     94     RefPtr<SVGNumberOptionalNumber> fromNumber = toSVGNumberOptionalNumber(from);
     95     RefPtr<SVGNumberOptionalNumber> toNumber = toSVGNumberOptionalNumber(to);
     96     RefPtr<SVGNumberOptionalNumber> toAtEndOfDurationNumber = toSVGNumberOptionalNumber(toAtEndOfDuration);
     97 
     98     float x = m_firstNumber->value();
     99     float y = m_secondNumber->value();
    100     animationElement->animateAdditiveNumber(percentage, repeatCount, fromNumber->firstNumber()->value(), toNumber->firstNumber()->value(), toAtEndOfDurationNumber->firstNumber()->value(), x);
    101     animationElement->animateAdditiveNumber(percentage, repeatCount, fromNumber->secondNumber()->value(), toNumber->secondNumber()->value(), toAtEndOfDurationNumber->secondNumber()->value(), y);
    102     m_firstNumber->setValue(x);
    103     m_secondNumber->setValue(y);
    104 }
    105 
    106 float SVGNumberOptionalNumber::calculateDistance(PassRefPtr<SVGPropertyBase> other, SVGElement*)
    107 {
    108     // FIXME: Distance calculation is not possible for SVGNumberOptionalNumber right now. We need the distance for every single value.
    109     return -1;
    110 }
    111 
    112 }
    113