PDF Rendererdoc/{zh,en}/design/plugins/pdf-render.md

PDF Renderer

Status: built 2026-12-03 as the follow-up of slice S4d of the media parsing research (P100, ADR 0018), with the owner's choices of that day: a native Go renderer (no PDFium, no Pop

Status: built 2026-12-03 as the follow-up of slice S4d of the media parsing research (P100, ADR 0018), with the owner's choices of that day: a native Go renderer (no PDFium, no Poppler or MuPDF, no new module dependency), and every text page compared with OCR of its drawing. Verified on macOS arm64; the code has no platform files, and Linux and Windows are cross-compiled, not run.

1. What it does

A PDF page's text layer can be wrong in ways the layer itself does not show: a font whose Unicode map names the wrong letters (a Type 3 font whose glyph names lie), words hidden under a white box or outside a clip, or no layer at all because the words are drawn as outlines. Only what the page shows tells, so Orquelo draws the page and reads the drawing:

  • ◆Garbled pages (most characters without Unicode) and pages without text that draw shapes or images are read from the drawing by the image model where media.models.image names one, else by local OCR.
  • ◆Every other page with at least 4 letters is compared with local OCR of its drawing. A layer that matches keeps its text, tables and layout; one that does not is replaced by the reading (the image model's where one is configured, else local OCR's), and its text is not shown.
  • ◆Each page read from its drawing gets a visible note saying why, which reader read it and, for a replaced layer, the similarity; media.Quality records reread (scanned, garbled, drawn, disagrees), read_by and ocr_similarity, and the properties list "pages read from their rendering". A page that should be read but cannot be (no reader, the read fails) shows no text and a note naming why; a page that cannot be checked keeps its layer.
  • ◆Scanned pages are read as before, from their own scan image.

orquelo media parse does the same with local OCR (or the image model with -c); orquelo media eval reports per file how many pages were compared, the lowest similarity, and the pages read from their images by reason.

2. Structure

PackageRoleOwner of
pkg/pdf (draw*.go, colour.go, function.go)Orchestrator of a page's drawing: runs the content stream, keeps the graphics state, colours, clips, text state; hands each paint to a Device (Page.Draw)the interpreter's state, per call
pkg/pdf/renderProvider: the Device that rasterizes onto an RGBA canvas (render.Page)canvas, clip masks, loaded fonts, per call
pkg/pdf/fontprogProvider: glyph outlines of embedded TrueType/OpenType, CFF and Type 1 programsnothing shared
plugins/parse-pdfplugin.PageImager: a scan's own image, else the page drawn, as PNGnothing
internal/host/parsing (scans.go, reread.go, agree.go)Orchestrator of the rereads: which pages, which reader, comparison, replacementthe document being built

The Host knows no PDF: it reads media.Quality and calls the parser's PageImage and the reader that produces ocr, as for scans (S5a).

3. What renders and what does not

RendersApproximated or not rendered
Paths: lines, Béziers, nonzero and even-odd fills (exact coverage across a pixel, four sub rows), strokes with width, caps, joins, miter limit, dashes; a line at least one pixelStrokes under a non-uniform transform use the mean scale
Clipping by paths (rectangles exactly, others as masks) and by text in modes 4–7Form XObject bounding boxes do not clip
Gray, RGB, CMYK, ICC by components, Lab, Indexed, Separation and DeviceN through function types 0, 2 and 3Type 4 tint transforms: gray by the darkest tint; no colour management
Images: raw, JPEG, JPEG 2000, JBIG2, CCITT, with soft masks, stencil masks and image masks; inline imagesColour-key masks; images sampled at the nearest pixel (masks bilinear)
Shadings and shading patterns as one flat colour (halfway along their function); tiling patterns as a flat colourGradients, mesh shadings, pattern cells
Constant opacity; Multiply, Darken, Lighten, ScreenOther blend modes as Normal; what an ExtGState soft mask covers is left out
Fonts: embedded TrueType, OpenType (glyf or CFF), bare CFF (name- or CID-keyed), Type 1 (with seac, flex), Type 3 as content; render modes fill, stroke, both, invisibleHinting; fonts not embedded are drawn with the Go fonts by their encoding's text and fitted to the PDF's widths, Latin, Greek and Cyrillic only, as viewers draw them, whatever their ToUnicode says: CJK fonts not embedded count as missing glyphs

