keyset-font 0.3.2

Font type and functionality for keyset
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
//! This crate contains the font loading and parsing logic used internally by [keyset].
//!
//! [keyset]: https://crates.io/crates/keyset

#![warn(
    missing_docs,
    clippy::all,
    clippy::correctness,
    clippy::suspicious,
    clippy::style,
    clippy::complexity,
    clippy::perf,
    clippy::pedantic,
    clippy::cargo,
    clippy::nursery
)]

mod default;
mod error;
mod face;

use std::fmt::Debug;
use std::sync::OnceLock;

use dashmap::DashMap;
use geom::{BezPath, Rect, Shape};
use log::warn;
use ttf_parser::GlyphId;

pub use self::error::{Error, Result};
use face::Face;

/// A glyph loaded from a [`Font`]
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Glyph {
    /// The outline of the glyph
    pub path: BezPath,
    /// The bounds of the glyph's outline. The value of `glyph.bounds` is equivalent to the result
    /// of `glyph.path.bounding_box()`
    pub bounds: Rect,
    /// The glyphs horizontal advance
    pub advance: f64,
}

impl Glyph {
    fn parse_from(face: &Face, gid: GlyphId) -> Option<Self> {
        struct BezPathBuilder(BezPath);

        // GRCOV_EXCL_START // TODO these are pretty trivial but we could cover them in tests
        impl ttf_parser::OutlineBuilder for BezPathBuilder {
            fn move_to(&mut self, x: f32, y: f32) {
                // Y axis is flipped in fonts compared to SVGs
                self.0.move_to((x.into(), (-y).into()));
            }

            fn line_to(&mut self, x: f32, y: f32) {
                // Y axis is flipped in fonts compared to SVGs
                self.0.line_to((x.into(), (-y).into()));
            }

            fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
                // Y axis is flipped in fonts compared to SVGs
                self.0
                    .quad_to((x1.into(), (-y1).into()), (x.into(), (-y).into()));
            }

            fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
                // Y axis is flipped in fonts compared to SVGs
                self.0.curve_to(
                    (x1.into(), (-y1).into()),
                    (x2.into(), (-y2).into()),
                    (x.into(), (-y).into()),
                );
            }

            fn close(&mut self) {
                self.0.close_path();
            }
        }
        // GRCOV_EXCL_STOP

        let mut builder = BezPathBuilder(BezPath::new());

        let bounds = face.outline_glyph(gid, &mut builder)?;
        let path = builder.0;

        let bounds = Rect::new(
            f64::from(bounds.x_min),
            f64::from(bounds.y_min),
            f64::from(bounds.x_max),
            f64::from(bounds.y_max),
        );

        let advance = f64::from(face.glyph_hor_advance(gid)?);

        Some(Self {
            path,
            bounds,
            advance,
        })
    }
}

/// A parsed font
#[derive(Clone)]
pub struct Font {
    face: Face,
    family: OnceLock<String>,
    name: OnceLock<String>,
    cap_height: OnceLock<f64>,
    x_height: OnceLock<f64>,
    notdef: OnceLock<Glyph>,
    glyphs: DashMap<char, Option<Glyph>>,
    kerning: DashMap<(char, char), f64>,
}

impl Debug for Font {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("Font").field(self.name()).finish()
    }
}

impl Default for Font {
    fn default() -> Self {
        Self::default_ref().clone()
    }
}

