Debian Codegraph @

contrib/pcl3/eprn/eprnparm.c

raw
1 /******************************************************************************
2 File: $Id: eprnparm.c,v 1.24 2001/08/18 17:42:34 Martin Rel $
3 Contents: Device parameter handling for the ghostscript device 'eprn'
4 Author: Martin Lottermoser, Greifswaldstrasse 28, 38124 Braunschweig,
5 Germany. E-mail: [email protected].
6
7 *******************************************************************************
8 * *
9 * Copyright (C) 2000, 2001 by Martin Lottermoser *
10 * All rights reserved *
11 * *
12 *******************************************************************************
13
14 Preprocessor symbols:
15
16 EPRN_GS_HAS_MEDIAPOSITION
17 Define this if ghostscript should in the future implement the standard
18 PostScript page device parameter "MediaPosition" as a device parameter.
19 Otherwise it will be stored in the eprn device. Note that
20 ghostscript's input media selection algorithm *does* react to the
21 parameter, and you could also specify it from PostScript. This
22 implementation is only needed to make the parameter available as a
23 command line option.
24
25 EPRN_NO_PAGECOUNTFILE
26 Define this if you do not want to use eprn's pagecount-file feature.
27 You very likely must define this on Microsoft Windows. This is
28 automatically defined under Visual Studio builds.
29
30 EPRN_TRACE
31 Define this to enable tracing. Only useful for development.
32
33 ******************************************************************************/
34
35 /*****************************************************************************/
36
37 #ifndef _XOPEN_SOURCE
38 #define _XOPEN_SOURCE 500
39 #endif
40
41 /* Special Aladdin header, must be included before <sys/types.h> on some
42 platforms (e.g., FreeBSD). */
43 #include "std.h"
44
45 /* Standard headers */
46 #include <assert.h>
47 #include <ctype.h>
48 #include <errno.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52
53 /* Ghostscript headers */
54 #ifdef EPRN_TRACE
55 #include "gdebug.h"
56 #endif /* EPRN_TRACE */
57
58 /* Special headers */
59 #include "gdeveprn.h"
60 #include "gp.h"
61
62 #include "gscoord.h" /* for gs_setdefaultmatrix() */
63
64 /*****************************************************************************/
65
66 #define ERRPREF "? eprn: "
67 #define WARNPREF "?-W eprn: "
68
69 /*****************************************************************************/
70
71 /* Data structures for string arguments to parameters */
72
73 const eprn_StringAndInt
74 /* Colour models */
75 eprn_colour_model_list[] = {
76 /* Values of type 'eprn_ColourModel' are assumed to be usable as indices
77 into this array in order to find string representations for them. */
78 { "Gray", eprn_DeviceGray },
79 { "RGB", eprn_DeviceRGB },
80 { "CMY", eprn_DeviceCMY },
81 { "CMY+K", eprn_DeviceCMY_plus_K },
82 { "CMYK", eprn_DeviceCMYK },
83 { NULL, 0 }
84 };
85
86 static const eprn_StringAndInt
87 /* Intensity rendering methods */
88 intensity_rendering_list[] = {
89 { "printer", eprn_IR_printer },
90 { "halftones", eprn_IR_halftones },
91 { "Floyd-Steinberg", eprn_IR_FloydSteinberg },
92 { NULL, 0}
93 };
94
95 /******************************************************************************
96
97 Function: eprn_get_string
98
99 This function returns a string representation of 'in_value' in '*out_value',
100 based on 'table'. 'table' must be an array terminated with an entry having
101 NULL as the 'name' value and must be permanently allocated and constant.
102 If 'in_value' cannot be found in 'table', the function returns a non-zero
103 value, otherwise zero.
104
105 The string buffer in '*out_value' will be a statically allocated area which
106 must not be modified.
107
108 ******************************************************************************/
109
110 int eprn_get_string(int in_value, const eprn_StringAndInt *table,
111 gs_param_string *out_value)
112 {
113 while (table->name != NULL && table->value != in_value) table++;
114 if (table->name == NULL) return -1;
115
116 out_value->data = (const byte *)table->name;
117 out_value->size = strlen(table->name);
118 out_value->persistent = true;
119
120 return 0;
121 }
122
123 /******************************************************************************
124
125 Function: eprn_get_int
126
127 This function parses 'in_value' based on 'table' and returns the result in
128 '*out_value'. 'table' must be an array, terminated with an entry having NULL
129 as the value for 'name'.
130
131 'in_value' must be a string present in 'table'. If it is, the function
132 returns 0, otherwise a non-zero ghostscript error value.
133
134 On returning 'gs_error_VMerror', the function will have issued an error
135 message.
136
137 ******************************************************************************/
138
139 int eprn_get_int(const gs_param_string *in_value,
140 const eprn_StringAndInt *table, int *out_value)
141 {
142 char *s;
143
144 /* First we construct a properly NUL-terminated string */
145 s = (char *) malloc(in_value->size + 1);
146 if (s == NULL) {
147 eprintf1(ERRPREF
148 "Memory allocation failure in eprn_get_int(): %s.\n",
149 strerror(errno));
150 return_error(gs_error_VMerror);
151 }
152 strncpy(s, (const char *)in_value->data, in_value->size);
153 s[in_value->size] = '\0';
154
155 /* Loop over table */
156 while (table->name != NULL && strcmp(table->name, s) != 0) table++;
157 if (table->name != NULL) *out_value = table->value;
158 else {
159 free(s); s = NULL;
160 return_error(gs_error_rangecheck);
161 }
162
163 free(s); s = NULL;
164
165 return 0;
166 }
167
168 /******************************************************************************
169
170 Function: eprn_dump_parameter_list
171
172 This function is only used for debugging. It dumps the names of the
173 parameters in the parameter list 'plist' on the debugging stream.
174
175 ******************************************************************************/
176
177 #ifdef EPRN_TRACE
178
179 void eprn_dump_parameter_list(gs_param_list *plist)
180 {
181 gs_param_enumerator_t iterator;
182 gs_param_key_t key;
183 int count = 0;
184
185 param_init_enumerator(&iterator);
186 while (param_get_next_key(plist, &iterator, &key) == 0) {
187 int j;
188
189 count++;
190 dmlprintf(plist->memory, " `");
191 for (j = 0; j < key.size; j++) dmputc(plist->memory, key.data[j]);
192 dmprintf(plist->memory, "'\n");
193 }
194 dmlprintf1(plist->memory, " Number of parameters: %d.\n", count);
195
196 return;
197 }
198
199 #endif /* EPRN_TRACE */
200
201 /******************************************************************************
202
203 Function: eprn_fillpage
204 This is just a "call-through" to the default, so we can grab the gs_gstate
205
206 ******************************************************************************/
207 int
208 eprn_fillpage(gx_device *dev, gs_gstate * pgs, gx_device_color *pdevc)
209 {
210 eprn_Eprn *eprn = &((eprn_Device *)dev)->eprn;
211
212 eprn->pgs = pgs;
213
214 return (*eprn->orig_fillpage)(dev, pgs, pdevc);
215 }
216
217
218 static void eprn_replace_fillpage(gx_device *dev)
219 {
220 eprn_Eprn *eprn = &((eprn_Device *)dev)->eprn;
221
222 if (dev->procs.fillpage != eprn_fillpage) {
223 eprn->orig_fillpage = dev->procs.fillpage;
224 dev->procs.fillpage = eprn_fillpage;
225 }
226 }
227
228
229 /******************************************************************************
230
231 Function: eprn_get_params
232
233 This function returns to the caller information about the values of
234 parameters defined for the device in the 'eprn' part and its base devices.
235
236 The function returns zero on success and a negative value on error.
237
238 ******************************************************************************/
239
240 int eprn_get_params(gx_device *device, gs_param_list *plist)
241 {
242 gs_param_string string_value;
243 const eprn_Eprn *eprn = &((eprn_Device *)device)->eprn;
244 int rc;
245
246 #ifdef EPRN_TRACE
247 if_debug0(EPRN_TRACE_CHAR, "! eprn_get_params()...\n");
248 #endif
249
250 eprn_replace_fillpage(device);
251
252 /* Base class parameters */
253 rc = gdev_prn_get_params(device, plist);
254 if (rc < 0) return rc;
255
256 /* Number of intensity levels. The casts are used to get rid of compiler
257 warnings because the *_levels are unsigned. */
258 if ((rc = param_write_int(plist, "BlackLevels",
259 (const int *)&eprn->black_levels)) < 0) return rc;
260 if ((rc = param_write_int(plist, "CMYLevels",
261 (const int *)&eprn->non_black_levels)) < 0) return rc;
262 if ((rc = param_write_int(plist, "RGBLevels",
263 (const int *)&eprn->non_black_levels)) < 0) return rc;
264
265 /* Colour model */
266 eprn_get_string(eprn->colour_model, eprn_colour_model_list, &string_value);
267 if ((rc = param_write_string(plist, "ColourModel", &string_value)) < 0 ||
268 (rc = param_write_string(plist, "ColorModel", &string_value)) < 0)
269 return rc;
270
271 /* CUPS page accounting */
272 if ((rc = param_write_bool(plist, "CUPSAccounting", &eprn->CUPS_accounting))
273 < 0) return rc;
274
275 /* CUPS message format */
276 if ((rc = param_write_bool(plist, "CUPSMessages", &eprn->CUPS_messages)) < 0)
277 return rc;
278
279 /* Intensity rendering */
280 eprn_get_string(eprn->intensity_rendering, intensity_rendering_list,
281 &string_value);
282 if ((rc = param_write_string(plist, "IntensityRendering", &string_value)) < 0)
283 return rc;
284
285 /* Leading edge */
286 if (eprn->leading_edge_set) {
287 if ((rc = param_write_int(plist, "LeadingEdge", &eprn->default_orientation))
288 < 0) return rc;
289 }
290 else
291 if ((rc = param_write_null(plist, "LeadingEdge")) < 0) return rc;
292
293 /* Media configuration file */
294 if (eprn->media_file == NULL) {
295 if ((rc = param_write_null(plist, "MediaConfigurationFile")) < 0)
296 return rc;
297 }
298 else {
299 string_value.data = (const byte *)eprn->media_file;
300 string_value.size = strlen((const char *)string_value.data);
301 string_value.persistent = false;
302 if ((rc =
303 param_write_string(plist, "MediaConfigurationFile", &string_value)) < 0)
304 return rc;
305 }
306
307 #ifndef EPRN_GS_HAS_MEDIAPOSITION
308 /* Requested input media position */
309 if (eprn->media_position_set) {
310 if ((rc = param_write_int(plist, "MediaPosition", &eprn->media_position))
311 < 0) return rc;
312 }
313 else
314 if ((rc = param_write_null(plist, "MediaPosition")) < 0) return rc;
315 #endif /* EPRN_GS_HAS_MEDIAPOSITION */
316
317 #ifndef EPRN_NO_PAGECOUNTFILE
318 /* Page count file */
319 if (eprn->pagecount_file == NULL) {
320 if ((rc = param_write_null(plist, "PageCountFile")) < 0) return rc;
321 }
322 else {
323 string_value.data = (const byte *)eprn->pagecount_file;
324 string_value.size = strlen((const char *)string_value.data);
325 string_value.persistent = false;
326 if ((rc = param_write_string(plist, "PageCountFile", &string_value)) < 0)
327 return rc;
328 }
329 #endif /* EPRN_NO_PAGECOUNTFILE */
330
331 return 0;
332 }
333
334 /******************************************************************************
335
336 Function: is_word
337
338 This function returns a non-zero value iff the string beginning at 's' is
339 identical with the string pointed to by 'word' and is followed either by a
340 blank character or '\0'.
341
342 ******************************************************************************/
343
344 static int is_word(const char *s, const char *word)
345 {
346 size_t l = strlen(word);
347 if (strncmp(s, word, l) != 0) return 0;
348 return s[l] == '\0' || isspace(s[l]);
349 }
350
351 /******************************************************************************
352
353 Function: next_word
354
355 This function returns a pointer to the beginning of the next blank-separated
356 word in the string pointed to by 's'. If s[0] is not blank, the character is
357 considered to be part of the current word, i.e. the word to be returned is
358 the one following.
359
360 If there is no next word in this sense, the function returns NULL.
361
362 ******************************************************************************/
363
364 static char *next_word(char *s)
365 {
366 /* Skip current word */
367 while (*s != '\0' && !isspace(*s)) s++;
368
369 /* Skip intermediate blanks */
370 while (*s != '\0' && isspace(*s)) s++;
371
372 return *s == '\0'? NULL: s;
373 }
374
375 /******************************************************************************
376
377 Function: eprn_read_media_data
378
379 This function reads a media configuration file and stores the result in
380 '*eprn'. The file name must already have been stored in 'eprn->media_file',
381 'eprn->media_overrides' should be NULL.
382
383 The function returns zero on success and a non-zero ghostscript error value
384 otherwise. In the latter case, an error message will have been issued.
385
386 ******************************************************************************/
387
388 #define BUFFER_SIZE 200
389 /* should be large enough for a single line */
390
391 #define cleanup() (free(list), gp_fclose(f))
392
393 static int eprn_read_media_data(mediasize_table *tables, eprn_Eprn *eprn, gs_memory_t *memory)
394 {
395 char buffer[BUFFER_SIZE];
396 const char
397 *epref = eprn->CUPS_messages? CUPS_ERRPREF: "",
398 *wpref = eprn->CUPS_messages? CUPS_WARNPREF: "";
399 gp_file *f;
400 float conversion_factor = BP_PER_IN;
401 /* values read have to be multiplied by this value to obtain bp */
402 int
403 line = 0, /* line number */
404 read = 0; /* number of entries read so far */
405 eprn_PageDescription *list = NULL;
406
407 /* Open the file */
408 if ((f = gp_fopen(memory, eprn->media_file, "r")) == NULL) {
409 eprintf5("%s" ERRPREF "Error opening the media configuration file\n"
410 "%s `%s'\n%s for reading: %s.\n",
411 epref, epref, eprn->media_file, epref, strerror(errno));
412 return_error(gs_error_invalidfileaccess);
413 }
414
415 /* Loop over input lines */
416 while (gp_fgets(buffer, BUFFER_SIZE, f) != NULL) {
417 char *s, *t;
418 eprn_PageDescription *current;
419 int chars_read;
420
421 line++;
422
423 /* Check for buffer overflow */
424 if ((s = strchr(buffer, '\n')) == NULL && gp_fgetc(f) != EOF) {
425 eprintf5("%s" ERRPREF "Exceeding line length %d in "
426 "media configuration file\n%s %s, line %d.\n",
427 epref, BUFFER_SIZE - 2 /* '\n'+'\0' */, epref, eprn->media_file, line);
428 cleanup();
429 return_error(gs_error_limitcheck);
430 }
431
432 /* Eliminate the newline character */
433 if (s != NULL) *s = '\0';
434
435 /* Originally, I did nothing further at this point and used a
436 "%g %g %g %g %n" format in the sscanf() call below to skip trailing
437 blanks. This does not work with Microsoft Visual C up to at least
438 version 6 (_MSC_VER is 1200) because the variable for %n will never be
439 set. If one drops the blank, it will be set, also if there are
440 additional directives after %n. In addition, Cygwin does not (as of
441 early 2001) set the %n variable if there is trailing white space in the
442 string scanned. I don't want to know what's going on there, I just
443 foil these bugs by removing all trailing white space from the input
444 line which means I don't have to scan it afterwards.
445 */
446 if (s == NULL) s = strchr(buffer, '\0');
447 while (buffer < s && isspace(*(s-1))) s--;
448 *s = '\0';
449
450 /* Ignore blank and comment lines */
451 s = buffer;
452 while (isspace(*s)) s++;
453 if (*s == '\0' || *s == '#') continue;
454
455 /* Check for unit specification */
456 if (is_word(s, "unit")) {
457 char *unit_name = next_word(s);
458 if (unit_name != NULL) {
459 s = next_word(unit_name);
460 if (s == NULL) {
461 if (is_word(unit_name, "in")) {
462 conversion_factor = BP_PER_IN;
463 continue;
464 }
465 if (is_word(unit_name, "mm")) {
466 conversion_factor = BP_PER_MM;
467 continue;
468 }
469 }
470 /* If 's' is not NULL or the unit is not recognized, the error message
471 will be generated when the attempt to read the whole line as a media
472 specification will fail because there is no media size called
473 "unit". */
474 }
475 }
476
477 /* Extend the list */
478 {
479 eprn_PageDescription *new_list;
480 new_list = (eprn_PageDescription *)
481 realloc(list, ((size_t)read+1)*sizeof(eprn_PageDescription));
482 if (new_list == NULL) {
483 eprintf2("%s" ERRPREF
484 "Memory allocation failure in eprn_read_media_data(): %s.\n",
485 epref, strerror(errno));
486 cleanup();
487 return_error(gs_error_VMerror);
488 }
489 list = new_list;
490 }
491
492 /* Set 'current' on the new entry */
493 current = list + read;
494
495 /* Isolate and identify the media size name */
496 s = buffer;
497 while (isspace(*s)) s++;
498 t = s + 1; /* we checked above that the line is not empty */
499 while (*t != '\0' && !isspace(*t)) t++;
500 if (*t != '\0') {
501 *t = '\0';
502 t++;
503 }
504 {
505 ms_MediaCode code = ms_find_code_from_name(tables, s, eprn->flag_desc);
506 if (code == ms_none) {
507 eprintf5("%s" ERRPREF "Unknown media name (%s) in "
508 "media configuration file\n%s %s, line %d.\n",
509 epref, s, epref, eprn->media_file, line);
510 cleanup();
511 return_error(gs_error_rangecheck);
512 }
513 if (code & MS_ROTATED_FLAG) {
514 eprintf5("%s" ERRPREF "Invalid substring \"" MS_ROTATED_STRING
515 "\" in media name (%s)\n"
516 "%s in media configuration file %s, line %d.\n",
517 epref, s, epref, eprn->media_file, line);
518 cleanup();
519 return_error(gs_error_rangecheck);
520 }
521 current->code = code;
522 }
523
524 /* Look for margins */
525 if (sscanf(t, "%g %g %g %g%n", &current->left,
526 &current->bottom, &current->right, &current->top, &chars_read) != 4 ||
527 t[chars_read] != '\0') {
528 if (*t != '\0') *(t-1) = ' '; /* remove NUL after media name */
529 eprintf5("%s" ERRPREF
530 "Syntax error in media configuration file %s, line %d:\n%s %s\n",
531 epref, eprn->media_file, line, epref, buffer);
532 cleanup();
533 return_error(gs_error_rangecheck);
534 }
535
536 /* Check for sign */
537 if (current->left < 0 || current->bottom < 0 || current->right < 0 ||
538 current->top < 0) {
539 eprintf4("%s" ERRPREF
540 "Ghostscript does not support negative margins (line %d in the\n"
541 "%s media configuration file %s).\n",
542 epref, line, epref, eprn->media_file);
543 cleanup();
544 return_error(gs_error_rangecheck);
545 }
546
547 read++;
548
549 /* Convert to bp */
550 current->left *= conversion_factor;
551 current->bottom *= conversion_factor;
552 current->right *= conversion_factor;
553 current->top *= conversion_factor;
554
555 /* A margin for custom page sizes without the corresponding capability in
556 the printer is useless although it would not lead to a failure of eprn.
557 The user might not notice the reason without help, hence we check. */
558 if (ms_without_flags(current->code) == ms_CustomPageSize &&
559 eprn->cap->custom == NULL)
560 eprintf6("%s" WARNPREF "The media configuration file %s\n"
561 "%s contains a custom page size entry in line %d, "
562 "but custom page sizes\n"
563 "%s are not supported by the %s.\n",
564 wpref, eprn->media_file, wpref, line, wpref, eprn->cap->name);
565 }
566 if (gp_ferror(f)) {
567 eprintf2("%s" ERRPREF
568 "Unidentified system error while reading `%s'.\n",
569 epref, eprn->media_file);
570 cleanup();
571 return_error(gs_error_invalidfileaccess);
572 }
573 gp_fclose(f);
574
575 /* Was the file empty? */
576 if (read == 0) {
577 eprintf3("%s" ERRPREF "The media configuration file %s\n"
578 "%s does not contain any media information.\n",
579 epref, eprn->media_file, epref);
580 return_error(gs_error_rangecheck);
581 }
582
583 /* Create a list in the device structure */
584 eprn->media_overrides = (eprn_PageDescription *) gs_malloc(memory, read + 1,
585 sizeof(eprn_PageDescription), "eprn_read_media_data");
586 if (eprn->media_overrides == NULL) {
587 eprintf1("%s" ERRPREF
588 "Memory allocation failure from gs_malloc() in eprn_read_media_data().\n",
589 epref);
590 free(list);
591 return_error(gs_error_VMerror);
592 }
593
594 /* Copy the list and set the sentinel entry */
595 memcpy(eprn->media_overrides, list, read*sizeof(eprn_PageDescription));
596 eprn->media_overrides[read].code = ms_none;
597
598 /* Cleanup */
599 free(list);
600
601 return 0;
602 }
603
604 #undef BUFFER_SIZE
605 #undef cleanup
606
607 /******************************************************************************
608
609 Function: eprn_set_media_data
610
611 This function sets the media size and margin information in an 'eprn' device
612 from the specified media configuration file.
613
614 The return code will be zero an success and a ghostscript error code
615 otherwise. In the latter case, an error message will have been issued.
616
617 The 'length' may be positive in which case it denotes the length of the
618 string 'media_file' or zero in which case the string is assumed to be
619 NUL-terminated.
620
621 A NULL value or an empty string for 'media_file' is permitted and removes
622 all previous media descriptions read from a media configuration file.
623
624 ******************************************************************************/
625
626 int eprn_set_media_data(eprn_Device *dev, const char *media_file, size_t length)
627 {
628 eprn_Eprn *eprn = &dev->eprn;
629 const char *epref = eprn->CUPS_messages? CUPS_ERRPREF: "";
630 int rc = 0;
631
632 /* Any previous size determination is obsolete now */
633 eprn->code = ms_none;
634
635 /* Free old storage */
636 if (eprn->media_file != NULL) {
637 gs_free(dev->memory->non_gc_memory, eprn->media_file, strlen(eprn->media_file) + 1,
638 sizeof(char), "eprn_set_media_data");
639 eprn->media_file = NULL;
640 }
641 if (eprn->media_overrides != NULL) {
642 int n = 0;
643 while (eprn->media_overrides[n].code != ms_none) n++;
644 gs_free(dev->memory->non_gc_memory, eprn->media_overrides, n+1, sizeof(eprn_PageDescription),
645 "eprn_set_media_data");
646 eprn->media_overrides = NULL;
647 }
648
649 /* Set the file name length if not given */
650 if (media_file != NULL && length == 0) length = strlen(media_file);
651
652 /* Read media configuration file, unless the name is NULL or the empty
653 string */
654 if (media_file != NULL && length > 0) {
655 eprn->media_file = (char *)gs_malloc(dev->memory->non_gc_memory, length + 1, sizeof(char),
656 "eprn_set_media_data");
657 if (eprn->media_file == NULL) {
658 eprintf1("%s" ERRPREF
659 "Memory allocation failure from gs_malloc() in "
660 "eprn_set_media_data().\n",
661 epref);
662 rc = gs_error_VMerror;
663 }
664 else {
665 strncpy(eprn->media_file, media_file, length);
666 eprn->media_file[length] = '\0';
667 if ((rc = eprn_read_media_data(&dev->eprn.table, eprn, dev->memory->non_gc_memory)) != 0) {
668 gs_free(dev->memory->non_gc_memory, eprn->media_file, length + 1, sizeof(char),
669 "eprn_set_media_data");
670 eprn->media_file = NULL;
671 }
672 }
673 }
674
675 return rc;
676 }
677
678 /******************************************************************************
679
680 Function: eprn_bits_for_levels
681
682 This function returns the number of bits used to represent 'levels' intensity
683 levels. 'levels' must be <= (ULONG_MAX+1)/2.
684
685 ******************************************************************************/
686
687 unsigned int eprn_bits_for_levels(unsigned int levels)
688 {
689 unsigned int bits = 0;
690 unsigned long n;
691
692 for (n = 1; n < levels; n *= 2) bits++;
693
694 return bits;
695 }
696
697 /******************************************************************************
698
699 Function: res_supported
700
701 'list' must either bei NULL (all resolutions are accepted) or point to a
702 list of resolutions terminated with a {0.0, 0.0} entry.
703
704 ******************************************************************************/
705
706 static bool res_supported(const eprn_Resolution *list, float hres, float vres)
707 {
708 if (list == NULL) return true;
709
710 while (list->h > 0.0 && (list->h != hres || list->v != vres)) list++;
711
712 return list->h > 0.0;
713 }
714
715 /******************************************************************************
716
717 Function: levels_supported
718
719 'list' may not be NULL and must point to a {0,0}-terminated list of
720 supported ranges.
721
722 ******************************************************************************/
723
724 static bool levels_supported(const eprn_IntensityLevels *list,
725 unsigned int levels)
726 {
727 while (list->from > 0 && (levels < list->from || list->to < levels)) list++;
728
729 return list->from > 0;
730 }
731
732 /******************************************************************************
733
734 Function: reslev_supported
735
736 ******************************************************************************/
737
738 static int reslev_supported(const eprn_ResLev *entry, float hres, float vres,
739 unsigned int levels)
740 {
741 return res_supported(entry->resolutions, hres, vres) &&
742 levels_supported(entry->levels, levels);
743 }
744
745 /******************************************************************************
746
747 Function: eprn_check_colour_info
748
749 This function checks the arguments starting at 'model' whether they are
750 supported according to 'list'. This list must satisfy the constraints for
751 'colour_info' in 'eprn_PrinterDescription'.
752
753 The function returns zero if the values are supported and a non-zero value
754 if they are not.
755
756 ******************************************************************************/
757
758 int eprn_check_colour_info(const eprn_ColourInfo *list,
759 eprn_ColourModel *model, float *hres, float *vres,
760 unsigned int *black_levels, unsigned int *non_black_levels)
761 {
762 const eprn_ColourInfo *entry;
763
764 /* Search for a match. Successful exits are in the middle of the loop. */
765 for (entry = list; entry->info[0] != NULL; entry++)
766 if (entry->colour_model == *model ||
767 (entry->colour_model == eprn_DeviceCMYK &&
768 *model == eprn_DeviceCMY_plus_K)) {
769 const eprn_ResLev *rl;
770 unsigned int levels = (entry->colour_model == eprn_DeviceRGB ||
771 entry->colour_model == eprn_DeviceCMY? *non_black_levels:
772 *black_levels);
773
774 for (rl = entry->info[0]; rl->levels != NULL; rl++)
775 if (reslev_supported(rl, *hres, *vres, levels)) {
776 const eprn_ResLev *rl2 = NULL;
777
778 /* Check on info[1] needed? */
779 if (entry->colour_model == eprn_DeviceGray ||
780 entry->colour_model == eprn_DeviceRGB ||
781 entry->colour_model == eprn_DeviceCMY)
782 return 0;
783
784 /* CMY+K or CMYK process colour models */
785 if (entry->info[1] != NULL) {
786 for (rl2 = entry->info[1]; rl2->levels != NULL; rl2++)
787 if (reslev_supported(rl2, *hres, *vres, *non_black_levels)) break;
788 }
789 if ((entry->info[1] == NULL && *black_levels == *non_black_levels) ||
790 (entry->info[1] != NULL && rl2->levels != NULL))
791 return 0;
792 }
793 }
794
795 return -1;
796 }
797
798 /******************************************************************************
799
800 Function: set_derived_colour_data
801
802 This routine determines and sets various derived values in the device
803 structure based on the number of black and non-black levels requested.
804
805 The values to be set are 'eprn.bits_per_colorant' and the fields 'depth',
806 'max_gray', 'max_color', 'dither_grays' and 'dither_colors' in the
807 'color_info' structure.
808
809 The parameters 'black_levels' and 'non_black_levels' must be in the range
810 0 to 256 without 1. At least one of the two must be positive.
811
812 ******************************************************************************/
813
814 static void set_derived_colour_data(eprn_Device *dev)
815 {
816 eprn_Eprn *eprn = &dev->eprn;
817 unsigned int levels;
818
819 /* Choose equal number of bits in 'gx_color_index' for all components present
820 */
821 if (eprn->intensity_rendering == eprn_IR_FloydSteinberg) levels = 256;
822 else if (eprn->black_levels >= eprn->non_black_levels)
823 levels = eprn->black_levels;
824 else levels = eprn->non_black_levels;
825 eprn->bits_per_colorant = eprn_bits_for_levels(levels);
826
827 /* For the depth, consider all components and adjust to possible values.
828 Ghostscript permits pixel depths 1, 2, 4, 8, 16, 24 and 32. */
829 dev->color_info.depth =
830 (eprn->non_black_levels == 0? 1: 4) * eprn->bits_per_colorant;
831 /* No distinction between non-Gray colour models */
832 if (dev->color_info.depth > 2) {
833 if (dev->color_info.depth <= 4) dev->color_info.depth = 4;
834 else if (dev->color_info.depth <= 8) dev->color_info.depth = 8;
835 else dev->color_info.depth = ((dev->color_info.depth + 7)/8)*8;
836 /* Next multiple of 8 */
837 }
838
839 /* Set ghostscript's color_info data. This is an area where ghostscript's
840 documentation (Drivers.htm) is not particularly intelligible. For
841 example: can there be situations where the dither_* parameters are
842 different from their corresponding max_* parameter plus one?
843 */
844 if (eprn->intensity_rendering != eprn_IR_halftones) {
845 /* Here we cover two cases: retaining as much colour information as
846 possible and effectively setting up 1-pixel halftone cells. Both
847 demand that ghostscript is prevented from doing halftoning; the
848 remaining difference (which is the essential part) is then handled in
849 our colour mapping functions.
850 According to Drivers.htm, a value of at least 31 for max_gray or
851 max_color (actually, the documentation says "max_rgb") leads to
852 ghostscript using the colour returned by the device if valid
853 instead of using the dither parameters for halftoning. The actual
854 values for the max_* parameters should then be irrelevant.
855 */
856 if (eprn->non_black_levels > 0) dev->color_info.max_color = 255;
857 else dev->color_info.max_color = 0;
858 dev->color_info.max_gray = 255;
859
860 /* As long as our colour mapping functions return valid color indices, the
861 following parameters should be irrelevant. */
862 dev->color_info.dither_grays = 256;
863 if (dev->color_info.num_components == 1) dev->color_info.dither_colors = 0;
864 else dev->color_info.dither_colors = dev->color_info.max_color + 1;
865 }
866 else {
867 /* Let ghostscript do halftoning */
868 if (eprn->non_black_levels > 0)
869 dev->color_info.max_color = eprn->non_black_levels - 1;
870 else dev->color_info.max_color = 0;
871 if (eprn->black_levels > 0)
872 dev->color_info.max_gray = eprn->black_levels - 1;
873 else dev->color_info.max_gray = dev->color_info.max_color;
874
875 /* The dither parameters */
876 if (eprn->black_levels > 0)
877 dev->color_info.dither_grays = eprn->black_levels;
878 else dev->color_info.dither_grays = eprn->non_black_levels;
879 if (eprn->non_black_levels > 0)
880 dev->color_info.dither_colors = eprn->non_black_levels;
881 else dev->color_info.dither_colors = 0;
882 }
883
884 return;
885 }
886
887 /******************************************************************************
888
889 Function: eprn_put_params
890
891 This function reads a parameter list, extracts the parameters known to the
892 'eprn' device, and configures the device appropriately. This includes
893 parameters defined by base devices.
894
895 If an error occurs in the processing of parameters, the function will
896 return a negative value, otherwise zero.
897
898 This function does *not* exhibit transactional behaviour as requested in
899 gsparam.h, i.e. on error the parameter values in the device structure
900 might have changed. However, all values will be individually valid.
901
902 For most of the parameters an attempt to set them closes the device if it is
903 open.
904
905 ******************************************************************************/
906
907 int eprn_put_params(gx_device *dev, gs_param_list *plist)
908 {
909 bool colour_mode_given_and_valid = false;
910 gs_param_name pname;
911 gs_param_string string_value;
912 eprn_Eprn *eprn = &((eprn_Device *)dev)->eprn;
913 const char
914 *epref = eprn->CUPS_messages? CUPS_ERRPREF: "",
915 *wpref = eprn->CUPS_messages? CUPS_WARNPREF: "";
916 float mediasize[2];
917 int
918 height = dev->height,
919 last_error = 0,
920 temp,
921 rc,
922 width = dev->width;
923
924 #ifdef EPRN_TRACE
925 if (gs_debug_c(EPRN_TRACE_CHAR)) {
926 dmlprintf(dev->memory,
927 "! eprn_put_params() called with the following device parameters:\n");
928 eprn_dump_parameter_list(plist);
929 }
930 #endif /* EPRN_TRACE */
931
932 eprn_replace_fillpage(dev);
933
934 /* Remember initial page size */
935 for (temp = 0; temp < 2; temp++) mediasize[temp] = dev->MediaSize[temp];
936
937 /* CUPS message format. This should be the first parameter to be set which is
938 a problem because it should happen even before the put_params methods of
939 derived devices are called. However, note that in a CUPS filter
940 "CUPSMessages" will usually be set from the command line while most
941 remaining parameters will be set via setpagedevice. Hence this will come
942 first anyway except for problems for which the print administrator is
943 responsible, not the ordinary user. */
944 if ((rc = param_read_bool(plist, "CUPSMessages", &eprn->CUPS_messages)) == 0)
945 {
946 epref = eprn->CUPS_messages? CUPS_ERRPREF: "";
947 wpref = eprn->CUPS_messages? CUPS_WARNPREF: "";
948 }
949 else if (rc < 0) last_error = rc;
950
951 /* Read colour model into 'temp'. For those colonials across the pond we also
952 accept the barbarized spelling variant.
953 */
954 #define colour_model(option) \
955 if ((rc = param_read_string(plist, (pname = option), &string_value)) == 0) { \
956 rc = eprn_get_int(&string_value, eprn_colour_model_list, &temp); \
957 if (rc != 0) { \
958 if (rc != gs_error_VMerror) { \
959 eprintf1("%s" ERRPREF "Unknown colour model: `", epref); \
960 errwrite(dev->memory, (const char *)string_value.data, sizeof(char)*string_value.size); \
961 eprintf("'.\n"); \
962 } \
963 last_error = rc; \
964 param_signal_error(plist, pname, last_error); \
965 } \
966 else colour_mode_given_and_valid = true; \
967 } \
968 else if (rc < 0) last_error = rc;
969
970 colour_model("ColorModel")
971 colour_model("ColourModel") /* overrides if both are given */
972
973 #undef colour_model
974
975 if (colour_mode_given_and_valid) {
976 if (eprn->colour_model != temp && dev->is_open) gs_closedevice(dev);
977 /* The close_device method can fail, but what should I do then? */
978 eprn->colour_model = temp;
979
980 /* Set the native colour space */
981 switch(eprn->colour_model) {
982 case eprn_DeviceGray:
983 dev->color_info.num_components = 1; break;
984 case eprn_DeviceRGB:
985 /*FALLTHROUGH*/
986 case eprn_DeviceCMY:
987 /*FALLTHROUGH*/
988 case eprn_DeviceCMY_plus_K:
989 dev->color_info.num_components = 3; break;
990 case eprn_DeviceCMYK:
991 dev->color_info.num_components = 4; break;
992 default:
993 assert(0);
994 }
995
996 dev->color_info.polarity =
997 dci_std_polarity(dev->color_info.num_components);
998
999 /* Adjust black levels */
1000 if (eprn->colour_model == eprn_DeviceCMY ||
1001 eprn->colour_model == eprn_DeviceRGB) {
1002 if (eprn->black_levels != 0) eprn->black_levels = 0;
1003 }
1004 else
1005 if (eprn->black_levels == 0) eprn->black_levels = 2;
1006 /* Adjust non-black levels if they are too small for colour */
1007 if (dev->color_info.num_components > 1 && eprn->non_black_levels <= 0)
1008 eprn->non_black_levels = 2;
1009
1010 /* Adjustments to pixel depth, 'max_gray', 'max_color' and dither levels
1011 will occur near the end of this routine */
1012 }
1013
1014 /* BlackLevels. Various depending values will be adjusted below. */
1015 if ((rc = param_read_int(plist, (pname = "BlackLevels"), &temp)) == 0) {
1016 if ((temp == 0 && (eprn->colour_model == eprn_DeviceRGB ||
1017 eprn->colour_model == eprn_DeviceCMY)) ||
1018 (2 <= temp && temp <= 256 &&
1019 eprn->colour_model != eprn_DeviceRGB &&
1020 eprn->colour_model != eprn_DeviceCMY)) {
1021 if (eprn->black_levels != temp && dev->is_open) gs_closedevice(dev);
1022 eprn->black_levels = temp;
1023 }
1024 else {
1025 eprintf2("%s" ERRPREF
1026 "The value for BlackLevels is outside the range permitted: %d.\n",
1027 epref, temp);
1028 last_error = gs_error_rangecheck;
1029 param_signal_error(plist, pname, last_error);
1030 }
1031 }
1032 else if (rc < 0) last_error = rc;
1033
1034 /* CMYLevels */
1035 if ((rc = param_read_int(plist, (pname = "CMYLevels"), &temp)) == 0) {
1036 if ((temp == 0 && eprn->colour_model == eprn_DeviceGray) ||
1037 (2 <= temp && temp <= 256 && eprn->colour_model != eprn_DeviceGray)) {
1038 if (eprn->non_black_levels != temp && dev->is_open) gs_closedevice(dev);
1039 eprn->non_black_levels = temp;
1040 }
1041 else {
1042 eprintf2("%s" ERRPREF
1043 "The value for CMYLevels is outside the range permitted: %d.\n",
1044 epref, temp);
1045 last_error = gs_error_rangecheck;
1046 param_signal_error(plist, pname, last_error);
1047 }
1048 }
1049 else if (rc < 0) last_error = rc;
1050
1051 /* CUPS page accounting messages */
1052 {
1053 bool temp;
1054 if ((rc = param_read_bool(plist, "CUPSAccounting", &temp)) == 0) {
1055 if (eprn->CUPS_accounting && !temp)
1056 eprintf(CUPS_WARNPREF WARNPREF
1057 "Attempt to set CUPSAccounting from true to false.\n");
1058 else eprn->CUPS_accounting = temp;
1059 }
1060 else if (rc < 0) last_error = rc;
1061 }
1062
1063 /* Intensity rendering */
1064 if ((rc = param_read_string(plist, (pname = "IntensityRendering"),
1065 &string_value)) == 0) {
1066 rc = eprn_get_int(&string_value, intensity_rendering_list, &temp);
1067 if (rc == 0) {
1068 if (temp != eprn->intensity_rendering && dev->is_open)
1069 gs_closedevice(dev);
1070 eprn->intensity_rendering = temp;
1071 }
1072 else {
1073 eprintf1("%s" ERRPREF "Invalid method for IntensityRendering: `",
1074 epref);
1075 errwrite(dev->memory, (const char *)string_value.data, sizeof(char)*string_value.size);
1076 eprintf("'.\n");
1077 last_error = gs_error_rangecheck;
1078 param_signal_error(plist, pname, last_error);
1079 }
1080 }
1081 else if (rc < 0) last_error = rc;
1082
1083 /* Leading edge */
1084 if ((rc = param_read_null(plist, (pname = "LeadingEdge"))) == 0) {
1085 if (eprn->leading_edge_set && dev->is_open) gs_closedevice(dev);
1086 eprn->leading_edge_set = false;
1087 }
1088 else if (rc < 0 && rc != gs_error_typecheck) last_error = rc;
1089 else if ((rc = param_read_int(plist, (pname = "LeadingEdge"), &temp)) == 0) {
1090 if (0 <= temp && temp <= 3) {
1091 if ((!eprn->leading_edge_set || eprn->default_orientation != temp) &&
1092 dev->is_open) gs_closedevice(dev);
1093 eprn->leading_edge_set = true;
1094 eprn->default_orientation = temp;
1095 }
1096 else {
1097 eprintf2(
1098 "%s" ERRPREF "LeadingEdge may only have values 0 to 3, not %d.\n",
1099 epref, temp);
1100 last_error = gs_error_rangecheck;
1101 param_signal_error(plist, pname, last_error);
1102 }
1103 }
1104 else if (rc < 0) last_error = rc;
1105
1106 /* Media configuration file */
1107 if ((rc = param_read_null(plist, (pname = "MediaConfigurationFile"))) == 0) {
1108 if (eprn->media_file != NULL && dev->is_open) gs_closedevice(dev);
1109 rc = eprn_set_media_data((eprn_Device *)dev, NULL, 0);
1110 }
1111 else if (rc < 0 && rc != gs_error_typecheck) last_error = rc;
1112 else if ((rc = param_read_string(plist, pname, &string_value)) == 0) {
1113 if (string_value.size > 0) {
1114 if ((eprn->media_file == NULL ||
1115 strncmp(eprn->media_file, (const char *)string_value.data,
1116 string_value.size) != 0 ||
1117 eprn->media_file[string_value.size] != '\0') && dev->is_open)
1118 gs_closedevice(dev);
1119 rc = eprn_set_media_data((eprn_Device *)dev,
1120 (const char *)string_value.data, string_value.size);
1121 }
1122 else {
1123 if (eprn->media_file != NULL && dev->is_open) gs_closedevice(dev);
1124 rc = eprn_set_media_data((eprn_Device *)dev, NULL, 0);
1125 }
1126
1127 if (rc != 0) {
1128 last_error = rc;
1129 param_signal_error(plist, pname, last_error);
1130 }
1131 }
1132 else if (rc < 0) last_error = rc;
1133
1134 #ifndef EPRN_GS_HAS_MEDIAPOSITION
1135 if ((rc = param_read_null(plist, (pname = "MediaPosition"))) == 0)
1136 eprn->media_position_set = false;
1137 else if (rc < 0 && (rc = param_read_int(plist, pname, &eprn->media_position))
1138 == 0) {
1139 /* Current (up to at least gs 6.50) ghostscript versions do not accept
1140 negative MediaPosition values. */
1141 if (eprn->media_position < 0)
1142 eprintf3("%s" WARNPREF
1143 "Ghostscript does not accept negative values (%d) for the\n"
1144 "%s `MediaPosition' parameter.\n",
1145 wpref, eprn->media_position, wpref);
1146 /* The error message is left for ghostscript to generate during input
1147 media selection, should such an entry be a match. */
1148 eprn->media_position_set = true;
1149 }
1150 else if (rc < 0) last_error = rc;
1151 #endif /* EPRN_GS_HAS_MEDIAPOSITION */
1152
1153 #ifndef EPRN_NO_PAGECOUNTFILE
1154 /* Page count file */
1155 if ((rc = param_read_null(plist, (pname = "PageCountFile"))) == 0) {
1156 if (eprn->pagecount_file != NULL) {
1157 gs_free(dev->memory->non_gc_memory, eprn->pagecount_file, strlen(eprn->pagecount_file) + 1,
1158 sizeof(char), "eprn_put_params");
1159 eprn->pagecount_file = NULL;
1160 }
1161 }
1162 else if (rc < 0 && rc != gs_error_typecheck) last_error = rc;
1163 else if ((rc = param_read_string(plist, pname, &string_value)) == 0) {
1164 /* Free old storage */
1165 if (eprn->pagecount_file != NULL) {
1166 gs_free(dev->memory->non_gc_memory, eprn->pagecount_file, strlen(eprn->pagecount_file) + 1,
1167 sizeof(char), "eprn_put_params");
1168 eprn->pagecount_file = NULL;
1169 }
1170
1171 /* Store file name unless it is the empty string */
1172 if (string_value.size > 0) {
1173 eprn->pagecount_file = (char *)gs_malloc(dev->memory->non_gc_memory, string_value.size + 1,
1174 sizeof(char), "eprn_put_params");
1175 if (eprn->pagecount_file == NULL) {
1176 eprintf1( "%s" ERRPREF
1177 "Memory allocation failure from gs_malloc() in eprn_put_params().\n",
1178 epref);
1179 last_error = gs_error_VMerror;
1180 param_signal_error(plist, pname, last_error);
1181 }
1182 else {
1183 strncpy(eprn->pagecount_file, (const char *)string_value.data,
1184 string_value.size);
1185 eprn->pagecount_file[string_value.size] = '\0';
1186 }
1187 }
1188 }
1189 #endif /* EPRN_NO_PAGECOUNTFILE */
1190
1191 /* RGBLevels */
1192 if ((rc = param_read_int(plist, (pname = "RGBLevels"), &temp)) == 0) {
1193 if (temp == 0 || (2 <= temp && temp <= 256)) {
1194 if (eprn->non_black_levels != temp && dev->is_open) gs_closedevice(dev);
1195 eprn->non_black_levels = temp;
1196 }
1197 else {
1198 eprintf2("%s" ERRPREF
1199 "The value for RGBLevels is outside the range permitted: %d.\n",
1200 epref, temp);
1201 last_error = gs_error_rangecheck;
1202 param_signal_error(plist, pname, last_error);
1203 }
1204 }
1205 else if (rc < 0) last_error = rc;
1206
1207 /* Determine various derived colour parameters */
1208 set_derived_colour_data((eprn_Device *)dev);
1209
1210 /* Catch a specification of BitsPerPixel --- otherwise a wrong value gives
1211 just a rangecheck error from gs without any readily understandable
1212 information (see gx_default_put_params()). This confused one hpdj user
1213 so much that he wrote in a newsgroup that gs was dumping core in this
1214 case :-).
1215 */
1216 if ((rc = param_read_int(plist, (pname = "BitsPerPixel"), &temp)) == 0) {
1217 if (temp != dev->color_info.depth) {
1218 eprintf3("%s" ERRPREF
1219 "Attempt to set `BitsPerPixel' to a value (%d)\n"
1220 "%s other than the one selected by the driver.\n",
1221 epref, temp, epref);
1222 last_error = gs_error_rangecheck;
1223 param_signal_error(plist, pname, last_error);
1224 }
1225 } else if (rc < 0) last_error = rc;
1226
1227 /* Check whether ".HWMargins" is specified and, if it is, refrain from
1228 overwriting it. */
1229 {
1230 gs_param_typed_value temp;
1231 if (param_read_typed(plist, ".HWMargins", &temp) == 0) {
1232 /* ".HWMargins" is specified */
1233 #ifdef EPRN_TRACE
1234 if_debug1(EPRN_TRACE_CHAR, "! .HWMargins is specified: type is %d.\n",
1235 (int)temp.type);
1236 #endif
1237 eprn->keep_margins = true;
1238 }
1239 }
1240
1241 /* Process parameters defined by base classes (should occur after treating
1242 parameters defined for the derived class, see gsparam.h) */
1243 rc = gdev_prn_put_params(dev, plist);
1244 if (rc < 0 || (rc > 0 && last_error >= 0))
1245 last_error = rc;
1246
1247 if (last_error < 0) return_error(last_error);
1248
1249 /* If the page size was modified, close the device */
1250 if (dev->is_open && (dev->width != width || dev->height != height ||
1251 mediasize[0] != dev->MediaSize[0] || mediasize[1] != dev->MediaSize[1])) {
1252 gs_closedevice(dev);
1253 #ifdef EPRN_TRACE
1254 if_debug0(EPRN_TRACE_CHAR,
1255 "! Closing device because of page size modification.\n");
1256 #endif
1257 }
1258
1259 return last_error;
1260 }