diff --git a/compositor/font/font.mbt b/compositor/font/font.mbt new file mode 100644 index 0000000..e15d214 --- /dev/null +++ b/compositor/font/font.mbt @@ -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 +} diff --git a/compositor/font/font_native.mbt b/compositor/font/font_native.mbt index 504fc75..81ada6d 100644 --- a/compositor/font/font_native.mbt +++ b/compositor/font/font_native.mbt @@ -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 } } diff --git a/compositor/font/font_wasm.mbt b/compositor/font/font_wasm.mbt index 7731525..59fd0c7 100644 --- a/compositor/font/font_wasm.mbt +++ b/compositor/font/font_wasm.mbt @@ -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), + } } diff --git a/compositor/font/moon.pkg b/compositor/font/moon.pkg index 40b8728..76c7df0 100644 --- a/compositor/font/moon.pkg +++ b/compositor/font/moon.pkg @@ -1,6 +1,7 @@ import { "moonbitlang/core/builtin", "moonplasma/desktop/compositor/renderer" @renderer, + "moonplasma/desktop/interface/moonplasma/host/font" @hostfont, } supported_targets = "+native+wasm" diff --git a/host-wasm/Cargo.lock b/host-wasm/Cargo.lock index f481caf..53f447f 100644 --- a/host-wasm/Cargo.lock +++ b/host-wasm/Cargo.lock @@ -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" diff --git a/host-wasm/Cargo.toml b/host-wasm/Cargo.toml index d12550c..d64ffa6 100644 --- a/host-wasm/Cargo.toml +++ b/host-wasm/Cargo.toml @@ -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"] } diff --git a/host-wasm/src/main.rs b/host-wasm/src/main.rs index d6ed2ff..139ab38 100644 --- a/host-wasm/src/main.rs +++ b/host-wasm/src/main.rs @@ -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, bus_tried: bool, started: Option, + /// TTF faces loaded by the component (`font.load`), by slot. + fonts: [Option; 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 = Linker::new(&engine); moonplasma::host::sys::add_to_linker::>(&mut linker, |s| s)?; + moonplasma::host::font::add_to_linker::>(&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)?; diff --git a/interface/moonplasma/host/font/README.md b/interface/moonplasma/host/font/README.md new file mode 100644 index 0000000..897476d --- /dev/null +++ b/interface/moonplasma/host/font/README.md @@ -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). \ No newline at end of file diff --git a/interface/moonplasma/host/font/ffi.mbt b/interface/moonplasma/host/font/ffi.mbt new file mode 100644 index 0000000..76adefd --- /dev/null +++ b/interface/moonplasma/host/font/ffi.mbt @@ -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) + diff --git a/interface/moonplasma/host/font/moon.pkg b/interface/moonplasma/host/font/moon.pkg new file mode 100644 index 0000000..615617d --- /dev/null +++ b/interface/moonplasma/host/font/moon.pkg @@ -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"], + }, +) diff --git a/interface/moonplasma/host/font/native_stub.mbt b/interface/moonplasma/host/font/native_stub.mbt new file mode 100644 index 0000000..815a603 --- /dev/null +++ b/interface/moonplasma/host/font/native_stub.mbt @@ -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") +} diff --git a/interface/moonplasma/host/font/top.mbt b/interface/moonplasma/host/font/top.mbt new file mode 100644 index 0000000..dac561a --- /dev/null +++ b/interface/moonplasma/host/font/top.mbt @@ -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 + +} + diff --git a/interface/moonplasma/host/sys/ffi.mbt b/interface/moonplasma/host/sys/ffi.mbt index 642fa31..ebed274 100644 --- a/interface/moonplasma/host/sys/ffi.mbt +++ b/interface/moonplasma/host/sys/ffi.mbt @@ -73,32 +73,10 @@ fn wasmImportHasExe(p0 : Int, p1 : Int) -> Int = "moonplasma:host/sys@0.1.0" "ha fn wasmImportSpawn(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int = "moonplasma:host/sys@0.1.0" "spawn" ///| -#owned(str) -extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = +#owned(bytes) +extern "wasm" fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int = #|(func (param i32) (result i32) 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_free(position : Int) = -#|(func (param i32) local.get 0 call $moonbit.decref) - -///| -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_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_ptr2str(ptr : Int, len : Int) -> String = #|(func (param i32) (param i32) (result i32) @@ -107,7 +85,29 @@ extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = #| local.get 0) ///| -#owned(bytes) -extern "wasm" fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int = +#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) + diff --git a/interface/moonplasma/host/sys/native_stub.mbt b/interface/moonplasma/host/sys/native_stub.mbt index 69948c1..8ffc57b 100644 --- a/interface/moonplasma/host/sys/native_stub.mbt +++ b/interface/moonplasma/host/sys/native_stub.mbt @@ -121,27 +121,7 @@ fn wasmImportSpawn(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int { } ///| -fn mbt_ffi_str2ptr(str : String) -> Int { - abort("wasm-only") -} - -///| -fn mbt_ffi_load32(offset : Int) -> Int { - abort("wasm-only") -} - -///| -fn mbt_ffi_free(position : Int) -> Unit { - abort("wasm-only") -} - -///| -fn mbt_ffi_malloc(size : Int) -> Int { - abort("wasm-only") -} - -///| -fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] { +fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int { abort("wasm-only") } @@ -151,6 +131,26 @@ fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String { } ///| -fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int { +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") } diff --git a/wit/host/sys.wit b/wit/host/sys.wit index 0d1eea0..aeb8878 100644 --- a/wit/host/sys.wit +++ b/wit/host/sys.wit @@ -52,9 +52,29 @@ interface sys { spawn: func(cmd: string, display: string) -> bool; } +/// 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). +interface font { + /// Load the TTF file at `path` into `slot` (0..3). False when it can't be read or parsed. + load: func(slot: u32, path: string) -> bool; + + /// 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. + record glyph { + width: s32, + height: s32, + xoff: s32, + yoff: s32, + advance: s32, + coverage: list, + } + rasterise: func(slot: u32, codepoint: u32, size: u32) -> glyph; +} + /// The compositor as one component: it imports the OS surface and exports its entry point. world compositor { import sys; + import font; /// Run the compositor until it exits. `args` are the command-line arguments (without argv[0]). export run: func(args: list) -> s32; }