Skip to content

script: Expose FontFace and FontFaceSet to workers - #50046

Merged
TimvdLippe merged 2 commits into
servo:mainfrom
rogerkorantenng:fontface-in-workers
Nov 19, 2026
Merged

TimvdLippe merged 2 commits into
servo:mainfrom
rogerkorantenng:fontface-in-workers

Conversation

@rogerkorantenng

@rogerkorantenng rogerkorantenng commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

This exposes FontFace, FontFaceSet, FontFaceSetLoadEvent and self.fonts to workers. The code mostly needed the document for a base URL and quirks mode, so it now uses the global instead. Workers use their own URL, like Firefox, and no-quirks mode. web_font_context() moved from Window to GlobalScope.

In a worker, the font set switches to loaded once none of its fonts are loading. Without that, self.fonts.ready never resolves.

Workers still share the page's FontContext, so fonts loaded in a worker aren't stored yet, and they can delay the page's document.fonts.ready. The next PR gives each worker its own.

Testing: offscreencanvas-worker-font-load-crash.html, fontfaceset-load-var.html, fontfaceset-load-css-wide-keywords.html and two worker OffscreenCanvas text tests now pass. I also ran the same FontFace checks in a worker and a window, and they behave the same.

Part of #49892

@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Nov 18, 2026
@TimvdLippe TimvdLippe added the T-linux-wpt Do a try run of the WPT label Nov 18, 2026
@github-actions github-actions Bot removed the T-linux-wpt Do a try run of the WPT label Nov 18, 2026
@github-actions

Copy link
Copy Markdown

