Fix right alignment with pre-wrap trailing spaces#2750
Open
moreaki wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
This adjusts line alignment so that trailing spaces preserved by
white-space: pre-wraphang for alignment purposes. The spaces remain in the line box, buttext-align: right/end/centercomputes 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-wrapshould hang rather than contribute to the visible right-alignment inset.Why
In #2384, right-aligned
white-space: pre-wraptext 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_sizestill defaults tonormal,nowrap, andpre-line; the newpre-wrappath is requested explicitly by text alignment.Related investigation
I followed the #2384 comments and related links before making this patch:
dl/dt/ddcontent and anonymous block construction arounddt::before. This PR does not claim to fix Right align in sample invoice #751.display: inline#2504 / PR Add tests for PDF/UA #2496 in the context of PDF/UA sample invoice testing. Those links did not include an existing whitespace alignment patch.display: inline#2504,pre-wrap,white-space,hang spaces, and trailing-space terms, and did not find an existing upstream PR for this specific fix.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.pyvenv/bin/python -m pytest tests/test_text.py tests/layout/test_inline.py tests/layout/test_preferred.py -q-> 294 passed, 3 xfailedvenv/bin/python -m pytest tests/draw/test_text.py -q-> 42 passed, 5 xfailedvenv/bin/python -m pytest-> 4026 passed, 40 xfailedFixes #2384.
Related to #751 and #2504.