summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 02b211d)
raw | patch | inline | side by side (parent: 02b211d)
author | jaspervdg <jaspervdg@users.sourceforge.net> | |
Tue, 20 May 2008 18:25:46 +0000 (18:25 +0000) | ||
committer | jaspervdg <jaspervdg@users.sourceforge.net> | |
Tue, 20 May 2008 18:25:46 +0000 (18:25 +0000) |
src/svg/path-string.cpp | patch | blob | history | |
src/svg/path-string.h | patch | blob | history |
index 3006508111a02f3b9742e484cfe9e0172930784e..44b474dd246162541d722850834c447d5db0789d 100644 (file)
--- a/src/svg/path-string.cpp
+++ b/src/svg/path-string.cpp
*/
#include "svg/path-string.h"
+#include "svg/stringstream.h"
#include "prefs-utils.h"
+#include <algorithm>
Inkscape::SVG::PathString::PathString() :
allow_relative_coordinates(0 != prefs_get_int_attribute("options.svgoutput", "allowrelativecoordinates", 1)),
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 ) {
+ if ( false && _rel_state.str.size()+2 < _abs_state.str.size()+abs_added_size && allow_relative_coordinates ) {
// Copy rel to abs
_abs_state = _rel_state;
_abs_state.switches++;
// _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 ) {
+ } else if ( false && _abs_state.str.size()+2 < _rel_state.str.size()+rel_added_size ) {
// Copy abs to rel
_rel_state = _abs_state;
_abs_state.switches++;
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(NR::Point p) {
+ SVGOStringStream os;
+ os << ' ' << p[NR::X] << ',' << p[NR::Y];
+ str.append(os.str());
+}
+
+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(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;
+}
+
+// NOTE: The following two appendRelative methods 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 << ' ';
+ if (r == 0) {
+ os.precision(precision);
+ os << v;
+ } else if (v == 0) {
+ os.precision(precision);
+ os << -r;
+ } else if (digitsBegin>digitsEnd) {
+ os.precision(digitsBegin-digitsEnd);
+ os << (v-r);
+ } else {
+ // This assumes the input numbers are already rounded to 'precision' digits
+ os << '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());
+}
+
/*
Local Variables:
mode:c++
diff --git a/src/svg/path-string.h b/src/svg/path-string.h
index bc0628a5abaad5c439601275ccb805eed9e5907c..9f06facd3e1130dbda7c38e27fd935c285402901 100644 (file)
--- a/src/svg/path-string.h
+++ b/src/svg/path-string.h
#include <glibmm/ustring.h>
#include "libnr/nr-point.h"
#include "libnr/nr-point-ops.h"
-#include "svg/stringstream.h"
namespace Inkscape {
PathString &moveTo(NR::Point p) {
_appendOp('M','m');
- _appendPoint(p);
+ _appendPoint(p, true);
- _current_point = _initial_point = p;
+ _initial_point = _current_point;
return *this;
}
PathString &lineTo(NR::Point p) {
_appendOp('L','l');
- _appendPoint(p);
-
- _current_point = p;
+ _appendPoint(p, true);
return *this;
}
PathString &quadTo(NR::Point c, NR::Point p) {
_appendOp('Q','q');
- _appendPoint(c);
- _appendPoint(p);
-
- _current_point = p;
+ _appendPoint(c, false);
+ _appendPoint(p, true);
return *this;
}
PathString &curveTo(NR::Point c0, NR::Point c1, NR::Point p) {
_appendOp('C','c');
- _appendPoint(c0);
- _appendPoint(c1);
- _appendPoint(p);
-
- _current_point = p;
+ _appendPoint(c0, false);
+ _appendPoint(c1, false);
+ _appendPoint(p, true);
return *this;
}
_appendValue(rot);
_appendFlag(large_arc);
_appendFlag(sweep);
- _appendPoint(p);
-
- _current_point = p;
+ _appendPoint(p, true);
return *this;
}
_rel_state.append(p);
}
- void _appendX(NR::Coord x) {
- _abs_state.append(x);
- _rel_state.append(x-_current_point[NR::X]);
+ void _appendX(NR::Coord x, bool sc) {
+ double rx;
+ _abs_state.append(x, rx);
+ _rel_state.appendRelative(rx, _current_point[NR::X]);
+ if (sc) _current_point[NR::X] = rx;
}
- void _appendY(NR::Coord y) {
- _abs_state.append(y);
- _rel_state.append(y-_current_point[NR::Y]);
+ void _appendY(NR::Coord y, bool sc) {
+ double ry;
+ _abs_state.append(y, ry);
+ _rel_state.appendRelative(ry, _current_point[NR::Y]);
+ if (sc) _current_point[NR::Y] = ry;
}
- void _appendPoint(NR::Point p) {
- _abs_state.append(p);
- _rel_state.append(p-_current_point);
+ void _appendPoint(NR::Point p, bool sc) {
+ NR::Point rp;
+ _abs_state.append(p, rp);
+ _rel_state.appendRelative(rp, _current_point);
+ if (sc) _current_point = rp;
}
struct State {
str.append(1, ( flag ? '1' : '0' ));
}
- void append(NR::Coord v) {
- SVGOStringStream os;
- os << ' ' << v;
- str.append(os.str());
- }
-
- void append(NR::Point p) {
- SVGOStringStream os;
- os << ' ' << p[NR::X] << ',' << p[NR::Y];
- str.append(os.str());
- }
+ void append(NR::Coord v);
+ void append(NR::Point v);
+ void append(NR::Coord v, NR::Coord& rv);
+ void append(NR::Point p, NR::Point& rp);
+ void appendRelative(NR::Coord v, NR::Coord r);
+ void appendRelative(NR::Point p, NR::Point r);
bool operator<=(const State& s) const {
if ( str.size() < s.str.size() ) return true;