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