Skip to content

Commit

Permalink
fix cursor calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
gabm committed Dec 7, 2023
1 parent 644419d commit 09b4cc6
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/tools/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 09b4cc6

Please sign in to comment.