2019-05-11 12:10:50 -07:00
|
|
|
# © 2019 James R. Barlow: github.com/jbarlow83
|
|
|
|
#
|
2020-08-05 00:44:42 -07:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2019-05-11 12:10:50 -07:00
|
|
|
|
2022-07-23 00:39:24 -07:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-05-11 12:10:50 -07:00
|
|
|
import logging
|
2022-01-25 23:45:47 -08:00
|
|
|
import subprocess
|
2019-05-11 12:10:50 -07:00
|
|
|
from decimal import Decimal
|
2022-01-25 23:45:47 -08:00
|
|
|
from unittest.mock import patch
|
2019-05-11 12:10:50 -07:00
|
|
|
|
|
|
|
import pikepdf
|
|
|
|
import pytest
|
2022-01-25 23:45:47 -08:00
|
|
|
from PIL import Image, UnidentifiedImageError
|
2019-05-11 12:10:50 -07:00
|
|
|
|
2020-06-09 14:55:54 -07:00
|
|
|
from ocrmypdf._exec.ghostscript import rasterize_pdf
|
2019-11-28 16:19:58 -08:00
|
|
|
from ocrmypdf.exceptions import ExitCode
|
2020-04-24 04:12:05 -07:00
|
|
|
from ocrmypdf.helpers import Resolution
|
2019-05-11 12:10:50 -07:00
|
|
|
|
2021-04-07 02:09:45 -07:00
|
|
|
from .conftest import check_ocrmypdf, run_ocrmypdf
|
2020-06-09 15:27:14 -07:00
|
|
|
|
|
|
|
# pylint: disable=redefined-outer-name
|
2019-11-28 16:19:58 -08:00
|
|
|
|
|
|
|
|
2019-05-11 12:10:50 -07:00
|
|
|
@pytest.fixture
|
2019-12-31 17:20:28 -08:00
|
|
|
def francais(resources):
|
|
|
|
path = resources / 'francais.pdf'
|
2019-05-11 12:10:50 -07:00
|
|
|
return path, pikepdf.open(path)
|
|
|
|
|
|
|
|
|
2020-06-09 15:27:14 -07:00
|
|
|
def test_rasterize_size(francais, outdir):
|
2019-12-31 17:20:28 -08:00
|
|
|
path, pdf = francais
|
2019-05-11 12:10:50 -07:00
|
|
|
page_size_pts = (pdf.pages[0].MediaBox[2], pdf.pages[0].MediaBox[3])
|
|
|
|
assert pdf.pages[0].MediaBox[0] == pdf.pages[0].MediaBox[1] == 0
|
|
|
|
page_size = (page_size_pts[0] / Decimal(72), page_size_pts[1] / Decimal(72))
|
2019-12-31 17:20:28 -08:00
|
|
|
target_size = Decimal('50.0'), Decimal('30.0')
|
2020-04-24 04:12:05 -07:00
|
|
|
forced_dpi = Resolution(42.0, 4242.0)
|
2019-05-11 12:10:50 -07:00
|
|
|
|
|
|
|
rasterize_pdf(
|
|
|
|
path,
|
|
|
|
outdir / 'out.png',
|
|
|
|
raster_device='pngmono',
|
2020-04-24 04:12:05 -07:00
|
|
|
raster_dpi=Resolution(
|
|
|
|
target_size[0] / page_size[0], target_size[1] / page_size[1]
|
|
|
|
),
|
2019-12-31 17:20:28 -08:00
|
|
|
page_dpi=forced_dpi,
|
2019-05-11 12:10:50 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
with Image.open(outdir / 'out.png') as im:
|
|
|
|
assert im.size == target_size
|
2019-12-31 17:20:28 -08:00
|
|
|
assert im.info['dpi'] == forced_dpi
|
2019-05-11 12:10:50 -07:00
|
|
|
|
|
|
|
|
2019-12-31 17:20:28 -08:00
|
|
|
def test_rasterize_rotated(francais, outdir, caplog):
|
|
|
|
path, pdf = francais
|
2019-05-11 12:10:50 -07:00
|
|
|
page_size_pts = (pdf.pages[0].MediaBox[2], pdf.pages[0].MediaBox[3])
|
|
|
|
assert pdf.pages[0].MediaBox[0] == pdf.pages[0].MediaBox[1] == 0
|
|
|
|
page_size = (page_size_pts[0] / Decimal(72), page_size_pts[1] / Decimal(72))
|
2019-12-31 17:20:28 -08:00
|
|
|
target_size = Decimal('50.0'), Decimal('30.0')
|
2020-04-24 04:12:05 -07:00
|
|
|
forced_dpi = Resolution(42.0, 4242.0)
|
2019-05-11 12:10:50 -07:00
|
|
|
|
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
rasterize_pdf(
|
|
|
|
path,
|
|
|
|
outdir / 'out.png',
|
|
|
|
raster_device='pngmono',
|
2020-04-24 04:12:05 -07:00
|
|
|
raster_dpi=Resolution(
|
|
|
|
target_size[0] / page_size[0], target_size[1] / page_size[1]
|
|
|
|
),
|
2019-12-31 17:20:28 -08:00
|
|
|
page_dpi=forced_dpi,
|
2019-05-11 12:10:50 -07:00
|
|
|
rotation=90,
|
|
|
|
)
|
|
|
|
|
|
|
|
with Image.open(outdir / 'out.png') as im:
|
|
|
|
assert im.size == (target_size[1], target_size[0])
|
2021-07-14 02:34:28 -07:00
|
|
|
assert im.info['dpi'] == forced_dpi.flip_axis()
|
2019-11-28 16:19:58 -08:00
|
|
|
|
|
|
|
|
2020-06-09 00:00:25 -07:00
|
|
|
def test_gs_render_failure(resources, outpdf):
|
2021-12-06 17:00:25 -08:00
|
|
|
p = run_ocrmypdf(
|
2020-06-01 03:06:40 -07:00
|
|
|
resources / 'blank.pdf',
|
|
|
|
outpdf,
|
|
|
|
'--plugin',
|
|
|
|
'tests/plugins/tesseract_noop.py',
|
2020-06-09 00:00:25 -07:00
|
|
|
'--plugin',
|
|
|
|
'tests/plugins/gs_render_failure.py',
|
2019-11-28 16:19:58 -08:00
|
|
|
)
|
2021-12-06 17:00:25 -08:00
|
|
|
assert 'Casper is not a friendly ghost' in p.stderr
|
2019-11-28 16:19:58 -08:00
|
|
|
assert p.returncode == ExitCode.child_process_error
|
|
|
|
|
|
|
|
|
2020-06-09 00:00:25 -07:00
|
|
|
def test_gs_raster_failure(resources, outpdf):
|
2021-12-06 17:00:25 -08:00
|
|
|
p = run_ocrmypdf(
|
2020-06-01 03:06:40 -07:00
|
|
|
resources / 'francais.pdf',
|
|
|
|
outpdf,
|
|
|
|
'--plugin',
|
|
|
|
'tests/plugins/tesseract_noop.py',
|
2020-06-09 00:00:25 -07:00
|
|
|
'--plugin',
|
|
|
|
'tests/plugins/gs_raster_failure.py',
|
2019-11-28 16:19:58 -08:00
|
|
|
)
|
2021-12-06 17:00:25 -08:00
|
|
|
assert 'Ghost story archive not found' in p.stderr
|
2019-11-28 16:19:58 -08:00
|
|
|
assert p.returncode == ExitCode.child_process_error
|
|
|
|
|
|
|
|
|
2020-06-09 00:00:25 -07:00
|
|
|
def test_ghostscript_pdfa_failure(resources, outpdf):
|
2021-12-06 17:00:25 -08:00
|
|
|
p = run_ocrmypdf(
|
2020-06-01 03:06:40 -07:00
|
|
|
resources / 'francais.pdf',
|
|
|
|
outpdf,
|
|
|
|
'--plugin',
|
|
|
|
'tests/plugins/tesseract_noop.py',
|
2020-06-09 00:00:25 -07:00
|
|
|
'--plugin',
|
|
|
|
'tests/plugins/gs_pdfa_failure.py',
|
2019-11-28 16:19:58 -08:00
|
|
|
)
|
|
|
|
assert (
|
|
|
|
p.returncode == ExitCode.pdfa_conversion_failed
|
|
|
|
), "Unexpected return when PDF/A fails"
|
|
|
|
|
|
|
|
|
2020-06-09 00:00:25 -07:00
|
|
|
def test_ghostscript_feature_elision(resources, outpdf):
|
2020-06-01 03:06:40 -07:00
|
|
|
check_ocrmypdf(
|
|
|
|
resources / 'francais.pdf',
|
|
|
|
outpdf,
|
|
|
|
'--plugin',
|
|
|
|
'tests/plugins/tesseract_noop.py',
|
2020-06-09 00:00:25 -07:00
|
|
|
'--plugin',
|
|
|
|
'tests/plugins/gs_feature_elision.py',
|
2020-06-01 03:06:40 -07:00
|
|
|
)
|
2022-01-25 23:45:47 -08:00
|
|
|
|
|
|
|
|
|
|
|
def test_rasterize_pdf_errors(resources, no_outpdf, caplog):
|
|
|
|
with patch('ocrmypdf._exec.ghostscript.run') as mock:
|
|
|
|
# ghostscript can produce
|
|
|
|
mock.return_value = subprocess.CompletedProcess(
|
|
|
|
['fakegs'], returncode=0, stdout=b'', stderr=b'error this is an error'
|
|
|
|
)
|
|
|
|
with pytest.raises(UnidentifiedImageError):
|
|
|
|
rasterize_pdf(
|
|
|
|
resources / 'francais.pdf',
|
|
|
|
no_outpdf,
|
|
|
|
raster_device='pngmono',
|
|
|
|
raster_dpi=Resolution(100, 100),
|
|
|
|
)
|
|
|
|
assert "this is an error" in caplog.text
|
|
|
|
assert "invalid page image file" in caplog.text
|