Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGNumber.cpp
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
37 namespace blink {
38
39 SVGNumber::SVGNumber(float value)
40     : m_value(value)
41 {
42 }
43
44 PassRefPtr<SVGNumber> SVGNumber::clone() const
45 {
46     return create(m_value);
47 }
48
49 String SVGNumber::valueAsString() const
50 {
51     return String::number(m_value);
52 }
53
54 template<typename CharType>
55 bool SVGNumber::parse(const CharType*& ptr, const CharType* end)
56 {
57     if (!parseNumber(ptr, end, m_value, AllowLeadingAndTrailingWhitespace)) {
58         m_value = 0;
59         return false;
60     }
61
62     if (ptr != end) {
63         m_value = 0;
64         return false;
65     }
66
67     return true;
68 }
69
70 void SVGNumber::setValueAsString(const String& string, ExceptionState& exceptionState)
71 {
72     if (string.isEmpty()) {
73         m_value = 0;
74         return;
75     }
76
77     bool valid = false;
78     if (string.is8Bit()) {
79         const LChar* ptr = string.characters8();
80         const LChar* end = ptr + string.length();
81         valid = parse(ptr, end);
82     } else {
83         const UChar* ptr = string.characters16();
84         const UChar* end = ptr + string.length();
85         valid = parse(ptr, end);
86     }
87
88     if (!valid) {
89         exceptionState.throwDOMException(SyntaxError, "The value provided ('" + string + "') is invalid.");
90         m_value = 0;
91     }
92 }
93
94 void SVGNumber::add(PassRefPtrWillBeRawPtr<SVGPropertyBase> other, SVGElement*)
95 {
96     setValue(m_value + toSVGNumber(other)->value());
97 }
98
99 void SVGNumber::calculateAnimatedValue(SVGAnimationElement* animationElement, float percentage, unsigned repeatCount, PassRefPtr<SVGPropertyBase> from, PassRefPtr<SVGPropertyBase> to, PassRefPtr<SVGPropertyBase> toAtEndOfDuration, SVGElement*)
100 {
101     ASSERT(animationElement);
102
103     RefPtr<SVGNumber> fromNumber = toSVGNumber(from);
104     RefPtr<SVGNumber> toNumber = toSVGNumber(to);
105     RefPtr<SVGNumber> toAtEndOfDurationNumber = toSVGNumber(toAtEndOfDuration);
106
107     animationElement->animateAdditiveNumber(percentage, repeatCount, fromNumber->value(), toNumber->value(), toAtEndOfDurationNumber->value(), m_value);
108 }
109
110 float SVGNumber::calculateDistance(PassRefPtr<SVGPropertyBase> other, SVGElement*)
111 {
112     return fabsf(m_value - toSVGNumber(other)->value());
113 }
114
115 PassRefPtr<SVGNumber> SVGNumberAcceptPercentage::clone() const
116 {
117     return create(m_value);
118 }
119
120 void SVGNumberAcceptPercentage::setValueAsString(const String& string, ExceptionState& exceptionState)
121 {
122     bool valid = parseNumberOrPercentage(string, m_value);
123
124     if (!valid) {
125         exceptionState.throwDOMException(SyntaxError, "The value provided ('" + string + "') is invalid.");
126         m_value = 0;
127     }
128 }
129
130 SVGNumberAcceptPercentage::SVGNumberAcceptPercentage(float value)
131     : SVGNumber(value)
132 {
133 }
134
135 }