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

make corrections #122

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion mupdf-sys/wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ fz_rect mupdf_bound_page(fz_context *ctx, fz_page *page, mupdf_error_t **errptr)
return rect;
}

fz_pixmap *mupdf_page_to_pixmap(fz_context *ctx, fz_page *page, fz_matrix ctm, fz_colorspace *cs, float alpha, bool show_extras, mupdf_error_t **errptr)
fz_pixmap *mupdf_page_to_pixmap(fz_context *ctx, fz_page *page, fz_matrix ctm, fz_colorspace *cs, bool alpha, bool show_extras, mupdf_error_t **errptr)
{
fz_pixmap *pixmap = NULL;
fz_try(ctx)
Expand Down
17 changes: 15 additions & 2 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,21 @@ impl Document {
Self { inner: ptr }
}

pub fn open(filename: &str) -> Result<Self, Error> {
let c_name = CString::new(filename)?;
pub fn open<P: AsRef<std::path::Path>>(p: P) -> Result<Self, Error> {
// Must be UTF8 on Windows
#[cfg(windows)]
let raw = match <&str>::try_from(p.as_ref().as_os_str()) {
Ok(s) => s,
Err(e) => {
use std::io::{self, ErrorKind};
let err = Error::Io(io::Error::new(ErrorKind::Other, e));
return Err(err);
}
};

#[cfg(not(windows))]
let raw = p.as_ref().as_os_str().as_encoded_bytes();
let c_name = CString::new(raw)?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure about this code. I understand the idea, however we are setting the users up with the expectation that every path is accepted here, which then is not the case on windows.

Maybe creating a newtype that is From<&str> for X and AsRef<X> for &str while creatable from a Path if the path is utf-8 on windows. This would allow us to skip with check entirely if a &str is used, as we already know it's utf-8. It would also make it a bit clearer that this is a failure that could occur. I'm only throwing ideas out so maybe that's a bit overkill. Else this looks good.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I can create a newtype.

unsafe { ffi_try!(mupdf_open_document(context(), c_name.as_ptr())) }
.map(|inner| Self { inner })
}
Expand Down
2 changes: 1 addition & 1 deletion src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Page {
&self,
ctm: &Matrix,
cs: &Colorspace,
alpha: f32,
alpha: bool,
show_extras: bool,
) -> Result<Pixmap, Error> {
unsafe {
Expand Down
2 changes: 1 addition & 1 deletion tests/test_issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn test_issue_16_pixmap_to_png() {
let page = document.load_page(0).unwrap();
let matrix = Matrix::new_scale(72f32 / 72f32, 72f32 / 72f32);
let pixmap = page
.to_pixmap(&matrix, &Colorspace::device_rgb(), 0.0, true)
.to_pixmap(&matrix, &Colorspace::device_rgb(), false, true)
.unwrap();
pixmap
.save_as("tests/output/test.png", ImageFormat::PNG)
Expand Down