Debian Codegraph @

contrib/pcl3/src/gdevpcl3.c

raw
1 /******************************************************************************
2 File: $Id: gdevpcl3.c,v 1.32 2001/08/14 15:22:35 Martin Rel $
3 Contents: Ghostscript device 'pcl3' for PCL-3+ printers
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 GS_REVISION (integer)
17 If defined, this must be the ghostscript version number, e.g., 601 for
18 ghostscript 6.01.
19
20 PCL3_MEDIA_FILE (const char *)
21 Define this to set a media configuration file for the "unspec" device
22 unless the user overrides it.
23
24 ******************************************************************************/
25
26 /*****************************************************************************/
27
28 #ifndef _XOPEN_SOURCE
29 #define _XOPEN_SOURCE 500
30 #endif
31
32 /* Special Aladdin header, must be included before <sys/types.h> on some
33 platforms (e.g., FreeBSD). */
34 #include "std.h"
35
36 /* Standard headers */
37 #include <assert.h>
38 #include <ctype.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43
44 /* Ghostscript headers */
45 #ifdef EPRN_TRACE
46 #include "gdebug.h"
47 #endif /* EPRN_TRACE */
48
49 /* Driver-specific headers */
50 #include "gdeveprn.h"
51 #include "pclcap.h"
52 #include "pclgen.h"
53 #include "pclsize.h"
54
55 /*****************************************************************************/
56
57 /* Does the argument point to an instance of the generic (pcl3) device? */
58 #define is_generic_device(dev) (strcmp(dev->dname, "pcl3") == 0)
59
60 /*****************************************************************************/
61
62 /* Combined type with a range of bool plus null */
63 typedef enum {bn_null, bn_true, bn_false} bool_or_null;
64
65 /* Type for duplex capabilities */
66 typedef enum {Duplex_none, Duplex_sameLeadingEdge, Duplex_oppositeLeadingEdge,
67 Duplex_both} DuplexCapabilities;
68
69 /* Device structure */
70 typedef struct {
71 gx_eprn_device_common; /* eprn part including base types */
72
73 /* Printer selection and other data not directly mappable to PCL */
74 pcl_Printer printer;
75 bool_or_null use_card;
76 DuplexCapabilities duplex_capability;
77 bool tumble; /* only relevant if 'Duplex' is 'true' */
78
79 /* PCL generation */
80 bool
81 initialized, /* Has init() been run on this device instance? */
82 configured, /* Has the output file been configured? */
83 configure_every_page; /* Repeat the configuration for every page? */
84 pcl_FileData file_data;
85 pcl3_sizetable table;
86 } pcl3_Device;
87
88 /*****************************************************************************/
89
90 /* Device procedures */
91 static dev_proc_open_device(pcl3_open_device);
92 static dev_proc_close_device(pcl3_close_device);
93 static dev_proc_get_params(pcl3_get_params);
94 static dev_proc_put_params(pcl3_put_params);
95
96 /* Device procedures */
97 static void
98 eprn_initialize_device_procs(gx_device *dev)
99 {
100 gdev_prn_initialize_device_procs(dev);
101
102 set_dev_proc(dev, open_device, pcl3_open_device);
103 set_dev_proc(dev, get_initial_matrix, eprn_get_initial_matrix);
104 set_dev_proc(dev, close_device, pcl3_close_device);
105 set_dev_proc(dev, map_rgb_color, eprn_map_rgb_color_for_CMY_or_K);
106 set_dev_proc(dev, map_color_rgb, eprn_map_color_rgb);
107 set_dev_proc(dev, map_cmyk_color, eprn_map_cmyk_color_glob);
108 set_dev_proc(dev, get_params, pcl3_get_params);
109 set_dev_proc(dev, put_params, pcl3_put_params);
110 set_dev_proc(dev, fillpage, eprn_fillpage);
111 };
112
113 /* prn procedure implementations */
114 #if !defined(GS_REVISION) || GS_REVISION >= 550
115 static prn_dev_proc_print_page(pcl3_print_page);
116 #else
117 static dev_proc_print_page(pcl3_print_page);
118 #endif
119
120 /*****************************************************************************/
121
122 /* Media flags known to this device in addition to the standard ones */
123 static const ms_Flag
124 flag_description[] = {
125 {PCL_CARD_FLAG, PCL_CARD_STRING},
126 {ms_none, NULL}
127 };
128
129 /* List of possible optional media flags */
130 static const ms_MediaCode
131 card_is_optional[] = {PCL_CARD_FLAG, ms_none};
132
133 /*****************************************************************************/
134
135 /* Forward declaration */
136 static void pcl3_flag_mismatch_reporter(
137 const struct s_eprn_Device *eprn, bool no_match);
138
139 /* Macro for creating device structure instances */
140 #define pcl3_device_instance(dname, printer) \
141 pcl3_Device gs_##dname##_device = { \
142 eprn_device_initdata( \
143 pcl3_Device, /* device type */ \
144 eprn_initialize_device_procs, /* initialize dev_procs */ \
145 #dname, /* device name */ \
146 300.0, 300.0, /* horizontal and vertical resolution */\
147 pcl3_print_page, /* print page routine */ \
148 &pcl3_printers[printer].desc, /* printer capability description */ \
149 flag_description, /* flag descriptions */ \
150 ms_none, /* desired media flags */ \
151 card_is_optional, /* list of optional flags */ \
152 &pcl3_flag_mismatch_reporter), /* reporting function */\
153 printer, /* printer */ \
154 bn_null, /* use_card */ \
155 Duplex_none, /* duplex_capability */ \
156 false, /* tumble */ \
157 false /* initialized */ \
158 /* The remaining fields will be set in init(). */ \
159 }
160
161 /* Generic and flexible device structure instance */
162 pcl3_device_instance(pcl3, pcl3_generic_new);
163
164 /* Printer-specific and fixed device structure instances */
165 /* At present there is no entry for the HP DeskJet because its natural name
166 collides with the hpdj driver. */
167 pcl3_device_instance(hpdjplus, HPDeskJetPlus);
168 pcl3_device_instance(hpdjportable, HPDJPortable);
169 pcl3_device_instance(hpdj310, HPDJ310);
170 pcl3_device_instance(hpdj320, HPDJ320);
171 pcl3_device_instance(hpdj340, HPDJ340);
172 pcl3_device_instance(hpdj400, HPDJ400);
173 pcl3_device_instance(hpdj500, HPDJ500);
174 pcl3_device_instance(hpdj500c, HPDJ500C);
175 pcl3_device_instance(hpdj510, HPDJ510);
176 pcl3_device_instance(hpdj520, HPDJ520);
177 pcl3_device_instance(hpdj540, HPDJ540);
178 pcl3_device_instance(hpdj550c, HPDJ550C);
179 pcl3_device_instance(hpdj560c, HPDJ560C);
180 pcl3_device_instance(hpdj600, HPDJ600);
181 pcl3_device_instance(hpdj660c, HPDJ660C);
182 pcl3_device_instance(hpdj670c, HPDJ670C);
183 pcl3_device_instance(hpdj680c, HPDJ680C);
184 pcl3_device_instance(hpdj690c, HPDJ690C);
185 pcl3_device_instance(hpdj850c, HPDJ850C);
186 pcl3_device_instance(hpdj855c, HPDJ855C);
187 pcl3_device_instance(hpdj870c, HPDJ870C);
188 pcl3_device_instance(hpdj890c, HPDJ890C);
189 pcl3_device_instance(hpdj1120c, HPDJ1120C);
190
191 /*****************************************************************************/
192
193 #define ERRPREF "? pcl3: "
194 #define WARNPREF "?-W pcl3: "
195
196 #define array_size(a) (sizeof(a)/sizeof(a[0]))
197
198 /*****************************************************************************/
199
200 static const eprn_StringAndInt
201 /* Names for duplex capabilities */
202 duplex_capabilities_list[] = {
203 { "none", Duplex_none },
204 { "sameLeadingEdge", Duplex_sameLeadingEdge },
205 { "oppositeLeadingEdge", Duplex_oppositeLeadingEdge },
206 { "both", Duplex_both },
207 { NULL, 0 }
208 },
209 /* Names for PCL Media Type values */
210 media_type_list[] = {
211 /* Canonical names */
212 { "plain paper", 0 },
213 { "bond paper", 1 },
214 { "HP Premium paper", 2 },
215 { "glossy paper", 3 },
216 { "transparency film", 4 },
217 { "quick dry glossy", 5 },
218 { "quick dry transparency", 6 },
219 /* Shortened names */
220 { "plain", 0 },
221 { "bond", 1 },
222 { "Premium", 2 },
223 { "glossy", 3 },
224 { "transparency", 4 },
225 { NULL, 0 }
226 },
227 /* Print Quality */
228 print_quality_list[] = {
229 { "draft", -1 },
230 { "normal", 0 },
231 { "presentation", 1 },
232 /* Start of synonyms */
233 { "econo", -1 },
234 { "best", 1 },
235 { NULL, 0 }
236 },
237 /* Subdevice names. They must be ordered by 'value' except for the last
238 (NULL) entry. At present, there are 26 non-NULL entries here. */
239 subdevice_list[] = {
240 { "hpdj", HPDeskJet },
241 { "hpdjplus", HPDeskJetPlus },
242 { "hpdjportable", HPDJPortable },
243 { "hpdj310", HPDJ310 },
244 { "hpdj320", HPDJ320 },
245 { "hpdj340", HPDJ340 },
246 { "hpdj400", HPDJ400 },
247 { "hpdj500", HPDJ500 },
248 { "hpdj500c", HPDJ500C },
249 { "hpdj510", HPDJ510 },
250 { "hpdj520", HPDJ520 },
251 { "hpdj540", HPDJ540 },
252 { "hpdj550c", HPDJ550C },
253 { "hpdj560c", HPDJ560C },
254 { "unspecold", pcl3_generic_old },
255 { "hpdj600", HPDJ600 },
256 { "hpdj660c", HPDJ660C },
257 { "hpdj670c", HPDJ670C },
258 { "hpdj680c", HPDJ680C },
259 { "hpdj690c", HPDJ690C },
260 { "hpdj850c", HPDJ850C },
261 { "hpdj855c", HPDJ855C },
262 { "hpdj870c", HPDJ870C },
263 { "hpdj890c", HPDJ890C },
264 { "hpdj1120c", HPDJ1120C },
265 { "unspec", pcl3_generic_new },
266 { NULL, 0 }
267 };
268
269 /******************************************************************************
270
271 Function: cmp_by_value
272
273 This function compares two 'eprn_StringAndInt' instances by their 'value'
274 fields.
275
276 ******************************************************************************/
277
278 static int cmp_by_value(const void *a, const void *b)
279 {
280 return ((const eprn_StringAndInt *)a)->value -
281 ((const eprn_StringAndInt *)b)->value;
282 }
283
284 /******************************************************************************
285
286 Function: get_string_for_int
287
288 This function returns a string representation of 'in_value' in '*out_value',
289 based on 'table'. 'table' must be an array terminated with an entry having
290 NULL as the 'name' value and must be permanently allocated and constant.
291 If 'in_value' cannot be found in 'table', the function returns a decimal
292 representation of 'in_value'.
293
294 The string buffer in '*out_value' will be a permanently allocated area which
295 must not be modified.
296
297 ******************************************************************************/
298
299 static void get_string_for_int(int in_value, const eprn_StringAndInt *table,
300 gs_param_string *out_value)
301 {
302 while (table->name != NULL && table->value != in_value) table++;
303 if (table->name != NULL) {
304 out_value->data = (const byte *)table->name;
305 out_value->size = strlen(table->name);
306 out_value->persistent = true;
307 }
308 else {
309 char buffer[22]; /* Must be sufficient for an 'int' */
310
311 gs_snprintf(buffer, sizeof(buffer), "%d", in_value);
312 assert(strlen(buffer) < sizeof(buffer));
313 out_value->data = (const byte *)buffer;
314 out_value->size = strlen(buffer);
315 out_value->persistent = false;
316 }
317
318 return;
319 }
320
321 /******************************************************************************
322
323 Function: get_int_for_string
324
325 This function parses 'in_value' based on 'table' and returns the result in
326 '*out_value'. 'table' must be an array, terminated with an entry having NULL
327 as the value for 'name'.
328
329 'in_value' must either be a decimal representation of an integer or must be
330 a string present in 'table'. In these cases, the function returns 0,
331 otherwise a non-zero ghostscript error value.
332
333 On returning 'gs_error_VMerror', the function will have issued an error
334 message.
335
336 ******************************************************************************/
337
338 static int get_int_for_string(const gs_param_string *in_value,
339 const eprn_StringAndInt *table, int *out_value)
340 {
341 char *s;
342 int read; /* counter */
343
344 /* First we construct a properly NUL-terminated string */
345 s = (char *) malloc(in_value->size + 1);
346 if (s == NULL) {
347 eprintf1(ERRPREF
348 "Memory allocation failure in get_int_for_string(): %s.\n",
349 strerror(errno));
350 return_error(gs_error_VMerror);
351 }
352 strncpy(s, (const char *)in_value->data, in_value->size);
353 s[in_value->size] = '\0';
354
355 /* To foil bugs in sscanf() on Windows (see gdeveprn.c in eprn) I'm removing
356 trailing white space here instead of skipping it with a format. */
357 {
358 char *t = strchr(s, '\0');
359
360 while (s < t && isspace(*(t-1))) t--;
361 *t = '\0';
362 }
363
364 /* Check for a numerical value */
365 if (sscanf(s, "%d%n", out_value, &read) != 1 || s[read] != '\0') {
366 /* What the user specified is not a valid numerical value */
367 while (table->name != NULL && strcmp(table->name, s) != 0) table++;
368 if (table->name == NULL) {
369 free(s); s = NULL;
370 return_error(gs_error_rangecheck);
371 }
372 *out_value = table->value;
373 }
374
375 free(s); s = NULL;
376
377 return 0;
378 }
379
380 /******************************************************************************
381
382 Function: init
383
384 This function does that part of initialization which cannot be performed at
385 compile time or which must be repeated whenever the subdevice is changed.
386 It must be called if 'initialized' is false and after the subdevice is
387 changed.
388
389 When this function is called, 'dev->printer' must have been set correctly.
390
391 This function must not and does not depend on the state of initialization of
392 its base devices.
393
394 ******************************************************************************/
395
396 static void init(pcl3_Device *dev)
397 {
398 #ifndef NDEBUG
399 /* Check that 'subdevice_list' is sorted by 'value' */
400 {
401 int j;
402 for (j = 1; j < array_size(subdevice_list) - 1; j++)
403 assert(cmp_by_value(subdevice_list + j - 1, subdevice_list + j) <= 0);
404 }
405 #endif /* !NDEBUG */
406
407 /* Base class fields */
408 if (is_generic_device(dev)) dev->Duplex_set = 0;
409 /* "Duplex" is null. See remarks on the "Duplex" page device parameter in
410 pcl3_put_params(). */
411
412 /* pcl3 fields */
413 dev->use_card = bn_null;
414 dev->duplex_capability = Duplex_none;
415 dev->tumble = false;
416 dev->configured = false;
417 dev->configure_every_page = false;
418
419 /* Initialize 'file_data' */
420 pcl3_fill_defaults(dev->printer, &dev->file_data);
421
422 dev->initialized = true;
423
424 return;
425 }
426
427 /******************************************************************************
428
429 Function: pcl3_flag_mismatch_reporter
430
431 Flag mismatch reporting function for the pcl3 device.
432
433 The 'desired_flags' field can only contain MS_BIG_FLAG and PCL_CARD_FLAG.
434
435 ******************************************************************************/
436
437 static void pcl3_flag_mismatch_reporter(
438 const struct s_eprn_Device *eprn, bool no_match)
439 {
440 const char *epref = eprn->CUPS_messages? CUPS_ERRPREF: "";
441
442 if (eprn->desired_flags == 0) {
443 eprintf2(
444 "%s" ERRPREF "The %s does not support the requested media properties.\n",
445 epref, eprn->cap->name);
446 }
447 else if (eprn->desired_flags == MS_BIG_FLAG) {
448 eprintf2("%s" ERRPREF "The %s does not support banner printing",
449 epref, eprn->cap->name);
450 if (!no_match) eprintf(" for this size");
451 eprintf(".\n");
452 }
453 else if (eprn->desired_flags == PCL_CARD_FLAG) {
454 eprintf2("%s" ERRPREF
455 "The %s does not support a `Card' variant for ",
456 epref, eprn->cap->name);
457 if (no_match) eprintf("any"); else eprintf("this");
458 eprintf(" size.\n");
459 }
460 else {
461 eprintf1(
462 "%s" ERRPREF "Banner printing on postcards?? You must be joking!\n",
463 epref);
464 }
465
466 return;
467 }
468
469 /******************************************************************************
470
471 Function: find_subdevice_name
472
473 This function returns a pointer to a static storage location containing
474 a NUL-terminated string with the name of the subdevice for 'subdev'.
475
476 It must not be called for invalid 'subdev' values.
477
478 ******************************************************************************/
479
480 static const char *find_subdevice_name(int subdev)
481 {
482 eprn_StringAndInt
483 key = {NULL, 0};
484 const eprn_StringAndInt
485 *found;
486
487 key.value = subdev;
488
489 found = (const eprn_StringAndInt *)bsearch(&key, subdevice_list,
490 array_size(subdevice_list) - 1, sizeof(eprn_StringAndInt), cmp_by_value);
491 assert(found != NULL);
492
493 return found->name;
494 }
495
496 /******************************************************************************
497
498 Function: pcl3_get_params
499
500 This function returns to the caller information about the values of
501 parameters defined for the device. This includes parameters defined in base
502 classes.
503
504 The function returns zero on success and a negative value on error.
505
506 ******************************************************************************/
507
508 static int pcl3_get_params(gx_device *device, gs_param_list *plist)
509 {
510 gs_param_string string_value;
511 pcl3_Device *dev = (pcl3_Device *)device;
512 const pcl_FileData *data = &dev->file_data;
513 int
514 temp, /* Used as an intermediate for various reasons */
515 rc;
516
517 /* Constructor */
518 if (!dev->initialized) init(dev);
519
520 /* Base class parameters */
521 rc = eprn_get_params(device, plist);
522 if (rc < 0) return rc;
523
524 /* Compression method */
525 temp = data->compression;
526 if ((rc = param_write_int(plist, "CompressionMethod", &temp)) < 0) return rc;
527
528 /* Configure every page */
529 if ((rc = param_write_bool(plist, "ConfigureEveryPage",
530 &dev->configure_every_page)) < 0) return rc;
531
532 /* Dry time */
533 if (data->dry_time < 0) {
534 if ((rc = param_write_null(plist, "DryTime")) < 0) return rc;
535 }
536 else if ((rc = param_write_int(plist, "DryTime", &data->dry_time)) < 0)
537 return rc;
538
539 /* Duplex capability */
540 if (is_generic_device(dev)) {
541 eprn_get_string(dev->duplex_capability, duplex_capabilities_list,
542 &string_value);
543 if ((rc = param_write_string(plist, "DuplexCapability", &string_value)) < 0)
544 return rc;
545 }
546
547 /* Manual feed */
548 {
549 bool temp = dev->file_data.manual_feed;
550 if ((rc = param_write_bool(plist, "ManualFeed", &temp)) < 0) return rc;
551 }
552
553 /* PCL media type */
554 get_string_for_int(data->media_type, media_type_list, &string_value);
555 if ((rc = param_write_string(plist, "Medium", &string_value)) < 0)
556 return rc;
557
558 /* Media destination */
559 if ((rc = param_write_int(plist, "%MediaDestination",
560 &data->media_destination)) < 0) return rc;
561
562 /* Media source */
563 if ((rc = param_write_int(plist, "%MediaSource", &data->media_source)) < 0)
564 return rc;
565
566 /* Use of Configure Raster Data */
567 if (is_generic_device(dev) || pcl_has_CRD(data->level)) {
568 bool temp = (data->level == pcl_level_3plus_CRD_only);
569 if ((rc = param_write_bool(plist, "OnlyCRD", &temp)) < 0) return rc;
570 }
571
572 /* PCL initilization strings */
573 if (data->init1.length == 0) {
574 if ((rc = param_write_null(plist, "PCLInit1")) < 0) return rc;
575 }
576 else {
577 string_value.data = (const byte *)data->init1.str;
578 string_value.size = data->init1.length;
579 string_value.persistent = false;
580 if ((rc = param_write_string(plist, "PCLInit1", &string_value)) < 0)
581 return rc;
582 }
583 if (data->init2.length == 0) {
584 if ((rc = param_write_null(plist, "PCLInit2")) < 0) return rc;
585 }
586 else {
587 string_value.data = (const byte *)data->init2.str;
588 string_value.size = data->init2.length;
589 string_value.persistent = false;
590 if ((rc = param_write_string(plist, "PCLInit2", &string_value)) < 0)
591 return rc;
592 }
593
594 /* PJL job name */
595 if (data->PJL_job == NULL) {
596 if ((rc = param_write_null(plist, "PJLJob")) < 0) return rc;
597 }
598 else {
599 string_value.data = (const byte *)data->PJL_job;
600 string_value.size = strlen(data->PJL_job);
601 string_value.persistent = false;
602 if ((rc = param_write_string(plist, "PJLJob", &string_value)) < 0)
603 return rc;
604 }
605
606 /* PJL language */
607 if (data->PJL_language == NULL) {
608 if ((rc = param_write_null(plist, "PJLLanguage")) < 0) return rc;
609 }
610 else {
611 string_value.data = (const byte *)data->PJL_language;
612 string_value.size = strlen(data->PJL_language);
613 string_value.persistent = false;
614 if ((rc = param_write_string(plist, "PJLLanguage", &string_value)) < 0)
615 return rc;
616 }
617
618 /* Print quality */
619 get_string_for_int(data->print_quality, print_quality_list, &string_value);
620 if ((rc = param_write_string(plist, "PrintQuality", &string_value)) < 0)
621 return rc;
622
623 /* Black bit planes first (standard PCL) or last */
624 {
625 bool temp = (data->order_CMYK == TRUE);
626 if ((rc = param_write_bool(plist, "SendBlackLast", &temp)) < 0) return rc;
627 }
628
629 /* NUL header */
630 if ((rc = param_write_int(plist, "SendNULs", &data->NULs_to_send)) < 0)
631 return rc;
632
633 /* Subdevice name, but only for the generic device */
634 if (is_generic_device(dev)) {
635 const char *name = find_subdevice_name(dev->printer);
636 string_value.data = (const byte *)name;
637 string_value.size = strlen(name);
638 string_value.persistent = true;
639 if ((rc = param_write_string(plist, "Subdevice", &string_value)) < 0)
640 return rc;
641 }
642
643 /* Tumble */
644 if (is_generic_device(dev))
645 if ((rc = param_write_bool(plist, "Tumble", &dev->tumble)) < 0) return rc;
646
647 /* UseCard */
648 if (dev->use_card == bn_null) {
649 if ((rc = param_write_null(plist, "UseCard")) < 0) return rc;
650 }
651 else {
652 bool temp = (dev->use_card == bn_true);
653 if ((rc = param_write_bool(plist, "UseCard", &temp)) < 0) return rc;
654 }
655
656 /* The old quality commands if they have meaning for this device */
657 if (pcl_use_oldquality(data->level)) {
658 /* Depletion */
659 if (data->depletion == 0) {
660 if ((rc = param_write_null(plist, "Depletion")) < 0) return rc;
661 }
662 else if ((rc = param_write_int(plist, "Depletion", &data->depletion)) < 0)
663 return rc;
664
665 /* Raster Graphics Quality */
666 if ((rc = param_write_int(plist, "RasterGraphicsQuality",
667 &data->raster_graphics_quality)) < 0) return rc;
668
669 /* Shingling */
670 if ((rc = param_write_int(plist, "Shingling", &data->shingling)) < 0)
671 return rc;
672 }
673 else if (is_generic_device(dev)) {
674 /* It is logically wrong for these parameters to be visible if we are
675 dealing with a group-3 device, but I haven't yet found a way to get
676 around ghostscript's undefined-parameter-update problem. */
677 if ((rc = param_write_null(plist, "Depletion")) < 0) return rc;
678 if ((rc = param_write_null(plist, "RasterGraphicsQuality")) < 0) return rc;
679 if ((rc = param_write_null(plist, "Shingling")) < 0) return rc;
680 }
681
682 #ifdef EPRN_TRACE
683 if (gs_debug_c(EPRN_TRACE_CHAR)) {
684 dmlprintf(dev->memory, "! pcl3_get_params() returns the following parameters:\n");
685 eprn_dump_parameter_list(plist);
686 }
687 #endif
688
689 return 0;
690 }
691
692 /******************************************************************************
693
694 Function: fetch_octets
695
696 This function checks whether 'plist' contains a string entry for the parameter
697 'pname' and returns the value via '*s' if it does.
698
699 Neither 'plist' nor 'pname' may be NULL. On entry, '*s' must be a valid octet
700 string; if it is non-zero, 's->str' must point to a gs_malloc()-allocated
701 storage area of length 's->length'.
702
703 If the parameter exists in 'plist', its type must be null or string. If it is
704 null, '*s' will become zero. If the parameter type is string, the value will
705 be copied to '*s'.
706
707 The function returns a negative ghostscript error code on error and zero
708 otherwise. In the former case an error message will have been issued,
709 using 'epref' as a prefix for the message.
710
711 ******************************************************************************/
712
713 static int fetch_octets(const char *epref,
714 gs_param_list *plist, const char *pname, pcl_OctetString *s)
715 {
716 gs_param_string string_value;
717 int rc;
718
719 if ((rc = param_read_null(plist, pname)) == 0) {
720 if (s->length != 0)
721 gs_free(plist->memory->non_gc_memory, s->str, s->length, sizeof(pcl_Octet), "fetch_octets");
722 s->str = NULL;
723 s->length = 0;
724 }
725 else if (rc < 0 &&
726 (rc = param_read_string(plist, pname, &string_value)) == 0) {
727 /* Free old storage */
728 if (s->length != 0)
729 gs_free(plist->memory->non_gc_memory, s->str, s->length, sizeof(pcl_Octet), "fetch_octets");
730
731 /* Allocate new */
732 s->str = (pcl_Octet *)gs_malloc(plist->memory->non_gc_memory, string_value.size, sizeof(pcl_Octet),
733 "fetch_octets");
734
735 if (s->str == NULL) {
736 s->length = 0;
737 eprintf1("%s" ERRPREF
738 "Memory allocation failure from gs_malloc().\n", epref);
739 rc = gs_error_VMerror;
740 param_signal_error(plist, pname, rc);
741 }
742 else {
743 memcpy(s->str, string_value.data, string_value.size);
744 s->length = string_value.size;
745 }
746 }
747 else if (rc > 0) rc = 0;
748
749 return rc;
750 }
751
752 /******************************************************************************
753
754 Function: fetch_cstring
755
756 This function checks whether 'plist' contains a string entry for the parameter
757 'pname' and returns the value via '*s' if it does.
758
759 Neither 'plist' nor 'pname' may be NULL. On entry, '*s' must be NULL or point
760 to a NUL-terminated string in a gs_malloc()-allocated storage area.
761
762 If the parameter exists in 'plist', its type must be null or string. If it is
763 null, '*s' will be gs_free()d and set to NULL. If it is a string, '*s' will
764 be reallocated to contain the NUL-terminated value of the string. Should the
765 string already contain a NUL value, only the part up to the NUL will be
766 copied.
767
768 The function returns a negative ghostscript error code on error and zero
769 otherwise. In the former case an error message will have been issued.
770
771 ******************************************************************************/
772
773 static int fetch_cstring(const char *epref,
774 gs_param_list *plist, const char *pname, char **s)
775 {
776 gs_param_string string_value;
777 int rc;
778
779 if ((rc = param_read_null(plist, pname)) == 0) {
780 if (*s != NULL) gs_free(plist->memory->non_gc_memory, *s, strlen(*s) + 1, sizeof(char), "fetch_cstring");
781 *s = NULL;
782 }
783 else if (rc < 0 &&
784 (rc = param_read_string(plist, pname, &string_value)) == 0) {
785 /* Free old storage */
786 if (*s != NULL) gs_free(plist->memory->non_gc_memory, *s, strlen(*s) + 1, sizeof(char), "fetch_cstring");
787
788 /* Allocate new */
789 *s = (char *)gs_malloc(plist->memory->non_gc_memory, string_value.size + 1, sizeof(char),
790 "fetch_cstring");
791
792 if (*s == NULL) {
793 eprintf1("%s" ERRPREF
794 "Memory allocation failure from gs_malloc().\n", epref);
795 rc = gs_error_VMerror;
796 param_signal_error(plist, pname, rc);
797 }
798 else {
799 strncpy(*s, (const char*)string_value.data, string_value.size);
800 (*s)[string_value.size] = '\0';
801 }
802 }
803 else if (rc > 0) rc = 0;
804
805 return rc;
806 }
807
808 /******************************************************************************
809
810 Function: set_palette
811
812 This function sets 'dev->file_data.palette' and some other fields in
813 agreement with 'dev->eprn.colour_model'.
814
815 ******************************************************************************/
816
817 static void set_palette(pcl3_Device *dev)
818 {
819 pcl_FileData *data = &dev->file_data;
820
821 switch(dev->eprn.colour_model) {
822 case eprn_DeviceGray:
823 {
824 const eprn_ColourInfo *ci = dev->eprn.cap->colour_info;
825
826 /* Can this printer switch palettes? */
827 while (ci->info[0] != NULL && ci->colour_model == eprn_DeviceGray) ci++;
828 if (ci->info[0] != NULL) data->palette = pcl_black;
829 else data->palette = pcl_no_palette;
830 }
831 data->number_of_colorants = 1;
832 data->depletion = 0; /* Depletion is only meaningful for colour. */
833 break;
834 case eprn_DeviceCMY:
835 data->palette = pcl_CMY;
836 data->number_of_colorants = 3;
837 break;
838 case eprn_DeviceRGB:
839 data->palette = pcl_RGB;
840 data->number_of_colorants = 3;
841 break;
842 case eprn_DeviceCMY_plus_K:
843 /*FALLTHROUGH*/
844 case eprn_DeviceCMYK:
845 data->palette = pcl_CMYK;
846 data->number_of_colorants = 4;
847 break;
848 default:
849 assert(0);
850 }
851
852 return;
853 }
854
855 /******************************************************************************
856
857 Function: pcl3_put_params
858
859 This function reads a parameter list, extracts the parameters known to the
860 device, and configures the device appropriately. This includes parameters
861 defined by base classes.
862
863 If an error occurs in the processing of parameters, the function will
864 return a negative value, otherwise zero.
865
866 This function does *not* exhibit transactional behaviour as requested in
867 gsparam.h, i.e. on error the parameter values in the device structure
868 might have changed. However, all values will be individually valid.
869
870 Some of the parameters determine derived data in base classes or are relevant
871 for device initialization. Setting any of these parameters closes the
872 device if it is open.
873
874 ******************************************************************************/
875
876 static int pcl3_put_params(gx_device *device, gs_param_list *plist)
877 {
878 bool new_quality = false; /* has someone requested the new variables? */
879 gs_param_name pname;
880 gs_param_string string_value;
881 pcl3_Device *dev = (pcl3_Device *)device;
882 const char
883 *epref = dev->eprn.CUPS_messages? CUPS_ERRPREF: "",
884 *wpref = dev->eprn.CUPS_messages? CUPS_WARNPREF: "";
885 eprn_ColourModel previous_colour_model = dev->eprn.colour_model;
886 pcl_FileData *data = &dev->file_data;
887 int
888 last_error = 0,
889 temp,
890 rc;
891 struct {
892 int depletion, quality, shingling;
893 } requested = {-1, -1, -1};
894 /* old quality parameters. -1 means "not specified". */
895
896 /* Check for subdevice selection */
897 if (is_generic_device(dev)) {
898 if ((rc = param_read_string(plist, (pname = "Subdevice"), &string_value))
899 == 0) {
900 /* This property must be a known string. */
901 int j = 0;
902 while (subdevice_list[j].name != NULL &&
903 (string_value.size != strlen(subdevice_list[j].name) ||
904 strncmp((const char *)string_value.data, subdevice_list[j].name,
905 string_value.size) != 0))
906 j++;
907 /* param_read_string() does not return NUL-terminated strings. */
908 if (subdevice_list[j].name != NULL) {
909 if (dev->is_open) gs_closedevice(device);
910 dev->printer = subdevice_list[j].value;
911 dev->initialized = false;
912 rc = eprn_init_device((eprn_Device *)dev, &pcl3_printers[dev->printer].desc);
913 if (rc < 0) {
914 last_error = rc;
915 param_signal_error(plist, pname, last_error);
916 }
917 }
918 else {
919 eprintf1("%s" ERRPREF "Unknown subdevice name: `", epref);
920 errwrite(dev->memory,
921 (const char *)string_value.data,
922 sizeof(char)*string_value.size);
923 eprintf("'.\n");
924 last_error = gs_error_rangecheck;
925 param_signal_error(plist, pname, last_error);
926 }
927 }
928 else if (rc < 0) last_error = rc;
929 }
930
931 /* Constructor */
932 if (!dev->initialized) init(dev);
933
934 /* Compression method */
935 if ((rc = param_read_int(plist, (pname = "CompressionMethod"), &temp))
936 == 0) {
937 if (temp != pcl_cm_none && temp != pcl_cm_rl && temp != pcl_cm_tiff &&
938 temp != pcl_cm_delta && temp != pcl_cm_crdr) {
939 eprintf2("%s" ERRPREF "Unsupported compression method: %d.\n",
940 epref, temp);
941 last_error = gs_error_rangecheck;
942 param_signal_error(plist, pname, last_error);
943 }
944 else {
945 if (temp == pcl_cm_crdr && (dev->printer == HPDeskJet ||
946 dev->printer == HPDeskJetPlus || dev->printer == HPDJ500)) {
947 /* This I know to be the case for the DJ 500. The others are guessed. */
948 eprintf2(
949 "%s" ERRPREF "The %s does not support compression method 9.\n",
950 epref, dev->eprn.cap->name);
951 last_error = gs_error_rangecheck;
952 param_signal_error(plist, pname, last_error);
953 }
954 else data->compression= temp;
955 }
956 }
957 else if (rc < 0) last_error = rc;
958
959 /* Configure every page */
960 if ((rc = param_read_bool(plist, "ConfigureEveryPage",
961 &dev->configure_every_page)) < 0) last_error = rc;
962
963 /* Depletion */
964 if ((rc = param_read_null(plist, (pname = "Depletion"))) == 0)
965 requested.depletion = 0;
966 else if (rc < 0 && (rc = param_read_int(plist, pname, &temp)) == 0) {
967 if (1 <= temp && temp <= 5 && (dev->printer != HPDJ500C || temp <= 3))
968 requested.depletion = temp;
969 else {
970 eprintf2("%s" ERRPREF "Invalid value for depletion: %d.\n",
971 epref, temp);
972 last_error = gs_error_rangecheck;
973 param_signal_error(plist, pname, last_error);
974 }
975 }
976 else if (rc < 0) last_error = rc;
977
978 /* Dry time */
979 if ((rc = param_read_null(plist, (pname = "DryTime"))) == 0)
980 data->dry_time = -1;
981 else if (rc < 0 &&
982 (rc = param_read_int(plist, pname, &temp)) == 0) {
983 if (0 <= temp && temp <= 1200) {
984 if (dev->printer == HPDJ500 || dev->printer == HPDJ500C) {
985 /* According to HP (DJ6/8 p. 18), only some of the series 600 and 800
986 DeskJets respond to this command. I also suspect that the same is
987 true for pre-DeskJet-500 printers. This should not matter, though,
988 because the PCL interpreter should merely ignore the command.
989 Hence I'm giving an error message only in those cases where HP
990 explicitly states that the printer does not support the command.
991 */
992 eprintf2(
993 "%s" ERRPREF "The %s does not support setting a dry time.\n",
994 epref, dev->eprn.cap->name);
995 last_error = gs_error_rangecheck;
996 param_signal_error(plist, pname, last_error);
997 }
998 else data->dry_time = temp;
999 }
1000 else {
1001 eprintf2("%s" ERRPREF "Invalid value for the dry time: %d.\n",
1002 epref, temp);
1003 last_error = gs_error_rangecheck;
1004 param_signal_error(plist, pname, last_error);
1005 }
1006 }
1007 else if (rc < 0) last_error = rc;
1008
1009 /* Duplex capability */
1010 if (is_generic_device(dev)) {
1011 if ((rc = param_read_string(plist, (pname = "DuplexCapability"),
1012 &string_value)) == 0) {
1013 rc = eprn_get_int(&string_value, duplex_capabilities_list, &temp);
1014 if (rc == 0) {
1015 if (dev->printer == pcl3_generic_new ||
1016 dev->printer == pcl3_generic_old || temp == Duplex_none) {
1017 dev->duplex_capability = temp;
1018 if (dev->duplex_capability == Duplex_none)
1019 dev->Duplex_set = 0; /* force to "null" */
1020 }
1021 else {
1022 eprintf2("%s" ERRPREF
1023 "You can use a non-trivial value for DuplexCapability\n"
1024 "%s only for unspec and unspecold.\n", epref, epref);
1025 last_error = gs_error_rangecheck;
1026 param_signal_error(plist, pname, last_error);
1027 }
1028 }
1029 else {
1030 eprintf1("%s" ERRPREF "Invalid duplex capability: `", epref);
1031 errwrite(dev->memory,
1032 (const char *)string_value.data,
1033 sizeof(char)*string_value.size);
1034 eprintf("'.\n");
1035 last_error = gs_error_rangecheck;
1036 param_signal_error(plist, pname, last_error);
1037 }
1038 }
1039 else if (rc < 0) last_error = rc;
1040 }
1041
1042 /* Check on "Duplex". This parameter is really read in gdev_prn_put_params(),
1043 but we check here to prevent it being set unless the printer really
1044 supports duplex printing. I would prefer to use the 'Duplex_set' variable
1045 for controlling the appearance of this page device parameter, but if I do,
1046 I can set the parameter only from PostScript and not from the command line.
1047 */
1048 {
1049 bool temp;
1050 if ((rc = param_read_bool(plist, (pname = "Duplex"), &temp)) == 0 &&
1051 temp && dev->duplex_capability == Duplex_none) {
1052 if (dev->printer == pcl3_generic_new || dev->printer == pcl3_generic_old)
1053 eprintf3("%s" ERRPREF
1054 "The '%s' device does not support duplex printing unless\n"
1055 "%s 'DuplexCapability' is not 'none'.\n",
1056 epref, find_subdevice_name(dev->printer), epref);
1057 else
1058 eprintf2("%s" ERRPREF
1059 "The %s does not support duplex printing.\n",
1060 epref, dev->eprn.cap->name);
1061 last_error = gs_error_rangecheck;
1062 param_signal_error(plist, pname, last_error);
1063 }
1064 /* NO check of rc because "null" is legal. */
1065 }
1066
1067 /* Manual feed */
1068 {
1069 bool temp;
1070 if ((rc = param_read_bool(plist, (pname = "ManualFeed"), &temp)) == 0)
1071 dev->file_data.manual_feed = temp;
1072 else if (rc < 0) last_error = rc;
1073 }
1074
1075 /* PCL media type */
1076 if ((rc = param_read_string(plist, (pname = "Medium"), &string_value)) == 0) {
1077 /* We accept numerical and string values. Numerical values at present
1078 officially defined are 0-6, but not all printers know all these values.
1079 We give the user the benefit of the doubt, though, because the
1080 value is simply passed through to the printer, except for the older
1081 DeskJets where we map illegal values to "plain paper".
1082 If the user specifies a string, however, it must be a known one.
1083 */
1084 rc = get_int_for_string(&string_value, media_type_list, &temp);
1085 if (rc != 0) {
1086 if (rc != gs_error_VMerror) {
1087 eprintf1("%s" ERRPREF "Unknown medium: `", epref);
1088 errwrite(dev->memory,
1089 (const char *)string_value.data,
1090 sizeof(char)*string_value.size);
1091 eprintf("'.\n");
1092 }
1093 last_error = rc;
1094 param_signal_error(plist, pname, last_error);
1095 }
1096 else {
1097 new_quality = true;
1098 if (temp < 0 || 6 < temp)
1099 eprintf2("%s" WARNPREF "Unknown media type code: %d.\n",
1100 wpref, temp);
1101 pcl3_set_mediatype(data, temp);
1102 }
1103 }
1104 else if (rc < 0) last_error = rc;
1105
1106 /* Media destination */
1107 if ((rc = param_read_int(plist, (pname = "%MediaDestination"),
1108 &data->media_destination)) < 0) last_error = rc;
1109
1110 /* Media source */
1111 if ((rc = param_read_int(plist, (pname = "%MediaSource"),
1112 &data->media_source)) < 0) last_error = rc;
1113 else if (rc == 0 && dev->is_open) gs_closedevice(device);
1114 /* In pcl3, %MediaSource is relevant for banner printing and hence page
1115 layout which is determined in the open routine. */
1116
1117 /* Use of Configure Raster Data */
1118 if (is_generic_device(dev) || pcl_has_CRD(data->level)) {
1119 bool temp;
1120
1121 if ((rc = param_read_bool(plist, (pname = "OnlyCRD"), &temp)) == 0) {
1122 if (pcl_has_CRD(data->level))
1123 data->level = (temp? pcl_level_3plus_CRD_only: pcl_level_3plus_S68);
1124 else if (temp == true) {
1125 eprintf1("%s" ERRPREF
1126 "OnlyCRD may be set only for group-3 devices.\n", epref);
1127 last_error = gs_error_rangecheck;
1128 param_signal_error(plist, pname, last_error);
1129 }
1130 }
1131 else if (rc < 0) last_error = rc;
1132 }
1133
1134 /* PCL initialization string 1 */
1135 rc = fetch_octets(epref, plist, "PCLInit1", &data->init1);
1136 if (rc < 0) last_error = rc;
1137
1138 /* PCL initialization string 2 */
1139 rc = fetch_octets(epref, plist, "PCLInit2", &data->init2);
1140 if (rc < 0) last_error = rc;
1141
1142 /* PJL job name */
1143 rc = fetch_cstring(epref, plist, "PJLJob", &data->PJL_job);
1144 if (rc < 0) last_error = rc;
1145
1146 /* PJL language */
1147 rc = fetch_cstring(epref, plist, "PJLLanguage", &data->PJL_language);
1148 if (rc < 0) last_error = rc;
1149
1150 /* Print Quality */
1151 if ((rc = param_read_string(plist, (pname = "PrintQuality"), &string_value))
1152 == 0) {
1153 /* The only known values are -1, 0 and 1. Again, however, we assume the
1154 user knows what s/he is doing if another value is given. */
1155 rc = get_int_for_string(&string_value, print_quality_list, &temp);
1156 if (rc != 0) {
1157 if (rc != gs_error_VMerror) {
1158 eprintf1("%s" ERRPREF "Unknown print quality: `", epref);
1159 errwrite(dev->memory,
1160 (const char *)string_value.data,
1161 sizeof(char)*string_value.size);
1162 eprintf("'.\n");
1163 }
1164 last_error = rc;
1165 param_signal_error(plist, pname, last_error);
1166 }
1167 else {
1168 new_quality = true;
1169 if (temp < -1 || 1 < temp)
1170 eprintf2("%s" WARNPREF "Unknown print quality: %d.\n",
1171 wpref, temp);
1172 pcl3_set_printquality(data, temp);
1173 }
1174 }
1175 else if (rc < 0) last_error = rc;
1176
1177 /* Raster Graphics Quality */
1178 if ((rc = param_read_null(plist, (pname = "RasterGraphicsQuality"))) == 0)
1179 ; /* ignore */
1180 else if (rc < 0 &&
1181 (rc = param_read_int(plist, (pname = "RasterGraphicsQuality"), &temp))
1182 == 0) {
1183 if (0 <= temp && temp <= 2) requested.quality = temp;
1184 else {
1185 eprintf2(
1186 "%s" ERRPREF "Invalid value for raster graphics quality: %d.\n",
1187 epref, temp);
1188 last_error = gs_error_rangecheck;
1189 param_signal_error(plist, pname, last_error);
1190 }
1191 }
1192 else if (rc < 0) last_error = rc;
1193
1194 /* Colorant order */
1195 {
1196 bool temp;
1197 if ((rc = param_read_bool(plist, (pname = "SendBlackLast"), &temp)) == 0)
1198 data->order_CMYK = temp;
1199 else if (rc < 0 ) last_error = rc;
1200 }
1201
1202 /* Sending of NULs */
1203 if ((rc = param_read_int(plist, (pname = "SendNULs"), &temp)) == 0) {
1204 if (data->NULs_to_send >= 0) data->NULs_to_send = temp;
1205 else {
1206 eprintf2(
1207 "%s" ERRPREF "Invalid value for SendNULs parameter: %d.\n",
1208 epref, temp);
1209 last_error = gs_error_rangecheck;
1210 param_signal_error(plist, pname, last_error);
1211 }
1212 }
1213 else if (rc < 0 ) last_error = rc;
1214
1215 /* Shingling */
1216 if ((rc = param_read_null(plist, (pname = "Shingling"))) == 0)
1217 ; /* ignore */
1218 else if (rc < 0 &&
1219 (rc = param_read_int(plist, pname, &temp)) == 0) {
1220 if (0 <= temp && temp <= 2) requested.shingling = temp;
1221 else {
1222 eprintf2("%s" ERRPREF "Invalid value for shingling: %d.\n",
1223 epref, temp);
1224 last_error = gs_error_rangecheck;
1225 param_signal_error(plist, pname, last_error);
1226 }
1227 }
1228 else if (rc < 0) last_error = rc;
1229
1230 /* Tumble */
1231 if (is_generic_device(dev))
1232 if ((rc = param_read_bool(plist, (pname = "Tumble"), &dev->tumble)) < 0)
1233 last_error = rc;
1234
1235 /* UseCard */
1236 {
1237 bool temp;
1238
1239 if ((rc = param_read_null(plist, (pname = "UseCard"))) == 0)
1240 dev->use_card = bn_null;
1241 else if (rc < 0 &&
1242 (rc = param_read_bool(plist, pname, &temp)) == 0)
1243 dev->use_card = (temp? bn_true: bn_false);
1244 else if (rc < 0 ) last_error = rc;
1245 }
1246
1247 /* Process parameters defined by base classes (should occur after treating
1248 parameters defined for the derived class, see gsparam.h) */
1249 rc = eprn_put_params(device, plist);
1250 if (rc < 0 || (rc > 0 && last_error >= 0))
1251 last_error = rc;
1252
1253 /* Act if the colour model was changed */
1254 if (previous_colour_model != dev->eprn.colour_model) set_palette(dev);
1255
1256 if (last_error < 0) return_error(last_error);
1257
1258 /* If we have seen new quality parameters, derive the old ones from them
1259 based on the current and possibly new value of the palette. */
1260 if (new_quality) pcl3_set_oldquality(data);
1261
1262 /* If we have seen old quality parameters, store them */
1263 if (pcl_use_oldquality(data->level)) {
1264 if (requested.depletion >= 0) data->depletion = requested.depletion;
1265 if (requested.quality >= 0)
1266 data->raster_graphics_quality = requested.quality;
1267 if (requested.shingling >= 0) data->shingling = requested.shingling;
1268 }
1269
1270 return 0;
1271 }
1272
1273 /******************************************************************************
1274
1275 Function: pcl3_open_device
1276
1277 ******************************************************************************/
1278
1279 static int pcl3_open_device(gx_device *device)
1280 {
1281 pcl3_Device *dev = (pcl3_Device *)device;
1282 const char
1283 *epref = dev->eprn.CUPS_messages? CUPS_ERRPREF: "",
1284 *wpref = dev->eprn.CUPS_messages? CUPS_WARNPREF: "";
1285 int rc;
1286
1287 /* Constructor */
1288 if (!dev->initialized) init(dev);
1289
1290 #ifdef PCL3_MEDIA_FILE
1291 /* Change default media descriptions for 'unspec' */
1292 if (dev->eprn.media_file == NULL && dev->printer == pcl3_generic_new) {
1293 if ((rc = eprn_set_media_data(device, PCL3_MEDIA_FILE, 0)) != 0)
1294 return rc;
1295 }
1296 #endif
1297
1298 /* Check on rendering parameters */
1299 if ((dev->eprn.black_levels > 2 || dev->eprn.non_black_levels > 2) &&
1300 dev->file_data.print_quality == -1)
1301 eprintf2(
1302 "%s" WARNPREF "More than 2 intensity levels and draft quality\n"
1303 "%s are unlikely to work in combination.\n", wpref, wpref);
1304
1305 /* Ensure correct media request flags */
1306 eprn_set_media_flags((eprn_Device *)dev,
1307 (dev->file_data.media_source == -1? MS_BIG_FLAG: ms_none) |
1308 (dev->use_card == bn_true? PCL_CARD_FLAG: ms_none),
1309 (dev->use_card == bn_null? card_is_optional: NULL));
1310
1311 dev->eprn.soft_tumble = false;
1312
1313 /* Open the "eprn" device part */
1314 if ((rc = eprn_open_device(device)) != 0) return rc;
1315
1316 /* if device has been subclassed (FirstPage/LastPage device) then make sure we use
1317 * the subclassed device.
1318 */
1319 while (device->child)
1320 device = device->child;
1321
1322 dev = (pcl3_Device *)device;
1323
1324 /* Fill the still unassigned parts of 'file_data' from the other data */
1325 {
1326 pcl_FileData *data = &dev->file_data;
1327 unsigned int j;
1328
1329 /* Media handling */
1330 data->size = pcl3_page_size(&dev->table, dev->eprn.code);
1331 if (data->size == pcl_ps_default) {
1332 /* This is due to a media description using a media size code for which
1333 there is no PCL Page Size code. This is either an error in a builtin
1334 description or the user specified it in a media configuration file.
1335 Note that there might be a "Card" flag, hence we should not talk
1336 about "size" only.
1337 */
1338 char buffer[50];
1339
1340 eprintf2("%s" ERRPREF
1341 "The current configuration for this driver has identified the\n"
1342 "%s page setup requested by the document as being for `",
1343 epref, epref);
1344 if (ms_find_name_from_code(buffer, sizeof(buffer),
1345 dev->eprn.code, flag_description) == 0) eprintf1("%s", buffer);
1346 else eprintf("UNKNOWN"); /* should never happen */
1347 eprintf3("' (%.0f x %.0f bp).\n"
1348 "%s The driver does not know how to do this in PCL.\n",
1349 dev->MediaSize[0], dev->MediaSize[1], epref);
1350 if (dev->eprn.media_file != NULL)
1351 eprintf2(
1352 "%s You should therefore not include such an entry in the\n"
1353 "%s media configuration file.\n", epref, epref);
1354 return_error(gs_error_rangecheck);
1355 }
1356 data->duplex = -1;
1357 if (dev->Duplex_set > 0) { /* Duplex is not null */
1358 if (dev->Duplex) {
1359 bool same_leading_edge;
1360
1361 /* Find direction of default user space y axis in device space */
1362 int orient = dev->eprn.default_orientation;
1363 if (dev->MediaSize[1] < dev->MediaSize[0]) /* landscape */
1364 orient++; /* rotate +90 degrees */
1365
1366 same_leading_edge = (orient % 2 == 0 /* y axis is vertical */) !=
1367 (dev->tumble != false);
1368 /* If there were a native 'bool' type in C, the last parenthesis
1369 could be reliably replaced by "dev->tumble". This is safer and
1370 just as fast, provided the compiler is sufficiently intelligent. */
1371
1372 dev->eprn.soft_tumble = dev->duplex_capability != Duplex_both &&
1373 ((same_leading_edge &&
1374 dev->duplex_capability != Duplex_sameLeadingEdge) ||
1375 (!same_leading_edge &&
1376 dev->duplex_capability != Duplex_oppositeLeadingEdge));
1377 if (dev->eprn.soft_tumble) same_leading_edge = !same_leading_edge;
1378
1379 /* I am assuming here that the values 1 and 2, specified by HP in
1380 BPL02705 as meaning "Long-Edge Binding" and "Short-Edge Binding",
1381 respectively, in fact mean what I've called the "same leading edge"
1382 and "opposite leading edge" settings for the second pass. */
1383 if (same_leading_edge) data->duplex = 1;
1384 else data->duplex = 2;
1385 }
1386 else data->duplex = 0; /* simplex */
1387 }
1388
1389 /* It is almost not necessary to set the palette here because the default
1390 settings of eprn and pcl3 agree and all other calls are routed through
1391 the put_params routines. But there is a special case: I want to use
1392 'pcl_no_palette' if the printer cannot switch palettes. */
1393 set_palette(dev);
1394
1395 /* Per-colorant information */
1396 for (j = 0; j < data->number_of_colorants; j++) {
1397 data->colorant_array[j].hres = (int)(dev->HWResolution[0] + 0.5);
1398 data->colorant_array[j].vres = (int)(dev->HWResolution[1] + 0.5);
1399 }
1400 if (data->palette == pcl_CMY || data->palette == pcl_RGB)
1401 for (j = 0; j < 3; j++)
1402 data->colorant_array[j].levels = dev->eprn.non_black_levels;
1403 else {
1404 data->colorant_array[0].levels = dev->eprn.black_levels;
1405 for (j = 1; j < data->number_of_colorants; j++)
1406 data->colorant_array[j].levels = dev->eprn.non_black_levels;
1407 }
1408 }
1409
1410 return rc;
1411 }
1412
1413 /******************************************************************************
1414
1415 Function: pcl3_close_device
1416
1417 ******************************************************************************/
1418
1419 static int pcl3_close_device(gx_device *device)
1420 {
1421 pcl3_Device *dev = (pcl3_Device *)device;
1422
1423 /* HP recommends that a driver should send the Printer Reset command at the
1424 end of each print job in order to leave the printer in its default state.
1425 This is a matter of courtesy for the next print job which could otherwise
1426 inherit some of the properties set for the present job unless it starts
1427 with a Printer Reset command itself (every job generated with this driver
1428 does).
1429
1430 Unfortunately, ghostscript does not have a corresponding device procedure.
1431 In particular, the 'close_device' procedure may be called multiple times
1432 during a job and for multi-file output it is even only called at the end
1433 of the sequence of files and then when 'dev->file' is already NULL.
1434 Hence this routine tries to get close by checking the 'configured' field:
1435 it is set if the pcl3_init_file() function has been called and therefore
1436 indicates that the driver has sent configuration commands to the printer.
1437 That part we can and should take back.
1438
1439 Of course one might reset the printer at the end of every page, but this
1440 would entail having to repeat the initialization at the beginning of
1441 every page. I regard this as logically inappropriate.
1442 */
1443
1444 if (dev->configured && dev->file != NULL) {
1445 pcl3_end_file(dev->file, &dev->file_data);
1446 dev->configured = false;
1447 }
1448
1449 return eprn_close_device(device);
1450 }
1451
1452 /******************************************************************************
1453
1454 Function: pcl3_print_page
1455
1456 This is the implementation of prn's print_page() method for this device.
1457
1458 It initializes the printer if necessary and prints the page.
1459
1460 ******************************************************************************/
1461
1462 /* Function to convert return codes from calls to pclgen routines. */
1463 static int convert(int code)
1464 {
1465 if (code > 0) return gs_error_Fatal;
1466 if (code < 0) return gs_error_ioerror;
1467 return 0;
1468 }
1469
1470 static int pcl3_print_page(gx_device_printer *device, gp_file *out)
1471 {
1472 int
1473 blank_lines,
1474 rc = 0;
1475 pcl3_Device *dev = (pcl3_Device *)device;
1476 const char *epref = dev->eprn.CUPS_messages? CUPS_ERRPREF: "";
1477 pcl_RasterData rd;
1478 unsigned int
1479 j,
1480 *lengths,
1481 planes;
1482 size_t alloc_size = 0;
1483
1484 /* Make sure out cleanup code will cope with partially-initialised data. */
1485 memset(&rd, 0, sizeof(pcl_RasterData)); /* Belt and braces. */
1486 planes = 0;
1487 lengths = NULL;
1488 rd.next = NULL;
1489 rd.previous = NULL;
1490 rd.workspace[0] = NULL;
1491 rd.workspace[1] = NULL;
1492
1493 /* If this is a new file or we've decided to re-configure, initialize the
1494 printer first */
1495 if (gdev_prn_file_is_new(device) || !dev->configured ||
1496 dev->configure_every_page) {
1497 rc = convert(pcl3_init_file(device->memory, out, &dev->file_data));
1498 if (rc) {
1499 (void) gs_note_error(rc);
1500 goto end;
1501 }
1502 dev->configured = true;
1503 }
1504
1505 /* Initialize raster data structure */
1506 rd.global = &dev->file_data;
1507 planes = eprn_number_of_bitplanes((eprn_Device *)dev);
1508 alloc_size = planes*sizeof(unsigned int);
1509 lengths = (unsigned int *)malloc(alloc_size);
1510 if (!lengths) {
1511 rc = gs_note_error(gs_error_VMerror);
1512 goto end;
1513 }
1514 eprn_number_of_octets((eprn_Device *)dev, lengths);
1515 alloc_size = planes*sizeof(pcl_OctetString);
1516 rd.next = (pcl_OctetString *)malloc(alloc_size);
1517 if (!rd.next) {
1518 rc = gs_note_error(gs_error_VMerror);
1519 goto end;
1520 }
1521 for (j=0; j<planes; j++) { /* Make sure we can free at any point. */
1522 rd.next[j].str = NULL;
1523 }
1524 if (pcl_cm_is_differential(dev->file_data.compression)) {
1525 rd.previous = (pcl_OctetString *)malloc(alloc_size);
1526 if (!rd.previous) {
1527 rc = gs_note_error(gs_error_VMerror);
1528 goto end;
1529 }
1530 for (j=0; j<planes; j++) { /* Make sure we can free at any point. */
1531 rd.previous[j].str = NULL;
1532 }
1533 for (j = 0; j < planes; j++) {
1534 alloc_size = lengths[j]*sizeof(eprn_Octet);
1535 rd.previous[j].str = (pcl_Octet *)malloc(alloc_size);
1536 if (!rd.previous[j].str) {
1537 rc = gs_note_error(gs_error_VMerror);
1538 goto end;
1539 }
1540 }
1541 }
1542 rd.width = 8*lengths[0]; /* all colorants have equal resolution */
1543 for (j = 0; j < planes; j++) {
1544 alloc_size = lengths[j]*sizeof(eprn_Octet);
1545 rd.next[j].str = (pcl_Octet *)malloc(alloc_size);
1546 if (!rd.next[j].str) {
1547 rc = gs_note_error(gs_error_VMerror);
1548 goto end;
1549 }
1550 }
1551 /* Note: 'pcl_Octet' must be identical with 'eprn_Octet'. */
1552 rd.workspace_allocated = lengths[0];
1553 for (j = 1; j < planes; j++)
1554 if (lengths[j] > rd.workspace_allocated)
1555 rd.workspace_allocated = lengths[j];
1556 for (j = 0;
1557 j < 2 && (j != 1 || dev->file_data.compression == pcl_cm_delta); j++) {
1558 alloc_size = rd.workspace_allocated*sizeof(pcl_Octet);
1559 rd.workspace[j] =
1560 (pcl_Octet *)malloc(alloc_size);
1561 if (!rd.workspace[j]) {
1562 rc = gs_note_error(gs_error_VMerror);
1563 goto end;
1564 }
1565 }
1566
1567 /* Open the page and start raster mode */
1568 rc = convert(pcl3_begin_page(out, &dev->file_data));
1569 if (rc) {
1570 (void) gs_note_error(rc);
1571 goto end;
1572 }
1573 rc = convert(pcl3_begin_raster(out, &rd));
1574 if (rc) {
1575 (void) gs_note_error(rc);
1576 goto end;
1577 }
1578
1579 /* Loop over scan lines */
1580 blank_lines = 0;
1581 while (eprn_get_planes((eprn_Device *)dev, (eprn_OctetString *)rd.next) == 0){
1582 /* Is this a blank (white) line? */
1583 if (dev->eprn.colour_model == eprn_DeviceRGB) {
1584 /* White results if all three colorants use their highest intensity.
1585 Fortunately, PCL-3+ can only support two intensity levels for all
1586 colorants in an RGB palette, hence this intensity must be one for all
1587 colorants simultaneously.
1588 Because the planes returned by eprn_get_planes() are guaranteed to
1589 have no trailing zero octets, we can easily check that they are of
1590 equal length before proceeding further.
1591 */
1592 for (j = 1; j < planes && rd.next[j].length == rd.next[0].length; j++);
1593 if (j >= planes && rd.next[0].length == lengths[0]) {
1594 int k;
1595 /* All planes have the same length and cover the whole width of the
1596 page. Check that they all contain 0xFF. */
1597 j = 0;
1598 do {
1599 k = rd.next[j].length - 1;
1600 while (k > 0 && rd.next[j].str[k] == 0xFF) k--;
1601 } while (k == 0 && rd.next[j].str[0] == 0xFF && ++j < planes);
1602 }
1603 }
1604 else
1605 /* White is zero */
1606 for (j = 0; j < planes && rd.next[j].length == 0; j++);
1607
1608 if (j == planes) blank_lines++;
1609 else {
1610 if (blank_lines > 0) {
1611 rc = convert(pcl3_skip_groups(out, &rd, blank_lines));
1612 if (rc) {
1613 (void) gs_note_error(rc);
1614 goto end;
1615 }
1616 blank_lines = 0;
1617 }
1618 rc = convert(pcl3_transfer_group(out, &rd));
1619 if (rc) {
1620 (void) gs_note_error(rc);
1621 goto end;
1622 }
1623 }
1624 }
1625
1626 /* Terminate raster mode and close the page */
1627 rc = convert(pcl3_end_raster(out, &rd));
1628 if (rc) {
1629 (void) gs_note_error(rc);
1630 goto end;
1631 }
1632 rc = convert(pcl3_end_page(out, &dev->file_data));
1633 if (rc) {
1634 (void) gs_note_error(rc);
1635 goto end;
1636 }
1637
1638 end:
1639 /* Free dynamic storage */
1640 if (rd.next) {
1641 for (j = 0; j < planes; j++) {
1642 free(rd.next[j].str);
1643 }
1644 }
1645 if (rd.previous) {
1646 for (j = 0; j < planes; j++) {
1647 free(rd.previous[j].str);
1648 }
1649 }
1650 for (j = 0; j < 2; j++) {
1651 free(rd.workspace[j]);
1652 }
1653 free(lengths);
1654 free(rd.next);
1655 free(rd.previous);
1656
1657 if (rc == gs_error_VMerror) {
1658 eprintf1("%s" ERRPREF "Memory allocation failure from malloc().\n",
1659 epref);
1660 }
1661
1662 return rc;
1663 }