Expand description
The layout document: the description of intended text that an emitter builds and hands to the printer.
A layout document describes output text without deciding where the lines
end. It holds the words as text atoms, the places where a line may end
(line, softbreak), the places
where a line must end (hardline,
blocksep), and the structure that sets indentation,
per-line prefixes, verbatim blocks and tables. The printer
(super::print) decides the line breaks for a given width.
An emitter creates a DocBuf, calls its constructors from the innermost
parts outwards (each returns the DocId of the node it creates), and
hands the id of the outermost node, the root, to the printer.
§The constructors
The constructors of DocBuf are the only way to build a layout
document; every one but fill_words creates one kind
of node:
| constructor | meaning |
|---|---|
text | an atom: printed whole, never split, never changed |
line | a break opportunity: one space when it does not break |
softbreak | a break opportunity: nothing when it does not break |
hardline | an unconditional line break |
blocksep | a line break followed by n blank lines |
nest | indent the child by a fixed number of columns |
align | indent the child to the column where it starts (a hanging indent) |
group | a unit whose break opportunities all stay unbroken (flat), or all break |
concat | a sequence |
prefix | write a string before every line of the child (> ) |
raw | verbatim lines, never reflowed |
table | rows of cells laid out in computed columns |
mark | ask the printer to report where the child is printed |
flush_right | set the child on one line, ending at the right edge of the width where it fits |
ruled | the child on lines of its own, with a rule line as wide as its widest line above it, under it, or both |
fill_words | prose: the words of a text, with one group around each space between two words |
Each constructor’s documentation states how the printer treats its node; the printer’s module documentation explains the mechanisms the constructors share.
§Representation: a flat arena
A DocBuf is a set of growable pools and nothing else:
nodes: every node, as one fixed-sizeNodevalue;text: every text atom, every line prefix and every raw block, copied into one sharedString; a node holds only a byte range into it;children: the children of every sequence, appended into one shared pool; aConcatnode holds only a range into it;rows,cells,cols,tables: the data of table nodes, held outside the nodes;rules: the data ofrulednodes, held outside the nodes.
Building a document therefore makes no heap allocation per node, and a
DocId is a plain u32, cheap to copy onto the printer’s work stack.
DocBuf::clear keeps the allocated capacity, so one arena can be reused
for one render after another.
Ids are given out bottom-up: a node’s children exist before the node
itself, so the arena cannot contain a cycle. A node may be shared: the same
DocId may appear in several sequences, and the printer prints it at
each occurrence (the arena is a directed acyclic graph, not a tree).
Structs§
- ColSpec
- The specification of one column of a table: the alignment of its cells.
- DocBuf
- A layout document: an arena of layout nodes, with its pools of text, children, rows, cells, columns, tables and rules.
- DocId
- The identifier of a node of one
DocBuf: its index in the arena. - MarkId
- The identifier of a
mark, chosen by the caller. - Table
Cell - One cell of a table: its content, a node of the same arena, and optionally an alignment of its own.
Enums§
- ColAlign
- Horizontal alignment of the cells in one table column.
- RawPolicy
- Whether the enclosing indentation and prefixes apply to the lines of a
rawblock. - Table
Frame - How a table is drawn around and between its cells.
Functions§
- is_
breakable_ space - Returns whether
cis whitespace at which a line may break: every Unicode whitespace character except the non-breaking spaces (U+00A0 no-break space, U+2007 figure space, U+202F narrow no-break space), which join the words on either side of them into one word.