Skip to content

Warn about unknown font families during parsing - #3854

Merged
laurmaedje merged 3 commits into
typst:mainfrom
niklasmohrin:unknown-font-error
Apr 17, 2024
Merged

laurmaedje merged 3 commits into
typst:mainfrom
niklasmohrin:unknown-font-error

Conversation

@niklasmohrin

@niklasmohrin niklasmohrin commented Apr 2, 2024 •

Copy link
Copy Markdown
Contributor

First progress on #1967

Edit: This started out as a PR to error when fallback: true, but we decided to first add a warning when encountering an unknown font, regardless of fallback. Original description below:


As discussed in the issue, I will try to emit errors first before thinking about warnings. This PR introduces a check for the easiest situation:

#text(font: "non-existing", fallback: false, "Hello")

yields

error: font family not found: non-existing
  ┌─ ex2.typ:1:6
  │
1 │ #text(font: "non-existing", fallback: false, "Hello")
  │       ^^^^^^^^^^^^^^^^^^^^
  │
  = hint: set `fallback: true` to use another font instead

However, the slightly more complicated case

#text(font: "Linux Libertine", fallback: false, text(font: "non-existing", "Hello"))

does not produce an error. This is because the check is currently in TextElem::construct and only inspects the direct arguments, not the inherited style.

Another example of this is

#text(font: "non-existing", text(fallback: false, "Hello"))

I think this is a bit unsatisfying, but better than the status-quo. Do you think it will be difficult to do the check later when style is inherited? Or am I missing an obvious way to inspect parent styles in the constructor here?

I had some trouble with getting the span of the font argument, because as far as I can tell, the TextElem does not know the spans of its arguments after construction, so I had to get the span just before setting the style.

@niklasmohrin niklasmohrin changed the title Emit an error when constructing TextElem with unknown font family Emit an error when constructing TextElem with unknown font family and fallback: false Apr 2, 2024
@laurmaedje

Copy link
Copy Markdown
Member

I think this is a bit unsatisfying, but better than the status-quo. Do you think it will be difficult to do the check later when style is inherited?

The parent styles are simply not know at this point, and there could actually be multiple different ones. Consider e.g.:

#let var = text(font: "non-existent")[Var]

#set text(fallback: true)
#var

#set text(fallback: false)
#var

After thinking some more about it, I think we can just only ever warn about this and not make the warning conditional on whether fallback is enabled. When fallback: false is specified, we will then still error at the point where we can't find a font for a glyph (in the future).

Another thing is that your current implementation doesn't work with set rules, because Construct only runs when text is directly called. To fix that, the check could be moved into a #[parse] handler. You can grep through the repo to see how those work.

I had some trouble with getting the span of the font argument, because as far as I can tell, the TextElem does not know the spans of its arguments after construction, so I had to get the span just before setting the style.

If you use #[parse] you can use Spanned<..> in combination with it. That's also done in some other places.

@niklasmohrin

Copy link
Copy Markdown
Contributor Author

Okay, makes sense! I updated the PR to warn in a parse handler instead. The span works nicely, this is what the warnings look like:

#text(font: "non-existing", "Hello")
#set text(font: "also-non-existing")
#let var = text(font: ("list-of", "non-existing-fonts"))[Var]
warning: unknown font family: non-existing
  ┌─ ex2.typ:1:12
  │
1 │ #text(font: "non-existing", "Hello")
  │             ^^^^^^^^^^^^^^

warning: unknown font family: also-non-existing
  ┌─ ex2.typ:2:16
  │
2 │ #set text(font: "also-non-existing")
  │                 ^^^^^^^^^^^^^^^^^^^

warning: unknown font family: list-of
  ┌─ ex2.typ:3:22
  │
3 │ #let var = text(font: ("list-of", "non-existing-fonts"))[Var]
  │                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: unknown font family: non-existing-fonts
  ┌─ ex2.typ:3:22
  │
3 │ #let var = text(font: ("list-of", "non-existing-fonts"))[Var]
  │                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I think the list of multiple fonts should use a smaller span to each single font, but maybe this is something for follow up work?

If you green light these changes, I can add some tests for the warning in particular. I already had to annotate warnings in two test files, I am not sure if it was intended that these fonts are missing. Should I look into it, or should we commit the warnings for now?

@laurmaedje

Copy link
Copy Markdown
Member

I think the list of multiple fonts should use a smaller span to each single font, but maybe this is something for follow up work?

It's a common limitation. There isn't really a way to recover these spans. It's okay as-is.

If you green light these changes, I can add some tests for the warning in particular. I already had to annotate warnings in two test files, I am not sure if it was intended that these fonts are missing. Should I look into it, or should we commit the warnings for now?

Yeah, the change looks good!

The existing failures aren't intentional. That's actually a problem your PR found in the existing tests! But you can commit the warnings for now.

@niklasmohrin niklasmohrin changed the title Emit an error when constructing TextElem with unknown font family and fallback: false Warn about unknown font families during parsing Apr 9, 2024
@niklasmohrin

Copy link
Copy Markdown
Contributor Author

@laurmaedje I added the examples of my last comment to one of the test files for text, rebased on current main and separated the changes into three reasonable commits - they can probably be squashed though.

@niklasmohrin
niklasmohrin marked this pull request as ready for review April 9, 2024 20:44
@laurmaedje

Copy link
Copy Markdown
Member

Looks good.

However, I decided to merge #3922 first because the whole spirit of that PR is to not merge any further large reference images, so you'll unfortunately have to merge/rebase. Sorry about that!

