Skip to main content

Module printer

Module printer 

Source
Expand description

The printer: it turns a layout document into text, and reports where each marked part of the document was printed.

print() and print_full take a layout document (DocBuf), the id of its root node and a Profile (the target width). They return the text; print_full also returns one MarkSpan per mark (PrintOutput).

The output depends only on the document, the root node and the profile: the printer makes one pass over the document with one explicit work stack, uses no hash-map iteration and no floating point, and keeps only a record of the nodes it has printed apart (table cells, and the children of flush_right and ruled nodes), which depends on nothing but the node and the width, so two runs give the same bytes. The printer calls itself only for the nodes it prints apart, in separate runs: table cells, and the children of flush_right and ruled nodes (section 6).

The rest of this documentation describes the six mechanisms that make up the printer’s behavior, for a reader who needs to predict the output exactly or to maintain the printer. The names in code font below that no link leads to (Frame, Flat, Break, fits, IndentId, IndentUnit, Spaces, Prefix) are the printer’s internal names, given for maintainers; the lowercase ones (group, line, nest, …) are the constructors of DocBuf.

§1. The work stack and the two modes

Each entry of the work stack is a Frame: the indentation stack that applies, a mode, and the work to do (a node to print, or “close this mark”). The mode says what a break opportunity prints: in Flat mode a line prints as a space and a softbreak as nothing; in Break mode both print as a line break. The root frame starts in Break mode when the profile has a width, and in Flat mode when it has none (“no width” means “never wrap”, so a line outside any group prints as a space).

A group reached in Flat mode stays flat: once a group has been found to fit on the line, everything inside it is flat too, and nothing more is tested. A group reached in Break mode is tested by the fitting scan of section 2, and its content is printed in Flat mode if it fits, in Break mode otherwise.

§2. The fitting scan, including what follows the group

The fitting scan (fits) decides whether a group fits. It walks the group’s content in Flat mode, adding up widths against the room left on the current line, and then keeps walking the rest of the work stack, each remaining frame in the mode it was recorded with. It answers yes at the first break opportunity it reaches in a Break-mode frame (the line ends there in any case, so nothing after it can make this line too long), or when the whole rest of the document has been walked. It answers no as soon as the total exceeds the room left, or when it reaches an unconditional break (hardline, blocksep, a raw block of several lines, a table that has columns and rows, a ruled node whose child prints something) inside the group being tested, since such a group cannot print on one line at all. A flush_right node counts as an atom as wide as its gap and its child’s one line, or as nothing when its child prints nothing; the columns counted for the unbroken lines directly before it are given back, since the node replaces their spaces by its own (section 6).

Walking on past the group makes prose wrap correctly. Each space between two words of fill_words is its own group; when the scan for the last such group reaches a trailing text after the prose, a text with no break opportunity of its own, it finds that the word plus that text would be too long, and the group breaks. A scan that stopped at the group’s end would instead keep the word on the line, and the trailing text would make the line too long.

A group reached during the scan keeps the mode of the frame it came from: flat inside the group being tested, Break in the rest of the work stack. A group further along the work stack has not been decided yet, so its own break opportunities count as places where the line may end. Prose filled word by word therefore looks exactly one word ahead, instead of requiring that the rest of the paragraph fit.

The scan does not count indentation: nest, align and prefix change where later lines start, and the scan stops at the first line break, so the scan’s stack holds only (mode, node) pairs, and the indentation affects the scan only through the current column, where it starts. This is a known limitation: an indentation node inside a group at the start of a line does change that group’s first line, and the scan does not count it, so group(nest(4, ..)) printed right after a line break can be found to fit when its indented first line does not. Emitters keep indentation outside groups (fill_words puts groups only around single break opportunities), which avoids the case.

§3. The indentation stack

A frame’s indentation is not a number of columns but an IndentId: a reference into a persistent stack of IndentUnits whose stacks share their common outer part. Each unit is either Spaces(n) (from nest, and from align, which pads to the column where it starts) or a Prefix (from prefix). At each line start, the stack is written in nesting order, so a list inside a quotation gives "> " then the item’s indentation, and a quotation inside a list gives the item’s indentation then "> ". The printer stores the display width of each stack, and the width available for wrapping is measured from it, so content inside prefix("> ", ..) wraps two columns earlier.

Because a line’s indentation is written only when its first content character is (section 4), the rule “a prefix applies to the current line if it starts at a line start, and from the next line otherwise” needs no special case: a prefix that starts in the middle of a line cannot change the line’s indentation, which has already been written. Adjacent Spaces units are merged into one, so nest(2, nest(3, ..)) uses one unit and prints like nest(5, ..); the merge keeps the outer unit, so it never hides a prefix from the blank-line rule below.

§4. Writing a line only when content arrives: the pending break and the spaces not yet written

