summaryrefslogtreecommitdiff
path: root/src/style.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2019-06-22 15:32:19 +0200
committerLaurenz <laurmaedje@gmail.com>2019-06-22 15:32:19 +0200
commit099ce71aba54a40455b7ce35768c8fe003f7b16a (patch)
treed0a475e91967882d4608dea59ceb41c9a6232e07 /src/style.rs
parentc7ee2b393a369325b3578557e045f2ff94ceab8f (diff)
Unify font classes + By-value-contexts ⚖
Diffstat (limited to 'src/style.rs')
-rw-r--r--src/style.rs44
1 files changed, 32 insertions, 12 deletions
diff --git a/src/style.rs b/src/style.rs
index cd0a9579..042042bf 100644
--- a/src/style.rs
+++ b/src/style.rs
@@ -1,18 +1,17 @@
//! Styles for layouting.
-use crate::font::FontFamily;
+use crate::font::FontClass;
use crate::size::{Size, Size2D, SizeBox};
/// Default styles for text.
#[derive(Debug, Clone)]
pub struct TextStyle {
- /// A fallback list of font families to use.
- pub font_families: Vec<FontFamily>,
- /// Whether the font is in italics.
- pub italic: bool,
- /// Whether the font is bold.
- pub bold: bool,
+ /// The classes the font we want 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.
+ pub fallback: Vec<FontClass>,
/// The font size.
pub font_size: f32,
/// The line spacing (as a multiple of the font size).
@@ -21,14 +20,35 @@ pub struct TextStyle {
pub paragraph_spacing: f32,
}
+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 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);
+ }
+ } else {
+ if class == FontClass::Italic || class == FontClass::Bold {
+ self.classes.retain(|x| x != &FontClass::Regular);
+ }
+ self.classes.push(class);
+ }
+ }
+}
+
impl Default for TextStyle {
fn default() -> TextStyle {
- use FontFamily::*;
+ use FontClass::*;
TextStyle {
- // Default font family, font size and line spacing.
- font_families: vec![Serif, SansSerif, Monospace],
- italic: false,
- bold: false,
+ classes: vec![Regular],
+ fallback: vec![Serif],
font_size: 11.0,
line_spacing: 1.2,
paragraph_spacing: 1.5,