robotframework-doctestlibrary 0.29.1__tar.gz → 0.30.0.dev1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/DocTest/DocumentRepresentation.py +117 -4
  2. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/DocTest/PdfStructureModels.py +6 -0
  3. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/DocTest/PdfTest.py +539 -305
  4. robotframework_doctestlibrary-0.30.0.dev1/DocTest/TextNormalization.py +22 -0
  5. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/PKG-INFO +1 -1
  6. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/pyproject.toml +1 -1
  7. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/DocTest/Ai/__init__.py +0 -0
  8. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/DocTest/Ai/prompts.py +0 -0
  9. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/DocTest/Ai/renderers.py +0 -0
  10. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/DocTest/Ai/responses.py +0 -0
  11. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/DocTest/Downloader.py +0 -0
  12. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/DocTest/IgnoreAreaManager.py +0 -0
  13. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/DocTest/Ocr.py +0 -0
  14. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/DocTest/PdfStructureComparator.py +0 -0
  15. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/DocTest/PrintJob.py +0 -0
  16. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/DocTest/PrintJobTests.py +0 -0
  17. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/DocTest/VisualTest.py +0 -0
  18. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/DocTest/__init__.py +0 -0
  19. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/DocTest/config.py +0 -0
  20. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/DocTest/llm/__init__.py +0 -0
  21. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/DocTest/llm/client.py +0 -0
  22. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/DocTest/llm/config.py +0 -0
  23. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/DocTest/llm/exceptions.py +0 -0
  24. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/DocTest/llm/prompts.py +0 -0
  25. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/DocTest/llm/types.py +0 -0
  26. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/LICENSE +0 -0
  27. {robotframework_doctestlibrary-0.29.1 → robotframework_doctestlibrary-0.30.0.dev1}/README.md +0 -0
@@ -42,6 +42,12 @@ class Page:
42
42
  self._structure_cache: Dict[StructureExtractionConfig, PageStructure] = {}
43
43
  self.is_pdf = False
44
44
  self.ocr_tokens: List[str] = []
45
+ self.rotation: float = 0.0
46
+ self.mediabox: Optional[Tuple[float, float, float, float]] = None
47
+ self.fonts: List = []
48
+ self.images: List = []
49
+ self.signatures: List = []
50
+ self._pdf_ignore_rectangles: List[Tuple[float, float, float, float]] = []
45
51
 
46
52
  def release_resources(self):
47
53
  """Release heavy in-memory artifacts to assist streaming workflows."""
@@ -56,6 +62,7 @@ class Page:
56
62
  self.pixel_ignore_areas = []
57
63
  self._structure_cache.clear()
58
64
  self.ocr_tokens = []
65
+ self._pdf_ignore_rectangles = []
59
66
 
60
67
  def apply_ocr(self, ocr_engine: str = OCR_ENGINE_DEFAULT, tesseract_config: str = TESSERACT_CONFIG, confidence: int = DEFAULT_CONFIDENCE):
61
68
  """Perform OCR on the page image."""
@@ -101,6 +108,77 @@ class Page:
101
108
  self.image = original_image
102
109
  self.image_rescaled_for_ocr = False
103
110
 
