Sfoglia il codice sorgente

LibGfx: Add more query methods to FontDatabase and Typeface

Stephan Unverwerth 4 anni fa
parent
commit
5a70ccecb3

+ 20 - 4
Userland/Libraries/LibGfx/FontDatabase.cpp

@@ -159,10 +159,19 @@ RefPtr<Gfx::Font> FontDatabase::get_by_name(const StringView& name)
 
 RefPtr<Gfx::Font> FontDatabase::get(const String& family, unsigned size, unsigned weight)
 {
-    for (auto& it : m_private->full_name_to_font_map) {
-        auto& font = *it.value;
-        if (font.family() == family && font.presentation_size() == size && font.weight() == weight)
-            return font;
+    for (auto typeface : m_private->typefaces) {
+        if (typeface->family() == family && typeface->weight() == weight)
+            return typeface->get_font(size);
+    }
+    return nullptr;
+}
+
+RefPtr<Gfx::Font> FontDatabase::get(const String& family, const String& variant, unsigned size)
+{
+    dbgln("FontDatabase: Request font {} {} {}", family, variant, size);
+    for (auto typeface : m_private->typefaces) {
+        if (typeface->family() == family && typeface->variant() == variant)
+            return typeface->get_font(size);
     }
     return nullptr;
 }
@@ -178,4 +187,11 @@ RefPtr<Typeface> FontDatabase::get_or_create_typeface(const String& family, cons
     return typeface;
 }
 
+void FontDatabase::for_each_typeface(Function<void(const Typeface&)> callback)
+{
+    for (auto typeface : m_private->typefaces) {
+        callback(*typeface);
+    }
+}
+
 }

+ 4 - 1
Userland/Libraries/LibGfx/FontDatabase.h

@@ -45,16 +45,19 @@ public:
     static Font& default_bold_fixed_width_font();
 
     RefPtr<Gfx::Font> get(const String& family, unsigned size, unsigned weight);
+    RefPtr<Gfx::Font> get(const String& family, const String& variant, unsigned size);
     RefPtr<Gfx::Font> get_by_name(const StringView&);
     void for_each_font(Function<void(const Gfx::Font&)>);
     void for_each_fixed_width_font(Function<void(const Gfx::Font&)>);
 
-    RefPtr<Typeface> get_or_create_typeface(const String& family, const String& variant);
+    void for_each_typeface(Function<void(const Typeface&)>);
 
 private:
     FontDatabase();
     ~FontDatabase();
 
+    RefPtr<Typeface> get_or_create_typeface(const String& family, const String& variant);
+
     struct Private;
     OwnPtr<Private> m_private;
 };

+ 17 - 0
Userland/Libraries/LibGfx/Typeface.cpp

@@ -28,6 +28,16 @@
 
 namespace Gfx {
 
+unsigned Typeface::weight() const
+{
+    ASSERT(m_ttf_font || m_bitmap_fonts.size() > 0);
+
+    if (is_fixed_size())
+        return m_bitmap_fonts[0]->weight();
+
+    return m_ttf_font->weight();
+}
+
 void Typeface::add_bitmap_font(RefPtr<BitmapFont> font)
 {
     m_bitmap_fonts.append(font);
@@ -53,4 +63,11 @@ RefPtr<Font> Typeface::get_font(unsigned size)
     return {};
 }
 
+void Typeface::for_each_fixed_size_font(Function<void(const Font&)> callback) const
+{
+    for (auto font : m_bitmap_fonts) {
+        callback(*font);
+    }
+}
+
 }

+ 9 - 4
Userland/Libraries/LibGfx/Typeface.h

@@ -26,6 +26,7 @@
 
 #pragma once
 
+#include <AK/Function.h>
 #include <AK/RefCounted.h>
 #include <AK/String.h>
 #include <AK/Vector.h>
@@ -38,13 +39,17 @@ namespace Gfx {
 class Typeface : public RefCounted<Typeface> {
 public:
     Typeface(const String& family, const String& variant)
-    : m_family(family)
-    , m_variant(variant)
-    {}
+        : m_family(family)
+        , m_variant(variant)
+    {
+    }
 
     String family() const { return m_family; }
     String variant() const { return m_variant; }
-    unsigned weight() const { return 100; /*TODO*/ }
+    unsigned weight() const;
+
+    bool is_fixed_size() const { return !m_bitmap_fonts.is_empty(); }
+    void for_each_fixed_size_font(Function<void(const Font&)>) const;
 
     void add_bitmap_font(RefPtr<BitmapFont>);
     void set_ttf_font(RefPtr<TTF::Font>);