summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-08-29 15:09:54 +0200
committerLaurenz <laurmaedje@gmail.com>2021-08-29 15:09:54 +0200
commite4e79990dad90aea17ea99e54fcd60435927bb56 (patch)
tree2e893ac5e104867629c2dad38502ef3fdc426d33
parent411aba5b6f541f96bbdb4e23a40cf022a867dc11 (diff)
Allow body for font function once again
-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
-rw-r--r--tests/typ/text/decorations.typ2
-rw-r--r--tests/typ/text/font.typ26
-rw-r--r--tests/typ/text/links.typ2
7 files changed, 42 insertions, 23 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>>);
diff --git a/tests/typ/text/decorations.typ b/tests/typ/text/decorations.typ
index 3f953b07..9cd7b096 100644
--- a/tests/typ/text/decorations.typ
+++ b/tests/typ/text/decorations.typ
@@ -13,7 +13,7 @@
#underline(red)[Critical information is conveyed here.]
// Inherits font color.
-[#font(fill: red) #underline[Change with the wind.]]
+#font(fill: red, underline[Change with the wind.])
// Both over- and underline.
#overline(underline[Running amongst the wolves.])
diff --git a/tests/typ/text/font.typ b/tests/typ/text/font.typ
index ecc07886..98643565 100644
--- a/tests/typ/text/font.typ
+++ b/tests/typ/text/font.typ
@@ -2,35 +2,35 @@
---
// Set same font size in three different ways.
-[#font(22pt) A]
-[#font(200%) A]
-[#font(size: 16.5pt + 50%) A]
+#font(22pt)[A]
+#font(200%)[A]
+#font(size: 16.5pt + 50%)[A]
// Do nothing.
-[#font() Normal]
+#font()[Normal]
// Set style (is available).
-[#font(style: italic) Italic]
+#font(style: italic)[Italic]
// Set weight (is available).
-[#font(weight: bold) Bold]
+#font(weight: bold)[Bold]
// Set stretch (not available, matching closest).
-[#font(stretch: 50%) Condensed]
+#font(stretch: 50%)[Condensed]
// Set family.
-[#font(family: "PT Sans") Sans serif]
+#font(family: "PT Sans")[Sans serif]
// Emoji.
Emoji: 🐪, 🌋, 🏞
// Math.
-[#font("Latin Modern Math") ∫ 𝛼 + 3𝛽 d𝑡]
+#font("Latin Modern Math")[∫ 𝛼 + 3𝛽 d𝑡]
// Colors.
[
#font(fill: eastern)
- This is [#font(fill: rgb("FA644B")) way more] colorful.
+ This is #font(fill: rgb("FA644B"))[way more] colorful.
]
// Disable font fallback beyond the user-specified list.
@@ -41,9 +41,9 @@ Emoji: 🐪, 🌋, 🏞
---
// Test class definitions.
#font(sans-serif: "PT Sans")
-[#font(family: sans-serif) Sans-serif.] \
-[#font(monospace) Monospace.] \
-[#font(monospace, monospace: ("Nope", "Latin Modern Math")) Math.]
+#font(family: sans-serif)[Sans-serif.] \
+#font(monospace)[Monospace.] \
+#font(monospace, monospace: ("Nope", "Latin Modern Math"))[Math.]
---
// Test top and bottom edge.
diff --git a/tests/typ/text/links.typ b/tests/typ/text/links.typ
index eabb316a..e6aa1d89 100644
--- a/tests/typ/text/links.typ
+++ b/tests/typ/text/links.typ
@@ -8,5 +8,5 @@
This link appears #link("https://google.com/")[in the middle of] a paragraph.
// Styled with underline and color.
-#let link(url, body) = link(url, [#font(fill: rgb("283663")) #underline(body)])
+#let link(url, body) = link(url, font(fill: rgb("283663"), underline(body)))
You could also make the #link("https://html5zombo.com/")[link look way more typical.]