@laurmaedje laurmaedje added the waiting-on-author Pull request waits on author label Apr 16, 2024
@niklasmohrin

Copy link
Copy Markdown
Contributor Author

No problem, updated

@laurmaedje laurmaedje removed the waiting-on-author Pull request waits on author label Apr 16, 2024
@laurmaedje
laurmaedje added this pull request to the merge queue Apr 17, 2024
@laurmaedje

Copy link
Copy Markdown
Member

Thanks!

Merged via the queue into typst:main with commit 0523f22 Apr 17, 2024
@niklasmohrin
niklasmohrin deleted the unknown-font-error branch April 17, 2024 15:30
@gasche

gasche commented Oct 24, 2025 •

Copy link
Copy Markdown

The warning on fallback: true introduced by this PR is rather problematic in practice in my experience, see the tracking issue #6010. When template authors specify preferred fonts, they typically select a font that they like (but maybe it is only available by default on some operating systems, proprietary, or even not available freely), and then they add more common fonts as fallbacks. The result of the present PR is that users get warnings when they use the template, they don't know why, and template authors cannot hide them (see my original issue thatfloflo/typst-pc-letter#1 on the pc-letter template). It looks like the template has a bug, and the warning is annoying to end-users.

Is there something that can be done to fix this issue? For example, would Typst maintainers agree with the idea of emitting a warning only when none of the suggested fonts can be found? Any advice on preparing a PR to implement this?

@laurmaedje

Copy link
Copy Markdown
Member

Is there something that can be done to fix this issue? For example, would Typst maintainers agree with the idea of emitting a warning only when none of the suggested fonts can be found? Any advice on preparing a PR to implement this?

It's a tricky situation. Sometimes, you want a fallback list because two fonts are similar, and sometimes your fallback list contains a text and an emoji font and you need both. So I'm not sure it's desirable either.

The change you propose would be easy to implement, in check_font_list. More complex checks that interact with other properties (e.g. with fallback or anything else, if we want that) are a bit more difficult because other properties are not known at the position the warning is currently produced (when the set rule is called). The combination of properties is only known during text layout and then we potentially get multiple warnings triggered by different paragraph layouts. Those would probably end up deduplicated, but it's a bit unfortunate.

@gasche

gasche commented Oct 30, 2025

Copy link
Copy Markdown

My intuition is that we should ensure that the choice of behavior covers the common, easy/natural case (with an attention to backward-compatibility), and then possibly suggest a separate API to express the more subtle/advanced needs.

From a distance my impression is that "I would like one of these fonts" is a common case, while "I need both of these fallbacks at once" is the less common case, but this is subjective, I may be biased given that I encountered precisely the first need in practice and not the second.

@gasche

gasche commented Oct 30, 2025

Copy link
Copy Markdown

I looked at the code that you recommended and I see why things are more complex. There are two different concerns:

  1. Font choices can come with a "cover" regex that restricts a particular option to only some characters. If I specify two fonts with distinct / non-overlapping covers, I would expect a warning as soon as one of them is not available. But computing this correctly is a bit cumbersome, one would need to perform operations on the regexps. (We could maintain a regex that represents a set of characters for which no suitable font has been found yet, and as we go down the list remove from that set the cover of every found that is found on the system; but I looked and regex::Regex does not allow to compute intersection of regexes, and even the lower-level regex-automata crate does not make this easy.)

  2. There is a choice to make about emitting a warning "statically", when the list of fonts is decided, or "dynamically", when text is rendered with those fonts. The current approach is "static", but it may warn in situations where the font setting is made only locally, for inputs that are actually covered by the font list. Warning "dynamically" might not be too hard to implement, but it is more invasive -- it would go in the actual rendering code paths.


Ideally as a first step we want some logic that :

  1. Works well in the common and easy case where none of the fonts come with a specified cover.
  2. Degrades reasonably when covers are introduced in the font list.

I can see two approaches to do this:

A. We could handle each font as if it had no cover restriction, and thus warn only if none of the fonts are available; or,
B. We could pessimistically assume that fonts with cover restrictions are unavailable for some codepoints, and warn if none of the unrestricted fonts are available.

Approach (A) will err on the side of not warning enough in some cases, while approach (B) will err on the side of warning too much in some cases. Both work perfectly well in the common case where there are no cover restrictions. I don't have a strong opinion between A and B; I may give B a try, given that it is closer in spirit to the current implementation (it warns too much, just like today).

gasche added a commit to gasche/typst that referenced this pull request Oct 30, 2025
@gasche

gasche commented Oct 30, 2025

Copy link
Copy Markdown

I propose an implementation of approach (B) in #7272.

gasche added a commit to gasche/typst that referenced this pull request Oct 30, 2025
gasche added a commit to gasche/typst that referenced this pull request Nov 5, 2025
Dikluwe pushed a commit to Dikluwe/typst-crystalline that referenced this pull request Sep 12, 2026
YDX-2147483647 added a commit to YDX-2147483647/typst that referenced this pull request Oct 22, 2026
…dded

This test case predates the _unknown font family_ warning (typst#3854).
I guess the original author wanted to test IBM Plex Mono, but did not realize that the font falls back to Libertinus Serif.
YDX-2147483647 added a commit to YDX-2147483647/typst that referenced this pull request Oct 22, 2026
…dded

This test case predates the _unknown font family_ warning (typst#3854).
I guess the original author wanted to test IBM Plex Mono, but did not realize that the font falls back to Libertinus Serif.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants