floem_renderer/text/
attrs.rs

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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use std::ops::Range;

use crate::text::{fontdb, Family, Stretch, Style, Weight};
use peniko::Color;

/// An owned version of [`Family`]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum FamilyOwned {
    Name(String),
    Serif,
    SansSerif,
    Cursive,
    Fantasy,
    Monospace,
}

impl FamilyOwned {
    pub fn new(family: Family) -> Self {
        match family {
            Family::Name(name) => FamilyOwned::Name(name.to_string()),
            Family::Serif => FamilyOwned::Serif,
            Family::SansSerif => FamilyOwned::SansSerif,
            Family::Cursive => FamilyOwned::Cursive,
            Family::Fantasy => FamilyOwned::Fantasy,
            Family::Monospace => FamilyOwned::Monospace,
        }
    }

    pub fn as_family(&self) -> Family {
        match self {
            FamilyOwned::Name(name) => Family::Name(name),
            FamilyOwned::Serif => Family::Serif,
            FamilyOwned::SansSerif => Family::SansSerif,
            FamilyOwned::Cursive => Family::Cursive,
            FamilyOwned::Fantasy => Family::Fantasy,
            FamilyOwned::Monospace => Family::Monospace,
        }
    }

    pub fn parse_list(s: &str) -> impl Iterator<Item = FamilyOwned> + '_ + Clone {
        ParseList {
            source: s.as_bytes(),
            len: s.len(),
            pos: 0,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LineHeightValue {
    Normal(f32),
    Px(f32),
}

/// Text attributes
#[derive(Clone, Debug)]
pub struct AttrsOwned {
    attrs: cosmic_text::AttrsOwned,
    pub font_size: f32,
    line_height: LineHeightValue,
}
impl AttrsOwned {
    pub fn new(attrs: Attrs) -> Self {
        Self {
            attrs: cosmic_text::AttrsOwned::new(attrs.attrs),
            font_size: attrs.font_size,
            line_height: attrs.line_height,
        }
    }

    pub fn as_attrs(&self) -> Attrs {
        Attrs {
            attrs: self.attrs.as_attrs(),
            font_size: self.font_size,
            line_height: self.line_height,
        }
    }
}

/// Text attributes
#[derive(Clone, Copy, Debug)]
pub struct Attrs<'a> {
    attrs: cosmic_text::Attrs<'a>,
    pub font_size: f32,
    line_height: LineHeightValue,
}

impl Default for Attrs<'_> {
    fn default() -> Self {
        Self::new()
    }
}

impl<'a> Attrs<'a> {
    /// Create a new set of attributes with sane defaults
    ///
    /// This defaults to a regular Sans-Serif font.
    pub fn new() -> Self {
        Self {
            attrs: cosmic_text::Attrs::new(),
            font_size: 16.0,
            line_height: LineHeightValue::Normal(1.0),
        }
    }

    /// Set [Color]
    pub fn color(mut self, color: Color) -> Self {
        let c = color.to_rgba8();
        self.attrs = self
            .attrs
            .color(cosmic_text::Color::rgba(c.r, c.g, c.b, c.a));
        self
    }

    /// Set [Family]
    pub fn family(mut self, family: &'a [FamilyOwned]) -> Self {
        if let Some(family) = family.first() {
            self.attrs = self.attrs.family(family.as_family());
        }
        self
    }

    /// Set [Stretch]
    pub fn stretch(mut self, stretch: Stretch) -> Self {
        self.attrs = self.attrs.stretch(stretch);
        self
    }

    /// Set [Style]
    pub fn style(mut self, style: Style) -> Self {
        self.attrs = self.attrs.style(style);
        self
    }

    /// Set [Weight]
    pub fn weight(mut self, weight: Weight) -> Self {
        self.attrs = self.attrs.weight(weight);
        self
    }

    /// Set Weight from u16 value
    pub fn raw_weight(mut self, weight: u16) -> Self {
        self.attrs = self.attrs.weight(Weight(weight));
        self
    }

    fn get_metrics(&self) -> cosmic_text::Metrics {
        let line_height = match self.line_height {
            LineHeightValue::Normal(n) => self.font_size * n,
            LineHeightValue::Px(n) => n,
        };
        cosmic_text::Metrics::new(self.font_size, line_height)
    }