The printer never writes whitespace ahead of time:

  • A request for a line break (line or softbreak in Break mode, hardline, blocksep) writes no newline. It records a pending break with a number of blank lines (blocksep(n) asks for n, everything else for 0). A request made while another is pending is merged with it, and the larger number of blank lines is kept; the numbers are never added. So hardline next to blocksep(1) gives one blank line, and blocksep(2) next to blocksep(1) gives two.
  • The indentation stack, and the space of a flat line, are written only when a content character follows on that line. A content character is a character of the layout document’s own text that the printer writes, as opposed to the indentation, the prefixes, the spaces and the line breaks the printer adds.

Therefore no line ends in whitespace the printer added; a pending break at the very start of the output is dropped; a pending break at the very end is replaced by exactly one final newline; and a blank line can only come from blocksep or from a newline inside raw content. Runs of newlines cannot arise, so no pass removes them afterwards.

What a blank line contains. A blank line written for a pending break holds the longest common outer part of two indentation stacks (the stack that applied at the last content character and the stack that applies where the break is written), trimmed of trailing whitespace. The two stacks are compared by identity of their units, not by their text: each time a prefix node is printed it pushes a unit of its own, so the common part is exactly the units that the two stacks share. Consequences: a blank line between two paragraphs inside one prefix("> ") prints >, so the two paragraphs remain one quoted passage; a blank line between two sibling prefix("> ") nodes prints nothing, so they remain two separate quotations (the emitter decides whether two quotations are one, never the printer); and stacks made only of spaces trim to an empty line.

§5. Marks

A mark changes nothing in the output; it only records a position. Its span starts at the first content character of its child, after any pending break, its blank lines and the line’s indentation, and ends immediately after the child’s last content character, before any space or line break that is written later. The start is found when that character is written: opening a mark only records it as waiting, and the next content character gives every waiting mark its start. A prefix written on a blank line (a > on a blank quoted line) is not content for this purpose.

A child that prints nothing gives an empty span placed at the next content character or, if nothing follows, at the end of the text before the final newline. Marks may be nested to any depth, have no width for the fitting scan, and are reported in the order they are opened (the order in which the printer reaches their nodes). Lines and columns are counted from 0, columns are display columns, and the end position is excluded from the span. A marked document and the same document without marks print the same bytes.

A table’s spans follow the spans of the marks opened before it, the mark around the table included. A table given a border mark (DocBuf::table) first reports one span under that mark for each run of characters it draws on a line, in the order they are written. Then come the marks inside its cells, row by row and column by column, each cell’s marks in their own order. Each cell is printed by a separate run of the printer, and each of its marks is placed where the cell’s text lands in the table: on the table’s line that holds the cell’s line, at the column after the indentation, the frame or the gutter, and the padding before the cell.

A span of a mark inside a cell never covers a character the cell did not print. The printer reports one span per occurrence of a mark, except that a mark inside a table’s cell over several lines of the cell gives one span per line of the cell on which it covers at least one character, because the table interleaves the cell’s lines with the rest of the row: the first span runs from the mark’s start to the end of that line of the cell, the last from the start of its line of the cell to the mark’s end, and each one in between covers its whole line of the cell. The cell’s line breaks belong to no span, and a blank line of the cell gives none. The spans of one mark are consecutive, in line order, at the mark’s place among the cell’s marks. A table nested in a cell places its cells’ marks into the cell, and the enclosing table places them once more. A mark inside a cell whose row prints nothing gets an empty span at the next content character after the table or, if nothing follows, at the end of the text before the final newline.

The marks inside the child of a flush_right or a ruled node come at their place in the order the marks are opened, each placed where the child’s text lands (section 6), one span per mark, as anywhere else. A ruled node given a mark reports one span under it for its rule line above, before the marks of its child, and one for its rule line under, after them. A mark inside a child that prints nothing gets an empty span at the next content character.

§6. Nodes printed apart: flush_right and ruled

The printer prints the child of a flush_right or a ruled node in a separate run, as it prints a table’s cells, then writes the lines of that printing into its own output:

  • The child of flush_right is printed with no width limit, on one line. The node replaces the spaces not yet written on the current line (the space of an unbroken line before it) by its own: as many as bring the child’s end to the last column of the width, where the current column, the gap and the child fit in the width, and otherwise the gap, or none at the start of a line. The child’s text is then written as content.
  • The child of ruled is printed at the width the node’s indentation stack leaves, with an empty stack. The node is written as a block: the rule line above, if any, each line of the child under the node’s stack (a blank line with the stack trimmed, as for a table), and the rule line under, if any. A rule line is the node’s character repeated to the widest line of the child’s printing.

The record of the nodes printed apart serves the child’s printing to the fitting scan, which needs the width of a flush_right child and whether a ruled child prints anything.

Structs§

LineCol
A position in the printed text: a line number and a display column on that line, both counted from 0.
MarkSpan
Where the child of one mark was printed: the mark’s identifier, the byte range of the child’s content characters in the printed text, and their first and last line and column.
PrintOutput
The result of print_full: the printed text, and where each mark’s child was printed in it.
Profile
The printer’s option: the target line width.

Functions§

print
Prints the layout document buf from its node root, at the width of profile, and returns the text.
print_full
Prints the layout document buf from its node root, at the width of profile, and returns the text together with one MarkSpan per mark occurrence.