Skip to main content

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