Skip to content

Commit

Permalink
fix(convertPathData): fix some weird behavior (#1867)
Browse files Browse the repository at this point in the history
  • Loading branch information
KTibow authored Dec 24, 2023
1 parent 2c408ce commit a7859eb
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 33 deletions.
62 changes: 32 additions & 30 deletions plugins/convertPathData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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' ||
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/coa/testSvg/test.1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/coa/testSvg/test.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/plugins/convertPathData.30.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/plugins/convertPathData.31.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/svgo/plugins-order.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a7859eb

Please sign in to comment.