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