Repository navigation
Description
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:
<!-- 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 familiesFontCache::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
[-]insufficient font fallback handling[/-][+]insufficient font fallback handling does not handle cjk glyphs (and other glyphs)[/+][-]insufficient font fallback handling does not handle cjk glyphs (and other glyphs)[/-][+]insufficient font fallback handling fails to render cjk glyphs (and other glyphs)[/+]emilio commentedon Jun 11, 2017
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!
tigercosmos commentedon Aug 28, 2017
It seems the issue is still not fixed. I would like to try.

jdm commentedon Oct 4, 2017
https://angband.pl/doc/alluni.txt is another useful test.
tigercosmos commentedon Oct 17, 2017
@jdm How to add
alluni.txtto test font fallback handling? Are there any existed test files for font fallback handling?jdm commentedon Oct 17, 2017
@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.txtinto 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).jonleighton commentedon Feb 6, 2018
@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.
tigercosmos commentedon Feb 6, 2018
@jonleighton No. please take it. Thanks for working on it. :)
I agree.
jonleighton commentedon Feb 6, 2018
FWIW the spec leaves quite a lot of scope for the UA to decide how fallback works. Specifically, step 7:
tigercosmos commentedon Feb 6, 2018
For
system font fallback, there are some discussions in #4901Lazy load fonts in a FontGroup
23 remaining items
Implement font fallback
Implement font fallback
Implement font fallback
Auto merge of #20506 - jonleighton:font-fallback, r=<try>
Auto merge of #20506 - jonleighton:font-fallback, r=<try>
Implement font fallback
Implement font fallback
Auto merge of #20506 - jonleighton:font-fallback, r=<try>
Auto merge of #20506 - jonleighton:font-fallback, r=<try>
Auto merge of #20506 - jonleighton:font-fallback, r=emilio,mbrubeck
Auto merge of #20506 - jonleighton:font-fallback, r=emilio,mbrubeck
lino-levan commentedon Aug 17, 2023
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.
mrobinson commentedon Apr 21, 2024
Status of this:
fonts: Fix emoji font selection on macOS (#32122)
mrobinson commentedon Dec 19, 2025
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.