diff --git a/src/eval/template.rs b/src/eval/template.rs index 4fc6985a1..f4feda400 100644 --- a/src/eval/template.rs +++ b/src/eval/template.rs @@ -140,6 +140,19 @@ impl Template { self.make_mut().push(TemplateNode::Modify(Rc::new(f))); } + /// Return a new template which is modified from start to end. + pub fn modified(self, f: F) -> Self + where + F: Fn(&mut State) + 'static, + { + let mut wrapper = Self::new(); + wrapper.save(); + wrapper.modify(f); + wrapper += self; + wrapper.restore(); + wrapper + } + /// Build the stack node resulting from instantiating the template in the /// given state. pub fn to_stack(&self, state: &State) -> StackNode { diff --git a/src/library/layout.rs b/src/library/layout.rs index 3e8aa7d24..a62be646c 100644 --- a/src/library/layout.rs +++ b/src/library/layout.rs @@ -126,17 +126,17 @@ pub fn align(ctx: &mut EvalContext, args: &mut Arguments) -> TypResult { } }; - if let Some(body) = body { + Ok(if let Some(body) = body { let mut template = Template::new(); template.save(); realign(&mut template); template += body; template.restore(); - Ok(Value::Template(template)) + Value::Template(template) } else { realign(&mut ctx.template); - Ok(Value::None) - } + Value::None + }) } /// `box`: Place content in a rectangular box. diff --git a/src/library/mod.rs b/src/library/mod.rs index 9d25a0086..a549aa727 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -18,7 +18,7 @@ use std::rc::Rc; use crate::color::{Color, RgbaColor}; use crate::diag::TypResult; -use crate::eval::{Arguments, EvalContext, Scope, Str, Template, Value}; +use crate::eval::{Arguments, EvalContext, Scope, State, Str, Template, Value}; use crate::font::{FontFamily, FontStretch, FontStyle, FontWeight, VerticalFontMetric}; use crate::geom::*; use crate::layout::LayoutNode; diff --git a/src/library/text.rs b/src/library/text.rs index f12794aa8..2cb5ccaf7 100644 --- a/src/library/text.rs +++ b/src/library/text.rs @@ -20,8 +20,9 @@ pub fn font(ctx: &mut EvalContext, args: &mut Arguments) -> TypResult { let sans_serif = args.named("sans-serif")?; let monospace = args.named("monospace")?; let fallback = args.named("fallback")?; + let body = args.eat::