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
use rustybuzz::ttf_parser::name_id::{FAMILY, FULL_NAME, SUBFAMILY, UNIQUE_ID, VERSION};
use rustybuzz::ttf_parser::{GlyphId, OutlineBuilder, Tag};
use rustybuzz::{shape_with_plan, Face, ShapePlan, UnicodeBuffer};
pub use rustybuzz::{Direction, Feature, GlyphBuffer as Glyphs, Language, Script};

use crate::views::Text;

///
pub struct Font<'a> {
    face: Face<'a>,
    plan: ShapePlan,
    size: f32,
}

impl<'a> Font<'a> {
    /// Full font name that reflects all family and relevant subfamily descriptors.
    #[inline]
    pub fn full_name(&self) -> Option<String> {
        self.name(FULL_NAME)
    }

    /// Family name.
    #[inline]
    pub fn family(&self) -> Option<String> {
        self.name(FAMILY)
    }

    /// Subfamily name.
    #[inline]
    pub fn style(&self) -> Option<String> {
        self.name(SUBFAMILY)
    }

    /// Unique font identifier
    #[inline]
    pub fn identifier(&self) -> Option<String> {
        self.name(UNIQUE_ID)
    }

    /// Should begin with the syntax “Version _N_._M_”
    /// (upper case, lower case, or mixed, with a space between “Version” and the number).
    #[inline]
    pub fn version(&self) -> Option<String> {
        self.name(VERSION)
    }

    #[inline(never)]
    fn name(&self, id: u16) -> Option<String> {
        self.face
            .names()
            .into_iter()
            .find(|name| name.name_id == id)
            .and_then(|name| name.to_string())
    }

    /// Font size in points.
    #[inline]
    pub fn size(&self) -> f32 {
        self.size
    }

    /// Horizontal face ascender.
    pub fn ascender(&self) -> f32 {
        self.face.ascender() as f32
    }

    /// Horizontal face descender,
    pub fn descender(&self) -> f32 {
        self.face.descender() as f32
    }

    /// Horizontal height,
    #[inline]
    pub fn height(&self) -> f32 {
        self.face.height() as f32
    }

    /// Capital height,
    #[inline]
    pub fn capital_height(&self) -> f32 {
        self.face
            .capital_height()
            .unwrap_or_else(|| self.face.ascender()) as f32
    }

    /// Line gap,
    #[inline]
    pub fn line_gap(&self) -> f32 {
        self.face.line_gap() as f32
    }

    /// Returns a `Text` in this font.
    #[inline(never)]
    pub fn text(&self, rgba: [u8; 4], string: &str) -> Text {
        let mut unicode = UnicodeBuffer::new();
        unicode.push_str(string);

        let glyphs = shape_with_plan(&self.face, &self.plan, unicode);
        let scale = self.size / self.face.units_per_em() as f32;

        // TODO: both of these assume Direction::LeftToRight or RightToLeft
        let width = glyphs
            .glyph_positions()
            .iter()
            .fold(0.0, |width, position| {
                width + (position.x_offset + position.x_advance) as f32
            })
            * scale;

        Text {
            font: self,
            glyphs,
            width,
            scale,
            rgba,
        }
    }

    #[inline(always)]
    pub(crate) fn outline_glyph(&self, glyph: u32, builder: &mut impl OutlineBuilder) {
        self.face.outline_glyph(GlyphId(glyph as u16), builder);
    }
}

impl<'a> Font<'a> {
    /// Create a `Font` from the raw font data.
    #[inline(always)]
    pub fn from(data: &'a [u8]) -> Option<FontConfig> {
        Self::from_collection(data, 0)
    }

    /// Create a `Font` from a font collection.
    /// Returns the font at `index`, if any
    #[inline(never)]
    pub fn from_collection(data: &'a [u8], index: u32) -> Option<FontConfig> {
        let face = Face::from_slice(data, index)?;

        Some(FontConfig {
            face,
            features: Vec::default(),
            direction: None,
            script: None,
            language: None,
            weight: None,
        })
    }
}

///
#[derive(Clone)]
pub struct FontConfig<'a> {
    face: Face<'a>,
    features: Vec<Feature>,
    direction: Option<Direction>,
    script: Option<Script>,
    language: Option<Language>,
    weight: Option<f32>,
}

impl<'a> FontConfig<'a> {
    ///
    #[inline]
    pub fn direction(self, direction: Direction) -> Self {
        Self {
            direction: Some(direction),
            ..self
        }
    }

    ///
    #[inline]
    pub fn script(self, script: Script) -> Self {
        Self {
            script: Some(script),
            ..self
        }
    }

    ///
    #[inline]
    pub fn language(self, language: Language) -> Self {
        Self {
            language: Some(language),
            ..self
        }
    }

    ///
    #[inline]
    pub fn feature(mut self, tag: &[u8; 4], value: u32) -> Self {
        self.features
            .push(Feature::new(Tag::from_bytes(tag), value, ..));

        self
    }

    ///
    #[inline]
    pub fn weight(self, weight: f32) -> Self {
        Self {
            weight: Some(weight),
            ..self
        }
    }

    /// The final step in building a Font.
    #[inline(never)]
    pub fn size(mut self, size: f32) -> Font<'a> {
        for axis in self.face.variation_axes().into_iter() {
            match &axis.tag.to_bytes() {
                b"opsz" => {
                    let opsz = size.clamp(axis.min_value, axis.max_value);
                    self.face.set_variation(axis.tag, opsz);
                }
                b"wght" => {
                    let wght = self
                        .weight
                        .map(|w| w.clamp(axis.min_value, axis.max_value))
                        .unwrap_or(axis.def_value);

                    self.face.set_variation(axis.tag, wght);
                }
                _ => {}
            }
        }

        // Using direction.unwrap_or_default() would give an Direction::Invalid
        // and that will panic!() in ShapePlan::new()
        let direction = self.direction.unwrap_or(Direction::LeftToRight);

        let script = self
            .script
            .or_else(|| Script::from_iso15924_tag(Tag::from_bytes(b"Latn")));

        let plan = ShapePlan::new(
            &self.face,
            direction,
            script,
            self.language.as_ref(),
            &self.features,
        );

        Font {
            face: self.face,
            plan,
            size,
        }
    }
}