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
34 changes: 24 additions & 10 deletions plugins/convertPathData.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,25 @@ export const fn = (root, params) => {
computedStyle['stroke-linecap'] &&
(computedStyle['stroke-linecap'].type === 'dynamic' ||
computedStyle['stroke-linecap'].value !== 'butt');
const maybeHasStrokeAndLinecap = maybeHasStroke && maybeHasLinecap;
const isSafeToUseZ = maybeHasStroke
? computedStyle['stroke-linecap']?.type === 'static' &&
computedStyle['stroke-linecap'].value === 'round' &&
computedStyle['stroke-linejoin']?.type === 'static' &&
computedStyle['stroke-linejoin'].value === 'round'
: true;
const isSafeToRemove = (
/** @type {boolean} */ isFirstDraw,
/** @type {boolean} */ safeIfNotFirstDraw,
) => {
if (!maybeHasStroke) {
return true;
}
if (isFirstDraw) {
return !maybeHasLinecap;
} else {
return safeIfNotFirstDraw;
}
};

let data = path2js(node);

Expand All @@ -174,7 +186,7 @@ export const fn = (root, params) => {

data = filters(data, newParams, {
isSafeToUseZ,
maybeHasStrokeAndLinecap,
isSafeToRemove,
hasMarkerMid,
});

Expand Down Expand Up @@ -387,14 +399,10 @@ const convertToRelative = (pathData) => {
*
* @param {import('../lib/types.js').PathDataItem[]} path
* @param {InternalParams} params
* @param {{ isSafeToUseZ: boolean, maybeHasStrokeAndLinecap: boolean, hasMarkerMid: boolean }} param2
* @param {{ isSafeToUseZ: boolean, isSafeToRemove: (isFirstDraw: boolean, safeIfNotFirstDraw: boolean) => boolean, hasMarkerMid: boolean }} param2
* @returns {import('../lib/types.js').PathDataItem[]}
*/
function filters(
path,
params,
{ isSafeToUseZ, maybeHasStrokeAndLinecap, hasMarkerMid },
) {
function filters(path, params, { isSafeToUseZ, isSafeToRemove, hasMarkerMid }) {
const stringify = data2Path.bind(null, params);
const relSubpoint = [0, 0];
const pathBase = [0, 0];
Expand Down Expand Up @@ -844,7 +852,10 @@ function filters(
}

// remove useless non-first path segments
if (params.removeUseless && !maybeHasStrokeAndLinecap) {
if (
params.removeUseless &&
isSafeToRemove(prev.command == 'm' || prev.command == 'M', true)
) {
// l 0,0 / h 0 / v 0 / q 0,0 0,0 / t 0,0 / c 0,0 0,0 0,0 / s 0,0 0,0
if (
(command === 'l' ||
Expand Down Expand Up @@ -900,7 +911,10 @@ function filters(
if (
(command === 'Z' || command === 'z') &&
params.removeUseless &&
isSafeToUseZ &&
isSafeToRemove(
prev.command == 'm' || prev.command == 'M',
isSafeToUseZ,
) &&
// @ts-expect-error
Math.abs(item.base[0] - item.coords[0]) < error / 10 &&
// @ts-expect-error
Expand Down
15 changes: 15 additions & 0 deletions test/plugins/convertPathData.38.svg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Should not break dots.

See: https://github.com/svg/svgo/issues/2163

===

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
<path d="M10 10z" stroke="#00f" stroke-linecap="round" stroke-linejoin="round" />
</svg>

@@@

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
<path d="M10 10z" stroke="#00f" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
13 changes: 13 additions & 0 deletions test/plugins/convertPathData.39.svg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Should drop truely useless commands even when a linecap is present.

===

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
<path d="M 10 50 h 10 v 0" stroke="#00f" stroke-linecap="round" stroke-linejoin="round" />
</svg>

@@@

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
<path d="M10 50h10" stroke="#00f" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
Loading