fix(convertPathData): introducing isSafeToRemove
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
zcommandsisSafeToUseZ 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
zthat comes right after another command, but what if thezis 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.
It seems that this PR kills the optimization case when there is no stroke. Which was the goal at the first place.
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.