use std::sync::Arc;
use mln_text::HBShapeAdjust;
use mln_text::glyph::{FontData, Glyph, GlyphID, GlyphIDType};
pub trait FontFaceShaper {
fn shape(&self, text: &[u16], rtl: bool, glyph_ids: &mut Vec<GlyphID>, adjusts: &mut Vec<HBShapeAdjust>);
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) -> Option<Box<dyn FontFaceShaper>>;
}
#[cfg(feature = "font-face-shaper")]
#[must_use]
pub fn default_engine() -> Arc<dyn FontFaceEngine> {
Arc::new(in_process::HBShaperEngine)
}
#[cfg(not(feature = "font-face-shaper"))]
#[must_use]
pub 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_renderer's font-face-shaper feature), \
and the renderer was given no engine"
.to_owned()));
}
fn load(&self, _type: GlyphIDType, _data: FontData) -> Option<Box<dyn FontFaceShaper>> {
None
}
}
Arc::new(NoEngine)
}
#[cfg(feature = "font-face-shaper")]
mod in_process {
use mln_text::glyph::{FontData, Glyph, GlyphID, GlyphIDType};
use mln_text::{FreeTypeLibrary, HBShapeAdjust, HBShaper};
use super::{FontFaceEngine, FontFaceShaper};
pub(super) struct HBShaperEngine;
impl FontFaceEngine for HBShaperEngine {
fn prepare(&self, ready: Box<dyn FnOnce(Result<(), String>) + Send>) {
ready(Ok(()));
}
fn load(&self, type_: GlyphIDType, data: FontData) -> Option<Box<dyn FontFaceShaper>> {
let shaper = HBShaper::new(type_, data, &FreeTypeLibrary::new());
shaper.valid().then(|| -> Box<dyn FontFaceShaper> { Box::new(shaper) })
}
}
impl FontFaceShaper for HBShaper {
fn shape(&self, text: &[u16], rtl: bool, glyph_ids: &mut Vec<GlyphID>, adjusts: &mut Vec<HBShapeAdjust>) {
self.create_complex_glyph_ids(text, rtl, glyph_ids, adjusts);
}
fn rasterize(&self, glyph: &GlyphID) -> Glyph {
self.rasterize_glyph(glyph)
}
}
}