39d66d74ef5a3ecc926e7330c616a9d79fe7d29d
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 ( _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 ( _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 static void appendCoord(Glib::ustring& str, NR::Coord v, NR::Coord &rv) {
63 Inkscape::SVGOStringStream os;
64 os << v;
65 str += os.str();
66 double c;
67 sp_svg_number_read_d(os.str().c_str(), &c);
68 rv = c;
69 /*{
70 Inkscape::SVGOStringStream ost;
71 ost << rv;
72 if (ost.str()!=os.str()) {
73 FILE* file = fopen("pathstring log.txt","at");
74 fprintf(file, "v: %g, rv: %g\n", v, rv);
75 fclose(file);
76 }
77 }*/
78 }
80 void Inkscape::SVG::PathString::State::append(NR::Point p, NR::Point &rp) {
81 str += ' ';
82 appendCoord(str, p[NR::X], rp[NR::X]);
83 str += ',';
84 appendCoord(str, p[NR::Y], rp[NR::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 static void appendRelativeCoord(Glib::ustring& str, NR::Coord v, NR::Coord r) {
93 Inkscape::SVGOStringStream os;
94 int precision = (int)os.precision();
95 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
96 double roundeddiff = floor((v-r)*pow(10.,-digitsEnd-1)+.5);
97 int numDigits = (int)floor(log10(fabs(roundeddiff)))+1; // Number of digits in roundeddiff
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 (numDigits>0) {
105 os.precision(numDigits);
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());
112 {
113 /*double c;
114 sp_svg_number_read_d(os.str().c_str(), &c);
115 if (fabs((v-r)-c)>5.*pow(10.,digitsEnd)) {
116 FILE* file = fopen("pathstring log.txt","at");
117 fprintf(file, "bad, v: %.9g, r: %.9g, os: %s, c: %.12g, roundeddiff: %.12g, precision: %d, digitsEnd: %d, numDigits: %d\n", v, r, os.str().c_str(), c, roundeddiff, precision, digitsEnd, numDigits);
118 fclose(file);
119 }
120 Inkscape::SVGOStringStream ostr1, ostr2;
121 ostr1 << v;
122 ostr2 << (r+c);
123 if (ostr1.str() != ostr2.str()) {
124 FILE* file = fopen("pathstring log.txt","at");
125 fprintf(file, "bad, v: %.9g, r: %.9g, os: %s, c: %.12g, ostr1: %s, ostr2: %s, roundeddiff: %.12g\n", v, r, os.str().c_str(), c, ostr1.str().c_str(), ostr2.str().c_str(), roundeddiff);
126 fclose(file);
127 }*/
128 /*FILE* file = fopen("pathstring log.txt","at");
129 fprintf(file, "good, v: %.9g, r: %.9g, os: %s, c: %.12g, roundeddiff: %.12g, precision: %d, digitsEnd: %d, numDigits: %d\n", v, r, os.str().c_str(), c, roundeddiff, precision, digitsEnd, numDigits);
130 fclose(file);*/
131 }
132 }
134 void Inkscape::SVG::PathString::State::appendRelative(NR::Point p, NR::Point r) {
135 str += ' ';
136 appendRelativeCoord(str, p[NR::X], r[NR::X]);
137 str += ',';
138 appendRelativeCoord(str, p[NR::Y], r[NR::Y]);
139 }
141 /*
142 Local Variables:
143 mode:c++
144 c-file-style:"stroustrup"
145 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
146 indent-tabs-mode:nil
147 fill-column:99
148 End:
149 */
150 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :