From 69ac4cffff595c46b3f8dd2bcceab6bccf6e4581 Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Thu, 15 Aug 2013 21:10:29 +0200 Subject: Add option to write out path data using only relative coordinates (in addition to using only absolute coordinates or using a mixture of absolute and relative coordinates optimized for length). (bzr r12480) --- src/svg/path-string.cpp | 72 ++++++++++++++++++++++++++++++++----------------- src/svg/path-string.h | 24 ++++++++++++----- 2 files changed, 65 insertions(+), 31 deletions(-) (limited to 'src/svg') diff --git a/src/svg/path-string.cpp b/src/svg/path-string.cpp index 61e9c90a2..6dddeadff 100644 --- a/src/svg/path-string.cpp +++ b/src/svg/path-string.cpp @@ -2,6 +2,7 @@ * Inkscape::SVG::PathString - builder for SVG path strings * * Copyright 2008 Jasper van de Gronde + * Copyright 2013 Tavmjong Bah * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -25,44 +26,65 @@ static int const maxprec = 16; int Inkscape::SVG::PathString::numericprecision; int Inkscape::SVG::PathString::minimumexponent; +Inkscape::SVG::PATHSTRING_FORMAT Inkscape::SVG::PathString::format; Inkscape::SVG::PathString::PathString() : - 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(); + format = (PATHSTRING_FORMAT)prefs->getIntLimited("/options/svgoutput/pathstring_format", 1, 0, PATHSTRING_FORMAT_SIZE - 1 ); numericprecision = std::max(minprec,std::min(maxprec, prefs->getInt("/options/svgoutput/numericprecision", 8))); minimumexponent = prefs->getInt("/options/svgoutput/minimumexponent", -8); } +// For absolute and relative paths... the entire path is kept in the "tail". +// For optimized path, at a switch between absolute and relative, add tail to commonbase. 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 ( _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++; - abs_op_repeated = false; - // We do not have to copy abs to rel: - // _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 ( _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++; - rel_op_repeated = false; + + // For absolute and relative paths... do nothing. + switch (format) { + case PATHSTRING_ABSOLUTE: + if ( !abs_op_repeated ) _abs_state.appendOp(abs_op); + break; + case PATHSTRING_RELATIVE: + if ( !rel_op_repeated ) _rel_state.appendOp(rel_op); + break; + case PATHSTRING_OPTIMIZE: + { + unsigned int const abs_added_size = abs_op_repeated ? 0 : 2; + unsigned int const rel_added_size = rel_op_repeated ? 0 : 2; + if ( _rel_state.str.size()+2 < _abs_state.str.size()+abs_added_size ) { + + // Store common prefix + commonbase += _rel_state.str; + _rel_state.str.clear(); + // Copy rel to abs + _abs_state = _rel_state; + _abs_state.switches++; + abs_op_repeated = false; + // We do not have to copy abs to rel: + // _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 ( _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++; + rel_op_repeated = false; + } + if ( !abs_op_repeated ) _abs_state.appendOp(abs_op); + if ( !rel_op_repeated ) _rel_state.appendOp(rel_op); + } + break; + default: + std::cout << "Better not be here!" << std::endl; } - if ( !abs_op_repeated ) _abs_state.appendOp(abs_op); - if ( !rel_op_repeated ) _rel_state.appendOp(rel_op); } void Inkscape::SVG::PathString::State::append(Geom::Coord v) { diff --git a/src/svg/path-string.h b/src/svg/path-string.h index 11018e65c..3a891873d 100644 --- a/src/svg/path-string.h +++ b/src/svg/path-string.h @@ -1,6 +1,7 @@ /* * Copyright 2007 MenTaLguY * Copyright 2008 Jasper van de Gronde + * Copyright 2013 Tavmjong Bah * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -23,6 +24,14 @@ namespace Inkscape { namespace SVG { +// Relative vs. absolute coordinates +enum PATHSTRING_FORMAT { + PATHSTRING_ABSOLUTE, // Use only absolute coordinates + PATHSTRING_RELATIVE, // Use only relative coordinates + PATHSTRING_OPTIMIZE, // Optimize for path string length + PATHSTRING_FORMAT_SIZE +}; + /** * Builder for SVG path strings. */ @@ -38,6 +47,7 @@ public: final.reserve(commonbase.size()+t.size()); final = commonbase; final += tail(); + // std::cout << " final: " << final << std::endl; return final; } @@ -130,12 +140,10 @@ public: } PathString &closePath() { - commonbase += _abs_state.str; - _abs_state.str.clear(); - _rel_state = _abs_state; + _abs_state.appendOp('Z'); _rel_state.appendOp('z'); - _rel_state.switches++; + _current_point = _initial_point; return *this; } @@ -229,9 +237,13 @@ private: // to cause a quadratic time complexity (in the number of characters/operators) std::string commonbase; std::string final; - std::string const &tail() const { return ((_abs_state <= _rel_state || !allow_relative_coordinates) ? _abs_state.str : _rel_state.str); } + std::string const &tail() const { + return ( (format == PATHSTRING_ABSOLUTE) || + (format == PATHSTRING_OPTIMIZE && _abs_state <= _rel_state ) ? + _abs_state.str : _rel_state.str ); + } - bool const allow_relative_coordinates; + static PATHSTRING_FORMAT format; bool const force_repeat_commands; static int numericprecision; static int minimumexponent; -- cgit v1.2.3 From 733c08860b33111126b46fbedb4fc4d744931e9a Mon Sep 17 00:00:00 2001 From: "Johan B. C. Engelen" Date: Thu, 12 Sep 2013 00:01:00 +0200 Subject: partially revert 12471, this fixes bug 1215575, but undoes the fix for the less important bug 1208002. (also improved unit conversion legibility) Fixed bugs: - https://launchpad.net/bugs/1215575 (bzr r12506) --- src/svg/svg-length.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'src/svg') diff --git a/src/svg/svg-length.cpp b/src/svg/svg-length.cpp index ea438e91a..05a3ef4b8 100644 --- a/src/svg/svg-length.cpp +++ b/src/svg/svg-length.cpp @@ -285,6 +285,10 @@ std::vector sp_svg_length_list_read(gchar const *str) static unsigned sp_svg_length_read_lff(gchar const *str, SVGLength::Unit *unit, float *val, float *computed, char **next) { +/* note: this function is sometimes fed a string with several consecutive numbers, e.g. by sp_svg_length_list_read. +So after the number, the string does not necessarily have a \0 or a unit, it might also contain a space or comma and then the next number! +*/ + if (!str) { return 0; } @@ -330,8 +334,6 @@ static unsigned sp_svg_length_read_lff(gchar const *str, SVGLength::Unit *unit, *next = (char *) e + 1; } return 1; - } else if (g_ascii_isspace(e[0])) { - return 0; // spaces are not allowed } else { /* Unitless */ if (unit) { @@ -367,7 +369,7 @@ static unsigned sp_svg_length_read_lff(gchar const *str, SVGLength::Unit *unit, *unit = SVGLength::PT; } if (computed) { - *computed = v * Inkscape::Util::Quantity::convert(1, "pt", "px"); + *computed = Inkscape::Util::Quantity::convert(v, "pt", "px"); } break; case UVAL('p','c'): @@ -375,7 +377,7 @@ static unsigned sp_svg_length_read_lff(gchar const *str, SVGLength::Unit *unit, *unit = SVGLength::PC; } if (computed) { - *computed = v * Inkscape::Util::Quantity::convert(1, "pc", "px"); + *computed = Inkscape::Util::Quantity::convert(v, "pc", "px"); } break; case UVAL('m','m'): @@ -383,7 +385,7 @@ static unsigned sp_svg_length_read_lff(gchar const *str, SVGLength::Unit *unit, *unit = SVGLength::MM; } if (computed) { - *computed = v * Inkscape::Util::Quantity::convert(1, "mm", "px"); + *computed = Inkscape::Util::Quantity::convert(v, "mm", "px"); } break; case UVAL('c','m'): @@ -391,7 +393,7 @@ static unsigned sp_svg_length_read_lff(gchar const *str, SVGLength::Unit *unit, *unit = SVGLength::CM; } if (computed) { - *computed = v * Inkscape::Util::Quantity::convert(1, "cm", "px"); + *computed = Inkscape::Util::Quantity::convert(v, "cm", "px"); } break; case UVAL('i','n'): @@ -399,7 +401,7 @@ static unsigned sp_svg_length_read_lff(gchar const *str, SVGLength::Unit *unit, *unit = SVGLength::INCH; } if (computed) { - *computed = v * Inkscape::Util::Quantity::convert(1, "in", "px"); + *computed = Inkscape::Util::Quantity::convert(v, "in", "px"); } break; case UVAL('f','t'): @@ -407,7 +409,7 @@ static unsigned sp_svg_length_read_lff(gchar const *str, SVGLength::Unit *unit, *unit = SVGLength::FOOT; } if (computed) { - *computed = v * Inkscape::Util::Quantity::convert(1, "ft", "px"); + *computed = Inkscape::Util::Quantity::convert(v, "ft", "px"); } break; case UVAL('e','m'): -- cgit v1.2.3 From fdec8836e698fd195be7d9dffaa98f1ea033fe6c Mon Sep 17 00:00:00 2001 From: Matthew Petroff Date: Thu, 12 Sep 2013 17:08:00 -0400 Subject: Fix handling of SVG lengths with spaces between value and unit (hopefully without breaking things this time) [Bug #1208002]. Fixed bugs: - https://launchpad.net/bugs/1208002 (bzr r12508) --- src/svg/svg-length.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/svg') diff --git a/src/svg/svg-length.cpp b/src/svg/svg-length.cpp index 05a3ef4b8..09662991b 100644 --- a/src/svg/svg-length.cpp +++ b/src/svg/svg-length.cpp @@ -334,6 +334,8 @@ So after the number, the string does not necessarily have a \0 or a unit, it mig *next = (char *) e + 1; } return 1; + } else if (g_ascii_isspace(e[0]) && g_ascii_isalpha(e[1])) { + return 0; // spaces between value and unit are not allowed } else { /* Unitless */ if (unit) { -- cgit v1.2.3 From 626f3946684ea961d126c86b1ea0edda12ae0562 Mon Sep 17 00:00:00 2001 From: Matthew Petroff Date: Thu, 12 Sep 2013 17:14:17 -0400 Subject: Forgot check in last commit. (bzr r12509) --- src/svg/svg-length.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/svg') diff --git a/src/svg/svg-length.cpp b/src/svg/svg-length.cpp index 09662991b..bb6cc5428 100644 --- a/src/svg/svg-length.cpp +++ b/src/svg/svg-length.cpp @@ -334,7 +334,7 @@ So after the number, the string does not necessarily have a \0 or a unit, it mig *next = (char *) e + 1; } return 1; - } else if (g_ascii_isspace(e[0]) && g_ascii_isalpha(e[1])) { + } else if (g_ascii_isspace(e[0]) && e[1] && g_ascii_isalpha(e[1])) { return 0; // spaces between value and unit are not allowed } else { /* Unitless */ -- cgit v1.2.3