use std::sync::Arc;
use crate::text::ShapedText;
use crate::text::glyph::{FontData, Glyph, GlyphID, GlyphIDType};
pub trait FontFaceShaper {
fn shape(&self, text: &[char], rtl: bool, shaped: &mut ShapedText);
fn glyph_of(&self, c: char) -> Option<GlyphID>;
fn rasterize(&self, glyph: GlyphID) -> Glyph;
}
pub trait FontFaceEngine: Send + Sync {
fn prepare(&self, ready: Box<dyn FnOnce(Result<(), String>) + Send>);
fn load(&self, type_: GlyphIDType, data: FontData, weight: u16) -> Option<Box<dyn FontFaceShaper>>;
}
#[cfg(feature = "font-face-shaper")]
#[must_use]
pub fn default_engine() -> Arc<dyn FontFaceEngine> {
Arc::new(in_process::ShaperEngine)
}
#[cfg(not(feature = "font-face-shaper"))]
#[must_use]
pub(crate) fn default_engine() -> Arc<dyn FontFaceEngine> {
struct NoEngine;
impl FontFaceEngine for NoEngine {
fn prepare(&self, ready: Box<dyn FnOnce(Result<(), String>) + Send>) {
ready(Err("this build has no font-face shaper (mln_engine's font-face-shaper feature), \
and the renderer was given no engine"
.to_owned()));
}
fn load(&self, _type: GlyphIDType, _data: FontData, _weight: u16) -> Option<Box<dyn FontFaceShaper>> {
None
}
}
Arc::new(NoEngine)
}
#[cfg(feature = "font-face-shaper")]
mod in_process {
use super::{FontFaceEngine, FontFaceShaper};
use crate::text::glyph::{FontData, Glyph, GlyphID, GlyphIDType};
use crate::text::{ShapedText, Shaper};
pub(super) struct ShaperEngine;
impl FontFaceEngine for ShaperEngine {
fn prepare(&self, ready: Box<dyn FnOnce(Result<(), String>) + Send>) {
ready(Ok(()));
}
fn load(&self, type_: GlyphIDType, data: FontData, weight: u16) -> Option<Box<dyn FontFaceShaper>> {
let shaper = Shaper::at_weight(type_, data, f32::from(weight));
shaper.valid().then(|| -> Box<dyn FontFaceShaper> { Box::new(shaper) })
}
}
impl FontFaceShaper for Shaper {
fn shape(&self, text: &[char], rtl: bool, shaped: &mut ShapedText) {
Shaper::shape(self, text, rtl, shaped);
}
fn glyph_of(&self, c: char) -> Option<GlyphID> {
Shaper::glyph_of(self, c)
}
fn rasterize(&self, glyph: GlyphID) -> Glyph {
self.rasterize_glyph(glyph)
}
}
}