Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions plugins/convertPathData.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ function filters(
// to get closer to absolute coordinates. Sum of rounded value remains same:
// l .25 3 .25 2 .25 3 .25 2 -> l .3 3 .2 2 .3 3 .2 2
if (precision !== false) {
// Correct for accumulated error
if (
command === 'm' ||
command === 'l' ||
Expand All @@ -624,6 +625,21 @@ function filters(
// @ts-expect-error
data[6] += item.base[1] - relSubpoint[1];
}
// Correct l commands heading home
if (
command == 'l' &&
prev.command != 'M' &&
prev.command != 'm' &&
// @ts-expect-error
Math.abs(item.coords[0] - pathBase[0]) < error &&
// @ts-expect-error
Math.abs(item.coords[1] - pathBase[1]) < error
) {
// @ts-expect-error
data[0] = pathBase[0] - item.base[0];
// @ts-expect-error
data[1] = pathBase[1] - item.base[1];
}
roundData(data);

if (command == 'h') {
Expand Down Expand Up @@ -877,10 +893,8 @@ function filters(
(command === 'l' || command === 'h' || command === 'v')
) {
if (
// @ts-expect-error
Math.abs(pathBase[0] - item.coords[0]) < error &&
// @ts-expect-error
Math.abs(pathBase[1] - item.coords[1]) < error
Math.abs(pathBase[0] - relSubpoint[0]) < error &&
Math.abs(pathBase[1] - relSubpoint[1]) < error
) {
command = 'z';
data = [];
Expand Down Expand Up @@ -918,7 +932,7 @@ function filters(
prevQControlPoint = reflectPoint(qControlPoint, item.base);
} else {
// @ts-expect-error
prevQControlPoint = item.coords;
prevQControlPoint = relSubpoint.slice();
}
} else {
prevQControlPoint = undefined;
Expand Down
17 changes: 17 additions & 0 deletions test/plugins/convertPathData.39.svg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Proper, post-rounding coordinate system (1, 1), not pre-rounding (0.8, 0.8), should be used when converting going home to z

===

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10">
<path d="M 0 0 l 0.4 0.4 l 0.4 0.4" />
</svg>

@@@

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10">
<path d="m0 0 1 1"/>
</svg>

@@@

{ "floatPrecision": 0 }