///| Wasm font backend: the component has no C rasteriser, so glyphs come from the host's /// `chandra:host/font` import (the host draws them from the TTF files it loads). ///| 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), } } ///| Cached `.notdef` glyph (cp 0 at 16 px) per slot for wasm glyph-presence checks. let wasm_notdef : Map[Int, RawGlyph] = {} ///| Cached `b_has_glyph` results keyed by `slot * 0x110000 + cp`. let wasm_has_cache : Map[Int, Bool] = {} ///| fn raw_glyph_eq(a : RawGlyph, b : RawGlyph) -> Bool { a.w == b.w && a.h == b.h && a.xoff == b.xoff && a.yoff == b.yoff && a.advance == b.advance && a.coverage == b.coverage } ///| Whether `slot` has a glyph for `cp` (not `.notdef`). fn b_has_glyph(slot : Int, cp : Int) -> Bool { if cp >= 0x1F000 { return false } let key = slot * 0x110000 + cp match wasm_has_cache.get(key) { Some(v) => return v None => () } let notdef = match wasm_notdef.get(slot) { Some(g) => g None => { let g = b_glyph(slot, 0, 16) wasm_notdef.set(slot, g) g } } let has = !raw_glyph_eq(b_glyph(slot, cp, 16), notdef) wasm_has_cache.set(key, has) has }