typst-template 0.1.1

Generate PDFs with Typst straight from your Rust types: derive Typst inputs from structs and render templates, on a reusable world for cheap repeated generation.
Documentation
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//! The Typst [`World`] split into a reusable base and per-generation concrete
//! worlds.

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::{fmt, fs};

use typst::diag::{FileError, FileResult, SourceResult, Warned};
use typst::foundations::{Bytes, Datetime, Dict, Str};
use typst::layout::PagedDocument;
use typst::syntax::{FileId, Source, VirtualPath};
use typst::text::{Font, FontBook};
use typst::utils::LazyHash;
use typst::{Library, LibraryExt, World};
use typst_kit::fonts::{FontSearcher, FontSlot, Fonts};
use typst_pdf::{PdfOptions, pdf};

use crate::value::{ToDict, ToValue};

/// A long-lived, cheaply cloneable Typst world.
///
/// Holds the project root and the loaded font set. Build a `WorldBase` once and
/// derive a [`ConcreteWorld`] from it for each document via
/// [`concrete`](Self::concrete). Cloning shares the fonts through an [`Arc`].
#[derive(Clone)]
#[must_use]
pub struct WorldBase {
    inner: Arc<WorldBaseInner>,
}

struct WorldBaseInner {
    root: PathBuf,
    book: LazyHash<FontBook>,
    fonts: Vec<FontSlot>,
}

impl fmt::Debug for WorldBase {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("WorldBase")
            .field("root", &self.inner.root)
            .finish_non_exhaustive()
    }
}

/// Builder for a [`WorldBase`].
///
/// By default both system and embedded fonts are searched; add extra font
/// directories with [`font_path`](Self::font_path).
#[derive(Debug, Clone)]
#[must_use]
pub struct WorldBaseConfig {
    root: PathBuf,
    font_paths: Vec<PathBuf>,
    system_fonts: bool,
    embedded_fonts: bool,
}

impl WorldBaseConfig {
    /// Starts a config rooted at `root`. Real files are resolved relative to it.
    pub fn new(root: impl Into<PathBuf>) -> Self {
        Self { root: root.into(), font_paths: Vec::new(), system_fonts: true, embedded_fonts: true }
    }

    /// Adds a directory to search for fonts (searched recursively).
    pub fn font_path(mut self, path: impl Into<PathBuf>) -> Self {
        self.font_paths.push(path.into());
        self
    }

    /// Adds several font directories.
    pub fn font_paths<P: Into<PathBuf>>(mut self, paths: impl IntoIterator<Item = P>) -> Self {
        self.font_paths.extend(paths.into_iter().map(Into::into));
        self
    }

    /// Whether to include fonts installed on the system (default `true`).
    pub fn system_fonts(mut self, include: bool) -> Self {
        self.system_fonts = include;
        self
    }

    /// Whether to include the fonts embedded in `typst-assets` (default `true`).
    pub fn embedded_fonts(mut self, include: bool) -> Self {
        self.embedded_fonts = include;
        self
    }

    /// Builds the base, searching for fonts now.
    pub fn build(self) -> WorldBase {
        let mut searcher = FontSearcher::new();
        searcher.include_system_fonts(self.system_fonts);
        searcher.include_embedded_fonts(self.embedded_fonts);
        let Fonts { book, fonts } = searcher.search_with(&self.font_paths);
        let inner = WorldBaseInner { root: self.root, book: LazyHash::new(book), fonts };
        WorldBase { inner: Arc::new(inner) }
    }
}

impl WorldBase {
    /// Builds a base rooted at `root` with the default font configuration.
    ///
    /// Use [`WorldBaseConfig`] for finer control.
    pub fn new(root: impl Into<PathBuf>) -> Self {
        WorldBaseConfig::new(root).build()
    }

    /// Starts building a [`ConcreteWorld`] whose main file is `main` (a path
    /// relative to the root, or a virtual path added with
    /// [`file`](ConcreteWorldBuilder::file)).
    pub fn concrete(&self, main: impl AsRef<Path>) -> ConcreteWorldBuilder {
        ConcreteWorldBuilder {
            base: self.clone(),
            main: file_id(main),
            inputs: Dict::new(),
            files: HashMap::new(),
            today: Today::default(),
        }
    }
}

