Real fonts in the wasm compositor: the host rasterises glyphs (Allo Variable, Adwaita Mono)
CI / test (push) Failing after 27s
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:
1 parent
0b9af11232
commit
5c3bebc7fd
15 files changed
+408
-143
No files matched your search
Generated
+33
@@ -2,6 +2,22 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "ab_glyph"
|
||||
version = "0.2.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "01c0457472c38ea5bd1c3b5ada5e368271cb550be7a4ca4a0b4634e9913f6cc2"
|
||||
dependencies = [
|
||||
"ab_glyph_rasterizer",
|
||||
"owned_ttf_parser",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ab_glyph_rasterizer"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "366ffbaa4442f4684d91e2cd7c5ea7c4ed8add41959a31447066e279e432b618"
|
||||
|
||||
[[package]]
|
||||
name = "addr2line"
|
||||
version = "0.26.1"
|
||||
@@ -552,6 +568,8 @@ dependencies = [
|
||||
name = "moon-plasma-host"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"ab_glyph",
|
||||
"ab_glyph_rasterizer",
|
||||
"libc",
|
||||
"wasmtime",
|
||||
]
|
||||
@@ -580,6 +598,15 @@ version = "1.21.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
|
||||
|
||||
[[package]]
|
||||
name = "owned_ttf_parser"
|
||||
version = "0.25.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "36820e9051aca1014ddc75770aab4d68bc1e9e632f0f5627c4086bc216fb583b"
|
||||
dependencies = [
|
||||
"ttf-parser",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.18"
|
||||
@@ -834,6 +861,12 @@ dependencies = [
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ttf-parser"
|
||||
version = "0.25.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d2df906b07856748fa3f6e0ad0cbaa047052d4a7dd609e231c4f72cee8c36f31"
|
||||
|
||||
[[package]]
|
||||
name = "typenum"
|
||||
version = "1.20.1"
|
||||
|
||||
@@ -5,6 +5,8 @@ edition = "2021"
|
||||
description = "Runs the moon-plasma compositor wasm component (build/components/compositor.wasm) and gives it the OS surface it imports (wit/host/sys.wit)."
|
||||
|
||||
[dependencies]
|
||||
ab_glyph = "0.2"
|
||||
ab_glyph_rasterizer = "0.1"
|
||||
libc = "0.2"
|
||||
wasmtime = { version = "50", default-features = false, features = ["component-model", "cranelift", "runtime", "std"] }
|
||||
|
||||
|
||||
@@ -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)?;
|
||||
|
||||
Reference in new issue
Block a user