1 #define __SP_SVG_PARSE_C__
2 /*
3 svg-path.c: Parse SVG path element data into bezier path.
5 Copyright (C) 2000 Eazel, Inc.
6 Copyright (C) 2000 Lauris Kaplinski
7 Copyright (C) 2001 Ximian, Inc.
8 Copyright (C) 2008 Johan Engelen
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of the
13 License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public
21 License along with this program; if not, write to the
22 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA.
25 Authors:
26 Johan Engelen
27 (old nartbpath code that has been deleted: Raph Levien <raph@artofcode.com>)
28 (old nartbpath code that has been deleted: Lauris Kaplinski <lauris@ximian.com>)
29 */
31 #include <cstring>
32 #include <string>
33 #include <cassert>
34 #include <glib/gmem.h>
35 #include <glib/gmessages.h>
36 #include <glib/gstrfuncs.h>
37 #include <glib.h> // g_assert()
39 #include "svg/svg.h"
40 #include "svg/path-string.h"
42 #include <2geom/pathvector.h>
43 #include <2geom/path.h>
44 #include <2geom/curves.h>
45 #include <2geom/sbasis-to-bezier.h>
46 #include <2geom/svg-path.h>
47 #include <2geom/svg-path-parser.h>
48 #include <2geom/exception.h>
50 /*
51 * Parses the path in str. When an error is found in the pathstring, this method
52 * returns a truncated path up to where the error was found in the pathstring.
53 * Returns an empty PathVector when str==NULL
54 */
55 Geom::PathVector sp_svg_read_pathv(char const * str)
56 {
57 Geom::PathVector pathv;
58 if (!str)
59 return pathv; // return empty pathvector when str == NULL
62 typedef std::back_insert_iterator<Geom::PathVector> Inserter;
63 Inserter iter(pathv);
64 Geom::SVGPathGenerator<Inserter> generator(iter);
66 try {
67 Geom::parse_svg_path(str, generator);
68 }
69 catch (Geom::SVGPathParseError e) {
70 generator.finish();
71 g_warning("Malformed SVG path, truncated path up to where error was found.\n Input path=\"%s\"\n Parsed path=\"%s\"", str, sp_svg_write_path(pathv));
72 }
74 return pathv;
75 }
77 static void sp_svg_write_curve(Inkscape::SVG::PathString & str, Geom::Curve const * c) {
78 if(Geom::LineSegment const *line_segment = dynamic_cast<Geom::LineSegment const *>(c)) {
79 // don't serialize stitch segments
80 if (!dynamic_cast<Geom::Path::StitchSegment const *>(c)) {
81 str.lineTo( (*line_segment)[1][0], (*line_segment)[1][1] );
82 }
83 }
84 else if(Geom::QuadraticBezier const *quadratic_bezier = dynamic_cast<Geom::QuadraticBezier const *>(c)) {
85 str.quadTo( (*quadratic_bezier)[1][0], (*quadratic_bezier)[1][1],
86 (*quadratic_bezier)[2][0], (*quadratic_bezier)[2][1] );
87 }
88 else if(Geom::CubicBezier const *cubic_bezier = dynamic_cast<Geom::CubicBezier const *>(c)) {
89 str.curveTo( (*cubic_bezier)[1][0], (*cubic_bezier)[1][1],
90 (*cubic_bezier)[2][0], (*cubic_bezier)[2][1],
91 (*cubic_bezier)[3][0], (*cubic_bezier)[3][1] );
92 }
93 else if(Geom::SVGEllipticalArc const *svg_elliptical_arc = dynamic_cast<Geom::SVGEllipticalArc const *>(c)) {
94 str.arcTo( svg_elliptical_arc->ray(0), svg_elliptical_arc->ray(1),
95 svg_elliptical_arc->rotation_angle(),
96 svg_elliptical_arc->large_arc_flag(), svg_elliptical_arc->sweep_flag(),
97 svg_elliptical_arc->finalPoint() );
98 }
99 else if(Geom::HLineSegment const *hline_segment = dynamic_cast<Geom::HLineSegment const *>(c)) {
100 str.horizontalLineTo( hline_segment->finalPoint()[0] );
101 }
102 else if(Geom::VLineSegment const *vline_segment = dynamic_cast<Geom::VLineSegment const *>(c)) {
103 str.verticalLineTo( vline_segment->finalPoint()[1] );
104 } else {
105 //this case handles sbasis as well as all other curve types
106 Geom::Path sbasis_path = Geom::cubicbezierpath_from_sbasis(c->toSBasis(), 0.1);
108 //recurse to convert the new path resulting from the sbasis to svgd
109 for(Geom::Path::iterator iter = sbasis_path.begin(); iter != sbasis_path.end(); ++iter) {
110 sp_svg_write_curve(str, &(*iter));
111 }
112 }
113 }
115 static void sp_svg_write_path(Inkscape::SVG::PathString & str, Geom::Path const & p) {
116 str.moveTo( p.initialPoint()[0], p.initialPoint()[1] );
118 for(Geom::Path::const_iterator cit = p.begin(); cit != p.end_open(); cit++) {
119 sp_svg_write_curve(str, &(*cit));
120 }
122 if (p.closed()) {
123 str.closePath();
124 }
125 }
127 gchar * sp_svg_write_path(Geom::PathVector const &p) {
128 Inkscape::SVG::PathString str;
130 for(Geom::PathVector::const_iterator pit = p.begin(); pit != p.end(); pit++) {
131 sp_svg_write_path(str, *pit);
132 }
134 return g_strdup(str.c_str());
135 }
137 gchar * sp_svg_write_path(Geom::Path const &p) {
138 Inkscape::SVG::PathString str;
140 sp_svg_write_path(str, p);
142 return g_strdup(str.c_str());
143 }
145 /*
146 Local Variables:
147 mode:c++
148 c-file-style:"stroustrup"
149 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
150 indent-tabs-mode:nil
151 fill-column:99
152 End:
153 */
154 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :