I’m working on a package with displays a syntax tree using cetz, but I’m having trouble calculating the width of the leaf nodes. Currently it only works for strings with an approximation of label.len() / 7 + 0.5
which has worked surprisingly well, but I want to do better.
This is what I’ve been trying:
let width(node) = context {
let (_label, children) = node
if type(children) == content {
measure(children).width.cm
} else if type(children) == array {
children.map(width).sum()
}
}
But I’m getting Assertion failed: Incorrect type for body: content
from cetz.
For reference the broader function and where it’s called from is
let tree-recursive(node, id: (0,), x: 0, y: 0) = context {
let wid = width(node)
...
// show label with cetz.draw.content
...
// call recursively on children with modified coords
}
cetz.canvas(tree-recursive(node))
Any ideas on how to solve this? My instinct is that I shouldn’t need this much context, but I don’t know how to get any better an approximation without using it.