Skip to content

Commit

Permalink
Fix #2596
Browse files Browse the repository at this point in the history
When continuing to use a page object after the PDF has been saved with garbage options may change the MuPDF cache such that e.g. rendering the page after the save will produce wrong appearance or even garbled appearance.
The user needs to reload the page (page = doc.reload(page)) before using it further, especially for rendering.
The reload method now always empties the cache.
  • Loading branch information
JorjMcKie committed Oct 3, 2023
1 parent b5fb574 commit 7fe47ba
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions fitz/fitz.i
Original file line number Diff line number Diff line change
Expand Up @@ -4625,6 +4625,7 @@ if basestate:
old_annots[k] = v
page._erase() # remove the page
page = None
TOOLS.store_shrink(100)
page = self.load_page(pno) # reload the page

# copy annot refs over to the new dictionary
Expand Down
1 change: 1 addition & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5042,6 +5042,7 @@ def reload_page(self, page: "struct Page *") -> "struct Page *":
old_annots[k] = v
page._erase() # remove the page
page = None
TOOLS.store_shrink(100)
page = self.load_page(pno) # reload the page

# copy annot refs over to the new dictionary
Expand Down
14 changes: 14 additions & 0 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,17 @@ def test_2692():
clip=fitz.Rect(0,0,10,10),
)


def test_2596():
"""Cconfirm correctly abandoning cache when reloading a page."""
doc = fitz.Document("resources/test_2596.pdf")
page = doc[0]
pix0 = page.get_pixmap() # render the page
_ = doc.tobytes(garbage=3) # save with garbage collection

# Note this will invalidate cache content for this page.
# Reloading the page now empties the cache, so rendering
# will deliver the same pixmap
page = doc.reload_page(page)
pix1 = page.get_pixmap()
assert pix1.samples == pix0.samples

0 comments on commit 7fe47ba

Please sign in to comment.