Skip to content

Fix punctuation overhang after labeled words - #7376

Closed
Ri-Nai wants to merge 4 commits into
typst:mainfrom
Ri-Nai:fix/overhang-skip-zero-advance-glyphs
Closed

Ri-Nai wants to merge 4 commits into
typst:mainfrom
Ri-Nai:fix/overhang-skip-zero-advance-glyphs

Conversation

@Ri-Nai

@Ri-Nai Ri-Nai commented Nov 13, 2025

Copy link
Copy Markdown
Contributor

related to #7219

  • skip zero-advance glyphs when finding the first/last glyph for line overhang
  • restore comma and dash overhang when a preceding word injects label-driven zero-width glyphs
  • keep existing behavior for lines that already start or end with regular glyphs

@laurmaedje laurmaedje added layout Related to the layout category, which is about composing, positioning, etc. text Related to the text category, which is all about text handling, shaping, etc. fix A bug fix. waiting-on-review This PR is waiting to be reviewed. labels Nov 17, 2025

@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. I'm not sure I understand exactly how you arrived at this condition.

Overall, doing something like this is probably fine, but it's more of a band-aid fix as there are more tag-related issue in inline layout. In the medium term a more robust solution for tag handling across all of inline layout will be necessary.

Comment thread crates/typst-layout/src/inline/line.rs Outdated
/// Access the first glyph with non-zero advance in the line.
pub fn leading_glyph(&self) -> Option<(&ShapedText<'a>, &ShapedGlyph)> {
for (_, item) in self.0.iter() {
let Item::Text(text) = &**item else { continue };

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.

This will skip over non-tag non-text items, which shouldn't be the case.

Comment thread crates/typst-layout/src/inline/line.rs Outdated
if let Some(glyph) = text
.glyphs
.iter()
.find(|glyph| glyph.x_advance.at(glyph.size) > Abs::zero())

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.

Why exactly zero-width glyphs. Could you elaborate on this condition?

@laurmaedje laurmaedje added waiting-on-author Pull request waits on author and removed waiting-on-review This PR is waiting to be reviewed. labels Nov 18, 2025
@Ri-Nai

Ri-Nai commented Nov 19, 2025

Copy link
Copy Markdown
Contributor Author

Thanks for looking! This change is scoped specifically to the regression where labels insert a zero-advance glyph between the word and the punctuation, causing the overhang logic to "miss" the comma.

To verify exactly what was happening in the renderer, I added some temporary debug printing to inspect the frame structure for the example.

#set par(justify: true)
Regular:
#block(stroke: 0.5pt + blue, width: 4cm)[This is a text with just, the right length.]
Label the word:
#block(stroke: 0.5pt + blue, width: 4cm)[This is a text with just,<label> the right length.]

Without a label, we get two clean text nodes:

==== baseline ====
  Text "This is a text with just, " (glyphs: 26)
  Text "the right length." (glyphs: 17)

With a <label> present, the frame contains the tag pair plus a standalone space which gets collapsed:

==== labelled ====
  Tag Start("text", …)
  Text "This is a text with just," (glyphs: 25)
  Tag End(…)
  Text " " (glyphs: 1)
  Text "the right length." (glyphs: 17)

That lone space glyph (the Text " " above) is kept for copy/paste support, but its advance is collapsed to zero (trimmed during collect_range / ShapedText::build). Because the tag mechanism splices it between the word and the comma, the old overhang logic stopped on that zero-width glyph and never saw the punctuation.

The new helper skips such glyphs to find the comma again. I agree that longer-term we need a broader tag story, but this unblocks the overhang regression without touching other inline layout code.


This will skip over non-tag non-text items, which shouldn't be the case.

I’ve tightened both helpers so they now break immediately at the first non-tag/non-text item. This matches the previous semantics while still skipping the zero-advance glyphs caused by tags. I've pushed the update.


Why exactly zero-width glyphs. Could you elaborate on this condition?

This is directly related to the frame structure I described in the general comment. The "zero-width glyphs" here are precisely those artifacts (like the collapsed space glyph in Text " ") that appear at the boundary due to the tag split.

Since ShapedText::build zeroes their advance (to keep them invisible but copyable), the old logic saw them as the "leading/trailing glyph" and returned a width of 0, effectively blocking the overhang check. The condition x_advance > 0 allows the iterator to skip these invisible artifacts and land on the actual punctuation (the comma) to correctly calculate its overhang.

@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels Nov 19, 2025
@Ri-Nai Ri-Nai closed this Feb 21, 2026
@Enivex

Enivex commented Feb 21, 2026

Copy link
Copy Markdown
Collaborator

Any particular reason why you closed your PR?

@Ri-Nai

Ri-Nai commented Feb 21, 2026

Copy link
Copy Markdown
Contributor Author

Any particular reason why you closed your PR?

It's been a few months with no activity, and I unfortunately no longer have the time to maintain or update this PR. I also felt the implementation might still need some work. I decided to close it for now, but anyone is welcome to continue where I left off."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix A bug fix. layout Related to the layout category, which is about composing, positioning, etc. text Related to the text category, which is all about text handling, shaping, etc. waiting-on-review This PR is waiting to be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants