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