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::rc::Rc;
6
7use dom_struct::dom_struct;
8use fonts::FontContextWebFontMethods;
9use js::rust::HandleObject;
10
11use super::fontface::FontFace;
12use crate::dom::bindings::codegen::Bindings::FontFaceSetBinding::FontFaceSetMethods;
13use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
14use crate::dom::bindings::refcounted::TrustedPromise;
15use crate::dom::bindings::reflector::{DomGlobal, reflect_dom_object_with_proto};
16use crate::dom::bindings::root::DomRoot;
17use crate::dom::bindings::str::DOMString;
18use crate::dom::eventtarget::EventTarget;
19use crate::dom::globalscope::GlobalScope;
20use crate::dom::promise::Promise;
21use crate::dom::window::Window;
22use crate::script_runtime::CanGc;
23
24/// <https://drafts.csswg.org/css-font-loading/#FontFaceSet-interface>
25#[dom_struct]
26pub(crate) struct FontFaceSet {
27    target: EventTarget,
28
29    /// <https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-readypromise-slot>
30    #[conditional_malloc_size_of]
31    promise: Rc<Promise>,
32}
33
34impl FontFaceSet {
35    fn new_inherited(global: &GlobalScope, can_gc: CanGc) -> Self {
36        FontFaceSet {
37            target: EventTarget::new_inherited(),
38            promise: Promise::new(global, can_gc),
39        }
40    }
41
42    pub(crate) fn new(
43        global: &GlobalScope,
44        proto: Option<HandleObject>,
45        can_gc: CanGc,
46    ) -> DomRoot<Self> {
47        reflect_dom_object_with_proto(
48            Box::new(FontFaceSet::new_inherited(global, can_gc)),
49            global,
50            proto,
51            can_gc,
52        )
53    }
54
55    pub(super) fn handle_font_face_status_changed(&self, font_face: &FontFace) {
56        if font_face.loaded() {
57            let Some(window) = DomRoot::downcast::<Window>(self.global()) else {
58                return;
59            };
60
61            let (family_name, template) = font_face
62                .template()
63                .expect("A loaded web font should have a template");
64            window
65                .font_context()
66                .add_template_to_font_context(family_name, template);
67            window.Document().dirty_all_nodes();
68        }
69    }
70
71    /// Fulfill the font ready promise, returning true if it was not already fulfilled beforehand.
72    pub(crate) fn fulfill_ready_promise_if_needed(&self, can_gc: CanGc) -> bool {
73        if self.promise.is_fulfilled() {
74            return false;
75        }
76        self.promise.resolve_native(self, can_gc);
77        true
78    }
79
80    pub(crate) fn waiting_to_fullfill_promise(&self) -> bool {
81        !self.promise.is_fulfilled()
82    }
83}
84
85impl FontFaceSetMethods<crate::DomTypeHolder> for FontFaceSet {
86    /// <https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-ready>
87    fn Ready(&self) -> Rc<Promise> {
88        self.promise.clone()
89    }
90
91    /// <https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-add>
92    fn Add(&self, font_face: &FontFace) -> DomRoot<FontFaceSet> {
93        font_face.set_associated_font_face_set(self);
94        self.handle_font_face_status_changed(font_face);
95        DomRoot::from_ref(self)
96    }
97
98    /// <https://drafts.csswg.org/css-font-loading/#dom-fontfaceset-load>
99    fn Load(&self, _font: DOMString, _text: DOMString, can_gc: CanGc) -> Rc<Promise> {
100        // Step 1. Let font face set be the FontFaceSet object this method was called on. Let
101        // promise be a newly-created promise object.
102        let promise = Promise::new(&self.global(), can_gc);
103
104        // TODO: Step 3. Find the matching font faces from font face set using the font and text
105        // arguments passed to the function, and let font face list be the return value (ignoring
106        // the found faces flag). If a syntax error was returned, reject promise with a SyntaxError
107        // exception and terminate these steps.
108
109        let trusted = TrustedPromise::new(promise.clone());
110        // Step 4. Queue a task to run the following steps synchronously:
111        self.global()
112            .task_manager()
113            .font_loading_task_source()
114            .queue(task!(resolve_font_face_set_load_task: move || {
115                let promise = trusted.root();
116
117                // TODO: Step 4.1. For all of the font faces in the font face list, call their load()
118                // method.
119
120                // TODO: Step 4.2. Resolve promise with the result of waiting for all of the
121                // [[FontStatusPromise]]s of each font face in the font face list, in order.
122                let matched_fonts = Vec::<&FontFace>::new();
123                promise.resolve_native(&matched_fonts, CanGc::note());
124            }));
125
126        // Step 2. Return promise. Complete the rest of these steps asynchronously.
127        promise
128    }
129}