Skip to content

tree: Fix transformation and support named nodes #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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 14 additions & 4 deletions src/lib/tree.typ
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,16 @@
children: children,
content: content,
drawables: none,
elements: none,
)

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

if measure-content {
if bounds != none {
Expand Down Expand Up @@ -197,12 +199,20 @@

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

// Update all the nodes anchors
for elem in node.elements.filter(elem => elem.at("name", default: none) != none) {
elem.anchors = util.transform-anchors(elem, transform)
ctx.nodes.insert(elem.name, elem)
ctx.groups.last().push(elem.name)
}

return (
ctx: ctx,
drawables: drawable.apply-transform(
translation, node.drawables),
transform, node.drawables),
)
},)
},
Expand Down
5 changes: 4 additions & 1 deletion src/process.typ
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
ctx: ctx,
bounds: bounds,
drawables: element.at("drawables", default: ()),
element: element,
)
}

Expand All @@ -74,6 +75,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 +87,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)
}
14 changes: 14 additions & 0 deletions src/util.typ
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,17 @@
}
return num
}

/// Returns a new anchor function that applies
/// the given transformation on each anchor returned
/// by the elements previous anchor function.
#let transform-anchors(element, transform) = {
let next-resolver = element.anchors
return (key) => {
if key == () {
next-resolver(key)
} else {
matrix.mul4x4-vec3(transform, next-resolver(key))
}
}
}
Loading