use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use once_cell::sync::Lazy;
use typst::diag::{FileError, FileResult};
use typst::foundations::{Bytes, Datetime, Dict, IntoValue};
use typst::syntax::{FileId, Source};
use typst::text::{Font, FontBook};
use typst::utils::LazyHash;
use typst::Library;
use typst_kit::fonts::{FontSearcher, FontSlot};
static CACHED_FONTS: Lazy<(FontBook, Vec<Font>)> = Lazy::new(|| {
let mut font_searcher = FontSearcher::new();
let font_searcher = font_searcher.include_system_fonts(true);
let fonts = match std::env::var_os("FONTS_DIR") {
Some(fonts_dir) => {
let fonts_dir = PathBuf::from(fonts_dir);
font_searcher.search_with([&fonts_dir])
}
None => font_searcher.search(),
};
let book = fonts.book;
let fonts = fonts
.fonts
.iter()
.map(FontSlot::get)
.filter_map(|f| f)
.collect::<Vec<_>>();
(book, fonts)
});
#[derive(Debug)]
pub struct TypstWorld {
pub source: Source,
library: LazyHash<Library>,
book: LazyHash<FontBook>,
fonts: Vec<Font>,
files: Arc<Mutex<HashMap<FileId, FileEntry>>>,
#[allow(dead_code)]
cache_directory: PathBuf,
time: time::OffsetDateTime,
}
impl TypstWorld {
pub fn new(template_content: String, data: String) -> Self {
let (book, fonts) = CACHED_FONTS.clone();
let mut inputs_dict = Dict::new();
inputs_dict.insert("data".into(), data.as_str().into_value());
let library = Library::builder().with_inputs(inputs_dict).build();
Self {
library: LazyHash::new(library),
book: LazyHash::new(book),
fonts, source: Source::detached(template_content),
time: time::OffsetDateTime::now_utc(),
cache_directory: std::env::var_os("CACHE_DIRECTORY")
.map(|os_path| os_path.into())
.unwrap_or(std::env::temp_dir()),
files: Arc::new(Mutex::new(HashMap::new())),
}
}
pub fn update_data(&mut self, data: String) -> Result<(), String> {
let mut inputs_dict = Dict::new();
inputs_dict.insert("data".into(), data.as_str().into_value());
let library = Library::builder().with_inputs(inputs_dict).build();
self.library = LazyHash::new(library);
Ok(())
}
}
#[derive(Clone, Debug)]
struct FileEntry {
bytes: Bytes,
source: Option<Source>,
}
impl FileEntry {
#[allow(dead_code)]
fn new(bytes: Vec<u8>, source: Option<Source>) -> Self {
Self {
bytes: Bytes::new(bytes),
source,
}
}
fn source(&mut self, id: FileId) -> FileResult<Source> {
let source = if let Some(source) = &self.source {
source
} else {
let contents = std::str::from_utf8(&self.bytes).map_err(|_| FileError::InvalidUtf8)?;
let contents = contents.trim_start_matches('\u{feff}');
let source = Source::new(id, contents.into());
self.source.insert(source)
};
Ok(source.clone())
}
}
impl TypstWorld {
fn file(&self, id: FileId) -> FileResult<FileEntry> {
let files = self.files.lock().map_err(|_| FileError::AccessDenied)?;
if let Some(entry) = files.get(&id) {
return Ok(entry.clone());
}
eprintln!("accessing file id: {id:?}");
Err(FileError::AccessDenied)
}
}
impl typst::World for TypstWorld {
fn library(&self) -> &LazyHash<Library> {
&self.library
}
fn book(&self) -> &LazyHash<FontBook> {
&self.book
}
fn main(&self) -> FileId {
self.source.id()
}
fn source(&self, id: FileId) -> FileResult<Source> {
if id == self.source.id() {
Ok(self.source.clone())
} else {
self.file(id)?.source(id)
}
}
fn file(&self, id: FileId) -> FileResult<Bytes> {
self.file(id).map(|file| file.bytes.clone())
}
fn font(&self, id: usize) -> Option<Font> {
self.fonts.get(id).cloned()
}
fn today(&self, offset: Option<i64>) -> Option<Datetime> {
let offset = offset.unwrap_or(0);
let offset = time::UtcOffset::from_hms(offset.try_into().ok()?, 0, 0).ok()?;
let time = self.time.checked_to_offset(offset)?;
Some(Datetime::Date(time.date()))
}
}