impl WorldBaseInner {
    fn load_file(&self, id: FileId) -> FileResult<Vec<u8>> {
        let path = id
            .vpath()
            .resolve(&self.root)
            .ok_or(FileError::AccessDenied)?;
        // TODO: Cache reads in the base and re-validate against mtime on the
        // next request. See TODO.md ("Disk file read cache").
        fs::read(&path).map_err(|err| match err.kind() {
            std::io::ErrorKind::NotFound => FileError::NotFound(path),
            std::io::ErrorKind::PermissionDenied => FileError::AccessDenied,
            std::io::ErrorKind::IsADirectory => FileError::IsDirectory,
            _ => FileError::Other(Some(format!("{err}").into())),
        })
    }
}

/// Builder for a [`ConcreteWorld`].
#[derive(Clone)]
#[must_use]
pub struct ConcreteWorldBuilder {
    base: WorldBase,
    main: FileId,
    inputs: Dict,
    files: HashMap<FileId, Bytes>,
    today: Today,
}

impl fmt::Debug for ConcreteWorldBuilder {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ConcreteWorldBuilder")
            .field("main", &self.main)
            .field("inputs", &self.inputs)
            .finish_non_exhaustive()
    }
}

impl ConcreteWorldBuilder {
    /// Sets the whole `sys.inputs` dictionary, replacing any earlier inputs.
    pub fn inputs(mut self, inputs: impl ToDict) -> Self {
        self.inputs = inputs.into_dict();
        self
    }

    /// Adds a single `sys.inputs` entry.
    pub fn input(mut self, key: impl Into<Str>, value: impl ToValue) -> Self {
        self.inputs.insert(key.into(), value.into_value());
        self
    }

    /// Adds a virtual in-memory file at `path`, visible to the document instead
    /// of any real file at that path.
    pub fn file(mut self, path: impl AsRef<Path>, content: impl Into<Vec<u8>>) -> Self {
        self.files.insert(file_id(path), Bytes::new(content.into()));
        self
    }

    /// Pins `datetime.today()` to a fixed value.
    ///
    /// Answers only offset-less (`auto`) requests; `datetime.today(offset: N)`
    /// returns nothing.
    pub fn today(mut self, today: Datetime) -> Self {
        self.today = Today::Fixed(today);
        self
    }

    /// Makes `datetime.today()` read the system clock, treating "local" as the
    /// operating system's time zone.
    ///
    /// With the `time` backend, "local" falls back to UTC when the OS offset is
    /// unavailable (e.g. in a multithreaded process); use
    /// [`today_system_in`](Self::today_system_in) for a specific zone.
    #[cfg(any(feature = "chrono", feature = "time"))]
    #[cfg_attr(docsrs, doc(cfg(any(feature = "chrono", feature = "time"))))]
    pub fn today_system(mut self) -> Self {
        self.today = Today::SystemLocal;
        self
    }

    /// Makes `datetime.today()` read the system clock, treating "local" as the
    /// given named time zone (DST-aware).
    ///
    /// Accepts a [`chrono_tz::Tz`](crate::chrono_tz::Tz) (feature `chrono-tz`)
    /// or a `&'static` [`time_tz::Tz`](crate::time_tz::Tz) (feature `time-tz`)
    /// via [`Into<Zone>`](Zone).
    #[cfg(any(feature = "chrono-tz", feature = "time-tz"))]
    #[cfg_attr(docsrs, doc(cfg(any(feature = "chrono-tz", feature = "time-tz"))))]
    pub fn today_system_in(mut self, zone: impl Into<Zone>) -> Self {
        self.today = Today::SystemZone(zone.into());
        self
    }

    /// Makes `datetime.today()` return nothing.
    pub fn today_disabled(mut self) -> Self {
        self.today = Today::Disabled;
        self
    }

    /// Builds the concrete world.
    pub fn build(self) -> ConcreteWorld {
        let library = Library::builder().with_inputs(self.inputs).build();
        ConcreteWorld {
            base: self.base.inner,
            library: LazyHash::new(library),
            files: self.files,
            main: self.main,
            today: self.today,
        }
    }
}

/// A single-document world: a [`WorldBase`] plus this generation's inputs and
/// virtual files.
///
/// Build one via [`WorldBase::concrete`], then call
/// [`render_pdf`](Self::render_pdf).
#[must_use]
pub struct ConcreteWorld {
    base: Arc<WorldBaseInner>,
    library: LazyHash<Library>,
    files: HashMap<FileId, Bytes>,
    main: FileId,
    today: Today,
}

