diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-29 16:14:49 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-29 16:14:49 +0200 |
| commit | 007af47ed42931b887483be6ec95073c198ab07f (patch) | |
| tree | e626be89b4ea0f61025d6a85ef2d7075b878371e /typst/src/lib.rs | |
| parent | 4551f9c554c1fef32ef2b7e55b01b1fb90186cbf (diff) | |
feat(typst): extern rust functions for C ABI
Diffstat (limited to 'typst/src/lib.rs')
| -rw-r--r-- | typst/src/lib.rs | 23 |
1 files changed, 19 insertions, 4 deletions
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::<PagedDocument>(&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)); + } } |
