Skip to content

wpt: Wait for web fonts before measuring in Ahem-based testharness tests - #49502

Merged
TimvdLippe merged 1 commit into
servo:mainfrom
gabrielecabrini:wpt-ahem
Nov 1, 2026
Merged

TimvdLippe merged 1 commit into
servo:mainfrom
gabrielecabrini:wpt-ahem

Conversation

@gabrielecabrini

Copy link
Copy Markdown
Contributor

These tests read layout synchronously while the page is parsing, before the
Ahem web font from /fonts/ahem.css has loaded. Their measurements then use a
fallback font's metrics, and the results depend on load timing. Run the
measurements after document.fonts.ready so the results are deterministic.

With the font loaded, every subtest of
block-in-inline-self-collapsing-margin-001.html passes, so its expectation
file is removed.

Fixes #49500
Fixes #49491
Fixes #49488
Fixes #49487
Fixes #43763

@servo-wpt-sync

Copy link
Copy Markdown
Collaborator

🤖 Opened new upstream WPT pull request (web-platform-tests/wpt#64511) with upstreamable changes.

@gabrielecabrini
gabrielecabrini marked this pull request as ready for review November 1, 2026 17:27
@servo-highfive servo-highfive removed the S-awaiting-review There is new code that needs to be reviewed. label Nov 1, 2026
@TimvdLippe
TimvdLippe added this pull request to the merge queue Nov 1, 2026
@servo-highfive servo-highfive added the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Nov 1, 2026
Merged via the queue into servo:main with commit 2b94d76 Nov 1, 2026
38 checks passed
@servo-highfive servo-highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Nov 1, 2026
jschwe pushed a commit to jschwe/servo that referenced this pull request Nov 2, 2026
These tests measure layout in `onload`, which can fire before web fonts
have loaded. Wait for `document.fonts.ready` first, as in servo#49493 and
servo#49502.

This also removes unused Ahem css in related test files.

Testing: These are test changes. With the font response delayed by 1s
(wptserve `trickle` pipe), the old tests fail on every run and the
updated tests pass on every run.
Fixes: servo#49075
Fixes: servo#49301
Fixes: servo#47132
Fixes: servo#47104
Fixes: servo#47563
Fixes: servo#47257
Fixes: servo#49238

---------

Signed-off-by: Gabriele Cabrini <[email protected]>
Signed-off-by: Euclid Ye <[email protected]>
Co-authored-by: Euclid Ye <[email protected]>
@gabrielecabrini
gabrielecabrini deleted the wpt-ahem branch November 2, 2026 20:00
jschwe added a commit to openharmony-rs/servo-ohos that referenced this pull request Nov 9, 2026
Every @font-face rule was fetched, sanitised and kept in memory as soon as
its stylesheet applied, even when no style used its family or its
unicode-range covered no character on the page. CSS Fonts 4 requires user
agents to only download fonts that are used.

A new rule now only registers an unloaded face, matched by its descriptors.
Font matching asks for the face once it selects it for a character, and the
loads it asked for start after layout. `FontFace.load()` and
`FontFaceSet.load()` ask for faces the same way. Faces whose sources are all
`local()` are still loaded right away.

Because matching now decides which faces load, adding @font-face rules
dirties the document so that text is matched again, `document.fonts.ready`
flushes layout, and a reflow that starts loads makes it pending again.

The font store now holds templates for faces whose data has not been
downloaded. `FontGroupFamilyTemplate::font_if_matches` skips them, but
`FontContext::matching_templates` and `FontContext::font` are also used
directly by the SVG font resolver, which would hit the "Should never create
a web font without data" panic in `PlatformFont::new_from_template` for any
SVG `<text>` using a web font. `FontContext::font` returns `None` for a web
font template without data, without caching the failure, so the face can be
used once it has loaded. The SVG font resolver asks for the face to be
loaded, like layout does when it matches one, and falls back to the next
family in the meantime.

An element whose font-family names a web font but that has no text never
picks the font for a character, but its first available font is needed
too, for instance for `line-height: normal` and font-relative lengths, and
Chrome and Firefox load it. After an element is restyled, layout asks for
the first available font of its style to be loaded if it is a web font that
has not been fetched. Only elements whose font differs from their parent's
are looked at, nothing is done while the document has no web fonts, and the
lookup runs once per font group. It does not create fonts, so system fonts
are not loaded for elements that never draw text.

Testing: A new WPT test checks that unused faces stay "unloaded". A canvas
test that expected a font not used by the page to be loaded now fails, as it
does in Chrome, Firefox and Safari. svg/painting/svg-with-outline.html,
css/css-transforms/transform-box/view-box-mutation-002.html and
resize-observer/svg.html keep their expected results instead of crashing,
and content-security-policy/font-src/font-stylesheet-font-blocked.sub.html
(a body without text) and preload/preload-resource-match.https.html (an
empty paragraph) still see the font fetched.

Fork note: second of four patches, meant for upstream. The upstream-based
version (branch lazy-webfonts) does not have the two fixes for SVG text and
for elements without text yet, which the servo-ohos PR 12 try run found.
Motivation: generated-UI pages that link Google Fonts CJK families declare
hundreds of unicode-range subsets (441 @font-face rules on the "keai"
concert-article test page), and Servo downloaded all of them (437 files,
10.5 MiB) where ArkWeb downloads 64.

Relies on: the previous patch (the load state on `FontFaceRuleInfo`, which is
how matching tells unloaded faces apart and how `FontFace.status` shows it);
upstream a8d5fda (servo#47564, its `FontFaceSet.load` font query is what makes
`document.fonts.load()` ask for faces); matching going through
`FontGroupFamilyTemplate::font_if_matches` and `char_in_unicode_range`
(components/shared/fonts/font_template.rs); `LayoutThread::reflow` returning a
`ReflowResult` and `can_skip_reflow_request_entirely` gating it; and the
readiness checks in `Document::maybe_fulfill_font_ready_promise` and
`Window::maybe_resolve_pending_screenshot_readiness_requests` reading
`FontContext::web_fonts_still_loading`.

Drop when upstream loads @font-face rules lazily: the test this patch adds,
css/css-font-loading/fontface-unused-stays-unloaded.html, passes on plain
upstream.

Two of upstream's MathML reftests, mo-lspace-rspace-dynamic and
operator-dictionary-empty-and-three-chars, pass on plain upstream and
fail with this patch. Their screenshots differ from the references only
in the red square that
`ensure_for_match_reftest("has_operator_spacing")` of
mathml/support/feature-detection.js adds when it measures no operator
spacing, which Servo doesn't implement. Why the check passes on plain
upstream wasn't traced; since servo#49485, upstream expects
operator-dictionary-empty-and-three-chars to fail as well.

Validation: `./mach test-wpt --release --processes 1 css/css-font-loading css/css-fonts html/canvas/element/text html/canvas/offscreen/text` (948 tests): the new fontface-unused-stays-unloaded passes; 2d.text.draw.fontface.notinpage now fails as in Chrome/Firefox/Safari; six "(drawing text in a canvas)" subtests of generic-family-keywords-003 failed with this patch on base/2026-08-29 (Chrome and Firefox pass them) and pass again since base/2026-10-08, so their expectations were dropped; the two @font-face layer-override subtests of css/css-cascade/layer-font-face-override (FAIL on plain upstream) pass with this patch, so their expectations were dropped too; matching/font-unicode-PUA subtests are intermittent (test race, both results seen); the only other unexpected result, font-variant-emoji-1 FAIL, also happens on plain ohos-main. `./mach test-wpt --release svg resize-observer css/css-transforms/transform-box` (1355 tests): all as expected (svg-with-outline, view-box-mutation-002 and resize-observer/svg crash without the SVG font resolver change). `./mach test-wpt --release css/css-fonts css/css-font-loading css/css-flexbox css/css-grid css/cssom-view css/css-inline css/css-text html/canvas/element/text html/canvas/offscreen/text content-security-policy/font-src preload svg` (8650 tests): no new unexpected results besides font-variant-emoji-1 and the known intermittent grid-self-alignment-fit-content-001; font-stylesheet-font-blocked.sub.html and preload-resource-match.https.html pass, and fail without the first-available-font change. Host release build, keai page served locally over HTTP/1.1 keep-alive (3 runs each): font files fetched 437 -> 66, RSS 525.3 -> 471.6 MiB, heap 340.1 -> 286.4 MiB, `--exit -o` screenshot byte-identical to ohos-main. The servo-webfont-eager-download reproducer (6 @font-face rules, 1 needed) fetches 1 font instead of 6. On a HarmonyOS phone, production build with `--features tracing-hitrace`, the keai page served from the host through `hdc rport`, measured with this patch and the next two applied (they change invalidation, not which fonts load): 437 -> 66 font files per load in each of 5 `servoperf page` loads, and PSS 30 s after launch 507.6 -> 467.9 MB (-7.8%, 4 interleaved loads each, every pair 35-49 MB apart). 2026-11-09 sync: `./mach test-wpt --processes 8 --timeout-multiplier 2 mathml` with release builds (2026-10-29): both reftests pass with the stack up to the previous patch and fail from this one on, with SpiderMonkey and QuickJS. Release builds of this sync's stack: font-face-unicode-range-no-match-no-download.html passes in the full QuickJS run and three runs with each engine, and fails on plain 0.8.0; the table-cell subtest of first-baseline-block-in-inline-001.html passes on plain 0.8.0, passes 3 of 3 times with the stack up to the previous patch and fails 3 of 3 times with this one, and passes 3 of 3 times when it measures after the load event.
AI-assisted: Yes (agent session; designed with the maintainer, written and tested by the agent, ported from upstream main to ohos-main's Rc<Promise> API)
AI-assisted: Yes (agent session; root-caused from the servo-ohos PR 12 try run and written by the agent)
AI-assisted: Yes (agent session; 2026-10-08 sync -- conflict resolution for upstream's RootedPromise and `icu_locale_core` changes, resolved to the code of the upstream-bound lazy-webfonts branch; MANIFEST.json entry re-inserted; dropped the generic-family-keywords-003 and layer-font-face-override FAIL expectations that now pass)
AI-assisted: Yes (agent session; 2026-10-09 restack -- folded in "fonts: Do not create fonts for web font faces that are not loaded yet" and "fonts: Load the first available web font of elements without text", which fixed regressions of this patch; the upstream FontFaceSet.load change replaces the dropped backport placeholder in "Relies on")
AI-assisted: Yes (agent session; 2026-11-09 sync -- folded in "fixup! fonts: Only load @font-face fonts that the page uses" (bisected and written by the agent between syncs): the expectations of two MathML reftests that fail with this patch, of which upstream's WPT sync (servo#49485) now has the same for operator-dictionary-empty-and-three-chars; upstream's WPT sync (servo#49485) added font-face-unicode-range-no-match-no-download.html with a FAIL expectation, and it passes with this patch, so the expectation goes; the same sync added first-baseline-block-in-inline-001.html, whose table-cell subtest passes on plain 0.8.0 and fails with this patch (bisected): the test measures while parsing, before Ahem, which this patch only fetches on use, has loaded; upstream's test fix servo#49502 (bisected) makes it wait, backported at the end of the stack)
Signed-off-by: Jonathan Schwender <[email protected]>
jschwe pushed a commit to openharmony-rs/servo-ohos that referenced this pull request Nov 9, 2026
…harness tests (servo#49502)

Upstream 2b94d76, which came after Servo 0.8.0, unchanged:

These tests read layout synchronously while the page is parsing, before
the
Ahem web font from /fonts/ahem.css has loaded. Their measurements then
use a
fallback font's metrics, and the results depend on load timing. Run the
measurements after document.fonts.ready so the results are
deterministic.

With the font loaded, every subtest of
block-in-inline-self-collapsing-margin-001.html passes, so its
expectation
file is removed.

Fixes servo#49500
Fixes servo#49491
Fixes servo#49488
Fixes servo#49487
Fixes servo#43763

Fork note: with "fonts: Only load @font-face fonts that the page uses",
Ahem is only fetched once text uses it, so on 0.8.0 the table-cell
subtest of first-baseline-block-in-inline-001.html measured a fallback
font and failed. Drop this patch at the first sync onto a release that
contains servo#49502, where it applies empty.

Validation: the five changed tests, release builds of this sync's stack on Servo 0.8.0, three runs with SpiderMonkey and three with QuickJS: all as expected (96 subtests each), the table-cell subtest of first-baseline-block-in-inline-001.html passes in each, without an expectation; with the stack's previous version of the test it fails 3 of 3 times. On upstream, bisected: the code of "fonts: Only load @font-face fonts that the page uses" on upstream's commits after 0.8.0 fails it until this commit and passes it from this commit on.
AI-assisted: Yes (agent session; backported unchanged by the agent during the 2026-11-09 sync)
Signed-off-by: Gabriele Cabrini <[email protected]>
Signed-off-by: Jonathan Schwender <[email protected]>
jschwe added a commit to openharmony-rs/servo-ohos that referenced this pull request Nov 10, 2026
Every @font-face rule was fetched, sanitised and kept in memory as soon as
its stylesheet applied, even when no style used its family or its
unicode-range covered no character on the page. CSS Fonts 4 requires user
agents to only download fonts that are used.

A new rule now only registers an unloaded face, matched by its descriptors.
Font matching asks for the face once it selects it for a character, and the
loads it asked for start after layout. `FontFace.load()` and
`FontFaceSet.load()` ask for faces the same way. Faces whose sources are all
`local()` are still loaded right away.

Because matching now decides which faces load, adding @font-face rules
dirties the document so that text is matched again, `document.fonts.ready`
flushes layout, and a reflow that starts loads makes it pending again.

The font store now holds templates for faces whose data has not been
downloaded. `FontGroupFamilyTemplate::font_if_matches` skips them, but
`FontContext::matching_templates` and `FontContext::font` are also used
directly by the SVG font resolver, which would hit the "Should never create
a web font without data" panic in `PlatformFont::new_from_template` for any
SVG `<text>` using a web font. `FontContext::font` returns `None` for a web
font template without data, without caching the failure, so the face can be
used once it has loaded. The SVG font resolver asks for the face to be
loaded, like layout does when it matches one, and falls back to the next
family in the meantime.

An element whose font-family names a web font but that has no text never
picks the font for a character, but its first available font is needed
too, for instance for `line-height: normal` and font-relative lengths, and
Chrome and Firefox load it. After an element is restyled, layout asks for
the first available font of its style to be loaded if it is a web font that
has not been fetched. Only elements whose font differs from their parent's
are looked at, nothing is done while the document has no web fonts, and the
lookup runs once per font group. It does not create fonts, so system fonts
are not loaded for elements that never draw text.

Testing: A new WPT test checks that unused faces stay "unloaded". A canvas
test that expected a font not used by the page to be loaded now fails, as it
does in Chrome, Firefox and Safari. svg/painting/svg-with-outline.html,
css/css-transforms/transform-box/view-box-mutation-002.html and
resize-observer/svg.html keep their expected results instead of crashing,
and content-security-policy/font-src/font-stylesheet-font-blocked.sub.html
(a body without text) and preload/preload-resource-match.https.html (an
empty paragraph) still see the font fetched.

Fork note: second of four patches, meant for upstream. The upstream-based
version (branch lazy-webfonts) does not have the two fixes for SVG text and
for elements without text yet, which the servo-ohos PR 12 try run found.
Motivation: generated-UI pages that link Google Fonts CJK families declare
hundreds of unicode-range subsets (441 @font-face rules on the "keai"
concert-article test page), and Servo downloaded all of them (437 files,
10.5 MiB) where ArkWeb downloads 64.

Relies on: the previous patch (the load state on `FontFaceRuleInfo`, which is
how matching tells unloaded faces apart and how `FontFace.status` shows it);
upstream a8d5fda (servo#47564, its `FontFaceSet.load` font query is what makes
`document.fonts.load()` ask for faces); matching going through
`FontGroupFamilyTemplate::font_if_matches` and `char_in_unicode_range`
(components/shared/fonts/font_template.rs); `LayoutThread::reflow` returning a
`ReflowResult` and `can_skip_reflow_request_entirely` gating it; and the
readiness checks in `Document::maybe_fulfill_font_ready_promise` and
`Window::maybe_resolve_pending_screenshot_readiness_requests` reading
`FontContext::web_fonts_still_loading`.

Drop when upstream loads @font-face rules lazily: the test this patch adds,
css/css-font-loading/fontface-unused-stays-unloaded.html, passes on plain
upstream.

Two of upstream's MathML reftests, mo-lspace-rspace-dynamic and
operator-dictionary-empty-and-three-chars, pass on plain upstream and
fail with this patch. Their screenshots differ from the references only
in the red square that
`ensure_for_match_reftest("has_operator_spacing")` of
mathml/support/feature-detection.js adds when it measures no operator
spacing, which Servo doesn't implement. Why the check passes on plain
upstream wasn't traced; since servo#49485, upstream expects
operator-dictionary-empty-and-three-chars to fail as well.

Validation: `./mach test-wpt --release --processes 1 css/css-font-loading css/css-fonts html/canvas/element/text html/canvas/offscreen/text` (948 tests): the new fontface-unused-stays-unloaded passes; 2d.text.draw.fontface.notinpage now fails as in Chrome/Firefox/Safari; six "(drawing text in a canvas)" subtests of generic-family-keywords-003 failed with this patch on base/2026-08-29 (Chrome and Firefox pass them) and pass again since base/2026-10-08, so their expectations were dropped; the two @font-face layer-override subtests of css/css-cascade/layer-font-face-override (FAIL on plain upstream) pass with this patch, so their expectations were dropped too; matching/font-unicode-PUA subtests are intermittent (test race, both results seen); the only other unexpected result, font-variant-emoji-1 FAIL, also happens on plain ohos-main. `./mach test-wpt --release svg resize-observer css/css-transforms/transform-box` (1355 tests): all as expected (svg-with-outline, view-box-mutation-002 and resize-observer/svg crash without the SVG font resolver change). `./mach test-wpt --release css/css-fonts css/css-font-loading css/css-flexbox css/css-grid css/cssom-view css/css-inline css/css-text html/canvas/element/text html/canvas/offscreen/text content-security-policy/font-src preload svg` (8650 tests): no new unexpected results besides font-variant-emoji-1 and the known intermittent grid-self-alignment-fit-content-001; font-stylesheet-font-blocked.sub.html and preload-resource-match.https.html pass, and fail without the first-available-font change. Host release build, keai page served locally over HTTP/1.1 keep-alive (3 runs each): font files fetched 437 -> 66, RSS 525.3 -> 471.6 MiB, heap 340.1 -> 286.4 MiB, `--exit -o` screenshot byte-identical to ohos-main. The servo-webfont-eager-download reproducer (6 @font-face rules, 1 needed) fetches 1 font instead of 6. On a HarmonyOS phone, production build with `--features tracing-hitrace`, the keai page served from the host through `hdc rport`, measured with this patch and the next two applied (they change invalidation, not which fonts load): 437 -> 66 font files per load in each of 5 `servoperf page` loads, and PSS 30 s after launch 507.6 -> 467.9 MB (-7.8%, 4 interleaved loads each, every pair 35-49 MB apart). 2026-11-09 sync: `./mach test-wpt --processes 8 --timeout-multiplier 2 mathml` with release builds (2026-10-29): both reftests pass with the stack up to the previous patch and fail from this one on, with SpiderMonkey and QuickJS. Release builds of this sync's stack: font-face-unicode-range-no-match-no-download.html passes in the full QuickJS run and three runs with each engine, and fails on plain 0.8.0; the table-cell subtest of first-baseline-block-in-inline-001.html passes on plain 0.8.0, passes 3 of 3 times with the stack up to the previous patch and fails 3 of 3 times with this one, and passes 3 of 3 times when it measures after the load event.
AI-assisted: Yes (agent session; designed with the maintainer, written and tested by the agent, ported from upstream main to ohos-main's Rc<Promise> API)
AI-assisted: Yes (agent session; root-caused from the servo-ohos PR 12 try run and written by the agent)
AI-assisted: Yes (agent session; 2026-10-08 sync -- conflict resolution for upstream's RootedPromise and `icu_locale_core` changes, resolved to the code of the upstream-bound lazy-webfonts branch; MANIFEST.json entry re-inserted; dropped the generic-family-keywords-003 and layer-font-face-override FAIL expectations that now pass)
AI-assisted: Yes (agent session; 2026-10-09 restack -- folded in "fonts: Do not create fonts for web font faces that are not loaded yet" and "fonts: Load the first available web font of elements without text", which fixed regressions of this patch; the upstream FontFaceSet.load change replaces the dropped backport placeholder in "Relies on")
AI-assisted: Yes (agent session; 2026-11-09 sync -- folded in "fixup! fonts: Only load @font-face fonts that the page uses" (bisected and written by the agent between syncs): the expectations of two MathML reftests that fail with this patch, of which upstream's WPT sync (servo#49485) now has the same for operator-dictionary-empty-and-three-chars; upstream's WPT sync (servo#49485) added font-face-unicode-range-no-match-no-download.html with a FAIL expectation, and it passes with this patch, so the expectation goes; the same sync added first-baseline-block-in-inline-001.html, whose table-cell subtest passes on plain 0.8.0 and fails with this patch (bisected): the test measures while parsing, before Ahem, which this patch only fetches on use, has loaded; upstream's test fix servo#49502 (bisected) makes it wait, backported at the end of the stack)
Signed-off-by: Jonathan Schwender <[email protected]>
jschwe pushed a commit to openharmony-rs/servo-ohos that referenced this pull request Nov 10, 2026
…harness tests (servo#49502)

These tests read layout synchronously while the page is parsing, before
the
Ahem web font from /fonts/ahem.css has loaded. Their measurements then
use a
fallback font's metrics, and the results depend on load timing. Run the
measurements after document.fonts.ready so the results are
deterministic.

With the font loaded, every subtest of
block-in-inline-self-collapsing-margin-001.html passes, so its
expectation
file is removed.

Fixes servo#49500
Fixes servo#49491
Fixes servo#49488
Fixes servo#49487
Fixes servo#43763

Signed-off-by: Gabriele Cabrini <[email protected]>

Fork note: upstream 2b94d76, which came after Servo 0.8.0, backported
unchanged. With "fonts: Only load @font-face fonts that the page
uses", Ahem is only fetched once text uses it, so on 0.8.0 the
table-cell subtest of first-baseline-block-in-inline-001.html measured
a fallback font and failed. Drop this patch at the first sync onto a
release that contains servo#49502, where it applies empty.

Validation: the five changed tests, release builds of this sync's stack on Servo 0.8.0, three runs with SpiderMonkey and three with QuickJS: all as expected (96 subtests each), the table-cell subtest of first-baseline-block-in-inline-001.html passes in each, without an expectation; with the stack's previous version of the test it fails 3 of 3 times. On upstream, bisected: the code of "fonts: Only load @font-face fonts that the page uses" on upstream's commits after 0.8.0 fails it until this commit and passes it from this commit on.
AI-assisted: Yes (agent session; backported unchanged by the agent during the 2026-11-09 sync)
Signed-off-by: Jonathan Schwender <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment