diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-29 16:28:14 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-29 16:28:14 +0200 |
| commit | 99d2408ad3113f3d79b496ed30798bbd15bc6cfc (patch) | |
| tree | f7cd38f5c7ca985982c649ab7853f48528ff7da9 /typst | |
| parent | 007af47ed42931b887483be6ec95073c198ab07f (diff) | |
feat(typst): escape string in math mode
Diffstat (limited to 'typst')
| -rw-r--r-- | typst/example.c | 2 | ||||
| -rw-r--r-- | typst/src/lib.rs | 23 | ||||
| -rw-r--r-- | typst/typst.h | 4 |
3 files changed, 22 insertions, 7 deletions
diff --git a/typst/example.c b/typst/example.c index a1df659..39e7a06 100644 --- a/typst/example.c +++ b/typst/example.c @@ -4,6 +4,6 @@ int main() { const char* res = typst_generateSVG("Hello world"); printf("%s\n", res); - typst_freeSVG(res); + typst_freeString(res); return 0; } diff --git a/typst/src/lib.rs b/typst/src/lib.rs index c561c19..83a0afb 100644 --- a/typst/src/lib.rs +++ b/typst/src/lib.rs @@ -1,5 +1,5 @@ //! Based on https://github.com/zeon256/minimal-typst-svg-renderer -use std::ffi::{CString, CStr}; +use std::ffi::{CStr, CString}; use std::os::raw::c_char; use typst::layout::PagedDocument; @@ -23,17 +23,30 @@ pub fn compile(content: &str) -> String { svg_frame(&doc.pages[0].frame) } -#[unsafe(no_mangle)] -pub extern "C" fn typst_generateSVG(source: *const c_char) -> *const c_char { +pub fn escape_math(content: &str) -> String { + content.replace("$", r"\$") +} + +unsafe fn convert_call(source: *const c_char, f: fn(&str) -> String) -> *const c_char { unsafe { - let res = compile(CStr::from_ptr(source).to_str().unwrap()); + let res = f(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) { +pub unsafe extern "C" fn typst_generateSVG(source: *const c_char) -> *const c_char { + unsafe { convert_call(source, compile) } +} + +#[unsafe(no_mangle)] +pub unsafe extern "C" fn typst_freeString(res: *mut c_char) { unsafe { drop(CString::from_raw(res)); } } + +#[unsafe(no_mangle)] +pub unsafe extern "C" fn typst_espaceMath(source: *const c_char) -> *const c_char { + unsafe { convert_call(source, escape_math) } +} diff --git a/typst/typst.h b/typst/typst.h index ca6c21f..755314a 100644 --- a/typst/typst.h +++ b/typst/typst.h @@ -1,2 +1,4 @@ extern const char *typst_generateSVG(const char*); -extern void typst_freeSVG(const char*); +extern const char *typst_escapeMath(const char*); + +extern void typst_freeString(const char*); |
