seems to work
This commit is contained in:
parent
040afcc435
commit
73c45323c8
2 changed files with 234 additions and 92 deletions
|
|
@ -2,14 +2,16 @@
|
|||
// See Notices.txt for copyright information
|
||||
|
||||
use mupdf_sys::{
|
||||
fz_clone_context, fz_color_params, fz_colorspace, fz_concat, fz_context, fz_device,
|
||||
fz_document, fz_drop_context, fz_drop_device, fz_drop_document, fz_drop_page, fz_drop_path,
|
||||
fz_drop_text, fz_error_type_FZ_ERROR_GENERIC, fz_font, fz_font_ascender, fz_font_descender,
|
||||
fz_font_is_bold, fz_font_is_italic, fz_font_name, fz_matrix, fz_matrix_expansion, fz_page,
|
||||
fz_path, fz_path_walker, fz_point, fz_rect, fz_stroke_state, fz_text, fz_text_item,
|
||||
fz_text_span, fz_transform_point, fz_transform_point_xy, fz_transform_vector, fz_walk_path,
|
||||
mupdf_document_page_count, mupdf_drop_error, mupdf_error_t, mupdf_load_page,
|
||||
mupdf_new_base_context, mupdf_new_derived_device, mupdf_open_document, mupdf_run_page,
|
||||
fz_buffer, fz_buffer_storage, fz_clone_context, fz_color_params, fz_colorspace, fz_concat,
|
||||
fz_context, fz_device, fz_document, fz_drop_buffer, fz_drop_context, fz_drop_device,
|
||||
fz_drop_document, fz_drop_page, fz_drop_path, fz_drop_text, fz_error_type_FZ_ERROR_GENERIC,
|
||||
fz_font, fz_font_ascender, fz_font_descender, fz_font_is_bold, fz_font_is_italic, fz_font_name,
|
||||
fz_matrix, fz_matrix_expansion, fz_page, fz_path, fz_path_walker, fz_point, fz_rect,
|
||||
fz_stroke_state, fz_text, fz_text_item, fz_text_span, fz_transform_point,
|
||||
fz_transform_point_xy, fz_transform_vector, fz_walk_path, mupdf_document_page_count,
|
||||
mupdf_drop_error, mupdf_error_t, mupdf_load_page, mupdf_new_base_context,
|
||||
mupdf_new_derived_device, mupdf_open_document, mupdf_page_to_xml, mupdf_pdf_page_transform,
|
||||
mupdf_run_page, pdf_page, pdf_page_from_fz_page,
|
||||
};
|
||||
use std::{
|
||||
cell::{Cell, UnsafeCell},
|
||||
|
|
@ -172,6 +174,33 @@ impl<'ctx> Drop for Document<'ctx> {
|
|||
}
|
||||
}
|
||||
|
||||
struct Buffer<'ctx> {
|
||||
ptr: *mut fz_buffer,
|
||||
ctx: ContextRef<'ctx>,
|
||||
}
|
||||
|
||||
impl<'ctx> Buffer<'ctx> {
|
||||
fn storage(&mut self) -> &mut [u8] {
|
||||
unsafe {
|
||||
let mut ptr = ptr::null_mut();
|
||||
let len = fz_buffer_storage(self.ctx.0.get(), self.ptr, &raw mut ptr);
|
||||
if len == 0 {
|
||||
&mut []
|
||||
} else {
|
||||
std::slice::from_raw_parts_mut(ptr, len)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ctx> Drop for Buffer<'ctx> {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
fz_drop_buffer(self.ctx.0.get(), self.ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct Page<'ctx> {
|
||||
ptr: *mut fz_page,
|
||||
ctx: ContextRef<'ctx>,
|
||||
|
|
@ -199,6 +228,25 @@ impl<'ctx> Page<'ctx> {
|
|||
})
|
||||
}
|
||||
}
|
||||
pub(crate) fn to_xml(&self) -> Result<String, MuPdfError> {
|
||||
unsafe {
|
||||
let mut buffer =
|
||||
mupdf_try(|errptr| mupdf_page_to_xml(self.ctx.0.get(), self.ptr, errptr))
|
||||
.map(|ptr| Buffer { ptr, ctx: self.ctx })?;
|
||||
Ok(str::from_utf8(buffer.storage())
|
||||
.map_err(MuPdfError::new_generic)?
|
||||
.into())
|
||||
}
|
||||
}
|
||||
pub(crate) fn pdf_page<'a>(&'a self) -> Option<PdfPageRef<'a, 'ctx>> {
|
||||
unsafe {
|
||||
let ptr = pdf_page_from_fz_page(self.ctx.0.get(), self.ptr);
|
||||
NonNull::new(ptr).map(|ptr| PdfPageRef {
|
||||
ptr: &*ptr.as_ptr().cast(),
|
||||
ctx: self.ctx,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ctx> Drop for Page<'ctx> {
|
||||
|
|
@ -209,6 +257,20 @@ impl<'ctx> Drop for Page<'ctx> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) struct PdfPageRef<'a, 'ctx> {
|
||||
ptr: &'a UnsafeCell<pdf_page>,
|
||||
ctx: ContextRef<'ctx>,
|
||||
}
|
||||
|
||||
impl<'a, 'ctx> PdfPageRef<'a, 'ctx> {
|
||||
pub(crate) fn transform(self) -> Result<fz_matrix, MuPdfError> {
|
||||
unsafe {
|
||||
mupdf_try(|errptr| mupdf_pdf_page_transform(self.ctx.0.get(), self.ptr.get(), errptr))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct Device<'ctx, T: 'ctx> {
|
||||
dev: *mut fz_device,
|
||||
ctx: ContextRef<'ctx>,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue