Refactor: move ocr_element to a better location

This commit is contained in:
James R. Barlow
2026-01-27 14:01:30 -08:00
parent de5f2b80f0
commit 6b37583674
7 changed files with 22 additions and 16 deletions
+1 -1
View File
@@ -35,7 +35,7 @@ from ocrmypdf.exceptions import (
TesseractConfigError,
UnsupportedImageFormatError,
)
from ocrmypdf.hocrtransform import (
from ocrmypdf.models.ocr_element import (
Baseline,
BoundingBox,
FontInfo,
+1 -1
View File
@@ -20,7 +20,7 @@ from fpdf.enums import TextMode
from pikepdf import Matrix, Rectangle
from ocrmypdf.font import FontManager, MultiFontManager
from ocrmypdf.hocrtransform.ocr_element import OcrClass, OcrElement
from ocrmypdf.models.ocr_element import OcrClass, OcrElement
log = logging.getLogger(__name__)
+1 -1
View File
@@ -27,7 +27,7 @@ from ocrmypdf.hocrtransform.hocr_parser import (
HocrParseError,
HocrParser,
)
from ocrmypdf.hocrtransform.ocr_element import (
from ocrmypdf.models.ocr_element import (
Baseline,
BoundingBox,
FontInfo,
+5 -5
View File
@@ -20,9 +20,9 @@ import re
import unicodedata
from pathlib import Path
from typing import Literal, cast
from xml.etree import ElementTree
from xml.etree import ElementTree as ET
from ocrmypdf.hocrtransform.ocr_element import (
from ocrmypdf.models.ocr_element import (
Baseline,
BoundingBox,
FontInfo,
@@ -34,7 +34,7 @@ TextDirection = Literal["ltr", "rtl"]
log = logging.getLogger(__name__)
Element = ElementTree.Element
Element = ET.Element
class HocrParseError(Exception):
@@ -132,8 +132,8 @@ class HocrParser:
"""
self._hocr_path = Path(hocr_file)
try:
self._tree = ElementTree.parse(os.fspath(hocr_file))
except ElementTree.ParseError as e:
self._tree = ET.parse(os.fspath(hocr_file))
except ET.ParseError as e:
raise HocrParseError(f"Failed to parse hOCR file: {e}") from e
# Detect XML namespace
+6
View File
@@ -0,0 +1,6 @@
# SPDX-FileCopyrightText: 2025 James R. Barlow
# SPDX-License-Identifier: MPL-2.0
"""OCRmyPDF models for plugin options and cross-cutting concerns."""
from __future__ import annotations
+8 -8
View File
@@ -16,7 +16,7 @@ from ocrmypdf.fpdf_renderer import (
Fpdf2PdfRenderer,
)
from ocrmypdf.hocrtransform.hocr_parser import HocrParser
from ocrmypdf.hocrtransform.ocr_element import OcrClass
from ocrmypdf.models.ocr_element import OcrClass
@pytest.fixture
@@ -80,7 +80,7 @@ class TestFpdf2PdfRenderer:
def test_requires_page_element(self, multi_font_manager):
"""Test that renderer requires ocr_page element."""
from ocrmypdf.hocrtransform.ocr_element import BoundingBox, OcrElement
from ocrmypdf.models.ocr_element import BoundingBox, OcrElement
# Create a non-page element
word = OcrElement(
@@ -98,7 +98,7 @@ class TestFpdf2PdfRenderer:
def test_requires_bbox(self, multi_font_manager):
"""Test that renderer requires page with bounding box."""
from ocrmypdf.hocrtransform.ocr_element import OcrElement
from ocrmypdf.models.ocr_element import OcrElement
page = OcrElement(ocr_class=OcrClass.PAGE)
@@ -111,7 +111,7 @@ class TestFpdf2PdfRenderer:
def test_render_simple_page(self, multi_font_manager, tmp_path):
"""Test rendering a simple page with one word."""
from ocrmypdf.hocrtransform.ocr_element import BoundingBox, OcrElement
from ocrmypdf.models.ocr_element import BoundingBox, OcrElement
# Create a simple page with one word
word = OcrElement(
@@ -145,7 +145,7 @@ class TestFpdf2PdfRenderer:
def test_render_invisible_text(self, multi_font_manager, tmp_path):
"""Test rendering invisible text (OCR layer)."""
from ocrmypdf.hocrtransform.ocr_element import BoundingBox, OcrElement
from ocrmypdf.models.ocr_element import BoundingBox, OcrElement
word = OcrElement(
ocr_class=OcrClass.WORD,
@@ -191,7 +191,7 @@ class TestFpdf2MultiPageRenderer:
def test_render_multiple_pages(self, multi_font_manager, tmp_path):
"""Test rendering multiple pages."""
from ocrmypdf.hocrtransform.ocr_element import BoundingBox, OcrElement
from ocrmypdf.models.ocr_element import BoundingBox, OcrElement
pages_data = []
for i in range(3):
@@ -385,7 +385,7 @@ class TestWordSegmentation:
"""
from pdfminer.high_level import extract_text
from ocrmypdf.hocrtransform.ocr_element import BoundingBox, OcrElement
from ocrmypdf.models.ocr_element import BoundingBox, OcrElement
# Create a page with multiple words on one line
word1 = OcrElement(
@@ -451,7 +451,7 @@ class TestWordSegmentation:
"""
from pdfminer.high_level import extract_text
from ocrmypdf.hocrtransform.ocr_element import BoundingBox, OcrElement
from ocrmypdf.models.ocr_element import BoundingBox, OcrElement
# Create a page with CJK words (Chinese characters)
# 你好 = "Hello" in Chinese