mln_engine/text/
font_face_engine.rs1use std::sync::Arc;
12
13use crate::text::ShapedText;
14use crate::text::glyph::{FontData, Glyph, GlyphID, GlyphIDType};
15
16pub trait FontFaceShaper {
18 fn shape(&self, text: &[char], rtl: bool, shaped: &mut ShapedText);
21
22 fn glyph_of(&self, c: char) -> Option<GlyphID>;
25
26 fn rasterize(&self, glyph: GlyphID) -> Glyph;
28}
29
30pub trait FontFaceEngine: Send + Sync {
32 fn prepare(&self, ready: Box<dyn FnOnce(Result<(), String>) + Send>);
36
37 fn load(&self, type_: GlyphIDType, data: FontData, weight: u16) -> Option<Box<dyn FontFaceShaper>>;
42}
43
44#[cfg(feature = "font-face-shaper")]
48#[must_use]
49pub fn default_engine() -> Arc<dyn FontFaceEngine> {
50 Arc::new(in_process::ShaperEngine)
51}
52
53#[cfg(not(feature = "font-face-shaper"))]
57#[must_use]
58pub 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 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}