diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/model/library.rs | 11 | ||||
| -rw-r--r-- | src/model/realize.rs | 21 | ||||
| -rw-r--r-- | src/model/typeset.rs | 20 |
3 files changed, 31 insertions, 21 deletions
diff --git a/src/model/library.rs b/src/model/library.rs index 518caca1..eee69675 100644 --- a/src/model/library.rs +++ b/src/model/library.rs @@ -2,15 +2,13 @@ use std::fmt::{self, Debug, Formatter}; use std::hash::{Hash, Hasher}; use std::num::NonZeroUsize; -use comemo::Tracked; use once_cell::sync::OnceCell; -use super::{Content, NodeId, Scope, StyleChain, StyleMap}; +use super::{Content, NodeId, Scope, StyleChain, StyleMap, Vt}; use crate::diag::SourceResult; use crate::doc::Document; use crate::geom::{Abs, Dir}; use crate::util::{hash128, EcoString}; -use crate::World; /// Definition of Typst's standard library. #[derive(Debug, Clone, Hash)] @@ -27,11 +25,8 @@ pub struct Library { #[derive(Clone)] pub struct LangItems { /// The root layout function. - pub layout: fn( - world: Tracked<dyn World>, - content: &Content, - styles: StyleChain, - ) -> SourceResult<Document>, + pub layout: + fn(vt: &mut Vt, content: &Content, styles: StyleChain) -> SourceResult<Document>, /// Access the em size. pub em: fn(StyleChain) -> Abs, /// Access the text direction. diff --git a/src/model/realize.rs b/src/model/realize.rs index 9d7c4aec..46e0d678 100644 --- a/src/model/realize.rs +++ b/src/model/realize.rs @@ -1,8 +1,5 @@ -use comemo::Tracked; - -use super::{capability, Content, NodeId, Recipe, Selector, StyleChain}; +use super::{capability, Content, NodeId, Recipe, Selector, StyleChain, Vt}; use crate::diag::SourceResult; -use crate::World; /// Whether the target is affected by show rules in the given style chain. pub fn applicable(target: &Content, styles: StyleChain) -> bool { @@ -22,7 +19,7 @@ pub fn applicable(target: &Content, styles: StyleChain) -> bool { /// Apply the show rules in the given style chain to a target. pub fn realize( - world: Tracked<dyn World>, + vt: &mut Vt, target: &Content, styles: StyleChain, ) -> SourceResult<Option<Content>> { @@ -34,7 +31,7 @@ pub fn realize( for recipe in styles.recipes() { let guard = Guard::Nth(n); if recipe.applicable(target) && !target.is_guarded(guard) { - if let Some(content) = try_apply(world, &target, recipe, guard)? { + if let Some(content) = try_apply(vt, &target, recipe, guard)? { realized = Some(content); break; } @@ -46,7 +43,7 @@ pub fn realize( if let Some(showable) = target.with::<dyn Show>() { let guard = Guard::Base(target.id()); if realized.is_none() && !target.is_guarded(guard) { - realized = Some(showable.show(world, styles)); + realized = Some(showable.show(vt, target, styles)); } } @@ -64,7 +61,7 @@ pub fn realize( /// Try to apply a recipe to the target. fn try_apply( - world: Tracked<dyn World>, + vt: &Vt, target: &Content, recipe: &Recipe, guard: Guard, @@ -75,7 +72,7 @@ fn try_apply( return Ok(None); } - recipe.apply(world, target.clone().guarded(guard)).map(Some) + recipe.apply(vt.world(), target.clone().guarded(guard)).map(Some) } Some(Selector::Label(label)) => { @@ -83,7 +80,7 @@ fn try_apply( return Ok(None); } - recipe.apply(world, target.clone().guarded(guard)).map(Some) + recipe.apply(vt.world(), target.clone().guarded(guard)).map(Some) } Some(Selector::Regex(regex)) => { @@ -107,7 +104,7 @@ fn try_apply( } let piece = make(m.as_str().into()).guarded(guard); - let transformed = recipe.apply(world, piece)?; + let transformed = recipe.apply(vt.world(), piece)?; result.push(transformed); cursor = m.end(); } @@ -131,7 +128,7 @@ fn try_apply( #[capability] pub trait Show { /// Execute the base recipe for this node. - fn show(&self, world: Tracked<dyn World>, styles: StyleChain) -> Content; + fn show(&self, vt: &mut Vt, this: &Content, styles: StyleChain) -> Content; } /// Post-process a node after it was realized. diff --git a/src/model/typeset.rs b/src/model/typeset.rs index 451c6eb0..b422e4b6 100644 --- a/src/model/typeset.rs +++ b/src/model/typeset.rs @@ -10,5 +10,23 @@ use crate::World; pub fn typeset(world: Tracked<dyn World>, content: &Content) -> SourceResult<Document> { let library = world.library(); let styles = StyleChain::new(&library.styles); - (library.items.layout)(world, content, styles) + let mut vt = Vt { world }; + (library.items.layout)(&mut vt, content, styles) +} + +/// A virtual typesetter. +/// +/// Holds the state needed to [typeset] content. This is the equivalent to the +/// [Vm](super::Vm) for typesetting. +pub struct Vt<'a> { + /// The compilation environment. + #[doc(hidden)] + pub world: Tracked<'a, dyn World>, +} + +impl<'a> Vt<'a> { + /// Access the underlying world. + pub fn world(&self) -> Tracked<'a, dyn World> { + self.world + } } |
