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