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