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