Skip to content

Typst chooses different fonts when setting font as SimSun in text #6205

Closed

Description

@Origami404

Description

Recently, when using typst, I encountered a confusing behavior: when I used set text to specify the font as SimSun, typst would embed a different font into the compiled PDF instead of SimSun. This behavior only occurred when set text(font: "SimSun") was used; specifying any other font worked normally.

Initially, I tried replacing the SimSun font file several times, but this behavior would still occur intermittently (it wasn't consistently replaced, nor was it consistently replaced by the same other font). Subsequently, I was determined to investigate further and after a binary search of the font list on my machine, I discovered that this behavior was confusingly related to some kind of metadata of the parent folder where the font file was located. This is the minimal reproducible example I provided case.tar.gz.

The composition of this test case is as follows:

.
├── fonts
│   ├── LiSu.ttf
│   ├── W
│   │   └── simsunb.ttf
│   └── l
│       └── SimSun.ttc
├── test.sh
├── test.typ
└── update.sh
// test.typ
#set text(font: "simsun")
#set text(lang: "zh", region: "CN")
这是一段测试文字。 // This is a piece of test text.
# test.sh
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
typst compile --ignore-system-fonts --font-path="$1" $SCRIPT_DIR/test.typ - | pdffonts -
# update.sh
mv "$1" /tmp/tmpfile
mv /tmp/tmpfile "$1"
rm -rf /tmp/tmpfile

The three font files in the example are all necessary, and simsunb.ttf and SimSun.ttc must be located in two different subdirectories, not directly in the fonts folder. On my machine, typst's behavior is as follows:

origami@pc ./case % ./update.sh fonts/l/
origami@pc ./case % ./test.sh fonts
name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
FCEZEI+LiSu                          CID TrueType      Identity-H       yes yes yes      4  0
origami@pc ./case % ./update.sh fonts/W
origami@pc ./case % ./test.sh fonts
name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
IWJAHX+SimSun                        CID TrueType      Identity-H       yes yes yes      4  0

Depending on the different "last updated times" of the W folder and the l folder, the font that typst chooses to embed in the PDF also changes.

The typst version I am using is as follows:

# typst --version
typst 0.13.1 (8ace67d9)

My system is archlinux with gnome. I have tested this example using docker in debian 12, and the behavior is consistent with archlinux.

Reproduction URL

No response

Operating system

Linux

Typst version

  • I am using the latest version of Typst

Activity

  1. added
    bug
    on Apr 23, 2025
  2. Enter-tainer commented on Apr 23, 2025

    @Enter-tainer
    Contributor

    @Origami404 Does this only affects pdf? I guess it would be better if we rename this issue as "Typst choose different font depending on last update time of font files"

  3. Origami404 commented on Apr 23, 2025

    @Origami404
    Author

    Does this only affects pdf?

    Just tested, outputing svg is also affected by this problem.

    depending on last update time of font files

    I'm afraid this isn't quite right.
    (1) I cannot directly confirm that the last updated time of the folder (ctime? atime? mtime?) is the sole influencing factor. It is known that simply using touch on the corresponding folder does not change the font selected by typst.
    (2) The issue is actually triggered by the "metadata of the folder containing the font", rather than the information of the font itself. If the two fonts in the test case were not nested within a subfolder layer, this problem would not occur.

  4. Origami404 commented on Apr 23, 2025

    @Origami404
    Author

    And I only seeing #set text(font: "SimSun") will trigger this problem, and any other fonts I tested will not. If, for example, I use #set text(font: "STSong") in test.typ and put the STSong.ttf into fonts replacing SimSun.ttc, typst will always choose the right font. The SimSun is the key factor AFAIK.

  5. changed the title [-]Typst embeds a different font into the PDF only when using the `SimSun` font[/-] [+]Typst chooses different fonts when setting `font` as `SimSun` in `text`[/+] on Apr 23, 2025
  6. added
    text
    on Apr 23, 2025
  7. YDX-2147483647 commented on Jul 3, 2025

    @YDX-2147483647
    Contributor

    I notice the file fonts/l/.gitattributes in your case.tar.gz. Is that the cause?

    *.ttf filter=lfs diff=lfs merge=lfs -text
    *.ttc filter=lfs diff=lfs merge=lfs -text
    *.otf filter=lfs diff=lfs merge=lfs -text

    My system is archlinux with gnome. I have tested this example using docker in debian 12, and the behavior is consistent with archlinux.

    Besides, I cannot reproduce it on Ubuntu or Windows.

    The cause of this issue (retrospectively added on 2026-11-21 after #8042)

    If two fonts have the identical score, which is the case for SimSun and SimSun-ExtB, then the font that is first iterated will take precedence.

    Typst uses fontdb to iterate fonts, and fontdb uses the std::fs::read_dir function to iterate files. However, quoting from Rust docs:

    The order in which read_dir returns entries can change between calls. If reproducible ordering is required, the entries should be explicitly sorted.

  8. Artoria2e5 commented on Sep 28, 2025

    @Artoria2e5

    gitattributes cannot have anything to do with this. The most it can do is mess with the timestamp, which is accounted for by update.sh.

    That said update.sh might be fragile in one way: I am not sure whether it depends on /tmp being on a different filesystem to cause the timestamp change behavior. I recommend rewriting it to use "touch".

    #!/bin/sh
    find "$1" -exec touch {} +
    
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

    bugtext

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions