From 99013ac3d56be14d6bb4f99e9053fee93cca7de4 Mon Sep 17 00:00:00 2001 From: LuK1337 Date: Sun, 11 Jul 2021 01:05:36 +0200 Subject: [PATCH] LibTTF: Port width calculation changes from BitmapFont --- Userland/Libraries/LibTTF/Font.cpp | 35 ++++++++++++++---------------- Userland/Libraries/LibTTF/Font.h | 3 +++ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Userland/Libraries/LibTTF/Font.cpp b/Userland/Libraries/LibTTF/Font.cpp index 9836998bae..030fa8899f 100644 --- a/Userland/Libraries/LibTTF/Font.cpp +++ b/Userland/Libraries/LibTTF/Font.cpp @@ -488,32 +488,29 @@ bool Font::is_fixed_width() const return glyph_metrics(glyph_id_for_code_point('.'), 1, 1).advance_width == glyph_metrics(glyph_id_for_code_point('X'), 1, 1).advance_width; } -int ScaledFont::width(const StringView& string) const -{ - Utf8View utf8 { string }; - return width(utf8); -} +int ScaledFont::width(StringView const& view) const { return unicode_view_width(Utf8View(view)); } +int ScaledFont::width(Utf8View const& view) const { return unicode_view_width(view); } +int ScaledFont::width(Utf32View const& view) const { return unicode_view_width(view); } -int ScaledFont::width(const Utf8View& utf8) const +template +ALWAYS_INLINE int ScaledFont::unicode_view_width(T const& view) const { + if (view.is_empty()) + return 0; int width = 0; - for (u32 code_point : utf8) { + int longest_width = 0; + for (auto code_point : view) { + if (code_point == '\n' || code_point == '\r') { + longest_width = max(width, longest_width); + width = 0; + continue; + } u32 glyph_id = glyph_id_for_code_point(code_point); auto metrics = glyph_metrics(glyph_id); width += metrics.advance_width; } - return width; -} - -int ScaledFont::width(const Utf32View& utf32) const -{ - int width = 0; - for (size_t i = 0; i < utf32.length(); i++) { - u32 glyph_id = glyph_id_for_code_point(utf32.code_points()[i]); - auto metrics = glyph_metrics(glyph_id); - width += metrics.advance_width; - } - return width; + longest_width = max(width, longest_width); + return longest_width; } RefPtr ScaledFont::raster_glyph(u32 glyph_id) const diff --git a/Userland/Libraries/LibTTF/Font.h b/Userland/Libraries/LibTTF/Font.h index f7fd922bef..6d94f9ce9a 100644 --- a/Userland/Libraries/LibTTF/Font.h +++ b/Userland/Libraries/LibTTF/Font.h @@ -151,6 +151,9 @@ private: float m_point_width { 0.0f }; float m_point_height { 0.0f }; mutable HashMap> m_cached_glyph_bitmaps; + + template + int unicode_view_width(T const& view) const; }; }