Skip to main content

script/dom/css/
fontfaceset.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::cell::RefCell;
6use std::rc::Rc;
7
8use dom_struct::dom_struct;
9use js::context::JSContext;
10use js::gc::Handle;
11use js::jsapi::Value;
12use js::realm::CurrentRealm;
13use js::rust::HandleObject;
14use script_bindings::cell::DomRefCell;
15use script_bindings::codegen::GenericBindings::FontFaceBinding::{
16    FontFaceLoadStatus, FontFaceMethods,
17};
18use script_bindings::like::Setlike;
19use script_bindings::reflector::reflect_dom_object_with_proto_and_cx;
20
21use crate::dom::bindings::codegen::Bindings::FontFaceSetBinding::FontFaceSetMethods;
22use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
23use crate::dom::bindings::refcounted::TrustedPromise;
24use crate::dom::bindings::reflector::DomGlobal;
25use crate::dom::bindings::root::{Dom, DomRoot};
26use crate::dom::bindings::str::DOMString;
27use crate::dom::eventtarget::EventTarget;
28use crate::dom::fontface::FontFace;
29use crate::dom::globalscope::GlobalScope;
30use crate::dom::promise::Promise;
31use crate::dom::promisenativehandler::Callback;
32use crate::dom::types::PromiseNativeHandler;
33use crate::dom::window::Window;
34use crate::realms::enter_auto_realm;
35
36/// <https://drafts.csswg.org/css-font-loading/#FontFaceSet-interface>
37#[dom_struct]
38pub(crate) struct FontFaceSet {
39    target: EventTarget,
40
41    /// <https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-readypromise-slot>
42    #[conditional_malloc_size_of]
43    promise: RefCell<Rc<Promise>>,
44
45    set_entries: DomRefCell<Vec<Dom<FontFace>>>,
46}
47
48impl FontFaceSet {
49    fn new_inherited(cx: &mut JSContext, global: &GlobalScope) -> Self {
50        FontFaceSet {
51            target: EventTarget::new_inherited(),
52            promise: Promise::new(cx, global).into(),
53            set_entries: Default::default(),
54        }
55    }
56
57    pub(crate) fn new(
58        cx: &mut JSContext,
59        global: &GlobalScope,
60        proto: Option<HandleObject>,
61    ) -> DomRoot<Self> {
62        reflect_dom_object_with_proto_and_cx(
63            Box::new(FontFaceSet::new_inherited(cx, global)),
64            global,
65            proto,
66            cx,
67        )
68    }
69
70    pub(super) fn handle_font_face_status_changed(&self, cx: &mut JSContext, font_face: &FontFace) {
71        match font_face.Status() {
72            FontFaceLoadStatus::Loading => {
73                self.switch_to_loading(cx);
74            },
75            FontFaceLoadStatus::Loaded => {
76                let Some(window) = DomRoot::downcast::<Window>(self.global()) else {
77                    return;
78                };
79
80                let (family_name, template) = font_face
81                    .template()
82                    .expect("A loaded web font should have a template");
83                window
84                    .font_context()
85                    .add_template_to_font_context(family_name, template);
86                window.Document().dirty_all_nodes();
87            },
88            _ => {},
89        }
90    }
91
92    /// Fulfill the font ready promise, returning true if it was not already fulfilled beforehand.
93    pub(crate) fn fulfill_ready_promise_if_needed(&self, cx: &mut JSContext) -> bool {
94        let promise = self.promise.borrow().clone();
95        if promise.is_fulfilled() {
96            return false;
97        }
98        promise.resolve_native(cx, self);
99        true
100    }
101
102    pub(crate) fn waiting_to_fullfill_promise(&self) -> bool {
103        !self.promise.borrow().is_fulfilled()
104    }
105
106    fn contains_face(&self, target: &FontFace) -> bool {
107        self.set_entries
108            .borrow()
109            .iter()
110            .any(|face| &**face == target)
111    }
112
113    /// Removes a face from the set's set entries.
114    fn delete_face(&self, target: &FontFace) -> bool {
115        let mut set_entries = self.set_entries.borrow_mut();
116        let Some(index) = set_entries.iter().position(|face| &**face == target) else {
117            return false;
118        };
119        set_entries.remove(index);
120        true
121    }
122
123    /// <https://drafts.csswg.org/css-font-loading/#switch-the-fontfaceset-to-loading>
124    pub(crate) fn switch_to_loading(&self, cx: &mut JSContext) {
125        // Step 1. Let font face set be the given FontFaceSet.
126        // Note: This is self.
127
128        // Step 2. Set the status attribute of font face set to "loading".
129        // TODO: Implement the FontFaceSet status attribute.
130
131        // Step 3. If font face set’s [[ReadyPromise]] slot currently holds a fulfilled
132        // promise, replace it with a fresh pending promise.
133        if self.promise.borrow().is_fulfilled() {
134            *self.promise.borrow_mut() = Promise::new(cx, &self.global());
135        }
136
137        // Step 4. Queue a task to fire a font load event named loading at font face set.
138        // TODO: Implement support for font loading events.
139    }
140}
141
142impl FontFaceSetMethods<crate::DomTypeHolder> for FontFaceSet {
143    /// <https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-ready>
144    fn Ready(&self) -> Rc<Promise> {
145        self.promise.borrow().clone()
146    }
147
148    /// <https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-add>
149    fn Add(&self, cx: &mut JSContext, font_face: &FontFace) -> DomRoot<FontFaceSet> {
150        // Step 1. If font is already in the FontFaceSet’s set entries,
151        // skip to the last step of this algorithm immediately.
152        if self.contains_face(font_face) {
153            return DomRoot::from_ref(self);
154        }
155
156        // TODO: Step 2. If font is CSS-connected, throw an InvalidModificationError
157        // exception and exit this algorithm immediately.
158
159        // Step 3. Add the font argument to the FontFaceSet’s set entries.
160        self.set_entries.borrow_mut().push(Dom::from_ref(font_face));
161        font_face.set_associated_font_face_set(self);
162
163        // Step 4. If font’s status attribute is "loading":
164        // Step 4.1 If the FontFaceSet’s [[LoadingFonts]] list is empty, switch the FontFaceSet to loading.
165        // Step 4.2 Append font to the FontFaceSet’s [[LoadingFonts]] list.
166        self.handle_font_face_status_changed(cx, font_face);
167
168        // Step 5. Return the FontFaceSet.
169        DomRoot::from_ref(self)
170    }
171
172    /// <https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-delete>
173    fn Delete(&self, to_delete: &FontFace) -> bool {
174        // TODO Step 1. If font is CSS-connected, return false and exit this algorithm immediately.
175
176        // Step 2. Let deleted be the result of removing font from the FontFaceSet’s set entries.
177        // TODO: Step 3. If font is present in the FontFaceSet’s [[LoadedFonts]], or [[FailedFonts]] lists, remove it.
178        // TODO: Step 4. If font is present in the FontFaceSet’s [[LoadingFonts]] list, remove it. If font was the last
179        // item in that list (and so the list is now empty), switch the FontFaceSet to loaded.
180        // Step 5. Return deleted.
181        self.delete_face(to_delete)
182    }
183
184    /// <https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-clear>
185    fn Clear(&self) {
186        // Step 1. Remove all non-CSS-connected items from the FontFaceSet’s set entries,
187        // its [[LoadedFonts]] list, and its [[FailedFonts]] list.
188        self.set_entries.borrow_mut().clear();
189
190        // TODO Step 2. If the FontFaceSet’s [[LoadingFonts]] list is non-empty, remove all items from it,
191        // then switch the FontFaceSet to loaded.
192    }
193
194    /// <https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-load>
195    fn Load(&self, cx: &mut JSContext, _font: DOMString, _text: DOMString) -> Rc<Promise> {
196        // Step 1. Let font face set be the FontFaceSet object this method was called on. Let
197        // promise be a newly-created promise object.
198        let load_promise = Promise::new(cx, &self.global());
199
200        // Step 3. Find the matching font faces from font face set using the font and text
201        // arguments passed to the function, and let font face list be the return value (ignoring
202        // the found faces flag). If a syntax error was returned, reject promise with a SyntaxError
203        // exception and terminate these steps.
204        //
205        // TODO: Implement this.
206
207        #[derive(MallocSizeOf, JSTraceable)]
208        struct LoadPromiseFulfillmentHandler {
209            #[conditional_malloc_size_of]
210            load_promise: Rc<Promise>,
211        }
212        impl Callback for LoadPromiseFulfillmentHandler {
213            fn callback(&self, cx: &mut CurrentRealm, _: Handle<Value>) {
214                self.load_promise
215                    .resolve_native(cx, &Vec::<&FontFace>::new());
216            }
217        }
218
219        // Step 4. Queue a task to run the following steps synchronously:
220        let trusted_ready_promise = TrustedPromise::new(self.promise.borrow().clone());
221        let trusted_load_promise = TrustedPromise::new(load_promise.clone());
222        self.global()
223            .task_manager()
224            .font_loading_task_source()
225            .queue(task!(resolve_font_face_set_load_task: move |cx| {
226                let ready_promise = trusted_ready_promise.root();
227                let load_promise = trusted_load_promise.root();
228
229                // Step 4.1. For all of the font faces in the font face list, call their load()
230                // method.
231                // Step 4.2. Resolve promise with the result of waiting for all of the
232                // [[FontStatusPromise]]s of each font face in the font face list, in order.
233                //
234                // TODO: These steps are not implemented. Instead we wait until all fonts
235                // are loaded by resolving the returned promise when
236                // `document.fonts.ready` is resolved. The return list of fonts will not
237                // be correct, but any code that waits on the promise will have
238                // conservatively consistent behavior. This is important for preventing
239                // intermittent results in WPT tests.
240                let global = ready_promise.global();
241                let handler = PromiseNativeHandler::new(
242                    cx,
243                    &global,
244                    Some(Box::new(LoadPromiseFulfillmentHandler {
245                        load_promise,
246                    })),
247                    None,
248                );
249
250                let mut realm = enter_auto_realm(cx, &*global);
251                ready_promise.append_native_handler(&mut realm.current_realm(), &handler);
252            }));
253
254        // Step 2. Return promise. Complete the rest of these steps asynchronously.
255        load_promise
256    }
257
258    /// <https://html.spec.whatwg.org/multipage/#customstateset>
259    fn Size(&self) -> u32 {
260        self.set_entries.borrow().len() as u32
261    }
262}
263
264impl Setlike for FontFaceSet {
265    type Key = DomRoot<FontFace>;
266
267    #[inline(always)]
268    fn get_index(&self, index: u32) -> Option<Self::Key> {
269        self.set_entries
270            .borrow()
271            .get(index as usize)
272            .map(|face| face.as_rooted())
273    }
274
275    #[inline(always)]
276    fn size(&self) -> u32 {
277        self.set_entries.borrow().len() as u32
278    }
279
280    #[inline(always)]
281    fn add(&self, face: Self::Key) {
282        self.set_entries.borrow_mut().push(face.as_traced());
283    }
284
285    #[inline(always)]
286    fn has(&self, target: Self::Key) -> bool {
287        self.contains_face(&target)
288    }
289
290    #[inline(always)]
291    fn clear(&self) {
292        self.set_entries.borrow_mut().clear();
293    }
294
295    #[inline(always)]
296    fn delete(&self, to_delete: Self::Key) -> bool {
297        self.delete_face(&to_delete)
298    }
299}