1 /*
2 * Copyright (C) Research In Motion Limited 2010. 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 #include "core/svg/SVGPathSegListSource.h"
23
24 #include "core/svg/SVGPathSegArc.h"
25 #include "core/svg/SVGPathSegCurvetoCubic.h"
26 #include "core/svg/SVGPathSegCurvetoCubicSmooth.h"
27 #include "core/svg/SVGPathSegCurvetoQuadratic.h"
28 #include "core/svg/SVGPathSegLinetoHorizontal.h"
29 #include "core/svg/SVGPathSegLinetoVertical.h"
30
31 namespace WebCore {
32
SVGPathSegListSource(const SVGPathSegList & pathSegList)33 SVGPathSegListSource::SVGPathSegListSource(const SVGPathSegList& pathSegList)
34 : m_pathSegList(pathSegList)
35 {
36 m_itemCurrent = 0;
37 m_itemEnd = m_pathSegList.size();
38 }
39
hasMoreData() const40 bool SVGPathSegListSource::hasMoreData() const
41 {
42 return m_itemCurrent < m_itemEnd;
43 }
44
parseSVGSegmentType(SVGPathSegType & pathSegType)45 bool SVGPathSegListSource::parseSVGSegmentType(SVGPathSegType& pathSegType)
46 {
47 m_segment = m_pathSegList.at(m_itemCurrent);
48 pathSegType = static_cast<SVGPathSegType>(m_segment->pathSegType());
49 ++m_itemCurrent;
50 return true;
51 }
52
nextCommand(SVGPathSegType)53 SVGPathSegType SVGPathSegListSource::nextCommand(SVGPathSegType)
54 {
55 m_segment = m_pathSegList.at(m_itemCurrent);
56 SVGPathSegType pathSegType = static_cast<SVGPathSegType>(m_segment->pathSegType());
57 ++m_itemCurrent;
58 return pathSegType;
59 }
60
parseMoveToSegment(FloatPoint & targetPoint)61 bool SVGPathSegListSource::parseMoveToSegment(FloatPoint& targetPoint)
62 {
63 ASSERT(m_segment);
64 ASSERT(m_segment->pathSegType() == PathSegMoveToAbs || m_segment->pathSegType() == PathSegMoveToRel);
65 SVGPathSegSingleCoordinate* moveTo = static_cast<SVGPathSegSingleCoordinate*>(m_segment.get());
66 targetPoint = FloatPoint(moveTo->x(), moveTo->y());
67 return true;
68 }
69
parseLineToSegment(FloatPoint & targetPoint)70 bool SVGPathSegListSource::parseLineToSegment(FloatPoint& targetPoint)
71 {
72 ASSERT(m_segment);
73 ASSERT(m_segment->pathSegType() == PathSegLineToAbs || m_segment->pathSegType() == PathSegLineToRel);
74 SVGPathSegSingleCoordinate* lineTo = static_cast<SVGPathSegSingleCoordinate*>(m_segment.get());
75 targetPoint = FloatPoint(lineTo->x(), lineTo->y());
76 return true;
77 }
78
parseLineToHorizontalSegment(float & x)79 bool SVGPathSegListSource::parseLineToHorizontalSegment(float& x)
80 {
81 ASSERT(m_segment);
82 ASSERT(m_segment->pathSegType() == PathSegLineToHorizontalAbs || m_segment->pathSegType() == PathSegLineToHorizontalRel);
83 SVGPathSegLinetoHorizontal* horizontal = static_cast<SVGPathSegLinetoHorizontal*>(m_segment.get());
84 x = horizontal->x();
85 return true;
86 }
87
parseLineToVerticalSegment(float & y)88 bool SVGPathSegListSource::parseLineToVerticalSegment(float& y)
89 {
90 ASSERT(m_segment);
91 ASSERT(m_segment->pathSegType() == PathSegLineToVerticalAbs || m_segment->pathSegType() == PathSegLineToVerticalRel);
92 SVGPathSegLinetoVertical* vertical = static_cast<SVGPathSegLinetoVertical*>(m_segment.get());
93 y = vertical->y();
94 return true;
95 }
96
parseCurveToCubicSegment(FloatPoint & point1,FloatPoint & point2,FloatPoint & targetPoint)97 bool SVGPathSegListSource::parseCurveToCubicSegment(FloatPoint& point1, FloatPoint& point2, FloatPoint& targetPoint)
98 {
99 ASSERT(m_segment);
100 ASSERT(m_segment->pathSegType() == PathSegCurveToCubicAbs || m_segment->pathSegType() == PathSegCurveToCubicRel);
101 SVGPathSegCurvetoCubic* cubic = static_cast<SVGPathSegCurvetoCubic*>(m_segment.get());
102 point1 = FloatPoint(cubic->x1(), cubic->y1());
103 point2 = FloatPoint(cubic->x2(), cubic->y2());
104 targetPoint = FloatPoint(cubic->x(), cubic->y());
105 return true;
106 }
107
parseCurveToCubicSmoothSegment(FloatPoint & point2,FloatPoint & targetPoint)108 bool SVGPathSegListSource::parseCurveToCubicSmoothSegment(FloatPoint& point2, FloatPoint& targetPoint)
109 {
110 ASSERT(m_segment);
111 ASSERT(m_segment->pathSegType() == PathSegCurveToCubicSmoothAbs || m_segment->pathSegType() == PathSegCurveToCubicSmoothRel);
112 SVGPathSegCurvetoCubicSmooth* cubicSmooth = static_cast<SVGPathSegCurvetoCubicSmooth*>(m_segment.get());
113 point2 = FloatPoint(cubicSmooth->x2(), cubicSmooth->y2());
114 targetPoint = FloatPoint(cubicSmooth->x(), cubicSmooth->y());
115 return true;
116 }
117
parseCurveToQuadraticSegment(FloatPoint & point1,FloatPoint & targetPoint)118 bool SVGPathSegListSource::parseCurveToQuadraticSegment(FloatPoint& point1, FloatPoint& targetPoint)
119 {
120 ASSERT(m_segment);
121 ASSERT(m_segment->pathSegType() == PathSegCurveToQuadraticAbs || m_segment->pathSegType() == PathSegCurveToQuadraticRel);
122 SVGPathSegCurvetoQuadratic* quadratic = static_cast<SVGPathSegCurvetoQuadratic*>(m_segment.get());
123 point1 = FloatPoint(quadratic->x1(), quadratic->y1());
124 targetPoint = FloatPoint(quadratic->x(), quadratic->y());
125 return true;
126 }
127
parseCurveToQuadraticSmoothSegment(FloatPoint & targetPoint)128 bool SVGPathSegListSource::parseCurveToQuadraticSmoothSegment(FloatPoint& targetPoint)
129 {
130 ASSERT(m_segment);
131 ASSERT(m_segment->pathSegType() == PathSegCurveToQuadraticSmoothAbs || m_segment->pathSegType() == PathSegCurveToQuadraticSmoothRel);
132 SVGPathSegSingleCoordinate* quadraticSmooth = static_cast<SVGPathSegSingleCoordinate*>(m_segment.get());
133 targetPoint = FloatPoint(quadraticSmooth->x(), quadraticSmooth->y());
134 return true;
135 }
136
parseArcToSegment(float & rx,float & ry,float & angle,bool & largeArc,bool & sweep,FloatPoint & targetPoint)137 bool SVGPathSegListSource::parseArcToSegment(float& rx, float& ry, float& angle, bool& largeArc, bool& sweep, FloatPoint& targetPoint)
138 {
139 ASSERT(m_segment);
140 ASSERT(m_segment->pathSegType() == PathSegArcAbs || m_segment->pathSegType() == PathSegArcRel);
141 SVGPathSegArc* arcTo = static_cast<SVGPathSegArc*>(m_segment.get());
142 rx = arcTo->r1();
143 ry = arcTo->r2();
144 angle = arcTo->angle();
145 largeArc = arcTo->largeArcFlag();
146 sweep = arcTo->sweepFlag();
147 targetPoint = FloatPoint(arcTo->x(), arcTo->y());
148 return true;
149 }
150
151 }
152