Skip to content

Implement font fallback based on grapheme clusters #25883

Open

Description

@SimonSapin

Currently we always use the first font that matches computed styles, regardless of the text being rendered:

.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

Activity

  1. added
    A-layout/2020
    on Mar 3, 2020
  2. khaledhosny commented on Mar 3, 2020

    @khaledhosny

    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.

  3. moved this to Architecture in Layout 2020on Apr 28, 2023
  4. mrobinson commented on Sep 28, 2023

    @mrobinson
    Member

    wait, this issue stuck for 3 years? any possible fix for this issue?

    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.

  5. moved this from Architecture to In progress in Layout 2020on Nov 17, 2023
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/2020

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions