feat: implement Phase 3 enhanced text rendering with alignment and formatting

Enhance Direct track text rendering with comprehensive layout preservation:

**Text Alignment (Task 5.3)**
- Add support for left/right/center/justify alignment from StyleInfo
- Calculate line position based on alignment setting
- Implement word spacing distribution for justify alignment
- Apply alignment per-line in _draw_text_element_direct

**Paragraph Formatting (Task 5.2)**
- Extract indentation from element metadata (indent, first_line_indent)
- Apply first line indent to first line, regular indent to subsequent lines
- Add paragraph spacing support (spacing_before, spacing_after)
- Respect available width after applying indentation

**Line Rendering Enhancements (Task 5.1)**
- Split text content on newlines for multi-line rendering
- Calculate line height as font_size * 1.2
- Position each line with proper vertical spacing
- Scale font dynamically to fit available width

**Implementation Details**
- Modified: backend/app/services/pdf_generator_service.py:1497-1629
  - Enhanced _draw_text_element_direct with alignment logic
  - Added justify mode with word-by-word positioning
  - Integrated indentation and spacing from metadata
- Updated: openspec/changes/pdf-layout-restoration/tasks.md
  - Marked Phase 3 tasks 5.1-5.3 as completed

**Technical Notes**
- Justify alignment only applies to non-final lines (last line left-aligned)
- Font scaling applies per-line if text exceeds available width
- Empty lines skipped but maintain line spacing
- Alignment extracted from StyleInfo.alignment attribute

🤖 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 08:05:48 +08:00
parent 09cf9149ce
commit 77fe4ccb8b
2 changed files with 79 additions and 24 deletions

View File

@@ -77,18 +77,19 @@
## Phase 3: Advanced Layout (P2 - Week 2)
### 5. Enhanced Text Rendering
- [ ] 5.1 Implement line-by-line rendering
- [ ] 5.1.1 Split text content by newlines
- [ ] 5.1.2 Calculate line height from font size
- [ ] 5.1.3 Render each line with proper spacing
- [ ] 5.2 Add paragraph handling
- [ ] 5.2.1 Detect paragraph boundaries
- [ ] 5.2.2 Apply paragraph spacing
- [ ] 5.2.3 Handle indentation
- [ ] 5.3 Implement text alignment
- [ ] 5.3.1 Support left/right/center/justify
- [ ] 5.3.2 Calculate positioning based on alignment
- [ ] 5.3.3 Apply to each text block
- [x] 5.1 Implement line-by-line rendering
- [x] 5.1.1 Split text content by newlines (text.split('\n'))
- [x] 5.1.2 Calculate line height from font size (font_size * 1.2)
- [x] 5.1.3 Render each line with proper spacing (line_y = pdf_y - i * line_height)
- [x] 5.2 Add paragraph handling
- [x] 5.2.1 Detect paragraph boundaries (via element.type PARAGRAPH)
- [x] 5.2.2 Apply paragraph spacing (spacing_before/spacing_after from metadata)
- [x] 5.2.3 Handle indentation (indent/first_line_indent from metadata)
- [x] 5.3 Implement text alignment
- [x] 5.3.1 Support left/right/center/justify (from StyleInfo.alignment)
- [x] 5.3.2 Calculate positioning based on alignment (line_x calculation)
- [x] 5.3.3 Apply to each text block (per-line alignment in _draw_text_element_direct)
- [x] 5.3.4 Justify alignment with word spacing distribution
### 6. List Formatting
- [ ] 6.1 Detect list elements