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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
use std::convert::TryFrom;
use std::fmt::Debug;
use std::hash::Hash;
use std::mem;
use std::ops::{Add, AddAssign};
use super::Styles;
use crate::diag::StrResult;
use crate::geom::SpecAxis;
use crate::layout::{Layout, PackedNode};
use crate::library::{
DocumentNode, FlowChild, FlowNode, PageNode, ParChild, ParNode, PlacedNode,
SpacingKind, SpacingNode, TextNode,
};
use crate::util::EcoString;
/// A partial representation of a layout node.
///
/// A node is a composable intermediate representation that can be converted
/// into a proper layout node by lifting it to a block-level or document node.
#[derive(Debug, PartialEq, Clone, Hash)]
pub enum Node {
/// A word space.
Space,
/// A line break.
Linebreak,
/// A paragraph break.
Parbreak,
/// A page break.
Pagebreak,
/// Plain text.
Text(EcoString),
/// Spacing.
Spacing(SpecAxis, SpacingKind),
/// An inline node.
Inline(PackedNode),
/// A block node.
Block(PackedNode),
/// A sequence of nodes (which may themselves contain sequences).
Sequence(Vec<(Self, Styles)>),
}
impl Node {
/// Create an empty node.
pub fn new() -> Self {
Self::Sequence(vec![])
}
/// Create an inline-level node.
pub fn inline<T>(node: T) -> Self
where
T: Layout + Debug + Hash + 'static,
{
Self::Inline(node.pack())
}
/// Create a block-level node.
pub fn block<T>(node: T) -> Self
where
T: Layout + Debug + Hash + 'static,
{
Self::Block(node.pack())
}
/// Style this node.
pub fn styled(self, styles: Styles) -> Self {
match self {
Self::Inline(inline) => Self::Inline(inline.styled(styles)),
Self::Block(block) => Self::Block(block.styled(styles)),
other => Self::Sequence(vec![(other, styles)]),
}
}
/// Style this node in monospace.
pub fn monospaced(self) -> Self {
self.styled(Styles::one(TextNode::MONOSPACE, true))
}
/// Lift to a type-erased block-level node.
pub fn into_block(self) -> PackedNode {
if let Node::Block(packed) = self {
packed
} else {
let mut packer = NodePacker::new(true);
packer.walk(self, Styles::new());
packer.into_block()
}
}
/// Lift to a document node, the root of the layout tree.
pub fn into_document(self) -> DocumentNode {
let mut packer = NodePacker::new(false);
packer.walk(self, Styles::new());
packer.into_document()
}
/// Repeat this template `n` times.
pub fn repeat(&self, n: i64) -> StrResult<Self> {
let count = usize::try_from(n)
.map_err(|_| format!("cannot repeat this template {} times", n))?;
// TODO(set): Make more efficient.
Ok(Self::Sequence(vec![(self.clone(), Styles::new()); count]))
}
}
impl Default for Node {
fn default() -> Self {
Self::new()
}
}
impl Add for Node {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
// TODO(set): Make more efficient.
Self::Sequence(vec![(self, Styles::new()), (rhs, Styles::new())])
}
}
impl AddAssign for Node {
fn add_assign(&mut self, rhs: Self) {
*self = mem::take(self) + rhs;
}
}
/// Packs a [`Node`] into a flow or whole document.
struct NodePacker {
/// Whether packing should produce a block-level node.
block: bool,
/// The accumulated page nodes.
pages: Vec<PageNode>,
/// The accumulated flow children.
flow: Vec<FlowChild>,
/// The common style properties of all items on the current flow.
flow_styles: Styles,
/// The kind of thing that was last added to the current flow.
flow_last: Last<FlowChild>,
/// The accumulated paragraph children.
par: Vec<ParChild>,
/// The common style properties of all items in the current paragraph.
par_styles: Styles,
/// The kind of thing that was last added to the current paragraph.
par_last: Last<ParChild>,
}
impl NodePacker {
/// Start a new node-packing session.
fn new(block: bool) -> Self {
Self {
block,
pages: vec![],
flow: vec![],
flow_styles: Styles::new(),
flow_last: Last::None,
par: vec![],
par_styles: Styles::new(),
par_last: Last::None,
}
}
/// Finish up and return the resulting flow.
fn into_block(mut self) -> PackedNode {
self.finish_par();
FlowNode(self.flow).pack()
}
/// Finish up and return the resulting document.
fn into_document(mut self) -> DocumentNode {
self.pagebreak(true);
DocumentNode(self.pages)
}
/// Consider a node with the given styles.
fn walk(&mut self, node: Node, styles: Styles) {
match node {
Node::Space => {
if self.is_flow_compatible(&styles) && self.is_par_compatible(&styles) {
self.par_last.soft(ParChild::text(' ', styles));
}
}
Node::Linebreak => {
self.par_last.hard();
self.push_inline(ParChild::text('\n', styles));
self.par_last.hard();
}
Node::Parbreak => {
self.parbreak(Some(styles));
}
Node::Pagebreak => {
self.pagebreak(true);
self.flow_styles = styles;
}
Node::Text(text) => {
self.push_inline(ParChild::text(text, styles));
}
Node::Spacing(SpecAxis::Horizontal, kind) => {
self.par_last.hard();
self.push_inline(ParChild::Spacing(SpacingNode { kind, styles }));
self.par_last.hard();
}
Node::Spacing(SpecAxis::Vertical, kind) => {
self.finish_par();
self.flow.push(FlowChild::Spacing(SpacingNode { kind, styles }));
self.flow_last.hard();
}
Node::Inline(inline) => {
self.push_inline(ParChild::Node(inline.styled(styles)));
}
Node::Block(block) => {
self.push_block(block.styled(styles));
}
Node::Sequence(list) => {
for (node, mut inner) in list {
inner.apply(&styles);
self.walk(node, inner);
}
}
}
}
/// Insert an inline-level element into the current paragraph.
fn push_inline(&mut self, child: ParChild) {
if let Some(child) = self.par_last.any() {
self.push_inline_impl(child);
}
// The node must be both compatible with the current page and the
// current paragraph.
self.make_flow_compatible(child.styles());
self.make_par_compatible(child.styles());
self.push_inline_impl(child);
self.par_last = Last::Any;
}
/// Push a paragraph child, coalescing text nodes with compatible styles.
fn push_inline_impl(&mut self, child: ParChild) {
if let ParChild::Text(right) = &child {
if let Some(ParChild::Text(left)) = self.par.last_mut() {
if left.styles.compatible(&right.styles, TextNode::has_property) {
left.text.push_str(&right.text);
return;
}
}
}
self.par.push(child);
}
/// Insert a block-level element into the current flow.
fn push_block(&mut self, node: PackedNode) {
let mut is_placed = false;
if let Some(placed) = node.downcast::<PlacedNode>() {
is_placed = true;
// This prevents paragraph spacing after the placed node if it
// is completely out-of-flow.
if placed.out_of_flow() {
self.flow_last = Last::None;
}
}
self.parbreak(None);
self.make_flow_compatible(&node.styles);
if let Some(child) = self.flow_last.any() {
self.flow.push(child);
}
self.flow.push(FlowChild::Node(node));
self.parbreak(None);
// This prevents paragraph spacing between the placed node and
// the paragraph below it.
if is_placed {
self.flow_last = Last::None;
}
}
/// Advance to the next paragraph.
fn parbreak(&mut self, break_styles: Option<Styles>) {
self.finish_par();
// Insert paragraph spacing.
self.flow_last
.soft(FlowChild::Parbreak(break_styles.unwrap_or_default()));
}
fn finish_par(&mut self) {
let mut children = mem::take(&mut self.par);
let styles = mem::take(&mut self.par_styles);
self.par_last = Last::None;
// No empty paragraphs.
if !children.is_empty() {
// Erase any styles that will be inherited anyway.
for child in &mut children {
child.styles_mut().erase(&styles);
}
if let Some(child) = self.flow_last.any() {
self.flow.push(child);
}
// The paragraph's children are all compatible with the page, so the
// paragraph is too, meaning we don't need to check or intersect
// anything here.
let node = ParNode(children).pack().styled(styles);
self.flow.push(FlowChild::Node(node));
}
}
/// Advance to the next page.
fn pagebreak(&mut self, keep: bool) {
if self.block {
return;
}
self.finish_par();
let styles = mem::take(&mut self.flow_styles);
let mut children = mem::take(&mut self.flow);
self.flow_last = Last::None;
if keep || !children.is_empty() {
// Erase any styles that will be inherited anyway.
for child in &mut children {
child.styles_mut().erase(&styles);
}
let node = PageNode { node: FlowNode(children).pack(), styles };
self.pages.push(node);
}
}
/// Break to a new paragraph if the `styles` contain paragraph styles that
/// are incompatible with the current paragraph.
fn make_par_compatible(&mut self, styles: &Styles) {
if self.par.is_empty() {
self.par_styles = styles.clone();
return;
}
if !self.is_par_compatible(styles) {
self.parbreak(None);
self.par_styles = styles.clone();
return;
}
self.par_styles.intersect(&styles);
}
/// Break to a new page if the `styles` contain page styles that are
/// incompatible with the current flow.
fn make_flow_compatible(&mut self, styles: &Styles) {
if self.flow.is_empty() && self.par.is_empty() {
self.flow_styles = styles.clone();
return;
}
if !self.is_flow_compatible(styles) {
self.pagebreak(false);
self.flow_styles = styles.clone();
return;
}
self.flow_styles.intersect(styles);
}
/// Whether the given styles are compatible with the current page.
fn is_par_compatible(&self, styles: &Styles) -> bool {
self.par_styles.compatible(&styles, ParNode::has_property)
}
/// Whether the given styles are compatible with the current flow.
fn is_flow_compatible(&self, styles: &Styles) -> bool {
self.block || self.flow_styles.compatible(&styles, PageNode::has_property)
}
}
/// Finite state machine for spacing coalescing.
enum Last<N> {
None,
Any,
Soft(N),
}
impl<N> Last<N> {
fn any(&mut self) -> Option<N> {
match mem::replace(self, Self::Any) {
Self::Soft(soft) => Some(soft),
_ => None,
}
}
fn soft(&mut self, soft: N) {
if let Self::Any = self {
*self = Self::Soft(soft);
}
}
fn hard(&mut self) {
*self = Self::None;
}
}
|