111
+ def apply_pixel_masks_to_pdf_text(self):
112
+ """Remove PDF text artifacts that intersect ignore rectangles."""
113
+ if not self.pixel_ignore_areas:
114
+ return
115
+ if not (self.pdf_text_dict or self.pdf_text_words or self.pdf_text_blocks):
116
+ return
117
+
118
+ pdf_rects: List[Tuple[float, float, float, float]] = []
119
+ scale = 72.0 / max(self.dpi, 1)
120
+ for area in self.pixel_ignore_areas:
121
+ x0 = area['x'] * scale
122
+ y0 = area['y'] * scale
123
+ x1 = (area['x'] + area['width']) * scale
124
+ y1 = (area['y'] + area['height']) * scale
125
+ pdf_rects.append((x0, y0, x1, y1))
126
+
127
+ if not pdf_rects:
128
+ return
129
+
130
+ self._pdf_ignore_rectangles = pdf_rects
131
+
132
+ def intersects(bbox: Tuple[float, float, float, float]) -> bool:
133
+ for rx0, ry0, rx1, ry1 in pdf_rects:
134
+ if not (bbox[2] <= rx0 or rx1 <= bbox[0] or bbox[3] <= ry0 or ry1 <= bbox[1]):
135
+ return True
136
+ return False
137
+
138
+ if self.pdf_text_words:
139
+ filtered_words = []
140
+ for word in self.pdf_text_words:
141
+ bbox = (word[0], word[1], word[2], word[3])
142
+ if intersects(bbox):
143
+ continue
144
+ filtered_words.append(word)
145
+ self.pdf_text_words = filtered_words
146
+
147
+ if self.pdf_text_dict and self.pdf_text_dict.get('blocks'):
148
+ filtered_blocks = []
149
+ for block in self.pdf_text_dict.get('blocks', []):
150
+ if block.get('type') != 0:
151
+ filtered_blocks.append(block)
152
+ continue
153
+ new_lines = []
154
+ for line in block.get('lines', []):
155
+ new_spans = []
156
+ for span in line.get('spans', []):
157
+ bbox = tuple(span.get('bbox', (0.0, 0.0, 0.0, 0.0)))
158
+ if intersects(bbox):
159
+ continue
160
+ new_spans.append(span)
161
+ if new_spans:
162
+ line['spans'] = new_spans
163
+ new_lines.append(line)
164
+ if new_lines:
165
+ block['lines'] = new_lines
166
+ filtered_blocks.append(block)
167
+ self.pdf_text_dict['blocks'] = filtered_blocks
168
+
169
+ if self.pdf_text_blocks:
170
+ filtered_text_blocks = []
171
+ for block in self.pdf_text_blocks:
172
+ bbox = None
173
+ if isinstance(block, (list, tuple)) and len(block) > 1 and isinstance(block[1], (list, tuple)):
174
+ bbox = tuple(block[1])
175
+ if bbox and intersects(bbox):
176
+ continue
177
+ filtered_text_blocks.append(block)
178
+ self.pdf_text_blocks = filtered_text_blocks
179
+
180
+ self._structure_cache.clear()
181
+
104
182
  def get_area(self, area: Dict):
105
183
  """Gets the area of the image specified by the coordinates."""
106
184
  x, y, w, h = area['x'], area['y'], area['width'], area['height']
@@ -637,6 +715,9 @@ class Page:
637
715
  elif unit == 'cm':
638
716
  constant = self.dpi / 2.54
639
717
  x, y, w, h = int(x * constant), int(y * constant), int(w * constant), int(h * constant)
718
+ elif unit == 'pt':
719
+ constant = self.dpi / 72.0
720
+ x, y, w, h = int(x * constant), int(y * constant), int(w * constant), int(h * constant)
640
721
  return x, y, w, h
641
722
 
642
723
  def _process_area_ignore_area(self, ignore_area: Dict):
@@ -796,6 +877,8 @@ class DocumentRepresentation:
796
877
  self._pdf_document = None
797
878
  self.page_count: int = 0
798
879
  self._force_ocr_for_ignore_areas = force_ocr
880
+ self.metadata: Dict = {}
881
+ self.sigflags: Optional[int] = None
799
882
 
800
883
  self.load_document()
801
884
  self.create_abstract_ignore_areas(ignore_area_file, ignore_area)
@@ -838,6 +921,8 @@ class DocumentRepresentation:
838
921
  import fitz
839
922
  fitz.TOOLS.set_aa_level(0) # PyMuPDF
840
923
  doc = fitz.open(str(self.file_path))
924
+ self.metadata = doc.metadata
925
+ self.sigflags = doc.get_sigflags()
841
926
  for page_num in range(len(doc)):
842
927
  page = doc.load_page(page_num)
843
928
  pix = page.get_pixmap(dpi=self.dpi)
@@ -849,6 +934,11 @@ class DocumentRepresentation:
849
934
  page_obj.pdf_text_dict = page.get_text("dict", sort=True)
850
935
  page_obj.pdf_text_words = page.get_text("words", sort=True)
851
936
  page_obj.pdf_text_blocks = page.get_text("blocks", sort=True)
937
+ page_obj.rotation = page.rotation
938
+ page_obj.mediabox = tuple(page.mediabox)
939
+ page_obj.fonts = page.get_fonts()
940
+ page_obj.images = page.get_images()
941
+ page_obj.signatures = self._extract_signatures(page)
852
942
  self.pages.append(page_obj)
853
943
  self.page_count = len(self.pages)
854
944
  except ImportError:
@@ -859,6 +949,8 @@ class DocumentRepresentation:
859
949
  import fitz
