mln_engine/text/face.rs
1//! A font face that the map shapes text with and draws glyphs from.
2
3use std::sync::{Arc, OnceLock};
4
5use skrifa::{Font, GlyphId, HintOptions, ScalerFont};
6
7use crate::text::glyph::{FontData, Glyph, GlyphID};
8
9/// The size glyphs are drawn and shaped at, in pixels per em.
10pub(crate) const SDF_FONT_SIZE: i32 = 24;
11
12#[derive(Clone)]
13pub(crate) struct Face {
14 /// The font at its weight (`weight::REGULAR_WEIGHT`, unless
15 /// made at another weight), or `None` when the data
16 /// holds no font this can read or the font maps no characters.
17 font: Option<Font>,
18 /// The hinted glyph loader at `SDF_FONT_SIZE`, made on first use.
19 scaler: Arc<OnceLock<ScalerFont>>,
20}
21
22impl Face {
23 /// Opens a face on `font_data`: an SFNT font or collection, or a WOFF or
24 /// WOFF2 file. Its character map is a Unicode, symbol or Mac Roman
25 /// `cmap` subtable; a font with none of those, such as a bare CFF font,
26 /// isn't valid.
27 ///
28 /// The face reads `font_data` instead of copying it, unless it is a WOFF
29 /// or WOFF2 file: then it reads the font decoded from it, and isn't valid
30 /// when that fails. A variable font is at the OpenType `weight` within
31 /// its `wght` axis.
32 pub(crate) fn new(font_data: impl Into<FontData>, weight: f32) -> Self {
33 let font = crate::text::woff::to_sfnt(font_data.into())
34 .ok()
35 .and_then(|data| Font::new(data.shared(), data.face_index()))
36 .filter(|font| font.charmap().has_unicode())
37 .map(|font| crate::text::weight::at_weight(font, weight));
38 Face { font, scaler: Arc::default() }
39 }
40
41 #[must_use]
42 pub(crate) fn is_valid(&self) -> bool {
43 self.font.is_some()
44 }
45
46 /// The font at the face's weight, `None` when not valid.
47 #[must_use]
48 pub(crate) fn font(&self) -> Option<&Font> {
49 self.font.as_ref()
50 }
51
52 /// The hinted glyph loader of a valid face: the font's own hinting, or
53 /// the autohinter for a font that has none.
54 pub(crate) fn scaler(&self) -> Option<&ScalerFont> {
55 let font = self.font.as_ref()?;
56 Some(
57 self.scaler
58 .get_or_init(|| ScalerFont::hinted(font.clone(), SDF_FONT_SIZE as f32, HintOptions::auto_fallback())),
59 )
60 }
61
62 /// The index of the glyph the font maps `c` to, if the map can draw it:
63 /// a glyph other than `.notdef`, whose index fits a glyph ID and whose
64 /// outline the map reads.
65 pub(crate) fn glyph_of(&self, c: char) -> Option<u16> {
66 let glyph = self.font()?.charmap().map_unicode(c)?;
67 let index = u16::try_from(glyph.to_u32()).ok().filter(|&index| index != 0)?;
68 self.scaler()?.outline(GlyphId::new(u32::from(index)))?;
69 Some(index)
70 }
71
72 #[must_use]
73 pub(crate) fn rasterize_glyph(&self, glyph: GlyphID) -> Glyph {
74 crate::text::rasterize::rasterize_glyph(self, glyph)
75 }
76}
77
78/// A font file, opened to find which characters the map can draw from it.
79#[derive(Clone)]
80pub struct FontFile {
81 face: Face,
82}
83
84impl FontFile {
85 /// The font in `data`, as a font face reads it: `None` when it isn't a
86 /// font the map can draw.
87 #[must_use]
88 pub fn new(data: FontData) -> Option<Self> {
89 // Which characters a font draws doesn't depend on its weight.
90 let face = Face::new(data, crate::text::weight::REGULAR_WEIGHT);
91 face.is_valid().then_some(FontFile { face })
92 }
93
94 /// Whether the map can draw `c` from the font: the font maps it to a
95 /// glyph whose outline the map reads. A font whose outlines are in a
96 /// format the map doesn't read has none.
97 #[must_use]
98 pub fn has_char(&self, c: char) -> bool {
99 self.face.glyph_of(c).is_some()
100 }
101}
102
103impl std::fmt::Debug for FontFile {
104 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
105 f.debug_struct("FontFile").finish_non_exhaustive()
106 }
107}