Skip to content

Make test cases independent to font fallbacks - #8991

Open
YDX-2147483647 wants to merge 11 commits into
typst:mainfrom
YDX-2147483647:test-font-fallback
Open

YDX-2147483647 wants to merge 11 commits into
typst:mainfrom
YDX-2147483647:test-font-fallback

Conversation

@YDX-2147483647

@YDX-2147483647 YDX-2147483647 commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

At present, many test cases rely on the specific font fallback rule of Typst, especially those using Arabic, Han/CJK, Hebrew, Thai, and other non-Latin characters.
This PR makes them independent to font fallbacks by explicitly adding either set text(font: …) or set text(fallback: true).


Disadvantages of having test cases dependent on font fallbacks:

  • Adding a new font to typst-dev-assets can change the output of these test cases. For some test cases, even renaming an existing font can change the output.

    This prevents us from updating the Noto CJK / Source Han fonts. (Update Noto CJK or add Source Han typst-dev-assets#18)

    As a concrete example, Add FontFlags exceptions for Libertinus fonts #9048 added an exception for Libertinus Serif, and it caused U+3002 。 IDEOGRAPHIC FULL STOP in two test cases changed from Garamond-Math to Noto Serif CJK JP.

  • Sometimes the resulting fallback font is confusing or even wrong. It is unsuitable to use them as the reference outputs.

    Here are some examples of font fallback quirks:

    图片
    Code
    #set page(height: auto, width: auto, margin: 1em)
    #set par(justify: true)
    #table(
      columns: (auto, auto, auto, 20em),
      align: (center, center, left, left).map(a => a + horizon),
      stroke: (x, y) => if y > 0 { (top: 1pt) },
    
      table.header[*Example 1*][*Example 2*][*Difference*][*Comment*],
      [国。],
      [2。],
      [The glyph of #text(font: "Noto Serif CJK JP")[。]],
      [It is U+3002 IDEOGRAPHIC FULL STOP. _Noto Serif CJK JP_ provides a correct glyph, but _Garamond-Math_ does not.],
    
      [空格],
      [*空格*],
      [The glyph of 空],
      [Only the Regular weight exists for _Noto Serif CJK JP_ in typst-dev-assets, while both Regular and Bold exist for _Noto Serif CJK SC_.],
    
      [换行],
      [换 行],
      [The metadata of 换],
      [_Noto Serif CJK JP_ covers 行 but not 换. If 换行 are processed together, they use the _JP_ font; but when they are processed separately, 换 uses the _SC_ font and _行_ uses the _JP_ font],
    )

    Also note that a font may affect the output even if it is not embedded in the resulting PDF: it can change the width of space characters.

    Typst can hardly improve the font fallback mechanism (see the discussions in Writing non-Latin (e.g. Chinese) text without a configured font leads to unpredictable font fallback #5040, Chinese rendering error #5900, etc.), so it's better not to depend on it.

    (Update on 2026-11-26: The example of 。 was somehow changed in Add FontFlags exceptions for Libertinus fonts #9048.)


Previous discussions on Discord 2026-10-21:

Is it intentional that most CJK test cases do not set text(font: …) and rely on the specific font fallback rule?
I see that some test cases change if I add a new CJK font to typst-dev-assets without changing existing fonts. …

It's not really intentional. We can change it as long as we keep one or two fallback tests.

Besides, #2648 (comment) once discussed about the font fallbacks in text-lang-unknown-region and related test cases.

Technical details

This PR makes sure that all test cases produce the identical output when they are prepended with the following prelude:

#set text(fallback: false)
#set text(font: ("Libertinus Serif", "Twitter Color Emoji"))
#show math.equation: set text(font: ("New Computer Modern Math", "Twitter Color Emoji"))

(At first I also added Twitter Color Emoji to test cases, but later I found that there are too many test cases containing emojis, and emojis won't be affected when we update CJK fonts in the future.)

I recommend to review this PR commit by commits. I organized the changes as the following steps. You can run cargo testit on any intermediate commit and observe that it passes.

  1. Lock font settings without changing hashes.

    There are many test cases that rely on font fallbacks. I categorized them into three types:

    • intended use cases:

      • Explicitly setting fonts is recommended here.
      • The default fallback is ideal and there is no need to change text.lang.

      For intended use cases, locking font settings is the only change needed.

    • half intended use cases:

      • Explicitly setting fonts is recommended here.
      • The default fallback is acceptable (although not ideal), and/or text.lang has to be changed.

      For half intended use cases, I added NOTE(font): … comments during this step. I'll improve them in step 4.

    • unintended use cases:

      • Explicitly setting fonts is recommended here.
      • The default fallback is weird. For example, a single paragraph mixes glyphs from Simplified Chinese and Korean.

      For unintended use cases, I added TODO(font): … comments during this step. I'll fix them later.

  2. Improve a specific test case.

    The test case named text-edge uses IBM Plex Mono, but this font does not exist in typst-dev-assets.

    This test case predates the unknown font family warning (Warn about unknown font families during parsing #3854). I guess the original author wanted to test IBM Plex Mono, but did not realize that it actually falls back to Libertinus Serif.

    Therefore, I split it into two tests: one testing text edges, and the other testing the font warning.

  3. Edit TestWorld and disable font fallback.

  4. Fix font settings. This step changes the hashes.

    • For half-intended use cases, I replaced the NOTE(font): … comments with the actual fix.

    • For unintended use cases, if the problem could be fixed without updating typst-dev-assets, I replaced the TODO(font): … comment with the actual fix; otherwise, I left the comment as is (I plan to fix them when Source Han Sans is added to typst-dev-assets).

  5. Revert the edit to TestWorld.

Please be aware that the above steps can only prove that I didn't miss anything, but it cannot rule out the possibility of having redundant changes. Specifically, if I added set text(font: (A, B, C)), it's possible that set text(font: (A, B)) is sufficient.
However, since I edited the files incrementally, it should be unlikely to have redundant changes. With that said, I did come across a redundant change where A,B,C are Noto Serif CJK variants (which has now been fixed)…

@YDX-2147483647
YDX-2147483647 marked this pull request as draft October 22, 2026 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting-on-review This PR is waiting to be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants