Skip to main content

Module doc

Module doc 

Source
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:

constructormeaning
textan atom: printed whole, never split, never changed
linea break opportunity: one space when it does not break
softbreaka break opportunity: nothing when it does not break
hardlinean unconditional line break
blocksepa line break followed by n blank lines
nestindent the child by a fixed number of columns
alignindent the child to the column where it starts (a hanging indent)
groupa unit whose break opportunities all stay unbroken (flat), or all break
concata sequence
prefixwrite a string before every line of the child (> )
rawverbatim lines, never reflowed
tablerows of cells laid out in computed columns
markask the printer to report where the child is printed
flush_rightset the child on one line, ending at the right edge of the width where it fits
ruledthe child on lines of its own, with a rule line as wide as its widest line above it, under it, or both
fill_wordsprose: 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-size Node value;
  • text: every text atom, every line prefix and every raw block, copied into one shared String; a node holds only a byte range into it;
  • children: the children of every sequence, appended into one shared pool; a Concat node holds only a range into it;
  • rows, cells, cols, tables: the data of table nodes, held outside the nodes;
  • rules: the data of ruled nodes, 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.
TableCell
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 raw block.
TableFrame
How a table is drawn around and between its cells.

Functions§

is_breakable_space
Returns whether c is 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.