]> git.ipfire.org Git - thirdparty/cups.git/blob - filter/bannertops.c
Merge changes from CUPS 1.5svn-r9400
[thirdparty/cups.git] / filter / bannertops.c
1 /*
2 * "$Id$"
3 *
4 * Banner to PostScript filter for CUPS.
5 *
6 * Copyright 2008-2010 by Apple Inc.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
13 *
14 * This file is subject to the Apple OS-Developed Software exception.
15 *
16 * Contents:
17 *
18 * main() - Generate PostScript cover pages.
19 * load_banner() - Load the banner file.
20 * ps_ascii85() - Print binary data as a series of base-85 numbers.
21 * write_banner() - Write a banner page...
22 * write_epilogue() - Write the PostScript file epilogue.
23 * write_prolog() - Write the PostScript file prolog with options.
24 */
25
26 /*
27 * Include necessary headers...
28 */
29
30 #include "pstext.h"
31 #include "image.h"
32 #include <cups/language-private.h>
33
34
35 /*
36 * Constants...
37 */
38
39 #define SHOW_IMAGEABLE_AREA 1 /* Show imageable area */
40 #define SHOW_JOB_BILLING 2 /* Show billing string */
41 #define SHOW_JOB_ID 4 /* Show job ID */
42 #define SHOW_JOB_NAME 8 /* Show job title */
43 #define SHOW_JOB_ORIGINATING_USER_NAME 16 /* Show owner of job */
44 #define SHOW_JOB_ORIGINATING_HOST_NAME 32 /* Show submitting system */
45 #define SHOW_JOB_UUID 64 /* Show job UUID */
46 #define SHOW_OPTIONS 128 /* Show print options */
47 #define SHOW_PAPER_NAME 256 /* Show paper size name */
48 #define SHOW_PAPER_SIZE 512 /* Show paper dimensions */
49 #define SHOW_PRINTER_DRIVER_NAME 1024 /* Show printer driver name */
50 #define SHOW_PRINTER_DRIVER_VERSION 2048 /* Show printer driver version */
51 #define SHOW_PRINTER_INFO 4096 /* Show printer description */
52 #define SHOW_PRINTER_LOCATION 8192 /* Show printer location */
53 #define SHOW_PRINTER_MAKE_AND_MODEL 16384 /* Show printer make and model */
54 #define SHOW_PRINTER_NAME 32768 /* Show printer queue ID */
55 #define SHOW_TIME_AT_CREATION 65536 /* Show date/time when submitted */
56 #define SHOW_TIME_AT_PROCESSING 131072 /* Show date/time when printed */
57
58
59 /*
60 * Structures...
61 */
62
63 typedef struct banner_file_s /**** Banner file data ****/
64 {
65 int show; /* What to show */
66 char *header, /* Header text */
67 *footer; /* Footer text */
68 cups_array_t *notices, /* Notices to show */
69 *images; /* Images to show */
70 } banner_file_t;
71
72
73 /*
74 * Local functions...
75 */
76
77 static banner_file_t *load_banner(const char *filename);
78 static int write_banner(banner_file_t *banner, ppd_file_t *ppd,
79 ps_text_t *fonts, int job_id,
80 const char *title, const char *username,
81 int num_options, cups_option_t *options);
82 static void write_epilogue(int num_pages);
83 static ps_text_t *write_prolog(const char *title, const char *user);
84
85
86 /*
87 * 'main()' - Generate PostScript cover pages.
88 */
89
90 int /* O - Exit status */
91 main(int argc, /* I - Number of command-line args */
92 char *argv[]) /* I - Command-line arguments */
93 {
94 banner_file_t *banner; /* Banner file data */
95 int num_options; /* Number of print options */
96 cups_option_t *options; /* Print options */
97 ppd_file_t *ppd; /* PPD file */
98 ps_text_t *fonts; /* Fonts for output */
99 int job_id; /* Job ID from command-line */
100 const char *title, /* Title from command-line */
101 *username; /* Username from command-line */
102 int num_pages; /* Number of pages printed */
103
104
105 /*
106 * Make sure status messages are not buffered...
107 */
108
109 setbuf(stderr, NULL);
110
111 /*
112 * Check command-line...
113 */
114
115 if (argc < 6 || argc > 7)
116 {
117 _cupsLangPrintf(stderr,
118 _("Usage: %s job-id user title copies options file"),
119 argv[0]);
120 return (1);
121 }
122
123 /*
124 * Get stuff from command-line...
125 */
126
127 job_id = atoi(argv[1]);
128 username = argv[2];
129 title = argv[3];
130 options = NULL;
131 num_options = cupsParseOptions(argv[5], 0, &options);
132 banner = load_banner(argv[6]);
133
134 /*
135 * Set standard options and get the PPD file for this printer...
136 */
137
138 ppd = SetCommonOptions(num_options, options, 1);
139
140 /*
141 * Write a PostScript banner document and return...
142 */
143
144 fonts = write_prolog(title, username);
145 num_pages = write_banner(banner, ppd, fonts, job_id, title, username,
146 num_options, options);
147
148 write_epilogue(num_pages);
149
150 return (0);
151 }
152
153
154 /*
155 * 'load_banner()' - Load the banner file.
156 */
157
158 static banner_file_t * /* O - Banner file data */
159 load_banner(const char *filename) /* I - Filename or NULL for stdin */
160 {
161 cups_file_t *fp; /* File */
162 char line[2048], /* Line buffer */
163 *ptr; /* Pointer into line */
164 int linenum; /* Current line number */
165 banner_file_t *banner; /* Banner file data */
166 const char *cups_docroot; /* CUPS_DOCROOT environment variable */
167
168
169 fprintf(stderr, "DEBUG: load_banner(filename=\"%s\")\n",
170 filename ? filename : "(stdin)");
171
172 /*
173 * Open the banner file...
174 */
175
176 if (filename)
177 fp = cupsFileOpen(filename, "r");
178 else
179 fp = cupsFileStdin();
180
181 if (!fp)
182 {
183 _cupsLangPrintError("ERROR", _("Unable to open print file"));
184 exit(1);
185 }
186
187 /*
188 * Read the banner file...
189 */
190
191 if ((cups_docroot = getenv("CUPS_DOCROOT")) == NULL)
192 cups_docroot = CUPS_DOCROOT;
193
194 banner = calloc(1, sizeof(banner_file_t));
195 linenum = 0;
196
197 while (cupsFileGets(fp, line, sizeof(line)))
198 {
199 /*
200 * Skip blank and comment lines...
201 */
202
203 linenum ++;
204
205 if (line[0] == '#' || !line[0])
206 continue;
207
208 /*
209 * Break the line into keyword and value parts...
210 */
211
212 for (ptr = line; *ptr && !isspace(*ptr & 255); ptr ++);
213
214 while (isspace(*ptr & 255))
215 *ptr++ = '\0';
216
217 if (!*ptr)
218 {
219 _cupsLangPrintFilter(stderr, "ERROR",
220 _("Missing value on line %d of banner file."),
221 linenum);
222 continue;
223 }
224
225 /*
226 * Save keyword values in the appropriate places...
227 */
228
229 if (!strcasecmp(line, "Footer"))
230 {
231 if (banner->footer)
232 fprintf(stderr, "DEBUG: Extra \"Footer\" on line %d of banner file\n",
233 linenum);
234 else
235 banner->footer = strdup(ptr);
236 }
237 else if (!strcasecmp(line, "Header"))
238 {
239 if (banner->header)
240 fprintf(stderr, "DEBUG: Extra \"Header\" on line %d of banner file\n",
241 linenum);
242 else
243 banner->header = strdup(ptr);
244 }
245 else if (!strcasecmp(line, "Image"))
246 {
247 char imagefile[1024]; /* Image filename */
248
249
250 if (ptr[0] == '/')
251 strlcpy(imagefile, ptr, sizeof(imagefile));
252 else
253 snprintf(imagefile, sizeof(imagefile), "%s/%s", cups_docroot, ptr);
254
255 if (access(imagefile, R_OK))
256 {
257 fprintf(stderr, "DEBUG: Image \"%s\" on line %d of banner file: %s\n",
258 ptr, linenum, strerror(errno));
259 }
260 else
261 {
262 if (!banner->images)
263 banner->images = cupsArrayNew(NULL, NULL);
264
265 cupsArrayAdd(banner->images, strdup(imagefile));
266 }
267 }
268 else if (!strcasecmp(line, "Notice"))
269 {
270 if (!banner->notices)
271 banner->notices = cupsArrayNew(NULL, NULL);
272
273 cupsArrayAdd(banner->notices, strdup(ptr));
274 }
275 else if (!strcasecmp(line, "Show"))
276 {
277 char *value; /* Current value */
278
279
280 for (value = ptr; *value; value = ptr)
281 {
282 /*
283 * Find the end of the current value
284 */
285
286 while (*ptr && !isspace(*ptr & 255))
287 ptr ++;
288
289 while (*ptr && isspace(*ptr & 255))
290 *ptr++ = '\0';
291
292 /*
293 * Add the value to the show flags...
294 */
295 if (!strcasecmp(value, "imageable-area"))
296 banner->show |= SHOW_IMAGEABLE_AREA;
297 else if (!strcasecmp(value, "job-billing"))
298 banner->show |= SHOW_JOB_BILLING;
299 else if (!strcasecmp(value, "job-id"))
300 banner->show |= SHOW_JOB_ID;
301 else if (!strcasecmp(value, "job-name"))
302 banner->show |= SHOW_JOB_NAME;
303 else if (!strcasecmp(value, "job-originating-host-name"))
304 banner->show |= SHOW_JOB_ORIGINATING_HOST_NAME;
305 else if (!strcasecmp(value, "job-originating-user-name"))
306 banner->show |= SHOW_JOB_ORIGINATING_USER_NAME;
307 else if (!strcasecmp(value, "job-uuid"))
308 banner->show |= SHOW_JOB_UUID;
309 else if (!strcasecmp(value, "options"))
310 banner->show |= SHOW_OPTIONS;
311 else if (!strcasecmp(value, "paper-name"))
312 banner->show |= SHOW_PAPER_NAME;
313 else if (!strcasecmp(value, "paper-size"))
314 banner->show |= SHOW_PAPER_SIZE;
315 else if (!strcasecmp(value, "printer-driver-name"))
316 banner->show |= SHOW_PRINTER_DRIVER_NAME;
317 else if (!strcasecmp(value, "printer-driver-version"))
318 banner->show |= SHOW_PRINTER_DRIVER_VERSION;
319 else if (!strcasecmp(value, "printer-info"))
320 banner->show |= SHOW_PRINTER_INFO;
321 else if (!strcasecmp(value, "printer-location"))
322 banner->show |= SHOW_PRINTER_LOCATION;
323 else if (!strcasecmp(value, "printer-make-and-model"))
324 banner->show |= SHOW_PRINTER_MAKE_AND_MODEL;
325 else if (!strcasecmp(value, "printer-name"))
326 banner->show |= SHOW_PRINTER_NAME;
327 else if (!strcasecmp(value, "time-at-creation"))
328 banner->show |= SHOW_TIME_AT_CREATION;
329 else if (!strcasecmp(value, "time-at-processing"))
330 banner->show |= SHOW_TIME_AT_PROCESSING;
331 else
332 {
333 fprintf(stderr,
334 "DEBUG: Unknown \"Show\" value \"%s\" on line %d of banner "
335 "file\n", value, linenum);
336 }
337 }
338 }
339 else
340 fprintf(stderr, "DEBUG: Unknown key \"%s\" on line %d of banner file\n",
341 line, linenum);
342 }
343
344 if (filename)
345 cupsFileClose(fp);
346
347 return (banner);
348 }
349
350
351 /*
352 * 'ps_ascii85()' - Print binary data as a series of base-85 numbers.
353 */
354
355 static void
356 ps_ascii85(cups_ib_t *data, /* I - Data to print */
357 int length, /* I - Number of bytes to print */
358 int last_line) /* I - Last line of raster data? */
359 {
360 unsigned b; /* Binary data word */
361 unsigned char c[5]; /* ASCII85 encoded chars */
362 static int col = 0; /* Current column */
363
364
365 while (length > 3)
366 {
367 b = (((((data[0] << 8) | data[1]) << 8) | data[2]) << 8) | data[3];
368
369 if (b == 0)
370 {
371 putchar('z');
372 col ++;
373 }
374 else
375 {
376 c[4] = (b % 85) + '!';
377 b /= 85;
378 c[3] = (b % 85) + '!';
379 b /= 85;
380 c[2] = (b % 85) + '!';
381 b /= 85;
382 c[1] = (b % 85) + '!';
383 b /= 85;
384 c[0] = b + '!';
385
386 fwrite(c, 5, 1, stdout);
387 col += 5;
388 }
389
390 data += 4;
391 length -= 4;
392
393 if (col >= 75)
394 {
395 putchar('\n');
396 col = 0;
397 }
398 }
399
400 if (last_line)
401 {
402 if (length > 0)
403 {
404 memset(data + length, 0, 4 - length);
405 b = (((((data[0] << 8) | data[1]) << 8) | data[2]) << 8) | data[3];
406
407 c[4] = (b % 85) + '!';
408 b /= 85;
409 c[3] = (b % 85) + '!';
410 b /= 85;
411 c[2] = (b % 85) + '!';
412 b /= 85;
413 c[1] = (b % 85) + '!';
414 b /= 85;
415 c[0] = b + '!';
416
417 fwrite(c, length + 1, 1, stdout);
418 }
419
420 puts("~>");
421 col = 0;
422 }
423 }
424
425
426 /*
427 * 'write_banner()' - Write a banner page...
428 */
429
430 static int /* O - Number of pages */
431 write_banner(banner_file_t *banner, /* I - Banner file */
432 ppd_file_t *ppd, /* I - PPD file */
433 ps_text_t *fonts, /* I - Fonts */
434 int job_id, /* I - Job ID */
435 const char *title, /* I - Title of job */
436 const char *username, /* I - Owner of job */
437 int num_options, /* I - Number of options */
438 cups_option_t *options) /* I - Options */
439 {
440 char *notice; /* Current notice */
441 char *imagefile; /* Current image file */
442 cups_array_t *images; /* Images */
443 cups_image_t *image; /* Current image */
444 const char *option; /* Option value */
445 int i, j; /* Looping vars */
446 float x, /* Current X position */
447 y; /* Current Y position */
448 cups_lang_t *language; /* Default language */
449 int showlines; /* Number of lines to show */
450 float fontsize; /* Font size to use */
451 int num_pages; /* Number of pages */
452 float print_width, /* Printable width of page */
453 print_height, /* Printable height of page */
454 info_top, /* Top of info fields */
455 info_height, /* Height of info fields */
456 line_height, /* Height of info lines */
457 notices_height, /* Height of all notices */
458 images_width, /* Width of all images */
459 images_height, /* Height of all images */
460 total_height; /* Height of all content */
461 char text[1024]; /* Formatted field text */
462
463
464 /*
465 * Figure out how many lines of text will be shown...
466 */
467
468 showlines = 0;
469 if (banner->show & SHOW_IMAGEABLE_AREA)
470 showlines += 2;
471 if (banner->show & SHOW_JOB_BILLING)
472 showlines ++;
473 if (banner->show & SHOW_JOB_ID)
474 showlines ++;
475 if (banner->show & SHOW_JOB_NAME)
476 showlines ++;
477 if (banner->show & SHOW_JOB_ORIGINATING_USER_NAME)
478 showlines ++;
479 if (banner->show & SHOW_JOB_ORIGINATING_HOST_NAME)
480 showlines ++;
481 if (banner->show & SHOW_JOB_UUID)
482 showlines ++;
483 if (banner->show & SHOW_OPTIONS)
484 {
485 for (j = 0; j < num_options; j ++)
486 {
487 if (strcasecmp("media", options[j].name) &&
488 strcasecmp("PageSize", options[j].name) &&
489 strcasecmp("PageRegion", options[j].name) &&
490 strcasecmp("InputSlot", options[j].name) &&
491 strcasecmp("MediaType", options[j].name) &&
492 strcasecmp("finishings", options[j].name) &&
493 strcasecmp("sides", options[j].name) &&
494 strcasecmp("Duplex", options[j].name) &&
495 strcasecmp("orientation-requested", options[j].name) &&
496 strcasecmp("landscape", options[j].name) &&
497 strcasecmp("number-up", options[j].name) &&
498 strcasecmp("OutputOrder", options[j].name))
499 continue;
500
501 showlines ++;
502 }
503 }
504 if (banner->show & SHOW_PAPER_NAME)
505 showlines ++;
506 if (banner->show & SHOW_PAPER_SIZE)
507 showlines += 2;
508 if (banner->show & SHOW_PRINTER_DRIVER_NAME)
509 showlines ++;
510 if (banner->show & SHOW_PRINTER_DRIVER_VERSION)
511 showlines ++;
512 if (banner->show & SHOW_PRINTER_INFO)
513 showlines ++;
514 if (banner->show & SHOW_PRINTER_LOCATION)
515 showlines ++;
516 if (banner->show & SHOW_PRINTER_MAKE_AND_MODEL)
517 showlines ++;
518 if (banner->show & SHOW_PRINTER_NAME)
519 showlines ++;
520 if (banner->show & SHOW_TIME_AT_CREATION)
521 showlines ++;
522 if (banner->show & SHOW_TIME_AT_PROCESSING)
523 showlines ++;
524
525 /*
526 * Figure out the dimensions and positions of everything...
527 */
528
529 print_width = PageRight - PageLeft;
530 print_height = PageTop - PageBottom;
531 fontsize = print_height / 60; /* Nominally 12pts */
532 line_height = 1.2 * fontsize;
533 info_height = showlines * line_height;
534 notices_height = cupsArrayCount(banner->notices) * line_height;
535
536 if (cupsArrayCount(banner->images))
537 {
538 images = cupsArrayNew(NULL, NULL);
539 images_height = print_height / 10; /* Nominally 1" */
540
541 for (imagefile = (char *)cupsArrayFirst(banner->images), images_width = 0.0;
542 imagefile;
543 imagefile = (char *)cupsArrayNext(banner->images))
544 {
545 if ((image = cupsImageOpen(imagefile, ColorDevice ? CUPS_IMAGE_RGB_CMYK :
546 CUPS_IMAGE_WHITE,
547 CUPS_IMAGE_WHITE, 100, 0, NULL)) == NULL)
548 {
549 fprintf(stderr, "DEBUG: Unable to open image file \"%s\"\n",
550 imagefile);
551 continue;
552 }
553
554 images_width += cupsImageGetWidth(image) * images_height /
555 cupsImageGetHeight(image);
556 cupsArrayAdd(images, image);
557 }
558 }
559 else
560 {
561 images = NULL;
562 images_height = 0;
563 images_width = 0;
564 }
565
566 total_height = info_height + notices_height + images_height;
567 if (cupsArrayCount(banner->notices) && showlines)
568 total_height += 2 * line_height;
569 if (cupsArrayCount(banner->images) &&
570 (showlines || cupsArrayCount(banner->notices)))
571 total_height += 2 * line_height;
572
573 info_top = 0.5 * (print_height + total_height);
574
575 /*
576 * Write the page(s)...
577 */
578
579 language = cupsLangDefault();
580 num_pages = Duplex ? 2 : 1;
581
582 for (i = 1; i <= num_pages; i ++)
583 {
584 /*
585 * Start the page...
586 */
587
588 printf("%%%%Page: %s %d\n", i == 1 ? "coverpage" : "coverback", i);
589 puts("gsave");
590 if (i == 1)
591 printf("%.1f %.1f translate\n", PageLeft, PageBottom);
592 else
593 printf("%.1f %.1f translate\n", PageWidth - PageRight,
594 PageLength - PageTop);
595 puts("0 setgray");
596
597 y = info_top;
598
599 /*
600 * Information...
601 */
602
603 if (banner->show)
604 {
605 x = 0.33 * print_width;
606
607 if (banner->show & SHOW_PRINTER_NAME)
608 {
609 printf("%.1f %.1f moveto", x, y);
610 y -= line_height;
611 psTextUTF8(fonts, fontsize, PS_BOLD, PS_RIGHT,
612 _cupsLangString(language, _("Printer Name: ")));
613 psTextUTF8(fonts, fontsize, PS_NORMAL, PS_LEFT, getenv("PRINTER"));
614 }
615 if (banner->show & SHOW_JOB_ID)
616 {
617 snprintf(text, sizeof(text), "%s-%d", getenv("PRINTER"), job_id);
618 printf("%.1f %.1f moveto", x, y);
619 y -= line_height;
620 psTextUTF8(fonts, fontsize, PS_BOLD, PS_RIGHT,
621 _cupsLangString(language, _("Job ID: ")));
622 psTextUTF8(fonts, fontsize, PS_NORMAL, PS_LEFT, text);
623 }
624 if (banner->show & SHOW_JOB_UUID)
625 {
626 printf("%.1f %.1f moveto", x, y);
627 y -= line_height;
628 psTextUTF8(fonts, fontsize, PS_BOLD, PS_RIGHT,
629 _cupsLangString(language, _("Job UUID: ")));
630 psTextUTF8(fonts, fontsize, PS_NORMAL, PS_LEFT,
631 cupsGetOption("job-uuid", num_options, options));
632 }
633 if (banner->show & SHOW_JOB_NAME)
634 {
635 printf("%.1f %.1f moveto", x, y);
636 y -= line_height;
637 psTextUTF8(fonts, fontsize, PS_BOLD, PS_RIGHT,
638 _cupsLangString(language, _("Title: ")));
639 psTextUTF8(fonts, fontsize, PS_NORMAL, PS_LEFT, title);
640 }
641 if (banner->show & SHOW_JOB_ORIGINATING_USER_NAME)
642 {
643 printf("%.1f %.1f moveto", x, y);
644 y -= line_height;
645 psTextUTF8(fonts, fontsize, PS_BOLD, PS_RIGHT,
646 _cupsLangString(language, _("Printed For: ")));
647 psTextUTF8(fonts, fontsize, PS_NORMAL, PS_LEFT, username);
648 }
649 if (banner->show & SHOW_JOB_ORIGINATING_HOST_NAME)
650 {
651 printf("%.1f %.1f moveto", x, y);
652 y -= line_height;
653 psTextUTF8(fonts, fontsize, PS_BOLD, PS_RIGHT,
654 _cupsLangString(language, _("Printed From: ")));
655 psTextUTF8(fonts, fontsize, PS_NORMAL, PS_LEFT,
656 cupsGetOption("job-originating-host-name", num_options,
657 options));
658 }
659 if (banner->show & SHOW_JOB_BILLING)
660 {
661 printf("%.1f %.1f moveto", x, y);
662 y -= line_height;
663 psTextUTF8(fonts, fontsize, PS_BOLD, PS_RIGHT,
664 _cupsLangString(language, _("Billing Information: ")));
665 psTextUTF8(fonts, fontsize, PS_NORMAL, PS_LEFT,
666 cupsGetOption("job-billing", num_options, options));
667 }
668 if (banner->show & SHOW_OPTIONS)
669 {
670 printf("%.1f %.1f moveto", x, y);
671 psTextUTF8(fonts, fontsize, PS_BOLD, PS_RIGHT,
672 _cupsLangString(language, _("Options: ")));
673
674 for (j = 0; j < num_options; j ++)
675 {
676 if (strcasecmp("media", options[j].name) &&
677 strcasecmp("PageSize", options[j].name) &&
678 strcasecmp("PageRegion", options[j].name) &&
679 strcasecmp("InputSlot", options[j].name) &&
680 strcasecmp("MediaType", options[j].name) &&
681 strcasecmp("finishings", options[j].name) &&
682 strcasecmp("sides", options[j].name) &&
683 strcasecmp("Duplex", options[j].name) &&
684 strcasecmp("orientation-requested", options[j].name) &&
685 strcasecmp("landscape", options[j].name) &&
686 strcasecmp("number-up", options[j].name) &&
687 strcasecmp("OutputOrder", options[j].name))
688 continue;
689
690 if (!strcasecmp("landscape", options[j].name))
691 strlcpy(text, "orientation-requested=landscape", sizeof(text));
692 else if (!strcasecmp("orientation-requested", options[j].name))
693 {
694 switch (atoi(options[j].value))
695 {
696 default :
697 case IPP_PORTRAIT :
698 strlcpy(text, "orientation-requested=portrait",
699 sizeof(text));
700 break;
701
702 case IPP_LANDSCAPE :
703 strlcpy(text, "orientation-requested=landscape",
704 sizeof(text));
705 break;
706
707 case IPP_REVERSE_PORTRAIT :
708 strlcpy(text, "orientation-requested=reverse-portrait",
709 sizeof(text));
710 break;
711
712 case IPP_REVERSE_LANDSCAPE :
713 strlcpy(text, "orientation-requested=reverse-landscape",
714 sizeof(text));
715 break;
716 }
717 }
718 else
719 snprintf(text, sizeof(text), "%s=%s", options[j].name,
720 options[j].value);
721
722 printf("%.1f %.1f moveto", x, y);
723 y -= line_height;
724 psTextUTF8(fonts, fontsize, PS_NORMAL, PS_LEFT, text);
725 }
726 }
727
728 if (banner->show & SHOW_PRINTER_INFO)
729 {
730 printf("%.1f %.1f moveto", x, y);
731 y -= line_height;
732 psTextUTF8(fonts, fontsize, PS_BOLD, PS_RIGHT,
733 _cupsLangString(language, _("Description: ")));
734 psTextUTF8(fonts, fontsize, PS_NORMAL, PS_LEFT,
735 getenv("PRINTER_INFO"));
736 }
737 if (banner->show & SHOW_PRINTER_LOCATION)
738 {
739 printf("%.1f %.1f moveto", x, y);
740 y -= line_height;
741 psTextUTF8(fonts, fontsize, PS_BOLD, PS_RIGHT,
742 _cupsLangString(language, _("Location: ")));
743 psTextUTF8(fonts, fontsize, PS_NORMAL, PS_LEFT,
744 getenv("PRINTER_LOCATION"));
745 }
746 if (banner->show & SHOW_PRINTER_MAKE_AND_MODEL)
747 {
748 printf("%.1f %.1f moveto", x, y);
749 y -= line_height;
750 psTextUTF8(fonts, fontsize, PS_BOLD, PS_RIGHT,
751 _cupsLangString(language, _("Make and Model: ")));
752 psTextUTF8(fonts, fontsize, PS_NORMAL, PS_LEFT,
753 ppd ? ppd->nickname : NULL);
754 }
755
756 if (banner->show & SHOW_PAPER_NAME)
757 {
758 if ((option = cupsGetOption("media", num_options, options)) == NULL)
759 if ((option = cupsGetOption("PageSize", num_options, options)) == NULL)
760 if ((option = cupsGetOption("PageRegion", num_options,
761 options)) == NULL)
762 option = "Default";
763
764 printf("%.1f %.1f moveto", x, y);
765 y -= line_height;
766 psTextUTF8(fonts, fontsize, PS_BOLD, PS_RIGHT,
767 _cupsLangString(language, _("Media Name: ")));
768 psTextUTF8(fonts, fontsize, PS_NORMAL, PS_LEFT, option);
769 }
770 if (banner->show & SHOW_PAPER_SIZE)
771 {
772 snprintf(text, sizeof(text),
773 _cupsLangString(language, _("%.2f x %.2f inches")),
774 PageWidth / 72.0, PageLength / 72.0);
775 printf("%.1f %.1f moveto", x, y);
776 y -= line_height;
777 psTextUTF8(fonts, fontsize, PS_BOLD, PS_RIGHT,
778 _cupsLangString(language, _("Media Dimensions: ")));
779 psTextUTF8(fonts, fontsize, PS_NORMAL, PS_LEFT, text);
780
781 snprintf(text, sizeof(text),
782 _cupsLangString(language, _("%.0f x %.0f millimeters")),
783 PageWidth * 25.4 / 72.0, PageLength * 25.4 / 72.0);
784 printf("%.1f %.1f moveto", x, y);
785 y -= line_height;
786 psTextUTF8(fonts, fontsize, PS_NORMAL, PS_LEFT, text);
787 }
788 if (banner->show & SHOW_IMAGEABLE_AREA)
789 {
790 snprintf(text, sizeof(text),
791 _cupsLangString(language,
792 _("%.2f x %.2f to %.2f x %.2f inches")),
793 PageLeft / 72.0, PageBottom / 72.0,
794 PageRight / 72.0, PageTop / 72.0);
795 printf("%.1f %.1f moveto", x, y);
796 y -= line_height;
797 psTextUTF8(fonts, fontsize, PS_BOLD, PS_RIGHT,
798 _cupsLangString(language, _("Media Limits: ")));
799 psTextUTF8(fonts, fontsize, PS_NORMAL, PS_LEFT, text);
800
801 snprintf(text, sizeof(text),
802 _cupsLangString(language,
803 _("%.0f x %.0f to %.0f x %.0f millimeters")),
804 PageLeft * 25.4 / 72.0, PageBottom * 25.4 / 72.0,
805 PageRight * 25.4 / 72.0, PageTop * 25.4 / 72.0);
806 printf("%.1f %.1f moveto", x, y);
807 y -= line_height;
808 psTextUTF8(fonts, fontsize, PS_NORMAL, PS_LEFT, text);
809
810 printf("gsave 2 setlinewidth 1 1 %.1f %.1f rectstroke grestore\n",
811 print_width - 2.0, print_height - 2.0);
812 }
813 if (banner->show & SHOW_PRINTER_DRIVER_NAME)
814 {
815 printf("%.1f %.1f moveto", x, y);
816 y -= line_height;
817 psTextUTF8(fonts, fontsize, PS_BOLD, PS_RIGHT,
818 _cupsLangString(language, _("Driver Name: ")));
819 psTextUTF8(fonts, fontsize, PS_NORMAL, PS_LEFT,
820 ppd ? ppd->pcfilename : NULL);
821 }
822 if (banner->show & SHOW_PRINTER_DRIVER_VERSION)
823 {
824 ppd_attr_t *file_version = ppdFindAttr(ppd, "FileVersion", NULL);
825
826 printf("%.1f %.1f moveto", x, y);
827 y -= line_height;
828 psTextUTF8(fonts, fontsize, PS_BOLD, PS_RIGHT,
829 _cupsLangString(language, _("Driver Version: ")));
830 psTextUTF8(fonts, fontsize, PS_NORMAL, PS_LEFT,
831 file_version ? file_version->value : NULL);
832 }
833 if (banner->show & SHOW_TIME_AT_CREATION)
834 {
835 if ((option = cupsGetOption("time-at-creation", num_options,
836 options)) != NULL)
837 {
838 time_t curtime; /* Current time */
839 struct tm *curdate; /* Current date */
840
841 curtime = (time_t)atoi(option);
842 curdate = localtime(&curtime);
843
844 strftime(text, sizeof(text), "%c", curdate);
845 }
846 else
847 strlcpy(text, "?", sizeof(text));
848
849 printf("%.1f %.1f moveto", x, y);
850 y -= line_height;
851 psTextUTF8(fonts, fontsize, PS_BOLD, PS_RIGHT,
852 _cupsLangString(language, _("Created On: ")));
853 psTextUTF8(fonts, fontsize, PS_NORMAL, PS_LEFT, text);
854 }
855 if (banner->show & SHOW_TIME_AT_PROCESSING)
856 {
857 if ((option = cupsGetOption("time-at-processing", num_options,
858 options)) != NULL)
859 {
860 time_t curtime; /* Current time */
861 struct tm *curdate; /* Current date */
862
863 curtime = (time_t)atoi(option);
864 curdate = localtime(&curtime);
865
866 strftime(text, sizeof(text), "%c", curdate);
867 }
868 else
869 strlcpy(text, "?", sizeof(text));
870
871 printf("%.1f %.1f moveto", x, y);
872 y -= line_height;
873 psTextUTF8(fonts, fontsize, PS_BOLD, PS_RIGHT,
874 _cupsLangString(language, _("Printed On: ")));
875 psTextUTF8(fonts, fontsize, PS_NORMAL, PS_LEFT, text);
876 }
877 }
878
879 /*
880 * Notices...
881 */
882
883 if (cupsArrayCount(banner->notices))
884 {
885 if (banner->show)
886 y -= 2 * line_height;
887
888 x = 0.5 * print_width;
889
890 for (notice = (char *)cupsArrayFirst(banner->notices);
891 notice;
892 notice = (char *)cupsArrayNext(banner->notices))
893 {
894 printf("%.1f %.1f moveto", x, y);
895 y -= line_height;
896 psTextUTF8(fonts, fontsize, PS_NORMAL, PS_CENTER, notice);
897 }
898 }
899
900 /*
901 * Images...
902 */
903
904 if (cupsArrayCount(images))
905 {
906 if (banner->show || cupsArrayCount(banner->notices))
907 y -= 2 * line_height;
908
909 x = 0.5 * (print_width - images_width);
910
911 for (image = (cups_image_t *)cupsArrayFirst(images);
912 image;
913 image = (cups_image_t *)cupsArrayNext(images))
914 {
915 float temp_width; /* Width of this image */
916 int depth, /* Bytes per pixel */
917 num_cols, /* Number of columns */
918 row, /* Current row */
919 num_rows, /* Number of rows */
920 out_length, /* Length of data to write */
921 out_offset; /* Offset in line buffer */
922 unsigned char *line; /* Data for current row */
923
924
925 depth = cupsImageGetDepth(image);
926 num_cols = cupsImageGetWidth(image);
927 num_rows = cupsImageGetHeight(image);
928 line = malloc(depth * num_cols + 3);
929 temp_width = num_cols * images_height / num_rows;
930
931 printf("gsave %.1f %.1f translate %.3f %.3f scale\n", x, y,
932 temp_width / num_cols, images_height / num_rows);
933 x += temp_width;
934
935 switch (cupsImageGetColorSpace(image))
936 {
937 default :
938 case CUPS_IMAGE_WHITE :
939 printf("/DeviceGray setcolorspace"
940 "<<"
941 "/ImageType 1"
942 "/Width %d"
943 "/Height %d"
944 "/BitsPerComponent 8"
945 "/Decode[0 1]\n",
946 num_cols, num_rows);
947 break;
948
949 case CUPS_IMAGE_RGB :
950 printf("/DeviceRGB setcolorspace"
951 "<<"
952 "/ImageType 1"
953 "/Width %d"
954 "/Height %d"
955 "/BitsPerComponent 8"
956 "/Decode[0 1 0 1 0 1]\n",
957 num_cols, num_rows);
958 break;
959
960 case CUPS_IMAGE_CMYK :
961 printf("/DeviceCMYK setcolorspace"
962 "<<"
963 "/ImageType 1"
964 "/Width %d"
965 "/Height %d"
966 "/BitsPerComponent 8"
967 "/Decode[0 1 0 1 0 1 0 1]\n",
968 num_cols, num_rows);
969 break;
970 }
971
972 puts("/DataSource currentfile"
973 "/ASCII85Decode filter"
974 "/ImageMatrix[1 0 0 -1 0 1]>>image");
975
976 for (row = 0, out_offset = 0; row < num_rows; row ++)
977 {
978 cupsImageGetRow(image, 0, row, num_cols, line + out_offset);
979
980 out_length = num_cols * depth + out_offset;
981 out_offset = out_length & 3;
982
983 ps_ascii85(line, out_length, row == (num_rows - 1));
984
985 if (out_offset > 0)
986 memcpy(line, line + out_length - out_offset, out_offset);
987 }
988
989 puts("grestore");
990
991 if (i == num_pages)
992 cupsImageClose(image);
993 }
994 }
995
996 /*
997 * Header and footer...
998 */
999
1000 x = 0.5 * print_width;
1001
1002 if (banner->header)
1003 {
1004 printf("%.1f %.1f moveto", x, print_height - 2 * fontsize);
1005 psTextUTF8(fonts, 2 * fontsize, PS_BOLD, PS_CENTER, banner->header);
1006 }
1007
1008 if (banner->footer)
1009 {
1010 printf("%.1f %.1f moveto", x, fontsize);
1011 psTextUTF8(fonts, 2 * fontsize, PS_BOLD, PS_CENTER, banner->footer);
1012 }
1013
1014 /*
1015 * Show the page...
1016 */
1017
1018 puts("grestore");
1019 puts("showpage");
1020 }
1021
1022 return (num_pages);
1023 }
1024
1025
1026 /*
1027 * 'write_epilogue()' - Write the PostScript file epilogue.
1028 */
1029
1030 static void
1031 write_epilogue(int num_pages) /* I - Number of pages */
1032 {
1033 puts("%%Trailer");
1034 printf("%%%%Pages: %d\n", num_pages);
1035 puts("%%EOF");
1036 }
1037
1038
1039 /*
1040 * 'write_prolog()' - Write the PostScript file prolog with options.
1041 */
1042
1043 ps_text_t * /* O - Fonts */
1044 write_prolog(const char *title, /* I - Title of job */
1045 const char *username) /* I - Username */
1046 {
1047 time_t curtime; /* Current time */
1048 struct tm *curtm; /* Current date */
1049 char curdate[255]; /* Current date (text format) */
1050 ps_text_t *fonts; /* Fonts */
1051
1052
1053 /*
1054 * Get the fonts we'll need...
1055 */
1056
1057 fonts = psTextInitialize();
1058
1059 /*
1060 * Output the DSC header...
1061 */
1062
1063 curtime = time(NULL);
1064 curtm = localtime(&curtime);
1065 strftime(curdate, sizeof(curdate), "%c", curtm);
1066
1067 puts("%!PS-Adobe-3.0");
1068 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", PageLeft, PageBottom,
1069 PageRight, PageTop);
1070 printf("%%cupsRotation: %d\n", (Orientation & 3) * 90);
1071 puts("%%Creator: bannertops/" CUPS_SVERSION);
1072 printf("%%%%CreationDate: %s\n", curdate);
1073 puts("%%LanguageLevel: 2");
1074 puts("%%DocumentData: Clean7Bit");
1075 WriteTextComment("Title", title);
1076 WriteTextComment("For", username);
1077 printf("%%%%Pages: %d\n", Duplex ? 2 : 1);
1078 psTextListFonts(fonts);
1079 puts("%%EndComments");
1080 puts("%%BeginProlog");
1081 psTextEmbedFonts(fonts);
1082 puts("%%EndProlog");
1083
1084 return (fonts);
1085 }
1086
1087
1088 /*
1089 * End of "$Id$".
1090 */