Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
path-string.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
/*
5 * Authors: see git history
6 *
7 * Copyright 2008 Jasper van de Gronde <th.v.d.gronde@hccnet.nl>
8 * Copyright 2013 Tavmjong Bah <tavmjong@free.fr>
9 * Copyright (C) 2018 Authors
10 *
11 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
12 */
13
14#include "svg/path-string.h"
15#include "svg/stringstream.h"
16#include "svg/svg.h"
17#include "preferences.h"
18
19// 1<=numericprecision<=16, doubles are only accurate upto (slightly less than) 16 digits (and less than one digit doesn't make sense)
20// Please note that these constants are used to allocate sufficient space to hold serialized numbers
21static int const minprec = 1;
22static int const maxprec = 16;
23
24namespace Inkscape {
25namespace SVG {
26
31{
32 // Load the pathstring configuration from a standard set of preferences
34 _format = (PATHSTRING_FORMAT)prefs->getIntLimited("/options/svgoutput/pathstring_format", 1, 0, PATHSTRING_FORMAT_SIZE - 1 );
35 _force_repeat_commands = !prefs->getBool("/options/svgoutput/disable_optimizations" ) && prefs->getBool("/options/svgoutput/forcerepeatcommands");
36 int precision = std::max<int>(minprec,std::min<int>(maxprec, prefs->getInt("/options/svgoutput/numericprecision", 8)));
37 int minexp = prefs->getInt("/options/svgoutput/minimumexponent", -8);
38 _abs_state = State(precision, minexp);
39 _rel_state = State(precision, minexp);
40}
41
45PathString::PathString(PATHSTRING_FORMAT format, int precision, int minexp, bool force_repeat)
46 : _abs_state(precision, minexp)
47 , _rel_state(precision, minexp)
48 , _format(format)
49 , _force_repeat_commands(force_repeat)
50{
51}
52
53// For absolute and relative paths... the entire path is kept in the "tail".
54// For optimized path, at a switch between absolute and relative, add tail to commonbase.
55void PathString::_appendOp(char abs_op, char rel_op) {
56 bool abs_op_repeated = _abs_state.prevop == abs_op && !_force_repeat_commands;
57 bool rel_op_repeated = _rel_state.prevop == rel_op && !_force_repeat_commands;
58
59 // For absolute and relative paths... do nothing.
60 switch (_format) {
62 if ( !abs_op_repeated ) _abs_state.appendOp(abs_op);
63 break;
65 if ( !rel_op_repeated ) _rel_state.appendOp(rel_op);
66 break;
68 {
69 unsigned int const abs_added_size = abs_op_repeated ? 0 : 2;
70 unsigned int const rel_added_size = rel_op_repeated ? 0 : 2;
71 if ( _rel_state.str.size()+2 < _abs_state.str.size()+abs_added_size ) {
72
73 // Store common prefix
75 _rel_state.str.clear();
76 // Copy rel to abs
79 abs_op_repeated = false;
80 // We do not have to copy abs to rel:
81 // _rel_state.str.size()+2 < _abs_state.str.size()+abs_added_size
82 // _rel_state.str.size()+rel_added_size < _abs_state.str.size()+2
83 // _abs_state.str.size()+2 > _rel_state.str.size()+rel_added_size
84 } else if ( _abs_state.str.size()+2 < _rel_state.str.size()+rel_added_size ) {
85
86 // Store common prefix
88 _abs_state.str.clear();
89 // Copy abs to rel
92 rel_op_repeated = false;
93 }
94 if ( !abs_op_repeated ) _abs_state.appendOp(abs_op);
95 if ( !rel_op_repeated ) _rel_state.appendOp(rel_op);
96 }
97 break;
98 default:
99 std::cerr << "Better not be here!" << std::endl;
100 }
101}
102
107
109 str += ' ';
110 appendNumber(p[Geom::X], _precision, _minexp);
111 str += ',';
112 appendNumber(p[Geom::Y], _precision, _minexp);
113}
114
116 str += ' ';
117 appendNumber(v, rv);
118}
119
121 str += ' ';
122 appendNumber(p[Geom::X], rp[Geom::X]);
123 str += ',';
124 appendNumber(p[Geom::Y], rp[Geom::Y]);
125}
126
127// NOTE: The following appendRelativeCoord function will not be exact if the total number of digits needed
128// to represent the difference exceeds the precision of a double. This is not very likely though, and if
129// it does happen the imprecise value is not likely to be chosen (because it will probably be a lot longer
130// than the absolute value).
131
132// NOTE: This assumes v and r are already rounded (this includes flushing to zero if they are < 10^minexp)
134 int const minexp = _minexp - _precision + 1;
135 int const digitsEnd = (int)floor(log10(std::min(fabs(v),fabs(r)))) - _precision; // Position just beyond the last significant digit of the smallest (in absolute sense) number
136 double const roundeddiff = floor((v-r)*pow(10.,-digitsEnd-1)+.5);
137 int const numDigits = (int)floor(log10(fabs(roundeddiff)))+1; // Number of digits in roundeddiff
138 if (r == 0) {
139 appendNumber(v, _precision, minexp);
140 } else if (v == 0) {
141 appendNumber(-r, _precision, minexp);
142 } else if (numDigits>0) {
143 appendNumber(v-r, numDigits, minexp);
144 } else {
145 // This assumes the input numbers are already rounded to 'precision' digits
146 str += '0';
147 }
148}
149
151 str += ' ';
152 appendRelativeCoord(p[Geom::X], r[Geom::X]);
153 str += ',';
154 appendRelativeCoord(p[Geom::Y], r[Geom::Y]);
155}
156
158 str += ' ';
159 appendRelativeCoord(v, r);
160}
161
162void PathString::State::appendNumber(double v, int precision, int minexp) {
163
164 str.append(sp_svg_number_write_de(v, precision, minexp));
165}
166
167void PathString::State::appendNumber(double v, double &rv) {
168 size_t const oldsize = str.size();
169 appendNumber(v, _precision, _minexp);
170 char* begin_of_num = const_cast<char*>(str.data()+oldsize); // Slightly evil, I know (but std::string should be storing its data in one big block of memory, so...)
171 sp_svg_number_read_d(begin_of_num, &rv);
172}
173
174}}
175
176/*
177 Local Variables:
178 mode:c++
179 c-file-style:"stroustrup"
180 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
181 indent-tabs-mode:nil
182 fill-column:99
183 End:
184*/
185// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Two-dimensional point that doubles as a vector.
Definition point.h:66
Preference storage class.
Definition preferences.h:61
bool getBool(Glib::ustring const &pref_path, bool def=false)
Retrieve a Boolean value.
static Preferences * get()
Access the singleton Preferences object.
int getInt(Glib::ustring const &pref_path, int def=0)
Retrieve an integer.
int getIntLimited(Glib::ustring const &pref_path, int def=0, int min=INT_MIN, int max=INT_MAX)
Retrieve a limited integer.
struct Inkscape::SVG::PathString::State _abs_state
PATHSTRING_FORMAT _format
PathString()
Construct a path string using Inkscape's default preferences.
void _appendOp(char abs_op, char rel_op)
struct Inkscape::SVG::PathString::State _rel_state
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
auto floor(Geom::Rect const &rect)
Definition geom.h:131
Helper class to stream background task notifications as a series of messages.
static int const minprec
static int const maxprec
Inkscape::SVG::PathString - builder for SVG path strings.
Singleton class to access the preferences file in a convenient way.
void appendNumber(double v, int precision, int minexp)
void appendRelativeCoord(Geom::Coord v, Geom::Coord r)
void appendRelative(Geom::Coord v, Geom::Coord r)
std::string sp_svg_number_write_de(double val, unsigned int tprec, int min_exp)
unsigned int sp_svg_number_read_d(gchar const *str, double *val)
TODO: insert short description here.