font.rs
2.0 kB · rust · 59 lines
1use mrlyrs::core::json;2use wasm_bindgen::prelude::*;34/// Lays the text out as one 0/1 grid: its row count, its column count and its rows as strings of '0' and '1', as JSON.5#[wasm_bindgen]6pub fn font_raster(text: &str) -> String {7 let grid = mrlyrs::font::raster(text);8 let cols = grid.first().map_or(0, |row| row.len());9 let rows: Vec<String> = grid10 .iter()11 .map(|row| {12 row.iter()13 .map(|bit| if *bit == 1 { '1' } else { '0' })14 .collect()15 })16 .collect();17 json!({ "rows": grid.len(), "cols": cols, "grid": rows }).to_string()18}1920/// Writes the text cell by cell in stroke order on a board padded by pad: the board size, the rate and the lit cell indices of every frame, as JSON.21#[wasm_bindgen]22pub fn font_animate(text: &str, pad: usize) -> String {23 anim_json(&mrlyrs::font::animate(text, pad))24}2526/// Loops the text's write-and-fold cycle, holding hold frames between the movements, in the same shape as font_animate.27#[wasm_bindgen]28pub fn font_cycle(text: &str, pad: usize, hold: usize) -> String {29 let write = mrlyrs::font::animate(text, pad);30 let merged = mrlyrs::font::merge(text, pad);31 anim_json(&mrlyrs::font::cycle(&write, &merged, hold))32}3334/// Returns the least strokes that can write the character, the minimum cover of its cells by 4-adjacent paths, or 0 outside the font.35#[wasm_bindgen]36pub fn font_floor(c: &str) -> usize {37 c.chars()38 .next()39 .and_then(mrlyrs::font::glyph)40 .map_or(0, |glyph| {41 mrlyrs::font::floor(&mrlyrs::font::trim(&glyph.rows))42 })43}4445/// Returns every character the font supports, in font order, as one string.46#[wasm_bindgen]47pub fn font_chars() -> String {48 mrlyrs::font::supported().into_iter().collect()49}5051fn anim_json(anim: &mrlyrs::font::Anim) -> String {52 json!({53 "rows": anim.rows,54 "cols": anim.cols,55 "fps": anim.fps,56 "frames": anim.frames,57 })58 .to_string()59}