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