abey79 / vsvg

Fast and portable tools for plotter users
http://whisk.rs
MIT License
111 stars 13 forks source link

Layer ID determination #7

Open abey79 opened 1 year ago

abey79 commented 1 year ago

I currently don't have a plan to revolutionise the way vpype identifies its layers: namely using a non-zero integer value, with higher-ID layers drawn on top of lower-ID layers.

vpype currently uses the following approach to determine layer IDs (from vpype read --help):

By default, the read command attempts to preserve the layer structure of the SVG. In this context, top-level groups () are each considered a layer. If any, all non-group, top-level SVG elements are imported into layer 1.

The following logic is used to determine in which layer each SVG top-level group is imported:

  • If a inkscape:label attribute is present and contains digit characters, it is stripped of non-digit characters the resulting number is used as target layer. If the resulting number is 0, layer 1 is used instead.
  • If the previous step fails, the same logic is applied to the id attribute.
  • If both previous steps fail, the target layer matches the top-level group's order of appearance.

However, the stripping non-digit character strategy has led to unexpected behaviour: https://github.com/abey79/vpype/issues/594. Further, EMSL introduced an informal spec on layer names here, which would be worth considering (especially for SVG output).

For the time being, I'll use "first group of digit" rather "all digits" to extract a layer ID from inkscape:label or id.

abey79 commented 1 year ago

I don't recall of a good reason to enforce strictly-positive layer ID, except 1-based numbering feeling more natural to non-tech users. On Rust side, it can be enforced with NonZeroUsize, but it's annoying to use (NonZeroUsize::new(1).unwrap()). Plus, it can lead to unexpected behaviour if a layer is explicitly numbered 0 by the user.

Moving on, I'm going to accept 0 as layer ID, and use the following heuristics:

abey79 commented 1 year ago

For the time being, I'll use "first group of digit" rather "all digits" to extract a layer ID from inkscape:label or id.

This doesn't solve all of the possible "WTF" behaviour, e.g. with layers named after colours such as "magenta90" and "orange90".

This particular case would be gracefully handled by the EMSL standard where only the starting group of digit is interpreted as layer ID, but any other group of digit is not.