Skip to content

Commit

Permalink
Fix pikepdf PdfMatrix deprecation warning; v15.4.3 release notes
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarlow83 committed Nov 16, 2023
1 parent 27d5229 commit 9898904
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
6 changes: 6 additions & 0 deletions docs/release_notes.rst
Expand Up @@ -28,6 +28,12 @@ tagged yet.

.. |OCRmyPDF PyPI| image:: https://img.shields.io/pypi/v/ocrmypdf.svg

v15.4.3
=======

- Fixed deprecation warning in pikepdf older than 8.7.1; pikepdf >= 8.7.1 is
now required.

v15.4.2
=======

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Expand Up @@ -17,7 +17,7 @@ dependencies = [
"img2pdf>=0.4.4",
"packaging>=20",
"pdfminer.six>=20220319",
"pikepdf>=8",
"pikepdf>=8.7.1",
"pluggy>=0.13.0",
"reportlab>=3.6.8",
"rich>=13",
Expand Down
17 changes: 9 additions & 8 deletions src/ocrmypdf/_graft.py
Expand Up @@ -12,12 +12,12 @@

from pikepdf import (
Dictionary,
Matrix,
Name,
Operator,
Page,
Pdf,
PdfError,
PdfMatrix,
Stream,
parse_content_stream,
unparse_content_stream,
Expand Down Expand Up @@ -268,13 +268,13 @@ def _graft_text_layer(
mediabox = base_page.mediabox
wp, hp = mediabox[2] - mediabox[0], mediabox[3] - mediabox[1]

translate = PdfMatrix().translated(-wt / 2, -ht / 2)
untranslate = PdfMatrix().translated(wp / 2, hp / 2)
corner = PdfMatrix().translated(mediabox[0], mediabox[1])
translate = Matrix().translated(-wt / 2, -ht / 2)
untranslate = Matrix().translated(wp / 2, hp / 2)
corner = Matrix().translated(mediabox[0], mediabox[1])
# -rotation because the input is a clockwise angle and this formula
# uses CCW
text_rotation = -text_rotation % 360
rotate = PdfMatrix().rotated(text_rotation)
rotate = Matrix().rotated(text_rotation)

# 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
Expand All @@ -285,12 +285,13 @@ def _graft_text_layer(
scale_y = hp / ht

# log.debug('%r', scale_x, scale_y)
scale = PdfMatrix().scaled(scale_x, scale_y)
scale = Matrix().scaled(scale_x, scale_y)

# 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, and
# finally move the lower left corner to match the mediabox
ctm = translate @ rotate @ scale @ untranslate @ corner
# finally move the lower left corner to match the mediabox. All transforms
# must be premultiplied so they are applied in reverse order here.
ctm = corner @ untranslate @ scale @ rotate @ translate

base_resources = _ensure_dictionary(base_page.obj, Name.Resources)
base_xobjs = _ensure_dictionary(base_resources, Name.XObject)
Expand Down
12 changes: 6 additions & 6 deletions src/ocrmypdf/pdfinfo/info.py
Expand Up @@ -25,13 +25,13 @@

from pdfminer.layout import LTPage, LTTextBox
from pikepdf import (
Matrix,
Name,
Object,
Page,
Pdf,
PdfImage,
PdfInlineImage,
PdfMatrix,
Stream,
UnsupportedImageTypeError,
parse_content_stream,
Expand Down Expand Up @@ -209,7 +209,7 @@ def _interpret_contents(contentstream: Object, initial_shorthand=UNIT_SQUARE):
CTM unchanged.
"""
stack = []
ctm = PdfMatrix(initial_shorthand)
ctm = Matrix(initial_shorthand)
xobject_settings: list[XobjectSettings] = []
inline_images: list[InlineSettings] = []
name_index = defaultdict(lambda: [])
Expand Down Expand Up @@ -240,7 +240,7 @@ def _interpret_contents(contentstream: Object, initial_shorthand=UNIT_SQUARE):
# to do. Just pretend nothing happened, keep calm and carry on.
warn("PDF graphics stack underflowed - PDF may be malformed")
elif operator == 'cm':
ctm = PdfMatrix(operands) @ ctm
ctm = Matrix(operands) @ ctm
elif operator == 'Do':
image_name = operands[0]
settings = XobjectSettings(
Expand Down Expand Up @@ -614,12 +614,12 @@ def _process_content_streams(
):
# Set the CTM to the state it was when the "Do" operator was
# encountered that is drawing this instance of the Form XObject
ctm = PdfMatrix(shorthand) if shorthand else PdfMatrix.identity()
ctm = Matrix(shorthand) if shorthand else Matrix()

# A Form XObject may provide its own matrix to map form space into
# user space. Get this if one exists
form_shorthand = container.get(Name.Matrix, PdfMatrix.identity())
form_matrix = PdfMatrix(form_shorthand)
form_shorthand = container.get(Name.Matrix, Matrix())
form_matrix = Matrix(form_shorthand)

# Concatenate form matrix with CTM to ensure CTM is correct for
# drawing this instance of the XObject
Expand Down

0 comments on commit 9898904

Please sign in to comment.