2 * Hewlett-Packard Page Control Language filter for CUPS.
4 * Copyright 2007-2015 by Apple Inc.
5 * Copyright 1993-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include <cups/cups.h>
16 #include <cups/string-private.h>
17 #include <cups/language-private.h>
18 #include <cups/raster.h>
28 unsigned char *Planes
[4], /* Output buffers */
29 *CompBuffer
, /* Compression buffer */
30 *BitBuffer
; /* Buffer for output bits */
31 unsigned NumPlanes
, /* Number of color planes */
32 ColorBits
, /* Number of bits per color */
33 Feed
; /* Number of lines to skip */
34 cups_bool_t Duplex
; /* Current duplex mode */
35 int Page
, /* Current page number */
36 Canceled
; /* Has the current job been canceled? */
44 void StartPage(ppd_file_t
*ppd
, cups_page_header2_t
*header
);
48 void CancelJob(int sig
);
49 void CompressData(unsigned char *line
, unsigned length
, unsigned plane
, unsigned type
);
50 void OutputLine(cups_page_header2_t
*header
);
54 * 'Setup()' - Prepare the printer for printing.
61 * Send a PCL reset sequence.
70 * 'StartPage()' - Start a page of graphics.
74 StartPage(ppd_file_t
*ppd
, /* I - PPD file */
75 cups_page_header2_t
*header
) /* I - Page header */
77 unsigned plane
; /* Looping var */
81 * Show page device dictionary...
84 fprintf(stderr
, "DEBUG: StartPage...\n");
85 fprintf(stderr
, "DEBUG: Duplex = %d\n", header
->Duplex
);
86 fprintf(stderr
, "DEBUG: HWResolution = [ %d %d ]\n", header
->HWResolution
[0], header
->HWResolution
[1]);
87 fprintf(stderr
, "DEBUG: ImagingBoundingBox = [ %d %d %d %d ]\n", header
->ImagingBoundingBox
[0], header
->ImagingBoundingBox
[1], header
->ImagingBoundingBox
[2], header
->ImagingBoundingBox
[3]);
88 fprintf(stderr
, "DEBUG: Margins = [ %d %d ]\n", header
->Margins
[0], header
->Margins
[1]);
89 fprintf(stderr
, "DEBUG: ManualFeed = %d\n", header
->ManualFeed
);
90 fprintf(stderr
, "DEBUG: MediaPosition = %d\n", header
->MediaPosition
);
91 fprintf(stderr
, "DEBUG: NumCopies = %d\n", header
->NumCopies
);
92 fprintf(stderr
, "DEBUG: Orientation = %d\n", header
->Orientation
);
93 fprintf(stderr
, "DEBUG: PageSize = [ %d %d ]\n", header
->PageSize
[0], header
->PageSize
[1]);
94 fprintf(stderr
, "DEBUG: cupsWidth = %d\n", header
->cupsWidth
);
95 fprintf(stderr
, "DEBUG: cupsHeight = %d\n", header
->cupsHeight
);
96 fprintf(stderr
, "DEBUG: cupsMediaType = %d\n", header
->cupsMediaType
);
97 fprintf(stderr
, "DEBUG: cupsBitsPerColor = %d\n", header
->cupsBitsPerColor
);
98 fprintf(stderr
, "DEBUG: cupsBitsPerPixel = %d\n", header
->cupsBitsPerPixel
);
99 fprintf(stderr
, "DEBUG: cupsBytesPerLine = %d\n", header
->cupsBytesPerLine
);
100 fprintf(stderr
, "DEBUG: cupsColorOrder = %d\n", header
->cupsColorOrder
);
101 fprintf(stderr
, "DEBUG: cupsColorSpace = %d\n", header
->cupsColorSpace
);
102 fprintf(stderr
, "DEBUG: cupsCompression = %d\n", header
->cupsCompression
);
105 * Setup printer/job attributes...
108 Duplex
= header
->Duplex
;
109 ColorBits
= header
->cupsBitsPerColor
;
111 if ((!Duplex
|| (Page
& 1)) && header
->MediaPosition
)
112 printf("\033&l%dH", /* Set media position */
113 header
->MediaPosition
);
115 if (Duplex
&& ppd
&& ppd
->model_number
== 2)
118 * Handle duplexing on new DeskJet printers...
121 printf("\033&l-2H"); /* Load media */
124 printf("\033&l2S"); /* Set duplex mode */
127 if (!Duplex
|| (Page
& 1) || (ppd
&& ppd
->model_number
== 2))
130 * Set the media size...
133 printf("\033&l6D\033&k12H"); /* Set 6 LPI, 10 CPI */
134 printf("\033&l0O"); /* Set portrait orientation */
136 switch (header
->PageSize
[1])
138 case 540 : /* Monarch Envelope */
139 printf("\033&l80A"); /* Set page size */
143 printf("\033&l25A"); /* Set page size */
146 case 624 : /* DL Envelope */
147 printf("\033&l90A"); /* Set page size */
150 case 649 : /* C5 Envelope */
151 printf("\033&l91A"); /* Set page size */
154 case 684 : /* COM-10 Envelope */
155 printf("\033&l81A"); /* Set page size */
158 case 709 : /* B5 Envelope */
159 printf("\033&l100A"); /* Set page size */
162 case 756 : /* Executive */
163 printf("\033&l1A"); /* Set page size */
166 case 792 : /* Letter */
167 printf("\033&l2A"); /* Set page size */
171 printf("\033&l26A"); /* Set page size */
174 case 1008 : /* Legal */
175 printf("\033&l3A"); /* Set page size */
179 printf("\033&l27A"); /* Set page size */
182 case 1224 : /* Tabloid */
183 printf("\033&l6A"); /* Set page size */
187 printf("\033&l%dP", /* Set page length */
188 header
->PageSize
[1] / 12);
189 printf("\033&l0E"); /* Set top margin to 0 */
192 if (!Duplex
|| (Page
& 1))
195 * Set other job options...
198 printf("\033&l%dX", header
->NumCopies
); /* Set number copies */
200 if (header
->cupsMediaType
&&
201 (!ppd
|| ppd
->model_number
!= 2 || header
->HWResolution
[0] == 600))
202 printf("\033&l%dM", /* Set media type */
203 header
->cupsMediaType
);
205 if (!ppd
|| ppd
->model_number
!= 2)
207 int mode
= Duplex
? 1 + header
->Tumble
!= 0 : 0;
209 printf("\033&l%dS", mode
); /* Set duplex mode */
210 printf("\033&l0L"); /* Turn off perforation skip */
213 else if (!ppd
|| ppd
->model_number
!= 2)
214 printf("\033&a2G"); /* Set back side */
217 * Set graphics mode...
220 if (ppd
&& ppd
->model_number
== 2)
223 * Figure out the number of color planes...
226 if (header
->cupsColorSpace
== CUPS_CSPACE_KCMY
)
232 * Set the resolution and top-of-form...
235 printf("\033&u%dD", header
->HWResolution
[0]);
237 printf("\033&l0e0L"); /* Reset top and don't skip */
238 printf("\033*p0Y\033*p0X"); /* Set top of form */
241 * Send 26-byte configure image data command with horizontal and
242 * vertical resolutions as well as a color count...
246 putchar(2); /* Format 2 */
247 putchar((int)NumPlanes
); /* Output planes */
249 putchar((int)(header
->HWResolution
[0] >> 8));/* Black resolution */
250 putchar((int)header
->HWResolution
[0]);
251 putchar((int)(header
->HWResolution
[1] >> 8));
252 putchar((int)header
->HWResolution
[1]);
254 putchar(1 << ColorBits
); /* # of black levels */
256 putchar((int)(header
->HWResolution
[0] >> 8));/* Cyan resolution */
257 putchar((int)header
->HWResolution
[0]);
258 putchar((int)(header
->HWResolution
[1] >> 8));
259 putchar((int)header
->HWResolution
[1]);
261 putchar(1 << ColorBits
); /* # of cyan levels */
263 putchar((int)(header
->HWResolution
[0] >> 8));/* Magenta resolution */
264 putchar((int)header
->HWResolution
[0]);
265 putchar((int)(header
->HWResolution
[1] >> 8));
266 putchar((int)header
->HWResolution
[1]);
268 putchar(1 << ColorBits
); /* # of magenta levels */
270 putchar((int)(header
->HWResolution
[0] >> 8));/* Yellow resolution */
271 putchar((int)header
->HWResolution
[0]);
272 putchar((int)(header
->HWResolution
[1] >> 8));
273 putchar((int)header
->HWResolution
[1]);
275 putchar(1 << ColorBits
); /* # of yellow levels */
277 printf("\033&l0H"); /* Set media position */
281 printf("\033*t%uR", header
->HWResolution
[0]);
284 if (header
->cupsColorSpace
== CUPS_CSPACE_KCMY
)
287 printf("\033*r-4U"); /* Set KCMY graphics */
289 else if (header
->cupsColorSpace
== CUPS_CSPACE_CMY
)
292 printf("\033*r-3U"); /* Set CMY graphics */
295 NumPlanes
= 1; /* Black&white graphics */
298 * Set size and position of graphics...
301 printf("\033*r%uS", header
->cupsWidth
); /* Set width */
302 printf("\033*r%uT", header
->cupsHeight
); /* Set height */
304 printf("\033&a0H"); /* Set horizontal position */
307 printf("\033&a%.0fV", /* Set vertical position */
308 10.0 * (ppd
->sizes
[0].length
- ppd
->sizes
[0].top
));
310 printf("\033&a0V"); /* Set top-of-page */
313 printf("\033*r1A"); /* Start graphics */
315 if (header
->cupsCompression
)
316 printf("\033*b%uM", /* Set compression */
317 header
->cupsCompression
);
319 Feed
= 0; /* No blank lines yet */
322 * Allocate memory for a line of graphics...
325 if ((Planes
[0] = malloc(header
->cupsBytesPerLine
+ NumPlanes
)) == NULL
)
327 fputs("ERROR: Unable to allocate memory\n", stderr
);
331 for (plane
= 1; plane
< NumPlanes
; plane
++)
332 Planes
[plane
] = Planes
[0] + plane
* header
->cupsBytesPerLine
/ NumPlanes
;
335 BitBuffer
= malloc(ColorBits
* ((header
->cupsWidth
+ 7) / 8));
339 if (header
->cupsCompression
)
340 CompBuffer
= malloc(header
->cupsBytesPerLine
* 2 + 2);
347 * 'EndPage()' - Finish a page of graphics.
354 * Eject the current page...
359 printf("\033*rC"); /* End color GFX */
361 if (!(Duplex
&& (Page
& 1)))
362 printf("\033&l0H"); /* Eject current page */
366 printf("\033*r0B"); /* End GFX */
368 if (!(Duplex
&& (Page
& 1)))
369 printf("\014"); /* Eject current page */
389 * 'Shutdown()' - Shutdown the printer.
396 * Send a PCL reset sequence.
405 * 'CancelJob()' - Cancel the current job...
409 CancelJob(int sig
) /* I - Signal */
418 * 'CompressData()' - Compress a line of graphics.
422 CompressData(unsigned char *line
, /* I - Data to compress */
423 unsigned length
, /* I - Number of bytes */
424 unsigned plane
, /* I - Color plane */
425 unsigned type
) /* I - Type of compression */
427 unsigned char *line_ptr
, /* Current byte pointer */
428 *line_end
, /* End-of-line byte pointer */
429 *comp_ptr
, /* Pointer into compression buffer */
430 *start
; /* Start of compression sequence */
431 unsigned count
; /* Count of bytes for output */
438 * Do no compression...
442 line_end
= line
+ length
;
447 * Do run-length encoding...
450 line_end
= line
+ length
;
451 for (line_ptr
= line
, comp_ptr
= CompBuffer
;
453 comp_ptr
+= 2, line_ptr
+= count
)
456 (line_ptr
+ count
) < line_end
&&
457 line_ptr
[0] == line_ptr
[count
] &&
461 comp_ptr
[0] = (unsigned char)(count
- 1);
462 comp_ptr
[1] = line_ptr
[0];
465 line_ptr
= CompBuffer
;
471 * Do TIFF pack-bits encoding...
475 line_end
= line
+ length
;
476 comp_ptr
= CompBuffer
;
478 while (line_ptr
< line_end
)
480 if ((line_ptr
+ 1) >= line_end
)
483 * Single byte on the end...
487 *comp_ptr
++ = *line_ptr
++;
489 else if (line_ptr
[0] == line_ptr
[1])
492 * Repeated sequence...
498 while (line_ptr
< (line_end
- 1) &&
499 line_ptr
[0] == line_ptr
[1] &&
506 *comp_ptr
++ = (unsigned char)(257 - count
);
507 *comp_ptr
++ = *line_ptr
++;
512 * Non-repeated sequence...
519 while (line_ptr
< (line_end
- 1) &&
520 line_ptr
[0] != line_ptr
[1] &&
527 *comp_ptr
++ = (unsigned char)(count
- 1);
529 memcpy(comp_ptr
, start
, count
);
534 line_ptr
= CompBuffer
;
540 * Set the length of the data and write a raster plane...
543 printf("\033*b%d%c", (int)(line_end
- line_ptr
), plane
);
544 fwrite(line_ptr
, (size_t)(line_end
- line_ptr
), 1, stdout
);
549 * 'OutputLine()' - Output a line of graphics.
553 OutputLine(cups_page_header2_t
*header
) /* I - Page header */
555 unsigned plane
, /* Current plane */
556 bytes
, /* Bytes to write */
557 count
; /* Bytes to convert */
558 unsigned char bit
, /* Current plane data */
559 bit0
, /* Current low bit data */
560 bit1
, /* Current high bit data */
561 *plane_ptr
, /* Pointer into Planes */
562 *bit_ptr
; /* Pointer into BitBuffer */
566 * Output whitespace as needed...
571 printf("\033*b%dY", Feed
);
576 * Write bitmap data as needed...
579 bytes
= (header
->cupsWidth
+ 7) / 8;
581 for (plane
= 0; plane
< NumPlanes
; plane
++)
588 CompressData(Planes
[plane
], bytes
, plane
< (NumPlanes
- 1) ? 'V' : 'W',
589 header
->cupsCompression
);
594 * Separate low and high bit data into separate buffers.
597 for (count
= header
->cupsBytesPerLine
/ NumPlanes
,
598 plane_ptr
= Planes
[plane
], bit_ptr
= BitBuffer
;
600 count
-= 2, plane_ptr
+= 2, bit_ptr
++)
604 bit0
= (unsigned char)(((bit
& 64) << 1) | ((bit
& 16) << 2) | ((bit
& 4) << 3) | ((bit
& 1) << 4));
605 bit1
= (unsigned char)((bit
& 128) | ((bit
& 32) << 1) | ((bit
& 8) << 2) | ((bit
& 2) << 3));
611 bit0
|= (unsigned char)((bit
& 1) | ((bit
& 4) >> 1) | ((bit
& 16) >> 2) | ((bit
& 64) >> 3));
612 bit1
|= (unsigned char)(((bit
& 2) >> 1) | ((bit
& 8) >> 2) | ((bit
& 32) >> 3) | ((bit
& 128) >> 4));
616 bit_ptr
[bytes
] = bit1
;
620 * Send low and high bits...
623 CompressData(BitBuffer
, bytes
, 'V', header
->cupsCompression
);
624 CompressData(BitBuffer
+ bytes
, bytes
, plane
< (NumPlanes
- 1) ? 'V' : 'W',
625 header
->cupsCompression
);
633 * 'main()' - Main entry and processing of driver.
636 int /* O - Exit status */
637 main(int argc
, /* I - Number of command-line arguments */
638 char *argv
[]) /* I - Command-line arguments */
640 int fd
; /* File descriptor */
641 cups_raster_t
*ras
; /* Raster stream for printing */
642 cups_page_header2_t header
; /* Page header from file */
643 unsigned y
; /* Current line */
644 ppd_file_t
*ppd
; /* PPD file */
645 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
646 struct sigaction action
; /* Actions for POSIX signals */
647 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
651 * Make sure status messages are not buffered...
654 setbuf(stderr
, NULL
);
657 * Check command-line...
660 if (argc
< 6 || argc
> 7)
663 * We don't have the correct number of arguments; write an error message
667 _cupsLangPrintFilter(stderr
, "ERROR",
668 _("%s job-id user title copies options [file]"),
674 * Open the page stream...
679 if ((fd
= open(argv
[6], O_RDONLY
)) == -1)
681 _cupsLangPrintError("ERROR", _("Unable to open raster file"));
689 ras
= cupsRasterOpen(fd
, CUPS_RASTER_READ
);
692 * Register a signal handler to eject the current page if the
698 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
699 sigset(SIGTERM
, CancelJob
);
700 #elif defined(HAVE_SIGACTION)
701 memset(&action
, 0, sizeof(action
));
703 sigemptyset(&action
.sa_mask
);
704 action
.sa_handler
= CancelJob
;
705 sigaction(SIGTERM
, &action
, NULL
);
707 signal(SIGTERM
, CancelJob
);
708 #endif /* HAVE_SIGSET */
711 * Initialize the print device...
714 ppd
= ppdOpenFile(getenv("PPD"));
717 ppd_status_t status
; /* PPD error */
718 int linenum
; /* Line number */
720 _cupsLangPrintFilter(stderr
, "ERROR",
721 _("The PPD file could not be opened."));
723 status
= ppdLastError(&linenum
);
725 fprintf(stderr
, "DEBUG: %s on line %d.\n", ppdErrorString(status
), linenum
);
733 * Process pages as needed...
738 while (cupsRasterReadHeader2(ras
, &header
))
741 * Write a status message with the page number and number of copies.
749 fprintf(stderr
, "PAGE: %d %d\n", Page
, header
.NumCopies
);
750 _cupsLangPrintFilter(stderr
, "INFO", _("Starting page %d."), Page
);
756 StartPage(ppd
, &header
);
759 * Loop for each line on the page...
762 for (y
= 0; y
< header
.cupsHeight
; y
++)
765 * Let the user know how far we have progressed...
773 _cupsLangPrintFilter(stderr
, "INFO",
774 _("Printing page %d, %u%% complete."),
775 Page
, 100 * y
/ header
.cupsHeight
);
776 fprintf(stderr
, "ATTR: job-media-progress=%u\n",
777 100 * y
/ header
.cupsHeight
);
781 * Read a line of graphics...
784 if (cupsRasterReadPixels(ras
, Planes
[0], header
.cupsBytesPerLine
) < 1)
788 * See if the line is blank; if not, write it to the printer...
792 memcmp(Planes
[0], Planes
[0] + 1, header
.cupsBytesPerLine
- 1))
802 _cupsLangPrintFilter(stderr
, "INFO", _("Finished page %d."), Page
);
811 * Shutdown the printer...
820 * Close the raster stream...
823 cupsRasterClose(ras
);
828 * If no pages were printed, send an error message...
833 _cupsLangPrintFilter(stderr
, "ERROR", _("No pages were found."));