Use newer pikepdf API for objgen
This commit is contained in:
@@ -248,7 +248,7 @@ setup(
|
|||||||
install_requires=[
|
install_requires=[
|
||||||
'cffi >= 1.9.1', # must be a setup and install requirement
|
'cffi >= 1.9.1', # must be a setup and install requirement
|
||||||
'img2pdf >= 0.2.4', # pure Python, so track HEAD closely
|
'img2pdf >= 0.2.4', # pure Python, so track HEAD closely
|
||||||
'pikepdf > 0.2.0',
|
'pikepdf >= 0.2.2, < 0.3',
|
||||||
'Pillow >= 4.0.0, != 5.1.0 ; sys_platform == "darwin"',
|
'Pillow >= 4.0.0, != 5.1.0 ; sys_platform == "darwin"',
|
||||||
# Pillow < 4 has BytesIO/TIFF bug w/img2pdf 0.2.3
|
# Pillow < 4 has BytesIO/TIFF bug w/img2pdf 0.2.3
|
||||||
# block 5.1.0, broken wheels
|
# block 5.1.0, broken wheels
|
||||||
|
|||||||
+12
-23
@@ -79,23 +79,12 @@ def _weave_layers_graft(
|
|||||||
for v in range(4)]
|
for v in range(4)]
|
||||||
wp, hp = mediabox[2] - mediabox[0], mediabox[3] - mediabox[1]
|
wp, hp = mediabox[2] - mediabox[0], mediabox[3] - mediabox[1]
|
||||||
|
|
||||||
translate = pikepdf.PdfMatrix((1, 0, 0, 1, -wt / 2, -ht / 2))
|
translate = pikepdf.PdfMatrix().translated(-wt / 2, -ht / 2)
|
||||||
untranslate = pikepdf.PdfMatrix((1, 0, 0, 1, wp / 2, hp / 2))
|
untranslate = pikepdf.PdfMatrix().translated(wp / 2, hp / 2)
|
||||||
# -rotation because the input is a clockwise angle and this formula
|
# -rotation because the input is a clockwise angle and this formula
|
||||||
# uses CCW
|
# uses CCW
|
||||||
rotation = -rotation % 360
|
rotation = -rotation % 360
|
||||||
if rotation == 0:
|
rotate = pikepdf.PdfMatrix().rotated(rotation)
|
||||||
c, s = 1, 0
|
|
||||||
elif rotation == 90:
|
|
||||||
c, s = 0, 1
|
|
||||||
elif rotation == 180:
|
|
||||||
c, s = -1, 0
|
|
||||||
elif rotation == 270:
|
|
||||||
c, s = 0, -1
|
|
||||||
else:
|
|
||||||
raise NotImplementedError("rotation to arbitrary angle")
|
|
||||||
|
|
||||||
rotate = pikepdf.PdfMatrix((c, s, -s, c, 0, 0))
|
|
||||||
|
|
||||||
# Because of rounding of DPI, we might get a text layer that is not
|
# Because of rounding of DPI, we might get a text layer that is not
|
||||||
# identically sized to the target page. Scale to adjust. Normally this
|
# identically sized to the target page. Scale to adjust. Normally this
|
||||||
@@ -106,7 +95,7 @@ def _weave_layers_graft(
|
|||||||
scale_y = hp / ht
|
scale_y = hp / ht
|
||||||
|
|
||||||
log.debug('%r', (scale_x, scale_y))
|
log.debug('%r', (scale_x, scale_y))
|
||||||
scale = pikepdf.PdfMatrix((scale_x, 0, 0, scale_y, 0, 0))
|
scale = pikepdf.PdfMatrix().scaled(scale_x, scale_y)
|
||||||
|
|
||||||
# Translate the text so it is centered at (0, 0), rotate it there, adjust
|
# Translate the text so it is centered at (0, 0), rotate it there, adjust
|
||||||
# for a size different between initial and text PDF, then untranslate
|
# for a size different between initial and text PDF, then untranslate
|
||||||
@@ -181,15 +170,15 @@ def _fix_toc(pdf_base, pageref_remap, log):
|
|||||||
return
|
return
|
||||||
pageref = dest_node[0]
|
pageref = dest_node[0]
|
||||||
if pageref['/Type'] == '/Page' and \
|
if pageref['/Type'] == '/Page' and \
|
||||||
pageref._objgen in pageref_remap:
|
pageref.objgen in pageref_remap:
|
||||||
new_objgen = pageref_remap[pageref._objgen]
|
new_objgen = pageref_remap[pageref.objgen]
|
||||||
dest_node[0] = pdf_base._get_object_id(*new_objgen)
|
dest_node[0] = pdf_base.get_object(new_objgen)
|
||||||
|
|
||||||
queue.add(pdf_base.root.Outlines._objgen)
|
queue.add(pdf_base.root.Outlines.objgen)
|
||||||
while queue:
|
while queue:
|
||||||
objgen = queue.pop()
|
objgen = queue.pop()
|
||||||
visited.add(objgen)
|
visited.add(objgen)
|
||||||
node = pdf_base._get_object_id(*objgen)
|
node = pdf_base.get_object(objgen)
|
||||||
log.debug('fix toc: visiting %r', objgen)
|
log.debug('fix toc: visiting %r', objgen)
|
||||||
|
|
||||||
# Enumerate other nodes we could visit from here
|
# Enumerate other nodes we could visit from here
|
||||||
@@ -199,7 +188,7 @@ def _fix_toc(pdf_base, pageref_remap, log):
|
|||||||
item = node[key]
|
item = node[key]
|
||||||
if not item.is_indirect:
|
if not item.is_indirect:
|
||||||
continue
|
continue
|
||||||
objgen = item._objgen
|
objgen = item.objgen
|
||||||
if objgen not in visited:
|
if objgen not in visited:
|
||||||
queue.add(objgen)
|
queue.add(objgen)
|
||||||
|
|
||||||
@@ -285,7 +274,7 @@ def weave_layers(
|
|||||||
# We are replacing the old page with a rasterized PDF of the new
|
# We are replacing the old page with a rasterized PDF of the new
|
||||||
# page
|
# page
|
||||||
log.debug("Replace")
|
log.debug("Replace")
|
||||||
old_objgen = pdf_base.pages[page_num - 1]._objgen
|
old_objgen = pdf_base.pages[page_num - 1].objgen
|
||||||
|
|
||||||
pdf_image = pikepdf.open(image)
|
pdf_image = pikepdf.open(image)
|
||||||
keep_open.append(pdf_image)
|
keep_open.append(pdf_image)
|
||||||
@@ -296,7 +285,7 @@ def weave_layers(
|
|||||||
# so we need to update any references to it. qpdf did not like
|
# so we need to update any references to it. qpdf did not like
|
||||||
# my attempt to update the old object in place, but that is an
|
# my attempt to update the old object in place, but that is an
|
||||||
# option to consider
|
# option to consider
|
||||||
pagerefs[old_objgen] = pdf_base.pages[page_num - 1]._objgen
|
pagerefs[old_objgen] = pdf_base.pages[page_num - 1].objgen
|
||||||
replacing = True
|
replacing = True
|
||||||
|
|
||||||
autorotate_correction = context.get_rotation(page_num - 1)
|
autorotate_correction = context.get_rotation(page_num - 1)
|
||||||
|
|||||||
Reference in New Issue
Block a user