Truly independent web browser
/*
* Copyright (c) 2018-2020, Andreas Kling <[email protected]>
* Copyright (c) 2026, Tim Ledbetter <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/FlyString.h>
#include <LibCore/StandardPaths.h>
#include <LibGfx/Font/Font.h>
#include <LibGfx/Font/FontDatabase.h>
#include <LibGfx/Font/SystemFallbackFonts.h>
#include <LibGfx/Font/SystemFontMatcher.h>
#if defined(AK_OS_HAIKU)
# include <FindDirectory.h>
#endif
#ifdef USE_FONTCONFIG
# include <LibGfx/Font/GlobalFontConfig.h>
#endif
namespace Gfx {
RefPtr<Typeface> SystemFontProvider::get_typeface_by_local_name(String const&)
{
return {};
}
RefPtr<Typeface> FontDatabase::get_typeface_by_local_name(String const& name)
{
return m_system_font_provider->get_typeface_by_local_name(name);
}
// Key function for SystemFontProvider to emit the vtable here
SystemFontProvider::~SystemFontProvider() = default;
RefPtr<Typeface> SystemFontProvider::get_typeface_by_id(u64, u64)
{
return nullptr;
}
RefPtr<Gfx::Font> SystemFontProvider::get_font_for_code_point(u32 code_point, float point_size, u16 weight, u16 width, u8 slope, bool prefer_color_emoji)
{
auto match = SystemFontMatcher::match_code_point(code_point, weight, width, slope, prefer_color_emoji);
if (!match.has_value())
return nullptr;
auto typeface = SystemFontMatcher::load(*match);
if (typeface.is_error())
return nullptr;
return typeface.value()->font(point_size, {});
}
Optional<FlyString> SystemFontProvider::resolve_generic_family(StringView family_name, u16 weight, u8 slope)
{
return SystemFontMatcher::resolve_generic_family(family_name, weight, slope);
}
FontDatabase& FontDatabase::the()
{
static FontDatabase& database = *new FontDatabase;
return database;
}
SystemFontProvider& FontDatabase::install_system_font_provider(NonnullOwnPtr<SystemFontProvider> provider)
{
VERIFY(!m_system_font_provider);
clear_system_fallback_font_cache();
m_system_font_provider = move(provider);
return *m_system_font_provider;
}
FontDatabase::FontDatabase() = default;
RefPtr<Gfx::Font> FontDatabase::get(FlyString const& family, float point_size, unsigned weight, unsigned width, unsigned slope, Optional<FontVariationSettings> const& font_variation_settings, Optional<Gfx::ShapeFeatures> const& shape_features)
{
return m_system_font_provider->get_font(family, point_size, weight, width, slope, font_variation_settings, shape_features);
}
RefPtr<Gfx::Font> FontDatabase::get_font_for_code_point(u32 code_point, float point_size, u16 weight, u16 width, u8 slope, bool prefer_color_emoji)
{
return m_system_font_provider->get_font_for_code_point(code_point, point_size, weight, width, slope, prefer_color_emoji);
}
RefPtr<Typeface> FontDatabase::get_typeface_by_id(u64 generation, u64 face_id)
{
return m_system_font_provider->get_typeface_by_id(generation, face_id);
}
Optional<FlyString> FontDatabase::resolve_generic_family(StringView family_name, u16 weight, u8 slope)
{
return m_system_font_provider->resolve_generic_family(family_name, weight, slope);
}
void FontDatabase::for_each_typeface_with_family_name(FlyString const& family_name, Function<void(Typeface const&)> callback)
{
m_system_font_provider->for_each_typeface_with_family_name(family_name, move(callback));
}
ErrorOr<Vector<String>> FontDatabase::font_directories()
{
#if defined(USE_FONTCONFIG)
Vector<String> paths;
FcConfig* config = Gfx::GlobalFontConfig::the().get();
FcStrList* dirs = FcConfigGetFontDirs(config);
while (FcChar8* dir = FcStrListNext(dirs)) {
char const* dir_cstring = reinterpret_cast<char const*>(dir);
paths.append(TRY(String::from_utf8(StringView { dir_cstring, strlen(dir_cstring) })));
}
FcStrListDone(dirs);
return paths;
#elif defined(AK_OS_HAIKU)
Vector<String> paths_vector;
char** paths;
size_t paths_count;
if (find_paths(B_FIND_PATH_FONTS_DIRECTORY, NULL, &paths, &paths_count) == B_OK) {
for (size_t i = 0; i < paths_count; ++i) {
StringBuilder builder;
builder.append(paths[i], strlen(paths[i]));
paths_vector.append(TRY(builder.to_string()));
}
}
return paths_vector;
#else
# if defined(AK_OS_SERENITY)
return Vector<String> { {
"/res/fonts"_string,
} };
# elif defined(AK_OS_MACOS)
return Vector<String> { {
"/System/Library/Fonts"_string,
"/Library/Fonts"_string,
TRY(String::formatted("{}/Library/Fonts"sv, Core::StandardPaths::home_directory())),
} };
# elif defined(AK_OS_ANDROID)
return Vector<String> { {
// FIXME: We should be using the ASystemFontIterator NDK API here.
// There is no guarantee that this will continue to exist on future versions of Android.
"/system/fonts"_string,
} };
# elif defined(AK_OS_WINDOWS)
return Vector<String> { {
TRY(String::formatted(R"({}\Fonts)"sv, getenv("WINDIR"))),
TRY(String::formatted(R"({}\Microsoft\Windows\Fonts)"sv, getenv("LOCALAPPDATA"))),
} };
# else
Vector<String> paths;
auto user_data_directory = Core::StandardPaths::user_data_directory();
paths.append(TRY(String::formatted("{}/fonts", user_data_directory)));
paths.append(TRY(String::formatted("{}/X11/fonts", user_data_directory)));
auto data_directories = Core::StandardPaths::system_data_directories();
for (auto& data_directory : data_directories) {
paths.append(TRY(String::formatted("{}/fonts", data_directory)));
paths.append(TRY(String::formatted("{}/X11/fonts", data_directory)));
}
return paths;
# endif
#endif
}
}