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