Allow body for font function once again

This commit is contained in:
Laurenz 2021-08-29 15:09:54 +02:00
parent 411aba5b6f
commit e4e79990da
7 changed files with 42 additions and 23 deletions

View File

@ -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 {

View File

@ -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.

View File

@ -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;

View File

@ -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>>);

View File

@ -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.])

View File

@ -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.

View File

@ -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.]