Truly independent web browser
/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/HashMap.h>
#include <AK/Mutex.h>
#include <AK/Singleton.h>
#include <LibGfx/Font/Font.h>
#include <LibGfx/Font/FontDatabase.h>
#include <LibGfx/Font/SystemFallbackFonts.h>
namespace Gfx {
namespace {
struct SystemFallbackFontKeyTraits : public DefaultTraits<SystemFallbackFontKey> {
static unsigned hash(SystemFallbackFontKey const& key)
{
auto style = pair_int_hash(pair_int_hash(key.weight, key.width), pair_int_hash(key.slope, key.prefer_color_emoji));
return pair_int_hash(key.code_point, style);
}
};
struct SystemFallbackFontCache {
AK_ALLOC_WITH_KMALLOC;
Mutex mutex;
// A miss is an answer too: without it every code point no family covers asks the provider again
// on every lookup, and for the shared provider that is an IPC round trip.
// NB: The memo holds typefaces, not fonts, so a font of each size that a page asks for lives only while it is
// in use or among the fonts made last, instead of until the font set changes.
HashMap<SystemFallbackFontKey, RefPtr<Typeface const>, SystemFallbackFontKeyTraits> typefaces;
};
Singleton<SystemFallbackFontCache> s_system_fallback_font_cache;
SystemFallbackFontCache& system_fallback_font_cache()
{
return *s_system_fallback_font_cache;
}
}
RefPtr<Font const> system_fallback_font(SystemFallbackFontKey const& key, float point_size, ShapeFeatures const& shape_features)
{
auto& cache = system_fallback_font_cache();
// NB: The lookup runs under the lock rather than beside it, so one code point is matched once
// even when several threads want it. The cost falls on misses only.
MutexLocker locker(cache.mutex);
if (auto cached = cache.typefaces.get(key); cached.has_value()) {
if (!*cached)
return nullptr;
return (*cached)->font(point_size, {}, shape_features);
}
RefPtr<Font const> font = FontDatabase::the().get_font_for_code_point(
key.code_point, point_size, key.weight, key.width, key.slope, key.prefer_color_emoji);
cache.typefaces.set(key, font ? &font->typeface() : nullptr);
if (font && !shape_features.is_empty())
return font->typeface().font(point_size, {}, shape_features);
return font;
}
void clear_system_fallback_font_cache()
{
auto& cache = system_fallback_font_cache();
MutexLocker locker(cache.mutex);
cache.typefaces.clear();
}
size_t system_fallback_font_cache_size()
{
auto& cache = system_fallback_font_cache();
MutexLocker locker(cache.mutex);
return cache.typefaces.size();
}
}
extern "C" {
void const* ladybird_gfx_system_fallback_font(u32, u16, u16, u8, bool, float, Gfx::ShapeFeature const*, size_t);
void const* ladybird_gfx_font_invisible_variant(void const*);
}
// Transfers one reference to the caller, or answers null where no installed font covers the code point.
extern "C" void const* ladybird_gfx_system_fallback_font(u32 code_point, u16 weight, u16 width, u8 slope, bool prefer_color_emoji, float point_size, Gfx::ShapeFeature const* features, size_t feature_count)
{
Gfx::ShapeFeatures shape_features;
shape_features.append(features, feature_count);
auto font = Gfx::system_fallback_font(
{
.code_point = code_point,
.weight = weight,
.width = width,
.slope = slope,
.prefer_color_emoji = prefer_color_emoji,
},
point_size, shape_features);
return font ? font.leak_ref() : nullptr;
}
// https://drafts.csswg.org/css-fonts-4/#invisible-fallback
// An anonymous face with the selected face's metrics and no ink. Transfers one reference to the caller, which owns the
// variant and memoizes it for as long as it needs it.
extern "C" void const* ladybird_gfx_font_invisible_variant(void const* font)
{
VERIFY(font);
return &static_cast<Gfx::Font const*>(font)->invisible_variant().leak_ref();
}