diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-01-04 22:43:26 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-01-04 22:43:26 +0100 |
| commit | 7b84f3b553de672e5374e142467f63b10009aeca (patch) | |
| tree | fd8265abc8c62213520628babc0cfc3a6f8e98aa /src/style.rs | |
| parent | 5dfaffc5bdfa811c135f0140c0a0ba917eb8c70f (diff) | |
Adopt new font loading engine ⚙
Diffstat (limited to 'src/style.rs')
| -rw-r--r-- | src/style.rs | 71 |
1 files changed, 28 insertions, 43 deletions
diff --git a/src/style.rs b/src/style.rs index df5e13d1..ffa10d51 100644 --- a/src/style.rs +++ b/src/style.rs @@ -1,11 +1,11 @@ //! Styles for text and pages. -use toddle::query::FontClass; -use FontClass::*; +use toddle::query::{FontFallbackTree, FontVariant, FontStyle, FontWeight}; use crate::size::{Size, Size2D, SizeBox, ValueBox, PSize}; use crate::syntax::ParseResult; + /// Defines properties of pages and text. #[derive(Debug, Default, Clone)] pub struct LayoutStyle { @@ -16,11 +16,10 @@ pub struct LayoutStyle { /// Defines which fonts to use and how to space text. #[derive(Debug, Clone)] pub struct TextStyle { - /// The classes the font has to be part of. - pub classes: Vec<FontClass>, - /// The fallback classes from which the font needs to match the leftmost - /// possible one. - pub fallback: Vec<FontClass>, + /// A tree of font names and generic family names. + pub fallback: FontFallbackTree, + /// The selected font variant. + pub variant: FontVariant, /// The base font size. pub base_font_size: Size, /// The font scale to apply on the base font size. @@ -53,48 +52,34 @@ impl TextStyle { pub fn paragraph_spacing(&self) -> Size { (self.paragraph_spacing_scale - 1.0) * self.font_size() } +} - /// Toggle a class. - /// - /// If the class was one of _italic_ or _bold_, then: - /// - If it was not present before, the _regular_ class will be removed. - /// - If it was present before, the _regular_ class will be added in case - /// the other style class is not present. - pub fn toggle_class(&mut self, class: FontClass) { - if self.classes.contains(&class) { - // If we retain a Bold or Italic class, we will not add the Regular - // class. - let mut regular = true; - self.classes.retain(|x| { - if class == *x { - false - } else { - if class == Bold || class == Italic { - regular = false; - } - true - } - }); - - if regular { - self.classes.push(Regular); - } - } else { - // If we add an Italic or Bold class, we remove the Regular class. - if class == Italic || class == Bold { - self.classes.retain(|x| x != &Regular); - } - - self.classes.push(class); - } - } +macro_rules! fallback { + (($($f:expr),*), $($c:expr => ($($cf:expr),*)),*) => ({ + let mut fallback = FontFallbackTree::new(vec![$($f.to_string()),*]); + $( + fallback.set_class_list($c.to_string(), vec![$($cf.to_string()),*]) + .expect("TextStyle::default: unexpected error \ + when setting class list"); + )* + fallback + }); } impl Default for TextStyle { fn default() -> TextStyle { TextStyle { - classes: vec![Regular], - fallback: vec![Serif], + fallback: fallback! { + ("sans-serif"), + "serif" => ("source serif pro", "noto serif", "noto emoji"), + "sans-serif" => ("source sans pro", "noto sans", "noto emoji"), + "monospace" => ("source code pro", "noto sans mono", "noto emoji"), + "math" => ("latin modern math", "serif") + }, + variant: FontVariant { + style: FontStyle::Normal, + weight: FontWeight(400), + }, base_font_size: Size::pt(11.0), font_scale: 1.0, word_spacing_scale: 0.25, |
