1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//! The layout engine: it lays text out in lines of a given width, for any
//! output written as characters in fixed-width columns (plain text, a
//! terminal).
//!
//! The engine takes two inputs and produces one output:
//!
//! - **A layout document**, built by the caller in a [`DocBuf`]. It describes
//! the intended text without deciding where the lines end: the words as
//! *atoms* (texts printed whole), the places where a line may end and where
//! it must end, indentation, per-line prefixes such as a quotation's `> `,
//! verbatim blocks, tables, a text set at the right edge of a line, rule
//! lines as wide as the block they stand above or under, and *marks*
//! (requests to report where a part of the document is printed). The
//! constructors of [`DocBuf`] are the whole set of things a layout document
//! can hold; the [`doc`] module lists them.
//! - **A profile** ([`Profile`]): the target line width in columns, or `None`
//! for no wrapping. It is the engine's only option.
//! - **The output**: the printer, [`print()`] or [`print_full`], decides the
//! line breaks for the profile's width and returns the text; [`print_full`]
//! also returns, for every mark, the byte range and the line and column range
//! of the marked part in that text ([`PrintOutput`], [`MarkSpan`]).
//!
//! An emitter (a backend's emit stage, which writes the output's bytes; see
//! [backends, lowering, and
//! outputs](crate::guide::concepts_overview#backends-lowering-and-outputs))
//! uses the engine when its output is text in columns: it builds one layout
//! document while it walks its input, then prints it once. In this library
//! the text backend's emitter ([`crate::stdlib::backends::text`]) is the only
//! caller of the printer; the HTML emitter and the presentation helpers use
//! [`is_breakable_space`] so that they split words at the same characters.
//! [The plain-text backend](crate::guide::rendering#the-plain-text-backend)
//! and [the layout engine](crate::guide::rendering#the-layout-engine) sections
//! of the rendering guide describe both.
//!
//! This module does not depend on anything else in the crate (neither on techy
//! nor on the document model), so any program that writes text in columns can
//! use it.
//!
//! # What the engine guarantees
//!
//! - **Breaking by width**: a [`group`](DocBuf::group) prints on one line if
//! it fits together with everything that follows it on that line; otherwise
//! every break opportunity directly inside it becomes a line break. Prose is
//! filled with [`fill_words`](DocBuf::fill_words), which puts one group
//! around each space between two words; each space then decides on its own
//! whether to stay a space, which gives correct greedy wrapping, even before
//! a trailing text that has no break opportunity of its own.
//! - **Display width by grapheme cluster**: the width of a text in columns is
//! the sum, over its grapheme clusters (what a reader sees as one
//! character), of each cluster's Unicode width. The width of two texts
//! joined is therefore the sum of their widths, and a longer text never
//! measures narrower than its beginning. `Über Bäume — 日本語` is 19 columns,
//! a family emoji 2. [`display_width`] gives the same measure to an emitter
//! that aligns text by hand.
//! - **One indentation stack**: [`nest`](DocBuf::nest),
//! [`align`](DocBuf::align) and [`prefix`](DocBuf::prefix) all push units
//! that are written at the start of every line, in nesting order, so a list
//! inside a quotation and a quotation inside a list are both laid out
//! correctly, and a prefix's width is subtracted from the width available
//! for wrapping.
//! - **Blank lines only from [`blocksep`](DocBuf::blocksep)**: adjacent
//! requests for a line break are merged into one, which keeps the larger
//! number of blank lines; a line break at the start or at the end of the
//! output is dropped; a blank line is written with the part of the
//! indentation stack that the lines around it have in common, trimmed of
//! trailing whitespace. The blank line between two paragraphs of one
//! `> `-prefixed quotation therefore prints `>`, and the blank line between
//! two such quotations prints nothing.
//! - **No trailing whitespace added by the engine**: indentation and the
//! space of an unbroken break opportunity are written only when a content
//! character follows on the same line.
//! - **[`raw`](DocBuf::raw) blocks are never reflowed**: their lines, blank
//! lines included, are written byte for byte, either under the indentation
//! stack or at column 0, as their [`RawPolicy`] says.
//! - **[`table`](DocBuf::table) columns are computed** from each column's
//! narrowest and widest content: the width left over is shared out in
//! proportion to each column's room to grow, in whole columns, after the
//! gutters or the frame are subtracted from the width; cells are wrapped at
//! their column's width and drawn as the table's [`TableFrame`] says: two
//! spaces apart with a rule of `-` under the header rows (`Gutter`), or
//! inside a frame of box-drawing characters with a rule of the frame under
//! the header rows (`Box`). A table given a border mark reports what it
//! draws under that mark, one span per run of drawn characters on a line.
//! - **[`flush_right`](DocBuf::flush_right) reaches the right edge**: its
//! child, set on one line, ends at the last column of the width where the
//! columns before it on its line, its gap and its own width fit in the
//! width, and otherwise follows what precedes it after the gap; inside a
//! table cell the edge is the cell's. The fitting scan counts it as an atom
//! of its gap and its child.
//! - **[`ruled`](DocBuf::ruled) rules are as wide as their child**: each rule
//! line repeats one character to the display width of the child's widest
//! printed line, a line it wraps included, and is written under the
//! child's indentation and prefixes. The node reports its rule lines under
//! its mark, one span per line.
//! - **[`mark`](DocBuf::mark) positions**: [`print_full`] reports, for every
//! occurrence of a mark, the byte range and the 0-based line and column
//! range of the marked part's content characters, in the order the marks
//! are opened; a table's border runs and the marks inside its cells follow
//! the marks opened before the table, the cells' marks row by row and
//! column by column, each placed where its cell's text lands in the table.
//! A mark inside a cell over several lines of the cell gives one span per
//! line of the cell, because the table interleaves the cell's lines with
//! the rest of the row. The marks inside the child of a `flush_right` or a
//! `ruled` node are reported where its text lands, in their order, one span
//! per mark. A mark never changes the layout.
//! - **Determinism**: the output depends only on the layout document, the root
//! node passed to the printer and the profile. The printer uses no hash-map
//! iteration and no floating point, and the record it keeps of the nodes it
//! has printed apart (table cells, and the children of `flush_right` and
//! `ruled` nodes) depends only on each node and the width, so two runs give
//! the same bytes.
//!
//! # Example
//!
//! ```
//! use flm_core::layout::{DocBuf, Profile, print};
//!
//! let mut b = DocBuf::new();
//! let label = b.text(" • ");
//! let body = b.fill_words("a list item whose text wraps under its own first word");
//! let hung = b.align(body);
//! let item = b.concat(&[label, hung]);
//!
//! let out = print(&b, item, &Profile::new(Some(32)));
//! assert_eq!(
//! out,
//! " • a list item whose text wraps\n under its own first word\n"
//! );
//! ```
//!
//! [`Profile`] carries the target width and nothing else: `None` means "never
//! wrap". Every other behavior is fixed: lines end in `\n`, a non-empty output
//! ends in exactly one `\n`, and an atom is never split. The width measure
//! cannot be changed either. The [`printer`] module's documentation explains
//! how the printer works.
//!
//! # Items of this module
//!
//! - [`doc`]: the arena [`DocBuf`] and its constructors; [`DocId`],
//! [`MarkId`], [`RawPolicy`], [`ColSpec`], [`ColAlign`], [`TableCell`],
//! [`is_breakable_space`].
//! - [`printer`]: [`Profile`], [`print()`], [`print_full`], [`PrintOutput`],
//! [`MarkSpan`], [`LineCol`], and the printer itself.
//! - `table` (crate-internal): the column solver and the assembly of rows into
//! lines.
//! - `width` (crate-internal): the width measures; [`display_width`] is
//! re-exported here.
//!
//! # Properties of the interface
//!
//! - **The constructors of [`DocBuf`] are the whole public interface for
//! building a layout document.** The nodes of the arena and the functions
//! that read them are crate-internal: a caller only builds layout documents,
//! and keeping their representation private leaves it free to change.
//! - **The width measure is public, and it cannot be replaced.** [`Profile`]
//! has no setting for the measure. [`display_width`] is the measure the
//! printer wraps against, offered for an emitter that aligns text by hand (a
//! rule of `=` as wide as a heading, a label padded to a column).
//! - **The requirements on the constructors' input are checked in debug builds
//! only.** An atom or a prefix must not contain `'\n'` or `'\t'`, the child
//! of a [`flush_right`](DocBuf::flush_right) must print on one line, and the
//! character of a [`ruled`](DocBuf::ruled) node's rule lines must be one
//! column wide. These are requirements on the emitter's code, not on a
//! document's content, and a violation has to be fixed in the emitter, so a
//! release build does not spend time on the check.
//! - **A table or a `ruled` node that prints nothing is not a block.** A table
//! with no columns, with no rows, or whose cells all print nothing, and a
//! [`ruled`](DocBuf::ruled) node whose child prints nothing, leave the
//! surrounding line and any requested line break as they are, because
//! output that does not exist should not force a line break. The fitting
//! scan (the printer's test of whether a group fits on the line, see the
//! [`printer`] module) agrees for a `ruled` node, and for the two cases of a
//! table it can decide without printing the cells (no columns, no rows); it
//! still treats a table whose rows all print nothing as a block.
//!
//! # Known limitations
//!
//! - The fitting scan does not count indentation, so an indentation node
//! inside a group at the start of a line is not counted against that group's
//! first line. Emitters keep indentation outside groups, which avoids the
//! case.
//! - A [`raw`](DocBuf::raw) block of one line is measured like an atom by the
//! fitting scan, even under [`RawPolicy::Exempt`], where it starts a new
//! line.
//! - The child of a [`ruled`](DocBuf::ruled) node is printed on its own and
//! its lines are written under the node's indentation, as a table's cells
//! are; a [`raw`](DocBuf::raw) block under [`RawPolicy::Exempt`] inside it
//! is therefore written under that indentation, not at column 0.
//! - The fitting scan stops once the width is used up by content characters,
//! but it can pass through many nodes of zero width in a layout document
//! whose nodes are shared many times; it has no limit on its number of
//! steps.
// Maintainer notes on the points above. The trait behind the width measure
// stays crate-internal so that the choice of measure remains free to change.
// The limitations listed above are kept as they are, each pinned by a test, so
// that a deliberate change to the layout can be told apart from an accidental
// one. Correcting the indentation limitation is not local: the fitting scan
// would have to track the indentation accumulated up to a group's first
// visible character.
// The scan needs no step limit as long as emitters build layout documents as
// trees rather than as heavily shared graphs, since its walk is then
// proportional to the size of the document.
pub use ;
pub use ;
pub use display_width;