code wiki / (root) / nx_doc_print.nx

nx_doc_print.nx source

↩ module page · 146 lines · 9349 B

1// nx_doc_print.nx -- PRINT A REAL DOCUMENT, end to end, 100% sovereign: text -> grayscale page (nx_doc_layout 2// + nx_font8x8) -> URF (nx_urf) -> Print-Job (nx_ipp_submit) -> POST (nx_ipp_transport) -> poll job-state, 3// and record REAL toner consumed (nx_toner_meter/ledger). Outward-facing (consumes paper/toner): operator 4// approved live printing. never-brick (#26): normal print job, no firmware. license_tier: ORIGINAL 5// genealogy_id: project-printer-management-ipp-sclass-2026-06-20 6 7import "nx_syscalls.nx" 8import "nx_font8x8.nx" 9import "nx_doc_layout.nx" 10import "nx_doc_to_urf.nx" 11import "nx_pwg_rle.nx" 12import "nx_urf.nx" 13import "nx_ipp_codec.nx" 14import "nx_ipp_transport.nx" 15import "nx_ipp_submit.nx" 16import "nx_toner_meter.nx" 17import "nx_toner_ledger.nx" 18import "nx_toner_store.nx" 19const K_MAGIC_2550: i64 = 2550 20const K_MAGIC_3300: i64 = 3300 21const K_MAGIC_8192: i64 = 8192 22const K_MAGIC_262144: i64 = 262144 23const K_MAGIC_10000: i64 = 10000 24const K_MAGIC_4096: i64 = 4096 25const K_MAGIC_131072: i64 = 131072 26const K_MAGIC_2048: i64 = 2048 27const K_MAGIC_32768: i64 = 32768 28const K_MAGIC_1500: i64 = 1500 29 30func p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 31func pn(v: i64) -> i64 { 32 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 } 33 var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 34 let t: *u8 = sys_mmap(24); var k: i64 = 0 35 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 36 let b: *u8 = sys_mmap(24); var i: i64 = 0 37 while i < k { b[i] = t[k - 1 - i]; i = i + 1 } 38 sys_write(1, b, k); return 0 39} 40func sapp(buf: *u8, off: i64, s: *u8) -> i64 { 41 var o: i64 = off; var i: i64 = 0 42 while s[i] != (0 as u8) { buf[o] = s[i]; o = o + 1; i = i + 1 } 43 return o 44} 45func write_all(fd: i64, buf: *u8, len: i64) -> i64 { 46 var sent: i64 = 0 47 while sent < len { let w: i64 = sys_write(fd, ((buf as i64) + sent) as *u8, len - sent); if w <= 0 { return 0 - 1 } sent = sent + w } 48 return 0 49} 50 51func main() -> i64 { 52 let a: i64 = 192 53 let b: i64 = 168 54 let c: i64 = 8 55 let d: i64 = 127 56 let W: i64 = K_MAGIC_2550 57 let H: i64 = K_MAGIC_3300 58 p("=== PRINT REAL DOCUMENT TWO-SIDED (waste-aware: sides=two-sided-long-edge -> half the sheets) ===\n") 59 60 // 1. compose a longer document (exercises word-wrap + multi-page flow) 61 let txt: *u8 = sys_mmap(K_MAGIC_8192) 62 var o: i64 = 0 63 o = sapp(txt, o, "NISHI SOVEREIGN PRINTING\n") 64 o = sapp(txt, o, "========================\n\n") 65 o = sapp(txt, o, "This multi-page document was rendered and printed by a 100 percent sovereign stack: our own bitmap font, word-wrap layout engine, PWG/URF rasterizer, IPP client and TLS -- with no vendor driver, no cloud, and no Brother application anywhere on the path.\n\n") 66 o = sapp(txt, o, "Long paragraphs now wrap at word boundaries instead of splitting words mid-line, and when a page fills the text flows automatically onto the next page, assembled into a single multi-page URF document.\n\n") 67 o = sapp(txt, o, "The quick brown fox jumps over the lazy dog. 0123456789 !?.,:;-()[]{}+=/*&%$#@\n\n") 68 o = sapp(txt, o, "Real toner is measured by ink coverage across every page, so the printer Replace Toner lie is ignored and aftermarket toner simply works. Security, waste and supply levels are surfaced honestly on a mobile-ready status page.\n\n") 69 o = sapp(txt, o, "This proves the document renderer: text in, real pages out, on any IPP printer, end to end.\n\n") 70 o = sapp(txt, o, "DUPLEX DEMONSTRATION: this document is submitted with the IPP job attribute sides=two-sided-long-edge, so the printer puts page two on the back of sheet one. A vendor driver ships one-sided by default and would use one sheet per page; the sovereign waste-aware path uses ceil(pages over two) sheets, roughly half the paper, on the same duplex-capable hardware, with no driver and no cloud anywhere on the path.\n\n") 71 o = sapp(txt, o, "Additional body so the document reliably spans more than a single page: the quick brown fox jumps over the lazy dog, and then jumps back again and again, while the sovereign stack lays out every word at a clean boundary and flows the overflow onto the next page automatically so that the second page lands on the reverse of the first sheet.\n\n") 72 o = sapp(txt, o, "Paper economics, stated plainly and at length so the body clearly overflows onto a second page: every duplex sheet carries two printed sides, so a ten page report becomes five sheets instead of ten, a twenty page report becomes ten sheets instead of twenty, and across a year of household and small office printing that single default change quietly removes about half of the paper, half of the filing space, and half of the cost, with no loss of content and no change to the words on the page. The sovereign stack makes that the default by attaching one standard IPP job attribute, sides two sided long edge, to the print job it already builds, and the very same code path works on any IPP, Mopria or AirPrint printer without a vendor driver, without a cloud account, and without any application from the printer manufacturer installed anywhere on the machine. This paragraph exists mainly to guarantee that the rendered document is comfortably longer than one page, so that the duplex behaviour is demonstrated physically, with page two landing on the reverse of sheet one rather than consuming a second sheet of paper.\n") 73 txt[o] = 0 as u8 74 75 // 2. render across pages -> one multi-page URF (also tallies real ink coverage over all pages) 76 let table: *u8 = font8x8_table() 77 let bm: *u8 = sys_mmap(W * H) 78 let doc: *u8 = sys_mmap(W * H + K_MAGIC_262144) 79 let pages: *i64 = sys_mmap(8) as *i64 80 let covsum: *i64 = sys_mmap(8) as *i64 81 let doclen: i64 = nx_doc_to_urf(doc, bm, W, H, table, txt, 5, 160, 200, 160, 160, 22, 300, NX_URF_DUP_LONG, NX_URF_Q_NORMAL, pages, covsum) 82 let cov: i64 = covsum[0] 83 p("pages="); pn(pages[0]); p(" URF bytes="); pn(doclen) 84 p(" total coverage="); pn(cov / K_MAGIC_10000); p("."); let cf: i64 = (cov % K_MAGIC_10000) / 100; if cf < 10 { p("0") } pn(cf); p("%\n") 85 let fd: i64 = sys_openat_wr("knowledge/fetched/nishi_doc.urf\x00" as *u8, 0x1a4) 86 if fd >= 0 { write_all(fd, doc, doclen); sys_close(fd) } 87 88 // 5. Print-Job + document data 89 let pay: *u8 = sys_mmap(doclen + K_MAGIC_4096) 90 let hlen: i64 = nx_ipp_build_print_job_sides(pay, a, b, c, d, "/ipp/print", "nishi", "nishi-document", "image/urf", "two-sided-long-edge", 19) 91 var j: i64 = 0 92 while j < doclen { pay[hlen + j] = doc[j]; j = j + 1 } 93 let total: i64 = hlen + doclen 94 95 let resp: *u8 = sys_mmap(K_MAGIC_131072) 96 let body: *u8 = sys_mmap(K_MAGIC_131072) 97 let out2: *i64 = sys_mmap(16) as *i64 98 let v: i64 = nx_ipt_post_ipp(a, b, c, d, 631, "/ipp/print", pay, total, resp, K_MAGIC_131072, body, K_MAGIC_131072, 15, out2) 99 p("submit transport="); p(nx_ipt_verdict_name(v)); p(" http="); pn(out2[0]); p(" ipp=0x"); 100 let st: i64 = nx_ipp_status(body) 101 if st < 16 { p("000") } pn(st); p("\n") 102 if v != NX_IPT_OK { p("submit failed\n"); sys_exit(1); return 1 } 103 let sc: *i64 = sys_mmap(64) as *i64 104 let jid: *i64 = sys_mmap(8) as *i64 105 var job: i64 = 0 - 1 106 if nx_ipp_get_enum(body, out2[1], "job-id", sc, jid) == NX_IPP_OK { job = jid[0]; p("job-id="); pn(job); p("\n") } 107 108 // 6. record real toner for this print 109 let y: *i64 = sys_mmap(8) as *i64 110 let cons: *i64 = sys_mmap(8) as *i64 111 if nx_tstore_load("knowledge/fetched/toner_ledger.bin\x00" as *u8, y, cons) == 0 { y[0] = NX_TONER_YIELD_TN630; cons[0] = 0 } 112 cons[0] = nx_ledger_add(cons[0], cov) 113 nx_tstore_save("knowledge/fetched/toner_ledger.bin\x00" as *u8, y[0], cons[0]) 114 let bp: i64 = nx_ledger_remaining_bp(y[0], cons[0]) 115 p("REAL toner now: "); pn(bp / 100); p("."); let bf: i64 = bp % 100; if bf < 10 { p("0") } pn(bf); p("% (printer still lies 0%)\n") 116 117 // 7. poll job-state to confirm it prints (distinct buffers: request / response / body) 118 if job >= 0 { 119 let prq: *u8 = sys_mmap(K_MAGIC_2048) 120 let prs: *u8 = sys_mmap(K_MAGIC_32768) 121 let pbd: *u8 = sys_mmap(K_MAGIC_32768) 122 let pj: *i64 = sys_mmap(8) as *i64 123 var tries: i64 = 0 124 var done: i64 = 0 125 while tries < 12 { 126 if done == 0 { 127 let rl: i64 = nx_ipp_build_get_job_attrs(prq, a, b, c, d, "/ipp/print", "nishi", job) 128 let pv: i64 = nx_ipt_post_ipp(a, b, c, d, 631, "/ipp/print", prq, rl, prs, K_MAGIC_32768, pbd, K_MAGIC_32768, 6, out2) 129 if pv == NX_IPT_OK { 130 let s2: i64 = nx_ipp_status(pbd) 131 if s2 == 0x0406 { p(" job completed (purged by printer)\n"); done = 1 } 132 else { 133 if nx_ipp_get_enum(pbd, out2[1], "job-state", sc, pj) == NX_IPP_OK { 134 p(" job-state="); pn(pj[0]); p("\n") 135 if pj[0] >= 7 { done = 1 } 136 } 137 } 138 } 139 if done == 0 { sys_sleep_ms(K_MAGIC_1500) } 140 } 141 tries = tries + 1 142 } 143 } 144 p("=== DONE: a real text document printed from the sovereign stack ===\n") 145 sys_exit(0); return 0 146}