graphemy-typst/typst/graphemy.typ
2025-06-17 17:17:19 +02:00

60 lines
1.8 KiB
Typst

#let the-plugin = {
plugin("./graphemy_typst.wasm")
}
#let load-gmy(graphemy-project) = {
let script = plugin.transition(
the-plugin.initialise,
read(encoding: none, graphemy-project)
)
(
text,
..opt-args
) => {
let opts = opt-args.named()
let scale = opts.at("scale", default: 1)
let has(dict, key) = {dict.keys().contains(key)}
let torgba(c) = color.rgb(c).components(alpha: true).map(it => int(float(it)*255))
let render= (
size:
if has(opts, "width") and has(opts, "height") {(MaxRect: (opts.width.pt(), opts.height.pt()))}
else if has(opts, "width") {(Width: opts.width.pt() / scale)}
else if has(opts, "height") {(Height: opts.height.pt() / scale)}
else {(Scale: 1.0)},
margin: opts.at("margin", default: 0.0),
comment: opts.at("comment", default: none),
color_mode: (Static: (
fg: torgba(opts.at("fg", default: color.black)),
bg: if has(opts, "bg") { torgba(opts.bg) } else { none }
)),
)
let typeset= (
area: (LineSize: opts.at("linewidth", default: opts.at("width", default: 100pt).pt() / scale)),
align: (min: "Min", max: "Max", center: "Center", justify: "Justify").at(
opts.at("align", default: "justify")
),
)
let imgopts = (:)
if has(opts, "width") {
imgopts.insert("width", opts.width)
}
if has(opts, "height") {
imgopts.insert("width", opts.height * scale)
}
let boxopts = (:)
if has(opts, "baseline") {boxopts.insert("baseline", opts.baseline)}
box(..boxopts, image(format: "svg", ..imgopts, script.render(bytes(json.encode(render)), bytes(json.encode(typeset)), bytes(text))))
}
}
#let all_options = (
width: 100pt,
hegith: 100pt,
margin: 5,
comment: "comment",
fg: color.black,
bg: color.white,
align: "justify"
)