🔨 Triggering try run (#44393623195) for Linux (WPT)

@TimvdLippe TimvdLippe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! I have two small comments

@servo-highfive servo-highfive removed the S-awaiting-review There is new code that needs to be reviewed. label Nov 18, 2026
@github-actions

Copy link
Copy Markdown

Test results for linux-wpt from try job (#44393623195):

Flaky unexpected result (18)
  • CRASH [expected OK] /IndexedDB/idbcursor_continue_index.any.html
  • CRASH [expected ERROR] /IndexedDB/idbfactory_deleteDatabase.any.serviceworker.html
  • OK /IndexedDB/idbtransaction-oncomplete.any.worker.html (#42804)
    • FAIL [expected PASS] subtest: IDBTransaction - complete event

      assert_array_equals: lengths differ, expected array ["upgradeneeded", "complete", "success", "opencursor"] length 4, got ["upgradeneeded", "complete", "success"] length 3
      

  • FAIL [expected PASS] /css/css-backgrounds/background-size-041.html
  • TIMEOUT [expected PASS] /dom/nodes/moveBefore/moveBefore-range-iframe-crash.html
  • OK /html/browsers/browsing-the-web/navigating-across-documents/008.html (#24456)
    • PASS [expected FAIL] subtest: Link with onclick form submit to javascript url and href navigation
  • TIMEOUT /html/browsers/history/the-history-interface/001.html (#12580)
    • FAIL [expected PASS] subtest: traversing history must also traverse hash changes

      assert_equals: (this could cause other failures later on) expected "" but got "test"
      

  • ERROR [expected TIMEOUT] /html/browsers/the-window-object/open-close/open-features-is-popup-condition.html?combination
  • OK /html/semantics/embedded-content/media-elements/seeking/seek-to-currentTime.html (#46014)
    • FAIL [expected PASS] subtest: seek to currentTime

      assert_greater_than: seekable ranges expected a number greater than 0 but got 0
      

  • CRASH [expected FAIL] /html/semantics/interestfor/interestfor-plain-inline-element.tentative.html
  • TIMEOUT [expected OK] /html/user-activation/navigation-state-reset-sameorigin.html (#45151)
    • TIMEOUT [expected FAIL] subtest: Post-navigation state reset.

      Test timed out
      

  • OK /infrastructure/testdriver/click_iframe.html (#47688)
    • FAIL [expected PASS] subtest: TestDriver click on a document in an iframe

      Unhandled rejection: assert_unreached: click failed Reached unreachable code
      

  • FAIL [expected PASS] /mathml/presentation-markup/operators/mo-form-dynamic-002.html (#49169)
  • PASS [expected FAIL] /mathml/presentation-markup/operators/operator-dictionary-empty-and-three-chars.html (#49173)
  • OK /pointerevents/pointerevent_pointercapture_in_frame.html?pen (#45536)
    • PASS [expected FAIL] subtest: Test penpointer capture in same-origin frame: Pointer down at inner frame and set pointer capture.
    • PASS [expected FAIL] subtest: Test penpointer capture in same-origin frame: Pointer down at inner frame and set pointer capture to outer frame should not capture.
  • OK /sanitizer-api/sanitizer-inert-document.html (#45260)
    • FAIL [expected PASS] subtest: Test whether setHTML loads the image.

      promise_test: Unhandled rejection with value: "The server observed a request. It shouldn't have."
      

    • PASS [expected FAIL] subtest: Test whether setHTMLUnsafe loads the image.
  • OK /upgrade-insecure-requests/gen/sharedworker-classic-data.meta/upgrade/websocket.https.html
    • FAIL [expected PASS] subtest: Upgrade-Insecure-Requests: Expects allowed for websocket to cross-ws-downgrade origin and no-redirect redirection from https context.

      assert_equals: The resource request should be 'allowed'. expected "allowed" but got "blocked"
      

    • FAIL [expected PASS] subtest: Upgrade-Insecure-Requests: Expects allowed for websocket to same-ws-downgrade origin and no-redirect redirection from https context.

      assert_equals: The resource request should be 'allowed'. expected "allowed" but got "blocked"
      

  • OK /webdriver/tests/classic/perform_actions/pointer_mouse.py (#49662)
    • FAIL [expected PASS] subtest: test_down_closes_browsing_context[without up]

      Failed: DID NOT RAISE <class 'webdriver.error.NoSuchWindowException'>
      

Stable unexpected results that are known to be intermittent (9)
  • OK /beacon/beacon-basic.https.window.html (#41723)
    • FAIL [expected PASS] subtest: Payload size restriction should be accumulated: type = blob

      assert_false: expected false got true
      

  • OK /css/CSS2/floats/float-in-block-in-inline-position-001.html (#49534)
    • FAIL [expected PASS] subtest: A line after a bordered block containing a float avoids it at the block's position (sibling)

      assert_equals: the line's block-axis position expected 108 but got 106.35000000000002
      

  • OK /css/CSS2/floats/floats-in-inline-content-intrinsic-size-001.html (#49728)
    • FAIL [expected PASS] subtest: An empty float next to text (emptyFloatAndText, min-content)

      assert_equals: expected 70 but got 50
      

  • OK /css/css-fonts/variations/at-font-face-font-matching.html (#20684)
    • FAIL [expected PASS] subtest: Matching font-weight: '430' should prefer '420 440' over '450 460'

      assert_equals: Unexpected font on test element expected 487 but got 532
      

    • FAIL [expected PASS] subtest: Matching font-stretch: '100%' should prefer '110% 120%' over '115% 116%'

      assert_equals: Unexpected font on test element expected 487 but got 532
      

    • FAIL [expected PASS] subtest: Matching font-style: 'italic' should prefer 'oblique 5deg 10deg' over 'oblique 5deg'

      assert_equals: Unexpected font on test element expected 487 but got 532
      

    • FAIL [expected PASS] subtest: Matching font-style: 'oblique -21deg' should prefer 'oblique -60deg -40deg' over 'oblique -10deg'

      assert_equals: Unexpected font on test element expected 487 but got 532
      

  • TIMEOUT [expected ERROR] /html/browsers/history/the-location-interface/location_replace_session_history.html (#41896)
  • PASS [expected FAIL] /mathml/presentation-markup/operators/mo-lspace-rspace-dynamic.html (#49168)
  • ERROR [expected TIMEOUT] /performance-timeline/not-restored-reasons/abort-block-bfcache.window.html (#42802)
  • OK /preload/prefetch-document.html (#37210)
    • PASS [expected FAIL] subtest: different-site document prefetch with 'as=document' should not be consumed
  • OK /user-timing/measure_associated_with_navigation_timing.html (#47273)
    • FAIL [expected PASS] subtest: loadTime plus loadEventEnd to a mark "a" should equal to navigationStart to "a".

      assert_equals: loadTime plus loadEventEnd to a mark "a" should equal to navigationStart to "a". expected 22.35 but got 22.349999999999998
      

Stable unexpected results (2)
  • OK /fetch/h1-parsing/h1-parsing.tentative.html?include=after-colon
    • FAIL [expected PASS] subtest: SP at <after-colon> should fetch without an error

      promise_test: Unhandled rejection with value: object "TypeError: Network error: HTTP failure: client error (SendRequest)"
      

  • OK /html/canvas/offscreen/text/2d.text.measure.width.nullCharacter.worker.html
    • FAIL [expected PASS] subtest: Null character does not take up space

      assert_equals: ctx.measureText('\u0000').width === 0 (got 10[number], expected 0[number]) expected 0 but got 10
      

@github-actions

Copy link
Copy Markdown

⚠️ Try run (#44393623195) failed!

@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Nov 18, 2026
@TimvdLippe

Copy link
Copy Markdown
Contributor

I don't think these test failures are related, but they do confuse me. Did you rebase this PR on main recently? That usually fixes such issues.

Expose `FontFace`, `FontFaceSet`, `FontFaceSetLoadEvent` and
`self.fonts` to workers. Workers have no document, so they parse font
descriptors against their own URL in no-quirks mode, and
`web_font_context()` moves from `Window` to `GlobalScope`.

A worker's font face set is never pending on the environment, so it
switches to loaded as soon as none of its fonts are loading.

Fonts loaded in a worker are not added to a font context yet, because
workers still share the page's `FontContext`.

Signed-off-by: rogerkorantenng <[email protected]>
Signed-off-by: rogerkorantenng <[email protected]>
@rogerkorantenng

Copy link
Copy Markdown
Contributor Author

I hadn't, it was based on a commit from this morning. I've rebased it on main. The failures are known intermittents apart from h1-parsing, which is a network error, so I don't think they're related either.

@TimvdLippe
TimvdLippe added this pull request to the merge queue Nov 19, 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 19, 2026
Merged via the queue into servo:main with commit f0c02de Nov 19, 2026
35 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 19, 2026
willypuzzle pushed a commit to willypuzzle/servo that referenced this pull request Nov 20, 2026
Dedicated and shared workers used to share the page's `FontContext`.
Fonts a worker loaded could end up in the page, and a font loading in a
worker made the page's `document.fonts.ready` wait for it. Now each of
these workers gets its own empty `FontContext`, which matches the spec
("Within a Worker document, the font source is initially empty") and
what Firefox does. Fonts loaded with `self.fonts` are stored there, and
their WebRender resources are freed when the worker shuts down, like the
layout thread does for pages. Service workers already have their own
`FontContext`, so they're unchanged.

This also fixes loading a second `FontFace` for a URL that was already
loaded for the same family. That failed with "Failed to load font data"
on pages too, because script loads skipped URLs that were already
loaded.

Testing: a page's `document.fonts.ready` no longer waits for a slow font
in a worker (7 ms instead of 2.7 s). With servo#49915 applied locally, a
worker's canvas draws with the font the worker loaded, and can't see
fonts loaded by the page. I also created and closed 20 workers that each
load a font and draw text. Two page tests that loaded the same
`FontFace` twice no longer error, and the WPT `workers` and canvas text
tests still pass.

This builds on servo#50046, so its commits are included here until that one
lands.

Part of servo#49892

Signed-off-by: rogerkorantenng <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-awaiting-review There is new code that needs to be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants