summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/eval/template.rs13
-rw-r--r--src/library/layout.rs8
-rw-r--r--src/library/mod.rs2
-rw-r--r--src/library/text.rs12
4 files changed, 27 insertions, 8 deletions
diff --git a/src/eval/template.rs b/src/eval/template.rs
index 4fc6985a..f4feda40 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<F>(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 3e8aa7d2..a62be646 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<Value> {
}
};
- 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 9d25a008..a549aa72 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 f12794aa..2cb5ccaf 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<Value> {
let sans_serif = args.named("sans-serif")?;
let monospace = args.named("monospace")?;
let fallback = args.named("fallback")?;
+ let body = args.eat::<Template>();
- ctx.template.modify(move |state| {
+ let f = move |state: &mut State| {
let font = state.font_mut();
if let Some(size) = size {
@@ -71,9 +72,14 @@ pub fn font(ctx: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> {
if let Some(fallback) = fallback {
font.fallback = fallback;
}
- });
+ };
- Ok(Value::None)
+ Ok(if let Some(body) = body {
+ Value::Template(body.modified(f))
+ } else {
+ ctx.template.modify(f);
+ Value::None
+ })
}
struct FontDef(Rc<Vec<FontFamily>>);