diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-03-22 14:08:50 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-03-22 14:08:50 +0100 |
| commit | 98336bfafb947f0b4d55a79c422b915bb417c185 (patch) | |
| tree | bf0ead89a3d7eed3d8bb87d013c2a813633c75e4 /src/library | |
| parent | 39f55481ed7bc5ebc6d310924e90e7b6c0760d3b (diff) | |
Better font family definitions ✒
Diffstat (limited to 'src/library')
| -rw-r--r-- | src/library/font.rs | 72 | ||||
| -rw-r--r-- | src/library/mod.rs | 2 |
2 files changed, 22 insertions, 52 deletions
diff --git a/src/library/font.rs b/src/library/font.rs index ed2c0ef3..6afc617b 100644 --- a/src/library/font.rs +++ b/src/library/font.rs @@ -17,9 +17,9 @@ use super::*; /// - Top edge of the font: `top-edge`, of type `vertical-font-metric`. /// - Bottom edge of the font: `bottom-edge`, of type `vertical-font-metric`. /// - Color the glyphs: `color`, of type `color`. -/// - Serif family definition: `serif`, of type `font-familiy-list`. -/// - Sans-serif family definition: `sans-serif`, of type `font-familiy-list`. -/// - Monospace family definition: `monospace`, of type `font-familiy-list`. +/// - Serif family definition: `serif`, of type `font-family-definition`. +/// - Sans-serif family definition: `sans-serif`, of type `font-family-definition`. +/// - Monospace family definition: `monospace`, of type `font-family-definition`. /// /// # Return value /// A template that configures font properties. The effect is scoped to the body @@ -31,10 +31,9 @@ use super::*; /// - `sans-serif` /// - `monospace` /// - coerces from `string` -/// - Type `font-family-list` +/// - Type `font-family-definition` /// - coerces from `string` /// - coerces from `array` -/// - coerces from `font-family` /// - Type `font-style` /// - `normal` /// - `italic` @@ -58,7 +57,7 @@ use super::*; /// - `descender` pub fn font(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { let size = args.find::<Linear>(ctx); - let list: Vec<_> = args.filter::<FontFamily>(ctx).map(|f| f.to_string()).collect(); + let list: Vec<_> = args.filter::<FontFamily>(ctx).collect(); let style = args.get(ctx, "style"); let weight = args.get(ctx, "weight"); let stretch = args.get(ctx, "stretch"); @@ -83,9 +82,7 @@ pub fn font(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { } if !list.is_empty() { - let families = ctx.state.font.families_mut(); - families.list = list.clone(); - families.flatten(); + ctx.state.font.families_mut().list = list.clone(); } if let Some(style) = style { @@ -112,17 +109,16 @@ pub fn font(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { ctx.state.font.color = Fill::Color(color); } - 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(); - } + if let Some(FontFamilies(serif)) = &serif { + ctx.state.font.families_mut().serif = serif.clone(); + } + + if let Some(FontFamilies(sans_serif)) = &sans_serif { + ctx.state.font.families_mut().sans_serif = sans_serif.clone(); + } + + if let Some(FontFamilies(monospace)) = &monospace { + ctx.state.font.families_mut().monospace = monospace.clone(); } if let Some(body) = &body { @@ -132,45 +128,19 @@ pub fn font(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { }) } -/// A list of font families. +/// A list of font family names. #[derive(Debug, Clone, PartialEq)] -struct FontFamilies(Vec<FontFamily>); - -/// A single font family. -#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)] -pub(super) enum FontFamily { - Serif, - SansSerif, - Monospace, - Named(String), -} - -impl FontFamily { - pub fn as_str(&self) -> &str { - match self { - Self::Serif => "serif", - Self::SansSerif => "sans-serif", - Self::Monospace => "monospace", - Self::Named(s) => s, - } - } -} - -impl Display for FontFamily { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - f.pad(self.as_str()) - } -} +struct FontFamilies(Vec<String>); typify! { - FontFamilies: "font family or array of font families", - Value::Str(string) => Self(vec![FontFamily::Named(string.to_lowercase())]), + FontFamilies: "string or array of strings", + Value::Str(string) => Self(vec![string.to_lowercase()]), Value::Array(values) => Self(values .into_iter() .filter_map(|v| v.cast().ok()) + .map(|string: String| string.to_lowercase()) .collect() ), - #(family: FontFamily) => Self(vec![family]), } typify! { diff --git a/src/library/mod.rs b/src/library/mod.rs index 62640ef4..58e62d56 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -31,7 +31,7 @@ use fontdock::{FontStyle, FontWeight}; use crate::eval::{AnyValue, FuncValue, Scope}; use crate::eval::{EvalContext, FuncArgs, TemplateValue, Value}; -use crate::exec::{Exec, ExecContext}; +use crate::exec::{Exec, ExecContext, FontFamily}; use crate::geom::*; use crate::layout::VerticalFontMetric; use crate::syntax::{Node, Spanned}; |
