summaryrefslogtreecommitdiff
path: root/src/library/styles.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2019-10-10 23:36:17 +0200
committerLaurenz <laurmaedje@gmail.com>2019-10-10 23:38:03 +0200
commit8f788f9a4f5e970bbe6147987b711470d57aca8d (patch)
treecc89008dfdfc62ecf4eb2517d92ec7c7095fa573 /src/library/styles.rs
parent61470fba68e9f19b481034427add5f3d8cfbc0a8 (diff)
Add standard `align` function and support right-alignment ➡️
Diffstat (limited to 'src/library/styles.rs')
-rw-r--r--src/library/styles.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/library/styles.rs b/src/library/styles.rs
new file mode 100644
index 00000000..c84b9fed
--- /dev/null
+++ b/src/library/styles.rs
@@ -0,0 +1,64 @@
+//! Basic style functions: bold, italic, monospace.
+
+use super::prelude::*;
+use toddle::query::FontClass;
+
+
+
+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) }
+}