kpresenter

KPrSVGPathParser.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005 Thorsten Zachmann <zachmann@kde.org>
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 "KPrSVGPathParser.h"
21
22
23ObjType KPrSVGPathParser::getType( TQString &d )
24{
25 m_type = true;
26 m_pathType = 0;
27 ObjType objType = OT_UNDEFINED;
28 parseSVG( d, true );
29 if ( m_pathType & UNSUPPORTED )
30 {
31 objType = OT_UNDEFINED;
32 }
33 else if ( m_pathType & SEEN_CLOSE )
34 {
35 objType = OT_CLOSED_LINE;
36 }
37 else if ( m_pathType & NO_BEZIER )
38 {
39 objType = OT_FREEHAND;
40 }
41 else if ( ( m_pathType & SEEN_QUADRIC ) && ! ( m_pathType & SEEN_CUBIC ) )
42 {
43 objType = OT_QUADRICBEZIERCURVE;
44 }
45 else if ( m_pathType & SEEN_CUBIC )
46 {
47 objType = OT_CUBICBEZIERCURVE;
48 }
49 return objType;
50}
51
52
53KoPointArray KPrSVGPathParser::getPoints( TQString &d, bool convert2lines )
54{
55 m_type = false;
56 m_pointIdx = 0;
57 m_convert2lines = convert2lines;
58 parseSVG( d, true );
59 return m_points;
60}
61
62
63void KPrSVGPathParser::svgMoveTo( double x1, double y1, bool /*abs*/ )
64{
65 if ( m_type )
66 {
67 if ( m_pathType != 0 )
68 {
69 m_pathType |= UNSUPPORTED;
70 }
71 else
72 {
73 m_pathType |= SEEN_MOVE;
74 }
75 }
76 else
77 {
78 m_curPoint = KoPoint( x1, y1 );
79 }
80}
81
82
83void KPrSVGPathParser::svgLineTo( double x1, double y1, bool /*abs*/ )
84{
85 if ( m_type )
86 {
87 if ( m_pathType & SEEN_MOVE )
88 {
89 if ( m_pathType & SEEN_LINE )
90 {
91 m_pathType |= NO_BEZIER;
92 }
93 m_pathType |= SEEN_LINE;
94 }
95 else
96 {
97 m_pathType |= UNSUPPORTED;
98 }
99 }
100 else
101 {
102 m_points.putPoints( m_pointIdx, 2, m_curPoint.x(), m_curPoint.y(), x1, y1 );
103 m_curPoint = KoPoint( x1, y1 );
104 m_pointIdx += 2;
105 }
106}
107
108
109void KPrSVGPathParser::svgCurveToCubic( double x1, double y1, double x2, double y2, double x, double y, bool /*abs*/ )
110{
111 if ( m_type )
112 {
113 if ( m_pathType & SEEN_MOVE )
114 {
115 if ( m_pathType & SEEN_LINE )
116 {
117 m_pathType |= NO_BEZIER;
118 }
119
120 if ( x1 == x2 && y1 == y2 )
121 {
122 m_pathType |= SEEN_QUADRIC;
123 }
124 else
125 {
126 m_pathType |= SEEN_CUBIC;
127 }
128 }
129 else
130 {
131 m_pathType |= UNSUPPORTED;
132 }
133 }
134 else
135 {
136 if ( m_convert2lines )
137 {
138 KoPointArray bezierPoints;
139 bezierPoints.putPoints( 0, 4, m_curPoint.x(), m_curPoint.y(), x1, y1, x2, y2, x, y );
140
141 bezierPoints = bezierPoints.cubicBezier();
142 KoPointArray::ConstIterator it;
143 for ( it = bezierPoints.begin(); it != bezierPoints.end(); ++it )
144 {
145 KoPoint point = (*it);
146 m_points.putPoints( m_pointIdx, 1, point.x(), point.y() );
147 ++m_pointIdx;
148 }
149 }
150 else
151 {
152 m_points.putPoints( m_pointIdx, 4, m_curPoint.x(), m_curPoint.y(), x, y, x1, y1, x2, y2 );
153 m_pointIdx += 4;
154 }
155 m_curPoint = KoPoint( x, y );
156 }
157}
158
159
160// TODO
161void KPrSVGPathParser::svgClosePath()
162{
163 if ( m_type )
164 {
165 if ( m_pathType & SEEN_CLOSE )
166 {
167 m_pathType |= UNSUPPORTED;
168 }
169 else
170 {
171 m_pathType |= SEEN_CLOSE;
172 }
173 }
174 else
175 {
176 }
177}
178
179