A page with three or more missing glyphs making up over a tenth of its glyphs is refused (media.unsupported), as is one with three or more glyphs drawn with a stand-in for a font that is neither embedded nor standard (Helvetica, Times, Courier, Symbol, ZapfDingbats, Arial, Courier New) making up over half its glyphs, since the stand-in may not look like it (a Chinese font used as a simple font draws Latin-1 symbols), as is one that ran over its time (media.timeout): its drawing could not speak for its layer, so the layer is kept.

Bounds: 200 dpi, the longer side at most 2,000 pixels and 4 million pixels in all (the size local OCR reads at), 30 s a page or what is left of the parse, 2^20 edges a fill, 2^26 pixels an image, 16 clip masks and 4,096 outlines a font kept; font programs bound stack, subroutine nesting, operators, segments and composite components. A panic is an error with the drawing so far. Fuzz targets: FuzzDraw, FuzzRender, FuzzPrograms.

4. The comparison and its calibration

Both texts are reduced to letters and digits, lowercased and NFKC-normalised; the score is the share of the layer's distinct character trigrams that the reading also has. Reading order, spacing, punctuation and a merged table cell repeated in every row it covers weigh little; text the reading finds beyond the layer (words in pictures) weighs nothing; wrong or hidden characters weigh much. Under 40 letters a layer has too few trigrams for one misread character not to weigh much, so there the better of that share and its character similarity counts: 1 minus the edit distance of the layer to the part of the reading most like it, over the layer's length. A layer is compared only with at least 4 letters, four in five of them in scripts local OCR reads in the recognition language (Latin, Han and Kana by default; Hangul, Cyrillic or Arabic in theirs). A layer scoring under 0.5 is replaced, unless the reading found no letters at all (then nothing tells it is wrong, and it is kept).

The threshold was calibrated on the corpus of orquelo media eval (64 PDFs, scripts/media-corpus): its 76 compared pages, all with correct layers, scored 0.68 to 1.00 (eight of them short pages compared since the gate went from 30 letters to 4) (lowest: a table of state names, 0.68; an OCRmyPDF layer over a graph, 0.71; attention's attention-map figures, pages 13–15, 0.77). Wrong layers score far below: the lying Type 3 page 0.00 (a six-letter label, "QWXK 93" printed "HAZE 47", 0.00 too), a layer of letters shifted by seven 0.00, words under a white box and outside a clip 0.17, unrelated English text under 0.15 in the unit tests. The first metric, counting repeated trigrams, scored a correct table with merged cells 0.14 and replaced it; distinct trigrams fixed that.

orquelo media evalBeforeAfter
Text mean (42 files)0.3500.350
Table mean (14 files)0.2610.261
Scan detection (113 pages)precision 1.000, recall 1.000the same
Pages compared / replaced—76 / 0
Pages read from their images23 scanned23 scanned, 4 drawn (OCRmyPDF's vector sample, simple2, two pages of vertical)
Time for the corpus1 min 19 s4 min 56 s

5. Cost

Drawing takes 0.1–0.25 s a page (attention, 15 pages, 3.5 s with PNG encoding); local OCR of a drawn page about 3 s on an M-series laptop, so media parse of attention went from 0.26 s to 46 s. Pages are drawn four at once within the existing worker limit; the OCR worker reads one at a time. The result is cached with the document, keyed by the parsers' versions (parse-pdf 0.9.0).

6. Not built

Gradients and pattern cells; colour-key masks; Type 4 functions; transparency groups and soft masks; CJK fonts not embedded (no stand-in font with those glyphs ships); vertical CJK layers are not compared when their glyphs cannot be drawn; a page with a small hidden part among much correct text keeps its layer, hidden words included (the score stays above 0.5), and so does a page whose every word is hidden (the reading finds nothing); a model is not used for the comparison itself, only to re-read pages that fail it.