Code

Merge from trunk
[inkscape.git] / src / svg / svg-path.cpp
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>
49 #include <2geom/angle.h>
51 /*
52  * Parses the path in str. When an error is found in the pathstring, this method
53  * returns a truncated path up to where the error was found in the pathstring.
54  * Returns an empty PathVector when str==NULL
55  */
56 Geom::PathVector sp_svg_read_pathv(char const * str)
57 {
58     Geom::PathVector pathv;
59     if (!str)
60         return pathv;  // return empty pathvector when str == NULL
63     typedef std::back_insert_iterator<Geom::PathVector> Inserter;
64     Inserter iter(pathv);
65     Geom::SVGPathGenerator<Inserter> generator(iter);
67     try {
68         Geom::parse_svg_path(str, generator);
69     }
70     catch (Geom::SVGPathParseError e) {
71         generator.finish();
72         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));
73     }
75     return pathv;
76 }
78 static void sp_svg_write_curve(Inkscape::SVG::PathString & str, Geom::Curve const * c) {
79     if(Geom::LineSegment const *line_segment = dynamic_cast<Geom::LineSegment const  *>(c)) {
80         // don't serialize stitch segments
81         if (!dynamic_cast<Geom::Path::StitchSegment const *>(c)) {
82             str.lineTo( (*line_segment)[1][0], (*line_segment)[1][1] );
83         }
84     }
85     else if(Geom::QuadraticBezier const *quadratic_bezier = dynamic_cast<Geom::QuadraticBezier const  *>(c)) {
86         str.quadTo( (*quadratic_bezier)[1][0], (*quadratic_bezier)[1][1],
87                     (*quadratic_bezier)[2][0], (*quadratic_bezier)[2][1] );
88     }
89     else if(Geom::CubicBezier const *cubic_bezier = dynamic_cast<Geom::CubicBezier const  *>(c)) {
90         str.curveTo( (*cubic_bezier)[1][0], (*cubic_bezier)[1][1],
91                      (*cubic_bezier)[2][0], (*cubic_bezier)[2][1],
92                      (*cubic_bezier)[3][0], (*cubic_bezier)[3][1] );
93     }
94     else if(Geom::SVGEllipticalArc const *svg_elliptical_arc = dynamic_cast<Geom::SVGEllipticalArc const *>(c)) {
95         str.arcTo( svg_elliptical_arc->ray(0), svg_elliptical_arc->ray(1),
96                    Geom::rad_to_deg(svg_elliptical_arc->rotation_angle()),
97                    svg_elliptical_arc->large_arc_flag(), svg_elliptical_arc->sweep_flag(),
98                    svg_elliptical_arc->finalPoint() );
99     }
100     else if(Geom::HLineSegment const *hline_segment = dynamic_cast<Geom::HLineSegment const  *>(c)) {
101         str.horizontalLineTo( hline_segment->finalPoint()[0] );
102     }
103     else if(Geom::VLineSegment const *vline_segment = dynamic_cast<Geom::VLineSegment const  *>(c)) {
104         str.verticalLineTo( vline_segment->finalPoint()[1] );
105     } else { 
106         //this case handles sbasis as well as all other curve types
107         Geom::Path sbasis_path = Geom::cubicbezierpath_from_sbasis(c->toSBasis(), 0.1);
109         //recurse to convert the new path resulting from the sbasis to svgd
110         for(Geom::Path::iterator iter = sbasis_path.begin(); iter != sbasis_path.end(); ++iter) {
111             sp_svg_write_curve(str, &(*iter));
112         }
113     }
116 static void sp_svg_write_path(Inkscape::SVG::PathString & str, Geom::Path const & p) {
117     str.moveTo( p.initialPoint()[0], p.initialPoint()[1] );
119     for(Geom::Path::const_iterator cit = p.begin(); cit != p.end_open(); cit++) {
120         sp_svg_write_curve(str, &(*cit));
121     }
123     if (p.closed()) {
124         str.closePath();
125     }
128 gchar * sp_svg_write_path(Geom::PathVector const &p) {
129     Inkscape::SVG::PathString str;
131     for(Geom::PathVector::const_iterator pit = p.begin(); pit != p.end(); pit++) {
132         sp_svg_write_path(str, *pit);
133     }
135     return g_strdup(str.c_str());
138 gchar * sp_svg_write_path(Geom::Path const &p) {
139     Inkscape::SVG::PathString str;
141     sp_svg_write_path(str, p);
143     return g_strdup(str.c_str());
146 /*
147   Local Variables:
148   mode:c++
149   c-file-style:"stroustrup"
150   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
151   indent-tabs-mode:nil
152   fill-column:99
153   End:
154 */
155 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :