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