Skip to content

Conversation

KTibow
Copy link
Contributor

@KTibow KTibow commented Aug 9, 2025

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 convertPathData removes valid paths #2158 and old issue killing most content is back #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 KTibow marked this pull request as draft August 9, 2025 20:13
@KTibow KTibow marked this pull request as ready for review August 9, 2025 20:16
@KTibow KTibow changed the title feat(convertPathData): introducing isSafeToRemove fix(convertPathData): introducing isSafeToRemove Aug 9, 2025
@GreLI
Copy link
Member

GreLI commented Aug 10, 2025

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

@KTibow
Copy link
Contributor Author

KTibow commented Aug 10, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

old issue killing most content is back convertPathData removes valid paths

2 participants