Skip to content

Wrong selection rectangles for justified text #396

@kekelp

Description

@kekelp

If I set the editor example to use Alignment::Justify, the selection rectangles are shorter than they should, for all lines except the first and the last:

Image

I found a fix following this thought process: in Selection::geometry_with, the non-first and non-last lines go through a different path:

if line_ix == line_start_ix || line_ix == line_end_ix {
                // We only need to run the expensive logic on the first and
                // last lines
                // ...
            } else {
                let x = metrics.offset as f64;
                let width = metrics.advance as f64;
                f(
                    Rect::new(x, line_min, x + width + newline_whitespace, line_max),
                    line_ix,
                );
            }

The logic makes sense, so metrics.advance is probably not right for justified text.

In align_impl in alignment.rs, the line metrics are modified based on the requested alignment. For Justify, an adjustment is calculated and added to the advance of the clusters:

                            if cluster.info.whitespace().is_space_or_nbsp() {
                                cluster.advance += adjustment;
                                applied += 1;
                            }

Adding this adjustment to the advance of the whole line as well seems to fix the issue.

                            if cluster.info.whitespace().is_space_or_nbsp() {
                                cluster.advance += adjustment;
                                line.metrics.advance += adjustment;
                                applied += 1;
                            }
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions