diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-29 15:10:29 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-29 15:10:29 +0200 |
| commit | 4551f9c554c1fef32ef2b7e55b01b1fb90186cbf (patch) | |
| tree | 0a1fc54632067dfff4c9beca8a3ce00056c45da9 /typst/src | |
| parent | 2f2014fb9cf9b22593088f0fcc16b8b9d8a436ae (diff) | |
feat(typst): minimal rust lib building svg
Diffstat (limited to 'typst/src')
| -rw-r--r-- | typst/src/lib.rs | 24 | ||||
| -rw-r--r-- | typst/src/world.rs | 97 |
2 files changed, 121 insertions, 0 deletions
diff --git a/typst/src/lib.rs b/typst/src/lib.rs new file mode 100644 index 0000000..5d9dcc4 --- /dev/null +++ b/typst/src/lib.rs @@ -0,0 +1,24 @@ +//! Based on https://github.com/zeon256/minimal-typst-svg-renderer + +use typst::layout::PagedDocument; +use typst_svg::svg_frame; + +use crate::world::MinimalWorld; + +mod world; + +pub fn compile() { + let content = include_str!("../../template.typ"); + let world = MinimalWorld::new(content); + + let res = typst::compile::<PagedDocument>(&world); + + if !res.warnings.is_empty() { + eprintln!("Warnings: {:?}", res.warnings); + } + + let doc = res.output.expect("Error compiling typst"); + + let svg = svg_frame(&doc.pages[0].frame); + println!("{}", svg) +} diff --git a/typst/src/world.rs b/typst/src/world.rs new file mode 100644 index 0000000..5bbefee --- /dev/null +++ b/typst/src/world.rs @@ -0,0 +1,97 @@ +//! Based on https://github.com/zeon256/minimal-typst-svg-renderer + +use std::path::PathBuf; + +use typst::Library; +use typst::LibraryExt; +use typst::World; +use typst::diag::{FileError, FileResult}; +use typst::foundations::{Bytes, Datetime}; +use typst::syntax::{FileId, Source}; +use typst::text::{Font, FontBook}; +use typst::utils::LazyHash; +use typst_kit::fonts::Fonts; + +/// Main interface that determines the environment for Typst. +pub struct MinimalWorld { + /// The content of a source. + source: Source, + + /// The standard library. + library: LazyHash<Library>, + + /// Metadata about all known fonts. + book: LazyHash<FontBook>, + + /// Metadata about all known fonts. + fonts: Vec<Font>, +} + +impl MinimalWorld { + pub fn new(source: impl Into<String>) -> Self { + let (fonts, book) = Self::load_fonts(); + + Self { + library: LazyHash::new(Library::default()), + book: LazyHash::new(book), + fonts: fonts, + source: Source::detached(source), + } + } + + fn load_fonts() -> (Vec<Font>, FontBook) { + let mut searcher = Fonts::searcher(); + searcher.include_system_fonts(true); + + let mut fonts = Vec::new(); + let mut book = FontBook::new(); + for font in searcher.search().fonts { + book.push(font.get().unwrap().info().clone()); + fonts.push(font.get().unwrap()); + } + (fonts, book) + } +} + +impl World for MinimalWorld { + /// Standard library. + fn library(&self) -> &LazyHash<Library> { + &self.library + } + + /// Metadata about all known Books. + fn book(&self) -> &LazyHash<FontBook> { + &self.book + } + + /// Accessing the main source file. + fn main(&self) -> FileId { + self.source.id() + } + + /// Accessing a specified source file (based on `FileId`). + fn source(&self, id: FileId) -> FileResult<Source> { + if id == self.source.id() { + Ok(self.source.clone()) + } else { + Err(FileError::NotFound(PathBuf::new())) + } + } + + /// Accessing a specified file (non-file). + fn file(&self, _id: FileId) -> FileResult<Bytes> { + Err(FileError::NotFound(PathBuf::new())) + } + + /// Accessing a specified font per index of font book. + fn font(&self, id: usize) -> Option<Font> { + self.fonts.get(id).cloned() + } + + /// Get the current date. + /// + /// Optionally, an offset in hours is given. + fn today(&self, _offset: Option<i64>) -> Option<Datetime> { + None + } +} |
