/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
//! Servo-specific logic for [`Device`].
usecrate::color::AbsoluteColor; usecrate::context::QuirksMode; usecrate::custom_properties::CssEnvironment; usecrate::font_metrics::FontMetrics; usecrate::logical_geometry::WritingMode; usecrate::media_queries::MediaType; usecrate::properties::style_structs::Font; usecrate::properties::ComputedValues; usecrate::queries::values::PrefersColorScheme; usecrate::values::computed::font::GenericFontFamily; usecrate::values::computed::{CSSPixelLength, Length, LineHeight, NonNegativeLength}; usecrate::values::specified::color::{ColorSchemeFlags, ForcedColors, SystemColor}; usecrate::values::specified::font::{
QueryFontMetricsFlags, FONT_MEDIUM_CAP_PX, FONT_MEDIUM_CH_PX, FONT_MEDIUM_EX_PX,
FONT_MEDIUM_IC_PX, FONT_MEDIUM_LINE_HEIGHT_PX, FONT_MEDIUM_PX,
}; usecrate::values::specified::ViewportVariant; usecrate::values::KeyframesName; use app_units::{Au, AU_PER_PX}; use euclid::default::Size2D as UntypedSize2D; use euclid::{Scale, SideOffsets2D, Size2D}; use malloc_size_of_derive::MallocSizeOf; use mime::Mime; use parking_lot::RwLock; use servo_arc::Arc; use std::fmt::Debug; use std::sync::atomic::{AtomicBool, AtomicU32, Ordering}; use style_traits::{CSSPixel, DevicePixel};
usecrate::device::Device;
/// A trait used to query font metrics in clients of Stylo. This is used by Device to /// query font metrics in a way that is specific to the client using Stylo. pubtrait FontMetricsProvider: Debug + Sync { /// Query the font metrics for the given font and the given base font size. fn query_font_metrics(
&self,
vertical: bool,
font: &Font,
base_size: CSSPixelLength,
flags: QueryFontMetricsFlags,
) -> FontMetrics; /// Gets the base size given a generic font family. fn base_size_for_generic(&self, generic: GenericFontFamily) -> Length;
}
#[derive(Debug, MallocSizeOf)] pub(super) struct ExtraDeviceData { /// The current media type used by de device.
media_type: MediaType, /// The current viewport size, in CSS pixels.
viewport_size: Size2D<f32, CSSPixel>, /// The current device pixel ratio, from CSS pixels to device pixels.
device_pixel_ratio: Scale<f32, CSSPixel, DevicePixel>, /// The current quirks mode. #[ignore_malloc_size_of = "Pure stack type"]
quirks_mode: QuirksMode, /// Whether the user prefers light mode or dark mode #[ignore_malloc_size_of = "Pure stack type"]
prefers_color_scheme: PrefersColorScheme, /// An implementation of a trait which implements support for querying font metrics. #[ignore_malloc_size_of = "Owned by embedder"]
font_metrics_provider: Box<dyn FontMetricsProvider>,
}
/// Returns the computed line-height for the font in a given computed values instance. /// /// If you pass down an element, then the used line-height is returned. pubfn calc_line_height(
&self,
font: &crate::properties::style_structs::Font,
_writing_mode: WritingMode,
_element: Option<()>,
) -> NonNegativeLength {
(match font.line_height { // TODO: compute `normal` from the font metrics
LineHeight::Normal => CSSPixelLength::new(0.),
LineHeight::Number(number) => font.font_size.computed_size() * number.0,
LineHeight::Length(length) => length.0,
})
.into()
}
/// Get the quirks mode of the current device. pubfn quirks_mode(&self) -> QuirksMode { self.extra.quirks_mode
}
/// Gets the base size given a generic font family. pubfn base_size_for_generic(&self, generic: GenericFontFamily) -> Length { self.extra
.font_metrics_provider
.base_size_for_generic(generic)
}
/// Whether a given animation name may be referenced from style. pubfn animation_name_may_be_referenced(&self, _: &KeyframesName) -> bool { // Assume it is, since we don't have any good way to prove it's not. true
}
/// Get the viewport size on this [`Device`]. pubfn viewport_size(&self) -> Size2D<f32, CSSPixel> { self.extra.viewport_size
}
/// Set the viewport size on this [`Device`]. /// /// Note that this does not update any associated `Stylist`. For this you must call /// `Stylist::media_features_change_changed_style` and /// `Stylist::force_stylesheet_origins_dirty`. pubfn set_viewport_size(&mutself, viewport_size: Size2D<f32, CSSPixel>) { self.extra.viewport_size = viewport_size;
}
/// Returns the viewport size of the current device in app units, needed, /// among other things, to resolve viewport units. #[inline] pubfn au_viewport_size(&self) -> UntypedSize2D<Au> {
Size2D::new(
Au::from_f32_px(self.extra.viewport_size.width),
Au::from_f32_px(self.extra.viewport_size.height),
)
}
/// Like the above, but records that we've used viewport units. pubfn au_viewport_size_for_viewport_unit_resolution(
&self,
_: ViewportVariant,
) -> UntypedSize2D<Au> { self.used_viewport_size.store(true, Ordering::Relaxed); // Servo doesn't have dynamic UA interfaces that affect the viewport, // so we can just ignore the ViewportVariant. self.au_viewport_size()
}
/// Returns the number of app units per device pixel we're using currently. pubfn app_units_per_device_pixel(&self) -> i32 {
(AU_PER_PX as f32 / self.extra.device_pixel_ratio.0) as i32
}
/// Returns the device pixel ratio, ignoring the full zoom factor. pubfn device_pixel_ratio_ignoring_full_zoom(&self) -> Scale<f32, CSSPixel, DevicePixel> { self.extra.device_pixel_ratio
}
/// Set a new device pixel ratio on this [`Device`]. /// /// Note that this does not update any associated `Stylist`. For this you must call /// `Stylist::media_features_change_changed_style` and /// `Stylist::force_stylesheet_origins_dirty`. pubfn set_device_pixel_ratio(
&mutself,
device_pixel_ratio: Scale<f32, CSSPixel, DevicePixel>,
) { self.extra.device_pixel_ratio = device_pixel_ratio;
}
/// Gets the size of the scrollbar in CSS pixels. pubfn scrollbar_inline_size(&self) -> CSSPixelLength { // TODO: implement this.
CSSPixelLength::new(0.0)
}
/// Queries font metrics using the [`FontMetricsProvider`] interface. pubfn query_font_metrics(
&self,
vertical: bool,
font: &Font,
base_size: CSSPixelLength,
flags: QueryFontMetricsFlags,
track_usage: bool,
) -> FontMetrics { if track_usage { self.used_font_metrics.store(true, Ordering::Relaxed);
} self.extra
.font_metrics_provider
.query_font_metrics(vertical, font, base_size, flags)
}
/// Return the media type of the current device. pubfn media_type(&self) -> MediaType { self.extra.media_type.clone()
}
/// Set the [`PrefersColorScheme`] value on this [`Device`]. /// /// Note that this does not update any associated `Stylist`. For this you must call /// `Stylist::media_features_change_changed_style` and /// `Stylist::force_stylesheet_origins_dirty`. pubfn set_color_scheme(&mutself, new_color_scheme: PrefersColorScheme) { self.extra.prefers_color_scheme = new_color_scheme;
}
/// Returns the color scheme of this [`Device`]. pubfn color_scheme(&self) -> PrefersColorScheme { self.extra.prefers_color_scheme
}
/// Returns true if the given MIME type is supported pubfn is_supported_mime_type(&self, mime_type: &str) -> bool { match mime_type.parse::<Mime>() {
Ok(m) => { // Keep this in sync with 'image_classifer' from // components/net/mime_classifier.rs
m == mime::IMAGE_BMP
|| m == mime::IMAGE_GIF
|| m == mime::IMAGE_PNG
|| m == mime::IMAGE_JPEG
|| m == "image/x-icon"
|| m == "image/webp"
},
_ => false,
}
}
/// Return whether the document is a chrome document. #[inline] pubfn chrome_rules_enabled_for_document(&self) -> bool { false
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.15 Sekunden
(vorverarbeitet am 2026-08-25)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.