Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
path-sink.h
Go to the documentation of this file.
1/*
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 */
31
32#ifndef LIB2GEOM_SEEN_PATH_SINK_H
33#define LIB2GEOM_SEEN_PATH_SINK_H
34
35#include <2geom/forward.h>
36#include <2geom/pathvector.h>
37#include <2geom/curves.h>
38#include <iterator>
39
40namespace Geom {
41
42
56class PathSink {
57public:
60 virtual void moveTo(Point const &p) = 0;
62 virtual void lineTo(Point const &p) = 0;
64 virtual void curveTo(Point const &c0, Point const &c1, Point const &p) = 0;
66 virtual void quadTo(Point const &c, Point const &p) = 0;
69 virtual void arcTo(Coord rx, Coord ry, Coord angle,
70 bool large_arc, bool sweep, Point const &p) = 0;
71
73 virtual void closePath() = 0;
79 virtual void flush() = 0;
80 // Get the current point, e.g. where the initial point of the next segment will be.
81 //virtual Point currentPoint() const = 0;
82
86 virtual bool backspace() { return false; }
87
88 // these have a default implementation
89 virtual void feed(Curve const &c, bool moveto_initial = true);
99 virtual void feed(Path const &p);
102 virtual void feed(PathVector const &v);
104 virtual void feed(Rect const &);
106 virtual void feed(Circle const &e);
108 virtual void feed(Ellipse const &e);
109
110 virtual ~PathSink() {}
111};
112
115template <typename OutputIterator>
117public:
118 explicit PathIteratorSink(OutputIterator out)
119 : _in_path(false), _out(out) {}
120
121 void moveTo(Point const &p) override {
122 flush();
123 _path.start(p);
124 _start_p = p;
125 _in_path = true;
126 }
127//TODO: what if _in_path = false?
128
133 bool inPath() const {
134 return _in_path;
135 }
136
137 void lineTo(Point const &p) override {
138 // check for implicit moveto, like in: "M 1,1 L 2,2 z l 2,2 z"
139 if (!_in_path) {
141 }
142 _path.template appendNew<LineSegment>(p);
143 }
144
145 void quadTo(Point const &c, Point const &p) override {
146 // check for implicit moveto, like in: "M 1,1 L 2,2 z l 2,2 z"
147 if (!_in_path) {
149 }
150 _path.template appendNew<QuadraticBezier>(c, p);
151 }
152
153 void curveTo(Point const &c0, Point const &c1, Point const &p) override {
154 // check for implicit moveto, like in: "M 1,1 L 2,2 z l 2,2 z"
155 if (!_in_path) {
157 }
158 _path.template appendNew<CubicBezier>(c0, c1, p);
159 }
160
161 void arcTo(Coord rx, Coord ry, Coord angle,
162 bool large_arc, bool sweep, Point const &p) override
163 {
164 // check for implicit moveto, like in: "M 1,1 L 2,2 z l 2,2 z"
165 if (!_in_path) {
167 }
168 _path.template appendNew<EllipticalArc>(rx, ry, angle,
169 large_arc, sweep, p);
170 }
171
172 bool backspace() override
173 {
174 if (_in_path && _path.size() > 0) {
176 return true;
177 }
178 return false;
179 }
180
181 void append(Path const &other)
182 {
183 if (!_in_path) {
184 moveTo(other.initialPoint());
185 }
186 _path.append(other);
187 }
188
189 void closePath() override {
190 if (_in_path) {
191 _path.close();
192 flush();
193 }
194 }
195
196 void flush() override {
197 if (_in_path) {
198 _in_path = false;
199 *_out++ = _path;
200 _path.clear();
201 }
202 }
203
204 void setStitching(bool s) {
206 }
207
208 using PathSink::feed;
209 void feed(Path const &other) override
210 {
211 flush();
212 *_out++ = other;
213 }
214
215protected:
217 OutputIterator _out;
220};
221
222typedef std::back_insert_iterator<PathVector> SubpathInserter;
223
240
241}
242
243#endif
244/*
245 Local Variables:
246 mode:c++
247 c-file-style:"stroustrup"
248 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
249 indent-tabs-mode:nil
250 fill-column:99
251 End:
252*/
253// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Set of all points at a fixed distance from the center.
Definition circle.h:55
Abstract continuous curve on a plane defined on [0,1].
Definition curve.h:78
Set of points with a constant sum of distances from two foci.
Definition ellipse.h:68
Store paths to a PathVector.
Definition path-sink.h:226
void clear()
Clear the stored path vector.
Definition path-sink.h:238
PathBuilder(PathVector &pv)
Create a builder that outputs to pathvector given by reference.
Definition path-sink.h:233
PathVector const & peek() const
Retrieve the path.
Definition path-sink.h:236
PathVector _pathset
Definition path-sink.h:228
PathBuilder()
Create a builder that outputs to an internal pathvector.
Definition path-sink.h:231
Store paths to an output iterator.
Definition path-sink.h:116
void quadTo(Point const &c, Point const &p) override
Output a cubic Bezier segment.
Definition path-sink.h:145
bool inPath() const
Detect if the builder is in a path and thus will NOT create a new moveTo command when given the next ...
Definition path-sink.h:133
void setStitching(bool s)
Definition path-sink.h:204
void moveTo(Point const &p) override
Move to a different point without creating a segment.
Definition path-sink.h:121
void append(Path const &other)
Definition path-sink.h:181
bool backspace() override
Undo the last segment.
Definition path-sink.h:172
void arcTo(Coord rx, Coord ry, Coord angle, bool large_arc, bool sweep, Point const &p) override
Output an elliptical arc segment.
Definition path-sink.h:161
void lineTo(Point const &p) override
Output a line segment.
Definition path-sink.h:137
PathIteratorSink(OutputIterator out)
Definition path-sink.h:118
void feed(Path const &other) override
Output a subpath.
Definition path-sink.h:209
void flush() override
Flush any internal state of the generator.
Definition path-sink.h:196
void curveTo(Point const &c0, Point const &c1, Point const &p) override
Output a quadratic Bezier segment.
Definition path-sink.h:153
void closePath() override
Close the current path with a line segment.
Definition path-sink.h:189
OutputIterator _out
Definition path-sink.h:217
Callback interface for processing path data.
Definition path-sink.h:56
virtual void arcTo(Coord rx, Coord ry, Coord angle, bool large_arc, bool sweep, Point const &p)=0
Output an elliptical arc segment.
virtual void flush()=0
Flush any internal state of the generator.
virtual ~PathSink()
Definition path-sink.h:110
virtual void lineTo(Point const &p)=0
Output a line segment.
virtual void feed(Curve const &c, bool moveto_initial=true)
Definition path-sink.cpp:39
virtual void quadTo(Point const &c, Point const &p)=0
Output a cubic Bezier segment.
virtual void closePath()=0
Close the current path with a line segment.
virtual void curveTo(Point const &c0, Point const &c1, Point const &p)=0
Output a quadratic Bezier segment.
virtual bool backspace()
Undo the last segment.
Definition path-sink.h:86
virtual void moveTo(Point const &p)=0
Move to a different point without creating a segment.
Sequence of subpaths.
Definition pathvector.h:122
void clear()
Remove all paths from the vector.
Definition pathvector.h:195
Sequence of contiguous curves, aka spline.
Definition path.h:353
void setStitching(bool x)
Enable or disable the throwing of exceptions when stitching discontinuities.
Definition path.h:827
void close(bool closed=true)
Set whether the path is closed.
Definition path.cpp:322
void clear()
Remove all curves from the path.
Definition path.cpp:337
void append(Curve *curve)
Add a new curve to the end of the path.
Definition path.h:750
Point initialPoint() const
Get the first point in the path.
Definition path.h:705
void erase_last()
Definition path.h:700
size_type size() const
Natural size of the path.
Definition path.h:490
void start(Point const &p)
Definition path.cpp:426
Two-dimensional point that doubles as a vector.
Definition point.h:66
Axis aligned, non-empty rectangle.
Definition rect.h:92
Include all curve types.
double c[8][4]
Contains forward declarations of 2geom types.
double Coord
Floating point type used to store coordinates.
Definition coord.h:76
Various utility functions.
Definition affine.h:22
std::back_insert_iterator< PathVector > SubpathInserter
Definition path-sink.h:222
PathVector - a sequence of subpaths.