plat/print.rs#
Source: lib/plat/print.rs
print -- printing, the OS-independent part: paper sizes, page layout, rows over white, the
job as a one-page PDF [src/plat/print.s, src/img/pdfwrite.s; docs/modules/PRINT.md]
print(job) -> 0 printed / ERR_CANCEL / ERR_UNSUPPORTED / ERR_IO / ERR_NOMEM / ERR_DIMS
the OS part: win_print.rs (PrintDlgExW + GDI), linux_print.rs (portal Print + PDF)
page_init(paper, flags) -> PPage the paper PAPER_* in pt (PJF_LANDSCAPE swaps
width / height), no unprintable edge
layout(job, page) -> Place the image rectangle and the printable area on
the page (pt, from the top-left)
row_white(dst, src, n, mode) n pixels (B, G, R, A) -> n * 3 bytes composited
over white (PRW_* mode bits)
print_pdf(job, page) -> PDF bytes / ERR_* the job laid out on page as a one-page PDF
(job.page_w / page_h set)
print_dpi(unit, dpu) -> f64 pixels per inch from a resolution in unit (0 none
-> 96, 1 inch, 2 cm)
pdf_write_image(src, opt) -> PDF bytes / ERR_* one image on one PDF page (pdfwrite.s)
set_print_deflate(f) the zlib compressor of the PDF streams
PrintDoc (E9): a job of several pages
PrintDoc::begin(&job, pages) -> Result<PrintDoc, ERR_*> the system's print dialog
(printer, paper, orientation, copies; a page range when pages > 0) where there
is one; job gives the title, paper, PJF_LANDSCAPE, margin, scale, dpi (its src
is not used)
d.page() -> PPage, d.copies(), d.wants(n) the chosen page in pt, copies, whether
page n (from 1) is in the chosen range
d.add_image(&src) -> 0 / ERR_* one page: the image laid out as layout() places it
d.pdf_native(), d.add_pdf(bytes) the platform prints a PDF as it is (Linux)
d.end() -> 0 printed / ERR_*, d.abort()
Windows: PrintDlgExW + GDI, a StartPage per page (win_print.rs); Linux: the
portal's dialog, the pages as one PDF of several pages for Print (linux_print.rs);
test variant everywhere: jfgui_PRINT_TO=FILE (Windows: through the printer
jfgui_PRINT_PRINTER; elsewhere our PDF into FILE); else ERR_UNSUPPORTED
Deviation (plat-shared): plat must not depend on the image crates, so pdfwrite.s is ported
here. The one thing it took from img -- zlib_deflate -- is compress::zlib since 0.2.2, the
same encoder, so the PDF is byte-identical to the assembly's. set_print_deflate, the hook
of before, still replaces it (deprecated). The source is a PrintSrc (rows of B, G, R, A
bytes; the gfx::Surface fields) instead of a SURFACE pointer.
results (the ERR_* of jfgui.inc / plugin.inc that plat returns)
ERR_NOMEM: i32 = -8
ERR_UNSUPPORTED: i32 = -9
ERR_DIMS: i32 = -12
ERR_IO: i32 = -13
ERR_CANCEL: i32 = -14
ERR_LIMIT: i32 = -15
the source formats (FMT_* of jfgui.inc)
PRINT_XRGB: u8 = 0 // alpha byte ignored
PRINT_PARGB: u8 = 1 // premultiplied
PRINT_ARGB: u8 = 3 // straight
PrintJob::flags
PJF_LANDSCAPE: u32 = 1 // Querformat
PJF_CENTER: u32 = 2 // centre in the printable area (else its top-left corner)
PJF_SMASK: u32 = 4 // PDF only: straight colours + alpha as /SMask (no white)
PrintJob::scale
PJS_FIT: u32 = 0 // as large as the printable area allows (aspect kept)
PJS_DPI: u32 = 1 // original size: pixels / DPI inches
PJS_PERCENT: u32 = 2 // PrintJob::percent of the original size
paper
PAPER_A4: u32 = 0
PAPER_LETTER: u32 = 1
PAPER_A5: u32 = 2
PAPER_LEGAL: u32 = 3
PAPER_A3: u32 = 4
PAPER_N: u32 = 5
row_white modes
PRW_BGR: u32 = 1 // B, G, R byte order (GDI DIB) instead of R, G, B (PDF)
PRW_PREMUL: u32 = 2 // source is premultiplied
PRW_COPY: u32 = 4 // straight colours unchanged (no white; PDF with SMask)
PRW_OPAQUE: u32 = 8 // alpha ignored (XRGB)
struct PrintSrc<'a>the pixels of a print job: rows of 32-bit B, G, R, A (the fields of gfx::Surface)px: &'a [u8]w: i32h: i32stride: i32fmt: u8-- PRINT_ARGB (straight), PRINT_PARGB or PRINT_XRGB
struct PrintJob<'a>one print job (PRINTJOB)src: Option<PrintSrc<'a>>title: &'a str-- document name (spooler job, PDF /Title); "" = "Bild" (no PDF /Title)dpi_x: f64-- image pixels per inch (<= 0: 96)dpi_y: f64-- (<= 0: dpi_x)flags: u32-- PJF_*scale: u32-- PJS_*percent: u32-- PJS_PERCENT: 1..10000 % of the size by DPImargin: u32-- on every side, 1/100 mm (the printer's unprintable edge wins when larger)paper: u32-- PAPER_* suggested to the dialog / used without onecopies: u32-- copies (informative, 0 = unknown)page_w: f64-- page size in pt the job was laid out onpage_h: f64
impl<'a> PrintJob<'a>
fn new(src: Option<PrintSrc<'a>>, title: &'a str) -> PrintJob<'a>struct PPagethe page (PPAGE, pt = 1/72 inch)w: f64-- page size (orientation applied)h: f64l: f64-- unprintable edge left / top / right / bottomt: f64r: f64b: f64token: u32-- print system cookie (portal token)flags: u32-- PJF_LANDSCAPE as chosen in the dialog
struct Placewhere the image goes (PLACE, pt from the page's top-left)x: f64-- image rectangley: f64w: f64h: f64ax: f64-- printable area (margins applied); the image is clipped to itay: f64aw: f64ah: f64
struct PdfOpt<'a>PDFOPT of pdf_write_imagepage_w: f64-- MediaBox in ptpage_h: f64x: f64-- image rectangle, pt from the page's top-lefty: f64w: f64h: f64cx: f64-- clip rectangle (cw <= 0: no clip)cy: f64cw: f64ch: f64title: &'a str-- /Title ("" = none)flags: u32-- PJF_SMASKlevel: i32-- deflate level (<= 0: 6)
fn page_init(paper: u32, flags: u32) -> PPagethe paper in pt, PJF_LANDSCAPE swaps; no unprintable edge [print_page_init]fn print_dpi(unit: u32, dpu: f64) -> f64pixels per inch from a document resolution: unit 0 none -> 96, 1 inch, 2 cm (* 2.54); dpu <= 0 -> 96 [print_dpi]fn layout(job: &PrintJob, page: &PPage) -> Placethe image rectangle and the printable area on the page [print_layout]fn row_white(dst: &mut [u8], src: &[u8], n: usize, mode: u32)n pixels of src (B, G, R, A) -> n * 3 bytes of dst (R, G, B; PRW_BGR: B, G, R). Straight: c * a / 255 + 255 - a (rounded); PRW_PREMUL: c + 255 - a; PRW_COPY / PRW_OPAQUE: c [print_row_white]fn print_pdf(job: &mut PrintJob, page: &PPage) -> Result<Vec<u8>, i32>the job laid out on page as a one-page PDF (job.page_w / page_h set) [print_pdf]type DeflateFn = fn(&[u8], i32) -> Vec<u8>a zlib compressor: (data, level) -> zlib streamfn set_print_deflate(f: Option<DeflateFn>)another compressor of the PDF streams (None: compress::zlib, the default). Not needed any more: without a hook the streams are compressed with compress::zlib, the encoder the hook was set to until 0.2.1.fn pdf_write_image(src: &PrintSrc, opt: &PdfOpt) -> Result<Vec<u8>, i32>one image on one PDF 1.4 page: objects 1 Catalog, 2 Pages, 3 Page, 4 Contents, 5 Image (/DeviceRGB 8 bit /FlateDecode, composited over white -- with PJF_SMASK and a straight source: straight colours), 6 Info, [7 /SMask alpha]; classic xref [pdf_write_image]struct PrintDoca print job of several pages (see the file header; E9)
impl PrintDoc
fn begin(job: &PrintJob, pages: u32) -> Result<PrintDoc, i32>start a job: the system's print dialog where the platform has one (printer, paper, orientation, copies; with pages > 0 a page range of 1..=pages), never one in the test variant (jfgui_PRINT_TO). job: the title (spooler, PDF /Title; "" = "Dokument"), paper, PJF_LANDSCAPE / PJF_CENTER / PJF_SMASK, margin, scale, percent, dpi -- its src is not used. Err: ERR_CANCEL (the dialog was cancelled), ERR_UNSUPPORTED (no printing here, no window, the manifest forbids it), ERR_IOfn page(&self) -> PPagethe page the job prints on (pt; the printer's unprintable edges; the orientation the dialog chose in flags): lay the pages out for itfn copies(&self) -> u32the copies the dialog chose (informative: the printer makes them; 1 in the test variant)fn wants(&self, n: u32) -> boolpage n (from 1) is in the range the dialog chose (always without a range)fn added(&self) -> u32the pages added so farfn pdf_native(&self) -> boolthe platform prints a PDF document as it is (add_pdf): Linux's portal and the test variant there; Windows and the others: images onlyfn add_image(&mut self, src: &PrintSrc) -> i32one page: the image laid out on the page as layout() places it (the job's scale, dpi, margin, PJF_CENTER; clipped to the printable area) -> 0 / ERR_DIMS / ERR_UNSUPPORTED (a format or after add_pdf) / ERR_IO / ERR_NOMEMfn add_pdf(&mut self, pdf: &[u8]) -> i32the whole job as a PDF of the app's own, printed as it is (only where pdf_native(), before any add_image) -> 0 / ERR_UNSUPPORTEDfn end(mut self) -> i32finish: the job goes to the printer -> 0 printed / ERR_CANCEL (the portal's job was cancelled) / ERR_DIMS (no page) / ERR_IOfn abort(mut self)drop the job (nothing is printed)