Skip to content

Compiling a document with a non-existent font and fallback: false should cause an error #1967

Closed

Description

@Enivex

Description

The following

#set text(font: "totallynotafont", fallback:false)
Hello world

will just result in an empty page, but no error.

Reproduction URL

No response

Operating system

No response

Typst version

  • I am using the latest version of Typst

Activity

  1. added
    bug
    diagnostics
    on Aug 21, 2023
  2. added
    change request
    and removed
    bug
    on Nov 14, 2023
  3. Dherse commented on Dec 7, 2023

    @Dherse
    Contributor

    BTW, how about an error when the font is not known and fallback: false and a warning when fallback: true?

  4. niklasmohrin commented on Apr 1, 2024

    @niklasmohrin
    Contributor

    I played around with this a bit today and this is what I found and the questions that came up for me (I don't have any prior experience with the typst codebase, so take all of this with a grain of salt):

    1. Warnings would be issued in
      fn shape_segment<'a>(
      Is there a more appropriate place?
    2. To issue a warning in this function, the ShapingContext needs to change so that it takes engine as a mutable reference instead of a shared one, because the engine's Tracer is used to issue warnings. Repeatedly changing all references to Engine to mutable until we arrive at the owner of the Engine works without much trouble. Is there any good reason to keep it as a shared reference instead?
    3. There are two kinds of fallbacks: In the families iterator (which is the supplied argument for shape_segment), fallback font families are added if fallback: true, and in the shape_segment function where, if fallback is active, the FontBook is asked to pick any of its available fonts. In the shape_segment function, we cannot tell anymore which families came from the style chain, and which are fallbacks added from the families iterator, so issuing a warning based on this might be difficult. One possibility I could see is making families return a new type Families instead of the impl Iterator, so that it can support something like families.is_currently_yielding_fallbacks() and shape_segment can use this to decide whether or not it has picked one of the fallback families. Alternatively, the fallback attribute could be more specific, for example allowing something like none, builtin, and choose-any instead of just a boolean value. What do you think?
    4. The case when no fallback has been found at all should be when selection is None here. I think a good first work towards closing this issue here would be to start with just erroring in this case and working on warnings later. What do you think about this plan?
    5. Issuing an error does not work exactly like tracer.warn(...), but instead by returning a SourceResult or alternatively recording a delayed error in the tracer. I am not sure which of the two is more appropriate.
    6. What should happen with text(font: "installed-family", text(font: "nonexisting-family", fallback: false, "foo"))? If I understand the code with TextElem::font_in in the families iterator correctly, for "foo", the style chain would have both font families, even if with the fallback: false - as a user, I would want to get an error for the nonexisting family though.
    7. Regardless or errors or warnings, I could not get a nice Span. Later in shape_segment, a start is calculated based on some "glyph infos", but we can't get those without a font. I thought that ctx.spans.span_at(base).0 would be the way to go, but it yielded the detached span.

    Could you give your thoughts on each of the points?

  5. laurmaedje commented on Apr 2, 2024

    @laurmaedje
    Member

    First of all, I'd like to note that there is already an open PR related to this. There is some further discussion about problems there. Edit: The other PR was closed by its author.

    Still, here are answers to your questions:

    1. Warnings would be issued in [...]

    There are different cases:

    • A non-existent font would ideally warn directly at the set rule instead of during shaping. This could probably be added directly to the argument parsing in the TextElem.

    • If there is fallback during shaping, then the place you identified looks correct.

    1. To issue a warning in this function, the ShapingContext needs to change so that it takes engine as a mutable reference instead of a shared one.

    If the compiler is happy, then it seems fine.

    1. There are two kinds of fallbacks: [...]

    A custom iterator sounds fine. Alternative, the iterator could yield (&str, bool) pairs, indicating whether it is a fallback family.

    1. The case when no fallback has been found at all should be when selection is None here. I think a good first work towards closing this issue here would be to start with just erroring in this case and working on warnings later.

    This case shouldn't unconditionally error in my opinion. Similarly to how missing labels for references are annoying because they always fail compilation with no way out, it would be frustrating if a missing font would unconditionally fail compilation. The suggestion to only error with fallback: false and warn otherwise sounds fair to me.

    1. Issuing an error does not work exactly like tracer.warn(...), but instead by returning a SourceResult or alternatively recording a delayed error in the tracer. I am not sure which of the two is more appropriate.

    The correct way is to return a result. The delayed errors in the tracer are not intended to be used directly, there are only populated by the engine.

    1. What should happen with text(font: "installed-family", text(font: "nonexisting-family", fallback: false, "foo"))?

    Unless I am mistaken, the font property isn't folded (i.e. combined across the style chain), so for "foo", it would only have "nonexisting-family".

    1. Regardless or errors or warnings, I could not get a nice Span. Later in shape_segment, a start is calculated based on some "glyph infos", but we can't get those without a font. I thought that ctx.spans.span_at(base).0 would be the way to go, but it yielded the detached span.

    In principle, ctx.spans is the way to go. Can you share the snippet where it yielded a detached span, so I can look into it?

  6. niklasmohrin commented on Apr 2, 2024

    @niklasmohrin
    Contributor

    Whoops, I didn't find the other PR. Seeing that, I think I am going to lower my goal here for now and really only focus on returning errors. Some of the discussions may not be relevant anymore, but here are still all my responses just in case:

    A non-existent font would ideally warn directly at the set rule instead of during shaping. This could probably be added directly to the argument parsing in the TextElem.

    Good point, this seems more natural! I chose this place for an attempt in #3854

    This case shouldn't unconditionally error in my opinion.

    Oh yeah, I may have phrased that a bit unfortunately, I agree with you here. I meant to error there only if fallback: false.

    Unless I am mistaken, the font property isn't folded

    Yes, you are right, so we can ignore my comment there.

    In principle, ctx.spans is the way to go. Can you share the snippet where it yielded a detached span, so I can look into it?

    My naive way was to use ctx.spans.span_at(base) in the body of shape_segment, because further down the function I saw ctx.spans.span_at(start) (with start = base + cluster), but that yielded Span::detached(). I suppose cluster does not necessarily start at 0.

  7. laurmaedje commented on Jul 15, 2024

    @laurmaedje
    Member

    Closed by #3854

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

    change requestdiagnostics

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions