From ba62679a7af41d032e90a0e9e057a69e25ff104a Mon Sep 17 00:00:00 2001 From: Callum Law Date: Sun, 17 Aug 2025 01:03:44 +1200 Subject: [PATCH] LibWeb: Make text cursor same height as text Previously we would paint the cursor the entire height of the text fragment - this didn't look great with large line-heights. Now we only paint it the height of the actual text, with the top of the cursor aligning with the font "ascent" and the bottom the "descent". --- Libraries/LibWeb/Painting/PaintableBox.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Libraries/LibWeb/Painting/PaintableBox.cpp b/Libraries/LibWeb/Painting/PaintableBox.cpp index 82d28606485..eec4d0ea267 100644 --- a/Libraries/LibWeb/Painting/PaintableBox.cpp +++ b/Libraries/LibWeb/Painting/PaintableBox.cpp @@ -723,11 +723,15 @@ void paint_cursor_if_needed(DisplayListRecordingContext& context, TextPaintable auto const& font = fragment.glyph_run() ? fragment.glyph_run()->font() : fragment.layout_node().first_available_font(); auto cursor_offset = font.width(text.substring_view(0, cursor_position->offset() - fragment.start_offset())); + auto font_metrics = font.pixel_metrics(); + + auto cursor_height = font_metrics.ascent + font_metrics.descent; + CSSPixelRect cursor_rect { fragment_rect.x() + CSSPixels::nearest_value_for(cursor_offset), - fragment_rect.top(), + fragment_rect.top() + fragment.baseline() - CSSPixels::nearest_value_for(font_metrics.ascent), 1, - fragment_rect.height() + CSSPixels::nearest_value_for(cursor_height) }; auto cursor_device_rect = context.rounded_device_rect(cursor_rect).to_type();