Repository navigation
Description
Description
Compile the following on typst.app, download PDF, and copy the text.
#set text(lang: "zh", region: "CN", font: "Noto Serif CJK SC")
力量You will get 力量 (U+F98A U+F97E in CJK Compatibility Ideographs), instead of 力量 (U+529B U+91CF in CJK Unified Ideographs).
CJK Compatibility Ideographs often look bizarre, because they fallback to the system font.
Continues #3416/#3435
Might relate to typst/webapp-issues#48
Cause
/ToUnicode in PDF maps glyphs to Unicode code points, making it possible to copy and search text.
However, sometimes a glyph in a font is shared by multiple code points.
There are 3 code points mapping to the glyph cid11384 in SourceHanSerifCN-Regular.otf (downloaded from Region Specific Subset OTFs Simplified Chinese (简体中文)):
- ⼒ (U+2F12 KANGXI RADICAL POWER): cid11384
- 力 (U+529B CJK UNIFIED IDEOGRAPH-529B): cid11384
- 力 (U+F98A CJK COMPATIBILITY IDEOGRAPH-F98A): cid11384
(generated by a Python script)
from unicodedata import name
from fontTools.ttLib import TTFont
font = TTFont("SourceHanSerifCN-Regular.otf")
cmap = font["cmap"].getBestCmap()
the_character = "\u529b"
the_glyph = cmap[ord(the_character)]
for c, g in cmap.items():
if g == the_glyph:
print(f"{chr(c)} (U+{c:X} {name(chr(c))}): {g}")
print("---")
for c in "⼒力力":
print(f"{c} (U+{ord(c):X} {name(c)}): {ord(c) in cmap = }")Only one code point can be written to /ToUnicode, and Typst chooses U+F98A 力 (CJK compatibility).
Adobe’s implementation
Adobe explicitly maps the glyph to the most used one, U+529B 力 (CJK unified).
To UVS, Or Not To UVS - CJK Type Blog | Adobe:
A ToUnicode mapping file does exactly what its name suggests: it maps CIDs to Unicode code points, or to code point sequences. Unlike CMap resources that map Unicode code points to CIDs, or 'cmap' tables that map code points to GIDs that may also be CIDs, a ToUnicode mapping file specifies the inverse mapping. Some omissions and ambiguities can arise, either because a glyph is represented as a sequence, or it is mapped from multiple code points.
An excellent example of the latter is Adobe-Japan1-7 CID+1200, which is mapped from U+2F00 ⼀ KANGXI RADICAL ONE and U+4E00 一 (a CJK Unified Ideograph). If CID+1200 is included in a PDF, one would naturally expect U+4E00 一 to be copied, not U+2F00 ⼀ as its use is more obscure. The Adobe-Japan1-UCS2 ToUnicode mapping file makes this mapping preference explicit (04b0 is the zero-padded hexadecimal form of decimal 1200):
<04b0> <4e00>
There may be cases of single CIDs being referenced in multiple encoding points within a single CMap file.
These cases are comma-delimited, within the same column.
CMap
-
CID … UniGB-UCS2 2543 … 2f12,529b -
CID … UniAKR-UTF16 14593 … 2f12,529b,f98a
/ToUnicode
-
<09ef> <529b> # <09ef> is 2543 in hex. -
<3901> <529b> # <3901> is 14593 in hex.
Further example
#set page(height: auto, width: 30em)
#set text(lang: "zh", region: "CN", fallback: false)
#let fonts = (
// "Linux Libertine", // no glyph
// "Noto Serif", // no glyph
"Noto Serif CJK SC",
"Noto Sans CJK SC",
)
#for f in fonts {
text(font: f)[
#f
- ⼒ U+2F12 Kangxi radical
- 力 U+529B CJK unified
- 力 U+F98A CJK compatibility
地球发动机是人类建造的力量最大的机器,比如我们所在的华北794号,全功率运行时能向大地产生150亿吨的推力。
#pagebreak()
]
}Screenshot of the result in a nerd font:

