LibGfx: Add more query methods to FontDatabase and Typeface

This commit is contained in:
Stephan Unverwerth 2021-01-02 18:20:10 +01:00 committed by Andreas Kling
parent e504d4ef96
commit 5a70ccecb3
Notes: sideshowbarker 2024-07-18 22:18:04 +09:00
4 changed files with 50 additions and 9 deletions

View File

@ -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) RefPtr<Gfx::Font> FontDatabase::get(const String& family, unsigned size, unsigned weight)
{ {
for (auto& it : m_private->full_name_to_font_map) { for (auto typeface : m_private->typefaces) {
auto& font = *it.value; if (typeface->family() == family && typeface->weight() == weight)
if (font.family() == family && font.presentation_size() == size && font.weight() == weight) return typeface->get_font(size);
return font; }
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; return nullptr;
} }
@ -178,4 +187,11 @@ RefPtr<Typeface> FontDatabase::get_or_create_typeface(const String& family, cons
return typeface; return typeface;
} }
void FontDatabase::for_each_typeface(Function<void(const Typeface&)> callback)
{
for (auto typeface : m_private->typefaces) {
callback(*typeface);
}
}
} }

View File

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

View File

@ -28,6 +28,16 @@
namespace Gfx { 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) void Typeface::add_bitmap_font(RefPtr<BitmapFont> font)
{ {
m_bitmap_fonts.append(font); m_bitmap_fonts.append(font);
@ -53,4 +63,11 @@ RefPtr<Font> Typeface::get_font(unsigned size)
return {}; return {};
} }
void Typeface::for_each_fixed_size_font(Function<void(const Font&)> callback) const
{
for (auto font : m_bitmap_fonts) {
callback(*font);
}
}
} }

View File

@ -26,6 +26,7 @@
#pragma once #pragma once
#include <AK/Function.h>
#include <AK/RefCounted.h> #include <AK/RefCounted.h>
#include <AK/String.h> #include <AK/String.h>
#include <AK/Vector.h> #include <AK/Vector.h>
@ -38,13 +39,17 @@ namespace Gfx {
class Typeface : public RefCounted<Typeface> { class Typeface : public RefCounted<Typeface> {
public: public:
Typeface(const String& family, const String& variant) Typeface(const String& family, const String& variant)
: m_family(family) : m_family(family)
, m_variant(variant) , m_variant(variant)
{} {
}
String family() const { return m_family; } String family() const { return m_family; }
String variant() const { return m_variant; } 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 add_bitmap_font(RefPtr<BitmapFont>);
void set_ttf_font(RefPtr<TTF::Font>); void set_ttf_font(RefPtr<TTF::Font>);