6f6a1f20f4a2b66545058a2cbc21b9bdcb78f882
1 /*
2 * Inkscape::SVG::PathString - builder for SVG path strings
3 *
4 * Copyright 2008 Jasper van de Gronde <th.v.d.gronde@hccnet.nl>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * See the file COPYING for details.
12 *
13 */
15 #include "svg/path-string.h"
16 #include "svg/stringstream.h"
17 #include "svg/svg.h"
18 #include "prefs-utils.h"
19 #include <algorithm>
21 // 1<=numericprecision<=16, doubles are only accurate upto (slightly less than) 16 digits (and less than one digit doesn't make sense)
22 // Please note that these constants are used to allocate sufficient space to hold serialized numbers
23 static int const minprec = 1;
24 static int const maxprec = 16;
26 int Inkscape::SVG::PathString::numericprecision;
27 int Inkscape::SVG::PathString::minimumexponent;
29 Inkscape::SVG::PathString::PathString() :
30 allow_relative_coordinates(0 != prefs_get_int_attribute("options.svgoutput", "allowrelativecoordinates", 1)),
31 force_repeat_commands(0 != prefs_get_int_attribute("options.svgoutput", "forcerepeatcommands", 0))
32 {
33 numericprecision = std::max<int>(minprec,std::min<int>(maxprec,prefs_get_int_attribute("options.svgoutput", "numericprecision", 8)));
34 minimumexponent = prefs_get_int_attribute("options.svgoutput", "minimumexponent", -8);
35 }
37 void Inkscape::SVG::PathString::_appendOp(char abs_op, char rel_op) {
38 bool abs_op_repeated = _abs_state.prevop == abs_op && !force_repeat_commands;
39 bool rel_op_repeated = _rel_state.prevop == rel_op && !force_repeat_commands;
40 unsigned int const abs_added_size = abs_op_repeated ? 0 : 2;
41 unsigned int const rel_added_size = rel_op_repeated ? 0 : 2;
42 if ( _rel_state.str.size()+2 < _abs_state.str.size()+abs_added_size && allow_relative_coordinates ) {
43 // Store common prefix
44 commonbase += _rel_state.str;
45 _rel_state.str.clear();
46 // Copy rel to abs
47 _abs_state = _rel_state;
48 _abs_state.switches++;
49 abs_op_repeated = false;
50 // We do not have to copy abs to rel:
51 // _rel_state.str.size()+2 < _abs_state.str.size()+abs_added_size
52 // _rel_state.str.size()+rel_added_size < _abs_state.str.size()+2
53 // _abs_state.str.size()+2 > _rel_state.str.size()+rel_added_size
54 } else if ( _abs_state.str.size()+2 < _rel_state.str.size()+rel_added_size ) {
55 // Store common prefix
56 commonbase += _abs_state.str;
57 _abs_state.str.clear();
58 // Copy abs to rel
59 _rel_state = _abs_state;
60 _abs_state.switches++;
61 rel_op_repeated = false;
62 }
63 if ( !abs_op_repeated ) _abs_state.appendOp(abs_op);
64 if ( !rel_op_repeated ) _rel_state.appendOp(rel_op);
65 }
67 void Inkscape::SVG::PathString::State::append(NR::Coord v) {
68 str += ' ';
69 appendNumber(v);
70 }
72 void Inkscape::SVG::PathString::State::append(NR::Point p) {
73 str += ' ';
74 appendNumber(p[NR::X]);
75 str += ',';
76 appendNumber(p[NR::Y]);
77 }
79 void Inkscape::SVG::PathString::State::append(NR::Coord v, NR::Coord& rv) {
80 str += ' ';
81 appendNumber(v, rv);
82 }
84 void Inkscape::SVG::PathString::State::append(NR::Point p, NR::Point &rp) {
85 str += ' ';
86 appendNumber(p[NR::X], rp[NR::X]);
87 str += ',';
88 appendNumber(p[NR::Y], rp[NR::Y]);
89 }
91 // NOTE: The following appendRelativeCoord function will not be exact if the total number of digits needed
92 // to represent the difference exceeds the precision of a double. This is not very likely though, and if
93 // it does happen the imprecise value is not likely to be chosen (because it will probably be a lot longer
94 // than the absolute value).
96 // NOTE: This assumes v and r are already rounded (this includes flushing to zero if they are < 10^minexp)
97 void Inkscape::SVG::PathString::State::appendRelativeCoord(NR::Coord v, NR::Coord r) {
98 int const minexp = minimumexponent-numericprecision+1;
99 int const digitsEnd = (int)floor(log10(std::min(fabs(v),fabs(r)))) - numericprecision; // Position just beyond the last significant digit of the smallest (in absolute sense) number
100 double const roundeddiff = floor((v-r)*pow(10.,-digitsEnd-1)+.5);
101 int const numDigits = (int)floor(log10(fabs(roundeddiff)))+1; // Number of digits in roundeddiff
102 if (r == 0) {
103 appendNumber(v, numericprecision, minexp);
104 } else if (v == 0) {
105 appendNumber(-r, numericprecision, minexp);
106 } else if (numDigits>0) {
107 appendNumber(v-r, numDigits, minexp);
108 } else {
109 // This assumes the input numbers are already rounded to 'precision' digits
110 str += '0';
111 }
112 }
114 void Inkscape::SVG::PathString::State::appendRelative(NR::Point p, NR::Point r) {
115 str += ' ';
116 appendRelativeCoord(p[NR::X], r[NR::X]);
117 str += ',';
118 appendRelativeCoord(p[NR::Y], r[NR::Y]);
119 }
121 void Inkscape::SVG::PathString::State::appendRelative(NR::Coord v, NR::Coord r) {
122 str += ' ';
123 appendRelativeCoord(v, r);
124 }
126 void Inkscape::SVG::PathString::State::appendNumber(double v, int precision, int minexp) {
127 size_t const reserve = precision+1+1+1+1+3; // Just large enough to hold the maximum number of digits plus a sign, a period, the letter 'e', another sign and three digits for the exponent
128 size_t const oldsize = str.size();
129 str.append(reserve, (char)0);
130 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...)
131 size_t added = sp_svg_number_write_de(begin_of_num, v, precision, minexp);
132 str.resize(oldsize+added); // remove any trailing characters
133 }
135 void Inkscape::SVG::PathString::State::appendNumber(double v, double &rv, int precision, int minexp) {
136 size_t const oldsize = str.size();
137 appendNumber(v, precision, minexp);
138 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...)
139 sp_svg_number_read_d(begin_of_num, &rv);
140 }
142 /*
143 Local Variables:
144 mode:c++
145 c-file-style:"stroustrup"
146 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
147 indent-tabs-mode:nil
148 fill-column:99
149 End:
150 */
151 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :