Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
svg-path-writer.cpp
Go to the documentation of this file.
1/*
4 * Authors:
5 * Krzysztof KosiƄski <tweenk.pl@gmail.com>
6 *
7 * Copyright 2014 Authors
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it either under the terms of the GNU Lesser General Public
11 * License version 2.1 as published by the Free Software Foundation
12 * (the "LGPL") or, at your option, under the terms of the Mozilla
13 * Public License Version 1.1 (the "MPL"). If you do not alter this
14 * notice, a recipient may use your version of this file under either
15 * the MPL or the LGPL.
16 *
17 * You should have received a copy of the LGPL along with this library
18 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * You should have received a copy of the MPL along with this library
21 * in the file COPYING-MPL-1.1
22 *
23 * The contents of this file are subject to the Mozilla Public License
24 * Version 1.1 (the "License"); you may not use this file except in
25 * compliance with the License. You may obtain a copy of the License at
26 * http://www.mozilla.org/MPL/
27 *
28 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
29 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
30 * the specific language governing rights and limitations.
31 */
32
33#include <cmath>
34#include <iomanip>
35#include <2geom/coord.h>
37#include <glib.h>
38
39namespace Geom {
40
41static inline bool is_digit(char c) {
42 return c >= '0' && c <= '9';
43}
44
46 : _epsilon(0)
47 , _precision(-1)
48 , _optimize(false)
49 , _use_shorthands(true)
50 , _command(0)
51{
52 // always use C locale for number formatting
53 _ns.imbue(std::locale::classic());
54 _ns.unsetf(std::ios::floatfield);
55}
56
58{
59 _setCommand('M');
60 _current_pars.push_back(p[X]);
61 _current_pars.push_back(p[Y]);
62
64 if (!_optimize) {
65 flush();
66 }
67}
68
70{
71 // The weird setting of _current is to avoid drift with many almost-aligned segments
72 // The additional conditions ensure that the smaller dimension is rounded to zero
73 bool written = false;
74 if (_use_shorthands) {
75 Point r = _current - p;
76 if (are_near(p[X], _current[X], _epsilon) && std::abs(r[X]) < std::abs(r[Y])) {
77 // emit vlineto
78 _setCommand('V');
79 _current_pars.push_back(p[Y]);
80 _current[Y] = p[Y];
81 written = true;
82 } else if (are_near(p[Y], _current[Y], _epsilon) && std::abs(r[Y]) < std::abs(r[X])) {
83 // emit hlineto
84 _setCommand('H');
85 _current_pars.push_back(p[X]);
86 _current[X] = p[X];
87 written = true;
88 }
89 }
90
91 if (!written) {
92 // emit normal lineto
93 if (_command != 'M' && _command != 'L') {
94 _setCommand('L');
95 }
96 _current_pars.push_back(p[X]);
97 _current_pars.push_back(p[Y]);
98 _current = p;
99 }
100
102 if (!_optimize) {
103 flush();
104 }
105}
106
107void SVGPathWriter::quadTo(Point const &c, Point const &p)
108{
109 bool shorthand = _use_shorthands && are_near(c, _quad_tangent, _epsilon);
110
111 _setCommand(shorthand ? 'T' : 'Q');
112 if (!shorthand) {
113 _current_pars.push_back(c[X]);
114 _current_pars.push_back(c[Y]);
115 }
116 _current_pars.push_back(p[X]);
117 _current_pars.push_back(p[Y]);
118
120 _quad_tangent = p + (p - c);
121 if (!_optimize) {
122 flush();
123 }
124}
125
126void SVGPathWriter::curveTo(Point const &p1, Point const &p2, Point const &p3)
127{
128 bool shorthand = _use_shorthands && are_near(p1, _cubic_tangent, _epsilon);
129
130 _setCommand(shorthand ? 'S' : 'C');
131 if (!shorthand) {
132 _current_pars.push_back(p1[X]);
133 _current_pars.push_back(p1[Y]);
134 }
135 _current_pars.push_back(p2[X]);
136 _current_pars.push_back(p2[Y]);
137 _current_pars.push_back(p3[X]);
138 _current_pars.push_back(p3[Y]);
139
140 _current = _quad_tangent = p3;
141 _cubic_tangent = p3 + (p3 - p2);
142 if (!_optimize) {
143 flush();
144 }
145}
146
147void SVGPathWriter::arcTo(double rx, double ry, double angle,
148 bool large_arc, bool sweep, Point const &p)
149{
150 _setCommand('A');
151 _current_pars.push_back(rx);
152 _current_pars.push_back(ry);
153 _current_pars.push_back(deg_from_rad(angle));
154 _current_pars.push_back(large_arc ? 1. : 0.);
155 _current_pars.push_back(sweep ? 1. : 0.);
156 _current_pars.push_back(p[X]);
157 _current_pars.push_back(p[Y]);
158
160 if (!_optimize) {
161 flush();
162 }
163}
164
166{
167 flush();
168 if (_optimize) {
169 _s << "z";
170 } else {
171 _s << " z";
172 }
174}
175
177{
178 if (_command == 0 || _current_pars.empty()) return;
179
180 if (_optimize) {
181 _s << _command;
182 } else {
183 if (_s.tellp() != 0) {
184 _s << ' ';
185 }
186 _s << _command;
187 }
188
189 char lastchar = _command;
190 bool contained_dot = false;
191
192 for (double _current_par : _current_pars) {
193 // TODO: optimize the use of absolute / relative coords
194 std::string cs = _formatCoord(_current_par);
195
196 // Separator handling logic.
197 // Floating point values can end with a digit or dot
198 // and start with a digit, a plus or minus sign, or a dot.
199 // The following cases require a separator:
200 // * digit-digit
201 // * digit-dot (only if the previous number didn't contain a dot)
202 // * dot-digit
203 if (_optimize) {
204 // C++11: change to front()
205 char firstchar = cs[0];
206 if (is_digit(lastchar)) {
207 if (is_digit(firstchar)) {
208 _s << " ";
209 } else if (firstchar == '.' && !contained_dot) {
210 _s << " ";
211 }
212 } else if (lastchar == '.' && is_digit(firstchar)) {
213 _s << " ";
214 }
215 _s << cs;
216
217 // C++11: change to back()
218 lastchar = cs[cs.length()-1];
219 contained_dot = cs.find('.') != std::string::npos;
220 } else {
221 _s << " " << cs;
222 }
223 }
224 _current_pars.clear();
225 _command = 0;
226}
227
229{
230 _s.clear();
231 _s.str("");
232 _ns.clear();
233 _ns.str("");
234 _command = 0;
235 _current_pars.clear();
236 _current = Point(0,0);
237 _subpath_start = Point(0,0);
238}
239
241{
242 _precision = prec;
243 if (prec < 0) {
244 _epsilon = 0;
245 } else {
246 _epsilon = std::pow(10., -prec);
247 _ns << std::setprecision(_precision);
248 }
249}
250
252{
253 if (_command != 0 && _command != cmd) {
254 flush();
255 }
256 _command = cmd;
257}
258
260{
261 std::string ret;
262 if (_precision < 0) {
263 ret = format_coord_shortest(par);
264 } else {
265 _ns << par;
266 ret = _ns.str();
267 _ns.clear();
268 _ns.str("");
269 }
270 return ret;
271}
272
273
274std::string write_svg_path(PathVector const &pv, int prec, bool optimize, bool shorthands)
275{
276 SVGPathWriter writer;
277 writer.setPrecision(prec);
278 writer.setOptimize(optimize);
279 writer.setUseShorthands(shorthands);
280
281 writer.feed(pv);
282 return writer.str();
283}
284
285} // namespace Geom
286
287/*
288 Local Variables:
289 mode:c++
290 c-file-style:"stroustrup"
291 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
292 indent-tabs-mode:nil
293 fill-column:99
294 End:
295*/
296// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
virtual void feed(Curve const &c, bool moveto_initial=true)
Definition path-sink.cpp:39
Sequence of subpaths.
Definition pathvector.h:122
Two-dimensional point that doubles as a vector.
Definition point.h:66
Serialize paths to SVG path data strings.
void setPrecision(int prec)
Set output precision.
void lineTo(Point const &p) override
Output a line segment.
void quadTo(Point const &c, Point const &p) override
Output a cubic Bezier segment.
void setOptimize(bool opt)
Enable or disable length optimization.
std::ostringstream _s
std::vector< Coord > _current_pars
void flush() override
Flush any internal state of the generator.
void clear()
Clear any path data written so far.
void moveTo(Point const &p) override
Move to a different point without creating a segment.
void curveTo(Point const &c0, Point const &c1, Point const &p) override
Output a quadratic Bezier segment.
std::string _formatCoord(Coord par)
void closePath() override
Close the current path with a line segment.
void arcTo(double rx, double ry, double angle, bool large_arc, bool sweep, Point const &p) override
Output an elliptical arc segment.
void setUseShorthands(bool use)
Enable or disable the use of V, H, T and S commands where possible.
std::string str() const
Retrieve the generated path data string.
void _setCommand(char cmd)
std::ostringstream _ns
Integral and real coordinate types and some basic utilities.
double c[8][4]
double const _precision
double Coord
Floating point type used to store coordinates.
Definition coord.h:76
@ Y
Definition coord.h:48
@ X
Definition coord.h:48
Various utility functions.
Definition affine.h:22
static bool is_digit(char c)
std::string write_svg_path(PathVector const &pv, int prec=-1, bool optimize=false, bool shorthands=true)
std::string format_coord_shortest(Coord x)
Definition coord.cpp:77
bool are_near(Affine const &a1, Affine const &a2, Coord eps=EPSILON)
Path sink which writes an SVG-compatible command string.
Glib::RefPtr< Gtk::Adjustment > optimize