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 <iterator>
37 namespace Geom {
39 class SVGPathSink {
40 public:
41 virtual void moveTo(Point p) = 0;
42 virtual void hlineTo(Coord v) = 0;
43 virtual void vlineTo(Coord v) = 0;
44 virtual void lineTo(Point p) = 0;
45 virtual void curveTo(Point c0, Point c1, Point p) = 0;
46 virtual void quadTo(Point c, Point p) = 0;
47 virtual void arcTo(double rx, double ry, double angle,
48 bool large_arc, bool sweep, Point p) = 0;
49 virtual void closePath() = 0;
50 virtual void finish() = 0;
51 virtual ~SVGPathSink() {}
52 };
54 void output_svg_path(Path &path, SVGPathSink &sink);
56 template <typename OutputIterator>
57 class SVGPathGenerator : public SVGPathSink {
58 public:
59 explicit SVGPathGenerator(OutputIterator out)
60 : _in_path(false), _out(out) {}
62 void moveTo(Point p) {
63 finish();
64 _path.start(p);
65 _in_path = true;
66 }
67 //TODO: what if _in_path = false?
69 void hlineTo(Coord v) {
70 _path.template appendNew<HLineSegment>(Point(v, _path.finalPoint()[Y]));
71 }
73 void vlineTo(Coord v) {
74 _path.template appendNew<VLineSegment>(Point(_path.finalPoint()[X], v));
75 }
77 void lineTo(Point p) {
78 _path.template appendNew<LineSegment>(p);
79 }
81 void quadTo(Point c, Point p) {
82 _path.template appendNew<QuadraticBezier>(c, p);
83 }
85 void curveTo(Point c0, Point c1, Point p) {
86 _path.template appendNew<CubicBezier>(c0, c1, p);
87 }
89 void arcTo(double rx, double ry, double angle,
90 bool large_arc, bool sweep, Point p)
91 {
92 _path.template appendNew<SVGEllipticalArc>(rx, ry, angle,
93 large_arc, sweep, p);
94 }
96 void closePath() {
97 _path.close();
98 finish();
99 }
101 void finish() {
102 if (_in_path) {
103 _in_path = false;
104 *_out = _path;
105 _path.clear();
106 _path.close(false);
107 }
108 }
110 protected:
111 bool _in_path;
112 OutputIterator _out;
113 Path _path;
114 };
116 typedef std::back_insert_iterator<std::vector<Path> > iter;
118 class PathBuilder : public SVGPathGenerator<iter> {
119 private:
120 std::vector<Path> _pathset;
121 public:
122 PathBuilder() : SVGPathGenerator<iter>(iter(_pathset)) {}
123 std::vector<Path> const &peek() const {return _pathset;}
124 };
126 }
128 #endif
129 /*
130 Local Variables:
131 mode:c++
132 c-file-style:"stroustrup"
133 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
134 indent-tabs-mode:nil
135 fill-column:99
136 End:
137 */
138 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :