Skip to content

tree: Fix Transformation & Support Node-Anchors #904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# 0.4.1
- Added a `n-star` shape for drawing n-pointed stars (#886)
- Added `breakable: false` to the `block` element of the canvas (#880)
- A new tree layout algorithm, implemented by @MichaelGoodale in Rust, can handle differently sized tree nodes (#889)
- **BREAKING** The `tree` callback for drawing nodes does not pass the parent nodes name anymor (new signature is `(node) => elements`) (#889)
- **BREAKING** The `parent-position` argument of `tree` got removed (#889, #891)
- Added a `n-star` shape for drawing n-pointed stars
- Added `breakable: false` to the `block` element of the canvas
- A new tree layout algorithm, implemented by @MichaelGoodale in Rust, can handle differently sized tree nodes
- **BREAKING** The `tree` callback for drawing nodes does not pass the parent nodes name anymore (new signature is `(node) => elements`)
- **BREAKING** The `parent-position` argument of `tree` got removed
- Tree nodes now support anchors: custom anchors from the `draw-node` callback
are accessible.
- `styles.resolve` now supports nested roots by passing an array (#914)

# 0.4.0
Expand Down
34 changes: 11 additions & 23 deletions src/lib/tree.typ
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,15 @@
depth: depth,
children: children,
content: content,
drawables: none,
)

// Pre-Render the node
let (ctx: _, drawables, bounds) = process.many(ctx, draw.scope({
draw.set-origin((0, 0))
(draw-node)(node)
}))
node.drawables = drawables

// Measure the node
if measure-content {
let (ctx: _, drawables: _, bounds) = process.many(ctx, {
draw.set-origin((0, 0))
(draw-node)(node)
})

if bounds != none {
(node.width, node.height, _) = aabb.size(bounds)
}
Expand Down Expand Up @@ -190,21 +188,11 @@
node.group-name = "g" + name
node.element = {
draw.anchor(node.name, node-position(node))
draw.group(
name: node.group-name,
{
draw.anchor("default", node-position(node))

(ctx => {
let (x, y) = node-position(node)
let translation = matrix.transform-translate(x, -y, 0)

return (
ctx: ctx,
drawables: drawable.apply-transform(
translation, node.drawables),
)
},)
draw.group(name: node.group-name, ctx => {
let (x, y) = node-position(node)
draw.translate((x, y, 0))
draw.anchor("default", (0, 0))
draw-node(node)
},
)
}
Expand Down
6 changes: 5 additions & 1 deletion src/process.typ
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
}

let name = element.at("name", default: none)
element.name = name
if name != none {
assert.eq(type(name), str,
message: "Element name must be a string")
Expand Down Expand Up @@ -64,6 +65,7 @@
ctx: ctx,
bounds: bounds,
drawables: element.at("drawables", default: ()),
element: element,
)
}

Expand All @@ -74,6 +76,7 @@
#let many(ctx, body) = {
let drawables = ()
let bounds = none
let elements = ()

for el in body {
let r = element(ctx, el)
Expand All @@ -85,6 +88,7 @@
ctx = r.ctx
drawables += r.drawables
}
elements.push(r.element)
}
return (ctx: ctx, bounds: bounds, drawables: drawables)
return (ctx: ctx, bounds: bounds, drawables: drawables, elements: elements)
}
Loading