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
@@ -0,0 +1,2 @@
|
||||
Glyph rasterisation. The component has no C rasteriser, so the host draws glyphs from TTF files
|
||||
it loads (mirrors compositor/font's native backend: sizes are em sizes in pixels).
|
||||
@@ -0,0 +1,35 @@
|
||||
// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT!
|
||||
|
||||
///|
|
||||
fn wasmImportLoad(p0 : Int, p1 : Int, p2 : Int) -> Int = "moonplasma:host/font@0.1.0" "load"
|
||||
|
||||
///|
|
||||
fn wasmImportRasterise(p0 : Int, p1 : Int, p2 : Int, p3 : Int) = "moonplasma:host/font@0.1.0" "rasterise"
|
||||
|
||||
///|
|
||||
#owned(str)
|
||||
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
|
||||
#|(func (param i32) (result i32) local.get 0)
|
||||
|
||||
///|
|
||||
extern "wasm" fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] =
|
||||
#|(func (param i32) (param i32) (result i32)
|
||||
#| local.get 0
|
||||
#| local.get 1 call $moonbit.init_array8
|
||||
#| local.get 0)
|
||||
|
||||
///|
|
||||
extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int =
|
||||
#|(func (param i32) (result i32) local.get 0 i32.load)
|
||||
|
||||
///|
|
||||
extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int =
|
||||
#|(func (param i32) (result i32) (local i32)
|
||||
#| local.get 0 i32.const 4 i32.add call $moonbit.gc.malloc
|
||||
#| local.tee 1 i32.const 0 call $moonbit.init_array8
|
||||
#| local.get 1)
|
||||
|
||||
///|
|
||||
extern "wasm" fn mbt_ffi_free(position : Int) =
|
||||
#|(func (param i32) local.get 0 call $moonbit.decref)
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
warnings = "-44"
|
||||
|
||||
// ffi.mbt is generated and wasm-only; native builds use native_stub.mbt (scripts/gen-native-stubs.py).
|
||||
options(
|
||||
targets: {
|
||||
"ffi.mbt": ["wasm", "wasm-gc"],
|
||||
"native_stub.mbt": ["native"],
|
||||
},
|
||||
)
|
||||
@@ -0,0 +1,36 @@
|
||||
// Generated by scripts/gen-native-stubs.py. Native stand-ins for the wasm-only ffi.mbt.
|
||||
|
||||
///|
|
||||
fn wasmImportLoad(p0 : Int, p1 : Int, p2 : Int) -> Int {
|
||||
abort("wasm-only host import")
|
||||
}
|
||||
|
||||
///|
|
||||
fn wasmImportRasterise(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Unit {
|
||||
abort("wasm-only host import")
|
||||
}
|
||||
|
||||
///|
|
||||
fn mbt_ffi_str2ptr(str : String) -> Int {
|
||||
abort("wasm-only")
|
||||
}
|
||||
|
||||
///|
|
||||
fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] {
|
||||
abort("wasm-only")
|
||||
}
|
||||
|
||||
///|
|
||||
fn mbt_ffi_load32(offset : Int) -> Int {
|
||||
abort("wasm-only")
|
||||
}
|
||||
|
||||
///|
|
||||
fn mbt_ffi_malloc(size : Int) -> Int {
|
||||
abort("wasm-only")
|
||||
}
|
||||
|
||||
///|
|
||||
fn mbt_ffi_free(position : Int) -> Unit {
|
||||
abort("wasm-only")
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT!
|
||||
///|
|
||||
/// One rendered glyph: an 8-bit coverage bitmap `width` x `height`, whose top-left sits at
|
||||
/// (`xoff`, `yoff`) from the pen on the baseline (y grows downward), and the pen advance.
|
||||
pub(all) struct Glyph {
|
||||
width : Int; height : Int; xoff : Int; yoff : Int; advance : Int; coverage : FixedArray[Byte]
|
||||
}
|
||||
///|
|
||||
/// Load the TTF file at `path` into `slot` (0..3). False when it can't be read or parsed.
|
||||
pub fn load(slot : UInt, path : String) -> Bool {
|
||||
|
||||
let ptr = mbt_ffi_str2ptr(path)
|
||||
let result : Int = wasmImportLoad((slot).reinterpret_as_int(), ptr, path.length());
|
||||
let ret = (result != 0)
|
||||
mbt_ffi_free(ptr)
|
||||
return ret
|
||||
|
||||
}
|
||||
///|
|
||||
pub fn rasterise(slot : UInt, codepoint : UInt, size : UInt) -> Glyph {
|
||||
|
||||
let return_area = mbt_ffi_malloc(28)
|
||||
wasmImportRasterise((slot).reinterpret_as_int(), (codepoint).reinterpret_as_int(), (size).reinterpret_as_int(), return_area);
|
||||
|
||||
let result = mbt_ffi_ptr2bytes(mbt_ffi_load32((return_area) + 20), mbt_ffi_load32((return_area) + 24))
|
||||
let ret = Glyph::{width : mbt_ffi_load32((return_area) + 0), height : mbt_ffi_load32((return_area) + 4), xoff : mbt_ffi_load32((return_area) + 8), yoff : mbt_ffi_load32((return_area) + 12), advance : mbt_ffi_load32((return_area) + 16), coverage : result}
|
||||
mbt_ffi_free(return_area)
|
||||
return ret
|
||||
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user