Repository navigation
Description
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/tmpfileThe 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 0Depending 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
Enter-tainer commentedon Apr 23, 2025
@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"
Origami404 commentedon Apr 23, 2025
Just tested, outputing svg is also affected by this problem.
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.
Origami404 commentedon Apr 23, 2025
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")intest.typand put the STSong.ttf intofontsreplacingSimSun.ttc, typst will always choose the right font. TheSimSunis the key factor AFAIK.[-]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`[/+]YDX-2147483647 commentedon Jul 3, 2025
I notice the file
fonts/l/.gitattributesin yourcase.tar.gz. Is that the cause?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_dirfunction to iterate files. However, quoting from Rust docs:Artoria2e5 commentedon Sep 28, 2025
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".
windows-fonts: split TTC into individual TTFs for Typst compatibility
Exclude SimSun-ExtB (simsunb.ttf) from windows-fonts