nx_nishi_font.nx source
↩ module page · 64 lines · 2691 B
1// nx_nishi_font.nx -- the SYSCALL-backed u8/PNG path of the sovereign Nishi stroke font. The PURE glyph core
2// (all stroke designs N I S H B R O W E + 0-9 + the WASM-safe i64-framebuffer blit) now lives in the
3// import-free nx_nishi_font_core.nx; this file adds nf_render_glyph (black AA ink onto a WHITE RGB u8 buffer,
4// allocates its own supersample mask via sys_mmap) + nf_draw_text (u8 string), used by the demo + the gate.
5// Splitting the pure core out lets the browser arcade (nx_wasm_arcade) reuse the EXACT glyphs with no syscalls.
6// All integer (no float). license_tier: ORIGINAL
7import "nx_syscalls.nx"
8import "nx_png_write.nx"
9import "nx_nishi_font_core.nx"
10
11// render glyph `gid` at (ox,oy) cell-height ch into a white RGB buffer (black AA ink). Returns advance px.
12func nf_render_glyph(rgb: *u8, iw: i64, ih: i64, ox: i64, oy: i64, ch: i64, gid: i64) -> i64 {
13 let adv: i64 = ch * NF_ADV_U / NF_EM
14 let mw: i64 = adv * NF_SS
15 let mh: i64 = ch * NF_SS
16 var r: i64 = ch * NF_SS / 16
17 if r < 1 { r = 1 }
18 let mask: *u8 = sys_mmap(mw * mh + 16)
19 nf_glyph(mask, mw, mh, gid, ch, r)
20 let ss2: i64 = NF_SS * NF_SS
21 var oy2: i64 = 0
22 while oy2 < ch {
23 var ox2: i64 = 0
24 while ox2 < adv {
25 var cov: i64 = 0
26 var by: i64 = oy2 * NF_SS
27 let byend: i64 = by + NF_SS
28 while by < byend {
29 var bx: i64 = ox2 * NF_SS
30 let bxend: i64 = bx + NF_SS
31 while bx < bxend { if (mask[by*mw + bx]&0xff) == 1 { cov = cov + 1 } bx = bx + 1 }
32 by = by + 1
33 }
34 if cov > 0 {
35 let alpha: i64 = cov * 255 / ss2
36 let gray: i64 = 255 - alpha
37 let px: i64 = oy + oy2
38 let pxx: i64 = ox + ox2
39 if px >= 0 { if px < ih { if pxx >= 0 { if pxx < iw {
40 let idx: i64 = (px*iw + pxx) * 3
41 // darken toward black (so overlapping strokes don't lighten)
42 if (gray as i64) < (rgb[idx]&0xff) { rgb[idx]=gray as u8; rgb[idx+1]=gray as u8; rgb[idx+2]=gray as u8 }
43 } } } }
44 }
45 ox2 = ox2 + 1
46 }
47 oy2 = oy2 + 1
48 }
49 return adv
50}
51// draw a NUL/len string at (ox,oy) cell-height ch; returns the x advance used.
52func nf_draw_text(rgb: *u8, iw: i64, ih: i64, ox: i64, oy: i64, ch: i64, s: *u8, slen: i64) -> i64 {
53 var x: i64 = ox
54 var i: i64 = 0
55 while i < slen {
56 let c: i64 = s[i] & 0xff
57 let adv: i64 = nf_render_glyph(rgb, iw, ih, x, oy, ch, c)
58 x = x + adv
59 i = i + 1
60 }
61 return x - ox
62}
63
64func main() -> i64 { return 0 }