Skip to main content

script/dom/canvas/2d/
textmetrics.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 dom_struct::dom_struct;
6use js::context::JSContext;
7use script_bindings::reflector::{Reflector, reflect_dom_object_with_cx};
8
9use crate::dom::bindings::codegen::Bindings::TextMetricsBinding::TextMetricsMethods;
10use crate::dom::bindings::num::Finite;
11use crate::dom::bindings::root::DomRoot;
12use crate::dom::globalscope::GlobalScope;
13
14#[dom_struct]
15#[expect(non_snake_case)]
16pub(crate) struct TextMetrics {
17    reflector_: Reflector,
18    width: Finite<f64>,
19    actualBoundingBoxLeft: Finite<f64>,
20    actualBoundingBoxRight: Finite<f64>,
21    fontBoundingBoxAscent: Finite<f64>,
22    fontBoundingBoxDescent: Finite<f64>,
23    actualBoundingBoxAscent: Finite<f64>,
24    actualBoundingBoxDescent: Finite<f64>,
25    emHeightAscent: Finite<f64>,
26    emHeightDescent: Finite<f64>,
27    hangingBaseline: Finite<f64>,
28    alphabeticBaseline: Finite<f64>,
29    ideographicBaseline: Finite<f64>,
30}
31
32#[expect(non_snake_case)]
33impl TextMetrics {
34    #[expect(clippy::too_many_arguments)]
35    fn new_inherited(
36        width: f64,
37        actualBoundingBoxLeft: f64,
38        actualBoundingBoxRight: f64,
39        fontBoundingBoxAscent: f64,
40        fontBoundingBoxDescent: f64,
41        actualBoundingBoxAscent: f64,
42        actualBoundingBoxDescent: f64,
43        emHeightAscent: f64,
44        emHeightDescent: f64,
45        hangingBaseline: f64,
46        alphabeticBaseline: f64,
47        ideographicBaseline: f64,
48    ) -> TextMetrics {
49        TextMetrics {
50            reflector_: Reflector::new(),
51            width: Finite::wrap(width),
52            actualBoundingBoxLeft: Finite::wrap(actualBoundingBoxLeft),
53            actualBoundingBoxRight: Finite::wrap(actualBoundingBoxRight),
54            fontBoundingBoxAscent: Finite::wrap(fontBoundingBoxAscent),
55            fontBoundingBoxDescent: Finite::wrap(fontBoundingBoxDescent),
56            actualBoundingBoxAscent: Finite::wrap(actualBoundingBoxAscent),
57            actualBoundingBoxDescent: Finite::wrap(actualBoundingBoxDescent),
58            emHeightAscent: Finite::wrap(emHeightAscent),
59            emHeightDescent: Finite::wrap(emHeightDescent),
60            hangingBaseline: Finite::wrap(hangingBaseline),
61            alphabeticBaseline: Finite::wrap(alphabeticBaseline),
62            ideographicBaseline: Finite::wrap(ideographicBaseline),
63        }
64    }
65
66    #[expect(clippy::too_many_arguments)]
67    pub(crate) fn new(
68        global: &GlobalScope,
69        cx: &mut JSContext,
70        width: f64,
71        actualBoundingBoxLeft: f64,
72        actualBoundingBoxRight: f64,
73        fontBoundingBoxAscent: f64,
74        fontBoundingBoxDescent: f64,
75        actualBoundingBoxAscent: f64,
76        actualBoundingBoxDescent: f64,
77        emHeightAscent: f64,
78        emHeightDescent: f64,
79        hangingBaseline: f64,
80        alphabeticBaseline: f64,
81        ideographicBaseline: f64,
82    ) -> DomRoot<TextMetrics> {
83        reflect_dom_object_with_cx(
84            Box::new(TextMetrics::new_inherited(
85                width,
86                actualBoundingBoxLeft,
87                actualBoundingBoxRight,
88                fontBoundingBoxAscent,
89                fontBoundingBoxDescent,
90                actualBoundingBoxAscent,
91                actualBoundingBoxDescent,
92                emHeightAscent,
93                emHeightDescent,
94                hangingBaseline,
95                alphabeticBaseline,
96                ideographicBaseline,
97            )),
98            global,
99            cx,
100        )
101    }
102}
103
104impl TextMetricsMethods<crate::DomTypeHolder> for TextMetrics {
105    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-width>
106    fn Width(&self) -> Finite<f64> {
107        self.width
108    }
109
110    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-actualboundingboxleft>
111    fn ActualBoundingBoxLeft(&self) -> Finite<f64> {
112        self.actualBoundingBoxLeft
113    }
114
115    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-actualboundingboxright>
116    fn ActualBoundingBoxRight(&self) -> Finite<f64> {
117        self.actualBoundingBoxRight
118    }
119
120    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-fontboundingboxascent>
121    fn FontBoundingBoxAscent(&self) -> Finite<f64> {
122        self.fontBoundingBoxAscent
123    }
124
125    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-fontboundingboxascent>
126    fn FontBoundingBoxDescent(&self) -> Finite<f64> {
127        self.fontBoundingBoxDescent
128    }
129
130    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-actualboundingboxascent>
131    fn ActualBoundingBoxAscent(&self) -> Finite<f64> {
132        self.actualBoundingBoxAscent
133    }
134
135    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-actualboundingboxdescent>
136    fn ActualBoundingBoxDescent(&self) -> Finite<f64> {
137        self.actualBoundingBoxDescent
138    }
139
140    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-emheightascent>
141    fn EmHeightAscent(&self) -> Finite<f64> {
142        self.emHeightAscent
143    }
144
145    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-emheightdescent>
146    fn EmHeightDescent(&self) -> Finite<f64> {
147        self.emHeightDescent
148    }
149
150    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-hangingbaseline>
151    fn HangingBaseline(&self) -> Finite<f64> {
152        self.hangingBaseline
153    }
154
155    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-alphabeticbaseline>
156    fn AlphabeticBaseline(&self) -> Finite<f64> {
157        self.alphabeticBaseline
158    }
159
160    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-ideographicbaseline>
161    fn IdeographicBaseline(&self) -> Finite<f64> {
162        self.ideographicBaseline
163    }
164}