Skip to main content

Module path_data

Module path_data 

Source
Expand description

SVG path-data parser for VectorIcon.

Deliberately a subset: M, L, H, V, C, A, Z in both absolute and relative form. That is everything hand-authored icon artwork uses, and it lets a host paste an SVG’s d attribute in verbatim rather than re-deriving the geometry by hand — the icon in the app then stays byte-comparable with the artwork it came from. Full SVG (gradients, transforms, text, S/T/Q shorthands) is the job of crate::svg, which brings in usvg; this module exists so a 200-byte icon does not need a document parser.

Output is already-flattened closed contours in icon space, because everything downstream (painting, hit-free bounds queries, tests) wants points rather than curves, and the flattening tolerance only has to satisfy a ~16-px icon.

§One thing the subset does NOT accept: compressed arc flags

SVG lets an arc’s two boolean flags run together with the numbers around them (a1 1 0 0110 10 means 0 1 10 10), because a flag is by definition one character. This scanner reads every argument as a full number, so it sees 0110 instead. That is a deliberate omission — the artwork this module exists for is written with separators, and the alternative is a scanner whose number parsing depends on which argument position it is in.

It fails loudly, which is why the omission is safe: swallowing 0110 as one number leaves the arc one argument short, so the path is rejected with IconPathError::MissingArgument rather than drawn wrong (pinned by compressed_arc_flags_are_rejected_not_misdrawn in this module’s tests). A path that got past that check would still land on the stated endpoint — the arc’s own bulge is the only thing flags control.

Curves are flattened at a fixed angular / parametric step chosen for that size: arcs are cut at 7.5° (chord error r·(1−cos 3.75°), about 0.05 units on a 64-unit box — a thousandth of the icon) and cubics into 16 pieces. Icon artwork is commonly already chopped into short arc segments so that naive flatteners look right; this step is fine enough that such artwork is unaffected either way.

Enums§

IconPathError
Why a path-data string could not be parsed.

Functions§

parse_path
Parse SVG path data into closed contours.

Type Aliases§

Contour
One closed contour: consecutive points in icon space.