diff options
| author | Laurenz <laurmaedje@gmail.com> | 2019-10-10 23:36:17 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2019-10-10 23:38:03 +0200 |
| commit | 8f788f9a4f5e970bbe6147987b711470d57aca8d (patch) | |
| tree | cc89008dfdfc62ecf4eb2517d92ec7c7095fa573 /src/func.rs | |
| parent | 61470fba68e9f19b481034427add5f3d8cfbc0a8 (diff) | |
Add standard `align` function and support right-alignment ➡️
Diffstat (limited to 'src/func.rs')
| -rw-r--r-- | src/func.rs | 72 |
1 files changed, 4 insertions, 68 deletions
diff --git a/src/func.rs b/src/func.rs index c8e464f8..776f0e98 100644 --- a/src/func.rs +++ b/src/func.rs @@ -3,12 +3,10 @@ use std::any::Any; use std::collections::HashMap; use std::fmt::{self, Debug, Formatter}; -use toddle::query::FontClass; -use crate::layout::{layout, Layout, LayoutContext, LayoutResult}; -use crate::layout::flex::FlexLayout; -use crate::parsing::{parse, ParseContext, ParseError, ParseResult}; -use crate::syntax::{SyntaxTree, FuncHeader}; +use crate::layout::{Layout, LayoutContext, LayoutResult}; +use crate::parsing::{ParseContext, ParseResult}; +use crate::syntax::FuncHeader; /// Typesetting function types. @@ -79,11 +77,7 @@ impl Scope { /// Create a new scope with the standard functions contained. pub fn with_std() -> Scope { - let mut std = Scope::new(); - std.add::<BoldFunc>("bold"); - std.add::<ItalicFunc>("italic"); - std.add::<MonospaceFunc>("mono"); - std + crate::library::std() } /// Add a function type to the scope giving it a name. @@ -108,61 +102,3 @@ impl Debug for Scope { write!(f, "{:?}", self.parsers.keys()) } } - -/// Creates style functions like bold and italic. -macro_rules! style_func { - ($(#[$outer:meta])* pub struct $struct:ident { $name:expr }, - $style:ident => $style_change:block) => { - $(#[$outer])* - #[derive(Debug, PartialEq)] - pub struct $struct { body: SyntaxTree } - impl Function for $struct { - fn parse(header: &FuncHeader, body: Option<&str>, ctx: ParseContext) - -> ParseResult<Self> where Self: Sized { - // Accept only invocations without arguments and with body. - if header.args.is_empty() && header.kwargs.is_empty() { - if let Some(body) = body { - Ok($struct { body: parse(body, ctx)? }) - } else { - Err(ParseError::new(format!("expected body for function `{}`", $name))) - } - } else { - Err(ParseError::new(format!("unexpected arguments to function `{}`", $name))) - } - } - - fn layout(&self, ctx: LayoutContext) -> LayoutResult<Option<Layout>> { - // Change the context. - let mut $style = ctx.style.clone(); - $style_change - - // Create a box and put it into a flex layout. - let boxed = layout(&self.body, LayoutContext { - style: &$style, - .. ctx - })?; - let flex = FlexLayout::from_box(boxed); - - Ok(Some(Layout::Flex(flex))) - } - } - }; -} - -style_func! { - /// Typesets text in bold. - pub struct BoldFunc { "bold" }, - style => { style.toggle_class(FontClass::Bold) } -} - -style_func! { - /// Typesets text in italics. - pub struct ItalicFunc { "italic" }, - style => { style.toggle_class(FontClass::Italic) } -} - -style_func! { - /// Typesets text in monospace. - pub struct MonospaceFunc { "mono" }, - style => { style.toggle_class(FontClass::Monospace) } -} |
