diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-01-24 12:44:04 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-01-24 12:44:04 +0100 |
| commit | 03fddaf3aea778057aedd74dbcb27bae971ec22f (patch) | |
| tree | 37e3136e29e0e5d69ec8f56e43d156739d2931ab /src/library/font.rs | |
| parent | 78da2bdd5d77d1b8572e5e9da119bfa68127a3fa (diff) | |
Non-fatal argument parsing 🌋
Diffstat (limited to 'src/library/font.rs')
| -rw-r--r-- | src/library/font.rs | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/src/library/font.rs b/src/library/font.rs new file mode 100644 index 00000000..b4a6218a --- /dev/null +++ b/src/library/font.rs @@ -0,0 +1,104 @@ +use toddle::query::{FontWeight, FontStyle}; +use super::*; + + +function! { + /// `font.family`: Set the font family. + #[derive(Debug, Clone, PartialEq)] + pub struct FontFamilyFunc { + body: Option<SyntaxModel>, + list: Vec<String>, + } + + parse(header, body, ctx, errors, decos) { + FontFamilyFunc { + body: body!(opt: body, ctx, errors, decos), + list: header.args.pos.get_all::<StringLike>(errors).collect(), + } + } + + layout(self, ctx, errors) { + styled(&self.body, ctx, Some(&self.list), + |s, l| s.fallback.list = l.clone()) + } +} + +function! { + /// `font.style`: Set the font style (normal / italic). + #[derive(Debug, Clone, PartialEq)] + pub struct FontStyleFunc { + body: Option<SyntaxModel>, + style: Option<FontStyle>, + } + + parse(header, body, ctx, errors, decos) { + FontStyleFunc { + body: body!(opt: body, ctx, errors, decos), + style: header.args.pos.get::<FontStyle>(errors) + .or_missing(errors, header.name.span, "style"), + } + } + + layout(self, ctx, errors) { + styled(&self.body, ctx, self.style, |t, s| t.variant.style = s) + } +} + +function! { + /// `font.weight`: Set text with a given weight. + #[derive(Debug, Clone, PartialEq)] + pub struct FontWeightFunc { + body: Option<SyntaxModel>, + weight: Option<FontWeight>, + } + + parse(header, body, ctx, errors, decos) { + let body = body!(opt: body, ctx, errors, decos); + let weight = header.args.pos.get::<Spanned<FontWeight>>(errors) + .map(|Spanned { v: (weight, is_clamped), span }| { + if is_clamped { + errors.push(err!(@Warning: span; + "weight should be between \ + 100 and 900, clamped to {}", weight.0)); + } + + weight + }) + .or_missing(errors, header.name.span, "weight"); + + FontWeightFunc { body, weight } + } + + layout(self, ctx, errors) { + styled(&self.body, ctx, self.weight, |t, w| t.variant.weight = w) + } +} + +function! { + /// `font.size`: Sets the font size. + #[derive(Debug, Clone, PartialEq)] + pub struct FontSizeFunc { + body: Option<SyntaxModel>, + size: Option<ScaleSize>, + } + + parse(header, body, ctx, errors, decos) { + FontSizeFunc { + body: body!(opt: body, ctx, errors, decos), + size: header.args.pos.get::<FSize>(errors) + .or_missing(errors, header.name.span, "size") + } + } + + layout(self, ctx, errors) { + styled(&self.body, ctx, self.size, |t, s| { + match s { + ScaleSize::Absolute(size) => { + t.base_font_size = size; + t.font_scale = 1.0; + } + ScaleSize::Scaled(scale) => t.font_scale = scale, + } + }) + } +} |
