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 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include <cups/cups.h>
22 #include <cups/string-private.h>
23 #include <cups/language-private.h>
24 #include <cups/raster.h>
34 unsigned char *Planes
[4], /* Output buffers */
35 *CompBuffer
, /* Compression buffer */
36 *BitBuffer
; /* Buffer for output bits */
37 unsigned NumPlanes
, /* Number of color planes */
38 ColorBits
, /* Number of bits per color */
39 Feed
; /* Number of lines to skip */
40 int Duplex
, /* Current duplex mode */
41 Page
, /* Current page number */
42 Canceled
; /* Has the current job been canceled? */
50 void StartPage(ppd_file_t
*ppd
, cups_page_header2_t
*header
);
54 void CancelJob(int sig
);
55 void CompressData(unsigned char *line
, unsigned length
, unsigned plane
, unsigned type
);
56 void OutputLine(cups_page_header2_t
*header
);
60 * 'Setup()' - Prepare the printer for printing.
67 * Send a PCL reset sequence.
76 * 'StartPage()' - Start a page of graphics.
80 StartPage(ppd_file_t
*ppd
, /* I - PPD file */
81 cups_page_header2_t
*header
) /* I - Page header */
83 unsigned plane
; /* Looping var */
87 * Show page device dictionary...
90 fprintf(stderr
, "DEBUG: StartPage...\n");
91 fprintf(stderr
, "DEBUG: Duplex = %d\n", header
->Duplex
);
92 fprintf(stderr
, "DEBUG: HWResolution = [ %d %d ]\n", header
->HWResolution
[0], header
->HWResolution
[1]);
93 fprintf(stderr
, "DEBUG: ImagingBoundingBox = [ %d %d %d %d ]\n", header
->ImagingBoundingBox
[0], header
->ImagingBoundingBox
[1], header
->ImagingBoundingBox
[2], header
->ImagingBoundingBox
[3]);
94 fprintf(stderr
, "DEBUG: Margins = [ %d %d ]\n", header
->Margins
[0], header
->Margins
[1]);
95 fprintf(stderr
, "DEBUG: ManualFeed = %d\n", header
->ManualFeed
);
96 fprintf(stderr
, "DEBUG: MediaPosition = %d\n", header
->MediaPosition
);
97 fprintf(stderr
, "DEBUG: NumCopies = %d\n", header
->NumCopies
);
98 fprintf(stderr
, "DEBUG: Orientation = %d\n", header
->Orientation
);
99 fprintf(stderr
, "DEBUG: PageSize = [ %d %d ]\n", header
->PageSize
[0], header
->PageSize
[1]);
100 fprintf(stderr
, "DEBUG: cupsWidth = %d\n", header
->cupsWidth
);
101 fprintf(stderr
, "DEBUG: cupsHeight = %d\n", header
->cupsHeight
);
102 fprintf(stderr
, "DEBUG: cupsMediaType = %d\n", header
->cupsMediaType
);
103 fprintf(stderr
, "DEBUG: cupsBitsPerColor = %d\n", header
->cupsBitsPerColor
);
104 fprintf(stderr
, "DEBUG: cupsBitsPerPixel = %d\n", header
->cupsBitsPerPixel
);
105 fprintf(stderr
, "DEBUG: cupsBytesPerLine = %d\n", header
->cupsBytesPerLine
);
106 fprintf(stderr
, "DEBUG: cupsColorOrder = %d\n", header
->cupsColorOrder
);
107 fprintf(stderr
, "DEBUG: cupsColorSpace = %d\n", header
->cupsColorSpace
);
108 fprintf(stderr
, "DEBUG: cupsCompression = %d\n", header
->cupsCompression
);
111 * Setup printer/job attributes...
114 Duplex
= header
->Duplex
;
115 ColorBits
= header
->cupsBitsPerColor
;
117 if ((!Duplex
|| (Page
& 1)) && header
->MediaPosition
)
118 printf("\033&l%dH", /* Set media position */
119 header
->MediaPosition
);
121 if (Duplex
&& ppd
&& ppd
->model_number
== 2)
124 * Handle duplexing on new DeskJet printers...
127 printf("\033&l-2H"); /* Load media */
130 printf("\033&l2S"); /* Set duplex mode */
133 if (!Duplex
|| (Page
& 1) || (ppd
&& ppd
->model_number
== 2))
136 * Set the media size...
139 printf("\033&l6D\033&k12H"); /* Set 6 LPI, 10 CPI */
140 printf("\033&l0O"); /* Set portrait orientation */
142 switch (header
->PageSize
[1])
144 case 540 : /* Monarch Envelope */
145 printf("\033&l80A"); /* Set page size */
149 printf("\033&l25A"); /* Set page size */
152 case 624 : /* DL Envelope */
153 printf("\033&l90A"); /* Set page size */
156 case 649 : /* C5 Envelope */
157 printf("\033&l91A"); /* Set page size */
160 case 684 : /* COM-10 Envelope */
161 printf("\033&l81A"); /* Set page size */
164 case 709 : /* B5 Envelope */
165 printf("\033&l100A"); /* Set page size */
168 case 756 : /* Executive */
169 printf("\033&l1A"); /* Set page size */
172 case 792 : /* Letter */
173 printf("\033&l2A"); /* Set page size */
177 printf("\033&l26A"); /* Set page size */
180 case 1008 : /* Legal */
181 printf("\033&l3A"); /* Set page size */
185 printf("\033&l27A"); /* Set page size */
188 case 1224 : /* Tabloid */
189 printf("\033&l6A"); /* Set page size */
193 printf("\033&l%dP", /* Set page length */
194 header
->PageSize
[1] / 12);
195 printf("\033&l0E"); /* Set top margin to 0 */
198 if (!Duplex
|| (Page
& 1))
201 * Set other job options...
204 printf("\033&l%dX", header
->NumCopies
); /* Set number copies */
206 if (header
->cupsMediaType
&&
207 (!ppd
|| ppd
->model_number
!= 2 || header
->HWResolution
[0] == 600))
208 printf("\033&l%dM", /* Set media type */
209 header
->cupsMediaType
);
211 if (!ppd
|| ppd
->model_number
!= 2)
213 int mode
= Duplex
? 1 + header
->Tumble
!= 0 : 0;
215 printf("\033&l%dS", mode
); /* Set duplex mode */
216 printf("\033&l0L"); /* Turn off perforation skip */
219 else if (!ppd
|| ppd
->model_number
!= 2)
220 printf("\033&a2G"); /* Set back side */
223 * Set graphics mode...
226 if (ppd
&& ppd
->model_number
== 2)
229 * Figure out the number of color planes...
232 if (header
->cupsColorSpace
== CUPS_CSPACE_KCMY
)
238 * Set the resolution and top-of-form...
241 printf("\033&u%dD", header
->HWResolution
[0]);
243 printf("\033&l0e0L"); /* Reset top and don't skip */
244 printf("\033*p0Y\033*p0X"); /* Set top of form */
247 * Send 26-byte configure image data command with horizontal and
248 * vertical resolutions as well as a color count...
252 putchar(2); /* Format 2 */
253 putchar((int)NumPlanes
); /* Output planes */
255 putchar((int)(header
->HWResolution
[0] >> 8));/* Black resolution */
256 putchar((int)header
->HWResolution
[0]);
257 putchar((int)(header
->HWResolution
[1] >> 8));
258 putchar((int)header
->HWResolution
[1]);
260 putchar(1 << ColorBits
); /* # of black levels */
262 putchar((int)(header
->HWResolution
[0] >> 8));/* Cyan resolution */
263 putchar((int)header
->HWResolution
[0]);
264 putchar((int)(header
->HWResolution
[1] >> 8));
265 putchar((int)header
->HWResolution
[1]);
267 putchar(1 << ColorBits
); /* # of cyan levels */
269 putchar((int)(header
->HWResolution
[0] >> 8));/* Magenta resolution */
270 putchar((int)header
->HWResolution
[0]);
271 putchar((int)(header
->HWResolution
[1] >> 8));
272 putchar((int)header
->HWResolution
[1]);
274 putchar(1 << ColorBits
); /* # of magenta levels */
276 putchar((int)(header
->HWResolution
[0] >> 8));/* Yellow resolution */
277 putchar((int)header
->HWResolution
[0]);
278 putchar((int)(header
->HWResolution
[1] >> 8));
279 putchar((int)header
->HWResolution
[1]);
281 putchar(1 << ColorBits
); /* # of yellow levels */
283 printf("\033&l0H"); /* Set media position */
287 printf("\033*t%uR", header
->HWResolution
[0]);
290 if (header
->cupsColorSpace
== CUPS_CSPACE_KCMY
)
293 printf("\033*r-4U"); /* Set KCMY graphics */
295 else if (header
->cupsColorSpace
== CUPS_CSPACE_CMY
)
298 printf("\033*r-3U"); /* Set CMY graphics */
301 NumPlanes
= 1; /* Black&white graphics */
304 * Set size and position of graphics...
307 printf("\033*r%uS", header
->cupsWidth
); /* Set width */
308 printf("\033*r%uT", header
->cupsHeight
); /* Set height */
310 printf("\033&a0H"); /* Set horizontal position */
313 printf("\033&a%.0fV", /* Set vertical position */
314 10.0 * (ppd
->sizes
[0].length
- ppd
->sizes
[0].top
));
316 printf("\033&a0V"); /* Set top-of-page */
319 printf("\033*r1A"); /* Start graphics */
321 if (header
->cupsCompression
)
322 printf("\033*b%uM", /* Set compression */
323 header
->cupsCompression
);
325 Feed
= 0; /* No blank lines yet */
328 * Allocate memory for a line of graphics...
331 if ((Planes
[0] = malloc(header
->cupsBytesPerLine
+ NumPlanes
)) == NULL
)
333 fputs("ERROR: Unable to allocate memory\n", stderr
);
337 for (plane
= 1; plane
< NumPlanes
; plane
++)
338 Planes
[plane
] = Planes
[0] + plane
* header
->cupsBytesPerLine
/ NumPlanes
;
341 BitBuffer
= malloc(ColorBits
* ((header
->cupsWidth
+ 7) / 8));
345 if (header
->cupsCompression
)
346 CompBuffer
= malloc(header
->cupsBytesPerLine
* 2 + 2);
353 * 'EndPage()' - Finish a page of graphics.
360 * Eject the current page...
365 printf("\033*rC"); /* End color GFX */
367 if (!(Duplex
&& (Page
& 1)))
368 printf("\033&l0H"); /* Eject current page */
372 printf("\033*r0B"); /* End GFX */
374 if (!(Duplex
&& (Page
& 1)))
375 printf("\014"); /* Eject current page */
395 * 'Shutdown()' - Shutdown the printer.
402 * Send a PCL reset sequence.
411 * 'CancelJob()' - Cancel the current job...
415 CancelJob(int sig
) /* I - Signal */
424 * 'CompressData()' - Compress a line of graphics.
428 CompressData(unsigned char *line
, /* I - Data to compress */
429 unsigned length
, /* I - Number of bytes */
430 unsigned plane
, /* I - Color plane */
431 unsigned type
) /* I - Type of compression */
433 unsigned char *line_ptr
, /* Current byte pointer */
434 *line_end
, /* End-of-line byte pointer */
435 *comp_ptr
, /* Pointer into compression buffer */
436 *start
; /* Start of compression sequence */
437 unsigned count
; /* Count of bytes for output */
444 * Do no compression...
448 line_end
= line
+ length
;
453 * Do run-length encoding...
456 line_end
= line
+ length
;
457 for (line_ptr
= line
, comp_ptr
= CompBuffer
;
459 comp_ptr
+= 2, line_ptr
+= count
)
462 (line_ptr
+ count
) < line_end
&&
463 line_ptr
[0] == line_ptr
[count
] &&
467 comp_ptr
[0] = (unsigned char)(count
- 1);
468 comp_ptr
[1] = line_ptr
[0];
471 line_ptr
= CompBuffer
;
477 * Do TIFF pack-bits encoding...
481 line_end
= line
+ length
;
482 comp_ptr
= CompBuffer
;
484 while (line_ptr
< line_end
)
486 if ((line_ptr
+ 1) >= line_end
)
489 * Single byte on the end...
493 *comp_ptr
++ = *line_ptr
++;
495 else if (line_ptr
[0] == line_ptr
[1])
498 * Repeated sequence...
504 while (line_ptr
< (line_end
- 1) &&
505 line_ptr
[0] == line_ptr
[1] &&
512 *comp_ptr
++ = (unsigned char)(257 - count
);
513 *comp_ptr
++ = *line_ptr
++;
518 * Non-repeated sequence...
525 while (line_ptr
< (line_end
- 1) &&
526 line_ptr
[0] != line_ptr
[1] &&
533 *comp_ptr
++ = (unsigned char)(count
- 1);
535 memcpy(comp_ptr
, start
, count
);
540 line_ptr
= CompBuffer
;
546 * Set the length of the data and write a raster plane...
549 printf("\033*b%d%c", (int)(line_end
- line_ptr
), plane
);
550 fwrite(line_ptr
, (size_t)(line_end
- line_ptr
), 1, stdout
);
555 * 'OutputLine()' - Output a line of graphics.
559 OutputLine(cups_page_header2_t
*header
) /* I - Page header */
561 unsigned plane
, /* Current plane */
562 bytes
, /* Bytes to write */
563 count
; /* Bytes to convert */
564 unsigned char bit
, /* Current plane data */
565 bit0
, /* Current low bit data */
566 bit1
, /* Current high bit data */
567 *plane_ptr
, /* Pointer into Planes */
568 *bit_ptr
; /* Pointer into BitBuffer */
572 * Output whitespace as needed...
577 printf("\033*b%dY", Feed
);
582 * Write bitmap data as needed...
585 bytes
= (header
->cupsWidth
+ 7) / 8;
587 for (plane
= 0; plane
< NumPlanes
; plane
++)
594 CompressData(Planes
[plane
], bytes
, plane
< (NumPlanes
- 1) ? 'V' : 'W',
595 header
->cupsCompression
);
600 * Separate low and high bit data into separate buffers.
603 for (count
= header
->cupsBytesPerLine
/ NumPlanes
,
604 plane_ptr
= Planes
[plane
], bit_ptr
= BitBuffer
;
606 count
-= 2, plane_ptr
+= 2, bit_ptr
++)
610 bit0
= (unsigned char)(((bit
& 64) << 1) | ((bit
& 16) << 2) | ((bit
& 4) << 3) | ((bit
& 1) << 4));
611 bit1
= (unsigned char)((bit
& 128) | ((bit
& 32) << 1) | ((bit
& 8) << 2) | ((bit
& 2) << 3));
617 bit0
|= (unsigned char)((bit
& 1) | ((bit
& 4) >> 1) | ((bit
& 16) >> 2) | ((bit
& 64) >> 3));
618 bit1
|= (unsigned char)(((bit
& 2) >> 1) | ((bit
& 8) >> 2) | ((bit
& 32) >> 3) | ((bit
& 128) >> 4));
622 bit_ptr
[bytes
] = bit1
;
626 * Send low and high bits...
629 CompressData(BitBuffer
, bytes
, 'V', header
->cupsCompression
);
630 CompressData(BitBuffer
+ bytes
, bytes
, plane
< (NumPlanes
- 1) ? 'V' : 'W',
631 header
->cupsCompression
);
639 * 'main()' - Main entry and processing of driver.
642 int /* O - Exit status */
643 main(int argc
, /* I - Number of command-line arguments */
644 char *argv
[]) /* I - Command-line arguments */
646 int fd
; /* File descriptor */
647 cups_raster_t
*ras
; /* Raster stream for printing */
648 cups_page_header2_t header
; /* Page header from file */
649 unsigned y
; /* Current line */
650 ppd_file_t
*ppd
; /* PPD file */
651 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
652 struct sigaction action
; /* Actions for POSIX signals */
653 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
657 * Make sure status messages are not buffered...
660 setbuf(stderr
, NULL
);
663 * Check command-line...
666 if (argc
< 6 || argc
> 7)
669 * We don't have the correct number of arguments; write an error message
673 _cupsLangPrintFilter(stderr
, "ERROR",
674 _("%s job-id user title copies options [file]"),
680 * Open the page stream...
685 if ((fd
= open(argv
[6], O_RDONLY
)) == -1)
687 _cupsLangPrintError("ERROR", _("Unable to open raster file"));
695 ras
= cupsRasterOpen(fd
, CUPS_RASTER_READ
);
698 * Register a signal handler to eject the current page if the
704 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
705 sigset(SIGTERM
, CancelJob
);
706 #elif defined(HAVE_SIGACTION)
707 memset(&action
, 0, sizeof(action
));
709 sigemptyset(&action
.sa_mask
);
710 action
.sa_handler
= CancelJob
;
711 sigaction(SIGTERM
, &action
, NULL
);
713 signal(SIGTERM
, CancelJob
);
714 #endif /* HAVE_SIGSET */
717 * Initialize the print device...
720 ppd
= ppdOpenFile(getenv("PPD"));
723 ppd_status_t status
; /* PPD error */
724 int linenum
; /* Line number */
726 _cupsLangPrintFilter(stderr
, "ERROR",
727 _("The PPD file could not be opened."));
729 status
= ppdLastError(&linenum
);
731 fprintf(stderr
, "DEBUG: %s on line %d.\n", ppdErrorString(status
), linenum
);
739 * Process pages as needed...
744 while (cupsRasterReadHeader2(ras
, &header
))
747 * Write a status message with the page number and number of copies.
755 fprintf(stderr
, "PAGE: %d %d\n", Page
, header
.NumCopies
);
756 _cupsLangPrintFilter(stderr
, "INFO", _("Starting page %d."), Page
);
762 StartPage(ppd
, &header
);
765 * Loop for each line on the page...
768 for (y
= 0; y
< header
.cupsHeight
; y
++)
771 * Let the user know how far we have progressed...
779 _cupsLangPrintFilter(stderr
, "INFO",
780 _("Printing page %d, %u%% complete."),
781 Page
, 100 * y
/ header
.cupsHeight
);
782 fprintf(stderr
, "ATTR: job-media-progress=%u\n",
783 100 * y
/ header
.cupsHeight
);
787 * Read a line of graphics...
790 if (cupsRasterReadPixels(ras
, Planes
[0], header
.cupsBytesPerLine
) < 1)
794 * See if the line is blank; if not, write it to the printer...
798 memcmp(Planes
[0], Planes
[0] + 1, header
.cupsBytesPerLine
- 1))
808 _cupsLangPrintFilter(stderr
, "INFO", _("Finished page %d."), Page
);
817 * Shutdown the printer...
826 * Close the raster stream...
829 cupsRasterClose(ras
);
834 * If no pages were printed, send an error message...
839 _cupsLangPrintFilter(stderr
, "ERROR", _("No pages were found."));