Add page labels
This commit is contained in:
43
src/lib.rs
43
src/lib.rs
@@ -2,6 +2,7 @@ use std::{
|
|||||||
cmp::{max, min},
|
cmp::{max, min},
|
||||||
ffi::{CStr, CString, OsStr, c_void},
|
ffi::{CStr, CString, OsStr, c_void},
|
||||||
fs,
|
fs,
|
||||||
|
hash::Hash,
|
||||||
os::unix::ffi::OsStrExt,
|
os::unix::ffi::OsStrExt,
|
||||||
path::Path,
|
path::Path,
|
||||||
ptr::{self, NonNull},
|
ptr::{self, NonNull},
|
||||||
@@ -11,7 +12,7 @@ use std::{
|
|||||||
use ::typst::{
|
use ::typst::{
|
||||||
Document,
|
Document,
|
||||||
layout::{Abs, FrameItem, Page, Point, Rect},
|
layout::{Abs, FrameItem, Page, Point, Rect},
|
||||||
model::Destination,
|
model::{Destination, Numbering, NumberingPattern},
|
||||||
text::{Glyph, TextItem},
|
text::{Glyph, TextItem},
|
||||||
};
|
};
|
||||||
use cairo::{Format, ImageSurface};
|
use cairo::{Format, ImageSurface};
|
||||||
@@ -19,7 +20,16 @@ use cairo::{Format, ImageSurface};
|
|||||||
use crate::{
|
use crate::{
|
||||||
typst::FrameItemIterator,
|
typst::FrameItemIterator,
|
||||||
zathura::{
|
zathura::{
|
||||||
cairo_t, girara_list_t, zathura_document_get_data, zathura_document_get_path, zathura_document_s, zathura_document_set_data, zathura_document_set_number_of_pages, zathura_link_destination_type_e_ZATHURA_LINK_DESTINATION_UNKNOWN, zathura_link_destination_type_e_ZATHURA_LINK_DESTINATION_XYZ, zathura_link_free, zathura_link_new, zathura_link_target_s, zathura_link_type_e_ZATHURA_LINK_GOTO_DEST, zathura_link_type_e_ZATHURA_LINK_GOTO_REMOTE, zathura_link_type_e_ZATHURA_LINK_URI, zathura_page_get_document, zathura_page_get_index, zathura_page_set_data, zathura_page_set_height, zathura_page_set_width, zathura_page_t, zathura_rectangle_s, GiraraList, ZathuraResult
|
GiraraList, ZathuraResult, cairo_t, girara_list_t, zathura_document_get_data,
|
||||||
|
zathura_document_get_path, zathura_document_s, zathura_document_set_data,
|
||||||
|
zathura_document_set_number_of_pages,
|
||||||
|
zathura_link_destination_type_e_ZATHURA_LINK_DESTINATION_UNKNOWN,
|
||||||
|
zathura_link_destination_type_e_ZATHURA_LINK_DESTINATION_XYZ, zathura_link_free,
|
||||||
|
zathura_link_new, zathura_link_target_s, zathura_link_type_e_ZATHURA_LINK_GOTO_DEST,
|
||||||
|
zathura_link_type_e_ZATHURA_LINK_GOTO_REMOTE, zathura_link_type_e_ZATHURA_LINK_URI,
|
||||||
|
zathura_page_get_document, zathura_page_get_index, zathura_page_set_data,
|
||||||
|
zathura_page_set_height, zathura_page_set_width, zathura_page_t, zathura_plugin_error_e,
|
||||||
|
zathura_rectangle_s,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -55,7 +65,7 @@ pub static zathura_plugin_6_7: zathura::zathura_plugin_definition_s =
|
|||||||
page_get_text: Some(page_get_text),
|
page_get_text: Some(page_get_text),
|
||||||
page_get_selection: Some(page_get_selection),
|
page_get_selection: Some(page_get_selection),
|
||||||
page_render_cairo: Some(page_render_cairo),
|
page_render_cairo: Some(page_render_cairo),
|
||||||
page_get_label: None,
|
page_get_label: Some(page_get_label),
|
||||||
page_get_signatures: None,
|
page_get_signatures: None,
|
||||||
},
|
},
|
||||||
mime_types_size: 1,
|
mime_types_size: 1,
|
||||||
@@ -414,7 +424,7 @@ unsafe extern "C" fn page_links_get(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
unsafe { zathura_link_new(ty, rect.into(), target) }
|
unsafe { zathura_link_new(ty, rect.into(), target) }
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -426,5 +436,30 @@ unsafe extern "C" fn page_links_get(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsafe extern "C" fn page_get_label(
|
||||||
|
zpage: *mut zathura_page_t,
|
||||||
|
page: *mut c_void,
|
||||||
|
out: *mut *mut i8,
|
||||||
|
) -> ZathuraResult {
|
||||||
|
let doc = unsafe {
|
||||||
|
&mut *(zathura_document_get_data(zathura_page_get_document(zpage)) as *mut TypstDocument)
|
||||||
|
};
|
||||||
|
let page = unsafe { &*(page as *mut Page) };
|
||||||
|
let numbering = match page.numbering.clone() {
|
||||||
|
Some(Numbering::Pattern(numbering)) => numbering,
|
||||||
|
_ => "1".parse().expect("1 is a valid numbering style"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let label = numbering.apply(&[page.number]);
|
||||||
|
let label = doc
|
||||||
|
.cstring_cache
|
||||||
|
.entry(label.to_string())
|
||||||
|
.or_insert_with_key(|k| CString::new(k.to_owned()).expect("It's a string"))
|
||||||
|
.as_ptr();
|
||||||
|
|
||||||
|
unsafe { out.write(label as _) };
|
||||||
|
ZathuraResult::OK
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: render warnings
|
// TODO: render warnings
|
||||||
// TODO: better caching
|
// TODO: better caching
|
||||||
|
|||||||
Reference in New Issue
Block a user