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

Ensure page rotation 0 for non-PDF documents #2946

Merged
merged 1 commit into from
Dec 27, 2023

Conversation

JorjMcKie
Copy link
Collaborator

Accessing page.rotation for non-PDF leads to an interpreter crash. Return zero as rotation for non-PDF documents.

src/__init__.py Outdated
Comment on lines 9249 to 9251
if not self.parent.is_pdf:
return 0
page = self.this if isinstance(self.this, mupdf.PdfPage) else mupdf.pdf_page_from_fz_page(self.this)
if not page:
return 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think a simpler fix might be to change if not page: to if not page.m_internal. [I thought i'd fixed all of these mistakes but i missed this one.]

[Actually we could write a function _as_fz_page() similar to as_pdf_page() and as_pdf_document(), which does the down-cast and returns with .m_internal None if not a pdf page, but that can be done separately.]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

let me try this one then.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Indeed, this check resolves the problem.
Is there any documentation about what property m_internal does? Feels like I should have known this ...

Copy link
Collaborator

Choose a reason for hiding this comment

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

Apart from POD types (e.g. fz_rect and FzRect), all C++ wrapper classes have an m_internal member which is a pointer to the underlying MuPDF C struct. So for example mupdf::PdfPage has a pdf_page* m_internal member.

If a MuPDF function returns a NULL pdf_page*, then when wraped in C++ or Python, it will return a PdfPage instance with a NULL or None m_internal. This unfortunately breaks simple translation of code that checks whether a pointer is non-NULL, as in this PR.

[I've often wondered about adding an operator bool() member function to the C++ wrapper classes, to generically avoid the problem, but have never got round to trying it out.]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah, ok, thanks for the explanation!

Copy link
Collaborator

Choose a reason for hiding this comment

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

There is very little information about m_internal at https://mupdf.readthedocs.io/en/latest/language-bindings.html. I've added a todo item to improve this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thank you!

Use success of fz_page conversion to pdf_page as an indicator for rotation = 0.
@JorjMcKie JorjMcKie merged commit 80d8588 into main Dec 27, 2023
2 checks passed
@JorjMcKie JorjMcKie deleted the worng-rotation-evaluation branch December 27, 2023 23:12
@github-actions github-actions bot locked and limited conversation to collaborators Dec 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants