Truly independent web browser
LibGfx+LibWeb: Apply font features to system fallback fonts
sideshowbarker55dbd578cf87
diff --git a/Libraries/LibGfx/Font/SystemFallbackFonts.cpp b/Libraries/LibGfx/Font/SystemFallbackFonts.cppindex f1afb2fa009..9762412d3c1 100644--- a/Libraries/LibGfx/Font/SystemFallbackFonts.cpp+++ b/Libraries/LibGfx/Font/SystemFallbackFonts.cpp@@ -43,7 +43,7 @@ SystemFallbackFontCache& system_fallback_font_cache()}-RefPtr<Font const> system_fallback_font(SystemFallbackFontKey const& key, float point_size)+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@@ -52,11 +52,13 @@ RefPtr<Font const> system_fallback_font(SystemFallbackFontKey const& key, floatif (auto cached = cache.typefaces.get(key); cached.has_value()) {if (!*cached)return nullptr;- return (*cached)->font(point_size, {});+ 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;}@@ -77,13 +79,15 @@ size_t system_fallback_font_cache_size()}extern "C" {-void const* ladybird_gfx_system_fallback_font(u32, u16, u16, u8, bool, float);+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)+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,@@ -92,7 +96,7 @@ extern "C" void const* ladybird_gfx_system_fallback_font(u32 code_point, u16 wei.slope = slope,.prefer_color_emoji = prefer_color_emoji,},- point_size);+ point_size, shape_features);return font ? font.leak_ref() : nullptr;}diff --git a/Libraries/LibGfx/Font/SystemFallbackFonts.h b/Libraries/LibGfx/Font/SystemFallbackFonts.hindex dde98f46aed..2e2c76b5ad4 100644--- a/Libraries/LibGfx/Font/SystemFallbackFonts.h+++ b/Libraries/LibGfx/Font/SystemFallbackFonts.h@@ -9,6 +9,7 @@#include <AK/RefPtr.h>#include <AK/Types.h>#include <LibGfx/Forward.h>+#include <LibGfx/ShapeFeature.h>namespace Gfx {@@ -26,7 +27,7 @@ struct SystemFallbackFontKey {// font set alone, never on a document, so one memo serves every document in the process and the// answer for a key never changes while the font set stands. The memo keeps one typeface per code// point and style until the font set changes; the point size only picks a font from that typeface.-RefPtr<Font const> system_fallback_font(SystemFallbackFontKey const&, float point_size);+RefPtr<Font const> system_fallback_font(SystemFallbackFontKey const&, float point_size, ShapeFeatures const& = {});// Drops every memoized answer. Call this whenever the installed font set changes.void clear_system_fallback_font_cache();diff --git a/Libraries/LibGfx/FontCascadeList.cpp b/Libraries/LibGfx/FontCascadeList.cppindex 48db9e46583..e3da52d96b9 100644--- a/Libraries/LibGfx/FontCascadeList.cpp+++ b/Libraries/LibGfx/FontCascadeList.cpp@@ -426,6 +426,7 @@ struct FfiCascadeSnapshotHeader {u16 system_fallback_width;u8 system_fallback_slope;bool has_system_fallback;+ size_t system_fallback_feature_count;};struct FfiCascadeSnapshotEntry {@@ -455,6 +456,7 @@ void const* ladybird_gfx_font_cascade_list_frozen(void const*);void const* ladybird_gfx_cascade_snapshot_begin(void const*);void ladybird_gfx_cascade_snapshot_header(void const*, Gfx::FFI::FfiCascadeSnapshotHeader*);void ladybird_gfx_cascade_snapshot_fill(void const*, bool, Gfx::FFI::FfiCascadeSnapshotEntry*, Gfx::FFI::FfiCascadeSnapshotRange*);+void ladybird_gfx_cascade_snapshot_system_fallback_features(void const*, Gfx::ShapeFeature*);void ladybird_gfx_cascade_snapshot_end(void const*);bool ladybird_gfx_resolve_pending_face(u64);}@@ -519,6 +521,7 @@ struct CascadeSnapshot {u16 system_fallback_weight { 0 };u16 system_fallback_width { 0 };u8 system_fallback_slope { 0 };+ Gfx::ShapeFeatures system_fallback_features {};};size_t total_range_count(Vector<Gfx::FontCascadeList::SnapshotEntry> const& entries)@@ -554,6 +557,7 @@ extern "C" void const* ladybird_gfx_cascade_snapshot_begin(void const* list)snapshot->system_fallback_weight = request.weight;snapshot->system_fallback_width = request.width;snapshot->system_fallback_slope = request.slope;+ snapshot->system_fallback_features = request.shape_features;}return snapshot;}@@ -574,9 +578,22 @@ extern "C" void ladybird_gfx_cascade_snapshot_header(void const* handle, Gfx::FF.system_fallback_width = snapshot.system_fallback_width,.system_fallback_slope = snapshot.system_fallback_slope,.has_system_fallback = snapshot.has_system_fallback,+ .system_fallback_feature_count = snapshot.system_fallback_features.size(),};}+// NB: Rust reads these as its FfiShapeFeature.+static_assert(sizeof(Gfx::ShapeFeature) == 8);+static_assert(offsetof(Gfx::ShapeFeature, value) == 4);++extern "C" void ladybird_gfx_cascade_snapshot_system_fallback_features(void const* handle, Gfx::ShapeFeature* out_features)+{+ VERIFY(handle);+ auto const& features = static_cast<CascadeSnapshot const*>(handle)->system_fallback_features;+ for (size_t index = 0; index < features.size(); ++index)+ out_features[index] = features[index];+}+extern "C" void ladybird_gfx_cascade_snapshot_fill(void const* handle, bool fallback, Gfx::FFI::FfiCascadeSnapshotEntry* out_entries, Gfx::FFI::FfiCascadeSnapshotRange* out_ranges){VERIFY(handle);diff --git a/Libraries/LibGfx/FontCascadeList.h b/Libraries/LibGfx/FontCascadeList.hindex 21db5bf7042..2fc036909ab 100644--- a/Libraries/LibGfx/FontCascadeList.h+++ b/Libraries/LibGfx/FontCascadeList.h@@ -59,6 +59,8 @@ public:u16 weight { 0 };u16 width { 0 };u8 slope { 0 };+ // The features the cascade's request asks of every font, which a system fallback font applies as well.+ ShapeFeatures shape_features;};using SystemFontFallbackCallback = Function<RefPtr<Font const>(u32, EmojiPresentation, SystemFallbackRequest const&)>;diff --git a/Libraries/LibGfx/Rust/src/font.rs b/Libraries/LibGfx/Rust/src/font.rsindex f1eba12709c..c6b3ecbb3e6 100644--- a/Libraries/LibGfx/Rust/src/font.rs+++ b/Libraries/LibGfx/Rust/src/font.rs@@ -411,6 +411,10 @@ unsafe extern "C" {out_entries: *mut FfiCascadeSnapshotEntry,out_ranges: *mut FfiCascadeSnapshotRange,);+ fn ladybird_gfx_cascade_snapshot_system_fallback_features(+ snapshot: *const c_void,+ out_features: *mut FfiShapeFeature,+ );fn ladybird_gfx_cascade_snapshot_end(snapshot: *const c_void);fn ladybird_gfx_system_fallback_font(code_point: u32,@@ -419,6 +423,8 @@ unsafe extern "C" {slope: u8,prefer_color_emoji: bool,point_size: f32,+ features: *const FfiShapeFeature,+ feature_count: usize,) -> *const c_void;fn ladybird_gfx_font_invisible_variant(font: *const c_void) -> *const c_void;@@ -444,6 +450,15 @@ struct FfiCascadeSnapshotHeader {system_fallback_width: u16,system_fallback_slope: u8,has_system_fallback: bool,+ system_fallback_feature_count: usize,+}++/// A `Gfx::ShapeFeature`: an OpenType feature tag and its value.+#[repr(C)]+#[derive(Clone, Copy, Default, Debug, PartialEq)]+struct FfiShapeFeature {+ tag: [u8; 4],+ value: u32,}#[repr(C)]@@ -521,12 +536,14 @@ struct EntrySelection<'a> {}/// How this cascade asks for a font no listed family covers.-#[derive(Clone, Copy, Debug, PartialEq)]+#[derive(Clone, Debug, PartialEq)]struct SystemFallbackStyle {point_size: f32,weight: u16,width: u16,slope: u8,+ /// The features the cascade's request asks of every font, which the system fallback font applies as well.+ features: Box<[FfiShapeFeature]>,}/// The index of the first entry with a font and the ASCII code points it resolves, where one does.@@ -951,6 +968,11 @@ impl FrozenFontList {ffi_fallback_ranges.as_mut_ptr(),);}+ let mut system_fallback_features = vec![FfiShapeFeature::default(); header.system_fallback_feature_count];+ // SAFETY: The buffer is exactly the size the header asked for.+ unsafe {+ ladybird_gfx_cascade_snapshot_system_fallback_features(snapshot, system_fallback_features.as_mut_ptr())+ };let last_resort = (!header.last_resort_font.is_null()).then(|| {// SAFETY: The list owns its last-resort font for the snapshot's duration.unsafe { FontHandle::intern(header.last_resort_font) }@@ -1028,6 +1050,7 @@ impl FrozenFontList {weight: header.system_fallback_weight,width: header.system_fallback_width,slope: header.system_fallback_slope,+ features: system_fallback_features.into_boxed_slice(),}),fonts: fonts.into_boxed_slice(),ascii_cache: std::array::from_fn(|_| std::sync::atomic::AtomicU64::new(0)),@@ -1091,7 +1114,7 @@ impl FrozenFontList {}fn system_fallback_font(&self, code_point: u32, presentation: EmojiPresentation) -> Option<FontHandle> {- let style = self.system_fallback?;+ let style = self.system_fallback.as_ref()?;// SAFETY: The service answers with a font it transfers one reference to, or with null.let raw = unsafe {ladybird_gfx_system_fallback_font(@@ -1101,6 +1124,8 @@ impl FrozenFontList {style.slope,presentation.is_emoji,style.point_size,+ style.features.as_ptr(),+ style.features.len(),)};// SAFETY: A non-null answer carries the reference this handle takes over.diff --git a/Libraries/LibWeb/CSS/FontResolution.cpp b/Libraries/LibWeb/CSS/FontResolution.cppindex 5c3cb9e4b7f..1dcb23a5980 100644--- a/Libraries/LibWeb/CSS/FontResolution.cpp+++ b/Libraries/LibWeb/CSS/FontResolution.cpp@@ -579,6 +579,7 @@ NonnullRefPtr<Gfx::FontCascadeList const> resolve_font_cascade(FontFaceSnapshot.weight = static_cast<u16>(clamp(weight, 1, 1000)),.width = static_cast<u16>(font_width_bucket_from_percentage(font_width.value())),.slope = static_cast<u8>(slope),+ .shape_features = shape_features_for(key, nullptr),};font_list->set_system_font_fallback_callback(request, [](u32 code_point, Gfx::EmojiPresentation presentation, Gfx::FontCascadeList::SystemFallbackRequest const& request) -> RefPtr<Gfx::Font const> {Gfx::SystemFallbackFontKey key {@@ -588,7 +589,7 @@ NonnullRefPtr<Gfx::FontCascadeList const> resolve_font_cascade(FontFaceSnapshot.slope = request.slope,.prefer_color_emoji = presentation == Gfx::EmojiPresentation::Emoji,};- return Gfx::system_fallback_font(key, request.point_size);+ return Gfx::system_fallback_font(key, request.point_size, request.shape_features);});}diff --git a/Libraries/LibWeb/Rust/src/gfx_test_stubs.rs b/Libraries/LibWeb/Rust/src/gfx_test_stubs.rsindex 0c8e3ea44f3..4d88e3c6234 100644--- a/Libraries/LibWeb/Rust/src/gfx_test_stubs.rs+++ b/Libraries/LibWeb/Rust/src/gfx_test_stubs.rs@@ -89,6 +89,8 @@ extern "C" fn ladybird_gfx_system_fallback_font(_slope: u8,_prefer_color_emoji: bool,_point_size: f32,+ _features: *const c_void,+ _feature_count: usize,) -> *const c_void {unreachable!("no unit test reads a font");}diff --git a/Tests/LibGfx/TestFont.cpp b/Tests/LibGfx/TestFont.cppindex fd759e5a4be..dffaf85c48c 100644--- a/Tests/LibGfx/TestFont.cpp+++ b/Tests/LibGfx/TestFont.cpp@@ -436,6 +436,49 @@ TEST_CASE(system_fallback_fonts_can_be_matched_on_several_threads)EXPECT_EQ(Gfx::system_fallback_font_cache_size(), 2u);}+// A system fallback font applies the features its request asks for, as the cascade's own fonts do, whether the memo+// already knew its typeface or not.+TEST_CASE(system_fallback_fonts_apply_the_requested_features)+{+ Gfx::clear_system_fallback_font_cache();+ Gfx::SystemFallbackFontKey key {+ .code_point = 'A',+ .weight = 400,+ .width = Gfx::FontWidth::Normal,+ .slope = 0,+ .prefer_color_emoji = false,+ };+ Gfx::ShapeFeatures features { { { 'l', 'i', 'g', 'a' }, 0 }, { { 's', 'm', 'c', 'p' }, 1 } };++ // NB: A machine without a font covering the code point answers null, which applies nothing.+ auto first = Gfx::system_fallback_font(key, 12, features);+ if (!first)+ return;+ EXPECT_EQ(first->features(), features);+ EXPECT_EQ(Gfx::system_fallback_font(key, 12, features)->features(), features);+ EXPECT(Gfx::system_fallback_font(key, 12)->features().is_empty());+}++// A frozen cascade asks the system fallback for its request's features too.+TEST_CASE(frozen_cascade_asks_the_system_fallback_for_the_requested_features)+{+ Gfx::clear_system_fallback_font_cache();+ auto last_resort_font = load_text_font(16);+ Gfx::ShapeFeatures features { { { 'l', 'i', 'g', 'a' }, 0 } };+ auto cascade = Gfx::FontCascadeList::create();+ cascade->add(last_resort_font, { { 'a', 'a' } });+ cascade->set_last_resort_font(last_resort_font);+ cascade->set_system_font_fallback_callback({ .point_size = 12, .weight = 400, .width = Gfx::FontWidth::Normal, .slope = 0, .shape_features = features },+ [](u32, Gfx::EmojiPresentation, Gfx::FontCascadeList::SystemFallbackRequest const&) -> RefPtr<Gfx::Font const> { return nullptr; });+ cascade->freeze();++ // NB: Where no installed font covers the code point, the cascade answers with its last-resort font instead.+ auto const& font = cascade->frozen_font_for_code_point('A');+ if (&font == last_resort_font.ptr())+ return;+ EXPECT_EQ(font.features(), features);+}+// A frozen cascade answers the same question as the live one, without entering the document.TEST_CASE(frozen_cascade_matches_the_live_lookup){