Skip to content

Commit 3c92118

Browse files
committed
Optionally restrict whether content can grow or shrink during fitting
1 parent 0112c70 commit 3c92118

1 file changed

Lines changed: 26 additions & 14 deletions

File tree

utils/utils.typ

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@
6060
box(width: mutable-width, body)
6161
}
6262

63-
#let fit-to-height(height, width: none, prescale-width: none, body) = {
63+
#let fit-to-height(
64+
width: none, prescale-width: none, grow: true, shrink: true, height, body
65+
) = {
6466
// Place two labels with the requested vertical separation to be able to
6567
// measure their vertical distance in pt.
6668
// Using this approach instead of using `measure` allows us to accept fractions
@@ -113,29 +115,39 @@
113115
let w-ratio = mutable-width / size.width
114116
let ratio = calc.min(h-ratio, w-ratio) * 100%
115117

116-
let new-width = size.width * ratio
117-
v(-available-height)
118-
// If not boxed, the content can overflow to the next page even though it will fit.
119-
// This is because scale doesn't update the layout information.
120-
// Boxing in a container without clipping will inform typst that content
121-
// will indeed fit in the remaining space
122-
box(
123-
width: new-width,
124-
height: available-height,
125-
scale(x: ratio, y: ratio, origin: top + left, boxed-content)
126-
)
118+
if (
119+
(shrink and (ratio < 100%))
120+
or (grow and (ratio > 100%))
121+
) {
122+
let new-width = size.width * ratio
123+
v(-available-height)
124+
// If not boxed, the content can overflow to the next page even though it will
125+
// fit. This is because scale doesn't update the layout information.
126+
// Boxing in a container without clipping will inform typst that content
127+
// will indeed fit in the remaining space
128+
box(
129+
width: new-width,
130+
height: available-height,
131+
scale(x: ratio, y: ratio, origin: top + left, boxed-content)
132+
)
133+
} else {
134+
body
135+
}
127136
})
128137
})
129138
})
130139
}
131140

132-
#let fit-to-width(width, content) = {
141+
#let fit-to-width(grow: true, shrink: true, width, content) = {
133142
style(styles => {
134143
layout(layout-size => {
135144
let content-size = measure(content, styles)
136145
let content-width = content-size.width
137146
let width = _size-to-pt(width, styles, layout-size.width)
138-
if width < content-width {
147+
if (
148+
(shrink and (width < content-width))
149+
or (grow and (width > content-width))
150+
) {
139151
let ratio = width / content-width * 100%
140152
// The first box keeps content from prematurely wrapping
141153
let scaled = scale(

0 commit comments

Comments
 (0)