Code

make spcurve::first_point and last_point boost::optional
[inkscape.git] / src / 2geom / svg-path.h
1 /*
2  * callback interface for SVG path data
3  *
4  * Copyright 2007 MenTaLguY <mental@rydia.net>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it either under the terms of the GNU Lesser General Public
8  * License version 2.1 as published by the Free Software Foundation
9  * (the "LGPL") or, at your option, under the terms of the Mozilla
10  * Public License Version 1.1 (the "MPL"). If you do not alter this
11  * notice, a recipient may use your version of this file under either
12  * the MPL or the LGPL.
13  *
14  * You should have received a copy of the LGPL along with this library
15  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  * You should have received a copy of the MPL along with this library
18  * in the file COPYING-MPL-1.1
19  *
20  * The contents of this file are subject to the Mozilla Public License
21  * Version 1.1 (the "License"); you may not use this file except in
22  * compliance with the License. You may obtain a copy of the License at
23  * http://www.mozilla.org/MPL/
24  *
25  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27  * the specific language governing rights and limitations.
28  *
29  */
31 #ifndef SEEN_SVG_PATH_H
32 #define SEEN_SVG_PATH_H
34 #include <2geom/path.h>
35 #include <2geom/curves.h>
36 #include <iterator>
38 namespace Geom {
40 class SVGPathSink {
41 public:
42     virtual void moveTo(Point p) = 0;
43     virtual void hlineTo(Coord v) = 0;
44     virtual void vlineTo(Coord v) = 0;
45     virtual void lineTo(Point p) = 0;
46     virtual void curveTo(Point c0, Point c1, Point p) = 0;
47     virtual void quadTo(Point c, Point p) = 0;
48     virtual void arcTo(double rx, double ry, double angle,
49                        bool large_arc, bool sweep, Point p) = 0;
50     virtual void closePath() = 0;
51     virtual void finish() = 0;
52     virtual ~SVGPathSink() {}
53 };
55 void output_svg_path(Path &path, SVGPathSink &sink);
57 template <typename OutputIterator>
58 class SVGPathGenerator : public SVGPathSink {
59 public:
60     explicit SVGPathGenerator(OutputIterator out)
61     : _in_path(false), _out(out) {}
63     void moveTo(Point p) {
64         finish();
65         _path.start(p);
66         _in_path = true;
67     }
68 //TODO: what if _in_path = false?
70     void hlineTo(Coord v) {
71         _path.template appendNew<HLineSegment>(Point(v, _path.finalPoint()[Y]));
72     }
74     void vlineTo(Coord v) {
75         _path.template appendNew<VLineSegment>(Point(_path.finalPoint()[X], v));
76     }
78     void lineTo(Point p) {
79         _path.template appendNew<LineSegment>(p);
80     }
82     void quadTo(Point c, Point p) {
83         _path.template appendNew<QuadraticBezier>(c, p);
84     }
86     void curveTo(Point c0, Point c1, Point p) {
87         _path.template appendNew<CubicBezier>(c0, c1, p);
88     }
90     void arcTo(double rx, double ry, double angle,
91                bool large_arc, bool sweep, Point p)
92     {
93         _path.template appendNew<SVGEllipticalArc>(rx, ry, angle,
94                                                  large_arc, sweep, p);
95     }
97     void closePath() {
98         _path.close();
99         finish();
100     }
102     void finish() {
103         if (_in_path) {
104             _in_path = false;
105             *_out = _path;
106             _path.clear();
107             _path.close(false);
108         }
109     }
111 protected:
112     bool _in_path;
113     OutputIterator _out;
114     Path _path;
115 };
117 typedef std::back_insert_iterator<std::vector<Path> > iter;
119 class PathBuilder : public SVGPathGenerator<iter> {
120 private:
121     std::vector<Path> _pathset;
122 public:
123     PathBuilder() : SVGPathGenerator<iter>(iter(_pathset)) {}
124     std::vector<Path> const &peek() const {return _pathset;}
125 };
129 #endif
130 /*
131   Local Variables:
132   mode:c++
133   c-file-style:"stroustrup"
134   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
135   indent-tabs-mode:nil
136   fill-column:99
137   End:
138 */
139 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :