1use crate::color::AbsoluteColor;
8use crate::context::QuirksMode;
9use crate::custom_properties::CssEnvironment;
10use crate::font_metrics::FontMetrics;
11use crate::logical_geometry::WritingMode;
12use crate::media_queries::MediaType;
13use crate::properties::style_structs::Font;
14use crate::properties::ComputedValues;
15use crate::queries::feature::{AllowsRanges, Evaluator, FeatureFlags, QueryFeatureDescription};
16use crate::queries::values::PrefersColorScheme;
17use crate::values::computed::font::GenericFontFamily;
18use crate::values::computed::{
19 CSSPixelLength, Context, Length, LineHeight, NonNegativeLength, Resolution,
20};
21use crate::values::specified::color::{ColorSchemeFlags, ForcedColors};
22use crate::values::specified::font::{
23 QueryFontMetricsFlags, FONT_MEDIUM_LINE_HEIGHT_PX, FONT_MEDIUM_PX,
24};
25use crate::values::specified::ViewportVariant;
26use crate::values::KeyframesName;
27use app_units::{Au, AU_PER_PX};
28use euclid::default::Size2D as UntypedSize2D;
29use euclid::{Scale, SideOffsets2D, Size2D};
30use mime::Mime;
31use servo_arc::Arc;
32use std::fmt::Debug;
33use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
34use style_traits::{CSSPixel, DevicePixel};
35
36pub trait FontMetricsProvider: Debug + Sync {
39 fn query_font_metrics(
41 &self,
42 vertical: bool,
43 font: &Font,
44 base_size: CSSPixelLength,
45 flags: QueryFontMetricsFlags,
46 ) -> FontMetrics;
47 fn base_size_for_generic(&self, generic: GenericFontFamily) -> Length;
49}
50
51#[derive(Debug, MallocSizeOf)]
56pub struct Device {
57 media_type: MediaType,
59 viewport_size: Size2D<f32, CSSPixel>,
61 device_pixel_ratio: Scale<f32, CSSPixel, DevicePixel>,
63 #[ignore_malloc_size_of = "Pure stack type"]
65 quirks_mode: QuirksMode,
66
67 #[ignore_malloc_size_of = "Pure stack type"]
76 root_font_size: AtomicU32,
77 #[ignore_malloc_size_of = "Pure stack type"]
79 root_line_height: AtomicU32,
80 #[ignore_malloc_size_of = "Pure stack type"]
83 used_root_font_size: AtomicBool,
84 #[ignore_malloc_size_of = "Pure stack type"]
87 used_root_line_height: AtomicBool,
88 used_font_metrics: AtomicBool,
90 #[ignore_malloc_size_of = "Pure stack type"]
92 used_viewport_units: AtomicBool,
93 #[ignore_malloc_size_of = "Pure stack type"]
95 prefers_color_scheme: PrefersColorScheme,
96 environment: CssEnvironment,
99 #[ignore_malloc_size_of = "Owned by embedder"]
101 font_metrics_provider: Box<dyn FontMetricsProvider>,
102 #[ignore_malloc_size_of = "Arc is shared"]
104 default_computed_values: Arc<ComputedValues>,
105}
106
107impl Device {
108 pub fn new(
110 media_type: MediaType,
111 quirks_mode: QuirksMode,
112 viewport_size: Size2D<f32, CSSPixel>,
113 device_pixel_ratio: Scale<f32, CSSPixel, DevicePixel>,
114 font_metrics_provider: Box<dyn FontMetricsProvider>,
115 default_computed_values: Arc<ComputedValues>,
116 prefers_color_scheme: PrefersColorScheme,
117 ) -> Device {
118 Device {
119 media_type,
120 viewport_size,
121 device_pixel_ratio,
122 quirks_mode,
123 root_font_size: AtomicU32::new(FONT_MEDIUM_PX.to_bits()),
124 root_line_height: AtomicU32::new(FONT_MEDIUM_LINE_HEIGHT_PX.to_bits()),
125 used_root_font_size: AtomicBool::new(false),
126 used_root_line_height: AtomicBool::new(false),
127 used_font_metrics: AtomicBool::new(false),
128 used_viewport_units: AtomicBool::new(false),
129 prefers_color_scheme,
130 environment: CssEnvironment,
131 font_metrics_provider,
132 default_computed_values,
133 }
134 }
135
136 #[inline]
138 pub fn environment(&self) -> &CssEnvironment {
139 &self.environment
140 }
141
142 pub fn default_computed_values(&self) -> &ComputedValues {
144 &self.default_computed_values
145 }
146
147 pub fn root_font_size(&self) -> CSSPixelLength {
149 self.used_root_font_size.store(true, Ordering::Relaxed);
150 CSSPixelLength::new(f32::from_bits(self.root_font_size.load(Ordering::Relaxed)))
151 }
152
153 pub fn set_root_font_size(&self, size: f32) {
155 self.root_font_size.store(size.to_bits(), Ordering::Relaxed)
156 }
157
158 pub fn root_line_height(&self) -> CSSPixelLength {
160 self.used_root_line_height.store(true, Ordering::Relaxed);
161 CSSPixelLength::new(f32::from_bits(
162 self.root_line_height.load(Ordering::Relaxed),
163 ))
164 }
165
166 pub fn set_root_line_height(&self, size: f32) {
168 self.root_line_height
169 .store(size.to_bits(), Ordering::Relaxed);
170 }
171
172 pub fn calc_line_height(
176 &self,
177 font: &crate::properties::style_structs::Font,
178 _writing_mode: WritingMode,
179 _element: Option<()>,
180 ) -> NonNegativeLength {
181 (match font.line_height {
182 LineHeight::Normal => CSSPixelLength::new(0.),
184 LineHeight::Number(number) => font.font_size.computed_size() * number.0,
185 LineHeight::Length(length) => length.0,
186 })
187 .into()
188 }
189
190 pub fn quirks_mode(&self) -> QuirksMode {
192 self.quirks_mode
193 }
194
195 pub fn set_body_text_color(&self, _color: AbsoluteColor) {
199 }
201
202 pub fn base_size_for_generic(&self, generic: GenericFontFamily) -> Length {
204 self.font_metrics_provider.base_size_for_generic(generic)
205 }
206
207 pub fn animation_name_may_be_referenced(&self, _: &KeyframesName) -> bool {
209 true
211 }
212
213 pub fn used_root_font_size(&self) -> bool {
215 self.used_root_font_size.load(Ordering::Relaxed)
216 }
217
218 pub fn used_root_line_height(&self) -> bool {
220 self.used_root_line_height.load(Ordering::Relaxed)
221 }
222
223 pub fn used_font_metrics(&self) -> bool {
225 self.used_font_metrics.load(Ordering::Relaxed)
226 }
227
228 #[inline]
231 pub fn au_viewport_size(&self) -> UntypedSize2D<Au> {
232 Size2D::new(
233 Au::from_f32_px(self.viewport_size.width),
234 Au::from_f32_px(self.viewport_size.height),
235 )
236 }
237
238 pub fn au_viewport_size_for_viewport_unit_resolution(
240 &self,
241 _: ViewportVariant,
242 ) -> UntypedSize2D<Au> {
243 self.used_viewport_units.store(true, Ordering::Relaxed);
244 self.au_viewport_size()
247 }
248
249 pub fn used_viewport_units(&self) -> bool {
251 self.used_viewport_units.load(Ordering::Relaxed)
252 }
253
254 pub fn app_units_per_device_pixel(&self) -> i32 {
256 (AU_PER_PX as f32 / self.device_pixel_ratio.0) as i32
257 }
258
259 pub fn device_pixel_ratio_ignoring_full_zoom(&self) -> Scale<f32, CSSPixel, DevicePixel> {
261 self.device_pixel_ratio
262 }
263
264 pub fn device_pixel_ratio(&self) -> Scale<f32, CSSPixel, DevicePixel> {
266 self.device_pixel_ratio
267 }
268
269 pub fn scrollbar_inline_size(&self) -> CSSPixelLength {
271 CSSPixelLength::new(0.0)
273 }
274
275 pub fn query_font_metrics(
277 &self,
278 vertical: bool,
279 font: &Font,
280 base_size: CSSPixelLength,
281 flags: QueryFontMetricsFlags,
282 ) -> FontMetrics {
283 self.used_font_metrics.store(true, Ordering::Relaxed);
284 self.font_metrics_provider
285 .query_font_metrics(vertical, font, base_size, flags)
286 }
287
288 pub fn media_type(&self) -> MediaType {
290 self.media_type.clone()
291 }
292
293 pub fn forced_colors(&self) -> ForcedColors {
295 ForcedColors::None
296 }
297
298 pub fn default_background_color(&self) -> AbsoluteColor {
300 AbsoluteColor::WHITE
301 }
302
303 pub fn default_color(&self) -> AbsoluteColor {
305 AbsoluteColor::BLACK
306 }
307
308 pub fn color_scheme(&self) -> PrefersColorScheme {
310 self.prefers_color_scheme
311 }
312
313 pub(crate) fn is_dark_color_scheme(&self, _: ColorSchemeFlags) -> bool {
314 false
315 }
316
317 pub fn safe_area_insets(&self) -> SideOffsets2D<f32, CSSPixel> {
319 SideOffsets2D::zero()
320 }
321
322 pub fn is_supported_mime_type(&self, mime_type: &str) -> bool {
324 match mime_type.parse::<Mime>() {
325 Ok(m) => {
326 m == mime::IMAGE_BMP
329 || m == mime::IMAGE_GIF
330 || m == mime::IMAGE_PNG
331 || m == mime::IMAGE_JPEG
332 || m == "image/x-icon"
333 || m == "image/webp"
334 },
335 _ => false,
336 }
337 }
338
339 #[inline]
341 pub fn chrome_rules_enabled_for_document(&self) -> bool {
342 false
343 }
344}
345
346fn eval_width(context: &Context) -> CSSPixelLength {
348 CSSPixelLength::new(context.device().au_viewport_size().width.to_f32_px())
349}
350
351#[derive(Clone, Copy, Debug, FromPrimitive, Parse, ToCss)]
352#[repr(u8)]
353enum Scan {
354 Progressive,
355 Interlace,
356}
357
358fn eval_scan(_: &Context, _: Option<Scan>) -> bool {
360 false
363}
364
365fn eval_resolution(context: &Context) -> Resolution {
367 Resolution::from_dppx(context.device().device_pixel_ratio.0)
368}
369
370fn eval_device_pixel_ratio(context: &Context) -> f32 {
372 eval_resolution(context).dppx()
373}
374
375fn eval_prefers_color_scheme(context: &Context, query_value: Option<PrefersColorScheme>) -> bool {
376 match query_value {
377 Some(v) => context.device().prefers_color_scheme == v,
378 None => true,
379 }
380}
381
382pub static MEDIA_FEATURES: [QueryFeatureDescription; 6] = [
384 feature!(
385 atom!("width"),
386 AllowsRanges::Yes,
387 Evaluator::Length(eval_width),
388 FeatureFlags::empty(),
389 ),
390 feature!(
391 atom!("scan"),
392 AllowsRanges::No,
393 keyword_evaluator!(eval_scan, Scan),
394 FeatureFlags::empty(),
395 ),
396 feature!(
397 atom!("resolution"),
398 AllowsRanges::Yes,
399 Evaluator::Resolution(eval_resolution),
400 FeatureFlags::empty(),
401 ),
402 feature!(
403 atom!("device-pixel-ratio"),
404 AllowsRanges::Yes,
405 Evaluator::Float(eval_device_pixel_ratio),
406 FeatureFlags::WEBKIT_PREFIX,
407 ),
408 feature!(
409 atom!("-moz-device-pixel-ratio"),
410 AllowsRanges::Yes,
411 Evaluator::Float(eval_device_pixel_ratio),
412 FeatureFlags::empty(),
413 ),
414 feature!(
415 atom!("prefers-color-scheme"),
416 AllowsRanges::No,
417 keyword_evaluator!(eval_prefers_color_scheme, PrefersColorScheme),
418 FeatureFlags::empty(),
419 ),
420];