1use alloc::string::String;
41use alloc::vec::Vec;
42use core::fmt::Write;
43use core::sync::atomic::{AtomicUsize, Ordering};
44
45use rt::fx::{cvtd64, maxsd, minsd};
46
47pub const ERR_NOMEM: i32 = -8;
49pub const ERR_UNSUPPORTED: i32 = -9;
50pub const ERR_DIMS: i32 = -12;
51pub const ERR_IO: i32 = -13;
52pub const ERR_CANCEL: i32 = -14;
53pub const ERR_LIMIT: i32 = -15;
54
55pub const PRINT_XRGB: u8 = 0; pub const PRINT_PARGB: u8 = 1; pub const PRINT_ARGB: u8 = 3; pub const PJF_LANDSCAPE: u32 = 1; pub const PJF_CENTER: u32 = 2; pub const PJF_SMASK: u32 = 4; pub const PJS_FIT: u32 = 0; pub const PJS_DPI: u32 = 1; pub const PJS_PERCENT: u32 = 2; pub const PAPER_A4: u32 = 0;
70pub const PAPER_LETTER: u32 = 1;
71pub const PAPER_A5: u32 = 2;
72pub const PAPER_LEGAL: u32 = 3;
73pub const PAPER_A3: u32 = 4;
74pub const PAPER_N: u32 = 5;
75pub const PRW_BGR: u32 = 1; pub const PRW_PREMUL: u32 = 2; pub const PRW_COPY: u32 = 4; pub const PRW_OPAQUE: u32 = 8; const TITLE_MAX: usize = 256; const PDF_LEVEL_DEF: i32 = 6;
83
84#[derive(Clone, Copy)]
86pub struct PrintSrc<'a> {
87 pub px: &'a [u8],
88 pub w: i32,
89 pub h: i32,
90 pub stride: i32,
91 pub fmt: u8, }
93
94#[derive(Clone, Copy)]
96pub struct PrintJob<'a> {
97 pub src: Option<PrintSrc<'a>>,
98 pub title: &'a str, pub dpi_x: f64, pub dpi_y: f64, pub flags: u32, pub scale: u32, pub percent: u32, pub margin: u32, pub paper: u32, pub copies: u32, pub page_w: f64, pub page_h: f64,
110}
111
112impl<'a> PrintJob<'a> {
113 pub fn new(src: Option<PrintSrc<'a>>, title: &'a str) -> PrintJob<'a> {
114 PrintJob {
115 src,
116 title,
117 dpi_x: 96.0,
118 dpi_y: 96.0,
119 flags: 0,
120 scale: PJS_FIT,
121 percent: 100,
122 margin: 0,
123 paper: PAPER_A4,
124 copies: 0,
125 page_w: 0.0,
126 page_h: 0.0,
127 }
128 }
129}
130
131#[derive(Clone, Copy, Default, Debug)]
133pub struct PPage {
134 pub w: f64, pub h: f64,
136 pub l: f64, pub t: f64,
138 pub r: f64,
139 pub b: f64,
140 pub token: u32, pub flags: u32, }
143
144#[derive(Clone, Copy, Default, Debug)]
146pub struct Place {
147 pub x: f64, pub y: f64,
149 pub w: f64,
150 pub h: f64,
151 pub ax: f64, pub ay: f64,
153 pub aw: f64,
154 pub ah: f64,
155}
156
157#[derive(Clone, Copy, Default)]
159pub struct PdfOpt<'a> {
160 pub page_w: f64, pub page_h: f64,
162 pub x: f64, pub y: f64,
164 pub w: f64,
165 pub h: f64,
166 pub cx: f64, pub cy: f64,
168 pub cw: f64,
169 pub ch: f64,
170 pub title: &'a str, pub flags: u32, pub level: i32, }
174
175static PAPER_PT: [[f64; 2]; 5] = [
177 [595.2755905511812, 841.8897637795276], [612.0, 792.0], [419.5275590551181, 595.2755905511812], [612.0, 1008.0], [841.8897637795276, 1190.551181102362], ];
183const MM100_PT: f64 = 0.028346456692913385; pub fn page_init(paper: u32, flags: u32) -> PPage {
187 let p = PAPER_PT[if paper >= PAPER_N { 0 } else { paper as usize }];
188 let (w, h) = if flags & PJF_LANDSCAPE != 0 { (p[1], p[0]) } else { (p[0], p[1]) };
189 PPage { w, h, l: 0.0, t: 0.0, r: 0.0, b: 0.0, token: 0, flags: flags & PJF_LANDSCAPE }
190}
191
192pub fn print_dpi(unit: u32, dpu: f64) -> f64 {
195 if unit < 1 || !(dpu > 0.0) {
196 return 96.0;
197 }
198 if unit == 2 { dpu * 2.54 } else { dpu }
199}
200
201pub fn layout(job: &PrintJob, page: &PPage) -> Place {
203 let dx = if job.dpi_x > 0.0 { job.dpi_x } else { 96.0 };
204 let dy = if job.dpi_y > 0.0 { job.dpi_y } else { dx };
205 let (pw, ph) = job.src.map_or((1, 1), |s| (s.w, s.h));
206 let mut iw = pw as f64 * 72.0 / dx; let mut ih = ph as f64 * 72.0 / dy;
208 let m = job.margin as f64 * MM100_PT;
209 let mut pl = Place::default();
210 pl.ax = maxsd(page.l, m);
211 pl.aw = maxsd(page.w - maxsd(page.r, m) - pl.ax, 1.0);
212 pl.ay = maxsd(page.t, m);
213 pl.ah = maxsd(page.h - maxsd(page.b, m) - pl.ay, 1.0);
214 if job.scale != PJS_DPI {
215 let s = if job.scale == PJS_PERCENT {
216 job.percent.clamp(1, 10000) as f64 / 100.0
217 } else {
218 minsd(pl.aw / iw, pl.ah / ih)
219 };
220 iw *= s;
221 ih *= s;
222 }
223 pl.w = iw;
224 pl.h = ih;
225 pl.x = pl.ax;
226 pl.y = pl.ay;
227 if job.flags & PJF_CENTER != 0 {
228 pl.x += (pl.aw - iw) * 0.5;
229 pl.y += (pl.ah - ih) * 0.5;
230 }
231 pl
232}
233
234pub fn row_white(dst: &mut [u8], src: &[u8], n: usize, mode: u32) {
238 for (d, s) in dst.chunks_exact_mut(3).zip(src.chunks_exact(4)).take(n) {
239 let (b, g, r) = (s[0] as u32, s[1] as u32, s[2] as u32);
240 let a = s[3] as u32;
241 let (b, g, r) = if mode & (PRW_OPAQUE | PRW_COPY) != 0 {
242 (b, g, r)
243 } else if mode & PRW_PREMUL != 0 {
244 let k = 255 - a;
245 ((b + k).min(255), (g + k).min(255), (r + k).min(255))
246 } else {
247 let w = (255 - a) * 255 + 128;
248 let st = |c: u32| {
249 let t = c * a + w;
250 (t + (t >> 8)) >> 8
251 };
252 (st(b), st(g), st(r))
253 };
254 if mode & PRW_BGR != 0 {
255 d[0] = b as u8;
256 d[1] = g as u8;
257 d[2] = r as u8;
258 } else {
259 d[0] = r as u8;
260 d[1] = g as u8;
261 d[2] = b as u8;
262 }
263 }
264}
265
266pub fn print_pdf(job: &mut PrintJob, page: &PPage) -> Result<Vec<u8>, i32> {
268 let src = job.src.ok_or(ERR_DIMS)?;
269 let pl = layout(job, page);
270 job.page_w = page.w;
271 job.page_h = page.h;
272 let opt = PdfOpt {
273 page_w: page.w,
274 page_h: page.h,
275 x: pl.x,
276 y: pl.y,
277 w: pl.w,
278 h: pl.h,
279 cx: pl.ax, cy: pl.ay,
281 cw: pl.aw,
282 ch: pl.ah,
283 title: job.title,
284 flags: job.flags & PJF_SMASK,
285 level: 0,
286 };
287 pdf_write_image(&src, &opt)
288}
289
290pub type DeflateFn = fn(&[u8], i32) -> Vec<u8>;
294
295static DEFLATE: AtomicUsize = AtomicUsize::new(0);
296
297pub fn set_print_deflate(f: Option<DeflateFn>) {
299 DEFLATE.store(f.map_or(0, |f| f as usize), Ordering::SeqCst);
300}
301
302fn deflate(data: &[u8], level: i32) -> Vec<u8> {
303 let p = DEFLATE.load(Ordering::SeqCst);
304 if p != 0 {
305 let f: DeflateFn = unsafe { core::mem::transmute::<usize, DeflateFn>(p) };
307 return f(data, level);
308 }
309 let mut out = Vec::with_capacity(data.len() + data.len() / 65535 * 5 + 16);
311 out.extend_from_slice(&[0x78, 0x01]);
312 let mut chunks = data.chunks(65535).peekable();
313 if chunks.peek().is_none() {
314 out.extend_from_slice(&[1, 0, 0, 0xFF, 0xFF]);
315 }
316 while let Some(c) = chunks.next() {
317 out.push(chunks.peek().is_none() as u8);
318 out.extend_from_slice(&(c.len() as u16).to_le_bytes());
319 out.extend_from_slice(&(!(c.len() as u16)).to_le_bytes());
320 out.extend_from_slice(c);
321 }
322 let (mut a, mut b) = (1u32, 0u32);
323 for &x in data {
324 a = (a + x as u32) % 65521;
325 b = (b + a) % 65521;
326 }
327 out.extend_from_slice(&(b << 16 | a).to_be_bytes());
328 out
329}
330
331fn pw_num(o: &mut String, v: f64) {
335 let m = cvtd64(v * 1000.0);
336 if m < 0 {
337 o.push('-');
338 }
339 let u = m.unsigned_abs();
340 let _ = write!(o, "{}.{:03}", u / 1000, u % 1000);
341}
342
343fn pw_nums(o: &mut String, a: f64, b: f64) {
344 o.push(' ');
345 pw_num(o, a);
346 o.push(' ');
347 pw_num(o, b);
348}
349
350fn pw_title(o: &mut String, s: &str) {
352 o.push_str(" /Title <FEFF");
353 for c in s.chars().take_while(|&c| c != '\0').take(TITLE_MAX) {
354 let mut buf = [0u16; 2];
355 for u in c.encode_utf16(&mut buf) {
356 let _ = write!(o, "{:04X}", u);
357 }
358 }
359 o.push('>');
360}
361
362fn pdf_image_data(src: &PrintSrc, flags: u32, level: i32) -> Result<(Vec<u8>, Option<Vec<u8>>), i32> {
366 let (w, h) = (src.w, src.h);
367 if w <= 0 || h <= 0 || src.px.is_empty() {
368 return Err(ERR_DIMS);
369 }
370 let mut mode = match src.fmt {
371 PRINT_ARGB => 0,
372 PRINT_PARGB => PRW_PREMUL,
373 PRINT_XRGB => PRW_OPAQUE,
374 _ => return Err(ERR_UNSUPPORTED),
375 };
376 if src.fmt == PRINT_ARGB && flags & PJF_SMASK != 0 {
377 mode = PRW_COPY;
378 }
379 let level = if level <= 0 { PDF_LEVEL_DEF } else { level };
380 let (wu, hu) = (w as usize, h as usize);
381 if src.stride < w.saturating_mul(4) {
383 return Err(ERR_DIMS);
384 }
385 let covered = wu.checked_mul(4).and_then(|rw| (hu - 1).checked_mul(src.stride as usize)?.checked_add(rw));
386 if covered.is_none_or(|n| n > src.px.len()) {
387 return Err(ERR_DIMS);
388 }
389 let stride = src.stride as isize;
390 let row_of = |y: usize| -> &[u8] {
391 let o = (y as isize * stride) as usize;
392 src.px.get(o..o + wu * 4).unwrap_or(&[])
393 };
394 let rb = wu * 3;
396 let mut raw = alloc::vec![0u8; rb * hu];
397 for y in 0..hu {
398 row_white(&mut raw[y * rb..(y + 1) * rb], row_of(y), wu, mode);
399 }
400 let rgb = deflate(&raw, level);
401 drop(raw);
402 let alpha = if mode == PRW_COPY {
404 let mut a = Vec::with_capacity(wu * hu);
405 for y in 0..hu {
406 let r = row_of(y);
407 for x in 0..wu {
408 a.push(r.get(x * 4 + 3).copied().unwrap_or(0));
409 }
410 }
411 Some(deflate(&a, level))
412 } else {
413 None
414 };
415 Ok((rgb, alpha))
416}
417
418fn pdf_content(opt: &PdfOpt) -> String {
420 let mut c = String::from("q\n");
421 if opt.cw > 0.0 {
422 pw_nums(&mut c, opt.cx, opt.page_h - opt.cy - opt.ch); pw_nums(&mut c, opt.cw, opt.ch);
424 c.push_str(" re W n\n");
425 }
426 pw_nums(&mut c, opt.w, 0.0);
427 pw_nums(&mut c, 0.0, opt.h);
428 pw_nums(&mut c, opt.x, opt.page_h - opt.y - opt.h);
429 c.push_str(" cm\n/Im1 Do\nQ\n");
430 c
431}
432
433fn pdf_image_obj(f: &mut Vec<u8>, n: usize, w: i32, h: i32, rgb: &[u8], smask: Option<usize>) {
435 let mut s = String::new();
436 let _ = write!(s, "{n} 0 obj\n<< /Type /XObject /Subtype /Image /Width {w} /Height {h}");
437 s.push_str(" /ColorSpace /DeviceRGB /BitsPerComponent 8 /Filter /FlateDecode");
438 if let Some(m) = smask {
439 let _ = write!(s, " /SMask {m} 0 R");
440 }
441 let _ = write!(s, " /Length {} >>\nstream\n", rgb.len());
442 f.extend_from_slice(s.as_bytes());
443 f.extend_from_slice(rgb);
444 f.extend_from_slice(b"\nendstream\nendobj\n");
445}
446
447fn pdf_mask_obj(f: &mut Vec<u8>, n: usize, w: i32, h: i32, a: &[u8]) {
449 let mut s = String::new();
450 let _ = write!(s, "{n} 0 obj\n<< /Type /XObject /Subtype /Image /Width {w} /Height {h}");
451 let _ = write!(s, " /ColorSpace /DeviceGray /BitsPerComponent 8 /Filter /FlateDecode /Length {} >>\nstream\n", a.len());
452 f.extend_from_slice(s.as_bytes());
453 f.extend_from_slice(a);
454 f.extend_from_slice(b"\nendstream\nendobj\n");
455}
456
457pub fn pdf_write_image(src: &PrintSrc, opt: &PdfOpt) -> Result<Vec<u8>, i32> {
461 let (w, h) = (src.w, src.h);
462 let (rgb, alpha) = pdf_image_data(src, opt.flags, opt.level)?;
463 let nobj = if alpha.is_some() { 7 } else { 6 };
464 let mut off = [0usize; 8];
465 let mut f: Vec<u8> = Vec::with_capacity(rgb.len() + alpha.as_ref().map_or(0, |a| a.len()) + 4096);
466 let mut s = String::new();
467 f.extend_from_slice(b"%PDF-1.4\n%\xE2\xE3\xCF\xD3\n");
468 off[1] = f.len();
469 f.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
470 off[2] = f.len();
471 f.extend_from_slice(b"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n");
472 off[3] = f.len();
474 s.push_str("3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0");
475 pw_nums(&mut s, opt.page_w, opt.page_h);
476 s.push_str("] /Resources << /XObject << /Im1 5 0 R >> /ProcSet [/PDF /ImageC] >> /Contents 4 0 R >>\nendobj\n");
477 f.extend_from_slice(s.as_bytes());
478 let c = pdf_content(opt);
480 off[4] = f.len();
481 s.clear();
482 let _ = write!(s, "4 0 obj\n<< /Length {} >>\nstream\n", c.len());
483 f.extend_from_slice(s.as_bytes());
484 f.extend_from_slice(c.as_bytes());
485 f.extend_from_slice(b"\nendstream\nendobj\n");
486 off[5] = f.len();
488 pdf_image_obj(&mut f, 5, w, h, &rgb, (nobj == 7).then_some(7));
489 off[6] = f.len();
491 s.clear();
492 s.push_str("6 0 obj\n<< /Producer (jfguiLib)");
493 if !opt.title.is_empty() && !opt.title.starts_with('\0') {
494 pw_title(&mut s, opt.title);
495 }
496 s.push_str(" >>\nendobj\n");
497 f.extend_from_slice(s.as_bytes());
498 if let Some(a) = &alpha {
500 off[7] = f.len();
501 pdf_mask_obj(&mut f, 7, w, h, a);
502 }
503 let startxref = f.len();
505 s.clear();
506 let _ = write!(s, "xref\n0 {}\n0000000000 65535 f \n", nobj + 1);
507 for o in &off[1..=nobj] {
508 let _ = write!(s, "{o:010} 00000 n \n");
509 }
510 let _ = write!(s, "trailer\n<< /Size {} /Root 1 0 R /Info 6 0 R >>\nstartxref\n{startxref}\n", nobj + 1);
511 s.push_str("%%EOF\n");
512 f.extend_from_slice(s.as_bytes());
513 Ok(f)
514}
515
516#[derive(Default)]
521pub(crate) struct PdfPages {
522 objs: Vec<Vec<u8>>,
524 pages: Vec<usize>,
525}
526
527impl PdfPages {
528 fn add(&mut self, src: &PrintSrc, opt: &PdfOpt) -> Result<(), i32> {
530 let (rgb, alpha) = pdf_image_data(src, opt.flags, opt.level)?;
531 let n = 4 + self.objs.len(); let mut s = String::new();
533 let _ = write!(s, "{n} 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0");
534 pw_nums(&mut s, opt.page_w, opt.page_h);
535 let _ = write!(s, "] /Resources << /XObject << /Im1 {} 0 R >> /ProcSet [/PDF /ImageC] >> /Contents {} 0 R >>\nendobj\n", n + 2, n + 1);
536 self.objs.push(s.into_bytes());
537 let c = pdf_content(opt);
538 let mut o = Vec::with_capacity(c.len() + 64);
539 let mut s = String::new();
540 let _ = write!(s, "{} 0 obj\n<< /Length {} >>\nstream\n", n + 1, c.len());
541 o.extend_from_slice(s.as_bytes());
542 o.extend_from_slice(c.as_bytes());
543 o.extend_from_slice(b"\nendstream\nendobj\n");
544 self.objs.push(o);
545 let mut o = Vec::with_capacity(rgb.len() + 200);
546 pdf_image_obj(&mut o, n + 2, src.w, src.h, &rgb, alpha.as_ref().map(|_| n + 3));
547 self.objs.push(o);
548 if let Some(a) = &alpha {
549 let mut o = Vec::with_capacity(a.len() + 200);
550 pdf_mask_obj(&mut o, n + 3, src.w, src.h, a);
551 self.objs.push(o);
552 }
553 self.pages.push(n);
554 Ok(())
555 }
556
557 fn finish(&self, title: &str) -> Vec<u8> {
559 let total: usize = self.objs.iter().map(|o| o.len()).sum();
560 let mut f: Vec<u8> = Vec::with_capacity(total + 1024 + self.pages.len() * 16);
561 let mut off = Vec::with_capacity(self.objs.len() + 4);
562 f.extend_from_slice(b"%PDF-1.4\n%\xE2\xE3\xCF\xD3\n");
563 off.push(f.len());
564 f.extend_from_slice(b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
565 off.push(f.len());
566 let mut s = String::from("2 0 obj\n<< /Type /Pages /Kids [");
567 for (k, p) in self.pages.iter().enumerate() {
568 let _ = write!(s, "{}{p} 0 R", if k > 0 { " " } else { "" });
569 }
570 let _ = write!(s, "] /Count {} >>\nendobj\n", self.pages.len());
571 f.extend_from_slice(s.as_bytes());
572 off.push(f.len());
573 s.clear();
574 s.push_str("3 0 obj\n<< /Producer (jfguiLib)");
575 if !title.is_empty() && !title.starts_with('\0') {
576 pw_title(&mut s, title);
577 }
578 s.push_str(" >>\nendobj\n");
579 f.extend_from_slice(s.as_bytes());
580 for o in &self.objs {
581 off.push(f.len());
582 f.extend_from_slice(o);
583 }
584 let startxref = f.len();
585 s.clear();
586 let _ = write!(s, "xref\n0 {}\n0000000000 65535 f \n", off.len() + 1);
587 for o in &off {
588 let _ = write!(s, "{o:010} 00000 n \n");
589 }
590 let _ = write!(s, "trailer\n<< /Size {} /Root 1 0 R /Info 3 0 R >>\nstartxref\n{startxref}\n%%EOF\n", off.len() + 1);
591 f.extend_from_slice(s.as_bytes());
592 f
593 }
594}
595
596#[derive(Default)]
599pub(crate) struct DocSys {
600 #[cfg_attr(not(windows), allow(dead_code))]
601 pub hdc: isize,
602 #[cfg_attr(not(windows), allow(dead_code))]
603 pub dpi: [f64; 2],
604 #[cfg_attr(not(windows), allow(dead_code))]
605 pub off: [i32; 2],
606 #[cfg_attr(not(windows), allow(dead_code))]
607 pub started: bool,
608 pub pdf: Option<PdfPages>,
610 pub raw: Option<Vec<u8>>,
612 pub to: Option<String>,
614 pub ranges: Vec<(u32, u32)>,
616}
617
618pub struct PrintDoc {
620 pub(crate) title: String,
621 pub(crate) dpi_x: f64,
622 pub(crate) dpi_y: f64,
623 pub(crate) flags: u32,
624 pub(crate) scale: u32,
625 pub(crate) percent: u32,
626 pub(crate) margin: u32,
627 pub(crate) paper: u32,
628 #[cfg_attr(not(windows), allow(dead_code))] pub(crate) pages: u32,
630 pub(crate) page: PPage,
631 pub(crate) copies: u32,
632 pub(crate) added: u32,
633 pub(crate) sys: DocSys,
634}
635
636impl PrintDoc {
637 pub fn begin(job: &PrintJob, pages: u32) -> Result<PrintDoc, i32> {
644 if !crate::policy::print_allowed() {
645 return Err(ERR_UNSUPPORTED);
646 }
647 let title = if job.title.is_empty() || job.title.starts_with('\0') { "Dokument" } else { job.title };
648 let mut d = PrintDoc {
649 title: String::from(title),
650 dpi_x: job.dpi_x,
651 dpi_y: job.dpi_y,
652 flags: job.flags,
653 scale: job.scale,
654 percent: job.percent,
655 margin: job.margin,
656 paper: job.paper,
657 pages,
658 page: page_init(job.paper, job.flags),
659 copies: 0,
660 added: 0,
661 sys: DocSys::default(),
662 };
663 let r = crate::doc_begin(&mut d);
664 if r != 0 {
665 return Err(r);
666 }
667 Ok(d)
668 }
669
670 pub fn page(&self) -> PPage {
673 self.page
674 }
675
676 pub fn copies(&self) -> u32 {
678 self.copies
679 }
680
681 pub fn wants(&self, n: u32) -> bool {
683 self.sys.ranges.is_empty() || self.sys.ranges.iter().any(|&(a, b)| n >= a && n <= b)
684 }
685
686 pub fn added(&self) -> u32 {
688 self.added
689 }
690
691 pub fn pdf_native(&self) -> bool {
694 self.sys.pdf.is_some()
695 }
696
697 fn job_of<'a>(&'a self, src: &PrintSrc<'a>) -> PrintJob<'a> {
699 PrintJob {
700 src: Some(*src),
701 title: &self.title,
702 dpi_x: self.dpi_x,
703 dpi_y: self.dpi_y,
704 flags: self.flags,
705 scale: self.scale,
706 percent: self.percent,
707 margin: self.margin,
708 paper: self.paper,
709 copies: 0,
710 page_w: self.page.w,
711 page_h: self.page.h,
712 }
713 }
714
715 pub fn add_image(&mut self, src: &PrintSrc) -> i32 {
719 if src.w <= 0 || src.h <= 0 || src.px.is_empty() {
720 return ERR_DIMS;
721 }
722 if self.sys.raw.is_some() {
723 return ERR_UNSUPPORTED;
724 }
725 let pl = layout(&self.job_of(src), &self.page);
726 let r = if let Some(pdf) = self.sys.pdf.as_mut() {
727 let opt = PdfOpt {
728 page_w: self.page.w,
729 page_h: self.page.h,
730 x: pl.x,
731 y: pl.y,
732 w: pl.w,
733 h: pl.h,
734 cx: pl.ax,
735 cy: pl.ay,
736 cw: pl.aw,
737 ch: pl.ah,
738 title: "",
739 flags: self.flags & PJF_SMASK,
740 level: 0,
741 };
742 pdf.add(src, &opt).err().unwrap_or(0)
743 } else {
744 crate::doc_image(self, src, &pl)
745 };
746 if r == 0 {
747 self.added += 1;
748 }
749 r
750 }
751
752 pub fn add_pdf(&mut self, pdf: &[u8]) -> i32 {
755 if !self.pdf_native() || self.added > 0 || self.sys.raw.is_some() || !pdf.starts_with(b"%PDF-") {
756 return ERR_UNSUPPORTED;
757 }
758 self.sys.raw = Some(Vec::from(pdf));
759 self.added = 1;
760 0
761 }
762
763 pub fn end(mut self) -> i32 {
766 if self.added == 0 {
767 crate::doc_abort(&mut self);
768 return ERR_DIMS;
769 }
770 crate::doc_end(&mut self)
771 }
772
773 pub fn abort(mut self) {
775 crate::doc_abort(&mut self);
776 }
777
778 pub(crate) fn pdf_bytes(&self) -> Option<Vec<u8>> {
780 if let Some(r) = &self.sys.raw {
781 return Some(r.clone());
782 }
783 self.sys.pdf.as_ref().map(|p| p.finish(&self.title))
784 }
785}
786
787#[allow(dead_code)]
790pub(crate) fn doc_begin_pdf_only(d: &mut PrintDoc) -> i32 {
791 match crate::env("jfgui_PRINT_TO").filter(|v| !v.is_empty()) {
792 Some(to) => {
793 d.sys.to = Some(to);
794 d.sys.pdf = Some(PdfPages::default());
795 d.copies = 1;
796 0
797 }
798 None => ERR_UNSUPPORTED,
799 }
800}
801
802#[allow(dead_code)]
804pub(crate) fn doc_end_pdf_file(d: &mut PrintDoc) -> i32 {
805 let (Some(to), Some(pdf)) = (d.sys.to.clone(), d.pdf_bytes()) else { return ERR_UNSUPPORTED };
806 if crate::write_file_raw(&to, &pdf) { 0 } else { ERR_IO }
807}