From 007af47ed42931b887483be6ec95073c198ab07f Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Wed, 29 Apr 2026 16:14:49 +0200 Subject: feat(typst): extern rust functions for C ABI --- typst/.gitignore | 2 ++ typst/Cargo.toml | 3 ++- typst/README.md | 12 ++++++++++++ typst/example.c | 9 +++++++++ typst/src/lib.rs | 23 +++++++++++++++++++---- typst/typst.h | 2 ++ 6 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 typst/README.md create mode 100644 typst/example.c create mode 100644 typst/typst.h (limited to 'typst') diff --git a/typst/.gitignore b/typst/.gitignore index 8afaf0c..695217d 100644 --- a/typst/.gitignore +++ b/typst/.gitignore @@ -9,3 +9,5 @@ target/ # MSVC Windows builds of rustc generate these, which store debugging information *.pdb + +example diff --git a/typst/Cargo.toml b/typst/Cargo.toml index 4a01bf0..3cdc8a7 100644 --- a/typst/Cargo.toml +++ b/typst/Cargo.toml @@ -3,7 +3,8 @@ name = "typdown-typst" version = "0.1.0" edition = "2024" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +crate-type = ["cdylib"] [dependencies] typst = "0.14.2" diff --git a/typst/README.md b/typst/README.md new file mode 100644 index 0000000..6e2530f --- /dev/null +++ b/typst/README.md @@ -0,0 +1,12 @@ +# typst library + +This folder contains a Rust library used to compile typdown's typst components. +We cannot write it in Zig, because there are no existing C bindings for typst. + +## Build example + +```shell +$ cargo build +$ gcc example.c -o example -ltypdown_typst -L./target/debug +$ LD_LIBRARY_PATH=./target/debug ./example +``` diff --git a/typst/example.c b/typst/example.c new file mode 100644 index 0000000..a1df659 --- /dev/null +++ b/typst/example.c @@ -0,0 +1,9 @@ +#include "typst.h" +#include + +int main() { + const char* res = typst_generateSVG("Hello world"); + printf("%s\n", res); + typst_freeSVG(res); + return 0; +} diff --git a/typst/src/lib.rs b/typst/src/lib.rs index 5d9dcc4..c561c19 100644 --- a/typst/src/lib.rs +++ b/typst/src/lib.rs @@ -1,4 +1,6 @@ //! Based on https://github.com/zeon256/minimal-typst-svg-renderer +use std::ffi::{CString, CStr}; +use std::os::raw::c_char; use typst::layout::PagedDocument; use typst_svg::svg_frame; @@ -7,8 +9,7 @@ use crate::world::MinimalWorld; mod world; -pub fn compile() { - let content = include_str!("../../template.typ"); +pub fn compile(content: &str) -> String { let world = MinimalWorld::new(content); let res = typst::compile::(&world); @@ -19,6 +20,20 @@ pub fn compile() { let doc = res.output.expect("Error compiling typst"); - let svg = svg_frame(&doc.pages[0].frame); - println!("{}", svg) + svg_frame(&doc.pages[0].frame) +} + +#[unsafe(no_mangle)] +pub extern "C" fn typst_generateSVG(source: *const c_char) -> *const c_char { + unsafe { + let res = compile(CStr::from_ptr(source).to_str().unwrap()); + CString::new(res).unwrap().into_raw() + } +} + +#[unsafe(no_mangle)] +pub extern "C" fn typst_freeSVG(res: *mut c_char) { + unsafe { + drop(CString::from_raw(res)); + } } diff --git a/typst/typst.h b/typst/typst.h new file mode 100644 index 0000000..ca6c21f --- /dev/null +++ b/typst/typst.h @@ -0,0 +1,2 @@ +extern const char *typst_generateSVG(const char*); +extern void typst_freeSVG(const char*); -- cgit v1.2.3