Skip to content

Truly independent web browser

LibWeb: Start FontFaceSet font loads in the current task

Timothy Flynnd37ed25a57bb
diff --git a/Libraries/LibWeb/Bindings/Rust/src/css/font_face_set.rs b/Libraries/LibWeb/Bindings/Rust/src/css/font_face_set.rs
index e60344543bf..f939fd0f379 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/css/font_face_set.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/css/font_face_set.rs
@@ -15,10 +15,7 @@ use core::ptr::NonNull;
use std::collections::{HashMap, HashSet};
use ak::Utf16String;
-use libjs_rust::api::{
- Array, Gc, GcRefCell, MarkedVec, Must, Object, PrimitiveString, PropertyKey, Realm, Set, Trace, Value, Visitor, Vm,
- length_of_array_like,
-};
+use libjs_rust::api::{Array, Gc, GcRefCell, MarkedVec, Object, Realm, Set, Trace, Value, Visitor, Vm};
use libweb_rust::typed_om::font_family_names;
use super::css_rule::wrap_cpp_cell;
@@ -31,8 +28,8 @@ use crate::bindings::interface::{
with_temporary_execution_context_and_callbacks,
};
use crate::bindings::promise::{
- TaskSource, WebIdlPromise, create_promise, get_promise_for_wait_for_all, in_parallel, is_promise_fulfilled,
- promise_from_abi, queue_a_task, react_to_promise, reject_promise, reject_promise_with_exception, resolve_promise,
+ TaskSource, WebIdlPromise, create_promise, get_promise_for_wait_for_all, is_promise_fulfilled, promise_from_abi,
+ queue_a_task, react_to_promise, reject_promise, reject_promise_with_exception, resolve_promise,
};
use crate::bindings::setlike::context_of_set;
use crate::bindings::setlike::{Setlike, WorldEntries};
@@ -594,80 +591,48 @@ impl FontFaceSet {
// promise object.
let promise = create_promise(vm, realm);
- // NB: What steps capture is copied, so the strings travel as JS strings.
- let font = PrimitiveString::create(vm, font);
- let text = PrimitiveString::create(vm, text);
- in_parallel(
- vm,
- realm,
- (self.this(), promise, font, text),
- |vm, (this, promise, font, text)| {
- let realm = this.realm;
-
- // 3. Find the matching font faces from font face set using the font and text arguments passed to the
- // function, and let font face list be the return value (ignoring the found faces flag). If a syntax
- // error was returned, reject promise with a SyntaxError exception and terminate these steps.
- let font_face_list = match this.find_matching_font_faces(vm, &font.utf16_string(), &text.utf16_string())
- {
- Ok(font_face_list) => font_face_list,
- Err(exception) => {
- with_temporary_execution_context_and_callbacks(realm, || {
- reject_promise_with_exception(vm, promise, exception);
- });
- return;
- }
- };
-
- // NB: The list travels as the array that promise is resolved with, of the wrappers of the font faces.
- let cx = CallContext::new(vm, realm);
- let wrappers = MarkedVec::with_capacity(vm, font_face_list.len());
- for font_face in font_face_list.values().iter() {
- wrappers.push(font_face.wrapper(&cx));
- }
- let font_face_list = Array::create_from(vm, realm, &wrappers.to_vec());
-
- // 4. Queue a task to run the following steps synchronously:
- queue_a_task(
- vm,
- realm,
- TaskSource::FontLoading,
- (realm, promise, font_face_list),
- |vm, (realm, promise, font_face_list)| {
- // 1. For all of the font faces in the font face list, call their load() method.
- let promises = MarkedVec::new(vm);
- let font_face_list_object = font_face_list.upcast::<Object>();
- let length = length_of_array_like(vm, &font_face_list_object).must();
- for index in 0..length {
- let wrapper =
- font_face_list_object.get_without_side_effects(vm, &PropertyKey::from(index as u32));
- promises.push(CppFontFaceState::of_wrapper(wrapper.as_object()).load());
- }
-
- // 2. Resolve promise with the result of waiting for all of the [[FontStatusPromise]]s of each font
- // face in the font face list, in order.
- with_temporary_execution_context_and_callbacks(realm, || {
- react_to_promise(
- vm,
- get_promise_for_wait_for_all(vm, realm, &promises.values()),
- (promise, font_face_list),
- Some(|cx, (promise, font_face_list), _| {
- resolve_promise(
- cx.vm(),
- promise,
- Value::from_object(font_face_list.upcast::<Object>()),
- );
- Ok(Value::UNDEFINED)
- }),
- Some(|cx, (promise, _), reason| {
- reject_promise(cx.vm(), promise, reason);
- Ok(Value::UNDEFINED)
- }),
- );
- });
- },
- );
- },
- );
+ // 3. Find the matching font faces from font face set using the font and text arguments passed to the function,
+ // and let font face list be the return value (ignoring the found faces flag). If a syntax error was
+ // returned, reject promise with a SyntaxError exception and terminate these steps.
+ let font_face_list = match self.find_matching_font_faces(vm, &font, &text) {
+ Ok(font_face_list) => font_face_list,
+ Err(exception) => {
+ reject_promise_with_exception(vm, promise, exception);
+ return Ok(promise);
+ }
+ };
+
+ // 4. Queue a task to run the following steps synchronously:
+ // AD-HOC: Firefox and Chromium both match faces and start loading them in the current task. The web relies on
+ // this behavior, e.g. Instagram's story viewer. See the following spec issue for more details:
+ // https://github.com/w3c/csswg-drafts/issues/14804
+ {
+ // 1. For all of the font faces in the font face list, call their load() method.
+ let wrappers = MarkedVec::with_capacity(vm, font_face_list.len());
+ let promises = MarkedVec::with_capacity(vm, font_face_list.len());
+ let cx = CallContext::new(vm, realm);
+ for font_face in font_face_list.values().iter() {
+ wrappers.push(font_face.wrapper(&cx));
+ promises.push(font_face.load());
+ }
+ let font_face_list = Array::create_from(vm, realm, &wrappers.to_vec());
+
+ // 2. Resolve promise with the result of waiting for all of the [[FontStatusPromise]]s of each font face in
+ // the font face list, in order.
+ react_to_promise(
+ vm,
+ get_promise_for_wait_for_all(vm, realm, &promises.values()),
+ (promise, font_face_list),
+ Some(|cx, (promise, font_face_list), _| {
+ resolve_promise(cx.vm(), promise, Value::from_object(font_face_list.upcast::<Object>()));
+ Ok(Value::UNDEFINED)
+ }),
+ Some(|cx, (promise, _), reason| {
+ reject_promise(cx.vm(), promise, reason);
+ Ok(Value::UNDEFINED)
+ }),
+ );
+ }
// 2. Return promise. Complete the rest of these steps asynchronously.
Ok(promise)
diff --git a/Tests/LibWeb/Text/expected/css/FontFaceSet-load-settled-fonts.txt b/Tests/LibWeb/Text/expected/css/FontFaceSet-load-settled-fonts.txt
new file mode 100644
index 00000000000..7eeb05dc80c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/FontFaceSet-load-settled-fonts.txt
@@ -0,0 +1,8 @@
+Loaded font fulfilled within microtasks: true
+Loaded font returned matching faces in order: true
+Multiple loaded fonts fulfilled within microtasks: true
+Multiple loaded fonts returned matching faces in order: true
+Excluded unicode range fulfilled within microtasks: true
+Excluded unicode range returned matching faces in order: true
+System font fulfilled within microtasks: true
+System font returned matching faces in order: true
diff --git a/Tests/LibWeb/Text/input/css/FontFaceSet-load-settled-fonts.html b/Tests/LibWeb/Text/input/css/FontFaceSet-load-settled-fonts.html
new file mode 100644
index 00000000000..d13102dd9a9
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/FontFaceSet-load-settled-fonts.html
@@ -0,0 +1,35 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ const first = new FontFace("SettledFont", "url(../../../Assets/HashSans.woff)", { unicodeRange: "U+0041" });
+ const second = new FontFace("SettledFont", "url(../../../Assets/HashSans.woff)", { unicodeRange: "U+0042" });
+ document.fonts.add(first);
+ document.fonts.add(second);
+ await Promise.all([first.load(), second.load()]);
+
+ async function checkLoad(label, font, text, expectedFaces) {
+ let fulfilled = false;
+ const promise = document.fonts.load(font, text).then(faces => {
+ fulfilled = true;
+ return faces;
+ });
+
+ // Allow promise reactions to finish without giving a font-loading task a turn.
+ for (let i = 0; i < 10; ++i) {
+ await Promise.resolve();
+ }
+ println(`${label} fulfilled within microtasks: ${fulfilled}`);
+
+ const faces = await promise;
+ println(
+ `${label} returned matching faces in order: ${faces.length === expectedFaces.length && faces.every((face, i) => face === expectedFaces[i])}`
+ );
+ }
+
+ await checkLoad("Loaded font", "16px SettledFont", "A", [first]);
+ await checkLoad("Multiple loaded fonts", "16px SettledFont", "AB", [first, second]);
+ await checkLoad("Excluded unicode range", "16px SettledFont", "C", []);
+ await checkLoad("System font", "16px sans-serif", "A", []);
+ });
+</script>