2 * Copyright (C) Research In Motion Limited 2010-2011. All rights reserved.
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.
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.
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.
21 #include "core/svg/SVGPathStringBuilder.h"
23 #include "wtf/text/WTFString.h"
27 String SVGPathStringBuilder::result()
29 unsigned size = m_stringBuilder.length();
33 // Remove trailing space.
34 m_stringBuilder.resize(size - 1);
35 return m_stringBuilder.toString();
38 static void appendFloat(StringBuilder& stringBuilder, float value)
40 stringBuilder.append(' ');
41 stringBuilder.appendNumber(value);
44 static void appendBool(StringBuilder& stringBuilder, bool value)
46 stringBuilder.append(' ');
47 stringBuilder.appendNumber(value);
50 static void appendPoint(StringBuilder& stringBuilder, const FloatPoint& point)
52 appendFloat(stringBuilder, point.x());
53 appendFloat(stringBuilder, point.y());
56 static void emitCommand1Arg(StringBuilder& stringBuilder, char commandChar, float argument)
58 stringBuilder.append(commandChar);
59 appendFloat(stringBuilder, argument);
60 stringBuilder.append(' ');
63 static void emitCommand1Arg(StringBuilder& stringBuilder, char commandChar, const FloatPoint& argumentPoint)
65 stringBuilder.append(commandChar);
66 appendPoint(stringBuilder, argumentPoint);
67 stringBuilder.append(' ');
70 static void emitCommand2Arg(StringBuilder& stringBuilder, char commandChar, const FloatPoint& argument1Point, const FloatPoint& argument2Point)
72 stringBuilder.append(commandChar);
73 appendPoint(stringBuilder, argument1Point);
74 appendPoint(stringBuilder, argument2Point);
75 stringBuilder.append(' ');
78 void SVGPathStringBuilder::moveTo(const FloatPoint& targetPoint, bool, PathCoordinateMode mode)
80 emitCommand1Arg(m_stringBuilder, (mode == AbsoluteCoordinates) ? 'M' : 'm', targetPoint);
83 void SVGPathStringBuilder::lineTo(const FloatPoint& targetPoint, PathCoordinateMode mode)
85 emitCommand1Arg(m_stringBuilder, (mode == AbsoluteCoordinates) ? 'L' : 'l', targetPoint);
88 void SVGPathStringBuilder::lineToHorizontal(float x, PathCoordinateMode mode)
90 emitCommand1Arg(m_stringBuilder, (mode == AbsoluteCoordinates) ? 'H' : 'h', x);
93 void SVGPathStringBuilder::lineToVertical(float y, PathCoordinateMode mode)
95 emitCommand1Arg(m_stringBuilder, (mode == AbsoluteCoordinates) ? 'V' : 'v', y);
98 void SVGPathStringBuilder::curveToCubic(const FloatPoint& point1, const FloatPoint& point2, const FloatPoint& targetPoint, PathCoordinateMode mode)
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(' ');
107 void SVGPathStringBuilder::curveToCubicSmooth(const FloatPoint& point2, const FloatPoint& targetPoint, PathCoordinateMode mode)
109 emitCommand2Arg(m_stringBuilder, (mode == AbsoluteCoordinates) ? 'S' : 's', point2, targetPoint);
112 void SVGPathStringBuilder::curveToQuadratic(const FloatPoint& point1, const FloatPoint& targetPoint, PathCoordinateMode mode)
114 emitCommand2Arg(m_stringBuilder, (mode == AbsoluteCoordinates) ? 'Q' : 'q', point1, targetPoint);
117 void SVGPathStringBuilder::curveToQuadraticSmooth(const FloatPoint& targetPoint, PathCoordinateMode mode)
119 emitCommand1Arg(m_stringBuilder, (mode == AbsoluteCoordinates) ? 'T' : 't', targetPoint);
122 void SVGPathStringBuilder::arcTo(float r1, float r2, float angle, bool largeArcFlag, bool sweepFlag, const FloatPoint& targetPoint, PathCoordinateMode mode)
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(' ');
134 void SVGPathStringBuilder::closePath()
136 m_stringBuilder.appendLiteral("Z ");