Real fonts in the wasm compositor: the host rasterises glyphs (Allo Variable, Adwaita Mono)
CI / test (push) Failing after 27s

New moonplasma:host/font import (load a TTF into a slot, rasterise a glyph). compositor/font now
shares its loading/caching/install logic and has two backends: stb_truetype through C (native) and
the host import (wasm). The Rust host draws glyphs with ab_glyph's outlines on an integer pixel grid,
using stb_truetype's em-size scaling, so text matches the native build.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Sonnet 5.5 committed 2026-11-25 01:50:01 -08:00
1 parent 0b9af11232
commit 5c3bebc7fd
15 files changed
+408 -143

No files matched your search

+55
View File
@@ -20,6 +20,9 @@ wasmtime::component::bindgen!({
world: "compositor",
});
use ab_glyph::{Font, FontVec, OutlineCurve};
use ab_glyph_rasterizer::{point, Rasterizer};
use moonplasma::host::font::Glyph;
use moonplasma::host::sys::{Host, Received};
#[derive(Default)]
@@ -28,6 +31,8 @@ struct State {
private_bus: Option<String>,
bus_tried: bool,
started: Option<std::time::Instant>,
/// TTF faces loaded by the component (`font.load`), by slot.
fonts: [Option<FontVec>; 4],
}
fn sockaddr_un(path: &str) -> (libc::sockaddr_un, libc::socklen_t) {
@@ -86,6 +91,55 @@ impl State {
}
}
impl moonplasma::host::font::Host for State {
fn load(&mut self, slot: u32, path: String) -> bool {
if slot as usize >= self.fonts.len() {
return false;
}
match std::fs::read(path).ok().and_then(|d| FontVec::try_from_vec(d).ok()) {
Some(f) => {
self.fonts[slot as usize] = Some(f);
true
}
None => false,
}
}
/// Same maths as stb_truetype in the native build: the size is an em size in pixels, the glyph
/// origin sits on the integer pixel grid, and the bitmap box is the outline's floor/ceil bounds.
fn rasterise(&mut self, slot: u32, codepoint: u32, size: u32) -> Glyph {
let blank = Glyph { width: 0, height: 0, xoff: 0, yoff: 0, advance: 0, coverage: Vec::new() };
let Some(Some(font)) = self.fonts.get(slot as usize) else { return blank };
let ch = char::from_u32(codepoint).unwrap_or('\u{fffd}');
let id = font.glyph_id(ch);
let s = size as f32 / font.units_per_em().unwrap_or(1000.0);
let advance = (font.h_advance_unscaled(id) * s + 0.5) as i32;
let Some(outline) = font.outline(id) else { return Glyph { advance, ..blank } };
// Font units grow upward, pixels downward; ab_glyph stores the outline's top edge as
// `min.y` and its bottom edge as `max.y`, both in font units.
let x0 = (outline.bounds.min.x * s).floor() as i32;
let x1 = (outline.bounds.max.x * s).ceil() as i32;
let y0 = (-outline.bounds.min.y * s).floor() as i32;
let y1 = (-outline.bounds.max.y * s).ceil() as i32;
let (w, h) = (x1 - x0, y1 - y0);
if w <= 0 || h <= 0 {
return Glyph { advance, ..blank };
}
let tp = |p: ab_glyph::Point| point(p.x * s - x0 as f32, -p.y * s - y0 as f32);
let mut r = Rasterizer::new(w as usize, h as usize);
for c in &outline.curves {
match c {
OutlineCurve::Line(a, b) => r.draw_line(tp(*a), tp(*b)),
OutlineCurve::Quad(a, b, c) => r.draw_quad(tp(*a), tp(*b), tp(*c)),
OutlineCurve::Cubic(a, b, c, d) => r.draw_cubic(tp(*a), tp(*b), tp(*c), tp(*d)),
}
}
let mut coverage = vec![0u8; (w * h) as usize];
r.for_each_pixel(|i, a| coverage[i] = (a.clamp(0.0, 1.0) * 255.0 + 0.5) as u8);
Glyph { width: w, height: h, xoff: x0, yoff: y0, advance, coverage }
}
}
impl Host for State {
fn unix_listen(&mut self, path: String) -> i32 {
let _ = std::fs::remove_file(&path);
@@ -423,6 +477,7 @@ fn main() -> Result<()> {
};
let mut linker: Linker<State> = Linker::new(&engine);
moonplasma::host::sys::add_to_linker::<State, HasSelf<State>>(&mut linker, |s| s)?;
moonplasma::host::font::add_to_linker::<State, HasSelf<State>>(&mut linker, |s| s)?;
let mut store = Store::new(&engine, State::default());
let compositor = Compositor::instantiate(&mut store, &component, &linker)?;
let code = compositor.call_run(&mut store, &args)?;