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