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