From 9a1838f1023d676f2dcfbe6ec4844b8e69b14898 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Thu, 27 Oct 2016 15:35:00 -0700 Subject: [PATCH] pageinfo: accept "cm/Do" image drawing without the usual "q/Q" wrapper Some PDFs omit the traditional q/Q wrapper and alter ctm with a stack depth of zero, so make our test for stack depth specifically test for the case where the PDF calls for rendering to an uninitialized ctm. Probably related to #97. --- ocrmypdf/pageinfo.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/ocrmypdf/pageinfo.py b/ocrmypdf/pageinfo.py index c0d55314..617f4bf7 100644 --- a/ocrmypdf/pageinfo.py +++ b/ocrmypdf/pageinfo.py @@ -3,7 +3,7 @@ from subprocess import Popen, PIPE from decimal import Decimal, getcontext -from math import hypot +from math import hypot, isclose import re import sys import PyPDF2 as pypdf @@ -52,6 +52,9 @@ FRIENDLY_COMP = { } +UNIT_SQUARE = (1.0, 0.0, 0.0, 1.0, 0.0, 0.0) + + def _matrix_from_shorthand(shorthand): """Convert from PDF matrix shorthand to full matrix @@ -72,6 +75,12 @@ def _shorthand_from_matrix(matrix): e, f = matrix[2][0], matrix[2][1] return tuple(map(float, (a, b, c, d, e, f))) + +def _is_unit_square(shorthand): + values = map(float, shorthand) + pairwise = zip(values, UNIT_SQUARE) + return all([isclose(a, b, rel_tol=1e-3) for a, b in pairwise]) + RasterSettings = namedtuple('RasterSettings', ['name', 'shorthand', 'stack_depth']) @@ -106,7 +115,7 @@ def _interpret_contents(contentstream): operations = contentstream.operations stack = [] - ctm = _matrix_from_shorthand((1, 0, 0, 1, 0, 0)) + ctm = _matrix_from_shorthand(UNIT_SQUARE) image_raster_settings = [] inline_images = [] @@ -293,7 +302,7 @@ def _find_page_regular_images(page, pageinfo, contentsinfo): if raster.name != image['name']: continue - if raster.stack_depth == 0: + if raster.stack_depth == 0 and _is_unit_square(raster.shorthand): # At least one PDF in the wild (and test suite) draws an image # when the graphics stack depth is 0, meaning that the image # gets drawn into a square of 1x1 PDF units (or 1/72",