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
|
use std::convert::TryFrom;
use std::fmt::{self, Debug, Formatter};
use std::hash::Hash;
use std::mem;
use std::ops::{Add, AddAssign};
use crate::diag::StrResult;
use crate::geom::SpecAxis;
use crate::layout::{Layout, PackedNode};
use crate::library::{
Decoration, DocumentNode, FlowChild, FlowNode, PageNode, ParChild, ParNode, Spacing,
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 the block or page level.
#[derive(Clone)]
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, Spacing),
/// An inline node.
Inline(PackedNode),
/// A block node.
Block(PackedNode),
/// A sequence of nodes (which may themselves contain sequences).
Seq(Vec<Self>),
}
impl Node {
/// Create an empty node.
pub fn new() -> Self {
Self::Seq(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())
}
/// Decoration this node.
pub fn decorate(self, _: Decoration) -> Self {
// TODO(set): Actually decorate.
self
}
/// 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();
packer.walk(self);
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();
packer.walk(self);
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::Seq(vec![self.clone(); count]))
}
}
impl Debug for Node {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.pad("<node>")
}
}
impl Default for Node {
fn default() -> Self {
Self::new()
}
}
impl PartialEq for Node {
fn eq(&self, _: &Self) -> bool {
// TODO(set): Figure out what to do here.
false
}
}
impl Add for Node {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
// TODO(set): Make more efficient.
Self::Seq(vec![self, rhs])
}
}
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 {
document: Vec<PageNode>,
flow: Vec<FlowChild>,
par: Vec<ParChild>,
}
impl NodePacker {
fn new() -> Self {
Self {
document: vec![],
flow: vec![],
par: vec![],
}
}
fn into_block(mut self) -> PackedNode {
self.parbreak();
FlowNode(self.flow).pack()
}
fn into_document(mut self) -> DocumentNode {
self.parbreak();
self.pagebreak();
DocumentNode(self.document)
}
fn walk(&mut self, node: Node) {
match node {
Node::Space => {
self.push_inline(ParChild::Text(TextNode(' '.into())));
}
Node::Linebreak => {
self.push_inline(ParChild::Text(TextNode('\n'.into())));
}
Node::Parbreak => {
self.parbreak();
}
Node::Pagebreak => {
self.pagebreak();
}
Node::Text(text) => {
self.push_inline(ParChild::Text(TextNode(text)));
}
Node::Spacing(axis, amount) => match axis {
SpecAxis::Horizontal => self.push_inline(ParChild::Spacing(amount)),
SpecAxis::Vertical => self.push_block(FlowChild::Spacing(amount)),
},
Node::Inline(inline) => {
self.push_inline(ParChild::Node(inline));
}
Node::Block(block) => {
self.push_block(FlowChild::Node(block));
}
Node::Seq(list) => {
for node in list {
self.walk(node);
}
}
}
}
fn parbreak(&mut self) {
let children = mem::take(&mut self.par);
if !children.is_empty() {
self.flow.push(FlowChild::Node(ParNode(children).pack()));
}
}
fn pagebreak(&mut self) {
let children = mem::take(&mut self.flow);
self.document.push(PageNode(FlowNode(children).pack()));
}
fn push_inline(&mut self, child: ParChild) {
self.par.push(child);
}
fn push_block(&mut self, child: FlowChild) {
self.parbreak();
self.flow.push(child);
}
}
|