You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Compiling a document with a non-existent font and fallback: false should cause an error #1967
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):
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?
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?
The case when no fallback has been found at all should be when selection is Nonehere. 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?
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.
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.
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?
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:
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.
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.
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.
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.
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.
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".
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?
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.
Activity
Dherse commentedon Dec 7, 2023
BTW, how about an error when the font is not known and
fallback: falseand a warning whenfallback: true?niklasmohrin commentedon Apr 1, 2024
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):
typst/crates/typst/src/layout/inline/shaping.rs
Line 645 in 1e645b3
ShapingContextneeds to change so that it takesengineas a mutable reference instead of a shared one, because the engine'sTraceris used to issue warnings. Repeatedly changing all references toEngineto mutable until we arrive at the owner of theEngineworks without much trouble. Is there any good reason to keep it as a shared reference instead?shape_segment), fallback font families are added iffallback: true, and in theshape_segmentfunction where, if fallback is active, theFontBookis asked to pick any of its available fonts. In theshape_segmentfunction, 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 makingfamiliesreturn a new typeFamiliesinstead of theimpl Iterator, so that it can support something likefamilies.is_currently_yielding_fallbacks()andshape_segmentcan use this to decide whether or not it has picked one of the fallback families. Alternatively, thefallbackattribute could be more specific, for example allowing something likenone,builtin, andchoose-anyinstead of just a boolean value. What do you think?selectionisNonehere. 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?tracer.warn(...), but instead by returning aSourceResultor alternatively recording a delayed error in the tracer. I am not sure which of the two is more appropriate.text(font: "installed-family", text(font: "nonexisting-family", fallback: false, "foo"))? If I understand the code withTextElem::font_inin thefamiliesiterator correctly, for "foo", the style chain would have both font families, even if with thefallback: false- as a user, I would want to get an error for the nonexisting family though.Span. Later inshape_segment, astartis calculated based on some "glyph infos", but we can't get those without a font. I thought thatctx.spans.span_at(base).0would be the way to go, but it yielded the detached span.Could you give your thoughts on each of the points?
laurmaedje commentedon Apr 2, 2024
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:
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.
If the compiler is happy, then it seems fine.
A custom iterator sounds fine. Alternative, the iterator could yield
(&str, bool)pairs, indicating whether it is a fallback family.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: falseand warn otherwise sounds fair to me.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.
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".In principle,
ctx.spansis the way to go. Can you share the snippet where it yielded a detached span, so I can look into it?niklasmohrin commentedon Apr 2, 2024
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:
Good point, this seems more natural! I chose this place for an attempt in #3854
Oh yeah, I may have phrased that a bit unfortunately, I agree with you here. I meant to error there only if
fallback: false.Yes, you are right, so we can ignore my comment there.
My naive way was to use
ctx.spans.span_at(base)in the body ofshape_segment, because further down the function I sawctx.spans.span_at(start)(withstart = base + cluster), but that yieldedSpan::detached(). I supposeclusterdoes not necessarily start at 0.laurmaedje commentedon Jul 15, 2024
Closed by #3854