Spaces:
Sleeping
Sleeping
File size: 11,448 Bytes
27fda3f 0d61aa0 27fda3f 0d61aa0 27fda3f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 | """
Screen Reader Simulator Module
Simulates how NVDA and JAWS would read a PDF page, supporting both
tagged (structure tree) and untagged (visual order fallback) PDFs.
"""
from typing import Dict, List, Any, Optional, Tuple
import pikepdf
from structure_tree import extract_structure_tree, StructureNode
def simulate_screen_reader(
pdf_path: str,
page_index: int,
blocks: List[Any],
reader_type: str = "NVDA",
detail_level: str = "default",
order_mode: str = "tblr"
) -> Dict[str, Any]:
"""
Simulate screen reader output for a PDF page.
Args:
pdf_path: Path to PDF file
page_index: 0-based page index
blocks: List of BlockInfo objects from extract_blocks_spans
reader_type: "NVDA" or "JAWS"
detail_level: "minimal", "default", or "verbose"
order_mode: Reading order mode for untagged fallback ("raw", "tblr", "columns")
Returns:
Dictionary with transcript, analysis, and metadata
"""
# Try tagged approach first
root = extract_structure_tree(pdf_path)
if root:
# Use structure tree
transcript, analysis = _simulate_tagged(
root, page_index, reader_type, detail_level
)
mode = "tagged"
else:
# Fallback to visual order
transcript, analysis = _simulate_untagged(
blocks, reader_type, detail_level, order_mode
)
mode = "untagged"
return {
'transcript': transcript,
'analysis': analysis,
'mode': mode,
'reader_type': reader_type,
'detail_level': detail_level
}
def _simulate_tagged(
root: StructureNode,
page_index: int,
reader_type: str,
detail_level: str
) -> Tuple[str, str]:
"""
Simulate screen reader for tagged PDF using structure tree.
Args:
root: Root StructureNode
page_index: Page to simulate (0-based)
reader_type: "NVDA" or "JAWS"
detail_level: Detail level
Returns:
Tuple of (transcript, analysis)
"""
# Collect structure elements for this page
page_elements = []
def _collect_page_elements(node: StructureNode):
# Include node if it's for this page or has no page ref (document-level)
if node.page_ref is None or node.page_ref == page_index:
if node.tag_type not in ['StructTreeRoot', 'MCID']:
page_elements.append(node)
for child in node.children:
_collect_page_elements(child)
_collect_page_elements(root)
# Generate transcript
transcript_lines = []
element_count = 0
for element in page_elements:
announcement = _format_element_announcement(
element, reader_type, detail_level
)
if announcement:
transcript_lines.append(announcement)
element_count += 1
transcript = '\n\n'.join(transcript_lines)
# Generate analysis
analysis_lines = [
"## Screen Reader Analysis (Tagged Mode)",
"",
f"**Structure**: This page uses PDF tagging (accessible structure tree)",
f"**Elements Found**: {element_count}",
""
]
# Count element types
tag_counts = {}
for element in page_elements:
tag_counts[element.tag_type] = tag_counts.get(element.tag_type, 0) + 1
if tag_counts:
analysis_lines.extend([
"### Element Types",
""
])
for tag, count in sorted(tag_counts.items()):
analysis_lines.append(f"- **{tag}**: {count}")
# Check for alt text coverage
elements_needing_alt = [e for e in page_elements if e.tag_type in ['Figure', 'Formula', 'Artifact']]
elements_with_alt = [e for e in elements_needing_alt if e.alt_text]
if elements_needing_alt:
coverage = len(elements_with_alt) / len(elements_needing_alt) * 100
analysis_lines.extend([
"",
"### Alt Text Coverage",
"",
f"**Elements needing alt text**: {len(elements_needing_alt)}",
f"**Elements with alt text**: {len(elements_with_alt)}",
f"**Coverage**: {coverage:.1f}%",
""
])
if coverage < 100:
analysis_lines.append("⚠️ Some elements are missing alt text")
analysis = '\n'.join(analysis_lines)
return transcript, analysis
def _simulate_untagged(
blocks: List[Any],
reader_type: str,
detail_level: str,
order_mode: str
) -> Tuple[str, str]:
"""
Simulate screen reader for untagged PDF using visual order.
Args:
blocks: List of BlockInfo objects
reader_type: "NVDA" or "JAWS"
detail_level: Detail level
order_mode: Reading order mode
Returns:
Tuple of (transcript, analysis)
"""
from layout_utils import order_blocks # Import the ordering function
# Order blocks according to mode
ordered_blocks = order_blocks(blocks, order_mode)
# Generate transcript
transcript_lines = []
text_block_count = 0
image_block_count = 0
for idx, block in ordered_blocks:
if block.block_type == 0: # Text block
# Infer heading from font size
is_heading = False
heading_level = None
if block.spans:
avg_size = sum(s.size for s in block.spans) / len(block.spans)
if avg_size > 18:
is_heading = True
heading_level = 1
elif avg_size > 14:
is_heading = True
heading_level = 2
# Format announcement
if is_heading and detail_level != "minimal":
if reader_type == "NVDA":
transcript_lines.append(f"Heading level {heading_level}")
transcript_lines.append(block.text.strip())
else: # JAWS
transcript_lines.append(f"Heading {heading_level}: {block.text.strip()}")
else:
transcript_lines.append(block.text.strip())
text_block_count += 1
elif block.block_type == 1: # Image block
if detail_level != "minimal":
transcript_lines.append("[Image - no alt text available]")
image_block_count += 1
transcript = '\n\n'.join(transcript_lines)
# Generate analysis
analysis_lines = [
"## Screen Reader Analysis (Untagged Mode)",
"",
"⚠️ **No Structure**: This page does not use PDF tagging",
"",
"Screen readers will read text in visual order with limited context.",
"",
f"**Reading Order Mode**: {order_mode}",
f"**Text Blocks**: {text_block_count}",
f"**Images**: {image_block_count}",
"",
"### Limitations",
"",
"- No semantic information (headings, lists, tables)",
"- No alt text for images",
"- Reading order may not match intended flow",
"- Navigation by elements not possible",
"",
"**Recommendation**: Add PDF tagging for better accessibility"
]
analysis = '\n'.join(analysis_lines)
return transcript, analysis
def _format_element_announcement(
element: StructureNode,
reader_type: str,
detail_level: str
) -> Optional[str]:
"""
Format a structure element as a screen reader announcement.
Args:
element: StructureNode to announce
reader_type: "NVDA" or "JAWS"
detail_level: "minimal", "default", or "verbose"
Returns:
Formatted announcement string or None
"""
tag = element.tag_type
lines = []
# Map PDF tag types to screen reader announcements
if tag.startswith('H'):
# Heading
level = tag[1:] if len(tag) > 1 else '1'
text = element.actual_text or "[Heading]"
if detail_level == "minimal":
return text
if reader_type == "NVDA":
lines.append(f"Heading level {level}")
lines.append(text)
else: # JAWS
lines.append(f"Heading {level}: {text}")
elif tag == 'P':
# Paragraph
text = element.actual_text or "[Paragraph]"
if detail_level == "minimal":
return text
if detail_level == "verbose":
if reader_type == "NVDA":
lines.append("Paragraph")
lines.append(text)
if reader_type == "NVDA" and detail_level == "verbose":
lines.append("Out of paragraph")
else:
lines.append(text)
elif tag == 'Figure':
# Figure/Image
alt_text = element.alt_text or "[Image - no alt text]"
if detail_level == "minimal":
return None
if reader_type == "NVDA":
lines.append("Graphic")
lines.append(alt_text)
else: # JAWS
lines.append(f"Graphic: {alt_text}")
elif tag == 'Formula':
# Math formula
alt_text = element.alt_text or element.actual_text or "[Formula]"
if detail_level == "minimal":
return alt_text
if reader_type == "NVDA":
lines.append("Formula")
lines.append(alt_text)
else: # JAWS
lines.append(f"Formula: {alt_text}")
elif tag in ['L', 'LI']:
# List/List Item
text = element.actual_text or "[List item]"
if detail_level == "minimal":
return text
if tag == 'L' and detail_level == "verbose":
lines.append("List start")
else:
if reader_type == "NVDA":
lines.append("List item")
lines.append(text)
else: # JAWS
lines.append(f"Bullet: {text}")
elif tag == 'Table':
# Table
if detail_level != "minimal":
if reader_type == "NVDA":
lines.append("Table")
else: # JAWS
lines.append("Table start")
elif tag in ['TR', 'TD', 'TH']:
# Table row/cell
text = element.actual_text or ""
if text and detail_level != "minimal":
lines.append(text)
elif tag == 'Link':
# Link
text = element.actual_text or "[Link]"
if detail_level == "minimal":
return text
if reader_type == "NVDA":
lines.append("Link")
lines.append(text)
else: # JAWS
lines.append(f"Link: {text}")
elif tag == 'Span':
# Inline text
text = element.actual_text or ""
if text:
return text
elif tag in ['Document', 'Part', 'Sect', 'Div', 'Art']:
# Container elements - usually not announced
return None
else:
# Unknown tag type
if element.actual_text:
return element.actual_text
if lines:
return '\n'.join(lines)
return None
def format_transcript(result: Dict[str, Any]) -> str:
"""
Format screen reader transcript for display.
Args:
result: Result from simulate_screen_reader
Returns:
Formatted transcript string
"""
header = f"# {result['reader_type']} Transcript ({result['detail_level']} detail)\n\n"
if result['mode'] == 'untagged':
header += "⚠️ Simulated from visual order (PDF not tagged)\n\n"
header += "---\n\n"
return header + result['transcript']
|