code wiki / (root) / nx_doc_to_urf.nx

nx_doc_to_urf.nx source

↩ module page · 49 lines · 2547 B

1// nx_doc_to_urf.nx -- render a whole text document across as many pages as needed into ONE multi-page URF. 2// Composes nx_doc_layout (word-wrap layout, returns chars-consumed per page) + nx_urf (file/page headers) + 3// nx_pwg_rle. URF multi-page layout = file-header(pageCount) then N x (32-byte page-header + RLE data). 4// PURE w.r.t. the printer (builds bytes) -> never-brick. Sovereign. license_tier: ORIGINAL 5// genealogy_id: project-printer-management-ipp-sclass-2026-06-20 6 7import "nx_doc_layout.nx" 8import "nx_pwg_rle.nx" 9import "nx_urf.nx" 10import "nx_toner_meter.nx" 11 12const NX_DOC_MAX_PAGES: i64 = 200 // safety cap (a runaway loop backstop) 13 14// Render `text` into a multi-page URF in `out`. Caller provides `bm` (a W*H scratch bitmap) and `table` 15// (font8x8_table()). Writes out_pages[0] = page count. Returns total URF byte length. 16func nx_doc_to_urf(out: *u8, bm: *u8, W: i64, H: i64, table: *u8, text: *u8, scale: i64, 17 mleft: i64, mtop: i64, mright: i64, mbottom: i64, line_gap: i64, 18 dpi: i64, duplex: i64, quality: i64, out_pages: *i64, out_cov_sum: *i64) -> i64 { 19 var off: i64 = 12 // reserve the 12-byte file header (written last, once N is known) 20 var pages: i64 = 0 21 var doff: i64 = 0 22 var stop: i64 = 0 23 out_cov_sum[0] = 0 // total ink coverage (ppm summed over pages) for real-toner accounting 24 while stop == 0 { 25 if (text[doff] as i64) == 0 { stop = 1 } 26 else { 27 if pages >= NX_DOC_MAX_PAGES { stop = 1 } 28 else { 29 // blank page to white 30 var i: i64 = 0 31 while i < W * H { bm[i] = 255 as u8; i = i + 1 } 32 let consumed: i64 = nx_doc_render(bm, W, H, table, ((text as i64) + doff) as *u8, scale, 33 mleft, mtop, mright, mbottom, line_gap) 34 if consumed <= 0 { stop = 1 } 35 else { 36 out_cov_sum[0] = out_cov_sum[0] + nx_toner_coverage_ppm(bm, W * H) 37 off = nx_urf_page_header(out, off, 8, NX_URF_CS_SGRAY, duplex, quality, W, H, dpi) 38 let rlen: i64 = nx_rle_encode_page(bm, W, H, 1, ((out as i64) + off) as *u8) 39 off = off + rlen 40 pages = pages + 1 41 doff = doff + consumed 42 } 43 } 44 } 45 } 46 nx_urf_file_header(out, pages) // now write the file header (UNIRAST + pageCount) at offset 0 47 out_pages[0] = pages 48 return off 49}