From 4786604f8bb7cdeb72f69c392d3bd76a3ea8c286 Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Tue, 3 Sep 2024 21:12:29 +0200 Subject: [PATCH] LibGfx: Replace ad-hoc text width calculation with harfbuzz Since harfbuzz is already used to calculate glyph positions, let's also use it to measure width. --- Userland/Libraries/LibGfx/Font/ScaledFont.cpp | 31 ++----------------- Userland/Libraries/LibGfx/Font/ScaledFont.h | 3 -- Userland/Libraries/LibGfx/TextLayout.cpp | 7 +++++ Userland/Libraries/LibGfx/TextLayout.h | 1 + 4 files changed, 11 insertions(+), 31 deletions(-) diff --git a/Userland/Libraries/LibGfx/Font/ScaledFont.cpp b/Userland/Libraries/LibGfx/Font/ScaledFont.cpp index 9971c84595a..2d9f2aa3e9c 100644 --- a/Userland/Libraries/LibGfx/Font/ScaledFont.cpp +++ b/Userland/Libraries/LibGfx/Font/ScaledFont.cpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace Gfx { @@ -34,34 +35,8 @@ ScaledFont::ScaledFont(NonnullRefPtr typeface, float point_width, floa }; } -float ScaledFont::width(StringView view) const { return unicode_view_width(Utf8View(view)); } -float ScaledFont::width(Utf8View const& view) const { return unicode_view_width(view); } - -template -ALWAYS_INLINE float ScaledFont::unicode_view_width(T const& view) const -{ - if (view.is_empty()) - return 0; - float width = 0; - float longest_width = 0; - u32 last_code_point = 0; - - for (auto it = view.begin(); it != view.end(); last_code_point = *it, ++it) { - auto code_point = *it; - - if (code_point == '\n' || code_point == '\r') { - longest_width = max(width, longest_width); - width = 0; - continue; - } - - auto kerning = glyphs_horizontal_kerning(last_code_point, code_point); - width += kerning + glyph_or_emoji_width(it); - } - - longest_width = max(width, longest_width); - return longest_width; -} +float ScaledFont::width(StringView view) const { return measure_text_width(Utf8View(view), *this); } +float ScaledFont::width(Utf8View const& view) const { return measure_text_width(view, *this); } float ScaledFont::glyph_width(u32 code_point) const { diff --git a/Userland/Libraries/LibGfx/Font/ScaledFont.h b/Userland/Libraries/LibGfx/Font/ScaledFont.h index 7ad91010b3f..1acafdd0654 100644 --- a/Userland/Libraries/LibGfx/Font/ScaledFont.h +++ b/Userland/Libraries/LibGfx/Font/ScaledFont.h @@ -59,9 +59,6 @@ private: float m_pixel_size { 0.0f }; int m_pixel_size_rounded_up { 0 }; - - template - float unicode_view_width(T const& view) const; }; } diff --git a/Userland/Libraries/LibGfx/TextLayout.cpp b/Userland/Libraries/LibGfx/TextLayout.cpp index c7a78dc703b..8f4fba48ccd 100644 --- a/Userland/Libraries/LibGfx/TextLayout.cpp +++ b/Userland/Libraries/LibGfx/TextLayout.cpp @@ -54,4 +54,11 @@ void for_each_glyph_position(FloatPoint baseline_start, Utf8View string, Gfx::Fo *width = point.x(); } +float measure_text_width(Utf8View const& string, Gfx::Font const& font) +{ + float width = 0; + for_each_glyph_position({}, string, font, [&](DrawGlyphOrEmoji const&) {}, width); + return width; +} + } diff --git a/Userland/Libraries/LibGfx/TextLayout.h b/Userland/Libraries/LibGfx/TextLayout.h index d1dffb198e7..4c2eb6a1611 100644 --- a/Userland/Libraries/LibGfx/TextLayout.h +++ b/Userland/Libraries/LibGfx/TextLayout.h @@ -74,5 +74,6 @@ private: }; void for_each_glyph_position(FloatPoint baseline_start, Utf8View string, Gfx::Font const& font, Function callback, Optional width = {}); +float measure_text_width(Utf8View const& string, Gfx::Font const& font); }