Debian Codegraph @

src/cups/rastertogutenprint.c

raw symbols SCIP
1 /*
2 * Gutenprint based raster filter for the Common UNIX Printing System.
3 *
4 * Copyright 1993-2008 by Mike Sweet.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * Contents:
20 *
21 * main() - Main entry and processing of driver.
22 * cups_writefunc() - Write data to a file...
23 * cancel_job() - Cancel the current job...
24 * Image_get_appname() - Get the application we are running.
25 * Image_get_row() - Get one row of the image.
26 * Image_height() - Return the height of an image.
27 * Image_init() - Initialize an image.
28 * Image_conclude() - Close the progress display.
29 * Image_width() - Return the width of an image.
30 */
31
32 /*
33 * Include necessary headers...
34 */
35
36 #pragma GCC diagnostic push
37 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
38
39 #if 0
40 #define ENABLE_CUPS_LOAD_SAVE_OPTIONS
41 #endif
42
43 #ifdef HAVE_CONFIG_H
44 #include <config.h>
45 #endif
46 #include <cups/cups.h>
47 #include <cups/ppd.h>
48 #include <cups/raster.h>
49 #include <signal.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53 #include <fcntl.h>
54 #include <errno.h>
55 #include <sys/times.h>
56 #include <strings.h>
57 #include <sys/time.h>
58 #ifdef HAVE_LIMITS_H
59 #include <limits.h>
60 #endif
61 #include "i18n.h"
62 #include <gutenprint/xml.h>
63
64 /* Solaris with gcc has problems because gcc's limits.h doesn't #define */
65 /* this */
66 #ifndef CHAR_BIT
67 #define CHAR_BIT 8
68 #endif
69
70 /*
71 * Structure for page raster data...
72 */
73
74 #if (CUPS_VERSION_MAJOR > 1 || (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR > 1))
75 #define CUPS_HEADER_T cups_page_header2_t
76 #define CUPS_READ_HEADER cupsRasterReadHeader2
77 #else
78 #define CUPS_HEADER_T cups_page_header_t
79 #define CUPS_READ_HEADER cupsRasterReadHeader
80 #endif
81
82 typedef struct
83 {
84 cups_raster_t *ras; /* Raster stream to read from */
85 int page; /* Current page number */
86 unsigned int row; /* Current row number */
87 int left;
88 int right;
89 int bottom;
90 int top;
91 unsigned int width;
92 unsigned int height;
93 int left_trim;
94 int right_trim;
95 int top_trim;
96 int bottom_trim;
97 unsigned int adjusted_width;
98 unsigned int adjusted_height;
99 stp_dimension_t d_left;
100 stp_dimension_t d_right;
101 stp_dimension_t d_bottom;
102 stp_dimension_t d_top;
103 stp_dimension_t d_width;
104 stp_dimension_t d_height;
105 stp_dimension_t d_left_trim;
106 stp_dimension_t d_right_trim;
107 stp_dimension_t d_bottom_trim;
108 stp_dimension_t d_top_trim;
109 int last_percent;
110 int shrink_to_fit;
111 CUPS_HEADER_T header; /* Page header from file */
112 } cups_image_t;
113
114 static void cups_writefunc(void *file, const char *buf, size_t bytes);
115 static void cups_errfunc(void *file, const char *buf, size_t bytes);
116 static void cups_dbgfunc(void *file, const char *buf, size_t bytes);
117 static void cancel_job(int sig);
118 static const char *Image_get_appname(stp_image_t *image);
119 static stp_image_status_t Image_get_row(stp_image_t *image,
120 unsigned char *data,
121 size_t byte_limit, int row);
122 static int Image_height(stp_image_t *image);
123 static int Image_width(stp_image_t *image);
124 static void Image_conclude(stp_image_t *image);
125 static void Image_init(stp_image_t *image);
126
127 static stp_image_t theImage =
128 {
129 Image_init,
130 NULL, /* reset */
131 Image_width,
132 Image_height,
133 Image_get_row,
134 Image_get_appname,
135 Image_conclude,
136 NULL
137 };
138
139 static volatile stp_image_status_t Image_status = STP_IMAGE_STATUS_OK;
140 static double total_bytes_printed = 0;
141 static int print_messages_as_errors = 0;
142 static int suppress_messages = 0;
143 static int suppress_verbose_messages = 0;
144 static const stp_string_list_t *po = NULL;
145 #ifdef ENABLE_CUPS_LOAD_SAVE_OPTIONS
146 static const char *save_file_name = NULL;
147 static const char *load_file_name = NULL;
148 #endif /* ENABLE_CUPS_LOAD_SAVE_OPTIONS */
149
150 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
151 #pragma GCC diagnostic push
152 #pragma GCC diagnostic ignored "-Wcast-qual"
153 static inline void *
154 cast_safe(const void *ptr)
155 {
156 return (void *)ptr;
157 }
158 #pragma GCC diagnostic pop
159
160 static void
161 set_string_parameter(stp_vars_t *v, const char *name, const char *val)
162 {
163 if (! suppress_messages)
164 fprintf(stderr, "DEBUG: Gutenprint: Set special string %s to %s\n", name, val);
165 stp_set_string_parameter(v, name, val);
166 }
167
168
169 static void
170 set_special_parameter(stp_vars_t *v, const char *name, int choice)
171 {
172 stp_parameter_t desc;
173 stp_describe_parameter(v, name, &desc);
174 if (desc.p_type == STP_PARAMETER_TYPE_STRING_LIST)
175 {
176 #if 0
177 if (stp_check_string_parameter(v, name, STP_PARAMETER_ACTIVE))
178 {
179 if (! suppress_messages)
180 fprintf(stderr, "DEBUG: Gutenprint: Not overriding special parameter %s (%s)\n",
181 name, stp_get_string_parameter(v, name));
182 }
183 else
184 #endif
185 if (choice < 0)
186 {
187 stp_clear_string_parameter(v, name);
188 if (! suppress_messages)
189 fprintf(stderr, "DEBUG: Gutenprint: Clear special parameter %s\n",
190 name);
191 }
192 else if (choice >= (int)stp_string_list_count(desc.bounds.str))
193 {
194 if (! suppress_messages)
195 stp_i18n_printf(po, _("ERROR: Unable to set Gutenprint option %s "
196 "(%d > %d)!\n"), name, choice,
197 stp_string_list_count(desc.bounds.str));
198 }
199 else
200 {
201 stp_set_string_parameter
202 (v, name, stp_string_list_param(desc.bounds.str, choice)->name);
203 if (! suppress_messages)
204 fprintf(stderr, "DEBUG: Gutenprint: Set special parameter %s to choice %d (%s)\n",
205 name, choice,
206 stp_string_list_param(desc.bounds.str, choice)->name);
207 }
208 }
209 else
210 {
211 if (! suppress_messages)
212 fprintf(stderr, "DEBUG: Gutenprint: Unable to set special %s: not a string\n",
213 name);
214 }
215 stp_parameter_description_destroy(&desc);
216 }
217
218 static void
219 print_debug_block(const stp_vars_t *v, const cups_image_t *cups)
220 {
221 fprintf(stderr, "DEBUG: Gutenprint: Page data:\n");
222 fprintf(stderr, "DEBUG: Gutenprint: MediaClass = \"%s\"\n", cups->header.MediaClass);
223 fprintf(stderr, "DEBUG: Gutenprint: MediaColor = \"%s\"\n", cups->header.MediaColor);
224 fprintf(stderr, "DEBUG: Gutenprint: MediaType = \"%s\"\n", cups->header.MediaType);
225 fprintf(stderr, "DEBUG: Gutenprint: OutputType = \"%s\"\n", cups->header.OutputType);
226
227 fprintf(stderr, "DEBUG: Gutenprint: AdvanceDistance = %d\n", cups->header.AdvanceDistance);
228 fprintf(stderr, "DEBUG: Gutenprint: AdvanceMedia = %d\n", cups->header.AdvanceMedia);
229 fprintf(stderr, "DEBUG: Gutenprint: Collate = %d\n", cups->header.Collate);
230 fprintf(stderr, "DEBUG: Gutenprint: CutMedia = %d\n", cups->header.CutMedia);
231 fprintf(stderr, "DEBUG: Gutenprint: Duplex = %d\n", cups->header.Duplex);
232 fprintf(stderr, "DEBUG: Gutenprint: HWResolution = [ %d %d ]\n", cups->header.HWResolution[0],
233 cups->header.HWResolution[1]);
234 fprintf(stderr, "DEBUG: Gutenprint: ImagingBoundingBox = [ %d %d %d %d ]\n",
235 cups->header.ImagingBoundingBox[0], cups->header.ImagingBoundingBox[1],
236 cups->header.ImagingBoundingBox[2], cups->header.ImagingBoundingBox[3]);
237 fprintf(stderr, "DEBUG: Gutenprint: InsertSheet = %d\n", cups->header.InsertSheet);
238 fprintf(stderr, "DEBUG: Gutenprint: Jog = %d\n", cups->header.Jog);
239 fprintf(stderr, "DEBUG: Gutenprint: LeadingEdge = %d\n", cups->header.LeadingEdge);
240 fprintf(stderr, "DEBUG: Gutenprint: Margins = [ %d %d ]\n", cups->header.Margins[0],
241 cups->header.Margins[1]);
242 fprintf(stderr, "DEBUG: Gutenprint: ManualFeed = %d\n", cups->header.ManualFeed);
243 fprintf(stderr, "DEBUG: Gutenprint: MediaPosition = %d\n", cups->header.MediaPosition);
244 fprintf(stderr, "DEBUG: Gutenprint: MediaWeight = %d\n", cups->header.MediaWeight);
245 fprintf(stderr, "DEBUG: Gutenprint: MirrorPrint = %d\n", cups->header.MirrorPrint);
246 fprintf(stderr, "DEBUG: Gutenprint: NegativePrint = %d\n", cups->header.NegativePrint);
247 fprintf(stderr, "DEBUG: Gutenprint: NumCopies = %d\n", cups->header.NumCopies);
248 fprintf(stderr, "DEBUG: Gutenprint: Orientation = %d\n", cups->header.Orientation);
249 fprintf(stderr, "DEBUG: Gutenprint: OutputFaceUp = %d\n", cups->header.OutputFaceUp);
250 fprintf(stderr, "DEBUG: Gutenprint: PageSize = [ %d %d ]\n", cups->header.PageSize[0],
251 cups->header.PageSize[1]);
252 fprintf(stderr, "DEBUG: Gutenprint: Separations = %d\n", cups->header.Separations);
253 fprintf(stderr, "DEBUG: Gutenprint: TraySwitch = %d\n", cups->header.TraySwitch);
254 fprintf(stderr, "DEBUG: Gutenprint: Tumble = %d\n", cups->header.Tumble);
255 fprintf(stderr, "DEBUG: Gutenprint: cupsWidth = %d\n", cups->header.cupsWidth);
256 fprintf(stderr, "DEBUG: Gutenprint: cupsHeight = %d\n", cups->header.cupsHeight);
257 fprintf(stderr, "DEBUG: Gutenprint: cups->width = %u\n", cups->width);
258 fprintf(stderr, "DEBUG: Gutenprint: cups->height = %u\n", cups->height);
259 fprintf(stderr, "DEBUG: Gutenprint: cups->adjusted_width = %u\n", cups->adjusted_width);
260 fprintf(stderr, "DEBUG: Gutenprint: cups->adjusted_height = %u\n", cups->adjusted_height);
261 fprintf(stderr, "DEBUG: Gutenprint: cupsMediaType = %d\n", cups->header.cupsMediaType);
262 fprintf(stderr, "DEBUG: Gutenprint: cupsBitsPerColor = %d\n", cups->header.cupsBitsPerColor);
263 fprintf(stderr, "DEBUG: Gutenprint: cupsBitsPerPixel = %d\n", cups->header.cupsBitsPerPixel);
264 fprintf(stderr, "DEBUG: Gutenprint: cupsBytesPerLine = %d\n", cups->header.cupsBytesPerLine);
265 fprintf(stderr, "DEBUG: Gutenprint: cupsColorOrder = %d\n", cups->header.cupsColorOrder);
266 fprintf(stderr, "DEBUG: Gutenprint: cupsColorSpace = %d\n", cups->header.cupsColorSpace);
267 fprintf(stderr, "DEBUG: Gutenprint: cupsCompression = %d\n", cups->header.cupsCompression);
268 fprintf(stderr, "DEBUG: Gutenprint: cupsRowCount = %d\n", cups->header.cupsRowCount);
269 fprintf(stderr, "DEBUG: Gutenprint: cupsRowFeed = %d\n", cups->header.cupsRowFeed);
270 fprintf(stderr, "DEBUG: Gutenprint: cupsRowStep = %d\n", cups->header.cupsRowStep);
271 fprintf(stderr, "DEBUG: Gutenprint: shrink page to fit %d\n", cups->shrink_to_fit);
272 stp_vars_print_error(v, "DEBUG");
273 fprintf(stderr, "DEBUG: Gutenprint: End page data\n");
274 }
275
276 static int
277 printer_supports_bw(const stp_vars_t *v)
278 {
279 stp_parameter_t desc;
280 int status = 0;
281 stp_describe_parameter(v, "PrintingMode", &desc);
282 if (stp_string_list_is_present(desc.bounds.str, "BW"))
283 status = 1;
284 stp_parameter_description_destroy(&desc);
285 return status;
286 }
287
288 static void
289 validate_options(stp_vars_t *v, cups_image_t *cups)
290 {
291 stp_parameter_list_t params = stp_get_parameter_list(v);
292 int nparams = stp_parameter_list_count(params);
293 int i;
294 (void)cups;
295 if (! suppress_messages)
296 fprintf(stderr, "DEBUG: Gutenprint: Validating options\n");
297 for (i = 0; i < nparams; i++)
298 {
299 const stp_parameter_t *param = stp_parameter_list_param(params, i);
300 stp_parameter_t desc;
301 stp_describe_parameter(v, param->name, &desc);
302 if (desc.p_type == STP_PARAMETER_TYPE_STRING_LIST)
303 {
304 if (!stp_string_list_is_present
305 (desc.bounds.str, stp_get_string_parameter(v, desc.name)))
306 {
307 if (! suppress_messages)
308 {
309 const char *val = stp_get_string_parameter(v, desc.name);
310 fprintf(stderr, "DEBUG: Gutenprint: Clearing string %s (%s)\n",
311 desc.name, val ? val : "(null)");
312 }
313 stp_clear_string_parameter(v, desc.name);
314 if (!desc.read_only && desc.is_mandatory && desc.is_active)
315 {
316 if (! suppress_messages)
317 fprintf(stderr, "DEBUG: Gutenprint: Setting default string %s to %s\n",
318 desc.name, desc.deflt.str ? desc.deflt.str : "(null)");
319 stp_set_string_parameter(v, desc.name, desc.deflt.str);
320 if (strcmp(desc.name, "PageSize") == 0)
321 {
322 const stp_papersize_t *ps =
323 stp_describe_papersize(v, desc.deflt.str);
324 if (ps->width > 0)
325 {
326 if (! suppress_messages)
327 fprintf(stderr, "DEBUG: Gutenprint: Setting page width to %.3f\n",
328 ps->width);
329 if (ps->width < stp_get_page_width(v))
330 stp_set_page_width(v, ps->width);
331 }
332 if (ps->height > 0)
333 {
334 if (! suppress_messages)
335 fprintf(stderr, "DEBUG: Gutenprint: Setting page height to %.3f\n",
336 ps->height);
337 if (ps->height < stp_get_page_height(v))
338 stp_set_page_height(v, ps->height);
339 }
340 }
341 }
342 }
343 }
344 stp_parameter_description_destroy(&desc);
345 }
346 if (! suppress_messages)
347 fprintf(stderr, "DEBUG: Gutenprint: Done validating options\n");
348 stp_parameter_list_destroy(params);
349 }
350
351 static stp_vars_t *
352 initialize_page(cups_image_t *cups, const stp_vars_t *default_settings,
353 const char *page_size_name)
354 {
355 stp_dimension_t tmp_left, tmp_right, tmp_top, tmp_bottom;
356 stp_vars_t *v = stp_vars_create_copy(default_settings);
357
358 if (! suppress_messages)
359 fprintf(stderr, "DEBUG: Gutenprint: Initialize page\n");
360
361 if (cups->header.cupsBitsPerColor == 16)
362 set_string_parameter(v, "ChannelBitDepth", "16");
363 else
364 set_string_parameter(v, "ChannelBitDepth", "8");
365 switch (cups->header.cupsColorSpace)
366 {
367 case CUPS_CSPACE_W :
368 /* DyeSub photo printers don't support black & white ink! */
369 if (printer_supports_bw(v))
370 set_string_parameter(v, "PrintingMode", "BW");
371 set_string_parameter(v, "InputImageType", "Whitescale");
372 break;
373 case CUPS_CSPACE_K :
374 /* DyeSub photo printers don't support black & white ink! */
375 if (printer_supports_bw(v))
376 set_string_parameter(v, "PrintingMode", "BW");
377 set_string_parameter(v, "InputImageType", "Grayscale");
378 break;
379 case CUPS_CSPACE_RGB :
380 set_string_parameter(v, "PrintingMode", "Color");
381 set_string_parameter(v, "InputImageType", "RGB");
382 break;
383 case CUPS_CSPACE_CMY :
384 set_string_parameter(v, "PrintingMode", "Color");
385 set_string_parameter(v, "InputImageType", "CMY");
386 break;
387 case CUPS_CSPACE_CMYK :
388 set_string_parameter(v, "PrintingMode", "Color");
389 set_string_parameter(v, "InputImageType", "CMYK");
390 break;
391 case CUPS_CSPACE_KCMY :
392 set_string_parameter(v, "PrintingMode", "Color");
393 set_string_parameter(v, "InputImageType", "KCMY");
394 break;
395 default :
396 stp_i18n_printf(po, _("ERROR: Gutenprint detected a bad colorspace "
397 "(%d)!\n"), cups->header.cupsColorSpace);
398 break;
399 }
400
401 set_special_parameter(v, "Resolution", cups->header.cupsCompression - 1);
402
403 set_special_parameter(v, "Quality", cups->header.cupsRowFeed - 1);
404
405 if (strlen(cups->header.MediaClass) > 0)
406 set_string_parameter(v, "InputSlot", cups->header.MediaClass);
407
408 if (strlen(cups->header.MediaType) > 0)
409 set_string_parameter(v, "MediaType", cups->header.MediaType);
410
411 if (! suppress_messages)
412 fprintf(stderr, "DEBUG: Gutenprint: PageSize = %dx%d\n", cups->header.PageSize[0],
413 cups->header.PageSize[1]);
414
415 if (page_size_name)
416 {
417 if (strcmp(page_size_name, "Custom") == 0)
418 {
419 if (!suppress_messages)
420 fprintf(stderr, "DEBUG: Gutenprint: Using custom page size for (%d, %d)\n",
421 cups->header.PageSize[1], cups->header.PageSize[0]);
422 stp_set_page_width(v, cups->header.PageSize[0]);
423 stp_set_page_height(v, cups->header.PageSize[1]);
424 }
425 else if (stp_describe_papersize(v, page_size_name))
426 {
427 stp_dimension_t width, height;
428 if (!suppress_messages)
429 fprintf(stderr, "DEBUG: Gutenprint: Using page size %s with (%d, %d)\n",
430 page_size_name, cups->header.PageSize[1], cups->header.PageSize[0]);
431 set_string_parameter(v, "PageSize", page_size_name);
432 stp_get_media_size(v, &width, &height);
433 if (width > 0)
434 stp_set_page_width(v, width);
435 else
436 stp_set_page_width(v, cups->header.PageSize[0]);
437 if (height > 0)
438 stp_set_page_height(v, height);
439 else
440 stp_set_page_height(v, cups->header.PageSize[1]);
441 }
442 else
443 {
444 if (!suppress_messages)
445 fprintf(stderr, "DEBUG: Gutenprint: Can't find page size %s with (%d, %d), using custom page size\n",
446 page_size_name, cups->header.PageSize[1], cups->header.PageSize[0]);
447 stp_set_page_width(v, cups->header.PageSize[0]);
448 stp_set_page_height(v, cups->header.PageSize[1]);
449 }
450 }
451 else
452 {
453 if (! suppress_messages)
454 fprintf(stderr, "DEBUG: Gutenprint: No named media size for (%d, %d)\n",
455 cups->header.PageSize[1], cups->header.PageSize[0]);
456 stp_set_page_width(v, cups->header.PageSize[0]);
457 stp_set_page_height(v, cups->header.PageSize[1]);
458 }
459
460 /*
461 * Duplex
462 * Note that the names MUST match those in the printer driver(s)
463 */
464
465 if (cups->header.Duplex != 0)
466 {
467 if (cups->header.Tumble != 0)
468 set_string_parameter(v, "Duplex", "DuplexTumble");
469 else
470 set_string_parameter(v, "Duplex", "DuplexNoTumble");
471 }
472
473 cups->shrink_to_fit =
474 (stp_check_int_parameter(v, "CUPSShrinkPage", STP_PARAMETER_ACTIVE) ?
475 stp_get_int_parameter(v, "CUPSShrinkPage") : 0);
476
477 set_string_parameter(v, "JobMode", "Job");
478 validate_options(v, cups);
479 stp_get_media_size(v, &(cups->d_width), &(cups->d_height));
480 stp_get_maximum_imageable_area(v, &tmp_left, &tmp_right,
481 &tmp_bottom, &tmp_top);
482 stp_get_imageable_area(v, &(cups->d_left), &(cups->d_right),
483 &(cups->d_bottom), &(cups->d_top));
484 if (! suppress_messages)
485 {
486 fprintf(stderr, "DEBUG: Gutenprint: limits w %.3f l %.3f r %.3f h %.3f t %.3f b %.3f\n",
487 cups->d_width, cups->d_left, cups->d_right, cups->d_height, cups->d_top, cups->d_bottom);
488 fprintf(stderr, "DEBUG: Gutenprint: max limits l %.3f r %.3f t %.3f b %.3f\n",
489 tmp_left, tmp_right, tmp_top, tmp_bottom);
490 }
491
492 if (tmp_left < 0)
493 tmp_left = 0;
494 if (tmp_top < 0)
495 tmp_top = 0;
496 if (tmp_right > tmp_left + cups->d_width)
497 tmp_right = cups->d_width;
498 if (tmp_bottom > tmp_top + cups->d_height)
499 tmp_bottom = cups->d_height;
500 if (tmp_left <= cups->d_left)
501 {
502 if (cups->shrink_to_fit != 1)
503 {
504 cups->d_left_trim = cups->d_left - tmp_left;
505 tmp_left = cups->d_left;
506 }
507 else
508 cups->d_left_trim = 0;
509 if (! suppress_messages)
510 fprintf(stderr, "DEBUG: Gutenprint: left margin %.3f\n", cups->d_left_trim);
511 }
512 else
513 {
514 cups->d_left_trim = 0;
515 if (! suppress_messages)
516 fprintf(stderr, "DEBUG: Gutenprint: Adjusting left margin from %.3f to %.3f\n",
517 cups->d_left, tmp_left);
518 cups->d_left = tmp_left;
519 }
520 if (tmp_right >= cups->d_right)
521 {
522 if (cups->shrink_to_fit != 1)
523 {
524 cups->d_right_trim = tmp_right - cups->d_right;
525 tmp_right = cups->d_right;
526 }
527 else
528 cups->d_right_trim = 0;
529 if (! suppress_messages)
530 fprintf(stderr, "DEBUG: Gutenprint: right margin %.3f\n", cups->d_right_trim);
531 }
532 else
533 {
534 cups->d_right_trim = 0;
535 if (! suppress_messages)
536 fprintf(stderr, "DEBUG: Gutenprint: Adjusting right margin from %.3f to %.3f\n",
537 cups->d_right, tmp_right);
538 cups->d_right = tmp_right;
539 }
540 if (tmp_top <= cups->d_top)
541 {
542 if (cups->shrink_to_fit != 1)
543 {
544 cups->d_top_trim = cups->d_top - tmp_top;
545 tmp_top = cups->d_top;
546 }
547 else
548 cups->d_top_trim = 0;
549 if (! suppress_messages)
550 fprintf(stderr, "DEBUG: Gutenprint: top margin %.3f\n", cups->d_top_trim);
551 }
552 else
553 {
554 cups->d_top_trim = 0;
555 if (! suppress_messages)
556 fprintf(stderr, "DEBUG: Gutenprint: Adjusting top margin from %.3f to %.3f\n",
557 cups->d_top, tmp_top);
558 cups->d_top = tmp_top;
559 }
560 if (tmp_bottom >= cups->d_bottom)
561 {
562 if (cups->shrink_to_fit != 1)
563 {
564 cups->d_bottom_trim = tmp_bottom - cups->d_bottom;
565 tmp_bottom = cups->d_bottom;
566 }
567 else
568 cups->d_bottom_trim = 0;
569 if (! suppress_messages)
570 fprintf(stderr, "DEBUG: Gutenprint: bottom margin %.3f\n", cups->d_bottom_trim);
571 }
572 else
573 {
574 cups->d_bottom_trim = 0;
575 if (! suppress_messages)
576 fprintf(stderr, "DEBUG: Gutenprint: Adjusting bottom margin from %.3f to %.3f\n",
577 cups->d_bottom, tmp_bottom);
578 cups->d_bottom = tmp_bottom;
579 }
580
581 if (cups->shrink_to_fit == 2)
582 {
583 stp_dimension_t t_left, t_right, t_bottom, t_top;
584 stp_get_imageable_area(v, &(t_left), &(t_right), &(t_bottom), &(t_top));
585 stp_set_width(v, t_right - t_left);
586 stp_set_height(v, t_bottom - t_top);
587 stp_set_left(v, t_left);
588 stp_set_top(v, t_top);
589 }
590 else
591 {
592 stp_set_width(v, cups->d_right - cups->d_left);
593 stp_set_height(v, cups->d_bottom - cups->d_top);
594 stp_set_left(v, cups->d_left);
595 stp_set_top(v, cups->d_top);
596 }
597
598 cups->d_right = cups->d_width - cups->d_right;
599 if (cups->shrink_to_fit == 1)
600 cups->d_width = tmp_right - tmp_left;
601 else
602 cups->d_width = cups->d_width - cups->d_left - cups->d_right;
603 cups->width = cups->header.HWResolution[0] * cups->d_width / 72;
604 cups->left = cups->header.HWResolution[0] * cups->d_left / 72;
605 cups->right = cups->header.HWResolution[0] * cups->d_right / 72;
606 cups->left_trim = cups->header.HWResolution[0] * cups->d_left_trim / 72;
607 cups->right_trim = cups->header.HWResolution[0] * cups->d_right_trim / 72;
608 cups->adjusted_width = cups->width;
609 if (cups->adjusted_width > cups->header.cupsWidth)
610 cups->adjusted_width = cups->header.cupsWidth;
611
612 cups->d_bottom = cups->d_height - cups->d_bottom;
613 if (cups->shrink_to_fit == 1)
614 cups->d_height = tmp_bottom - tmp_top;
615 else
616 cups->d_height = cups->d_height - cups->d_top - cups->d_bottom;
617 cups->height = cups->header.HWResolution[1] * cups->d_height / 72;
618 cups->top = cups->header.HWResolution[1] * cups->d_top / 72;
619 cups->bottom = cups->header.HWResolution[1] * cups->d_bottom / 72;
620 cups->top_trim = cups->header.HWResolution[1] * cups->d_top_trim / 72;
621 cups->bottom_trim = cups->header.HWResolution[1] * cups->d_bottom_trim / 72;
622 cups->adjusted_height = cups->height;
623 if (cups->adjusted_height > cups->header.cupsHeight)
624 cups->adjusted_height = cups->header.cupsHeight;
625 if (! suppress_messages)
626 {
627 fprintf(stderr, "DEBUG: Gutenprint: CUPS settings w %d l %d r %d h %d t %d b %d\n",
628 cups->width, cups->left, cups->right,
629 cups->height, cups->top, cups->bottom);
630 fprintf(stderr, "DEBUG: Gutenprint: adjusted w %d h %d\n",
631 cups->adjusted_width, cups->adjusted_height);
632
633 }
634
635 if (! suppress_messages)
636 fprintf(stderr, "DEBUG: Gutenprint: End initialize page\n");
637 return v;
638 }
639
640 static void
641 purge_excess_data(cups_image_t *cups)
642 {
643 char *buffer = stp_malloc(cups->header.cupsBytesPerLine);
644 if (buffer)
645 {
646 if (! suppress_messages && ! suppress_verbose_messages )
647 fprintf(stderr, "DEBUG2: Gutenprint: Purging %d row%s\n",
648 cups->header.cupsHeight - cups->row,
649 ((cups->header.cupsHeight - cups->row) == 1 ? "" : "s"));
650 while (cups->row < cups->header.cupsHeight)
651 {
652 cupsRasterReadPixels(cups->ras, (unsigned char *)buffer,
653 cups->header.cupsBytesPerLine);
654 cups->row ++;
655 }
656 }
657 stp_free(buffer);
658 }
659
660 static void
661 set_all_options(stp_vars_t *v, cups_option_t *options, int num_options,
662 ppd_file_t *ppd)
663 {
664 stp_parameter_list_t params = stp_get_parameter_list(v);
665 int nparams = stp_parameter_list_count(params);
666 int i;
667 const char *val; /* CUPS option value */
668 ppd_option_t *ppd_option;
669 if (! suppress_messages)
670 fprintf(stderr, "DEBUG: Gutenprint: Set options:\n");
671 val = cupsGetOption("StpiShrinkOutput", num_options, options);
672 if (!val)
673 {
674 ppd_option = ppdFindOption(ppd, "StpiShrinkOutput");
675 if (ppd_option)
676 val = ppd_option->defchoice;
677 }
678 if (val)
679 {
680 if (!strcasecmp(val, "crop"))
681 stp_set_int_parameter(v, "CUPSShrinkPage", 0);
682 else if (!strcasecmp(val, "expand"))
683 stp_set_int_parameter(v, "CUPSShrinkPage", 2);
684 else
685 stp_set_int_parameter(v, "CUPSShrinkPage", 1);
686 }
687 else
688 stp_set_int_parameter(v, "CUPSShrinkPage", 1);
689 for (i = 0; i < nparams; i++)
690 {
691 const stp_parameter_t *param = stp_parameter_list_param(params, i);
692 stp_parameter_t desc;
693 char *ppd_option_name = stp_malloc(strlen(param->name) + 8); /* StpFineFOO\0 */
694
695 stp_describe_parameter(v, param->name, &desc);
696 if (desc.p_type == STP_PARAMETER_TYPE_DOUBLE)
697 {
698 sprintf(ppd_option_name, "Stp%s", desc.name);
699 val = cupsGetOption(ppd_option_name, num_options, options);
700 if (!val)
701 {
702 ppd_option = ppdFindOption(ppd, ppd_option_name);
703 if (ppd_option)
704 val = ppd_option->defchoice;
705 }
706 if (val && !strncasecmp(val, "Custom.", 7))
707 {
708 double dval = atof(val + 7);
709
710 if (! suppress_messages)
711 fprintf(stderr, "DEBUG: Gutenprint: Set float %s to %f\n",
712 desc.name, dval);
713 if (dval > desc.bounds.dbl.upper)
714 dval = desc.bounds.dbl.upper;
715 stp_set_float_parameter(v, desc.name, dval);
716 }
717 else if (val && strlen(val) > 0 && strcmp(val, "None") != 0)
718 {
719 double fine_val = 0;
720 if (strchr(val, (int) '.'))
721 {
722 fine_val = atof(val);
723 if (! suppress_messages)
724 fprintf(stderr, "DEBUG: Gutenprint: Set float %s to %f (%s)\n",
725 desc.name, fine_val, val);
726 }
727 else
728 {
729 double coarse_val = atof(val) * 0.001;
730 sprintf(ppd_option_name, "StpFine%s", desc.name);
731 val = cupsGetOption(ppd_option_name, num_options, options);
732 if (!val)
733 {
734 ppd_option = ppdFindOption(ppd, ppd_option_name);
735 if (ppd_option)
736 val = ppd_option->defchoice;
737 }
738 if (val && strlen(val) > 0 && strcmp(val, "None") != 0)
739 fine_val = atof(val) * 0.001;
740 if (! suppress_messages)
741 fprintf(stderr, "DEBUG: Gutenprint: Set float %s to %f + %f\n",
742 desc.name, coarse_val, fine_val);
743 fine_val += coarse_val;
744 }
745 if (fine_val > desc.bounds.dbl.upper)
746 fine_val = desc.bounds.dbl.upper;
747 if (fine_val < desc.bounds.dbl.lower)
748 fine_val = desc.bounds.dbl.lower;
749 stp_set_float_parameter(v, desc.name, fine_val);
750 }
751 }
752 else
753 {
754 sprintf(ppd_option_name, "Stp%s", desc.name);
755 val = cupsGetOption(ppd_option_name, num_options, options);
756 if (!val)
757 {
758 ppd_option = ppdFindOption(ppd, ppd_option_name);
759 if (ppd_option)
760 val = ppd_option->defchoice;
761 }
762 if (val && ((strlen(val) > 0 && strcmp(val, "None") != 0) ||
763 (desc.p_type == STP_PARAMETER_TYPE_STRING_LIST)))
764 {
765 stp_curve_t *curve;
766 stp_raw_t *raw;
767 switch (desc.p_type)
768 {
769 case STP_PARAMETER_TYPE_STRING_LIST:
770 if (! suppress_messages)
771 fprintf(stderr, "DEBUG: Gutenprint: Set string %s to %s\n",
772 desc.name, val);
773 set_string_parameter(v, desc.name, val);
774 break;
775 case STP_PARAMETER_TYPE_INT:
776 if (!strncasecmp(val, "Custom.", 7))
777 val += 7;
778
779 if (! suppress_messages)
780 fprintf(stderr, "DEBUG: Gutenprint: Set int %s to %s (%d)\n",
781 desc.name, val, atoi(val));
782 stp_set_int_parameter(v, desc.name, atoi(val));
783 break;
784 case STP_PARAMETER_TYPE_DIMENSION:
785 if (!strncasecmp(val, "Custom.", 7))
786 val += 7;
787
788 if (! suppress_messages)
789 fprintf(stderr, "DEBUG: Gutenprint: Set dimension %s to %s (%d)\n",
790 desc.name, val, atoi(val));
791
792 stp_set_dimension_parameter(v, desc.name, atoi(val));
793 break;
794 case STP_PARAMETER_TYPE_BOOLEAN:
795 if (! suppress_messages)
796 fprintf(stderr, "DEBUG: Gutenprint: Set bool %s to %s (%d)\n",
797 desc.name, val, strcasecmp(val, "true") == 0 ? 1 : 0);
798 stp_set_boolean_parameter
799 (v, desc.name, strcasecmp(val, "true") == 0 ? 1 : 0);
800 break;
801 case STP_PARAMETER_TYPE_CURVE:
802 curve = stp_curve_create_from_string(val);
803 if (! suppress_messages)
804 fprintf(stderr, "DEBUG: Gutenprint: Set curve %s to %s\n",
805 desc.name, curve ? val : "(NULL)");
806 if (curve)
807 {
808 stp_set_curve_parameter(v, desc.name, curve);
809 stp_curve_destroy(curve);
810 }
811 break;
812 case STP_PARAMETER_TYPE_RAW: /* figure this out later, too */
813 raw = stp_xmlstrtoraw(val);
814 if (! suppress_messages)
815 fprintf(stderr, "DEBUG: Gutenprint: Set raw %s to %s\n",
816 desc.name, raw ? val : "(NULL)");
817 if (raw)
818 {
819 stp_set_raw_parameter(v, desc.name, raw->data, raw->bytes);
820 stp_free(cast_safe(raw->data));
821 stp_free(raw);
822 }
823 break;
824 case STP_PARAMETER_TYPE_FILE: /* Probably not, security hole */
825 if (! suppress_messages)
826 fprintf(stderr, "DEBUG: Gutenprint: Ignoring option %s %s type %d\n",
827 desc.name, val, desc.p_type);
828 break;
829 default:
830 break;
831 }
832 }
833 else if (val)
834 {
835 if (! suppress_messages)
836 fprintf(stderr, "DEBUG: Gutenprint: Not setting %s to '%s'\n",
837 desc.name, val);
838 }
839 else
840 {
841 if (! suppress_messages)
842 fprintf(stderr, "DEBUG: Gutenprint: Not setting %s to (null)\n",
843 desc.name);
844 }
845 }
846 stp_parameter_description_destroy(&desc);
847 stp_free(ppd_option_name);
848 }
849 if (! suppress_messages)
850 fprintf(stderr, "DEBUG: Gutenprint: End options\n");
851 stp_parameter_list_destroy(params);
852 }
853
854 #ifdef ENABLE_CUPS_LOAD_SAVE_OPTIONS
855 static void
856 save_options(const char *save_name, const stp_vars_t *v)
857 {
858 FILE *f_options;
859 int i;
860 stp_vars_t *c = stp_vars_create();
861 stp_parameter_list_t params = stp_get_parameter_list(v);
862 stp_parameter_t desc;
863 stp_mxml_node_t *mxml = NULL;
864 int param_count;
865
866 if (!params)
867 {
868 stp_vars_destroy(c);
869 return;
870 }
871 f_options = fopen(save_name, "w");
872 if (!f_options)
873 {
874 stp_parameter_list_destroy(params);
875 stp_vars_destroy(c);
876 return;
877 }
878 param_count = stp_parameter_list_count(params);
879 stp_set_driver(c, stp_get_driver(v));
880 if (!suppress_messages)
881 fprintf(stderr, "DEBUG: Gutenprint: Saving parameters to %s\n", save_name);
882 for (i = 0; i < param_count; i++)
883 {
884 const stp_parameter_t *lparam =
885 stp_parameter_list_param(params, i);
886 stp_describe_parameter(v, lparam->name, &desc);
887 if (desc.read_only || !strcmp(desc.name, "ChannelBitDepth") ||
888 !stp_parameter_has_category_value(v, &desc, "Color", "Yes"))
889 {
890 if (!suppress_messages)
891 fprintf(stderr, "DEBUG: Gutenprint: skipping non-color %s\n",
892 desc.name);
893 stp_parameter_description_destroy(&desc);
894 continue;
895 }
896 switch (desc.p_type)
897 {
898 case STP_PARAMETER_TYPE_STRING_LIST:
899 if (stp_check_string_parameter(v, desc.name,
900 STP_PARAMETER_DEFAULTED))
901 {
902 if (!suppress_messages)
903 fprintf(stderr, "DEBUG: Gutenprint: SAVING string %s %s\n",
904 desc.name, stp_get_string_parameter(v, desc.name));
905 stp_set_string_parameter(c, desc.name,
906 stp_get_string_parameter(v, desc.name));
907 }
908 else if (desc.is_mandatory)
909 {
910 if (!suppress_messages)
911 fprintf(stderr, "DEBUG: Gutenprint: SAVING defaulted string %s %s\n",
912 desc.name, desc.deflt.str);
913 stp_set_string_parameter(c, desc.name, desc.deflt.str);
914 }
915 else if (!suppress_messages)
916 fprintf(stderr, "DEBUG: Gutenprint: skipping string %s\n", desc.name);
917 break;
918 case STP_PARAMETER_TYPE_RAW:
919 if (stp_check_raw_parameter(v, desc.name,
920 STP_PARAMETER_DEFAULTED))
921 {
922 const stp_raw_t *raw = stp_get_raw_parameter(v, desc.name);
923 if (!suppress_messages)
924 fprintf(stderr, "DEBUG: Gutenprint: SAVING raw %s\n", desc.name);
925 stp_set_raw_parameter(c, desc.name, raw->data, raw->bytes);
926 }
927 else if (!suppress_messages)
928 fprintf(stderr, "DEBUG: Gutenprint: skipping raw %s\n",
929 desc.name);
930 break;
931 case STP_PARAMETER_TYPE_BOOLEAN:
932 if (stp_check_boolean_parameter(v, desc.name, STP_PARAMETER_DEFAULTED))
933 {
934 if (!suppress_messages)
935 fprintf(stderr, "DEBUG: Gutenprint: SAVING bool %s %d\n",
936 desc.name, stp_get_boolean_parameter(v, desc.name));
937 stp_set_boolean_parameter(c, desc.name,
938 stp_get_boolean_parameter(v, desc.name));
939 }
940 else if (desc.is_mandatory)
941 {
942 if (!suppress_messages)
943 fprintf(stderr, "DEBUG: Gutenprint: SAVING defaulted bool %s %d\n",
944 desc.name, desc.deflt.boolean);
945 stp_set_boolean_parameter(c, desc.name, desc.deflt.boolean);
946 }
947 else if (!suppress_messages)
948 fprintf(stderr, "DEBUG: Gutenprint: skipping bool %s\n", desc.name);
949 break;
950 case STP_PARAMETER_TYPE_INT:
951 if (stp_check_int_parameter(v, desc.name, STP_PARAMETER_DEFAULTED))
952 {
953 if (!suppress_messages)
954 fprintf(stderr, "DEBUG: Gutenprint: SAVING int %s %d\n",
955 desc.name, stp_get_int_parameter(v, desc.name));
956 stp_set_int_parameter(c, desc.name, stp_get_int_parameter(v, desc.name));
957 }
958 else if (desc.is_mandatory)
959 {
960 if (!suppress_messages)
961 fprintf(stderr, "DEBUG: Gutenprint: SAVING defaulted int %s %d\n",
962 desc.name, desc.deflt.integer);
963 stp_set_int_parameter(c, desc.name, desc.deflt.integer);
964 }
965 else if (!suppress_messages)
966 fprintf(stderr, "DEBUG: Gutenprint: skipping int %s\n", desc.name);
967 break;
968 case STP_PARAMETER_TYPE_DOUBLE:
969 if (stp_check_float_parameter(v, desc.name, STP_PARAMETER_DEFAULTED))
970 {
971 if (!suppress_messages)
972 fprintf(stderr, "DEBUG: Gutenprint: SAVING float %s %f\n",
973 desc.name, stp_get_float_parameter(v, desc.name));
974 stp_set_float_parameter(c, desc.name,
975 stp_get_float_parameter(v, desc.name));
976 }
977 else if (desc.is_mandatory)
978 {
979 if (!suppress_messages)
980 fprintf(stderr, "DEBUG: Gutenprint: SAVING defaulted float %s %f\n",
981 desc.name, desc.deflt.dbl);
982 stp_set_float_parameter(c, desc.name, desc.deflt.dbl);
983 }
984 else if (!suppress_messages)
985 fprintf(stderr, "DEBUG: Gutenprint: skipping float %s\n", desc.name);
986 break;
987 case STP_PARAMETER_TYPE_DIMENSION:
988 if (stp_check_dimension_parameter(v, desc.name, STP_PARAMETER_DEFAULTED))
989 {
990 if (!suppress_messages)
991 fprintf(stderr, "DEBUG: Gutenprint: SAVING dimension %s %d\n",
992 desc.name, stp_get_dimension_parameter(v, desc.name));
993 stp_set_dimension_parameter(c, desc.name,
994 stp_get_dimension_parameter(v, desc.name));
995 }
996 else if (desc.is_mandatory)
997 {
998 if (!suppress_messages)
999 fprintf(stderr, "DEBUG: Gutenprint: SAVING defaulted dimension %s %d\n",
1000 desc.name, desc.deflt.dimension);
1001 stp_set_dimension_parameter(c, desc.name, desc.deflt.dimension);
1002 }
1003 else if (!suppress_messages)
1004 fprintf(stderr, "DEBUG: Gutenprint: skipping dimension %s\n", desc.name);
1005 break;
1006 case STP_PARAMETER_TYPE_CURVE:
1007 if (stp_check_curve_parameter(v, desc.name, STP_PARAMETER_DEFAULTED))
1008 {
1009 if (!suppress_messages)
1010 fprintf(stderr, "DEBUG: Gutenprint: SAVING curve %s\n", desc.name);
1011 stp_set_curve_parameter(c, desc.name,
1012 stp_get_curve_parameter(v, desc.name));
1013 }
1014 else if (desc.is_mandatory)
1015 {
1016 if (!suppress_messages)
1017 fprintf(stderr, "DEBUG: Gutenprint: SAVING defaulted curve %s\n", desc.name);
1018 stp_set_curve_parameter(c, desc.name, desc.deflt.curve);
1019 }
1020 else if (!suppress_messages)
1021 fprintf(stderr, "DEBUG: Gutenprint: skipping curve %s\n",
1022 desc.name);
1023 break;
1024 default:
1025 if (!suppress_messages)
1026 fprintf(stderr, "DEBUG: Gutenprint: Ignoring unknown type parameter %s (%d)\n",
1027 desc.name, desc.p_type);
1028 break;
1029 }
1030 stp_parameter_description_destroy(&desc);
1031 }
1032 stp_parameter_list_destroy(params);
1033 mxml = stp_xmltree_create_from_vars(c);
1034 if (mxml)
1035 {
1036 fputs("<?xml version=\"1.0\"?>\n\n", f_options);
1037 stp_mxmlSaveFile(mxml, f_options, STP_MXML_NO_CALLBACK);
1038 stp_mxmlDelete(mxml);
1039 }
1040 (void) fclose(f_options);
1041 stp_vars_destroy(c);
1042 if (!suppress_messages)
1043 fprintf(stderr, "DEBUG: Gutenprint: Done saving parameters to %s\n", save_name);
1044 }
1045
1046 static stp_vars_t *
1047 load_options(const char *load_name)
1048 {
1049 FILE *f_options = fopen(load_name, "r");
1050 if (f_options)
1051 {
1052 stp_vars_t *settings = NULL;
1053 stp_mxml_node_t *mxml;
1054 mxml = stp_mxmlLoadFile(NULL, f_options, STP_MXML_NO_CALLBACK);
1055 if (mxml)
1056 {
1057 stp_mxml_node_t *nxml =
1058 stp_mxmlFindElement(mxml, mxml, "vars", NULL, NULL,
1059 STP_MXML_DESCEND);
1060 if (nxml)
1061 {
1062 settings = stp_vars_create_from_xmltree_ref(nxml->child, mxml);
1063 if (! suppress_messages)
1064 fprintf(stderr, "DEBUG: Gutenprint: loading options from %s\n",
1065 load_file_name);
1066 if (! suppress_messages)
1067 stp_vars_print_error(settings, "DEBUG");
1068 }
1069 }
1070 else
1071 fprintf(stderr, "DEBUG: Unable to load options from %s\n",
1072 load_file_name);
1073 fclose(f_options);
1074 return settings;
1075 }
1076 return NULL;
1077 }
1078
1079 #endif /* ENABLE_CUPS_LOAD_SAVE_OPTIONS */
1080
1081 /*
1082 * 'main()' - Main entry and processing of driver.
1083 */
1084
1085 int /* O - Exit status */
1086 main(int argc, /* I - Number of command-line arguments */
1087 char *argv[]) /* I - Command-line arguments */
1088 {
1089 int fd; /* File descriptor */
1090 cups_image_t cups; /* CUPS image */
1091 const char *ppdfile; /* PPD environment variable */
1092 ppd_file_t *ppd; /* PPD file */
1093 ppd_size_t *size;
1094 const stp_printer_t *printer; /* Printer driver */
1095 int num_options; /* Number of CUPS options */
1096 cups_option_t *options; /* CUPS options */
1097 stp_vars_t *v = NULL;
1098 stp_vars_t *default_settings;
1099 int initialized_job = 0;
1100 const char *version_id;
1101 struct tms tms;
1102 long clocks_per_sec;
1103 struct timeval t1, t2;
1104 char *page_size_name = NULL;
1105 int aborted = 0;
1106 #ifdef ENABLE_CUPS_LOAD_SAVE_OPTIONS
1107 stp_vars_t *loaded_settings = NULL;
1108 #endif /* ENABLE_CUPS_LOAD_SAVE_OPTIONS */
1109
1110
1111 /*
1112 * Don't buffer error/status messages...
1113 */
1114
1115 setbuf(stderr, NULL);
1116
1117 if (getenv("STP_SUPPRESS_MESSAGES"))
1118 suppress_messages = 1;
1119
1120 if (getenv("STP_SUPPRESS_VERBOSE_MESSAGES"))
1121 suppress_verbose_messages = 1;
1122
1123 /*
1124 * Initialize libgutenprint
1125 */
1126
1127 po = stp_i18n_load(getenv("LANG"));
1128
1129 theImage.rep = &cups;
1130
1131 (void) gettimeofday(&t1, NULL);
1132 stp_set_global_errfunc(cups_errfunc);
1133 stp_set_global_dbgfunc(cups_dbgfunc);
1134 stp_set_global_errdata(stderr);
1135 stp_set_global_dbgdata(stderr);
1136 stp_init();
1137 version_id = stp_get_version();
1138 default_settings = stp_vars_create();
1139 stp_set_outfunc(default_settings, cups_writefunc);
1140 stp_set_outdata(default_settings, stdout);
1141
1142 /*
1143 * Check for valid arguments...
1144 */
1145 if (argc < 6 || argc > 7)
1146 {
1147 /*
1148 * We don't have the correct number of arguments; write an error message
1149 * and return.
1150 */
1151
1152 stp_i18n_printf(po, _("Usage: rastertoprinter job-id user title copies "
1153 "options [file]\n"));
1154 return (1);
1155 }
1156
1157 if (! suppress_messages)
1158 {
1159 fprintf(stderr, "DEBUG: Gutenprint: ============================================================\n");
1160 fprintf(stderr, "DEBUG: Gutenprint: VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\n");
1161 fprintf(stderr, "DEBUG: Gutenprint: %s Starting\n", version_id);
1162 fprintf(stderr, "DEBUG: Gutenprint: command line: %s '%s' '%s' '%s' '%s' %s%s%s%s\n",
1163 argv[0], argv[1], argv[2], argv[3], argv[4], "<args>",
1164 argc >= 7 ? " '" : "",
1165 argc >= 7 ? argv[6] : "",
1166 argc >= 7 ? "'" : "");
1167 }
1168
1169 /*
1170 * Get the PPD file...
1171 */
1172
1173 if ((ppdfile = getenv("PPD")) == NULL)
1174 {
1175 stp_i18n_printf(po, _("ERROR: No PPD file, unable to continue!\n"));
1176 return (1);
1177 }
1178 if (! suppress_messages)
1179 fprintf(stderr, "DEBUG: Gutenprint: using PPD file %s\n", ppdfile);
1180
1181 if ((ppd = ppdOpenFile(ppdfile)) == NULL)
1182 {
1183 stp_i18n_printf(po, _("ERROR: Gutenprint was unable to load PPD file "
1184 "\"%s\"!\n"), ppdfile);
1185 return (1);
1186 }
1187
1188 if (ppd->modelname == NULL)
1189 {
1190 stp_i18n_printf(po, _("ERROR: Gutenprint did not find a ModelName "
1191 "attribute in PPD file \"%s\"!\n"), ppdfile);
1192 ppdClose(ppd);
1193 return (1);
1194 }
1195
1196 if (ppd->nickname == NULL)
1197 {
1198 stp_i18n_printf(po, _("ERROR: Gutenprint did not find a NickName attribute "
1199 "in PPD file \"%s\"!\n"), ppdfile);
1200 ppdClose(ppd);
1201 return (1);
1202 }
1203 else if (strlen(ppd->nickname) <
1204 strlen(ppd->modelname) + strlen(CUPS_PPD_NICKNAME_STRING) + 3)
1205 {
1206 stp_i18n_printf(po, _("ERROR: Gutenprint found a corrupted NickName "
1207 "attribute in PPD file \"%s\"!\n"), ppdfile);
1208 ppdClose(ppd);
1209 return (1);
1210 }
1211 else if (strcmp(ppd->nickname + strlen(ppd->modelname) +
1212 strlen(CUPS_PPD_NICKNAME_STRING), version_id) != 0 &&
1213 (strlen(ppd->nickname + strlen(ppd->modelname) +
1214 strlen(CUPS_PPD_NICKNAME_STRING)) < strlen(version_id) ||
1215 !((strncmp(ppd->nickname + strlen(ppd->modelname) +
1216 strlen(CUPS_PPD_NICKNAME_STRING), version_id,
1217 strlen(version_id)) == 0) &&
1218 *(ppd->nickname + strlen(ppd->modelname) +
1219 strlen(CUPS_PPD_NICKNAME_STRING)) != ' ')))
1220 {
1221 stp_i18n_printf(po, _("ERROR: The PPD version (%s) is not compatible with "
1222 "Gutenprint %s. Please run `%scups-genppdupdate' as administrator.\n"),
1223 ppd->nickname+strlen(ppd->modelname)+strlen(CUPS_PPD_NICKNAME_STRING),
1224 version_id, SBINDIR);
1225 fprintf(stderr, "DEBUG: Gutenprint: If you have upgraded your version of Gutenprint\n");
1226 fprintf(stderr, "DEBUG: Gutenprint: recently, you must reinstall all printer queues.\n");
1227 fprintf(stderr, "DEBUG: Gutenprint: If the previous installed version of Gutenprint\n");
1228 fprintf(stderr, "DEBUG: Gutenprint: was 5.0.0 or higher, you can use the `cups-genppdupdate'\n");
1229 fprintf(stderr, "DEBUG: Gutenprint: program to do this; if the previous installed version\n");
1230 fprintf(stderr, "DEBUG: Gutenprint: was older, you can use the Modify Printer command via\n");
1231 fprintf(stderr, "DEBUG: Gutenprint: the CUPS web interface: http://localhost:631/printers.\n");
1232 ppdClose(ppd);
1233 return 1;
1234 }
1235
1236 /*
1237 * Get the STP options, if any...
1238 */
1239
1240 num_options = cupsParseOptions(argv[5], 0, &options);
1241 ppdMarkDefaults(ppd);
1242 cupsMarkOptions(ppd, num_options, options);
1243 size = ppdPageSize(ppd, NULL);
1244
1245 if (size)
1246 page_size_name = stp_strdup(size->name);
1247
1248 if (! suppress_messages)
1249 fprintf(stderr, "DEBUG: Gutenprint: CUPS option count is %d (%d bytes)\n",
1250 num_options, (int)strlen(argv[5]));
1251
1252 if (num_options > 0)
1253 {
1254 int i;
1255 for (i = 0; i < num_options; i++)
1256 {
1257 if (! suppress_messages)
1258 fprintf(stderr, "DEBUG: Gutenprint: CUPS option %d %s = %s\n",
1259 i, options[i].name, options[i].value);
1260 #ifdef ENABLE_CUPS_LOAD_SAVE_OPTIONS
1261 if (!strcmp(options[i].name, "SaveFileName"))
1262 save_file_name = options[i].value;
1263 if (!strcmp(options[i].name, "LoadFileName"))
1264 load_file_name = options[i].value;
1265 #endif /* ENABLE_CUPS_LOAD_SAVE_OPTIONS */
1266 }
1267 }
1268
1269 /*
1270 * Figure out which driver to use...
1271 */
1272
1273 printer = stp_get_printer_by_driver(ppd->modelname);
1274 if (!printer)
1275 printer = stp_get_printer_by_long_name(ppd->modelname);
1276
1277 if (printer == NULL)
1278 {
1279 stp_i18n_printf(po, _("ERROR: Unable to find Gutenprint driver named "
1280 "\"%s\"!\n"), ppd->modelname);
1281 ppdClose(ppd);
1282 return (1);
1283 }
1284 if (! suppress_messages)
1285 fprintf(stderr, "DEBUG: Gutenprint: Driver %s\n", ppd->modelname);
1286
1287 /*
1288 * Open the page stream...
1289 */
1290
1291 if (argc == 7)
1292 {
1293 if ((fd = open(argv[6], O_RDONLY)) == -1)
1294 {
1295 stp_i18n_printf(po, _("ERROR: Gutenprint was unable to open raster file "
1296 "\"%s\" - %s"), argv[6], strerror(errno));
1297 sleep(1);
1298 return (1);
1299 }
1300 }
1301 else
1302 fd = 0;
1303 if (! suppress_messages)
1304 fprintf(stderr, "DEBUG: Gutenprint: Using fd %d\n", fd);
1305
1306 stp_set_printer_defaults(default_settings, printer);
1307 #ifdef ENABLE_CUPS_LOAD_SAVE_OPTIONS
1308 if (load_file_name)
1309 loaded_settings = load_options(load_file_name);
1310 #endif /* ENABLE_CUPS_LOAD_SAVE_OPTIONS */
1311 stp_set_float_parameter(default_settings, "AppGamma", 1.0);
1312 set_all_options(default_settings, options, num_options, ppd);
1313
1314 cupsFreeOptions(num_options, options);
1315 ppdClose(ppd);
1316
1317 cups.ras = cupsRasterOpen(fd, CUPS_RASTER_READ);
1318
1319 /*
1320 * Process pages as needed...
1321 */
1322
1323 cups.page = 0;
1324
1325 if (! suppress_messages)
1326 fprintf(stderr, "DEBUG: Gutenprint: About to start printing loop.\n");
1327
1328 /*
1329 * Read the first page header, which we need in order to set up
1330 * the page.
1331 */
1332 signal(SIGTERM, cancel_job);
1333 while (CUPS_READ_HEADER(cups.ras, &cups.header))
1334 {
1335 /*
1336 * We don't know how many pages we're going to print, and
1337 * we need to call stp_end_job at the completion of the job.
1338 * Therefore, we need to keep v in scope after the termination
1339 * of the loop to permit calling stp_end_job then. Therefore,
1340 * we have to free the previous page's stp_vars_t at the start
1341 * of the loop.
1342 */
1343 if (v)
1344 stp_vars_destroy(v);
1345
1346 /*
1347 * Setup printer driver variables...
1348 */
1349 if (! suppress_messages)
1350 {
1351 fprintf(stderr, "DEBUG: Gutenprint: ================ Printing page %d ================\n", cups.page + 1);
1352 fprintf(stderr, "PAGE: %d %d\n", cups.page + 1, cups.header.NumCopies);
1353 }
1354 v = initialize_page(&cups, default_settings, page_size_name);
1355 #ifdef ENABLE_CUPS_LOAD_SAVE_OPTIONS
1356 if (loaded_settings)
1357 stp_copy_vars_from(v, loaded_settings);
1358 if (save_file_name)
1359 {
1360 save_options(save_file_name, v);
1361 save_file_name = NULL;
1362 }
1363 #endif /* ENABLE_CUPS_LOAD_SAVE_OPTIONS */
1364 if (! suppress_messages)
1365 {
1366 fprintf(stderr, "DEBUG: Gutenprint: Interim page settings:\n");
1367 stp_vars_print_error(v, "DEBUG");
1368 }
1369
1370 stp_merge_printvars(v, stp_printer_get_defaults(printer));
1371
1372 /* Pass along Collation settings */
1373 stp_set_boolean_parameter(v, "Collate", cups.header.Collate);
1374 stp_set_boolean_parameter_active(v, "Collate", STP_PARAMETER_ACTIVE);
1375 /* Pass along Copy settings */
1376 stp_set_int_parameter(v, "NumCopies", cups.header.NumCopies);
1377 stp_set_int_parameter_active(v, "NumCopies", STP_PARAMETER_ACTIVE);
1378 /* Pass along the page number */
1379 stp_set_int_parameter(v, "PageNumber", cups.page);
1380 cups.row = 0;
1381 if (! suppress_messages)
1382 print_debug_block(v, &cups);
1383 print_messages_as_errors = 1;
1384
1385 if (!initialized_job)
1386 {
1387 stp_start_job(v, &theImage);
1388 initialized_job = 1;
1389 }
1390
1391 if (!stp_print(v, &theImage))
1392 {
1393 if (Image_status != STP_IMAGE_STATUS_ABORT)
1394 {
1395 fprintf(stderr, "DEBUG: Gutenprint: Options failed to verify.\n");
1396 fprintf(stderr, "DEBUG: Gutenprint: Make sure that you are using ESP Ghostscript rather\n");
1397 fprintf(stderr, "DEBUG: Gutenprint: than GNU or AFPL Ghostscript with CUPS.\n");
1398 fprintf(stderr, "DEBUG: Gutenprint: If this is not the cause, set LogLevel to debug to identify the problem.\n");
1399 }
1400 aborted = 1;
1401 break;
1402 }
1403 print_messages_as_errors = 0;
1404
1405 fflush(stdout);
1406
1407 /*
1408 * Purge any remaining bitmap data...
1409 */
1410 if (cups.row < cups.header.cupsHeight)
1411 purge_excess_data(&cups);
1412 if (! suppress_messages)
1413 fprintf(stderr, "DEBUG: Gutenprint: ================ Done printing page %d ================\n", cups.page + 1);
1414 cups.page ++;
1415 }
1416 if (v)
1417 {
1418 if (! suppress_messages)
1419 fprintf(stderr, "DEBUG: Gutenprint: %s job\n",
1420 aborted ? "Aborted" : "Ending");
1421 stp_end_job(v, &theImage);
1422 fflush(stdout);
1423 stp_vars_destroy(v);
1424 }
1425 cupsRasterClose(cups.ras);
1426 (void) times(&tms);
1427 (void) gettimeofday(&t2, NULL);
1428 clocks_per_sec = sysconf(_SC_CLK_TCK);
1429 fprintf(stderr, "DEBUG: Gutenprint: stats %.0fB, %.3fu, %.3fs, %.3fel\n",
1430 total_bytes_printed,
1431 (double) tms.tms_utime / clocks_per_sec,
1432 (double) tms.tms_stime / clocks_per_sec,
1433 (double) (t2.tv_sec - t1.tv_sec) +
1434 ((double) (t2.tv_usec - t1.tv_usec)) / 1000000.0);
1435 if (!suppress_messages)
1436 {
1437 fprintf(stderr, "DEBUG: Gutenprint: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
1438 fprintf(stderr, "DEBUG: Gutenprint: ============================================================\n");
1439 }
1440 stp_vars_destroy(default_settings);
1441 if (page_size_name)
1442 stp_free(page_size_name);
1443 if (fd != 0)
1444 close(fd);
1445 return aborted ? 1 : 0;
1446 }
1447
1448
1449 /*
1450 * 'cups_writefunc()' - Write data to a file...
1451 */
1452
1453 static void
1454 cups_writefunc(void *file, const char *buf, size_t bytes)
1455 {
1456 FILE *prn = (FILE *)file;
1457 total_bytes_printed += bytes;
1458 fwrite(buf, 1, bytes, prn);
1459 }
1460
1461 static void
1462 cups_errfunc(void *file, const char *buf, size_t bytes)
1463 {
1464 size_t next_nl = 0;
1465 size_t where = 0;
1466 FILE *prn = (FILE *)file;
1467 while (where < bytes)
1468 {
1469 if (bytes - where > 6 && strncmp(buf, "ERROR:", 6) == 0)
1470 {
1471 fputs("ERROR: Gutenprint:", prn);
1472 buf += 6;
1473 }
1474 else if (print_messages_as_errors)
1475 fputs("ERROR: Gutenprint: ", prn);
1476 else if (strncmp(buf, "DEBUG", 5) != 0)
1477 fputs("DEBUG: Gutenprint: ", prn);
1478 while (next_nl < bytes)
1479 {
1480 if (buf[next_nl++] == '\n')
1481 break;
1482 }
1483 fwrite(buf + where, 1, next_nl - where, prn);
1484 where = next_nl;
1485 }
1486 }
1487
1488 static void
1489 cups_dbgfunc(void *file, const char *buf, size_t bytes)
1490 {
1491 size_t next_nl = 0;
1492 size_t where = 0;
1493 FILE *prn = (FILE *)file;
1494 while (where < bytes)
1495 {
1496 if (bytes - where > 6 && strncmp(buf, "ERROR:", 6) == 0)
1497 {
1498 fputs("ERROR: Gutenprint:", prn);
1499 buf += 6;
1500 }
1501 else if (strncmp(buf, "DEBUG", 5) != 0)
1502 fputs("DEBUG: Gutenprint: ", prn);
1503 while (next_nl < bytes)
1504 {
1505 if (buf[next_nl++] == '\n')
1506 break;
1507 }
1508 fwrite(buf + where, 1, next_nl - where, prn);
1509 where = next_nl;
1510 }
1511 }
1512
1513
1514 /*
1515 * 'cancel_job()' - Cancel the current job...
1516 */
1517
1518 static void
1519 cancel_job(int sig) /* I - Signal */
1520 {
1521 (void)sig;
1522 Image_status = STP_IMAGE_STATUS_ABORT;
1523 }
1524
1525 /*
1526 * 'Image_get_appname()' - Get the application we are running.
1527 */
1528
1529 static const char * /* O - Application name */
1530 Image_get_appname(stp_image_t *image) /* I - Image */
1531 {
1532 (void)image;
1533
1534 return ("CUPS driver based on Gutenprint");
1535 }
1536
1537
1538 /*
1539 * 'Image_get_row()' - Get one row of the image.
1540 */
1541
1542 static void
1543 throwaway_data(int amount, cups_image_t *cups)
1544 {
1545 unsigned char trash[4096]; /* Throwaway */
1546 int block_count = amount / 4096;
1547 int leftover = amount % 4096;
1548 while (block_count > 0)
1549 {
1550 cupsRasterReadPixels(cups->ras, trash, 4096);
1551 block_count--;
1552 }
1553 if (leftover)
1554 cupsRasterReadPixels(cups->ras, trash, leftover);
1555 }
1556
1557 static stp_image_status_t
1558 Image_get_row(stp_image_t *image, /* I - Image */
1559 unsigned char *data, /* O - Row */
1560 size_t byte_limit, /* I - how many bytes in data */
1561 int row) /* I - Row number */
1562 {
1563 cups_image_t *cups; /* CUPS image */
1564 int i; /* Looping var */
1565 int bytes_per_line;
1566 int margin;
1567 stp_image_status_t tmp_image_status = Image_status;
1568 unsigned char *orig = data; /* Temporary pointer */
1569 static int warned = 0; /* Error warning printed? */
1570 int new_percent;
1571 int left_margin, right_margin;
1572 (void)byte_limit;
1573
1574 if ((cups = (cups_image_t *)(image->rep)) == NULL)
1575 {
1576 stp_i18n_printf(po, _("ERROR: Gutenprint image is not initialized! "
1577 "Please report this bug to "
1578 "[email protected]\n"));
1579 return STP_IMAGE_STATUS_ABORT;
1580 }
1581 bytes_per_line =
1582 ((cups->adjusted_width * cups->header.cupsBitsPerPixel) + CHAR_BIT - 1) /
1583 CHAR_BIT;
1584
1585 left_margin = ((cups->left_trim * cups->header.cupsBitsPerPixel) + CHAR_BIT - 1) /
1586 CHAR_BIT;
1587 right_margin = ((cups->right_trim * cups->header.cupsBitsPerPixel) + CHAR_BIT - 1) /
1588 CHAR_BIT;
1589 margin = cups->header.cupsBytesPerLine - left_margin - bytes_per_line -
1590 right_margin;
1591
1592 if (cups->row < cups->header.cupsHeight)
1593 {
1594 if (! suppress_messages && ! suppress_verbose_messages)
1595 fprintf(stderr, "DEBUG2: Gutenprint: Reading %d %d\n",
1596 bytes_per_line, cups->row);
1597 while (cups->row <= (unsigned int)row && cups->row < cups->header.cupsHeight)
1598 {
1599 if (left_margin > 0)
1600 {
1601 if (! suppress_messages && ! suppress_verbose_messages)
1602 fprintf(stderr, "DEBUG2: Gutenprint: Tossing left %d (%d)\n",
1603 left_margin, cups->left_trim);
1604 throwaway_data(left_margin, cups);
1605 }
1606 cupsRasterReadPixels(cups->ras, data, bytes_per_line);
1607 cups->row ++;
1608 if (margin + right_margin > 0)
1609 {
1610 if (! suppress_messages && ! suppress_verbose_messages)
1611 fprintf(stderr, "DEBUG2: Gutenprint: Tossing right %d (%d) + %d\n",
1612 right_margin, cups->right_trim, margin);
1613 throwaway_data(margin + right_margin, cups);
1614 }
1615 }
1616 }
1617 else
1618 {
1619 switch (cups->header.cupsColorSpace)
1620 {
1621 case CUPS_CSPACE_K:
1622 case CUPS_CSPACE_CMYK:
1623 case CUPS_CSPACE_KCMY:
1624 case CUPS_CSPACE_CMY:
1625 memset(data, 0, bytes_per_line);
1626 break;
1627 case CUPS_CSPACE_RGB:
1628 case CUPS_CSPACE_W:
1629 memset(data, ((1 << CHAR_BIT) - 1), bytes_per_line);
1630 break;
1631 default:
1632 stp_i18n_printf(po, _("ERROR: Gutenprint detected a bad colorspace "
1633 "(%d)!\n"), cups->header.cupsColorSpace);
1634 return STP_IMAGE_STATUS_ABORT;
1635 }
1636 }
1637
1638 /*
1639 * This exists to print non-ADSC input which has messed up the job
1640 * input, such as that generated by psnup. The output is barely
1641 * legible, but it's better than the garbage output otherwise.
1642 */
1643 data = orig;
1644 if (cups->header.cupsBitsPerPixel == 1)
1645 {
1646 if (warned == 0)
1647 {
1648 fputs(_("WARNING: Gutenprint detected a bad color depth (1). "
1649 "Output quality is degraded. Are you using psnup or "
1650 "non-ADSC PostScript?\n"), stderr);
1651 warned = 1;
1652 }
1653 for (i = cups->adjusted_width - 1; i >= 0; i--)
1654 {
1655 if ( (data[i/8] >> (7 - i%8)) &0x1)
1656 data[i]=255;
1657 else
1658 data[i]=0;
1659 }
1660 }
1661
1662 new_percent = (int) (100.0 * cups->row / cups->header.cupsHeight);
1663 if (new_percent > cups->last_percent)
1664 {
1665 if (! suppress_verbose_messages)
1666 {
1667 stp_i18n_printf(po, _("INFO: Printing page %d, %d%%\n"),
1668 cups->page + 1, new_percent);
1669 fprintf(stderr, "ATTR: job-media-progress=%d\n", new_percent);
1670 }
1671 cups->last_percent = new_percent;
1672 }
1673
1674 if (tmp_image_status != STP_IMAGE_STATUS_OK)
1675 {
1676 if (! suppress_messages)
1677 fprintf(stderr, "DEBUG: Gutenprint: Image status %d\n", tmp_image_status);
1678 }
1679 return tmp_image_status;
1680 }
1681
1682
1683 /*
1684 * 'Image_height()' - Return the height of an image.
1685 */
1686
1687 static int /* O - Height in pixels */
1688 Image_height(stp_image_t *image) /* I - Image */
1689 {
1690 cups_image_t *cups; /* CUPS image */
1691
1692
1693 if ((cups = (cups_image_t *)(image->rep)) == NULL)
1694 return (0);
1695
1696 if (! suppress_messages)
1697 fprintf(stderr, "DEBUG: Gutenprint: Image_height %d\n", cups->adjusted_height);
1698 return (cups->adjusted_height);
1699 }
1700
1701
1702 /*
1703 * 'Image_init()' - Initialize an image.
1704 */
1705
1706 static void
1707 Image_init(stp_image_t *image) /* I - Image */
1708 {
1709 cups_image_t *cups; /* CUPS image */
1710
1711 if ((cups = (cups_image_t *)(image->rep)) == NULL)
1712 return;
1713 cups->last_percent = 0;
1714
1715 if (! suppress_messages)
1716 stp_i18n_printf(po, _("INFO: Starting page %d...\n"), cups->page + 1);
1717 /* cups->page + 1 because users expect 1-based counting */
1718 }
1719
1720 /*
1721 * 'Image_progress_conclude()' - Close the progress display.
1722 */
1723
1724 static void
1725 Image_conclude(stp_image_t *image) /* I - Image */
1726 {
1727 cups_image_t *cups; /* CUPS image */
1728
1729
1730 if ((cups = (cups_image_t *)(image->rep)) == NULL)
1731 return;
1732
1733 if (! suppress_messages)
1734 stp_i18n_printf(po, _("INFO: Finished page %d...\n"), cups->page + 1);
1735 }
1736
1737 /*
1738 * 'Image_width()' - Return the width of an image.
1739 */
1740
1741 static int /* O - Width in pixels */
1742 Image_width(stp_image_t *image) /* I - Image */
1743 {
1744 cups_image_t *cups; /* CUPS image */
1745
1746
1747 if ((cups = (cups_image_t *)(image->rep)) == NULL)
1748 return (0);
1749
1750 if (! suppress_messages)
1751 fprintf(stderr, "DEBUG: Gutenprint: Image_width %d\n", cups->adjusted_width);
1752 return (cups->adjusted_width);
1753 }