Architecture¶
How a PDF becomes text. Module paths are under src/kurdpdf/.
Pipeline¶
open_pdf ─► census (pass 1) ─► font classes, map checks, legacy tables
│
per page (pass 2) ◄───┘
│
├─ no visible glyphs, mostly image ─► OCR page
├─ glyphs ─► resolve ─► order ─► verify ─► weak lines? ─► OCR lines / page
│ low coverage? ─► OCR page
└─ text layers: raw, resolved, text (orthography)
│
structure: blocks, furniture across pages
│
export: txt, md, docx, json
- Open (
pdf/document.py): file size and page limits, password, encryption. PDF actions, JavaScript and attachments are never touched. - Census (
pdf/census.py,pdf/runs.py): every page's text trace is turned into glyphs (glyph ID, ToUnicode value, font, position, box). Each font program is loaded with HarfBuzz (glyphs/faces.py), each distinct glyph gets an outline fingerprint once per document (glyphs/fingerprint.py), and each font gets a class (C1–C6, latin, unused) from its map, its program and what it draws. Glyphs that an opaque filled rectangle drawn later covers, and whose box shows only that fill's colour in a render, are invisible like render mode 3 text (runs.hidden_chars). - Map checks (
glyphs/gsub.py): a ToUnicode map that disagrees with the joining forms in the font's own GSUB table turns a C1 font into C2. - Legacy tables (
glyphs/legacy.py): C4 fonts are matched to a shipped family table by fingerprint. - Resolve (
glyphs/resolve.py): each glyph gets characters from the first source that passes its check: ToUnicode checked against the atlas, the legacy table, the atlas (glyphs/atlas.py), labels learned from trusted fonts in the same document, the font's own cmap and GSUB, glyph names. Look-alike letters are settled with the word list (text/ambiguity.py,text/lexicon.py). For maps that number letters in glyph order, the failed joining groups (MapCheck.variables) get one letter each for the whole document: a probe pass over sample pages picks the letters that make the most known words (ambiguity.choose_letters). An outline the atlas knows as several non-letters (. and ') takes the one an untrusted map names. Symbol-font bullets and boxes are read from a hand-labelled outline table (glyphs/symbols.py,data/symbols.json). A shadda+vowel glyph a subset can no longer shape is read from the shadda and vowel contours other fonts of the document map (glyphs/markpairs.py). Unresolved glyphs stay unresolved. - Order (
layout/order.py): lines, word spaces, ZWNJ and bidi runs are rebuilt from glyph positions, not from the order in the content stream. Marks go to the letter under their ink (font extents), not under their advance box. Ruled tables (layout/tables.py, grids from the PDF's drawn lines) are taken out first and their glyphs placed in cells; columns are split at a gutter unless their rows share baselines and do not look like prose columns (or book pages: wide, lines starting at one edge). Drawn frames that hold whole lines (tables.find_frames) are regions of their own, ordered the same way and put before the first line below them in their column. - Verify (
glyphs/verify.py): each line's text is shaped again with the document's fonts and compared with the glyphs drawn; each page gets a score. The reference font must holdFACE_SHARE(0.8) of a segment's drawn outlines and map every letter of its text; a shaped glyph the atlas reads as the same letter and form as a drawn one counts as a match (contextual alternates). Segments with no such font (legacy fonts read by table) or with visible glyphs that have no outline are not scored, so a page can have no score. - OCR (
ocr/): pages and lines the steps above could not read (see ocr.md). - Orthography (
text/orthography.py): the zimankit Sorani rules make thetextlayer, only on lines identified as Central Kurdish. Before identification, pages typed on an Arabic keyboard (ة ض ط ذ ث ؤ standing in for ە چ گ ژ پ ۆ) get the Kurdish letters, word by word with the word list (rulelegacy-arabic-letters); quoted Arabic keeps its letters. Arabic words that begin with an alef keep it instead of taking a hamza when the word list does not know them (rulearabic-initial). A suffix written apart from its word after a bracket, quote, hyphen or slash ((ڕوانگە)ەوە, /-ەکە/) takes no hamza (ruleattached-suffix). A word with the Arabic article or a hamza seat inside it and no Kurdish-only letter (السنة, فإذا) keeps its letters (rulearabic-words). A word-final ء stays ء (ضیاء, شیء), since Sorani never ends a word with ئ (rulefinal-hamza). Ruleswap-brackets(off by default) turns ( ) [ ] { } « » ‹ › round in right-to-left lines for books that set brackets the other way (text/brackets.py). Language ID runs on normalized text, so a Persian line such as نوشتهي can read as Sorani once its ه+ZWNJ becomes ە. A line with no Sorani-only letter that reads as another language as written stays Sorani only when the page's Sorani lines are legacy encoded and its other lines are not mostly Persian (_not_sorani). Short or undecided lines take the commonest language of their own script (Arabic script or Latin) on the page.--language(text/language.py) forces one language for every Arabic-script line or turns line languages off;Page.line_languagesandBlock.langcarry them to the exports, which write BCP 47, Office and RTF language tags per item. Persian lines take ی ک for Arabic ي ى ك and keep their ه and ZWNJ (rulepersian-letters). Marks and kashida zimankit drops are put back on the letter they were written on. - Structure (
layout/blocks.py): paragraphs, headings, lists, tables and furniture, using all pages (running headers are found by repetition). - Export (
converters/): TXT, Markdown, DOCX and JSON. The JSON Schema isdata/kurdpdf.schema.json.
extract.py holds the KurdPDF class that runs these steps; cli.py and commands/ are the
command line on top of it. web/ is the local web page (kurdpdf web): a standard-library HTTP
server on 127.0.0.1 that runs the same class in one worker thread and reuses the converters for
downloads. Every request needs the per-run token, the Host header must be the local address, and
the page is one self-contained HTML file under a strict Content Security Policy that renders PDF
text only as text. The page is also a page editor: web/editor.py keeps opened PDFs in memory
and renders thumbnails, the page list (source page or blank page, turn) lives in the browser, and
the server builds PDFs, per-page ZIPs or images from a list, or converts it to text through the
same job. web/pwa.py and web/sw.js make it installable (logo, manifest, a worker that caches
only the page and icons).
review/ is the glyph review (kurdpdf review): scan.py records unresolved and word-list
glyphs per PDF (outline as SVG, font, page, position, line context, a crop, suggestions) in a
store (store.py, default temp/review/: scans/, crops/, append-only reviews.jsonl,
labels.json, checks.json), and server.py serves the dashboard on the web/ handler.
Resolved decisions, keyed by outline fingerprint, go back into Resolver (reviewed, source
review) after the atlas, labels, the font's own cmap and glyph names, before word-list choices.
Rules the code keeps¶
- Faithful layers.
rawis the PDF's text layer andresolvedis the text from identified glyphs; neither is normalized. Only thetextlayer applies orthography, and every rule there has a name and can be turned off. - No guessing. A glyph no source identifies is reported, not filled in.
- HarfBuzz only for loading, drawing, measuring and shaping fonts. PyMuPDF is used for PDF structure, text traces, rendering and extracting font programs.
- Outlines, not names, identify fonts and glyphs.
- Local only. No outgoing network calls;
kurdpdf webonly listens on 127.0.0.1. OCR runs the localtesseractprogram on images in a private temporary folder.
Data model¶
model.py: Document has fonts (FontInfo), pages (Page), warnings and tools. A
Page has the method (raw, unicode, recovered, ocr, mixed, no-text), the three text
layers, coverage, the count of characters per source, the verification score, blocks, warnings,
OCR details and, on request, every glyph.