Debian Codegraph @

src/main/printers.c

raw symbols SCIP
1 /*
2 *
3 * Print plug-in driver utility functions for the GIMP.
4 *
5 * Copyright 1997-2000 Michael Sweet ([email protected]) and
6 * Robert Krawitz ([email protected])
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 /*
23 * This file must include only standard C header files. The core code must
24 * compile on generic platforms that don't support glib, gimp, gtk, etc.
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 #include <gutenprint/gutenprint.h>
31 #include "gutenprint-internal.h"
32 #include <gutenprint/gutenprint-intl-internal.h>
33 #include <stdint.h>
34 #include <math.h>
35 #ifdef HAVE_LIMITS_H
36 #include <limits.h>
37 #endif
38 #include <string.h>
39 #include <stdlib.h>
40
41 #define FMIN(a, b) ((a) < (b) ? (a) : (b))
42
43
44 static void stpi_printvars_freefunc(void *item);
45 static const char* stpi_printvars_namefunc(const void *item);
46
47 static stp_list_t *printvars_list = NULL;
48
49 typedef struct stp_printvars
50 {
51 const char *name;
52 stp_vars_t *printvars;
53 } stp_printvars_t;
54
55 static void stpi_printer_freefunc(void *item);
56 static const char* stpi_printer_namefunc(const void *item);
57 static const char* stpi_printer_long_namefunc(const void *item);
58
59 /*
60 * IMPORTANT NOTE ABOUT PRINTER LIST IMPLEMENTATION:
61 * For testing purposes, at a minimum, the list of printers must be
62 * returned in deterministic order. The Travis run splits up the list
63 * of printers by means of a rotor (each test job gets every Nth printer),
64 * so a given build must preserve ordering if we want to ensure that
65 * all printers are covered.
66 */
67
68 static stp_list_t *printer_list = NULL;
69
70 struct stp_printer
71 {
72 const char *driver;
73 char *long_name; /* Long name for UI */
74 char *family; /* Printer family */
75 char *manufacturer; /* Printer manufacturer */
76 char *device_id; /* IEEE 1284 device ID */
77 char *comment; /* Comment string, if any */
78 int model; /* Model number */
79 int vars_initialized;
80 const stp_printfuncs_t *printfuncs;
81 stp_vars_t *printvars;
82 };
83
84 static void
85 stpi_init_printvars_list(void)
86 {
87 if (!printvars_list)
88 {
89 printvars_list = stp_list_create();
90 stp_list_set_freefunc(printvars_list, stpi_printvars_freefunc);
91 stp_list_set_namefunc(printvars_list, stpi_printvars_namefunc);
92 stp_list_set_long_namefunc(printvars_list, stpi_printvars_namefunc);
93 }
94 }
95
96 static int
97 stpi_init_printer_list(void)
98 {
99 if(printer_list)
100 stp_list_destroy(printer_list);
101 printer_list = stp_list_create();
102 stp_list_set_freefunc(printer_list, stpi_printer_freefunc);
103 stp_list_set_namefunc(printer_list, stpi_printer_namefunc);
104 stp_list_set_long_namefunc(printer_list, stpi_printer_long_namefunc);
105 /* stp_list_set_sortfunc(printer_list, stpi_printer_sortfunc); */
106 return 0;
107 }
108
109 int
110 stp_printer_model_count(void)
111 {
112 if (printer_list == NULL)
113 {
114 stp_erprintf("No printer drivers found: "
115 "are STP_DATA_PATH and STP_MODULE_PATH correct?\n");
116 stpi_init_printer_list();
117 }
118 return stp_list_get_length(printer_list);
119 }
120
121 const stp_printer_t *
122 stp_get_printer_by_index(int idx)
123 {
124 stp_list_item_t *printer;
125 if (printer_list == NULL)
126 {
127 stp_erprintf("No printer drivers found: "
128 "are STP_DATA_PATH and STP_MODULE_PATH correct?\n");
129 stpi_init_printer_list();
130 }
131 printer = stp_list_get_item_by_index(printer_list, idx);
132 if (printer == NULL)
133 return NULL;
134 return (const stp_printer_t *) stp_list_item_get_data(printer);
135 }
136
137 static void
138 stpi_printer_freefunc(void *item)
139 {
140 stp_printer_t *printer = (stp_printer_t *) item;
141 if (printer->comment)
142 {
143 stp_free(printer->comment);
144 printer->comment = NULL;
145 }
146 stp_free(printer->long_name);
147 stp_free(printer->family);
148 stp_free(printer);
149 }
150
151 /* ARGSUSED */
152 static void
153 stpi_printvars_freefunc(void *item)
154 {
155 (void)item;
156 }
157
158 static const char *
159 stpi_printvars_namefunc(const void *item)
160 {
161 const stp_printvars_t *printvars = (const stp_printvars_t *) item;
162 return printvars->name;
163 }
164
165 const char *
166 stp_printer_get_driver(const stp_printer_t *printer)
167 {
168 return printer->driver;
169 }
170
171 static const char *
172 stpi_printer_namefunc(const void *item)
173 {
174 const stp_printer_t *printer = (const stp_printer_t *) item;
175 return printer->driver;
176 }
177
178 const char *
179 stp_printer_get_long_name(const stp_printer_t *printer)
180 {
181 return printer->long_name;
182 }
183
184 static const char *
185 stpi_printer_long_namefunc(const void *item)
186 {
187 const stp_printer_t *printer = (const stp_printer_t *) item;
188 return printer->long_name;
189 }
190
191 const char *
192 stp_printer_get_device_id(const stp_printer_t *printer)
193 {
194 return printer->device_id;
195 }
196
197 const char *
198 stp_printer_get_family(const stp_printer_t *printer)
199 {
200 return printer->family;
201 }
202
203 const char *
204 stp_printer_get_manufacturer(const stp_printer_t *printer)
205 {
206 return printer->manufacturer;
207 }
208
209 const char *
210 stp_printer_get_comment(const stp_printer_t *printer)
211 {
212 return printer->comment;
213 }
214
215 int
216 stp_printer_get_model(const stp_printer_t *printer)
217 {
218 return printer->model;
219 }
220
221 static inline const stp_printfuncs_t *
222 stpi_get_printfuncs(const stp_printer_t *printer)
223 {
224 return printer->printfuncs;
225 }
226
227
228 const stp_printer_t *
229 stp_get_printer_by_long_name(const char *long_name)
230 {
231 stp_list_item_t *printer_item;
232 if (printer_list == NULL)
233 {
234 stp_erprintf("No printer drivers found: "
235 "are STP_DATA_PATH and STP_MODULE_PATH correct?\n");
236 stpi_init_printer_list();
237 }
238 printer_item = stp_list_get_item_by_long_name(printer_list, long_name);
239 if (!printer_item)
240 return NULL;
241 return (const stp_printer_t *) stp_list_item_get_data(printer_item);
242 }
243
244 const stp_printer_t *
245 stp_get_printer_by_driver(const char *driver)
246 {
247 stp_list_item_t *printer_item;
248 if (printer_list == NULL)
249 {
250 stp_erprintf("No printer drivers found: "
251 "are STP_DATA_PATH and STP_MODULE_PATH correct?\n");
252 stpi_init_printer_list();
253 }
254 printer_item = stp_list_get_item_by_name(printer_list, driver);
255 if (!printer_item)
256 return NULL;
257 return (const stp_printer_t *) stp_list_item_get_data(printer_item);
258 }
259
260 const stp_printer_t *
261 stp_get_printer_by_device_id(const char *device_id)
262 {
263 stp_list_item_t *printer_item;
264 if (printer_list == NULL)
265 {
266 stp_erprintf("No printer drivers found: "
267 "are STP_DATA_PATH and STP_MODULE_PATH correct?\n");
268 stpi_init_printer_list();
269 }
270 if (! device_id || strcmp(device_id, "") == 0)
271 return NULL;
272
273 printer_item = stp_list_get_start(printer_list);
274 while (printer_item)
275 {
276 if (strcmp(((const stp_printer_t *) stp_list_item_get_data(printer_item))->device_id,
277 device_id) == 0)
278 return ((const stp_printer_t *) stp_list_item_get_data(printer_item));
279 printer_item = stp_list_item_next(printer_item);
280 }
281 return NULL;
282 }
283
284 int
285 stp_get_printer_index_by_driver(const char *driver)
286 {
287 /* There should be no need to ever know the index! */
288 int idx = 0;
289 for (idx = 0; idx < stp_printer_model_count(); idx++)
290 {
291 const stp_printer_t *printer = stp_get_printer_by_index(idx);
292 if (!strcmp(stp_printer_get_driver(printer), driver))
293 return idx;
294 }
295 return -1;
296 }
297
298 const stp_printer_t *
299 stp_get_printer(const stp_vars_t *v)
300 {
301 return stp_get_printer_by_driver(stp_get_driver(v));
302 }
303
304 int
305 stp_get_model_id(const stp_vars_t *v)
306 {
307 const stp_printer_t *printer = stp_get_printer_by_driver(stp_get_driver(v));
308 return printer->model;
309 }
310
311 stp_parameter_list_t
312 stp_printer_list_parameters(const stp_vars_t *v)
313 {
314 const stp_printfuncs_t *printfuncs =
315 stpi_get_printfuncs(stp_get_printer(v));
316 return (printfuncs->list_parameters)(v);
317 }
318
319 void
320 stp_printer_describe_parameter(const stp_vars_t *v, const char *name,
321 stp_parameter_t *description)
322 {
323 const stp_printfuncs_t *printfuncs =
324 stpi_get_printfuncs(stp_get_printer(v));
325 (printfuncs->parameters)(v, name, description);
326 }
327
328 static void
329 set_printer_defaults(stp_vars_t *v, int core_only, int soft)
330 {
331 stp_parameter_list_t *params;
332 int count;
333 int i;
334 stp_parameter_t desc;
335 params = stp_get_parameter_list(v);
336 count = stp_parameter_list_count(params);
337 for (i = 0; i < count; i++)
338 {
339 const stp_parameter_t *p = stp_parameter_list_param(params, i);
340 if (p->is_mandatory &&
341 (!core_only || p->p_class == STP_PARAMETER_CLASS_CORE))
342 {
343 stp_describe_parameter(v, p->name, &desc);
344 switch (p->p_type)
345 {
346 case STP_PARAMETER_TYPE_STRING_LIST:
347 if (!soft ||
348 !stp_check_string_parameter(v, p->name, STP_PARAMETER_DEFAULTED))
349 {
350 stp_set_string_parameter(v, p->name, desc.deflt.str);
351 stp_set_string_parameter_active(v, p->name, STP_PARAMETER_ACTIVE);
352 }
353 break;
354 case STP_PARAMETER_TYPE_DOUBLE:
355 if (!soft ||
356 !stp_check_float_parameter(v, p->name, STP_PARAMETER_DEFAULTED))
357 {
358 stp_set_float_parameter(v, p->name, desc.deflt.dbl);
359 stp_set_float_parameter_active(v, p->name, STP_PARAMETER_ACTIVE);
360 }
361 break;
362 case STP_PARAMETER_TYPE_INT:
363 if (!soft ||
364 !stp_check_int_parameter(v, p->name, STP_PARAMETER_DEFAULTED))
365 {
366 stp_set_int_parameter(v, p->name, desc.deflt.integer);
367 stp_set_int_parameter_active(v, p->name, STP_PARAMETER_ACTIVE);
368 }
369 break;
370 case STP_PARAMETER_TYPE_DIMENSION:
371 if (!soft ||
372 !stp_check_dimension_parameter(v, p->name, STP_PARAMETER_DEFAULTED))
373 {
374 stp_set_dimension_parameter(v, p->name, desc.deflt.dimension);
375 stp_set_dimension_parameter_active(v, p->name, STP_PARAMETER_ACTIVE);
376 }
377 break;
378 case STP_PARAMETER_TYPE_BOOLEAN:
379 if (!soft ||
380 !stp_check_boolean_parameter(v, p->name, STP_PARAMETER_DEFAULTED))
381 {
382 stp_set_boolean_parameter(v, p->name, desc.deflt.boolean);
383 stp_set_boolean_parameter_active(v, p->name, STP_PARAMETER_ACTIVE);
384 }
385 break;
386 case STP_PARAMETER_TYPE_CURVE:
387 if (!soft ||
388 !stp_check_curve_parameter(v, p->name, STP_PARAMETER_DEFAULTED))
389 {
390 stp_set_curve_parameter(v, p->name, desc.deflt.curve);
391 stp_set_curve_parameter_active(v, p->name, STP_PARAMETER_ACTIVE);
392 }
393 break;
394 case STP_PARAMETER_TYPE_ARRAY:
395 if (!soft ||
396 !stp_check_array_parameter(v, p->name, STP_PARAMETER_DEFAULTED))
397 {
398 stp_set_array_parameter(v, p->name, desc.deflt.array);
399 stp_set_array_parameter_active(v, p->name, STP_PARAMETER_ACTIVE);
400 }
401 break;
402 default:
403 break;
404 }
405 stp_parameter_description_destroy(&desc);
406 }
407 }
408 stp_parameter_list_destroy(params);
409 }
410
411 void
412 stp_set_printer_defaults(stp_vars_t *v, const stp_printer_t *printer)
413 {
414 stp_set_driver(v, stp_printer_get_driver(printer));
415 set_printer_defaults(v, 0, 0);
416 }
417
418 void
419 stp_set_printer_defaults_soft(stp_vars_t *v, const stp_printer_t *printer)
420 {
421 stp_set_driver(v, stp_printer_get_driver(printer));
422 set_printer_defaults(v, 0, 1);
423 }
424
425 void
426 stp_initialize_printer_defaults(void)
427 {
428 if (printer_list == NULL)
429 {
430 stpi_init_printer_list();
431 stp_deprintf
432 (STP_DBG_PRINTERS,
433 "stpi_family_register(): initialising printer_list...\n");
434 }
435 }
436
437 const stp_vars_t *
438 stp_printer_get_defaults(const stp_printer_t *printer)
439 {
440 if (! printer->vars_initialized)
441 {
442 stp_printer_t *nc_printer = (stp_printer_t *) stpi_cast_safe(printer);
443 stp_deprintf(STP_DBG_PRINTERS, " ==>init %s\n", printer->driver);
444 set_printer_defaults (nc_printer->printvars, 1, 0);
445 nc_printer->vars_initialized = 1;
446 }
447 return printer->printvars;
448 }
449
450 void
451 stp_get_media_size(const stp_vars_t *v, stp_dimension_t *width, stp_dimension_t *height)
452 {
453 const stp_printfuncs_t *printfuncs =
454 stpi_get_printfuncs(stp_get_printer(v));
455 (printfuncs->media_size)(v, width, height);
456 }
457
458 void
459 stp_get_imageable_area(const stp_vars_t *v,
460 stp_dimension_t *left, stp_dimension_t *right,
461 stp_dimension_t *bottom, stp_dimension_t *top)
462 {
463 const stp_printfuncs_t *printfuncs =
464 stpi_get_printfuncs(stp_get_printer(v));
465 (printfuncs->imageable_area)(v, left, right, bottom, top);
466 }
467
468 void
469 stp_get_maximum_imageable_area(const stp_vars_t *v,
470 stp_dimension_t *left, stp_dimension_t *right,
471 stp_dimension_t *bottom, stp_dimension_t *top)
472 {
473 const stp_printfuncs_t *printfuncs =
474 stpi_get_printfuncs(stp_get_printer(v));
475 (printfuncs->maximum_imageable_area)(v, left, right, bottom, top);
476 }
477
478 void
479 stp_get_size_limit(const stp_vars_t *v,
480 stp_dimension_t *max_width, stp_dimension_t *max_height,
481 stp_dimension_t *min_width, stp_dimension_t *min_height)
482 {
483 const stp_printfuncs_t *printfuncs =
484 stpi_get_printfuncs(stp_get_printer(v));
485 (printfuncs->limit)(v, max_width, max_height, min_width,min_height);
486 }
487
488 void
489 stp_describe_resolution(const stp_vars_t *v, stp_resolution_t *x, stp_resolution_t *y)
490 {
491 const stp_printfuncs_t *printfuncs =
492 stpi_get_printfuncs(stp_get_printer(v));
493 (printfuncs->describe_resolution)(v, x, y);
494 }
495
496 const char *
497 stp_describe_output(const stp_vars_t *v)
498 {
499 const stp_printfuncs_t *printfuncs =
500 stpi_get_printfuncs(stp_get_printer(v));
501 return (printfuncs->describe_output)(v);
502 }
503
504 int
505 stp_verify(stp_vars_t *v)
506 {
507 if (stp_get_verified(v))
508 return 1;
509 const stp_printfuncs_t *printfuncs =
510 stpi_get_printfuncs(stp_get_printer(v));
511 stp_vars_t *nv = stp_vars_create_copy(v);
512 int status;
513 stp_prune_inactive_options(nv);
514 status = (printfuncs->verify)(nv);
515 stp_set_verified(v, stp_get_verified(nv));
516 stp_vars_destroy(nv);
517 return status;
518 }
519
520 int
521 stp_print(const stp_vars_t *v, stp_image_t *image)
522 {
523 const stp_printfuncs_t *printfuncs =
524 stpi_get_printfuncs(stp_get_printer(v));
525 return (printfuncs->print)(v, image);
526 }
527
528 int
529 stp_start_job(const stp_vars_t *v, stp_image_t *image)
530 {
531 const stp_printfuncs_t *printfuncs =
532 stpi_get_printfuncs(stp_get_printer(v));
533 if (!stp_get_string_parameter(v, "JobMode") ||
534 strcmp(stp_get_string_parameter(v, "JobMode"), "Page") == 0)
535 return 1;
536 if (printfuncs->start_job)
537 return (printfuncs->start_job)(v, image);
538 else
539 return 1;
540 }
541
542 int
543 stp_end_job(const stp_vars_t *v, stp_image_t *image)
544 {
545 const stp_printfuncs_t *printfuncs =
546 stpi_get_printfuncs(stp_get_printer(v));
547 if (!stp_get_string_parameter(v, "JobMode") ||
548 strcmp(stp_get_string_parameter(v, "JobMode"), "Page") == 0)
549 return 1;
550 if (printfuncs->end_job)
551 return (printfuncs->end_job)(v, image);
552 else
553 return 1;
554 }
555
556 stp_string_list_t *
557 stp_get_external_options(const stp_vars_t *v)
558 {
559 const stp_printfuncs_t *printfuncs =
560 stpi_get_printfuncs(stp_get_printer(v));
561 if (printfuncs->get_external_options)
562 return (printfuncs->get_external_options)(v);
563 else
564 return NULL;
565 }
566
567 const stp_papersize_t *
568 stpi_printer_describe_papersize(const stp_vars_t *v, const char *name)
569 {
570 const stp_printfuncs_t *printfuncs =
571 stpi_get_printfuncs(stp_get_printer(v));
572 return (printfuncs->describe_papersize)(v, name);
573 }
574
575 static int
576 verify_string_param(const stp_vars_t *v, const char *parameter,
577 stp_parameter_t *desc, int quiet)
578 {
579 stp_parameter_verify_t answer = PARAMETER_OK;
580 stp_dprintf(STP_DBG_VARS, v, " Verifying string %s\n", parameter);
581 if (desc->is_mandatory ||
582 stp_check_string_parameter(v, parameter, STP_PARAMETER_ACTIVE))
583 {
584 const char *checkval = stp_get_string_parameter(v, parameter);
585 stp_string_list_t *vptr = desc->bounds.str;
586 size_t count = 0;
587 stp_dprintf(STP_DBG_VARS, v, " value %s\n",
588 checkval ? checkval : "(null)");
589 if (vptr)
590 count = stp_string_list_count(vptr);
591 answer = PARAMETER_BAD;
592 if (checkval == NULL)
593 {
594 if (count == 0)
595 answer = PARAMETER_OK;
596 else
597 {
598 if (!quiet)
599 stp_eprintf(v, _("Value must be set for %s\n"), parameter);
600 answer = PARAMETER_BAD;
601 }
602 }
603 else if (count > 0)
604 {
605 if (stp_string_list_is_present(vptr, checkval))
606 answer = PARAMETER_OK;
607 if (!answer && !quiet)
608 stp_eprintf(v, _("`%s' is not a valid %s\n"), checkval, parameter);
609 }
610 else if (strlen(checkval) == 0)
611 answer = PARAMETER_OK;
612 else if (!quiet)
613 stp_eprintf(v, _("`%s' is not a valid %s\n"), checkval, parameter);
614 }
615 stp_parameter_description_destroy(desc);
616 return answer;
617 }
618
619 static int
620 verify_double_param(const stp_vars_t *v, const char *parameter,
621 stp_parameter_t *desc, int quiet)
622 {
623 stp_dprintf(STP_DBG_VARS, v, " Verifying double %s\n", parameter);
624 if (desc->is_mandatory ||
625 stp_check_float_parameter(v, parameter, STP_PARAMETER_ACTIVE))
626 {
627 double checkval = stp_get_float_parameter(v, parameter);
628 if (checkval < desc->bounds.dbl.lower ||
629 checkval > desc->bounds.dbl.upper)
630 {
631 if (!quiet)
632 stp_eprintf(v, _("%s must be between %f and %f (is %f)\n"),
633 parameter, desc->bounds.dbl.lower,
634 desc->bounds.dbl.upper, checkval);
635 return PARAMETER_BAD;
636 }
637 }
638 return PARAMETER_OK;
639 }
640
641 static int
642 verify_int_param(const stp_vars_t *v, const char *parameter,
643 stp_parameter_t *desc, int quiet)
644 {
645 stp_dprintf(STP_DBG_VARS, v, " Verifying int %s\n", parameter);
646 if (desc->is_mandatory ||
647 stp_check_int_parameter(v, parameter, STP_PARAMETER_ACTIVE))
648 {
649 int checkval = stp_get_int_parameter(v, parameter);
650 if (checkval < desc->bounds.integer.lower ||
651 checkval > desc->bounds.integer.upper)
652 {
653 if (!quiet)
654 stp_eprintf(v, _("%s must be between %d and %d (is %d)\n"),
655 parameter, desc->bounds.integer.lower,
656 desc->bounds.integer.upper, checkval);
657 stp_parameter_description_destroy(desc);
658 return PARAMETER_BAD;
659 }
660 }
661 stp_parameter_description_destroy(desc);
662 return PARAMETER_OK;
663 }
664
665 static int
666 verify_dimension_param(const stp_vars_t *v, const char *parameter,
667 stp_parameter_t *desc, int quiet)
668 {
669 stp_dprintf(STP_DBG_VARS, v, " Verifying dimension %s\n", parameter);
670 if (desc->is_mandatory ||
671 stp_check_dimension_parameter(v, parameter, STP_PARAMETER_ACTIVE))
672 {
673 stp_dimension_t checkval = stp_get_dimension_parameter(v, parameter);
674 if (checkval < desc->bounds.dimension.lower ||
675 checkval > desc->bounds.dimension.upper)
676 {
677 if (!quiet)
678 stp_eprintf(v, _("%s must be between %f and %f (is %f)\n"),
679 parameter, desc->bounds.dimension.lower,
680 desc->bounds.dimension.upper, checkval);
681 stp_parameter_description_destroy(desc);
682 return PARAMETER_BAD;
683 }
684 }
685 stp_parameter_description_destroy(desc);
686 return PARAMETER_OK;
687 }
688
689 static int
690 verify_curve_param(const stp_vars_t *v, const char *parameter,
691 stp_parameter_t *desc, int quiet)
692 {
693 stp_parameter_verify_t answer = 1;
694 stp_dprintf(STP_DBG_VARS, v, " Verifying curve %s\n", parameter);
695 if (desc->bounds.curve &&
696 (desc->is_mandatory ||
697 stp_check_curve_parameter(v, parameter, STP_PARAMETER_ACTIVE)))
698 {
699 const stp_curve_t *checkval = stp_get_curve_parameter(v, parameter);
700 if (checkval)
701 {
702 double u0, l0;
703 double u1, l1;
704 stp_curve_get_bounds(checkval, &l0, &u0);
705 stp_curve_get_bounds(desc->bounds.curve, &l1, &u1);
706 if (u0 > u1 || l0 < l1)
707 {
708 if (!quiet)
709 stp_eprintf(v, _("%s bounds must be between %f and %f\n"),
710 parameter, l1, u1);
711 answer = PARAMETER_BAD;
712 }
713 if (stp_curve_get_wrap(checkval) !=
714 stp_curve_get_wrap(desc->bounds.curve))
715 {
716 if (!quiet)
717 stp_eprintf(v, _("%s wrap mode must be %s\n"),
718 parameter,
719 (stp_curve_get_wrap(desc->bounds.curve) ==
720 STP_CURVE_WRAP_NONE) ?
721 _("no wrap") : _("wrap around"));
722 answer = PARAMETER_BAD;
723 }
724 }
725 }
726 stp_parameter_description_destroy(desc);
727 return answer;
728 }
729
730 stp_parameter_verify_t
731 stp_verify_parameter(const stp_vars_t *v, const char *parameter,
732 int quiet)
733 {
734 stp_parameter_t desc;
735 quiet = 0;
736 stp_describe_parameter(v, parameter, &desc);
737 stp_dprintf(STP_DBG_VARS, v, " Verifying %s %d %d\n", parameter,
738 desc.is_active, desc.read_only);
739 if (!desc.is_active || desc.read_only)
740 {
741 stp_parameter_description_destroy(&desc);
742 return PARAMETER_INACTIVE;
743 }
744 switch (desc.p_type)
745 {
746 case STP_PARAMETER_TYPE_STRING_LIST:
747 return verify_string_param(v, parameter, &desc, quiet);
748 case STP_PARAMETER_TYPE_DOUBLE:
749 return verify_double_param(v, parameter, &desc, quiet);
750 case STP_PARAMETER_TYPE_INT:
751 return verify_int_param(v, parameter, &desc, quiet);
752 case STP_PARAMETER_TYPE_DIMENSION:
753 return verify_dimension_param(v, parameter, &desc, quiet);
754 case STP_PARAMETER_TYPE_CURVE:
755 return verify_curve_param(v, parameter, &desc, quiet);
756 case STP_PARAMETER_TYPE_RAW:
757 case STP_PARAMETER_TYPE_FILE:
758 stp_parameter_description_destroy(&desc);
759 return PARAMETER_OK; /* No way to verify this here */
760 case STP_PARAMETER_TYPE_BOOLEAN:
761 stp_parameter_description_destroy(&desc);
762 return PARAMETER_OK; /* Booleans always OK */
763 default:
764 if (!quiet)
765 stp_eprintf(v, _("Unknown type parameter %s (%d)\n"),
766 parameter, desc.p_type);
767 stp_parameter_description_destroy(&desc);
768 return 0;
769 }
770 }
771
772 #define CHECK_INT_RANGE(v, component, min, max) \
773 do \
774 { \
775 if (stp_get_##component((v)) < (min) || stp_get_##component((v)) > (max)) \
776 { \
777 answer = 0; \
778 stp_eprintf(v, _("%s out of range (value %d, min %d, max %d)\n"), \
779 #component, stp_get_##component(v), min, max); \
780 } \
781 } while (0)
782
783 #define CHECK_INT_RANGE_INTERNAL(v, component, min, max) \
784 do \
785 { \
786 if (stpi_get_##component((v)) < (min) || stpi_get_##component((v)) > (max)) \
787 { \
788 answer = 0; \
789 stp_eprintf(v, _("%s out of range (value %d, min %d, max %d)\n"), \
790 #component, stpi_get_##component(v), min, max); \
791 } \
792 } while (0)
793
794 typedef struct
795 {
796 char *data;
797 size_t bytes;
798 } errbuf_t;
799
800 static void
801 fill_buffer_writefunc(void *priv, const char *buffer, size_t bytes)
802 {
803 errbuf_t *errbuf = (errbuf_t *) priv;
804 if (errbuf->bytes == 0)
805 errbuf->data = stp_malloc(bytes + 1);
806 else
807 errbuf->data = stp_realloc(errbuf->data, errbuf->bytes + bytes + 1);
808 memcpy(errbuf->data + errbuf->bytes, buffer, bytes);
809 errbuf->bytes += bytes;
810 errbuf->data[errbuf->bytes] = '\0';
811 }
812
813 int
814 stp_verify_printer_params(stp_vars_t *v)
815 {
816 errbuf_t errbuf;
817 stp_outfunc_t ofunc = stp_get_errfunc(v);
818 void *odata = stp_get_errdata(v);
819 stp_parameter_list_t params;
820 int nparams;
821 int i;
822 int answer = 1;
823 stp_dimension_t left, top, bottom, right;
824 const char *pagesize = stp_get_string_parameter(v, "PageSize");
825
826 stp_dprintf(STP_DBG_VARS, v, "** Entering stp_verify_printer_params(0x%p)\n",
827 (void *) v);
828
829 stp_set_errfunc((stp_vars_t *) v, fill_buffer_writefunc);
830 stp_set_errdata((stp_vars_t *) v, &errbuf);
831
832 errbuf.data = NULL;
833 errbuf.bytes = 0;
834
835 if (pagesize && strlen(pagesize) > 0)
836 {
837 if (stp_verify_parameter(v, "PageSize", 0) == 0)
838 answer = 0;
839 }
840 else
841 {
842 stp_dimension_t width, height, min_height, min_width;
843 stp_get_size_limit(v, &width, &height, &min_width, &min_height);
844 if (stp_get_page_height(v) <= min_height ||
845 stp_get_page_height(v) > height ||
846 stp_get_page_width(v) <= min_width || stp_get_page_width(v) > width)
847 {
848 answer = 0;
849 stp_eprintf(v, _("Page size is not valid\n"));
850 }
851 stp_dprintf(STP_DBG_PAPER, v,
852 "page size max %f %f min %f %f actual %f %f\n",
853 width, height, min_width, min_height,
854 stp_get_page_width(v), stp_get_page_height(v));
855 }
856
857 stp_get_imageable_area(v, &left, &right, &bottom, &top);
858
859 stp_dprintf(STP_DBG_PAPER, v,
860 "page left %f top %f right %f bottom %f\n",
861 left, top, right, bottom);
862 stp_dprintf(STP_DBG_PAPER, v,
863 "requested left %f top %f width %f height %f\n",
864 stp_get_left(v), stp_get_top(v),
865 stp_get_width(v), stp_get_height(v));
866
867 stp_dimension_t epsilon = 1e-6;
868
869 if (stp_get_top(v) + epsilon < top)
870 {
871 answer = 0;
872 stp_eprintf(v, _("Top margin must not be less than %f\n"), top);
873 }
874
875 if (stp_get_left(v) + epsilon < left)
876 {
877 answer = 0;
878 stp_eprintf(v, _("Left margin must not be less than %f\n"), left);
879 }
880
881 if (stp_get_height(v) <= -epsilon)
882 {
883 answer = 0;
884 stp_eprintf(v, _("Height must be greater than zero\n"));
885 }
886
887 if (stp_get_width(v) <= -epsilon)
888 {
889 answer = 0;
890 stp_eprintf(v, _("Width must be greater than zero\n"));
891 }
892
893 if (stp_get_left(v) + stp_get_width(v) > right + epsilon)
894 {
895 answer = 0;
896 stp_eprintf(v, _("Image is too wide for the page: left margin is %f, width %f, right edge is %f\n"),
897 stp_get_left(v), stp_get_width(v), right);
898 }
899
900 if (stp_get_top(v) + stp_get_height(v) > bottom + epsilon)
901 {
902 answer = 0;
903 stp_eprintf(v, _("Image is too long for the page: top margin is %f, height %f, bottom edge is %f\n"),
904 stp_get_top(v), stp_get_height(v), bottom);
905 }
906
907 params = stp_get_parameter_list(v);
908 nparams = stp_parameter_list_count(params);
909 for (i = 0; i < nparams; i++)
910 {
911 const stp_parameter_t *param = stp_parameter_list_param(params, i);
912 stp_dprintf(STP_DBG_VARS, v, "Checking %s %d %d\n", param->name,
913 param->is_active, param->verify_this_parameter);
914
915 if (strcmp(param->name, "PageSize") != 0 &&
916 param->is_active && param->verify_this_parameter &&
917 stp_verify_parameter(v, param->name, 0) == 0)
918 answer = 0;
919 }
920 stp_parameter_list_destroy(params);
921 stp_set_errfunc((stp_vars_t *) v, ofunc);
922 stp_set_errdata((stp_vars_t *) v, odata);
923 stp_set_verified((stp_vars_t *) v, answer);
924 if (errbuf.bytes > 0)
925 {
926 stp_eprintf(v, "%s", errbuf.data);
927 stp_free(errbuf.data);
928 }
929 stp_dprintf(STP_DBG_VARS, v, "** Exiting stp_verify_printer_params(0x%p) => %d\n",
930 (void *) v, answer);
931 return answer;
932 }
933
934 static const stp_vars_t *
935 stp_find_params(const char *name, const char *family)
936 {
937 if (printvars_list)
938 {
939 char *stmp =
940 stp_malloc(strlen(family) + strlen("::") + strlen(name) + 1);
941 stp_list_item_t *item;
942 strcpy(stmp, family);
943 strcat(stmp, "::");
944 strcat(stmp, name);
945 item = stp_list_get_item_by_name(printvars_list, stmp);
946 if (item)
947 {
948 stp_free(stmp);
949 return ((const stp_printvars_t *)
950 stp_list_item_get_data(item))->printvars;
951 }
952 strcpy(stmp, name);
953 item = stp_list_get_item_by_name(printvars_list, stmp);
954 stp_free(stmp);
955 if (item)
956 return ((const stp_printvars_t *)
957 stp_list_item_get_data(item))->printvars;
958 }
959 return NULL;
960 }
961
962 /* Why couldn't strcmp be a valid comparison function... */
963 static int
964 compare_names(const void *n1, const void *n2)
965 {
966 return strcmp((const char *) n1, (const char *) n2);
967 }
968
969 void
970 stpi_find_duplicate_printers(void)
971 {
972 size_t nelts = stp_printer_model_count();
973 if (nelts == 0)
974 return;
975 const char **str_data = stp_zalloc(sizeof(const char *) * nelts);
976 stp_list_item_t *printer_item = stp_list_get_start(printer_list);
977 size_t i = 0;
978 int found_dups = 0;
979 const stp_printer_t *printer;
980 while (printer_item)
981 {
982 printer = stp_list_item_get_data(printer_item);
983 STPI_ASSERT(i < nelts, NULL);
984 str_data[i] = printer->driver;
985 printer_item = stp_list_item_next(printer_item);
986 i++;
987 }
988 qsort(str_data, nelts, sizeof(const char *), compare_names);
989 for (i = 0; i < nelts - 1; i++)
990 {
991 if (!strcmp(str_data[i], str_data[i+1]))
992 {
993 printer_item =
994 stp_list_get_item_by_name(printer_list, str_data[i]);
995 printer = stp_list_item_get_data(printer_item);
996 stp_erprintf("Duplicate printer entry '%s' (%s)\n",
997 printer->driver, printer->long_name);
998 found_dups++;
999 }
1000 }
1001 printer_item = stp_list_get_start(printer_list);
1002 i = 0;
1003 while (printer_item)
1004 {
1005 printer = stp_list_item_get_data(printer_item);
1006 STPI_ASSERT(i < nelts, NULL);
1007 str_data[i] = printer->long_name;
1008 printer_item = stp_list_item_next(printer_item);
1009 i++;
1010 }
1011 qsort(str_data, nelts, sizeof(const char *), compare_names);
1012 for (i = 0; i < nelts - 1; i++)
1013 {
1014 if (!strcmp(str_data[i], str_data[i+1]))
1015 {
1016 printer_item =
1017 stp_list_get_item_by_long_name(printer_list, str_data[i]);
1018 printer = stp_list_item_get_data(printer_item);
1019 stp_erprintf("Duplicate printer entry '%s' (%s)\n",
1020 printer->driver, printer->long_name);
1021 found_dups++;
1022 }
1023 }
1024 stp_free(str_data);
1025 if (found_dups > 0)
1026 {
1027 stp_erprintf("FATAL Duplicate printers in printer list. Aborting!\n");
1028 stp_abort();
1029 }
1030 }
1031
1032 int
1033 stpi_family_register(stp_list_t *family)
1034 {
1035 stp_list_item_t *printer_item;
1036 const stp_printer_t *printer;
1037
1038 if (printer_list == NULL)
1039 {
1040 stpi_init_printer_list();
1041 stp_deprintf
1042 (STP_DBG_PRINTERS,
1043 "stpi_family_register(): initialising printer_list...\n");
1044 }
1045
1046 if (family)
1047 {
1048 /* Check for duplicates after loading printers */
1049 printer_item = stp_list_get_start(family);
1050
1051 while(printer_item)
1052 {
1053 printer = (const stp_printer_t *) stp_list_item_get_data(printer_item);
1054 stp_list_item_create(printer_list, NULL, printer);
1055 printer_item = stp_list_item_next(printer_item);
1056 }
1057 }
1058 return 0;
1059 }
1060
1061 int
1062 stpi_family_unregister(stp_list_t *family)
1063 {
1064 stp_list_item_t *printer_item;
1065 stp_list_item_t *old_printer_item;
1066 const stp_printer_t *printer;
1067
1068 if (printer_list == NULL)
1069 {
1070 stpi_init_printer_list();
1071 stp_deprintf
1072 (STP_DBG_PRINTERS,
1073 "stpi_family_unregister(): initialising printer_list...\n");
1074 }
1075
1076 if (family)
1077 {
1078 printer_item = stp_list_get_start(family);
1079
1080 while(printer_item)
1081 {
1082 printer = (const stp_printer_t *) stp_list_item_get_data(printer_item);
1083 old_printer_item =
1084 stp_list_get_item_by_name(printer_list, printer->driver);
1085
1086 if (old_printer_item)
1087 stp_list_item_destroy(printer_list, old_printer_item);
1088 printer_item = stp_list_item_next(printer_item);
1089 }
1090 }
1091 return 0;
1092 }
1093
1094 static stp_printvars_t *
1095 stp_printvars_create_from_xmltree(stp_mxml_node_t *printer,
1096 const char *family)
1097 {
1098 stp_mxml_node_t *prop; /* Temporary node pointer */
1099 const char *stmp; /* Temporary string */
1100 char *sbuf;
1101 stp_printvars_t *outprintvars;
1102 outprintvars = stp_zalloc(sizeof(stp_printvars_t));
1103 if (!outprintvars)
1104 return NULL;
1105 outprintvars->printvars = stp_vars_create();
1106 if (outprintvars->printvars == NULL)
1107 {
1108 stp_free(outprintvars);
1109 return NULL;
1110 }
1111 stmp = stp_mxmlElementGetAttr(printer, "name");
1112 if (!stmp)
1113 {
1114 stp_vars_destroy(outprintvars->printvars);
1115 stp_free(outprintvars);
1116 return NULL;
1117 }
1118 sbuf = stp_malloc(strlen(family) + strlen("::") + strlen(stmp) + 1);
1119 strcpy(sbuf, family);
1120 strcat(sbuf, "::");
1121 strcat(sbuf, stmp);
1122 outprintvars->name = sbuf;
1123 prop = printer->child;
1124 stp_deprintf(STP_DBG_XML, ">>stp_printvars_create_from_xmltree: %p, %s\n",
1125 (void *) (outprintvars->printvars), outprintvars->name);
1126 stp_vars_fill_from_xmltree(prop, outprintvars->printvars);
1127 stp_deprintf(STP_DBG_XML, "<<stp_printvars_create_from_xmltree: %p, %s\n",
1128 (void *) (outprintvars->printvars), outprintvars->name);
1129 return outprintvars;
1130 }
1131
1132
1133 /*
1134 * Parse the printer node, and return the generated printer. Returns
1135 * NULL on failure.
1136 */
1137 static stp_printer_t*
1138 stp_printer_create_from_xmltree(stp_mxml_node_t *printer, /* The printer node */
1139 const char *family, /* Family name */
1140 const stp_printfuncs_t *printfuncs)
1141 /* Family printfuncs */
1142 {
1143 stp_mxml_node_t *prop; /* Temporary node pointer */
1144 stp_mxml_node_t *child;
1145 const char *stmp; /* Temporary string */
1146 stp_printer_t *outprinter; /* Generated printer */
1147 size_t slen = 0;
1148 int
1149 driver = 0, /* Check driver */
1150 long_name = 0;
1151
1152 outprinter = stp_zalloc(sizeof(stp_printer_t));
1153 if (!outprinter)
1154 return NULL;
1155 stmp = stp_mxmlElementGetAttr(printer, "parameters");
1156 if (stmp && !stp_find_params(stmp, family))
1157 stp_erprintf("stp_printer_create_from_xmltree: cannot find parameters %s::%s\n",
1158 family, stmp);
1159 if (stmp && stp_find_params(stmp, family))
1160 outprinter->printvars = stp_vars_create_copy(stp_find_params(stmp, family));
1161 else
1162 outprinter->printvars = stp_vars_create();
1163 if (outprinter->printvars == NULL)
1164 {
1165 stp_free(outprinter);
1166 return NULL;
1167 }
1168
1169 stmp = stp_mxmlElementGetAttr(printer, "driver");
1170 stp_set_driver(outprinter->printvars, (const char *) stmp);
1171
1172 outprinter->long_name = stp_strdup(stp_mxmlElementGetAttr(printer, "name"));
1173 outprinter->manufacturer = stp_strdup(stp_mxmlElementGetAttr(printer, "manufacturer"));
1174 outprinter->model = stp_xmlstrtol(stp_mxmlElementGetAttr(printer, "model"));
1175 outprinter->family = stp_strdup((const char *) family);
1176 stmp = stp_mxmlElementGetAttr(printer, "deviceid");
1177 if (stmp)
1178 outprinter->device_id = stp_strdup(stmp);
1179
1180 child = printer->child;
1181 while (child)
1182 {
1183 if (child->type == STP_MXML_TEXT)
1184 {
1185 if (outprinter->comment)
1186 {
1187 size_t oslen = slen;
1188 slen += strlen(child->value.text.string);
1189 if (child->value.text.whitespace)
1190 slen += 1;
1191 outprinter->comment = stp_realloc(outprinter->comment, slen + 1);
1192 (void) memset(outprinter->comment + oslen, 0, slen - oslen);
1193 if (child->value.text.whitespace)
1194 outprinter->comment[oslen++] = ' ';
1195 strncat(outprinter->comment + oslen,
1196 child->value.text.string, slen - oslen);
1197 }
1198 else
1199 {
1200 outprinter->comment = stp_strdup(child->value.text.string);
1201 slen = strlen(outprinter->comment);
1202 }
1203 }
1204 child = child->next;
1205 }
1206
1207 if (stp_get_driver(outprinter->printvars))
1208 driver = 1;
1209 if (outprinter->long_name)
1210 long_name = 1;
1211
1212 outprinter->printfuncs = printfuncs;
1213
1214 prop = printer->child;
1215 stp_vars_fill_from_xmltree(prop, outprinter->printvars);
1216 if (driver && long_name && printfuncs)
1217 {
1218 if (stp_get_debug_level() & STP_DBG_XML)
1219 {
1220 stmp = stp_mxmlElementGetAttr(printer, "driver");
1221 stp_erprintf("stp_printer_create_from_xmltree: printer: %s\n", stmp);
1222 }
1223 outprinter->driver = stp_get_driver(outprinter->printvars);
1224 return outprinter;
1225 }
1226 stp_free(outprinter);
1227 return NULL;
1228 }
1229
1230 /*
1231 * Parse the <family> node.
1232 */
1233 static void
1234 stpi_xml_process_family(stp_mxml_node_t *family) /* The family node */
1235 {
1236 stp_list_t *family_module_list = NULL; /* List of valid families */
1237 stp_list_item_t *family_module_item; /* Current family */
1238 const char *family_name; /* Name of family */
1239 stp_mxml_node_t *printer; /* printer child node */
1240 stp_module_t *family_module_data; /* Family module data */
1241 stp_family_t *family_data = NULL; /* Family data */
1242 int family_valid = 0; /* Is family valid? */
1243
1244 family_module_list = stp_module_get_class(STP_MODULE_CLASS_FAMILY);
1245 if (!family_module_list)
1246 return;
1247
1248 family_name = stp_mxmlElementGetAttr(family, "name");
1249 family_module_item = stp_list_get_start(family_module_list);
1250 while (family_module_item)
1251 {
1252 family_module_data = (stp_module_t *)
1253 stp_list_item_get_data(family_module_item);
1254 if (!strcmp(family_name, family_module_data->name))
1255 {
1256 stp_deprintf(STP_DBG_XML,
1257 "stpi_xml_process_family: family module: %s\n",
1258 family_module_data->name);
1259 family_data = family_module_data->syms;
1260 if (family_data->printer_list == NULL)
1261 family_data->printer_list = stp_list_create();
1262 family_valid = 1;
1263 }
1264 family_module_item = stp_list_item_next(family_module_item);
1265 }
1266
1267 printer = family->child;
1268 while (family_valid && printer)
1269 {
1270 if (printer->type == STP_MXML_ELEMENT)
1271 {
1272 const char *printer_name = printer->value.element.name;
1273 if (!strcmp(printer_name, "printer"))
1274 {
1275 stp_printer_t *outprinter =
1276 stp_printer_create_from_xmltree(printer, family_name,
1277 family_data->printfuncs);
1278 if (outprinter)
1279 stp_list_item_create(family_data->printer_list, NULL,
1280 outprinter);
1281 }
1282 else if (!strcmp(printer_name, "parameters"))
1283 {
1284 stp_printvars_t *printvars =
1285 stp_printvars_create_from_xmltree(printer, family_name);
1286 if (printvars)
1287 {
1288 stpi_init_printvars_list();
1289 stp_list_item_create(printvars_list, NULL, printvars);
1290 }
1291 }
1292 }
1293 printer = printer->next;
1294 }
1295
1296 stp_list_destroy(family_module_list);
1297 return;
1298 }
1299
1300 /*
1301 * Parse the <printdef> node.
1302 */
1303 static int
1304 stpi_xml_process_printdef(stp_mxml_node_t *printdef, const char *file) /* The printdef node */
1305 {
1306 stp_mxml_node_t *family; /* Family child node */
1307 (void)file;
1308
1309 family = printdef->child;
1310 while (family)
1311 {
1312 if (family->type == STP_MXML_ELEMENT)
1313 {
1314 const char *family_name = family->value.element.name;
1315 if (!strcmp(family_name, "family"))
1316 {
1317 stpi_xml_process_family(family);
1318 }
1319 }
1320 family = family->next;
1321 }
1322 return 1;
1323 }
1324
1325 void
1326 stpi_init_printer(void)
1327 {
1328 stp_register_xml_parser("printdef", stpi_xml_process_printdef);
1329 }