//! The _Typst_ standard library. use crate::func::prelude::*; use crate::layout::{LayoutContext, Commands}; use crate::syntax::scope::Scope; macro_rules! lib { ($name:ident) => { mod $name; pub use $name::*; }} lib!(font); lib!(layout); lib!(page); lib!(spacing); /// Create a scope with all standard functions. pub fn std() -> Scope { let mut std = Scope::new::(); std.add::("val"); std.add::("font"); std.add::("page"); std.add::("align"); std.add::("box"); std.add::("parbreak"); std.add::("pagebreak"); std.add_with_meta::("h", Horizontal); std.add_with_meta::("v", Vertical); std } function! { /// `val`: Layouts the body with no special effect. #[derive(Debug, Clone, PartialEq)] pub struct ValFunc { body: Option, } parse(header, body, state, f) { header.args.pos.0.clear(); header.args.key.0.clear(); ValFunc { body: body!(opt: body, state, f) } } layout(self, ctx, f) { match &self.body { Some(tree) => vec![LayoutSyntaxTree(tree)], None => vec![], } } } /// Layout an optional body with a change of the text style. fn styled<'a, T, F>( body: &'a Option, ctx: LayoutContext<'_>, data: Option, f: F, ) -> Commands<'a> where F: FnOnce(&mut TextStyle, T) { if let Some(data) = data { let mut style = ctx.style.text.clone(); f(&mut style, data); match body { Some(tree) => vec![ SetTextStyle(style), LayoutSyntaxTree(tree), SetTextStyle(ctx.style.text.clone()), ], None => vec![SetTextStyle(style)], } } else { vec![] } }