Skip to content

Commit

Permalink
Content Auto-Scaling (#799)
Browse files Browse the repository at this point in the history
* content: Support canvas scaling

* content: Scaling tests
  • Loading branch information
johannes-wolf authored Jan 23, 2025
1 parent 9d9aaf9 commit aad210a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/draw/shapes.typ
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@
/// *Root*: `content`
/// - padding (number, dictionary) = 0: Sets the spacing around content. Can be a single number to set padding on all sides or a dictionary to specify each side specifically. The dictionary follows Typst's `pad` function: https://typst.app/docs/reference/layout/pad/
/// - frame (str, none) = none: Sets the frame style. Can be {{none}}, `"rect"` or `"circle"` and inherits the `stroke` and `fill` style.
/// - auto-scale (bool): If `true`, apply current canvas scaling to the content. Defaults to `false`.
///
/// ## Anchors
/// Supports border anchors, the default anchor is set to **center**.
Expand Down Expand Up @@ -857,6 +858,7 @@
}

return (ctx => {
let body = body
let style = styles.resolve(ctx.style, merge: style, root: "content")
let padding = util.as-padding-dict(style.padding)
for (k, v) in padding {
Expand All @@ -881,6 +883,14 @@
// Typst's `rotate` function is clockwise relative to x-axis, which is backwards from us
angle = angle * -1

// Optionally scale content with current canvas scaling
if style.auto-scale == true {
let sx = vector.len(matrix.column(ctx.transform, 0))
let sy = vector.len(matrix.column(ctx.transform, 1))

body = std.scale(x: sx * 100%, y: sy * 100%, body, reflow: true)
}

// Height from the baseline to content-north
let (content-width, baseline-height) = util.measure(ctx, text(top-edge: "cap-height", bottom-edge: "baseline", body))

Expand Down
2 changes: 2 additions & 0 deletions src/styles.typ
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@
frame: none,
fill: auto,
stroke: auto,
// Apply canvas scaling to content
auto-scale: false,
),
)

Expand Down
Binary file modified tests/content/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions tests/content/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@
line("a", "b")
})

// #792 Scaling
#test-case({
import draw:*
scale(2)
set-style(content: (auto-scale: false))
content((0,0), [Text])
set-style(content: (auto-scale: true))
content((0,1), [Text])
})

// Compiler crash
#test-case({
import draw: *
Expand Down

0 comments on commit aad210a

Please sign in to comment.