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

+97
View File
@@ -0,0 +1,97 @@
///| Real text rendering: Allo Variable (UI text, falling back to Adwaita Sans) and Adwaita Mono
/// (code), rasterised and plugged into the renderer's glyph hook. The pure-MoonBit renderer keeps its
/// bitmap font when no hook is set. Two backends supply the glyphs: font_native.mbt (stb_truetype
/// through a C shim) and font_wasm.mbt (the host's `moonplasma:host/font` import).
///| A rasterised glyph as a backend returns it: 8-bit coverage `w` x `h` whose top-left is at
/// (`xoff`, `yoff`) from the pen on the baseline, and the pen `advance`.
pub(all) struct RawGlyph {
w : Int
h : Int
xoff : Int
yoff : Int
advance : Int
coverage : Bytes
}
///| Where to look for each face: bundled copy first, then the system one.
let regular_paths : Array[String] = [
"assets/fonts/AdwaitaMono-Regular.ttf",
"/usr/share/fonts/Adwaita/AdwaitaMono-Regular.ttf",
]
///|
let sans_paths : Array[String] = [
"assets/fonts/AlloVariable.ttf",
"/usr/local/share/fonts/a/AlloVariable.ttf",
"assets/fonts/AdwaitaSans-Regular.ttf",
"/usr/share/fonts/Adwaita/AdwaitaSans-Regular.ttf",
]
///|
let bold_paths : Array[String] = [
"assets/fonts/AdwaitaMono-Bold.ttf",
"/usr/share/fonts/Adwaita/AdwaitaMono-Bold.ttf",
]
///|
fn load_first(slot : Int, paths : Array[String]) -> Bool {
for p in paths {
if b_load(slot, p) {
return true
}
}
false
}
///|
let cache : Map[Int, @renderer.Glyph] = {}
///|
fn rasterise(slot : Int, cp : Int, size : Int, embolden : Bool) -> @renderer.Glyph {
let key = cp + 0x110000 * (size + 256 * (slot * 2 + (if embolden { 1 } else { 0 })))
match cache.get(key) {
Some(g) => g
None => {
let r = b_glyph(slot, cp, size)
let g : @renderer.Glyph = {
w: r.w,
h: r.h,
xoff: r.xoff,
yoff: r.yoff,
advance: r.advance,
coverage: r.coverage,
embolden,
}
cache.set(key, g)
g
}
}
}
///| Install Allo Variable (UI text, falling back to Adwaita Sans) and Adwaita Mono (code) as the renderer's
/// faces. Returns false (keeping the bitmap font) when no face can be found.
/// Slots: 0 mono, 1 mono bold, 2 sans. Sans is a variable font whose bold
/// instance stb_truetype can't select, so bold sans is emboldened.
pub fn install() -> Bool {
let has_mono = load_first(0, regular_paths)
let has_sans = load_first(2, sans_paths)
if !has_mono && !has_sans {
return false
}
let has_bold = has_mono && load_first(1, bold_paths)
@renderer.set_glyph_source(fn(cp, size, bold, mono) {
if mono && has_mono {
if bold && has_bold {
rasterise(1, cp, size, false)
} else {
rasterise(0, cp, size, bold)
}
} else if has_sans {
rasterise(2, cp, size, bold)
} else {
rasterise(0, cp, size, bold)
}
})
true
}
+17 -90
View File
@@ -1,6 +1,4 @@
///| Real text rendering for the native compositor: Adwaita Mono rasterised with
/// stb_truetype, plugged into the renderer's glyph hook. The pure-MoonBit
/// renderer (also built for wasm) keeps its bitmap font when no hook is set.
///| Native font backend: glyphs from stb_truetype through a C shim (font_stub.c).
///|
#borrow(path)
@@ -49,94 +47,23 @@ fn utf8(s : String) -> Bytes {
Bytes::from_array(out)
}
///| Where to look for each face: bundled copy first, then the system one.
let regular_paths : Array[String] = [
"assets/fonts/AdwaitaMono-Regular.ttf",
"/usr/share/fonts/Adwaita/AdwaitaMono-Regular.ttf",
]
///|
let sans_paths : Array[String] = [
"assets/fonts/AlloVariable.ttf",
"/usr/local/share/fonts/a/AlloVariable.ttf",
"assets/fonts/AdwaitaSans-Regular.ttf",
"/usr/share/fonts/Adwaita/AdwaitaSans-Regular.ttf",
]
///|
let bold_paths : Array[String] = [
"assets/fonts/AdwaitaMono-Bold.ttf",
"/usr/share/fonts/Adwaita/AdwaitaMono-Bold.ttf",
]
///|
fn load_first(slot : Int, paths : Array[String]) -> Bool {
for p in paths {
if c_font_load(slot, utf8(p)) == 0 {
return true
}
}
false
///| Load a TTF file into `slot`.
fn b_load(slot : Int, path : String) -> Bool {
c_font_load(slot, utf8(path)) == 0
}
///|
let cache : Map[Int, @renderer.Glyph] = {}
///|
fn rasterise(slot : Int, cp : Int, size : Int, embolden : Bool) -> @renderer.Glyph {
let key = cp + 0x110000 * (size + 256 * (slot * 2 + (if embolden { 1 } else { 0 })))
match cache.get(key) {
Some(g) => g
None => {
let info : FixedArray[Int] = FixedArray::make(5, 0)
c_font_metrics(slot, cp, size, info)
let w = info[0]
let h = info[1]
let buf = if w > 0 && h > 0 {
let b = Bytes::make(w * h, b'\x00')
c_font_bitmap(slot, cp, size, w, h, b)
b
} else {
Bytes::make(0, b'\x00')
}
let g : @renderer.Glyph = {
w,
h,
xoff: info[2],
yoff: info[3],
advance: info[4],
coverage: buf,
embolden,
}
cache.set(key, g)
g
}
///| Rasterise one glyph.
fn b_glyph(slot : Int, cp : Int, size : Int) -> RawGlyph {
let info : FixedArray[Int] = FixedArray::make(5, 0)
c_font_metrics(slot, cp, size, info)
let w = info[0]
let h = info[1]
let buf = if w > 0 && h > 0 {
let b = Bytes::make(w * h, b'\x00')
c_font_bitmap(slot, cp, size, w, h, b)
b
} else {
Bytes::make(0, b'\x00')
}
}
///| Install Allo Variable (UI text, falling back to Adwaita Sans) and Adwaita Mono (code) as the renderer's
/// faces. Returns false (keeping the bitmap font) when no face can be found.
/// Slots: 0 mono, 1 mono bold, 2 sans. Sans is a variable font whose bold
/// instance stb_truetype can't select, so bold sans is emboldened.
pub fn install() -> Bool {
let has_mono = load_first(0, regular_paths)
let has_sans = load_first(2, sans_paths)
if !has_mono && !has_sans {
return false
}
let has_bold = has_mono && load_first(1, bold_paths)
@renderer.set_glyph_source(fn(cp, size, bold, mono) {
if mono && has_mono {
if bold && has_bold {
rasterise(1, cp, size, false)
} else {
rasterise(0, cp, size, bold)
}
} else if has_sans {
rasterise(2, cp, size, bold)
} else {
rasterise(0, cp, size, bold)
}
})
true
{ w, h, xoff: info[2], yoff: info[3], advance: info[4], coverage: buf }
}
+22 -5
View File
@@ -1,7 +1,24 @@
///| The compositor as a wasm component has no C rasteriser. Until the host offers glyphs through
/// an import, text uses the renderer's built-in bitmap font.
///| Wasm font backend: the component has no C rasteriser, so glyphs come from the host's
/// `moonplasma:host/font` import (the host draws them from the TTF files it loads).
///| Install real fonts; always false here, so the renderer keeps its bitmap font.
pub fn install() -> Bool {
false
///| Load a TTF file into `slot`.
fn b_load(slot : Int, path : String) -> Bool {
@hostfont.load(slot.reinterpret_as_uint(), path)
}
///| Rasterise one glyph.
fn b_glyph(slot : Int, cp : Int, size : Int) -> RawGlyph {
let g = @hostfont.rasterise(
slot.reinterpret_as_uint(),
cp.reinterpret_as_uint(),
size.reinterpret_as_uint(),
)
{
w: g.width,
h: g.height,
xoff: g.xoff,
yoff: g.yoff,
advance: g.advance,
coverage: Bytes::from_fixedarray(g.coverage),
}
}
+1
View File
@@ -1,6 +1,7 @@
import {
"moonbitlang/core/builtin",
"moonplasma/desktop/compositor/renderer" @renderer,
"moonplasma/desktop/interface/moonplasma/host/font" @hostfont,
}
supported_targets = "+native+wasm"