code wiki / _hdl_build / nx_render_page_gate.nx
nx_render_page_gate.nx source
↩ module page · 125 lines · 7260 B
1// nx_render_page_gate.nx -- VERDICT gate for the PAINT + ENCODE back half of the sovereign HTML renderer
2// (browser Phase 2-4). The float gate proved LAYOUT geometry; this proves a realistic page actually
3// PAINTS and ENCODES correctly: render a mini-article (h1/h2 bars, float:right infobox, blue link, ul
4// bullets, flex table) to a 24-bit BMP, read the BMP back, and assert dimensions + that every UA-sheet
5// color is actually present in the pixels. Expectations are MEASURED (nx_render_page_probe), not guessed.
6// LIAR-KILL: a color the page never uses (#00ff00) must be ABSENT -- proving the pixel scanner has teeth
7// and "PRESENT" is not a rubber stamp. GREEN iff every assertion holds. 100% sovereign. license_tier: ORIGINAL
8import "nx_syscalls.nx"
9import "nx_render_html.nx"
10import "nx_layout_block.nx" // NX_INLINE_LINE_H -- the page height below is line-derived, not a constant
11
12func p_w(s: *u8) -> i64 { var k: i64=0; while s[k]!=(0 as u8){k=k+1} sys_write(1,s,k); return 0 }
13func p_n(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 }
14func 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) }
15
16// scan the BMP pixel block for an exact (R,G,B); 1 if any pixel matches. BMP rows are BGR, bottom-up.
17func color_present(data: *u8, off: i64, w: i64, h: i64, R: i64, G: i64, B: i64) -> i64 {
18 let row3: i64 = w*3
19 let pad: i64 = (4-(row3%4))%4
20 let rowstride: i64 = row3+pad
21 var y: i64 = 0
22 while y < h {
23 var x: i64 = 0
24 let rowbase: i64 = off + y*rowstride
25 while x < w {
26 let p: i64 = rowbase + x*3
27 if (data[p]&0xff)==B { if (data[p+1]&0xff)==G { if (data[p+2]&0xff)==R { return 1 } } }
28 x = x+1
29 }
30 y = y+1
31 }
32 return 0
33}
34func count_nonwhite(data: *u8, off: i64, w: i64, h: i64) -> i64 {
35 let row3: i64 = w*3
36 let pad: i64 = (4-(row3%4))%4
37 let rowstride: i64 = row3+pad
38 var n: i64 = 0
39 var y: i64 = 0
40 while y < h {
41 var x: i64 = 0
42 let rowbase: i64 = off + y*rowstride
43 while x < w {
44 let p: i64 = rowbase + x*3
45 var white: i64 = 0
46 if (data[p]&0xff)==255 { if (data[p+1]&0xff)==255 { if (data[p+2]&0xff)==255 { white=1 } } }
47 if white==0 { n=n+1 }
48 x = x+1
49 }
50 y = y+1
51 }
52 return n
53}
54
55func chk(cond: i64, label: *u8, pass: *i64) -> i64 {
56 p_w(" " as *u8); p_w(label); p_w(": " as *u8)
57 if cond==1 { p_w("OK\n" as *u8); pass[0]=pass[0]+1 } else { p_w("FAIL\n" as *u8) }
58 return cond
59}
60func chkv(got: i64, want: i64, label: *u8, pass: *i64) -> i64 {
61 p_w(" " as *u8); p_w(label); p_w(": " as *u8)
62 if got==want { p_w("OK (" as *u8); p_n(got); p_w(")\n" as *u8); pass[0]=pass[0]+1 }
63 else { p_w("FAIL got=" as *u8); p_n(got); p_w(" want=" as *u8); p_n(want); p_w("\n" as *u8) }
64 return 0
65}
66// assert a color's presence/absence matches expectation (want_present 1/0). Unifies the positive paint
67// asserts and the liar-kill (the control color with want_present=0).
68func chk_color(data: *u8, off: i64, w: i64, h: i64, R: i64, G: i64, B: i64, want_present: i64, label: *u8, pass: *i64) -> i64 {
69 let pres: i64 = color_present(data, off, w, h, R, G, B)
70 p_w(" " as *u8); p_w(label); p_w(": " as *u8)
71 if pres==want_present { p_w("OK\n" as *u8); pass[0]=pass[0]+1 }
72 else { p_w("FAIL present=" as *u8); p_n(pres); p_w(" want=" as *u8); p_n(want_present); p_w("\n" as *u8) }
73 return 0
74}
75
76func main() -> i64 {
77 let pass: *i64 = sys_mmap(8) as *i64; pass[0]=0
78 p_w("=== RENDER-PAGE GATE (paint + encode, asserted pixels, sovereign nx_cc->nxasm) ===\n" as *u8)
79
80 // Fixed realistic mini-article: h1/h2 heading bars, float:right .infobox, blue <a> link, ul bullets,
81 // flex <table>. The UA reader stylesheet inside nx_render_html supplies the styling.
82 let html: *u8 = "<body><h1>Nishi Browser</h1><div class=\"infobox\">Fact box: sovereign, bits-up.</div><p>A from-scratch renderer. See <a href=\"/about\">about</a> for more.</p><h2>Features</h2><ul><li>HTML tokenizer</li><li>CSS cascade</li><li>Float and flex layout</li></ul><table><tr><td>Phase</td><td>Status</td></tr><tr><td>Layout</td><td>Done</td></tr></table></body>\x00" as *u8
83 var hlen: i64 = 0
84 while html[hlen]!=(0 as u8) { hlen=hlen+1 }
85
86 let nbmp: i64 = nx_render_html_to_bmp(html, hlen, "knowledge/status/render_page_gate.bmp\x00" as *u8)
87 nx_render_html_to_png(html, hlen, "knowledge/status/render_page_gate.png\x00" as *u8) // viewable artifact
88
89 // ---- structure: the render produced the expected box tree ----
90 chkv(nbmp, 31, "box count == 31\x00" as *u8, pass)
91
92 // ---- encode: read the BMP back and validate the container ----
93 let lp: *i64 = sys_mmap(8) as *i64
94 let bmp: *u8 = sys_read_file("knowledge/status/render_page_gate.bmp\x00" as *u8, lp)
95 let blen: i64 = lp[0]
96 chk((blen >= 54) as i64, "bmp file >= 54 bytes\x00" as *u8, pass)
97 if blen < 54 { p_w("RENDER-PAGE rows=12 pass=" as *u8); p_n(pass[0]); p_w(" verdict=RED (no image)\n" as *u8); sys_exit(1); return 1 }
98 chk(((bmp[0]&0xff)==66) as i64 * (((bmp[1]&0xff)==77) as i64), "BMP signature 'BM'\x00" as *u8, pass)
99 let off: i64 = u32le(bmp, 10)
100 let w: i64 = u32le(bmp, 18)
101 let h: i64 = u32le(bmp, 22)
102 chkv(off, 54, "pixel offset == 54\x00" as *u8, pass)
103 chkv(w, 800, "width == 800\x00" as *u8, pass)
104 // Fixed chrome plus N text lines; the line term moves with the font, so a literal goes stale the
105 // moment the renderer changes metrics -- which is what happened when NX_INLINE_LINE_H went 10 -> 17
106 // and this read 243 against a hardcoded 166 while every paint assertion still passed.
107 // DERIVED from the measured pair: 243-166 = 11*(17-10), so N=11 lines and chrome = 166-110 = 56.
108 let PAGE_CHROME_PX: i64 = 56
109 let PAGE_TEXT_LINES: i64 = 11
110 chkv(h, PAGE_CHROME_PX + PAGE_TEXT_LINES * NX_INLINE_LINE_H, "height == chrome + 11 lines\x00" as *u8, pass)
111
112 // ---- paint: a realistic page is substantially inked, and every UA-sheet color landed ----
113 chk((count_nonwhite(bmp, off, w, h) > 10000) as i64, "substantially painted (>10000 non-white px)\x00" as *u8, pass)
114 chk_color(bmp, off, w, h, 0xa8,0xc8,0xff, 1, "h1 bar #a8c8ff present\x00" as *u8, pass)
115 chk_color(bmp, off, w, h, 0xd0,0xd0,0xd0, 1, "h2 bar #d0d0d0 present\x00" as *u8, pass)
116 chk_color(bmp, off, w, h, 0xf8,0xf8,0xf8, 1, "float infobox #f8f8f8 present\x00" as *u8, pass)
117 chk_color(bmp, off, w, h, 0x33,0x66,0xcc, 1, "link color #3366cc present\x00" as *u8, pass)
118
119 // ---- LIAR-KILL: a color the page never uses must be ABSENT (scanner has teeth) ----
120 chk_color(bmp, off, w, h, 0,0xff,0, 0, "LIAR-KILL: green #00ff00 absent\x00" as *u8, pass)
121
122 p_w("RENDER-PAGE rows=12 pass=" as *u8); p_n(pass[0])
123 if pass[0]==12 { p_w(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 }
124 p_w(" verdict=RED\n" as *u8); sys_exit(1); return 1
125}