From 8d0b0fbdd380571fb16405c465a85336d7580864 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 22 Feb 2023 12:08:44 -0500 Subject: [PATCH] LibWeb: Make text fragment indexing handle multi-code point glyphs --- Userland/Libraries/LibWeb/Layout/LineBoxFragment.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibWeb/Layout/LineBoxFragment.cpp b/Userland/Libraries/LibWeb/Layout/LineBoxFragment.cpp index 584e415d3a..481473c894 100644 --- a/Userland/Libraries/LibWeb/Layout/LineBoxFragment.cpp +++ b/Userland/Libraries/LibWeb/Layout/LineBoxFragment.cpp @@ -58,11 +58,15 @@ int LineBoxFragment::text_index_at(CSSPixels x) const CSSPixels width_so_far = 0; for (auto it = view.begin(); it != view.end(); ++it) { - CSSPixels glyph_width = font.glyph_or_emoji_width(*it); + auto previous_it = it; + CSSPixels glyph_width = font.glyph_or_emoji_width(it); + if ((width_so_far + (glyph_width + glyph_spacing) / 2) > relative_x) - return m_start + view.byte_offset_of(it); + return m_start + view.byte_offset_of(previous_it); + width_so_far += glyph_width + glyph_spacing; } + return m_start + m_length; }