Note
This issue cannot be repoduced with testit cjk --pdf and https://github.com/typst/typst/blob/42754477886f6a12afbabfd2a64d8c787a57bc03/tests/suite/layout/inline/cjk.typ, because https://github.com/typst/typst-dev-assets/blob/48a924d9de82b631bc775124a69384c8d860db04/files/fonts/NotoSerifCJKsc-Regular.otf does not contain U+2F12 ⼒ (Kangxi radical) and U+F98A 力 (CJK compatibility).
Reproduction URL
No response
Operating system
Web app, Windows
Typst version
- I am using the latest version of Typst

Activity
Enter-tainer commentedon Jul 18, 2024
https://zhuanlan.zhihu.com/p/704523436
related discussion(in chinese) by the author of ptex-ng.
YDX-2147483647 commentedon Jul 18, 2024
Besides, https://itextpdf.com/sites/default/files/2018-12/PP_Advanced_typography_in_PDF-compressed.pdf mentioned a transposed problem: multiple glyphs, one code point.
But I cannot read this language, and have no idea if Typst supports it.
LaurenzV commentedon Jul 18, 2024
Somewhat relevant issues: #526 #4225
laurmaedje commentedon Jul 18, 2024
Isn't this a direct duplicate of #4225?
LaurenzV commentedon Jul 18, 2024
Not sure, but since this issue is pretty descriptive I think it makes sense to keep it open.
laurmaedje commentedon Jul 18, 2024
Yeah, I agree that this issue is very well written. Perhaps we could close the other one instead.
YDX-2147483647 commentedon Jul 18, 2024
Well, I think Chinese and devanagari are two distinct issue.
The slide I posted above (#4582 (comment)) describes #4225. (I unhide it from off-topic now.)
laurmaedje commentedon Jul 18, 2024
Okay, I guess we can keep both issues open. But I think a proper solution will fix both. Probably it is writing
/ToUnicodefor one (e.g. the most common) and then using/ActualTexteverywhere else.YDX-2147483647 commentedon Jul 18, 2024
A rough example in European languages might be helpful.
YDX-2147483647 commentedon Jul 19, 2024
How the code point is chosen now
Among Kangxi radical (⼒), CJK unified (力), CJK compatibility (力), Typst tends to choose CJK compatibility.
But Apple, Microsoft, and Adobe products tend to choose Kangxi radical, according to the Chinese article mentioned in #4582 (comment) and a Japanese report of 有限会社サイパック.
Typst chooses CJK compatibility because it has larger Unicode code point number.
When
write_normal_text(), Typst records the code point that occurs first for each glyph. Later inimprove_glyph_sets(), Typst replaces code points from the font's cmap table. If a glyph is mapped from multiple code points, it gets replaced multiple times, and the largest code point wins at last.typst/crates/typst-pdf/src/page.rs
Line 55 in 4275447
typst/crates/typst-pdf/src/font.rs
Line 237 in 4275447
OpenType offers tables for many platforms and encodings (Windows/Unicode, full repertoire / BMP, etc.), and Typst iterates all of them.
Details on those tables
getBestCmap- cmap: Character to Glyph Index Mapping Table — fontTools Documentation: (updated according to cmap - Character To Glyph Index Mapping Table (OpenType 1.9.1) - Typography | Microsoft Learn)ttf\_parser::cmap- Rust:In ttf-parser v0.12, only
Face::character_mapping_subtablesis supported. Then it changed toFace::tables().cmapin v0.13+. Typst upgraded ttf-parser from v0.12 to v0.15 in ed6550f.Use texts of first occurrences for `/ToUnicode` CMap
Resolves typst#4582
Use texts of the first occurrences for `/ToUnicode` CMap
Use texts of the first occurrences for `/ToUnicode` CMap
Use texts of the first occurrences for `/ToUnicode` CMap