From 09b4cc6530f77f667a5c05820df075c82ddc633b Mon Sep 17 00:00:00 2001 From: Matthias Gabriel Date: Thu, 7 Dec 2023 10:52:45 +0100 Subject: [PATCH] fix cursor calculation --- src/tools/text.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/tools/text.rs b/src/tools/text.rs index f569feb..c7fea32 100644 --- a/src/tools/text.rs +++ b/src/tools/text.rs @@ -50,7 +50,14 @@ impl Drawable for Text { pangocairo::show_layout(cx, &layout); if self.editing { - let (cursor, _) = layout.cursor_pos(self.text_buffer.cursor_position()); + // GTK is working with UTF-8 and character positions, pango is working with UTF-8 but byte positions. + // here we transform one into the other! + let (cursor_byte_pos, _) = text + .char_indices() + .nth((self.text_buffer.cursor_position()) as usize) + .unwrap_or((text.bytes().count(), 'X')); + + let (cursor, _) = layout.cursor_pos(cursor_byte_pos as i32); let cursor_pos = self.pos + Vec2D::new((cursor.x() / SCALE) as f64, (cursor.y() / SCALE) as f64);