summaryrefslogtreecommitdiff
path: root/src/style.rs
blob: 3bc1e33f27525b72eeb08fa45c9e203ce0ec0b5c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//! Layouting styles.

use toddle::query::FontClass;

use crate::size::{Size, Size2D, SizeBox};

/// Default styles for text.
#[derive(Debug, Clone)]
pub struct TextStyle {
    /// 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).
    pub line_spacing: f32,
    /// The paragraphs spacing (as a multiple of the line spacing).
    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 FontClass::*;
        TextStyle {
            classes: vec![Regular],
            fallback: vec![Serif],
            font_size: 11.0,
            line_spacing: 1.2,
            paragraph_spacing: 1.5,
        }
    }
}

/// Default styles for pages.
#[derive(Debug, Clone)]
pub struct PageStyle {
    /// Width and height of the page.
    pub dimensions: Size2D,
    /// The amount of white space on each side.
    pub margins: SizeBox,
}

impl Default for PageStyle {
    fn default() -> PageStyle {
        PageStyle {
            // A4 paper.
            dimensions: Size2D {
                x: Size::mm(210.0),
                y: Size::mm(297.0),
            },

            // All the same margins.
            margins: SizeBox {
                left: Size::cm(2.5),
                top: Size::cm(2.5),
                right: Size::cm(2.5),
                bottom: Size::cm(2.5),
            },
        }
    }
}