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