From 9898904be70bf957154cc66cb5d6f57bac7c26b4 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Wed, 15 Nov 2023 20:27:16 -0800 Subject: [PATCH] Fix pikepdf PdfMatrix deprecation warning; v15.4.3 release notes --- docs/release_notes.rst | 6 ++++++ pyproject.toml | 2 +- src/ocrmypdf/_graft.py | 17 +++++++++-------- src/ocrmypdf/pdfinfo/info.py | 12 ++++++------ 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/docs/release_notes.rst b/docs/release_notes.rst index 90c4e1cf..99213621 100644 --- a/docs/release_notes.rst +++ b/docs/release_notes.rst @@ -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 ======= diff --git a/pyproject.toml b/pyproject.toml index c839f0a6..7c8db814 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", diff --git a/src/ocrmypdf/_graft.py b/src/ocrmypdf/_graft.py index 1c79c49c..10c6108a 100644 --- a/src/ocrmypdf/_graft.py +++ b/src/ocrmypdf/_graft.py @@ -12,12 +12,12 @@ from pathlib import Path from pikepdf import ( Dictionary, + Matrix, Name, Operator, Page, Pdf, PdfError, - PdfMatrix, Stream, parse_content_stream, unparse_content_stream, @@ -268,13 +268,13 @@ class OcrGrafter: 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 @@ -285,12 +285,13 @@ class OcrGrafter: 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) diff --git a/src/ocrmypdf/pdfinfo/info.py b/src/ocrmypdf/pdfinfo/info.py index 7bb2ea82..e6b6f19d 100644 --- a/src/ocrmypdf/pdfinfo/info.py +++ b/src/ocrmypdf/pdfinfo/info.py @@ -25,13 +25,13 @@ from warnings import warn from pdfminer.layout import LTPage, LTTextBox from pikepdf import ( + Matrix, Name, Object, Page, Pdf, PdfImage, PdfInlineImage, - PdfMatrix, Stream, UnsupportedImageTypeError, parse_content_stream, @@ -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: []) @@ -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( @@ -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