typst / typst/typst

Allowing orphans creates widows for 2-line paragraphs

Open Beginner friendly
#8,604 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug layout
Dominant language
Rust
Stars
56.1k
Forks
1.7k
Avg merge
3d 10h
Merged PRs (30d)
22

Description

Description

While reproducing a book previously formatted in a different system that was configured to permit orphans but not widows, I found that adjusting text costs to permit orphans (#set text(costs: (orphan: 0%))) while leaving widow cost at 100% produced widows for 2-line paragraphs. I expected widow prevention to trump permitting orphans in that case.

From what I can see, the issue appears to be in Collector::lines (collect.rs in typst-layout, line 222):
} else if prevent_widows && i >= 2 && i + 2 == len {
where the i >= 2 check blocks widow handling for the short paragraphs. The check seems unnecessary to me, and removing it does resolve the issue, but I'm not sure if I'm missing something. Looking back through the history of the code it seems like it could be a holdover from converting earlier widow handling logic in PR #4767 where it protected an array access, so might be superfluous now.

Attached is a small source to exhibit the issue, along with current bad output (typst 0.15.0) and the corrected result (0.15.0 without the i>=2 check).

widow.typ.txt

widow-bad.pdf

widow-fix.pdf

Operating system

macOS, Linux

Typst version
  • I am using the latest version of Typst

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in typst-layout's collect.rs at Collector::lines, around line 222, and reproduce the issue with the attached widow.typ.txt source. Compare the current bad output with the corrected PDF to verify behavior for 2-line paragraphs when orphan cost is 0% and widow cost is 100%. Done means widow prevention works as expected without regressing the surrounding line-collection logic.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.