Repository navigation
Description
Currently we always use the first font that matches computed styles, regardless of the text being rendered:
servo/components/layout_2020/flow/inline.rs
Line 617 in c0d9ee0
| .first(font_context) |
If that font doesn’t have glyphs some of the characters in the text, since Harfbuzz is only given one font it’ll render blank rectangles (a.k.a. tofu, a.k.a. the "notdef" glyph). Instead, we should (before invoking Harfbuzz) detect this and fall back to the next-best matching font, and so on until we tried all matching font. Even then, we should try a last resort font that hopefully makes text readable (even if not in the desired style). This means that a single DOM text node can end up split into multiple text runs given to Harfbuzz, each with a different font.
https://drafts.csswg.org/css-fonts/#font-matching-algorithm defines a fairly precise algorithm for this. It discusses combining code points in a separate section https://drafts.csswg.org/css-fonts/#cluster-matching, but we should likely consider the text one grapheme cluster https://drafts.csswg.org/css-text/#grapheme-cluster at a time. The absence of combining code points "just" leads to clusters that happen to only contain one code point.
Touching font code may be the opportunity to move away from using Fontconfig/FreeType/Harfbuzz etc directly in components/gfx, and use https://github.com/servo/font-kit and https://github.com/linebender/skribo instead. As for this writing Skribo still needs significant work and contains more docs about problems to solve in order to implement high-quality text than code solving them. But I don’t know if components/gfx does much better on this anyway. I feel improving Skribo is the way forward.
See also: servo/font-kit#99, servo/font-kit#100
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsIn progress
- StatusShow more project fieldsNo status
Activity
khaledhosny commentedon Mar 3, 2020
Chrome does something a bit more clever, it shapes first then detects missing characters based on the shaping output and re-shapes them with fallback font(s) (with some optimization to avoid doing too much reshaping with long fallback lists). This is an improvement compared to the strategy described above as HarfBuzz will apply NFC or NFD normalization when a character is missing from the font, so if input has decomposed form and the font only supports composed and vice versa the sequence will still be rendered. This also has the potential of making web fonts smaller by allowing them to skip the composed glyphs and support only the decomposed form.
mrobinson commentedon Sep 28, 2023
Yes, work has recently restarted on the new layout engine so we hope to fix issues like these in the next few months, even though the issue was identified 3 years ago.