impl fmt::Debug for ConcreteWorld {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ConcreteWorld")
            .field("main", &self.main)
            .finish_non_exhaustive()
    }
}

impl ConcreteWorld {
    /// Adds (or replaces) a virtual in-memory file after construction.
    pub fn add_file(&mut self, path: impl AsRef<Path>, content: impl Into<Vec<u8>>) {
        self.files.insert(file_id(path), Bytes::new(content.into()));
    }

    /// Compiles the document, returning the paged document (or errors) together
    /// with any warnings.
    pub fn compile(&self) -> Warned<SourceResult<PagedDocument>> {
        typst::compile(self)
    }

    /// Compiles and exports to PDF, returning the bytes (or errors) together
    /// with any compile warnings.
    pub fn render_pdf(&self, options: &PdfOptions) -> Warned<SourceResult<Vec<u8>>> {
        let Warned { output, warnings } = self.compile();
        let output = output.and_then(|document| pdf(&document, options));
        Warned { output, warnings }
    }

    /// Like [`render_pdf`](Self::render_pdf) with [`PdfOptions::default`].
    pub fn render_pdf_default(&self) -> Warned<SourceResult<Vec<u8>>> {
        self.render_pdf(&PdfOptions::default())
    }

    fn read(&self, id: FileId) -> FileResult<Bytes> {
        match self.files.get(&id) {
            Some(bytes) => Ok(bytes.clone()),
            None => Ok(Bytes::new(self.base.load_file(id)?)),
        }
    }
}

impl World for ConcreteWorld {
    fn library(&self) -> &LazyHash<Library> {
        &self.library
    }

    fn book(&self) -> &LazyHash<FontBook> {
        &self.base.book
    }

    fn main(&self) -> FileId {
        self.main
    }

    fn source(&self, id: FileId) -> FileResult<Source> {
        let bytes = self.read(id)?;
        let text = std::str::from_utf8(&bytes).map_err(|_| FileError::InvalidUtf8)?;
        Ok(Source::new(id, text.to_owned()))
    }

    fn file(&self, id: FileId) -> FileResult<Bytes> {
        self.read(id)
    }

    fn font(&self, index: usize) -> Option<Font> {
        self.base.fonts.get(index).and_then(FontSlot::get)
    }

    fn today(&self, offset: Option<i64>) -> Option<Datetime> {
        self.today.resolve(offset)
    }
}

/// How the world answers `datetime.today()`.
#[derive(Clone)]
enum Today {
    /// `datetime.today()` returns nothing.
    Disabled,
    /// A fixed value; answers only offset-less requests.
    Fixed(Datetime),
    /// The system clock, with "local" meaning the operating system's time zone.
    #[cfg(any(feature = "chrono", feature = "time"))]
    SystemLocal,
    /// The system clock, with "local" meaning a named time zone.
    #[cfg(any(feature = "chrono-tz", feature = "time-tz"))]
    SystemZone(Zone),
}

/// Defaults to the system clock when a `chrono`/`time` feature is enabled,
/// otherwise leaves `datetime.today()` disabled.
impl Default for Today {
    fn default() -> Self {
        #[cfg(any(feature = "chrono", feature = "time"))]
        {
            Today::SystemLocal
        }
        #[cfg(not(any(feature = "chrono", feature = "time")))]
        {
            Today::Disabled
        }
    }
}

impl Today {
    fn resolve(&self, offset: Option<i64>) -> Option<Datetime> {
        match self {
            Today::Disabled => None,
            // A fixed value answers only offset-less requests.
            Today::Fixed(today) => match offset {
                None => Some(*today),
                Some(_) => None,
            },
            #[cfg(any(feature = "chrono", feature = "time"))]
            Today::SystemLocal => match offset {
                Some(hours) => offset_now(hours),
                None => local_now(),
            },
            #[cfg(any(feature = "chrono-tz", feature = "time-tz"))]
            // An explicit offset overrides the configured zone.
            Today::SystemZone(zone) => match offset {
                Some(hours) => offset_now(hours),
                None => zone.now(),
            },
        }
    }
}

/// A named time zone for [`today_system_in`](ConcreteWorldBuilder::today_system_in).
///
/// Build one with `.into()` from a [`chrono_tz::Tz`](crate::chrono_tz::Tz)
/// (feature `chrono-tz`) or a `&'static` [`time_tz::Tz`](crate::time_tz::Tz)
/// (feature `time-tz`). Both features may be enabled at once.
#[cfg(any(feature = "chrono-tz", feature = "time-tz"))]
#[derive(Clone)]
pub struct Zone(ZoneRepr);

