svg-path-test.h revision 9eb886f0efdd076022ef171278e8475a580a1e91
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm#include <cxxtest/TestSuite.h>
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm#include "libnr/n-art-bpath.h"
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm#include "svg/svg.h"
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm#include "2geom/coord.h"
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm#include "prefs-utils.h"
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm#include "streq.h"
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm#include <string>
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm#include <vector>
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm#include <glib/gmem.h>
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrmclass SvgPathTest : public CxxTest::TestSuite
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm{
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrmprivate:
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm std::vector<std::string> rectanglesAbsoluteClosed;
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm std::vector<std::string> rectanglesRelativeClosed;
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm std::vector<std::string> rectanglesAbsoluteOpen;
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm std::vector<std::string> rectanglesRelativeOpen;
43f848b15ee40a6c108ae9d965cf06f055ecb7a5johanengelen NArtBpath rectangleBpath[5+1];
364d9045bfd158352b8040721b3e2e2602e921eatheAdibpublic:
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm SvgPathTest() {
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm // Lots of ways to define the same rectangle
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm rectanglesAbsoluteClosed.push_back("M 1,2 L 4,2 L 4,8 L 1,8 L 1,2 Z");
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm rectanglesAbsoluteClosed.push_back("M 1,2 L 4,2 L 4,8 L 1,8 z");
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm rectanglesAbsoluteClosed.push_back("M 1,2 4,2 4,8 1,8 z");
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm rectanglesAbsoluteClosed.push_back("M 1,2 H 4 V 8 H 1 z");
afb717c6e7d29c86c404b6627855c438d6dc2c0fjohanengelen rectanglesRelativeClosed.push_back("m 1,2 l 3,0 l 0,6 l -3,0 z");
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm rectanglesRelativeClosed.push_back("m 1,2 3,0 0,6 -3,0 z");
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm rectanglesRelativeClosed.push_back("m 1,2 h 3 v 6 h -3 z");
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm rectanglesAbsoluteOpen.push_back("M 1,2 L 4,2 L 4,8 L 1,8 L 1,2");
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm rectanglesAbsoluteOpen.push_back("M 1,2 4,2 4,8 1,8 1,2");
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm rectanglesAbsoluteOpen.push_back("M 1,2 H 4 V 8 H 1 V 2");
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm rectanglesRelativeOpen.push_back("m 1,2 l 3,0 l 0,6 l -3,0 l 0,-6");
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm rectanglesRelativeOpen.push_back("m 1,2 3,0 0,6 -3,0 0,-6");
62d835b4bbb0f1f046e30d9b67f8e9517cc6175cjohanengelen rectanglesRelativeOpen.push_back("m 1,2 h 3 v 6 h -3 v -6");
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm rectangleBpath[0].code = NR_MOVETO;
1673045848db9654bcb07a81fad34afb4b0623d1johanengelen rectangleBpath[0].x3 = 1;
3cc77bbe681dd68bde92a2c4796cd5020aecb214johanengelen rectangleBpath[0].y3 = 2;
afb717c6e7d29c86c404b6627855c438d6dc2c0fjohanengelen rectangleBpath[1].code = NR_LINETO;
c0cd5511d3b975ebe07d019c1f5528108725e438johanengelen rectangleBpath[1].x3 = 4;
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm rectangleBpath[1].y3 = 2;
94f2e710e9cc2e0eb14c8d73abfe77d60f961db2johanengelen rectangleBpath[2].code = NR_LINETO;
94f2e710e9cc2e0eb14c8d73abfe77d60f961db2johanengelen rectangleBpath[2].x3 = 4;
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm rectangleBpath[2].y3 = 8;
94f2e710e9cc2e0eb14c8d73abfe77d60f961db2johanengelen rectangleBpath[3].code = NR_LINETO;
9bddaf7172a6bb788ccae3ba5a30599d63acf424johanengelen rectangleBpath[3].x3 = 1;
94f2e710e9cc2e0eb14c8d73abfe77d60f961db2johanengelen rectangleBpath[3].y3 = 8;
94f2e710e9cc2e0eb14c8d73abfe77d60f961db2johanengelen rectangleBpath[4].code = NR_LINETO;
42e99769805c14a5cc01c805faa3c3b03f9dd1c0johanengelen rectangleBpath[4].x3 = 1;
d431763a9ec8059aa4962688de8144319969fb0fjohanengelen rectangleBpath[4].y3 = 2;
d431763a9ec8059aa4962688de8144319969fb0fjohanengelen rectangleBpath[5].code = NR_END;
c874d2ee5e49a890191f12deb8846ca099359e1ecilix // TODO: Also test some (smooth) cubic/quadratic beziers and elliptical arcs
fb5a72174252e0e79107dcad3bf5a2bbd73e349cjohanengelen }
d37634d73670180f99a3e0ea583621373d90ec4fJohan Engelen
bdd7add6c064afee52d2eabeaa18f745430e5a4djohanengelen// createSuite and destroySuite get us per-suite setup and teardown
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm// without us having to worry about static initialization order, etc.
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm static SvgPathTest *createSuite() { return new SvgPathTest(); }
c776c7f3860f657ea9db62d62e9da98df09b0c70johanengelen static void destroySuite( SvgPathTest *suite ) { delete suite; }
c776c7f3860f657ea9db62d62e9da98df09b0c70johanengelen
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm void testReadRectanglesAbsoluteClosed()
390745fa4524b55aad12d5d2d656cd73e75a0bc0cilix {
390745fa4524b55aad12d5d2d656cd73e75a0bc0cilix rectangleBpath[0].code = NR_MOVETO;
390745fa4524b55aad12d5d2d656cd73e75a0bc0cilix for(size_t i=0; i<rectanglesAbsoluteClosed.size(); i++) {
afb717c6e7d29c86c404b6627855c438d6dc2c0fjohanengelen NArtBpath * bpath = sp_svg_read_path(rectanglesAbsoluteClosed[i].c_str());
1673045848db9654bcb07a81fad34afb4b0623d1johanengelen TS_ASSERT(bpathEqual(bpath,rectangleBpath));
1673045848db9654bcb07a81fad34afb4b0623d1johanengelen g_free(bpath);
1673045848db9654bcb07a81fad34afb4b0623d1johanengelen }
1673045848db9654bcb07a81fad34afb4b0623d1johanengelen }
1673045848db9654bcb07a81fad34afb4b0623d1johanengelen
1673045848db9654bcb07a81fad34afb4b0623d1johanengelen void testReadRectanglesRelativeClosed()
442e92e07cfe50f73dda0d06638025a65e23a956johanengelen {
43f848b15ee40a6c108ae9d965cf06f055ecb7a5johanengelen rectangleBpath[0].code = NR_MOVETO;
43f848b15ee40a6c108ae9d965cf06f055ecb7a5johanengelen for(size_t i=0; i<rectanglesRelativeClosed.size(); i++) {
43f848b15ee40a6c108ae9d965cf06f055ecb7a5johanengelen NArtBpath * bpath = sp_svg_read_path(rectanglesRelativeClosed[i].c_str());
43f848b15ee40a6c108ae9d965cf06f055ecb7a5johanengelen TS_ASSERT(bpathEqual(bpath,rectangleBpath));
43f848b15ee40a6c108ae9d965cf06f055ecb7a5johanengelen g_free(bpath);
43f848b15ee40a6c108ae9d965cf06f055ecb7a5johanengelen }
43f848b15ee40a6c108ae9d965cf06f055ecb7a5johanengelen }
43f848b15ee40a6c108ae9d965cf06f055ecb7a5johanengelen
43f848b15ee40a6c108ae9d965cf06f055ecb7a5johanengelen void testReadRectanglesAbsoluteOpen()
43f848b15ee40a6c108ae9d965cf06f055ecb7a5johanengelen {
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm rectangleBpath[0].code = NR_MOVETO_OPEN;
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm for(size_t i=0; i<rectanglesAbsoluteOpen.size(); i++) {
539b02bd98b6985f91ccbf7041f88fdf93061531johanengelen NArtBpath * bpath = sp_svg_read_path(rectanglesAbsoluteOpen[i].c_str());
43f848b15ee40a6c108ae9d965cf06f055ecb7a5johanengelen TS_ASSERT(bpathEqual(bpath,rectangleBpath));
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm g_free(bpath);
c776c7f3860f657ea9db62d62e9da98df09b0c70johanengelen }
c776c7f3860f657ea9db62d62e9da98df09b0c70johanengelen }
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm
afb717c6e7d29c86c404b6627855c438d6dc2c0fjohanengelen void testReadRectanglesRelativeOpen()
afb717c6e7d29c86c404b6627855c438d6dc2c0fjohanengelen {
afb717c6e7d29c86c404b6627855c438d6dc2c0fjohanengelen rectangleBpath[0].code = NR_MOVETO_OPEN;
afb717c6e7d29c86c404b6627855c438d6dc2c0fjohanengelen for(size_t i=0; i<rectanglesRelativeOpen.size(); i++) {
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm NArtBpath * bpath = sp_svg_read_path(rectanglesRelativeOpen[i].c_str());
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm TS_ASSERT(bpathEqual(bpath,rectangleBpath));
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm g_free(bpath);
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm }
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm }
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm void testReadConcatenatedPaths()
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm {
NArtBpath bpath_good[4*5+1];
for(size_t i=0; i<4; i++) {
memcpy(bpath_good+i*5,rectangleBpath,sizeof(rectangleBpath[0])*5);
}
bpath_good[0*5].code = NR_MOVETO;
bpath_good[1*5].code = NR_MOVETO_OPEN;
bpath_good[2*5].code = NR_MOVETO;
bpath_good[3*5].code = NR_MOVETO_OPEN;
bpath_good[4*5].code = NR_END;
for(size_t i=0; i<5; i++) {
bpath_good[1*5+i].x3 += bpath_good[0*5+4].x3;
bpath_good[1*5+i].y3 += bpath_good[0*5+4].y3;
}
for(size_t i=0; i<5; i++) {
bpath_good[2*5+i].x3 += bpath_good[1*5+4].x3;
bpath_good[2*5+i].y3 += bpath_good[1*5+4].y3;
}
std::string path_str = rectanglesAbsoluteClosed[0] + rectanglesRelativeOpen[0] + rectanglesRelativeClosed[0] + rectanglesAbsoluteOpen[0];
NArtBpath * bpath = sp_svg_read_path(path_str.c_str());
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
void testReadZeroLengthSubpaths() {
// Per the SVG 1.1 specification (section F5) zero-length subpaths are relevant
NArtBpath bpath_good[8+1];
bpath_good[0].code = NR_MOVETO_OPEN;
bpath_good[0].x3 = bpath_good[0].y3 = 0;
bpath_good[1].code = NR_MOVETO_OPEN;
bpath_good[1].x3 = bpath_good[1].y3 = 1;
bpath_good[2].code = NR_LINETO;
bpath_good[2].x3 = bpath_good[2].y3 = 2;
bpath_good[3].code = NR_MOVETO;
bpath_good[3].x3 = bpath_good[3].y3 = 3;
bpath_good[4].code = NR_MOVETO;
bpath_good[4].x3 = bpath_good[4].y3 = 4;
bpath_good[5].code = NR_LINETO;
bpath_good[5].x3 = bpath_good[5].y3 = 5;
bpath_good[6].code = NR_LINETO;
bpath_good[6].x3 = bpath_good[6].y3 = 4;
bpath_good[7].code = NR_MOVETO_OPEN;
bpath_good[7].x3 = bpath_good[7].y3 = 6;
bpath_good[8].code = NR_END;
{ // Test absolute version
char const * path_str = "M 0,0 M 1,1 L 2,2 M 3,3 z M 4,4 L 5,5 z M 6,6";
NArtBpath * bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
{ // Test relative version
char const * path_str = "m 0,0 m 1,1 l 1,1 m 1,1 z m 1,1 l 1,1 z m 2,2";
NArtBpath * bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
}
void testReadImplicitMoveto() {
NArtBpath bpath_good[6+1];
bpath_good[0].code = NR_MOVETO;
bpath_good[0].x3 = bpath_good[0].y3 = 1;
bpath_good[1].code = NR_LINETO;
bpath_good[1].x3 = bpath_good[1].y3 = 2;
bpath_good[2].code = NR_LINETO;
bpath_good[2].x3 = bpath_good[2].y3 = 1;
bpath_good[3].code = NR_MOVETO;
bpath_good[3].x3 = bpath_good[3].y3 = 1;
bpath_good[4].code = NR_LINETO;
bpath_good[4].x3 = bpath_good[4].y3 = 3;
bpath_good[5].code = NR_LINETO;
bpath_good[5].x3 = bpath_good[5].y3 = 1;
bpath_good[6].code = NR_END;
{ // Test absolute version
char const * path_str = "M 1,1 L 2,2 z L 3,3 z";
NArtBpath * bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
{ // Test relative version
char const * path_str = "M 1,1 L 2,2 z L 3,3 z";
NArtBpath * bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
}
void testReadFloatingPoint() {
NArtBpath bpath_good[5+1];
bpath_good[0].code = NR_MOVETO;
bpath_good[0].x3 = .01;
bpath_good[0].y3 = .02;
bpath_good[1].code = NR_LINETO;
bpath_good[1].x3 = .04;
bpath_good[1].y3 = .02;
bpath_good[2].code = NR_LINETO;
bpath_good[2].x3 = 1.5;
bpath_good[2].y3 = 1.6;
bpath_good[3].code = NR_LINETO;
bpath_good[3].x3 = .01;
bpath_good[3].y3 = .08;
bpath_good[4].code = NR_LINETO;
bpath_good[4].x3 = .01;
bpath_good[4].y3 = .02;
bpath_good[5].code = NR_END;
{ // Test decimals
char const * path_str = "M .01,.02 L.04.02 L1.5,1.6L0.01,0.08 .01.02 z";
NArtBpath * bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
{ // Test exponent
char const * path_str = "M 1e-2,.2e-1 L 0.004e1,0.0002e+2 L0150E-2,1.6e0L1.0e-2,80e-3 z";
NArtBpath * bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
}
void testReadImplicitSeparation() {
// Coordinates need not be separated by whitespace if they can still be read unambiguously
NArtBpath bpath_good[5+1];
bpath_good[0].code = NR_MOVETO;
bpath_good[0].x3 = .1;
bpath_good[0].y3 = .2;
bpath_good[1].code = NR_LINETO;
bpath_good[1].x3 = .4;
bpath_good[1].y3 = .2;
bpath_good[2].code = NR_LINETO;
bpath_good[2].x3 = .4;
bpath_good[2].y3 = .8;
bpath_good[3].code = NR_LINETO;
bpath_good[3].x3 = .1;
bpath_good[3].y3 = .8;
bpath_good[4].code = NR_LINETO;
bpath_good[4].x3 = .1;
bpath_good[4].y3 = .2;
bpath_good[5].code = NR_END;
{ // Test absolute
char const * path_str = "M .1.2+0.4.2e0.4e0+8e-1.1.8 z";
NArtBpath * bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
{ // Test relative
char const * path_str = "m .1.2+0.3.0e0.0e0+6e-1-.3.0 z";
NArtBpath * bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
}
void testReadErrorMisplacedCharacter() {
char const * path_str;
NArtBpath * bpath;
NArtBpath * bpath_good = rectangleBpath;
bpath_good[0].code = NR_MOVETO;
// Comma in the wrong place (commas may only appear between parameters)
path_str = "M 1,2 4,2 4,8 1,8 z , m 13,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// Comma in the wrong place (commas may only appear between parameters)
path_str = "M 1,2 4,2 4,8 1,8 z m,13,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// Period in the wrong place (no numbers after a 'z')
path_str = "M 1,2 4,2 4,8 1,8 z . m 13,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// Sign in the wrong place (no numbers after a 'z')
path_str = "M 1,2 4,2 4,8 1,8 z + - m 13,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// Digit in the wrong place (no numbers after a 'z')
path_str = "M 1,2 4,2 4,8 1,8 z 9809 m 13,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// Digit in the wrong place (no numbers after a 'z')
path_str = "M 1,2 4,2 4,8 1,8 z 9809 876 m 13,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
void testReadErrorUnrecognizedCharacter() {
char const * path_str;
NArtBpath * bpath;
NArtBpath * bpath_good = rectangleBpath;
bpath_good[0].code = NR_MOVETO;
// Unrecognized character
path_str = "M 1,2 4,2 4,8 1,8 z&m 13,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// Unrecognized character
path_str = "M 1,2 4,2 4,8 1,8 z m &13,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
void testReadErrorTypo() {
char const * path_str;
NArtBpath * bpath;
NArtBpath * bpath_good = rectangleBpath;
bpath_good[0].code = NR_MOVETO;
// Typo
path_str = "M 1,2 4,2 4,8 1,8 z j 13,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
bpath_good[0].code = NR_MOVETO_OPEN;
// Typo
path_str = "M 1,2 4,2 4,8 1,8 L 1,2 x m 13,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
void testReadErrorIllformedNumbers() {
char const * path_str;
NArtBpath * bpath;
NArtBpath * bpath_good = rectangleBpath;
bpath_good[0].code = NR_MOVETO;
// Double exponent
path_str = "M 1,2 4,2 4,8 1,8 z m 13e4e5,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// Double sign
path_str = "M 1,2 4,2 4,8 1,8 z m +-13,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// Double sign
path_str = "M 1,2 4,2 4,8 1,8 z m 13e+-12,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// No digit
path_str = "M 1,2 4,2 4,8 1,8 z m .e12,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// No digit
path_str = "M 1,2 4,2 4,8 1,8 z m .,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// No digit
path_str = "M 1,2 4,2 4,8 1,8 z m +,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// No digit
path_str = "M 1,2 4,2 4,8 1,8 z m +.e+,15";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
void testReadErrorJunk() {
char const * path_str;
NArtBpath * bpath;
NArtBpath * bpath_good = rectangleBpath;
bpath_good[0].code = NR_MOVETO;
// Junk
path_str = "M 1,2 4,2 4,8 1,8 z j 357 hkjh.,34e34 90ih6kj4 h5k6vlh4N.,6,45wikuyi3yere..3487 m 13,23";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
void testReadErrorStopReading() {
char const * path_str;
NArtBpath * bpath;
NArtBpath * bpath_good = rectangleBpath;
bpath_good[0].code = NR_MOVETO;
// Unrecognized parameter
path_str = "M 1,2 4,2 4,8 1,8 z m #$%,23,34";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// Invalid parameter
path_str = "M 1,2 4,2 4,8 1,8 z m #$%,23,34";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
// Illformed parameter
path_str = "M 1,2 4,2 4,8 1,8 z m +-12,23,34";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
bpath_good[0].code = NR_MOVETO_OPEN;
// "Third" parameter
path_str = "M 1,2 4,2 4,8 1,8 1,2,3 M 12,23";
bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,bpath_good));
g_free(bpath);
}
void testRoundTrip() {
// This is the easiest way to (also) test writing path data, as a path can be written in more than one way.
NArtBpath * bpath;
NArtBpath * new_bpath;
char * path_str;
// Rectangle (closed)
bpath = sp_svg_read_path(rectanglesAbsoluteClosed[0].c_str());
path_str = sp_svg_write_path(bpath);
new_bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,new_bpath));
g_free(bpath); g_free(path_str); g_free(new_bpath);
// Rectangle (open)
bpath = sp_svg_read_path(rectanglesAbsoluteOpen[0].c_str());
path_str = sp_svg_write_path(bpath);
new_bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,new_bpath));
g_free(bpath); g_free(path_str); g_free(new_bpath);
// Concatenated rectangles
bpath = sp_svg_read_path((rectanglesAbsoluteClosed[0] + rectanglesRelativeOpen[0] + rectanglesRelativeClosed[0] + rectanglesAbsoluteOpen[0]).c_str());
path_str = sp_svg_write_path(bpath);
new_bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,new_bpath));
g_free(bpath); g_free(path_str); g_free(new_bpath);
// Zero-length subpaths
bpath = sp_svg_read_path("M 0,0 M 1,1 L 2,2 M 3,3 z M 4,4 L 5,5 z M 6,6");
path_str = sp_svg_write_path(bpath);
new_bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath,new_bpath));
g_free(bpath); g_free(path_str); g_free(new_bpath);
// Floating-point
bpath = sp_svg_read_path("M .01,.02 L 0.04,0.02 L.04,.08L0.01,0.08 z""M 1e-2,.2e-1 L 0.004e1,0.0002e+2 L04E-2,.08e0L1.0e-2,80e-3 z");
path_str = sp_svg_write_path(bpath);
new_bpath = sp_svg_read_path(path_str);
TS_ASSERT(bpathEqual(bpath, new_bpath, 1e-17));
g_free(bpath); g_free(path_str); g_free(new_bpath);
}
void testMinexpPrecision() {
NArtBpath * bpath;
char * path_str;
// Default values
prefs_set_int_attribute("options.svgoutput", "allowrelativecoordinates", 1);
prefs_set_int_attribute("options.svgoutput", "forcerepeatcommands", 0);
prefs_set_int_attribute("options.svgoutput", "numericprecision", 8);
prefs_set_int_attribute("options.svgoutput", "minimumexponent", -8);
bpath = sp_svg_read_path("M 123456781,1.23456781e-8 L 123456782,1.23456782e-8 L 123456785,1.23456785e-8 L 10123456400,1.23456785e-8 L 123456789,1.23456789e-8 L 123456789,101.234564e-8 L 123456789,1.23456789e-8");
path_str = sp_svg_write_path(bpath);
TS_ASSERT_RELATION( streq_rel , "m 123456780,1.2345678e-8 0,0 10,1e-15 9999999210,0 -9999999210,0 0,9.99999921e-7 0,-9.99999921e-7" , path_str );
g_free(bpath); g_free(path_str);
}
private:
bool bpathEqual(NArtBpath const * a, NArtBpath const * b, double eps = 1e-16) {
while(a->code != NR_END && b->code == a->code) {
switch(a->code) {
case NR_MOVETO:
case NR_MOVETO_OPEN:
case NR_LINETO:
if (!Geom::are_near(a->x3,b->x3, eps) || !Geom::are_near(a->y3,b->y3, eps)) return false;
break;
case NR_CURVETO:
if (!Geom::are_near(a->x1,b->x1, eps) || !Geom::are_near(a->y1,b->y1, eps)) return false;
if (!Geom::are_near(a->x2,b->x2, eps) || !Geom::are_near(a->y2,b->y2, eps)) return false;
if (!Geom::are_near(a->x3,b->x3, eps) || !Geom::are_near(a->y3,b->y3, eps)) return false;
break;
default:
TS_FAIL("Unknown path code!");
}
a++;
b++;
}
return a->code == b->code;
}
};
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :