]> git.ipfire.org Git - thirdparty/cups.git/blob - filter/pstops.c
Full sweep of all Clang warnings, plus some bug fixes for incorrect memcpy usage.
[thirdparty/cups.git] / filter / pstops.c
1 /*
2 * "$Id$"
3 *
4 * PostScript filter for CUPS.
5 *
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1993-2007 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 */
17
18 /*
19 * Include necessary headers...
20 */
21
22 #include "common.h"
23 #include <limits.h>
24 #include <math.h>
25 #include <cups/file.h>
26 #include <cups/array.h>
27 #include <cups/language-private.h>
28 #include <signal.h>
29
30
31 /*
32 * Constants...
33 */
34
35 #define PSTOPS_BORDERNONE 0 /* No border or hairline border */
36 #define PSTOPS_BORDERTHICK 1 /* Think border */
37 #define PSTOPS_BORDERSINGLE 2 /* Single-line hairline border */
38 #define PSTOPS_BORDERSINGLE2 3 /* Single-line thick border */
39 #define PSTOPS_BORDERDOUBLE 4 /* Double-line hairline border */
40 #define PSTOPS_BORDERDOUBLE2 5 /* Double-line thick border */
41
42 #define PSTOPS_LAYOUT_LRBT 0 /* Left to right, bottom to top */
43 #define PSTOPS_LAYOUT_LRTB 1 /* Left to right, top to bottom */
44 #define PSTOPS_LAYOUT_RLBT 2 /* Right to left, bottom to top */
45 #define PSTOPS_LAYOUT_RLTB 3 /* Right to left, top to bottom */
46 #define PSTOPS_LAYOUT_BTLR 4 /* Bottom to top, left to right */
47 #define PSTOPS_LAYOUT_TBLR 5 /* Top to bottom, left to right */
48 #define PSTOPS_LAYOUT_BTRL 6 /* Bottom to top, right to left */
49 #define PSTOPS_LAYOUT_TBRL 7 /* Top to bottom, right to left */
50
51 #define PSTOPS_LAYOUT_NEGATEY 1 /* The bits for the layout */
52 #define PSTOPS_LAYOUT_NEGATEX 2 /* definitions above... */
53 #define PSTOPS_LAYOUT_VERTICAL 4
54
55
56 /*
57 * Types...
58 */
59
60 typedef struct /**** Page information ****/
61 {
62 char *label; /* Page label */
63 int bounding_box[4]; /* PageBoundingBox */
64 off_t offset; /* Offset to start of page */
65 ssize_t length; /* Number of bytes for page */
66 int num_options; /* Number of options for this page */
67 cups_option_t *options; /* Options for this page */
68 } pstops_page_t;
69
70 typedef struct /**** Document information ****/
71 {
72 int page; /* Current page */
73 int bounding_box[4]; /* BoundingBox from header */
74 int new_bounding_box[4]; /* New composite bounding box */
75 int num_options; /* Number of document-wide options */
76 cups_option_t *options; /* Document-wide options */
77 int normal_landscape, /* Normal rotation for landscape? */
78 saw_eof, /* Saw the %%EOF comment? */
79 slow_collate, /* Collate copies by hand? */
80 slow_duplex, /* Duplex pages slowly? */
81 slow_order, /* Reverse pages slowly? */
82 use_ESPshowpage; /* Use ESPshowpage? */
83 cups_array_t *pages; /* Pages in document */
84 cups_file_t *temp; /* Temporary file, if any */
85 char tempfile[1024]; /* Temporary filename */
86 int job_id; /* Job ID */
87 const char *user, /* User name */
88 *title; /* Job name */
89 int copies; /* Number of copies */
90 const char *ap_input_slot, /* AP_FIRSTPAGE_InputSlot value */
91 *ap_manual_feed, /* AP_FIRSTPAGE_ManualFeed value */
92 *ap_media_color, /* AP_FIRSTPAGE_MediaColor value */
93 *ap_media_type, /* AP_FIRSTPAGE_MediaType value */
94 *ap_page_region, /* AP_FIRSTPAGE_PageRegion value */
95 *ap_page_size; /* AP_FIRSTPAGE_PageSize value */
96 int collate, /* Collate copies? */
97 emit_jcl, /* Emit JCL commands? */
98 fit_to_page; /* Fit pages to media */
99 const char *input_slot, /* InputSlot value */
100 *manual_feed, /* ManualFeed value */
101 *media_color, /* MediaColor value */
102 *media_type, /* MediaType value */
103 *page_region, /* PageRegion value */
104 *page_size; /* PageSize value */
105 int mirror, /* doc->mirror/mirror pages */
106 number_up, /* Number of pages on each sheet */
107 number_up_layout, /* doc->number_up_layout of N-up pages */
108 output_order, /* Requested reverse output order? */
109 page_border; /* doc->page_border around pages */
110 const char *page_label, /* page-label option, if any */
111 *page_ranges, /* page-ranges option, if any */
112 *page_set; /* page-set option, if any */
113 } pstops_doc_t;
114
115
116 /*
117 * Convenience macros...
118 */
119
120 #define is_first_page(p) (doc->number_up == 1 || \
121 ((p) % doc->number_up) == 1)
122 #define is_last_page(p) (doc->number_up == 1 || \
123 ((p) % doc->number_up) == 0)
124 #define is_not_last_page(p) (doc->number_up > 1 && \
125 ((p) % doc->number_up) != 0)
126
127
128 /*
129 * Local globals...
130 */
131
132 static int JobCanceled = 0;/* Set to 1 on SIGTERM */
133
134
135 /*
136 * Local functions...
137 */
138
139 static pstops_page_t *add_page(pstops_doc_t *doc, const char *label);
140 static void cancel_job(int sig);
141 static int check_range(pstops_doc_t *doc, int page);
142 static void copy_bytes(cups_file_t *fp, off_t offset,
143 size_t length);
144 static ssize_t copy_comments(cups_file_t *fp, pstops_doc_t *doc,
145 ppd_file_t *ppd, char *line,
146 ssize_t linelen, size_t linesize);
147 static void copy_dsc(cups_file_t *fp, pstops_doc_t *doc,
148 ppd_file_t *ppd, char *line, ssize_t linelen,
149 size_t linesize);
150 static void copy_non_dsc(cups_file_t *fp, pstops_doc_t *doc,
151 ppd_file_t *ppd, char *line,
152 ssize_t linelen, size_t linesize);
153 static ssize_t copy_page(cups_file_t *fp, pstops_doc_t *doc,
154 ppd_file_t *ppd, int number, char *line,
155 ssize_t linelen, size_t linesize);
156 static ssize_t copy_prolog(cups_file_t *fp, pstops_doc_t *doc,
157 ppd_file_t *ppd, char *line,
158 ssize_t linelen, size_t linesize);
159 static ssize_t copy_setup(cups_file_t *fp, pstops_doc_t *doc,
160 ppd_file_t *ppd, char *line,
161 ssize_t linelen, size_t linesize);
162 static ssize_t copy_trailer(cups_file_t *fp, pstops_doc_t *doc,
163 ppd_file_t *ppd, int number, char *line,
164 ssize_t linelen, size_t linesize);
165 static void do_prolog(pstops_doc_t *doc, ppd_file_t *ppd);
166 static void do_setup(pstops_doc_t *doc, ppd_file_t *ppd);
167 static void doc_printf(pstops_doc_t *doc, const char *format, ...)
168 __attribute__ ((__format__ (__printf__, 2, 3)));
169 static void doc_puts(pstops_doc_t *doc, const char *s);
170 static void doc_write(pstops_doc_t *doc, const char *s, size_t len);
171 static void end_nup(pstops_doc_t *doc, int number);
172 static int include_feature(ppd_file_t *ppd, const char *line,
173 int num_options,
174 cups_option_t **options);
175 static char *parse_text(const char *start, char **end, char *buffer,
176 size_t bufsize);
177 static void set_pstops_options(pstops_doc_t *doc, ppd_file_t *ppd,
178 char *argv[], int num_options,
179 cups_option_t *options);
180 static ssize_t skip_page(cups_file_t *fp, char *line, ssize_t linelen,
181 size_t linesize);
182 static void start_nup(pstops_doc_t *doc, int number,
183 int show_border, const int *bounding_box);
184 static void write_label_prolog(pstops_doc_t *doc, const char *label,
185 float bottom, float top,
186 float width);
187 static void write_labels(pstops_doc_t *doc, int orient);
188 static void write_options(pstops_doc_t *doc, ppd_file_t *ppd,
189 int num_options, cups_option_t *options);
190
191
192 /*
193 * 'main()' - Main entry.
194 */
195
196 int /* O - Exit status */
197 main(int argc, /* I - Number of command-line args */
198 char *argv[]) /* I - Command-line arguments */
199 {
200 pstops_doc_t doc; /* Document information */
201 cups_file_t *fp; /* Print file */
202 ppd_file_t *ppd; /* PPD file */
203 int num_options; /* Number of print options */
204 cups_option_t *options; /* Print options */
205 char line[8192]; /* Line buffer */
206 ssize_t len; /* Length of line buffer */
207 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
208 struct sigaction action; /* Actions for POSIX signals */
209 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
210
211
212 /*
213 * Make sure status messages are not buffered...
214 */
215
216 setbuf(stderr, NULL);
217
218 /*
219 * Ignore broken pipe signals...
220 */
221
222 signal(SIGPIPE, SIG_IGN);
223
224 /*
225 * Check command-line...
226 */
227
228 if (argc < 6 || argc > 7)
229 {
230 _cupsLangPrintf(stderr,
231 _("Usage: %s job-id user title copies options [file]"),
232 argv[0]);
233 return (1);
234 }
235
236 /*
237 * Register a signal handler to cleanly cancel a job.
238 */
239
240 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
241 sigset(SIGTERM, cancel_job);
242 #elif defined(HAVE_SIGACTION)
243 memset(&action, 0, sizeof(action));
244
245 sigemptyset(&action.sa_mask);
246 action.sa_handler = cancel_job;
247 sigaction(SIGTERM, &action, NULL);
248 #else
249 signal(SIGTERM, cancel_job);
250 #endif /* HAVE_SIGSET */
251
252 /*
253 * If we have 7 arguments, print the file named on the command-line.
254 * Otherwise, send stdin instead...
255 */
256
257 if (argc == 6)
258 fp = cupsFileStdin();
259 else
260 {
261 /*
262 * Try to open the print file...
263 */
264
265 if ((fp = cupsFileOpen(argv[6], "r")) == NULL)
266 {
267 _cupsLangPrintError("ERROR", _("Unable to open print file"));
268 return (1);
269 }
270 }
271
272 /*
273 * Read the first line to see if we have DSC comments...
274 */
275
276 if ((len = (ssize_t)cupsFileGetLine(fp, line, sizeof(line))) == 0)
277 {
278 fputs("DEBUG: The print file is empty.\n", stderr);
279 return (1);
280 }
281
282 /*
283 * Process command-line options...
284 */
285
286 options = NULL;
287 num_options = cupsParseOptions(argv[5], 0, &options);
288 ppd = SetCommonOptions(num_options, options, 1);
289
290 set_pstops_options(&doc, ppd, argv, num_options, options);
291
292 /*
293 * Write any "exit server" options that have been selected...
294 */
295
296 ppdEmit(ppd, stdout, PPD_ORDER_EXIT);
297
298 /*
299 * Write any JCL commands that are needed to print PostScript code...
300 */
301
302 if (doc.emit_jcl)
303 ppdEmitJCL(ppd, stdout, doc.job_id, doc.user, doc.title);
304
305 /*
306 * Start with a DSC header...
307 */
308
309 puts("%!PS-Adobe-3.0");
310
311 /*
312 * Skip leading PJL in the document...
313 */
314
315 while (!strncmp(line, "\033%-12345X", 9) || !strncmp(line, "@PJL ", 5))
316 {
317 /*
318 * Yup, we have leading PJL fun, so skip it until we hit the line
319 * with "ENTER LANGUAGE"...
320 */
321
322 fputs("DEBUG: Skipping PJL header...\n", stderr);
323
324 while (strstr(line, "ENTER LANGUAGE") == NULL && strncmp(line, "%!", 2))
325 if ((len = (ssize_t)cupsFileGetLine(fp, line, sizeof(line))) == 0)
326 break;
327
328 if (!strncmp(line, "%!", 2))
329 break;
330
331 if ((len = (ssize_t)cupsFileGetLine(fp, line, sizeof(line))) == 0)
332 break;
333 }
334
335 /*
336 * Now see if the document conforms to the Adobe Document Structuring
337 * Conventions...
338 */
339
340 if (!strncmp(line, "%!PS-Adobe-", 11))
341 {
342 /*
343 * Yes, filter the document...
344 */
345
346 copy_dsc(fp, &doc, ppd, line, len, sizeof(line));
347 }
348 else
349 {
350 /*
351 * No, display an error message and treat the file as if it contains
352 * a single page...
353 */
354
355 copy_non_dsc(fp, &doc, ppd, line, len, sizeof(line));
356 }
357
358 /*
359 * Send %%EOF as needed...
360 */
361
362 if (!doc.saw_eof)
363 puts("%%EOF");
364
365 /*
366 * End the job with the appropriate JCL command or CTRL-D...
367 */
368
369 if (doc.emit_jcl)
370 {
371 if (ppd && ppd->jcl_end)
372 ppdEmitJCLEnd(ppd, stdout);
373 else
374 putchar(0x04);
375 }
376
377 /*
378 * Close files and remove the temporary file if needed...
379 */
380
381 if (doc.temp)
382 {
383 cupsFileClose(doc.temp);
384 unlink(doc.tempfile);
385 }
386
387 ppdClose(ppd);
388 cupsFreeOptions(num_options, options);
389
390 cupsFileClose(fp);
391
392 return (0);
393 }
394
395
396 /*
397 * 'add_page()' - Add a page to the pages array.
398 */
399
400 static pstops_page_t * /* O - New page info object */
401 add_page(pstops_doc_t *doc, /* I - Document information */
402 const char *label) /* I - Page label */
403 {
404 pstops_page_t *pageinfo; /* New page info object */
405
406
407 if (!doc->pages)
408 doc->pages = cupsArrayNew(NULL, NULL);
409
410 if (!doc->pages)
411 {
412 _cupsLangPrintError("EMERG", _("Unable to allocate memory for pages array"));
413 exit(1);
414 }
415
416 if ((pageinfo = calloc(1, sizeof(pstops_page_t))) == NULL)
417 {
418 _cupsLangPrintError("EMERG", _("Unable to allocate memory for page info"));
419 exit(1);
420 }
421
422 pageinfo->label = strdup(label);
423 pageinfo->offset = cupsFileTell(doc->temp);
424
425 cupsArrayAdd(doc->pages, pageinfo);
426
427 doc->page ++;
428
429 return (pageinfo);
430 }
431
432
433 /*
434 * 'cancel_job()' - Flag the job as canceled.
435 */
436
437 static void
438 cancel_job(int sig) /* I - Signal number (unused) */
439 {
440 (void)sig;
441
442 JobCanceled = 1;
443 }
444
445
446 /*
447 * 'check_range()' - Check to see if the current page is selected for
448 * printing.
449 */
450
451 static int /* O - 1 if selected, 0 otherwise */
452 check_range(pstops_doc_t *doc, /* I - Document information */
453 int page) /* I - Page number */
454 {
455 const char *range; /* Pointer into range string */
456 int lower, upper; /* Lower and upper page numbers */
457
458
459 if (doc->page_set)
460 {
461 /*
462 * See if we only print even or odd pages...
463 */
464
465 if (!_cups_strcasecmp(doc->page_set, "even") && (page & 1))
466 return (0);
467
468 if (!_cups_strcasecmp(doc->page_set, "odd") && !(page & 1))
469 return (0);
470 }
471
472 if (!doc->page_ranges)
473 return (1); /* No range, print all pages... */
474
475 for (range = doc->page_ranges; *range != '\0';)
476 {
477 if (*range == '-')
478 {
479 lower = 1;
480 range ++;
481 upper = (int)strtol(range, (char **)&range, 10);
482 }
483 else
484 {
485 lower = (int)strtol(range, (char **)&range, 10);
486
487 if (*range == '-')
488 {
489 range ++;
490 if (!isdigit(*range & 255))
491 upper = 65535;
492 else
493 upper = (int)strtol(range, (char **)&range, 10);
494 }
495 else
496 upper = lower;
497 }
498
499 if (page >= lower && page <= upper)
500 return (1);
501
502 if (*range == ',')
503 range ++;
504 else
505 break;
506 }
507
508 return (0);
509 }
510
511
512 /*
513 * 'copy_bytes()' - Copy bytes from the input file to stdout.
514 */
515
516 static void
517 copy_bytes(cups_file_t *fp, /* I - File to read from */
518 off_t offset, /* I - Offset to page data */
519 size_t length) /* I - Length of page data */
520 {
521 char buffer[8192]; /* Data buffer */
522 ssize_t nbytes; /* Number of bytes read */
523 size_t nleft; /* Number of bytes left/remaining */
524
525
526 nleft = length;
527
528 if (cupsFileSeek(fp, offset) < 0)
529 {
530 _cupsLangPrintError("ERROR", _("Unable to see in file"));
531 return;
532 }
533
534 while (nleft > 0 || length == 0)
535 {
536 if (nleft > sizeof(buffer) || length == 0)
537 nbytes = sizeof(buffer);
538 else
539 nbytes = (ssize_t)nleft;
540
541 if ((nbytes = cupsFileRead(fp, buffer, (size_t)nbytes)) < 1)
542 return;
543
544 nleft -= (size_t)nbytes;
545
546 fwrite(buffer, 1, (size_t)nbytes, stdout);
547 }
548 }
549
550
551 /*
552 * 'copy_comments()' - Copy all of the comments section.
553 *
554 * This function expects "line" to be filled with a comment line.
555 * On return, "line" will contain the next line in the file, if any.
556 */
557
558 static ssize_t /* O - Length of next line */
559 copy_comments(cups_file_t *fp, /* I - File to read from */
560 pstops_doc_t *doc, /* I - Document info */
561 ppd_file_t *ppd, /* I - PPD file */
562 char *line, /* I - Line buffer */
563 ssize_t linelen, /* I - Length of initial line */
564 size_t linesize) /* I - Size of line buffer */
565 {
566 int saw_bounding_box, /* Saw %%BoundingBox: comment? */
567 saw_for, /* Saw %%For: comment? */
568 saw_pages, /* Saw %%Pages: comment? */
569 saw_title; /* Saw %%Title: comment? */
570
571
572 /*
573 * Loop until we see %%EndComments or a non-comment line...
574 */
575
576 saw_bounding_box = 0;
577 saw_for = 0;
578 saw_pages = 0;
579 saw_title = 0;
580
581 while (line[0] == '%')
582 {
583 /*
584 * Strip trailing whitespace...
585 */
586
587 while (linelen > 0)
588 {
589 linelen --;
590
591 if (!isspace(line[linelen] & 255))
592 break;
593 else
594 line[linelen] = '\0';
595 }
596
597 /*
598 * Log the header...
599 */
600
601 fprintf(stderr, "DEBUG: %s\n", line);
602
603 /*
604 * Pull the headers out...
605 */
606
607 if (!strncmp(line, "%%Pages:", 8))
608 {
609 int pages; /* Number of pages */
610
611 if (saw_pages)
612 fputs("DEBUG: A duplicate %%Pages: comment was seen.\n", stderr);
613
614 saw_pages = 1;
615
616 if (Duplex && (pages = atoi(line + 8)) > 0 && pages <= doc->number_up)
617 {
618 /*
619 * Since we will only be printing on a single page, disable duplexing.
620 */
621
622 Duplex = 0;
623 doc->slow_duplex = 0;
624
625 if (cupsGetOption("sides", doc->num_options, doc->options))
626 doc->num_options = cupsAddOption("sides", "one-sided",
627 doc->num_options, &(doc->options));
628
629 if (cupsGetOption("Duplex", doc->num_options, doc->options))
630 doc->num_options = cupsAddOption("Duplex", "None",
631 doc->num_options, &(doc->options));
632
633 if (cupsGetOption("EFDuplex", doc->num_options, doc->options))
634 doc->num_options = cupsAddOption("EFDuplex", "None",
635 doc->num_options, &(doc->options));
636
637 if (cupsGetOption("EFDuplexing", doc->num_options, doc->options))
638 doc->num_options = cupsAddOption("EFDuplexing", "False",
639 doc->num_options, &(doc->options));
640
641 if (cupsGetOption("KD03Duplex", doc->num_options, doc->options))
642 doc->num_options = cupsAddOption("KD03Duplex", "None",
643 doc->num_options, &(doc->options));
644
645 if (cupsGetOption("JCLDuplex", doc->num_options, doc->options))
646 doc->num_options = cupsAddOption("JCLDuplex", "None",
647 doc->num_options, &(doc->options));
648
649 ppdMarkOption(ppd, "Duplex", "None");
650 ppdMarkOption(ppd, "EFDuplex", "None");
651 ppdMarkOption(ppd, "EFDuplexing", "False");
652 ppdMarkOption(ppd, "KD03Duplex", "None");
653 ppdMarkOption(ppd, "JCLDuplex", "None");
654 }
655 }
656 else if (!strncmp(line, "%%BoundingBox:", 14))
657 {
658 if (saw_bounding_box)
659 fputs("DEBUG: A duplicate %%BoundingBox: comment was seen.\n", stderr);
660 else if (strstr(line + 14, "(atend)"))
661 {
662 /*
663 * Do nothing for now but use the default imageable area...
664 */
665 }
666 else if (sscanf(line + 14, "%d%d%d%d", doc->bounding_box + 0,
667 doc->bounding_box + 1, doc->bounding_box + 2,
668 doc->bounding_box + 3) != 4)
669 {
670 fputs("DEBUG: A bad %%BoundingBox: comment was seen.\n", stderr);
671
672 doc->bounding_box[0] = (int)PageLeft;
673 doc->bounding_box[1] = (int)PageBottom;
674 doc->bounding_box[2] = (int)PageRight;
675 doc->bounding_box[3] = (int)PageTop;
676 }
677
678 saw_bounding_box = 1;
679 }
680 else if (!strncmp(line, "%%For:", 6))
681 {
682 saw_for = 1;
683 doc_printf(doc, "%s\n", line);
684 }
685 else if (!strncmp(line, "%%Title:", 8))
686 {
687 saw_title = 1;
688 doc_printf(doc, "%s\n", line);
689 }
690 else if (!strncmp(line, "%cupsRotation:", 14))
691 {
692 /*
693 * Reset orientation of document?
694 */
695
696 int orient = (atoi(line + 14) / 90) & 3;
697
698 if (orient != Orientation)
699 {
700 /*
701 * Yes, update things so that the pages come out right...
702 */
703
704 Orientation = (4 - Orientation + orient) & 3;
705 UpdatePageVars();
706 Orientation = orient;
707 }
708 }
709 else if (!strcmp(line, "%%EndComments"))
710 {
711 linelen = (ssize_t)cupsFileGetLine(fp, line, linesize);
712 break;
713 }
714 else if (strncmp(line, "%!", 2) && strncmp(line, "%cups", 5))
715 doc_printf(doc, "%s\n", line);
716
717 if ((linelen = (ssize_t)cupsFileGetLine(fp, line, linesize)) == 0)
718 break;
719 }
720
721 if (!saw_bounding_box)
722 fputs("DEBUG: There wasn't a %%BoundingBox: comment in the header.\n",
723 stderr);
724
725 if (!saw_pages)
726 fputs("DEBUG: There wasn't a %%Pages: comment in the header.\n", stderr);
727
728 if (!saw_for)
729 WriteTextComment("For", doc->user);
730
731 if (!saw_title)
732 WriteTextComment("Title", doc->title);
733
734 if (doc->copies != 1 && (!doc->collate || !doc->slow_collate))
735 {
736 /*
737 * Tell the document processor the copy and duplex options
738 * that are required...
739 */
740
741 doc_printf(doc, "%%%%Requirements: numcopies(%d)%s%s\n", doc->copies,
742 doc->collate ? " collate" : "",
743 Duplex ? " duplex" : "");
744
745 /*
746 * Apple uses RBI comments for various non-PPD options...
747 */
748
749 doc_printf(doc, "%%RBINumCopies: %d\n", doc->copies);
750 }
751 else
752 {
753 /*
754 * Tell the document processor the duplex option that is required...
755 */
756
757 if (Duplex)
758 doc_puts(doc, "%%Requirements: duplex\n");
759
760 /*
761 * Apple uses RBI comments for various non-PPD options...
762 */
763
764 doc_puts(doc, "%RBINumCopies: 1\n");
765 }
766
767 doc_puts(doc, "%%Pages: (atend)\n");
768 doc_puts(doc, "%%BoundingBox: (atend)\n");
769 doc_puts(doc, "%%EndComments\n");
770
771 return (linelen);
772 }
773
774
775 /*
776 * 'copy_dsc()' - Copy a DSC-conforming document.
777 *
778 * This function expects "line" to be filled with the %!PS-Adobe comment line.
779 */
780
781 static void
782 copy_dsc(cups_file_t *fp, /* I - File to read from */
783 pstops_doc_t *doc, /* I - Document info */
784 ppd_file_t *ppd, /* I - PPD file */
785 char *line, /* I - Line buffer */
786 ssize_t linelen, /* I - Length of initial line */
787 size_t linesize) /* I - Size of line buffer */
788 {
789 int number; /* Page number */
790 pstops_page_t *pageinfo; /* Page information */
791
792
793 /*
794 * Make sure we use ESPshowpage for EPS files...
795 */
796
797 if (strstr(line, "EPSF"))
798 {
799 doc->use_ESPshowpage = 1;
800 doc->number_up = 1;
801 }
802
803 /*
804 * Start sending the document with any commands needed...
805 */
806
807 fprintf(stderr, "DEBUG: Before copy_comments - %s", line);
808 linelen = copy_comments(fp, doc, ppd, line, linelen, linesize);
809
810 /*
811 * Now find the prolog section, if any...
812 */
813
814 fprintf(stderr, "DEBUG: Before copy_prolog - %s", line);
815 linelen = copy_prolog(fp, doc, ppd, line, linelen, linesize);
816
817 /*
818 * Then the document setup section...
819 */
820
821 fprintf(stderr, "DEBUG: Before copy_setup - %s", line);
822 linelen = copy_setup(fp, doc, ppd, line, linelen, linesize);
823
824 /*
825 * Copy until we see %%Page:...
826 */
827
828 while (strncmp(line, "%%Page:", 7) && strncmp(line, "%%Trailer", 9))
829 {
830 doc_write(doc, line, (size_t)linelen);
831
832 if ((linelen = (ssize_t)cupsFileGetLine(fp, line, linesize)) == 0)
833 break;
834 }
835
836 /*
837 * Then process pages until we have no more...
838 */
839
840 number = 0;
841
842 fprintf(stderr, "DEBUG: Before page loop - %s", line);
843 while (!strncmp(line, "%%Page:", 7))
844 {
845 if (JobCanceled)
846 break;
847
848 number ++;
849
850 if (check_range(doc, (number - 1) / doc->number_up + 1))
851 {
852 fprintf(stderr, "DEBUG: Copying page %d...\n", number);
853 linelen = copy_page(fp, doc, ppd, number, line, linelen, linesize);
854 }
855 else
856 {
857 fprintf(stderr, "DEBUG: Skipping page %d...\n", number);
858 linelen = skip_page(fp, line, linelen, linesize);
859 }
860 }
861
862 /*
863 * Finish up the last page(s)...
864 */
865
866 if (number && is_not_last_page(number) && cupsArrayLast(doc->pages) &&
867 check_range(doc, (number - 1) / doc->number_up + 1))
868 {
869 pageinfo = (pstops_page_t *)cupsArrayLast(doc->pages);
870
871 start_nup(doc, doc->number_up, 0, doc->bounding_box);
872 doc_puts(doc, "showpage\n");
873 end_nup(doc, doc->number_up);
874
875 pageinfo->length = (ssize_t)(cupsFileTell(doc->temp) - pageinfo->offset);
876 }
877
878 if (doc->slow_duplex && (doc->page & 1))
879 {
880 /*
881 * Make sure we have an even number of pages...
882 */
883
884 pageinfo = add_page(doc, "(filler)");
885
886 if (!doc->slow_order)
887 {
888 if (!ppd || !ppd->num_filters)
889 fprintf(stderr, "PAGE: %d %d\n", doc->page,
890 doc->slow_collate ? 1 : doc->copies);
891
892 printf("%%%%Page: (filler) %d\n", doc->page);
893 }
894
895 start_nup(doc, doc->number_up, 0, doc->bounding_box);
896 doc_puts(doc, "showpage\n");
897 end_nup(doc, doc->number_up);
898
899 pageinfo->length = (ssize_t)(cupsFileTell(doc->temp) - pageinfo->offset);
900 }
901
902 /*
903 * Make additional copies as necessary...
904 */
905
906 number = doc->slow_order ? 0 : doc->page;
907
908 if (doc->temp && !JobCanceled && cupsArrayCount(doc->pages) > 0)
909 {
910 int copy; /* Current copy */
911
912
913 /*
914 * Reopen the temporary file for reading...
915 */
916
917 cupsFileClose(doc->temp);
918
919 doc->temp = cupsFileOpen(doc->tempfile, "r");
920
921 /*
922 * Make the copies...
923 */
924
925 if (doc->slow_collate)
926 copy = !doc->slow_order;
927 else
928 copy = doc->copies - 1;
929
930 for (; copy < doc->copies; copy ++)
931 {
932 if (JobCanceled)
933 break;
934
935 /*
936 * Send end-of-job stuff followed by any start-of-job stuff required
937 * for the JCL options...
938 */
939
940 if (number && doc->emit_jcl && ppd && ppd->jcl_end)
941 {
942 /*
943 * Send the trailer...
944 */
945
946 puts("%%Trailer");
947 printf("%%%%Pages: %d\n", cupsArrayCount(doc->pages));
948 if (doc->number_up > 1 || doc->fit_to_page)
949 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
950 PageLeft, PageBottom, PageRight, PageTop);
951 else
952 printf("%%%%BoundingBox: %d %d %d %d\n",
953 doc->new_bounding_box[0], doc->new_bounding_box[1],
954 doc->new_bounding_box[2], doc->new_bounding_box[3]);
955 puts("%%EOF");
956
957 /*
958 * Start a new document...
959 */
960
961 ppdEmitJCLEnd(ppd, stdout);
962 ppdEmitJCL(ppd, stdout, doc->job_id, doc->user, doc->title);
963
964 puts("%!PS-Adobe-3.0");
965
966 number = 0;
967 }
968
969 /*
970 * Copy the prolog as needed...
971 */
972
973 if (!number)
974 {
975 pageinfo = (pstops_page_t *)cupsArrayFirst(doc->pages);
976 copy_bytes(doc->temp, 0, (size_t)pageinfo->offset);
977 }
978
979 /*
980 * Then copy all of the pages...
981 */
982
983 pageinfo = doc->slow_order ? (pstops_page_t *)cupsArrayLast(doc->pages) :
984 (pstops_page_t *)cupsArrayFirst(doc->pages);
985
986 while (pageinfo)
987 {
988 if (JobCanceled)
989 break;
990
991 number ++;
992
993 if (!ppd || !ppd->num_filters)
994 fprintf(stderr, "PAGE: %d %d\n", number,
995 doc->slow_collate ? 1 : doc->copies);
996
997 if (doc->number_up > 1)
998 {
999 printf("%%%%Page: (%d) %d\n", number, number);
1000 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1001 PageLeft, PageBottom, PageRight, PageTop);
1002 }
1003 else
1004 {
1005 printf("%%%%Page: %s %d\n", pageinfo->label, number);
1006 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1007 pageinfo->bounding_box[0], pageinfo->bounding_box[1],
1008 pageinfo->bounding_box[2], pageinfo->bounding_box[3]);
1009 }
1010
1011 copy_bytes(doc->temp, pageinfo->offset, (size_t)pageinfo->length);
1012
1013 pageinfo = doc->slow_order ? (pstops_page_t *)cupsArrayPrev(doc->pages) :
1014 (pstops_page_t *)cupsArrayNext(doc->pages);
1015 }
1016 }
1017 }
1018
1019 /*
1020 * Restore the old showpage operator as needed...
1021 */
1022
1023 if (doc->use_ESPshowpage)
1024 puts("userdict/showpage/ESPshowpage load put\n");
1025
1026 /*
1027 * Write/copy the trailer...
1028 */
1029
1030 if (!JobCanceled)
1031 copy_trailer(fp, doc, ppd, number, line, linelen, linesize);
1032 }
1033
1034
1035 /*
1036 * 'copy_non_dsc()' - Copy a document that does not conform to the DSC.
1037 *
1038 * This function expects "line" to be filled with the %! comment line.
1039 */
1040
1041 static void
1042 copy_non_dsc(cups_file_t *fp, /* I - File to read from */
1043 pstops_doc_t *doc, /* I - Document info */
1044 ppd_file_t *ppd, /* I - PPD file */
1045 char *line, /* I - Line buffer */
1046 ssize_t linelen, /* I - Length of initial line */
1047 size_t linesize) /* I - Size of line buffer */
1048 {
1049 int copy; /* Current copy */
1050 char buffer[8192]; /* Copy buffer */
1051 ssize_t bytes; /* Number of bytes copied */
1052
1053
1054 (void)linesize;
1055
1056 /*
1057 * First let the user know that they are attempting to print a file
1058 * that may not print correctly...
1059 */
1060
1061 fputs("DEBUG: This document does not conform to the Adobe Document "
1062 "Structuring Conventions and may not print correctly.\n", stderr);
1063
1064 /*
1065 * Then write a standard DSC comment section...
1066 */
1067
1068 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", PageLeft, PageBottom,
1069 PageRight, PageTop);
1070
1071 if (doc->slow_collate && doc->copies > 1)
1072 printf("%%%%Pages: %d\n", doc->copies);
1073 else
1074 puts("%%Pages: 1");
1075
1076 WriteTextComment("For", doc->user);
1077 WriteTextComment("Title", doc->title);
1078
1079 if (doc->copies != 1 && (!doc->collate || !doc->slow_collate))
1080 {
1081 /*
1082 * Tell the document processor the copy and duplex options
1083 * that are required...
1084 */
1085
1086 printf("%%%%Requirements: numcopies(%d)%s%s\n", doc->copies,
1087 doc->collate ? " collate" : "",
1088 Duplex ? " duplex" : "");
1089
1090 /*
1091 * Apple uses RBI comments for various non-PPD options...
1092 */
1093
1094 printf("%%RBINumCopies: %d\n", doc->copies);
1095 }
1096 else
1097 {
1098 /*
1099 * Tell the document processor the duplex option that is required...
1100 */
1101
1102 if (Duplex)
1103 puts("%%Requirements: duplex");
1104
1105 /*
1106 * Apple uses RBI comments for various non-PPD options...
1107 */
1108
1109 puts("%RBINumCopies: 1");
1110 }
1111
1112 puts("%%EndComments");
1113
1114 /*
1115 * Then the prolog...
1116 */
1117
1118 puts("%%BeginProlog");
1119
1120 do_prolog(doc, ppd);
1121
1122 puts("%%EndProlog");
1123
1124 /*
1125 * Then the setup section...
1126 */
1127
1128 puts("%%BeginSetup");
1129
1130 do_setup(doc, ppd);
1131
1132 puts("%%EndSetup");
1133
1134 /*
1135 * Finally, embed a copy of the file inside a %%Page...
1136 */
1137
1138 if (!ppd || !ppd->num_filters)
1139 fprintf(stderr, "PAGE: 1 %d\n", doc->temp ? 1 : doc->copies);
1140
1141 puts("%%Page: 1 1");
1142 puts("%%BeginPageSetup");
1143 ppdEmit(ppd, stdout, PPD_ORDER_PAGE);
1144 puts("%%EndPageSetup");
1145 puts("%%BeginDocument: nondsc");
1146
1147 fwrite(line, (size_t)linelen, 1, stdout);
1148
1149 if (doc->temp)
1150 cupsFileWrite(doc->temp, line, (size_t)linelen);
1151
1152 while ((bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0)
1153 {
1154 fwrite(buffer, 1, (size_t)bytes, stdout);
1155
1156 if (doc->temp)
1157 cupsFileWrite(doc->temp, buffer, (size_t)bytes);
1158 }
1159
1160 puts("%%EndDocument");
1161
1162 if (doc->use_ESPshowpage)
1163 {
1164 WriteLabels(Orientation);
1165 puts("ESPshowpage");
1166 }
1167
1168 if (doc->temp && !JobCanceled)
1169 {
1170 /*
1171 * Reopen the temporary file for reading...
1172 */
1173
1174 cupsFileClose(doc->temp);
1175
1176 doc->temp = cupsFileOpen(doc->tempfile, "r");
1177
1178 /*
1179 * Make the additional copies as needed...
1180 */
1181
1182 for (copy = 1; copy < doc->copies; copy ++)
1183 {
1184 if (JobCanceled)
1185 break;
1186
1187 if (!ppd || !ppd->num_filters)
1188 fputs("PAGE: 1 1\n", stderr);
1189
1190 printf("%%%%Page: %d %d\n", copy + 1, copy + 1);
1191 puts("%%BeginPageSetup");
1192 ppdEmit(ppd, stdout, PPD_ORDER_PAGE);
1193 puts("%%EndPageSetup");
1194 puts("%%BeginDocument: nondsc");
1195
1196 copy_bytes(doc->temp, 0, 0);
1197
1198 puts("%%EndDocument");
1199
1200 if (doc->use_ESPshowpage)
1201 {
1202 WriteLabels(Orientation);
1203 puts("ESPshowpage");
1204 }
1205 }
1206 }
1207
1208 /*
1209 * Restore the old showpage operator as needed...
1210 */
1211
1212 if (doc->use_ESPshowpage)
1213 puts("userdict/showpage/ESPshowpage load put\n");
1214 }
1215
1216
1217 /*
1218 * 'copy_page()' - Copy a page description.
1219 *
1220 * This function expects "line" to be filled with a %%Page comment line.
1221 * On return, "line" will contain the next line in the file, if any.
1222 */
1223
1224 static ssize_t /* O - Length of next line */
1225 copy_page(cups_file_t *fp, /* I - File to read from */
1226 pstops_doc_t *doc, /* I - Document info */
1227 ppd_file_t *ppd, /* I - PPD file */
1228 int number, /* I - Current page number */
1229 char *line, /* I - Line buffer */
1230 ssize_t linelen, /* I - Length of initial line */
1231 size_t linesize) /* I - Size of line buffer */
1232 {
1233 char label[256], /* Page label string */
1234 *ptr; /* Pointer into line */
1235 int level; /* Embedded document level */
1236 pstops_page_t *pageinfo; /* Page information */
1237 int first_page; /* First page on N-up output? */
1238 int has_page_setup = 0; /* Does the page have %%Begin/EndPageSetup? */
1239 int bounding_box[4]; /* PageBoundingBox */
1240
1241
1242 /*
1243 * Get the page label for this page...
1244 */
1245
1246 first_page = is_first_page(number);
1247
1248 if (!parse_text(line + 7, &ptr, label, sizeof(label)))
1249 {
1250 fputs("DEBUG: There was a bad %%Page: comment in the file.\n", stderr);
1251 label[0] = '\0';
1252 number = doc->page;
1253 }
1254 else if (strtol(ptr, &ptr, 10) == LONG_MAX || !isspace(*ptr & 255))
1255 {
1256 fputs("DEBUG: There was a bad %%Page: comment in the file.\n", stderr);
1257 number = doc->page;
1258 }
1259
1260 /*
1261 * Create or update the current output page...
1262 */
1263
1264 if (first_page)
1265 pageinfo = add_page(doc, label);
1266 else
1267 pageinfo = (pstops_page_t *)cupsArrayLast(doc->pages);
1268
1269 /*
1270 * Handle first page override...
1271 */
1272
1273 if (doc->ap_input_slot || doc->ap_manual_feed)
1274 {
1275 if ((doc->page == 1 && (!doc->slow_order || !Duplex)) ||
1276 (doc->page == 2 && doc->slow_order && Duplex))
1277 {
1278 /*
1279 * First page/sheet gets AP_FIRSTPAGE_* options...
1280 */
1281
1282 pageinfo->num_options = cupsAddOption("InputSlot", doc->ap_input_slot,
1283 pageinfo->num_options,
1284 &(pageinfo->options));
1285 pageinfo->num_options = cupsAddOption("ManualFeed",
1286 doc->ap_input_slot ? "False" :
1287 doc->ap_manual_feed,
1288 pageinfo->num_options,
1289 &(pageinfo->options));
1290 pageinfo->num_options = cupsAddOption("MediaColor", doc->ap_media_color,
1291 pageinfo->num_options,
1292 &(pageinfo->options));
1293 pageinfo->num_options = cupsAddOption("MediaType", doc->ap_media_type,
1294 pageinfo->num_options,
1295 &(pageinfo->options));
1296 pageinfo->num_options = cupsAddOption("PageRegion", doc->ap_page_region,
1297 pageinfo->num_options,
1298 &(pageinfo->options));
1299 pageinfo->num_options = cupsAddOption("PageSize", doc->ap_page_size,
1300 pageinfo->num_options,
1301 &(pageinfo->options));
1302 }
1303 else if (doc->page == (Duplex + 2))
1304 {
1305 /*
1306 * Second page/sheet gets default options...
1307 */
1308
1309 pageinfo->num_options = cupsAddOption("InputSlot", doc->input_slot,
1310 pageinfo->num_options,
1311 &(pageinfo->options));
1312 pageinfo->num_options = cupsAddOption("ManualFeed",
1313 doc->input_slot ? "False" :
1314 doc->manual_feed,
1315 pageinfo->num_options,
1316 &(pageinfo->options));
1317 pageinfo->num_options = cupsAddOption("MediaColor", doc->media_color,
1318 pageinfo->num_options,
1319 &(pageinfo->options));
1320 pageinfo->num_options = cupsAddOption("MediaType", doc->media_type,
1321 pageinfo->num_options,
1322 &(pageinfo->options));
1323 pageinfo->num_options = cupsAddOption("PageRegion", doc->page_region,
1324 pageinfo->num_options,
1325 &(pageinfo->options));
1326 pageinfo->num_options = cupsAddOption("PageSize", doc->page_size,
1327 pageinfo->num_options,
1328 &(pageinfo->options));
1329 }
1330 }
1331
1332 /*
1333 * Scan comments until we see something other than %%Page*: or
1334 * %%Include*...
1335 */
1336
1337 memcpy(bounding_box, doc->bounding_box, sizeof(bounding_box));
1338
1339 while ((linelen = (ssize_t)cupsFileGetLine(fp, line, linesize)) > 0)
1340 {
1341 if (!strncmp(line, "%%PageBoundingBox:", 18))
1342 {
1343 /*
1344 * %%PageBoundingBox: llx lly urx ury
1345 */
1346
1347 if (sscanf(line + 18, "%d%d%d%d", bounding_box + 0,
1348 bounding_box + 1, bounding_box + 2,
1349 bounding_box + 3) != 4)
1350 {
1351 fputs("DEBUG: There was a bad %%PageBoundingBox: comment in the file.\n", stderr);
1352 memcpy(bounding_box, doc->bounding_box,
1353 sizeof(bounding_box));
1354 }
1355 else if (doc->number_up == 1 && !doc->fit_to_page && Orientation)
1356 {
1357 int temp_bbox[4]; /* Temporary bounding box */
1358
1359
1360 memcpy(temp_bbox, bounding_box, sizeof(temp_bbox));
1361
1362 fprintf(stderr, "DEBUG: Orientation = %d\n", Orientation);
1363 fprintf(stderr, "DEBUG: original bounding_box = [ %d %d %d %d ]\n",
1364 bounding_box[0], bounding_box[1],
1365 bounding_box[2], bounding_box[3]);
1366 fprintf(stderr, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
1367 PageWidth, PageLength);
1368
1369 switch (Orientation)
1370 {
1371 case 1 : /* Landscape */
1372 bounding_box[0] = (int)(PageLength - temp_bbox[3]);
1373 bounding_box[1] = temp_bbox[0];
1374 bounding_box[2] = (int)(PageLength - temp_bbox[1]);
1375 bounding_box[3] = temp_bbox[2];
1376 break;
1377
1378 case 2 : /* Reverse Portrait */
1379 bounding_box[0] = (int)(PageWidth - temp_bbox[2]);
1380 bounding_box[1] = (int)(PageLength - temp_bbox[3]);
1381 bounding_box[2] = (int)(PageWidth - temp_bbox[0]);
1382 bounding_box[3] = (int)(PageLength - temp_bbox[1]);
1383 break;
1384
1385 case 3 : /* Reverse Landscape */
1386 bounding_box[0] = temp_bbox[1];
1387 bounding_box[1] = (int)(PageWidth - temp_bbox[2]);
1388 bounding_box[2] = temp_bbox[3];
1389 bounding_box[3] = (int)(PageWidth - temp_bbox[0]);
1390 break;
1391 }
1392
1393 fprintf(stderr, "DEBUG: updated bounding_box = [ %d %d %d %d ]\n",
1394 bounding_box[0], bounding_box[1],
1395 bounding_box[2], bounding_box[3]);
1396 }
1397 }
1398 #if 0
1399 else if (!strncmp(line, "%%PageCustomColors:", 19) ||
1400 !strncmp(line, "%%PageMedia:", 12) ||
1401 !strncmp(line, "%%PageOrientation:", 18) ||
1402 !strncmp(line, "%%PageProcessColors:", 20) ||
1403 !strncmp(line, "%%PageRequirements:", 18) ||
1404 !strncmp(line, "%%PageResources:", 16))
1405 {
1406 /*
1407 * Copy literal...
1408 */
1409 }
1410 #endif /* 0 */
1411 else if (!strncmp(line, "%%PageCustomColors:", 19))
1412 {
1413 /*
1414 * %%PageCustomColors: ...
1415 */
1416 }
1417 else if (!strncmp(line, "%%PageMedia:", 12))
1418 {
1419 /*
1420 * %%PageMedia: ...
1421 */
1422 }
1423 else if (!strncmp(line, "%%PageOrientation:", 18))
1424 {
1425 /*
1426 * %%PageOrientation: ...
1427 */
1428 }
1429 else if (!strncmp(line, "%%PageProcessColors:", 20))
1430 {
1431 /*
1432 * %%PageProcessColors: ...
1433 */
1434 }
1435 else if (!strncmp(line, "%%PageRequirements:", 18))
1436 {
1437 /*
1438 * %%PageRequirements: ...
1439 */
1440 }
1441 else if (!strncmp(line, "%%PageResources:", 16))
1442 {
1443 /*
1444 * %%PageResources: ...
1445 */
1446 }
1447 else if (!strncmp(line, "%%IncludeFeature:", 17))
1448 {
1449 /*
1450 * %%IncludeFeature: *MainKeyword OptionKeyword
1451 */
1452
1453 if (doc->number_up == 1 &&!doc->fit_to_page)
1454 pageinfo->num_options = include_feature(ppd, line,
1455 pageinfo->num_options,
1456 &(pageinfo->options));
1457 }
1458 else if (!strncmp(line, "%%BeginPageSetup", 16))
1459 {
1460 has_page_setup = 1;
1461 break;
1462 }
1463 else
1464 break;
1465 }
1466
1467 if (doc->number_up == 1)
1468 {
1469 /*
1470 * Update the document's composite and page bounding box...
1471 */
1472
1473 memcpy(pageinfo->bounding_box, bounding_box,
1474 sizeof(pageinfo->bounding_box));
1475
1476 if (bounding_box[0] < doc->new_bounding_box[0])
1477 doc->new_bounding_box[0] = bounding_box[0];
1478 if (bounding_box[1] < doc->new_bounding_box[1])
1479 doc->new_bounding_box[1] = bounding_box[1];
1480 if (bounding_box[2] > doc->new_bounding_box[2])
1481 doc->new_bounding_box[2] = bounding_box[2];
1482 if (bounding_box[3] > doc->new_bounding_box[3])
1483 doc->new_bounding_box[3] = bounding_box[3];
1484 }
1485
1486 /*
1487 * Output the page header as needed...
1488 */
1489
1490 if (!doc->slow_order && first_page)
1491 {
1492 if (!ppd || !ppd->num_filters)
1493 fprintf(stderr, "PAGE: %d %d\n", doc->page,
1494 doc->slow_collate ? 1 : doc->copies);
1495
1496 if (doc->number_up > 1)
1497 {
1498 printf("%%%%Page: (%d) %d\n", doc->page, doc->page);
1499 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1500 PageLeft, PageBottom, PageRight, PageTop);
1501 }
1502 else
1503 {
1504 printf("%%%%Page: %s %d\n", pageinfo->label, doc->page);
1505 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1506 pageinfo->bounding_box[0], pageinfo->bounding_box[1],
1507 pageinfo->bounding_box[2], pageinfo->bounding_box[3]);
1508 }
1509 }
1510
1511 /*
1512 * Copy any page setup commands...
1513 */
1514
1515 if (first_page)
1516 doc_puts(doc, "%%BeginPageSetup\n");
1517
1518 if (has_page_setup)
1519 {
1520 int feature = 0; /* In a Begin/EndFeature block? */
1521
1522 while ((linelen = (ssize_t)cupsFileGetLine(fp, line, linesize)) > 0)
1523 {
1524 if (!strncmp(line, "%%EndPageSetup", 14))
1525 break;
1526 else if (!strncmp(line, "%%BeginFeature:", 15))
1527 {
1528 feature = 1;
1529
1530 if (doc->number_up > 1 || doc->fit_to_page)
1531 continue;
1532 }
1533 else if (!strncmp(line, "%%EndFeature", 12))
1534 {
1535 feature = 0;
1536
1537 if (doc->number_up > 1 || doc->fit_to_page)
1538 continue;
1539 }
1540 else if (!strncmp(line, "%%IncludeFeature:", 17))
1541 {
1542 pageinfo->num_options = include_feature(ppd, line,
1543 pageinfo->num_options,
1544 &(pageinfo->options));
1545 continue;
1546 }
1547 else if (!strncmp(line, "%%Include", 9))
1548 continue;
1549
1550 if (line[0] != '%' && !feature)
1551 break;
1552
1553 if (!feature || (doc->number_up == 1 && !doc->fit_to_page))
1554 doc_write(doc, line, (size_t)linelen);
1555 }
1556
1557 /*
1558 * Skip %%EndPageSetup...
1559 */
1560
1561 if (linelen > 0 && !strncmp(line, "%%EndPageSetup", 14))
1562 linelen = (ssize_t)cupsFileGetLine(fp, line, linesize);
1563 }
1564
1565 if (first_page)
1566 {
1567 char *page_setup; /* PageSetup commands to send */
1568
1569
1570 if (pageinfo->num_options > 0)
1571 write_options(doc, ppd, pageinfo->num_options, pageinfo->options);
1572
1573 /*
1574 * Output commands for the current page...
1575 */
1576
1577 page_setup = ppdEmitString(ppd, PPD_ORDER_PAGE, 0);
1578
1579 if (page_setup)
1580 {
1581 doc_puts(doc, page_setup);
1582 free(page_setup);
1583 }
1584 }
1585
1586 /*
1587 * Prep for the start of the page description...
1588 */
1589
1590 start_nup(doc, number, 1, bounding_box);
1591
1592 if (first_page)
1593 doc_puts(doc, "%%EndPageSetup\n");
1594
1595 /*
1596 * Read the rest of the page description...
1597 */
1598
1599 level = 0;
1600
1601 do
1602 {
1603 if (level == 0 &&
1604 (!strncmp(line, "%%Page:", 7) ||
1605 !strncmp(line, "%%Trailer", 9) ||
1606 !strncmp(line, "%%EOF", 5)))
1607 break;
1608 else if (!strncmp(line, "%%BeginDocument", 15) ||
1609 !strncmp(line, "%ADO_BeginApplication", 21))
1610 {
1611 doc_write(doc, line, (size_t)linelen);
1612
1613 level ++;
1614 }
1615 else if ((!strncmp(line, "%%EndDocument", 13) ||
1616 !strncmp(line, "%ADO_EndApplication", 19)) && level > 0)
1617 {
1618 doc_write(doc, line, (size_t)linelen);
1619
1620 level --;
1621 }
1622 else if (!strncmp(line, "%%BeginBinary:", 14) ||
1623 (!strncmp(line, "%%BeginData:", 12) &&
1624 !strstr(line, "ASCII") && !strstr(line, "Hex")))
1625 {
1626 /*
1627 * Copy binary data...
1628 */
1629
1630 int bytes; /* Bytes of data */
1631
1632
1633 doc_write(doc, line, (size_t)linelen);
1634
1635 bytes = atoi(strchr(line, ':') + 1);
1636
1637 while (bytes > 0)
1638 {
1639 if ((size_t)bytes > linesize)
1640 linelen = cupsFileRead(fp, line, linesize);
1641 else
1642 linelen = cupsFileRead(fp, line, (size_t)bytes);
1643
1644 if (linelen < 1)
1645 {
1646 line[0] = '\0';
1647 perror("ERROR: Early end-of-file while reading binary data");
1648 return (0);
1649 }
1650
1651 doc_write(doc, line, (size_t)linelen);
1652
1653 bytes -= linelen;
1654 }
1655 }
1656 else
1657 doc_write(doc, line, (size_t)linelen);
1658 }
1659 while ((linelen = (ssize_t)cupsFileGetLine(fp, line, linesize)) > 0);
1660
1661 /*
1662 * Finish up this page and return...
1663 */
1664
1665 end_nup(doc, number);
1666
1667 pageinfo->length = (ssize_t)(cupsFileTell(doc->temp) - pageinfo->offset);
1668
1669 return (linelen);
1670 }
1671
1672
1673 /*
1674 * 'copy_prolog()' - Copy the document prolog section.
1675 *
1676 * This function expects "line" to be filled with a %%BeginProlog comment line.
1677 * On return, "line" will contain the next line in the file, if any.
1678 */
1679
1680 static ssize_t /* O - Length of next line */
1681 copy_prolog(cups_file_t *fp, /* I - File to read from */
1682 pstops_doc_t *doc, /* I - Document info */
1683 ppd_file_t *ppd, /* I - PPD file */
1684 char *line, /* I - Line buffer */
1685 ssize_t linelen, /* I - Length of initial line */
1686 size_t linesize) /* I - Size of line buffer */
1687 {
1688 while (strncmp(line, "%%BeginProlog", 13))
1689 {
1690 if (!strncmp(line, "%%BeginSetup", 12) || !strncmp(line, "%%Page:", 7))
1691 break;
1692
1693 doc_write(doc, line, (size_t)linelen);
1694
1695 if ((linelen = (ssize_t)cupsFileGetLine(fp, line, linesize)) == 0)
1696 break;
1697 }
1698
1699 doc_puts(doc, "%%BeginProlog\n");
1700
1701 do_prolog(doc, ppd);
1702
1703 if (!strncmp(line, "%%BeginProlog", 13))
1704 {
1705 while ((linelen = (ssize_t)cupsFileGetLine(fp, line, linesize)) > 0)
1706 {
1707 if (!strncmp(line, "%%EndProlog", 11) ||
1708 !strncmp(line, "%%BeginSetup", 12) ||
1709 !strncmp(line, "%%Page:", 7))
1710 break;
1711
1712 doc_write(doc, line, (size_t)linelen);
1713 }
1714
1715 if (!strncmp(line, "%%EndProlog", 11))
1716 linelen = (ssize_t)cupsFileGetLine(fp, line, linesize);
1717 else
1718 fputs("DEBUG: The %%EndProlog comment is missing.\n", stderr);
1719 }
1720
1721 doc_puts(doc, "%%EndProlog\n");
1722
1723 return (linelen);
1724 }
1725
1726
1727 /*
1728 * 'copy_setup()' - Copy the document setup section.
1729 *
1730 * This function expects "line" to be filled with a %%BeginSetup comment line.
1731 * On return, "line" will contain the next line in the file, if any.
1732 */
1733
1734 static ssize_t /* O - Length of next line */
1735 copy_setup(cups_file_t *fp, /* I - File to read from */
1736 pstops_doc_t *doc, /* I - Document info */
1737 ppd_file_t *ppd, /* I - PPD file */
1738 char *line, /* I - Line buffer */
1739 ssize_t linelen, /* I - Length of initial line */
1740 size_t linesize) /* I - Size of line buffer */
1741 {
1742 int num_options; /* Number of options */
1743 cups_option_t *options; /* Options */
1744
1745
1746 while (strncmp(line, "%%BeginSetup", 12))
1747 {
1748 if (!strncmp(line, "%%Page:", 7))
1749 break;
1750
1751 doc_write(doc, line, (size_t)linelen);
1752
1753 if ((linelen = (ssize_t)cupsFileGetLine(fp, line, linesize)) == 0)
1754 break;
1755 }
1756
1757 doc_puts(doc, "%%BeginSetup\n");
1758
1759 do_setup(doc, ppd);
1760
1761 num_options = 0;
1762 options = NULL;
1763
1764 if (!strncmp(line, "%%BeginSetup", 12))
1765 {
1766 while (strncmp(line, "%%EndSetup", 10))
1767 {
1768 if (!strncmp(line, "%%Page:", 7))
1769 break;
1770 else if (!strncmp(line, "%%IncludeFeature:", 17))
1771 {
1772 /*
1773 * %%IncludeFeature: *MainKeyword OptionKeyword
1774 */
1775
1776 if (doc->number_up == 1 && !doc->fit_to_page)
1777 num_options = include_feature(ppd, line, num_options, &options);
1778 }
1779 else if (strncmp(line, "%%BeginSetup", 12))
1780 doc_write(doc, line, (size_t)linelen);
1781
1782 if ((linelen = (ssize_t)cupsFileGetLine(fp, line, linesize)) == 0)
1783 break;
1784 }
1785
1786 if (!strncmp(line, "%%EndSetup", 10))
1787 linelen = (ssize_t)cupsFileGetLine(fp, line, linesize);
1788 else
1789 fputs("DEBUG: The %%EndSetup comment is missing.\n", stderr);
1790 }
1791
1792 if (num_options > 0)
1793 {
1794 write_options(doc, ppd, num_options, options);
1795 cupsFreeOptions(num_options, options);
1796 }
1797
1798 doc_puts(doc, "%%EndSetup\n");
1799
1800 return (linelen);
1801 }
1802
1803
1804 /*
1805 * 'copy_trailer()' - Copy the document trailer.
1806 *
1807 * This function expects "line" to be filled with a %%Trailer comment line.
1808 * On return, "line" will contain the next line in the file, if any.
1809 */
1810
1811 static ssize_t /* O - Length of next line */
1812 copy_trailer(cups_file_t *fp, /* I - File to read from */
1813 pstops_doc_t *doc, /* I - Document info */
1814 ppd_file_t *ppd, /* I - PPD file */
1815 int number, /* I - Number of pages */
1816 char *line, /* I - Line buffer */
1817 ssize_t linelen, /* I - Length of initial line */
1818 size_t linesize) /* I - Size of line buffer */
1819 {
1820 /*
1821 * Write the trailer comments...
1822 */
1823
1824 (void)ppd;
1825
1826 puts("%%Trailer");
1827
1828 while (linelen > 0)
1829 {
1830 if (!strncmp(line, "%%EOF", 5))
1831 break;
1832 else if (strncmp(line, "%%Trailer", 9) &&
1833 strncmp(line, "%%Pages:", 8) &&
1834 strncmp(line, "%%BoundingBox:", 14))
1835 fwrite(line, 1, (size_t)linelen, stdout);
1836
1837 linelen = (ssize_t)cupsFileGetLine(fp, line, linesize);
1838 }
1839
1840 fprintf(stderr, "DEBUG: Wrote %d pages...\n", number);
1841
1842 printf("%%%%Pages: %d\n", number);
1843 if (doc->number_up > 1 || doc->fit_to_page)
1844 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
1845 PageLeft, PageBottom, PageRight, PageTop);
1846 else
1847 printf("%%%%BoundingBox: %d %d %d %d\n",
1848 doc->new_bounding_box[0], doc->new_bounding_box[1],
1849 doc->new_bounding_box[2], doc->new_bounding_box[3]);
1850
1851 return (linelen);
1852 }
1853
1854
1855 /*
1856 * 'do_prolog()' - Send the necessary document prolog commands.
1857 */
1858
1859 static void
1860 do_prolog(pstops_doc_t *doc, /* I - Document information */
1861 ppd_file_t *ppd) /* I - PPD file */
1862 {
1863 char *ps; /* PS commands */
1864
1865
1866 /*
1867 * Send the document prolog commands...
1868 */
1869
1870 if (ppd && ppd->patches)
1871 {
1872 doc_puts(doc, "%%BeginFeature: *JobPatchFile 1\n");
1873 doc_puts(doc, ppd->patches);
1874 doc_puts(doc, "\n%%EndFeature\n");
1875 }
1876
1877 if ((ps = ppdEmitString(ppd, PPD_ORDER_PROLOG, 0.0)) != NULL)
1878 {
1879 doc_puts(doc, ps);
1880 free(ps);
1881 }
1882
1883 /*
1884 * Define ESPshowpage here so that applications that define their
1885 * own procedure to do a showpage pick it up...
1886 */
1887
1888 if (doc->use_ESPshowpage)
1889 doc_puts(doc, "userdict/ESPshowpage/showpage load put\n"
1890 "userdict/showpage{}put\n");
1891 }
1892
1893
1894 /*
1895 * 'do_setup()' - Send the necessary document setup commands.
1896 */
1897
1898 static void
1899 do_setup(pstops_doc_t *doc, /* I - Document information */
1900 ppd_file_t *ppd) /* I - PPD file */
1901 {
1902 char *ps; /* PS commands */
1903
1904
1905 /*
1906 * Disable CTRL-D so that embedded files don't cause printing
1907 * errors...
1908 */
1909
1910 doc_puts(doc, "% Disable CTRL-D as an end-of-file marker...\n");
1911 doc_puts(doc, "userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put\n");
1912
1913 /*
1914 * Mark job options...
1915 */
1916
1917 cupsMarkOptions(ppd, doc->num_options, doc->options);
1918
1919 /*
1920 * Send all the printer-specific setup commands...
1921 */
1922
1923 if ((ps = ppdEmitString(ppd, PPD_ORDER_DOCUMENT, 0.0)) != NULL)
1924 {
1925 doc_puts(doc, ps);
1926 free(ps);
1927 }
1928
1929 if ((ps = ppdEmitString(ppd, PPD_ORDER_ANY, 0.0)) != NULL)
1930 {
1931 doc_puts(doc, ps);
1932 free(ps);
1933 }
1934
1935 /*
1936 * Set the number of copies for the job...
1937 */
1938
1939 if (doc->copies != 1 && (!doc->collate || !doc->slow_collate))
1940 {
1941 doc_printf(doc, "%%RBIBeginNonPPDFeature: *NumCopies %d\n", doc->copies);
1942 doc_printf(doc,
1943 "%d/languagelevel where{pop languagelevel 2 ge}{false}ifelse\n"
1944 "{1 dict begin/NumCopies exch def currentdict end "
1945 "setpagedevice}\n"
1946 "{userdict/#copies 3 -1 roll put}ifelse\n", doc->copies);
1947 doc_puts(doc, "%RBIEndNonPPDFeature\n");
1948 }
1949
1950 /*
1951 * If we are doing N-up printing, disable setpagedevice...
1952 */
1953
1954 if (doc->number_up > 1)
1955 {
1956 doc_puts(doc, "userdict/CUPSsetpagedevice/setpagedevice load put\n");
1957 doc_puts(doc, "userdict/setpagedevice{pop}bind put\n");
1958 }
1959
1960 /*
1961 * Make sure we have rectclip and rectstroke procedures of some sort...
1962 */
1963
1964 doc_puts(doc,
1965 "% x y w h ESPrc - Clip to a rectangle.\n"
1966 "userdict/ESPrc/rectclip where{pop/rectclip load}\n"
1967 "{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1968 "neg 0 rlineto closepath clip newpath}bind}ifelse put\n");
1969
1970 doc_puts(doc,
1971 "% x y w h ESPrf - Fill a rectangle.\n"
1972 "userdict/ESPrf/rectfill where{pop/rectfill load}\n"
1973 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1974 "neg 0 rlineto closepath fill grestore}bind}ifelse put\n");
1975
1976 doc_puts(doc,
1977 "% x y w h ESPrs - Stroke a rectangle.\n"
1978 "userdict/ESPrs/rectstroke where{pop/rectstroke load}\n"
1979 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1980 "neg 0 rlineto closepath stroke grestore}bind}ifelse put\n");
1981
1982 /*
1983 * Write the page and label prologs...
1984 */
1985
1986 if (doc->number_up == 2 || doc->number_up == 6)
1987 {
1988 /*
1989 * For 2- and 6-up output, rotate the labels to match the orientation
1990 * of the pages...
1991 */
1992
1993 if (Orientation & 1)
1994 write_label_prolog(doc, doc->page_label, PageBottom,
1995 PageWidth - PageLength + PageTop, PageLength);
1996 else
1997 write_label_prolog(doc, doc->page_label, PageLeft, PageRight,
1998 PageLength);
1999 }
2000 else
2001 write_label_prolog(doc, doc->page_label, PageBottom, PageTop, PageWidth);
2002 }
2003
2004
2005 /*
2006 * 'doc_printf()' - Send a formatted string to stdout and/or the temp file.
2007 *
2008 * This function should be used for all page-level output that is affected
2009 * by ordering, collation, etc.
2010 */
2011
2012 static void
2013 doc_printf(pstops_doc_t *doc, /* I - Document information */
2014 const char *format, /* I - Printf-style format string */
2015 ...) /* I - Additional arguments as needed */
2016 {
2017 va_list ap; /* Pointer to arguments */
2018 char buffer[1024]; /* Output buffer */
2019 ssize_t bytes; /* Number of bytes to write */
2020
2021
2022 va_start(ap, format);
2023 bytes = vsnprintf(buffer, sizeof(buffer), format, ap);
2024 va_end(ap);
2025
2026 if ((size_t)bytes > sizeof(buffer))
2027 {
2028 _cupsLangPrintFilter(stderr, "ERROR",
2029 _("Buffer overflow detected, aborting."));
2030 exit(1);
2031 }
2032
2033 doc_write(doc, buffer, (size_t)bytes);
2034 }
2035
2036
2037 /*
2038 * 'doc_puts()' - Send a nul-terminated string to stdout and/or the temp file.
2039 *
2040 * This function should be used for all page-level output that is affected
2041 * by ordering, collation, etc.
2042 */
2043
2044 static void
2045 doc_puts(pstops_doc_t *doc, /* I - Document information */
2046 const char *s) /* I - String to send */
2047 {
2048 doc_write(doc, s, strlen(s));
2049 }
2050
2051
2052 /*
2053 * 'doc_write()' - Send data to stdout and/or the temp file.
2054 */
2055
2056 static void
2057 doc_write(pstops_doc_t *doc, /* I - Document information */
2058 const char *s, /* I - Data to send */
2059 size_t len) /* I - Number of bytes to send */
2060 {
2061 if (!doc->slow_order)
2062 fwrite(s, 1, len, stdout);
2063
2064 if (doc->temp)
2065 cupsFileWrite(doc->temp, s, len);
2066 }
2067
2068
2069 /*
2070 * 'end_nup()' - End processing for N-up printing.
2071 */
2072
2073 static void
2074 end_nup(pstops_doc_t *doc, /* I - Document information */
2075 int number) /* I - Page number */
2076 {
2077 if (doc->number_up > 1)
2078 doc_puts(doc, "userdict/ESPsave get restore\n");
2079
2080 switch (doc->number_up)
2081 {
2082 case 1 :
2083 if (doc->use_ESPshowpage)
2084 {
2085 write_labels(doc, Orientation);
2086 doc_puts(doc, "ESPshowpage\n");
2087 }
2088 break;
2089
2090 case 2 :
2091 case 6 :
2092 if (is_last_page(number) && doc->use_ESPshowpage)
2093 {
2094 if (Orientation & 1)
2095 {
2096 /*
2097 * Rotate the labels back to portrait...
2098 */
2099
2100 write_labels(doc, Orientation - 1);
2101 }
2102 else if (Orientation == 0)
2103 {
2104 /*
2105 * Rotate the labels to landscape...
2106 */
2107
2108 write_labels(doc, doc->normal_landscape ? 1 : 3);
2109 }
2110 else
2111 {
2112 /*
2113 * Rotate the labels to landscape...
2114 */
2115
2116 write_labels(doc, doc->normal_landscape ? 3 : 1);
2117 }
2118
2119 doc_puts(doc, "ESPshowpage\n");
2120 }
2121 break;
2122
2123 default :
2124 if (is_last_page(number) && doc->use_ESPshowpage)
2125 {
2126 write_labels(doc, Orientation);
2127 doc_puts(doc, "ESPshowpage\n");
2128 }
2129 break;
2130 }
2131
2132 fflush(stdout);
2133 }
2134
2135
2136 /*
2137 * 'include_feature()' - Include a printer option/feature command.
2138 */
2139
2140 static int /* O - New number of options */
2141 include_feature(
2142 ppd_file_t *ppd, /* I - PPD file */
2143 const char *line, /* I - DSC line */
2144 int num_options, /* I - Number of options */
2145 cups_option_t **options) /* IO - Options */
2146 {
2147 char name[255], /* Option name */
2148 value[255]; /* Option value */
2149 ppd_option_t *option; /* Option in file */
2150
2151
2152 /*
2153 * Get the "%%IncludeFeature: *Keyword OptionKeyword" values...
2154 */
2155
2156 if (sscanf(line + 17, "%254s%254s", name, value) != 2)
2157 {
2158 fputs("DEBUG: The %%IncludeFeature: comment is not valid.\n", stderr);
2159 return (num_options);
2160 }
2161
2162 /*
2163 * Find the option and choice...
2164 */
2165
2166 if ((option = ppdFindOption(ppd, name + 1)) == NULL)
2167 {
2168 _cupsLangPrintFilter(stderr, "WARNING", _("Unknown option \"%s\"."),
2169 name + 1);
2170 return (num_options);
2171 }
2172
2173 if (option->section == PPD_ORDER_EXIT ||
2174 option->section == PPD_ORDER_JCL)
2175 {
2176 _cupsLangPrintFilter(stderr, "WARNING",
2177 _("Option \"%s\" cannot be included via "
2178 "%%%%IncludeFeature."), name + 1);
2179 return (num_options);
2180 }
2181
2182 if (!ppdFindChoice(option, value))
2183 {
2184 _cupsLangPrintFilter(stderr, "WARNING",
2185 _("Unknown choice \"%s\" for option \"%s\"."),
2186 value, name + 1);
2187 return (num_options);
2188 }
2189
2190 /*
2191 * Add the option to the option array and return...
2192 */
2193
2194 return (cupsAddOption(name + 1, value, num_options, options));
2195 }
2196
2197
2198 /*
2199 * 'parse_text()' - Parse a text value in a comment.
2200 *
2201 * This function parses a DSC text value as defined on page 36 of the
2202 * DSC specification. Text values are either surrounded by parenthesis
2203 * or whitespace-delimited.
2204 *
2205 * The value returned is the literal characters for the entire text
2206 * string, including any parenthesis and escape characters.
2207 */
2208
2209 static char * /* O - Value or NULL on error */
2210 parse_text(const char *start, /* I - Start of text value */
2211 char **end, /* O - End of text value */
2212 char *buffer, /* I - Buffer */
2213 size_t bufsize) /* I - Size of buffer */
2214 {
2215 char *bufptr, /* Pointer in buffer */
2216 *bufend; /* End of buffer */
2217 int level; /* Parenthesis level */
2218
2219
2220 /*
2221 * Skip leading whitespace...
2222 */
2223
2224 while (isspace(*start & 255))
2225 start ++;
2226
2227 /*
2228 * Then copy the value...
2229 */
2230
2231 level = 0;
2232 bufptr = buffer;
2233 bufend = buffer + bufsize - 1;
2234
2235 while (bufptr < bufend)
2236 {
2237 if (isspace(*start & 255) && !level)
2238 break;
2239
2240 *bufptr++ = *start;
2241
2242 if (*start == '(')
2243 level ++;
2244 else if (*start == ')')
2245 {
2246 if (!level)
2247 {
2248 start ++;
2249 break;
2250 }
2251 else
2252 level --;
2253 }
2254 else if (*start == '\\')
2255 {
2256 /*
2257 * Copy escaped character...
2258 */
2259
2260 int i; /* Looping var */
2261
2262
2263 for (i = 1;
2264 i <= 3 && isdigit(start[i] & 255) && bufptr < bufend;
2265 *bufptr++ = start[i], i ++);
2266 }
2267
2268 start ++;
2269 }
2270
2271 *bufptr = '\0';
2272
2273 /*
2274 * Return the value and new pointer into the line...
2275 */
2276
2277 if (end)
2278 *end = (char *)start;
2279
2280 if (bufptr == bufend)
2281 return (NULL);
2282 else
2283 return (buffer);
2284 }
2285
2286
2287 /*
2288 * 'set_pstops_options()' - Set pstops options.
2289 */
2290
2291 static void
2292 set_pstops_options(
2293 pstops_doc_t *doc, /* I - Document information */
2294 ppd_file_t *ppd, /* I - PPD file */
2295 char *argv[], /* I - Command-line arguments */
2296 int num_options, /* I - Number of options */
2297 cups_option_t *options) /* I - Options */
2298 {
2299 const char *val; /* Option value */
2300 int intval; /* Integer option value */
2301 ppd_attr_t *attr; /* PPD attribute */
2302 ppd_option_t *option; /* PPD option */
2303 ppd_choice_t *choice; /* PPD choice */
2304 const char *content_type; /* Original content type */
2305 int max_copies; /* Maximum number of copies supported */
2306
2307
2308 /*
2309 * Initialize document information structure...
2310 */
2311
2312 memset(doc, 0, sizeof(pstops_doc_t));
2313
2314 doc->job_id = atoi(argv[1]);
2315 doc->user = argv[2];
2316 doc->title = argv[3];
2317 doc->copies = atoi(argv[4]);
2318
2319 if (ppd && ppd->landscape > 0)
2320 doc->normal_landscape = 1;
2321
2322 doc->bounding_box[0] = (int)PageLeft;
2323 doc->bounding_box[1] = (int)PageBottom;
2324 doc->bounding_box[2] = (int)PageRight;
2325 doc->bounding_box[3] = (int)PageTop;
2326
2327 doc->new_bounding_box[0] = INT_MAX;
2328 doc->new_bounding_box[1] = INT_MAX;
2329 doc->new_bounding_box[2] = INT_MIN;
2330 doc->new_bounding_box[3] = INT_MIN;
2331
2332 /*
2333 * AP_FIRSTPAGE_* and the corresponding non-first-page options.
2334 */
2335
2336 doc->ap_input_slot = cupsGetOption("AP_FIRSTPAGE_InputSlot", num_options,
2337 options);
2338 doc->ap_manual_feed = cupsGetOption("AP_FIRSTPAGE_ManualFeed", num_options,
2339 options);
2340 doc->ap_media_color = cupsGetOption("AP_FIRSTPAGE_MediaColor", num_options,
2341 options);
2342 doc->ap_media_type = cupsGetOption("AP_FIRSTPAGE_MediaType", num_options,
2343 options);
2344 doc->ap_page_region = cupsGetOption("AP_FIRSTPAGE_PageRegion", num_options,
2345 options);
2346 doc->ap_page_size = cupsGetOption("AP_FIRSTPAGE_PageSize", num_options,
2347 options);
2348
2349 if ((choice = ppdFindMarkedChoice(ppd, "InputSlot")) != NULL)
2350 doc->input_slot = choice->choice;
2351 if ((choice = ppdFindMarkedChoice(ppd, "ManualFeed")) != NULL)
2352 doc->manual_feed = choice->choice;
2353 if ((choice = ppdFindMarkedChoice(ppd, "MediaColor")) != NULL)
2354 doc->media_color = choice->choice;
2355 if ((choice = ppdFindMarkedChoice(ppd, "MediaType")) != NULL)
2356 doc->media_type = choice->choice;
2357 if ((choice = ppdFindMarkedChoice(ppd, "PageRegion")) != NULL)
2358 doc->page_region = choice->choice;
2359 if ((choice = ppdFindMarkedChoice(ppd, "PageSize")) != NULL)
2360 doc->page_size = choice->choice;
2361
2362 /*
2363 * collate, multiple-document-handling
2364 */
2365
2366 if ((val = cupsGetOption("multiple-document-handling", num_options, options)) != NULL)
2367 {
2368 /*
2369 * This IPP attribute is unnecessarily complicated...
2370 *
2371 * single-document, separate-documents-collated-copies, and
2372 * single-document-new-sheet all require collated copies.
2373 *
2374 * separate-documents-uncollated-copies allows for uncollated copies.
2375 */
2376
2377 doc->collate = _cups_strcasecmp(val, "separate-documents-uncollated-copies") != 0;
2378 }
2379
2380 if ((val = cupsGetOption("Collate", num_options, options)) != NULL &&
2381 (!_cups_strcasecmp(val, "true") ||!_cups_strcasecmp(val, "on") ||
2382 !_cups_strcasecmp(val, "yes")))
2383 doc->collate = 1;
2384
2385 /*
2386 * emit-jcl
2387 */
2388
2389 if ((val = cupsGetOption("emit-jcl", num_options, options)) != NULL &&
2390 (!_cups_strcasecmp(val, "false") || !_cups_strcasecmp(val, "off") ||
2391 !_cups_strcasecmp(val, "no") || !strcmp(val, "0")))
2392 doc->emit_jcl = 0;
2393 else
2394 doc->emit_jcl = 1;
2395
2396 /*
2397 * fit-to-page/ipp-attribute-fidelity
2398 *
2399 * (Only for original PostScript content)
2400 */
2401
2402 if ((content_type = getenv("CONTENT_TYPE")) == NULL)
2403 content_type = "application/postscript";
2404
2405 if (!_cups_strcasecmp(content_type, "application/postscript"))
2406 {
2407 if ((val = cupsGetOption("fit-to-page", num_options, options)) != NULL &&
2408 !_cups_strcasecmp(val, "true"))
2409 doc->fit_to_page = 1;
2410 else if ((val = cupsGetOption("ipp-attribute-fidelity", num_options,
2411 options)) != NULL &&
2412 !_cups_strcasecmp(val, "true"))
2413 doc->fit_to_page = 1;
2414 }
2415
2416 /*
2417 * mirror/MirrorPrint
2418 */
2419
2420 if ((choice = ppdFindMarkedChoice(ppd, "MirrorPrint")) != NULL)
2421 {
2422 val = choice->choice;
2423 choice->marked = 0;
2424 }
2425 else
2426 val = cupsGetOption("mirror", num_options, options);
2427
2428 if (val && (!_cups_strcasecmp(val, "true") || !_cups_strcasecmp(val, "on") ||
2429 !_cups_strcasecmp(val, "yes")))
2430 doc->mirror = 1;
2431
2432 /*
2433 * number-up
2434 */
2435
2436 if ((val = cupsGetOption("number-up", num_options, options)) != NULL)
2437 {
2438 switch (intval = atoi(val))
2439 {
2440 case 1 :
2441 case 2 :
2442 case 4 :
2443 case 6 :
2444 case 9 :
2445 case 16 :
2446 doc->number_up = intval;
2447 break;
2448 default :
2449 _cupsLangPrintFilter(stderr, "ERROR",
2450 _("Unsupported number-up value %d, using "
2451 "number-up=1."), intval);
2452 doc->number_up = 1;
2453 break;
2454 }
2455 }
2456 else
2457 doc->number_up = 1;
2458
2459 /*
2460 * number-up-layout
2461 */
2462
2463 if ((val = cupsGetOption("number-up-layout", num_options, options)) != NULL)
2464 {
2465 if (!_cups_strcasecmp(val, "lrtb"))
2466 doc->number_up_layout = PSTOPS_LAYOUT_LRTB;
2467 else if (!_cups_strcasecmp(val, "lrbt"))
2468 doc->number_up_layout = PSTOPS_LAYOUT_LRBT;
2469 else if (!_cups_strcasecmp(val, "rltb"))
2470 doc->number_up_layout = PSTOPS_LAYOUT_RLTB;
2471 else if (!_cups_strcasecmp(val, "rlbt"))
2472 doc->number_up_layout = PSTOPS_LAYOUT_RLBT;
2473 else if (!_cups_strcasecmp(val, "tblr"))
2474 doc->number_up_layout = PSTOPS_LAYOUT_TBLR;
2475 else if (!_cups_strcasecmp(val, "tbrl"))
2476 doc->number_up_layout = PSTOPS_LAYOUT_TBRL;
2477 else if (!_cups_strcasecmp(val, "btlr"))
2478 doc->number_up_layout = PSTOPS_LAYOUT_BTLR;
2479 else if (!_cups_strcasecmp(val, "btrl"))
2480 doc->number_up_layout = PSTOPS_LAYOUT_BTRL;
2481 else
2482 {
2483 _cupsLangPrintFilter(stderr, "ERROR",
2484 _("Unsupported number-up-layout value %s, using "
2485 "number-up-layout=lrtb."), val);
2486 doc->number_up_layout = PSTOPS_LAYOUT_LRTB;
2487 }
2488 }
2489 else
2490 doc->number_up_layout = PSTOPS_LAYOUT_LRTB;
2491
2492 /*
2493 * OutputOrder
2494 */
2495
2496 if ((val = cupsGetOption("OutputOrder", num_options, options)) != NULL)
2497 {
2498 if (!_cups_strcasecmp(val, "Reverse"))
2499 doc->output_order = 1;
2500 }
2501 else if (ppd)
2502 {
2503 /*
2504 * Figure out the right default output order from the PPD file...
2505 */
2506
2507 if ((choice = ppdFindMarkedChoice(ppd, "OutputBin")) != NULL &&
2508 (attr = ppdFindAttr(ppd, "PageStackOrder", choice->choice)) != NULL &&
2509 attr->value)
2510 doc->output_order = !_cups_strcasecmp(attr->value, "Reverse");
2511 else if ((attr = ppdFindAttr(ppd, "DefaultOutputOrder", NULL)) != NULL &&
2512 attr->value)
2513 doc->output_order = !_cups_strcasecmp(attr->value, "Reverse");
2514 }
2515
2516 /*
2517 * page-border
2518 */
2519
2520 if ((val = cupsGetOption("page-border", num_options, options)) != NULL)
2521 {
2522 if (!_cups_strcasecmp(val, "none"))
2523 doc->page_border = PSTOPS_BORDERNONE;
2524 else if (!_cups_strcasecmp(val, "single"))
2525 doc->page_border = PSTOPS_BORDERSINGLE;
2526 else if (!_cups_strcasecmp(val, "single-thick"))
2527 doc->page_border = PSTOPS_BORDERSINGLE2;
2528 else if (!_cups_strcasecmp(val, "double"))
2529 doc->page_border = PSTOPS_BORDERDOUBLE;
2530 else if (!_cups_strcasecmp(val, "double-thick"))
2531 doc->page_border = PSTOPS_BORDERDOUBLE2;
2532 else
2533 {
2534 _cupsLangPrintFilter(stderr, "ERROR",
2535 _("Unsupported page-border value %s, using "
2536 "page-border=none."), val);
2537 doc->page_border = PSTOPS_BORDERNONE;
2538 }
2539 }
2540 else
2541 doc->page_border = PSTOPS_BORDERNONE;
2542
2543 /*
2544 * page-label
2545 */
2546
2547 doc->page_label = cupsGetOption("page-label", num_options, options);
2548
2549 /*
2550 * page-ranges
2551 */
2552
2553 doc->page_ranges = cupsGetOption("page-ranges", num_options, options);
2554
2555 /*
2556 * page-set
2557 */
2558
2559 doc->page_set = cupsGetOption("page-set", num_options, options);
2560
2561 /*
2562 * Now figure out if we have to force collated copies, etc.
2563 */
2564
2565 if ((attr = ppdFindAttr(ppd, "cupsMaxCopies", NULL)) != NULL)
2566 max_copies = atoi(attr->value);
2567 else if (ppd && ppd->manual_copies)
2568 max_copies = 1;
2569 else
2570 max_copies = 9999;
2571
2572 if (doc->copies > max_copies)
2573 doc->collate = 1;
2574 else if (ppd && ppd->manual_copies && Duplex && doc->copies > 1)
2575 {
2576 /*
2577 * Force collated copies when printing a duplexed document to
2578 * a non-PS printer that doesn't do hardware copy generation.
2579 * Otherwise the copies will end up on the front/back side of
2580 * each page.
2581 */
2582
2583 doc->collate = 1;
2584 }
2585
2586 /*
2587 * See if we have to filter the fast or slow way...
2588 */
2589
2590 if (doc->collate && doc->copies > 1)
2591 {
2592 /*
2593 * See if we need to manually collate the pages...
2594 */
2595
2596 doc->slow_collate = 1;
2597
2598 if (doc->copies <= max_copies &&
2599 (choice = ppdFindMarkedChoice(ppd, "Collate")) != NULL &&
2600 !_cups_strcasecmp(choice->choice, "True"))
2601 {
2602 /*
2603 * Hardware collate option is selected, see if the option is
2604 * conflicting - if not, collate in hardware. Otherwise,
2605 * turn the hardware collate option off...
2606 */
2607
2608 if ((option = ppdFindOption(ppd, "Collate")) != NULL &&
2609 !option->conflicted)
2610 doc->slow_collate = 0;
2611 else
2612 ppdMarkOption(ppd, "Collate", "False");
2613 }
2614 }
2615 else
2616 doc->slow_collate = 0;
2617
2618 if (!ppdFindOption(ppd, "OutputOrder") && doc->output_order)
2619 doc->slow_order = 1;
2620 else
2621 doc->slow_order = 0;
2622
2623 if (Duplex &&
2624 (doc->slow_collate || doc->slow_order ||
2625 ((attr = ppdFindAttr(ppd, "cupsEvenDuplex", NULL)) != NULL &&
2626 attr->value && !_cups_strcasecmp(attr->value, "true"))))
2627 doc->slow_duplex = 1;
2628 else
2629 doc->slow_duplex = 0;
2630
2631 /*
2632 * Create a temporary file for page data if we need to filter slowly...
2633 */
2634
2635 if (doc->slow_order || doc->slow_collate)
2636 {
2637 if ((doc->temp = cupsTempFile2(doc->tempfile,
2638 sizeof(doc->tempfile))) == NULL)
2639 {
2640 perror("DEBUG: Unable to create temporary file");
2641 exit(1);
2642 }
2643 }
2644
2645 /*
2646 * Figure out if we should use ESPshowpage or not...
2647 */
2648
2649 if (doc->page_label || getenv("CLASSIFICATION") || doc->number_up > 1 ||
2650 doc->page_border)
2651 {
2652 /*
2653 * Yes, use ESPshowpage...
2654 */
2655
2656 doc->use_ESPshowpage = 1;
2657 }
2658
2659 fprintf(stderr, "DEBUG: slow_collate=%d, slow_duplex=%d, slow_order=%d\n",
2660 doc->slow_collate, doc->slow_duplex, doc->slow_order);
2661 }
2662
2663
2664 /*
2665 * 'skip_page()' - Skip past a page that won't be printed.
2666 */
2667
2668 static ssize_t /* O - Length of next line */
2669 skip_page(cups_file_t *fp, /* I - File to read from */
2670 char *line, /* I - Line buffer */
2671 ssize_t linelen, /* I - Length of initial line */
2672 size_t linesize) /* I - Size of line buffer */
2673 {
2674 int level; /* Embedded document level */
2675
2676
2677 level = 0;
2678
2679 while ((linelen = (ssize_t)cupsFileGetLine(fp, line, linesize)) > 0)
2680 {
2681 if (level == 0 &&
2682 (!strncmp(line, "%%Page:", 7) || !strncmp(line, "%%Trailer", 9)))
2683 break;
2684 else if (!strncmp(line, "%%BeginDocument", 15) ||
2685 !strncmp(line, "%ADO_BeginApplication", 21))
2686 level ++;
2687 else if ((!strncmp(line, "%%EndDocument", 13) ||
2688 !strncmp(line, "%ADO_EndApplication", 19)) && level > 0)
2689 level --;
2690 else if (!strncmp(line, "%%BeginBinary:", 14) ||
2691 (!strncmp(line, "%%BeginData:", 12) &&
2692 !strstr(line, "ASCII") && !strstr(line, "Hex")))
2693 {
2694 /*
2695 * Skip binary data...
2696 */
2697
2698 ssize_t bytes; /* Bytes of data */
2699
2700 bytes = atoi(strchr(line, ':') + 1);
2701
2702 while (bytes > 0)
2703 {
2704 if ((size_t)bytes > linesize)
2705 linelen = (ssize_t)cupsFileRead(fp, line, linesize);
2706 else
2707 linelen = (ssize_t)cupsFileRead(fp, line, (size_t)bytes);
2708
2709 if (linelen < 1)
2710 {
2711 line[0] = '\0';
2712 perror("ERROR: Early end-of-file while reading binary data");
2713 return (0);
2714 }
2715
2716 bytes -= linelen;
2717 }
2718 }
2719 }
2720
2721 return (linelen);
2722 }
2723
2724
2725 /*
2726 * 'start_nup()' - Start processing for N-up printing.
2727 */
2728
2729 static void
2730 start_nup(pstops_doc_t *doc, /* I - Document information */
2731 int number, /* I - Page number */
2732 int show_border, /* I - Show the border? */
2733 const int *bounding_box) /* I - BoundingBox value */
2734 {
2735 int pos; /* Position on page */
2736 int x, y; /* Relative position of subpage */
2737 double w, l, /* Width and length of subpage */
2738 tx, ty; /* Translation values for subpage */
2739 double pagew, /* Printable width of page */
2740 pagel; /* Printable height of page */
2741 int bboxx, /* BoundingBox X origin */
2742 bboxy, /* BoundingBox Y origin */
2743 bboxw, /* BoundingBox width */
2744 bboxl; /* BoundingBox height */
2745 double margin = 0; /* Current margin for border */
2746
2747
2748 if (doc->number_up > 1)
2749 doc_puts(doc, "userdict/ESPsave save put\n");
2750
2751 pos = (number - 1) % doc->number_up;
2752 pagew = PageRight - PageLeft;
2753 pagel = PageTop - PageBottom;
2754
2755 if (doc->fit_to_page)
2756 {
2757 bboxx = bounding_box[0];
2758 bboxy = bounding_box[1];
2759 bboxw = bounding_box[2] - bounding_box[0];
2760 bboxl = bounding_box[3] - bounding_box[1];
2761 }
2762 else
2763 {
2764 bboxx = 0;
2765 bboxy = 0;
2766 bboxw = (int)PageWidth;
2767 bboxl = (int)PageLength;
2768 }
2769
2770 fprintf(stderr, "DEBUG: pagew = %.1f, pagel = %.1f\n", pagew, pagel);
2771 fprintf(stderr, "DEBUG: bboxx = %d, bboxy = %d, bboxw = %d, bboxl = %d\n",
2772 bboxx, bboxy, bboxw, bboxl);
2773 fprintf(stderr, "DEBUG: PageLeft = %.1f, PageRight = %.1f\n",
2774 PageLeft, PageRight);
2775 fprintf(stderr, "DEBUG: PageTop = %.1f, PageBottom = %.1f\n",
2776 PageTop, PageBottom);
2777 fprintf(stderr, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
2778 PageWidth, PageLength);
2779
2780 switch (Orientation)
2781 {
2782 case 1 : /* Landscape */
2783 doc_printf(doc, "%.1f 0.0 translate 90 rotate\n", PageLength);
2784 break;
2785 case 2 : /* Reverse Portrait */
2786 doc_printf(doc, "%.1f %.1f translate 180 rotate\n", PageWidth,
2787 PageLength);
2788 break;
2789 case 3 : /* Reverse Landscape */
2790 doc_printf(doc, "0.0 %.1f translate -90 rotate\n", PageWidth);
2791 break;
2792 }
2793
2794 /*
2795 * Mirror the page as needed...
2796 */
2797
2798 if (doc->mirror)
2799 doc_printf(doc, "%.1f 0.0 translate -1 1 scale\n", PageWidth);
2800
2801 /*
2802 * Offset and scale as necessary for fit_to_page/fit-to-page/number-up...
2803 */
2804
2805 if (Duplex && doc->number_up > 1 && ((number / doc->number_up) & 1))
2806 doc_printf(doc, "%.1f %.1f translate\n", PageWidth - PageRight, PageBottom);
2807 else if (doc->number_up > 1 || doc->fit_to_page)
2808 doc_printf(doc, "%.1f %.1f translate\n", PageLeft, PageBottom);
2809
2810 switch (doc->number_up)
2811 {
2812 default :
2813 if (doc->fit_to_page)
2814 {
2815 w = pagew;
2816 l = w * bboxl / bboxw;
2817
2818 if (l > pagel)
2819 {
2820 l = pagel;
2821 w = l * bboxw / bboxl;
2822 }
2823
2824 tx = 0.5 * (pagew - w);
2825 ty = 0.5 * (pagel - l);
2826
2827 doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n", tx, ty,
2828 w / bboxw, l / bboxl);
2829 }
2830 else
2831 w = PageWidth;
2832 break;
2833
2834 case 2 :
2835 if (Orientation & 1)
2836 {
2837 x = pos & 1;
2838
2839 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
2840 x = 1 - x;
2841
2842 w = pagel;
2843 l = w * bboxl / bboxw;
2844
2845 if (l > (pagew * 0.5))
2846 {
2847 l = pagew * 0.5;
2848 w = l * bboxw / bboxl;
2849 }
2850
2851 tx = 0.5 * (pagew * 0.5 - l);
2852 ty = 0.5 * (pagel - w);
2853
2854 if (doc->normal_landscape)
2855 doc_printf(doc, "0.0 %.1f translate -90 rotate\n", pagel);
2856 else
2857 doc_printf(doc, "%.1f 0.0 translate 90 rotate\n", pagew);
2858
2859 doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n",
2860 ty, tx + pagew * 0.5 * x, w / bboxw, l / bboxl);
2861 }
2862 else
2863 {
2864 x = pos & 1;
2865
2866 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
2867 x = 1 - x;
2868
2869 l = pagew;
2870 w = l * bboxw / bboxl;
2871
2872 if (w > (pagel * 0.5))
2873 {
2874 w = pagel * 0.5;
2875 l = w * bboxl / bboxw;
2876 }
2877
2878 tx = 0.5 * (pagel * 0.5 - w);
2879 ty = 0.5 * (pagew - l);
2880
2881 if (doc->normal_landscape)
2882 doc_printf(doc, "%.1f 0.0 translate 90 rotate\n", pagew);
2883 else
2884 doc_printf(doc, "0.0 %.1f translate -90 rotate\n", pagel);
2885
2886 doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n",
2887 tx + pagel * 0.5 * x, ty, w / bboxw, l / bboxl);
2888 }
2889 break;
2890
2891 case 4 :
2892 if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL)
2893 {
2894 x = (pos / 2) & 1;
2895 y = pos & 1;
2896 }
2897 else
2898 {
2899 x = pos & 1;
2900 y = (pos / 2) & 1;
2901 }
2902
2903 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
2904 x = 1 - x;
2905
2906 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
2907 y = 1 - y;
2908
2909 w = pagew * 0.5;
2910 l = w * bboxl / bboxw;
2911
2912 if (l > (pagel * 0.5))
2913 {
2914 l = pagel * 0.5;
2915 w = l * bboxw / bboxl;
2916 }
2917
2918 tx = 0.5 * (pagew * 0.5 - w);
2919 ty = 0.5 * (pagel * 0.5 - l);
2920
2921 doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n",
2922 tx + x * pagew * 0.5, ty + y * pagel * 0.5,
2923 w / bboxw, l / bboxl);
2924 break;
2925
2926 case 6 :
2927 if (Orientation & 1)
2928 {
2929 if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL)
2930 {
2931 x = pos / 3;
2932 y = pos % 3;
2933
2934 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
2935 x = 1 - x;
2936
2937 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
2938 y = 2 - y;
2939 }
2940 else
2941 {
2942 x = pos & 1;
2943 y = pos / 2;
2944
2945 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
2946 x = 1 - x;
2947
2948 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
2949 y = 2 - y;
2950 }
2951
2952 w = pagel * 0.5;
2953 l = w * bboxl / bboxw;
2954
2955 if (l > (pagew * 0.333))
2956 {
2957 l = pagew * 0.333;
2958 w = l * bboxw / bboxl;
2959 }
2960
2961 tx = 0.5 * (pagel - 2 * w);
2962 ty = 0.5 * (pagew - 3 * l);
2963
2964 if (doc->normal_landscape)
2965 doc_printf(doc, "0 %.1f translate -90 rotate\n", pagel);
2966 else
2967 doc_printf(doc, "%.1f 0 translate 90 rotate\n", pagew);
2968
2969 doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n",
2970 tx + x * w, ty + y * l, l / bboxl, w / bboxw);
2971 }
2972 else
2973 {
2974 if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL)
2975 {
2976 x = pos / 2;
2977 y = pos & 1;
2978
2979 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
2980 x = 2 - x;
2981
2982 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
2983 y = 1 - y;
2984 }
2985 else
2986 {
2987 x = pos % 3;
2988 y = pos / 3;
2989
2990 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
2991 x = 2 - x;
2992
2993 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
2994 y = 1 - y;
2995 }
2996
2997 l = pagew * 0.5;
2998 w = l * bboxw / bboxl;
2999
3000 if (w > (pagel * 0.333))
3001 {
3002 w = pagel * 0.333;
3003 l = w * bboxl / bboxw;
3004 }
3005
3006 tx = 0.5 * (pagel - 3 * w);
3007 ty = 0.5 * (pagew - 2 * l);
3008
3009 if (doc->normal_landscape)
3010 doc_printf(doc, "%.1f 0 translate 90 rotate\n", pagew);
3011 else
3012 doc_printf(doc, "0 %.1f translate -90 rotate\n", pagel);
3013
3014 doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n",
3015 tx + w * x, ty + l * y, w / bboxw, l / bboxl);
3016
3017 }
3018 break;
3019
3020 case 9 :
3021 if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL)
3022 {
3023 x = (pos / 3) % 3;
3024 y = pos % 3;
3025 }
3026 else
3027 {
3028 x = pos % 3;
3029 y = (pos / 3) % 3;
3030 }
3031
3032 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
3033 x = 2 - x;
3034
3035 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
3036 y = 2 - y;
3037
3038 w = pagew * 0.333;
3039 l = w * bboxl / bboxw;
3040
3041 if (l > (pagel * 0.333))
3042 {
3043 l = pagel * 0.333;
3044 w = l * bboxw / bboxl;
3045 }
3046
3047 tx = 0.5 * (pagew * 0.333 - w);
3048 ty = 0.5 * (pagel * 0.333 - l);
3049
3050 doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n",
3051 tx + x * pagew * 0.333, ty + y * pagel * 0.333,
3052 w / bboxw, l / bboxl);
3053 break;
3054
3055 case 16 :
3056 if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL)
3057 {
3058 x = (pos / 4) & 3;
3059 y = pos & 3;
3060 }
3061 else
3062 {
3063 x = pos & 3;
3064 y = (pos / 4) & 3;
3065 }
3066
3067 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
3068 x = 3 - x;
3069
3070 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
3071 y = 3 - y;
3072
3073 w = pagew * 0.25;
3074 l = w * bboxl / bboxw;
3075
3076 if (l > (pagel * 0.25))
3077 {
3078 l = pagel * 0.25;
3079 w = l * bboxw / bboxl;
3080 }
3081
3082 tx = 0.5 * (pagew * 0.25 - w);
3083 ty = 0.5 * (pagel * 0.25 - l);
3084
3085 doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n",
3086 tx + x * pagew * 0.25, ty + y * pagel * 0.25,
3087 w / bboxw, l / bboxl);
3088 break;
3089 }
3090
3091 /*
3092 * Draw borders as necessary...
3093 */
3094
3095 if (doc->page_border && show_border)
3096 {
3097 int rects; /* Number of border rectangles */
3098 double fscale; /* Scaling value for points */
3099
3100
3101 rects = (doc->page_border & PSTOPS_BORDERDOUBLE) ? 2 : 1;
3102 fscale = PageWidth / w;
3103 margin = 2.25 * fscale;
3104
3105 /*
3106 * Set the line width and color...
3107 */
3108
3109 doc_puts(doc, "gsave\n");
3110 doc_printf(doc, "%.3f setlinewidth 0 setgray newpath\n",
3111 (doc->page_border & PSTOPS_BORDERTHICK) ? 0.5 * fscale :
3112 0.24 * fscale);
3113
3114 /*
3115 * Draw border boxes...
3116 */
3117
3118 for (; rects > 0; rects --, margin += 2 * fscale)
3119 if (doc->number_up > 1)
3120 doc_printf(doc, "%.1f %.1f %.1f %.1f ESPrs\n",
3121 margin,
3122 margin,
3123 bboxw - 2 * margin,
3124 bboxl - 2 * margin);
3125 else
3126 doc_printf(doc, "%.1f %.1f %.1f %.1f ESPrs\n",
3127 PageLeft + margin,
3128 PageBottom + margin,
3129 PageRight - PageLeft - 2 * margin,
3130 PageTop - PageBottom - 2 * margin);
3131
3132 /*
3133 * Restore pen settings...
3134 */
3135
3136 doc_puts(doc, "grestore\n");
3137 }
3138
3139 if (doc->fit_to_page)
3140 {
3141 /*
3142 * Offset the page by its bounding box...
3143 */
3144
3145 doc_printf(doc, "%d %d translate\n", -bounding_box[0],
3146 -bounding_box[1]);
3147 }
3148
3149 if (doc->fit_to_page || doc->number_up > 1)
3150 {
3151 /*
3152 * Clip the page to the page's bounding box...
3153 */
3154
3155 doc_printf(doc, "%.1f %.1f %.1f %.1f ESPrc\n",
3156 bboxx + margin, bboxy + margin,
3157 bboxw - 2 * margin, bboxl - 2 * margin);
3158 }
3159 }
3160
3161
3162 /*
3163 * 'write_label_prolog()' - Write the prolog with the classification
3164 * and page label.
3165 */
3166
3167 static void
3168 write_label_prolog(pstops_doc_t *doc, /* I - Document info */
3169 const char *label, /* I - Page label */
3170 float bottom, /* I - Bottom position in points */
3171 float top, /* I - Top position in points */
3172 float width) /* I - Width in points */
3173 {
3174 const char *classification; /* CLASSIFICATION environment variable */
3175 const char *ptr; /* Temporary string pointer */
3176
3177
3178 /*
3179 * First get the current classification...
3180 */
3181
3182 if ((classification = getenv("CLASSIFICATION")) == NULL)
3183 classification = "";
3184 if (strcmp(classification, "none") == 0)
3185 classification = "";
3186
3187 /*
3188 * If there is nothing to show, bind an empty 'write labels' procedure
3189 * and return...
3190 */
3191
3192 if (!classification[0] && (label == NULL || !label[0]))
3193 {
3194 doc_puts(doc, "userdict/ESPwl{}bind put\n");
3195 return;
3196 }
3197
3198 /*
3199 * Set the classification + page label string...
3200 */
3201
3202 doc_puts(doc, "userdict");
3203 if (!strcmp(classification, "confidential"))
3204 doc_puts(doc, "/ESPpl(CONFIDENTIAL");
3205 else if (!strcmp(classification, "classified"))
3206 doc_puts(doc, "/ESPpl(CLASSIFIED");
3207 else if (!strcmp(classification, "secret"))
3208 doc_puts(doc, "/ESPpl(SECRET");
3209 else if (!strcmp(classification, "topsecret"))
3210 doc_puts(doc, "/ESPpl(TOP SECRET");
3211 else if (!strcmp(classification, "unclassified"))
3212 doc_puts(doc, "/ESPpl(UNCLASSIFIED");
3213 else
3214 {
3215 doc_puts(doc, "/ESPpl(");
3216
3217 for (ptr = classification; *ptr; ptr ++)
3218 {
3219 if (*ptr < 32 || *ptr > 126)
3220 doc_printf(doc, "\\%03o", *ptr);
3221 else if (*ptr == '_')
3222 doc_puts(doc, " ");
3223 else if (*ptr == '(' || *ptr == ')' || *ptr == '\\')
3224 doc_printf(doc, "\\%c", *ptr);
3225 else
3226 doc_printf(doc, "%c", *ptr);
3227 }
3228 }
3229
3230 if (label)
3231 {
3232 if (classification[0])
3233 doc_puts(doc, " - ");
3234
3235 /*
3236 * Quote the label string as needed...
3237 */
3238
3239 for (ptr = label; *ptr; ptr ++)
3240 {
3241 if (*ptr < 32 || *ptr > 126)
3242 doc_printf(doc, "\\%03o", *ptr);
3243 else if (*ptr == '(' || *ptr == ')' || *ptr == '\\')
3244 doc_printf(doc, "\\%c", *ptr);
3245 else
3246 doc_printf(doc, "%c", *ptr);
3247 }
3248 }
3249
3250 doc_puts(doc, ")put\n");
3251
3252 /*
3253 * Then get a 14 point Helvetica-Bold font...
3254 */
3255
3256 doc_puts(doc, "userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put\n");
3257
3258 /*
3259 * Finally, the procedure to write the labels on the page...
3260 */
3261
3262 doc_puts(doc, "userdict/ESPwl{\n");
3263 doc_puts(doc, " ESPpf setfont\n");
3264 doc_printf(doc, " ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n",
3265 width * 0.5f);
3266 doc_puts(doc, " 1 setgray\n");
3267 doc_printf(doc, " dup 6 sub %.0f 3 index 20 ESPrf\n", bottom - 2.0);
3268 doc_printf(doc, " dup 6 sub %.0f 3 index 20 ESPrf\n", top - 18.0);
3269 doc_puts(doc, " 0 setgray\n");
3270 doc_printf(doc, " dup 6 sub %.0f 3 index 20 ESPrs\n", bottom - 2.0);
3271 doc_printf(doc, " dup 6 sub %.0f 3 index 20 ESPrs\n", top - 18.0);
3272 doc_printf(doc, " dup %.0f moveto ESPpl show\n", bottom + 2.0);
3273 doc_printf(doc, " %.0f moveto ESPpl show\n", top - 14.0);
3274 doc_puts(doc, "pop\n");
3275 doc_puts(doc, "}bind put\n");
3276 }
3277
3278
3279 /*
3280 * 'write_labels()' - Write the actual page labels.
3281 *
3282 * This function is a copy of the one in common.c since we need to
3283 * use doc_puts/doc_printf instead of puts/printf...
3284 */
3285
3286 static void
3287 write_labels(pstops_doc_t *doc, /* I - Document information */
3288 int orient) /* I - Orientation of the page */
3289 {
3290 float width, /* Width of page */
3291 length; /* Length of page */
3292
3293
3294 doc_puts(doc, "gsave\n");
3295
3296 if ((orient ^ Orientation) & 1)
3297 {
3298 width = PageLength;
3299 length = PageWidth;
3300 }
3301 else
3302 {
3303 width = PageWidth;
3304 length = PageLength;
3305 }
3306
3307 switch (orient & 3)
3308 {
3309 case 1 : /* Landscape */
3310 doc_printf(doc, "%.1f 0.0 translate 90 rotate\n", length);
3311 break;
3312 case 2 : /* Reverse Portrait */
3313 doc_printf(doc, "%.1f %.1f translate 180 rotate\n", width, length);
3314 break;
3315 case 3 : /* Reverse Landscape */
3316 doc_printf(doc, "0.0 %.1f translate -90 rotate\n", width);
3317 break;
3318 }
3319
3320 doc_puts(doc, "ESPwl\n");
3321 doc_puts(doc, "grestore\n");
3322 }
3323
3324
3325 /*
3326 * 'write_options()' - Write options provided via %%IncludeFeature.
3327 */
3328
3329 static void
3330 write_options(
3331 pstops_doc_t *doc, /* I - Document */
3332 ppd_file_t *ppd, /* I - PPD file */
3333 int num_options, /* I - Number of options */
3334 cups_option_t *options) /* I - Options */
3335 {
3336 int i; /* Looping var */
3337 ppd_option_t *option; /* PPD option */
3338 float min_order; /* Minimum OrderDependency value */
3339 char *doc_setup, /* DocumentSetup commands to send */
3340 *any_setup; /* AnySetup commands to send */
3341
3342
3343 /*
3344 * Figure out the minimum OrderDependency value...
3345 */
3346
3347 if ((option = ppdFindOption(ppd, "PageRegion")) != NULL)
3348 min_order = option->order;
3349 else
3350 min_order = 999.0f;
3351
3352 for (i = 0; i < num_options; i ++)
3353 if ((option = ppdFindOption(ppd, options[i].name)) != NULL &&
3354 option->order < min_order)
3355 min_order = option->order;
3356
3357 /*
3358 * Mark and extract them...
3359 */
3360
3361 cupsMarkOptions(ppd, num_options, options);
3362
3363 doc_setup = ppdEmitString(ppd, PPD_ORDER_DOCUMENT, min_order);
3364 any_setup = ppdEmitString(ppd, PPD_ORDER_ANY, min_order);
3365
3366 /*
3367 * Then send them out...
3368 */
3369
3370 if (doc->number_up > 1)
3371 {
3372 /*
3373 * Temporarily restore setpagedevice so we can set the options...
3374 */
3375
3376 doc_puts(doc, "userdict/setpagedevice/CUPSsetpagedevice load put\n");
3377 }
3378
3379 if (doc_setup)
3380 {
3381 doc_puts(doc, doc_setup);
3382 free(doc_setup);
3383 }
3384
3385 if (any_setup)
3386 {
3387 doc_puts(doc, any_setup);
3388 free(any_setup);
3389 }
3390
3391 if (doc->number_up > 1)
3392 {
3393 /*
3394 * Disable setpagedevice again...
3395 */
3396
3397 doc_puts(doc, "userdict/setpagedevice{pop}bind put\n");
3398 }
3399 }
3400
3401
3402 /*
3403 * End of "$Id$".
3404 */