1
0
Fork 0
forked from josch/plakativ

use new snake_case naming for pymupdf 1.19.0 and later

This commit is contained in:
Johannes Schauer Marin Rodrigues 2022-06-26 22:59:11 +01:00 committed by Johannes Schauer Marin Rodrigues
parent a49d7b0a53
commit d6d0a6ea06
2 changed files with 65 additions and 19 deletions

View file

@ -326,17 +326,30 @@ class Plakativ:
return len(self.doc) return len(self.doc)
def get_input_page_size(self): def get_input_page_size(self):
width = self.doc[self.pagenr].getDisplayList().rect.width # since pymupdf 1.19.0 a warning will be issued if the deprecated names are used
height = self.doc[self.pagenr].getDisplayList().rect.height if hasattr(self.doc[self.pagenr], "get_displaylist"):
return (width, height) 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): def get_image(self, zoom):
mat_0 = fitz.Matrix(zoom, zoom) mat_0 = fitz.Matrix(zoom, zoom)
pix = ( # since pymupdf 1.19.0 a warning will be issued if the deprecated names are used
self.doc[self.pagenr].getDisplayList().getPixmap(matrix=mat_0, alpha=False) if hasattr(self.doc[self.pagenr], "get_displaylist"):
) gdl = self.doc[self.pagenr].get_displaylist()
# the getImageData() function was only introduced in pymupdf 1.14.5 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"): if hasattr(pix, "getImageData"):
# the getImageData() function was only introduced in pymupdf 1.14.5
return pix.getImageData("ppm") return pix.getImageData("ppm")
else: else:
# this is essentially the same thing that the getImageData() # this is essentially the same thing that the getImageData()
@ -369,8 +382,14 @@ class Plakativ:
printable_height = self.layout["output_pagesize"][1] - ( printable_height = self.layout["output_pagesize"][1] - (
border_top + border_bottom border_top + border_bottom
) )
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
inpage_height = pt_to_mm(self.doc[self.pagenr].getDisplayList().rect.height) 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 in ["size", "mult"]:
if mode == "size": if mode == "size":
@ -625,7 +644,12 @@ class Plakativ:
if not hasattr(self, "layout"): if not hasattr(self, "layout"):
raise LayoutNotComputedException() 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() outdoc = fitz.open()
@ -644,7 +668,13 @@ class Plakativ:
) )
/ (self.layout["overallsize"][1]), / (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 -1, # insert after last page
width=mm_to_pt(self.layout["output_pagesize"][0]), width=mm_to_pt(self.layout["output_pagesize"][0]),
height=mm_to_pt(self.layout["output_pagesize"][1]), height=mm_to_pt(self.layout["output_pagesize"][1]),
@ -674,7 +704,10 @@ class Plakativ:
bottom = self.layout["border_right"] * zoom_1 bottom = self.layout["border_right"] * zoom_1
left = self.layout["border_bottom"] * zoom_1 left = self.layout["border_bottom"] * zoom_1
# inner rectangle # inner rectangle
shape = page.newShape() if hasattr(page, "new_shape"):
shape = page.new_shape()
else:
shape = page.newShape()
shape.drawRect( shape.drawRect(
fitz.Rect( fitz.Rect(
x0, x0,
@ -714,7 +747,12 @@ class Plakativ:
else: else:
page_width = mm_to_pt(self.layout["output_pagesize"][1]) page_width = mm_to_pt(self.layout["output_pagesize"][1])
page_height = mm_to_pt(self.layout["output_pagesize"][0]) 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 -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)), 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 targetrect, # fill the whole page
self.doc, # input document self.doc, # input document
self.pagenr, # input page number self.pagenr, # input page number
clip=sourcerect, # part of the input page to use 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 guides:
if portrait: if portrait:
shape.drawRect( shape.drawRect(

View file

@ -149,8 +149,8 @@ def test_cases(postersize, input_pagesize, output_pagesize, strategy, expected):
height = mm_to_pt(input_pagesize[1]) height = mm_to_pt(input_pagesize[1])
doc = fitz.open() doc = fitz.open()
page = doc.newPage(pno=-1, width=width, height=height) page = doc.new_page(pno=-1, width=width, height=height)
img = page.newShape() img = page.new_shape()
red = fitz.utils.getColor("red") red = fitz.utils.getColor("red")
green = fitz.utils.getColor("green") green = fitz.utils.getColor("green")
@ -189,7 +189,7 @@ def test_cases(postersize, input_pagesize, output_pagesize, strategy, expected):
doc = fitz.open(outfile) 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) xreflist = doc._getPageInfo(pnum, 3)
assert len(xreflist) >= 1 assert len(xreflist) >= 1
xref, name, _, _ = xreflist[0] xref, name, _, _ = xreflist[0]
@ -209,7 +209,7 @@ def test_cases(postersize, input_pagesize, output_pagesize, strategy, expected):
# >> # >>
keyvals = dict( keyvals = dict(
tuple(line.strip().split(maxsplit=1)) 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() if " " in line.strip()
) )
assert "/BBox" in keyvals assert "/BBox" in keyvals