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
|
//! The document model.
mod content;
mod element;
mod introspect;
mod label;
mod realize;
mod selector;
mod styles;
#[doc(inline)]
pub use typst_macros::element;
pub use self::content::{Content, MetaElem, PlainText};
pub use self::element::{Construct, ElemFunc, Element, NativeElemFunc, Set};
pub use self::introspect::{Introspector, Location, Locator};
pub use self::label::{Label, Unlabellable};
pub use self::realize::{
applicable, realize, Behave, Behaviour, Finalize, Guard, Locatable, Show, Synthesize,
};
pub use self::selector::{LocatableSelector, Selector, ShowableSelector};
pub use self::styles::{
Fold, Property, Recipe, Resolve, Style, StyleChain, StyleVec, StyleVecBuilder,
Styles, Transform,
};
use std::mem::ManuallyDrop;
use comemo::{Track, Tracked, TrackedMut, Validate};
use crate::diag::{SourceError, SourceResult};
use crate::doc::Document;
use crate::eval::Tracer;
use crate::World;
/// Typeset content into a fully layouted document.
#[comemo::memoize]
#[tracing::instrument(skip(world, tracer, content))]
pub fn typeset(
world: Tracked<dyn World + '_>,
mut tracer: TrackedMut<Tracer>,
content: &Content,
) -> SourceResult<Document> {
tracing::info!("Starting typesetting");
let library = world.library();
let styles = StyleChain::new(&library.styles);
let mut iter = 0;
let mut document;
let mut delayed;
// We need `ManuallyDrop` until this lands in stable:
// https://github.com/rust-lang/rust/issues/70919
let mut introspector = ManuallyDrop::new(Introspector::new(&[]));
// Relayout until all introspections stabilize.
// If that doesn't happen within five attempts, we give up.
loop {
tracing::info!("Layout iteration {iter}");
delayed = DelayedErrors::default();
let constraint = <Introspector as Validate>::Constraint::new();
let mut locator = Locator::new();
let mut vt = Vt {
world,
tracer: TrackedMut::reborrow_mut(&mut tracer),
locator: &mut locator,
introspector: introspector.track_with(&constraint),
delayed: delayed.track_mut(),
};
// Layout!
let result = (library.items.layout)(&mut vt, content, styles)?;
// Drop the old introspector.
ManuallyDrop::into_inner(introspector);
// Only now assign the document and construct the new introspector.
document = result;
introspector = ManuallyDrop::new(Introspector::new(&document.pages));
iter += 1;
if iter >= 5 || introspector.validate(&constraint) {
break;
}
}
// Drop the introspector.
ManuallyDrop::into_inner(introspector);
// Promote delayed errors.
if !delayed.0.is_empty() {
return Err(Box::new(delayed.0));
}
Ok(document)
}
/// A virtual typesetter.
///
/// Holds the state needed to [typeset] content.
pub struct Vt<'a> {
/// The compilation environment.
pub world: Tracked<'a, dyn World + 'a>,
/// Provides access to information about the document.
pub introspector: Tracked<'a, Introspector>,
/// Provides stable identities to elements.
pub locator: &'a mut Locator<'a>,
/// Delayed errors that do not immediately terminate execution.
pub delayed: TrackedMut<'a, DelayedErrors>,
/// The tracer for inspection of the values an expression produces.
pub tracer: TrackedMut<'a, Tracer>,
}
impl Vt<'_> {
/// Perform a fallible operation that does not immediately terminate further
/// execution. Instead it produces a delayed error that is only promoted to
/// a fatal one if it remains at the end of the introspection loop.
pub fn delayed<F, T>(&mut self, f: F) -> T
where
F: FnOnce(&mut Self) -> SourceResult<T>,
T: Default,
{
match f(self) {
Ok(value) => value,
Err(errors) => {
for error in *errors {
self.delayed.push(error);
}
T::default()
}
}
}
}
/// Holds delayed errors.
#[derive(Default, Clone)]
pub struct DelayedErrors(Vec<SourceError>);
#[comemo::track]
impl DelayedErrors {
/// Push a delayed error.
fn push(&mut self, error: SourceError) {
self.0.push(error);
}
}
|