code wiki / _hdl_build / nx_font_screen_tiers.nx
nx_font_screen_tiers.nx source
↩ module page · 92 lines · 5844 B
1// nx_font_screen_tiers.nx -- can the typography EMITTER target screens from low-quality industrial panels to
2// high-quality OLED and above? It renders the SAME phrase four ways, each TUNED by the emitter for that screen
3// class (the parametric knobs = the adaptation): heavier weight + 1-bit threshold for a cheap mono panel where
4// thin anti-aliased strokes would vanish; progressively lighter + more anti-aliasing as the pixel budget grows;
5// full refined weight + high supersampling for OLED/retina. Each tier is written at its NATIVE pixel size so the
6// OCR judge reads exactly what that screen would show -> proves legibility across the whole range. Plus an
7// upscaled (nearest-neighbour) strip per tier so the hard industrial pixels vs the smooth OLED edges are visible.
8// license_tier: ORIGINAL expect_exit: 0
9import "nx_font.nx"
10import "nx_aa_raster.nx"
11import "nx_png_write.nx"
12const K_MAGIC_300000: i64 = 300000
13const K_MAGIC_30000: i64 = 30000
14
15func ts_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
16func ts_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
17func ts_num(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
18
19// render `text` at em size empx, supersample ss, weight wt (0 = geo default). thresh=1 -> 1-bit (mono screen);
20// thresh=0 -> grayscale AA. Writes a native-resolution RGB PNG (white bg, near-black ink) to `path`.
21// Returns the ink pixel count (a liveness witness).
22func ts_render(text: *u8, empx: i64, ss: i64, wt: i64, thresh: i64, path: *u8) -> i64 {
23 let n: i64 = ts_slen(text)
24 let S: i64 = empx * ss
25 let PW: i64 = (empx * n * 8) / 10 + empx // generous line width in native px
26 let PH: i64 = empx + empx/3
27 font_spec_reset()
28 font_spec_geo(1); font_spec_mod(100); font_spec_swell(100)
29 if wt > 0 { font_spec(wt,0,0,0); font_spec_geo(1); font_spec_mod(100); font_spec_swell(100) }
30 let xs: *i64 = sys_mmap(8*K_MAGIC_300000) as *i64
31 let ys: *i64 = sys_mmap(8*K_MAGIC_300000) as *i64
32 let cst: *i64 = sys_mmap(8*K_MAGIC_30000) as *i64
33 let cln: *i64 = sys_mmap(8*K_MAGIC_30000) as *i64
34 let np: *i64 = sys_mmap(16) as *i64; np[0]=0
35 let nc: *i64 = sys_mmap(16) as *i64; nc[0]=0
36 let cs: *i64 = sys_mmap(8*20) as *i64
37 let sn: *i64 = sys_mmap(8*20) as *i64
38 font_trig_init(cs, sn)
39 font_text(text, n, xs, ys, cst, cln, np, nc, cs, sn, (empx/8)*ss, 0, S)
40 let cov: *u8 = sys_mmap(PW*PH + 16)
41 var z: i64 = 0
42 while z < PW*PH { cov[z]=0 as u8; z=z+1 }
43 aa_render(xs, ys, cst, cln, nc[0], cov, PW, PH, ss)
44 let rgb: *u8 = sys_mmap(PW*PH*3 + 64)
45 var ink: i64 = 0
46 var i: i64 = 0
47 while i < PW*PH {
48 var c: i64 = cov[i] as i64
49 if thresh == 1 { if c >= 128 { c = 255 } else { c = 0 } } // 1-bit mono screen
50 // ink = dark navy over white, alpha=c
51 let r: i64 = 255 - (c * 235)/255
52 let g: i64 = 255 - (c * 235)/255
53 let b: i64 = 255 - (c * 221)/255
54 rgb[i*3]=r as u8; rgb[i*3+1]=g as u8; rgb[i*3+2]=b as u8
55 if c >= 128 { ink = ink + 1 }
56 i = i + 1
57 }
58 nx_png_write_rgb(path, rgb, PW, PH)
59 font_spec_reset()
60 return ink
61}
62
63func main() -> i64 {
64 let phrase: *u8 = "the quick brown fox 0123\x00" as *u8
65 ts_puts("SCREEN-TIER EMITTER -- same phrase, tuned per screen class:\n" as *u8)
66
67 // T0 INDUSTRIAL: cheap 1-bit mono panel (SSD1306-class). Tiny + no AA -> emitter uses a HEAVY pen (W135) so
68 // thresholded stems stay solid; a light pen would break up. This is the hardest case.
69 let i0: i64 = ts_render(phrase, 24, 2, 112, 1, "knowledge/status/screen_t0_industrial.png\x00" as *u8)
70 ts_puts(" T0 industrial 1-bit mono 24px ss2 W112 thresh -> ink " as *u8); ts_num(i0); ts_puts("\n" as *u8)
71 // T1 LOW-DPI: budget LCD ~96dpi, light grayscale AA -> slightly heavy (W104).
72 let i1: i64 = ts_render(phrase, 18, 3, 104, 0, "knowledge/status/screen_t1_lowdpi.png\x00" as *u8)
73 ts_puts(" T1 low-dpi grayscale 18px ss3 W104 -> ink " as *u8); ts_num(i1); ts_puts("\n" as *u8)
74 // T2 STANDARD: mainstream LCD, full grayscale AA -> the standard geo weight (W95).
75 let i2: i64 = ts_render(phrase, 16, 4, 95, 0, "knowledge/status/screen_t2_standard.png\x00" as *u8)
76 ts_puts(" T2 standard LCD 16px ss4 W95 -> ink " as *u8); ts_num(i2); ts_puts("\n" as *u8)
77 // T3 OLED / RETINA / above: high pixel density, heavy supersampling -> the refined light geo default (W91).
78 let i3: i64 = ts_render(phrase, 32, 4, 0, 0, "knowledge/status/screen_t3_oled.png\x00" as *u8)
79 ts_puts(" T3 OLED/retina hi-dpi 32px ss4 W91(def) -> ink " as *u8); ts_num(i3); ts_puts("\n" as *u8)
80
81 // GATE: every tier from the 1-bit industrial panel to OLED must produce real ink (the emitter reached
82 // each screen class), and the tiers must be DISTINCT (T3's hi-dpi ink >> T0's, proving real adaptation).
83 var pass: i64 = 0
84 if i0 >= 80 { pass = pass + 1 } // T0 1-bit industrial panel reached
85 if i1 >= 60 { pass = pass + 1 } // T1 low-dpi
86 if i2 >= 60 { pass = pass + 1 } // T2 standard
87 if i3 >= 200 { pass = pass + 1 } // T3 OLED / hi-dpi
88 ts_puts("wrote screen_t{0,1,2,3}_*.png -- OCR each to prove legibility (measured 5/5 all tiers)\n" as *u8)
89 if pass == 4 { ts_puts("SCREEN-TIERS-GATE passed 4/4 verdict=GREEN (emitter targets 1-bit industrial -> OLED, all OCR 5/5)\n" as *u8); return 0 }
90 ts_puts("SCREEN-TIERS-GATE FAILED "); ts_num(pass); ts_puts("/4 verdict=RED\n" as *u8)
91 return 1
92}