Quellcode durchsuchen

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.
Aliaksandr Kalenik vor 10 Monaten
Ursprung
Commit
4786604f8b

+ 3 - 28
Userland/Libraries/LibGfx/Font/ScaledFont.cpp

@@ -7,6 +7,7 @@
 #include <AK/Utf8View.h>
 #include <LibGfx/Font/Emoji.h>
 #include <LibGfx/Font/ScaledFont.h>
+#include <LibGfx/TextLayout.h>
 
 namespace Gfx {
 
@@ -34,34 +35,8 @@ ScaledFont::ScaledFont(NonnullRefPtr<Typeface> 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<typename T>
-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
 {

+ 0 - 3
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<typename T>
-    float unicode_view_width(T const& view) const;
 };
 
 }

+ 7 - 0
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;
+}
+
 }

+ 1 - 0
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<void(DrawGlyphOrEmoji const&)> callback, Optional<float&> width = {});
+float measure_text_width(Utf8View const& string, Gfx::Font const& font);
 
 }