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