code wiki / _hdl_build / nx_bg_shorthand_gate.nx
nx_bg_shorthand_gate.nx source
↩ module page · 84 lines · 5226 B
1// nx_bg_shorthand_gate.nx -- VERDICT gate for the CSS `background` SHORTHAND color (background:red,
2// background:#fff, background:url(x) blue ...). Reftests: shorthand renders IDENTICALLY to the
3// background-color longhand (diff=0), the shorthand actually paints (real red, not blank), a color is
4// extracted PAST a url() token, and a LIAR-KILL (shorthand red vs longhand green differ). 100% sovereign.
5import "nx_syscalls.nx"
6import "nx_render_html.nx"
7
8func g_w(s: *u8) -> i64 { var k: i64=0; while s[k]!=(0 as u8){k=k+1} sys_write(1,s,k); return 0 }
9func pn(v0: i64) -> i64 { var v: i64=v0; if v<0 { let m: *u8=sys_mmap(8); m[0]=45 as u8; sys_write(1,m,1); v=0-v } let b: *u8=sys_mmap(24); var k: i64=0; if v==0 {b[0]=48;k=1} while v>0 {b[k]=(48+(v%10)) as u8; v=v/10; k=k+1} let o: *u8=sys_mmap(24); var j: i64=0; while j<k {o[j]=b[k-1-j];j=j+1} sys_write(1,o,k); return 0 }
10func slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
11func u32le(d: *u8, p: i64) -> i64 { return (d[p]&0xff) + ((d[p+1]&0xff)<<8) + ((d[p+2]&0xff)<<16) + ((d[p+3]&0xff)<<24) }
12func bmp_diff(a: *u8, b: *u8) -> i64 {
13 let wa: i64=u32le(a,18); let ha: i64=u32le(a,22); let oa: i64=u32le(a,10)
14 let wb: i64=u32le(b,18); let hb: i64=u32le(b,22); let ob: i64=u32le(b,10)
15 if wa != wb { return 999999 }
16 if ha != hb { return 999999 }
17 let row3: i64=wa*3; let pad: i64=(4-(row3%4))%4; let rs: i64=row3+pad
18 var diff: i64=0; var y: i64=0
19 while y<ha { var x: i64=0
20 while x<wa { let pa: i64=oa+y*rs+x*3; let pb: i64=ob+y*rs+x*3
21 var d: i64=0
22 if (a[pa]&0xff)!=(b[pb]&0xff){d=1}
23 if (a[pa+1]&0xff)!=(b[pb+1]&0xff){d=1}
24 if (a[pa+2]&0xff)!=(b[pb+2]&0xff){d=1}
25 if d==1 { diff=diff+1 } x=x+1 }
26 y=y+1 }
27 return diff
28}
29// BMP is BGR. red: R>100,G<100,B<100 ; blue: B>100,G<100,R<100
30func count_col(a: *u8, want_red: i64) -> i64 {
31 let w: i64=u32le(a,18); let h: i64=u32le(a,22); let o: i64=u32le(a,10)
32 let row3: i64=w*3; let pad: i64=(4-(row3%4))%4; let rs: i64=row3+pad
33 var n: i64=0; var y: i64=0
34 while y<h { var x: i64=0
35 while x<w { let p: i64=o+y*rs+x*3
36 let bb: i64=(a[p]&0xff); let gg: i64=(a[p+1]&0xff); let rr: i64=(a[p+2]&0xff)
37 if gg<100 {
38 if want_red==1 { if rr>100 { if bb<100 { n=n+1 } } }
39 else { if bb>100 { if rr<100 { n=n+1 } } }
40 }
41 x=x+1 }
42 y=y+1 }
43 return n
44}
45func render(html: *u8, outp: *u8) -> *u8 {
46 nx_render_html_to_bmp(html, slen(html), outp)
47 let lp: *i64 = sys_mmap(8) as *i64
48 let a: *u8 = sys_read_file(outp, lp)
49 if lp[0] < 54 { return 0 as *u8 }
50 return a
51}
52func chk(cond: i64, label: *u8, pass: *i64) -> i64 { g_w(" " as *u8); g_w(label); g_w(": " as *u8); if cond==1 { g_w("OK\n" as *u8); pass[0]=pass[0]+1 } else { g_w("FAIL\n" as *u8) } return 0 }
53
54func main() -> i64 {
55 let pass: *i64 = sys_mmap(8) as *i64; pass[0]=0
56 g_w("=== BACKGROUND-SHORTHAND GATE (background:<color> == background-color) ===\n" as *u8)
57 let sh_red: *u8 = "<body><style>div{background:red;width:100px;height:100px}</style><div></div></body>\x00" as *u8
58 let lh_red: *u8 = "<body><style>div{background-color:red;width:100px;height:100px}</style><div></div></body>\x00" as *u8
59 let lh_grn: *u8 = "<body><style>div{background-color:green;width:100px;height:100px}</style><div></div></body>\x00" as *u8
60 let sh_url: *u8 = "<body><style>div{background:url(x.png) blue no-repeat;width:100px;height:100px}</style><div></div></body>\x00" as *u8
61
62 let a_sh: *u8 = render(sh_red, "knowledge/status/bgsh_sh.bmp\x00" as *u8)
63 let a_lh: *u8 = render(lh_red, "knowledge/status/bgsh_lh.bmp\x00" as *u8)
64 let a_gr: *u8 = render(lh_grn, "knowledge/status/bgsh_gr.bmp\x00" as *u8)
65 let a_ur: *u8 = render(sh_url, "knowledge/status/bgsh_ur.bmp\x00" as *u8)
66 if a_sh == (0 as *u8) { g_w("render FAIL\n" as *u8); sys_exit(1); return 1 }
67 if a_lh == (0 as *u8) { g_w("render FAIL\n" as *u8); sys_exit(1); return 1 }
68 if a_gr == (0 as *u8) { g_w("render FAIL\n" as *u8); sys_exit(1); return 1 }
69 if a_ur == (0 as *u8) { g_w("render FAIL\n" as *u8); sys_exit(1); return 1 }
70
71 let dmatch: i64 = bmp_diff(a_sh, a_lh)
72 let red_sh: i64 = count_col(a_sh, 1)
73 let blue_ur: i64 = count_col(a_ur, 0)
74 let dliar: i64 = bmp_diff(a_sh, a_gr)
75 g_w("shorthand-vs-longhand diff=" as *u8); pn(dmatch); g_w(" red_px(shorthand)=" as *u8); pn(red_sh); g_w(" blue_px(url+blue)=" as *u8); pn(blue_ur); g_w(" vs-green diff=" as *u8); pn(dliar); g_w("\n" as *u8)
76 chk((dmatch == 0) as i64, "background:red renders IDENTICAL to background-color:red\x00" as *u8, pass)
77 chk((red_sh > 5000) as i64, "background:red actually paints a red box (not blank)\x00" as *u8, pass)
78 chk((blue_ur > 5000) as i64, "background:url(x) blue -> color extracted PAST the url() token\x00" as *u8, pass)
79 chk((dliar > 1000) as i64, "LIAR-KILL: background:red vs background-color:green DIFFER\x00" as *u8, pass)
80
81 g_w("BGSHORT rows=4 pass=" as *u8); pn(pass[0])
82 if pass[0]==4 { g_w(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 }
83 g_w(" verdict=RED\n" as *u8); sys_exit(1); return 1
84}