script/dom/
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;
6
7use crate::dom::bindings::codegen::Bindings::TextMetricsBinding::TextMetricsMethods;
8use crate::dom::bindings::num::Finite;
9use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
10use crate::dom::bindings::root::DomRoot;
11use crate::dom::globalscope::GlobalScope;
12use crate::script_runtime::CanGc;
13
14#[dom_struct]
15#[allow(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#[allow(non_snake_case)]
33impl TextMetrics {
34    #[allow(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    #[allow(clippy::too_many_arguments)]
67    pub(crate) fn new(
68        global: &GlobalScope,
69        width: f64,
70        actualBoundingBoxLeft: f64,
71        actualBoundingBoxRight: f64,
72        fontBoundingBoxAscent: f64,
73        fontBoundingBoxDescent: f64,
74        actualBoundingBoxAscent: f64,
75        actualBoundingBoxDescent: f64,
76        emHeightAscent: f64,
77        emHeightDescent: f64,
78        hangingBaseline: f64,
79        alphabeticBaseline: f64,
80        ideographicBaseline: f64,
81        can_gc: CanGc,
82    ) -> DomRoot<TextMetrics> {
83        reflect_dom_object(
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            can_gc,
100        )
101    }
102
103    pub(crate) fn default(global: &GlobalScope, can_gc: CanGc) -> DomRoot<Self> {
104        reflect_dom_object(
105            Box::new(Self {
106                reflector_: Reflector::new(),
107                width: Default::default(),
108                actualBoundingBoxLeft: Default::default(),
109                actualBoundingBoxRight: Default::default(),
110                fontBoundingBoxAscent: Default::default(),
111                fontBoundingBoxDescent: Default::default(),
112                actualBoundingBoxAscent: Default::default(),
113                actualBoundingBoxDescent: Default::default(),
114                emHeightAscent: Default::default(),
115                emHeightDescent: Default::default(),
116                hangingBaseline: Default::default(),
117                alphabeticBaseline: Default::default(),
118                ideographicBaseline: Default::default(),
119            }),
120            global,
121            can_gc,
122        )
123    }
124}
125
126impl TextMetricsMethods<crate::DomTypeHolder> for TextMetrics {
127    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-width>
128    fn Width(&self) -> Finite<f64> {
129        self.width
130    }
131
132    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-actualboundingboxleft>
133    fn ActualBoundingBoxLeft(&self) -> Finite<f64> {
134        self.actualBoundingBoxLeft
135    }
136
137    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-actualboundingboxright>
138    fn ActualBoundingBoxRight(&self) -> Finite<f64> {
139        self.actualBoundingBoxRight
140    }
141
142    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-fontboundingboxascent>
143    fn FontBoundingBoxAscent(&self) -> Finite<f64> {
144        self.fontBoundingBoxAscent
145    }
146
147    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-fontboundingboxascent>
148    fn FontBoundingBoxDescent(&self) -> Finite<f64> {
149        self.fontBoundingBoxDescent
150    }
151
152    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-actualboundingboxascent>
153    fn ActualBoundingBoxAscent(&self) -> Finite<f64> {
154        self.actualBoundingBoxAscent
155    }
156
157    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-actualboundingboxdescent>
158    fn ActualBoundingBoxDescent(&self) -> Finite<f64> {
159        self.actualBoundingBoxDescent
160    }
161
162    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-emheightascent>
163    fn EmHeightAscent(&self) -> Finite<f64> {
164        self.emHeightAscent
165    }
166
167    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-emheightdescent>
168    fn EmHeightDescent(&self) -> Finite<f64> {
169        self.emHeightDescent
170    }
171
172    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-hangingbaseline>
173    fn HangingBaseline(&self) -> Finite<f64> {
174        self.hangingBaseline
175    }
176
177    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-alphabeticbaseline>
178    fn AlphabeticBaseline(&self) -> Finite<f64> {
179        self.alphabeticBaseline
180    }
181
182    /// <https://html.spec.whatwg.org/multipage/#dom-textmetrics-ideographicbaseline>
183    fn IdeographicBaseline(&self) -> Finite<f64> {
184        self.ideographicBaseline
185    }
186}