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
|
use std::fmt::{self, Debug, Formatter};
use std::hash::Hash;
use std::iter::Sum;
use std::ops::{Add, AddAssign};
use std::sync::Arc;
use comemo::Tracked;
use super::{
Builder, Key, Layout, LayoutNode, Property, Regions, Scratch, Selector, Show,
ShowNode, StyleChain, StyleEntry, StyleMap,
};
use crate::diag::{SourceResult, StrResult};
use crate::frame::Frame;
use crate::geom::Abs;
use crate::library::layout::{PageNode, Spacing};
use crate::library::structure::ListItem;
use crate::util::EcoString;
use crate::World;
/// Composable representation of styled content.
///
/// This results from:
/// - anything written between square brackets in Typst
/// - any node constructor
///
/// Content is represented as a tree of nodes. There are two nodes of special
/// interest:
///
/// 1. A `Styled` node attaches a style map to other content. For example, a
/// single bold word could be represented as a `Styled(Text("Hello"),
/// [TextNode::STRONG: true])` node.
///
/// 2. A `Sequence` node content combines other arbitrary content and is the
/// representation of a "flow" of other nodes. So, when you write `[Hi] +
/// [you]` in Typst, this type's [`Add`] implementation is invoked and the
/// two [`Text`](Self::Text) nodes are combined into a single
/// [`Sequence`](Self::Sequence) node. A sequence may contain nested
/// sequences.
#[derive(PartialEq, Clone, Hash)]
pub enum Content {
/// Empty content.
Empty,
/// A word space.
Space,
/// A forced line break.
Linebreak { justify: bool },
/// Horizontal spacing.
Horizontal { amount: Spacing, weak: bool },
/// Plain text.
Text(EcoString),
/// A smart quote.
Quote { double: bool },
/// An inline-level node.
Inline(LayoutNode),
/// A paragraph break.
Parbreak,
/// A column break.
Colbreak { weak: bool },
/// Vertical spacing.
Vertical {
amount: Spacing,
weak: bool,
generated: bool,
},
/// A block-level node.
Block(LayoutNode),
/// A list / enum item.
Item(ListItem),
/// A page break.
Pagebreak { weak: bool },
/// A page node.
Page(PageNode),
/// A node that can be realized with styles, optionally with attached
/// properties.
Show(ShowNode),
/// Content with attached styles.
Styled(Arc<(Self, StyleMap)>),
/// A sequence of multiple nodes.
Sequence(Arc<Vec<Self>>),
}
impl Content {
/// Create empty content.
pub fn new() -> Self {
Self::Empty
}
/// Create content from an inline-level node.
pub fn inline<T>(node: T) -> Self
where
T: Layout + Debug + Hash + Sync + Send + 'static,
{
Self::Inline(node.pack())
}
/// Create content from a block-level node.
pub fn block<T>(node: T) -> Self
where
T: Layout + Debug + Hash + Sync + Send + 'static,
{
Self::Block(node.pack())
}
/// Create content from a showable node.
pub fn show<T>(node: T) -> Self
where
T: Show + Debug + Hash + Sync + Send + 'static,
{
Self::Show(node.pack())
}
/// Create a new sequence node from multiples nodes.
pub fn sequence(seq: Vec<Self>) -> Self {
match seq.as_slice() {
[] => Self::Empty,
[_] => seq.into_iter().next().unwrap(),
_ => Self::Sequence(Arc::new(seq)),
}
}
/// Repeat this content `n` times.
pub fn repeat(&self, n: i64) -> StrResult<Self> {
let count = usize::try_from(n)
.map_err(|_| format!("cannot repeat this content {} times", n))?;
Ok(Self::sequence(vec![self.clone(); count]))
}
/// Style this content with a single style property.
pub fn styled<'k, K: Key<'k>>(self, key: K, value: K::Value) -> Self {
self.styled_with_entry(StyleEntry::Property(Property::new(key, value)))
}
/// Style this content with a style entry.
pub fn styled_with_entry(mut self, entry: StyleEntry) -> Self {
if let Self::Styled(styled) = &mut self {
if let Some((_, map)) = Arc::get_mut(styled) {
map.apply(entry);
return self;
}
}
Self::Styled(Arc::new((self, entry.into())))
}
/// Style this content with a full style map.
pub fn styled_with_map(mut self, styles: StyleMap) -> Self {
if styles.is_empty() {
return self;
}
if let Self::Styled(styled) = &mut self {
if let Some((_, map)) = Arc::get_mut(styled) {
map.apply_map(&styles);
return self;
}
}
Self::Styled(Arc::new((self, styles)))
}
/// Reenable the show rule identified by the selector.
pub fn unguard(&self, sel: Selector) -> Self {
self.clone().styled_with_entry(StyleEntry::Unguard(sel))
}
/// Add weak vertical spacing above and below the node.
pub fn spaced(self, above: Option<Abs>, below: Option<Abs>) -> Self {
if above.is_none() && below.is_none() {
return self;
}
let mut seq = vec![];
if let Some(above) = above {
seq.push(Content::Vertical {
amount: above.into(),
weak: true,
generated: true,
});
}
seq.push(self);
if let Some(below) = below {
seq.push(Content::Vertical {
amount: below.into(),
weak: true,
generated: true,
});
}
Self::sequence(seq)
}
}
impl Layout for Content {
fn layout(
&self,
world: Tracked<dyn World>,
regions: &Regions,
styles: StyleChain,
) -> SourceResult<Vec<Frame>> {
let scratch = Scratch::default();
let mut builder = Builder::new(world, &scratch, false);
builder.accept(self, styles)?;
let (flow, shared) = builder.into_flow(styles)?;
flow.layout(world, regions, shared)
}
fn pack(self) -> LayoutNode {
match self {
Content::Block(node) => node,
other => LayoutNode::new(other),
}
}
}
impl Default for Content {
fn default() -> Self {
Self::new()
}
}
impl Debug for Content {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::Empty => f.pad("Empty"),
Self::Space => f.pad("Space"),
Self::Linebreak { justify } => write!(f, "Linebreak({justify})"),
Self::Horizontal { amount, weak } => {
write!(f, "Horizontal({amount:?}, {weak})")
}
Self::Text(text) => write!(f, "Text({text:?})"),
Self::Quote { double } => write!(f, "Quote({double})"),
Self::Inline(node) => node.fmt(f),
Self::Parbreak => f.pad("Parbreak"),
Self::Colbreak { weak } => write!(f, "Colbreak({weak})"),
Self::Vertical { amount, weak, generated } => {
write!(f, "Vertical({amount:?}, {weak}, {generated})")
}
Self::Block(node) => node.fmt(f),
Self::Item(item) => item.fmt(f),
Self::Pagebreak { weak } => write!(f, "Pagebreak({weak})"),
Self::Page(page) => page.fmt(f),
Self::Show(node) => node.fmt(f),
Self::Styled(styled) => {
let (sub, map) = styled.as_ref();
map.fmt(f)?;
sub.fmt(f)
}
Self::Sequence(seq) => f.debug_list().entries(seq.iter()).finish(),
}
}
}
impl Add for Content {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::Sequence(match (self, rhs) {
(Self::Empty, rhs) => return rhs,
(lhs, Self::Empty) => return lhs,
(Self::Sequence(mut lhs), Self::Sequence(rhs)) => {
let mutable = Arc::make_mut(&mut lhs);
match Arc::try_unwrap(rhs) {
Ok(vec) => mutable.extend(vec),
Err(rc) => mutable.extend(rc.iter().cloned()),
}
lhs
}
(Self::Sequence(mut lhs), rhs) => {
Arc::make_mut(&mut lhs).push(rhs);
lhs
}
(lhs, Self::Sequence(mut rhs)) => {
Arc::make_mut(&mut rhs).insert(0, lhs);
rhs
}
(lhs, rhs) => Arc::new(vec![lhs, rhs]),
})
}
}
impl AddAssign for Content {
fn add_assign(&mut self, rhs: Self) {
*self = std::mem::take(self) + rhs;
}
}
impl Sum for Content {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
Self::sequence(iter.collect())
}
}
|