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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
//! Styles for text and pages.
use toddle::query::FontClass;
use FontClass::*;
use crate::size::{Size, Size2D, SizeBox};
/// 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>,
/// The font size.
pub font_size: Size,
/// The word spacing (as a multiple of the font size).
pub word_spacing: f32,
/// The line spacing (as a multiple of the font size).
pub line_spacing: f32,
/// The paragraphs spacing (as a multiple of the font size).
pub paragraph_spacing: f32,
}
impl TextStyle {
/// 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 == FontClass::Italic || class == FontClass::Bold {
self.classes.retain(|x| x != &FontClass::Regular);
}
self.classes.push(class);
}
}
}
impl Default for TextStyle {
fn default() -> TextStyle {
TextStyle {
classes: vec![Regular],
fallback: vec![Serif],
font_size: Size::pt(10.0),
word_spacing: 0.25,
line_spacing: 1.2,
paragraph_spacing: 1.4,
}
}
}
/// Defines the size and margins of a page.
#[derive(Debug, Copy, Clone)]
pub struct PageStyle {
/// The 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),
},
}
}
}
|