feat: Support font-relative ch and ic units (#32171)

* feat: Support font-relative `ch` and `ic` units

After #31966, which made it possible for the first time to resolve
font-relative CSS units, this change adds support for the `ch` and
`ic` units.

One difference with the `ex` unit that was added in that PR is that
these units must reflect the advance width of a character (the zero
digit in the case of `ch`, and the CJK water radical for `ic`) as it
would be rendered by the current font group. This means that the size
of these units don't only depend on the first available font, in the
case where that font does not contain a glyph for that character.

This is implemented by adding the advance width for these two
characters as optional fields of `FontMetrics`, so the advance width
computation happens in advance. Then, when the font metrics are
queried as part of unit resolution, the font group is searched for the
first font containing that character.

This change only implements support for these units in upright
typesetting modes, since Servo does not yet have support for vertical
writing modes. This means that many of the WPT tests that test for the
behavior of these units with vertical writing modes do not pass.

This change also makes a number of WPT tests pass, which relied on the
`ch` and `ic` units. It, however, also makes the test
`/css/css-text/white-space/text-wrap-balance-overflow-002.html` fail,
since it tests `text-wrap: balance`, which Servo does not yet
implement, and it was only previously passing by chance due to the
previous behavior of these units.

* Revert Python 3.10-related changes to wss

* Fix formatting

* Remove test expectation
This commit is contained in:
Andreu Botella 2024-05-02 09:17:32 +02:00 committed by GitHub
parent 928214518c
commit 8ec5344f70
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
68 changed files with 66 additions and 435 deletions

View file

@ -131,6 +131,8 @@ pub struct FontMetrics {
pub max_advance: Au,
pub average_advance: Au,
pub line_gap: Au,
pub zero_horizontal_advance: Option<Au>,
pub ic_horizontal_advance: Option<Au>,
}
impl FontMetrics {
@ -150,6 +152,8 @@ impl FontMetrics {
max_advance: Au(0),
average_advance: Au(0),
line_gap: Au(0),
zero_horizontal_advance: None,
ic_horizontal_advance: None,
}
}
}

View file

