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/src/lib.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'typst/src/lib.rs') 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)); + } } -- cgit v1.2.3