# Display list format A display list is what rtex gives a host to draw: glyphs from font files at exact positions, rules, images and colour changes. It is read straight from LuaTeX's internal boxes, without going through a PDF, and it places every glyph exactly where LuaTeX's own PDF writer puts it ([correctness.md](correctness.md)). Format revision: **1** (rtex 0.0.8). The revision is in the header, and readers must skip record types they do not know. ## Coordinates and units - All lengths are TeX **scaled points** (sp): 65536 sp = 1 pt, and 65781.76 sp = 1 PostScript point (bp). - x grows to the right and y grows **downward**. - A **unit list** (from a live update) has its origin at the top-left corner of the unit's box. Its `lines` are the unit's rows, in order. A `ParagraphUpdate`'s `fragments` say where each row goes on the page ([embedding.md](embedding.md#paragraphupdate-a-unit-was-re-typeset)). - A **page list** (from a layout) has its origin at the top-left corner of the page. Each line carries the unit and row it belongs to. Material that belongs to no unit (headers, footers, footnote rules, page-level colour) is in `other`, which comes before the lines. A *row* is a line box reached from the unit through vertical boxes only: a text line, a display-math row, a list item line, a float's image line or caption line, a table's outer box. Footnote text keeps the paragraph it came from (`par`) but belongs to no unit, so a live update of the paragraph replaces its rows and not its footnote text. ## Drawing it **Glyphs.** Draw glyph `glyph_index` of the font file `filename` (subfont `subfont` for collections) at `(x, baseline_y)`, at size `size_sp`. Then apply three adjustments: - scale it horizontally by `1 + expansion / 1 000 000` (microtype font expansion; LuaTeX writes the same factor into the PDF); - if `extend` is non-zero, also scale it by `extend / 1000`; - if `slant` is non-zero, shear it by `slant / 1000`. `x` and `baseline_y` already include the glyph's offsets. `char_code` is the character, for text extraction and for TFM/Type 1 fonts, which have no glyph index. `advance` is how far TeX moved after the glyph. It is informational only, because positions are absolute. **Rules** are filled rectangles. **Images** refer to an image resource index. `IMAGE_INFO` records give the file, the page (for PDF images) and the page count. graphicx draws a bitmap at its natural size inside a matrix group: - `MATRIX save`, then `MATRIX set "a b c d"` about a point `(x, y)`; - the image; - `MATRIX restore`. Apply the matrix about that point to the image rectangle. **Colour** records are LuaTeX colour-stack operations. `data` holds the raw PDF colour operators (for example `1 0 0 rg 1 0 0 RG`). `set` replaces the top of the stack, and `push`/`pop` nest. A unit list starts with a `set` when the colour at its start is not black. **Fonts** have per-process numeric ids. To recognise the same font across lists and processes, compare file, subfont, size, slant, extend and squeeze (`FontDesc::key()` in Rust). **Degraded content.** A list that contains any `FLAG`, `LITERAL` or `UNSUPPORTED` record is *degraded*: something on it could not be described. Draw that page from the layout's `pdf_fallback` PDF instead. `MATRIX` records do not degrade a page. A picture taken from the picture cache appears as `UNSUPPORTED { kind: "cached_picture", detail: " " }`. That is the picture's rectangle in the list's coordinates, and the page carries the `pic_cache` flag. In a live result, this tells a host that draws the page from the PDF where the picture now stands. It can copy that rectangle from its rendering of the layout PDF (same size) before drawing the unit's rows. ## Binary encoding Little-endian throughout. `str` is a `u16` byte length followed by UTF-8 bytes. ``` Header, 16 bytes 'L' 'O' 'D' 'L' magic u16 version 1 u16 flags bit 0: 1 = page list, 0 = unit list u32 total_length bytes, including this header u32 reserved 0 Records, until END: u8 tag, u32 payload_length, payload (skip any tag you do not know, using payload_length) 0x01 META i32 width, i32 height, i32 depth, i32 page, i32 page_width, i32 page_height, i32 origin_x, i32 origin_y, u32 glyph_count, u32 image_count In unit lists, origin_y holds the number of inserts (footnotes, marginal notes) the unit produced; their text is not in the list. 0x02 FONT u32 font_id, i32 size_sp, u8 kind, u8 reserved, u16 subfont, i32 slant, i32 extend, i32 squeeze, i32 designsize, str filename, str psname, str name, str fullname, str format kind: 0 unknown, 1 OpenType, 2 TrueType, 3 Type 1, 4 Type 3 (virtual fonts are expanded into their real fonts and never appear) slant/extend/squeeze in thousandths; 0 means unset 0x03 FLAG str key, i32 value reason the list is degraded 0x10 LINE i32 par, i32 line_index, i32 x, i32 baseline_y, i32 width, i32 height, i32 depth, f64 glue_set, u8 glue_sign, u8 glue_order 0x12 LINE_UNIT i32 unit, i32 row page lists only, right after LINE 0x11 LINE_END 0x20 GLYPHS u32 font_id, i32 baseline_y, i32 expansion, u32 n, n × { u32 char_code, u32 glyph_index (0xFFFFFFFF = none), i32 x, i32 advance } 0x21 RULE i32 x, i32 y_top, i32 width, i32 height 0x22 COLOR u8 cmd (0 set, 1 push, 2 pop, 3 current, 255 unknown), u8 reserved, u16 stack, str data 0x23 LITERAL i32 mode, str data raw PDF literal or \special (mode −1) 0x24 UNSUPPORTED str kind, str detail 0x25 MATH u8 on, i32 x inline math boundary, for hit-testing 0x26 IMAGE i32 resource_index, i32 x, i32 y_top, i32 width, i32 height 0x27 IMAGE_INFO u32 resource_index, u32 page, u32 pages, str file 0x28 MATRIX u8 op (0 save, 1 set, 2 restore), i32 x, i32 y, str data ("a b c d") 0x29 MATH_OWNERS u32 n, n × { i32 owner, u32 font, u32 char, i32 left, i32 top, i32 right, i32 bottom }, u32 m, m × { i32 owner, u8 role, u8 empty, u16 reserved, i32 left, i32 top, i32 right, i32 bottom }; nothing may follow 0xFF END ``` Decoders: `rtex_dl::DisplayList::from_binary` in Rust, and `rtex_dl_to_json` in the C library. Inside TeX, the encoder is `tex/rtex-dl-bin.lua`. ## JSON form `rtex dl2json ` and `rtex_dl_to_json` turn the binary form into JSON with the same content. `rtex serve` sends display lists in this form. ```json { "kind": "paragraph" | "page", "unit": "sp", "width": 0, "height": 0, "depth": 0, "page": 0, "page_width": 0, "page_height": 0, "origin": [0, 0], "fonts": { "": { "id", "size", "filename", "psname", "subfont", "slant", "extend", … } }, "images_info": { "": { "file", "page", "pages" } }, "flags": { "": 1 }, "glyphs": 0, "inserts": 0, "other": [ /* items that belong to no line, as below */ ], "lines": [ { "par": 0, "i": 1, "x": 0, "y": 0, "w": 0, "h": 0, "d": 0, "gs": 0.0, "gsign": 0, "gorder": 0, "unit": 0, "row": 1, // page lists only "items": [ … ] } ] } ``` | Item | Fields | Meaning | |---|---|---| | `["g", font, char, index, x, y, advance, expansion]` | `index` is null for TFM fonts | glyph | | `["r", x, y_top, w, h]` | | rule | | `["c", stack, cmd, data]` | `cmd`: 0 set, 1 push, 2 pop, 3 current | colour | | `["l", mode, data]` | | PDF literal or `\special{pdf:…}`; degrades the list. Other specials draw nothing in the PDF and are left out | | `["u", kind, detail]` | | something not representable; degrades the list | | `["m", "on"\|"off", x]` | | inline math boundary | | `["i", index, x, y_top, w, h]` | | image | | `["M", "save"\|"set"\|"restore", x, y, data]` | | transformation | ## Optional math ownership With `math_owner_attribute` configured, a list containing tagged nodes also has `math_owners` (binary record `0x29`). It is metadata, not drawing instructions. Both the JSON field and binary record are absent when the option is unset, or no tagged nodes were observed. Older binary readers skip the record as they do other unknown records. In the binary record a field's role is a code: 1 `num`, 2 `den`, 3 `radicand`, 4 `sup`, 5 `sub`, 6 `degree`, 7 `cell`, 8 `math`; `empty` is 0 or 1. Any other value, or trailing bytes, is malformed. ```json "math_owners": { "glyphs": [{"owner": 11, "font": 20, "char": 97, "bbox": [100, 200, 300, 400]}], "fields": [{"owner": 60, "role": "num", "bbox": [250, 180, 250, 180], "empty": true}] } ``` `owner` is the signed integer carried by the configured LuaTeX attribute, including zero and negative values (LuaTeX's unset value, `-2147483647`, produces no ownership). rtex does not interpret it as a source offset or keep a source registry. `font` and `char` identify the observed glyph **before virtual-font expansion**; a virtual glyph can draw several display-list glyphs. A glyph box uses its advance and font height/depth, not an outline's ink bounds. Every `bbox` is `[left, top, right, bottom]` in sp, in the same coordinate space as the list's items. Packed math fields have roles `num`, `den`, `sub`, `sup`, `radicand` or `degree`. Paired math boundaries have role `cell` inside an alignment cell, otherwise `math` (including classic LaTeX's boxed radical degree). Their vertical bounds use the enclosing line's height/depth. These are observations of LuaTeX output, not a reconstruction of the source tree; a macro may produce several observations for an owner. The host assigns and interprets the ids. A truly empty sub-mlist gets a zero-size marker at its typeset position, reported as `empty: true` with a point box. Empty math boundaries (including matrix cells) also give point boxes. Hosts can enlarge these for hit-testing without changing typesetting. Nonempty math fields receive no markers: putting a marker before a character can change italic correction. LuaTeX discards a zero-width radical degree under unicode-math. Its `degree` point instead uses the converted radical box, the effective `RadicalKernBeforeDegree` and `RadicalDegreeBottomRaisePercent` math parameters: the leading degree kern gives x, and the radical delimiter's height, depth and shift give the degree baseline. Only attributes carry these observations through conversion; no extra degree nodes alter the output. The point is the degree's left edge at its baseline, within the radical glyph's horizontal span for Latin Modern Math, rather than necessarily left of the glyph's origin. See [embedding.md](embedding.md#math-source-ownership).