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