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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
pub use fontdb::{Family, Stretch, Style, Weight};
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Color(pub u32);
impl Color {
pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
Self::rgba(r, g, b, 0xFF)
}
pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
Self(
((a as u32) << 24) |
((r as u32) << 16) |
((g as u32) << 8) |
(b as u32)
)
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Attrs<'a> {
pub color_opt: Option<Color>,
pub family: Family<'a>,
pub monospaced: bool,
pub stretch: Stretch,
pub style: Style,
pub weight: Weight,
}
impl<'a> Attrs<'a> {
pub fn new() -> Self {
Self {
color_opt: None,
family: Family::SansSerif,
monospaced: false,
stretch: Stretch::Normal,
style: Style::Normal,
weight: Weight::NORMAL,
}
}
pub fn color(mut self, color: Color) -> Self {
self.color_opt = Some(color);
self
}
pub fn family(mut self, family: Family<'a>) -> Self {
self.family = family;
self
}
pub fn monospaced(mut self, monospaced: bool) -> Self {
self.monospaced = monospaced;
self
}
pub fn stretch(mut self, stretch: Stretch) -> Self {
self.stretch = stretch;
self
}
pub fn style(mut self, style: Style) -> Self {
self.style = style;
self
}
pub fn weight(mut self, weight: Weight) -> Self {
self.weight = weight;
self
}
pub fn matches(&self, face: &fontdb::FaceInfo) -> bool {
face.post_script_name.contains("Emoji") ||
(
face.style == self.style &&
face.weight == self.weight &&
face.stretch == self.stretch &&
face.monospaced == self.monospaced
)
}
pub fn compatible(&self, other: &Self) -> bool {
self.family == other.family
&& self.monospaced == other.monospaced
&& self.stretch == other.stretch
&& self.style == other.style
&& self.weight == other.weight
}
}
#[derive(Eq, PartialEq)]
pub struct AttrsList<'a> {
defaults: Attrs<'a>,
spans: Vec<(usize, usize, Attrs<'a>)>,
}
impl<'a> AttrsList<'a> {
pub fn new(defaults: Attrs<'a>) -> Self {
Self {
defaults,
spans: Vec::new(),
}
}
pub fn defaults(&self) -> Attrs<'a> {
self.defaults
}
pub fn spans(&self) -> &Vec<(usize, usize, Attrs<'a>)> {
&self.spans
}
pub fn clear_spans(&mut self) {
self.spans.clear();
}
pub fn add_span(&mut self, start: usize, end: usize, attrs: Attrs<'a>) {
self.spans.push((start, end, attrs));
}
pub fn get_span(&self, start: usize, end: usize) -> Attrs<'a> {
let mut attrs = self.defaults;
for span in self.spans.iter() {
if start >= span.0 && end <= span.1 {
attrs = span.2;
}
}
attrs
}
}