impl Font {
    /// Returns a static reference to the default font
    ///
    /// This is equivalent to calling [`Default::default`] but returns a reference and avoids
    /// cloning any internal data
    #[must_use]
    pub fn default_ref() -> &'static Self {
        default::font()
    }

    /// Parse a font from TrueType or OpenType format font data
    ///
    /// # Errors
    ///
    /// If there is an error parsing the font data
    pub fn from_ttf(data: Vec<u8>) -> Result<Self> {
        Ok(Self {
            face: Face::from_ttf(data)?,
            family: OnceLock::new(),
            name: OnceLock::new(),
            cap_height: OnceLock::new(),
            x_height: OnceLock::new(),
            notdef: OnceLock::new(),
            glyphs: DashMap::new(),
            kerning: DashMap::new(),
        })
    }

    /// The font family name
    ///
    /// Returns `"unknown"` if the font does not specify a family name
    pub fn family(&self) -> &String {
        self.family.get_or_init(|| {
            self.face
                .names()
                .into_iter()
                .filter(|n| n.name_id == ttf_parser::name_id::FAMILY)
                .find_map(|n| n.to_string())
                .unwrap_or_else(|| {
                    warn!("cannot read font family name");
                    "unknown".to_owned()
                })
        })
    }

    /// The font's full name
    ///
    /// Returns `"unknown"` if the font does not specify a full name
    pub fn name(&self) -> &String {
        self.name.get_or_init(|| {
            self.face
                .names()
                .into_iter()
                .filter(|n| n.name_id == ttf_parser::name_id::FULL_NAME)
                .find_map(|n| n.to_string())
                .unwrap_or_else(|| {
                    warn!("cannot read font full name");
                    "unknown".to_owned()
                })
        })
    }

    /// The number font units per EM
    pub fn em_size(&self) -> f64 {
        f64::from(self.face.units_per_em())
    }

    /// The capital height in font units
    ///
    /// Measures the height of the uppercase `'M'` if it is not set. In case the font does not contain
    /// an uppercase `'M'`, a default value is returned
    pub fn cap_height(&self) -> f64 {
        *self.cap_height.get_or_init(|| {
            self.face
                .capital_height()
                .map(f64::from)
                .or_else(|| self.glyph('M').map(|g| g.path.bounding_box().height()))
                .unwrap_or_else(|| {
                    default::cap_height() / default::line_height() * self.line_height()
                })
        })
    }

    /// The x-height in font units
    ///
    /// Measures the height of the lowercase `'x'` if it is not set. In case the font does not contain
    /// a lowercase `'x'`, a default value is returned
    pub fn x_height(&self) -> f64 {
        *self.x_height.get_or_init(|| {
            self.face
                .x_height()
                .map(f64::from)
                .or_else(|| self.glyph('x').map(|g| g.path.bounding_box().height()))
                .unwrap_or_else(|| {
                    default::x_height() / default::line_height() * self.line_height()
                })
        })
    }

    /// The font's ascender in font units
    pub fn ascender(&self) -> f64 {
        f64::from(self.face.ascender())
    }

    /// The font's descender in font units
    pub fn descender(&self) -> f64 {
        -f64::from(self.face.descender())
    }

    /// The font's line gap in font units
    pub fn line_gap(&self) -> f64 {
        f64::from(self.face.line_gap())
    }

    /// The font's line height in font units
    ///
    /// This is equal to `self.ascender() + self.descender() + self.line_gap()`
    pub fn line_height(&self) -> f64 {
        self.ascender() + self.descender() + self.line_gap()
    }

    /// The font's slope angle in clockwise degrees if specified
    pub fn slope(&self) -> Option<f64> {
        self.face
            .italic_angle()
            .map(f64::from)
            .map(std::ops::Neg::neg) // Negate so forward = positive
    }

    /// The number of glyph outlines in the font
    pub fn num_glyphs(&self) -> usize {
        usize::from(self.face.number_of_glyphs())
    }

    /// Returns the flyph for a given character if present in the font
    #[allow(clippy::missing_panics_doc)] // Only unwrapping a mutex
    pub fn glyph(&self, char: char) -> Option<Glyph> {
        self.glyphs
            .entry(char)
            .or_insert_with(|| {
                self.face
                    .glyph_index(char)
                    .and_then(|gid| Glyph::parse_from(&self.face, gid))
            })
            .clone()
    }

    /// Returns the glyph for a given character, or the default replacement character if not present
    pub fn glyph_or_default(&self, char: char) -> Glyph {
        self.glyph(char).unwrap_or_else(|| self.notdef())
    }

    /// Returns the font's default replacement glyph, `.notdef`, or a builtin default if not present
    pub fn notdef(&self) -> Glyph {
        self.notdef
            .get_or_init(|| {
                Glyph::parse_from(&self.face, GlyphId(0)).unwrap_or_else(|| {
                    warn!("no valid outline for glyph .notdef in font");
                    default::notdef()
                })
            })
            .clone()
    }

    /// Returns the kerning between two characters' glyphs, or 0 if no kerning is specified in the
    /// font
    #[allow(clippy::missing_panics_doc)] // Only unwrapping a mutex
    pub fn kerning(&self, left: char, right: char) -> f64 {
        *self.kerning.entry((left, right)).or_insert_with(|| {
            if let (Some(lhs), Some(rhs)) =
                (self.face.glyph_index(left), self.face.glyph_index(right))
            {
                self.face.glyphs_kerning(lhs, rhs).map_or(0.0, f64::from)
            } else {
                0.0
            }
        })
    }
}

#[cfg(test)]
mod tests {
    use assert_approx_eq::assert_approx_eq;
    use assert_matches::assert_matches;

    use super::*;

