svgo icon indicating copy to clipboard operation
svgo copied to clipboard

fix(convertPathData): introducing isSafeToRemove

Open KTibow opened this issue 4 months ago • 2 comments

Removing useless commands: a tricky and inconsistent business. SVGO currently uses two approaches:

  • !maybeHasStrokeAndLinecap for the purpose of removing useless line commands

    This works decently. It allows for removing useless lines, while not removing dots.

    Flaw: Even in a path with linecaps, if there are "dot" commands after the first move, they wouldn't render at all and should be removed. They currently aren't.

  • isSafeToUseZ for the purpose of removing useless z commands

    isSafeToUseZ is defined as either having no stroke or having a all-round stroke. When that's true, a line home and a z command render the same way, and can be exchanged or removed.

    Flaw: This works great when removing a redundant z that comes right after another command, but what if the z is the first move (for the purpose of creating a dot)? Then it's incorrectly removed. This is shown in #2158 and #2163.

This PR introduces isSafeToRemove to help. It's worth reading through the code, but the TLDR is that it uses a more sane method, categorizing the path into non-stroked, stroked + first draw command, and stroked but non-first command, and using fitting behavior in each case.

Fixes #2158 and fixes #2163.

KTibow avatar Aug 09 '25 19:08 KTibow

It seems that this PR kills the optimization case when there is no stroke. Which was the goal at the first place.

GreLI avatar Aug 10 '25 08:08 GreLI

It seems that this PR kills the optimization case when there is no stroke.

What makes you say this?

if (!maybeHasStroke) {
  return true;
}

literally means "if there is no stroke, it is always safe to optimize". All the previous test cases also pass.

KTibow avatar Aug 10 '25 16:08 KTibow