diff --git a/plugins/convertPathData.js b/plugins/convertPathData.js index b4ef21147..b7b6beefc 100644 --- a/plugins/convertPathData.js +++ b/plugins/convertPathData.js @@ -678,24 +678,6 @@ function filters( } } - // convert going home to z - // m 0 0 h 5 v 5 l -5 -5 -> m 0 0 h 5 v 5 z - if ( - params.convertToZ && - (isSafeToUseZ || next?.command === 'Z' || next?.command === 'z') && - (command === 'l' || command === 'h' || command === 'v') - ) { - if ( - // @ts-ignore - Math.abs(pathBase[0] - item.coords[0]) < error && - // @ts-ignore - Math.abs(pathBase[1] - item.coords[1]) < error - ) { - command = 'z'; - data = []; - } - } - // collapse repeated commands // h 20 h 30 -> h 50 if ( @@ -733,9 +715,9 @@ function filters( // @ts-ignore prev.command === 'c' && // @ts-ignore - data[0] === -(prev.args[2] - prev.args[4]) && + Math.abs(data[0] - -(prev.args[2] - prev.args[4])) < error && // @ts-ignore - data[1] === -(prev.args[3] - prev.args[5]) + Math.abs(data[1] - -(prev.args[3] - prev.args[5])) < error ) { command = 's'; data = data.slice(2); @@ -746,9 +728,9 @@ function filters( // @ts-ignore prev.command === 's' && // @ts-ignore - data[0] === -(prev.args[0] - prev.args[2]) && + Math.abs(data[0] - -(prev.args[0] - prev.args[2])) < error && // @ts-ignore - data[1] === -(prev.args[1] - prev.args[3]) + Math.abs(data[1] - -(prev.args[1] - prev.args[3])) < error ) { command = 's'; data = data.slice(2); @@ -760,8 +742,8 @@ function filters( prev.command !== 'c' && // @ts-ignore prev.command !== 's' && - data[0] === 0 && - data[1] === 0 + Math.abs(data[0]) < error && + Math.abs(data[1]) < error ) { command = 's'; data = data.slice(2); @@ -775,9 +757,9 @@ function filters( // @ts-ignore prev.command === 'q' && // @ts-ignore - data[0] === prev.args[2] - prev.args[0] && + Math.abs(data[0] - (prev.args[2] - prev.args[0])) < error && // @ts-ignore - data[1] === prev.args[3] - prev.args[1] + Math.abs(data[1] - (prev.args[3] - prev.args[1])) < error ) { command = 't'; data = data.slice(2); @@ -788,9 +770,9 @@ function filters( // @ts-ignore prev.command === 't' && // @ts-ignore - data[2] === prev.args[0] && + Math.abs(data[2] - prev.args[0]) < error && // @ts-ignore - data[3] === prev.args[1] + Math.abs(data[3] - prev.args[1]) < error ) { command = 't'; data = data.slice(2); @@ -826,6 +808,24 @@ function filters( } } + // convert going home to z + // m 0 0 h 5 v 5 l -5 -5 -> m 0 0 h 5 v 5 z + if ( + params.convertToZ && + (isSafeToUseZ || next?.command === 'Z' || next?.command === 'z') && + (command === 'l' || command === 'h' || command === 'v') + ) { + if ( + // @ts-ignore + Math.abs(pathBase[0] - item.coords[0]) < error && + // @ts-ignore + Math.abs(pathBase[1] - item.coords[1]) < error + ) { + command = 'z'; + data = []; + } + } + item.command = command; item.args = data; } else { @@ -870,7 +870,8 @@ function convertToMixed(path, params) { var command = item.command, data = item.args, - adata = data.slice(); + adata = data.slice(), + rdata = data.slice(); if ( command === 'm' || @@ -898,9 +899,10 @@ function convertToMixed(path, params) { } roundData(adata); + roundData(rdata); var absoluteDataStr = cleanupOutData(adata, params), - relativeDataStr = cleanupOutData(data, params); + relativeDataStr = cleanupOutData(rdata, params); // Convert to absolute coordinates if it's shorter or forceAbsolutePath is true. // v-20 -> V0 diff --git a/test/coa/testSvg/test.1.svg b/test/coa/testSvg/test.1.svg index 94848f6de..d5f98e04d 100644 --- a/test/coa/testSvg/test.1.svg +++ b/test/coa/testSvg/test.1.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/test/coa/testSvg/test.svg b/test/coa/testSvg/test.svg index 94848f6de..d5f98e04d 100644 --- a/test/coa/testSvg/test.svg +++ b/test/coa/testSvg/test.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/test/plugins/convertPathData.30.svg b/test/plugins/convertPathData.30.svg new file mode 100644 index 000000000..1d29829d6 --- /dev/null +++ b/test/plugins/convertPathData.30.svg @@ -0,0 +1,13 @@ +Should use relative instead of absolute here. (Checking for proper rounding of relative data) + +=== + + + + + +@@@ + + + + \ No newline at end of file diff --git a/test/plugins/convertPathData.31.svg b/test/plugins/convertPathData.31.svg new file mode 100644 index 000000000..9a4d43348 --- /dev/null +++ b/test/plugins/convertPathData.31.svg @@ -0,0 +1,13 @@ +Should have no commands here. (Checking for removing useless before trying to convert to z) + +=== + + + + + +@@@ + + + + \ No newline at end of file diff --git a/test/svgo/plugins-order.svg b/test/svgo/plugins-order.svg index 038d32722..6feaad859 100644 --- a/test/svgo/plugins-order.svg +++ b/test/svgo/plugins-order.svg @@ -8,4 +8,4 @@ @@@ - +