summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 39507d79f0c89d460fdd4e4c56ef3565f3e09813 (plain) (blame)
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
//! The compiler for the _Typst_ typesetting language.
//!
//! # Steps
//! - **Parsing:** The parsing step first transforms a plain string into an
//!   [iterator of tokens][tokens]. This token stream is [parsed] into a
//!   [green tree]. The green tree itself is untyped, but a typed layer over it
//!   is provided in the [AST] module.
//! - **Evaluation:** The next step is to [evaluate] the markup. This produces a
//!   [module], consisting of a scope of values that were exported by the code
//!   and a [node] with the contents of the module. This node can be converted
//!   into a [layout tree], a hierarchical, styled representation of the
//!   document. The nodes of this tree are well structured and order-independent
//!   and thus much better suited for layouting than the raw markup.
//! - **Layouting:** Next, the tree is [layouted] into a portable version of the
//!   typeset document. The output of this is a collection of [`Frame`]s (one
//!   per page), ready for exporting. This step is supported by an incremental
//!   [cache] that enables reuse of intermediate layouting results.
//! - **Exporting:** The finished layout can be exported into a supported
//!   format. Currently, the only supported output format is [PDF].
//!
//! [tokens]: parse::Tokens
//! [parsed]: parse::parse
//! [green tree]: syntax::GreenNode
//! [AST]: syntax::ast
//! [evaluate]: Context::evaluate
//! [module]: eval::Module
//! [node]: eval::Node
//! [layout tree]: layout::RootNode
//! [layouted]: layout::RootNode::layout
//! [cache]: layout::LayoutCache
//! [PDF]: export::pdf

#![allow(clippy::len_without_is_empty)]
#![allow(clippy::or_fun_call)]
#![allow(clippy::try_err)]

#[macro_use]
pub mod util;
#[macro_use]
pub mod diag;
#[macro_use]
pub mod eval;
pub mod export;
pub mod font;
pub mod frame;
pub mod geom;
pub mod image;
pub mod layout;
pub mod library;
pub mod loading;
pub mod parse;
pub mod source;
pub mod syntax;

use std::rc::Rc;

use crate::diag::TypResult;
use crate::eval::{Eval, EvalContext, Module, Scope, StyleMap};
use crate::font::FontStore;
use crate::frame::Frame;
use crate::image::ImageStore;
#[cfg(feature = "layout-cache")]
use crate::layout::{EvictionPolicy, LayoutCache};
use crate::loading::Loader;
use crate::source::{SourceId, SourceStore};

/// The core context which holds the loader, configuration and cached artifacts.
pub struct Context {
    /// The loader the context was created with.
    pub loader: Rc<dyn Loader>,
    /// Stores loaded source files.
    pub sources: SourceStore,
    /// Stores parsed font faces.
    pub fonts: FontStore,
    /// Stores decoded images.
    pub images: ImageStore,
    /// Caches layouting artifacts.
    #[cfg(feature = "layout-cache")]
    pub layouts: LayoutCache,
    /// The standard library scope.
    std: Scope,
    /// The default styles.
    styles: StyleMap,
}

impl Context {
    /// Create a new context with the default settings.
    pub fn new(loader: Rc<dyn Loader>) -> Self {
        Self::builder().build(loader)
    }

    /// Create a new context with advanced settings.
    pub fn builder() -> ContextBuilder {
        ContextBuilder::default()
    }

    /// A read-only reference to the standard library scope.
    pub fn std(&self) -> &Scope {
        &self.std
    }

    /// A read-only reference to the styles.
    pub fn styles(&self) -> &StyleMap {
        &self.styles
    }

    /// Evaluate a source file and return the resulting module.
    ///
    /// Returns either a module containing a scope with top-level bindings and a
    /// layoutable node or diagnostics in the form of a vector of error message
    /// with file and span information.
    pub fn evaluate(&mut self, id: SourceId) -> TypResult<Module> {
        let markup = self.sources.get(id).ast()?;
        let mut ctx = EvalContext::new(self, id);
        let node = markup.eval(&mut ctx)?;
        Ok(Module { scope: ctx.scopes.top, node })
    }

    /// Typeset a source file into a collection of layouted frames.
    ///
    /// Returns either a vector of frames representing individual pages or
    /// diagnostics in the form of a vector of error message with file and span
    /// information.
    pub fn typeset(&mut self, id: SourceId) -> TypResult<Vec<Rc<Frame>>> {
        let module = self.evaluate(id)?;
        let tree = module.into_root();
        let frames = tree.layout(self);
        Ok(frames)
    }

    /// Garbage-collect caches.
    pub fn turnaround(&mut self) {
        #[cfg(feature = "layout-cache")]
        self.layouts.turnaround();
    }
}

/// A builder for a [`Context`].
///
/// This struct is created by [`Context::builder`].
pub struct ContextBuilder {
    std: Option<Scope>,
    styles: Option<StyleMap>,
    #[cfg(feature = "layout-cache")]
    policy: EvictionPolicy,
    #[cfg(feature = "layout-cache")]
    max_size: usize,
}

impl ContextBuilder {
    /// The scope containing definitions that are available everywhere
    /// (the standard library).
    pub fn std(mut self, std: Scope) -> Self {
        self.std = Some(std);
        self
    }

    /// The default properties for page size, font selection and so on.
    pub fn styles(mut self, styles: StyleMap) -> Self {
        self.styles = Some(styles);
        self
    }

    /// The policy for eviction of the layout cache.
    #[cfg(feature = "layout-cache")]
    pub fn cache_policy(mut self, policy: EvictionPolicy) -> Self {
        self.policy = policy;
        self
    }

    /// The maximum number of entries the layout cache should have.
    ///
    /// Note that this can be exceeded if more entries are categorized as [must
    /// keep][crate::layout::PatternProperties::must_keep].
    #[cfg(feature = "layout-cache")]
    pub fn cache_max_size(mut self, max_size: usize) -> Self {
        self.max_size = max_size;
        self
    }

    /// Finish building the context by providing the `loader` used to load
    /// fonts, images, source files and other resources.
    pub fn build(self, loader: Rc<dyn Loader>) -> Context {
        Context {
            sources: SourceStore::new(Rc::clone(&loader)),
            fonts: FontStore::new(Rc::clone(&loader)),
            images: ImageStore::new(Rc::clone(&loader)),
            loader,
            #[cfg(feature = "layout-cache")]
            layouts: LayoutCache::new(self.policy, self.max_size),
            std: self.std.unwrap_or_else(library::new),
            styles: self.styles.unwrap_or_default(),
        }
    }
}

impl Default for ContextBuilder {
    fn default() -> Self {
        Self {
            std: None,
            styles: None,
            #[cfg(feature = "layout-cache")]
            policy: EvictionPolicy::default(),
            #[cfg(feature = "layout-cache")]
            max_size: 2000,
        }
    }
}