2 * "$Id: rastertolabel.c 5665 2006-06-16 00:59:10Z 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
,
359 * Allocate memory for a line of graphics...
362 Buffer
= malloc(header
->cupsBytesPerLine
);
368 * 'EndPage()' - Finish a page of graphics.
372 EndPage(ppd_file_t
*ppd
, /* I - PPD file */
373 cups_page_header_t
*header
) /* I - Page header */
375 int val
; /* Option value */
376 ppd_choice_t
*choice
; /* Marked choice */
377 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
378 struct sigaction action
; /* Actions for POSIX signals */
379 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
386 * Eject the current page...
389 fputs("\033E", stdout
);
392 case ZEBRA_EPL_LINE
:
394 * End buffered output, eject the label...
397 fputs("\033E\014", stdout
);
400 case ZEBRA_EPL_PAGE
:
412 * Cancel bitmap download...
429 if ((choice
= ppdFindMarkedChoice(ppd
, "zePrintRate")) != NULL
&&
430 strcmp(choice
->choice
, "Default"))
432 val
= atoi(choice
->choice
);
433 printf("^PR%d,%d,%d\n", val
, val
, val
);
437 * Put label home in default position (0,0)...
443 * Set media tracking...
446 if (ppdIsMarked(ppd
, "zeMediaTracking", "Continuous"))
449 * Add label length command for continuous...
452 printf("^LL%d\n", header
->cupsHeight
);
455 else if (ppdIsMarked(ppd
, "zeMediaTracking", "Web"))
457 else if (ppdIsMarked(ppd
, "zeMediaTracking", "Mark"))
464 if (header
->cupsRowStep
!= 200)
465 printf("^LT%u\n", header
->cupsRowStep
);
471 if (!strcmp(header
->MediaType
, "Thermal"))
473 else if (!strcmp(header
->MediaType
, "Direct"))
480 if ((choice
= ppdFindMarkedChoice(ppd
, "zePrintMode")) != NULL
&&
481 strcmp(choice
->choice
, "Saved"))
485 if (!strcmp(choice
->choice
, "Tear"))
487 else if (!strcmp(choice
->choice
, "Peel"))
489 else if (!strcmp(choice
->choice
, "Rewind"))
491 else if (!strcmp(choice
->choice
, "Applicator"))
498 * Set tear-off adjust position...
501 if (header
->AdvanceDistance
!= 1000)
503 if ((int)header
->AdvanceDistance
< 0)
504 printf("~TA%04d\n", (int)header
->AdvanceDistance
);
506 printf("~TA%03d\n", (int)header
->AdvanceDistance
);
510 * Allow for reprinting after an error...
513 if (ppdIsMarked(ppd
, "zeErrorReprint", "Always"))
515 else if (ppdIsMarked(ppd
, "zeErrorReprint", "Never"))
519 * Print multiple copies
522 if (header
->NumCopies
> 1)
523 printf("^PQ%d, 0, 0, N\n", header
->NumCopies
);
526 * Display the label image...
529 puts("^FO0,0^XGR:CUPS.GRF,1,1^FS");
532 * End the label and eject...
538 * Free compression buffers...
547 * Set tear-off adjust position...
550 if (header
->AdvanceDistance
!= 1000)
551 printf("PRESENT-AT %d 1\r\n", (int)header
->AdvanceDistance
);
554 * Allow for reprinting after an error...
557 if (ppdIsMarked(ppd
, "zeErrorReprint", "Always"))
558 puts("ON-OUT-OF-PAPER WAIT\r");
559 else if (ppdIsMarked(ppd
, "zeErrorReprint", "Never"))
560 puts("ON-OUT-OF-PAPER PURGE\r");
566 if (header
->CutMedia
)
573 if (header
->cupsCompression
> 0)
574 printf("TONE %u\r\n", 2 * header
->cupsCompression
);
580 if ((choice
= ppdFindMarkedChoice(ppd
, "zePrintRate")) != NULL
&&
581 strcmp(choice
->choice
, "Default"))
583 val
= atoi(choice
->choice
);
584 printf("SPEED %d\r\n", val
);
599 * Unregister the signal handler...
602 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
603 sigset(SIGTERM
, SIG_IGN
);
604 #elif defined(HAVE_SIGACTION)
605 memset(&action
, 0, sizeof(action
));
607 sigemptyset(&action
.sa_mask
);
608 action
.sa_handler
= SIG_IGN
;
609 sigaction(SIGTERM
, &action
, NULL
);
611 signal(SIGTERM
, SIG_IGN
);
612 #endif /* HAVE_SIGSET */
623 * 'CancelJob()' - Cancel the current job...
627 CancelJob(int sig
) /* I - Signal */
630 * Tell the main loop to stop...
640 * 'OutputLine()' - Output a line of graphics...
644 OutputLine(ppd_file_t
*ppd
, /* I - PPD file */
645 cups_page_header_t
*header
, /* I - Page header */
646 int y
) /* I - Line number */
648 int i
; /* Looping var */
649 unsigned char *ptr
; /* Pointer into buffer */
650 unsigned char *compptr
; /* Pointer into compression buffer */
651 char repeat_char
; /* Repeated character */
652 int repeat_count
; /* Number of repeated characters */
653 static const char *hex
= "0123456789ABCDEF";
661 * See if the line is blank; if not, write it to the printer...
665 memcmp(Buffer
, Buffer
+ 1, header
->cupsBytesPerLine
- 1))
671 printf("\033f\001%c", 255);
675 printf("\033f\001%c", Feed
);
680 fwrite(Buffer
, header
->cupsBytesPerLine
, 1, stdout
);
685 * This hack works around a bug in the IRIX serial port driver when
686 * run at high baud rates (e.g. 115200 baud)... This results in
687 * slightly slower label printing, but at least the labels come
698 case ZEBRA_EPL_LINE
:
699 printf("\033g%03d", header
->cupsBytesPerLine
);
700 fwrite(Buffer
, 1, header
->cupsBytesPerLine
, stdout
);
704 case ZEBRA_EPL_PAGE
:
705 if (Buffer
[0] || memcmp(Buffer
, Buffer
+ 1, header
->cupsBytesPerLine
))
707 printf("GW0,%d,%d,1\n", y
, header
->cupsBytesPerLine
);
708 for (i
= header
->cupsBytesPerLine
, ptr
= Buffer
; i
> 0; i
--, ptr
++)
717 * Determine if this row is the same as the previous line.
718 * If so, output a ':' and return...
723 if (!memcmp(Buffer
, LastBuffer
, header
->cupsBytesPerLine
))
731 * Convert the line to hex digits...
734 for (ptr
= Buffer
, compptr
= CompBuffer
, i
= header
->cupsBytesPerLine
;
738 *compptr
++ = hex
[*ptr
>> 4];
739 *compptr
++ = hex
[*ptr
& 15];
745 * Run-length compress the graphics...
748 for (compptr
= CompBuffer
, repeat_char
= CompBuffer
[0], repeat_count
= 1;
751 if (*compptr
== repeat_char
)
755 ZPLCompress(repeat_char
, repeat_count
);
756 repeat_char
= *compptr
;
760 if (repeat_char
== '0')
763 * Handle 0's on the end of the line...
766 if (repeat_count
& 1)
772 ZPLCompress(repeat_char
, repeat_count
);
775 * Save this line for the next round...
778 memcpy(LastBuffer
, Buffer
, header
->cupsBytesPerLine
);
783 if (Buffer
[0] || memcmp(Buffer
, Buffer
+ 1, header
->cupsBytesPerLine
))
785 printf("CG %u 1 0 %d ", header
->cupsBytesPerLine
, y
);
786 fwrite(Buffer
, 1, header
->cupsBytesPerLine
, stdout
);
796 * 'ZPLCompress()' - Output a run-length compression sequence.
800 ZPLCompress(char repeat_char
, /* I - Character to repeat */
801 int repeat_count
) /* I - Number of repeated characters */
803 if (repeat_count
> 1)
806 * Print as many z's as possible - they are the largest denomination
807 * representing 400 characters (zC stands for 400 adjacent C's)
810 while (repeat_count
>= 400)
817 * Then print 'g' through 'y' as multiples of 20 characters...
820 if (repeat_count
>= 20)
822 putchar('f' + repeat_count
/ 20);
827 * Finally, print 'G' through 'Y' as 1 through 19 characters...
830 if (repeat_count
> 0)
831 putchar('F' + repeat_count
);
835 * Then the character to be repeated...
838 putchar(repeat_char
);
843 * 'main()' - Main entry and processing of driver.
846 int /* O - Exit status */
847 main(int argc
, /* I - Number of command-line arguments */
848 char *argv
[]) /* I - Command-line arguments */
850 int fd
; /* File descriptor */
851 cups_raster_t
*ras
; /* Raster stream for printing */
852 cups_page_header_t header
; /* Page header from file */
853 int y
; /* Current line */
854 ppd_file_t
*ppd
; /* PPD file */
855 int num_options
; /* Number of options */
856 cups_option_t
*options
; /* Options */
860 * Make sure status messages are not buffered...
863 setbuf(stderr
, NULL
);
866 * Check command-line...
869 if (argc
< 6 || argc
> 7)
872 * We don't have the correct number of arguments; write an error message
876 fputs("ERROR: rastertodymo job-id user title copies options [file]\n", stderr
);
881 * Open the page stream...
886 if ((fd
= open(argv
[6], O_RDONLY
)) == -1)
888 perror("ERROR: Unable to open raster file - ");
896 ras
= cupsRasterOpen(fd
, CUPS_RASTER_READ
);
899 * Open the PPD file and apply options...
902 num_options
= cupsParseOptions(argv
[5], 0, &options
);
904 if ((ppd
= ppdOpenFile(getenv("PPD"))) != NULL
)
906 ppdMarkDefaults(ppd
);
907 cupsMarkOptions(ppd
, num_options
, options
);
911 * Initialize the print device...
917 * Process pages as needed...
923 while (cupsRasterReadHeader(ras
, &header
))
926 * Write a status message with the page number and number of copies.
931 fprintf(stderr
, "PAGE: %d 1\n", Page
);
937 StartPage(ppd
, &header
);
940 * Loop for each line on the page...
943 for (y
= 0; y
< header
.cupsHeight
&& !Canceled
; y
++)
946 * Let the user know how far we have progressed...
950 fprintf(stderr
, "INFO: Printing page %d, %d%% complete...\n", Page
,
951 100 * y
/ header
.cupsHeight
);
954 * Read a line of graphics...
957 if (cupsRasterReadPixels(ras
, Buffer
, header
.cupsBytesPerLine
) < 1)
961 * Write it to the printer...
964 OutputLine(ppd
, &header
, y
);
971 EndPage(ppd
, &header
);
978 * Close the raster stream...
981 cupsRasterClose(ras
);
986 * Close the PPD file and free the options...
990 cupsFreeOptions(num_options
, options
);
993 * If no pages were printed, send an error message...
997 fputs("ERROR: No pages found!\n", stderr
);
999 fputs("INFO: Ready to print.\n", stderr
);
1006 * End of "$Id: rastertolabel.c 5665 2006-06-16 00:59:10Z mike $".