Ver código fonte

LibTTF: Port width calculation changes from BitmapFont

LuK1337 4 anos atrás
pai
commit
99013ac3d5
2 arquivos alterados com 19 adições e 19 exclusões
  1. 16 19
      Userland/Libraries/LibTTF/Font.cpp
  2. 3 0
      Userland/Libraries/LibTTF/Font.h

+ 16 - 19
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<typename T>
+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<Gfx::Bitmap> ScaledFont::raster_glyph(u32 glyph_id) const

+ 3 - 0
Userland/Libraries/LibTTF/Font.h

@@ -151,6 +151,9 @@ private:
     float m_point_width { 0.0f };
     float m_point_height { 0.0f };
     mutable HashMap<u32, RefPtr<Gfx::Bitmap>> m_cached_glyph_bitmaps;
+
+    template<typename T>
+    int unicode_view_width(T const& view) const;
 };
 
 }