X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fsvg%2Fpath-string.cpp;h=0baaf521ea59b1fbda10628f359c545489d6c07f;hb=ea479a317d3fb2890308a1cc99ee730dd7b76de8;hp=44b474dd246162541d722850834c447d5db0789d;hpb=a51d6261b417a0e5f541cf0d55e8938549c51e80;p=inkscape.git diff --git a/src/svg/path-string.cpp b/src/svg/path-string.cpp index 44b474dd2..0baaf521e 100644 --- a/src/svg/path-string.cpp +++ b/src/svg/path-string.cpp @@ -14,20 +14,36 @@ #include "svg/path-string.h" #include "svg/stringstream.h" -#include "prefs-utils.h" +#include "svg/svg.h" +#include "preferences.h" #include +// 1<=numericprecision<=16, doubles are only accurate upto (slightly less than) 16 digits (and less than one digit doesn't make sense) +// Please note that these constants are used to allocate sufficient space to hold serialized numbers +static int const minprec = 1; +static int const maxprec = 16; + +int Inkscape::SVG::PathString::numericprecision; +int Inkscape::SVG::PathString::minimumexponent; + Inkscape::SVG::PathString::PathString() : - allow_relative_coordinates(0 != prefs_get_int_attribute("options.svgoutput", "allowrelativecoordinates", 1)), - force_repeat_commands(0 != prefs_get_int_attribute("options.svgoutput", "forcerepeatcommands", 0)) -{} + allow_relative_coordinates(Inkscape::Preferences::get()->getBool("/options/svgoutput/allowrelativecoordinates", true)), + force_repeat_commands(Inkscape::Preferences::get()->getBool("/options/svgoutput/forcerepeatcommands")) +{ + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + numericprecision = std::max(minprec,std::min(maxprec, prefs->getInt("/options/svgoutput/numericprecision", 8))); + minimumexponent = prefs->getInt("/options/svgoutput/minimumexponent", -8); +} void Inkscape::SVG::PathString::_appendOp(char abs_op, char rel_op) { bool abs_op_repeated = _abs_state.prevop == abs_op && !force_repeat_commands; bool rel_op_repeated = _rel_state.prevop == rel_op && !force_repeat_commands; unsigned int const abs_added_size = abs_op_repeated ? 0 : 2; unsigned int const rel_added_size = rel_op_repeated ? 0 : 2; - if ( false && _rel_state.str.size()+2 < _abs_state.str.size()+abs_added_size && allow_relative_coordinates ) { + if ( _rel_state.str.size()+2 < _abs_state.str.size()+abs_added_size && allow_relative_coordinates ) { + // Store common prefix + commonbase += _rel_state.str; + _rel_state.str.clear(); // Copy rel to abs _abs_state = _rel_state; _abs_state.switches++; @@ -36,7 +52,10 @@ void Inkscape::SVG::PathString::_appendOp(char abs_op, char rel_op) { // _rel_state.str.size()+2 < _abs_state.str.size()+abs_added_size // _rel_state.str.size()+rel_added_size < _abs_state.str.size()+2 // _abs_state.str.size()+2 > _rel_state.str.size()+rel_added_size - } else if ( false && _abs_state.str.size()+2 < _rel_state.str.size()+rel_added_size ) { + } else if ( _abs_state.str.size()+2 < _rel_state.str.size()+rel_added_size ) { + // Store common prefix + commonbase += _abs_state.str; + _abs_state.str.clear(); // Copy abs to rel _rel_state = _abs_state; _abs_state.switches++; @@ -46,100 +65,79 @@ void Inkscape::SVG::PathString::_appendOp(char abs_op, char rel_op) { if ( !rel_op_repeated ) _rel_state.appendOp(rel_op); } -void Inkscape::SVG::PathString::State::append(NR::Coord v) { - SVGOStringStream os; - os << ' ' << v; - str.append(os.str()); +void Inkscape::SVG::PathString::State::append(Geom::Coord v) { + str += ' '; + appendNumber(v); } -void Inkscape::SVG::PathString::State::append(NR::Point p) { - SVGOStringStream os; - os << ' ' << p[NR::X] << ',' << p[NR::Y]; - str.append(os.str()); +void Inkscape::SVG::PathString::State::append(Geom::Point p) { + str += ' '; + appendNumber(p[Geom::X]); + str += ','; + appendNumber(p[Geom::Y]); } -void Inkscape::SVG::PathString::State::append(NR::Coord v, NR::Coord &rv) { - SVGOStringStream os; - os << ' ' << v; - str.append(os.str()); - double c; - sscanf(os.str().c_str(), " %lf", &c); - rv = c; +void Inkscape::SVG::PathString::State::append(Geom::Coord v, Geom::Coord& rv) { + str += ' '; + appendNumber(v, rv); } -void Inkscape::SVG::PathString::State::append(NR::Point p, NR::Point &rp) { - SVGOStringStream os; - os << ' ' << p[NR::X] << ',' << p[NR::Y]; - str.append(os.str()); - double x, y; - sscanf(os.str().c_str(), " %lf,%lf", &x, &y); - rp[NR::X] = x; - rp[NR::Y] = y; +void Inkscape::SVG::PathString::State::append(Geom::Point p, Geom::Point &rp) { + str += ' '; + appendNumber(p[Geom::X], rp[Geom::X]); + str += ','; + appendNumber(p[Geom::Y], rp[Geom::Y]); } -// NOTE: The following two appendRelative methods will not be exact if the total number of digits needed +// NOTE: The following appendRelativeCoord function will not be exact if the total number of digits needed // to represent the difference exceeds the precision of a double. This is not very likely though, and if // it does happen the imprecise value is not likely to be chosen (because it will probably be a lot longer // than the absolute value). -void Inkscape::SVG::PathString::State::appendRelative(NR::Coord v, NR::Coord r) { - SVGOStringStream os; - int precision = (int)os.precision(); - int digitsBegin = (int)floor(log10(fabs(v-r))); // Position of first digit of difference - 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 - os << ' '; +// NOTE: This assumes v and r are already rounded (this includes flushing to zero if they are < 10^minexp) +void Inkscape::SVG::PathString::State::appendRelativeCoord(Geom::Coord v, Geom::Coord r) { + int const minexp = minimumexponent-numericprecision+1; + 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 + double const roundeddiff = floor((v-r)*pow(10.,-digitsEnd-1)+.5); + int const numDigits = (int)floor(log10(fabs(roundeddiff)))+1; // Number of digits in roundeddiff if (r == 0) { - os.precision(precision); - os << v; + appendNumber(v, numericprecision, minexp); } else if (v == 0) { - os.precision(precision); - os << -r; - } else if (digitsBegin>digitsEnd) { - os.precision(digitsBegin-digitsEnd); - os << (v-r); + appendNumber(-r, numericprecision, minexp); + } else if (numDigits>0) { + appendNumber(v-r, numDigits, minexp); } else { // This assumes the input numbers are already rounded to 'precision' digits - os << '0'; + str += '0'; } - str.append(os.str()); } -void Inkscape::SVG::PathString::State::appendRelative(NR::Point p, NR::Point r) { - SVGOStringStream os; - int precision = (int)os.precision(); - int digitsBeginX = (int)floor(log10(fabs(p[NR::X]-r[NR::X]))); // Position of first digit of difference - 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 - int digitsBeginY = (int)floor(log10(fabs(p[NR::Y]-r[NR::Y]))); // Position of first digit of difference - 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 - os << ' '; - if (r[NR::X] == 0) { - os.precision(precision); - os << p[NR::X]; - } else if (p[NR::X] == 0) { - os.precision(precision); - os << -r[NR::X]; - } else if (digitsBeginX>digitsEndX) { - os.precision(digitsBeginX-digitsEndX); - os << (p[NR::X]-r[NR::X]); - } else { - // This assumes the input numbers are already rounded to 'precision' digits - os << '0'; - } - os << ','; - if (r[NR::Y] == 0) { - os.precision(precision); - os << p[NR::Y]; - } else if (p[NR::Y] == 0) { - os.precision(precision); - os << -r[NR::Y]; - } else if (digitsBeginY>digitsEndY) { - os.precision(digitsBeginY-digitsEndY); - os << (p[NR::Y]-r[NR::Y]); - } else { - // This assumes the input numbers are already rounded to 'precision' digits - os << '0'; - } - str.append(os.str()); +void Inkscape::SVG::PathString::State::appendRelative(Geom::Point p, Geom::Point r) { + str += ' '; + appendRelativeCoord(p[Geom::X], r[Geom::X]); + str += ','; + appendRelativeCoord(p[Geom::Y], r[Geom::Y]); +} + +void Inkscape::SVG::PathString::State::appendRelative(Geom::Coord v, Geom::Coord r) { + str += ' '; + appendRelativeCoord(v, r); +} + +void Inkscape::SVG::PathString::State::appendNumber(double v, int precision, int minexp) { + 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 + size_t const oldsize = str.size(); + str.append(reserve, (char)0); + char* begin_of_num = const_cast(str.data()+oldsize); // Slightly evil, I know (but std::string should be storing its data in one big block of memory, so...) + size_t added = sp_svg_number_write_de(begin_of_num, v, precision, minexp); + str.resize(oldsize+added); // remove any trailing characters +} + +void Inkscape::SVG::PathString::State::appendNumber(double v, double &rv, int precision, int minexp) { + size_t const oldsize = str.size(); + appendNumber(v, precision, minexp); + char* begin_of_num = const_cast(str.data()+oldsize); // Slightly evil, I know (but std::string should be storing its data in one big block of memory, so...) + sp_svg_number_read_d(begin_of_num, &rv); } /*