1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-08 05:27:14 +09:00

LibCore+LibGfx: Move font_directories from LibCore to LibGfx

the goal is to rely on fontconfig for font directory resolution. it
doesn't seem like it would be appropritate to call to fontconfig funcs
from within the LibCore.

i'm not 100% confident that FontDatabase is the correct place.. seems
okay?
This commit is contained in:
blukai 2025-05-21 02:40:06 +02:00 committed by Andrew Kaster
parent 6e00a38099
commit 4b3691ff39
Notes: github-actions[bot] 2025-05-26 18:15:38 +00:00
5 changed files with 53 additions and 51 deletions

View file

@ -22,10 +22,6 @@
# include <unistd.h>
#endif
#if defined(AK_OS_HAIKU)
# include <FindDirectory.h>
#endif
namespace Core {
static Optional<StringView> get_environment_if_not_empty(StringView name)
@ -237,49 +233,4 @@ ByteString StandardPaths::tempfile_directory()
#endif
}
ErrorOr<Vector<String>> StandardPaths::font_directories()
{
#if 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
auto paths = Vector { {
# if defined(AK_OS_SERENITY)
"/res/fonts"_string,
# elif defined(AK_OS_MACOS)
"/System/Library/Fonts"_string,
"/Library/Fonts"_string,
TRY(String::formatted("{}/Library/Fonts"sv, home_directory())),
# elif defined(AK_OS_ANDROID)
// 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)
TRY(String::formatted(R"({}\Fonts)"sv, getenv("WINDIR"))),
TRY(String::formatted(R"({}\Microsoft\Windows\Fonts)"sv, getenv("LOCALAPPDATA"))),
# else
TRY(String::formatted("{}/fonts", user_data_directory())),
TRY(String::formatted("{}/X11/fonts", user_data_directory())),
# endif
} };
# if !(defined(AK_OS_SERENITY) || defined(AK_OS_MACOS) || defined(AK_OS_WINDOWS))
auto data_directories = 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)));
}
# endif
return paths;
#endif
}
}

View file

@ -26,7 +26,6 @@ public:
static ByteString user_data_directory();
static Vector<ByteString> system_data_directories();
static ErrorOr<ByteString> runtime_directory();
static ErrorOr<Vector<String>> font_directories();
};
}

View file

@ -5,9 +5,14 @@
*/
#include <AK/FlyString.h>
#include <LibCore/StandardPaths.h>
#include <LibGfx/Font/Font.h>
#include <LibGfx/Font/FontDatabase.h>
#if defined(AK_OS_HAIKU)
# include <FindDirectory.h>
#endif
namespace Gfx {
// Key function for SystemFontProvider to emit the vtable here
@ -44,4 +49,49 @@ void FontDatabase::for_each_typeface_with_family_name(FlyString const& family_na
m_system_font_provider->for_each_typeface_with_family_name(family_name, move(callback));
}
ErrorOr<Vector<String>> FontDatabase::font_directories()
{
#if 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
auto paths = Vector { {
# if defined(AK_OS_SERENITY)
"/res/fonts"_string,
# elif defined(AK_OS_MACOS)
"/System/Library/Fonts"_string,
"/Library/Fonts"_string,
TRY(String::formatted("{}/Library/Fonts"sv, Core::StandardPaths::home_directory())),
# elif defined(AK_OS_ANDROID)
// 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)
TRY(String::formatted(R"({}\Fonts)"sv, getenv("WINDIR"))),
TRY(String::formatted(R"({}\Microsoft\Windows\Fonts)"sv, getenv("LOCALAPPDATA"))),
# else
TRY(String::formatted("{}/fonts", Core::StandardPaths::user_data_directory())),
TRY(String::formatted("{}/X11/fonts", Core::StandardPaths::user_data_directory())),
# endif
} };
# if !(defined(AK_OS_SERENITY) || defined(AK_OS_MACOS) || defined(AK_OS_WINDOWS))
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)));
}
# endif
return paths;
#endif
}
}

View file

@ -32,6 +32,8 @@ public:
void for_each_typeface_with_family_name(FlyString const& family_name, Function<void(Typeface const&)>);
[[nodiscard]] StringView system_font_provider_name() const;
static ErrorOr<Vector<String>> font_directories();
private:
FontDatabase();
~FontDatabase() = default;

View file

@ -36,7 +36,7 @@ FontPlugin::FontPlugin(bool is_layout_test_mode, Gfx::SystemFontProvider* font_p
if (is<Gfx::PathFontProvider>(*font_provider)) {
auto& path_font_provider = static_cast<Gfx::PathFontProvider&>(*font_provider);
// Load anything we can find in the system's font directories
for (auto const& path : Core::StandardPaths::font_directories().release_value_but_fixme_should_propagate_errors())
for (auto const& path : Gfx::FontDatabase::font_directories().release_value_but_fixme_should_propagate_errors())
path_font_provider.load_all_fonts_from_uri(MUST(String::formatted("file://{}", path)));
}