livesplit-core 0.13.0

livesplit-core is a library that provides a lot of functionality for creating a speedrun timer.
Documentation
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! An optional path based text engine is provided that allows you to create
//! fonts and manage text labels that are based on paths. That way the
//! underlying renderer doesn't by itself need to be able to render text, as all
//! the text gets turned into paths.

use crate::platform::{prelude::*, Arc, RwLock};

#[cfg(feature = "font-loading")]
use font_kit::{
    family_name::FamilyName,
    handle::Handle,
    properties::{Properties, Stretch, Style, Weight},
    source::SystemSource,
};
use hashbrown::HashMap;
use rustybuzz::{
    ttf_parser::{GlyphId, OutlineBuilder},
    Face, Feature, Tag, UnicodeBuffer, Variation,
};

use super::{
    font::{TEXT_FONT, TIMER_FONT},
    FontKind, PathBuilder, Rgba, SharedOwnership,
};
use crate::settings::{self, FontStretch, FontStyle, FontWeight};

use self::color_font::{iter_colored_glyphs, ColorTables};

mod color_font;

/// The path based text engine allows you to create fonts and manage text labels
/// that are based on paths. That way the underlying renderer doesn't by itself
/// need to be able to render text, as all the text gets turned into paths.
pub struct TextEngine {
    #[cfg(feature = "font-loading")]
    source: SystemSource,
    buffer: Option<UnicodeBuffer>,
}

impl Default for TextEngine {
    fn default() -> Self {
        Self::new()
    }
}

impl TextEngine {
    /// Creates a new path based text engine.
    pub fn new() -> Self {
        Self {
            #[cfg(feature = "font-loading")]
            source: SystemSource::new(),
            buffer: None,
        }
    }

    /// Creates a new font. You can call this directly from a
    /// [`ResourceAllocator`](super::ResourceAllocator).
    pub fn create_font<P>(
        &mut self,
        #[allow(unused)] font: Option<&settings::Font>,
        kind: FontKind,
    ) -> Font<P> {
        #[cfg(feature = "font-loading")]
        if let Some(font) = font {
            if let Some(font) = Font::try_load_font(&mut self.source, font, kind) {
                return font;
            }
        }
        #[cfg(not(feature = "font-loading"))]
        let _ = font;

        let (font_data, style, weight, stretch) = match kind {
            FontKind::Timer => (
                TIMER_FONT,
                FontStyle::Normal,
                FontWeight::Bold,
                FontStretch::Normal,
            ),
            FontKind::Times => (
                TEXT_FONT,
                FontStyle::Normal,
                FontWeight::Bold,
                FontStretch::Normal,
            ),
            FontKind::Text => (
                TEXT_FONT,
                FontStyle::Normal,
                FontWeight::Normal,
                FontStretch::Normal,
            ),
        };
        Font::from_slice(font_data, 0, style, weight, stretch, kind).unwrap()
    }

    /// Creates a new text label. You can call this directly from a
    /// [`ResourceAllocator`](super::ResourceAllocator).
    pub fn create_label<PB: PathBuilder>(
        &mut self,
        path_builder: impl FnMut() -> PB,
        text: &str,
        font: &mut Font<PB::Path>,
        max_width: Option<f32>,
    ) -> Label<PB::Path> {
        let mut label = Arc::new(RwLock::new(LockedLabel {
            width: 0.0,
            width_without_max_width: 0.0,
            scale: 0.0,
            glyphs: Vec::new(),
        }));

        self.update_label(path_builder, &mut label, text, font, max_width);

        label
    }