#[cfg(any(feature = "chrono-tz", feature = "time-tz"))]
impl fmt::Debug for Zone {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Zone").finish_non_exhaustive()
    }
}

#[cfg(any(feature = "chrono-tz", feature = "time-tz"))]
#[derive(Clone)]
enum ZoneRepr {
    #[cfg(feature = "chrono-tz")]
    Chrono(chrono_tz::Tz),
    #[cfg(feature = "time-tz")]
    Time(&'static time_tz::Tz),
}

#[cfg(feature = "chrono-tz")]
impl From<chrono_tz::Tz> for Zone {
    fn from(tz: chrono_tz::Tz) -> Self {
        Zone(ZoneRepr::Chrono(tz))
    }
}

#[cfg(feature = "time-tz")]
impl From<&'static time_tz::Tz> for Zone {
    fn from(tz: &'static time_tz::Tz) -> Self {
        Zone(ZoneRepr::Time(tz))
    }
}

#[cfg(any(feature = "chrono-tz", feature = "time-tz"))]
impl Zone {
    fn now(&self) -> Option<Datetime> {
        match &self.0 {
            #[cfg(feature = "chrono-tz")]
            ZoneRepr::Chrono(tz) => {
                use chrono::{Datelike, Timelike, Utc};
                let now = Utc::now().with_timezone(tz);
                crate::convert::datetime(
                    now.year(),
                    now.month() as u8,
                    now.day() as u8,
                    now.hour() as u8,
                    now.minute() as u8,
                    now.second() as u8,
                )
            }
            #[cfg(feature = "time-tz")]
            ZoneRepr::Time(tz) => {
                use time::OffsetDateTime;
                use time_tz::OffsetDateTimeExt;
                let now = OffsetDateTime::now_utc().to_timezone(*tz);
                crate::convert::datetime(
                    now.year(),
                    u8::from(now.month()),
                    now.day(),
                    now.hour(),
                    now.minute(),
                    now.second(),
                )
            }
        }
    }
}

/// The current time in the OS-local time zone, via `chrono`.
#[cfg(feature = "chrono")]
fn local_now() -> Option<Datetime> {
    use chrono::{Datelike, Local, Timelike};
    let now = Local::now().naive_local();
    crate::convert::datetime(
        now.year(),
        now.month() as u8,
        now.day() as u8,
        now.hour() as u8,
        now.minute() as u8,
        now.second() as u8,
    )
}

/// The current time via `time`. Falls back to UTC when the OS-local offset is
/// unavailable (e.g. in a multithreaded process); use
/// [`today_system_in`](ConcreteWorldBuilder::today_system_in) for a specific
/// zone.
#[cfg(all(feature = "time", not(feature = "chrono")))]
fn local_now() -> Option<Datetime> {
    use time::OffsetDateTime;
    let now = OffsetDateTime::now_local().unwrap_or_else(|_| OffsetDateTime::now_utc());
    crate::convert::datetime(now.year(), u8::from(now.month()), now.day(), now.hour(), now.minute(), now.second())
}

/// The current time at a fixed UTC offset (in hours). Uses `chrono` if enabled,
/// otherwise `time`. Returns `None` if the offset is too large to apply.
#[cfg(feature = "chrono")]
fn offset_now(hours: i64) -> Option<Datetime> {
    use chrono::{Datelike, Duration, Timelike, Utc};
    let now = Utc::now()
        .checked_add_signed(Duration::try_hours(hours)?)?
        .naive_utc();
    crate::convert::datetime(
        now.year(),
        now.month() as u8,
        now.day() as u8,
        now.hour() as u8,
        now.minute() as u8,
        now.second() as u8,
    )
}

#[cfg(all(feature = "time", not(feature = "chrono")))]
fn offset_now(hours: i64) -> Option<Datetime> {
    use time::OffsetDateTime;
    let seconds = hours.checked_mul(3600)?;
    let now = OffsetDateTime::now_utc().checked_add(time::Duration::seconds(seconds))?;
    crate::convert::datetime(now.year(), u8::from(now.month()), now.day(), now.hour(), now.minute(), now.second())
}

fn file_id(path: impl AsRef<Path>) -> FileId {
    FileId::new(None, VirtualPath::new(path))
}