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