svg/svgo

Add an option to always keep/add `Z` in `convertPathData`

alecmev opened this issue · 4 comments

Is your feature request related to a problem? Please describe.

I understand that Z can be skipped under certain conditions (though what are they precisely?), but we'd like for it to be explicit, as we have algorithms that operate on path data, and we don't want to add any logic for handling this.

Describe alternatives you've considered

We could adjust our code so that it can handle geometrically closed paths, but I'm unable to even find the spec. Currently using removeUseless: false to work around this, but I assume it disables more than just the removal of Z.

Additional context

M 50,50 80,50 80,80 50,80 50,51 (not closed, just for demo of self-closing below)
2024-01-22_20-36

M 50,50 80,50 80,80 50,80 50,50
M 50,50 80,50 80,80 50,80 z
2024-01-22_20-35

M 50,50 80,50 80,80 50,50
2024-01-22_20-37

M 50,50 80,50 80,80 z
2024-01-22_20-37_1

Z can be skipped under certain conditions (though what are they precisely?)

If the cursor is already at the home point and if it's safe. How do you determine if it's safe?

// This is how.
const isSafeToUseZ = maybeHasStroke
  ? computedStyle["stroke-linecap"]?.type === "static" &&
    computedStyle["stroke-linecap"].value === "round" &&
    computedStyle["stroke-linejoin"]?.type === "static" &&
    computedStyle["stroke-linejoin"].value === "round"
  : true;

If you're looking for help with implementation, consider that
a. you can already split paths into chunks by checking for move instructions
b. if you're trying to see if it's closed, just 1. keep track of the "home" point 2. keep track of the current cursor 3. see if the cursor is basically at the home point at the last command before the end or another move

Is isSafeToUseZ === !isSafeToSkipZ though? I feel like it isn't, no?

Also, as you can see with the triangle examples above, the stroke is not continuous despite the cursor arriving at the home point. So there must be some limit to the acuteness of the resulting corner, since the square does get auto-closed 🤔 These screenshots are from Firefox, just in case.

Is isSafeToUseZ === !isSafeToSkipZ though? I feel like it isn't, no?

You can look through the code, isSafeToUseZ === isSafeToSkipZ for all of SVGO's purposes. We define "safe" as not having a non-round stroke. It's really that simple.

Also, as you can see with the triangle examples above, the stroke is not continuous despite the cursor arriving at the home point.

You're correct, when you have a non-round stroke sometimes you can still skip z. However SVGO never skips z with a non-round stroke - I was initially planning to do so but the logic that converts lines to z if the following command is z is good enough.

Okay, I see, SVGO can be more aggressive if it's assumed that the image is used as-is. In our case, we later programmatically manipulate it and add/remove stroking, and change its style, the miter limit, etc. Would it make sense to have an option for this? Something like "don't remove Z if it could affect the behavior of any stroke cap/join style".