Skip to content
Edit this page

Fonts and the glyph atlas

KurdPDF does not trust a PDF's text layer. It reads the font programs embedded in the PDF with HarfBuzz and decides, font by font, how far the text layer can be trusted and what to use where it cannot.

Font classes

kurdpdf inspect book.pdf --fonts shows each font's class and the evidence for it.

Class What it is How it is read
C1 Unicode font with a good ToUnicode map the map, checked against the atlas
C2 the map is wrong (U+FFFD, private-use characters, or letters that disagree with the font's own joining forms), but the program loads the atlas by outline, labels learned from other fonts in the document, the font's own cmap and GSUB, the word list
C3 Arabic presentation forms, often in visual order mapped back to base letters; order rebuilt from glyph positions
C4 legacy font that draws Kurdish letters at Latin or symbol code points (Ali-K, Ali-A, Zanest, Mewlewi, Halabja) shipped legacy tables for Ali-K, Ali-A, Zanest and the Halabja family
C5 opaque program (MSTT31 fonts, Type 3) or one glyph per word (Quran page fonts) OCR of the lines it covers; driver-downloaded legacy fonts by glyph order (C4)
C6 no usable program (not embedded, or HarfBuzz cannot load it) OCR
latin no Arabic-script text the map as is
unused listed but draws nothing ignored

Fonts are identified by glyph outline fingerprints, never by name alone: the same font is often renamed or subset differently by each PDF producer. A fingerprint is the glyph outline drawn through HarfBuzz, scaled to a 1000-unit em, rounded to whole units and hashed. The same glyph in any subset of a font, and in the full font file, gives the same fingerprint.

The atlas

The atlas maps fingerprints to characters and their joining form. It is built on your machine from Unicode Kurdish fonts you have, and is not shipped.

kurdpdf atlas build --font ~/fonts/kurdish --font /usr/share/fonts/truetype/noto
kurdpdf atlas list --fonts

For each font, KurdPDF shapes every Sorani and Arabic letter in each joining form, digits, punctuation, marks, shadda with each vowel, common two-letter ligatures and the الله and لله ligatures (also as discretionary ligatures) with HarfBuzz, and records the fingerprint of each glyph it draws. Fonts already in the atlas are skipped; --fresh starts over. Fonts without the Arabic-script letters are skipped.

The more of the fonts used by your PDFs the atlas holds, the more glyphs can be identified. Coverage and the page method in inspect show how well it works for a given PDF. A glyph the atlas does not know is left unresolved and reported; it is never guessed.

The atlas holds fingerprints and labels, not outlines. Still, build it only from fonts you have the right to use.

Separate dot glyphs

Some fonts (Mirza, Nizar Ruqaa) build dotted letters from a dotless body and dot glyphs, and some applications write those glyphs apart. Their maps send the dots to the Arabic dot symbols (U+FBB2 to U+FBC1), the small v and hamza to combining marks, and the bodies to U+FFFD or to the dotless letter (ح for خ). KurdPDF joins each dot to the letter of the same size and line whose ink (from the font's glyph extents) is nearest, and composes the letter (ح + one dot above = خ). A U+FFFD body is a tooth (ب ت ن ی پ) or a feh (ف ق ڤ): a body that carries dots at least five times, almost never below, is a feh. The evidence is counted over the whole document by outline fingerprint, since such producers embed a new subset on every page; when only some pages are converted, up to 40 pages spread over the document are read for it. These letters have source dots.

Legacy fonts

Ali-K, Ali-A and Zanest fonts put every Kurdish letter in a fixed slot (a Latin or symbol code point). A legacy table maps each slot to the letter and joining form it draws. The tables ship in src/kurdpdf/data/legacy/ and are identified by fingerprints, so a renamed font still matches. Some letters are drawn as a base glyph plus a separate mark (ڕ ڵ ۆ ێ); the mark is joined to the letter under or over it by position.

Some documents carry Ali-K fonts with an Arabic ToUnicode map. When the map follows the usual stand-ins (ة for ە, ث for پ, ؤ for ۆ), it is kept. When a font's outlines match a family table but fewer than 55% of at least 100 glyphs have a ToUnicode letter with the same skeleton as the table's letter, the map is in some other order: the font becomes C4 and is read through the table (evidence ToUnicode agrees with the … outlines for only …).

The fonts of one Ali-K, Ali-A or Zanest design keep their glyphs in a fixed order, and PDF producers keep the glyph IDs when they subset a font, though not always its codes: Word renumbers the codes in order of use and gives glyphs outside the symbol cmap (its justification kashida, ligatures) codes such as !. The tables record the glyph orders of the family's fonts. A PDF font is checked against them by the slot its outlines or its ToUnicode values name for each glyph ID; when at least 10 glyph IDs, and three quarters of those compared, agree with one order, glyphs without a known outline take their slot from their glyph ID (evidence glyph IDs in the family order). Fonts in another order, or subsets renumbered from scratch, are not read by it.

A legacy design that none of your reference fonts holds (an Ali-K naskh embedded under the name TraditionalArabic) matches no fingerprint and no name, but it keeps the family's glyph order. A table is then chosen when the font follows one of its orders and the table reads at least 95% of the glyphs the font draws. Ali-K and Ali-A share their slots and differ in the letters drawn there (ە and ة, ی and ي, پ and ث), so the font's text is read with each candidate and the reading zimankit identifies as Sorani wins (evidence legacy table … (by glyph order, …)).

Windows printer drivers download a TrueType font into the PDF by glyph index as a Type 1 or CFF font named MSTT31…, with no ToUnicode map and its outlines converted, so neither the codes nor the fingerprints name a slot. Such a font is C5 unless every glyph is named G and two hex digits: those digits are the glyph ID in the original font, and the original glyph IDs of an Ali-K, Ali-A or Zanest font follow the family's glyph order. An order that reads 95% of the glyphs drawn is a candidate. A Latin font downloaded the same way also passes, so the first 4000 glyphs the font draws are read with each candidate, both as drawn and reversed, since drivers often draw in visual order. The reading needs at least 3 words of three letters or more in the word list, and with an imported list a quarter of its words. Of the readings that pass, the one zimankit identifies as Sorani wins, then the one with more known words (evidence glyph names give the original glyph IDs; legacy table … (by glyph order)).

Running heads, names and signatures often hold too few listed words of their own. A driver font whose reading fails takes the table and order that another driver font of the same document confirmed, when that order reads 95% of its glyphs. A driver font whose glyphs (95% of those drawn) all sit at glyph IDs 3–97 is Latin: Latin TrueType fonts keep the standard Macintosh glyph order there, glyph 3 the space and then U+0020–007E in order. When that reading looks like Latin text, figures or a web address, the font is read that way and classed latin (page numbers, years, www… in footers). A driver font that none of this reads stays C5 and goes to OCR. A Persian font in an Ali-A design takes Ali-K's letters when Ali-K is the order the document confirmed (ث read as پ): the word list cannot tell the two families apart.

The Halabja family (HalabjaRegular, MewlewiRegular, KerkukRegular, DiyarbekirRegular, NesrinRegular, NaliRegular, MettinRegular) is one byte encoding drawn by seven designs. Its outlines match no Unicode font, so its table was read from renders of the glyphs, not derived (HAND_LABELLED). Ligatures such as نج, لم and ڵا sit at their own codes, and 0x3F is a short kashida. Producers split these fonts into extra subsets (HalabjaRegular+1, +2) with reassigned codes; those glyphs are matched by fingerprint only. The narrow blank glyph the family sets after letters that do not join left is not a word space.

Adding a legacy family

  1. Collect the family's font files and a set of Unicode reference fonts.
  2. Derive the tables: a slot takes the letter of the reference glyph with the same outline.
from kurdpdf.glyphs.legacy import TABLE_DIR, derive_tables

tables, stats = derive_tables(legacy=["fonts/legacy/"], reference=["fonts/unicode/"])
for family, table in tables.items():
    table.save(TABLE_DIR / f"{family}.json.gz")

Fonts are sorted into families by name with NAME_HINTS in glyphs/legacy.py; add a pattern for the new family there (and to PRIORITY if it shares outlines with another family). 3. Slots no reference glyph matches can be set by hand in EXTRA (ALI_SHARED holds the ligatures and symbols Ali-K and Ali-A share; glyph orders are collected by derive_tables), and mark glyphs in MARKS. A family no reference font matches at all goes in HAND_LABELLED and its table is written by hand (LegacyTable(...).save(...)) from renders of its glyphs. 4. Make a PDF with the font in temp-different-pdf-files/ (never committed), run kurdpdf inspect, and add a test with the expected class and text to tests/test_realpdf.py. Add a synthetic test in tests/test_legacy.py as well.

Reporting a font that fails

Open an issue with:

  • the output of kurdpdf inspect book.pdf --fonts and --page-table
  • the font name and the producer from the PDF metadata
  • a page number, and what the text should say on a line or two

Do not attach the font or the PDF unless you have the right to share them.