diff options
| author | Laurenz <laurmaedje@gmail.com> | 2019-10-30 20:13:28 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2019-10-30 20:13:28 +0100 |
| commit | ccc4639c7d4dfe039d469d16236ac5ad121f4a07 (patch) | |
| tree | 950829c7bf8c5771bdf608c01a7a7b6b6614df56 /src/style.rs | |
| parent | b4be25e43b1ee9da924d13b7f2e8289f12bd2c3b (diff) | |
Improve documentation comments 📜
Diffstat (limited to 'src/style.rs')
| -rw-r--r-- | src/style.rs | 47 |
1 files changed, 30 insertions, 17 deletions
diff --git a/src/style.rs b/src/style.rs index 3bc1e33f..68bd9a27 100644 --- a/src/style.rs +++ b/src/style.rs @@ -1,22 +1,22 @@ -//! Layouting styles. +//! Styles for text and pages. use toddle::query::FontClass; - +use FontClass::*; use crate::size::{Size, Size2D, SizeBox}; -/// Default styles for text. +/// Defines which fonts to use and how to space text. #[derive(Debug, Clone)] pub struct TextStyle { - /// The classes the font we want has to be part of. + /// The classes the font has to be part of. pub classes: Vec<FontClass>, - /// A sequence of classes. We need the font to be part of at least one of - /// these and preferably the leftmost possible. + /// The fallback classes from which the font needs to match the + /// leftmost possible one. pub fallback: Vec<FontClass>, /// The font size. pub font_size: f32, /// The line spacing (as a multiple of the font size). pub line_spacing: f32, - /// The paragraphs spacing (as a multiple of the line spacing). + /// The paragraphs spacing (as a multiple of the font size). pub paragraph_spacing: f32, } @@ -24,21 +24,35 @@ impl TextStyle { /// Toggle a class. /// /// If the class was one of _italic_ or _bold_, then: - /// - If it was not present, the _regular_ class will be removed. - /// - If it was present, the _regular_ class will be added in case the other + /// - 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) { - self.classes.retain(|x| x != &class); - if (class == FontClass::Italic && !self.classes.contains(&FontClass::Bold)) - || (class == FontClass::Bold && !self.classes.contains(&FontClass::Italic)) - { - self.classes.push(FontClass::Regular); + // 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 == FontClass::Italic || class == FontClass::Bold { self.classes.retain(|x| x != &FontClass::Regular); } + self.classes.push(class); } } @@ -46,7 +60,6 @@ impl TextStyle { impl Default for TextStyle { fn default() -> TextStyle { - use FontClass::*; TextStyle { classes: vec![Regular], fallback: vec![Serif], @@ -57,10 +70,10 @@ impl Default for TextStyle { } } -/// Default styles for pages. +/// Defines the size and margins of a page. #[derive(Debug, Clone)] pub struct PageStyle { - /// Width and height of the page. + /// The width and height of the page. pub dimensions: Size2D, /// The amount of white space on each side. pub margins: SizeBox, |