@ -261,10 +261,16 @@ impl PlatformFontMethods for PlatformFont {
x_height = self.font_units_to_au(os2.sx_height as f64);
}
let average_advance = self
let zero_horizontal_advance = self
.glyph_index('0')
.and_then(|idx| self.glyph_h_advance(idx))
.map_or(max_advance, |advance| self.font_units_to_au(advance));
.map(Au::from_f64_px);
let ic_horizontal_advance = self
.glyph_index('\u{6C34}')
.and_then(|idx| self.glyph_h_advance(idx))
.map(Au::from_f64_px);
let average_advance = zero_horizontal_advance.unwrap_or(max_advance);
let metrics = FontMetrics {
underline_size,
@ -279,6 +285,8 @@ impl PlatformFontMethods for PlatformFont {
max_advance,
average_advance,
line_gap: height,
zero_horizontal_advance,
ic_horizontal_advance,
};
debug!("Font metrics (@{}px): {:?}", em_size.to_f32_px(), metrics);

View file

@ -259,11 +259,15 @@ impl PlatformFontMethods for PlatformFont {
let line_gap = (ascent + descent + leading + 0.5).floor();
let max_advance = Au::from_f64_px(self.ctfont.bounding_box().size.width);
let average_advance = self
let zero_horizontal_advance = self
.glyph_index('0')
.and_then(|idx| self.glyph_h_advance(idx))
.map(Au::from_f64_px)
.unwrap_or(max_advance);
.map(Au::from_f64_px);
let ic_horizontal_advance = self
.glyph_index('\u{6C34}')
.and_then(|idx| self.glyph_h_advance(idx))
.map(Au::from_f64_px);
let average_advance = zero_horizontal_advance.unwrap_or(max_advance);
let metrics = FontMetrics {
underline_size: Au::from_f64_au(underline_thickness),
@ -286,6 +290,8 @@ impl PlatformFontMethods for PlatformFont {
max_advance,
average_advance,
line_gap: Au::from_f64_px(line_gap),
zero_horizontal_advance,
ic_horizontal_advance,
};
debug!(
"Font metrics (@{} pt): {:?}",

View file

@ -232,6 +232,15 @@ impl PlatformFontMethods for PlatformFont {
// is pulled out here for clarity
let leading = dm.ascent - dm.capHeight;
let zero_horizontal_advance = self
.glyph_index('0')
.and_then(|idx| self.glyph_h_advance(idx))
.map(Au::from_f64_px);
let ic_horizontal_advance = self
.glyph_index('\u{6C34}')
.and_then(|idx| self.glyph_h_advance(idx))
.map(Au::from_f64_px);
let metrics = FontMetrics {
underline_size: au_from_du(dm.underlineThickness as i32),
underline_offset: au_from_du_s(dm.underlinePosition as i32),
@ -245,6 +254,8 @@ impl PlatformFontMethods for PlatformFont {
max_advance: au_from_pt(0.0), // FIXME
average_advance: au_from_pt(0.0), // FIXME
line_gap: au_from_du_s((dm.ascent + dm.descent + dm.lineGap as u16) as i32),
zero_horizontal_advance,
ic_horizontal_advance,
};
debug!("Font metrics (@{} pt): {:?}", self.em_size * 12., metrics);
metrics

View file

@ -1247,8 +1247,9 @@ impl FontMetricsProvider for LayoutFontMetricsProvider {
_retrieve_math_scales: bool,
) -> FontMetrics {
layout::context::with_thread_local_font_context(&self.0, move |font_context| {
let Some(servo_metrics) = font_context
.font_group_with_size(ServoArc::new(font.clone()), base_size.into())
let font_group =
font_context.font_group_with_size(ServoArc::new(font.clone()), base_size.into());
let Some(first_font_metrics) = font_group
.borrow_mut()
.first(font_context)
.map(|font| font.borrow().metrics.clone())
@ -1258,16 +1259,39 @@ impl FontMetricsProvider for LayoutFontMetricsProvider {
// Only use the x-height of this font if it is non-zero. Some fonts return
// inaccurate metrics, which shouldn't be used.
let x_height = Some(servo_metrics.x_height)
let x_height = Some(first_font_metrics.x_height)
.filter(|x_height| !x_height.is_zero())
.map(CSSPixelLength::from);
let zero_advance_measure = first_font_metrics
.zero_horizontal_advance
.or_else(|| {
font_group
.borrow_mut()
.find_by_codepoint(font_context, '0')?
.borrow()
.metrics
.zero_horizontal_advance
})
.map(CSSPixelLength::from);
let ic_width = first_font_metrics
.ic_horizontal_advance
.or_else(|| {
font_group
.borrow_mut()
.find_by_codepoint(font_context, '\u{6C34}')?
.borrow()
.metrics
.ic_horizontal_advance
})
.map(CSSPixelLength::from);
FontMetrics {
x_height,
zero_advance_measure: None,
zero_advance_measure,
cap_height: None,
ic_width: None,
ascent: servo_metrics.ascent.into(),
ic_width,
ascent: first_font_metrics.ascent.into(),
script_percent_scale_down: None,
script_script_percent_scale_down: None,
}

View file

@ -1,2 +0,0 @@
[float-nowrap-hyphen-rewind-1.html]
expected: FAIL

View file

@ -1,12 +0,0 @@
[inline-negative-margin-001.html]
[[data-expected-height\] 3]
expected: FAIL
[[data-expected-height\] 4]
expected: FAIL
[[data-expected-height\] 5]
expected: FAIL
[[data-expected-height\] 6]
expected: FAIL

View file

@ -1,2 +0,0 @@
[hyphenate-character-001.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[hyphens-none-014.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[hyphens-none-015.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[line-break-anywhere-012.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[line-break-anywhere-overrides-uax-behavior-015.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[overflow-wrap-break-word-007.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[text-align-justify-tabs-001.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[break-spaces-before-first-ideographic-char-001.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[break-spaces-before-first-ideographic-char-002.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[break-spaces-before-first-ideographic-char-003.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[break-spaces-before-first-ideographic-char-014.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[break-spaces-with-ideographic-space-002.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[break-spaces-with-ideographic-space-003.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[pre-wrap-008.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[pre-wrap-009.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[pre-wrap-011.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[pre-wrap-018.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[pre-wrap-020.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[pre-wrap-float-001.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[pre-wrap-leading-spaces-004.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[pre-wrap-leading-spaces-005.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[pre-wrap-leading-spaces-006.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[pre-wrap-leading-spaces-007.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[pre-wrap-leading-spaces-008.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[pre-wrap-leading-spaces-009.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[pre-wrap-leading-spaces-010.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[pre-wrap-leading-spaces-011.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[pre-wrap-leading-spaces-013.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[pre-wrap-leading-spaces-014.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[pre-wrap-leading-spaces-015.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[pre-wrap-leading-spaces-016.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[pre-wrap-leading-spaces-017.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[pre-wrap-tab-002.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[pre-wrap-tab-003.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[pre-wrap-tab-005.html]
expected: FAIL

View file

@ -0,0 +1,2 @@
[text-wrap-balance-overflow-002.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[textarea-break-spaces-002.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[textarea-pre-wrap-011.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[trailing-space-align-start.tentative.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[trailing-space-and-text-alignment-003.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[trailing-space-and-text-alignment-004.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[trailing-space-and-text-alignment-rtl-003.html]
expected: FAIL

View file

@ -1,6 +1,3 @@
[trailing-space-position-001.html]
[CSS Test: Positions of trailing collapsible spaces 1]
expected: FAIL
[CSS Test: Positions of trailing collapsible spaces 3]
expected: FAIL

View file

@ -1,2 +0,0 @@
[white-space-normal-011.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[white-space-pre-wrap-trailing-spaces-001.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[white-space-pre-wrap-trailing-spaces-005.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[white-space-pre-wrap-trailing-spaces-007.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[white-space-pre-wrap-trailing-spaces-008.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[white-space-pre-wrap-trailing-spaces-011.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[white-space-pre-wrap-trailing-spaces-012.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[white-space-pre-wrap-trailing-spaces-014.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[white-space-pre-wrap-trailing-spaces-015.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[white-space-pre-wrap-trailing-spaces-021.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[white-space-pre-wrap-trailing-spaces-022.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[white-space-pre-wrap-trailing-spaces-023.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[text-overflow-ellipsis-indent-001.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[ch-unit-008.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[ch-unit-016.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[ic-unit-013.html]
expected: FAIL

View file

@ -1,2 +0,0 @@
[ic-unit-014.html]
expected: FAIL

View file

@ -1,291 +0,0 @@
[line-break-ch-unit.html]
[3ch should fit]
expected: FAIL
[4ch should fit]
expected: FAIL
[5ch should fit]
expected: FAIL
[6ch should fit]
expected: FAIL
[7ch should fit]
expected: FAIL
[8ch should fit]
expected: FAIL
[9ch should fit]
expected: FAIL
[10ch should fit]
expected: FAIL
[11ch should fit]
expected: FAIL
[12ch should fit]
expected: FAIL
[13ch should fit]
expected: FAIL
[14ch should fit]
expected: FAIL
[15ch should fit]
expected: FAIL
[16ch should fit]
expected: FAIL
[17ch should fit]
expected: FAIL
[18ch should fit]
expected: FAIL
[19ch should fit]
expected: FAIL
[20ch should fit]
expected: FAIL
[21ch should fit]
expected: FAIL
[22ch should fit]
expected: FAIL
[23ch should fit]
expected: FAIL
[24ch should fit]
expected: FAIL
[25ch should fit]
expected: FAIL
[26ch should fit]
expected: FAIL
[27ch should fit]
expected: FAIL
[28ch should fit]
expected: FAIL
[29ch should fit]
expected: FAIL
[30ch should fit]
expected: FAIL
[31ch should fit]
expected: FAIL
[32ch should fit]
expected: FAIL
[33ch should fit]
expected: FAIL
[34ch should fit]
expected: FAIL
[35ch should fit]
expected: FAIL
[36ch should fit]
expected: FAIL
[37ch should fit]
expected: FAIL
[38ch should fit]
expected: FAIL
[39ch should fit]
expected: FAIL
[40ch should fit]
expected: FAIL
[41ch should fit]
expected: FAIL
[42ch should fit]
expected: FAIL
[43ch should fit]
expected: FAIL
[44ch should fit]
expected: FAIL
[45ch should fit]
expected: FAIL
[46ch should fit]
expected: FAIL
[47ch should fit]
expected: FAIL
[48ch should fit]
expected: FAIL
[49ch should fit]
expected: FAIL
[50ch should fit]
expected: FAIL
[51ch should fit]
expected: FAIL
[52ch should fit]
expected: FAIL
[53ch should fit]
expected: FAIL
[54ch should fit]
expected: FAIL
[55ch should fit]
expected: FAIL
[56ch should fit]
expected: FAIL
[57ch should fit]
expected: FAIL
[58ch should fit]
expected: FAIL
[59ch should fit]
expected: FAIL
[60ch should fit]
expected: FAIL
[61ch should fit]
expected: FAIL
[62ch should fit]
expected: FAIL
[63ch should fit]
expected: FAIL
[64ch should fit]
expected: FAIL
[65ch should fit]
expected: FAIL
[66ch should fit]
expected: FAIL
[67ch should fit]
expected: FAIL
[68ch should fit]
expected: FAIL
[69ch should fit]
expected: FAIL
[70ch should fit]
expected: FAIL
[71ch should fit]
expected: FAIL
[72ch should fit]
expected: FAIL
[73ch should fit]
expected: FAIL
[74ch should fit]
expected: FAIL
[75ch should fit]
expected: FAIL
[76ch should fit]
expected: FAIL
[77ch should fit]
expected: FAIL
[78ch should fit]
expected: FAIL
[79ch should fit]
expected: FAIL
[80ch should fit]
expected: FAIL
[81ch should fit]
expected: FAIL
[82ch should fit]
expected: FAIL
[83ch should fit]
expected: FAIL
[84ch should fit]
expected: FAIL
[85ch should fit]
expected: FAIL
[86ch should fit]
expected: FAIL
[87ch should fit]
expected: FAIL
[88ch should fit]
expected: FAIL
[89ch should fit]
expected: FAIL
[90ch should fit]
expected: FAIL
[91ch should fit]
expected: FAIL
[92ch should fit]
expected: FAIL
[93ch should fit]
expected: FAIL
[94ch should fit]
expected: FAIL
[95ch should fit]
expected: FAIL
[96ch should fit]
expected: FAIL
[97ch should fit]
expected: FAIL
[98ch should fit]
expected: FAIL
[99ch should fit]
expected: FAIL