Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
path-string.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
/*
5 * Authors: see git history
6 *
7 * Copyright 2007 MenTaLguY <mental@rydia.net>
8 * Copyright 2008 Jasper van de Gronde <th.v.d.gronde@hccnet.nl>
9 * Copyright 2013 Tavmjong Bah <tavmjong@free.fr>
10 * Copyright (C) 2014 Authors
11 *
12 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
13 */
14
15#ifndef SEEN_INKSCAPE_SVG_PATH_STRING_H
16#define SEEN_INKSCAPE_SVG_PATH_STRING_H
17
18#include <2geom/point.h>
19#include <cstdio>
20#include <glibmm/ustring.h>
21#include <string>
22
23namespace Inkscape {
24
25namespace SVG {
26
27// Relative vs. absolute coordinates
29 PATHSTRING_ABSOLUTE, // Use only absolute coordinates
30 PATHSTRING_RELATIVE, // Use only relative coordinates
31 PATHSTRING_OPTIMIZE, // Optimize for path string length
33};
34
39public:
40 PathString();
41 PathString(PATHSTRING_FORMAT format, int precision, int minexp, bool force);
42
43 // default copy
44 // default assign
45
46 std::string const &string() {
47 std::string const &t = tail();
48 final.reserve(commonbase.size()+t.size());
49 final = commonbase;
50 final += tail();
51 // std::cout << " final: " << final << std::endl;
52 return final;
53 }
54
55 operator std::string const &() {
56 return string();
57 }
58
59 operator Glib::ustring const () const {
60 return commonbase + tail();
61 }
62
63 char const *c_str() {
64 return string().c_str();
65 }
66
68 return moveTo(Geom::Point(x, y));
69 }
70
72 _appendOp('M','m');
73 _appendPoint(p, true);
74
76 return *this;
77 }
78
80 return lineTo(Geom::Point(x, y));
81 }
82
84 _appendOp('L','l');
85 _appendPoint(p, true);
86 return *this;
87 }
88
90 _appendOp('H','h');
91 _appendX(x, true);
92 return *this;
93 }
94
96 _appendOp('V','v');
97 _appendY(y, true);
98 return *this;
99 }
100
104
106 _appendOp('Q','q');
107 _appendPoint(c, false);
108 _appendPoint(p, true);
109 return *this;
110 }
111
115 {
116 return curveTo(Geom::Point(x0, y0), Geom::Point(x1, y1), Geom::Point(x, y));
117 }
118
120 _appendOp('C','c');
121 _appendPoint(c0, false);
122 _appendPoint(c1, false);
123 _appendPoint(p, true);
124 return *this;
125 }
126
131 bool large_arc, bool sweep,
132 Geom::Point p)
133 {
134 _appendOp('A','a');
136 _appendValue(rot);
137 _appendFlag(large_arc);
138 _appendFlag(sweep);
139 _appendPoint(p, true);
140 return *this;
141 }
142
144
145 _abs_state.appendOp('Z');
146 _rel_state.appendOp('z');
147
149 return *this;
150 }
151
152private:
153
154 void _appendOp(char abs_op, char rel_op);
155
156 void _appendFlag(bool flag) {
157 _abs_state.append(flag);
158 _rel_state.append(flag);
159 }
160
165
170
171 void _appendX(Geom::Coord x, bool sc) {
172 double rx;
173 _abs_state.append(x, rx);
175 if (sc) _current_point[Geom::X] = rx;
176 }
177
178 void _appendY(Geom::Coord y, bool sc) {
179 double ry;
180 _abs_state.append(y, ry);
182 if (sc) _current_point[Geom::Y] = ry;
183 }
184
185 void _appendPoint(Geom::Point p, bool sc) {
186 Geom::Point rp;
187 _abs_state.append(p, rp);
189 if (sc) _current_point = rp;
190 }
191
192 struct State {
193 State(int precision = 8, int minexp = -8) {
194 _precision = precision;
195 _minexp = minexp;
196 }
197
198 void appendOp(char op) {
199 if (prevop != 0) str += ' ';
200 str += op;
201 prevop = ( op == 'M' ? 'L' : op == 'm' ? 'l' : op );
202 }
203
204 void append(bool flag) {
205 str += ' ';
206 str += ( flag ? '1' : '0' );
207 }
208
209 void append(Geom::Coord v);
210 void append(Geom::Point v);
211 void append(Geom::Coord v, Geom::Coord& rv);
212 void append(Geom::Point p, Geom::Point& rp);
215
216 bool operator<=(const State& s) const {
217 if ( str.size() < s.str.size() ) return true;
218 if ( str.size() > s.str.size() ) return false;
219 if ( switches < s.switches ) return true;
220 if ( switches > s.switches ) return false;
221 return true;
222 }
223
224 // Note: changing this to Glib::ustring might cause problems in path-string.cpp because it assumes that
225 // size() returns the size of the string in BYTES (and Glib::ustring::resize is terribly slow)
226 std::string str;
227 unsigned int switches = 0;
228 char prevop = 0;
231
232 private:
233 void appendNumber(double v, int precision, int minexp);
234 void appendNumber(double v, double &rv);
236 } _abs_state, _rel_state; // State with the last operator being an absolute/relative operator
237
240
241 // If both states have a common prefix it is stored here.
242 // Separating out the common prefix prevents repeated copying between the states
243 // to cause a quadratic time complexity (in the number of characters/operators)
244 std::string commonbase;
245 std::string final;
246 std::string const &tail() const {
247 return ( (_format == PATHSTRING_ABSOLUTE) ||
250 }
251
254};
255
256}
257
258}
259
260#endif
261/*
262 Local Variables:
263 mode:c++
264 c-file-style:"stroustrup"
265 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
266 indent-tabs-mode:nil
267 fill-column:99
268 End:
269*/
270// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Cartesian point / 2D vector and related operations.
Two-dimensional point that doubles as a vector.
Definition point.h:66
Builder for SVG path strings.
Definition path-string.h:38
std::string const & tail() const
void _appendX(Geom::Coord x, bool sc)
void _appendY(Geom::Coord y, bool sc)
PathString & curveTo(Geom::Coord x0, Geom::Coord y0, Geom::Coord x1, Geom::Coord y1, Geom::Coord x, Geom::Coord y)
std::string const & string()
Definition path-string.h:46
PathString & lineTo(Geom::Point p)
Definition path-string.h:83
struct Inkscape::SVG::PathString::State _abs_state
PathString & moveTo(Geom::Coord x, Geom::Coord y)
Definition path-string.h:67
PATHSTRING_FORMAT _format
PathString & verticalLineTo(Geom::Coord y)
Definition path-string.h:95
PathString & arcTo(Geom::Coord rx, Geom::Coord ry, Geom::Coord rot, bool large_arc, bool sweep, Geom::Point p)
PathString & lineTo(Geom::Coord x, Geom::Coord y)
Definition path-string.h:79
PathString()
Construct a path string using Inkscape's default preferences.
PathString & quadTo(Geom::Coord cx, Geom::Coord cy, Geom::Coord x, Geom::Coord y)
PathString & moveTo(Geom::Point p)
Definition path-string.h:71
PathString & horizontalLineTo(Geom::Coord x)
Definition path-string.h:89
void _appendPoint(Geom::Point p, bool sc)
void _appendValue(Geom::Point p)
PathString & curveTo(Geom::Point c0, Geom::Point c1, Geom::Point p)
void _appendFlag(bool flag)
PathString & quadTo(Geom::Point c, Geom::Point p)
void _appendOp(char abs_op, char rel_op)
struct Inkscape::SVG::PathString::State _rel_state
void _appendValue(Geom::Coord v)
double c[8][4]
double Coord
Floating point type used to store coordinates.
Definition coord.h:76
@ Y
Definition coord.h:48
@ X
Definition coord.h:48
Helper class to stream background task notifications as a series of messages.
void appendNumber(double v, int precision, int minexp)
void appendRelativeCoord(Geom::Coord v, Geom::Coord r)
bool operator<=(const State &s) const
State(int precision=8, int minexp=-8)
void appendRelative(Geom::Coord v, Geom::Coord r)