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