code wiki / _hdl_build / nx_pdf_reflow.nx
nx_pdf_reflow.nx source
↩ module page · 55 lines · 4273 B
1// nx_pdf_reflow.nx -- KAT GATE for the sovereign PDF -> reflowable-mobile-HTML pipeline (_pdf_reflow_lib.nx).
2// Library arc R3 ("PDFs readable on mobile"). The pipeline lives in _pdf_reflow_lib (shared with the live
3// /reflow route in nx_media_server). This gate proves it: a hand-built KAT PDF + image-only negative controls
4// + an optional REAL PDF at /tmp/in.pdf (-> /tmp/out.reflow.html + a text preview). No hardware writes (Rule 26).
5// license_tier: ORIGINAL
6import "nx_syscalls.nx"
7import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
8import "_pdf_reflow_lib.nx"
9const K_MAGIC_4194304: i64 = 4194304
10
11func pr_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
12// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
13// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
14// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
15// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
16func pr_n(v: i64) -> i64 { nxi_out(v); return 0 }
17
18func main() -> i64 {
19 pr_p("=== nx_pdf_reflow: PDF -> reflowable mobile HTML (KAT + real) ===\n" as *u8)
20 let html: *u8 = sys_mmap(K_MAGIC_4194304)
21 let txt: *u8 = sys_mmap(K_MAGIC_4194304)
22 let dec: *u8 = sys_mmap(K_MAGIC_4194304)
23 let txc: *i64 = sys_mmap(8) as *i64
24 var pass: i64 = 0; var tot: i64 = 0
25
26 let kp: *u8 = "%PDF-1.4\n3 0 obj\n<< /Length 92 >>\nstream\nBT /F1 24 Tf 72 720 Td (Hello Nishi Library) Tj 0 -30 Td (Reflowable on mobile, sovereign.) Tj ET\nendstream\nendobj\n%%EOF\n\x00" as *u8
27 let kn: i64 = pr_slen(kp)
28 let kw: i64 = pr_pdf_to_reflow(kp, kn, html, txt, dec, txc)
29 pr_p("KAT extracted_text_chars="); pr_n(txc[0]); pr_p(" html_bytes="); pr_n(kw); pr_p("\n" as *u8)
30 tot=tot+1; pr_p(" T1 contains 'Hello Nishi Library': "); if pr_has(html,kw,"Hello Nishi Library" as *u8)==1 { pass=pass+1; pr_p("PASS\n" as *u8) } else { pr_p("FAIL\n" as *u8) }
31 tot=tot+1; pr_p(" T2 contains 'Reflowable on mobile, sovereign.': "); if pr_has(html,kw,"Reflowable on mobile, sovereign." as *u8)==1 { pass=pass+1; pr_p("PASS\n" as *u8) } else { pr_p("FAIL\n" as *u8) }
32 tot=tot+1; pr_p(" T3 reflow chrome (viewport meta): "); if pr_has(html,kw,"width=device-width" as *u8)==1 { pass=pass+1; pr_p("PASS\n" as *u8) } else { pr_p("FAIL\n" as *u8) }
33 tot=tot+1; pr_p(" T4 paragraph markup (<p>): "); if pr_has(html,kw,"<p>" as *u8)==1 { pass=pass+1; pr_p("PASS\n" as *u8) } else { pr_p("FAIL\n" as *u8) }
34
35 let np: *u8 = "%PDF-1.4\n4 0 obj\n<< /Length 30 >>\nstream\nq 100 0 0 100 0 0 cm /Im0 Do Q\nendstream\nendobj\n%%EOF\n\x00" as *u8
36 let nn: i64 = pr_slen(np)
37 let nw: i64 = pr_pdf_to_reflow(np, nn, html, txt, dec, txc)
38 tot=tot+1; pr_p(" N1 image-only -> 'No extractable text layer' note: "); if pr_has(html,nw,"No extractable text layer" as *u8)==1 { pass=pass+1; pr_p("PASS\n" as *u8) } else { pr_p("FAIL\n" as *u8) }
39 tot=tot+1; pr_p(" N2 image-only must NOT fabricate text: "); if pr_has(html,nw,"Hello Nishi Library" as *u8)==0 { pass=pass+1; pr_p("PASS\n" as *u8) } else { pr_p("FAIL\n" as *u8) }
40
41 let lp: *i64 = sys_mmap(8) as *i64
42 let rpdf: *u8 = sys_read_file("/tmp/in.pdf\x00" as *u8, lp)
43 if (rpdf as i64) != 0 { if lp[0] > 0 {
44 let rw: i64 = pr_pdf_to_reflow(rpdf, lp[0], html, txt, dec, txc)
45 pr_p("\nREAL /tmp/in.pdf bytes="); pr_n(lp[0]); pr_p(" -> extracted_text_chars="); pr_n(txc[0]); pr_p(" html_bytes="); pr_n(rw); pr_p("\n" as *u8)
46 let ofd: i64 = sys_openat_wr("/tmp/out.reflow.html\x00" as *u8, 0x1a4)
47 if ofd >= 0 { sys_write(ofd, html, rw); sys_close(ofd); pr_p(" wrote /tmp/out.reflow.html\n" as *u8) }
48 let bat: i64 = pr_sub(html, rw, 0, "<body>" as *u8, 6)
49 if bat >= 0 { var st2: i64 = bat + 6; var cnt: i64 = 0; pr_p(" PREVIEW: "); while cnt < 600 { if st2+cnt < rw { sys_write(1, ((html as i64)+st2+cnt) as *u8, 1); cnt=cnt+1 } else { cnt = 600 } } pr_p("\n" as *u8) }
50 } }
51
52 pr_p("\nPDF-REFLOW-GATE "); pr_n(pass); pr_p("/"); pr_n(tot)
53 if pass == tot { pr_p(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 }
54 pr_p(" verdict=RED\n" as *u8); sys_exit(1); return 1
55}