aboutsummaryrefslogtreecommitdiff
path: root/typst/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'typst/src/lib.rs')
-rw-r--r--typst/src/lib.rs23
1 files changed, 18 insertions, 5 deletions
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) }
+}