Skip to content

Truly independent web browser

LibWeb: Implement FontFaceSetLoadEvent in Rust

Andreas Kling7732a4a6ea4d
diff --git a/Libraries/LibWeb/Bindings/Rust/src/css/exports.rs b/Libraries/LibWeb/Bindings/Rust/src/css/exports.rs
index 73805235c54..4290b5ba493 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/css/exports.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/css/exports.rs
@@ -46,7 +46,7 @@ use super::css_numeric_value::AnyCSSNumericValue;
use super::css_page_rule::CSSPageRule;
use super::css_position_try_rule::CSSPositionTryRule;
use super::css_property_rule::CSSPropertyRule;
-use super::css_rule::AnyCSSRule;
+use super::css_rule::{AnyCSSRule, wrap_cpp_cell};
use super::css_rule_list::CSSRuleList;
use super::css_scope_rule::CSSScopeRule;
use super::css_style_rule::CSSStyleRule;
@@ -56,16 +56,17 @@ use super::css_supports_rule::CSSSupportsRule;
use super::css_transform_value::CSSTransformValue;
use super::css_unit_value::CSSUnitValue;
use super::css_unparsed_value::CSSUnparsedValue;
+use super::font_face_set_load_event::{FontFaceSetLoadEvent, FontFaceSetLoadEventInit};
use super::media_list::MediaList;
use super::media_query_list::MediaQueryList;
use super::screen::Screen;
use super::style_sheet_list::StyleSheetList;
use super::transition_event::{TransitionEvent, TransitionEventInit};
use super::typed_om;
-use crate::bindings::Exception;
use crate::bindings::conversions::CppInterface;
use crate::bindings::cpp_cell::CppCell;
use crate::bindings::interface::{Ref, allocate, cell_from_abi, cell_if_abi, cell_into_abi, realm_arguments};
+use crate::bindings::{CallContext, Exception};
use crate::dom::event::EventInit;
/// # Safety
@@ -497,6 +498,43 @@ pub unsafe extern "C" fn web_media_query_list_evaluate_and_report_change(vm: *mu
list.evaluate_and_report_change(vm);
}
+/// A new FontFaceSetLoadEvent with the name, as the Event that C++ dispatches, whose fontfaces attribute holds the
+/// FontFace objects that C++ passes.
+///
+/// # Safety
+///
+/// `vm` must be the live VM, `realm` a live realm, `name` a raw string that C++ hands over, and `font_faces` must point
+/// to `count` live FontFace objects.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_font_face_set_load_event_create(
+ vm: *mut c_void,
+ realm: *mut c_void,
+ name: usize,
+ font_faces: *const *mut c_void,
+ count: usize,
+) -> *mut c_void {
+ // SAFETY: The caller passes the live VM, a live realm and a raw string that it hands over.
+ let ((vm, realm), name) = unsafe { (realm_arguments(vm, realm), Utf16String::from_raw_owned(name)) };
+ let cx = CallContext::new(vm, realm);
+ let fontfaces = MarkedVec::with_capacity(vm, count);
+ // SAFETY: The caller passes `count` live FontFace objects.
+ for font_face in unsafe { ak::slice_from_raw_parts(font_faces, count) } {
+ // SAFETY: The caller passes live FontFace objects.
+ fontfaces.push(wrap_cpp_cell(&cx, &unsafe { CppCell::from_raw(*font_face) }));
+ }
+ let event_init = FontFaceSetLoadEventInit {
+ parent: EventInit {
+ bubbles: false,
+ cancelable: false,
+ composed: false,
+ },
+ fontfaces,
+ };
+ FontFaceSetLoadEvent::create(vm, realm, &name, event_init)
+ .event()
+ .as_ptr()
+}
+
/// A new Screen of the window.
///
/// # Safety
diff --git a/Libraries/LibWeb/Bindings/Rust/src/css/font_face_set_load_event.rs b/Libraries/LibWeb/Bindings/Rust/src/css/font_face_set_load_event.rs
new file mode 100644
index 00000000000..fe173dd1e74
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/css/font_face_set_load_event.rs
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2026, Tim Ledbetter <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! FontFaceSetLoadEvent, https://drafts.csswg.org/css-font-loading/#fontfacesetloadevent
+
+use core::ffi::c_void;
+use core::ptr::NonNull;
+
+use ak::Utf16String;
+use libjs_rust::api::{Gc, MarkedVec, Realm, Trace, Vm};
+
+use crate::bindings::conversions::CppInterface;
+use crate::bindings::event_target::CppEvent;
+use crate::bindings::interface::{Ref, Wrappable, allocate};
+use crate::bindings::{CallContext, WebIdlResult};
+use crate::dom::event::EventInit;
+
+/// The dictionary FontFaceSetLoadEventInit of CSS/FontFaceSetLoadEvent.idl.
+pub(crate) struct FontFaceSetLoadEventInit<'vm> {
+ pub(crate) parent: EventInit,
+ pub(crate) fontfaces: MarkedVec<'vm, CppInterface>,
+}
+
+// https://drafts.csswg.org/css-font-loading/#fontfacesetloadevent
+#[derive(Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct FontFaceSetLoadEvent {
+ event: CppEvent,
+ fontfaces: Vec<CppInterface>,
+}
+
+impl Wrappable for FontFaceSetLoadEvent {
+ fn cpp_parent(&self) -> Option<NonNull<c_void>> {
+ self.event.as_cpp_parent()
+ }
+}
+
+impl FontFaceSetLoadEvent {
+ pub(crate) fn create(
+ vm: &Vm,
+ realm: Gc<Realm>,
+ event_name: &Utf16String,
+ event_init: FontFaceSetLoadEventInit<'_>,
+ ) -> Ref<FontFaceSetLoadEvent> {
+ let event = CppEvent::create_for_owner(
+ realm,
+ event_name,
+ event_init.parent.bubbles,
+ event_init.parent.cancelable,
+ event_init.parent.composed,
+ );
+ let object = allocate(vm, |_| FontFaceSetLoadEvent {
+ event,
+ fontfaces: event_init.fontfaces.to_vec(),
+ });
+ object.event.set_owner(object);
+ object
+ }
+
+ // https://drafts.csswg.org/css-font-loading/#dom-fontfacesetloadevent-fontfacesetloadevent
+ pub(crate) fn construct(
+ cx: &mut CallContext,
+ r#type: Utf16String,
+ event_init_dict: FontFaceSetLoadEventInit<'_>,
+ ) -> WebIdlResult<Ref<FontFaceSetLoadEvent>> {
+ Ok(Self::create(cx.vm(), cx.realm(), &r#type, event_init_dict))
+ }
+
+ pub(crate) fn event(&self) -> &CppEvent {
+ &self.event
+ }
+
+ // https://drafts.csswg.org/css-font-loading/#dom-fontfacesetloadevent-fontfaces
+ pub(crate) fn fontfaces<'vm>(&self, cx: &mut CallContext<'vm>) -> WebIdlResult<MarkedVec<'vm, CppInterface>> {
+ let fontfaces = MarkedVec::with_capacity(cx.vm(), self.fontfaces.len());
+ fontfaces.extend_from_slice(&self.fontfaces);
+ Ok(fontfaces)
+ }
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/css/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/css/mod.rs
index e12c06307c0..1dd942fb212 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/css/mod.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/css/mod.rs
@@ -64,6 +64,7 @@ pub(crate) mod css_unit_value;
pub(crate) mod css_unparsed_value;
pub(crate) mod css_variable_reference_value;
pub(crate) mod exports;
+pub(crate) mod font_face_set_load_event;
pub(crate) mod media_list;
pub(crate) mod media_query_list;
pub(crate) mod media_query_list_event;
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt
index d836a0a89d7..bfba0d35bb5 100644
--- a/Libraries/LibWeb/CMakeLists.txt
+++ b/Libraries/LibWeb/CMakeLists.txt
@@ -109,7 +109,6 @@ set(SOURCES
CSS/FontLoading.cpp
CSS/FontResolution.cpp
CSS/FontFaceSet.cpp
- CSS/FontFaceSetLoadEvent.cpp
CSS/Frequency.cpp
CSS/Highlight.cpp
CSS/HighlightRegistry.cpp
diff --git a/Libraries/LibWeb/CSS/FontFaceSet.cpp b/Libraries/LibWeb/CSS/FontFaceSet.cpp
index ec6cf32acb0..4766df4b0cf 100644
--- a/Libraries/LibWeb/CSS/FontFaceSet.cpp
+++ b/Libraries/LibWeb/CSS/FontFaceSet.cpp
@@ -10,9 +10,11 @@
#include <LibGC/Heap.h>
#include <LibGC/HeapVector.h>
#include <LibGC/Root.h>
+#include <LibJS/HostClassBuilder.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/Realm.h>
#include <LibJS/Runtime/Set.h>
+#include <LibJS/Runtime/VM.h>
#include <LibWeb/Bindings/FontFace.h>
#include <LibWeb/Bindings/FontFaceSet.h>
#include <LibWeb/Bindings/Wrappable.h>
@@ -21,24 +23,29 @@
#include <LibWeb/CSS/FontComputer.h>
#include <LibWeb/CSS/FontFace.h>
#include <LibWeb/CSS/FontFaceSet.h>
-#include <LibWeb/CSS/FontFaceSetLoadEvent.h>
#include <LibWeb/CSS/Parser/Parser.h>
#include <LibWeb/CSS/PropertyID.h>
#include <LibWeb/CSS/StyleSheetState.h>
#include <LibWeb/CSS/StyleValues/ShorthandStyleValue.h>
#include <LibWeb/CSS/StyleValues/StyleValueList.h>
#include <LibWeb/DOM/Document.h>
+#include <LibWeb/DOM/Event.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
#include <LibWeb/HTML/Window.h>
-#include <LibWeb/HighResolutionTime/TimeOrigin.h>
#include <LibWeb/Page/RenderingStats.h>
#include <LibWeb/Platform/EventLoopPlugin.h>
#include <LibWeb/WebIDL/Promise.h>
namespace Web::CSS {
+extern "C" {
+
+// Defined in Bindings/Rust/src/css/exports.rs.
+void* web_font_face_set_load_event_create(JSVM*, void* realm, FlatPtr name, void* const* font_faces, size_t count);
+}
+
GC_DEFINE_ALLOCATOR(FontFaceSet);
GC::Ref<FontFaceSet> FontFaceSet::create(HTML::EnvironmentSettingsObject& environment)
@@ -467,7 +474,16 @@ void FontFaceSet::fire_a_font_load_event(Utf16FlyString name, Vector<NonnullRefP
// 1. The fontfaces attribute is initialized to the result of filtering font faces to only contain FontFace
// objects contained in target.
font_faces.remove_all_matching([&](auto const& font_face) { return !m_font_faces.contains_slow(font_face); });
- dispatch_event(FontFaceSetLoadEvent::create_for_fonts(name, move(font_faces), HighResolutionTime::current_high_resolution_time(relevant_settings_object().global_object())));
+ GC::RootVector<GC::Ref<FontFace>> objects;
+ objects.ensure_capacity(font_faces.size());
+ for (auto const& font_face : font_faces)
+ objects.unchecked_append(font_face->cssom_font_face());
+ Vector<void*> cells;
+ cells.ensure_capacity(objects.size());
+ for (auto font_face : objects)
+ cells.unchecked_append(static_cast<GC::Cell*>(font_face.ptr()));
+ auto* event = web_font_face_set_load_event_create(JS::HostABI::vm_to_abi(JS::VM::the()), &relevant_settings_object().realm(), name.to_utf16_string().into_raw(), cells.data(), cells.size());
+ dispatch_event(*static_cast<DOM::Event*>(static_cast<GC::Cell*>(event)));
}
// https://drafts.csswg.org/css-font-loading/#ref-for-fontfaceset-pending-on-the-environment%E2%91%A1
diff --git a/Libraries/LibWeb/CSS/FontFaceSetLoadEvent.cpp b/Libraries/LibWeb/CSS/FontFaceSetLoadEvent.cpp
deleted file mode 100644
index 7c587c98f48..00000000000
--- a/Libraries/LibWeb/CSS/FontFaceSetLoadEvent.cpp
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * Copyright (c) 2026, Tim Ledbetter <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibGC/Heap.h>
-#include <LibWeb/CSS/FontFace.h>
-#include <LibWeb/CSS/FontFaceSetLoadEvent.h>
-#include <LibWeb/HighResolutionTime/TimeOrigin.h>
-
-namespace Web::CSS {
-
-GC_DEFINE_ALLOCATOR(FontFaceSetLoadEvent);
-
-GC::Ref<FontFaceSetLoadEvent> FontFaceSetLoadEvent::create(Utf16FlyString const& event_name, Bindings::FontFaceSetLoadEventInit const& event_init, HighResolutionTime::DOMHighResTimeStamp time_stamp)
-{
- return GC::Heap::the().allocate<FontFaceSetLoadEvent>(event_name, event_init, time_stamp);
-}
-
-GC::Ref<FontFaceSetLoadEvent> FontFaceSetLoadEvent::create_for_fonts(Utf16FlyString const& event_name, Vector<NonnullRefPtr<FontFaceState>> fonts, HighResolutionTime::DOMHighResTimeStamp time_stamp)
-{
- return GC::Heap::the().allocate<FontFaceSetLoadEvent>(event_name, move(fonts), time_stamp);
-}
-
-// https://drafts.csswg.org/css-font-loading/#dom-fontfacesetloadevent-fontfacesetloadevent
-WebIDL::ExceptionOr<GC::Ref<FontFaceSetLoadEvent>> FontFaceSetLoadEvent::create_for_constructor(Utf16FlyString const& event_name, Bindings::FontFaceSetLoadEventInit const& event_init, HighResolutionTime::DOMHighResTimeStamp time_stamp)
-{
- return create(event_name, event_init, time_stamp);
-}
-
-FontFaceSetLoadEvent::FontFaceSetLoadEvent(Utf16FlyString const& event_name, Bindings::FontFaceSetLoadEventInit const& event_init, HighResolutionTime::DOMHighResTimeStamp time_stamp)
- : DOM::Event(FlyString { event_name.to_utf16_string().to_utf8() }, event_init, time_stamp)
-{
- m_fontfaces.ensure_capacity(event_init.fontfaces.size());
- for (auto const& font_face : event_init.fontfaces) {
- m_fontfaces.unchecked_append(font_face->state());
- }
-}
-
-FontFaceSetLoadEvent::FontFaceSetLoadEvent(Utf16FlyString const& event_name, Vector<NonnullRefPtr<FontFaceState>> fonts, HighResolutionTime::DOMHighResTimeStamp time_stamp)
- : FontFaceSetLoadEvent(event_name, Bindings::FontFaceSetLoadEventInit {}, time_stamp)
-{
- m_fontfaces = move(fonts);
-}
-
-GC::RootVector<GC::Ref<FontFace>> FontFaceSetLoadEvent::fontfaces() const
-{
- GC::RootVector<GC::Ref<FontFace>> faces;
- faces.ensure_capacity(m_fontfaces.size());
- for (auto const& font : m_fontfaces)
- faces.unchecked_append(font->cssom_font_face());
- return faces;
-}
-
-void FontFaceSetLoadEvent::visit_edges(Visitor& visitor)
-{
- Base::visit_edges(visitor);
- visitor.visit(m_fontfaces);
-}
-
-}
diff --git a/Libraries/LibWeb/CSS/FontFaceSetLoadEvent.h b/Libraries/LibWeb/CSS/FontFaceSetLoadEvent.h
deleted file mode 100644
index 08fcf631ce0..00000000000
--- a/Libraries/LibWeb/CSS/FontFaceSetLoadEvent.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Copyright (c) 2026, Tim Ledbetter <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/Vector.h>
-#include <LibGC/RootVector.h>
-#include <LibWeb/Bindings/FontFaceSetLoadEvent.h>
-#include <LibWeb/CSS/FontFaceState.h>
-#include <LibWeb/DOM/Event.h>
-#include <LibWebCommon/HighResolutionTime/DOMHighResTimeStamp.h>
-
-namespace Web::CSS {
-
-class FontFace;
-
-using FontFaceSetLoadEventInit = Bindings::FontFaceSetLoadEventInit;
-
-class FontFaceSetLoadEvent : public DOM::Event {
- WEB_WRAPPABLE(FontFaceSetLoadEvent, DOM::Event);
- GC_DECLARE_ALLOCATOR(FontFaceSetLoadEvent);
-
-public:
- [[nodiscard]] static GC::Ref<FontFaceSetLoadEvent> create(Utf16FlyString const& type, Bindings::FontFaceSetLoadEventInit const&, HighResolutionTime::DOMHighResTimeStamp);
- [[nodiscard]] static GC::Ref<FontFaceSetLoadEvent> create_for_fonts(Utf16FlyString const&, Vector<NonnullRefPtr<FontFaceState>>, HighResolutionTime::DOMHighResTimeStamp);
- static WebIDL::ExceptionOr<GC::Ref<FontFaceSetLoadEvent>> create_for_constructor(Utf16FlyString const& type, Bindings::FontFaceSetLoadEventInit const&, HighResolutionTime::DOMHighResTimeStamp);
-
- virtual ~FontFaceSetLoadEvent() override = default;
-
- GC::RootVector<GC::Ref<FontFace>> fontfaces() const;
-
-private:
- FontFaceSetLoadEvent(Utf16FlyString const& type, Bindings::FontFaceSetLoadEventInit const&, HighResolutionTime::DOMHighResTimeStamp);
- FontFaceSetLoadEvent(Utf16FlyString const&, Vector<NonnullRefPtr<FontFaceState>>, HighResolutionTime::DOMHighResTimeStamp);
-
- virtual void visit_edges(Visitor&) override;
-
- Vector<NonnullRefPtr<FontFaceState>> m_fontfaces;
-};
-
-}
diff --git a/Libraries/LibWeb/CSS/FontFaceSetLoadEvent.idl b/Libraries/LibWeb/CSS/FontFaceSetLoadEvent.idl
index c2e6efc098d..0d3b932df34 100644
--- a/Libraries/LibWeb/CSS/FontFaceSetLoadEvent.idl
+++ b/Libraries/LibWeb/CSS/FontFaceSetLoadEvent.idl
@@ -5,8 +5,8 @@ dictionary FontFaceSetLoadEventInit : EventInit {
// https://drafts.csswg.org/css-font-loading/#fontfacesetloadevent
-[Exposed=(Window,Worker)]
+[Exposed=(Window,Worker), ImplementedIn=Rust]
interface FontFaceSetLoadEvent : Event {
- constructor([FlyString] CSSOMString type, optional FontFaceSetLoadEventInit eventInitDict = {});
+ constructor(CSSOMString type, optional FontFaceSetLoadEventInit eventInitDict = {});
[SameObject] readonly attribute FrozenArray<FontFace> fontfaces;
};