Support cursor inside ligature

This commit is contained in:
Jeremy Soller 2022-10-24 12:02:50 -06:00
parent b4ae8ed01e
commit cb5bde83ed
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE

View file

@ -806,21 +806,37 @@ impl<'a> TextBuffer<'a> {
return; return;
} }
let cursor_glyph_opt = |cursor: &TextCursor| -> Option<usize> { let cursor_glyph_opt = |cursor: &TextCursor| -> Option<(usize, f32)> {
if cursor.line.get() == line_i { if cursor.line.get() == line_i {
for (glyph_i, glyph) in layout_line.glyphs.iter().enumerate() { for (glyph_i, glyph) in layout_line.glyphs.iter().enumerate() {
if cursor.index == glyph.start { if cursor.index == glyph.start {
return Some(glyph_i); return Some((glyph_i, 0.0));
} else if cursor.index > glyph.start && cursor.index < glyph.end {
// Guess x offset based on characters
//TODO: use EGCs?
let mut before = 0;
let mut total = 0;
let cluster = &line.text[glyph.start..glyph.end];
for (i, _) in cluster.char_indices() {
if glyph.start + i < cursor.index {
before += 1;
}
total += 1;
}
let offset = glyph.w * (before as f32) / (total as f32);
return Some((glyph_i, offset));
} }
} }
match layout_line.glyphs.last() { match layout_line.glyphs.last() {
Some(glyph) => { Some(glyph) => {
if cursor.index == glyph.end { if cursor.index == glyph.end {
return Some(layout_line.glyphs.len()); return Some((layout_line.glyphs.len(), 0.0));
} }
}, },
None => { None => {
return Some(0); return Some((0, 0.0));
} }
} }
} }
@ -877,35 +893,35 @@ impl<'a> TextBuffer<'a> {
} }
if inside { if inside {
let start_glyph = if start.line.get() == line_i { let (start_glyph, start_glyph_offset) = if start.line.get() == line_i {
cursor_glyph_opt(&start).unwrap_or(0) cursor_glyph_opt(&start).unwrap_or((0, 0.0))
} else { } else {
0 (0, 0.0)
}; };
let end_glyph = if end.line.get() == line_i { let (end_glyph, end_glyph_offset) = if end.line.get() == line_i {
cursor_glyph_opt(&end).unwrap_or(layout_line.glyphs.len() + 1) cursor_glyph_opt(&end).unwrap_or((layout_line.glyphs.len() + 1, 0.0))
} else { } else {
layout_line.glyphs.len() + 1 (layout_line.glyphs.len() + 1, 0.0)
}; };
if end_glyph > start_glyph { if end_glyph > start_glyph {
let (left_x, right_x) = if shape.rtl { let (left_x, right_x) = if shape.rtl {
( (
layout_line.glyphs.get(end_glyph - 1).map_or(0, |glyph| { layout_line.glyphs.get(end_glyph - 1).map_or(0, |glyph| {
glyph.x as i32 (glyph.x - end_glyph_offset) as i32
}), }),
layout_line.glyphs.get(start_glyph).map_or(self.width, |glyph| { layout_line.glyphs.get(start_glyph).map_or(self.width, |glyph| {
(glyph.x + glyph.w) as i32 (glyph.x + glyph.w - start_glyph_offset) as i32
}), }),
) )
} else { } else {
( (
layout_line.glyphs.get(start_glyph).map_or(0, |glyph| { layout_line.glyphs.get(start_glyph).map_or(0, |glyph| {
glyph.x as i32 (glyph.x + start_glyph_offset) as i32
}), }),
layout_line.glyphs.get(end_glyph - 1).map_or(self.width, |glyph| { layout_line.glyphs.get(end_glyph - 1).map_or(self.width, |glyph| {
(glyph.x + glyph.w) as i32 (glyph.x + glyph.w + end_glyph_offset) as i32
}), }),
) )
}; };
@ -923,14 +939,14 @@ impl<'a> TextBuffer<'a> {
// Draw cursor // Draw cursor
//TODO: draw at end of line but not start of next line //TODO: draw at end of line but not start of next line
if let Some(cursor_glyph) = cursor_glyph_opt(&self.cursor) { if let Some((cursor_glyph, cursor_glyph_offset)) = cursor_glyph_opt(&self.cursor) {
let x = match layout_line.glyphs.get(cursor_glyph) { let x = match layout_line.glyphs.get(cursor_glyph) {
Some(glyph) => { Some(glyph) => {
// Start of detected glyph // Start of detected glyph
if shape.rtl { if shape.rtl {
(glyph.x + glyph.w) as i32 (glyph.x + glyph.w - cursor_glyph_offset) as i32
} else { } else {
glyph.x as i32 (glyph.x + cursor_glyph_offset) as i32
} }
}, },
None => match layout_line.glyphs.last() { None => match layout_line.glyphs.last() {