From 2fa33e5e8c50a7ca2545f797602be783bddeb515 Mon Sep 17 00:00:00 2001 From: James Magahern Date: Tue, 17 Nov 2026 18:23:56 -0800 Subject: [PATCH] Cursor height stays lined with text instead of line --- src/cursor.rs | 5 ++--- src/fonts.rs | 13 ++++++++++++- src/render.rs | 44 +++++++++++++++++++++++++++++--------------- src/terminal.rs | 6 +++--- 4 files changed, 46 insertions(+), 22 deletions(-) diff --git a/src/cursor.rs b/src/cursor.rs index bfeb4e5..4830e87 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -113,9 +113,8 @@ impl CursorAnimator { Self::default() } - /// Cursor bounds are in window coordinates and cover a full terminal cell - /// (two cells for a wide block cursor). Pass the desired fill alpha in color; - /// an overlay renderer can use a translucent color to leave text readable. + /// Cursor bounds cover the text height and the width of one or two columns + /// in window coordinates. The fill alpha controls how much text shows through. pub fn set_target( &mut self, bounds: Bounds, diff --git a/src/fonts.rs b/src/fonts.rs index cfb0621..ce7bfa0 100644 --- a/src/fonts.rs +++ b/src/fonts.rs @@ -23,23 +23,34 @@ pub struct TerminalFont { pub size: f32, pub cell_width: f32, pub line_height: f32, + pub text_height: f32, } impl TerminalFont { fn new(face: Font, size: f32, text_system: &TextSystem) -> Self { + let id = text_system.resolve_font(&face); let cell_width = text_system - .advance(text_system.resolve_font(&face), px(size), 'M') + .advance(id, px(size), 'M') .map(|advance| f32::from(advance.width)) .unwrap_or(size * 0.6) .max(1.); + // Match the shaped line's ascent and descent, excluding interline spacing. + // The native font backends can report descent as a signed distance. + let text_height = f32::from(text_system.ascent(id, px(size))) + + f32::from(text_system.descent(id, px(size))).abs(); Self { face, size, cell_width, line_height: (size * 1.8).round(), + text_height, } } + pub fn text_top(&self) -> f32 { + (self.line_height - self.text_height) / 2. + } + pub fn adjust_size(&mut self, delta: f32, text_system: &TextSystem) -> bool { let size = (self.size + delta).clamp(6., 72.); if size == self.size { diff --git a/src/render.rs b/src/render.rs index 8661c6b..6b9e53b 100644 --- a/src/render.rs +++ b/src/render.rs @@ -93,7 +93,7 @@ impl Renderer { if cached.cells == cells { continue; } - let (backgrounds, decorations, cursors) = assemble_quads(&cells, self.font.line_height); + let (backgrounds, decorations, cursors) = assemble_quads(&cells, &self.font); let text = assemble_text(&cells) .into_iter() .map(|segment| { @@ -303,8 +303,10 @@ impl Renderer { fn assemble_quads( cells: &[RenderCell], - line_height: f32, + font: &TerminalFont, ) -> (Vec, Vec, Vec) { + let top = font.text_top(); + let bottom = top + font.text_height; let mut backgrounds: Vec = Vec::new(); let mut decorations: Vec = Vec::new(); let mut cursors = Vec::new(); @@ -328,7 +330,7 @@ fn assemble_quads( column, columns, cell.foreground, - line_height - 2., + bottom - 2., ); } if flags.contains(Flags::STRIKEOUT) { @@ -337,13 +339,13 @@ fn assemble_quads( column, columns, cell.foreground, - line_height / 2., + top + font.text_height / 2., ); } } let (y, height, width) = match cell.cursor { - CursorShape::Beam => (0., line_height, Some(2.)), - CursorShape::Underline => (line_height - 2., 2., None), + CursorShape::Beam => (top, font.text_height, Some(2.)), + CursorShape::Underline => (bottom - 2., 2., None), _ => continue, }; cursors.push(Decoration { @@ -492,6 +494,16 @@ fn assemble_text(cells: &[RenderCell]) -> Vec { mod tests { use super::*; + fn test_font(line_height: f32) -> TerminalFont { + TerminalFont { + face: gpui::font("Test Mono"), + size: 15., + cell_width: 9., + line_height, + text_height: 18., + } + } + fn cell(c: char) -> RenderCell { RenderCell { cell: Cell { @@ -508,7 +520,7 @@ mod tests { fn blank_screen_has_no_text_or_background_work() { let cells = vec![cell(' '); 250]; assert!(assemble_text(&cells).is_empty()); - let (backgrounds, decorations, cursors) = assemble_quads(&cells, 20.); + let (backgrounds, decorations, cursors) = assemble_quads(&cells, &test_font(20.)); assert!(backgrounds.is_empty() && decorations.is_empty() && cursors.is_empty()); } @@ -563,7 +575,7 @@ mod tests { } #[test] - fn decorations_and_cursors_follow_the_terminal_line_height() { + fn cursors_and_decorations_stay_with_the_text_as_row_spacing_increases() { let mut cells = vec![cell('a'), cell('b')]; cells[0] .cell @@ -571,12 +583,14 @@ mod tests { .insert(Flags::UNDERLINE | Flags::STRIKEOUT); cells[0].cursor = CursorShape::Underline; cells[1].cursor = CursorShape::Beam; - for height in [8., 20., 96.] { - let (_, decorations, cursors) = assemble_quads(&cells, height); - assert_eq!(decorations[0].y, height - 2.); - assert_eq!(decorations[1].y, height / 2.); - assert_eq!(cursors[0].y + cursors[0].height, height); - assert_eq!(cursors[1].height, height); + for (row_height, top) in [(18., 0.), (27., 4.5), (40., 11.)] { + let font = test_font(row_height); + let (_, decorations, cursors) = assemble_quads(&cells, &font); + let bottom = top + font.text_height; + assert_eq!(decorations[0].y, bottom - 2.); + assert_eq!(decorations[1].y, row_height / 2.); + assert_eq!((cursors[0].y, cursors[0].height), (bottom - 2., 2.)); + assert_eq!((cursors[1].y, cursors[1].height), (top, font.text_height)); } } @@ -587,7 +601,7 @@ mod tests { c.background = rgb(0x123456).into(); c.cell.flags.insert(Flags::UNDERLINE | Flags::STRIKEOUT); } - let (backgrounds, decorations, _) = assemble_quads(&cells, 20.); + let (backgrounds, decorations, _) = assemble_quads(&cells, &test_font(20.)); assert_eq!(backgrounds.len(), 1); assert_eq!((backgrounds[0].column, backgrounds[0].columns), (2, 5)); assert_eq!(decorations.len(), 2); diff --git a/src/terminal.rs b/src/terminal.rs index 44376ca..87e6259 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -1154,11 +1154,11 @@ impl TerminalPane { bounds.origin + point( px(PADDING + f32::from(cursor.column) * font.cell_width), - px(PADDING + cursor.row as f32 * font.line_height), + px(PADDING + cursor.row as f32 * font.line_height + font.text_top()), ), size( px(font.cell_width * if wide { 2. } else { 1. }), - px(font.line_height), + px(font.text_height), ), ); let mut cursor_color = colors::resolve(Color::Named(NamedColor::Cursor), &palette); @@ -1281,7 +1281,7 @@ impl Render for TerminalPane { pane.preedit.clone().into(), px(font.size), &[run], None, ); let origin = pane.cursor_bounds.origin; - let height = px(font.line_height); + let height = pane.cursor_bounds.size.height; let _ = line.paint_background(origin, height, window, cx); let _ = line.paint(origin, height, window, cx); }