Code

PathString now makes sure that relative coordinates are based on rounded absolute...
[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 "prefs-utils.h"
18 #include <algorithm>
20 Inkscape::SVG::PathString::PathString() :
21     allow_relative_coordinates(0 != prefs_get_int_attribute("options.svgoutput", "allowrelativecoordinates", 1)),
22     force_repeat_commands(0 != prefs_get_int_attribute("options.svgoutput", "forcerepeatcommands", 0))
23 {}
25 void Inkscape::SVG::PathString::_appendOp(char abs_op, char rel_op) {
26     bool abs_op_repeated = _abs_state.prevop == abs_op && !force_repeat_commands;
27     bool rel_op_repeated = _rel_state.prevop == rel_op && !force_repeat_commands;
28     unsigned int const abs_added_size = abs_op_repeated ? 0 : 2;
29     unsigned int const rel_added_size = rel_op_repeated ? 0 : 2;
30     if ( false && _rel_state.str.size()+2 < _abs_state.str.size()+abs_added_size && allow_relative_coordinates ) {
31         // Copy rel to abs
32         _abs_state = _rel_state;
33         _abs_state.switches++;
34         abs_op_repeated = false;
35         // We do not have to copy abs to rel:
36         //   _rel_state.str.size()+2 < _abs_state.str.size()+abs_added_size
37         //   _rel_state.str.size()+rel_added_size < _abs_state.str.size()+2
38         //   _abs_state.str.size()+2 > _rel_state.str.size()+rel_added_size
39     } else if ( false && _abs_state.str.size()+2 < _rel_state.str.size()+rel_added_size ) {
40         // Copy abs to rel
41         _rel_state = _abs_state;
42         _abs_state.switches++;
43         rel_op_repeated = false;
44     }
45     if ( !abs_op_repeated ) _abs_state.appendOp(abs_op);
46     if ( !rel_op_repeated ) _rel_state.appendOp(rel_op);
47 }
49 void Inkscape::SVG::PathString::State::append(NR::Coord v) {
50     SVGOStringStream os;
51     os << ' ' << v;
52     str.append(os.str());
53 }
55 void Inkscape::SVG::PathString::State::append(NR::Point p) {
56     SVGOStringStream os;
57     os << ' ' << p[NR::X] << ',' << p[NR::Y];
58     str.append(os.str());
59 }
61 void Inkscape::SVG::PathString::State::append(NR::Coord v, NR::Coord &rv) {
62     SVGOStringStream os;
63     os << ' ' << v;
64     str.append(os.str());
65     double c;
66     sscanf(os.str().c_str(), " %lf", &c);
67     rv = c;
68 }
70 void Inkscape::SVG::PathString::State::append(NR::Point p, NR::Point &rp) {
71     SVGOStringStream os;
72     os << ' ' << p[NR::X] << ',' << p[NR::Y];
73     str.append(os.str());
74     double x, y;
75     sscanf(os.str().c_str(), " %lf,%lf", &x, &y);
76     rp[NR::X] = x;
77     rp[NR::Y] = y;
78 }
80 // NOTE: The following two appendRelative methods will not be exact if the total number of digits needed
81 // to represent the difference exceeds the precision of a double. This is not very likely though, and if
82 // it does happen the imprecise value is not likely to be chosen (because it will probably be a lot longer
83 // than the absolute value).
85 void Inkscape::SVG::PathString::State::appendRelative(NR::Coord v, NR::Coord r) {
86     SVGOStringStream os;
87     int precision = (int)os.precision();
88     int digitsBegin = (int)floor(log10(fabs(v-r))); // Position of first digit of difference
89     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
90     os << ' ';
91     if (r == 0) {
92         os.precision(precision);
93         os << v;
94     } else if (v == 0) {
95         os.precision(precision);
96         os << -r;
97     } else if (digitsBegin>digitsEnd) {
98         os.precision(digitsBegin-digitsEnd);
99         os << (v-r);
100     } else {
101         // This assumes the input numbers are already rounded to 'precision' digits
102         os << '0';
103     }
104     str.append(os.str());
107 void Inkscape::SVG::PathString::State::appendRelative(NR::Point p, NR::Point r) {
108     SVGOStringStream os;
109     int precision = (int)os.precision();
110     int digitsBeginX = (int)floor(log10(fabs(p[NR::X]-r[NR::X]))); // Position of first digit of difference
111     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
112     int digitsBeginY = (int)floor(log10(fabs(p[NR::Y]-r[NR::Y]))); // Position of first digit of difference
113     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
114     os << ' ';
115     if (r[NR::X] == 0) {
116         os.precision(precision);
117         os << p[NR::X];
118     } else if (p[NR::X] == 0) {
119         os.precision(precision);
120         os << -r[NR::X];
121     } else if (digitsBeginX>digitsEndX) {
122         os.precision(digitsBeginX-digitsEndX);
123         os << (p[NR::X]-r[NR::X]);
124     } else {
125         // This assumes the input numbers are already rounded to 'precision' digits
126         os << '0';
127     }
128     os << ',';
129     if (r[NR::Y] == 0) {
130         os.precision(precision);
131         os << p[NR::Y];
132     } else if (p[NR::Y] == 0) {
133         os.precision(precision);
134         os << -r[NR::Y];
135     } else if (digitsBeginY>digitsEndY) {
136         os.precision(digitsBeginY-digitsEndY);
137         os << (p[NR::Y]-r[NR::Y]);
138     } else {
139         // This assumes the input numbers are already rounded to 'precision' digits
140         os << '0';
141     }
142     str.append(os.str());
145 /*
146   Local Variables:
147   mode:c++
148   c-file-style:"stroustrup"
149   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
150   indent-tabs-mode:nil
151   fill-column:99
152   End:
153 */
154 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :