2 * "$Id: rastertolabel.c 5703 2006-06-29 18:12:04Z mike $"
4 * Label printer filter for the Common UNIX Printing System (CUPS).
6 * Copyright 2001-2006 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * Setup() - Prepare the printer for printing.
29 * StartPage() - Start a page of graphics.
30 * EndPage() - Finish a page of graphics.
31 * CancelJob() - Cancel the current job...
32 * OutputLine() - Output a line of graphics.
33 * ZPLCompress() - Output a run-length compression sequence.
34 * main() - Main entry and processing of driver.
38 * Include necessary headers...
41 #include <cups/cups.h>
42 #include <cups/string.h>
51 * This driver filter currently supports Dymo and Zebra label printers.
53 * The Dymo portion of the driver has been tested with the 300, 330,
54 * and 330 Turbo label printers; it may also work with older models.
55 * The Dymo printers support printing at 136, 203, and 300 DPI.
57 * The Zebra portion of the driver has been tested with the LP-2844Z label
58 * printer; it may also work with other models. The driver supports EPL
59 * line mode, EPL page mode, ZPL, and CPCL as defined in Zebra's on-line
60 * developer documentation.
64 * Model number constants...
67 #define DYMO_3x0 0 /* Dymo Labelwriter 300/330/330 Turbo */
69 #define ZEBRA_EPL_LINE 0x10 /* Zebra EPL line mode printers */
70 #define ZEBRA_EPL_PAGE 0x11 /* Zebra EPL page mode printers */
71 #define ZEBRA_ZPL 0x12 /* Zebra ZPL-based printers */
72 #define ZEBRA_CPCL 0x13 /* Zebra CPCL-based printers */
79 unsigned char *Buffer
; /* Output buffer */
80 char *CompBuffer
; /* Compression buffer */
81 unsigned char *LastBuffer
; /* Last buffer */
82 int LastSet
; /* Number of repeat characters */
83 int ModelNumber
, /* cupsModelNumber attribute */
84 Page
, /* Current page */
85 Feed
, /* Number of lines to skip */
86 Canceled
; /* Non-zero if job is canceled */
93 void Setup(ppd_file_t
*ppd
);
94 void StartPage(ppd_file_t
*ppd
, cups_page_header_t
*header
);
95 void EndPage(ppd_file_t
*ppd
, cups_page_header_t
*header
);
96 void CancelJob(int sig
);
97 void OutputLine(ppd_file_t
*ppd
, cups_page_header_t
*header
, int y
);
98 void ZPLCompress(char repeat_char
, int repeat_count
);
102 * 'Setup()' - Prepare the printer for printing.
106 Setup(ppd_file_t
*ppd
) /* I - PPD file */
108 int i
; /* Looping var */
112 * Get the model number from the PPD file...
116 ModelNumber
= ppd
->model_number
;
119 * Initialize based on the model number...
126 * Clear any remaining data...
129 for (i
= 0; i
< 100; i
++)
133 * Reset the printer...
136 fputs("\033@", stdout
);
139 case ZEBRA_EPL_LINE
:
142 case ZEBRA_EPL_PAGE
:
155 * 'StartPage()' - Start a page of graphics.
159 StartPage(ppd_file_t
*ppd
, /* I - PPD file */
160 cups_page_header_t
*header
) /* I - Page header */
162 ppd_choice_t
*choice
; /* Marked choice */
163 int length
; /* Actual label length */
164 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
165 struct sigaction action
; /* Actions for POSIX signals */
166 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
170 * Show page device dictionary...
173 fprintf(stderr
, "DEBUG: StartPage...\n");
174 fprintf(stderr
, "DEBUG: MediaClass = \"%s\"\n", header
->MediaClass
);
175 fprintf(stderr
, "DEBUG: MediaColor = \"%s\"\n", header
->MediaColor
);
176 fprintf(stderr
, "DEBUG: MediaType = \"%s\"\n", header
->MediaType
);
177 fprintf(stderr
, "DEBUG: OutputType = \"%s\"\n", header
->OutputType
);
179 fprintf(stderr
, "DEBUG: AdvanceDistance = %d\n", header
->AdvanceDistance
);
180 fprintf(stderr
, "DEBUG: AdvanceMedia = %d\n", header
->AdvanceMedia
);
181 fprintf(stderr
, "DEBUG: Collate = %d\n", header
->Collate
);
182 fprintf(stderr
, "DEBUG: CutMedia = %d\n", header
->CutMedia
);
183 fprintf(stderr
, "DEBUG: Duplex = %d\n", header
->Duplex
);
184 fprintf(stderr
, "DEBUG: HWResolution = [ %d %d ]\n", header
->HWResolution
[0],
185 header
->HWResolution
[1]);
186 fprintf(stderr
, "DEBUG: ImagingBoundingBox = [ %d %d %d %d ]\n",
187 header
->ImagingBoundingBox
[0], header
->ImagingBoundingBox
[1],
188 header
->ImagingBoundingBox
[2], header
->ImagingBoundingBox
[3]);
189 fprintf(stderr
, "DEBUG: InsertSheet = %d\n", header
->InsertSheet
);
190 fprintf(stderr
, "DEBUG: Jog = %d\n", header
->Jog
);
191 fprintf(stderr
, "DEBUG: LeadingEdge = %d\n", header
->LeadingEdge
);
192 fprintf(stderr
, "DEBUG: Margins = [ %d %d ]\n", header
->Margins
[0],
194 fprintf(stderr
, "DEBUG: ManualFeed = %d\n", header
->ManualFeed
);
195 fprintf(stderr
, "DEBUG: MediaPosition = %d\n", header
->MediaPosition
);
196 fprintf(stderr
, "DEBUG: MediaWeight = %d\n", header
->MediaWeight
);
197 fprintf(stderr
, "DEBUG: MirrorPrint = %d\n", header
->MirrorPrint
);
198 fprintf(stderr
, "DEBUG: NegativePrint = %d\n", header
->NegativePrint
);
199 fprintf(stderr
, "DEBUG: NumCopies = %d\n", header
->NumCopies
);
200 fprintf(stderr
, "DEBUG: Orientation = %d\n", header
->Orientation
);
201 fprintf(stderr
, "DEBUG: OutputFaceUp = %d\n", header
->OutputFaceUp
);
202 fprintf(stderr
, "DEBUG: PageSize = [ %d %d ]\n", header
->PageSize
[0],
203 header
->PageSize
[1]);
204 fprintf(stderr
, "DEBUG: Separations = %d\n", header
->Separations
);
205 fprintf(stderr
, "DEBUG: TraySwitch = %d\n", header
->TraySwitch
);
206 fprintf(stderr
, "DEBUG: Tumble = %d\n", header
->Tumble
);
207 fprintf(stderr
, "DEBUG: cupsWidth = %d\n", header
->cupsWidth
);
208 fprintf(stderr
, "DEBUG: cupsHeight = %d\n", header
->cupsHeight
);
209 fprintf(stderr
, "DEBUG: cupsMediaType = %d\n", header
->cupsMediaType
);
210 fprintf(stderr
, "DEBUG: cupsBitsPerColor = %d\n", header
->cupsBitsPerColor
);
211 fprintf(stderr
, "DEBUG: cupsBitsPerPixel = %d\n", header
->cupsBitsPerPixel
);
212 fprintf(stderr
, "DEBUG: cupsBytesPerLine = %d\n", header
->cupsBytesPerLine
);
213 fprintf(stderr
, "DEBUG: cupsColorOrder = %d\n", header
->cupsColorOrder
);
214 fprintf(stderr
, "DEBUG: cupsColorSpace = %d\n", header
->cupsColorSpace
);
215 fprintf(stderr
, "DEBUG: cupsCompression = %d\n", header
->cupsCompression
);
218 * Register a signal handler to eject the current page if the
222 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
223 sigset(SIGTERM
, CancelJob
);
224 #elif defined(HAVE_SIGACTION)
225 memset(&action
, 0, sizeof(action
));
227 sigemptyset(&action
.sa_mask
);
228 action
.sa_handler
= CancelJob
;
229 sigaction(SIGTERM
, &action
, NULL
);
231 signal(SIGTERM
, CancelJob
);
232 #endif /* HAVE_SIGSET */
238 * Setup printer/job attributes...
241 length
= header
->PageSize
[1] * header
->HWResolution
[1] / 72;
243 printf("\033L%c%c", length
>> 8, length
);
244 printf("\033D%c", header
->cupsBytesPerLine
);
246 printf("\033%c", header
->cupsCompression
+ 'c'); /* Darkness */
249 case ZEBRA_EPL_LINE
:
254 if ((choice
= ppdFindMarkedChoice(ppd
, "zePrintRate")) != NULL
&&
255 strcmp(choice
->choice
, "Default"))
256 printf("\033S%.0f", atof(choice
->choice
) * 2.0 - 2.0);
262 if (header
->cupsCompression
> 0 && header
->cupsCompression
<= 100)
263 printf("\033D%d", 7 * header
->cupsCompression
/ 100);
266 * Set left margin to 0...
269 fputs("\033M01", stdout
);
272 * Start buffered output...
275 fputs("\033B", stdout
);
278 case ZEBRA_EPL_PAGE
:
280 * Start a new label...
287 * Set hardware options...
290 if (!strcmp(header
->MediaType
, "Direct"))
297 if ((choice
= ppdFindMarkedChoice(ppd
, "zePrintRate")) != NULL
&&
298 strcmp(choice
->choice
, "Default"))
300 float val
= atof(choice
->choice
);
303 printf("S%.0f\n", val
);
305 printf("S%.0f\n", val
* 2.0 - 2.0);
312 if (header
->cupsCompression
> 0 && header
->cupsCompression
<= 100)
313 printf("D%d\n", 15 * header
->cupsCompression
/ 100);
319 printf("q%d\n", header
->cupsWidth
);
327 if (header
->cupsCompression
> 0 && header
->cupsCompression
<= 100)
328 printf("~SD%02d\n", 30 * header
->cupsCompression
/ 100);
331 * Start bitmap graphics...
334 printf("~DGR:CUPS.GRF,%d,%d,\n",
335 header
->cupsHeight
* header
->cupsBytesPerLine
,
336 header
->cupsBytesPerLine
);
339 * Allocate compression buffers...
342 CompBuffer
= malloc(2 * header
->cupsBytesPerLine
+ 1);
343 LastBuffer
= malloc(header
->cupsBytesPerLine
);
352 printf("! 0 %u %u %u %u\r\n", header
->HWResolution
[0],
353 header
->HWResolution
[1], header
->cupsHeight
,
355 printf("PAGE-WIDTH %d\r\n", header
->cupsWidth
);
356 printf("PAGE-HEIGHT %d\r\n", header
->cupsWidth
);
361 * Allocate memory for a line of graphics...
364 Buffer
= malloc(header
->cupsBytesPerLine
);
370 * 'EndPage()' - Finish a page of graphics.
374 EndPage(ppd_file_t
*ppd
, /* I - PPD file */
375 cups_page_header_t
*header
) /* I - Page header */
377 int val
; /* Option value */
378 ppd_choice_t
*choice
; /* Marked choice */
379 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
380 struct sigaction action
; /* Actions for POSIX signals */
381 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
388 * Eject the current page...
391 fputs("\033E", stdout
);
394 case ZEBRA_EPL_LINE
:
396 * End buffered output, eject the label...
399 fputs("\033E\014", stdout
);
402 case ZEBRA_EPL_PAGE
:
414 * Cancel bitmap download...
431 if ((choice
= ppdFindMarkedChoice(ppd
, "zePrintRate")) != NULL
&&
432 strcmp(choice
->choice
, "Default"))
434 val
= atoi(choice
->choice
);
435 printf("^PR%d,%d,%d\n", val
, val
, val
);
439 * Put label home in default position (0,0)...
445 * Set media tracking...
448 if (ppdIsMarked(ppd
, "zeMediaTracking", "Continuous"))
451 * Add label length command for continuous...
454 printf("^LL%d\n", header
->cupsHeight
);
457 else if (ppdIsMarked(ppd
, "zeMediaTracking", "Web"))
459 else if (ppdIsMarked(ppd
, "zeMediaTracking", "Mark"))
466 if (header
->cupsRowStep
!= 200)
467 printf("^LT%u\n", header
->cupsRowStep
);
473 if (!strcmp(header
->MediaType
, "Thermal"))
475 else if (!strcmp(header
->MediaType
, "Direct"))
482 if ((choice
= ppdFindMarkedChoice(ppd
, "zePrintMode")) != NULL
&&
483 strcmp(choice
->choice
, "Saved"))
487 if (!strcmp(choice
->choice
, "Tear"))
489 else if (!strcmp(choice
->choice
, "Peel"))
491 else if (!strcmp(choice
->choice
, "Rewind"))
493 else if (!strcmp(choice
->choice
, "Applicator"))
500 * Set tear-off adjust position...
503 if (header
->AdvanceDistance
!= 1000)
505 if ((int)header
->AdvanceDistance
< 0)
506 printf("~TA%04d\n", (int)header
->AdvanceDistance
);
508 printf("~TA%03d\n", (int)header
->AdvanceDistance
);
512 * Allow for reprinting after an error...
515 if (ppdIsMarked(ppd
, "zeErrorReprint", "Always"))
517 else if (ppdIsMarked(ppd
, "zeErrorReprint", "Never"))
521 * Print multiple copies
524 if (header
->NumCopies
> 1)
525 printf("^PQ%d, 0, 0, N\n", header
->NumCopies
);
528 * Display the label image...
531 puts("^FO0,0^XGR:CUPS.GRF,1,1^FS");
534 * End the label and eject...
540 * Free compression buffers...
549 * Set tear-off adjust position...
552 if (header
->AdvanceDistance
!= 1000)
553 printf("PRESENT-AT %d 1\r\n", (int)header
->AdvanceDistance
);
556 * Allow for reprinting after an error...
559 if (ppdIsMarked(ppd
, "zeErrorReprint", "Always"))
560 puts("ON-OUT-OF-PAPER WAIT\r");
561 else if (ppdIsMarked(ppd
, "zeErrorReprint", "Never"))
562 puts("ON-OUT-OF-PAPER PURGE\r");
568 if (header
->CutMedia
)
575 if (header
->cupsCompression
> 0)
576 printf("TONE %u\r\n", 2 * header
->cupsCompression
);
582 if ((choice
= ppdFindMarkedChoice(ppd
, "zePrintRate")) != NULL
&&
583 strcmp(choice
->choice
, "Default"))
585 val
= atoi(choice
->choice
);
586 printf("SPEED %d\r\n", val
);
593 if ((choice
= ppdFindMarkedChoice(ppd
, "zeMediaTracking")) == NULL
||
594 strcmp(choice
->choice
, "Continuous"))
604 * Unregister the signal handler...
607 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
608 sigset(SIGTERM
, SIG_IGN
);
609 #elif defined(HAVE_SIGACTION)
610 memset(&action
, 0, sizeof(action
));
612 sigemptyset(&action
.sa_mask
);
613 action
.sa_handler
= SIG_IGN
;
614 sigaction(SIGTERM
, &action
, NULL
);
616 signal(SIGTERM
, SIG_IGN
);
617 #endif /* HAVE_SIGSET */
628 * 'CancelJob()' - Cancel the current job...
632 CancelJob(int sig
) /* I - Signal */
635 * Tell the main loop to stop...
645 * 'OutputLine()' - Output a line of graphics...
649 OutputLine(ppd_file_t
*ppd
, /* I - PPD file */
650 cups_page_header_t
*header
, /* I - Page header */
651 int y
) /* I - Line number */
653 int i
; /* Looping var */
654 unsigned char *ptr
; /* Pointer into buffer */
655 unsigned char *compptr
; /* Pointer into compression buffer */
656 char repeat_char
; /* Repeated character */
657 int repeat_count
; /* Number of repeated characters */
658 static const char *hex
= "0123456789ABCDEF";
666 * See if the line is blank; if not, write it to the printer...
670 memcmp(Buffer
, Buffer
+ 1, header
->cupsBytesPerLine
- 1))
676 printf("\033f\001%c", 255);
680 printf("\033f\001%c", Feed
);
685 fwrite(Buffer
, header
->cupsBytesPerLine
, 1, stdout
);
690 * This hack works around a bug in the IRIX serial port driver when
691 * run at high baud rates (e.g. 115200 baud)... This results in
692 * slightly slower label printing, but at least the labels come
703 case ZEBRA_EPL_LINE
:
704 printf("\033g%03d", header
->cupsBytesPerLine
);
705 fwrite(Buffer
, 1, header
->cupsBytesPerLine
, stdout
);
709 case ZEBRA_EPL_PAGE
:
710 if (Buffer
[0] || memcmp(Buffer
, Buffer
+ 1, header
->cupsBytesPerLine
))
712 printf("GW0,%d,%d,1\n", y
, header
->cupsBytesPerLine
);
713 for (i
= header
->cupsBytesPerLine
, ptr
= Buffer
; i
> 0; i
--, ptr
++)
722 * Determine if this row is the same as the previous line.
723 * If so, output a ':' and return...
728 if (!memcmp(Buffer
, LastBuffer
, header
->cupsBytesPerLine
))
736 * Convert the line to hex digits...
739 for (ptr
= Buffer
, compptr
= CompBuffer
, i
= header
->cupsBytesPerLine
;
743 *compptr
++ = hex
[*ptr
>> 4];
744 *compptr
++ = hex
[*ptr
& 15];
750 * Run-length compress the graphics...
753 for (compptr
= CompBuffer
, repeat_char
= CompBuffer
[0], repeat_count
= 1;
756 if (*compptr
== repeat_char
)
760 ZPLCompress(repeat_char
, repeat_count
);
761 repeat_char
= *compptr
;
765 if (repeat_char
== '0')
768 * Handle 0's on the end of the line...
771 if (repeat_count
& 1)
777 ZPLCompress(repeat_char
, repeat_count
);
780 * Save this line for the next round...
783 memcpy(LastBuffer
, Buffer
, header
->cupsBytesPerLine
);
788 if (Buffer
[0] || memcmp(Buffer
, Buffer
+ 1, header
->cupsBytesPerLine
))
790 printf("CG %u 1 0 %d ", header
->cupsBytesPerLine
, y
);
791 fwrite(Buffer
, 1, header
->cupsBytesPerLine
, stdout
);
801 * 'ZPLCompress()' - Output a run-length compression sequence.
805 ZPLCompress(char repeat_char
, /* I - Character to repeat */
806 int repeat_count
) /* I - Number of repeated characters */
808 if (repeat_count
> 1)
811 * Print as many z's as possible - they are the largest denomination
812 * representing 400 characters (zC stands for 400 adjacent C's)
815 while (repeat_count
>= 400)
822 * Then print 'g' through 'y' as multiples of 20 characters...
825 if (repeat_count
>= 20)
827 putchar('f' + repeat_count
/ 20);
832 * Finally, print 'G' through 'Y' as 1 through 19 characters...
835 if (repeat_count
> 0)
836 putchar('F' + repeat_count
);
840 * Then the character to be repeated...
843 putchar(repeat_char
);
848 * 'main()' - Main entry and processing of driver.
851 int /* O - Exit status */
852 main(int argc
, /* I - Number of command-line arguments */
853 char *argv
[]) /* I - Command-line arguments */
855 int fd
; /* File descriptor */
856 cups_raster_t
*ras
; /* Raster stream for printing */
857 cups_page_header_t header
; /* Page header from file */
858 int y
; /* Current line */
859 ppd_file_t
*ppd
; /* PPD file */
860 int num_options
; /* Number of options */
861 cups_option_t
*options
; /* Options */
865 * Make sure status messages are not buffered...
868 setbuf(stderr
, NULL
);
871 * Check command-line...
874 if (argc
< 6 || argc
> 7)
877 * We don't have the correct number of arguments; write an error message
881 fputs("ERROR: rastertodymo job-id user title copies options [file]\n", stderr
);
886 * Open the page stream...
891 if ((fd
= open(argv
[6], O_RDONLY
)) == -1)
893 perror("ERROR: Unable to open raster file - ");
901 ras
= cupsRasterOpen(fd
, CUPS_RASTER_READ
);
904 * Open the PPD file and apply options...
907 num_options
= cupsParseOptions(argv
[5], 0, &options
);
909 if ((ppd
= ppdOpenFile(getenv("PPD"))) != NULL
)
911 ppdMarkDefaults(ppd
);
912 cupsMarkOptions(ppd
, num_options
, options
);
916 * Initialize the print device...
922 * Process pages as needed...
928 while (cupsRasterReadHeader(ras
, &header
))
931 * Write a status message with the page number and number of copies.
936 fprintf(stderr
, "PAGE: %d 1\n", Page
);
942 StartPage(ppd
, &header
);
945 * Loop for each line on the page...
948 for (y
= 0; y
< header
.cupsHeight
&& !Canceled
; y
++)
951 * Let the user know how far we have progressed...
955 fprintf(stderr
, "INFO: Printing page %d, %d%% complete...\n", Page
,
956 100 * y
/ header
.cupsHeight
);
959 * Read a line of graphics...
962 if (cupsRasterReadPixels(ras
, Buffer
, header
.cupsBytesPerLine
) < 1)
966 * Write it to the printer...
969 OutputLine(ppd
, &header
, y
);
976 EndPage(ppd
, &header
);
983 * Close the raster stream...
986 cupsRasterClose(ras
);
991 * Close the PPD file and free the options...
995 cupsFreeOptions(num_options
, options
);
998 * If no pages were printed, send an error message...
1002 fputs("ERROR: No pages found!\n", stderr
);
1004 fputs("INFO: Ready to print.\n", stderr
);
1011 * End of "$Id: rastertolabel.c 5703 2006-06-29 18:12:04Z mike $".