LibGfx+LibWeb: Remove Gfx::Typeface

...and use VectorFont directly in FontDatabase.
This commit is contained in:
Aliaksandr Kalenik 2024-06-28 20:13:13 +02:00 committed by Andreas Kling
parent bdaa7f0e8e
commit e2726ce8e4
Notes: sideshowbarker 2024-07-16 23:55:09 +09:00
6 changed files with 24 additions and 127 deletions

View File

@ -20,7 +20,6 @@ set(SOURCES
Font/OpenType/Hinting/Opcodes.cpp
Font/OpenType/Tables.cpp
Font/ScaledFont.cpp
Font/Typeface.cpp
Font/VectorFont.cpp
Font/WOFF/Font.cpp
Font/WOFF2/Font.cpp

View File

@ -12,6 +12,7 @@
#include <LibGfx/Font/Font.h>
#include <LibGfx/Font/FontDatabase.h>
#include <LibGfx/Font/OpenType/Font.h>
#include <LibGfx/Font/ScaledFont.h>
#include <LibGfx/Font/WOFF/Font.h>
namespace Gfx {
@ -23,8 +24,7 @@ FontDatabase& FontDatabase::the()
}
struct FontDatabase::Private {
HashMap<ByteString, NonnullRefPtr<Gfx::Font>, CaseInsensitiveStringTraits> full_name_to_font_map;
HashMap<FlyString, Vector<NonnullRefPtr<Typeface>>, AK::ASCIICaseInsensitiveFlyStringTraits> typefaces;
HashMap<FlyString, Vector<NonnullRefPtr<VectorFont>>, AK::ASCIICaseInsensitiveFlyStringTraits> typeface_by_family;
};
void FontDatabase::load_all_fonts_from_uri(StringView uri)
@ -46,14 +46,18 @@ void FontDatabase::load_all_fonts_from_uri(StringView uri)
// FIXME: What about .otf
if (auto font_or_error = OpenType::Font::try_load_from_resource(resource); !font_or_error.is_error()) {
auto font = font_or_error.release_value();
auto typeface = get_or_create_typeface(font->family(), font->variant());
typeface->set_vector_font(move(font));
auto& family = m_private->typeface_by_family.ensure(font->family(), [] {
return Vector<NonnullRefPtr<VectorFont>> {};
});
family.append(font);
}
} else if (path.has_extension(".woff"sv)) {
if (auto font_or_error = WOFF::Font::try_load_from_resource(resource); !font_or_error.is_error()) {
auto font = font_or_error.release_value();
auto typeface = get_or_create_typeface(font->family(), font->variant());
typeface->set_vector_font(move(font));
auto& family = m_private->typeface_by_family.ensure(font->family(), [] {
return Vector<NonnullRefPtr<VectorFont>> {};
});
family.append(font);
}
}
return IterationDecision::Continue;
@ -68,46 +72,32 @@ FontDatabase::FontDatabase()
RefPtr<Gfx::Font> FontDatabase::get(FlyString const& family, float point_size, unsigned weight, unsigned width, unsigned slope)
{
auto it = m_private->typefaces.find(family);
if (it == m_private->typefaces.end())
auto it = m_private->typeface_by_family.find(family);
if (it == m_private->typeface_by_family.end())
return nullptr;
for (auto const& typeface : it->value) {
if (typeface->weight() == weight && typeface->width() == width && typeface->slope() == slope)
return typeface->get_font(point_size);
return typeface->scaled_font(point_size);
}
return nullptr;
}
RefPtr<Gfx::Font> FontDatabase::get(FlyString const& family, FlyString const& variant, float point_size)
{
auto it = m_private->typefaces.find(family);
if (it == m_private->typefaces.end())
auto it = m_private->typeface_by_family.find(family);
if (it == m_private->typeface_by_family.end())
return nullptr;
for (auto const& typeface : it->value) {
if (typeface->variant() == variant)
return typeface->get_font(point_size);
return typeface->scaled_font(point_size);
}
return nullptr;
}
RefPtr<Typeface> FontDatabase::get_or_create_typeface(FlyString const& family, FlyString const& variant)
void FontDatabase::for_each_typeface_with_family_name(FlyString const& family_name, Function<void(VectorFont const&)> callback)
{
auto it = m_private->typefaces.find(family);
if (it != m_private->typefaces.end()) {
for (auto const& typeface : it->value) {
if (typeface->variant() == variant)
return typeface;
}
}
auto typeface = adopt_ref(*new Typeface(family, variant));
m_private->typefaces.ensure(family).append(typeface);
return typeface;
}
void FontDatabase::for_each_typeface_with_family_name(FlyString const& family_name, Function<void(Typeface const&)> callback)
{
auto it = m_private->typefaces.find(family_name);
if (it == m_private->typefaces.end())
auto it = m_private->typeface_by_family.find(family_name);
if (it == m_private->typeface_by_family.end())
return;
for (auto const& typeface : it->value)
callback(*typeface);

View File

@ -12,7 +12,7 @@
#include <AK/HashMap.h>
#include <AK/OwnPtr.h>
#include <LibGfx/Font/FontWeight.h>
#include <LibGfx/Font/Typeface.h>
#include <LibGfx/Font/VectorFont.h>
#include <LibGfx/Forward.h>
namespace Gfx {
@ -24,7 +24,7 @@ public:
RefPtr<Gfx::Font> get(FlyString const& family, float point_size, unsigned weight, unsigned width, unsigned slope);
RefPtr<Gfx::Font> get(FlyString const& family, FlyString const& variant, float point_size);
void for_each_typeface_with_family_name(FlyString const& family_name, Function<void(Typeface const&)>);
void for_each_typeface_with_family_name(FlyString const& family_name, Function<void(VectorFont const&)>);
void load_all_fonts_from_uri(StringView);
@ -32,8 +32,6 @@ private:
FontDatabase();
~FontDatabase() = default;
RefPtr<Typeface> get_or_create_typeface(FlyString const& family, FlyString const& variant);
struct Private;
OwnPtr<Private> m_private;
};

View File

@ -1,43 +0,0 @@
/*
* Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGfx/Font/ScaledFont.h>
#include <LibGfx/Font/Typeface.h>
namespace Gfx {
unsigned Typeface::weight() const
{
return m_vector_font->weight();
}
unsigned Typeface::width() const
{
return m_vector_font->width();
}
u8 Typeface::slope() const
{
return m_vector_font->slope();
}
bool Typeface::is_fixed_width() const
{
return m_vector_font->is_fixed_width();
}
void Typeface::set_vector_font(RefPtr<VectorFont> font)
{
m_vector_font = move(font);
}
RefPtr<Font> Typeface::get_font(float point_size) const
{
VERIFY(point_size >= 0);
return m_vector_font->scaled_font(point_size);
}
}

View File

@ -1,46 +0,0 @@
/*
* Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/DeprecatedFlyString.h>
#include <AK/FlyString.h>
#include <AK/Function.h>
#include <AK/RefCounted.h>
#include <AK/Vector.h>
#include <LibGfx/Font/Font.h>
#include <LibGfx/Font/VectorFont.h>
namespace Gfx {
class Typeface : public RefCounted<Typeface> {
public:
Typeface(FlyString family, FlyString variant)
: m_family(move(family))
, m_variant(move(variant))
{
}
FlyString const& family() const { return m_family; }
FlyString const& variant() const { return m_variant; }
unsigned weight() const;
unsigned width() const;
u8 slope() const;
bool is_fixed_width() const;
void set_vector_font(RefPtr<VectorFont>);
RefPtr<Font> get_font(float point_size) const;
private:
FlyString m_family;
FlyString m_variant;
RefPtr<VectorFont> m_vector_font;
};
}

View File

@ -197,7 +197,7 @@ ErrorOr<NonnullRefPtr<Gfx::VectorFont>> FontLoader::try_load_font()
struct StyleComputer::MatchingFontCandidate {
FontFaceKey key;
Variant<FontLoaderList*, Gfx::Typeface const*> loader_or_typeface;
Variant<FontLoaderList*, Gfx::VectorFont const*> loader_or_typeface;
[[nodiscard]] RefPtr<Gfx::FontCascadeList const> font_with_point_size(float point_size) const
{
@ -210,8 +210,7 @@ struct StyleComputer::MatchingFontCandidate {
return font_list;
}
if (auto font = loader_or_typeface.get<Gfx::Typeface const*>()->get_font(point_size))
font_list->add(*font);
font_list->add(loader_or_typeface.get<Gfx::VectorFont const*>()->scaled_font(point_size));
return font_list;
}
};
@ -1854,7 +1853,7 @@ RefPtr<Gfx::FontCascadeList const> StyleComputer::font_matching_algorithm(FontFa
if (font_key_and_loader.key.family_name.equals_ignoring_ascii_case(key.family_name))
matching_family_fonts.empend(font_key_and_loader.key, const_cast<FontLoaderList*>(&font_key_and_loader.value));
}
Gfx::FontDatabase::the().for_each_typeface_with_family_name(key.family_name, [&](Gfx::Typeface const& typeface) {
Gfx::FontDatabase::the().for_each_typeface_with_family_name(key.family_name, [&](Gfx::VectorFont const& typeface) {
matching_family_fonts.empend(
FontFaceKey {
.family_name = typeface.family(),