    /// Updates a text label. You can call this directly from a
    /// [`ResourceAllocator`](super::ResourceAllocator).
    pub fn update_label<PB: PathBuilder>(
        &mut self,
        mut path_builder: impl FnMut() -> PB,
        label: &mut Label<PB::Path>,
        text: &str,
        font: &mut Font<PB::Path>,
        max_width: Option<f32>,
    ) {
        let mut label = label.write().unwrap();
        let label = &mut *label;

        let mut buffer = self.buffer.take().unwrap_or_else(UnicodeBuffer::new);
        buffer.push_str(text);

        let features = font
            .monotonic
            .as_ref()
            .map(|m| &m.features[..])
            .unwrap_or_default();

        let buffer = rustybuzz::shape(&font.face, features, buffer);

        let iter = Iterator::zip(buffer.glyph_infos().iter(), buffer.glyph_positions().iter());

        let (mut x, mut y) = (0.0, 0.0);

        label.glyphs.clear();

        if let Some(monotonic) = &font.monotonic {
            iter.for_each(|(info, pos)| {
                let glyph = GlyphId(info.glyph_id as _);
                let layer_glyphs = font.glyph_cache.entry(glyph).or_insert_with(|| {
                    let mut glyphs = Vec::new();
                    iter_colored_glyphs(&font.color_tables, 0, glyph, |glyph, color| {
                        let mut builder = GlyphBuilder(path_builder());
                        font.face.outline_glyph(glyph, &mut builder);
                        let path = builder.0.finish();
                        glyphs.push((color.map(|c| c.to_array()), path));
                    });
                    glyphs
                });
                let (x_advance, x_offset) = if monotonic.digit_glyphs.contains(&glyph) {
                    (
                        monotonic.digit_width,
                        0.5 * (monotonic.digit_width - pos.x_advance as f32) + pos.x_offset as f32,
                    )
                } else {
                    (pos.x_advance as f32, pos.x_offset as f32)
                };
                let (glyph_x, glyph_y) = (x + x_offset, y + pos.y_offset as f32);
                x += x_advance;
                y += pos.y_advance as f32;
                label
                    .glyphs
                    .extend(layer_glyphs.iter().map(|(color, path)| Glyph {
                        color: *color,
                        x: glyph_x,
                        y: glyph_y,
                        path: path.share(),
                    }));
            });
        } else {
            iter.for_each(|(info, pos)| {
                let glyph = GlyphId(info.glyph_id as _);
                let layer_glyphs = font.glyph_cache.entry(glyph).or_insert_with(|| {
                    let mut glyphs = Vec::new();
                    iter_colored_glyphs(&font.color_tables, 0, glyph, |glyph, color| {
                        let mut builder = GlyphBuilder(path_builder());
                        font.face.outline_glyph(glyph, &mut builder);
                        let path = builder.0.finish();
                        glyphs.push((color.map(|c| c.to_array()), path));
                    });
                    glyphs
                });
                let (glyph_x, glyph_y) = (x + pos.x_offset as f32, y + pos.y_offset as f32);
                x += pos.x_advance as f32;
                y += pos.y_advance as f32;
                label
                    .glyphs
                    .extend(layer_glyphs.iter().map(|(color, path)| Glyph {
                        color: *color,
                        x: glyph_x,
                        y: glyph_y,
                        path: path.share(),
                    }));
            });
        };

        label.width_without_max_width = x * font.scale_factor;

        if let Some(max_width) = max_width {
            let max_width = max_width / font.scale_factor;
            if x > max_width {
                let (ellipsis, ellipsis_width) = font.ellipsis;

                let x_to_look_for = max_width - ellipsis_width;

                let last_index = label
                    .glyphs
                    .iter()
                    .enumerate()
                    .rfind(|(_, g)| {
                        x = g.x;
                        y = g.y;
                        g.x <= x_to_look_for
                    })
                    .map(|(i, _)| i)
                    .unwrap_or_default();
                label.glyphs.drain(last_index..);
                let layer_glyphs = font.glyph_cache.entry(ellipsis).or_insert_with(|| {
                    let mut glyphs = Vec::new();
                    iter_colored_glyphs(&font.color_tables, 0, ellipsis, |glyph, color| {
                        let mut builder = GlyphBuilder(path_builder());
                        font.face.outline_glyph(glyph, &mut builder);
                        let path = builder.0.finish();
                        glyphs.push((color.map(|c| c.to_array()), path));
                    });
                    glyphs
                });
                label
                    .glyphs
                    .extend(layer_glyphs.iter().map(|(color, path)| Glyph {
                        color: *color,
                        x,
                        y,
                        path: path.share(),
                    }));
                x += ellipsis_width;
            }
        }

        self.buffer = Some(buffer.clear());

        label.width = x * font.scale_factor;
        label.scale = font.scale_factor;
    }
}

struct MonotonicInfo {
    digit_glyphs: [GlyphId; 10],
    digit_width: f32,
    features: [Feature; 1],
}

/// The font to use in the [`ResourceAllocator`](super::ResourceAllocator).
pub struct Font<P> {
    face: Face<'static>,
    color_tables: Option<ColorTables<'static>>,
    scale_factor: f32,
    monotonic: Option<MonotonicInfo>,
    /// The `GlyphId` and width of `…`.
    ellipsis: (GlyphId, f32),
    glyph_cache: HashMap<GlyphId, Vec<(Option<Rgba>, P)>>,
    #[cfg(feature = "font-loading")]
    _buf: Option<Box<[u8]>>,
}

impl<P> Font<P> {
    #[cfg(feature = "font-loading")]
    fn try_load_font(
        source: &mut SystemSource,
        font: &settings::Font,
        kind: FontKind,
    ) -> Option<Self> {
        let handle = source
            .select_best_match(
                &[FamilyName::Title(font.family.clone())],
                &Properties {
                    style: match font.style {
                        FontStyle::Normal => Style::Normal,
                        FontStyle::Italic => Style::Italic,
                    },
                    weight: Weight(font.weight.value()),
                    stretch: Stretch(font.stretch.factor()),
                },
            )
            .ok()?;

        let (buf, font_index) = match handle {
            Handle::Path { path, font_index } => (std::fs::read(path).ok()?, font_index),
            Handle::Memory { bytes, font_index } => (
                Arc::try_unwrap(bytes).unwrap_or_else(|bytes| (*bytes).clone()),
                font_index,
            ),
        };
        let buf = buf.into_boxed_slice();

        // Safety: We store our own buffer. If we never modify it and drop it
        // last, this is fine. It also needs to be heap allocated, so it's a
        // stable pointer. This is guaranteed by the boxed slice.
        unsafe {
            let slice: *const [u8] = &*buf;
            let mut font = Font::from_slice(
                &*slice,
                font_index,
                font.style,
                font.weight,
                font.stretch,
                kind,
            )?;
            font._buf = Some(buf);
            Some(font)
        }
    }

    fn from_slice(
        data: &'static [u8],
        index: u32,
        style: FontStyle,
        weight: FontWeight,
        stretch: FontStretch,
        kind: FontKind,
    ) -> Option<Self> {
        let mut face = Face::from_slice(data, index)?;

        let italic = style.value_for_italic();
        let weight = weight.value();
        let stretch = stretch.percentage();

        face.set_variations(&[
            Variation {
                tag: Tag::from_bytes(b"ital"),
                value: italic,
            },
            Variation {
                tag: Tag::from_bytes(b"wght"),
                value: weight,
            },
            Variation {
                tag: Tag::from_bytes(b"wdth"),
                value: stretch,
            },
        ]);

        let monotonic = kind.is_monospaced().then(|| {
            let mut digit_glyphs = [GlyphId(0); 10];
            let mut digit_width = 0;
            for (digit, glyph) in digit_glyphs.iter_mut().enumerate() {
                let (glyph_id, width) =
                    glyph_width(&face, char::from(digit as u8 + b'0')).unwrap_or_default();
                *glyph = glyph_id;
                if width > digit_width {
                    digit_width = width;
                }
            }
            MonotonicInfo {
                digit_glyphs,
                digit_width: digit_width as f32,
                features: [
                    // If the font has support for tabular numbers, we want to
                    // use it, so we don't have to fix up much. Though we still
                    // attempt to do so anyway, as we can neither query if tnum
                    // support is even available, nor can we really trust it all
                    // too much.
                    Feature::new(Tag::from_bytes(b"tnum"), 1, ..),
                    // FIXME: We may or may not want to disable kerning and
                    // possibly ligatures too. If the font doesn't support tnum,
                    // then kerning for e.g. `.1` may cause inconsistent
                    // positioning.
                    // Feature::new(Tag::from_bytes(b"kern"), 0, ..),
                ],
            }
        });

        let (ellipsis, ellipsis_width) = glyph_width(&face, '').unwrap_or_default();

        Some(Self {
            scale_factor: 1.0 / face.height() as f32,
            color_tables: ColorTables::new(&face),
            face,
            #[cfg(feature = "font-loading")]
            _buf: None,
            monotonic,
            ellipsis: (ellipsis, ellipsis_width as f32),
            glyph_cache: HashMap::new(),
        })
    }
}

struct GlyphBuilder<PB>(PB);

impl<PB: PathBuilder> OutlineBuilder for GlyphBuilder<PB> {
    fn move_to(&mut self, x: f32, y: f32) {
        self.0.move_to(x, -y);
    }
    fn line_to(&mut self, x: f32, y: f32) {
        self.0.line_to(x, -y);
    }
    fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
        self.0.quad_to(x1, -y1, x, -y);
    }
    fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
        self.0.curve_to(x1, -y1, x2, -y2, x, -y);
    }
    fn close(&mut self) {
        self.0.close();
    }
}

/// The label to use in the [`ResourceAllocator`](super::ResourceAllocator).
pub type Label<P> = Arc<RwLock<LockedLabel<P>>>;

/// You need to lock the [`Label`] to use it. This is the locked type that
/// provides all the methods you need to use the label.
pub struct LockedLabel<P> {
    width: f32,
    width_without_max_width: f32,
    scale: f32,
    glyphs: Vec<Glyph<P>>,
}

impl<P> LockedLabel<P> {
    /// Apply this scale to the transform of the [`Entity`](super::Entity) with
    /// [`Transform::pre_scale`](super::Transform::pre_scale).
    pub const fn scale(&self) -> f32 {
        self.scale
    }

    /// The glyphs to render.
    pub fn glyphs(&self) -> &[Glyph<P>] {
        &self.glyphs
    }
}

impl<P> super::Label for Label<P> {
    fn width(&self, scale: f32) -> f32 {
        self.read().unwrap().width * scale
    }

    fn width_without_max_width(&self, scale: f32) -> f32 {
        self.read().unwrap().width_without_max_width * scale
    }
}

/// A glyph to render.
pub struct Glyph<P> {
    /// A glyph may provide its own color that overrides the fill shader of the
    /// [`Entity`](super::Entity).
    pub color: Option<Rgba>,
    /// The x-coordinate of the glyph.
    pub x: f32,
    /// The y-coordinate of the glyph.
    pub y: f32,
    /// The path to render.
    pub path: P,
}

fn glyph_width(face: &Face<'_>, c: char) -> Option<(GlyphId, u16)> {
    let glyph_id = face.glyph_index(c)?;
    Some((glyph_id, face.glyph_hor_advance(glyph_id)?))
}