Skip to content

Add line-level pagination with widow/orphan control#120

Open
JSv4 wants to merge 1 commit intomainfrom
claude/investigate-issue-30-GY2bJ
Open

Add line-level pagination with widow/orphan control#120
JSv4 wants to merge 1 commit intomainfrom
claude/investigate-issue-30-GY2bJ

Conversation

@JSv4
Copy link
Copy Markdown
Owner

@JSv4 JSv4 commented Mar 21, 2026

Summary

Implements line-level paragraph splitting during pagination to prevent widows and orphans, addressing Issue #30. Paragraphs that exceed page height are now intelligently split at line boundaries rather than overflowing, with configurable minimum line constraints at page boundaries.

Key Changes

  • Line measurement infrastructure: Added measureLines() method using the Range API and getClientRects() to precisely detect line boundaries within paragraph elements
  • Paragraph splitting: Implemented splitParagraphAtLine() to split paragraphs at specific line boundaries using CSS clipping and margin adjustments
  • Widow/orphan constraints: Added trySplitWithWidowOrphan() to enforce minimum line counts:
    • minOrphanLines (default: 2): minimum lines kept at bottom of page
    • minWidowLines (default: 2): minimum lines moved to top of next page
  • Multi-page flow handling: Added flowSplitRemainder() and splitBlockAcrossPages() to handle remainders that span multiple pages
  • Configuration options: Extended PaginationOptions with minOrphanLines and minWidowLines parameters
  • Block metadata: Added widowControl property to MeasuredBlock interface, parsed from data-widow-control attribute (defaults to true per Word behavior)
  • Pagination logic: Updated main pagination loop to attempt line-level splitting before moving entire blocks to new pages, respecting keepLines property

Implementation Details

  • Line detection clusters character rectangles by vertical position to identify distinct lines
  • Split points are validated against both orphan and widow constraints before applying
  • Respects existing w:keepLines paragraph property to prevent splitting when specified
  • Handles edge cases: single-line paragraphs, blocks taller than full page height, and remainder overflow
  • Uses helper methods with callback parameters to manage state during complex multi-page splits

https://claude.ai/code/session_01PzsJ5ZDVcFnZ2tTo28bDBa

…ntrol (Issue #30)

Paragraphs exceeding page height are now split at line boundaries using
Range API measurement instead of overflowing. Widow/orphan control ensures
configurable minimum lines at page top/bottom (default 2). Respects Word's
w:widowControl and w:keepLines properties via existing data attributes.

https://claude.ai/code/session_01PzsJ5ZDVcFnZ2tTo28bDBa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants