2022-07-28 01:06:46 -07:00
|
|
|
# SPDX-FileCopyrightText: 2022 James R. Barlow
|
|
|
|
# SPDX-License-Identifier: 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
|
|
|
|
2023-09-24 20:21:37 -07:00
|
|
|
from ocrmypdf._exec.ghostscript import DuplicateFilter, 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
|
|
|
|
2023-10-15 23:41:35 -07:00
|
|
|
from .conftest import check_ocrmypdf, run_ocrmypdf_api
|
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
|
2023-10-15 23:03:13 -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
|
2019-05-11 12:10:50 -07:00
|
|
|
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
|
2023-10-15 23:03:13 -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
|
2019-05-11 12:10:50 -07:00
|
|
|
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
|
|
|
|
|
|
|
|
2023-10-15 23:41:35 -07:00
|
|
|
def test_gs_render_failure(resources, outpdf, caplog):
|
|
|
|
exitcode = run_ocrmypdf_api(
|
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
|
|
|
)
|
2023-10-15 23:41:35 -07:00
|
|
|
assert 'TEST ERROR: gs_render_failure.py' in caplog.text
|
|
|
|
assert exitcode == ExitCode.child_process_error
|
2019-11-28 16:19:58 -08:00
|
|
|
|
|
|
|
|
2023-10-15 23:41:35 -07:00
|
|
|
def test_gs_raster_failure(resources, outpdf, caplog):
|
|
|
|
exitcode = run_ocrmypdf_api(
|
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
|
|
|
)
|
2023-10-15 23:41:35 -07:00
|
|
|
assert 'TEST ERROR: gs_raster_failure.py' in caplog.text
|
|
|
|
assert exitcode == ExitCode.child_process_error
|
2019-11-28 16:19:58 -08:00
|
|
|
|
|
|
|
|
2023-10-15 23:41:35 -07:00
|
|
|
def test_ghostscript_pdfa_failure(resources, outpdf, caplog):
|
|
|
|
exitcode = run_ocrmypdf_api(
|
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 (
|
2023-10-15 23:41:35 -07:00
|
|
|
exitcode == ExitCode.pdfa_conversion_failed
|
2019-11-28 16:19:58 -08:00
|
|
|
), "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
|
2023-09-24 20:21:37 -07:00
|
|
|
|
|
|
|
|
|
|
|
class TestDuplicateFilter:
|
|
|
|
@pytest.fixture(scope='class', autouse=True)
|
|
|
|
def duplicate_filter_logger(self):
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
logger.addFilter(DuplicateFilter(logger))
|
|
|
|
return logger
|
|
|
|
|
|
|
|
def test_filter_duplicate_messages(self, duplicate_filter_logger, caplog):
|
|
|
|
log = duplicate_filter_logger
|
|
|
|
log.error("test error message")
|
|
|
|
log.error("test error message")
|
|
|
|
log.error("test error message")
|
|
|
|
log.error("another error message")
|
|
|
|
log.error("another error message")
|
|
|
|
log.error("yet another error message")
|
|
|
|
|
|
|
|
assert len(caplog.records) == 5
|
|
|
|
assert caplog.records[0].msg == "test error message"
|
|
|
|
assert caplog.records[1].msg == "(previous message repeated 2 times)"
|
|
|
|
assert caplog.records[2].msg == "another error message"
|
|
|
|
assert caplog.records[3].msg == "(previous message repeated 1 times)"
|
|
|
|
assert caplog.records[4].msg == "yet another error message"
|
|
|
|
|
|
|
|
def test_filter_does_not_affect_unique_messages(
|
|
|
|
self, duplicate_filter_logger, caplog
|
|
|
|
):
|
|
|
|
log = duplicate_filter_logger
|
|
|
|
log.error("test error message")
|
|
|
|
log.error("another error message")
|
|
|
|
log.error("yet another error message")
|
|
|
|
|
|
|
|
assert len(caplog.records) == 3
|
|
|
|
assert caplog.records[0].msg == "test error message"
|
|
|
|
assert caplog.records[1].msg == "another error message"
|
|
|
|
assert caplog.records[2].msg == "yet another error message"
|