]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
bc44d920 | 2 | * "$Id: emit.c 6649 2007-07-11 21:46:42Z mike $" |
ef416fc2 | 3 | * |
4 | * PPD code emission routines for the Common UNIX Printing System (CUPS). | |
5 | * | |
91c84a35 | 6 | * Copyright 2007-2008 by Apple Inc. |
b86bc4cf | 7 | * Copyright 1997-2007 by Easy Software Products, all rights reserved. |
ef416fc2 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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/". | |
ef416fc2 | 14 | * |
15 | * PostScript is a trademark of Adobe Systems, Inc. | |
16 | * | |
17 | * This file is subject to the Apple OS-Developed Software exception. | |
18 | * | |
19 | * Contents: | |
20 | * | |
fa73b229 | 21 | * ppdCollect() - Collect all marked options that reside in the |
22 | * specified section. | |
23 | * ppdCollect2() - Collect all marked options that reside in the | |
24 | * specified section and minimum order. | |
25 | * ppdEmit() - Emit code for marked options to a file. | |
757d2cad | 26 | * ppdEmitAfterOrder() - Emit a subset of the code for marked options to a |
27 | * file. | |
fa73b229 | 28 | * ppdEmitFd() - Emit code for marked options to a file. |
29 | * ppdEmitJCL() - Emit code for JCL options to a file. | |
30 | * ppdEmitJCLEnd() - Emit JCLEnd code to a file. | |
757d2cad | 31 | * ppdEmitString() - Get a string containing the code for marked options. |
fa73b229 | 32 | * ppd_handle_media() - Handle media selection... |
ef416fc2 | 33 | */ |
34 | ||
35 | /* | |
36 | * Include necessary headers... | |
37 | */ | |
38 | ||
39 | #include "ppd.h" | |
40 | #include <stdlib.h> | |
41 | #include "string.h" | |
757d2cad | 42 | #include <errno.h> |
8ca02f3c | 43 | #include "debug.h" |
ef416fc2 | 44 | |
45 | #if defined(WIN32) || defined(__EMX__) | |
46 | # include <io.h> | |
47 | #else | |
48 | # include <unistd.h> | |
49 | #endif /* WIN32 || __EMX__ */ | |
50 | ||
51 | ||
52 | /* | |
53 | * Local functions... | |
54 | */ | |
55 | ||
56 | static void ppd_handle_media(ppd_file_t *ppd); | |
ef416fc2 | 57 | |
58 | ||
59 | /* | |
60 | * Local globals... | |
61 | */ | |
62 | ||
63 | static const char ppd_custom_code[] = | |
64 | "pop pop pop\n" | |
65 | "<</PageSize[5 -2 roll]/ImagingBBox null>>setpagedevice\n"; | |
66 | ||
67 | ||
68 | /* | |
69 | * 'ppdCollect()' - Collect all marked options that reside in the specified | |
70 | * section. | |
71 | */ | |
72 | ||
73 | int /* O - Number of options marked */ | |
74 | ppdCollect(ppd_file_t *ppd, /* I - PPD file data */ | |
75 | ppd_section_t section, /* I - Section to collect */ | |
76 | ppd_choice_t ***choices) /* O - Pointers to choices */ | |
fa73b229 | 77 | { |
78 | return (ppdCollect2(ppd, section, 0.0, choices)); | |
79 | } | |
80 | ||
81 | ||
82 | /* | |
83 | * 'ppdCollect2()' - Collect all marked options that reside in the | |
84 | * specified section and minimum order. | |
85 | * | |
86 | * @since CUPS 1.2@ | |
87 | */ | |
88 | ||
89 | int /* O - Number of options marked */ | |
90 | ppdCollect2(ppd_file_t *ppd, /* I - PPD file data */ | |
91 | ppd_section_t section, /* I - Section to collect */ | |
92 | float min_order, /* I - Minimum OrderDependency value */ | |
93 | ppd_choice_t ***choices) /* O - Pointers to choices */ | |
ef416fc2 | 94 | { |
ef416fc2 | 95 | ppd_choice_t *c; /* Current choice */ |
0a682745 MS |
96 | ppd_section_t csection; /* Current section */ |
97 | float corder; /* Current OrderDependency value */ | |
ef416fc2 | 98 | int count; /* Number of choices collected */ |
99 | ppd_choice_t **collect; /* Collected choices */ | |
0a682745 | 100 | float *orders; /* Collected order values */ |
ef416fc2 | 101 | |
102 | ||
8ca02f3c | 103 | DEBUG_printf(("ppdCollect2(ppd=%p, section=%d, min_order=%f, choices=%p)\n", |
104 | ppd, section, min_order, choices)); | |
105 | ||
91c84a35 MS |
106 | if (!ppd || !choices) |
107 | { | |
108 | if (choices) | |
109 | *choices = NULL; | |
110 | ||
ef416fc2 | 111 | return (0); |
91c84a35 | 112 | } |
ef416fc2 | 113 | |
114 | /* | |
0a682745 | 115 | * Allocate memory for up to N selected choices... |
ef416fc2 | 116 | */ |
117 | ||
91c84a35 MS |
118 | count = 0; |
119 | if ((collect = calloc(sizeof(ppd_choice_t *), | |
120 | cupsArrayCount(ppd->marked))) == NULL) | |
121 | { | |
122 | *choices = NULL; | |
123 | return (0); | |
124 | } | |
125 | ||
126 | if ((orders = calloc(sizeof(float), cupsArrayCount(ppd->marked))) == NULL) | |
127 | { | |
128 | *choices = NULL; | |
129 | free(collect); | |
130 | return (0); | |
131 | } | |
ef416fc2 | 132 | |
133 | /* | |
134 | * Loop through all options and add choices as needed... | |
135 | */ | |
136 | ||
0a682745 MS |
137 | for (c = (ppd_choice_t *)cupsArrayFirst(ppd->marked); |
138 | c; | |
139 | c = (ppd_choice_t *)cupsArrayNext(ppd->marked)) | |
ef416fc2 | 140 | { |
0a682745 MS |
141 | csection = c->option->section; |
142 | corder = c->option->order; | |
143 | ||
144 | if (!strcmp(c->choice, "Custom")) | |
145 | { | |
146 | ppd_attr_t *attr; /* NonUIOrderDependency value */ | |
147 | float aorder; /* Order value */ | |
148 | char asection[17], /* Section name */ | |
149 | amain[PPD_MAX_NAME + 1], | |
150 | aoption[PPD_MAX_NAME]; | |
151 | /* *CustomFoo and True */ | |
152 | ||
153 | ||
154 | for (attr = ppdFindAttr(ppd, "NonUIOrderDependency", NULL); | |
155 | attr; | |
156 | attr = ppdFindNextAttr(ppd, "NonUIOrderDependency", NULL)) | |
157 | if (attr->value && | |
158 | sscanf(attr->value, "%f%16s%41s%40s", &aorder, asection, amain, | |
159 | aoption) == 4 && | |
160 | !strncmp(amain, "*Custom", 7) && | |
161 | !strcmp(amain + 7, c->option->keyword) && !strcmp(aoption, "True")) | |
162 | { | |
163 | /* | |
164 | * Use this NonUIOrderDependency... | |
165 | */ | |
ef416fc2 | 166 | |
0a682745 MS |
167 | corder = aorder; |
168 | ||
169 | if (!strcmp(asection, "DocumentSetup")) | |
170 | csection = PPD_ORDER_DOCUMENT; | |
171 | else if (!strcmp(asection, "ExitServer")) | |
172 | csection = PPD_ORDER_EXIT; | |
173 | else if (!strcmp(asection, "JCLSetup")) | |
174 | csection = PPD_ORDER_JCL; | |
175 | else if (!strcmp(asection, "PageSetup")) | |
176 | csection = PPD_ORDER_PAGE; | |
177 | else if (!strcmp(asection, "Prolog")) | |
178 | csection = PPD_ORDER_PROLOG; | |
179 | else | |
180 | csection = PPD_ORDER_ANY; | |
181 | ||
182 | break; | |
183 | } | |
184 | } | |
185 | ||
186 | if (csection == section && corder >= min_order) | |
187 | { | |
188 | collect[count] = c; | |
189 | orders[count] = corder; | |
190 | count ++; | |
191 | } | |
ef416fc2 | 192 | } |
193 | ||
194 | /* | |
195 | * If we have more than 1 marked choice, sort them... | |
196 | */ | |
197 | ||
198 | if (count > 1) | |
0a682745 MS |
199 | { |
200 | int i, j; /* Looping vars */ | |
201 | ||
202 | for (i = 0; i < (count - 1); i ++) | |
203 | for (j = i + 1; j < count; j ++) | |
204 | if (orders[i] > orders[j]) | |
205 | { | |
206 | c = collect[i]; | |
207 | corder = orders[i]; | |
208 | collect[i] = collect[j]; | |
209 | orders[i] = orders[j]; | |
210 | collect[j] = c; | |
211 | orders[j] = corder; | |
212 | } | |
213 | } | |
214 | ||
215 | free(orders); | |
ef416fc2 | 216 | |
8ca02f3c | 217 | DEBUG_printf(("ppdCollect2: %d marked choices...\n", count)); |
218 | ||
ef416fc2 | 219 | /* |
220 | * Return the array and number of choices; if 0, free the array since | |
221 | * it isn't needed. | |
222 | */ | |
223 | ||
224 | if (count > 0) | |
225 | { | |
226 | *choices = collect; | |
227 | return (count); | |
228 | } | |
229 | else | |
230 | { | |
231 | *choices = NULL; | |
232 | free(collect); | |
233 | return (0); | |
234 | } | |
235 | } | |
236 | ||
237 | ||
238 | /* | |
239 | * 'ppdEmit()' - Emit code for marked options to a file. | |
240 | */ | |
241 | ||
242 | int /* O - 0 on success, -1 on failure */ | |
243 | ppdEmit(ppd_file_t *ppd, /* I - PPD file record */ | |
244 | FILE *fp, /* I - File to write to */ | |
245 | ppd_section_t section) /* I - Section to write */ | |
fa73b229 | 246 | { |
247 | return (ppdEmitAfterOrder(ppd, fp, section, 0, 0.0)); | |
248 | } | |
249 | ||
250 | ||
251 | /* | |
252 | * 'ppdEmitAfterOrder()' - Emit a subset of the code for marked options to a file. | |
253 | * | |
254 | * When "limit" is non-zero, this function only emits options whose | |
255 | * OrderDependency value is greater than or equal to "min_order". | |
256 | * | |
257 | * When "limit" is zero, this function is identical to ppdEmit(). | |
258 | * | |
259 | * @since CUPS 1.2@ | |
260 | */ | |
261 | ||
262 | int /* O - 0 on success, -1 on failure */ | |
263 | ppdEmitAfterOrder( | |
264 | ppd_file_t *ppd, /* I - PPD file record */ | |
265 | FILE *fp, /* I - File to write to */ | |
266 | ppd_section_t section, /* I - Section to write */ | |
757d2cad | 267 | int limit, /* I - Non-zero to use min_order */ |
268 | float min_order) /* I - Lowest OrderDependency */ | |
ef416fc2 | 269 | { |
757d2cad | 270 | char *buffer; /* Option code */ |
271 | int status; /* Return status */ | |
ef416fc2 | 272 | |
273 | ||
274 | /* | |
757d2cad | 275 | * Range check input... |
ef416fc2 | 276 | */ |
277 | ||
757d2cad | 278 | if (!ppd || !fp) |
279 | return (-1); | |
ef416fc2 | 280 | |
281 | /* | |
757d2cad | 282 | * Get the string... |
ef416fc2 | 283 | */ |
284 | ||
757d2cad | 285 | buffer = ppdEmitString(ppd, section, min_order); |
ef416fc2 | 286 | |
757d2cad | 287 | /* |
288 | * Write it as needed and return... | |
289 | */ | |
ef416fc2 | 290 | |
757d2cad | 291 | if (buffer) |
292 | { | |
293 | status = fputs(buffer, fp) < 0 ? -1 : 0; | |
ef416fc2 | 294 | |
757d2cad | 295 | free(buffer); |
296 | } | |
297 | else | |
298 | status = 0; | |
ef416fc2 | 299 | |
757d2cad | 300 | return (status); |
ef416fc2 | 301 | } |
302 | ||
303 | ||
304 | /* | |
305 | * 'ppdEmitFd()' - Emit code for marked options to a file. | |
306 | */ | |
307 | ||
308 | int /* O - 0 on success, -1 on failure */ | |
309 | ppdEmitFd(ppd_file_t *ppd, /* I - PPD file record */ | |
310 | int fd, /* I - File to write to */ | |
311 | ppd_section_t section) /* I - Section to write */ | |
312 | { | |
757d2cad | 313 | char *buffer, /* Option code */ |
314 | *bufptr; /* Pointer into code */ | |
315 | size_t buflength; /* Length of option code */ | |
316 | ssize_t bytes; /* Bytes written */ | |
317 | int status; /* Return status */ | |
ef416fc2 | 318 | |
319 | ||
320 | /* | |
757d2cad | 321 | * Range check input... |
ef416fc2 | 322 | */ |
323 | ||
757d2cad | 324 | if (!ppd || fd < 0) |
325 | return (-1); | |
ef416fc2 | 326 | |
327 | /* | |
757d2cad | 328 | * Get the string... |
ef416fc2 | 329 | */ |
330 | ||
757d2cad | 331 | buffer = ppdEmitString(ppd, section, 0.0); |
ef416fc2 | 332 | |
757d2cad | 333 | /* |
334 | * Write it as needed and return... | |
335 | */ | |
ef416fc2 | 336 | |
757d2cad | 337 | if (buffer) |
338 | { | |
339 | buflength = strlen(buffer); | |
340 | bufptr = buffer; | |
341 | bytes = 0; | |
ef416fc2 | 342 | |
757d2cad | 343 | while (buflength > 0) |
344 | { | |
b86bc4cf | 345 | #ifdef WIN32 |
346 | if ((bytes = (ssize_t)write(fd, bufptr, (unsigned)buflength)) < 0) | |
347 | #else | |
757d2cad | 348 | if ((bytes = write(fd, bufptr, buflength)) < 0) |
b86bc4cf | 349 | #endif /* WIN32 */ |
ef416fc2 | 350 | { |
757d2cad | 351 | if (errno == EAGAIN || errno == EINTR) |
352 | continue; | |
ef416fc2 | 353 | |
757d2cad | 354 | break; |
ef416fc2 | 355 | } |
757d2cad | 356 | |
357 | buflength -= bytes; | |
358 | bufptr += bytes; | |
ef416fc2 | 359 | } |
360 | ||
757d2cad | 361 | status = bytes < 0 ? -1 : 0; |
362 | ||
363 | free(buffer); | |
364 | } | |
365 | else | |
366 | status = 0; | |
367 | ||
368 | return (status); | |
ef416fc2 | 369 | } |
370 | ||
371 | ||
372 | /* | |
373 | * 'ppdEmitJCL()' - Emit code for JCL options to a file. | |
374 | */ | |
375 | ||
376 | int /* O - 0 on success, -1 on failure */ | |
377 | ppdEmitJCL(ppd_file_t *ppd, /* I - PPD file record */ | |
378 | FILE *fp, /* I - File to write to */ | |
379 | int job_id, /* I - Job ID */ | |
380 | const char *user, /* I - Username */ | |
381 | const char *title) /* I - Title */ | |
382 | { | |
383 | char *ptr; /* Pointer into JCL string */ | |
384 | char temp[81]; /* Local title string */ | |
385 | ||
386 | ||
387 | /* | |
388 | * Range check the input... | |
389 | */ | |
390 | ||
757d2cad | 391 | if (!ppd || !ppd->jcl_begin || !ppd->jcl_ps) |
ef416fc2 | 392 | return (0); |
393 | ||
394 | /* | |
395 | * See if the printer supports HP PJL... | |
396 | */ | |
397 | ||
398 | if (!strncmp(ppd->jcl_begin, "\033%-12345X@", 10)) | |
399 | { | |
400 | /* | |
401 | * This printer uses HP PJL commands for output; filter the output | |
402 | * so that we only have a single "@PJL JOB" command in the header... | |
403 | * | |
404 | * To avoid bugs in the PJL implementation of certain vendors' products | |
405 | * (Xerox in particular), we add a dummy "@PJL" command at the beginning | |
406 | * of the PJL commands to initialize PJL processing. | |
407 | */ | |
408 | ||
f7deaa1a | 409 | ppd_attr_t *charset; /* PJL charset */ |
410 | ||
411 | ||
412 | if ((charset = ppdFindAttr(ppd, "cupsPJLCharset", NULL)) != NULL) | |
413 | { | |
414 | if (!charset->value || strcasecmp(charset->value, "UTF-8")) | |
415 | charset = NULL; | |
416 | } | |
417 | ||
ef416fc2 | 418 | fputs("\033%-12345X@PJL\n", fp); |
419 | for (ptr = ppd->jcl_begin + 9; *ptr;) | |
420 | if (!strncmp(ptr, "@PJL JOB", 8)) | |
421 | { | |
422 | /* | |
423 | * Skip job command... | |
424 | */ | |
425 | ||
426 | for (;*ptr; ptr ++) | |
427 | if (*ptr == '\n') | |
428 | break; | |
429 | ||
430 | if (*ptr) | |
431 | ptr ++; | |
432 | } | |
433 | else | |
434 | { | |
435 | /* | |
436 | * Copy line... | |
437 | */ | |
438 | ||
439 | for (;*ptr; ptr ++) | |
440 | { | |
441 | putc(*ptr, fp); | |
442 | if (*ptr == '\n') | |
443 | break; | |
444 | } | |
445 | ||
446 | if (*ptr) | |
447 | ptr ++; | |
448 | } | |
449 | ||
450 | /* | |
451 | * Eliminate any path info from the job title... | |
452 | */ | |
453 | ||
454 | if ((ptr = strrchr(title, '/')) != NULL) | |
455 | title = ptr + 1; | |
456 | ||
457 | /* | |
07725fee | 458 | * Replace double quotes with single quotes and 8-bit characters with |
459 | * question marks so that the title does not cause a PJL syntax error. | |
ef416fc2 | 460 | */ |
461 | ||
462 | strlcpy(temp, title, sizeof(temp)); | |
463 | ||
464 | for (ptr = temp; *ptr; ptr ++) | |
465 | if (*ptr == '\"') | |
466 | *ptr = '\''; | |
f7deaa1a | 467 | else if (charset && (*ptr & 128)) |
07725fee | 468 | *ptr = '?'; |
ef416fc2 | 469 | |
470 | /* | |
471 | * Send PJL JOB and PJL RDYMSG commands before we enter PostScript mode... | |
472 | */ | |
473 | ||
474 | fprintf(fp, "@PJL JOB NAME = \"%s\" DISPLAY = \"%d %s %s\"\n", temp, | |
475 | job_id, user, temp); | |
476 | fprintf(fp, "@PJL RDYMSG DISPLAY = \"%d %s %s\"\n", job_id, user, temp); | |
477 | } | |
478 | else | |
479 | fputs(ppd->jcl_begin, fp); | |
480 | ||
481 | ppdEmit(ppd, fp, PPD_ORDER_JCL); | |
482 | fputs(ppd->jcl_ps, fp); | |
483 | ||
484 | return (0); | |
485 | } | |
486 | ||
487 | ||
488 | /* | |
489 | * 'ppdEmitJCLEnd()' - Emit JCLEnd code to a file. | |
490 | * | |
491 | * @since CUPS 1.2@ | |
492 | */ | |
493 | ||
494 | int /* O - 0 on success, -1 on failure */ | |
495 | ppdEmitJCLEnd(ppd_file_t *ppd, /* I - PPD file record */ | |
496 | FILE *fp) /* I - File to write to */ | |
497 | { | |
ef416fc2 | 498 | /* |
499 | * Range check the input... | |
500 | */ | |
501 | ||
757d2cad | 502 | if (!ppd) |
ef416fc2 | 503 | return (0); |
504 | ||
757d2cad | 505 | if (!ppd->jcl_end) |
ef416fc2 | 506 | { |
507 | if (ppd->num_filters == 0) | |
757d2cad | 508 | putc(0x04, fp); |
ef416fc2 | 509 | |
510 | return (0); | |
511 | } | |
512 | ||
513 | /* | |
514 | * See if the printer supports HP PJL... | |
515 | */ | |
516 | ||
517 | if (!strncmp(ppd->jcl_end, "\033%-12345X@", 10)) | |
518 | { | |
519 | /* | |
520 | * This printer uses HP PJL commands for output; filter the output | |
521 | * so that we only have a single "@PJL JOB" command in the header... | |
522 | * | |
523 | * To avoid bugs in the PJL implementation of certain vendors' products | |
524 | * (Xerox in particular), we add a dummy "@PJL" command at the beginning | |
525 | * of the PJL commands to initialize PJL processing. | |
526 | */ | |
527 | ||
528 | fputs("\033%-12345X@PJL\n", fp); | |
529 | fputs("@PJL RDYMSG DISPLAY = \"READY\"\n", fp); | |
530 | fputs(ppd->jcl_end + 9, fp); | |
531 | } | |
532 | else | |
533 | fputs(ppd->jcl_end, fp); | |
534 | ||
535 | return (0); | |
536 | } | |
537 | ||
538 | ||
757d2cad | 539 | /* |
540 | * 'ppdEmitString()' - Get a string containing the code for marked options. | |
541 | * | |
542 | * When "min_order" is greater than zero, this function only includes options | |
543 | * whose OrderDependency value is greater than or equal to "min_order". | |
544 | * Otherwise, all options in the specified section are included in the | |
545 | * returned string. | |
546 | * | |
547 | * The return string is allocated on the heap and should be freed using | |
548 | * free() when you are done with it. | |
549 | * | |
550 | * @since CUPS 1.2@ | |
551 | */ | |
552 | ||
553 | char * /* O - String containing option code */ | |
554 | ppdEmitString(ppd_file_t *ppd, /* I - PPD file record */ | |
555 | ppd_section_t section, /* I - Section to write */ | |
556 | float min_order) /* I - Lowest OrderDependency */ | |
557 | { | |
558 | int i, j, /* Looping vars */ | |
559 | count; /* Number of choices */ | |
560 | ppd_choice_t **choices; /* Choices */ | |
561 | ppd_size_t *size; /* Custom page size */ | |
562 | ppd_coption_t *coption; /* Custom option */ | |
563 | ppd_cparam_t *cparam; /* Custom parameter */ | |
564 | size_t bufsize; /* Size of string buffer needed */ | |
565 | char *buffer, /* String buffer */ | |
566 | *bufptr, /* Pointer into buffer */ | |
567 | *bufend; /* End of buffer */ | |
568 | struct lconv *loc; /* Locale data */ | |
569 | ||
570 | ||
8ca02f3c | 571 | DEBUG_printf(("ppdEmitString(ppd=%p, section=%d, min_order=%f)\n", |
572 | ppd, section, min_order)); | |
573 | ||
757d2cad | 574 | /* |
575 | * Range check input... | |
576 | */ | |
577 | ||
578 | if (!ppd) | |
579 | return (NULL); | |
580 | ||
581 | /* | |
582 | * Use PageSize or PageRegion as required... | |
583 | */ | |
584 | ||
585 | ppd_handle_media(ppd); | |
586 | ||
587 | /* | |
588 | * Collect the options we need to emit... | |
589 | */ | |
590 | ||
591 | if ((count = ppdCollect2(ppd, section, min_order, &choices)) == 0) | |
592 | return (NULL); | |
593 | ||
594 | /* | |
595 | * Count the number of bytes that are required to hold all of the | |
596 | * option code... | |
597 | */ | |
598 | ||
599 | for (i = 0, bufsize = 1; i < count; i ++) | |
600 | { | |
601 | if (section != PPD_ORDER_EXIT && section != PPD_ORDER_JCL) | |
602 | { | |
603 | bufsize += 3; /* [{\n */ | |
604 | ||
605 | if ((!strcasecmp(choices[i]->option->keyword, "PageSize") || | |
606 | !strcasecmp(choices[i]->option->keyword, "PageRegion")) && | |
607 | !strcasecmp(choices[i]->choice, "Custom")) | |
608 | { | |
8ca02f3c | 609 | DEBUG_puts("ppdEmitString: Custom size set!"); |
610 | ||
e1d6a774 | 611 | bufsize += 37; /* %%BeginFeature: *CustomPageSize True\n */ |
757d2cad | 612 | bufsize += 50; /* Five 9-digit numbers + newline */ |
613 | } | |
614 | else if (!strcasecmp(choices[i]->choice, "Custom") && | |
615 | (coption = ppdFindCustomOption(ppd, | |
616 | choices[i]->option->keyword)) | |
617 | != NULL) | |
618 | { | |
e1d6a774 | 619 | bufsize += 17 + strlen(choices[i]->option->keyword) + 6; |
620 | /* %%BeginFeature: *keyword True\n */ | |
757d2cad | 621 | |
622 | ||
623 | for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params); | |
624 | cparam; | |
625 | cparam = (ppd_cparam_t *)cupsArrayNext(coption->params)) | |
626 | { | |
627 | switch (cparam->type) | |
628 | { | |
629 | case PPD_CUSTOM_CURVE : | |
630 | case PPD_CUSTOM_INVCURVE : | |
631 | case PPD_CUSTOM_POINTS : | |
632 | case PPD_CUSTOM_REAL : | |
633 | case PPD_CUSTOM_INT : | |
634 | bufsize += 10; | |
635 | break; | |
636 | ||
637 | case PPD_CUSTOM_PASSCODE : | |
638 | case PPD_CUSTOM_PASSWORD : | |
639 | case PPD_CUSTOM_STRING : | |
640 | bufsize += 3 + 4 * strlen(cparam->current.custom_string); | |
641 | break; | |
642 | } | |
643 | } | |
644 | } | |
645 | else | |
e1d6a774 | 646 | bufsize += 17 + strlen(choices[i]->option->keyword) + 1 + |
647 | strlen(choices[i]->choice) + 1; | |
648 | /* %%BeginFeature: *keyword choice\n */ | |
757d2cad | 649 | |
650 | bufsize += 13; /* %%EndFeature\n */ | |
651 | bufsize += 22; /* } stopped cleartomark\n */ | |
652 | } | |
653 | ||
654 | if (choices[i]->code) | |
e1d6a774 | 655 | bufsize += strlen(choices[i]->code) + 1; |
757d2cad | 656 | else |
657 | bufsize += strlen(ppd_custom_code); | |
658 | } | |
659 | ||
660 | /* | |
661 | * Allocate memory... | |
662 | */ | |
663 | ||
8ca02f3c | 664 | DEBUG_printf(("ppdEmitString: Allocating %d bytes for string...\n", bufsize)); |
665 | ||
757d2cad | 666 | if ((buffer = calloc(1, bufsize)) == NULL) |
667 | { | |
668 | free(choices); | |
669 | return (NULL); | |
670 | } | |
671 | ||
672 | bufend = buffer + bufsize - 1; | |
673 | loc = localeconv(); | |
674 | ||
675 | /* | |
676 | * Copy the option code to the buffer... | |
677 | */ | |
678 | ||
679 | for (i = 0, bufptr = buffer; i < count; i ++, bufptr += strlen(bufptr)) | |
680 | if (section != PPD_ORDER_EXIT && section != PPD_ORDER_JCL) | |
681 | { | |
682 | /* | |
683 | * Add wrapper commands to prevent printer errors for unsupported | |
684 | * options... | |
685 | */ | |
686 | ||
687 | strlcpy(bufptr, "[{\n", bufend - bufptr + 1); | |
688 | bufptr += 3; | |
689 | ||
690 | /* | |
691 | * Send DSC comments with option... | |
692 | */ | |
693 | ||
8ca02f3c | 694 | DEBUG_printf(("Adding code for %s=%s...\n", choices[i]->option->keyword, |
695 | choices[i]->choice)); | |
696 | ||
757d2cad | 697 | if ((!strcasecmp(choices[i]->option->keyword, "PageSize") || |
698 | !strcasecmp(choices[i]->option->keyword, "PageRegion")) && | |
699 | !strcasecmp(choices[i]->choice, "Custom")) | |
700 | { | |
701 | /* | |
702 | * Variable size; write out standard size options, using the | |
703 | * parameter positions defined in the PPD file... | |
704 | */ | |
705 | ||
706 | ppd_attr_t *attr; /* PPD attribute */ | |
707 | int pos, /* Position of custom value */ | |
708 | orientation; /* Orientation to use */ | |
709 | float values[5]; /* Values for custom command */ | |
710 | ||
711 | ||
712 | strlcpy(bufptr, "%%BeginFeature: *CustomPageSize True\n", | |
713 | bufend - bufptr + 1); | |
714 | bufptr += 37; | |
715 | ||
716 | size = ppdPageSize(ppd, "Custom"); | |
717 | ||
718 | memset(values, 0, sizeof(values)); | |
719 | ||
720 | if ((attr = ppdFindAttr(ppd, "ParamCustomPageSize", "Width")) != NULL) | |
721 | { | |
722 | pos = atoi(attr->value) - 1; | |
723 | ||
724 | if (pos < 0 || pos > 4) | |
725 | pos = 0; | |
726 | } | |
727 | else | |
728 | pos = 0; | |
729 | ||
730 | values[pos] = size->width; | |
731 | ||
732 | if ((attr = ppdFindAttr(ppd, "ParamCustomPageSize", "Height")) != NULL) | |
733 | { | |
734 | pos = atoi(attr->value) - 1; | |
735 | ||
736 | if (pos < 0 || pos > 4) | |
737 | pos = 1; | |
738 | } | |
739 | else | |
740 | pos = 1; | |
741 | ||
742 | values[pos] = size->length; | |
743 | ||
744 | /* | |
745 | * According to the Adobe PPD specification, an orientation of 1 | |
746 | * will produce a print that comes out upside-down with the X | |
747 | * axis perpendicular to the direction of feed, which is exactly | |
748 | * what we want to be consistent with non-PS printers. | |
749 | * | |
750 | * We could also use an orientation of 3 to produce output that | |
751 | * comes out rightside-up (this is the default for many large format | |
752 | * printer PPDs), however for consistency we will stick with the | |
753 | * value 1. | |
754 | * | |
755 | * If we wanted to get fancy, we could use orientations of 0 or | |
756 | * 2 and swap the width and length, however we don't want to get | |
757 | * fancy, we just want it to work consistently. | |
758 | * | |
759 | * The orientation value is range limited by the Orientation | |
760 | * parameter definition, so certain non-PS printer drivers that | |
761 | * only support an Orientation of 0 will get the value 0 as | |
762 | * expected. | |
763 | */ | |
764 | ||
765 | orientation = 1; | |
766 | ||
767 | if ((attr = ppdFindAttr(ppd, "ParamCustomPageSize", | |
768 | "Orientation")) != NULL) | |
769 | { | |
770 | int min_orient, max_orient; /* Minimum and maximum orientations */ | |
771 | ||
772 | ||
773 | if (sscanf(attr->value, "%d%*s%d%d", &pos, &min_orient, | |
774 | &max_orient) != 3) | |
775 | pos = 4; | |
776 | else | |
777 | { | |
778 | pos --; | |
779 | ||
780 | if (pos < 0 || pos > 4) | |
781 | pos = 4; | |
782 | ||
783 | if (orientation > max_orient) | |
784 | orientation = max_orient; | |
785 | else if (orientation < min_orient) | |
786 | orientation = min_orient; | |
787 | } | |
788 | } | |
789 | else | |
790 | pos = 4; | |
791 | ||
b86bc4cf | 792 | values[pos] = (float)orientation; |
757d2cad | 793 | |
794 | for (pos = 0; pos < 5; pos ++) | |
795 | { | |
796 | bufptr = _cupsStrFormatd(bufptr, bufend, values[pos], loc); | |
797 | *bufptr++ = '\n'; | |
798 | } | |
799 | ||
800 | if (!choices[i]->code) | |
801 | { | |
802 | /* | |
803 | * This can happen with certain buggy PPD files that don't include | |
804 | * a CustomPageSize command sequence... We just use a generic | |
805 | * Level 2 command sequence... | |
806 | */ | |
807 | ||
808 | strlcpy(bufptr, ppd_custom_code, bufend - bufptr + 1); | |
809 | bufptr += strlen(bufptr); | |
810 | } | |
811 | } | |
812 | else if (!strcasecmp(choices[i]->choice, "Custom") && | |
813 | (coption = ppdFindCustomOption(ppd, | |
814 | choices[i]->option->keyword)) | |
815 | != NULL) | |
816 | { | |
817 | /* | |
818 | * Custom option... | |
819 | */ | |
820 | ||
821 | const char *s; /* Pointer into string value */ | |
822 | ||
823 | ||
824 | snprintf(bufptr, bufend - bufptr + 1, | |
825 | "%%%%BeginFeature: *Custom%s True\n", coption->keyword); | |
826 | bufptr += strlen(bufptr); | |
827 | ||
828 | for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params); | |
829 | cparam; | |
830 | cparam = (ppd_cparam_t *)cupsArrayNext(coption->params)) | |
831 | { | |
832 | switch (cparam->type) | |
833 | { | |
834 | case PPD_CUSTOM_CURVE : | |
835 | case PPD_CUSTOM_INVCURVE : | |
836 | case PPD_CUSTOM_POINTS : | |
837 | case PPD_CUSTOM_REAL : | |
838 | bufptr = _cupsStrFormatd(bufptr, bufend, | |
839 | cparam->current.custom_real, loc); | |
840 | *bufptr++ = '\n'; | |
841 | break; | |
842 | ||
843 | case PPD_CUSTOM_INT : | |
844 | snprintf(bufptr, bufend - bufptr + 1, "%d\n", | |
845 | cparam->current.custom_int); | |
846 | bufptr += strlen(bufptr); | |
847 | break; | |
848 | ||
849 | case PPD_CUSTOM_PASSCODE : | |
850 | case PPD_CUSTOM_PASSWORD : | |
851 | case PPD_CUSTOM_STRING : | |
852 | *bufptr++ = '('; | |
853 | ||
854 | for (s = cparam->current.custom_string; *s; s ++) | |
855 | if (*s < ' ' || *s == '(' || *s == ')' || *s >= 127) | |
856 | { | |
857 | snprintf(bufptr, bufend - bufptr + 1, "\\%03o", *s & 255); | |
858 | bufptr += strlen(bufptr); | |
859 | } | |
860 | else | |
861 | *bufptr++ = *s; | |
862 | ||
863 | *bufptr++ = ')'; | |
864 | *bufptr++ = '\n'; | |
865 | break; | |
866 | } | |
867 | } | |
868 | } | |
869 | else | |
870 | { | |
871 | snprintf(bufptr, bufend - bufptr + 1, "%%%%BeginFeature: *%s %s\n", | |
872 | choices[i]->option->keyword, choices[i]->choice); | |
873 | bufptr += strlen(bufptr); | |
874 | } | |
875 | ||
876 | if (choices[i]->code && choices[i]->code[0]) | |
877 | { | |
b86bc4cf | 878 | j = (int)strlen(choices[i]->code); |
757d2cad | 879 | memcpy(bufptr, choices[i]->code, j); |
880 | bufptr += j; | |
881 | ||
882 | if (choices[i]->code[j - 1] != '\n') | |
883 | *bufptr++ = '\n'; | |
884 | } | |
885 | ||
886 | strlcpy(bufptr, "%%EndFeature\n" | |
887 | "} stopped cleartomark\n", bufend - bufptr + 1); | |
888 | bufptr += strlen(bufptr); | |
8ca02f3c | 889 | |
890 | DEBUG_printf(("ppdEmitString: Offset in string is %d...\n", | |
891 | bufptr - buffer)); | |
757d2cad | 892 | } |
893 | else | |
894 | { | |
895 | strlcpy(bufptr, choices[i]->code, bufend - bufptr + 1); | |
896 | bufptr += strlen(bufptr); | |
897 | } | |
898 | ||
899 | /* | |
900 | * Nul-terminate, free, and return... | |
901 | */ | |
902 | ||
903 | *bufptr = '\0'; | |
904 | ||
905 | free(choices); | |
906 | ||
907 | return (buffer); | |
908 | } | |
909 | ||
910 | ||
ef416fc2 | 911 | /* |
912 | * 'ppd_handle_media()' - Handle media selection... | |
913 | */ | |
914 | ||
915 | static void | |
916 | ppd_handle_media(ppd_file_t *ppd) | |
917 | { | |
918 | ppd_choice_t *manual_feed, /* ManualFeed choice, if any */ | |
919 | *input_slot, /* InputSlot choice, if any */ | |
920 | *page; /* PageSize/PageRegion */ | |
921 | ppd_size_t *size; /* Current media size */ | |
922 | ppd_attr_t *rpr; /* RequiresPageRegion value */ | |
923 | ||
924 | ||
925 | /* | |
926 | * This function determines if the user has selected a media source | |
927 | * via the InputSlot or ManualFeed options; if so, it marks the | |
928 | * PageRegion option corresponding to the current media size. | |
929 | * Otherwise it marks the PageSize option. | |
930 | */ | |
931 | ||
932 | if ((size = ppdPageSize(ppd, NULL)) == NULL) | |
933 | return; | |
934 | ||
935 | manual_feed = ppdFindMarkedChoice(ppd, "ManualFeed"); | |
936 | input_slot = ppdFindMarkedChoice(ppd, "InputSlot"); | |
937 | ||
938 | if (input_slot != NULL) | |
939 | rpr = ppdFindAttr(ppd, "RequiresPageRegion", input_slot->choice); | |
940 | else | |
941 | rpr = NULL; | |
942 | ||
943 | if (!rpr) | |
944 | rpr = ppdFindAttr(ppd, "RequiresPageRegion", "All"); | |
945 | ||
fa73b229 | 946 | if (!strcasecmp(size->name, "Custom") || (!manual_feed && !input_slot) || |
947 | !((manual_feed && !strcasecmp(manual_feed->choice, "True")) || | |
948 | (input_slot && input_slot->code && input_slot->code[0]))) | |
ef416fc2 | 949 | { |
950 | /* | |
951 | * Manual feed was not selected and/or the input slot selection does | |
952 | * not contain any PostScript code. Use the PageSize option... | |
953 | */ | |
954 | ||
955 | ppdMarkOption(ppd, "PageSize", size->name); | |
956 | } | |
957 | else | |
958 | { | |
959 | /* | |
960 | * Manual feed was selected and/or the input slot selection contains | |
961 | * PostScript code. Use the PageRegion option... | |
962 | */ | |
963 | ||
964 | ppdMarkOption(ppd, "PageRegion", size->name); | |
965 | ||
966 | /* | |
967 | * RequiresPageRegion does not apply to manual feed so we need to | |
968 | * check that we are not doing manual feed before unmarking PageRegion. | |
969 | */ | |
970 | ||
971 | if (!(manual_feed && !strcasecmp(manual_feed->choice, "True")) && | |
972 | ((rpr && rpr->value && !strcmp(rpr->value, "False")) || | |
973 | (!rpr && !ppd->num_filters))) | |
974 | { | |
975 | /* | |
976 | * Either the PPD file specifies no PageRegion code or the PPD file | |
977 | * not for a CUPS raster driver and thus defaults to no PageRegion | |
978 | * code... Unmark the PageRegion choice so that we don't output the | |
979 | * code... | |
980 | */ | |
981 | ||
982 | page = ppdFindMarkedChoice(ppd, "PageRegion"); | |
983 | ||
984 | if (page) | |
985 | page->marked = 0; | |
986 | } | |
987 | } | |
988 | } | |
989 | ||
990 | ||
ef416fc2 | 991 | /* |
bc44d920 | 992 | * End of "$Id: emit.c 6649 2007-07-11 21:46:42Z mike $". |
ef416fc2 | 993 | */ |