From 9ac125dea8d6ea6cc01814d04413225845b69d65 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sun, 10 Oct 2021 20:54:13 +0200 Subject: [PATCH] Rename `State` to `Style` and move it into its own module --- benches/oneshot.rs | 2 +- src/eval/mod.rs | 2 - src/eval/template.rs | 123 ++++++++++++++-------------- src/eval/walk.rs | 30 +++---- src/geom/paint.rs | 2 +- src/layout/par.rs | 8 +- src/layout/shaping.rs | 38 ++++----- src/lib.rs | 30 +++---- src/library/elements.rs | 8 +- src/library/layout.rs | 54 ++++++------ src/library/mod.rs | 3 +- src/library/text.rs | 34 ++++---- src/{eval/state.rs => style/mod.rs} | 89 ++++++++++---------- src/{ => style}/paper.rs | 56 ++++++------- tests/typeset.rs | 13 +-- 15 files changed, 249 insertions(+), 243 deletions(-) rename src/{eval/state.rs => style/mod.rs} (79%) rename src/{ => style}/paper.rs (89%) diff --git a/benches/oneshot.rs b/benches/oneshot.rs index dda3c5728..57e3b339d 100644 --- a/benches/oneshot.rs +++ b/benches/oneshot.rs @@ -60,7 +60,7 @@ fn bench_eval(iai: &mut Iai) { fn bench_to_tree(iai: &mut Iai) { let (mut ctx, id) = context(); let module = ctx.evaluate(id).unwrap(); - iai.run(|| module.template.to_tree(ctx.state())); + iai.run(|| module.template.to_tree(ctx.style())); } fn bench_layout(iai: &mut Iai) { diff --git a/src/eval/mod.rs b/src/eval/mod.rs index 384ebb2b1..2adf785e1 100644 --- a/src/eval/mod.rs +++ b/src/eval/mod.rs @@ -12,7 +12,6 @@ mod capture; mod function; mod ops; mod scope; -mod state; mod template; mod walk; @@ -22,7 +21,6 @@ pub use capture::*; pub use dict::*; pub use function::*; pub use scope::*; -pub use state::*; pub use template::*; pub use value::*; pub use walk::*; diff --git a/src/eval/template.rs b/src/eval/template.rs index 42b35fc32..0ea312e5d 100644 --- a/src/eval/template.rs +++ b/src/eval/template.rs @@ -4,13 +4,14 @@ use std::mem; use std::ops::{Add, AddAssign}; use std::rc::Rc; -use super::{State, Str}; +use super::Str; use crate::diag::StrResult; use crate::geom::{Align, Dir, Gen, GenAxis, Length, Linear, Sides, Size}; use crate::layout::{ Decoration, LayoutNode, LayoutTree, PadNode, PageRun, ParChild, ParNode, StackChild, StackNode, }; +use crate::style::Style; use crate::util::EcoString; /// A template value: `[*Hi* there]`. @@ -33,15 +34,15 @@ enum TemplateNode { /// Spacing. Spacing(GenAxis, Linear), /// An inline node builder. - Inline(Rc LayoutNode>, Vec), + Inline(Rc LayoutNode>, Vec), /// An block node builder. - Block(Rc LayoutNode>), - /// Save the current state. + Block(Rc LayoutNode>), + /// Save the current style. Save, - /// Restore the last saved state. + /// Restore the last saved style. Restore, - /// A function that can modify the current state. - Modify(Rc), + /// A function that can modify the current style. + Modify(Rc), } impl Template { @@ -53,7 +54,7 @@ impl Template { /// Create a template from a builder for an inline-level node. pub fn from_inline(f: F) -> Self where - F: Fn(&State) -> T + 'static, + F: Fn(&Style) -> T + 'static, T: Into, { let node = TemplateNode::Inline(Rc::new(move |s| f(s).into()), vec![]); @@ -63,7 +64,7 @@ impl Template { /// Create a template from a builder for a block-level node. pub fn from_block(f: F) -> Self where - F: Fn(&State) -> T + 'static, + F: Fn(&Style) -> T + 'static, T: Into, { let node = TemplateNode::Block(Rc::new(move |s| f(s).into())); @@ -98,7 +99,7 @@ impl Template { /// Add text, but in monospace. pub fn monospace(&mut self, text: impl Into) { self.save(); - self.modify(|state| state.font_mut().monospace = true); + self.modify(|style| style.text_mut().monospace = true); self.text(text); self.restore(); } @@ -126,16 +127,16 @@ impl Template { self.make_mut().push(TemplateNode::Save); } - /// Ensure that later nodes are untouched by state modifications made since + /// Ensure that later nodes are untouched by style modifications made since /// the last snapshot. pub fn restore(&mut self) { self.make_mut().push(TemplateNode::Restore); } - /// Modify the state. + /// Modify the style. pub fn modify(&mut self, f: F) where - F: Fn(&mut State) + 'static, + F: Fn(&mut Style) + 'static, { self.make_mut().push(TemplateNode::Modify(Rc::new(f))); } @@ -143,7 +144,7 @@ impl Template { /// Return a new template which is modified from start to end. pub fn modified(self, f: F) -> Self where - F: Fn(&mut State) + 'static, + F: Fn(&mut Style) + 'static, { let mut wrapper = Self::new(); wrapper.save(); @@ -153,18 +154,18 @@ impl Template { wrapper } - /// Build the stack node resulting from instantiating the template in the - /// given state. - pub fn to_stack(&self, state: &State) -> StackNode { - let mut builder = Builder::new(state, false); + /// Build the stack node resulting from instantiating the template with the + /// given style. + pub fn to_stack(&self, style: &Style) -> StackNode { + let mut builder = Builder::new(style, false); builder.template(self); builder.build_stack() } - /// Build the layout tree resulting from instantiating the template in the - /// given state. - pub fn to_tree(&self, state: &State) -> LayoutTree { - let mut builder = Builder::new(state, true); + /// Build the layout tree resulting from instantiating the template with the + /// given style. + pub fn to_tree(&self, style: &Style) -> LayoutTree { + let mut builder = Builder::new(style, true); builder.template(self); builder.build_tree() } @@ -238,10 +239,10 @@ impl Add