Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Last active September 11, 2024 16:00
Show Gist options
  • Select an option

  • Save peterhellberg/bfabcabbc07d2202bc3de445299d90dc to your computer and use it in GitHub Desktop.

Select an option

Save peterhellberg/bfabcabbc07d2202bc3de445299d90dc to your computer and use it in GitHub Desktop.
A WebAssembly plugin for Typst
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.resolveTargetQuery(.{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
});
const hello = b.addExecutable(.{
.name = "hello",
.root_source_file = b.path("hello.zig"),
.target = target,
.optimize = .ReleaseSmall,
.strip = true,
});
hello.entry = .disabled;
hello.rdynamic = true;
b.installArtifact(hello);
}
#set page(width: 10cm, height: 10cm)
#set text(font: "Inter")
== A WebAssembly plugin for Typst
#line(length: 100%)
#emph[Typst is capable of interfacing with plugins compiled to WebAssembly.]
#line(length: 100%)
#let wasm = plugin("zig-out/bin/hello.wasm")
#let resp = str(wasm.hello())
#eval(resp, mode: "markup")
const std = @import("std");
const allocator = std.heap.page_allocator;
export fn hello() i32 {
const message = "*Hello* from `hello.wasm` written in Zig!";
wasm_minimal_protocol_send_result_to_host(message.ptr, message.len);
return 0;
}
// ===
// Functions for the protocol
extern "typst_env" fn wasm_minimal_protocol_send_result_to_host(ptr: [*]const u8, len: usize) void;
extern "typst_env" fn wasm_minimal_protocol_write_args_to_buffer(ptr: [*]u8) void;
@peterhellberg

Copy link
Copy Markdown
Author

Note

Typst is a new markup-based typesetting system that is designed to be as powerful as LaTeX while being much easier to learn and use.

Links

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