Skip to content

Instantly share code, notes, and snippets.

@baptiste
Last active June 4, 2025 05:56
Show Gist options
  • Save baptiste/c9f1c885445e44a23017ec43b3d25c76 to your computer and use it in GitHub Desktop.
Save baptiste/c9f1c885445e44a23017ec43b3d25c76 to your computer and use it in GitHub Desktop.
## query sizes
typst query test.typ --input strings='["foo", "$ alpha = beta / (gamma + x^2)$"]' '<dimensions>' --field value
## produce images
typst compile test.typ --input strings='["foo", "$ alpha = beta / (gamma + x^2)$"]' '{p}.svg'
#let test-strings = `
["0", "0.5", "1", "Typst"]
`.text
#let strings = sys.inputs.at("strings", default: test-strings)
#let strings = json(bytes(strings))
#let strings = strings.map(s => eval(s, mode: "markup"))
#let measurements(body) = context {
let (width, height) = measure({
set text(top-edge: "bounds", bottom-edge: "bounds")
body
})
let ascender = measure({
set text(top-edge: "ascender", bottom-edge: "baseline")
body
}).height
let descender = measure({
set text(top-edge: "baseline", bottom-edge: "descender")
body
}).height
let dimensions = (height: height, width: width, descender: descender, ascender: ascender)
[#metadata(dimensions)<dimensions>]
}
#set page(width: auto, height: auto, margin: 0pt)
#set text(top-edge: "bounds", bottom-edge: "bounds")
#for string in strings {
measurements(string)
string
pagebreak()
}
// for debugging only
#context query(<dimensions>).map(m => m.value)
@baptiste
Copy link
Author

baptiste commented Jun 4, 2025

[{"height":"7.79pt","width":"14.58pt","descender":"2.71pt","ascender":"9.83pt"},{"height":"16.03pt","width":"43.99pt","descender":"7.59pt","ascender":"9.44pt"}]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment