Skip to main content

style/device/
mod.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//! Media-query device and expression representation.
6
7use crate::color::AbsoluteColor;
8use crate::custom_properties::CssEnvironment;
9#[cfg(feature = "servo")]
10use crate::derives::*;
11use crate::properties::ComputedValues;
12use crate::values::computed::font::QueryFontMetricsFlags;
13use crate::values::computed::Length;
14use parking_lot::RwLock;
15use servo_arc::Arc;
16use std::mem;
17use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
18
19#[cfg(feature = "gecko")]
20use crate::device::gecko::ExtraDeviceData;
21#[cfg(feature = "servo")]
22use crate::device::servo::ExtraDeviceData;
23
24#[cfg(feature = "gecko")]
25pub mod gecko;
26#[cfg(feature = "servo")]
27pub mod servo;
28
29/// A device is a structure that represents the current media a given document
30/// is displayed in.
31///
32/// This is the struct against which media queries are evaluated, has a default
33/// values computed, and contains all the viewport rule state.
34///
35/// This structure also contains atomics used for computing root font-relative
36/// units. These atomics use relaxed ordering, since when computing the style
37/// of the root element, there can't be any other style being computed at the
38/// same time (given we need the style of the parent to compute everything else).
39///
40/// In Gecko, it wraps a pres context.
41#[cfg_attr(feature = "servo", derive(Debug, MallocSizeOf))]
42pub struct Device {
43    /// The default computed values for this Device.
44    #[cfg_attr(feature = "servo", ignore_malloc_size_of = "Arc is shared")]
45    default_values: Arc<ComputedValues>,
46    /// Current computed style of the root element, used for calculations of
47    /// root font-relative units.
48    #[cfg_attr(feature = "servo", ignore_malloc_size_of = "Arc")]
49    root_style: RwLock<Arc<ComputedValues>>,
50    /// Font size of the root element, used for rem units in other elements.
51    root_font_size: AtomicU32,
52    /// Line height of the root element, used for rlh units in other elements.
53    root_line_height: AtomicU32,
54    /// X-height of the root element, used for rex units in other elements.
55    root_font_metrics_ex: AtomicU32,
56    /// Cap-height of the root element, used for rcap units in other elements.
57    root_font_metrics_cap: AtomicU32,
58    /// Advance measure (ch) of the root element, used for rch units in other elements.
59    root_font_metrics_ch: AtomicU32,
60    /// Ideographic advance measure of the root element, used for ric units in other elements.
61    root_font_metrics_ic: AtomicU32,
62    /// Whether any styles computed in the document relied on the root font-size
63    /// by using rem units.
64    used_root_font_size: AtomicBool,
65    /// Whether any styles computed in the document relied on the root line-height
66    /// by using rlh units.
67    used_root_line_height: AtomicBool,
68    /// Whether any styles computed in the document relied on the root font metrics
69    /// by using rcap, rch, rex, or ric units. This is a lock instead of an atomic
70    /// in order to prevent concurrent writes to the root font metric values.
71    #[cfg_attr(feature = "servo", ignore_malloc_size_of = "Pure stack type")]
72    used_root_font_metrics: RwLock<bool>,
73    /// Whether any styles computed in the document relied on font metrics.
74    used_font_metrics: AtomicBool,
75    /// Whether any styles computed in the document relied on the viewport size
76    /// by using vw/vh/vmin/vmax units.
77    used_viewport_size: AtomicBool,
78    /// Whether any styles computed in the document relied on the viewport size
79    /// by using dvw/dvh/dvmin/dvmax units.
80    used_dynamic_viewport_size: AtomicBool,
81    /// The CssEnvironment object responsible of getting CSS environment
82    /// variables.
83    environment: CssEnvironment,
84    /// The body text color, stored as an `nscolor`, used for the "tables
85    /// inherit from body" quirk.
86    ///
87    /// <https://quirks.spec.whatwg.org/#the-tables-inherit-color-from-body-quirk>
88    body_text_color: AtomicU32,
89
90    /// Extra Gecko-specific or Servo-specific data.
91    extra: ExtraDeviceData,
92}
93
94impl Device {
95    /// Get the relevant environment to resolve `env()` functions.
96    #[inline]
97    pub fn environment(&self) -> &CssEnvironment {
98        &self.environment
99    }
100
101    /// Returns the default computed values as a reference, in order to match
102    /// Servo.
103    pub fn default_computed_values(&self) -> &ComputedValues {
104        &self.default_values
105    }
106
107    /// Returns the default computed values as an `Arc`.
108    pub fn default_computed_values_arc(&self) -> &Arc<ComputedValues> {
109        &self.default_values
110    }
111
112    /// Store a pointer to the root element's computed style, for use in
113    /// calculation of root font-relative metrics.
114    pub fn set_root_style(&self, style: &Arc<ComputedValues>) {
115        *self.root_style.write() = style.clone();
116    }
117
118    /// Get the font size of the root element (for rem)
119    pub fn root_font_size(&self) -> Length {
120        self.used_root_font_size.store(true, Ordering::Relaxed);
121        Length::new(f32::from_bits(self.root_font_size.load(Ordering::Relaxed)))
122    }
123
124    /// Set the font size of the root element (for rem), in zoom-independent CSS pixels.
125    pub fn set_root_font_size(&self, size: f32) {
126        self.root_font_size.store(size.to_bits(), Ordering::Relaxed)
127    }
128
129    /// Get the line height of the root element (for rlh)
130    pub fn root_line_height(&self) -> Length {
131        self.used_root_line_height.store(true, Ordering::Relaxed);
132        Length::new(f32::from_bits(
133            self.root_line_height.load(Ordering::Relaxed),
134        ))
135    }
136
137    /// Set the line height of the root element (for rlh), in zoom-independent CSS pixels.
138    pub fn set_root_line_height(&self, size: f32) {
139        self.root_line_height
140            .store(size.to_bits(), Ordering::Relaxed);
141    }
142
143    /// Get the x-height of the root element (for rex)
144    pub fn root_font_metrics_ex(&self) -> Length {
145        self.ensure_root_font_metrics_updated();
146        Length::new(f32::from_bits(
147            self.root_font_metrics_ex.load(Ordering::Relaxed),
148        ))
149    }
150
151    /// Set the x-height of the root element (for rex), in zoom-independent CSS pixels.
152    pub fn set_root_font_metrics_ex(&self, size: f32) -> bool {
153        let size = size.to_bits();
154        let previous = self.root_font_metrics_ex.swap(size, Ordering::Relaxed);
155        previous != size
156    }
157
158    /// Get the cap-height of the root element (for rcap)
159    pub fn root_font_metrics_cap(&self) -> Length {
160        self.ensure_root_font_metrics_updated();
161        Length::new(f32::from_bits(
162            self.root_font_metrics_cap.load(Ordering::Relaxed),
163        ))
164    }
165
166    /// Set the cap-height of the root element (for rcap), in zoom-independent CSS pixels.
167    pub fn set_root_font_metrics_cap(&self, size: f32) -> bool {
168        let size = size.to_bits();
169        let previous = self.root_font_metrics_cap.swap(size, Ordering::Relaxed);
170        previous != size
171    }
172
173    /// Get the advance measure of the root element (for rch)
174    pub fn root_font_metrics_ch(&self) -> Length {
175        self.ensure_root_font_metrics_updated();
176        Length::new(f32::from_bits(
177            self.root_font_metrics_ch.load(Ordering::Relaxed),
178        ))
179    }
180
181    /// Set the advance measure of the root element (for rch), in zoom-independent CSS pixels.
182    pub fn set_root_font_metrics_ch(&self, size: f32) -> bool {
183        let size = size.to_bits();
184        let previous = self.root_font_metrics_ch.swap(size, Ordering::Relaxed);
185        previous != size
186    }
187
188    /// Get the ideographic advance measure of the root element (for ric)
189    pub fn root_font_metrics_ic(&self) -> Length {
190        self.ensure_root_font_metrics_updated();
191        Length::new(f32::from_bits(
192            self.root_font_metrics_ic.load(Ordering::Relaxed),
193        ))
194    }
195
196    /// Set the ideographic advance measure of the root element (for ric), in zoom-independent CSS pixels.
197    pub fn set_root_font_metrics_ic(&self, size: f32) -> bool {
198        let size = size.to_bits();
199        let previous = self.root_font_metrics_ic.swap(size, Ordering::Relaxed);
200        previous != size
201    }
202
203    fn ensure_root_font_metrics_updated(&self) {
204        let mut guard = self.used_root_font_metrics.write();
205        let previously_computed = mem::replace(&mut *guard, true);
206        if !previously_computed {
207            self.update_root_font_metrics();
208        }
209    }
210
211    /// Compute the root element's font metrics, and returns a bool indicating whether
212    /// the font metrics have changed since the previous restyle.
213    pub fn update_root_font_metrics(&self) -> bool {
214        let root_style = self.root_style.read();
215        let root_effective_zoom = (*root_style).effective_zoom;
216        let root_font_size = (*root_style).get_font().clone_font_size().computed_size();
217
218        let root_font_metrics = self.query_font_metrics(
219            (*root_style).writing_mode.is_upright(),
220            &(*root_style).get_font(),
221            root_font_size,
222            QueryFontMetricsFlags::USE_USER_FONT_SET
223                | QueryFontMetricsFlags::NEEDS_CH
224                | QueryFontMetricsFlags::NEEDS_IC,
225            /* track_usage = */ false,
226        );
227
228        let mut root_font_metrics_changed = false;
229        root_font_metrics_changed |= self.set_root_font_metrics_ex(
230            root_effective_zoom.unzoom(root_font_metrics.x_height_or_default(root_font_size).px()),
231        );
232        root_font_metrics_changed |= self.set_root_font_metrics_ch(
233            root_effective_zoom.unzoom(
234                root_font_metrics
235                    .zero_advance_measure_or_default(
236                        root_font_size,
237                        (*root_style).writing_mode.is_upright(),
238                    )
239                    .px(),
240            ),
241        );
242        root_font_metrics_changed |= self.set_root_font_metrics_cap(
243            root_effective_zoom.unzoom(root_font_metrics.cap_height_or_default().px()),
244        );
245        root_font_metrics_changed |= self.set_root_font_metrics_ic(
246            root_effective_zoom.unzoom(root_font_metrics.ic_width_or_default(root_font_size).px()),
247        );
248
249        root_font_metrics_changed
250    }
251
252    /// Returns whether we ever looked up the root font size of the Device.
253    pub fn used_root_font_size(&self) -> bool {
254        self.used_root_font_size.load(Ordering::Relaxed)
255    }
256
257    /// Returns whether we ever looked up the root line-height of the device.
258    pub fn used_root_line_height(&self) -> bool {
259        self.used_root_line_height.load(Ordering::Relaxed)
260    }
261
262    /// Returns whether we ever looked up the root font metrics of the device.
263    pub fn used_root_font_metrics(&self) -> bool {
264        *self.used_root_font_metrics.read()
265    }
266
267    /// Returns whether we ever looked up the viewport size of the Device.
268    pub fn used_viewport_size(&self) -> bool {
269        self.used_viewport_size.load(Ordering::Relaxed)
270    }
271
272    /// Returns whether we ever looked up the dynamic viewport size of the Device.
273    pub fn used_dynamic_viewport_size(&self) -> bool {
274        self.used_dynamic_viewport_size.load(Ordering::Relaxed)
275    }
276
277    /// Returns whether font metrics have been queried.
278    pub fn used_font_metrics(&self) -> bool {
279        self.used_font_metrics.load(Ordering::Relaxed)
280    }
281
282    /// Returns the body text color.
283    pub fn body_text_color(&self) -> AbsoluteColor {
284        AbsoluteColor::from_nscolor(self.body_text_color.load(Ordering::Relaxed))
285    }
286
287    /// Sets the body text color for the "inherit color from body" quirk.
288    ///
289    /// <https://quirks.spec.whatwg.org/#the-tables-inherit-color-from-body-quirk>
290    pub fn set_body_text_color(&self, color: AbsoluteColor) {
291        self.body_text_color
292            .store(color.to_nscolor(), Ordering::Relaxed)
293    }
294}