diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-02-09 19:46:57 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-02-09 19:46:57 +0100 |
| commit | 06ca740d01b428f12f6bd327257cd05dce737b03 (patch) | |
| tree | 995bf8ff3a606aedecf296c9e805e11e9cd0ae8e /src/library/style.rs | |
| parent | e35bbfffcb1f84b2fb0679759152ca0a5eabfad4 (diff) | |
Split evaluation and execution 🔪
Diffstat (limited to 'src/library/style.rs')
| -rw-r--r-- | src/library/style.rs | 113 |
1 files changed, 62 insertions, 51 deletions
diff --git a/src/library/style.rs b/src/library/style.rs index 670104d6..23bd5298 100644 --- a/src/library/style.rs +++ b/src/library/style.rs @@ -55,68 +55,71 @@ use crate::prelude::*; /// - `extra-expanded` /// - `ultra-expanded` pub fn font(ctx: &mut EvalContext, args: &mut Args) -> Value { - let snapshot = ctx.state.clone(); - - if let Some(linear) = args.find::<Linear>(ctx) { - if linear.rel.is_zero() { - ctx.state.font.size = linear.abs; - ctx.state.font.scale = Relative::ONE.into(); - } else { - ctx.state.font.scale = linear; - } - } - + let size = args.find::<Linear>(ctx); let list: Vec<_> = args.filter::<FontFamily>(ctx).map(|f| f.to_string()).collect(); - if !list.is_empty() { - let families = ctx.state.font.families_mut(); - families.list = list; - families.flatten(); - } + let style = args.get(ctx, "style"); + let weight = args.get(ctx, "weight"); + let stretch = args.get(ctx, "stretch"); + let serif = args.get(ctx, "serif"); + let sans_serif = args.get(ctx, "sans-serif"); + let monospace = args.get(ctx, "monospace"); + let body = args.find::<ValueTemplate>(ctx); + + Value::template(move |ctx| { + let snapshot = ctx.state.clone(); + + if let Some(linear) = size { + if linear.rel.is_zero() { + ctx.state.font.size = linear.abs; + ctx.state.font.scale = Relative::ONE.into(); + } else { + ctx.state.font.scale = linear; + } + } - if let Some(style) = args.get(ctx, "style") { - ctx.state.font.variant.style = style; - } + if !list.is_empty() { + let families = ctx.state.font.families_mut(); + families.list = list.clone(); + families.flatten(); + } - if let Some(weight) = args.get(ctx, "weight") { - ctx.state.font.variant.weight = weight; - } + if let Some(style) = style { + ctx.state.font.variant.style = style; + } - if let Some(stretch) = args.get(ctx, "stretch") { - ctx.state.font.variant.stretch = stretch; - } + if let Some(weight) = weight { + ctx.state.font.variant.weight = weight; + } - for variant in FontFamily::VARIANTS { - if let Some(FontFamilies(list)) = args.get(ctx, variant.as_str()) { - let strings = list.into_iter().map(|f| f.to_string()).collect(); - let families = ctx.state.font.families_mut(); - families.update_class_list(variant.to_string(), strings); - families.flatten(); + if let Some(stretch) = stretch { + ctx.state.font.variant.stretch = stretch; } - } - if let Some(body) = args.find::<ValueTemplate>(ctx) { - body.eval(ctx); - ctx.state = snapshot; - } + for (variant, arg) in &[ + (FontFamily::Serif, &serif), + (FontFamily::SansSerif, &sans_serif), + (FontFamily::Monospace, &monospace), + ] { + if let Some(FontFamilies(list)) = arg { + let strings = list.into_iter().map(|f| f.to_string()).collect(); + let families = ctx.state.font.families_mut(); + families.update_class_list(variant.to_string(), strings); + families.flatten(); + } + } - Value::None + if let Some(body) = &body { + body.exec(ctx); + ctx.state = snapshot; + } + }) } /// A list of font families. #[derive(Debug, Clone, PartialEq)] struct FontFamilies(Vec<FontFamily>); -impl_type! { - FontFamilies: "font family or array of font families", - Value::Str(string) => Self(vec![FontFamily::Named(string.to_lowercase())]), - Value::Array(values) => Self(values - .into_iter() - .filter_map(|v| v.cast().ok()) - .collect() - ), - #(family: FontFamily) => Self(vec![family]), -} - +/// A single font family. #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)] pub(crate) enum FontFamily { Serif, @@ -126,9 +129,6 @@ pub(crate) enum FontFamily { } impl FontFamily { - pub const VARIANTS: &'static [Self] = - &[Self::Serif, Self::SansSerif, Self::Monospace]; - pub fn as_str(&self) -> &str { match self { Self::Serif => "serif", @@ -146,6 +146,17 @@ impl Display for FontFamily { } impl_type! { + FontFamilies: "font family or array of font families", + Value::Str(string) => Self(vec![FontFamily::Named(string.to_lowercase())]), + Value::Array(values) => Self(values + .into_iter() + .filter_map(|v| v.cast().ok()) + .collect() + ), + #(family: FontFamily) => Self(vec![family]), +} + +impl_type! { FontFamily: "font family", Value::Str(string) => Self::Named(string.to_lowercase()) } |
