Code

Fix for PathString to eliminate use of sscanf (which is locale dependent).
[inkscape.git] / src / svg / path-string.cpp
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 Inkscape::SVG::PathString::PathString() :
22     allow_relative_coordinates(0 != prefs_get_int_attribute("options.svgoutput", "allowrelativecoordinates", 1)),
23     force_repeat_commands(0 != prefs_get_int_attribute("options.svgoutput", "forcerepeatcommands", 0))
24 {}
26 void Inkscape::SVG::PathString::_appendOp(char abs_op, char rel_op) {
27     bool abs_op_repeated = _abs_state.prevop == abs_op && !force_repeat_commands;
28     bool rel_op_repeated = _rel_state.prevop == rel_op && !force_repeat_commands;
29     unsigned int const abs_added_size = abs_op_repeated ? 0 : 2;
30     unsigned int const rel_added_size = rel_op_repeated ? 0 : 2;
31     if ( false && _rel_state.str.size()+2 < _abs_state.str.size()+abs_added_size && allow_relative_coordinates ) {
32         // Copy rel to abs
33         _abs_state = _rel_state;
34         _abs_state.switches++;
35         abs_op_repeated = false;
36         // We do not have to copy abs to rel:
37         //   _rel_state.str.size()+2 < _abs_state.str.size()+abs_added_size
38         //   _rel_state.str.size()+rel_added_size < _abs_state.str.size()+2
39         //   _abs_state.str.size()+2 > _rel_state.str.size()+rel_added_size
40     } else if ( false && _abs_state.str.size()+2 < _rel_state.str.size()+rel_added_size ) {
41         // Copy abs to rel
42         _rel_state = _abs_state;
43         _abs_state.switches++;
44         rel_op_repeated = false;
45     }
46     if ( !abs_op_repeated ) _abs_state.appendOp(abs_op);
47     if ( !rel_op_repeated ) _rel_state.appendOp(rel_op);
48 }
50 void Inkscape::SVG::PathString::State::append(NR::Coord v) {
51     SVGOStringStream os;
52     os << ' ' << v;
53     str.append(os.str());
54 }
56 void Inkscape::SVG::PathString::State::append(NR::Point p) {
57     SVGOStringStream os;
58     os << ' ' << p[NR::X] << ',' << p[NR::Y];
59     str.append(os.str());
60 }
62 void Inkscape::SVG::PathString::State::append(NR::Coord v, NR::Coord &rv) {
63     SVGOStringStream os;
64     os << v;
65     str += ' ';
66     str += os.str();
67     double c;
68     sp_svg_number_read_d(os.str().c_str(), &c);
69     rv = c;
70 }
72 void Inkscape::SVG::PathString::State::append(NR::Point p, NR::Point &rp) {
73     SVGOStringStream osx, osy;
74     osx << p[NR::X];
75     osy << p[NR::Y];
76     str += ' ';
77     str += osx.str();
78     str += ',';
79     str += osy.str();
80     double x, y;
81     sp_svg_number_read_d(osx.str().c_str(), &x);
82     sp_svg_number_read_d(osy.str().c_str(), &y);
83     rp[NR::X] = x;
84     rp[NR::Y] = y;
85 }
87 // NOTE: The following two appendRelative methods will not be exact if the total number of digits needed
88 // to represent the difference exceeds the precision of a double. This is not very likely though, and if
89 // it does happen the imprecise value is not likely to be chosen (because it will probably be a lot longer
90 // than the absolute value).
92 void Inkscape::SVG::PathString::State::appendRelative(NR::Coord v, NR::Coord r) {
93     SVGOStringStream os;
94     int precision = (int)os.precision();
95     int digitsBegin = (int)floor(log10(fabs(v-r))); // Position of first digit of difference
96     int 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
97     os << ' ';
98     if (r == 0) {
99         os.precision(precision);
100         os << v;
101     } else if (v == 0) {
102         os.precision(precision);
103         os << -r;
104     } else if (digitsBegin>digitsEnd) {
105         os.precision(digitsBegin-digitsEnd);
106         os << (v-r);
107     } else {
108         // This assumes the input numbers are already rounded to 'precision' digits
109         os << '0';
110     }
111     str.append(os.str());
114 void Inkscape::SVG::PathString::State::appendRelative(NR::Point p, NR::Point r) {
115     SVGOStringStream os;
116     int precision = (int)os.precision();
117     int digitsBeginX = (int)floor(log10(fabs(p[NR::X]-r[NR::X]))); // Position of first digit of difference
118     int digitsEndX   = (int)floor(log10(std::min(fabs(p[NR::X]),fabs(r[NR::X])))) - precision; // Position just beyond the last significant digit of the smallest (in absolute sense) number
119     int digitsBeginY = (int)floor(log10(fabs(p[NR::Y]-r[NR::Y]))); // Position of first digit of difference
120     int digitsEndY   = (int)floor(log10(std::min(fabs(p[NR::Y]),fabs(r[NR::Y])))) - precision; // Position just beyond the last significant digit of the smallest (in absolute sense) number
121     os << ' ';
122     if (r[NR::X] == 0) {
123         os.precision(precision);
124         os << p[NR::X];
125     } else if (p[NR::X] == 0) {
126         os.precision(precision);
127         os << -r[NR::X];
128     } else if (digitsBeginX>digitsEndX) {
129         os.precision(digitsBeginX-digitsEndX);
130         os << (p[NR::X]-r[NR::X]);
131     } else {
132         // This assumes the input numbers are already rounded to 'precision' digits
133         os << '0';
134     }
135     os << ',';
136     if (r[NR::Y] == 0) {
137         os.precision(precision);
138         os << p[NR::Y];
139     } else if (p[NR::Y] == 0) {
140         os.precision(precision);
141         os << -r[NR::Y];
142     } else if (digitsBeginY>digitsEndY) {
143         os.precision(digitsBeginY-digitsEndY);
144         os << (p[NR::Y]-r[NR::Y]);
145     } else {
146         // This assumes the input numbers are already rounded to 'precision' digits
147         os << '0';
148     }
149     str.append(os.str());
152 /*
153   Local Variables:
154   mode:c++
155   c-file-style:"stroustrup"
156   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
157   indent-tabs-mode:nil
158   fill-column:99
159   End:
160 */
161 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :