Skip to main content

mln_engine/text/
font_face_engine.rs

1// What GlyphStore shapes and rasterizes font files with: `Shaper`
2// behind a trait, so that a host can load it elsewhere.
3//
4// Natively, and for any build with the `font-face-shaper` feature, the
5// engine is `Shaper` in-process, ready at once. The browser build
6// (examples/web) turns the feature off, which leaves HarfRust, skrifa and
7// the glyph rasterizer out of its module, and sets an engine that loads them
8// as a second wasm module the first time a style declares font faces
9// (crates/mln_web_shaper).
10
11use std::sync::Arc;
12
13use crate::text::ShapedText;
14use crate::text::glyph::{FontData, Glyph, GlyphID, GlyphIDType};
15
16/// A font file loaded for shaping: what `Shaper` does for `GlyphStore`.
17pub trait FontFaceShaper {
18    /// Appends the glyphs of `text`, one run of one direction in logical
19    /// order, in visual order, and their positions.
20    fn shape(&self, text: &[char], rtl: bool, shaped: &mut ShapedText);
21
22    /// The glyph the font maps `c` to, unshaped: `None` unless it has one
23    /// that `rasterize` draws.
24    fn glyph_of(&self, c: char) -> Option<GlyphID>;
25
26    /// Rasterizes the glyph: its coverage at 24 px, before the SDF transform.
27    fn rasterize(&self, glyph: GlyphID) -> Glyph;
28}
29
30/// Loads font files as `FontFaceShaper`s.
31pub trait FontFaceEngine: Send + Sync {
32    /// Gets the engine ready to `load`, if it isn't yet, and then calls
33    /// `ready` with whether it is: before returning, or later from any
34    /// thread. It may be called again, before or after `ready` was called.
35    fn prepare(&self, ready: Box<dyn FnOnce(Result<(), String>) + Send>);
36
37    /// Loads a font, a variable font at the OpenType `weight`, once
38    /// `prepare` reported the engine ready: `None` when `data` isn't a font
39    /// the engine can read. The shaper may keep `data` (the response's
40    /// bytes) instead of copying it.
41    fn load(&self, type_: GlyphIDType, data: FontData, weight: u16) -> Option<Box<dyn FontFaceShaper>>;
42}
43
44/// The engine of a renderer that wasn't given one
45/// (`Renderer::set_font_face_engine`): `Shaper` with the
46/// `font-face-shaper` feature, else one that loads nothing.
47#[cfg(feature = "font-face-shaper")]
48#[must_use]
49pub fn default_engine() -> Arc<dyn FontFaceEngine> {
50    Arc::new(in_process::ShaperEngine)
51}
52
53/// Without the shaper, a style's font faces load nothing until a host sets
54/// an engine: their text is left out, as for a font file that fails to
55/// parse.
56#[cfg(not(feature = "font-face-shaper"))]
57#[must_use]
58pub(crate) fn default_engine() -> Arc<dyn FontFaceEngine> {
59    struct NoEngine;
60    impl FontFaceEngine for NoEngine {
61        fn prepare(&self, ready: Box<dyn FnOnce(Result<(), String>) + Send>) {
62            ready(Err("this build has no font-face shaper (mln_engine's font-face-shaper feature), \
63                       and the renderer was given no engine"
64                .to_owned()));
65        }
66
67        fn load(&self, _type: GlyphIDType, _data: FontData, _weight: u16) -> Option<Box<dyn FontFaceShaper>> {
68            None
69        }
70    }
71    Arc::new(NoEngine)
72}
73
74#[cfg(feature = "font-face-shaper")]
75mod in_process {
76    use super::{FontFaceEngine, FontFaceShaper};
77    use crate::text::glyph::{FontData, Glyph, GlyphID, GlyphIDType};
78    use crate::text::{ShapedText, Shaper};
79
80    /// `Shaper` in this thread.
81    pub(super) struct ShaperEngine;
82
83    impl FontFaceEngine for ShaperEngine {
84        fn prepare(&self, ready: Box<dyn FnOnce(Result<(), String>) + Send>) {
85            ready(Ok(()));
86        }
87
88        fn load(&self, type_: GlyphIDType, data: FontData, weight: u16) -> Option<Box<dyn FontFaceShaper>> {
89            let shaper = Shaper::at_weight(type_, data, f32::from(weight));
90            shaper.valid().then(|| -> Box<dyn FontFaceShaper> { Box::new(shaper) })
91        }
92    }
93
94    impl FontFaceShaper for Shaper {
95        fn shape(&self, text: &[char], rtl: bool, shaped: &mut ShapedText) {
96            Shaper::shape(self, text, rtl, shaped);
97        }
98
99        fn glyph_of(&self, c: char) -> Option<GlyphID> {
100            Shaper::glyph_of(self, c)
101        }
102
103        fn rasterize(&self, glyph: GlyphID) -> Glyph {
104            self.rasterize_glyph(glyph)
105        }
106    }
107}