feat: implement proper track-specific PDF rendering

Implement independent Direct and OCR track rendering methods with
complete separation of concerns and proper line break handling.

**Architecture Changes**:
- Created _generate_direct_track_pdf() for rich formatting
- Created _generate_ocr_track_pdf() for backward compatible rendering
- Modified generate_from_unified_document() to route by track type
- No more shared rendering path that loses information

**Direct Track Features** (_generate_direct_track_pdf):
- Processes UnifiedDocument directly (no legacy conversion)
- Preserves all StyleInfo without information loss
- Handles line breaks (\n) in text content
- Layer-based rendering: images → tables → text
- Three specialized helper methods:
  - _draw_text_element_direct(): Multi-line text with styling
  - _draw_table_element_direct(): Direct bbox table rendering
  - _draw_image_element_direct(): Image positioning from bbox

**OCR Track Features** (_generate_ocr_track_pdf):
- Uses legacy OCR data conversion pipeline
- Routes to existing _generate_pdf_from_data()
- Maintains full backward compatibility
- Simplified rendering for OCR-detected layout

**Line Break Handling** (Direct Track):
- Split text on '\n' into multiple lines
- Calculate line height as font_size * 1.2
- Render each line with proper vertical spacing
- Font scaling per line if width exceeds bbox

**Implementation Details**:
Lines 535-569: Track detection and routing
Lines 571-670: _generate_direct_track_pdf() main method
Lines 672-717: _generate_ocr_track_pdf() main method
Lines 1497-1575: _draw_text_element_direct() with line breaks
Lines 1577-1656: _draw_table_element_direct()
Lines 1658-1714: _draw_image_element_direct()

**Corrected Task Status**:
- Task 4.2: NOW properly implements separate Direct track pipeline
- Task 4.3: NOW properly implements separate OCR track pipeline
- Both with distinct rendering logic as designed

**Breaking vs Previous Commit**:
Previous commit (3fc32bc) only added conditional styling in shared
draw_text_region(). This commit creates true track-specific pipelines
as per design.md requirements.

Direct track PDFs will now:
 Process without legacy conversion (no info loss)
 Render multi-line text properly (split on \n)
 Apply StyleInfo per element
 Use precise bbox positioning
 Render images and tables directly

OCR track PDFs will:
 Use existing proven pipeline
 Maintain backward compatibility
 No changes to current behavior

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
egg
2025-11-24 07:53:17 +08:00
parent 3fc32bcdd7
commit 09cf9149ce
2 changed files with 393 additions and 19 deletions

View File

@@ -57,16 +57,19 @@
### 4. Track-Specific Rendering
- [x] 4.1 Add track detection in generate_from_unified_document
- [x] 4.1.1 Check unified_doc.metadata.processing_track (object and dict support)
- [x] 4.1.2 Store in self.current_processing_track for rendering methods
- [x] 4.2 Apply StyleInfo for Direct track
- [x] 4.2.1 Preserve style information in convert_unified_document_to_ocr_data
- [x] 4.2.2 Apply StyleInfo to text elements in draw_text_region
- [x] 4.2.3 Use precise positioning (existing implementation maintained)
- [x] 4.2.4 Track detection in draw_text_region (is_direct_track check)
- [x] 4.3 Simplified rendering for OCR track
- [x] 4.3.1 Use simple font selection when not Direct track
- [x] 4.3.2 Best-effort positioning (existing implementation)
- [x] 4.3.3 Estimated font sizes (bbox height-based heuristic)
- [x] 4.1.2 Route to _generate_direct_track_pdf or _generate_ocr_track_pdf
- [x] 4.2 Implement _generate_direct_track_pdf
- [x] 4.2.1 Process each page directly from UnifiedDocument (no legacy conversion)
- [x] 4.2.2 Apply StyleInfo to text elements (_draw_text_element_direct)
- [x] 4.2.3 Use precise positioning from element.bbox
- [x] 4.2.4 Preserve line breaks (split on \n, render multi-line)
- [x] 4.2.5 Implement _draw_text_element_direct with line break handling
- [x] 4.2.6 Implement _draw_table_element_direct for tables
- [x] 4.2.7 Implement _draw_image_element_direct for images
- [x] 4.3 Implement _generate_ocr_track_pdf
- [x] 4.3.1 Use legacy OCR data conversion (convert_unified_document_to_ocr_data)
- [x] 4.3.2 Route to existing _generate_pdf_from_data pipeline
- [x] 4.3.3 Maintain backward compatibility with OCR track behavior
- [ ] 4.4 Test track-specific rendering
- [ ] 4.4.1 Compare Direct track with original
- [ ] 4.4.2 Verify OCR track maintains quality