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
33#include "core/svg/SVGPoint.h"
34
35#include "bindings/core/v8/ExceptionState.h"
36#include "core/dom/ExceptionCode.h"
37#include "core/svg/SVGAnimationElement.h"
38#include "core/svg/SVGParserUtilities.h"
39#include "platform/transforms/AffineTransform.h"
40#include "wtf/text/StringBuilder.h"
41#include "wtf/text/WTFString.h"
42
43namespace blink {
44
45SVGPoint::SVGPoint()
46{
47}
48
49SVGPoint::SVGPoint(const FloatPoint& point)
50    : m_value(point)
51{
52}
53
54PassRefPtr<SVGPoint> SVGPoint::clone() const
55{
56    return SVGPoint::create(m_value);
57}
58
59template<typename CharType>
60void SVGPoint::parse(const CharType*& ptr, const CharType* end, ExceptionState& exceptionState)
61{
62    const CharType* start = ptr;
63
64    skipOptionalSVGSpaces(ptr, end);
65
66    float x = 0.0f;
67    float y = 0.0f;
68    bool valid = parseNumber(ptr, end, x) && parseNumber(ptr, end, y, DisallowWhitespace);
69
70    if (!valid) {
71        exceptionState.throwDOMException(SyntaxError, "Problem parsing point \"" + String(start, end - start) + "\"");
72        return;
73    }
74
75    skipOptionalSVGSpaces(ptr, end);
76    if (ptr < end) { // nothing should come after the last, fourth number
77        exceptionState.throwDOMException(SyntaxError, "Problem parsing point \"" + String(start, end - start) + "\"");
78        return;
79    }
80
81    m_value = FloatPoint(x, y);
82}
83
84FloatPoint SVGPoint::matrixTransform(const AffineTransform& transform) const
85{
86    double newX, newY;
87    transform.map(static_cast<double>(x()), static_cast<double>(y()), newX, newY);
88    return FloatPoint::narrowPrecision(newX, newY);
89}
90
91void SVGPoint::setValueAsString(const String& string, ExceptionState& exceptionState)
92{
93    if (string.isEmpty()) {
94        m_value = FloatPoint(0.0f, 0.0f);
95        return;
96    }
97
98    if (string.is8Bit()) {
99        const LChar* ptr = string.characters8();
100        const LChar* end = ptr + string.length();
101        parse(ptr, end, exceptionState);
102        return;
103    }
104
105    const UChar* ptr = string.characters16();
106    const UChar* end = ptr + string.length();
107    parse(ptr, end, exceptionState);
108}
109
110String SVGPoint::valueAsString() const
111{
112    StringBuilder builder;
113    builder.appendNumber(x());
114    builder.append(' ');
115    builder.appendNumber(y());
116    return builder.toString();
117}
118
119void SVGPoint::add(PassRefPtrWillBeRawPtr<SVGPropertyBase> other, SVGElement*)
120{
121    // SVGPoint is not animated by itself
122    ASSERT_NOT_REACHED();
123}
124
125void SVGPoint::calculateAnimatedValue(SVGAnimationElement* animationElement, float percentage, unsigned repeatCount, PassRefPtr<SVGPropertyBase> fromValue, PassRefPtr<SVGPropertyBase> toValue, PassRefPtr<SVGPropertyBase> toAtEndOfDurationValue, SVGElement*)
126{
127    // SVGPoint is not animated by itself
128    ASSERT_NOT_REACHED();
129}
130
131float SVGPoint::calculateDistance(PassRefPtr<SVGPropertyBase> to, SVGElement* contextElement)
132{
133    // SVGPoint is not animated by itself
134    ASSERT_NOT_REACHED();
135    return 0.0f;
136}
137
138}
139