Trust the font, not the label
A Word PDF looks perfect on screen, but copying its Khmer gives you the wrong letters. The file draws every letter correctly and then labels many of them wrongly. khpdf ignores the labels and asks the font that draws them.
The page is right, the copied text is not
Here is the heading on the first page of a 2016 World Bank report written in Khmer. Open the PDF and it reads ការរៀបចំក្របខ័ណ្ឌគ្រប់គ្រង ("preparing the management framework"). Copy it, or run Poppler's pdftotext (the extractor most tools are built on), and you get this:
The list is the PDF's /ToUnicode map. Khmer makes it hard to write: one glyph can be half a letter (the left part of ៀ), a letter plus a mark (កា), or a subscript form (្ឌ), and the same glyph is reused in hundreds of words. Word writes one label per glyph, and for Khmer many of those labels are wrong.
Look at each glyph
These are the 22 glyphs that draw the heading, with their real shapes and positions taken from the PDF's embedded Khmer OS Muol Light font. Pick a glyph to see what the PDF's label says, what the font says, and which one khpdf uses.
Glyphs are in drawing order, which is not reading order: េ and ្រ are drawn before the consonant they belong to, and khpdf moves them afterwards. Data: samples/wb-khmer.pdf page 1, from khpdf's per-glyph decisions and the embedded font read with fontTools.
The label isn't a property of the glyph
If the labels were wrong in a consistent way, a lookup table could fix them. They aren't. Khmer OS fonts give every glyph a fixed number, so glyph 409 is the pre-base ្រ in all of them. Here is what the labels say glyph 409 is in four fonts, from two reports.
Shapes from each embedded font. "Draws" is how many times the glyph is drawn in the whole document. The red letter is that PDF's /ToUnicode label for the glyph.
Four fonts, four different labels, none of them ្រ. Over the whole 54-page World Bank report, 34,277 of the 83,821 Khmer glyphs drawn have a label that differs from what the font says. Some differences cancel out across a word (a vowel credited to the neighbouring glyph), but many change the text, as the heading shows. The font, on the other hand, can't be wrong about its own glyphs: it is what draws them.
The pipeline: eight questions, font first
Pick a real glyph from the sample PDFs and watch where it gets its answer.
Every example is a real glyph drawn in samples/, with khpdf's actual decision. The order is the order in the code. Step 7 names a glyph the font lost by checking that the labels of several shapes built from it agree; a single label is never trusted. Step 8 is for a font embedded more than once, where one copy lost its tables. It was added for a copy in the ADB 2014 brief that turned out to be a khpdf bug: after a fake-bold letter drawn inside q … Q, khpdf kept using that letter's font instead of switching back. With that fixed, none of the sample PDFs needs step 8.
Technical detail: the decision in code
From src/pdf/font.rs. font_derived is what steps 1 to 8 found for the glyph. A font answer that contains Khmer wins over the PDF's label; a label that looks broken never wins.
// When the embedded font itself identifies a Khmer glyph, trust the font.
if let Some((recovered, method)) = font_derived {
if recovered.chars().any(is_khmer_char) {
return (recovered.clone(), *method);
}
}
if let Some(mapped) = self.to_unicode.get(&code) {
if !is_suspicious_to_unicode(mapped) {
return (mapped.clone(), RecoveryMethod::ToUnicode);
}
}The steps live in src/pdf/embedded.rs (cmap, block order, glyph names), src/pdf/gsub.rs (role inference, then reverse GSUB), src/pdf/khmer_os_glyphs.rs with khmer_os_template_applies in src/pdf/font_dict.rs (shared layout), infer_from_ligature_labels in src/pdf/gsub.rs (agreeing labels) and src/pdf/resources.rs (siblings). JSON output lists which ones each text span used, in recovery_methods.
Borrowing the numbering from another Khmer OS font
A 2023 Asian Development Bank report, also made in Word, embeds Khmer OS Muol Light with nothing left to read: no character map, no shaping rules, no glyph names. Only the shapes and the PDF's labels survive. Its heading came out of pdftotext as ការបង្កើតមជ្ឈដ្ឋានអំងោយផលសម្រាប់ងសដ្ាកិច្ចឌីជ្ីថល.
But glyph shapes are enough to prove the numbering is shared. Every glyph shape present in both this font and the Muol Light in the World Bank report from seven years earlier is identical, at the same number: 46 of 46.
Left: ADB 2023 (no map, no rules, no names). Right: World Bank 2016. Both drawn from the PDFs' embedded fonts; "identical" means every outline point matches.
So khpdf carries the Khmer OS numbering for 283 glyphs, built from complete copies of Khmer OS Siemreap and Muol Light. It is applied only to fonts named Khmer OS, and only when at least 80% of the PDF's own single-letter labels agree with it. That check keeps it away from a font that merely has the name.
Share of each font's single-letter /ToUnicode labels that match the Khmer OS layout; dashed line at 80%. The World Bank font doesn't pass, and doesn't need to: it still has its own map and rules.
Finding ្ and ា from the jobs they do
A 2014 ADB brief uses Khmer MEF1, which isn't a Khmer OS font, so there is no shared layout to borrow. Its subsets kept a character map, but without the coeng sign ្ and the vowel ា: neither is ever drawn on its own, so the subsetter dropped them. Without them, none of the subscript or ា combinations can be read from the shaping rules.
Shapes built by the font's rules from glyph 569 (identified as ្) and glyph 482 (identified as ា). Neither 569 nor 482 has a shape of its own. Font: Khmer MEF1 embedded in samples/adb-cds-nr-2014.pdf.
With both signs known, reverse shaping fills in every subscript and every ា pair, and the text comes back:
Technical detail: the rule, and a tie
infer_coeng_glyph in src/pdf/gsub.rs counts, over two-glyph ligatures, unmapped glyphs that come first before a known consonant (coeng candidates) and unmapped glyphs that come second after one (ា candidates). The top candidate is accepted if it appears in at least five ligatures and the font doesn't already have that sign.
In this font glyph 569 leads with 31. For ា three glyphs tie at 31: 482, 522 and 524. Two of them, 482 and 522, build exactly the same shapes with every consonant, as ា and an alternate form would. On a tie khpdf takes the lowest glyph number, 482. That rule is simple rather than clever; the output above is how we checked it.
What this changes
- A PDF's text layer is a claim, the font is evidence. When they disagree for Khmer, the font wins, because it is what readers actually see.
- Wrong labels aren't consistent. The same glyph gets a different wrong label in every font we looked at, so no correction table could work. Each font has to be read.
- Inference needs a check. The shared layout is used only when the PDF's own labels mostly agree with it; role inference needs at least five matching rules.
The examples are on the Problems page too. The shared layout comes from the Khmer OS fonts by the Khmer Software Initiative. Numbers on this page come from khpdf's own per-glyph decisions on the sample PDFs, checked against the embedded fonts with fontTools and pypdf.