nightshade-text 0.57.0

Text shaping, styling, and glyph geometry shared by the nightshade renderer and UI.
Documentation
//! Shapes a string with the embedded fonts, measures it in each family the
//! crate ships with, and re-flows it to a width.

use nightshade_text::font_engine::FontEngine;
use nightshade_text::text::TextProperties;
use nightshade_text::text_data::FontKind;
use nightshade_text::text_wrapping::wrap_text;

fn measure(font_engine: &mut FontEngine, text: &str, font_size: f32, font_kind: FontKind) -> f32 {
    let buffer = font_engine.shape_buffer(text, font_size, font_size * 1.2, None, font_kind);
    buffer
        .layout_runs()
        .map(|run| run.line_w)
        .fold(0.0_f32, f32::max)
}

fn main() {
    let mut font_engine = FontEngine::new();
    let properties = TextProperties::default();

    let sample = "Nightshade shapes text with cosmic-text.";
    let width = measure(
        &mut font_engine,
        sample,
        properties.font_size,
        properties.font_kind,
    );
    println!("{sample:?}");
    println!("  {:>8.2} px wide at {} px", width, properties.font_size);

    let mono_width = measure(
        &mut font_engine,
        sample,
        properties.font_size,
        FontKind::Mono,
    );
    println!("  {mono_width:>8.2} px wide in the monospace family");

    println!();
    for size in [12.0_f32, 24.0, 48.0] {
        let width = measure(&mut font_engine, sample, size, properties.font_kind);
        println!("{size:>5.0} px -> {width:>8.2} px wide");
    }

    println!();
    for limit in [200.0_f32, 120.0] {
        println!("wrapped to {limit:.0} px:");
        for line in wrap_text(&mut font_engine, sample, properties.font_size, limit).lines() {
            println!("  {line}");
        }
    }
}