From 4b3691ff39d76d2b8a557c1732ca9cdf1a6781f0 Mon Sep 17 00:00:00 2001 From: blukai <20866892+blukai@users.noreply.github.com> Date: Wed, 21 May 2025 02:40:06 +0200 Subject: [PATCH] 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? --- Libraries/LibCore/StandardPaths.cpp | 49 -------------------- Libraries/LibCore/StandardPaths.h | 1 - Libraries/LibGfx/Font/FontDatabase.cpp | 50 +++++++++++++++++++++ Libraries/LibGfx/Font/FontDatabase.h | 2 + Libraries/LibWebView/Plugins/FontPlugin.cpp | 2 +- 5 files changed, 53 insertions(+), 51 deletions(-) diff --git a/Libraries/LibCore/StandardPaths.cpp b/Libraries/LibCore/StandardPaths.cpp index 2b454a6486c..8ba738e8d02 100644 --- a/Libraries/LibCore/StandardPaths.cpp +++ b/Libraries/LibCore/StandardPaths.cpp @@ -22,10 +22,6 @@ # include #endif -#if defined(AK_OS_HAIKU) -# include -#endif - namespace Core { static Optional get_environment_if_not_empty(StringView name) @@ -237,49 +233,4 @@ ByteString StandardPaths::tempfile_directory() #endif } -ErrorOr> StandardPaths::font_directories() -{ -#if defined(AK_OS_HAIKU) - Vector 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 -} - } diff --git a/Libraries/LibCore/StandardPaths.h b/Libraries/LibCore/StandardPaths.h index e0183340142..028f860027a 100644 --- a/Libraries/LibCore/StandardPaths.h +++ b/Libraries/LibCore/StandardPaths.h @@ -26,7 +26,6 @@ public: static ByteString user_data_directory(); static Vector system_data_directories(); static ErrorOr runtime_directory(); - static ErrorOr> font_directories(); }; } diff --git a/Libraries/LibGfx/Font/FontDatabase.cpp b/Libraries/LibGfx/Font/FontDatabase.cpp index 9317b7eabc3..df94d269492 100644 --- a/Libraries/LibGfx/Font/FontDatabase.cpp +++ b/Libraries/LibGfx/Font/FontDatabase.cpp @@ -5,9 +5,14 @@ */ #include +#include #include #include +#if defined(AK_OS_HAIKU) +# include +#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> FontDatabase::font_directories() +{ +#if defined(AK_OS_HAIKU) + Vector 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 +} + } diff --git a/Libraries/LibGfx/Font/FontDatabase.h b/Libraries/LibGfx/Font/FontDatabase.h index 2787a2a1a9a..a4bd8496409 100644 --- a/Libraries/LibGfx/Font/FontDatabase.h +++ b/Libraries/LibGfx/Font/FontDatabase.h @@ -32,6 +32,8 @@ public: void for_each_typeface_with_family_name(FlyString const& family_name, Function); [[nodiscard]] StringView system_font_provider_name() const; + static ErrorOr> font_directories(); + private: FontDatabase(); ~FontDatabase() = default; diff --git a/Libraries/LibWebView/Plugins/FontPlugin.cpp b/Libraries/LibWebView/Plugins/FontPlugin.cpp index efb5aaf1060..44deb519d78 100644 --- a/Libraries/LibWebView/Plugins/FontPlugin.cpp +++ b/Libraries/LibWebView/Plugins/FontPlugin.cpp @@ -36,7 +36,7 @@ FontPlugin::FontPlugin(bool is_layout_test_mode, Gfx::SystemFontProvider* font_p if (is(*font_provider)) { auto& path_font_provider = static_cast(*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))); }