Skip to content

Wrong "monospace" font fallback for CJK characters in raw block #3385

Open

Description

@Myriad-Dreamin

Description

The following text are rendered wrongly on most Windows. This is because typst usually makes font fallback to "LiSu", an ancient chinese font, or even fails to fallback to any font.

运行结果:

```
请输入一个整数:16
该整数的平方根为: 4.0
```
image

Though Chinese users already have to style their documents with a template overriding many default settings, I believe it deserves to make our settings more sensible. I suggest to extend this fallback list for different systems:

out.set(TextElem::set_font(FontList(vec![FontFamily::new("DejaVu Sans Mono")])));

#show raw: set text(font: (
  "DejaVu Sans Mono",
  "Microsoft YaHei", // for Windows users
  "PingFang SC", // for macOS users
  "...", // for Linux users
))
image

Reproduction URL

No response

Operating system

Windows 11

Typst version

  • I am using the latest version of Typst

Reference: https://www.tug.org/texlive//Contents/live/texmf-dist/tex/latex/ctex/fontset/

Activity

  1. added
    bug
    on Feb 8, 2024
  2. added
    text
    on Feb 14, 2024
  3. peng1999 commented on Feb 18, 2024

    @peng1999
    Contributor

    It's not wise to hard encode fallback font list. It's better to detect the fallback list according to system interfaces. On MacOS this can be achieved using CTFontCopyDefaultCascadeListForLanguages, and on Linux we can use fontconfig.

  4. added
    cjk
    on Feb 10, 2025
  5. YDX-2147483647 commented on Nov 3, 2025

    @YDX-2147483647
    Contributor

    typst usually makes font fallback to "LiSu"

    This is because LiSu itself claims to be monospace, and has the shortest name.
    The fallback algorithm gives priority to such fonts.

    • claims to be monospace: In its OpenType post table, isFixedPitch is nonzero.

       C:\Windows\Fonts> ttx -t post SIMLI.TTF -o -
       Dumping "SIMLI.TTF" to "<stdout>"...
       <?xml version="1.0" encoding="UTF-8"?>
       <ttFont sfntVersion="\x00\x01\x00\x00" ttLibVersion="4.53">
       
       Dumping 'post' table...
         <post>
           <formatType value="3.0"/>
           <italicAngle value="0.0"/>
           <underlinePosition value="-59"/>
           <underlineThickness value="13"/>
           <isFixedPitch value="1"/>
           <minMemType42 value="0"/>
           <maxMemType42 value="0"/>
           <minMemType1 value="0"/>
           <maxMemType1 value="0"/>
         </post>
       
       </ttFont>

      In fact, abc in LiSu indeed have the same width, and they are narrower than 人一口.
      However, this is also true for KaiTi, but KaiTi declares that it is not monospace…

       $ echo '#context [#metadata("abc人一口".clusters().map(c => ((c): measure(text(font: "LiSu", c)).width)).join()) <data>]' | typst query - '<data>' --one --field value --format yaml
       a: 5.5pt
       b: 5.5pt
       c: 5.5pt
       人: 11pt
       一: 11pt
       口: 11pt
    • has the shortest name: Only 4 characters, shorter than SimSun, Microsoft YaHei, etc.

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

    bugcjktext

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions