Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test for issue 60 #61

Merged
merged 3 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions mupdf-sys/wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
20 changes: 14 additions & 6 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ impl Buffer {
Ok(Self { inner, offset: 0 })
}

pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
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 }
Expand All @@ -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<usize, Error> {
let len = buf.len();
let read_len = unsafe {
Expand Down Expand Up @@ -125,19 +137,15 @@ impl TryFrom<&[u8]> for Buffer {
type Error = Error;

fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
let mut buf = Buffer::with_capacity(bytes.len());
buf.write_bytes(bytes)?;
Ok(buf)
Buffer::from_bytes(bytes)
}
}

impl TryFrom<Vec<u8>> for Buffer {
type Error = Error;

fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
let mut buf = Buffer::with_capacity(bytes.len());
buf.write_bytes(&bytes)?;
Ok(buf)
Buffer::from_bytes(&bytes)
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/font.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -75,14 +74,13 @@ impl Font {

pub fn from_bytes_with_index(name: &str, index: i32, font_data: &[u8]) -> Result<Self, Error> {
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 })
Expand Down
Binary file added tests/files/p11.pdf
Binary file not shown.
17 changes: 17 additions & 0 deletions tests/test_issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Loading