Simplify our setup for font metric queries from style

This is a backport of https://phabricator.services.mozilla.com/D157589,
by Emilio Cobos Álvarez, plus some additions so that Servo compiles,
and some parts from https://phabricator.services.mozilla.com/D144455.

Should have no change in behavior.
This commit is contained in:
Oriol Brufau 2023-05-10 03:34:29 +02:00
parent feaa66b597
commit ab2ba273e3
21 changed files with 172 additions and 322 deletions

View file

@ -14,13 +14,15 @@ use super::generics::transform::IsParallelTo;
use super::generics::{self, GreaterThanOrEqualToOne, NonNegative, ZeroToOne};
use super::specified;
use super::{CSSFloat, CSSInteger};
use crate::computed_value_flags::ComputedValueFlags;
use crate::context::QuirksMode;
use crate::font_metrics::{get_metrics_provider_for_product, FontMetricsProvider};
use crate::font_metrics::{FontMetrics, FontMetricsOrientation};
use crate::media_queries::Device;
#[cfg(feature = "gecko")]
use crate::properties;
use crate::properties::{ComputedValues, LonghandId, StyleBuilder};
use crate::rule_cache::RuleCacheConditions;
use crate::values::specified::length::FontBaseSize;
use crate::{ArcSlice, Atom, One};
use euclid::{default, Point2D, Rect, Size2D};
use servo_arc::Arc;
@ -155,10 +157,6 @@ pub struct Context<'a> {
#[cfg(feature = "servo")]
pub cached_system_font: Option<()>,
/// A font metrics provider, used to access font metrics to implement
/// font-relative units.
pub font_metrics_provider: &'a dyn FontMetricsProvider,
/// Whether or not we are computing the media list in a media query
pub in_media_query: bool,
@ -192,11 +190,9 @@ impl<'a> Context<'a> {
F: FnOnce(&Context) -> R,
{
let mut conditions = RuleCacheConditions::default();
let provider = get_metrics_provider_for_product();
let context = Context {
builder: StyleBuilder::for_inheritance(device, None, None),
font_metrics_provider: &provider,
cached_system_font: None,
in_media_query: true,
quirks_mode,
@ -213,6 +209,49 @@ impl<'a> Context<'a> {
self.builder.device
}
/// Queries font metrics.
pub fn query_font_metrics(
&self,
base_size: FontBaseSize,
orientation: FontMetricsOrientation,
retrieve_math_scales: bool,
) -> FontMetrics {
if self.for_non_inherited_property.is_some() {
self.rule_cache_conditions.borrow_mut().set_uncacheable();
}
self.builder.add_flags(match base_size {
FontBaseSize::CurrentStyle => ComputedValueFlags::DEPENDS_ON_SELF_FONT_METRICS,
FontBaseSize::InheritedStyle => ComputedValueFlags::DEPENDS_ON_INHERITED_FONT_METRICS,
});
let size = base_size.resolve(self);
let style = self.style();
let (wm, font) = match base_size {
FontBaseSize::CurrentStyle => (style.writing_mode, style.get_font()),
// This is only used for font-size computation.
FontBaseSize::InheritedStyle => {
(*style.inherited_writing_mode(), style.get_parent_font())
},
};
let vertical = match orientation {
FontMetricsOrientation::MatchContextPreferHorizontal => {
wm.is_vertical() && wm.is_upright()
},
FontMetricsOrientation::MatchContextPreferVertical => {
wm.is_vertical() && !wm.is_sideways()
},
FontMetricsOrientation::Horizontal => false,
};
self.device().query_font_metrics(
vertical,
font,
size,
self.in_media_query,
retrieve_math_scales,
)
}
/// The current viewport size, used to resolve viewport units.
pub fn viewport_size_for_viewport_unit_resolution(&self) -> default::Size2D<Au> {
self.builder

View file

@ -4,6 +4,8 @@
//! Specified values for font properties
#[cfg(feature = "gecko")]
use crate::context::QuirksMode;
#[cfg(feature = "gecko")]
use crate::gecko_bindings::bindings;
use crate::parser::{Parse, ParserContext};
@ -811,8 +813,27 @@ impl FontSizeKeyword {
#[cfg(feature = "gecko")]
#[inline]
fn to_length(&self, cx: &Context) -> NonNegativeLength {
use crate::context::QuirksMode;
let gecko_font = cx.style().get_font().gecko();
let family = &gecko_font.mFont.family.families;
let generic = family
.single_generic()
.unwrap_or(computed::GenericFontFamily::None);
let base_size = unsafe {
Atom::with(gecko_font.mLanguage.mRawPtr, |language| {
cx.device().base_size_for_generic(language, generic)
})
};
self.to_length_without_context(cx.quirks_mode, base_size)
}
/// Resolve a keyword length without any context, with explicit arguments.
#[cfg(feature = "gecko")]
#[inline]
pub fn to_length_without_context(
&self,
quirks_mode: QuirksMode,
base_size: Length,
) -> NonNegativeLength {
// The tables in this function are originally from
// nsRuleNode::CalcFontPointSize in Gecko:
//
@ -856,19 +877,10 @@ impl FontSizeKeyword {
];
static FONT_SIZE_FACTORS: [i32; 8] = [60, 75, 89, 100, 120, 150, 200, 300];
let ref gecko_font = cx.style().get_font().gecko();
let base_size = unsafe {
Atom::with(gecko_font.mLanguage.mRawPtr, |atom| {
cx.font_metrics_provider
.get_size(atom, gecko_font.mGenericID)
})
};
let base_size_px = base_size.px().round() as i32;
let html_size = self.html_size() as usize;
NonNegative(if base_size_px >= 9 && base_size_px <= 16 {
let mapping = if cx.quirks_mode == QuirksMode::Quirks {
let mapping = if quirks_mode == QuirksMode::Quirks {
QUIRKS_FONT_SIZE_MAPPING
} else {
FONT_SIZE_MAPPING

View file

@ -7,7 +7,6 @@
//! [length]: https://drafts.csswg.org/css-values/#lengths
use super::{AllowQuirks, Number, Percentage, ToComputedValue};
use crate::computed_value_flags::ComputedValueFlags;
use crate::font_metrics::{FontMetrics, FontMetricsOrientation};
use crate::parser::{Parse, ParserContext};
use crate::values::computed::{self, CSSPixelLength, Context};
@ -159,16 +158,11 @@ impl FontRelativeLength {
base_size: FontBaseSize,
orientation: FontMetricsOrientation,
) -> FontMetrics {
context
.font_metrics_provider
.query(context, base_size, orientation)
let retrieve_math_scales = false;
context.query_font_metrics(base_size, orientation, retrieve_math_scales)
}
let reference_font_size = base_size.resolve(context);
let font_metrics_flag = match base_size {
FontBaseSize::CurrentStyle => ComputedValueFlags::DEPENDS_ON_SELF_FONT_METRICS,
FontBaseSize::InheritedStyle => ComputedValueFlags::DEPENDS_ON_INHERITED_FONT_METRICS,
};
match *self {
FontRelativeLength::Em(length) => {
if context.for_non_inherited_property.is_some() {
@ -183,10 +177,6 @@ impl FontRelativeLength {
(reference_font_size, length)
},
FontRelativeLength::Ex(length) => {
if context.for_non_inherited_property.is_some() {
context.rule_cache_conditions.borrow_mut().set_uncacheable();
}
context.builder.add_flags(font_metrics_flag);
// The x-height is an intrinsically horizontal metric.
let metrics =
query_font_metrics(context, base_size, FontMetricsOrientation::Horizontal);
@ -202,10 +192,6 @@ impl FontRelativeLength {
(reference_size, length)
},
FontRelativeLength::Ch(length) => {
if context.for_non_inherited_property.is_some() {
context.rule_cache_conditions.borrow_mut().set_uncacheable();
}
context.builder.add_flags(font_metrics_flag);
// https://drafts.csswg.org/css-values/#ch:
//
// Equal to the used advance measure of the “0” (ZERO,
@ -239,10 +225,6 @@ impl FontRelativeLength {
(reference_size, length)
},
FontRelativeLength::Cap(length) => {
if context.for_non_inherited_property.is_some() {
context.rule_cache_conditions.borrow_mut().set_uncacheable();
}
context.builder.add_flags(font_metrics_flag);
let metrics =
query_font_metrics(context, base_size, FontMetricsOrientation::Horizontal);
let reference_size = metrics.cap_height.unwrap_or_else(|| {