kas-gui / kas

Another GUI toolkit
Apache License 2.0
902 stars 25 forks source link

Text selection highlighting bugs #232

Open Aloso opened 3 years ago

Aloso commented 3 years ago

Text selection isn't rendered correctly when text is justified:

kas selection

Or when it contains tab stops:

kas-selection-2

dhardy commented 3 years ago

That's a known issue, except the part which crosses the screen on the left. It's to do with highlight_runs.

As for tab stops, those are zero-width glyphs, thus there's nothing to highlight. Some workaround is needed.

There's a similar problem with glyphs from a different script like Arabic; the selection rect height is wrong since the glyph height doesn't match the primary font's line height.

nyanpasu64 commented 3 years ago

I was playing around with right-to-left selection, and got the text and selection colors to desync. Is this a known bug?

Screenshot_20210929_002236_Widget Gallery

dhardy commented 3 years ago

I'd guess it's the same bug (or at least, bugs in the same rectangle generation logic).

By the way, I don't consider fixing this a priority since most of the time it's usable enough, and fixing it properly will require more substantial changes to kas-text: (1) tab stops and the extra gaps between words used for justified text are not actual characters, (2) glyphs from different fonts may have different line heights at the same size, (3) bidirectional text support implies that the lexical start/end of a line isn't always at a (geometric) end of the line.

That said, fixing at least some parts of this issue probably isn't so hard (e.g. merging selection rectangles where there is no other items between them).

(In retrospect, I'm not sure whether supporting bidirectional text already was worth it: more bugs/complexity and I guess most early users don't care much for it. But it's there now.)

dhardy commented 3 years ago

To illustrate a bit more, this selection rectangle is correct due to bidirection text not always being contiguous: selection-1 This, however, is not (only the white glyphs within the rectangle are selected): selection-2

Notice the differing block height? This is because text sizing uses the font's "Em size" (width of M in font units, which are convertible to points, which are convertible to pixels), not line-height, thus the latter may not match. The base font's line height is used for the space and full-stop while another font is used for the Arabic glyphs: selection-3

The second example here is probably not hard to fix, while the solution in the latter case is less obvious (the simplest would be to size fonts based on the line height instead of M size, I guess).

dhardy commented 2 years ago

https://github.com/kas-gui/kas-text/pull/67 fixed an issue apparently not mentioned here: the vertical offset of highlights when text is vertically aligned.

Other issues may or may not be fixed. The gaps in first/last line of justified text is definitely not. The highlight box escaping far to the left hopefully is.