Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

29 Commits

Repository files navigation

typ 🖨️

A small Zig ⚡ module, as a convenience for me when writing WebAssembly plugins for Typst

Note

Initially based on the hello.zig example in wasm-minimal-protocol

Requirements

You will want to have a fairly recent Zig as well as the Typst CLI

Important

I had to rustup default 1.79.0 when compiling the latest typst as there were some breaking change in 1.80.0

Tip

Some of the software that I have installed for a pretty nice Typst workflow in Neovim:

Usage

Use zig fetch to add a .typ to the .dependencies in your build.zig.zon

zig fetch --save https://github.com/peterhellberg/typ/archive/refs/tags/v0.1.0.tar.gz

Note

You should now be able to update your build.zig as described below.

build.zig

conststd=@import("std");
pubfnbuild(b: *std.Build) void {
consttarget=b.resolveTargetQuery(.{
.cpu_arch=.wasm32,
.os_tag=.freestanding,
});
consthello=b.addExecutable(.{
.name="hello",
.root_module=b.createModule(.{
.root_source_file=b.path("hello.zig"),
.strip=true,
.target=target,
.optimize=.ReleaseSmall,
}),
});
consttyp=b.dependency("typ", .{}).module("typ");
hello.root_module.addImport("typ", typ);
hello.entry=.disabled;
hello.rdynamic=true;
b.installArtifact(hello);
}

hello.zig

consttyp=@import("typ");
exportfnhello() i32 {
constmsg="*Hello* from `hello.wasm` written in Zig!";
returntyp.str(msg);
}
exportfnecho(len: usize) i32 {
varres=typ.alloc(u8, len*2) catchreturn1;
defertyp.free(res);
typ.write(res.ptr);
for (0..len) |i| {
res[i+len] =res[i];
}
returntyp.ok(res);
}

hello.typ

#setpage(width: 10cm, height: 10cm)
#settext(font: "Inter")
== A WebAssembly plugin for Typst#line(length: 100%)
#emph[Typst is capable of interfacing with plugins compiled to WebAssembly.]
#line(length: 100%)
#letp= plugin("zig-out/bin/hello.wasm")
#eval(str(p.hello()), mode: "markup")
#eval(str(p.echo(bytes("1+2"))), mode: "code")

Expected output

hello.png

About

A small Zig ⚡ module, as a convenience for me when writing WebAssembly plugins for Typst

Topics

Resources

Stars

20 stars

Watchers

1 watching

Forks

Releases

Contributors

Languages