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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
use std::any::TypeId;
use std::fmt::{self, Debug, Formatter, Write};
use std::hash::{Hash, Hasher};
use std::iter::{self, Sum};
use std::ops::{Add, AddAssign, Deref};
use comemo::Tracked;
use ecow::{eco_format, EcoString, EcoVec};
use once_cell::sync::Lazy;
use super::{node, Guard, Recipe, Style, StyleMap};
use crate::diag::{SourceResult, StrResult};
use crate::eval::{cast_from_value, Args, FuncInfo, Str, Value, Vm};
use crate::syntax::Span;
use crate::util::pretty_array_like;
use crate::World;
/// Composable representation of styled content.
#[derive(Clone, Hash)]
pub struct Content {
id: NodeId,
span: Span,
fields: EcoVec<(EcoString, Value)>,
modifiers: EcoVec<Modifier>,
}
/// Modifiers that can be attached to content.
#[derive(Debug, Clone, PartialEq, Hash)]
enum Modifier {
Prepared,
Guard(Guard),
}
impl Content {
pub fn new<T: Node>() -> Self {
Self {
id: T::id(),
span: Span::detached(),
fields: EcoVec::new(),
modifiers: EcoVec::new(),
}
}
/// Create empty content.
pub fn empty() -> Self {
SequenceNode::new(vec![]).pack()
}
/// Create a new sequence node from multiples nodes.
pub fn sequence(seq: Vec<Self>) -> Self {
match seq.as_slice() {
[_] => seq.into_iter().next().unwrap(),
_ => SequenceNode::new(seq).pack(),
}
}
/// The id of the contained node.
pub fn id(&self) -> NodeId {
self.id
}
/// Whether the contained node is of type `T`.
pub fn is<T>(&self) -> bool
where
T: Node + 'static,
{
self.id == NodeId::of::<T>()
}
/// Cast to `T` if the contained node is of type `T`.
pub fn to<T>(&self) -> Option<&T>
where
T: Node + 'static,
{
self.is::<T>().then(|| unsafe { std::mem::transmute(self) })
}
/// Whether this content has the given capability.
pub fn can<C>(&self) -> bool
where
C: ?Sized + 'static,
{
(self.id.0.vtable)(TypeId::of::<C>()).is_some()
}
/// Cast to a trait object if this content has the given capability.
pub fn with<C>(&self) -> Option<&C>
where
C: ?Sized + 'static,
{
let vtable = (self.id.0.vtable)(TypeId::of::<C>())?;
let data = self as *const Self as *const ();
Some(unsafe { &*crate::util::fat::from_raw_parts(data, vtable) })
}
/// The node's span.
pub fn span(&self) -> Span {
self.span
}
/// Attach a span to the content.
pub fn spanned(mut self, span: Span) -> Self {
self.span = span;
self
}
/// Access a field on the content.
pub fn field(&self, name: &str) -> Option<&Value> {
self.fields
.iter()
.find(|(field, _)| field == name)
.map(|(_, value)| value)
}
/// List all fields on the content.
pub fn fields(&self) -> &[(EcoString, Value)] {
&self.fields
}
/// Attach a field to the content.
pub fn with_field(
mut self,
name: impl Into<EcoString>,
value: impl Into<Value>,
) -> Self {
self.push_field(name, value);
self
}
/// Attach a field to the content.
pub fn push_field(&mut self, name: impl Into<EcoString>, value: impl Into<Value>) {
let name = name.into();
if let Some(i) = self.fields.iter().position(|(field, _)| *field == name) {
self.fields.make_mut()[i] = (name, value.into());
} else {
self.fields.push((name, value.into()));
}
}
/// Whether the content has the specified field.
pub fn has(&self, field: &str) -> bool {
self.field(field).is_some()
}
/// Borrow the value of the given field.
pub fn at(&self, field: &str) -> StrResult<&Value> {
self.field(field).ok_or_else(|| missing_field(field))
}
/// The content's label.
pub fn label(&self) -> Option<&Label> {
match self.field("label")? {
Value::Label(label) => Some(label),
_ => None,
}
}
/// Attach a label to the content.
pub fn labelled(self, label: Label) -> Self {
self.with_field("label", label)
}
/// Style this content with a style entry.
pub fn styled(self, style: impl Into<Style>) -> Self {
self.styled_with_map(style.into().into())
}
/// Style this content with a full style map.
pub fn styled_with_map(self, styles: StyleMap) -> Self {
if styles.is_empty() {
self
} else if let Some(styled) = self.to::<StyledNode>() {
let mut map = styled.styles();
map.apply(styles);
StyledNode::new(map, styled.body()).pack()
} else {
StyledNode::new(styles, self).pack()
}
}
/// Style this content with a recipe, eagerly applying it if possible.
pub fn styled_with_recipe(
self,
world: Tracked<dyn World>,
recipe: Recipe,
) -> SourceResult<Self> {
if recipe.selector.is_none() {
recipe.apply(world, self)
} else {
Ok(self.styled(Style::Recipe(recipe)))
}
}
/// 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]))
}
}
#[doc(hidden)]
impl Content {
/// Disable a show rule recipe.
pub fn guarded(mut self, id: Guard) -> Self {
self.modifiers.push(Modifier::Guard(id));
self
}
/// Mark this content as prepared.
pub fn prepared(mut self) -> Self {
self.modifiers.push(Modifier::Prepared);
self
}
/// Whether this node was prepared.
pub fn is_prepared(&self) -> bool {
self.modifiers.contains(&Modifier::Prepared)
}
/// Whether no show rule was executed for this node so far.
pub(super) fn is_pristine(&self) -> bool {
!self
.modifiers
.iter()
.any(|modifier| matches!(modifier, Modifier::Guard(_)))
}
/// Check whether a show rule recipe is disabled.
pub(super) fn is_guarded(&self, id: Guard) -> bool {
self.modifiers.contains(&Modifier::Guard(id))
}
/// Copy the modifiers from another piece of content.
pub(super) fn copy_modifiers(&mut self, from: &Content) {
self.span = from.span;
self.modifiers = from.modifiers.clone();
if let Some(label) = from.label() {
self.push_field("label", label.clone())
}
}
}
impl Debug for Content {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let name = self.id.name;
if let Some(text) = item!(text_str)(self) {
f.write_char('[')?;
f.write_str(&text)?;
f.write_char(']')?;
return Ok(());
} else if name == "space" {
return f.write_str("[ ]");
}
let pieces: Vec<_> = self
.fields
.iter()
.map(|(name, value)| eco_format!("{name}: {value:?}"))
.collect();
f.write_str(name)?;
f.write_str(&pretty_array_like(&pieces, false))
}
}
impl Default for Content {
fn default() -> Self {
Self::empty()
}
}
impl PartialEq for Content {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
&& self.fields.len() == other.fields.len()
&& self
.fields
.iter()
.all(|(name, value)| other.field(name) == Some(value))
}
}
impl Add for Content {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
let lhs = self;
let seq = match (lhs.to::<SequenceNode>(), rhs.to::<SequenceNode>()) {
(Some(lhs), Some(rhs)) => {
lhs.children().into_iter().chain(rhs.children()).collect()
}
(Some(lhs), None) => {
lhs.children().into_iter().chain(iter::once(rhs)).collect()
}
(None, Some(rhs)) => iter::once(lhs).chain(rhs.children()).collect(),
(None, None) => vec![lhs, rhs],
};
SequenceNode::new(seq).pack()
}
}
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())
}
}
/// A constructable, stylable content node.
pub trait Node: Construct + Set + Sized + 'static {
/// The node's ID.
fn id() -> NodeId;
/// Pack a node into type-erased content.
fn pack(self) -> Content;
}
/// A unique identifier for a node.
#[derive(Copy, Clone)]
pub struct NodeId(pub &'static NodeMeta);
impl NodeId {
/// Get the id of a node.
pub fn of<T: Node>() -> Self {
T::id()
}
}
impl Debug for NodeId {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.pad(self.name)
}
}
impl Hash for NodeId {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_usize(self.0 as *const _ as usize);
}
}
impl Eq for NodeId {}
impl PartialEq for NodeId {
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(self.0, other.0)
}
}
impl Deref for NodeId {
type Target = NodeMeta;
fn deref(&self) -> &Self::Target {
self.0
}
}
/// Static node for a node.
pub struct NodeMeta {
/// The node's name.
pub name: &'static str,
/// The node's vtable for caspability dispatch.
pub vtable: fn(of: TypeId) -> Option<*const ()>,
/// The node's constructor.
pub construct: fn(&Vm, &mut Args) -> SourceResult<Content>,
/// The node's set rule.
pub set: fn(&mut Args) -> SourceResult<StyleMap>,
/// Details about the function.
pub info: Lazy<FuncInfo>,
}
/// A node's constructor function.
pub trait Construct {
/// Construct a node from the arguments.
///
/// This is passed only the arguments that remain after execution of the
/// node's set rule.
fn construct(vm: &Vm, args: &mut Args) -> SourceResult<Content>;
}
/// A node's set rule.
pub trait Set {
/// Parse relevant arguments into style properties for this node.
fn set(args: &mut Args) -> SourceResult<StyleMap>;
}
/// Indicates that a node cannot be labelled.
pub trait Unlabellable {}
/// A label for a node.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Label(pub EcoString);
impl Debug for Label {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "<{}>", self.0)
}
}
/// A sequence of nodes.
///
/// Combines other arbitrary content. So, when you write `[Hi] + [you]` in
/// Typst, the two text nodes are combined into a single sequence node.
///
/// Display: Sequence
/// Category: special
#[node]
pub struct SequenceNode {
#[variadic]
pub children: Vec<Content>,
}
/// A node with applied styles.
///
/// Display: Styled
/// Category: special
#[node]
pub struct StyledNode {
/// The styles.
#[required]
pub styles: StyleMap,
/// The styled content.
#[required]
pub body: Content,
}
cast_from_value! {
StyleMap: "style map",
}
/// The missing key access error message.
#[cold]
#[track_caller]
fn missing_field(key: &str) -> EcoString {
eco_format!("content does not contain field {:?}", Str::from(key))
}
|