diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-06-29 13:45:59 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-06-29 13:49:50 +0200 |
| commit | 32a6b673bc4b73ade5863b0166c0779597cede5c (patch) | |
| tree | 7d32aa06c63052a759f7fc4d89bb551faeb258d0 /src | |
| parent | b89cd128ae118571efe8a5a5b40b9365e3007746 (diff) | |
Make use of wide calls
Diffstat (limited to 'src')
| -rw-r--r-- | src/exec/mod.rs | 14 | ||||
| -rw-r--r-- | src/library/layout.rs | 25 | ||||
| -rw-r--r-- | src/library/text.rs | 22 |
3 files changed, 24 insertions, 37 deletions
diff --git a/src/exec/mod.rs b/src/exec/mod.rs index fea6de33..882f1d0b 100644 --- a/src/exec/mod.rs +++ b/src/exec/mod.rs @@ -72,10 +72,10 @@ impl Exec for syntax::RawNode { ctx.parbreak(); } - let snapshot = ctx.state.clone(); + let snapshot = Rc::clone(&ctx.state.font); ctx.set_monospace(); ctx.push_text(&self.text); - ctx.state = snapshot; + ctx.state.font = snapshot; if self.block { ctx.parbreak(); @@ -85,16 +85,17 @@ impl Exec for syntax::RawNode { impl ExecWithMap for syntax::HeadingNode { fn exec_with_map(&self, ctx: &mut ExecContext, map: &ExprMap) { + ctx.parbreak(); + let snapshot = ctx.state.clone(); let font = ctx.state.font_mut(); - let upscale = 1.6 - 0.1 * self.level as f64; font.size *= upscale; font.strong = true; self.body.exec_with_map(ctx, map); - ctx.state = snapshot; + ctx.parbreak(); } } @@ -113,8 +114,6 @@ impl ExecWithMap for syntax::EnumItem { } fn exec_item(ctx: &mut ExecContext, label: String, body: &syntax::Tree, map: &ExprMap) { - ctx.parbreak(); - let label = ctx.exec_stack(|ctx| ctx.push_text(label)); let body = ctx.exec_tree_stack(body, map); let stack = StackNode { @@ -128,7 +127,6 @@ fn exec_item(ctx: &mut ExecContext, label: String, body: &syntax::Tree, map: &Ex }; ctx.push_into_stack(stack); - ctx.parbreak(); } impl Exec for Value { @@ -172,6 +170,8 @@ impl Exec for TemplateNode { impl Exec for TemplateFunc { fn exec(&self, ctx: &mut ExecContext) { + let snapshot = ctx.state.clone(); self(ctx); + ctx.state = snapshot; } } diff --git a/src/library/layout.rs b/src/library/layout.rs index 08924bad..4903077b 100644 --- a/src/library/layout.rs +++ b/src/library/layout.rs @@ -20,7 +20,7 @@ pub fn page(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { let right = args.named(ctx, "right"); let bottom = args.named(ctx, "bottom"); let flip = args.named(ctx, "flip"); - let body = args.eat::<TemplateValue>(ctx); + let body = args.expect::<TemplateValue>(ctx, "body").unwrap_or_default(); Value::template(move |ctx| { let snapshot = ctx.state.clone(); @@ -66,13 +66,10 @@ pub fn page(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { } ctx.pagebreak(false, true, span); + body.exec(ctx); - if let Some(body) = &body { - // TODO: Restrict body to a single page? - body.exec(ctx); - ctx.state = snapshot; - ctx.pagebreak(true, false, span); - } + ctx.state = snapshot; + ctx.pagebreak(true, false, span); }) } @@ -111,7 +108,7 @@ pub fn align(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { let second = args.eat::<AlignValue>(ctx); let mut horizontal = args.named::<AlignValue>(ctx, "horizontal"); let mut vertical = args.named::<AlignValue>(ctx, "vertical"); - let body = args.eat::<TemplateValue>(ctx); + let body = args.expect::<TemplateValue>(ctx, "body").unwrap_or_default(); for value in first.into_iter().chain(second) { match value.axis() { @@ -126,23 +123,19 @@ pub fn align(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { } Value::template(move |ctx| { - let snapshot = ctx.state.clone(); - if let Some(horizontal) = horizontal { ctx.state.aligns.cross = horizontal.to_align(ctx.state.lang.dir); } if let Some(vertical) = vertical { - ctx.state.aligns.main = vertical.to_align(Dir::TTB); - if ctx.state.aligns.main != snapshot.aligns.main { + let new = vertical.to_align(Dir::TTB); + if ctx.state.aligns.main != new { + ctx.state.aligns.main = new; ctx.parbreak(); } } - if let Some(body) = &body { - body.exec(ctx); - ctx.state = snapshot; - } + body.exec(ctx); }) } diff --git a/src/library/text.rs b/src/library/text.rs index 6a2fe9bb..28ca2bd8 100644 --- a/src/library/text.rs +++ b/src/library/text.rs @@ -17,10 +17,9 @@ pub fn font(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { let serif = args.named(ctx, "serif"); let sans_serif = args.named(ctx, "sans-serif"); let monospace = args.named(ctx, "monospace"); - let body = args.eat::<TemplateValue>(ctx); + let body = args.expect::<TemplateValue>(ctx, "body").unwrap_or_default(); Value::template(move |ctx| { - let snapshot = ctx.state.clone(); let font = ctx.state.font_mut(); if let Some(linear) = size { @@ -67,10 +66,7 @@ pub fn font(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { font.families_mut().monospace = monospace.clone(); } - if let Some(body) = &body { - body.exec(ctx); - ctx.state = snapshot; - } + body.exec(ctx); }) } @@ -161,6 +157,7 @@ pub fn par(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { let spacing = args.named(ctx, "spacing"); let leading = args.named(ctx, "leading"); let word_spacing = args.named(ctx, "word-spacing"); + let body = args.expect::<TemplateValue>(ctx, "body").unwrap_or_default(); Value::template(move |ctx| { if let Some(spacing) = spacing { @@ -176,6 +173,7 @@ pub fn par(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { } ctx.parbreak(); + body.exec(ctx); }) } @@ -190,6 +188,7 @@ pub fn lang(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { } None => None, }; + let body = args.expect::<TemplateValue>(ctx, "body").unwrap_or_default(); Value::template(move |ctx| { if let Some(dir) = dir.or(iso) { @@ -197,6 +196,7 @@ pub fn lang(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { } ctx.parbreak(); + body.exec(ctx); }) } @@ -232,7 +232,7 @@ fn line_impl( let position = args.named(ctx, "position"); let strength = args.named::<Linear>(ctx, "strength"); let extent = args.named(ctx, "extent").unwrap_or_default(); - let body = args.eat::<TemplateValue>(ctx); + let body = args.expect::<TemplateValue>(ctx, "body").unwrap_or_default(); // Suppress any existing strikethrough if strength is explicitly zero. let state = strength.map_or(true, |s| !s.is_zero()).then(|| { @@ -245,13 +245,7 @@ fn line_impl( }); Value::template(move |ctx| { - let snapshot = ctx.state.clone(); - *substate(ctx.state.font_mut()) = state.clone(); - - if let Some(body) = &body { - body.exec(ctx); - ctx.state = snapshot; - } + body.exec(ctx); }) } |
