109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)/*
209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) * Copyright (C) 2014 Google Inc. All rights reserved.
309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) *
409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) * modification, are permitted provided that the following conditions are
609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) * met:
709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) *
809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) *     * Redistributions of source code must retain the above copyright
909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) * notice, this list of conditions and the following disclaimer.
1009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) *     * Redistributions in binary form must reproduce the above
1109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
1209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) * in the documentation and/or other materials provided with the
1309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) * distribution.
1409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
1509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) * contributors may be used to endorse or promote products derived from
1609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) * this software without specific prior written permission.
1709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) *
1809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) */
3009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
3109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "config.h"
3209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "core/svg/SVGIntegerOptionalInteger.h"
3309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
3409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "core/svg/SVGAnimationElement.h"
3509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "core/svg/SVGParserUtilities.h"
3609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
37c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
3809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
3909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)SVGIntegerOptionalInteger::SVGIntegerOptionalInteger(PassRefPtr<SVGInteger> firstInteger, PassRefPtr<SVGInteger> secondInteger)
4007a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    : SVGPropertyBase(classType())
4109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    , m_firstInteger(firstInteger)
4209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    , m_secondInteger(secondInteger)
4309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
4409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
4509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
4609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)PassRefPtr<SVGIntegerOptionalInteger> SVGIntegerOptionalInteger::clone() const
4709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
4809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    return SVGIntegerOptionalInteger::create(m_firstInteger->clone(), m_secondInteger->clone());
4909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
5009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
5107a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben MurdochPassRefPtr<SVGPropertyBase> SVGIntegerOptionalInteger::cloneForAnimation(const String& value) const
5209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
5309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    float floatX, floatY;
5409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (!parseNumberOptionalNumber(value, floatX, floatY)) {
5509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        return SVGIntegerOptionalInteger::create(SVGInteger::create(0), SVGInteger::create(0));
5609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    }
5709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
5809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    int x = static_cast<int>(roundf(floatX));
5909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    int y = static_cast<int>(roundf(floatY));
6009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
6109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    return SVGIntegerOptionalInteger::create(SVGInteger::create(x), SVGInteger::create(y));
6209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
6309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
6409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)String SVGIntegerOptionalInteger::valueAsString() const
6509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
6609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (m_firstInteger->value() == m_secondInteger->value()) {
6709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        return String::number(m_firstInteger->value());
6809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    }
6909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
7009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    return String::number(m_firstInteger->value()) + " " + String::number(m_secondInteger->value());
7109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
7209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
7309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)void SVGIntegerOptionalInteger::setValueAsString(const String& value, ExceptionState& exceptionState)
7409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
7509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    float x, y;
7609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (!parseNumberOptionalNumber(value, x, y)) {
7709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        exceptionState.throwDOMException(SyntaxError, "The value provided ('" + value + "') is invalid.");
7809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        x = y = 0;
7909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    }
8009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
8109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    m_firstInteger->setValue(x);
8209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    m_secondInteger->setValue(y);
8309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
8409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
85d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)void SVGIntegerOptionalInteger::add(PassRefPtrWillBeRawPtr<SVGPropertyBase> other, SVGElement*)
8609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
8709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    RefPtr<SVGIntegerOptionalInteger> otherIntegerOptionalInteger = toSVGIntegerOptionalInteger(other);
8809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
8909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    m_firstInteger->setValue(m_firstInteger->value() + otherIntegerOptionalInteger->m_firstInteger->value());
9009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    m_secondInteger->setValue(m_secondInteger->value() + otherIntegerOptionalInteger->m_secondInteger->value());
9109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
9209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
9307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdochvoid SVGIntegerOptionalInteger::calculateAnimatedValue(SVGAnimationElement* animationElement, float percentage, unsigned repeatCount, PassRefPtr<SVGPropertyBase> from, PassRefPtr<SVGPropertyBase> to, PassRefPtr<SVGPropertyBase> toAtEndOfDuration, SVGElement*)
9409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
9509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    ASSERT(animationElement);
9609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
9709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    RefPtr<SVGIntegerOptionalInteger> fromInteger = toSVGIntegerOptionalInteger(from);
9809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    RefPtr<SVGIntegerOptionalInteger> toInteger = toSVGIntegerOptionalInteger(to);
9909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    RefPtr<SVGIntegerOptionalInteger> toAtEndOfDurationInteger = toSVGIntegerOptionalInteger(toAtEndOfDuration);
10009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
10109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    float x = m_firstInteger->value();
10209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    float y = m_secondInteger->value();
10309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    animationElement->animateAdditiveNumber(percentage, repeatCount, fromInteger->firstInteger()->value(), toInteger->firstInteger()->value(), toAtEndOfDurationInteger->firstInteger()->value(), x);
10409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    animationElement->animateAdditiveNumber(percentage, repeatCount, fromInteger->secondInteger()->value(), toInteger->secondInteger()->value(), toAtEndOfDurationInteger->secondInteger()->value(), y);
10509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    m_firstInteger->setValue(static_cast<int>(roundf(x)));
10609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    m_secondInteger->setValue(static_cast<int>(roundf(y)));
10709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
10809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
10907a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdochfloat SVGIntegerOptionalInteger::calculateDistance(PassRefPtr<SVGPropertyBase> other, SVGElement*)
11009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
11109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    // FIXME: Distance calculation is not possible for SVGIntegerOptionalInteger right now. We need the distance for every single value.
11209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    return -1;
11309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
11409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
11509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
116