Created
November 1, 2024 20:26
-
-
Save PhotonQuantum/81057d726256fc26a05b3e35f5468f7d to your computer and use it in GitHub Desktop.
Adaptive content size for Touying
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This theme is from https://github.com/andreasKroepelin/polylux/blob/main/themes/simple.typ | |
// Author: Andreas Kröpelin | |
#import "@preview/touying:0.5.3": * | |
#import themes.simple: * | |
// https://forum.typst.app/t/how-to-auto-size-text-and-images/1290 | |
#let fill-height-with-text(min: 0.3em, max: 5em, eps: 0.1em, debug: false, it) = layout(size => { | |
let fits(text-size, it) = { | |
measure(width: size.width, { | |
set text(text-size) | |
it | |
}).height <= size.height | |
} | |
let debug-msg = [] | |
if not fits(min, it) { | |
debug-msg += "Failed: min size too large" | |
it | |
} else if fits(max, it) { | |
debug-msg += "1 render" | |
set text(max) | |
it | |
} else { | |
let (a, b) = (min, max) | |
let cnt = 0 | |
while b - a > eps { | |
cnt += 1 | |
let new = 0.5 * (a + b) | |
if fits(new, it) { | |
a = new | |
} else { | |
b = new | |
} | |
} | |
debug-msg += [#cnt renders] | |
set text(a) | |
it | |
} + if debug and debug-msg != [] { | |
place( | |
top + right, | |
block( | |
fill: teal.transparentize(50%), | |
inset: 6pt, | |
text(16pt)[ | |
#debug-msg | |
], | |
), | |
) | |
} else [] | |
}) | |
#let adaptive-composer = (composer: auto, debug: false, precise-size: true, ..bodies) => components.side-by-side( | |
composer: composer, | |
..bodies.pos().map(it => block( | |
height: 1fr, | |
fill-height-with-text( | |
max: 1em, | |
eps: if precise-size { | |
0.01em | |
} else { | |
0.1em | |
}, | |
debug: debug, | |
it, | |
), | |
)), | |
); | |
#let slide = (debug: false, precise-size: true, ..args) => slide( | |
composer: adaptive-composer.with(debug: debug, precise-size: precise-size), | |
..args, | |
) | |
// debug: display re-render attempts | |
// precise-size: trade size accuracy with speed | |
#let theme = (debug: false, precise-size: true, ..args) => simple-theme( | |
primary: black, | |
config-common(slide-fn: slide.with(debug: debug, precise-size: precise-size)), | |
..args, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment