Home | History | Annotate | Download | only in svg
      1 /*
      2  * Copyright (C) Research In Motion Limited 2010-2011. All rights reserved.
      3  *
      4  * This library is free software; you can redistribute it and/or
      5  * modify it under the terms of the GNU Library General Public
      6  * License as published by the Free Software Foundation; either
      7  * version 2 of the License, or (at your option) any later version.
      8  *
      9  * This library is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  * Library General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU Library General Public License
     15  * along with this library; see the file COPYING.LIB.  If not, write to
     16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17  * Boston, MA 02110-1301, USA.
     18  */
     19 
     20 #include "config.h"
     21 #include "core/svg/SVGPathStringBuilder.h"
     22 
     23 #include "wtf/text/WTFString.h"
     24 
     25 namespace blink {
     26 
     27 String SVGPathStringBuilder::result()
     28 {
     29     unsigned size = m_stringBuilder.length();
     30     if (!size)
     31         return String();
     32 
     33     // Remove trailing space.
     34     m_stringBuilder.resize(size - 1);
     35     return m_stringBuilder.toString();
     36 }
     37 
     38 static void appendFloat(StringBuilder& stringBuilder, float value)
     39 {
     40     stringBuilder.append(' ');
     41     stringBuilder.appendNumber(value);
     42 }
     43 
     44 static void appendBool(StringBuilder& stringBuilder, bool value)
     45 {
     46     stringBuilder.append(' ');
     47     stringBuilder.appendNumber(value);
     48 }
     49 
     50 static void appendPoint(StringBuilder& stringBuilder, const FloatPoint& point)
     51 {
     52     appendFloat(stringBuilder, point.x());
     53     appendFloat(stringBuilder, point.y());
     54 }
     55 
     56 static void emitCommand1Arg(StringBuilder& stringBuilder, char commandChar, float argument)
     57 {
     58     stringBuilder.append(commandChar);
     59     appendFloat(stringBuilder, argument);
     60     stringBuilder.append(' ');
     61 }
     62 
     63 static void emitCommand1Arg(StringBuilder& stringBuilder, char commandChar, const FloatPoint& argumentPoint)
     64 {
     65     stringBuilder.append(commandChar);
     66     appendPoint(stringBuilder, argumentPoint);
     67     stringBuilder.append(' ');
     68 }
     69 
     70 static void emitCommand2Arg(StringBuilder& stringBuilder, char commandChar, const FloatPoint& argument1Point, const FloatPoint& argument2Point)
     71 {
     72     stringBuilder.append(commandChar);
     73     appendPoint(stringBuilder, argument1Point);
     74     appendPoint(stringBuilder, argument2Point);
     75     stringBuilder.append(' ');
     76 }
     77 
     78 void SVGPathStringBuilder::moveTo(const FloatPoint& targetPoint, bool, PathCoordinateMode mode)
     79 {
     80     emitCommand1Arg(m_stringBuilder, (mode == AbsoluteCoordinates) ? 'M' : 'm', targetPoint);
     81 }
     82 
     83 void SVGPathStringBuilder::lineTo(const FloatPoint& targetPoint, PathCoordinateMode mode)
     84 {
     85     emitCommand1Arg(m_stringBuilder, (mode == AbsoluteCoordinates) ? 'L' : 'l', targetPoint);
     86 }
     87 
     88 void SVGPathStringBuilder::lineToHorizontal(float x, PathCoordinateMode mode)
     89 {
     90     emitCommand1Arg(m_stringBuilder, (mode == AbsoluteCoordinates) ? 'H' : 'h', x);
     91 }
     92 
     93 void SVGPathStringBuilder::lineToVertical(float y, PathCoordinateMode mode)
     94 {
     95     emitCommand1Arg(m_stringBuilder, (mode == AbsoluteCoordinates) ? 'V' : 'v', y);
     96 }
     97 
     98 void SVGPathStringBuilder::curveToCubic(const FloatPoint& point1, const FloatPoint& point2, const FloatPoint& targetPoint, PathCoordinateMode mode)
     99 {
    100     m_stringBuilder.append((mode == AbsoluteCoordinates) ? 'C' : 'c');
    101     appendPoint(m_stringBuilder, point1);
    102     appendPoint(m_stringBuilder, point2);
    103     appendPoint(m_stringBuilder, targetPoint);
    104     m_stringBuilder.append(' ');
    105 }
    106 
    107 void SVGPathStringBuilder::curveToCubicSmooth(const FloatPoint& point2, const FloatPoint& targetPoint, PathCoordinateMode mode)
    108 {
    109     emitCommand2Arg(m_stringBuilder, (mode == AbsoluteCoordinates) ? 'S' : 's', point2, targetPoint);
    110 }
    111 
    112 void SVGPathStringBuilder::curveToQuadratic(const FloatPoint& point1, const FloatPoint& targetPoint, PathCoordinateMode mode)
    113 {
    114     emitCommand2Arg(m_stringBuilder, (mode == AbsoluteCoordinates) ? 'Q' : 'q', point1, targetPoint);
    115 }
    116 
    117 void SVGPathStringBuilder::curveToQuadraticSmooth(const FloatPoint& targetPoint, PathCoordinateMode mode)
    118 {
    119     emitCommand1Arg(m_stringBuilder, (mode == AbsoluteCoordinates) ? 'T' : 't', targetPoint);
    120 }
    121 
    122 void SVGPathStringBuilder::arcTo(float r1, float r2, float angle, bool largeArcFlag, bool sweepFlag, const FloatPoint& targetPoint, PathCoordinateMode mode)
    123 {
    124     m_stringBuilder.append((mode == AbsoluteCoordinates) ? 'A' : 'a');
    125     appendFloat(m_stringBuilder, r1);
    126     appendFloat(m_stringBuilder, r2);
    127     appendFloat(m_stringBuilder, angle);
    128     appendBool(m_stringBuilder, largeArcFlag);
    129     appendBool(m_stringBuilder, sweepFlag);
    130     appendPoint(m_stringBuilder, targetPoint);
    131     m_stringBuilder.append(' ');
    132 }
    133 
    134 void SVGPathStringBuilder::closePath()
    135 {
    136     m_stringBuilder.appendLiteral("Z ");
    137 }
    138 
    139 } // namespace blink
    140