Skip to content

Fix right alignment with pre-wrap trailing spaces#2750

Open
moreaki wants to merge 1 commit into
Kozea:mainfrom
moreaki:fix-pre-wrap-text-align
Open

Fix right alignment with pre-wrap trailing spaces#2750
moreaki wants to merge 1 commit into
Kozea:mainfrom
moreaki:fix-pre-wrap-text-align

Conversation

@moreaki
Copy link
Copy Markdown
Contributor

@moreaki moreaki commented Apr 22, 2026

What changed

This adjusts line alignment so that trailing spaces preserved by white-space: pre-wrap hang for alignment purposes. The spaces remain in the line box, but text-align: right/end/center computes the alignment offset from the non-hanging content width.

This matches the CSS Text phase 2 rule linked from #2384: end-of-line preserved spaces with pre-wrap should hang rather than contribute to the visible right-alignment inset.

Why

In #2384, right-aligned white-space: pre-wrap text gets an unwanted right-side gap. The line content includes the preserved break-triggering trailing spaces, and WeasyPrint was using that full line width to calculate the alignment offset. As a result, the visible words were shifted too far left.

Scope

This is a small targeted fix, not full CSS Text Level 4 whitespace support. Existing collapsing-space callers keep their current behavior because trailing_whitespace_size still defaults to normal, nowrap, and pre-line; the new pre-wrap path is requested explicitly by text alignment.

Related investigation

I followed the #2384 comments and related links before making this patch:

Tests

Added test_text_align_right_pre_wrap_trailing_space.

Validation run locally:

  • venv/bin/python -m ruff check weasyprint/layout/inline.py weasyprint/layout/preferred.py tests/test_text.py
  • venv/bin/python -m pytest tests/test_text.py tests/layout/test_inline.py tests/layout/test_preferred.py -q -> 294 passed, 3 xfailed
  • venv/bin/python -m pytest tests/draw/test_text.py -q -> 42 passed, 5 xfailed
  • venv/bin/python -m pytest -> 4026 passed, 40 xfailed

Fixes #2384.
Related to #751 and #2504.

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.

Right aligned text with white-space: pre-wrap Right align in sample invoice

1 participant