Skip to content
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

Feature: fit-to-width #96

Open
wants to merge 4 commits into
base: main
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
2 changes: 1 addition & 1 deletion polylux.typ
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
#import "logic.typ"
#import "logic.typ": polylux-slide, uncover, only, alternatives, alternatives-match, alternatives-fn, alternatives-cases, one-by-one, line-by-line, list-one-by-one, enum-one-by-one, terms-one-by-one, pause, enable-handout-mode
#import "utils/utils.typ"
#import "utils/utils.typ": polylux-outline, fit-to-height, side-by-side, pdfpc
#import "utils/utils.typ": polylux-outline, fit-to-height, fit-to-width, side-by-side, pdfpc
60 changes: 47 additions & 13 deletions utils/utils.typ
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#let last-slide-number = locate(loc => logic.logical-slide.final(loc).first())


// HEIGHT FITTING
// HEIGHT/WIDTH FITTING

#let _size-to-pt(size, styles, container-dimension) = {
let to-convert = size
Expand All @@ -60,7 +60,9 @@
box(width: mutable-width, body)
}

#let fit-to-height(height, width: none, prescale-width: none, body) = {
#let fit-to-height(
width: none, prescale-width: none, grow: true, shrink: true, height, body
) = {
// Place two labels with the requested vertical separation to be able to
// measure their vertical distance in pt.
// Using this approach instead of using `measure` allows us to accept fractions
Expand Down Expand Up @@ -113,22 +115,54 @@
let w-ratio = mutable-width / size.width
let ratio = calc.min(h-ratio, w-ratio) * 100%

let new-width = size.width * ratio
v(-available-height)
// If not boxed, the content can overflow to the next page even though it will fit.
// This is because scale doesn't update the layout information.
// Boxing in a container without clipping will inform typst that content
// will indeed fit in the remaining space
box(
width: new-width,
height: available-height,
scale(x: ratio, y: ratio, origin: top + left, boxed-content)
)
if (
(shrink and (ratio < 100%))
or (grow and (ratio > 100%))
) {
let new-width = size.width * ratio
v(-available-height)
// If not boxed, the content can overflow to the next page even though it will
// fit. This is because scale doesn't update the layout information.
// Boxing in a container without clipping will inform typst that content
// will indeed fit in the remaining space
box(
width: new-width,
height: available-height,
scale(x: ratio, y: ratio, origin: top + left, boxed-content)
)
} else {
body
}
})
})
})
}

#let fit-to-width(grow: true, shrink: true, width, content) = {
style(styles => {
layout(layout-size => {
let content-size = measure(content, styles)
let content-width = content-size.width
let width = _size-to-pt(width, styles, layout-size.width)
if (
(shrink and (width < content-width))
or (grow and (width > content-width))
) {
let ratio = width / content-width * 100%
// The first box keeps content from prematurely wrapping
let scaled = scale(
box(content, width: content-width), origin: top + left, x: ratio, y: ratio
)
// The second box lets typst know the post-scaled dimensions, since `scale`
// doesn't update layout information
box(scaled, width: width, height: content-size.height * ratio)
} else {
content
}
})
})
}

// SIDE BY SIDE

#let side-by-side(columns: none, gutter: 1em, ..bodies) = {
Expand Down