Code

enable writing "H/h" and "V/v" to SVG !
[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 ( _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 void Inkscape::SVG::PathString::State::append(NR::Coord v, NR::Coord& rv) {
88     str += ' ';
89     appendCoord(str, v, rv);
90 }
92 // NOTE: The following two appendRelative methods will not be exact if the total number of digits needed
93 // to represent the difference exceeds the precision of a double. This is not very likely though, and if
94 // it does happen the imprecise value is not likely to be chosen (because it will probably be a lot longer
95 // than the absolute value).
97 static void appendRelativeCoord(Glib::ustring& str, NR::Coord v, NR::Coord r) {
98     Inkscape::SVGOStringStream os;
99     int precision = (int)os.precision();
100     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
101     double roundeddiff = floor((v-r)*pow(10.,-digitsEnd-1)+.5);
102     int numDigits = (int)floor(log10(fabs(roundeddiff)))+1; // Number of digits in roundeddiff
103     if (r == 0) {
104         os.precision(precision);
105         os << v;
106     } else if (v == 0) {
107         os.precision(precision);
108         os << -r;
109     } else if (numDigits>0) {
110         os.precision(numDigits);
111         os << (v-r);
112     } else {
113         // This assumes the input numbers are already rounded to 'precision' digits
114         os << '0';
115     }
116     str.append(os.str());
117     {
118         /*double c;
119         sp_svg_number_read_d(os.str().c_str(), &c);
120         if (fabs((v-r)-c)>5.*pow(10.,digitsEnd)) {
121             FILE* file = fopen("pathstring log.txt","at");
122             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);
123             fclose(file);
124         }
125         Inkscape::SVGOStringStream ostr1, ostr2;
126         ostr1 << v;
127         ostr2 << (r+c);
128         if (ostr1.str() != ostr2.str()) {
129             FILE* file = fopen("pathstring log.txt","at");
130             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);
131             fclose(file);
132         }*/
133         /*FILE* file = fopen("pathstring log.txt","at");
134         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);
135         fclose(file);*/
136     }
139 void Inkscape::SVG::PathString::State::appendRelative(NR::Point p, NR::Point r) {
140     str += ' ';
141     appendRelativeCoord(str, p[NR::X], r[NR::X]);
142     str += ',';
143     appendRelativeCoord(str, p[NR::Y], r[NR::Y]);
146 void Inkscape::SVG::PathString::State::appendRelative(NR::Coord v, NR::Coord r) {
147     str += ' ';
148     appendRelativeCoord(str, v, r);
151 /*
152   Local Variables:
153   mode:c++
154   c-file-style:"stroustrup"
155   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
156   indent-tabs-mode:nil
157   fill-column:99
158   End:
159 */
160 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :