1
#[cfg(feature = "bundled-fonts-enabled")]
2
pub mod bundled_fonts;
3
pub(crate) mod font;
4
/// Types for handling perpared text.
5
pub mod prepared;
6
use figures::{Displayable, Figure};
7
pub use font::Font;
8
use rusttype::Scale;
9

            
10
use self::prepared::{GlyphInfo, PreparedSpan};
11
use crate::color::Color;
12
use crate::math::Scaled;
13
use crate::prelude::Target;
14

            
15
/// Text rendering functionality
16
pub enum Text {}
17

            
18
impl Text {
19
    /// Prepares `text` to be rendered with the provided settings.
20
    #[must_use]
21
    pub fn prepare(
22
        text: &str,
23
        font: &Font,
24
        size: Figure<f32, Scaled>,
25
        color: Color,
26
        scene: &Target,
27
    ) -> PreparedSpan {
28
        let size_in_pixels = size.to_pixels(scene.scale());
29
        let characters = text.chars().collect::<Vec<_>>();
30
        let mut caret = Figure::new(0.);
31
        let mut glyphs = Vec::with_capacity(characters.len());
32
        let mut last_glyph_id = None;
33
        for (source_offset, &c) in characters.iter().enumerate() {
34
            let base_glyph = font.glyph(c);
35
            if let Some(id) = last_glyph_id.take() {
36
                caret += Figure::new(font.pair_kerning(size_in_pixels.get(), id, base_glyph.id()));
37
            }
38
            last_glyph_id = Some(base_glyph.id());
39
            let glyph = base_glyph
40
                .scaled(Scale::uniform(size_in_pixels.get()))
41
                .positioned(rusttype::point(caret.get(), 0.0));
42

            
43
            caret += Figure::new(glyph.unpositioned().h_metrics().advance_width);
44
            glyphs.push(GlyphInfo {
45
                source_offset,
46
                source: c,
47
                glyph,
48
            });
49
        }
50

            
51
        PreparedSpan::new(
52
            font.clone(),
53
            size_in_pixels,
54
            color,
55
            caret,
56
            characters,
57
            glyphs,
58
            font.metrics(size_in_pixels),
59
        )
60
    }
61
}