From 888b1b5f6b99f623fd881d2e347588f5cb532953 Mon Sep 17 00:00:00 2001 From: KENZ Date: Sun, 4 May 2025 11:21:43 +0900 Subject: [PATCH] Handle wide character widths by using glyph width to detect wide chars --- src/terminal_box.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/terminal_box.rs b/src/terminal_box.rs index ed7f935..26dee49 100644 --- a/src/terminal_box.rs +++ b/src/terminal_box.rs @@ -249,17 +249,25 @@ where // Draw cursor let cursor = terminal.term.lock().renderable_content().cursor; - let col = cursor.point.column.0; + let mut col = cursor.point.column.0; let line = cursor.point.line.0; let width = terminal.size().cell_width; - let height = terminal.size().cell_height; - let bottom_left = view_position - + Vector::new( - (col as f32 * width).floor(), - ((line + 1) as f32 * height).floor(), - ); + let mut bottom_left = Vector::::ZERO; + terminal.with_buffer(|buffer| { + let layout = buffer.layout_runs().nth(line as usize).unwrap(); + for glyph in layout.glyphs { + bottom_left.x += glyph.w; + let ch_width = if glyph.w > width { 2 } else { 1 }; + if ch_width > col { + break; + } + col -= ch_width; + } + bottom_left.y = layout.line_top + layout.line_height; + }); + InputMethod::Enabled { - cursor: Rectangle::new(bottom_left, Size::default()), + cursor: Rectangle::new(view_position + bottom_left, Size::default()), purpose: input_method::Purpose::Normal, preedit: state.preedit.as_ref().map(input_method::Preedit::as_ref), }