From d6d0a6ea061df482324ac1607df8c24c78f6aa74 Mon Sep 17 00:00:00 2001 From: Johannes Schauer Marin Rodrigues Date: Sun, 26 Jun 2022 22:59:11 +0100 Subject: [PATCH] use new snake_case naming for pymupdf 1.19.0 and later --- plakativ.py | 76 ++++++++++++++++++++++++++++++++++++++---------- plakativ_test.py | 8 ++--- 2 files changed, 65 insertions(+), 19 deletions(-) diff --git a/plakativ.py b/plakativ.py index 5cbd622..67332c4 100755 --- a/plakativ.py +++ b/plakativ.py @@ -326,17 +326,30 @@ class Plakativ: return len(self.doc) def get_input_page_size(self): - width = self.doc[self.pagenr].getDisplayList().rect.width - height = self.doc[self.pagenr].getDisplayList().rect.height - return (width, height) + # since pymupdf 1.19.0 a warning will be issued if the deprecated names are used + if hasattr(self.doc[self.pagenr], "get_displaylist"): + gdl = self.doc[self.pagenr].get_displaylist + else: + gdl = self.doc[self.pagenr].getDisplayList + rect = gdl().rect + return (rect.width, rect.height) def get_image(self, zoom): mat_0 = fitz.Matrix(zoom, zoom) - pix = ( - self.doc[self.pagenr].getDisplayList().getPixmap(matrix=mat_0, alpha=False) - ) - # the getImageData() function was only introduced in pymupdf 1.14.5 + # since pymupdf 1.19.0 a warning will be issued if the deprecated names are used + if hasattr(self.doc[self.pagenr], "get_displaylist"): + gdl = self.doc[self.pagenr].get_displaylist() + else: + gdl = self.doc[self.pagenr].getDisplayList() + if hasattr(gdl, "get_pixmap"): + pix = gdl.get_pixmap(matrix=mat_0, alpha=False) + else: + pix = gdl.getPixmap(matrix=mat_0, alpha=False) + if hasattr(pix, "tobytes"): + # getImageData was deprecated in pymupdf 1.19.0 + return pix.tobytes("ppm") if hasattr(pix, "getImageData"): + # the getImageData() function was only introduced in pymupdf 1.14.5 return pix.getImageData("ppm") else: # this is essentially the same thing that the getImageData() @@ -369,8 +382,14 @@ class Plakativ: printable_height = self.layout["output_pagesize"][1] - ( border_top + border_bottom ) - inpage_width = pt_to_mm(self.doc[self.pagenr].getDisplayList().rect.width) - inpage_height = pt_to_mm(self.doc[self.pagenr].getDisplayList().rect.height) + # since pymupdf 1.19.0 a warning will be issued if the deprecated names are used + if hasattr(self.doc[self.pagenr], "get_displaylist"): + gdl = self.doc[self.pagenr].get_displaylist + else: + gdl = self.doc[self.pagenr].getDisplayList + rect = gdl().rect + inpage_width = pt_to_mm(rect.width) + inpage_height = pt_to_mm(rect.height) if mode in ["size", "mult"]: if mode == "size": @@ -625,7 +644,12 @@ class Plakativ: if not hasattr(self, "layout"): raise LayoutNotComputedException() - inpage_width = pt_to_mm(self.doc[self.pagenr].getDisplayList().rect.width) + # since pymupdf 1.19.0 a warning will be issued if the deprecated names are used + if hasattr(self.doc[self.pagenr], "get_displaylist"): + gdl = self.doc[self.pagenr].get_displaylist + else: + gdl = self.doc[self.pagenr].getDisplayList + inpage_width = pt_to_mm(gdl().rect.width) outdoc = fitz.open() @@ -644,7 +668,13 @@ class Plakativ: ) / (self.layout["overallsize"][1]), ) - page = outdoc.newPage( + # since pymupdf 1.19.0 a warning will be issued if the deprecated names are used + if hasattr(outdoc, "new_page"): + np = outdoc.new_page + else: + np = outdoc.newPage + + page = np( -1, # insert after last page width=mm_to_pt(self.layout["output_pagesize"][0]), height=mm_to_pt(self.layout["output_pagesize"][1]), @@ -674,7 +704,10 @@ class Plakativ: bottom = self.layout["border_right"] * zoom_1 left = self.layout["border_bottom"] * zoom_1 # inner rectangle - shape = page.newShape() + if hasattr(page, "new_shape"): + shape = page.new_shape() + else: + shape = page.newShape() shape.drawRect( fitz.Rect( x0, @@ -714,7 +747,12 @@ class Plakativ: else: page_width = mm_to_pt(self.layout["output_pagesize"][1]) page_height = mm_to_pt(self.layout["output_pagesize"][0]) - page = outdoc.newPage( + # since pymupdf 1.19.0 a warning will be issued if the deprecated names are used + if hasattr(outdoc, "new_page"): + np = outdoc.new_page + else: + np = outdoc.newPage + page = np( -1, width=page_width, height=page_height # insert after last page ) @@ -758,14 +796,22 @@ class Plakativ: mm_to_pt(factor * (target_y + target_height)), ) - page.showPDFpage( + # since pymupdf 1.19.0 a warning will be issued if the deprecated names are used + if hasattr(page, "show_pdf_page"): + spp = page.show_pdf_page + else: + spp = page.showPDFpage + spp( targetrect, # fill the whole page self.doc, # input document self.pagenr, # input page number clip=sourcerect, # part of the input page to use ) - shape = page.newShape() + if hasattr(page, "new_shape"): + shape = page.new_shape() + else: + shape = page.newShape() if guides: if portrait: shape.drawRect( diff --git a/plakativ_test.py b/plakativ_test.py index 31313fc..cf44808 100644 --- a/plakativ_test.py +++ b/plakativ_test.py @@ -149,8 +149,8 @@ def test_cases(postersize, input_pagesize, output_pagesize, strategy, expected): height = mm_to_pt(input_pagesize[1]) doc = fitz.open() - page = doc.newPage(pno=-1, width=width, height=height) - img = page.newShape() + page = doc.new_page(pno=-1, width=width, height=height) + img = page.new_shape() red = fitz.utils.getColor("red") green = fitz.utils.getColor("green") @@ -189,7 +189,7 @@ def test_cases(postersize, input_pagesize, output_pagesize, strategy, expected): doc = fitz.open(outfile) - for pnum, (bbox, matrix) in zip(range(doc.pageCount), expected): + for pnum, (bbox, matrix) in zip(range(doc.page_count), expected): xreflist = doc._getPageInfo(pnum, 3) assert len(xreflist) >= 1 xref, name, _, _ = xreflist[0] @@ -209,7 +209,7 @@ def test_cases(postersize, input_pagesize, output_pagesize, strategy, expected): # >> keyvals = dict( tuple(line.strip().split(maxsplit=1)) - for line in doc.xrefObject(xref).splitlines() + for line in doc.xref_object(xref).splitlines() if " " in line.strip() ) assert "/BBox" in keyvals