2 * Copyright (C) Research In Motion Limited 2010. 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.
23 #include "SVGPathStringSource.h"
25 #include "SVGParserUtilities.h"
29 SVGPathStringSource::SVGPathStringSource(const String& string)
31 , m_current(string.characters())
32 , m_end(m_current + string.length())
34 ASSERT(!string.isEmpty());
37 bool SVGPathStringSource::hasMoreData() const
39 return m_current < m_end;
42 bool SVGPathStringSource::moveToNextToken()
44 return skipOptionalSVGSpaces(m_current, m_end);
47 bool SVGPathStringSource::parseSVGSegmentType(SVGPathSegType& pathSegType)
49 switch (*(m_current++)) {
52 pathSegType = PathSegClosePath;
55 pathSegType = PathSegMoveToAbs;
58 pathSegType = PathSegMoveToRel;
61 pathSegType = PathSegLineToAbs;
64 pathSegType = PathSegLineToRel;
67 pathSegType = PathSegCurveToCubicAbs;
70 pathSegType = PathSegCurveToCubicRel;
73 pathSegType = PathSegCurveToQuadraticAbs;
76 pathSegType = PathSegCurveToQuadraticRel;
79 pathSegType = PathSegArcAbs;
82 pathSegType = PathSegArcRel;
85 pathSegType = PathSegLineToHorizontalAbs;
88 pathSegType = PathSegLineToHorizontalRel;
91 pathSegType = PathSegLineToVerticalAbs;
94 pathSegType = PathSegLineToVerticalRel;
97 pathSegType = PathSegCurveToCubicSmoothAbs;
100 pathSegType = PathSegCurveToCubicSmoothRel;
103 pathSegType = PathSegCurveToQuadraticSmoothAbs;
106 pathSegType = PathSegCurveToQuadraticSmoothRel;
109 pathSegType = PathSegUnknown;
114 SVGPathSegType SVGPathStringSource::nextCommand(SVGPathSegType previousCommand)
116 // Check for remaining coordinates in the current command.
117 if ((*m_current == '+' || *m_current == '-' || *m_current == '.' || (*m_current >= '0' && *m_current <= '9'))
118 && previousCommand != PathSegClosePath) {
119 if (previousCommand == PathSegMoveToAbs)
120 return PathSegLineToAbs;
121 if (previousCommand == PathSegMoveToRel)
122 return PathSegLineToRel;
123 return previousCommand;
125 SVGPathSegType nextCommand;
126 parseSVGSegmentType(nextCommand);
130 bool SVGPathStringSource::parseMoveToSegment(FloatPoint& targetPoint)
134 if (!parseNumber(m_current, m_end, toX) || !parseNumber(m_current, m_end, toY))
136 targetPoint = FloatPoint(toX, toY);
140 bool SVGPathStringSource::parseLineToSegment(FloatPoint& targetPoint)
144 if (!parseNumber(m_current, m_end, toX) || !parseNumber(m_current, m_end, toY))
146 targetPoint = FloatPoint(toX, toY);
150 bool SVGPathStringSource::parseLineToHorizontalSegment(float& x)
152 return parseNumber(m_current, m_end, x);
155 bool SVGPathStringSource::parseLineToVerticalSegment(float& y)
157 return parseNumber(m_current, m_end, y);
160 bool SVGPathStringSource::parseCurveToCubicSegment(FloatPoint& point1, FloatPoint& point2, FloatPoint& targetPoint)
168 if (!parseNumber(m_current, m_end, x1)
169 || !parseNumber(m_current, m_end, y1)
170 || !parseNumber(m_current, m_end, x2)
171 || !parseNumber(m_current, m_end, y2)
172 || !parseNumber(m_current, m_end, toX)
173 || !parseNumber(m_current, m_end, toY))
175 point1 = FloatPoint(x1, y1);
176 point2 = FloatPoint(x2, y2);
177 targetPoint = FloatPoint(toX, toY);
181 bool SVGPathStringSource::parseCurveToCubicSmoothSegment(FloatPoint& point1, FloatPoint& targetPoint)
187 if (!parseNumber(m_current, m_end, x1)
188 || !parseNumber(m_current, m_end, y1)
189 || !parseNumber(m_current, m_end, toX)
190 || !parseNumber(m_current, m_end, toY))
192 point1 = FloatPoint(x1, y1);
193 targetPoint = FloatPoint(toX, toY);
197 bool SVGPathStringSource::parseCurveToQuadraticSegment(FloatPoint& point2, FloatPoint& targetPoint)
203 if (!parseNumber(m_current, m_end, x2)
204 || !parseNumber(m_current, m_end, y2)
205 || !parseNumber(m_current, m_end, toX)
206 || !parseNumber(m_current, m_end, toY))
208 point2 = FloatPoint(x2, y2);
209 targetPoint = FloatPoint(toX, toY);
213 bool SVGPathStringSource::parseCurveToQuadraticSmoothSegment(FloatPoint& targetPoint)
217 if (!parseNumber(m_current, m_end, toX)
218 || !parseNumber(m_current, m_end, toY))
220 targetPoint = FloatPoint(toX, toY);
224 bool SVGPathStringSource::parseArcToSegment(float& rx, float& ry, float& angle, bool& largeArc, bool& sweep, FloatPoint& targetPoint)
228 if (!parseNumber(m_current, m_end, rx)
229 || !parseNumber(m_current, m_end, ry)
230 || !parseNumber(m_current, m_end, angle)
231 || !parseArcFlag(m_current, m_end, largeArc)
232 || !parseArcFlag(m_current, m_end, sweep)
233 || !parseNumber(m_current, m_end, toX)
234 || !parseNumber(m_current, m_end, toY))
236 targetPoint = FloatPoint(toX, toY);
243 #endif // ENABLE(SVG)