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