    /// Set font size
    pub fn font_size(mut self, font_size: f32) -> Self {
        self.font_size = font_size;
        self.attrs = self.attrs.metrics(self.get_metrics());
        self
    }

    /// Set line height
    pub fn line_height(mut self, line_height: LineHeightValue) -> Self {
        self.line_height = line_height;
        self.attrs = self.attrs.metrics(self.get_metrics());
        self
    }

    /// Set metadata
    pub fn metadata(mut self, metadata: usize) -> Self {
        self.attrs = self.attrs.metadata(metadata);
        self
    }

    /// Check if font matches
    pub fn matches(&self, face: &fontdb::FaceInfo) -> bool {
        self.attrs.matches(face)
    }

    /// Check if this set of attributes can be shaped with another
    pub fn compatible(&self, other: &Self) -> bool {
        self.attrs.compatible(&other.attrs)
    }
}

#[derive(PartialEq, Clone)]
pub struct AttrsList(pub cosmic_text::AttrsList);

impl AttrsList {
    /// Create a new attributes list with a set of default [Attrs]
    pub fn new(defaults: Attrs) -> Self {
        Self(cosmic_text::AttrsList::new(defaults.attrs))
    }

    /// Get the default [Attrs]
    pub fn defaults(&self) -> Attrs {
        self.0.defaults().into()
    }

    /// Clear the current attribute spans
    pub fn clear_spans(&mut self) {
        self.0.clear_spans();
    }

    /// Add an attribute span, removes any previous matching parts of spans
    pub fn add_span(&mut self, range: Range<usize>, attrs: Attrs) {
        self.0.add_span(range, attrs.attrs);
    }

    /// Get the attribute span for an index
    ///
    /// This returns a span that contains the index
    pub fn get_span(&self, index: usize) -> Attrs {
        self.0.get_span(index).into()
    }

    /// Split attributes list at an offset
    pub fn split_off(&mut self, index: usize) -> Self {
        let new = self.0.split_off(index);
        Self(new)
    }
}

impl<'a> From<cosmic_text::Attrs<'a>> for Attrs<'a> {
    fn from(attrs: cosmic_text::Attrs<'a>) -> Self {
        Self {
            attrs,
            font_size: 1.0,
            line_height: LineHeightValue::Normal(1.0),
        }
    }
}

#[derive(Clone)]
struct ParseList<'a> {
    source: &'a [u8],
    len: usize,
    pos: usize,
}

impl Iterator for ParseList<'_> {
    type Item = FamilyOwned;

    fn next(&mut self) -> Option<Self::Item> {
        let mut quote = None;
        let mut pos = self.pos;
        while pos < self.len && {
            let ch = self.source[pos];
            ch.is_ascii_whitespace() || ch == b','
        } {
            pos += 1;
        }
        self.pos = pos;
        if pos >= self.len {
            return None;
        }
        let first = self.source[pos];
        let mut start = pos;
        match first {
            b'"' | b'\'' => {
                quote = Some(first);
                pos += 1;
                start += 1;
            }
            _ => {}
        }
        if let Some(quote) = quote {
            while pos < self.len {
                if self.source[pos] == quote {
                    self.pos = pos + 1;
                    return Some(FamilyOwned::Name(
                        core::str::from_utf8(self.source.get(start..pos)?)
                            .ok()?
                            .trim()
                            .to_string(),
                    ));
                }
                pos += 1;
            }
            self.pos = pos;
            return Some(FamilyOwned::Name(
                core::str::from_utf8(self.source.get(start..pos)?)
                    .ok()?
                    .trim()
                    .to_string(),
            ));
        }
        let mut end = start;
        while pos < self.len {
            if self.source[pos] == b',' {
                pos += 1;
                break;
            }
            pos += 1;
            end += 1;
        }
        self.pos = pos;
        let name = core::str::from_utf8(self.source.get(start..end)?)
            .ok()?
            .trim();
        Some(match name.to_lowercase().as_str() {
            "serif" => FamilyOwned::Serif,
            "sans-serif" => FamilyOwned::SansSerif,
            "monospace" => FamilyOwned::Monospace,
            "cursive" => FamilyOwned::Cursive,
            "fantasy" => FamilyOwned::Fantasy,
            _ => FamilyOwned::Name(name.to_string()),
        })
    }
}