Skip to content

Remove tracking from the last glyph in a line - #8963

Merged
laurmaedje merged 4 commits into
typst:mainfrom
chinzhening:remove-last-glyph-tracking
Oct 21, 2026
Merged

laurmaedje merged 4 commits into
typst:mainfrom
chinzhening:remove-last-glyph-tracking

Conversation

@chinzhening

@chinzhening chinzhening commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

tracking is flattened into x_advance during the shaping stage and this is always retained by the last non-space glyph in a line except in (at least) two cases:

  1. when a hyphen is added to the line end (constructed with no tracking)
  2. and for the last glyph of the paragraph (no tracking added during shaping).

One consequence of this is that line width includes the trailing tracking, which is not the right behavior. This manifests in #8931 as a right gutter at the line end when justification is applied. A naive fix would be to subtract tracking from the x_advance of the last glyph of the line (if we are not in 1. or 2.). But this is fragile because it is unclear if tracking has been applied to the last glyph.

This PR adds a tracking_applied boolean field to shaped glyphs, so the line end fix only removes tracking when it has actually been applied to the glyph’s x_advance. The adjustment is applied in the line constructor

...
trim_weak_spacing(&mut items);

// Deal with glyph tracking at the end of the line. 
adjust_glyph_tracking_at_line_end(&mut items);

// Deal with CJ characters at line boundaries.
// Use the trimmed range for robust boundary checks.
adjust_cj_at_line_boundaries(p, trimmed_range, &mut items);
...

Generally, x_advance is not trustworthy because shrink, stretch and tracking calculations are absorbed by it at different points, so there is no guarantee on what it contains when read. Existing patches to start/end line behaviour:

  • e.g. adjust_cj_at_line_end
    work by subtracting an amount (the shrink applied earlier by add_cjk_latin_spacing) from the glyph’s x_advance. This bakes in the assumption that x_advance contains exactly that shrink, which must be respected by patches that come before it:
// If the first glyph is a CJK character adjusted by
// [`add_cjk_latin_spacing`], restore the original width.
let glyph = shaped.glyphs.to_mut().first_mut().unwrap();
let shrink = glyph.x_offset;
glyph.x_advance -= shrink;
glyph.x_offset = Em::zero();
glyph.adjustability.shrinkability.0 = Em::zero();

Apply adjust_glyph_tracking_at_line_end after adjust_cj_at_line_end then the change will cause end line CJK characters to overhang on the right by tracking.

A possible refactor could make explicit what x_advance contains by storing and updating shrink, stretch and tracking amounts separately, then compute x_advance when it’s needed. This would also aid a fix for the #8959 issue which confuses character width with width + tracking.

Fixes #8931.

@chinzhening
chinzhening force-pushed the remove-last-glyph-tracking branch from ce557f3 to 947f183 Compare October 16, 2026 11:11
@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. text Related to the text category, which is all about text handling, shaping, etc. labels Oct 19, 2026

@laurmaedje laurmaedje left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Ideally, I would like to have a cleaner solution, but I think that would require a larger refactor which should not block this fix. So I'm okay with proceeding here, pending a few comments:

  • Currently, the PR will unconditionally cause the glyphs to be cloned due to glyphs.to_mut. This should be avoided by checking whether a change will be made to the glyphs first (similarly to the CJK code paths).

  • I've made a PR to avoid a regression with CJK adjustability and fix some further tracking-related bugs, so this PR will require a rebase. In particular Em::from_abs(shaped.styles.resolve(..)) should become Em::from_length(shaped.styles.get(..)) to match the updated code in apply_tracking.

@laurmaedje laurmaedje added waiting-on-author Pull request waits on author and removed waiting-on-review This PR is waiting to be reviewed. labels Oct 20, 2026
@chinzhening
chinzhening force-pushed the remove-last-glyph-tracking branch from 947f183 to 9629ba0 Compare October 20, 2026 17:06
@chinzhening

Copy link
Copy Markdown
Contributor Author

Thanks for the review! I've resolved the function to check if a change is needed before cloning and amended the tracking calculation to be consistent with the updated code from your PR. I also made sure to reset tracking_applied after tracking is removed from x_advance for correctness.

@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels Oct 21, 2026
@laurmaedje
laurmaedje enabled auto-merge October 21, 2026 10:07
@laurmaedje

Copy link
Copy Markdown
Member

Thanks!

@laurmaedje laurmaedje removed the waiting-on-review This PR is waiting to be reviewed. label Oct 21, 2026
@chinzhening

chinzhening commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor Author

I think the minified test was not updated properly. Locally, the minified test generates a different .png to the one pushed.

@laurmaedje
laurmaedje force-pushed the remove-last-glyph-tracking branch from 75784d9 to 003f010 Compare October 21, 2026 11:02
@laurmaedje

Copy link
Copy Markdown
Member

Strange, I thought I looked at the image before pushing and considered it correct, even though it clearly wasn't. Should work now.

@laurmaedje
laurmaedje added this pull request to the merge queue Oct 21, 2026
Merged via the queue into typst:main with commit ddaaa2e Oct 21, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

text Related to the text category, which is all about text handling, shaping, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Text non-zero tracking with paragraph justify: true creates gutter on right side

2 participants