    #[test]
    fn glyph_parse_from() {
        let data = std::fs::read(env!("DEMO_TTF")).unwrap();
        let face = Face::from_ttf(data).unwrap();

        let a = Glyph::parse_from(&face, GlyphId(1)).unwrap();
        assert_approx_eq!(a.advance, 540.0);
        assert_approx_eq!(a.bounds.width(), 535.0);
        assert_approx_eq!(a.bounds.height(), 656.0);

        let v = Glyph::parse_from(&face, GlyphId(2)).unwrap();
        assert_approx_eq!(v.advance, 540.0);
        assert_approx_eq!(v.bounds.width(), 535.0);
        assert_approx_eq!(v.bounds.height(), 656.0);
    }

    #[test]
    fn font_debug() {
        let data = std::fs::read(env!("DEMO_TTF")).unwrap();
        let font = Font::from_ttf(data).unwrap();

        assert_eq!(format!("{:?}", font), r#"Font("demo regular")"#);
    }

    #[test]
    fn font_clone() {
        let data = std::fs::read(env!("DEMO_TTF")).unwrap();
        let font = Font::from_ttf(data).unwrap();

        let _ = font.clone(); // Shouldn't panic
    }

    #[test]
    fn font_default() {
        let _ = Font::default(); // Shouldn't panic
    }

    #[test]
    fn font_from_ttf() {
        let data = std::fs::read(env!("DEMO_TTF")).unwrap();
        let font = Font::from_ttf(data).unwrap();

        assert_matches!(font.face, Face { .. });
        assert!(font.family.get().is_none());
        assert!(font.name.get().is_none());
        assert!(font.cap_height.get().is_none());
        assert!(font.x_height.get().is_none());
        assert!(font.notdef.get().is_none());
        assert_eq!(font.glyphs.len(), 0);
        assert_eq!(font.kerning.len(), 0);
    }

    #[test]
    fn font_properties() {
        let data = std::fs::read(env!("DEMO_TTF")).unwrap();
        let font = Font::from_ttf(data).unwrap();

        assert_eq!(font.family(), "demo");
        assert_eq!(font.name(), "demo regular");
        assert_approx_eq!(font.em_size(), 1000.0);
        assert_approx_eq!(font.cap_height(), 650.0);
        assert_approx_eq!(font.x_height(), 450.0);
        assert_approx_eq!(font.ascender(), 1024.0);
        assert_approx_eq!(font.descender(), 400.0);
        assert_approx_eq!(font.line_gap(), 0.0);
        assert_approx_eq!(font.line_height(), 1424.0);
        assert_eq!(font.slope(), None);
        assert_eq!(font.num_glyphs(), 3);

        let data = std::fs::read(env!("NULL_TTF")).unwrap();
        let font = Font::from_ttf(data).unwrap();

        let line_scaling = font.line_height() / default::line_height();
        assert_eq!(font.family(), "unknown");
        assert_eq!(font.name(), "unknown");
        assert_approx_eq!(font.em_size(), 1000.0);
        assert_approx_eq!(font.cap_height(), default::cap_height() * line_scaling);
        assert_approx_eq!(font.x_height(), default::x_height() * line_scaling);
        assert_approx_eq!(font.ascender(), 600.0);
        assert_approx_eq!(font.descender(), 400.0);
        assert_approx_eq!(font.line_gap(), 200.0);
        assert_approx_eq!(font.line_height(), 1200.0);
        assert_eq!(font.slope(), None);
        assert_eq!(font.num_glyphs(), 1); // Just .notdef
    }

    #[test]
    fn font_glyph() {
        let data = std::fs::read(env!("DEMO_TTF")).unwrap();
        let font = Font::from_ttf(data).unwrap();

        assert!(font.glyph('A').is_some());
        assert!(font.glyph('B').is_none());
    }

    #[test]
    fn font_glyph_or_default() {
        let data = std::fs::read(env!("DEMO_TTF")).unwrap();
        let font = Font::from_ttf(data).unwrap();

        assert_ne!(font.glyph_or_default('A').path, font.notdef().path);
        assert_eq!(font.glyph_or_default('B').path, font.notdef().path);
    }

    #[test]
    fn font_notdef() {
        let data = std::fs::read(env!("DEMO_TTF")).unwrap();
        let font = Font::from_ttf(data).unwrap();

        assert_eq!(font.notdef().path.elements().len(), 12);

        let data = std::fs::read(env!("NULL_TTF")).unwrap();
        let font = Font::from_ttf(data).unwrap();

        assert_eq!(font.notdef().path.elements().len(), 26);
    }

    #[test]
    fn font_kerning() {
        let data = std::fs::read(env!("DEMO_TTF")).unwrap();
        let font = Font::from_ttf(data).unwrap();

        assert_approx_eq!(font.kerning('A', 'V'), -70.0);
        assert_approx_eq!(font.kerning('A', 'B'), 0.0);
    }
}