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