use uzor::render::{TextMetrics, WrappedLine};
use super::LineShaper;
use crate::model::FontSpec;
pub enum CosmicShaper<'a> {
FromCtx(&'a dyn TextMetrics),
Headless,
}
impl<'a> CosmicShaper<'a> {
pub fn from_ctx(ctx: &'a dyn TextMetrics) -> Self {
CosmicShaper::FromCtx(ctx)
}
pub fn headless() -> CosmicShaper<'static> {
CosmicShaper::Headless
}
}
impl<'a> LineShaper for CosmicShaper<'a> {
fn shape_wrapped(&self, text: &str, font: &FontSpec, max_width: f64) -> Vec<WrappedLine> {
let font_str = font.to_css_font();
match self {
CosmicShaper::FromCtx(ctx) => ctx.measure_text_wrapped(text, &font_str, max_width),
CosmicShaper::Headless => uzor::shaper::measure_glyphs_wrapped(text, &font_str, max_width),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use uzor::fonts::FontFamily;
use uzor_render_tiny_skia::TinySkiaCpuRenderContext;
#[test]
fn from_ctx_and_headless_agree_on_a_short_line() {
let font = FontSpec::new(FontFamily::Roboto, 16.0);
let ctx = TinySkiaCpuRenderContext::new(200, 100, 1.0);
let from_ctx = CosmicShaper::from_ctx(&ctx);
let headless = CosmicShaper::headless();
let a = from_ctx.shape_wrapped("Hello uzor-text", &font, 1000.0);
let b = headless.shape_wrapped("Hello uzor-text", &font, 1000.0);
assert_eq!(a.len(), 1);
assert_eq!(a.len(), b.len());
assert_eq!(a[0].glyphs.len(), b[0].glyphs.len());
for (ga, gb) in a[0].glyphs.iter().zip(b[0].glyphs.iter()) {
assert_eq!(ga.cluster, gb.cluster);
assert!((ga.x_offset - gb.x_offset).abs() < 0.01);
}
}
}