]>
Commit | Line | Data |
---|---|---|
516ba4c9 | 1 | /* |
c9d3f842 | 2 | * "$Id$" |
516ba4c9 | 3 | * |
ed19bd98 | 4 | * Image file to PostScript filter for the Common UNIX Printing System (CUPS). |
516ba4c9 | 5 | * |
4e8d321f | 6 | * Copyright 2007 by Apple Inc. |
59b77f42 | 7 | * Copyright 1993-2007 by Easy Software Products. |
516ba4c9 | 8 | * |
ed19bd98 | 9 | * These coded instructions, statements, and computer programs are the |
4e8d321f | 10 | * property of Apple Inc. and are protected by Federal copyright |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
12 | * which should have been included with this file. If this file is | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
d850a9e1 | 14 | * |
dab1a4d8 | 15 | * This file is subject to the Apple OS-Developed Software exception. |
16 | * | |
ed19bd98 | 17 | * Contents: |
516ba4c9 | 18 | * |
ed19bd98 | 19 | * main() - Main entry... |
20 | * ps_hex() - Print binary data as a series of hexadecimal numbers. | |
21 | * ps_ascii85() - Print binary data as a series of base-85 numbers. | |
516ba4c9 | 22 | */ |
23 | ||
24 | /* | |
25 | * Include necessary headers... | |
26 | */ | |
27 | ||
ed19bd98 | 28 | #include "common.h" |
516ba4c9 | 29 | #include "image.h" |
ed19bd98 | 30 | #include <math.h> |
59b77f42 | 31 | #include <cups/i18n.h> |
516ba4c9 | 32 | |
33 | ||
34 | /* | |
35 | * Globals... | |
36 | */ | |
37 | ||
ed19bd98 | 38 | int Flip = 0, /* Flip/mirror pages */ |
c5da5726 | 39 | XPosition = 0, /* Horizontal position on page */ |
40 | YPosition = 0, /* Vertical position on page */ | |
ed19bd98 | 41 | Collate = 0, /* Collate copies? */ |
42 | Copies = 1; /* Number of copies */ | |
516ba4c9 | 43 | |
44 | ||
45 | /* | |
46 | * Local functions... | |
47 | */ | |
48 | ||
d4d4d3f5 | 49 | static void ps_hex(cups_ib_t *, int, int); |
50 | static void ps_ascii85(cups_ib_t *, int, int); | |
516ba4c9 | 51 | |
52 | ||
53 | /* | |
54 | * 'main()' - Main entry... | |
55 | */ | |
56 | ||
d4d4d3f5 | 57 | int /* O - Exit status */ |
58 | main(int argc, /* I - Number of command-line arguments */ | |
59 | char *argv[]) /* I - Command-line arguments */ | |
516ba4c9 | 60 | { |
d4d4d3f5 | 61 | cups_image_t *img; /* Image to print */ |
62 | float xprint, /* Printable area */ | |
ed19bd98 | 63 | yprint, |
d4d4d3f5 | 64 | xinches, /* Total size in inches */ |
ed19bd98 | 65 | yinches; |
d4d4d3f5 | 66 | float xsize, /* Total size in points */ |
48c4ffef | 67 | ysize, |
68 | xsize2, | |
69 | ysize2; | |
d4d4d3f5 | 70 | float aspect; /* Aspect ratio */ |
71 | int xpages, /* # x pages */ | |
72 | ypages, /* # y pages */ | |
73 | xpage, /* Current x page */ | |
74 | ypage, /* Current y page */ | |
75 | page; /* Current page number */ | |
b3276fab | 76 | int xc0, yc0, /* Corners of the page in image coords */ |
77 | xc1, yc1; | |
78 | cups_ib_t *row; /* Current row */ | |
d4d4d3f5 | 79 | int y; /* Current Y coordinate in image */ |
80 | int colorspace; /* Output colorspace */ | |
81 | int out_offset, /* Offset into output buffer */ | |
82 | out_length; /* Length of output buffer */ | |
83 | ppd_file_t *ppd; /* PPD file */ | |
84 | ppd_choice_t *choice; /* PPD option choice */ | |
85 | int num_options; /* Number of print options */ | |
86 | cups_option_t *options; /* Print options */ | |
87 | const char *val; /* Option value */ | |
88 | int slowcollate; /* Collate copies the slow way */ | |
89 | float g; /* Gamma correction value */ | |
90 | float b; /* Brightness factor */ | |
91 | float zoom; /* Zoom facter */ | |
92 | int xppi, yppi; /* Pixels-per-inch */ | |
93 | int hue, sat; /* Hue and saturation adjustment */ | |
b3276fab | 94 | int realcopies, /* Real copies being printed */ |
95 | emit_jcl; /* Emit JCL? */ | |
d4d4d3f5 | 96 | float left, top; /* Left and top of image */ |
97 | char filename[1024]; /* Name of file to print */ | |
98 | time_t curtime; /* Current time */ | |
99 | struct tm *curtm; /* Current date */ | |
100 | char curdate[255]; /* Current date string */ | |
ed19bd98 | 101 | |
102 | ||
7ce9aa3d | 103 | /* |
cc787dc1 | 104 | * Make sure status messages are not buffered... |
105 | */ | |
106 | ||
107 | setbuf(stderr, NULL); | |
108 | ||
109 | /* | |
110 | * Check command-line... | |
7ce9aa3d | 111 | */ |
112 | ||
113 | if (argc < 6 || argc > 7) | |
ed19bd98 | 114 | { |
59b77f42 | 115 | fprintf(stderr, _("Usage: %s job-id user title copies options [file]\n"), |
116 | argv[0]); | |
ed19bd98 | 117 | return (1); |
118 | } | |
516ba4c9 | 119 | |
7ce9aa3d | 120 | /* |
121 | * Copy stdin as needed... | |
122 | */ | |
123 | ||
124 | if (argc == 6) | |
125 | { | |
1b5bf964 | 126 | int fd; /* File to write to */ |
7ce9aa3d | 127 | char buffer[8192]; /* Buffer to read into */ |
128 | int bytes; /* # of bytes to read */ | |
129 | ||
130 | ||
1b5bf964 | 131 | if ((fd = cupsTempFd(filename, sizeof(filename))) < 0) |
7ce9aa3d | 132 | { |
133 | perror("ERROR: Unable to copy image file"); | |
134 | return (1); | |
135 | } | |
136 | ||
1b5bf964 | 137 | fprintf(stderr, "DEBUG: imagetoraster - copying to temp print file \"%s\"\n", |
7ce9aa3d | 138 | filename); |
139 | ||
140 | while ((bytes = fread(buffer, 1, sizeof(buffer), stdin)) > 0) | |
1b5bf964 | 141 | write(fd, buffer, bytes); |
142 | ||
143 | close(fd); | |
7ce9aa3d | 144 | } |
145 | else | |
def978d5 | 146 | strlcpy(filename, argv[6], sizeof(filename)); |
7ce9aa3d | 147 | |
516ba4c9 | 148 | /* |
ed19bd98 | 149 | * Process command-line options and write the prolog... |
516ba4c9 | 150 | */ |
151 | ||
ed19bd98 | 152 | zoom = 0.0; |
332197a2 | 153 | xppi = 0; |
154 | yppi = 0; | |
ed19bd98 | 155 | hue = 0; |
156 | sat = 100; | |
f0d2f1a4 | 157 | g = 1.0; |
ed19bd98 | 158 | b = 1.0; |
516ba4c9 | 159 | |
ae818f4d | 160 | Copies = atoi(argv[4]); |
161 | ||
ed19bd98 | 162 | options = NULL; |
163 | num_options = cupsParseOptions(argv[5], 0, &options); | |
516ba4c9 | 164 | |
de8a4f54 | 165 | ppd = SetCommonOptions(num_options, options, 0); |
516ba4c9 | 166 | |
ed19bd98 | 167 | if ((val = cupsGetOption("multiple-document-handling", num_options, options)) != NULL) |
516ba4c9 | 168 | { |
ed19bd98 | 169 | /* |
170 | * This IPP attribute is unnecessarily complicated... | |
171 | * | |
172 | * single-document, separate-documents-collated-copies, and | |
173 | * single-document-new-sheet all require collated copies. | |
174 | * | |
3e41e580 | 175 | * separate-documents-uncollated-copies allows for uncollated copies. |
ed19bd98 | 176 | */ |
516ba4c9 | 177 | |
3e41e580 | 178 | Collate = strcasecmp(val, "separate-documents-uncollated-copies") != 0; |
ed19bd98 | 179 | } |
516ba4c9 | 180 | |
ed19bd98 | 181 | if ((val = cupsGetOption("Collate", num_options, options)) != NULL && |
a7b3e92e | 182 | strcasecmp(val, "True") == 0) |
ed19bd98 | 183 | Collate = 1; |
516ba4c9 | 184 | |
ed19bd98 | 185 | if ((val = cupsGetOption("gamma", num_options, options)) != NULL) |
b3276fab | 186 | { |
187 | /* | |
188 | * Get gamma value from 1 to 10000... | |
189 | */ | |
190 | ||
ed19bd98 | 191 | g = atoi(val) * 0.001f; |
516ba4c9 | 192 | |
b3276fab | 193 | if (g < 0.001f) |
194 | g = 0.001f; | |
195 | else if (g > 10.0f) | |
196 | g = 10.0f; | |
197 | } | |
198 | ||
ed19bd98 | 199 | if ((val = cupsGetOption("brightness", num_options, options)) != NULL) |
b3276fab | 200 | { |
201 | /* | |
202 | * Get brightness value from 10 to 1000. | |
203 | */ | |
204 | ||
ed19bd98 | 205 | b = atoi(val) * 0.01f; |
516ba4c9 | 206 | |
b3276fab | 207 | if (b < 0.1f) |
208 | b = 0.1f; | |
209 | else if (b > 10.0f) | |
210 | b = 10.0f; | |
211 | } | |
212 | ||
ed19bd98 | 213 | if ((val = cupsGetOption("scaling", num_options, options)) != NULL) |
214 | zoom = atoi(val) * 0.01; | |
766ef83d | 215 | else if (cupsGetOption("fitplot", num_options, options)) |
216 | zoom = 1.0; | |
516ba4c9 | 217 | |
ed19bd98 | 218 | if ((val = cupsGetOption("ppi", num_options, options)) != NULL) |
332197a2 | 219 | if (sscanf(val, "%dx%d", &xppi, &yppi) < 2) |
220 | yppi = xppi; | |
516ba4c9 | 221 | |
c5da5726 | 222 | if ((val = cupsGetOption("position", num_options, options)) != NULL) |
223 | { | |
224 | if (strcasecmp(val, "center") == 0) | |
225 | { | |
226 | XPosition = 0; | |
227 | YPosition = 0; | |
228 | } | |
229 | else if (strcasecmp(val, "top") == 0) | |
230 | { | |
231 | XPosition = 0; | |
232 | YPosition = 1; | |
233 | } | |
234 | else if (strcasecmp(val, "left") == 0) | |
235 | { | |
236 | XPosition = -1; | |
237 | YPosition = 0; | |
238 | } | |
239 | else if (strcasecmp(val, "right") == 0) | |
240 | { | |
241 | XPosition = 1; | |
242 | YPosition = 0; | |
243 | } | |
244 | else if (strcasecmp(val, "top-left") == 0) | |
245 | { | |
246 | XPosition = -1; | |
247 | YPosition = 1; | |
248 | } | |
249 | else if (strcasecmp(val, "top-right") == 0) | |
250 | { | |
251 | XPosition = 1; | |
252 | YPosition = 1; | |
253 | } | |
254 | else if (strcasecmp(val, "bottom") == 0) | |
255 | { | |
256 | XPosition = 0; | |
257 | YPosition = -1; | |
258 | } | |
259 | else if (strcasecmp(val, "bottom-left") == 0) | |
260 | { | |
261 | XPosition = -1; | |
262 | YPosition = -1; | |
263 | } | |
264 | else if (strcasecmp(val, "bottom-right") == 0) | |
265 | { | |
266 | XPosition = 1; | |
267 | YPosition = -1; | |
268 | } | |
269 | } | |
270 | ||
ed19bd98 | 271 | if ((val = cupsGetOption("saturation", num_options, options)) != NULL) |
272 | sat = atoi(val); | |
273 | ||
274 | if ((val = cupsGetOption("hue", num_options, options)) != NULL) | |
275 | hue = atoi(val); | |
516ba4c9 | 276 | |
78b946b5 | 277 | if ((choice = ppdFindMarkedChoice(ppd, "MirrorPrint")) != NULL) |
278 | { | |
279 | val = choice->choice; | |
280 | choice->marked = 0; | |
281 | } | |
282 | else | |
283 | val = cupsGetOption("mirror", num_options, options); | |
284 | ||
285 | if (val && (!strcasecmp(val, "true") || !strcasecmp(val, "on") || | |
286 | !strcasecmp(val, "yes"))) | |
a05e4307 | 287 | Flip = 1; |
288 | ||
b3276fab | 289 | if ((val = cupsGetOption("emit-jcl", num_options, options)) != NULL && |
290 | (!strcasecmp(val, "false") || !strcasecmp(val, "off") || | |
291 | !strcasecmp(val, "no") || !strcmp(val, "0"))) | |
292 | emit_jcl = 0; | |
293 | else | |
294 | emit_jcl = 1; | |
295 | ||
516ba4c9 | 296 | /* |
297 | * Open the input image to print... | |
298 | */ | |
299 | ||
d4d4d3f5 | 300 | colorspace = ColorDevice ? CUPS_IMAGE_RGB_CMYK : CUPS_IMAGE_WHITE; |
516ba4c9 | 301 | |
d4d4d3f5 | 302 | img = cupsImageOpen(filename, colorspace, CUPS_IMAGE_WHITE, sat, hue, NULL); |
7ce9aa3d | 303 | |
304 | if (argc == 6) | |
305 | unlink(filename); | |
306 | ||
307 | if (img == NULL) | |
ed19bd98 | 308 | { |
59b77f42 | 309 | fputs(_("ERROR: Unable to open image file for printing!\n"), stderr); |
ed19bd98 | 310 | ppdClose(ppd); |
311 | return (1); | |
312 | } | |
516ba4c9 | 313 | |
d4d4d3f5 | 314 | colorspace = cupsImageGetColorSpace(img); |
516ba4c9 | 315 | |
316 | /* | |
317 | * Scale as necessary... | |
318 | */ | |
319 | ||
332197a2 | 320 | if (zoom == 0.0 && xppi == 0) |
321 | { | |
d4d4d3f5 | 322 | xppi = cupsImageGetXPPI(img); |
323 | yppi = cupsImageGetYPPI(img); | |
332197a2 | 324 | } |
325 | ||
326 | if (yppi == 0) | |
327 | yppi = xppi; | |
328 | ||
34089ee8 | 329 | fprintf(stderr, "DEBUG: Before scaling: xppi=%d, yppi=%d, zoom=%.2f\n", |
330 | xppi, yppi, zoom); | |
e6d607f1 | 331 | |
332197a2 | 332 | if (xppi > 0) |
516ba4c9 | 333 | { |
334 | /* | |
335 | * Scale the image as neccesary to match the desired pixels-per-inch. | |
336 | */ | |
337 | ||
95a58aca | 338 | if (Orientation & 1) |
339 | { | |
340 | xprint = (PageTop - PageBottom) / 72.0; | |
341 | yprint = (PageRight - PageLeft) / 72.0; | |
342 | } | |
343 | else | |
344 | { | |
345 | xprint = (PageRight - PageLeft) / 72.0; | |
346 | yprint = (PageTop - PageBottom) / 72.0; | |
347 | } | |
348 | ||
349 | fprintf(stderr, "DEBUG: Before scaling: xprint=%.1f, yprint=%.1f\n", | |
350 | xprint, yprint); | |
351 | ||
d4d4d3f5 | 352 | xinches = (float)cupsImageGetWidth(img) / (float)xppi; |
353 | yinches = (float)cupsImageGetHeight(img) / (float)yppi; | |
48c4ffef | 354 | |
e6d607f1 | 355 | fprintf(stderr, "DEBUG: Image size is %.1f x %.1f inches...\n", |
356 | xinches, yinches); | |
357 | ||
753453e4 | 358 | if ((val = cupsGetOption("natural-scaling", num_options, options)) != NULL) |
359 | { | |
360 | xinches = xinches * atoi(val) / 100; | |
361 | yinches = yinches * atoi(val) / 100; | |
362 | } | |
363 | ||
a28d12c5 | 364 | if (cupsGetOption("orientation-requested", num_options, options) == NULL && |
b16dd9cb | 365 | cupsGetOption("landscape", num_options, options) == NULL) |
48c4ffef | 366 | { |
367 | /* | |
b16dd9cb | 368 | * Rotate the image if it will fit landscape but not portrait... |
48c4ffef | 369 | */ |
370 | ||
e6d607f1 | 371 | fputs("DEBUG: Auto orientation...\n", stderr); |
372 | ||
b16dd9cb | 373 | if ((xinches > xprint || yinches > yprint) && |
374 | xinches <= yprint && yinches <= xprint) | |
375 | { | |
376 | /* | |
377 | * Rotate the image as needed... | |
378 | */ | |
379 | ||
e6d607f1 | 380 | fputs("DEBUG: Using landscape orientation...\n", stderr); |
381 | ||
b16dd9cb | 382 | Orientation = (Orientation + 1) & 3; |
383 | xsize = yprint; | |
384 | yprint = xprint; | |
385 | xprint = xsize; | |
b16dd9cb | 386 | } |
48c4ffef | 387 | } |
516ba4c9 | 388 | } |
389 | else | |
390 | { | |
391 | /* | |
392 | * Scale percentage of page size... | |
393 | */ | |
394 | ||
95a58aca | 395 | xprint = (PageRight - PageLeft) / 72.0; |
396 | yprint = (PageTop - PageBottom) / 72.0; | |
d4d4d3f5 | 397 | aspect = (float)cupsImageGetYPPI(img) / (float)cupsImageGetXPPI(img); |
332197a2 | 398 | |
95a58aca | 399 | fprintf(stderr, "DEBUG: Before scaling: xprint=%.1f, yprint=%.1f\n", |
400 | xprint, yprint); | |
401 | ||
d4d4d3f5 | 402 | fprintf(stderr, "DEBUG: cupsImageGetXPPI(img) = %d, cupsImageGetYPPI(img) = %d, aspect = %f\n", |
403 | cupsImageGetXPPI(img), cupsImageGetYPPI(img), aspect); | |
332197a2 | 404 | |
ed19bd98 | 405 | xsize = xprint * zoom; |
d4d4d3f5 | 406 | ysize = xsize * cupsImageGetHeight(img) / cupsImageGetWidth(img) / aspect; |
0169953d | 407 | |
ed19bd98 | 408 | if (ysize > (yprint * zoom)) |
516ba4c9 | 409 | { |
ed19bd98 | 410 | ysize = yprint * zoom; |
d4d4d3f5 | 411 | xsize = ysize * cupsImageGetWidth(img) * aspect / cupsImageGetHeight(img); |
516ba4c9 | 412 | } |
0169953d | 413 | |
48c4ffef | 414 | xsize2 = yprint * zoom; |
d4d4d3f5 | 415 | ysize2 = xsize2 * cupsImageGetHeight(img) / cupsImageGetWidth(img) / aspect; |
48c4ffef | 416 | |
417 | if (ysize2 > (xprint * zoom)) | |
418 | { | |
419 | ysize2 = xprint * zoom; | |
d4d4d3f5 | 420 | xsize2 = ysize2 * cupsImageGetWidth(img) * aspect / cupsImageGetHeight(img); |
48c4ffef | 421 | } |
422 | ||
e6d607f1 | 423 | fprintf(stderr, "DEBUG: Portrait size is %.2f x %.2f inches\n", xsize, ysize); |
424 | fprintf(stderr, "DEBUG: Landscape size is %.2f x %.2f inches\n", xsize2, ysize2); | |
332197a2 | 425 | |
a28d12c5 | 426 | if (cupsGetOption("orientation-requested", num_options, options) == NULL && |
b16dd9cb | 427 | cupsGetOption("landscape", num_options, options) == NULL) |
48c4ffef | 428 | { |
429 | /* | |
b16dd9cb | 430 | * Choose the rotation with the largest area, but prefer |
431 | * portrait if they are equal... | |
48c4ffef | 432 | */ |
433 | ||
e6d607f1 | 434 | fputs("DEBUG: Auto orientation...\n", stderr); |
435 | ||
b16dd9cb | 436 | if ((xsize * ysize) < (xsize2 * xsize2)) |
437 | { | |
438 | /* | |
439 | * Do landscape orientation... | |
440 | */ | |
441 | ||
e6d607f1 | 442 | fputs("DEBUG: Using landscape orientation...\n", stderr); |
443 | ||
b16dd9cb | 444 | Orientation = 1; |
445 | xinches = xsize2; | |
446 | yinches = ysize2; | |
447 | xprint = (PageTop - PageBottom) / 72.0; | |
448 | yprint = (PageRight - PageLeft) / 72.0; | |
b16dd9cb | 449 | } |
450 | else | |
451 | { | |
452 | /* | |
453 | * Do portrait orientation... | |
454 | */ | |
455 | ||
e6d607f1 | 456 | fputs("DEBUG: Using portrait orientation...\n", stderr); |
457 | ||
b16dd9cb | 458 | Orientation = 0; |
459 | xinches = xsize; | |
460 | yinches = ysize; | |
461 | } | |
462 | } | |
463 | else if (Orientation & 1) | |
464 | { | |
e6d607f1 | 465 | fputs("DEBUG: Using landscape orientation...\n", stderr); |
466 | ||
48c4ffef | 467 | xinches = xsize2; |
468 | yinches = ysize2; | |
469 | xprint = (PageTop - PageBottom) / 72.0; | |
470 | yprint = (PageRight - PageLeft) / 72.0; | |
48c4ffef | 471 | } |
b6ea8f29 | 472 | else |
473 | { | |
e6d607f1 | 474 | fputs("DEBUG: Using portrait orientation...\n", stderr); |
475 | ||
b6ea8f29 | 476 | xinches = xsize; |
477 | yinches = ysize; | |
478 | xprint = (PageRight - PageLeft) / 72.0; | |
479 | yprint = (PageTop - PageBottom) / 72.0; | |
480 | } | |
ed19bd98 | 481 | } |
516ba4c9 | 482 | |
b5cb0608 | 483 | /* |
484 | * Compute the number of pages to print and the size of the image on each | |
485 | * page... | |
486 | */ | |
487 | ||
ed19bd98 | 488 | xpages = ceil(xinches / xprint); |
489 | ypages = ceil(yinches / yprint); | |
0169953d | 490 | |
b5cb0608 | 491 | xprint = xinches / xpages; |
492 | yprint = yinches / ypages; | |
493 | ||
95a58aca | 494 | fprintf(stderr, "DEBUG: xpages = %dx%.2fin, ypages = %dx%.2fin\n", |
495 | xpages, xprint, ypages, yprint); | |
496 | ||
b5cb0608 | 497 | /* |
498 | * Update the page size for custom sizes... | |
499 | */ | |
500 | ||
501 | if ((choice = ppdFindMarkedChoice(ppd, "PageSize")) != NULL && | |
502 | strcasecmp(choice->choice, "Custom") == 0) | |
503 | { | |
504 | float width, /* New width in points */ | |
505 | length; /* New length in points */ | |
506 | char s[255]; /* New custom page size... */ | |
507 | ||
508 | ||
df585e0d | 509 | /* |
510 | * Use the correct width and length for the current orientation... | |
511 | */ | |
512 | ||
513 | if (Orientation & 1) | |
514 | { | |
515 | width = yprint * 72.0; | |
516 | length = xprint * 72.0; | |
517 | } | |
518 | else | |
519 | { | |
520 | width = xprint * 72.0; | |
521 | length = yprint * 72.0; | |
522 | } | |
523 | ||
b5cb0608 | 524 | /* |
525 | * Add margins to page size... | |
526 | */ | |
527 | ||
df585e0d | 528 | width += ppd->custom_margins[0] + ppd->custom_margins[2]; |
529 | length += ppd->custom_margins[1] + ppd->custom_margins[3]; | |
b5cb0608 | 530 | |
531 | /* | |
532 | * Enforce minimums... | |
533 | */ | |
534 | ||
535 | if (width < ppd->custom_min[0]) | |
536 | width = ppd->custom_min[0]; | |
537 | ||
538 | if (length < ppd->custom_min[1]) | |
539 | length = ppd->custom_min[1]; | |
540 | ||
95a58aca | 541 | fprintf(stderr, "DEBUG: Updated custom page size to %.2f x %.2f inches...\n", |
542 | width / 72.0, length / 72.0); | |
543 | ||
b5cb0608 | 544 | /* |
545 | * Set the new custom size... | |
546 | */ | |
547 | ||
548 | sprintf(s, "Custom.%.0fx%.0f", width, length); | |
549 | ppdMarkOption(ppd, "PageSize", s); | |
550 | ||
551 | /* | |
552 | * Update page variables... | |
553 | */ | |
554 | ||
555 | PageWidth = width; | |
556 | PageLength = length; | |
557 | PageLeft = ppd->custom_margins[0]; | |
558 | PageRight = width - ppd->custom_margins[2]; | |
559 | PageBottom = ppd->custom_margins[1]; | |
560 | PageTop = length - ppd->custom_margins[3]; | |
b5cb0608 | 561 | } |
562 | ||
ed19bd98 | 563 | /* |
564 | * See if we need to collate, and if so how we need to do it... | |
565 | */ | |
516ba4c9 | 566 | |
ed19bd98 | 567 | if (xpages == 1 && ypages == 1) |
568 | Collate = 0; | |
516ba4c9 | 569 | |
ed19bd98 | 570 | slowcollate = Collate && ppdFindOption(ppd, "Collate") == NULL; |
516ba4c9 | 571 | |
b5cb0608 | 572 | if (Copies > 1 && !slowcollate) |
573 | { | |
574 | realcopies = Copies; | |
575 | Copies = 1; | |
576 | } | |
577 | else | |
578 | realcopies = 1; | |
579 | ||
ed19bd98 | 580 | /* |
581 | * Write any "exit server" options that have been selected... | |
582 | */ | |
516ba4c9 | 583 | |
ed19bd98 | 584 | ppdEmit(ppd, stdout, PPD_ORDER_EXIT); |
516ba4c9 | 585 | |
ed19bd98 | 586 | /* |
587 | * Write any JCL commands that are needed to print PostScript code... | |
588 | */ | |
589 | ||
b3276fab | 590 | if (emit_jcl) |
591 | ppdEmitJCL(ppd, stdout, atoi(argv[1]), argv[2], argv[3]); | |
516ba4c9 | 592 | |
593 | /* | |
ed19bd98 | 594 | * Start sending the document with any commands needed... |
516ba4c9 | 595 | */ |
596 | ||
b5cb0608 | 597 | curtime = time(NULL); |
598 | curtm = localtime(&curtime); | |
599 | ||
600 | puts("%!PS-Adobe-3.0"); | |
601 | printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", PageLeft, PageBottom, | |
602 | PageRight, PageTop); | |
603 | printf("%%%%LanguageLevel: %d\n", LanguageLevel); | |
604 | printf("%%%%Pages: %d\n", xpages * ypages * Copies); | |
605 | puts("%%DocumentData: Clean7Bit"); | |
606 | puts("%%DocumentNeededResources: font Helvetica-Bold"); | |
607 | puts("%%Creator: imagetops/" CUPS_SVERSION); | |
03f61bf3 | 608 | strftime(curdate, sizeof(curdate), "%c", curtm); |
b5cb0608 | 609 | printf("%%%%CreationDate: %s\n", curdate); |
13ae7ecd | 610 | WriteTextComment("Title", argv[3]); |
611 | WriteTextComment("For", argv[2]); | |
b5cb0608 | 612 | if (Orientation & 1) |
613 | puts("%%Orientation: Landscape"); | |
904eef76 | 614 | else |
615 | puts("%%Orientation: Portrait"); | |
b5cb0608 | 616 | puts("%%EndComments"); |
617 | puts("%%BeginProlog"); | |
ed19bd98 | 618 | |
37b19c12 | 619 | if (ppd != NULL && ppd->patches != NULL) |
620 | puts(ppd->patches); | |
621 | ||
ed19bd98 | 622 | ppdEmit(ppd, stdout, PPD_ORDER_DOCUMENT); |
623 | ppdEmit(ppd, stdout, PPD_ORDER_ANY); | |
624 | ppdEmit(ppd, stdout, PPD_ORDER_PROLOG); | |
516ba4c9 | 625 | |
ed19bd98 | 626 | if (g != 1.0 || b != 1.0) |
37b19c12 | 627 | printf("{ neg 1 add dup 0 lt { pop 1 } { %.3f exp neg 1 add } " |
628 | "ifelse %.3f mul } bind settransfer\n", g, b); | |
ed19bd98 | 629 | |
de8a4f54 | 630 | WriteCommon(); |
df585e0d | 631 | switch (Orientation) |
632 | { | |
633 | case 0 : | |
634 | WriteLabelProlog(cupsGetOption("page-label", num_options, options), | |
635 | PageBottom, PageTop, PageWidth); | |
636 | break; | |
637 | ||
638 | case 1 : | |
639 | WriteLabelProlog(cupsGetOption("page-label", num_options, options), | |
640 | PageLeft, PageRight, PageLength); | |
641 | break; | |
642 | ||
643 | case 2 : | |
644 | WriteLabelProlog(cupsGetOption("page-label", num_options, options), | |
645 | PageLength - PageTop, PageLength - PageBottom, | |
646 | PageWidth); | |
647 | break; | |
648 | ||
649 | case 3 : | |
650 | WriteLabelProlog(cupsGetOption("page-label", num_options, options), | |
651 | PageWidth - PageRight, PageWidth - PageLeft, | |
652 | PageLength); | |
653 | break; | |
654 | } | |
d11458ff | 655 | |
b5cb0608 | 656 | if (realcopies > 1) |
516ba4c9 | 657 | { |
83f08393 | 658 | if (ppd == NULL || ppd->language_level == 1) |
b5cb0608 | 659 | printf("/#copies %d def\n", realcopies); |
83f08393 | 660 | else |
b5cb0608 | 661 | printf("<</NumCopies %d>>setpagedevice\n", realcopies); |
ed19bd98 | 662 | } |
b5cb0608 | 663 | |
664 | puts("%%EndProlog"); | |
516ba4c9 | 665 | |
666 | /* | |
667 | * Output the pages... | |
668 | */ | |
669 | ||
d4d4d3f5 | 670 | row = malloc(cupsImageGetWidth(img) * abs(colorspace) + 3); |
0169953d | 671 | |
de8a4f54 | 672 | fprintf(stderr, "DEBUG: XPosition=%d, YPosition=%d, Orientation=%d\n", |
673 | XPosition, YPosition, Orientation); | |
674 | fprintf(stderr, "DEBUG: xprint=%.0f, yprint=%.0f\n", xprint, yprint); | |
675 | fprintf(stderr, "DEBUG: PageLeft=%.0f, PageRight=%.0f, PageWidth=%.0f\n", | |
676 | PageLeft, PageRight, PageWidth); | |
677 | fprintf(stderr, "DEBUG: PageBottom=%.0f, PageTop=%.0f, PageLength=%.0f\n", | |
678 | PageBottom, PageTop, PageLength); | |
679 | ||
df585e0d | 680 | switch (Orientation) |
681 | { | |
4a0fc5ef | 682 | default : |
df585e0d | 683 | switch (XPosition) |
684 | { | |
685 | case -1 : | |
686 | left = PageLeft; | |
687 | break; | |
688 | default : | |
689 | left = (PageRight + PageLeft - xprint * 72) / 2; | |
690 | break; | |
691 | case 1 : | |
692 | left = PageRight - xprint * 72; | |
693 | break; | |
694 | } | |
695 | ||
696 | switch (YPosition) | |
697 | { | |
698 | case -1 : | |
699 | top = PageBottom + yprint * 72; | |
700 | break; | |
701 | default : | |
702 | top = (PageTop + PageBottom + yprint * 72) / 2; | |
703 | break; | |
704 | case 1 : | |
705 | top = PageTop; | |
706 | break; | |
707 | } | |
708 | break; | |
709 | ||
710 | case 1 : | |
711 | switch (XPosition) | |
712 | { | |
713 | case -1 : | |
714 | left = PageBottom; | |
715 | break; | |
716 | default : | |
717 | left = (PageTop + PageBottom - xprint * 72) / 2; | |
718 | break; | |
719 | case 1 : | |
720 | left = PageTop - xprint * 72; | |
721 | break; | |
722 | } | |
723 | ||
724 | switch (YPosition) | |
725 | { | |
726 | case -1 : | |
727 | top = PageLeft + yprint * 72; | |
728 | break; | |
729 | default : | |
730 | top = (PageRight + PageLeft + yprint * 72) / 2; | |
731 | break; | |
732 | case 1 : | |
733 | top = PageRight; | |
734 | break; | |
735 | } | |
736 | break; | |
737 | ||
738 | case 2 : | |
739 | switch (XPosition) | |
740 | { | |
741 | case 1 : | |
742 | left = PageLeft; | |
743 | break; | |
744 | default : | |
745 | left = (PageRight + PageLeft - xprint * 72) / 2; | |
746 | break; | |
747 | case -1 : | |
748 | left = PageRight - xprint * 72; | |
749 | break; | |
750 | } | |
751 | ||
752 | switch (YPosition) | |
753 | { | |
754 | case 1 : | |
755 | top = PageBottom + yprint * 72; | |
756 | break; | |
757 | default : | |
758 | top = (PageTop + PageBottom + yprint * 72) / 2; | |
759 | break; | |
760 | case -1 : | |
761 | top = PageTop; | |
762 | break; | |
763 | } | |
764 | break; | |
765 | ||
766 | case 3 : | |
767 | switch (XPosition) | |
768 | { | |
769 | case 1 : | |
770 | left = PageBottom; | |
771 | break; | |
772 | default : | |
773 | left = (PageTop + PageBottom - xprint * 72) / 2; | |
774 | break; | |
775 | case -1 : | |
776 | left = PageTop - xprint * 72; | |
777 | break; | |
778 | } | |
779 | ||
780 | switch (YPosition) | |
781 | { | |
782 | case 1 : | |
783 | top = PageLeft + yprint * 72; | |
784 | break; | |
785 | default : | |
786 | top = (PageRight + PageLeft + yprint * 72) / 2; | |
787 | break; | |
788 | case -1 : | |
789 | top = PageRight; | |
790 | break; | |
791 | } | |
792 | break; | |
793 | } | |
794 | ||
795 | fprintf(stderr, "DEBUG: left=%.2f, top=%.2f\n", left, top); | |
796 | ||
ed19bd98 | 797 | for (page = 1; Copies > 0; Copies --) |
798 | for (xpage = 0; xpage < xpages; xpage ++) | |
799 | for (ypage = 0; ypage < ypages; ypage ++, page ++) | |
800 | { | |
b5cb0608 | 801 | if (ppd && ppd->num_filters == 0) |
802 | fprintf(stderr, "PAGE: %d %d\n", page, realcopies); | |
803 | ||
59b77f42 | 804 | fprintf(stderr, _("INFO: Printing page %d...\n"), page); |
b5cb0608 | 805 | |
806 | printf("%%%%Page: %d %d\n", page, page); | |
516ba4c9 | 807 | |
ed19bd98 | 808 | ppdEmit(ppd, stdout, PPD_ORDER_PAGE); |
516ba4c9 | 809 | |
ed19bd98 | 810 | puts("gsave"); |
516ba4c9 | 811 | |
ed19bd98 | 812 | if (Flip) |
813 | printf("%.0f 0 translate -1 1 scale\n", PageWidth); | |
814 | ||
815 | switch (Orientation) | |
816 | { | |
817 | case 1 : /* Landscape */ | |
df585e0d | 818 | printf("%.0f 0 translate 90 rotate\n", PageWidth); |
ed19bd98 | 819 | break; |
820 | case 2 : /* Reverse Portrait */ | |
821 | printf("%.0f %.0f translate 180 rotate\n", PageWidth, PageLength); | |
822 | break; | |
823 | case 3 : /* Reverse Landscape */ | |
df585e0d | 824 | printf("0 %.0f translate -90 rotate\n", PageLength); |
ed19bd98 | 825 | break; |
826 | } | |
516ba4c9 | 827 | |
df585e0d | 828 | puts("gsave"); |
829 | ||
b3276fab | 830 | xc0 = cupsImageGetWidth(img) * xpage / xpages; |
831 | xc1 = cupsImageGetWidth(img) * (xpage + 1) / xpages - 1; | |
832 | yc0 = cupsImageGetHeight(img) * ypage / ypages; | |
833 | yc1 = cupsImageGetHeight(img) * (ypage + 1) / ypages - 1; | |
516ba4c9 | 834 | |
c5da5726 | 835 | printf("%.1f %.1f translate\n", left, top); |
836 | ||
ed19bd98 | 837 | printf("%.3f %.3f scale\n\n", |
b3276fab | 838 | xprint * 72.0 / (xc1 - xc0 + 1), |
839 | yprint * 72.0 / (yc1 - yc0 + 1)); | |
516ba4c9 | 840 | |
ed19bd98 | 841 | if (LanguageLevel == 1) |
af526a52 | 842 | { |
b3276fab | 843 | printf("/picture %d string def\n", (xc1 - xc0 + 1) * abs(colorspace)); |
844 | printf("%d %d 8[1 0 0 -1 0 1]", (xc1 - xc0 + 1), (yc1 - yc0 + 1)); | |
ed19bd98 | 845 | |
d4d4d3f5 | 846 | if (colorspace == CUPS_IMAGE_WHITE) |
ed19bd98 | 847 | puts("{currentfile picture readhexstring pop} image"); |
af526a52 | 848 | else |
350807be | 849 | printf("{currentfile picture readhexstring pop} false %d colorimage\n", |
850 | abs(colorspace)); | |
ed19bd98 | 851 | |
b3276fab | 852 | for (y = yc0; y <= yc1; y ++) |
ed19bd98 | 853 | { |
b3276fab | 854 | cupsImageGetRow(img, xc0, y, xc1 - xc0 + 1, row); |
855 | ps_hex(row, (xc1 - xc0 + 1) * abs(colorspace), y == yc1); | |
ed19bd98 | 856 | } |
af526a52 | 857 | } |
858 | else | |
859 | { | |
350807be | 860 | switch (colorspace) |
861 | { | |
d4d4d3f5 | 862 | case CUPS_IMAGE_WHITE : |
350807be | 863 | puts("/DeviceGray setcolorspace"); |
864 | break; | |
d4d4d3f5 | 865 | case CUPS_IMAGE_RGB : |
350807be | 866 | puts("/DeviceRGB setcolorspace"); |
867 | break; | |
d4d4d3f5 | 868 | case CUPS_IMAGE_CMYK : |
350807be | 869 | puts("/DeviceCMYK setcolorspace"); |
870 | break; | |
871 | } | |
af526a52 | 872 | |
ed19bd98 | 873 | printf("<<" |
7536a9b5 | 874 | "/ImageType 1" |
ed19bd98 | 875 | "/Width %d" |
876 | "/Height %d" | |
877 | "/BitsPerComponent 8", | |
b3276fab | 878 | xc1 - xc0 + 1, yc1 - yc0 + 1); |
af526a52 | 879 | |
350807be | 880 | switch (colorspace) |
881 | { | |
d4d4d3f5 | 882 | case CUPS_IMAGE_WHITE : |
350807be | 883 | fputs("/Decode[0 1]", stdout); |
884 | break; | |
d4d4d3f5 | 885 | case CUPS_IMAGE_RGB : |
350807be | 886 | fputs("/Decode[0 1 0 1 0 1]", stdout); |
887 | break; | |
d4d4d3f5 | 888 | case CUPS_IMAGE_CMYK : |
350807be | 889 | fputs("/Decode[0 1 0 1 0 1 0 1]", stdout); |
890 | break; | |
891 | } | |
af526a52 | 892 | |
7536a9b5 | 893 | fputs("\n/DataSource currentfile/ASCII85Decode filter", stdout); |
af526a52 | 894 | |
b3276fab | 895 | if (((xc1 - xc0 + 1) / xprint) < 100.0) |
ed19bd98 | 896 | fputs("/Interpolate true", stdout); |
5398375f | 897 | |
7536a9b5 | 898 | puts("/ImageMatrix[1 0 0 -1 0 1]>>image"); |
af526a52 | 899 | |
b3276fab | 900 | for (y = yc0, out_offset = 0; y <= yc1; y ++) |
ed19bd98 | 901 | { |
b3276fab | 902 | cupsImageGetRow(img, xc0, y, xc1 - xc0 + 1, row + out_offset); |
af526a52 | 903 | |
b3276fab | 904 | out_length = (xc1 - xc0 + 1) * abs(colorspace) + out_offset; |
ed19bd98 | 905 | out_offset = out_length & 3; |
516ba4c9 | 906 | |
b3276fab | 907 | ps_ascii85(row, out_length, y == yc1); |
516ba4c9 | 908 | |
ed19bd98 | 909 | if (out_offset > 0) |
910 | memcpy(row, row + out_length - out_offset, out_offset); | |
911 | } | |
912 | } | |
516ba4c9 | 913 | |
ed19bd98 | 914 | puts("grestore"); |
df585e0d | 915 | WriteLabels(0); |
916 | puts("grestore"); | |
ed19bd98 | 917 | puts("showpage"); |
918 | } | |
516ba4c9 | 919 | |
b5cb0608 | 920 | puts("%%EOF"); |
921 | ||
ed19bd98 | 922 | /* |
923 | * End the job with the appropriate JCL command or CTRL-D otherwise. | |
924 | */ | |
516ba4c9 | 925 | |
b3276fab | 926 | if (emit_jcl) |
927 | { | |
928 | if (ppd && ppd->jcl_end) | |
929 | ppdEmitJCLEnd(ppd, stdout); | |
930 | else | |
931 | putchar(0x04); | |
932 | } | |
516ba4c9 | 933 | |
ed19bd98 | 934 | /* |
935 | * Close files... | |
936 | */ | |
516ba4c9 | 937 | |
d4d4d3f5 | 938 | cupsImageClose(img); |
ed19bd98 | 939 | ppdClose(ppd); |
516ba4c9 | 940 | |
ed19bd98 | 941 | return (0); |
516ba4c9 | 942 | } |
943 | ||
944 | ||
945 | /* | |
946 | * 'ps_hex()' - Print binary data as a series of hexadecimal numbers. | |
947 | */ | |
948 | ||
949 | static void | |
d4d4d3f5 | 950 | ps_hex(cups_ib_t *data, /* I - Data to print */ |
951 | int length, /* I - Number of bytes to print */ | |
952 | int last_line) /* I - Last line of raster data? */ | |
516ba4c9 | 953 | { |
d4d4d3f5 | 954 | static int col = 0; /* Current column */ |
516ba4c9 | 955 | static char *hex = "0123456789ABCDEF"; |
d4d4d3f5 | 956 | /* Hex digits */ |
516ba4c9 | 957 | |
958 | ||
959 | while (length > 0) | |
960 | { | |
961 | /* | |
ed19bd98 | 962 | * Put the hex chars out to the file; note that we don't use printf() |
516ba4c9 | 963 | * for speed reasons... |
964 | */ | |
965 | ||
ed19bd98 | 966 | putchar(hex[*data >> 4]); |
967 | putchar(hex[*data & 15]); | |
516ba4c9 | 968 | |
969 | data ++; | |
970 | length --; | |
af526a52 | 971 | |
b5cb0608 | 972 | col += 2; |
973 | if (col > 78) | |
974 | { | |
ed19bd98 | 975 | putchar('\n'); |
b5cb0608 | 976 | col = 0; |
977 | } | |
ed19bd98 | 978 | } |
516ba4c9 | 979 | |
b5cb0608 | 980 | if (last_line && col) |
981 | { | |
982 | putchar('\n'); | |
983 | col = 0; | |
984 | } | |
516ba4c9 | 985 | } |
986 | ||
987 | ||
988 | /* | |
989 | * 'ps_ascii85()' - Print binary data as a series of base-85 numbers. | |
990 | */ | |
991 | ||
992 | static void | |
d4d4d3f5 | 993 | ps_ascii85(cups_ib_t *data, /* I - Data to print */ |
994 | int length, /* I - Number of bytes to print */ | |
995 | int last_line) /* I - Last line of raster data? */ | |
516ba4c9 | 996 | { |
d4d4d3f5 | 997 | unsigned b; /* Binary data word */ |
998 | unsigned char c[5]; /* ASCII85 encoded chars */ | |
999 | static int col = 0; /* Current column */ | |
516ba4c9 | 1000 | |
1001 | ||
516ba4c9 | 1002 | while (length > 3) |
1003 | { | |
1004 | b = (((((data[0] << 8) | data[1]) << 8) | data[2]) << 8) | data[3]; | |
1005 | ||
1006 | if (b == 0) | |
b5cb0608 | 1007 | { |
ed19bd98 | 1008 | putchar('z'); |
b5cb0608 | 1009 | col ++; |
1010 | } | |
516ba4c9 | 1011 | else |
1012 | { | |
1013 | c[4] = (b % 85) + '!'; | |
1014 | b /= 85; | |
1015 | c[3] = (b % 85) + '!'; | |
1016 | b /= 85; | |
1017 | c[2] = (b % 85) + '!'; | |
1018 | b /= 85; | |
1019 | c[1] = (b % 85) + '!'; | |
1020 | b /= 85; | |
1021 | c[0] = b + '!'; | |
1022 | ||
ed19bd98 | 1023 | fwrite(c, 5, 1, stdout); |
b5cb0608 | 1024 | col += 5; |
ed19bd98 | 1025 | } |
516ba4c9 | 1026 | |
1027 | data += 4; | |
1028 | length -= 4; | |
b5cb0608 | 1029 | |
1030 | if (col >= 75) | |
1031 | { | |
1032 | putchar('\n'); | |
1033 | col = 0; | |
1034 | } | |
ed19bd98 | 1035 | } |
516ba4c9 | 1036 | |
1037 | if (last_line) | |
1038 | { | |
1039 | if (length > 0) | |
1040 | { | |
4364903a | 1041 | memset(data + length, 0, 4 - length); |
1042 | b = (((((data[0] << 8) | data[1]) << 8) | data[2]) << 8) | data[3]; | |
516ba4c9 | 1043 | |
1044 | c[4] = (b % 85) + '!'; | |
1045 | b /= 85; | |
1046 | c[3] = (b % 85) + '!'; | |
1047 | b /= 85; | |
1048 | c[2] = (b % 85) + '!'; | |
1049 | b /= 85; | |
1050 | c[1] = (b % 85) + '!'; | |
1051 | b /= 85; | |
1052 | c[0] = b + '!'; | |
1053 | ||
ed19bd98 | 1054 | fwrite(c, length + 1, 1, stdout); |
1055 | } | |
516ba4c9 | 1056 | |
ed19bd98 | 1057 | puts("~>"); |
b5cb0608 | 1058 | col = 0; |
516ba4c9 | 1059 | } |
516ba4c9 | 1060 | } |
1061 | ||
1062 | ||
1063 | /* | |
c9d3f842 | 1064 | * End of "$Id$". |
516ba4c9 | 1065 | */ |