mirror of
https://github.com/fergalmoran/ladybird.git
synced 2025-12-22 09:19:03 +00:00
Ladybird: Use fontconfig to choose generic fonts on non-Apple systems
If we get a suggestion from fontconfig, we try those fonts first, before falling back on the hard coded list of known suitable fonts for each generic family.
This commit is contained in:
committed by
Andreas Kling
parent
0c683af57e
commit
2f68e36137
@@ -13,11 +13,22 @@
|
|||||||
#include <LibGfx/Font/Emoji.h>
|
#include <LibGfx/Font/Emoji.h>
|
||||||
#include <LibGfx/Font/FontDatabase.h>
|
#include <LibGfx/Font/FontDatabase.h>
|
||||||
|
|
||||||
|
#ifdef USE_FONTCONFIG
|
||||||
|
# include <fontconfig/fontconfig.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace Ladybird {
|
namespace Ladybird {
|
||||||
|
|
||||||
FontPlugin::FontPlugin(bool is_layout_test_mode)
|
FontPlugin::FontPlugin(bool is_layout_test_mode)
|
||||||
: m_is_layout_test_mode(is_layout_test_mode)
|
: m_is_layout_test_mode(is_layout_test_mode)
|
||||||
{
|
{
|
||||||
|
#ifdef USE_FONTCONFIG
|
||||||
|
{
|
||||||
|
auto fontconfig_initialized = FcInit();
|
||||||
|
VERIFY(fontconfig_initialized);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
// Load anything we can find in the system's font directories
|
// 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 : Core::StandardPaths::font_directories().release_value_but_fixme_should_propagate_errors())
|
||||||
Gfx::FontDatabase::the().load_all_fonts_from_uri(MUST(String::formatted("file://{}", path)));
|
Gfx::FontDatabase::the().load_all_fonts_from_uri(MUST(String::formatted("file://{}", path)));
|
||||||
@@ -50,6 +61,79 @@ Gfx::Font& FontPlugin::default_fixed_width_font()
|
|||||||
return *m_default_fixed_width_font;
|
return *m_default_fixed_width_font;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef USE_FONTCONFIG
|
||||||
|
static Optional<String> query_fontconfig_for_generic_family(Web::Platform::GenericFont generic_font)
|
||||||
|
{
|
||||||
|
char const* pattern_string = nullptr;
|
||||||
|
switch (generic_font) {
|
||||||
|
case Web::Platform::GenericFont::Cursive:
|
||||||
|
pattern_string = "cursive";
|
||||||
|
break;
|
||||||
|
case Web::Platform::GenericFont::Fantasy:
|
||||||
|
pattern_string = "fantasy";
|
||||||
|
break;
|
||||||
|
case Web::Platform::GenericFont::Monospace:
|
||||||
|
pattern_string = "monospace";
|
||||||
|
break;
|
||||||
|
case Web::Platform::GenericFont::SansSerif:
|
||||||
|
pattern_string = "sans-serif";
|
||||||
|
break;
|
||||||
|
case Web::Platform::GenericFont::Serif:
|
||||||
|
pattern_string = "serif";
|
||||||
|
break;
|
||||||
|
case Web::Platform::GenericFont::UiMonospace:
|
||||||
|
pattern_string = "monospace";
|
||||||
|
break;
|
||||||
|
case Web::Platform::GenericFont::UiRounded:
|
||||||
|
pattern_string = "sans-serif";
|
||||||
|
break;
|
||||||
|
case Web::Platform::GenericFont::UiSansSerif:
|
||||||
|
pattern_string = "sans-serif";
|
||||||
|
break;
|
||||||
|
case Web::Platform::GenericFont::UiSerif:
|
||||||
|
pattern_string = "serif";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto* config = FcConfigGetCurrent();
|
||||||
|
VERIFY(config);
|
||||||
|
|
||||||
|
FcPattern* pattern = FcNameParse(reinterpret_cast<FcChar8 const*>(pattern_string));
|
||||||
|
VERIFY(pattern);
|
||||||
|
|
||||||
|
auto success = FcConfigSubstitute(config, pattern, FcMatchPattern);
|
||||||
|
VERIFY(success);
|
||||||
|
|
||||||
|
FcDefaultSubstitute(pattern);
|
||||||
|
|
||||||
|
// Never select bitmap fonts.
|
||||||
|
success = FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);
|
||||||
|
VERIFY(success);
|
||||||
|
|
||||||
|
// FIXME: Enable this once we can handle OpenType variable fonts.
|
||||||
|
success = FcPatternAddBool(pattern, FC_VARIABLE, FcFalse);
|
||||||
|
VERIFY(success);
|
||||||
|
|
||||||
|
Optional<String> name;
|
||||||
|
FcResult result {};
|
||||||
|
|
||||||
|
if (auto* matched = FcFontMatch(config, pattern, &result)) {
|
||||||
|
FcChar8* family = nullptr;
|
||||||
|
if (FcPatternGetString(matched, FC_FAMILY, 0, &family) == FcResultMatch) {
|
||||||
|
auto const* family_cstring = reinterpret_cast<char const*>(family);
|
||||||
|
if (auto string = String::from_utf8(StringView { family_cstring, strlen(family_cstring) }); !string.is_error()) {
|
||||||
|
name = string.release_value();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
FcPatternDestroy(matched);
|
||||||
|
}
|
||||||
|
FcPatternDestroy(pattern);
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void FontPlugin::update_generic_fonts()
|
void FontPlugin::update_generic_fonts()
|
||||||
{
|
{
|
||||||
// How we choose which system font to use for each CSS font:
|
// How we choose which system font to use for each CSS font:
|
||||||
@@ -69,10 +153,19 @@ void FontPlugin::update_generic_fonts()
|
|||||||
|
|
||||||
RefPtr<Gfx::Font const> gfx_font;
|
RefPtr<Gfx::Font const> gfx_font;
|
||||||
|
|
||||||
for (auto& fallback : fallbacks) {
|
#ifdef USE_FONTCONFIG
|
||||||
gfx_font = Gfx::FontDatabase::the().get(fallback, 16, 400, Gfx::FontWidth::Normal, 0);
|
auto name = query_fontconfig_for_generic_family(generic_font);
|
||||||
if (gfx_font)
|
if (name.has_value()) {
|
||||||
break;
|
gfx_font = Gfx::FontDatabase::the().get(name.value(), 16, 400, Gfx::FontWidth::Normal, 0);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (!gfx_font) {
|
||||||
|
for (auto const& fallback : fallbacks) {
|
||||||
|
gfx_font = Gfx::FontDatabase::the().get(fallback, 16, 400, Gfx::FontWidth::Normal, 0);
|
||||||
|
if (gfx_font)
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_generic_font_names[static_cast<size_t>(generic_font)] = gfx_font ? gfx_font->family() : String {};
|
m_generic_font_names[static_cast<size_t>(generic_font)] = gfx_font ? gfx_font->family() : String {};
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
include(accelerated_graphics)
|
include(accelerated_graphics)
|
||||||
|
include(fontconfig)
|
||||||
|
|
||||||
set(WEBCONTENT_SOURCE_DIR ${LADYBIRD_SOURCE_DIR}/Userland/Services/WebContent/)
|
set(WEBCONTENT_SOURCE_DIR ${LADYBIRD_SOURCE_DIR}/Userland/Services/WebContent/)
|
||||||
|
|
||||||
set(WEBCONTENT_SOURCES
|
set(WEBCONTENT_SOURCES
|
||||||
@@ -70,6 +72,10 @@ else()
|
|||||||
add_executable(WebContent main.cpp)
|
add_executable(WebContent main.cpp)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
if (HAS_FONTCONFIG)
|
||||||
|
target_link_libraries(webcontent PRIVATE Fontconfig::Fontconfig)
|
||||||
|
endif()
|
||||||
|
|
||||||
target_link_libraries(WebContent PRIVATE webcontent LibURL)
|
target_link_libraries(WebContent PRIVATE webcontent LibURL)
|
||||||
|
|
||||||
target_sources(webcontent PUBLIC FILE_SET ladybird TYPE HEADERS
|
target_sources(webcontent PUBLIC FILE_SET ladybird TYPE HEADERS
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include(fontconfig)
|
||||||
|
|
||||||
set(WEBWORKER_SOURCE_DIR ${LADYBIRD_SOURCE_DIR}/Userland/Services/WebWorker)
|
set(WEBWORKER_SOURCE_DIR ${LADYBIRD_SOURCE_DIR}/Userland/Services/WebWorker)
|
||||||
|
|
||||||
set(CMAKE_AUTOMOC OFF)
|
set(CMAKE_AUTOMOC OFF)
|
||||||
@@ -22,6 +24,10 @@ target_include_directories(webworker PRIVATE ${LADYBIRD_SOURCE_DIR}/Userland/)
|
|||||||
target_include_directories(webworker PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/..)
|
target_include_directories(webworker PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/..)
|
||||||
target_link_libraries(webworker PUBLIC LibCore LibFileSystem LibGfx LibIPC LibJS LibProtocol LibWeb LibWebView LibLocale LibImageDecoderClient LibMain LibURL)
|
target_link_libraries(webworker PUBLIC LibCore LibFileSystem LibGfx LibIPC LibJS LibProtocol LibWeb LibWebView LibLocale LibImageDecoderClient LibMain LibURL)
|
||||||
|
|
||||||
|
if (HAS_FONTCONFIG)
|
||||||
|
target_link_libraries(webworker PRIVATE Fontconfig::Fontconfig)
|
||||||
|
endif()
|
||||||
|
|
||||||
add_executable(WebWorker main.cpp)
|
add_executable(WebWorker main.cpp)
|
||||||
target_include_directories(WebWorker PRIVATE ${LADYBIRD_SOURCE_DIR}/Userland/)
|
target_include_directories(WebWorker PRIVATE ${LADYBIRD_SOURCE_DIR}/Userland/)
|
||||||
target_link_libraries(WebWorker PRIVATE webworker)
|
target_link_libraries(WebWorker PRIVATE webworker)
|
||||||
|
|||||||
5
Meta/CMake/fontconfig.cmake
Normal file
5
Meta/CMake/fontconfig.cmake
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
if (NOT APPLE)
|
||||||
|
find_package(Fontconfig REQUIRED)
|
||||||
|
set(HAS_FONTCONFIG ON CACHE BOOL "" FORCE)
|
||||||
|
add_compile_definitions(USE_FONTCONFIG=1)
|
||||||
|
endif()
|
||||||
10
vcpkg.json
10
vcpkg.json
@@ -2,7 +2,11 @@
|
|||||||
"builtin-baseline": "01f602195983451bc83e72f4214af2cbc495aa94",
|
"builtin-baseline": "01f602195983451bc83e72f4214af2cbc495aa94",
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
"sqlite3",
|
"sqlite3",
|
||||||
"woff2"
|
"woff2",
|
||||||
|
{
|
||||||
|
"name": "fontconfig",
|
||||||
|
"platform": "linux | freebsd | openbsd"
|
||||||
|
}
|
||||||
],
|
],
|
||||||
"overrides": [
|
"overrides": [
|
||||||
{
|
{
|
||||||
@@ -12,6 +16,10 @@
|
|||||||
{
|
{
|
||||||
"name": "woff2",
|
"name": "woff2",
|
||||||
"version": "1.0.2#4"
|
"version": "1.0.2#4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "fontconfig",
|
||||||
|
"version": "2.14.2#1"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user