Skip to content

Conversation

kekelp
Copy link
Contributor

@kekelp kekelp commented Aug 4, 2025

Resolves #396.

Accept suggestions

Co-authored-by: Daniel McNab <[email protected]>
Copy link
Member

@tomcur tomcur left a comment

Choose a reason for hiding this comment

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

I think the direction makes sense, as the current state of LineMetrics::advance not taking padding into account certainly can be confusing. I've placed a few comments/questions inline.

Comment on lines +111 to +112
/// Max advance for the line before justification.
pub(crate) unjustified_advance: f32,
Copy link
Member

Choose a reason for hiding this comment

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

It's probably fine to store the unjustified advance here, as the user likely doesn't need to know.

}
if cluster.info.whitespace().is_space_or_nbsp() {
cluster.advance += adjustment;
line.metrics.advance += adjustment;
Copy link
Member

Choose a reason for hiding this comment

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

Could this be set (outside the loop) as line.metrics.advance = layout.alignment_width, for all except the final line? For very wide lines with many clusters, this iterative calculation will probably become quite lossy.

Does the code in the PR currently lead to a line.metrics.advance larger than the alignment_width, as trailing whitespace is counted twice? (I haven't run this locally yet.)

Copy link
Contributor Author

@kekelp kekelp Aug 18, 2025

Choose a reason for hiding this comment

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

Setting line.metrics.advance = layout.alignment_width, this is what happens:

image

The 2nd and 3rd lines use line.metrics.advance for the width of their selection box, so now it stops exactly at the end of the alignment_width. The first line calculates the selection box manually without using line.metrics.advance, and it shows that the trailing whitespace lives beyond layout.alignment_width, because the justification code ignores it.

We could just change the selection box code, of course, but the thing is that line.metrics.advance is documented as "Full advance of the line, including trailing whitespace". So I think it's correct that line.metrics.advance is larger than the alignment_width in this case.

Still, we can probably still avoid the iterative calculation by just setting line.metrics.advance = layout.alignment_width + line.metrics.trailing_whitespace. I'm not sure about the RTL line and the one with a trailing newline instead of a trailing space, but this seems to work as well:

image

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.

Wrong selection rectangles for justified text

3 participants