diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 5e89133..13d8040 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -80,8 +80,9 @@ jobs: fetch-depth: 500 - uses: dtolnay/rust-toolchain@stable - uses: taiki-e/install-action@valgrind - - uses: taiki-e/install-action@cargo-valgrind - - run: cargo valgrind test + - run: cargo test + env: + CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER: "valgrind --error-exitcode=1 --track-origins=yes" fmt: name: Rustfmt diff --git a/mupdf-sys/wrapper.c b/mupdf-sys/wrapper.c index dc5b4a2..97390c4 100644 --- a/mupdf-sys/wrapper.c +++ b/mupdf-sys/wrapper.c @@ -369,12 +369,12 @@ fz_font *mupdf_new_font(fz_context *ctx, const char *name, int index, mupdf_erro return font; } -fz_font *mupdf_new_font_from_memory(fz_context *ctx, const char *name, int index, const unsigned char *data, int data_len, mupdf_error_t **errptr) +fz_font *mupdf_new_font_from_buffer(fz_context *ctx, const char *name, int index, fz_buffer *buffer, mupdf_error_t **errptr) { fz_font *font = NULL; fz_try(ctx) { - font = fz_new_font_from_memory(ctx, name, data, data_len, index, 0); + font = fz_new_font_from_buffer(ctx, name, buffer, index, 0); } fz_catch(ctx) { diff --git a/src/buffer.rs b/src/buffer.rs index d943866..7ad53a7 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -39,6 +39,12 @@ impl Buffer { Ok(Self { inner, offset: 0 }) } + pub fn from_bytes(bytes: &[u8]) -> Result { + let mut buf = Buffer::with_capacity(bytes.len()); + buf.write_bytes(bytes)?; + Ok(buf) + } + pub fn with_capacity(cap: usize) -> Self { let inner = unsafe { fz_new_buffer(context(), cap) }; Self { inner, offset: 0 } @@ -48,6 +54,12 @@ impl Buffer { unsafe { fz_buffer_storage(context(), self.inner, ptr::null_mut()) } } + pub fn into_inner(mut self) -> *mut fz_buffer { + let inner = self.inner; + self.inner = ptr::null_mut(); + inner + } + fn read_bytes(&mut self, buf: &mut [u8]) -> Result { let len = buf.len(); let read_len = unsafe { @@ -125,9 +137,7 @@ impl TryFrom<&[u8]> for Buffer { type Error = Error; fn try_from(bytes: &[u8]) -> Result { - let mut buf = Buffer::with_capacity(bytes.len()); - buf.write_bytes(bytes)?; - Ok(buf) + Buffer::from_bytes(bytes) } } @@ -135,9 +145,7 @@ impl TryFrom> for Buffer { type Error = Error; fn try_from(bytes: Vec) -> Result { - let mut buf = Buffer::with_capacity(bytes.len()); - buf.write_bytes(&bytes)?; - Ok(buf) + Buffer::from_bytes(&bytes) } } diff --git a/src/font.rs b/src/font.rs index 0ccad97..3175ce0 100644 --- a/src/font.rs +++ b/src/font.rs @@ -1,12 +1,11 @@ use std::ffi::{CStr, CString}; use std::fmt; -use std::os::raw::c_int; use std::str::FromStr; use mupdf_sys::*; use num_enum::TryFromPrimitive; -use crate::{context, Error, Matrix, Path}; +use crate::{context, Buffer, Error, Matrix, Path}; #[derive(Debug, Clone, Copy, PartialEq)] #[repr(C)] @@ -75,14 +74,13 @@ impl Font { pub fn from_bytes_with_index(name: &str, index: i32, font_data: &[u8]) -> Result { let c_name = CString::new(name)?; - let data_len = font_data.len() as c_int; + let buffer = Buffer::from_bytes(font_data)?; let inner = unsafe { - ffi_try!(mupdf_new_font_from_memory( + ffi_try!(mupdf_new_font_from_buffer( context(), c_name.as_ptr(), index, - font_data.as_ptr(), - data_len + buffer.into_inner() )) }; Ok(Self { inner }) diff --git a/tests/files/p11.pdf b/tests/files/p11.pdf new file mode 100644 index 0000000..e6af9e9 Binary files /dev/null and b/tests/files/p11.pdf differ diff --git a/tests/test_issues.rs b/tests/test_issues.rs index a711915..00ceab7 100644 --- a/tests/test_issues.rs +++ b/tests/test_issues.rs @@ -63,3 +63,20 @@ fn test_issue_43_malloc() { writer.end_page(device).unwrap(); } } + +#[test] +fn test_issue_60_display_list() { + let doc = PdfDocument::open("tests/files/p11.pdf").unwrap(); + let num_pages = doc.page_count().unwrap(); + println!("Document has {} page(s)", num_pages); + + let _display_list: Vec<(usize, mupdf::DisplayList)> = doc + .pages() + .unwrap() + .enumerate() + .map(|(index, p)| { + let display = p.unwrap().to_display_list(true).unwrap(); + (index, display) + }) + .collect(); +}