From 78f399521661c82e45e627bbbdc8ce4daa9e5207 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Wed, 29 Apr 2026 22:30:04 +0200 Subject: style(typst): move in lib folder --- lib/typst/src/world.rs | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 lib/typst/src/world.rs (limited to 'lib/typst/src/world.rs') diff --git a/lib/typst/src/world.rs b/lib/typst/src/world.rs new file mode 100644 index 0000000..c2fe7c7 --- /dev/null +++ b/lib/typst/src/world.rs @@ -0,0 +1,88 @@ +//! 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::{FontSlot, Fonts}; + +/// Main interface that determines the environment for Typst. +pub struct MinimalWorld { + /// The content of a source. + source: Source, + + /// The standard library. + library: LazyHash, + + /// Metadata about all known fonts. + book: LazyHash, + + /// Metadata about all known fonts. + fonts: Vec, +} + +impl MinimalWorld { + pub fn new(source: impl Into) -> Self { + let mut searcher = Fonts::searcher(); + searcher.include_system_fonts(true); + #[cfg(feature = "embed-fonts")] + searcher.include_embedded_fonts(true); + let fonts = searcher.search(); + + Self { + library: LazyHash::new(Library::default()), + book: LazyHash::new(fonts.book), + fonts: fonts.fonts, + source: Source::detached(source), + } + } +} + +impl World for MinimalWorld { + /// Standard library. + fn library(&self) -> &LazyHash { + &self.library + } + + /// Metadata about all known Books. + fn book(&self) -> &LazyHash { + &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 { + 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 { + Err(FileError::NotFound(PathBuf::new())) + } + + /// Accessing a specified font per index of font book. + fn font(&self, id: usize) -> Option { + self.fonts.get(id)?.get() + } + + /// Get the current date. + /// + /// Optionally, an offset in hours is given. + fn today(&self, _offset: Option) -> Option { + None + } +} -- cgit v1.2.3