|
60 | 60 | box(width: mutable-width, body) |
61 | 61 | } |
62 | 62 |
|
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 | +) = { |
64 | 66 | // Place two labels with the requested vertical separation to be able to |
65 | 67 | // measure their vertical distance in pt. |
66 | 68 | // Using this approach instead of using `measure` allows us to accept fractions |
|
113 | 115 | let w-ratio = mutable-width / size.width |
114 | 116 | let ratio = calc.min(h-ratio, w-ratio) * 100% |
115 | 117 |
|
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 | + } |
127 | 136 | }) |
128 | 137 | }) |
129 | 138 | }) |
130 | 139 | } |
131 | 140 |
|
132 | | -#let fit-to-width(width, content) = { |
| 141 | +#let fit-to-width(grow: true, shrink: true, width, content) = { |
133 | 142 | style(styles => { |
134 | 143 | layout(layout-size => { |
135 | 144 | let content-size = measure(content, styles) |
136 | 145 | let content-width = content-size.width |
137 | 146 | 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 | + ) { |
139 | 151 | let ratio = width / content-width * 100% |
140 | 152 | // The first box keeps content from prematurely wrapping |
141 | 153 | let scaled = scale( |
|
0 commit comments