diff options
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/content.rs | 30 | ||||
| -rw-r--r-- | src/model/layout.rs | 22 | ||||
| -rw-r--r-- | src/model/recipe.rs | 12 | ||||
| -rw-r--r-- | src/model/show.rs | 14 | ||||
| -rw-r--r-- | src/model/styles.rs | 12 |
5 files changed, 46 insertions, 44 deletions
diff --git a/src/model/content.rs b/src/model/content.rs index ae86e279..8076eff9 100644 --- a/src/model/content.rs +++ b/src/model/content.rs @@ -18,20 +18,20 @@ use crate::library::text::{ DecoNode, EmphNode, ParChild, ParNode, StrongNode, UNDERLINE, }; use crate::util::EcoString; +use crate::World; /// Layout content into a collection of pages. /// /// Relayouts until all pinned locations are converged. -pub fn layout(ctx: &mut Context, content: &Content) -> TypResult<Vec<Frame>> { - let copy = ctx.config.styles.clone(); - let styles = StyleChain::with_root(©); +pub fn layout(world: &dyn World, content: &Content) -> TypResult<Vec<Frame>> { + let styles = StyleChain::with_root(&world.config().styles); let scratch = Scratch::default(); - let mut builder = Builder::new(ctx, &scratch, true); + let mut builder = Builder::new(world, &scratch, true); builder.accept(content, styles)?; let (doc, shared) = builder.into_doc(styles)?; - doc.layout(ctx, shared) + doc.layout(world, shared) } /// Composable representation of styled content. @@ -232,15 +232,15 @@ impl Content { impl Layout for Content { fn layout( &self, - ctx: &mut Context, + world: &dyn World, regions: &Regions, styles: StyleChain, ) -> TypResult<Vec<Frame>> { let scratch = Scratch::default(); - let mut builder = Builder::new(ctx, &scratch, false); + let mut builder = Builder::new(world, &scratch, false); builder.accept(self, styles)?; let (flow, shared) = builder.into_flow(styles)?; - flow.layout(ctx, regions, shared) + flow.layout(world, regions, shared) } fn pack(self) -> LayoutNode { @@ -330,9 +330,9 @@ impl Sum for Content { } /// Builds a document or a flow node from content. -struct Builder<'a, 'ctx> { +struct Builder<'a, 'w> { /// The core context. - ctx: &'ctx mut Context, + world: &'w dyn World, /// Scratch arenas for building. scratch: &'a Scratch<'a>, /// The current document building state. @@ -354,10 +354,10 @@ struct Scratch<'a> { templates: Arena<Content>, } -impl<'a, 'ctx> Builder<'a, 'ctx> { - fn new(ctx: &'ctx mut Context, scratch: &'a Scratch<'a>, top: bool) -> Self { +impl<'a, 'w> Builder<'a, 'w> { + fn new(world: &'w dyn World, scratch: &'a Scratch<'a>, top: bool) -> Self { Self { - ctx, + world, scratch, doc: top.then(|| DocBuilder::default()), flow: FlowBuilder::default(), @@ -388,7 +388,7 @@ impl<'a, 'ctx> Builder<'a, 'ctx> { match content { Content::Empty => return Ok(()), Content::Text(text) => { - if let Some(realized) = styles.apply(self.ctx, Target::Text(text))? { + if let Some(realized) = styles.apply(self.world, Target::Text(text))? { let stored = self.scratch.templates.alloc(realized); return self.accept(stored, styles); } @@ -431,7 +431,7 @@ impl<'a, 'ctx> Builder<'a, 'ctx> { } fn show(&mut self, node: &ShowNode, styles: StyleChain<'a>) -> TypResult<()> { - if let Some(mut realized) = styles.apply(self.ctx, Target::Node(node))? { + if let Some(mut realized) = styles.apply(self.world, Target::Node(node))? { let mut map = StyleMap::new(); let barrier = Barrier::new(node.id()); map.push(StyleEntry::Barrier(barrier)); diff --git a/src/model/layout.rs b/src/model/layout.rs index d712a178..911cb4d5 100644 --- a/src/model/layout.rs +++ b/src/model/layout.rs @@ -15,7 +15,7 @@ use crate::geom::{ use crate::library::graphics::MoveNode; use crate::library::layout::{AlignNode, PadNode}; use crate::util::Prehashed; -use crate::Context; +use crate::World; /// A node that can be layouted into a sequence of regions. /// @@ -24,7 +24,7 @@ pub trait Layout: 'static { /// Layout this node into the given regions, producing frames. fn layout( &self, - ctx: &mut Context, + world: &dyn World, regions: &Regions, styles: StyleChain, ) -> TypResult<Vec<Frame>>; @@ -216,14 +216,14 @@ impl LayoutNode { impl Layout for LayoutNode { fn layout( &self, - ctx: &mut Context, + world: &dyn World, regions: &Regions, styles: StyleChain, ) -> TypResult<Vec<Frame>> { let barrier = StyleEntry::Barrier(Barrier::new(self.id())); let styles = barrier.chain(&styles); - let mut frames = self.0.layout(ctx, regions, styles)?; + let mut frames = self.0.layout(world, regions, styles)?; if let Some(role) = styles.role() { for frame in &mut frames { frame.apply_role(role); @@ -285,7 +285,7 @@ struct EmptyNode; impl Layout for EmptyNode { fn layout( &self, - _: &mut Context, + _: &dyn World, regions: &Regions, _: StyleChain, ) -> TypResult<Vec<Frame>> { @@ -307,7 +307,7 @@ struct SizedNode { impl Layout for SizedNode { fn layout( &self, - ctx: &mut Context, + world: &dyn World, regions: &Regions, styles: StyleChain, ) -> TypResult<Vec<Frame>> { @@ -331,7 +331,7 @@ impl Layout for SizedNode { }; // Layout the child. - let mut frames = self.child.layout(ctx, &pod, styles)?; + let mut frames = self.child.layout(world, &pod, styles)?; // Ensure frame size matches regions size if expansion is on. let frame = &mut frames[0]; @@ -354,11 +354,11 @@ struct FillNode { impl Layout for FillNode { fn layout( &self, - ctx: &mut Context, + world: &dyn World, regions: &Regions, styles: StyleChain, ) -> TypResult<Vec<Frame>> { - let mut frames = self.child.layout(ctx, regions, styles)?; + let mut frames = self.child.layout(world, regions, styles)?; for frame in &mut frames { let shape = Geometry::Rect(frame.size()).filled(self.fill); frame.prepend(Point::zero(), Element::Shape(shape)); @@ -379,11 +379,11 @@ struct StrokeNode { impl Layout for StrokeNode { fn layout( &self, - ctx: &mut Context, + world: &dyn World, regions: &Regions, styles: StyleChain, ) -> TypResult<Vec<Frame>> { - let mut frames = self.child.layout(ctx, regions, styles)?; + let mut frames = self.child.layout(world, regions, styles)?; for frame in &mut frames { let shape = Geometry::Rect(frame.size()).stroked(self.stroke); frame.prepend(Point::zero(), Element::Shape(shape)); diff --git a/src/model/recipe.rs b/src/model/recipe.rs index 6261e704..f5ca2cb9 100644 --- a/src/model/recipe.rs +++ b/src/model/recipe.rs @@ -5,7 +5,7 @@ use crate::diag::TypResult; use crate::eval::{Args, Func, Regex, Value}; use crate::library::structure::{EnumNode, ListNode}; use crate::syntax::Spanned; -use crate::Context; +use crate::World; /// A show rule recipe. #[derive(Clone, PartialEq, Hash)] @@ -29,7 +29,7 @@ impl Recipe { /// Try to apply the recipe to the target. pub fn apply( &self, - ctx: &mut Context, + world: &dyn World, styles: StyleChain, sel: Selector, target: Target, @@ -37,7 +37,7 @@ impl Recipe { let content = match (target, &self.pattern) { (Target::Node(node), &Pattern::Node(id)) if node.id() == id => { let node = node.unguard(sel); - self.call(ctx, || { + self.call(world, || { let dict = node.encode(styles); Value::Content(Content::Show(node, Some(dict))) })? @@ -53,7 +53,7 @@ impl Recipe { result.push(Content::Text(text[cursor .. start].into())); } - result.push(self.call(ctx, || Value::Str(mat.as_str().into()))?); + result.push(self.call(world, || Value::Str(mat.as_str().into()))?); cursor = mat.end(); } @@ -75,7 +75,7 @@ impl Recipe { } /// Call the recipe function, with the argument if desired. - fn call<F>(&self, ctx: &mut Context, arg: F) -> TypResult<Content> + fn call<F>(&self, world: &dyn World, arg: F) -> TypResult<Content> where F: FnOnce() -> Value, { @@ -85,7 +85,7 @@ impl Recipe { Args::new(self.func.span, [arg()]) }; - Ok(self.func.v.call_detached(ctx, args)?.display()) + Ok(self.func.v.call_detached(world, args)?.display()) } /// What kind of structure the property interrupts. diff --git a/src/model/show.rs b/src/model/show.rs index ac73cb76..e8d27977 100644 --- a/src/model/show.rs +++ b/src/model/show.rs @@ -6,7 +6,7 @@ use super::{Content, NodeId, Selector, StyleChain}; use crate::diag::TypResult; use crate::eval::Dict; use crate::util::Prehashed; -use crate::Context; +use crate::World; /// A node that can be realized given some styles. pub trait Show: 'static { @@ -18,7 +18,7 @@ pub trait Show: 'static { /// The base recipe for this node that is executed if there is no /// user-defined show rule. - fn realize(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Content>; + fn realize(&self, world: &dyn World, styles: StyleChain) -> TypResult<Content>; /// Finalize this node given the realization of a base or user recipe. Use /// this for effects that should work even in the face of a user-defined @@ -30,7 +30,7 @@ pub trait Show: 'static { #[allow(unused_variables)] fn finalize( &self, - ctx: &mut Context, + world: &dyn World, styles: StyleChain, realized: Content, ) -> TypResult<Content> { @@ -74,17 +74,17 @@ impl Show for ShowNode { self.0.encode(styles) } - fn realize(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Content> { - self.0.realize(ctx, styles) + fn realize(&self, world: &dyn World, styles: StyleChain) -> TypResult<Content> { + self.0.realize(world, styles) } fn finalize( &self, - ctx: &mut Context, + world: &dyn World, styles: StyleChain, realized: Content, ) -> TypResult<Content> { - self.0.finalize(ctx, styles, realized) + self.0.finalize(world, styles, realized) } fn pack(self) -> ShowNode { diff --git a/src/model/styles.rs b/src/model/styles.rs index eab33402..53ef926c 100644 --- a/src/model/styles.rs +++ b/src/model/styles.rs @@ -8,7 +8,7 @@ use crate::diag::TypResult; use crate::frame::Role; use crate::library::text::{FontFamily, TextNode}; use crate::util::ReadableTypeId; -use crate::Context; +use crate::World; /// A map of style properties. #[derive(Default, Clone, PartialEq, Hash)] @@ -277,7 +277,7 @@ impl<'a> StyleChain<'a> { } /// Apply show recipes in this style chain to a target. - pub fn apply(self, ctx: &mut Context, target: Target) -> TypResult<Option<Content>> { + pub fn apply(self, world: &dyn World, target: Target) -> TypResult<Option<Content>> { // Find out how many recipes there any and whether any of their patterns // match. let mut n = 0; @@ -296,7 +296,9 @@ impl<'a> StyleChain<'a> { let sel = Selector::Nth(n); if self.guarded(sel) { guarded = true; - } else if let Some(content) = recipe.apply(ctx, self, sel, target)? { + } else if let Some(content) = + recipe.apply(world, self, sel, target)? + { realized = Some(content); break; } @@ -312,7 +314,7 @@ impl<'a> StyleChain<'a> { if self.guarded(sel) { guarded = true; } else { - let content = node.unguard(sel).realize(ctx, self)?; + let content = node.unguard(sel).realize(world, self)?; realized = Some(content.styled_with_entry(StyleEntry::Guard(sel))); } } @@ -320,7 +322,7 @@ impl<'a> StyleChain<'a> { // Finalize only if guarding didn't stop any recipe. if !guarded { if let Some(content) = realized { - realized = Some(node.finalize(ctx, self, content)?); + realized = Some(node.finalize(world, self, content)?); } } } |
