script/dom/css/
cssfontfacerule.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;
6
7use dom_struct::dom_struct;
8use servo_arc::Arc;
9use style::shared_lock::{Locked, ToCssWithGuard};
10use style::stylesheets::{CssRuleType, FontFaceRule};
11
12use super::cssrule::{CSSRule, SpecificCSSRule};
13use super::cssstylesheet::CSSStyleSheet;
14use crate::dom::bindings::reflector::reflect_dom_object;
15use crate::dom::bindings::root::DomRoot;
16use crate::dom::bindings::str::DOMString;
17use crate::dom::window::Window;
18use crate::script_runtime::CanGc;
19
20#[dom_struct]
21pub(crate) struct CSSFontFaceRule {
22    cssrule: CSSRule,
23    #[ignore_malloc_size_of = "Stylo"]
24    #[no_trace]
25    fontfacerule: RefCell<Arc<Locked<FontFaceRule>>>,
26}
27
28impl CSSFontFaceRule {
29    fn new_inherited(
30        parent_stylesheet: &CSSStyleSheet,
31        fontfacerule: Arc<Locked<FontFaceRule>>,
32    ) -> CSSFontFaceRule {
33        CSSFontFaceRule {
34            cssrule: CSSRule::new_inherited(parent_stylesheet),
35            fontfacerule: RefCell::new(fontfacerule),
36        }
37    }
38
39    pub(crate) fn new(
40        window: &Window,
41        parent_stylesheet: &CSSStyleSheet,
42        fontfacerule: Arc<Locked<FontFaceRule>>,
43        can_gc: CanGc,
44    ) -> DomRoot<CSSFontFaceRule> {
45        reflect_dom_object(
46            Box::new(CSSFontFaceRule::new_inherited(
47                parent_stylesheet,
48                fontfacerule,
49            )),
50            window,
51            can_gc,
52        )
53    }
54
55    pub(crate) fn update_rule(&self, fontfacerule: Arc<Locked<FontFaceRule>>) {
56        *self.fontfacerule.borrow_mut() = fontfacerule;
57    }
58}
59
60impl SpecificCSSRule for CSSFontFaceRule {
61    fn ty(&self) -> CssRuleType {
62        CssRuleType::FontFace
63    }
64
65    fn get_css(&self) -> DOMString {
66        let guard = self.cssrule.shared_lock().read();
67        self.fontfacerule
68            .borrow()
69            .read_with(&guard)
70            .to_css_string(&guard)
71            .into()
72    }
73}