Fix rendering of text selection with combining characters with the GL backend

When the text selection end follows right after a grapheme that uses less
glyphs than characters, then there may not be a matching glyph with the byte
index, therefore we wouldn't set the selection_end_x and draw incorrectly.
Take the visual tail of the last glyph then.
This commit is contained in:
Simon Hausmann 2021-07-30 15:55:08 +02:00 committed by Simon Hausmann
parent 17b3fbc7cf
commit d867d7b58a

View file

@ -743,13 +743,19 @@ impl ItemRenderer for GLItemRenderer {
let (anchor_pos, cursor_pos) = text_input.selection_anchor_and_cursor(); let (anchor_pos, cursor_pos) = text_input.selection_anchor_and_cursor();
let mut selection_start_x = 0.; let mut selection_start_x = 0.;
let mut selection_end_x = 0.; let mut selection_end_x = 0.;
// Determine the first and last (inclusive) glyph of the selection. The anchor
// will always be at the start of a grapheme boundary, so there's at ShapedGlyph
// that has a matching byte index. For the selection end we have to look for the
// visual end of glyph before the cursor, because due to for example ligatures
// (or generally glyph substitution) there may not be a dedicated glyph.
for glyph in &metrics.glyphs { for glyph in &metrics.glyphs {
if glyph.byte_index == anchor_pos { if glyph.byte_index == anchor_pos {
selection_start_x = glyph.x; selection_start_x = glyph.x;
} }
if glyph.byte_index == (cursor_pos as i32 - 1).max(0) as usize { if glyph.byte_index == cursor_pos {
selection_end_x = glyph.x + glyph.advance_x; break;
} }
selection_end_x = glyph.x + glyph.advance_x;
} }
let selection_rect = Rect::new( let selection_rect = Rect::new(