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 _start_p = p;
68 _in_path = true;
69 }
70 //TODO: what if _in_path = false?
72 void hlineTo(Coord v) {
73 // check for implicit moveto, like in: "M 1,1 L 2,2 z l 2,2 z"
74 if (!_in_path) {
75 moveTo(_start_p);
76 }
77 _path.template appendNew<HLineSegment>(Point(v, _path.finalPoint()[Y]));
78 }
80 void vlineTo(Coord v) {
81 // check for implicit moveto, like in: "M 1,1 L 2,2 z l 2,2 z"
82 if (!_in_path) {
83 moveTo(_start_p);
84 }
85 _path.template appendNew<VLineSegment>(Point(_path.finalPoint()[X], v));
86 }
88 void lineTo(Point p) {
89 // check for implicit moveto, like in: "M 1,1 L 2,2 z l 2,2 z"
90 if (!_in_path) {
91 moveTo(_start_p);
92 }
93 _path.template appendNew<LineSegment>(p);
94 }
96 void quadTo(Point c, Point p) {
97 // check for implicit moveto, like in: "M 1,1 L 2,2 z l 2,2 z"
98 if (!_in_path) {
99 moveTo(_start_p);
100 }
101 _path.template appendNew<QuadraticBezier>(c, p);
102 }
104 void curveTo(Point c0, Point c1, Point p) {
105 // check for implicit moveto, like in: "M 1,1 L 2,2 z l 2,2 z"
106 if (!_in_path) {
107 moveTo(_start_p);
108 }
109 _path.template appendNew<CubicBezier>(c0, c1, p);
110 }
112 void arcTo(double rx, double ry, double angle,
113 bool large_arc, bool sweep, Point p)
114 {
115 // check for implicit moveto, like in: "M 1,1 L 2,2 z l 2,2 z"
116 if (!_in_path) {
117 moveTo(_start_p);
118 }
119 _path.template appendNew<SVGEllipticalArc>(rx, ry, angle,
120 large_arc, sweep, p);
121 }
123 void closePath() {
124 _path.close();
125 finish();
126 }
128 void finish() {
129 if (_in_path) {
130 _in_path = false;
131 *_out = _path;
132 _path.clear();
133 _path.close(false);
134 }
135 }
137 protected:
138 bool _in_path;
139 OutputIterator _out;
140 Path _path;
141 Point _start_p;
142 };
144 typedef std::back_insert_iterator<std::vector<Path> > iter;
146 class PathBuilder : public SVGPathGenerator<iter> {
147 private:
148 std::vector<Path> _pathset;
149 public:
150 PathBuilder() : SVGPathGenerator<iter>(iter(_pathset)) {}
151 std::vector<Path> const &peek() const {return _pathset;}
152 };
154 }
156 #endif
157 /*
158 Local Variables:
159 mode:c++
160 c-file-style:"stroustrup"
161 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
162 indent-tabs-mode:nil
163 fill-column:99
164 End:
165 */
166 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :