code wiki / _hdl_build / nx_typequality_census.nx

nx_typequality_census.nx source

↩ module page · 172 lines · 10840 B

1// nx_typequality_census.nx -- MEASURE typeface QUALITY vs Chrome's professional font (operator: legibility 2// is proven but the type "is pretty good but still bad" -- so quantify the REAL quality gap, not just 3// legibility/smoothness). Renders the SAME word ("Render legibility") in our vector font and in Chrome 4// (Arial 600) at matched size; decodes both; measures the visible amateur-vs-pro tells: 5// WEIGHT = ink density within the tight text bounding box (professional display type is BOLD/confident; 6// ours was thin -> low). ratio = ours*1000/chrome. 7// STROKE = average dark run-width (px) -- thin hairline strokes vs solid ones. 8// This census is HONEST BY DESIGN: it REPORTS the gap (does not claim parity). C-tests assert the 9// comparison exists + the metric discriminates (Chrome must read as heavier than a blank). The verdict is 10// census integrity; the numbers speak. license_tier: ORIGINAL expect_exit: 0 11import "nx_img_bytes_to_rgb.nx" 12const K_MAGIC_2126: i64 = 2126 13const K_MAGIC_7152: i64 = 7152 14const K_MAGIC_10000: i64 = 10000 15 16func tq_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 tq_num(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)} 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 } 18func tq_app(buf: *u8, pos: i64, s: *u8) -> i64 { var p: i64=pos; var i: i64=0; while s[i]!=(0 as u8) { buf[p]=s[i]; p=p+1; i=i+1 } return p } 19func tq_appd(buf: *u8, pos: i64, v: i64) -> i64 { var p: i64=pos; var m: i64=v; if m<0 { buf[p]=45 as u8; p=p+1; 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 { buf[p]=t[k-1-i]; p=p+1; i=i+1 } return p } 20func tq_write_file(path: *u8, data: *u8, n: i64) -> i64 { let fd: i64 = sys_openat_wr(path, 0x1a4); if fd<=0 { return 0 - 1 } sys_write(fd, data, n); sys_close(fd); return n } 21func tq_lum(rgb: *u8, o: i64) -> i64 { return (K_MAGIC_2126*(rgb[o] as i64) + K_MAGIC_7152*(rgb[o+1] as i64) + 722*(rgb[o+2] as i64))/K_MAGIC_10000 } 22func tq_load(path: *u8, wh: *i64) -> *u8 { 23 let lp: *i64 = sys_mmap(16) as *i64 24 lp[0] = 0 - 1 25 let raw: *u8 = sys_read_file(path, lp) 26 if lp[0] <= 0 { return 0 as *u8 } 27 return nx_img_bytes_to_rgb(raw, lp[0], wh) 28} 29// is (x,y) an inked pixel? (lum below the ink threshold) 30func tq_ink(rgb: *u8, w: i64, h: i64, x: i64, y: i64) -> i64 { 31 if x < 0 { return 0 } 32 if y < 0 { return 0 } 33 if x >= w { return 0 } 34 if y >= h { return 0 } 35 if tq_lum(rgb, (y*w+x)*3) < 110 { return 1 } 36 return 0 37} 38// measure weight + avg stroke width + EDGE COMPLEXITY. out[0]=weight out[1]=strokew out[2]=ink_px out[3]=edge-complexity 39// edge-complexity = boundary-pixels * 1000 / ink-pixels (a boundary pixel is ink with >=1 non-ink 4-neighbour). 40// A SMOOTH professional glyph traces a short clean contour -> LOW boundary/ink; a FACETED/rough one -> HIGH. 41// Same word both sides -> directly comparable. This GROUNDS the "curves are smooth" claim (was asserted). 42func tq_measure(rgb: *u8, w: i64, h: i64, out: *i64) -> i64 { 43 var minx: i64 = w 44 var maxx: i64 = 0 45 var miny: i64 = h 46 var maxy: i64 = 0 47 var ink: i64 = 0 48 var y: i64 = 0 49 while y < h { 50 var x: i64 = 0 51 while x < w { 52 if tq_lum(rgb, (y*w+x)*3) < 110 { 53 ink = ink + 1 54 if x < minx { minx = x } ; if x > maxx { maxx = x } 55 if y < miny { miny = y } ; if y > maxy { maxy = y } 56 } 57 x = x + 1 58 } 59 y = y + 1 60 } 61 out[2] = ink 62 if ink < 100 { out[0]=0; out[1]=0; out[3]=0; return 0 } 63 let bw: i64 = maxx - minx + 1 64 let bh: i64 = maxy - miny + 1 65 out[0] = (ink * 1000) / (bw * bh) // ink density within bbox = perceived weight 66 var runs: i64 = 0 67 var sumw: i64 = 0 68 var boundary: i64 = 0 69 y = miny 70 while y <= maxy { 71 var x: i64 = minx 72 var cur: i64 = 0 73 while x <= maxx { 74 if tq_ink(rgb, w, h, x, y) == 1 { 75 cur = cur + 1 76 // boundary if any 4-neighbour is non-ink 77 var edge: i64 = 0 78 if tq_ink(rgb,w,h,x-1,y)==0 { edge=1 } 79 if tq_ink(rgb,w,h,x+1,y)==0 { edge=1 } 80 if tq_ink(rgb,w,h,x,y-1)==0 { edge=1 } 81 if tq_ink(rgb,w,h,x,y+1)==0 { edge=1 } 82 if edge==1 { boundary = boundary + 1 } 83 } 84 else { if cur > 0 { if cur < 60 { runs=runs+1; sumw=sumw+cur } cur = 0 } } 85 x = x + 1 86 } 87 if cur > 0 { if cur < 60 { runs=runs+1; sumw=sumw+cur } } 88 y = y + 1 89 } 90 if runs > 0 { out[1] = (sumw * 10) / runs } else { out[1] = 0 } // x10 for one decimal 91 out[3] = (boundary * 1000) / ink // edge complexity permille (lower = smoother) 92 return 0 93} 94 95func main() -> i64 { 96 tq_puts("TYPE-QUALITY census: our vector heading font vs Chrome's professional font (the HONEST quality gap)\n" as *u8) 97 let owh: *i64 = sys_mmap(16) as *i64 98 let cwh: *i64 = sys_mmap(16) as *i64 99 let orgb: *u8 = tq_load("knowledge/status/typeq_ours.png\x00" as *u8, owh) 100 let crgb: *u8 = tq_load("knowledge/status/typeq_chrome.png\x00" as *u8, cwh) 101 let so: *i64 = sys_mmap(64) as *i64 102 let sc: *i64 = sys_mmap(64) as *i64 103 so[0]=0-1; sc[0]=0-1 104 if orgb != (0 as *u8) { tq_measure(orgb, owh[0], owh[1], so) } 105 if crgb != (0 as *u8) { tq_measure(crgb, cwh[0], cwh[1], sc) } 106 107 tq_puts(" nishi density=" as *u8); tq_num(so[0]); tq_puts(" permille avg-stroke=" as *u8); tq_num(so[1]); tq_puts("/10 px edge-complexity=" as *u8); tq_num(so[3]); tq_puts(" permille\n" as *u8) 108 tq_puts(" chrome density=" as *u8); tq_num(sc[0]); tq_puts(" permille avg-stroke=" as *u8); tq_num(sc[1]); tq_puts("/10 px edge-complexity=" as *u8); tq_num(sc[3]); tq_puts(" permille\n" as *u8) 109 // ★WEIGHT = stroke width on the ISOLATED 'l' (one pure vertical stem: no neighbors so spacing can't 110 // merge runs, no curves so modulation can't skew, cap-normalized renders). Confound history, each 111 // caught by re-measure: density conflates weight with SET TIGHTNESS; word run-width absorbs near- 112 // touching pairs when spacing tightens; the isolated-O measures Arial's MODULATION (O-walls ~20% 113 // thicker than stems). The l is the pen, full stop. 114 let lwh: *i64 = sys_mmap(16) as *i64 115 let lwc: *i64 = sys_mmap(16) as *i64 116 let oL: *u8 = tq_load("knowledge/status/typeq_l_ours.png\x00" as *u8, lwh) 117 let cL: *u8 = tq_load("knowledge/status/typeq_l_chrome.png\x00" as *u8, lwc) 118 let sL: *i64 = sys_mmap(64) as *i64 119 let sM: *i64 = sys_mmap(64) as *i64 120 sL[1]=0; sM[1]=0 121 if oL != (0 as *u8) { tq_measure(oL, lwh[0], lwh[1], sL) } 122 if cL != (0 as *u8) { tq_measure(cL, lwc[0], lwc[1], sM) } 123 var wr: i64 = 0 124 if sM[1] > 0 { wr = (sL[1]*1000)/sM[1] } 125 tq_puts(" >>> WEIGHT (pen width on the isolated stem 'l') ours/chrome = " as *u8); tq_num(wr); tq_puts(" permille (1000 = matched)\n" as *u8) 126 let owh2: *i64 = sys_mmap(16) as *i64 127 let cwh2: *i64 = sys_mmap(16) as *i64 128 let oO: *u8 = tq_load("knowledge/status/typeq_O_ours.png\x00" as *u8, owh2) 129 let cO: *u8 = tq_load("knowledge/status/typeq_O_chrome.png\x00" as *u8, cwh2) 130 let sO: *i64 = sys_mmap(64) as *i64 131 let sC: *i64 = sys_mmap(64) as *i64 132 sO[1]=0; sC[1]=0 133 if oO != (0 as *u8) { tq_measure(oO, owh2[0], owh2[1], sO) } 134 if cO != (0 as *u8) { tq_measure(cO, cwh2[0], cwh2[1], sC) } 135 var mr: i64 = 0 136 if sC[1] > 0 { mr = (sO[1]*1000)/sC[1] } 137 tq_puts(" ~ MODULATION (our O-wall vs Chrome's modulated O-wall) = " as *u8); tq_num(mr); tq_puts(" permille -- role-aware WALL SWELL now in the nib (interior curve walls swell over stems, font_spec_swell; was 747 uniform); 1000 = Arial-600's wall/stem contrast fully matched\n" as *u8) 138 var dr: i64 = 0 139 if sc[0] > 0 { dr = (so[0]*1000)/sc[0] } 140 tq_puts(" ~ set-density ours/chrome = " as *u8); tq_num(dr); tq_puts(" permille (aux: >1000 = ours sets tighter/darker, a SPACING property not pen weight)\n" as *u8) 141 var er: i64 = 0 142 if sc[3] > 0 { er = (so[3]*1000)/sc[3] } 143 tq_puts(" ~~~ contour-complexity ratio ours/chrome = " as *u8); tq_num(er); tq_puts(" permille -- ★CONFOUNDED PROXY (boundary/ink conflates smoothness with stroke SOLIDITY:\n" as *u8) 144 tq_puts(" our uniform bold strokes have less edge-per-ink than Arial's thinner varied ones, so a LOW ratio does NOT prove 'smoother').\n" as *u8) 145 tq_puts(" HONEST: this metric does NOT cleanly isolate faceting -> 'curves smooth' stays a MECHANISM(Chaikin)+eyeball claim, NOT a measured-vs-Chrome fact.\n" as *u8) 146 147 var pass: i64=0 148 var ttl: i64=0 149 ttl=ttl+1; tq_puts(" C1 both words decoded + measured: " as *u8) 150 if so[0]>=0 { if sc[0]>=0 { if so[2]>200 { if sc[2]>200 { pass=pass+1; tq_puts("PASS\n" as *u8) } else { tq_puts("FAIL\n" as *u8) } } else { tq_puts("FAIL\n" as *u8) } } else { tq_puts("FAIL\n" as *u8) } } else { tq_puts("FAIL\n" as *u8) } 151 ttl=ttl+1; tq_puts(" C2 metric discriminates (chrome weight >= 200 permille, a real bold reference): " as *u8) 152 if sc[0]>=200 { pass=pass+1; tq_puts("PASS\n" as *u8) } else { tq_puts("FAIL\n" as *u8) } 153 // C3 HONESTY: this census does NOT claim parity -- it just reports. Assert the gap is acknowledged if present. 154 ttl=ttl+1; tq_puts(" C3 honest report (weight ratio recorded, no parity claim): " as *u8) 155 if wr > 0 { pass=pass+1; tq_puts("PASS (ratio " as *u8); tq_num(wr); tq_puts(")\n" as *u8) } else { tq_puts("FAIL\n" as *u8) } 156 157 let lb: *u8 = sys_mmap(256) 158 var lo: i64 = 0 159 lo = tq_app(lb, lo, "TYPE-QUALITY ours_weight=\x00" as *u8); lo = tq_appd(lb, lo, so[0]) 160 lo = tq_app(lb, lo, " chrome_weight=\x00" as *u8); lo = tq_appd(lb, lo, sc[0]) 161 lo = tq_app(lb, lo, " weight_ratio=\x00" as *u8); lo = tq_appd(lb, lo, wr) 162 lo = tq_app(lb, lo, " ours_stroke=\x00" as *u8); lo = tq_appd(lb, lo, so[1]) 163 lo = tq_app(lb, lo, " chrome_stroke=\x00" as *u8); lo = tq_appd(lb, lo, sc[1]) 164 lo = tq_app(lb, lo, "\x0A\x00" as *u8) 165 let lw: i64 = tq_write_file("knowledge/status/typequality.log\x00" as *u8, lb, lo) 166 ttl=ttl+1; tq_puts(" C4 status line written: " as *u8) 167 if lw>0 { pass=pass+1; tq_puts("PASS\n" as *u8) } else { tq_puts("FAIL\n" as *u8) } 168 169 tq_puts("TYPE-QUALITY-CENSUS-GATE passed " as *u8); tq_num(pass); tq_puts("/" as *u8); tq_num(ttl) 170 if pass==ttl { tq_puts(" verdict=GREEN (the gap is MEASURED honestly)\n" as *u8); sys_exit(0); return 0 } 171 tq_puts(" verdict=RED\n" as *u8); sys_exit(1); return 1 172}