From b9bbe6f93db939cb89cf3169020dabdad23c0be3 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Sun, 4 Aug 2024 00:22:20 +0200 Subject: [PATCH] added auto fit wrapper function --- src/diagram.typ | 7 ++++--- src/utils.typ | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/diagram.typ b/src/diagram.typ index d34b4d6..fbba74a 100644 --- a/src/diagram.typ +++ b/src/diagram.typ @@ -1,4 +1,4 @@ -#import "utils.typ": get-group-span +#import "utils.typ": get-group-span, fit-canvas #import "renderer.typ": render #import "participant.typ" as participant: _par, PAR-SPECIALS @@ -18,7 +18,7 @@ ),) } -#let diagram(elements) = { +#let diagram(elements, width: auto) = { if elements == none { return } @@ -188,7 +188,8 @@ } set text(font: "Source Sans 3") - render(participants, elmts) + let canvas = render(participants, elmts) + fit-canvas(canvas, width: width) } #let from-plantuml(code) = { diff --git a/src/utils.typ b/src/utils.typ index 8e82d0e..95994df 100644 --- a/src/utils.typ +++ b/src/utils.typ @@ -40,4 +40,24 @@ } panic("Invalid type for parameter mods, expected auto or dictionary, got " + str(type(mods))) -} \ No newline at end of file +} + +#let fit-canvas(canvas, width: auto) = layout(size => { + let m = measure(canvas) + let w = m.width + let h = m.height + let r = if w == 0pt {0} else { + if width == auto {1} else { + size.width * width / w + } + } + let new-w = w * r + let new-h = h * r + r *= 100% + + box( + width: new-w, + height: new-h, + scale(x: r, y: r, reflow: true, canvas) + ) +}) \ No newline at end of file