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
22#if ENABLE(SVG)
23#include "SVGPathStringBuilder.h"
24#include <wtf/text/WTFString.h>
25
26namespace WebCore {
27
28String SVGPathStringBuilder::result()
29{
30    unsigned size = m_stringBuilder.length();
31    if (!size)
32        return String();
33
34    // Remove trailing space.
35    m_stringBuilder.resize(size - 1);
36    return m_stringBuilder.toString();
37}
38
39void SVGPathStringBuilder::moveTo(const FloatPoint& targetPoint, bool, PathCoordinateMode mode)
40{
41    if (mode == AbsoluteCoordinates)
42        m_stringBuilder.append("M " + String::number(targetPoint.x()) + ' ' + String::number(targetPoint.y()) + ' ');
43    else
44        m_stringBuilder.append("m " + String::number(targetPoint.x()) + ' ' + String::number(targetPoint.y()) + ' ');
45}
46
47void SVGPathStringBuilder::lineTo(const FloatPoint& targetPoint, PathCoordinateMode mode)
48{
49    if (mode == AbsoluteCoordinates)
50        m_stringBuilder.append("L " + String::number(targetPoint.x()) + ' ' + String::number(targetPoint.y()) + ' ');
51    else
52        m_stringBuilder.append("l " + String::number(targetPoint.x()) + ' ' + String::number(targetPoint.y()) + ' ');
53}
54
55void SVGPathStringBuilder::lineToHorizontal(float x, PathCoordinateMode mode)
56{
57    if (mode == AbsoluteCoordinates)
58        m_stringBuilder.append("H " + String::number(x) + ' ');
59    else
60        m_stringBuilder.append("h " + String::number(x) + ' ');
61}
62
63void SVGPathStringBuilder::lineToVertical(float y, PathCoordinateMode mode)
64{
65    if (mode == AbsoluteCoordinates)
66        m_stringBuilder.append("V " + String::number(y) + ' ');
67    else
68        m_stringBuilder.append("v " + String::number(y) + ' ');
69}
70
71void SVGPathStringBuilder::curveToCubic(const FloatPoint& point1, const FloatPoint& point2, const FloatPoint& targetPoint, PathCoordinateMode mode)
72{
73    if (mode == AbsoluteCoordinates) {
74        m_stringBuilder.append("C " + String::number(point1.x()) + ' ' + String::number(point1.y())
75                              + ' ' + String::number(point2.x()) + ' ' + String::number(point2.y())
76                              + ' ' + String::number(targetPoint.x()) + ' ' + String::number(targetPoint.y()) + ' ');
77        return;
78    }
79
80    m_stringBuilder.append("c " + String::number(point1.x()) + ' ' + String::number(point1.y())
81                          + ' ' + String::number(point2.x()) + ' ' + String::number(point2.y())
82                          + ' ' + String::number(targetPoint.x()) + ' ' + String::number(targetPoint.y()) + ' ');
83}
84
85void SVGPathStringBuilder::curveToCubicSmooth(const FloatPoint& point2, const FloatPoint& targetPoint, PathCoordinateMode mode)
86{
87    if (mode == AbsoluteCoordinates) {
88        m_stringBuilder.append("S " + String::number(point2.x()) + ' ' + String::number(point2.y())
89                              + ' ' + String::number(targetPoint.x()) + ' ' + String::number(targetPoint.y()) + ' ');
90        return;
91    }
92
93    m_stringBuilder.append("s " + String::number(point2.x()) + ' ' + String::number(point2.y())
94                          + ' ' + String::number(targetPoint.x()) + ' ' + String::number(targetPoint.y()) + ' ');
95}
96
97void SVGPathStringBuilder::curveToQuadratic(const FloatPoint& point1, const FloatPoint& targetPoint, PathCoordinateMode mode)
98{
99    if (mode == AbsoluteCoordinates) {
100        m_stringBuilder.append("Q " + String::number(point1.x()) + ' ' + String::number(point1.y())
101                              + ' ' + String::number(targetPoint.x()) + ' ' + String::number(targetPoint.y()) + ' ');
102        return;
103    }
104
105    m_stringBuilder.append("q " + String::number(point1.x()) + ' ' + String::number(point1.y())
106                          + ' ' + String::number(targetPoint.x()) + ' ' + String::number(targetPoint.y()) + ' ');
107}
108
109void SVGPathStringBuilder::curveToQuadraticSmooth(const FloatPoint& targetPoint, PathCoordinateMode mode)
110{
111    if (mode == AbsoluteCoordinates)
112        m_stringBuilder.append("T " + String::number(targetPoint.x()) + ' ' + String::number(targetPoint.y()) + ' ');
113    else
114        m_stringBuilder.append("t " + String::number(targetPoint.x()) + ' ' + String::number(targetPoint.y()) + ' ');
115}
116
117void SVGPathStringBuilder::arcTo(float r1, float r2, float angle, bool largeArcFlag, bool sweepFlag, const FloatPoint& targetPoint, PathCoordinateMode mode)
118{
119    if (mode == AbsoluteCoordinates) {
120        m_stringBuilder.append("A " + String::number(r1) + ' ' + String::number(r2)
121                              + ' ' + String::number(angle) + ' ' + String::number(largeArcFlag) + ' ' + String::number(sweepFlag)
122                              + ' ' + String::number(targetPoint.x()) + ' ' + String::number(targetPoint.y()) + ' ');
123        return;
124    }
125
126    m_stringBuilder.append("a " + String::number(r1) + ' ' + String::number(r2)
127                          + ' ' + String::number(angle) + ' ' + String::number(largeArcFlag) + ' ' + String::number(sweepFlag)
128                          + ' ' + String::number(targetPoint.x()) + ' ' + String::number(targetPoint.y()) + ' ');
129}
130
131void SVGPathStringBuilder::closePath()
132{
133    m_stringBuilder.append("Z ");
134}
135
136} // namespace WebCore
137
138#endif // ENABLE(SVG)
139