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))); 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 /// Build the stack node resulting from instantiating the template in the
/// given state. /// given state.
pub fn to_stack(&self, state: &State) -> StackNode { 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(); let mut template = Template::new();
template.save(); template.save();
realign(&mut template); realign(&mut template);
template += body; template += body;
template.restore(); template.restore();
Ok(Value::Template(template)) Value::Template(template)
} else { } else {
realign(&mut ctx.template); realign(&mut ctx.template);
Ok(Value::None) Value::None
} })
} }
/// `box`: Place content in a rectangular box. /// `box`: Place content in a rectangular box.

View File

@ -18,7 +18,7 @@ use std::rc::Rc;
use crate::color::{Color, RgbaColor}; use crate::color::{Color, RgbaColor};
use crate::diag::TypResult; 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::font::{FontFamily, FontStretch, FontStyle, FontWeight, VerticalFontMetric};
use crate::geom::*; use crate::geom::*;
use crate::layout::LayoutNode; 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 sans_serif = args.named("sans-serif")?;
let monospace = args.named("monospace")?; let monospace = args.named("monospace")?;
let fallback = args.named("fallback")?; 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(); let font = state.font_mut();
if let Some(size) = size { 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 { if let Some(fallback) = fallback {
font.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>>); struct FontDef(Rc<Vec<FontFamily>>);

View File

@ -13,7 +13,7 @@
#underline(red)[Critical information is conveyed here.] #underline(red)[Critical information is conveyed here.]
// Inherits font color. // Inherits font color.
[#font(fill: red) #underline[Change with the wind.]] #font(fill: red, underline[Change with the wind.])
// Both over- and underline. // Both over- and underline.
#overline(underline[Running amongst the wolves.]) #overline(underline[Running amongst the wolves.])

View File

@ -2,35 +2,35 @@
--- ---
// Set same font size in three different ways. // Set same font size in three different ways.
[#font(22pt) A] #font(22pt)[A]
[#font(200%) A] #font(200%)[A]
[#font(size: 16.5pt + 50%) A] #font(size: 16.5pt + 50%)[A]
// Do nothing. // Do nothing.
[#font() Normal] #font()[Normal]
// Set style (is available). // Set style (is available).
[#font(style: italic) Italic] #font(style: italic)[Italic]
// Set weight (is available). // Set weight (is available).
[#font(weight: bold) Bold] #font(weight: bold)[Bold]
// Set stretch (not available, matching closest). // Set stretch (not available, matching closest).
[#font(stretch: 50%) Condensed] #font(stretch: 50%)[Condensed]
// Set family. // Set family.
[#font(family: "PT Sans") Sans serif] #font(family: "PT Sans")[Sans serif]
// Emoji. // Emoji.
Emoji: 🐪, 🌋, 🏞 Emoji: 🐪, 🌋, 🏞
// Math. // Math.
[#font("Latin Modern Math") 𝛼 + 3𝛽 d𝑡] #font("Latin Modern Math")[ 𝛼 + 3𝛽 d𝑡]
// Colors. // Colors.
[ [
#font(fill: eastern) #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. // Disable font fallback beyond the user-specified list.
@ -41,9 +41,9 @@ Emoji: 🐪, 🌋, 🏞
--- ---
// Test class definitions. // Test class definitions.
#font(sans-serif: "PT Sans") #font(sans-serif: "PT Sans")
[#font(family: sans-serif) Sans-serif.] \ #font(family: sans-serif)[Sans-serif.] \
[#font(monospace) Monospace.] \ #font(monospace)[Monospace.] \
[#font(monospace, monospace: ("Nope", "Latin Modern Math")) Math.] #font(monospace, monospace: ("Nope", "Latin Modern Math"))[Math.]
--- ---
// Test top and bottom edge. // Test top and bottom edge.

View File

@ -8,5 +8,5 @@
This link appears #link("https://google.com/")[in the middle of] a paragraph. This link appears #link("https://google.com/")[in the middle of] a paragraph.
// Styled with underline and color. // 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.] You could also make the #link("https://html5zombo.com/")[link look way more typical.]