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