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
+398
-133
No files matched your search
@@ -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
|
||||||
|
}
|
||||||
@@ -1,6 +1,4 @@
|
|||||||
///| Real text rendering for the native compositor: Adwaita Mono rasterised with
|
///| Native font backend: glyphs from stb_truetype through a C shim (font_stub.c).
|
||||||
/// 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.
|
|
||||||
|
|
||||||
///|
|
///|
|
||||||
#borrow(path)
|
#borrow(path)
|
||||||
@@ -49,45 +47,13 @@ fn utf8(s : String) -> Bytes {
|
|||||||
Bytes::from_array(out)
|
Bytes::from_array(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
///| Where to look for each face: bundled copy first, then the system one.
|
///| Load a TTF file into `slot`.
|
||||||
let regular_paths : Array[String] = [
|
fn b_load(slot : Int, path : String) -> Bool {
|
||||||
"assets/fonts/AdwaitaMono-Regular.ttf",
|
c_font_load(slot, utf8(path)) == 0
|
||||||
"/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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///|
|
///| Rasterise one glyph.
|
||||||
let cache : Map[Int, @renderer.Glyph] = {}
|
fn b_glyph(slot : Int, cp : Int, size : Int) -> RawGlyph {
|
||||||
|
|
||||||
///|
|
|
||||||
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)
|
let info : FixedArray[Int] = FixedArray::make(5, 0)
|
||||||
c_font_metrics(slot, cp, size, info)
|
c_font_metrics(slot, cp, size, info)
|
||||||
let w = info[0]
|
let w = info[0]
|
||||||
@@ -99,44 +65,5 @@ fn rasterise(slot : Int, cp : Int, size : Int, embolden : Bool) -> @renderer.Gly
|
|||||||
} else {
|
} else {
|
||||||
Bytes::make(0, b'\x00')
|
Bytes::make(0, b'\x00')
|
||||||
}
|
}
|
||||||
let g : @renderer.Glyph = {
|
{ w, h, xoff: info[2], yoff: info[3], advance: info[4], coverage: buf }
|
||||||
w,
|
|
||||||
h,
|
|
||||||
xoff: info[2],
|
|
||||||
yoff: info[3],
|
|
||||||
advance: info[4],
|
|
||||||
coverage: buf,
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,24 @@
|
|||||||
///| The compositor as a wasm component has no C rasteriser. Until the host offers glyphs through
|
///| Wasm font backend: the component has no C rasteriser, so glyphs come from the host's
|
||||||
/// an import, text uses the renderer's built-in bitmap font.
|
/// `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.
|
///| Load a TTF file into `slot`.
|
||||||
pub fn install() -> Bool {
|
fn b_load(slot : Int, path : String) -> Bool {
|
||||||
false
|
@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,6 +1,7 @@
|
|||||||
import {
|
import {
|
||||||
"moonbitlang/core/builtin",
|
"moonbitlang/core/builtin",
|
||||||
"moonplasma/desktop/compositor/renderer" @renderer,
|
"moonplasma/desktop/compositor/renderer" @renderer,
|
||||||
|
"moonplasma/desktop/interface/moonplasma/host/font" @hostfont,
|
||||||
}
|
}
|
||||||
|
|
||||||
supported_targets = "+native+wasm"
|
supported_targets = "+native+wasm"
|
||||||
|
|||||||
Generated
+33
@@ -2,6 +2,22 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 4
|
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]]
|
[[package]]
|
||||||
name = "addr2line"
|
name = "addr2line"
|
||||||
version = "0.26.1"
|
version = "0.26.1"
|
||||||
@@ -552,6 +568,8 @@ dependencies = [
|
|||||||
name = "moon-plasma-host"
|
name = "moon-plasma-host"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"ab_glyph",
|
||||||
|
"ab_glyph_rasterizer",
|
||||||
"libc",
|
"libc",
|
||||||
"wasmtime",
|
"wasmtime",
|
||||||
]
|
]
|
||||||
@@ -580,6 +598,15 @@ version = "1.21.4"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
|
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]]
|
[[package]]
|
||||||
name = "pin-project-lite"
|
name = "pin-project-lite"
|
||||||
version = "0.2.18"
|
version = "0.2.18"
|
||||||
@@ -834,6 +861,12 @@ dependencies = [
|
|||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ttf-parser"
|
||||||
|
version = "0.25.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d2df906b07856748fa3f6e0ad0cbaa047052d4a7dd609e231c4f72cee8c36f31"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "typenum"
|
name = "typenum"
|
||||||
version = "1.20.1"
|
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)."
|
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]
|
[dependencies]
|
||||||
|
ab_glyph = "0.2"
|
||||||
|
ab_glyph_rasterizer = "0.1"
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
wasmtime = { version = "50", default-features = false, features = ["component-model", "cranelift", "runtime", "std"] }
|
wasmtime = { version = "50", default-features = false, features = ["component-model", "cranelift", "runtime", "std"] }
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,9 @@ wasmtime::component::bindgen!({
|
|||||||
world: "compositor",
|
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};
|
use moonplasma::host::sys::{Host, Received};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
@@ -28,6 +31,8 @@ struct State {
|
|||||||
private_bus: Option<String>,
|
private_bus: Option<String>,
|
||||||
bus_tried: bool,
|
bus_tried: bool,
|
||||||
started: Option<std::time::Instant>,
|
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) {
|
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 {
|
impl Host for State {
|
||||||
fn unix_listen(&mut self, path: String) -> i32 {
|
fn unix_listen(&mut self, path: String) -> i32 {
|
||||||
let _ = std::fs::remove_file(&path);
|
let _ = std::fs::remove_file(&path);
|
||||||
@@ -423,6 +477,7 @@ fn main() -> Result<()> {
|
|||||||
};
|
};
|
||||||
let mut linker: Linker<State> = Linker::new(&engine);
|
let mut linker: Linker<State> = Linker::new(&engine);
|
||||||
moonplasma::host::sys::add_to_linker::<State, HasSelf<State>>(&mut linker, |s| s)?;
|
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 mut store = Store::new(&engine, State::default());
|
||||||
let compositor = Compositor::instantiate(&mut store, &component, &linker)?;
|
let compositor = Compositor::instantiate(&mut store, &component, &linker)?;
|
||||||
let code = compositor.call_run(&mut store, &args)?;
|
let code = compositor.call_run(&mut store, &args)?;
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -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"
|
fn wasmImportSpawn(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int = "moonplasma:host/sys@0.1.0" "spawn"
|
||||||
|
|
||||||
///|
|
///|
|
||||||
#owned(str)
|
#owned(bytes)
|
||||||
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
|
extern "wasm" fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int =
|
||||||
#|(func (param i32) (result i32) local.get 0)
|
#|(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 =
|
extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String =
|
||||||
#|(func (param i32) (param i32) (result i32)
|
#|(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)
|
#| local.get 0)
|
||||||
|
|
||||||
///|
|
///|
|
||||||
#owned(bytes)
|
#owned(str)
|
||||||
extern "wasm" fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int =
|
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
|
||||||
#|(func (param i32) (result i32) local.get 0)
|
#|(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)
|
||||||
|
|
||||||
@@ -121,27 +121,7 @@ fn wasmImportSpawn(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
///|
|
///|
|
||||||
fn mbt_ffi_str2ptr(str : String) -> Int {
|
fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> 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] {
|
|
||||||
abort("wasm-only")
|
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")
|
abort("wasm-only")
|
||||||
}
|
}
|
||||||
@@ -52,9 +52,29 @@ interface sys {
|
|||||||
spawn: func(cmd: string, display: string) -> bool;
|
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<u8>,
|
||||||
|
}
|
||||||
|
rasterise: func(slot: u32, codepoint: u32, size: u32) -> glyph;
|
||||||
|
}
|
||||||
|
|
||||||
/// The compositor as one component: it imports the OS surface and exports its entry point.
|
/// The compositor as one component: it imports the OS surface and exports its entry point.
|
||||||
world compositor {
|
world compositor {
|
||||||
import sys;
|
import sys;
|
||||||
|
import font;
|
||||||
/// Run the compositor until it exits. `args` are the command-line arguments (without argv[0]).
|
/// Run the compositor until it exits. `args` are the command-line arguments (without argv[0]).
|
||||||
export run: func(args: list<string>) -> s32;
|
export run: func(args: list<string>) -> s32;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user