Skip to content

insufficient font fallback handling fails to render cjk glyphs (and other glyphs) #17267

Open

Description

@hinaria

hi there!

servo does not "properly" handle font family fallbacks: it effectively uses a single fallback font (when no font family is specified, when serif or sans-serif is specified, etc.

further, if no explicit font family is specified then the single fallback will be used. similarly, serif and sans-serif resolve to a single font family. on most platforms, the default fallback and the sans-serif families do not support cjk scripts, emoji, or other obscure glyphs, and so they won't render:

example

<!-- fail -->                  <div>のコ Hello! ✂📋👌</div>
<!-- fail -->                  <div style="font-family: Arial, serif, sans-serif">のコ Hello! ✂📋👌</div>
<!-- ok, only if installed --> <div style="font-family: 'MS Gothic'">のコ Hello! ✂📋👌</div>
<!-- ok, only if installed --> <div style="font-family: Meiryo">のコ Hello! ✂📋👌</div>
  • fn last_resort_font_families (/components/gfx/platform/{platform-name}/font_list.rs), returns a static list of potential fallback font families
  • FontCache::last_resort_font_template() (its only caller) from this list, finds the first font family that exists on the user's system, and discards the rest
  • callers of FontCache::last_resort_font_template() then adds that single fallback font to their candidate font templates

on windows, the fallback list is simply arial. macos uses arial unicode ms, arial and linux is a trio of fira sans, dejavu sans, arial. of these, only arial unicode ms (which is on the macos list) has support for cjk scripts, but that family isn't installed by default, so for many, servo will use arial.


in the short term, we could improve this situation by adding a few more fallbacks to the list of fallback font families (eg, arial + ms gothic / one that supports cjk glyphs), and then modifying FontContext::layout_font_group_for_style() to add all fallbacks to the list of candidate font templates which is used in layout and rendering.

we can't add too many fallbacks using the current scheme though, because FontContext::layout_font_group_for_style() returns a SmallVec<8> for each requested font group, and simply adding all possible fallbacks would massively bloat that vec. firefox's font fallback implementation for example (windows implementation linked) selects between 40(!) font families!

long term, i see servo needing a static decision table, similar to the current firefox implementation, and ways to take into account the user's own preferences for sans-serif and serif defaults.


i'm happy to help work on a fix, but i'm not a servo team member, so i'm not sure what the best path forward is (or if i'm completely wrong with this report).

Activity

  1. changed the title [-]insufficient font fallback handling[/-] [+]insufficient font fallback handling does not handle cjk glyphs (and other glyphs)[/+] on Jun 10, 2017
  2. changed the title [-]insufficient font fallback handling does not handle cjk glyphs (and other glyphs)[/-] [+]insufficient font fallback handling fails to render cjk glyphs (and other glyphs)[/+] on Jun 10, 2017
  3. emilio commented on Jun 11, 2017

    @emilio
    Member

    Your report sounds accurate to me, thanks for the detailed description of the problem! :)

    I'm not too much into fonts and text layout, but @glennw or @mbrubeck should be able to point out if there's any problem with the approach you're describing (though tbh if it works for FF, it likely works for Servo too).

    It'd be lovely to get fixes for this on tree, and I'd be happy to answer questions if needed :)

    Thanks a lot again for the report!

  4. added
    I-enhancement
    on Jun 11, 2017
  5. tigercosmos commented on Aug 28, 2017

    @tigercosmos
    Contributor

    It seems the issue is still not fixed. I would like to try.

  6. tigercosmos commented on Oct 17, 2017

    @tigercosmos
    Contributor

    @jdm How to add alluni.txt to test font fallback handling? Are there any existed test files for font fallback handling?

  7. jdm commented on Oct 17, 2017

    @jdm
    Member

    @tigercosmos Tests for font rendering always need some kind of reference rendering that we can compare against. I don't see how to turn alluni.txt into a useful test, but we could add individual reference tests for specific characters that we want to ensure receive a proper font fallback. https://github.com/servo/servo/blob/master/tests/wpt/mozilla/tests/css/per_glyph_font_fallback_a.html is an example of a test for per-glyph font fallback behaviour (and https://github.com/servo/servo/blob/master/tests/wpt/mozilla/tests/css/per_glyph_font_fallback_ref.html is the reference rendering).

  8. jonleighton commented on Feb 6, 2018

    @jonleighton
    Contributor

    @tigercosmos are you still working on this? If not, I'd like to take a look. I started investigating area of the code this while looking into #17321 and have drawn pretty much the same conclusions as the OP. I think a good approach would be to do a rough port of Firefox's fallback code, although Servo's code will need some restructuring to do that, I think.

  9. tigercosmos commented on Feb 6, 2018

    @tigercosmos
    Contributor

    @jonleighton No. please take it. Thanks for working on it. :)

    I think a good approach would be to do a rough port of Firefox's fallback code

    I agree.

  10. jonleighton commented on Feb 6, 2018

    @jonleighton
    Contributor

    FWIW the spec leaves quite a lot of scope for the UA to decide how fallback works. Specifically, step 7:

    If there are no more font families to be evaluated and no matching face has been found, then the user agent performs a system font fallback procedure to find the best match for the character to be rendered. The result of this procedure may vary across user agents.

  11. tigercosmos commented on Feb 6, 2018

    @tigercosmos
    Contributor

    For system font fallback, there are some discussions in #4901

  12. added a commit that references this issue on Feb 11, 2018
  13. 23 remaining items

  14. added 2 commits that reference this issue on May 15, 2018
  15. added 4 commits that reference this issue on May 19, 2018
  16. lino-levan commented on Aug 17, 2023

    @lino-levan

    Ran into this one earlier today using the minibrowser build on the servo website. I'm not sure if that build is up to date.

    image
  17. mrobinson commented on Apr 21, 2024

    @mrobinson
    Member

    Status of this:

    • Fixes for macOS at fonts: Fix emoji font selection on macOS #32122
    • Windows seems to be working properly after making sure that "Segoe UI Emoji" is added to the fallback list.
    • Linux: Not sure yet, but I suspect it's like Windows
    • All platforms: Need to detect when the font is a font with color glyphs and pass this information on to WebRender.
  18. mrobinson commented on Dec 19, 2025

    @mrobinson
    Member

    Update: Many emoji fonts are now COLR v1 fonts, so we have fallen behind a bit unfortunately. Adding COLR v1 supports requires more work in WebRender.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-layout/textA-platform/fontsI-enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions