]> git.ipfire.org Git - thirdparty/cups.git/blame - filter/rastertolabel.c
Merge changes from CUPS 1.5svn-r8849.
[thirdparty/cups.git] / filter / rastertolabel.c
CommitLineData
ef416fc2 1/*
b19ccc9e 2 * "$Id: rastertolabel.c 7720 2008-07-11 22:46:21Z mike $"
ef416fc2 3 *
4 * Label printer filter for the Common UNIX Printing System (CUPS).
5 *
4d301e69 6 * Copyright 2007-2009 by Apple Inc.
b86bc4cf 7 * Copyright 2001-2007 by Easy Software Products.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
19 * Setup() - Prepare the printer for printing.
20 * StartPage() - Start a page of graphics.
21 * EndPage() - Finish a page of graphics.
22 * CancelJob() - Cancel the current job...
23 * OutputLine() - Output a line of graphics.
f7deaa1a 24 * PCLCompress() - Output a PCL (mode 3) compressed line.
ef416fc2 25 * ZPLCompress() - Output a run-length compression sequence.
26 * main() - Main entry and processing of driver.
27 */
28
29/*
30 * Include necessary headers...
31 */
32
33#include <cups/cups.h>
34#include <cups/string.h>
c0e1af83 35#include <cups/i18n.h>
ac884b6a 36#include <cups/raster.h>
ef416fc2 37#include <stdlib.h>
38#include <unistd.h>
39#include <fcntl.h>
40#include <signal.h>
f0ab5bff 41#include <errno.h>
ef416fc2 42
43
44/*
f7deaa1a 45 * This driver filter currently supports Dymo, Intellitech, and Zebra
46 * label printers.
ef416fc2 47 *
48 * The Dymo portion of the driver has been tested with the 300, 330,
f7deaa1a 49 * and 330 Turbo label printers; it may also work with other models.
ef416fc2 50 * The Dymo printers support printing at 136, 203, and 300 DPI.
51 *
f7deaa1a 52 * The Intellitech portion of the driver has been tested with the
53 * Intellibar 408, 412, and 808 and supports their PCL variant.
54 *
55 * The Zebra portion of the driver has been tested with the LP-2844,
56 * LP-2844Z, QL-320, and QL-420 label printers; it may also work with
57 * other models. The driver supports EPL line mode, EPL page mode,
79e1d494 58 * ZPL, and CPCL as defined in Zebra's online developer documentation.
ef416fc2 59 */
60
61/*
62 * Model number constants...
63 */
64
65#define DYMO_3x0 0 /* Dymo Labelwriter 300/330/330 Turbo */
66
67#define ZEBRA_EPL_LINE 0x10 /* Zebra EPL line mode printers */
68#define ZEBRA_EPL_PAGE 0x11 /* Zebra EPL page mode printers */
69#define ZEBRA_ZPL 0x12 /* Zebra ZPL-based printers */
70#define ZEBRA_CPCL 0x13 /* Zebra CPCL-based printers */
71
f7deaa1a 72#define INTELLITECH_PCL 0x20 /* Intellitech PCL-based printers */
73
ef416fc2 74
75/*
76 * Globals...
77 */
78
79unsigned char *Buffer; /* Output buffer */
b86bc4cf 80unsigned char *CompBuffer; /* Compression buffer */
ef416fc2 81unsigned char *LastBuffer; /* Last buffer */
82int LastSet; /* Number of repeat characters */
83int ModelNumber, /* cupsModelNumber attribute */
84 Page, /* Current page */
85 Feed, /* Number of lines to skip */
86 Canceled; /* Non-zero if job is canceled */
87
88
89/*
90 * Prototypes...
91 */
92
93void Setup(ppd_file_t *ppd);
839a51c8
MS
94void StartPage(ppd_file_t *ppd, cups_page_header2_t *header);
95void EndPage(ppd_file_t *ppd, cups_page_header2_t *header);
ef416fc2 96void CancelJob(int sig);
839a51c8 97void OutputLine(ppd_file_t *ppd, cups_page_header2_t *header, int y);
f7deaa1a 98void PCLCompress(unsigned char *line, int length);
ef416fc2 99void ZPLCompress(char repeat_char, int repeat_count);
100
101
102/*
103 * 'Setup()' - Prepare the printer for printing.
104 */
105
106void
107Setup(ppd_file_t *ppd) /* I - PPD file */
108{
109 int i; /* Looping var */
110
111
112 /*
113 * Get the model number from the PPD file...
114 */
115
116 if (ppd)
117 ModelNumber = ppd->model_number;
118
119 /*
120 * Initialize based on the model number...
121 */
122
123 switch (ModelNumber)
124 {
125 case DYMO_3x0 :
126 /*
127 * Clear any remaining data...
128 */
129
130 for (i = 0; i < 100; i ++)
131 putchar(0x1b);
132
133 /*
134 * Reset the printer...
135 */
136
137 fputs("\033@", stdout);
138 break;
139
140 case ZEBRA_EPL_LINE :
141 break;
142
143 case ZEBRA_EPL_PAGE :
144 break;
145
146 case ZEBRA_ZPL :
147 break;
148
149 case ZEBRA_CPCL :
150 break;
f7deaa1a 151
152 case INTELLITECH_PCL :
153 /*
154 * Send a PCL reset sequence.
155 */
156
157 putchar(0x1b);
158 putchar('E');
159 break;
ef416fc2 160 }
161}
162
163
164/*
165 * 'StartPage()' - Start a page of graphics.
166 */
167
168void
e1d6a774 169StartPage(ppd_file_t *ppd, /* I - PPD file */
839a51c8 170 cups_page_header2_t *header) /* I - Page header */
ef416fc2 171{
172 ppd_choice_t *choice; /* Marked choice */
173 int length; /* Actual label length */
ef416fc2 174
175
e1d6a774 176 /*
177 * Show page device dictionary...
178 */
179
180 fprintf(stderr, "DEBUG: StartPage...\n");
181 fprintf(stderr, "DEBUG: MediaClass = \"%s\"\n", header->MediaClass);
182 fprintf(stderr, "DEBUG: MediaColor = \"%s\"\n", header->MediaColor);
183 fprintf(stderr, "DEBUG: MediaType = \"%s\"\n", header->MediaType);
184 fprintf(stderr, "DEBUG: OutputType = \"%s\"\n", header->OutputType);
185
186 fprintf(stderr, "DEBUG: AdvanceDistance = %d\n", header->AdvanceDistance);
187 fprintf(stderr, "DEBUG: AdvanceMedia = %d\n", header->AdvanceMedia);
188 fprintf(stderr, "DEBUG: Collate = %d\n", header->Collate);
189 fprintf(stderr, "DEBUG: CutMedia = %d\n", header->CutMedia);
190 fprintf(stderr, "DEBUG: Duplex = %d\n", header->Duplex);
191 fprintf(stderr, "DEBUG: HWResolution = [ %d %d ]\n", header->HWResolution[0],
192 header->HWResolution[1]);
193 fprintf(stderr, "DEBUG: ImagingBoundingBox = [ %d %d %d %d ]\n",
194 header->ImagingBoundingBox[0], header->ImagingBoundingBox[1],
195 header->ImagingBoundingBox[2], header->ImagingBoundingBox[3]);
196 fprintf(stderr, "DEBUG: InsertSheet = %d\n", header->InsertSheet);
197 fprintf(stderr, "DEBUG: Jog = %d\n", header->Jog);
198 fprintf(stderr, "DEBUG: LeadingEdge = %d\n", header->LeadingEdge);
199 fprintf(stderr, "DEBUG: Margins = [ %d %d ]\n", header->Margins[0],
200 header->Margins[1]);
201 fprintf(stderr, "DEBUG: ManualFeed = %d\n", header->ManualFeed);
202 fprintf(stderr, "DEBUG: MediaPosition = %d\n", header->MediaPosition);
203 fprintf(stderr, "DEBUG: MediaWeight = %d\n", header->MediaWeight);
204 fprintf(stderr, "DEBUG: MirrorPrint = %d\n", header->MirrorPrint);
205 fprintf(stderr, "DEBUG: NegativePrint = %d\n", header->NegativePrint);
206 fprintf(stderr, "DEBUG: NumCopies = %d\n", header->NumCopies);
207 fprintf(stderr, "DEBUG: Orientation = %d\n", header->Orientation);
208 fprintf(stderr, "DEBUG: OutputFaceUp = %d\n", header->OutputFaceUp);
209 fprintf(stderr, "DEBUG: PageSize = [ %d %d ]\n", header->PageSize[0],
210 header->PageSize[1]);
211 fprintf(stderr, "DEBUG: Separations = %d\n", header->Separations);
212 fprintf(stderr, "DEBUG: TraySwitch = %d\n", header->TraySwitch);
213 fprintf(stderr, "DEBUG: Tumble = %d\n", header->Tumble);
214 fprintf(stderr, "DEBUG: cupsWidth = %d\n", header->cupsWidth);
215 fprintf(stderr, "DEBUG: cupsHeight = %d\n", header->cupsHeight);
216 fprintf(stderr, "DEBUG: cupsMediaType = %d\n", header->cupsMediaType);
217 fprintf(stderr, "DEBUG: cupsBitsPerColor = %d\n", header->cupsBitsPerColor);
218 fprintf(stderr, "DEBUG: cupsBitsPerPixel = %d\n", header->cupsBitsPerPixel);
219 fprintf(stderr, "DEBUG: cupsBytesPerLine = %d\n", header->cupsBytesPerLine);
220 fprintf(stderr, "DEBUG: cupsColorOrder = %d\n", header->cupsColorOrder);
221 fprintf(stderr, "DEBUG: cupsColorSpace = %d\n", header->cupsColorSpace);
222 fprintf(stderr, "DEBUG: cupsCompression = %d\n", header->cupsCompression);
f7deaa1a 223 fprintf(stderr, "DEBUG: cupsRowCount = %d\n", header->cupsRowCount);
224 fprintf(stderr, "DEBUG: cupsRowFeed = %d\n", header->cupsRowFeed);
225 fprintf(stderr, "DEBUG: cupsRowStep = %d\n", header->cupsRowStep);
e1d6a774 226
ef416fc2 227 switch (ModelNumber)
228 {
229 case DYMO_3x0 :
230 /*
231 * Setup printer/job attributes...
232 */
233
234 length = header->PageSize[1] * header->HWResolution[1] / 72;
235
236 printf("\033L%c%c", length >> 8, length);
237 printf("\033D%c", header->cupsBytesPerLine);
238
239 printf("\033%c", header->cupsCompression + 'c'); /* Darkness */
240 break;
241
242 case ZEBRA_EPL_LINE :
243 /*
244 * Set print rate...
245 */
246
247 if ((choice = ppdFindMarkedChoice(ppd, "zePrintRate")) != NULL &&
248 strcmp(choice->choice, "Default"))
249 printf("\033S%.0f", atof(choice->choice) * 2.0 - 2.0);
250
251 /*
252 * Set darkness...
253 */
254
255 if (header->cupsCompression > 0 && header->cupsCompression <= 100)
256 printf("\033D%d", 7 * header->cupsCompression / 100);
257
258 /*
259 * Set left margin to 0...
260 */
261
262 fputs("\033M01", stdout);
263
264 /*
265 * Start buffered output...
266 */
267
268 fputs("\033B", stdout);
269 break;
270
271 case ZEBRA_EPL_PAGE :
272 /*
273 * Start a new label...
274 */
275
276 puts("");
277 puts("N");
278
279 /*
280 * Set hardware options...
281 */
282
283 if (!strcmp(header->MediaType, "Direct"))
284 puts("OD");
285
286 /*
287 * Set print rate...
288 */
289
290 if ((choice = ppdFindMarkedChoice(ppd, "zePrintRate")) != NULL &&
291 strcmp(choice->choice, "Default"))
292 {
293 float val = atof(choice->choice);
294
295 if (val >= 3.0)
296 printf("S%.0f\n", val);
297 else
298 printf("S%.0f\n", val * 2.0 - 2.0);
299 }
300
301 /*
302 * Set darkness...
303 */
304
305 if (header->cupsCompression > 0 && header->cupsCompression <= 100)
306 printf("D%d\n", 15 * header->cupsCompression / 100);
307
308 /*
309 * Set label size...
310 */
311
f42414bf 312 printf("q%d\n", (header->cupsWidth + 7) & ~7);
ef416fc2 313 break;
314
315 case ZEBRA_ZPL :
316 /*
317 * Set darkness...
318 */
319
320 if (header->cupsCompression > 0 && header->cupsCompression <= 100)
321 printf("~SD%02d\n", 30 * header->cupsCompression / 100);
322
323 /*
324 * Start bitmap graphics...
325 */
326
327 printf("~DGR:CUPS.GRF,%d,%d,\n",
328 header->cupsHeight * header->cupsBytesPerLine,
329 header->cupsBytesPerLine);
330
331 /*
332 * Allocate compression buffers...
333 */
334
335 CompBuffer = malloc(2 * header->cupsBytesPerLine + 1);
336 LastBuffer = malloc(header->cupsBytesPerLine);
337 LastSet = 0;
338 break;
339
340 case ZEBRA_CPCL :
341 /*
342 * Start label...
343 */
344
345 printf("! 0 %u %u %u %u\r\n", header->HWResolution[0],
346 header->HWResolution[1], header->cupsHeight,
347 header->NumCopies);
8ca02f3c 348 printf("PAGE-WIDTH %d\r\n", header->cupsWidth);
349 printf("PAGE-HEIGHT %d\r\n", header->cupsWidth);
ef416fc2 350 break;
f7deaa1a 351
352 case INTELLITECH_PCL :
353 /*
354 * Set the media size...
355 */
356
357 printf("\033&l6D\033&k12H"); /* Set 6 LPI, 10 CPI */
358 printf("\033&l0O"); /* Set portrait orientation */
359
360 switch (header->PageSize[1])
361 {
362 case 540 : /* Monarch Envelope */
363 printf("\033&l80A"); /* Set page size */
364 break;
365
366 case 624 : /* DL Envelope */
367 printf("\033&l90A"); /* Set page size */
368 break;
369
370 case 649 : /* C5 Envelope */
371 printf("\033&l91A"); /* Set page size */
372 break;
373
374 case 684 : /* COM-10 Envelope */
375 printf("\033&l81A"); /* Set page size */
376 break;
377
378 case 756 : /* Executive */
379 printf("\033&l1A"); /* Set page size */
380 break;
381
382 case 792 : /* Letter */
383 printf("\033&l2A"); /* Set page size */
384 break;
385
386 case 842 : /* A4 */
387 printf("\033&l26A"); /* Set page size */
388 break;
389
390 case 1008 : /* Legal */
391 printf("\033&l3A"); /* Set page size */
392 break;
393
394 default : /* Custom size */
395 printf("\033!f%dZ", header->PageSize[1] * 300 / 72);
396 break;
397 }
398
399 printf("\033&l%dP", /* Set page length */
400 header->PageSize[1] / 12);
401 printf("\033&l0E"); /* Set top margin to 0 */
402 printf("\033&l%dX", header->NumCopies);
403 /* Set number copies */
404 printf("\033&l0L"); /* Turn off perforation skip */
405
406 /*
407 * Print settings...
408 */
409
410 if (Page == 1)
411 {
412 if (header->cupsRowFeed) /* inPrintRate */
413 printf("\033!p%dS", header->cupsRowFeed);
414
415 if (header->cupsCompression != ~0)
416 /* inPrintDensity */
417 printf("\033&d%dA", 30 * header->cupsCompression / 100 - 15);
418
419 if ((choice = ppdFindMarkedChoice(ppd, "inPrintMode")) != NULL)
420 {
421 if (!strcmp(choice->choice, "Standard"))
422 fputs("\033!p0M", stdout);
423 else if (!strcmp(choice->choice, "Tear"))
424 {
425 fputs("\033!p1M", stdout);
426
427 if (header->cupsRowCount) /* inTearInterval */
428 printf("\033!n%dT", header->cupsRowCount);
429 }
430 else
431 {
432 fputs("\033!p2M", stdout);
433
434 if (header->cupsRowStep) /* inCutInterval */
435 printf("\033!n%dC", header->cupsRowStep);
436 }
437 }
438 }
439
440 /*
441 * Setup graphics...
442 */
443
444 printf("\033*t%dR", header->HWResolution[0]);
445 /* Set resolution */
446
447 printf("\033*r%dS", header->cupsWidth);
448 /* Set width */
449 printf("\033*r%dT", header->cupsHeight);
450 /* Set height */
451
452 printf("\033&a0H"); /* Set horizontal position */
453 printf("\033&a0V"); /* Set vertical position */
454 printf("\033*r1A"); /* Start graphics */
455 printf("\033*b3M"); /* Set compression */
456
457 /*
458 * Allocate compression buffers...
459 */
460
461 CompBuffer = malloc(2 * header->cupsBytesPerLine + 1);
462 LastBuffer = malloc(header->cupsBytesPerLine);
463 LastSet = 0;
464 break;
ef416fc2 465 }
466
467 /*
468 * Allocate memory for a line of graphics...
469 */
470
471 Buffer = malloc(header->cupsBytesPerLine);
472 Feed = 0;
473}
474
475
476/*
477 * 'EndPage()' - Finish a page of graphics.
478 */
479
480void
481EndPage(ppd_file_t *ppd, /* I - PPD file */
839a51c8 482 cups_page_header2_t *header) /* I - Page header */
ef416fc2 483{
484 int val; /* Option value */
485 ppd_choice_t *choice; /* Marked choice */
ef416fc2 486
487
488 switch (ModelNumber)
489 {
490 case DYMO_3x0 :
491 /*
492 * Eject the current page...
493 */
494
495 fputs("\033E", stdout);
496 break;
497
498 case ZEBRA_EPL_LINE :
499 /*
500 * End buffered output, eject the label...
501 */
502
503 fputs("\033E\014", stdout);
504 break;
505
506 case ZEBRA_EPL_PAGE :
507 /*
508 * Print the label...
509 */
510
511 puts("P1");
512 break;
513
514 case ZEBRA_ZPL :
515 if (Canceled)
516 {
517 /*
518 * Cancel bitmap download...
519 */
520
521 puts("~DN");
522 break;
523 }
524
525 /*
526 * Start label...
527 */
528
529 puts("^XA");
530
531 /*
532 * Set print rate...
533 */
534
535 if ((choice = ppdFindMarkedChoice(ppd, "zePrintRate")) != NULL &&
536 strcmp(choice->choice, "Default"))
537 {
538 val = atoi(choice->choice);
539 printf("^PR%d,%d,%d\n", val, val, val);
540 }
541
542 /*
543 * Put label home in default position (0,0)...
544 */
545
546 printf("^LH0,0\n");
547
548 /*
549 * Set media tracking...
550 */
551
552 if (ppdIsMarked(ppd, "zeMediaTracking", "Continuous"))
553 {
554 /*
555 * Add label length command for continuous...
556 */
557
558 printf("^LL%d\n", header->cupsHeight);
559 printf("^MNN\n");
560 }
561 else if (ppdIsMarked(ppd, "zeMediaTracking", "Web"))
562 printf("^MNY\n");
563 else if (ppdIsMarked(ppd, "zeMediaTracking", "Mark"))
564 printf("^MNM\n");
565
566 /*
567 * Set label top
568 */
569
570 if (header->cupsRowStep != 200)
571 printf("^LT%u\n", header->cupsRowStep);
572
573 /*
574 * Set media type...
575 */
576
577 if (!strcmp(header->MediaType, "Thermal"))
578 printf("^MTT\n");
579 else if (!strcmp(header->MediaType, "Direct"))
580 printf("^MTD\n");
581
582 /*
583 * Set print mode...
584 */
585
586 if ((choice = ppdFindMarkedChoice(ppd, "zePrintMode")) != NULL &&
587 strcmp(choice->choice, "Saved"))
588 {
589 printf("^MM");
590
591 if (!strcmp(choice->choice, "Tear"))
592 printf("T,Y\n");
593 else if (!strcmp(choice->choice, "Peel"))
594 printf("P,Y\n");
595 else if (!strcmp(choice->choice, "Rewind"))
596 printf("R,Y\n");
597 else if (!strcmp(choice->choice, "Applicator"))
598 printf("A,Y\n");
599 else
600 printf("C,Y\n");
601 }
602
603 /*
604 * Set tear-off adjust position...
605 */
606
607 if (header->AdvanceDistance != 1000)
608 {
609 if ((int)header->AdvanceDistance < 0)
610 printf("~TA%04d\n", (int)header->AdvanceDistance);
611 else
612 printf("~TA%03d\n", (int)header->AdvanceDistance);
613 }
614
615 /*
616 * Allow for reprinting after an error...
617 */
618
619 if (ppdIsMarked(ppd, "zeErrorReprint", "Always"))
620 printf("^JZY\n");
621 else if (ppdIsMarked(ppd, "zeErrorReprint", "Never"))
622 printf("^JZN\n");
623
624 /*
625 * Print multiple copies
626 */
627
628 if (header->NumCopies > 1)
629 printf("^PQ%d, 0, 0, N\n", header->NumCopies);
630
631 /*
632 * Display the label image...
633 */
634
635 puts("^FO0,0^XGR:CUPS.GRF,1,1^FS");
636
637 /*
638 * End the label and eject...
639 */
640
db1f069b
MS
641 puts("^IDR:CUPS.GRF^FS");
642 puts("^XZ");
ef416fc2 643
644 /*
645 * Free compression buffers...
646 */
647
648 free(CompBuffer);
649 free(LastBuffer);
650 break;
651
652 case ZEBRA_CPCL :
653 /*
654 * Set tear-off adjust position...
655 */
656
657 if (header->AdvanceDistance != 1000)
658 printf("PRESENT-AT %d 1\r\n", (int)header->AdvanceDistance);
659
660 /*
661 * Allow for reprinting after an error...
662 */
663
664 if (ppdIsMarked(ppd, "zeErrorReprint", "Always"))
665 puts("ON-OUT-OF-PAPER WAIT\r");
666 else if (ppdIsMarked(ppd, "zeErrorReprint", "Never"))
667 puts("ON-OUT-OF-PAPER PURGE\r");
668
669 /*
670 * Cut label?
671 */
672
673 if (header->CutMedia)
674 puts("CUT\r");
675
676 /*
677 * Set darkness...
678 */
679
680 if (header->cupsCompression > 0)
681 printf("TONE %u\r\n", 2 * header->cupsCompression);
682
683 /*
684 * Set print rate...
685 */
686
687 if ((choice = ppdFindMarkedChoice(ppd, "zePrintRate")) != NULL &&
688 strcmp(choice->choice, "Default"))
689 {
690 val = atoi(choice->choice);
691 printf("SPEED %d\r\n", val);
692 }
693
694 /*
695 * Print the label...
696 */
697
8ca02f3c 698 if ((choice = ppdFindMarkedChoice(ppd, "zeMediaTracking")) == NULL ||
699 strcmp(choice->choice, "Continuous"))
700 puts("FORM\r");
701
ef416fc2 702 puts("PRINT\r");
703 break;
f7deaa1a 704
705 case INTELLITECH_PCL :
706 printf("\033*rB"); /* End GFX */
707 printf("\014"); /* Eject current page */
708 break;
ef416fc2 709 }
710
711 fflush(stdout);
712
ef416fc2 713 /*
714 * Free memory...
715 */
716
717 free(Buffer);
718}
719
720
721/*
722 * 'CancelJob()' - Cancel the current job...
723 */
724
725void
726CancelJob(int sig) /* I - Signal */
727{
728 /*
729 * Tell the main loop to stop...
730 */
731
732 (void)sig;
733
734 Canceled = 1;
735}
736
737
738/*
739 * 'OutputLine()' - Output a line of graphics...
740 */
741
742void
743OutputLine(ppd_file_t *ppd, /* I - PPD file */
839a51c8 744 cups_page_header2_t *header, /* I - Page header */
ef416fc2 745 int y) /* I - Line number */
746{
747 int i; /* Looping var */
748 unsigned char *ptr; /* Pointer into buffer */
ed486911 749 unsigned char *compptr; /* Pointer into compression buffer */
ef416fc2 750 char repeat_char; /* Repeated character */
751 int repeat_count; /* Number of repeated characters */
752 static const char *hex = "0123456789ABCDEF";
753 /* Hex digits */
754
755
756 switch (ModelNumber)
757 {
758 case DYMO_3x0 :
759 /*
760 * See if the line is blank; if not, write it to the printer...
761 */
762
763 if (Buffer[0] ||
764 memcmp(Buffer, Buffer + 1, header->cupsBytesPerLine - 1))
765 {
766 if (Feed)
767 {
768 while (Feed > 255)
769 {
770 printf("\033f\001%c", 255);
771 Feed -= 255;
772 }
773
774 printf("\033f\001%c", Feed);
775 Feed = 0;
776 }
777
778 putchar(0x16);
779 fwrite(Buffer, header->cupsBytesPerLine, 1, stdout);
780 fflush(stdout);
781
782#ifdef __sgi
783 /*
784 * This hack works around a bug in the IRIX serial port driver when
785 * run at high baud rates (e.g. 115200 baud)... This results in
786 * slightly slower label printing, but at least the labels come
787 * out properly.
788 */
789
790 sginap(1);
791#endif /* __sgi */
792 }
793 else
794 Feed ++;
795 break;
796
797 case ZEBRA_EPL_LINE :
798 printf("\033g%03d", header->cupsBytesPerLine);
799 fwrite(Buffer, 1, header->cupsBytesPerLine, stdout);
800 fflush(stdout);
801 break;
802
803 case ZEBRA_EPL_PAGE :
804 if (Buffer[0] || memcmp(Buffer, Buffer + 1, header->cupsBytesPerLine))
805 {
806 printf("GW0,%d,%d,1\n", y, header->cupsBytesPerLine);
807 for (i = header->cupsBytesPerLine, ptr = Buffer; i > 0; i --, ptr ++)
808 putchar(~*ptr);
809 putchar('\n');
810 fflush(stdout);
811 }
812 break;
813
814 case ZEBRA_ZPL :
815 /*
816 * Determine if this row is the same as the previous line.
817 * If so, output a ':' and return...
818 */
819
820 if (LastSet)
821 {
822 if (!memcmp(Buffer, LastBuffer, header->cupsBytesPerLine))
823 {
824 putchar(':');
825 return;
826 }
827 }
828
829 /*
830 * Convert the line to hex digits...
831 */
832
833 for (ptr = Buffer, compptr = CompBuffer, i = header->cupsBytesPerLine;
834 i > 0;
835 i --, ptr ++)
836 {
837 *compptr++ = hex[*ptr >> 4];
838 *compptr++ = hex[*ptr & 15];
839 }
840
841 *compptr = '\0';
842
843 /*
844 * Run-length compress the graphics...
845 */
846
b86bc4cf 847 for (compptr = CompBuffer + 1, repeat_char = CompBuffer[0], repeat_count = 1;
ef416fc2 848 *compptr;
849 compptr ++)
850 if (*compptr == repeat_char)
851 repeat_count ++;
852 else
853 {
854 ZPLCompress(repeat_char, repeat_count);
855 repeat_char = *compptr;
856 repeat_count = 1;
857 }
858
859 if (repeat_char == '0')
860 {
861 /*
862 * Handle 0's on the end of the line...
863 */
864
865 if (repeat_count & 1)
b86bc4cf 866 {
867 repeat_count --;
ef416fc2 868 putchar('0');
b86bc4cf 869 }
ef416fc2 870
b86bc4cf 871 if (repeat_count > 0)
872 putchar(',');
ef416fc2 873 }
874 else
875 ZPLCompress(repeat_char, repeat_count);
876
b86bc4cf 877 fflush(stdout);
878
ef416fc2 879 /*
880 * Save this line for the next round...
881 */
882
883 memcpy(LastBuffer, Buffer, header->cupsBytesPerLine);
884 LastSet = 1;
885 break;
886
887 case ZEBRA_CPCL :
888 if (Buffer[0] || memcmp(Buffer, Buffer + 1, header->cupsBytesPerLine))
889 {
890 printf("CG %u 1 0 %d ", header->cupsBytesPerLine, y);
891 fwrite(Buffer, 1, header->cupsBytesPerLine, stdout);
892 puts("\r");
893 fflush(stdout);
894 }
895 break;
f7deaa1a 896
897 case INTELLITECH_PCL :
898 if (Buffer[0] ||
899 memcmp(Buffer, Buffer + 1, header->cupsBytesPerLine - 1))
900 {
901 if (Feed)
902 {
903 printf("\033*b%dY", Feed);
904 Feed = 0;
905 LastSet = 0;
906 }
907
908 PCLCompress(Buffer, header->cupsBytesPerLine);
909 }
910 else
911 Feed ++;
912 break;
913 }
914}
915
916
917/*
918 * 'PCLCompress()' - Output a PCL (mode 3) compressed line.
919 */
920
921void
922PCLCompress(unsigned char *line, /* I - Line to compress */
923 int length) /* I - Length of line */
924{
925 unsigned char *line_ptr, /* Current byte pointer */
926 *line_end, /* End-of-line byte pointer */
927 *comp_ptr, /* Pointer into compression buffer */
928 *start, /* Start of compression sequence */
929 *seed; /* Seed buffer pointer */
930 int count, /* Count of bytes for output */
931 offset; /* Offset of bytes for output */
932
933
934 /*
935 * Do delta-row compression...
936 */
937
938 line_ptr = line;
939 line_end = line + length;
940
941 comp_ptr = CompBuffer;
942 seed = LastBuffer;
943
944 while (line_ptr < line_end)
945 {
946 /*
947 * Find the next non-matching sequence...
948 */
949
950 start = line_ptr;
951
952 if (!LastSet)
953 {
954 /*
955 * The seed buffer is invalid, so do the next 8 bytes, max...
956 */
957
958 offset = 0;
959
960 if ((count = line_end - line_ptr) > 8)
961 count = 8;
962
963 line_ptr += count;
964 }
965 else
966 {
967 /*
968 * The seed buffer is valid, so compare against it...
969 */
970
971 while (*line_ptr == *seed &&
972 line_ptr < line_end)
973 {
974 line_ptr ++;
975 seed ++;
976 }
977
978 if (line_ptr == line_end)
979 break;
980
981 offset = line_ptr - start;
982
983 /*
984 * Find up to 8 non-matching bytes...
985 */
986
987 start = line_ptr;
988 count = 0;
989 while (*line_ptr != *seed &&
990 line_ptr < line_end &&
991 count < 8)
992 {
993 line_ptr ++;
994 seed ++;
995 count ++;
996 }
997 }
998
999 /*
1000 * Place mode 3 compression data in the buffer; see HP manuals
1001 * for details...
1002 */
1003
1004 if (offset >= 31)
1005 {
1006 /*
1007 * Output multi-byte offset...
1008 */
1009
1010 *comp_ptr++ = ((count - 1) << 5) | 31;
1011
1012 offset -= 31;
1013 while (offset >= 255)
1014 {
1015 *comp_ptr++ = 255;
1016 offset -= 255;
1017 }
1018
1019 *comp_ptr++ = offset;
1020 }
1021 else
1022 {
1023 /*
1024 * Output single-byte offset...
1025 */
1026
1027 *comp_ptr++ = ((count - 1) << 5) | offset;
1028 }
1029
1030 memcpy(comp_ptr, start, count);
1031 comp_ptr += count;
ef416fc2 1032 }
f7deaa1a 1033
f7deaa1a 1034 /*
1035 * Set the length of the data and write it...
1036 */
1037
355e94dc 1038 printf("\033*b%dW", (int)(comp_ptr - CompBuffer));
f7deaa1a 1039 fwrite(CompBuffer, comp_ptr - CompBuffer, 1, stdout);
1040
1041 /*
1042 * Save this line as a "seed" buffer for the next...
1043 */
1044
1045 memcpy(LastBuffer, line, length);
1046 LastSet = 1;
ef416fc2 1047}
1048
1049
1050/*
1051 * 'ZPLCompress()' - Output a run-length compression sequence.
1052 */
1053
1054void
1055ZPLCompress(char repeat_char, /* I - Character to repeat */
1056 int repeat_count) /* I - Number of repeated characters */
1057{
1058 if (repeat_count > 1)
1059 {
1060 /*
1061 * Print as many z's as possible - they are the largest denomination
1062 * representing 400 characters (zC stands for 400 adjacent C's)
1063 */
1064
1065 while (repeat_count >= 400)
1066 {
1067 putchar('z');
1068 repeat_count -= 400;
1069 }
1070
1071 /*
1072 * Then print 'g' through 'y' as multiples of 20 characters...
1073 */
1074
1075 if (repeat_count >= 20)
1076 {
1077 putchar('f' + repeat_count / 20);
1078 repeat_count %= 20;
1079 }
1080
1081 /*
1082 * Finally, print 'G' through 'Y' as 1 through 19 characters...
1083 */
1084
1085 if (repeat_count > 0)
1086 putchar('F' + repeat_count);
1087 }
1088
1089 /*
1090 * Then the character to be repeated...
1091 */
1092
1093 putchar(repeat_char);
1094}
1095
1096
1097/*
1098 * 'main()' - Main entry and processing of driver.
1099 */
1100
1101int /* O - Exit status */
1102main(int argc, /* I - Number of command-line arguments */
1103 char *argv[]) /* I - Command-line arguments */
1104{
1105 int fd; /* File descriptor */
1106 cups_raster_t *ras; /* Raster stream for printing */
839a51c8 1107 cups_page_header2_t header; /* Page header from file */
ef416fc2 1108 int y; /* Current line */
1109 ppd_file_t *ppd; /* PPD file */
1110 int num_options; /* Number of options */
1111 cups_option_t *options; /* Options */
839a51c8
MS
1112#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
1113 struct sigaction action; /* Actions for POSIX signals */
1114#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
ef416fc2 1115
1116
1117 /*
1118 * Make sure status messages are not buffered...
1119 */
1120
1121 setbuf(stderr, NULL);
1122
1123 /*
1124 * Check command-line...
1125 */
1126
1127 if (argc < 6 || argc > 7)
1128 {
1129 /*
1130 * We don't have the correct number of arguments; write an error message
1131 * and return.
1132 */
1133
f0ab5bff
MS
1134 _cupsLangPrintf(stderr,
1135 _("Usage: %s job-id user title copies options [file]\n"),
1136 "rastertolabel");
ef416fc2 1137 return (1);
1138 }
1139
1140 /*
1141 * Open the page stream...
1142 */
1143
1144 if (argc == 7)
1145 {
1146 if ((fd = open(argv[6], O_RDONLY)) == -1)
1147 {
f0ab5bff
MS
1148 _cupsLangPrintf(stderr, _("ERROR: Unable to open raster file - %s\n"),
1149 strerror(errno));
ef416fc2 1150 sleep(1);
1151 return (1);
1152 }
1153 }
1154 else
1155 fd = 0;
1156
1157 ras = cupsRasterOpen(fd, CUPS_RASTER_READ);
1158
839a51c8
MS
1159 /*
1160 * Register a signal handler to eject the current page if the
1161 * job is cancelled.
1162 */
1163
1164 Canceled = 0;
1165
1166#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
1167 sigset(SIGTERM, CancelJob);
1168#elif defined(HAVE_SIGACTION)
1169 memset(&action, 0, sizeof(action));
1170
1171 sigemptyset(&action.sa_mask);
1172 action.sa_handler = CancelJob;
1173 sigaction(SIGTERM, &action, NULL);
1174#else
1175 signal(SIGTERM, CancelJob);
1176#endif /* HAVE_SIGSET */
1177
ef416fc2 1178 /*
1179 * Open the PPD file and apply options...
1180 */
1181
1182 num_options = cupsParseOptions(argv[5], 0, &options);
1183
1184 if ((ppd = ppdOpenFile(getenv("PPD"))) != NULL)
1185 {
1186 ppdMarkDefaults(ppd);
1187 cupsMarkOptions(ppd, num_options, options);
1188 }
1189
1190 /*
1191 * Initialize the print device...
1192 */
1193
1194 Setup(ppd);
1195
1196 /*
1197 * Process pages as needed...
1198 */
1199
839a51c8 1200 Page = 0;
ef416fc2 1201
839a51c8 1202 while (cupsRasterReadHeader2(ras, &header))
ef416fc2 1203 {
1204 /*
1205 * Write a status message with the page number and number of copies.
1206 */
1207
839a51c8
MS
1208 if (Canceled)
1209 break;
1210
ef416fc2 1211 Page ++;
1212
1213 fprintf(stderr, "PAGE: %d 1\n", Page);
1214
1215 /*
1216 * Start the page...
1217 */
1218
1219 StartPage(ppd, &header);
1220
1221 /*
1222 * Loop for each line on the page...
1223 */
1224
1225 for (y = 0; y < header.cupsHeight && !Canceled; y ++)
1226 {
1227 /*
1228 * Let the user know how far we have progressed...
1229 */
1230
839a51c8
MS
1231 if (Canceled)
1232 break;
1233
ef416fc2 1234 if ((y & 15) == 0)
f0ab5bff
MS
1235 _cupsLangPrintf(stderr, _("INFO: Printing page %d, %d%% complete...\n"),
1236 Page, 100 * y / header.cupsHeight);
ef416fc2 1237
1238 /*
1239 * Read a line of graphics...
1240 */
1241
1242 if (cupsRasterReadPixels(ras, Buffer, header.cupsBytesPerLine) < 1)
1243 break;
1244
1245 /*
1246 * Write it to the printer...
1247 */
1248
1249 OutputLine(ppd, &header, y);
1250 }
1251
1252 /*
1253 * Eject the page...
1254 */
1255
1256 EndPage(ppd, &header);
1257
1258 if (Canceled)
1259 break;
1260 }
1261
1262 /*
1263 * Close the raster stream...
1264 */
1265
1266 cupsRasterClose(ras);
1267 if (fd != 0)
1268 close(fd);
1269
1270 /*
1271 * Close the PPD file and free the options...
1272 */
1273
1274 ppdClose(ppd);
1275 cupsFreeOptions(num_options, options);
1276
1277 /*
1278 * If no pages were printed, send an error message...
1279 */
1280
1281 if (Page == 0)
f0ab5bff 1282 {
4d301e69 1283 _cupsLangPuts(stderr, _("ERROR: No pages found\n"));
f0ab5bff
MS
1284 return (1);
1285 }
ef416fc2 1286 else
f0ab5bff
MS
1287 {
1288 _cupsLangPuts(stderr, _("INFO: Ready to print.\n"));
1289 return (0);
1290 }
ef416fc2 1291
1292 return (Page == 0);
1293}
1294
1295
1296/*
b19ccc9e 1297 * End of "$Id: rastertolabel.c 7720 2008-07-11 22:46:21Z mike $".
ef416fc2 1298 */