linebender / linebender/parley

Wrong selection rectangles for justified text

Open
#396 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
736
Forks
120
Avg merge
1d 3h
Merged PRs (30d)
53

Description

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:

```rust
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:

```rust
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.

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

Image

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue in the editor example using Alignment::Justify. Read Selection::geometry_with and align_impl in alignment.rs, then verify that selection rectangles cover the justified middle lines correctly. Done means the rectangles are no longer shorter than the rendered lines, while first and last line behavior remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
computer-graphics
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.