Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graceful handling of font files that fail to load (invalid font tables, bad format, etc.) #2991

Open
qwerasd205 opened this issue Dec 17, 2024 · 7 comments
Labels
font Issue within the font stack (typically src/font)

Comments

@qwerasd205
Copy link
Collaborator

We need to handle bad fonts (corrupted, or unsupported due to missing tables) better. Currently they cause rendering issues when they appear as fallbacks and they cause straight up crashes when set as the primary font. We need to handle this gracefully, and the font discovery mechanism(s) need to be updated to exclude bitmap fonts.

@anund
Copy link
Contributor

anund commented Dec 17, 2024

ghostty --font-family=Fixed is an example. Its a ~default font shipped as part of X11 I noticed used to produced scrambled text and now crashes.

@mitchellh mitchellh added the font Issue within the font stack (typically src/font) label Dec 17, 2024
@mitchellh
Copy link
Contributor

ghostty --font-family=Fixed is an example. It's a ~default font shipped as part of X11 I noticed used to produced scrambled text and now crashes.

The crash seems like a separate bug that should be fixed.

@mitchellh mitchellh changed the title Graceful handling of fonts that fail to load Graceful handling of font files that fail to load (invalid font tables, bad format, etc.) Dec 17, 2024
@mitchellh
Copy link
Contributor

I renamed the issue slightly in case anyone browsing our issue list is confused by this. I don't know if the rename helped but I didn't want people to think we didn't handle non-existent fonts. This issue is about fonts that exist that are invalid for whatever reason and we haven't programmed a way to handle it.

@Andriamanitra
Copy link

This issue caused font rendering to break for me when printing Thai numerals 1 because Ghostty would fall back to a bitmap font 2 which are not supported, even though I would have also had TrueType font with Thai glyphs (Noto Sans Thai) to fall back to.

To reproduce:

printf "THAI DIGIT SEVEN: -> \u0e57 <-\n"

or

$ ghostty +show-face --cp=0xE57
Unable to get face: error.CopyTableError

After the printf ghostty would log something like this (the warning about CopyTableError was repeatedly printed every ~0.5 seconds or so after the fallback was found):

info(font_codepoint_resolver): found codepoint 0xE57 in fallback face=Fixed Regular
warning(renderer_thread): error rendering err=error.CopyTableError
warning(renderer_thread): error rendering err=error.CopyTableError
warning(renderer_thread): error rendering err=error.CopyTableError
warning(renderer_thread): error rendering err=error.CopyTableError
warning(renderer_thread): error rendering err=error.CopyTableError
warning(renderer_thread): error rendering err=error.CopyTableError
warning(renderer_thread): error rendering err=error.CopyTableError
warning(renderer_thread): error rendering err=error.CopyTableError
warning(renderer_thread): error rendering err=error.CopyTableError
warning(renderer_thread): error rendering err=error.CopyTableError

To work around the issue you can explicitly choose a font that has the glyph:

$ ghostty --font-family="Noto Sans Thai"  +show-face --cp=0xE57
U+E57 « ๗ » found in face “Noto Sans Thai”.

this can also be specified as a secondary font in ~/.config/ghostty/config, you can pick multiple fonts like this:

font-family = Source Code Pro Semibold  # My preferred font
font-family = Noto Sans Thai

Footnotes

  1. screencast

  2. /usr/share/fonts/misc/h14.pcf.gz (Fixed Regular) in my case but I think any Unicode bitmap font would cause the same problem. Another Discord user was able to reproduce the bug after running sudo dnf install efont-unicode-bdf on Fedora.

mitchellh added a commit that referenced this issue Dec 27, 2024
We do not currently support bitmap fonts in a real capacity, and they
often are missing some tables which we currently rely on for metrics,
and we don't handle the metrics calculations failing gracefully right
now.

This needs to be fixed for the fontconfig discovery mechanism as well,
so this does NOT close issue #2168 (it fixes the problem on macOS but
not linux).

This greatly alleviates the effect of #2991 since most cases I've seen
that be a problem have been the accidental loading of a bitmap font; but
the underlying issue still exists.
@dyrkuwu
Copy link

dyrkuwu commented Dec 28, 2024

I can confirm its an issue with unicode bitmap font rendering as it crashes with Cozette.

@mitchellh
Copy link
Contributor

This should be mitigated significantly with #3550 on macOS. This prevents macOS from using CoreText in the fallback chain (or any chain really). This will be part of 1.0.1.

@mitchellh mitchellh modified the milestone: 1.0.1 Dec 28, 2024
@mitchellh
Copy link
Contributor

Removing this issue from 1.0.1 since the mitigation is in place. Keeping the issue open to address more robustly in the future.

mitchellh added a commit that referenced this issue Jan 8, 2025
This refactor enables two very significant improvements to our font
handling, which I will be implementing next:
1. Automatically adjust size of fallback faces to better align with the
primary face, a la CSS
[`font-size-adjust`](https://developer.mozilla.org/en-US/docs/Web/CSS/font-size-adjust).
[^1]
2. Move glyph resizing/positioning out of GPU-land and in to
`renderGlyph` and apply alignment/resizing rules from the nerd fonts
patcher[^2] to the glyphs at rasterization time, so that we can ensure
exact cell fits and swap out our embedded JB Mono with an unpatched
version with a separate dedicated symbols-only nerd font.

In addition to being necessary prep work for those two changes, this PR
is also a minor but real stand-alone improvement. By only computing the
cell metrics for our primary font, we avoid a *lot* of wasted work when
loading fallback fonts, and also avoid that being a source of load
errors, which we don't yet handle gracefully[^3].

To validate this PR I've run the full set of font backend tests locally
on my Mac with no failures, and did a sanity check of running Ghostty
with both renderers and with CoreText and FreeType font backends and
then `cat`ing a file that requires fallback fonts to render, and
everything looks correct.

[^1]: #3029

[^2]: The alignment and resizing rules for the nerd font symbols are
defined in the patcher
[here](https://github.com/ryanoasis/nerd-fonts/blob/6d0b8ba05af0d1380905aee1a136c1dedf71c1db/font-patcher#L866-L1151)

[^3]: #2991
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
font Issue within the font stack (typically src/font)
Projects
None yet
Development

No branches or pull requests

5 participants