diff --git a/src/ocrmypdf/hocrtransform/hocr_parser.py b/src/ocrmypdf/hocrtransform/hocr_parser.py index 898da4c8..b3088afb 100644 --- a/src/ocrmypdf/hocrtransform/hocr_parser.py +++ b/src/ocrmypdf/hocrtransform/hocr_parser.py @@ -99,7 +99,7 @@ class HocrParser: _x_font_pattern = re.compile( r''' x_font \s+ - (\S+) # font name: non-whitespace string + ([^\s;]+) # font name: non-whitespace, non-semicolon string ''', re.VERBOSE, ) diff --git a/tests/test_hocr_parser.py b/tests/test_hocr_parser.py new file mode 100644 index 00000000..ddc22eab --- /dev/null +++ b/tests/test_hocr_parser.py @@ -0,0 +1,530 @@ +# SPDX-FileCopyrightText: 2025 James R. Barlow +# SPDX-License-Identifier: MPL-2.0 + +"""Unit tests for HocrParser class.""" + +from __future__ import annotations + +from pathlib import Path +from textwrap import dedent + +import pytest + +from ocrmypdf.hocrtransform import ( + HocrParseError, + HocrParser, + OcrClass, +) + + +@pytest.fixture +def simple_hocr(tmp_path) -> Path: + """Create a simple valid hOCR file.""" + content = dedent("""\ + + + +
++ + Hello + World + +
++ + Line + one + + + Line + two + +
++ + German + text + +
++ + مرحبا + +
++ + Rotated + +
++ + Chapter + One + + + Body + text + + + Figure + 1 + +
++ + Styled + +
+No ocr_page
") + + parser = HocrParser(hocr_file) + with pytest.raises(HocrParseError, match="No ocr_page"): + parser.parse() + + def test_missing_page_bbox(self, tmp_path): + hocr_file = tmp_path / "no_bbox.hocr" + hocr_file.write_text( + "+ + + Valid + +
++ + + Valid + +
++ + Word + + + Valid + +
++ + fi + +
++ + NoNS + +
+