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
pub use font::{Direction, Font, FontConfig, Glyphs, Language, Script};

use crate::dependencies::Dependency;
use crate::views::{Bounds, Output, Size, Transform, View};

mod font;

/// Text data
#[doc(hidden)] // documented as views::Text
pub struct Text<'a> {
    font: &'a Font<'a>,
    glyphs: Glyphs,
    width: f32,
    scale: f32,
    rgba: [u8; 4],
}

impl Text<'_> {
    /// Height of the Text’s font.
    #[inline]
    pub fn height(&self) -> f32 {
        self.font.height() * self.scale
    }

    /// Ascender height of the Text’s font.
    #[inline]
    pub fn ascender(&self) -> f32 {
        self.font.ascender() * self.scale
    }

    /// Descender height of the Text’s font.  
    /// Note that this is a negative value.
    #[inline]
    pub fn descender(&self) -> f32 {
        self.font.descender() * self.scale
    }

    /// Capital height of the Text’s font.
    #[inline]
    pub fn capital_height(&self) -> f32 {
        self.font.capital_height() * self.scale
    }

    /// Line gap of the Text’s font.
    #[inline]
    pub fn line_gap(&self) -> f32 {
        self.font.line_gap() * self.scale
    }
}

impl View for Text<'_> {
    #[inline(always)]
    fn size(&self) -> Size {
        (self.width, self.height()).into()
    }

    fn draw(&self, bounds: Bounds, output: &mut impl Output) {
        struct Builder<'a, T: Output> {
            transform: Transform,
            output: &'a mut T,
            rgba: [u8; 4],
        }

        impl<'a, F: Output> rustybuzz::ttf_parser::OutlineBuilder for Builder<'a, F> {
            fn move_to(&mut self, x: f32, y: f32) {
                self.output.begin(x, y, self.rgba, &self.transform);
            }

            fn line_to(&mut self, x: f32, y: f32) {
                self.output.line_to(x, y);
            }

            fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
                self.output.quadratic_bezier_to(x1, y1, x, y);
            }

            fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
                self.output.cubic_bezier_to(x1, y1, x2, y2, x, y);
            }

            fn close(&mut self) {
                self.output.close();
            }
        }

        let transform = Dependency::<Transform>::new();
        let mut builder = Builder {
            transform: Transform::scale(self.scale, -self.scale) // negate y-axis
                .then_translate((0.0, self.ascender()).into()) // font baseline
                .then_translate(bounds.min.to_vector()) // start position,
                .then(&transform.unwrap_or_default()),
            rgba: self.rgba,
            output,
        };

        let positions = self.glyphs.glyph_positions().iter();
        let glyphs = self.glyphs.glyph_infos().iter();

        for (glyph, position) in Iterator::zip(glyphs, positions) {
            builder.transform = builder
                .transform // “How much the glyph moves on the [X/Y]-axis before drawing it”
                .pre_translate((position.x_offset as f32, position.y_offset as f32).into());

            self.font.outline_glyph(glyph.glyph_id, &mut builder);

            builder.transform = builder
                .transform // “How much the line advances after drawing this glyph”
                .pre_translate((position.x_advance as f32, position.y_advance as f32).into());
        }
    }
}