chickadee » svgpath

An SVG path data library for Chicken Scheme

This library can turn SVG path data to tagged sexp format (compatible with SXML). You can switch between absolute and relative coordinates, and turn it into a minified string.

The procedures work on both this library's own tagged format and on path data strings.

The data format uses single-letter symbols for the commands and exacts for the numbers. Every node is always a separate list, even when there's multiple subsequent nodes of the same command.

Every node can have attributes with an @ attribute marker, just like in SXML.

You can put in any attributes you want so you can keep track of node-local data. They're stripped out when you export the path back to string.

Here's an example of an H command with one argument, which happens to be 105, and with absolute coords added as an attribute:

(H (@ (coords (x 105) (y 200))) 105)

Procedures

path->string

(path->string "M100 100 L101,101 90 102")
⇒ "M100 100l1 1-11 1"

Reads a path (in sexp or string format) and returns a minified string.

->apath

(->apath "M100 200L200 100-100-200")
⇒ ((M 100 200) (L 200 100) (L -100 -200))

Reads a path (in sexp or string format) and returns sexp format with absolute coordinates.

->rpath

(->rpath "M100 200L200 100-100-200")
⇒ ((m 100 200) (l 100 -100) (l -300 -300))

Reads a path (in sexp or string format) and returns sexp format with relative coordinates.

->mpath

(->mpath "M100 200L200 100-100-200")
⇒ ((M 100 200) (L 200 100) (L -100 -200))

Reads a path (in sexp or string format) and returns sexp format with whatever coordinate format will print the shortest. This is almost never what useful when doing path programming, but path->string uses it internally.

add-coords

Change a path (in sexp or string format) into sexp format with a coords attribute added that stores the absolute coordinates of the node. If the path already had coords, only the newly computated coords will be returned.

add-distances

Change a path (in sexp or string format) into sexp format with a distance attribute added that stores the pythagorean diagonal distance to the next node. If the path has a coords attribute on all of its nodes, they will be trusted, otherwise new ones will be calculated and added for all nodes. If the path already had distance attributes, only the newly computated distance attributes will be returned.

Source code

For a repo,

git clone https://idiomdrottning.org/svgpath