860
950
  fitz.TOOLS.set_aa_level(0)
861
951
  self._pdf_document = fitz.open(str(self.file_path))
952
+ self.metadata = self._pdf_document.metadata
953
+ self.sigflags = self._pdf_document.get_sigflags()
862
954
  self.page_count = len(self._pdf_document)
863
955
  except ImportError:
864
956
  raise ImportError("PyMuPDF (fitz) is required for PDF processing.")
@@ -877,6 +969,11 @@ class DocumentRepresentation:
877
969
  page_obj.pdf_text_dict = page.get_text("dict", sort=True)
878
970
  page_obj.pdf_text_words = page.get_text("words", sort=True)
879
971
  page_obj.pdf_text_blocks = page.get_text("blocks", sort=True)
972
+ page_obj.rotation = page.rotation
973
+ page_obj.mediabox = tuple(page.mediabox)
974
+ page_obj.fonts = page.get_fonts()
975
+ page_obj.images = page.get_images()
976
+ page_obj.signatures = self._extract_signatures(page)
880
977
 
881
978
  self._apply_ignore_areas_to_page(page_obj, self._force_ocr_for_ignore_areas)
882
979
  if self.contains_barcodes:
@@ -1158,6 +1255,7 @@ class DocumentRepresentation:
1158
1255
  if (force_ocr or not page.pdf_text_data) and not page.ocr_performed:
1159
1256
  page.apply_ocr(ocr_engine=self.ocr_engine)
1160
1257
  page._process_ignore_area(ignore_area)
1258
+ page.apply_pixel_masks_to_pdf_text()
1161
1259
 
1162
1260
  def get_text_from_area(self, area: Dict, force_ocr: bool = False):
1163
1261
  """Extract text content from a specific area of the document."""
@@ -1220,11 +1318,26 @@ class DocumentRepresentation:
1220
1318
  if ignore_area.get('page') == page.page_number or ignore_area.get('page') == 'all':
1221
1319
  page.ignore_areas.append(ignore_area)
1222
1320
 
1321
+ @staticmethod
1322
+ def _extract_signatures(page):
1323
+ signatures = []
1324
+ try:
1325
+ widgets = page.widgets()
1326
+ except Exception:
1327
+ widgets = []
1328
+ if not widgets:
1329
+ return signatures
1330
+ for widget in widgets:
1331
+ if getattr(widget, "is_signed", False):
1332
+ signatures.append(
1333
+ [
1334
+ getattr(widget, "field_name", ""),
1335
+ getattr(widget, "field_label", ""),
1336
+ getattr(widget, "field_value", ""),
1337
+ ]
1338
+ )
1339
+ return signatures
1223
1340
 
1224
- def identify_barcodes(self):
1225
- """Detect barcodes in all pages."""
1226
- for page in self.pages:
1227
- page.identify_barcodes()
1228
1341
 
1229
1342
  def make_text(words):
1230
1343
  """Return textstring output of get_text("words").
@@ -3,6 +3,8 @@ from __future__ import annotations
3
3
  from dataclasses import dataclass, field
4
4
  from typing import Dict, Iterable, List, Optional, Sequence, Set, Tuple
5
5
 
6
+ from DocTest.TextNormalization import normalize_ligatures
7
+
6
8
 
7
9
  __all__ = [
8
10
  "TextSpan",
@@ -75,6 +77,7 @@ class StructureExtractionConfig:
75
77
  strip_line_edges: bool = True
76
78
  drop_empty_lines: bool = True
77
79
  round_precision: Optional[int] = 3
80
+ normalize_ligatures: bool = False
78
81
 
79
82
  def __hash__(self) -> int: # Allow usage as dictionary key for caching.
80
83
  return hash(
@@ -85,6 +88,7 @@ class StructureExtractionConfig:
85
88
  self.strip_line_edges,
86
89
  self.drop_empty_lines,
87
90
  self.round_precision,
91
+ self.normalize_ligatures,
88
92
  )
89
93
  )
90
94
 
@@ -135,6 +139,8 @@ def _sanitize_span_text(text: str, config: StructureExtractionConfig) -> str:
135
139
  text = collapse_whitespace(text, config.whitespace_replacement)
136
140
  if config.strip_line_edges:
137
141
  text = text.strip()
142
+ if config.normalize_ligatures:
143
+ text = normalize_ligatures(text)
138
144
  return text
139
145
 
140
146