Improve error handling: render errors through typst

This commit is contained in:
bluepython508
2025-10-31 16:41:53 +00:00
parent 01474ad6a0
commit 481369ea7c
5 changed files with 203 additions and 32 deletions

View File

@@ -4,13 +4,11 @@ use std::{
path::Path,
};
use ::typst::layout::Page;
use cairo::{Format, ImageSurface};
use crate::zathura::{
ZathuraResult, cairo_t, zathura_document_get_data, zathura_document_get_path,
zathura_document_s, zathura_document_set_data, zathura_document_set_number_of_pages,
zathura_page_get_document, zathura_page_get_index, zathura_page_set_height,
zathura_page_set_width, zathura_page_t,
cairo_t, zathura_document_get_data, zathura_document_get_path, zathura_document_s, zathura_document_set_data, zathura_document_set_number_of_pages, zathura_page_get_data, zathura_page_get_document, zathura_page_get_index, zathura_page_set_data, zathura_page_set_height, zathura_page_set_width, zathura_page_t, ZathuraResult
};
mod typst;
@@ -61,13 +59,10 @@ unsafe extern "C" fn document_open(doc: *mut zathura_document_s) -> ZathuraResul
CStr::from_ptr(zathura_document_get_path(doc)).to_bytes(),
))
};
let Ok(document) = TypstDocument::load(path) else {
return ZathuraResult::InvalidArguments;
};
let document = Box::new(document);
let document = Box::new(TypstDocument::load(path));
unsafe {
zathura_document_set_number_of_pages(doc, document.doc.pages.len() as _);
zathura_document_set_number_of_pages(doc, document.doc.pages.len() as u32);
zathura_document_set_data(doc, Box::into_raw(document) as _);
}
@@ -85,9 +80,11 @@ unsafe extern "C" fn page_init(page: *mut zathura_page_t) -> ZathuraResult {
let pageno = unsafe { zathura_page_get_index(page) };
let doc = unsafe { zathura_page_get_document(page) };
let typst_doc = unsafe { &mut *(zathura_document_get_data(doc) as *mut TypstDocument) };
let typst_page = &mut typst_doc.doc.pages[pageno as usize];
let typst_page: &Page = &typst_doc.doc.pages[pageno as usize];
unsafe {
zathura_page_set_data(page, typst_page as *const _ as _);
zathura_page_set_width(page, typst_page.frame.width().to_pt());
zathura_page_set_height(page, typst_page.frame.height().to_pt());
};
@@ -105,10 +102,7 @@ unsafe extern "C" fn page_render_cairo(
cairo: *mut cairo_t,
_printing: bool,
) -> ZathuraResult {
let pageno = unsafe { zathura_page_get_index(page) };
let doc = unsafe { zathura_page_get_document(page) };
let typst_doc = unsafe { &mut *(zathura_document_get_data(doc) as *mut TypstDocument) };
let typst_page = &mut typst_doc.doc.pages[pageno as usize];
let typst_page: &Page = unsafe { &*(zathura_page_get_data(page) as *mut _)};
let context = unsafe { cairo::Context::from_raw_none(cairo) };
let surface = context.target();
@@ -153,9 +147,9 @@ unsafe extern "C" fn page_render_cairo(
ZathuraResult::OK
}
// TODO: handle compile errors
// TODO: render warnings
// TODO: text/link/... handling
// TODO: better caching
// TODO: nixify compilation of magicdb (file -Cm ~/.magic:/nix/store/vi7ya34k19nid2m0dmkljqip5572g0bi-file-5.45/share/misc/magic.mgc)
/*
0 string/cW /*\ doc:\ typst\ */ typst text document