Spaces:
Sleeping
Sleeping
File size: 9,011 Bytes
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 | """
Content Stream Parser Module
Provides functionality for extracting and analyzing PDF content stream operators,
correlating them with visual blocks.
"""
import re
from typing import Dict, List, Optional, Any, Tuple
import fitz # PyMuPDF
def extract_content_stream_for_block(
pdf_path: str,
page_index: int,
block_index: int,
blocks: List[Any]
) -> Dict[str, Any]:
"""
Extract content stream operators for a specific block.
Args:
pdf_path: Path to the PDF file
page_index: 0-based page index
block_index: Index of the block to analyze
blocks: List of BlockInfo objects from extract_blocks_spans
Returns:
Dictionary with operators, raw stream, and metadata
"""
if block_index < 0 or block_index >= len(blocks):
return {
'error': 'Invalid block index',
'operators': [],
'raw_stream': ''
}
target_block = blocks[block_index]
try:
doc = fitz.open(pdf_path)
page = doc[page_index]
# Clean and consolidate content streams
page.clean_contents()
# Get the page's content stream xref
xref = page.get_contents()[0] # Get first content stream xref
# Extract raw stream data
stream_data = doc.xref_stream(xref)
try:
raw_stream = stream_data.decode('latin-1')
except:
raw_stream = stream_data.decode('utf-8', errors='ignore')
# Parse text objects from the stream
text_objects = _parse_text_objects(raw_stream)
# Find the text object that matches our target block
matching_object = _find_matching_text_object(text_objects, target_block)
doc.close()
if matching_object:
return {
'operators': matching_object['operators'],
'raw_stream': raw_stream,
'matched': True,
'block_text': target_block.text[:100]
}
else:
return {
'operators': [],
'raw_stream': raw_stream,
'matched': False,
'block_text': target_block.text[:100],
'message': 'Could not find matching text object in content stream'
}
except Exception as e:
return {
'error': str(e),
'operators': [],
'raw_stream': ''
}
def _parse_text_objects(content_stream: str) -> List[Dict[str, Any]]:
"""
Parse text objects (BT...ET blocks) from content stream.
Args:
content_stream: Raw PDF content stream text
Returns:
List of text objects with their operators
"""
text_objects = []
# Find all BT...ET blocks
bt_et_pattern = r'BT\s+(.*?)\s+ET'
matches = re.finditer(bt_et_pattern, content_stream, re.DOTALL)
for match in matches:
text_block = match.group(1)
operators = _parse_operators(text_block)
text_objects.append({
'operators': operators,
'text': _extract_text_from_operators(operators)
})
return text_objects
def _parse_operators(text_block: str) -> List[Dict[str, str]]:
"""
Parse individual operators from a text block.
Args:
text_block: Text between BT and ET
Returns:
List of operator dictionaries with type and value
"""
operators = []
# Text matrix (Tm)
tm_pattern = r'([\d.\-\s]+)\s+Tm'
for match in re.finditer(tm_pattern, text_block):
operators.append({
'type': 'Tm',
'value': match.group(1).strip(),
'description': 'Text Matrix'
})
# Font (Tf)
tf_pattern = r'/(\S+)\s+([\d.]+)\s+Tf'
for match in re.finditer(tf_pattern, text_block):
operators.append({
'type': 'Tf',
'value': f'/{match.group(1)} {match.group(2)}',
'description': f'Font: {match.group(1)}, Size: {match.group(2)}'
})
# Text positioning (Td, TD)
td_pattern = r'([\d.\-]+)\s+([\d.\-]+)\s+T[dD]'
for match in re.finditer(td_pattern, text_block):
operators.append({
'type': 'Td',
'value': f'{match.group(1)} {match.group(2)}',
'description': f'Move text position ({match.group(1)}, {match.group(2)})'
})
# Text showing (Tj)
tj_pattern = r'\((.*?)\)\s*Tj'
for match in re.finditer(tj_pattern, text_block):
text = match.group(1)
operators.append({
'type': 'Tj',
'value': f'({text})',
'description': f'Show text: {text[:50]}'
})
# Text showing (TJ - array)
tj_array_pattern = r'\[(.*?)\]\s*TJ'
for match in re.finditer(tj_array_pattern, text_block, re.DOTALL):
array_content = match.group(1)
operators.append({
'type': 'TJ',
'value': f'[{array_content[:100]}]',
'description': 'Show text array'
})
# Text leading (TL)
tl_pattern = r'([\d.\-]+)\s+TL'
for match in re.finditer(tl_pattern, text_block):
operators.append({
'type': 'TL',
'value': match.group(1),
'description': f'Text leading: {match.group(1)}'
})
# Color operators (rg, RG, g, G)
color_pattern = r'([\d.\s]+)\s+(rg|RG|g|G)'
for match in re.finditer(color_pattern, text_block):
operators.append({
'type': match.group(2),
'value': match.group(1).strip(),
'description': f'Color: {match.group(1).strip()}'
})
return operators
def _extract_text_from_operators(operators: List[Dict[str, str]]) -> str:
"""
Extract visible text from operator list.
Args:
operators: List of operator dictionaries
Returns:
Concatenated text content
"""
text_parts = []
for op in operators:
if op['type'] in ['Tj', 'TJ']:
# Extract text from parentheses or array
value = op['value']
# Simple extraction - just get content in parentheses
matches = re.findall(r'\((.*?)\)', value)
text_parts.extend(matches)
return ' '.join(text_parts)
def _find_matching_text_object(
text_objects: List[Dict[str, Any]],
target_block: Any
) -> Optional[Dict[str, Any]]:
"""
Find the text object that best matches the target block.
Args:
text_objects: List of parsed text objects
target_block: BlockInfo object to match
Returns:
Matching text object or None
"""
target_text = target_block.text.strip()
if not target_text:
return None
best_match = None
best_score = 0
for text_obj in text_objects:
obj_text = text_obj['text'].strip()
if not obj_text:
continue
# Calculate similarity score (simple substring matching)
# Check if either text contains the other
if target_text in obj_text or obj_text in target_text:
score = min(len(target_text), len(obj_text)) / max(len(target_text), len(obj_text))
if score > best_score:
best_score = score
best_match = text_obj
# Only return match if score is reasonable
if best_score > 0.3:
return best_match
return None
def format_operators_markdown(result: Dict[str, Any]) -> str:
"""
Format operators as readable Markdown.
Args:
result: Result dictionary from extract_content_stream_for_block
Returns:
Formatted Markdown string
"""
if 'error' in result:
return f"## Error\n\n{result['error']}"
lines = [
"## Content Stream Operators",
"",
f"**Block Text**: {result.get('block_text', 'N/A')}",
""
]
if not result.get('matched'):
lines.extend([
"⚠️ **Warning**: Could not find exact matching text object in content stream.",
"",
result.get('message', ''),
""
])
operators = result.get('operators', [])
if operators:
lines.extend([
"### Operators Found",
""
])
for i, op in enumerate(operators, 1):
lines.append(f"**{i}. {op['type']}**")
lines.append(f" - Value: `{op['value']}`")
lines.append(f" - {op['description']}")
lines.append("")
else:
lines.append("No operators found.")
return "\n".join(lines)
def format_raw_stream(raw_stream: str, max_lines: int = 100) -> str:
"""
Format raw content stream for display.
Args:
raw_stream: Raw PDF content stream text
max_lines: Maximum number of lines to display
Returns:
Formatted string
"""
lines = raw_stream.split('\n')
if len(lines) > max_lines:
lines = lines[:max_lines]
lines.append(f"\n... (truncated, {len(raw_stream.split('\n')) - max_lines} more lines)")
return '\n'.join(lines)
|