]> git.ipfire.org Git - thirdparty/cups.git/blame - filter/rastertolabel.c
Merge changes from CUPS 1.4svn-r7961.
[thirdparty/cups.git] / filter / rastertolabel.c
CommitLineData
ef416fc2 1/*
b19ccc9e 2 * "$Id: rastertolabel.c 7720 2008-07-11 22:46:21Z mike $"
ef416fc2 3 *
4 * Label printer filter for the Common UNIX Printing System (CUPS).
5 *
839a51c8 6 * Copyright 2007-2008 by Apple Inc.
b86bc4cf 7 * Copyright 2001-2007 by Easy Software Products.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
19 * Setup() - Prepare the printer for printing.
20 * StartPage() - Start a page of graphics.
21 * EndPage() - Finish a page of graphics.
22 * CancelJob() - Cancel the current job...
23 * OutputLine() - Output a line of graphics.
f7deaa1a 24 * PCLCompress() - Output a PCL (mode 3) compressed line.
ef416fc2 25 * ZPLCompress() - Output a run-length compression sequence.
26 * main() - Main entry and processing of driver.
27 */
28
29/*
30 * Include necessary headers...
31 */
32
33#include <cups/cups.h>
34#include <cups/string.h>
c0e1af83 35#include <cups/i18n.h>
ac884b6a 36#include <cups/raster.h>
ef416fc2 37#include <stdlib.h>
38#include <unistd.h>
39#include <fcntl.h>
40#include <signal.h>
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);
780
781#ifdef __sgi
782 /*
783 * This hack works around a bug in the IRIX serial port driver when
784 * run at high baud rates (e.g. 115200 baud)... This results in
785 * slightly slower label printing, but at least the labels come
786 * out properly.
787 */
788
789 sginap(1);
790#endif /* __sgi */
791 }
792 else
793 Feed ++;
794 break;
795
796 case ZEBRA_EPL_LINE :
797 printf("\033g%03d", header->cupsBytesPerLine);
798 fwrite(Buffer, 1, header->cupsBytesPerLine, stdout);
799 fflush(stdout);
800 break;
801
802 case ZEBRA_EPL_PAGE :
803 if (Buffer[0] || memcmp(Buffer, Buffer + 1, header->cupsBytesPerLine))
804 {
805 printf("GW0,%d,%d,1\n", y, header->cupsBytesPerLine);
806 for (i = header->cupsBytesPerLine, ptr = Buffer; i > 0; i --, ptr ++)
807 putchar(~*ptr);
808 putchar('\n');
809 fflush(stdout);
810 }
811 break;
812
813 case ZEBRA_ZPL :
814 /*
815 * Determine if this row is the same as the previous line.
816 * If so, output a ':' and return...
817 */
818
819 if (LastSet)
820 {
821 if (!memcmp(Buffer, LastBuffer, header->cupsBytesPerLine))
822 {
823 putchar(':');
824 return;
825 }
826 }
827
828 /*
829 * Convert the line to hex digits...
830 */
831
832 for (ptr = Buffer, compptr = CompBuffer, i = header->cupsBytesPerLine;
833 i > 0;
834 i --, ptr ++)
835 {
836 *compptr++ = hex[*ptr >> 4];
837 *compptr++ = hex[*ptr & 15];
838 }
839
840 *compptr = '\0';
841
842 /*
843 * Run-length compress the graphics...
844 */
845
b86bc4cf 846 for (compptr = CompBuffer + 1, repeat_char = CompBuffer[0], repeat_count = 1;
ef416fc2 847 *compptr;
848 compptr ++)
849 if (*compptr == repeat_char)
850 repeat_count ++;
851 else
852 {
853 ZPLCompress(repeat_char, repeat_count);
854 repeat_char = *compptr;
855 repeat_count = 1;
856 }
857
858 if (repeat_char == '0')
859 {
860 /*
861 * Handle 0's on the end of the line...
862 */
863
864 if (repeat_count & 1)
b86bc4cf 865 {
866 repeat_count --;
ef416fc2 867 putchar('0');
b86bc4cf 868 }
ef416fc2 869
b86bc4cf 870 if (repeat_count > 0)
871 putchar(',');
ef416fc2 872 }
873 else
874 ZPLCompress(repeat_char, repeat_count);
875
b86bc4cf 876 fflush(stdout);
877
ef416fc2 878 /*
879 * Save this line for the next round...
880 */
881
882 memcpy(LastBuffer, Buffer, header->cupsBytesPerLine);
883 LastSet = 1;
884 break;
885
886 case ZEBRA_CPCL :
887 if (Buffer[0] || memcmp(Buffer, Buffer + 1, header->cupsBytesPerLine))
888 {
889 printf("CG %u 1 0 %d ", header->cupsBytesPerLine, y);
890 fwrite(Buffer, 1, header->cupsBytesPerLine, stdout);
891 puts("\r");
892 fflush(stdout);
893 }
894 break;
f7deaa1a 895
896 case INTELLITECH_PCL :
897 if (Buffer[0] ||
898 memcmp(Buffer, Buffer + 1, header->cupsBytesPerLine - 1))
899 {
900 if (Feed)
901 {
902 printf("\033*b%dY", Feed);
903 Feed = 0;
904 LastSet = 0;
905 }
906
907 PCLCompress(Buffer, header->cupsBytesPerLine);
908 }
909 else
910 Feed ++;
911 break;
912 }
913}
914
915
916/*
917 * 'PCLCompress()' - Output a PCL (mode 3) compressed line.
918 */
919
920void
921PCLCompress(unsigned char *line, /* I - Line to compress */
922 int length) /* I - Length of line */
923{
924 unsigned char *line_ptr, /* Current byte pointer */
925 *line_end, /* End-of-line byte pointer */
926 *comp_ptr, /* Pointer into compression buffer */
927 *start, /* Start of compression sequence */
928 *seed; /* Seed buffer pointer */
929 int count, /* Count of bytes for output */
930 offset; /* Offset of bytes for output */
931
932
933 /*
934 * Do delta-row compression...
935 */
936
937 line_ptr = line;
938 line_end = line + length;
939
940 comp_ptr = CompBuffer;
941 seed = LastBuffer;
942
943 while (line_ptr < line_end)
944 {
945 /*
946 * Find the next non-matching sequence...
947 */
948
949 start = line_ptr;
950
951 if (!LastSet)
952 {
953 /*
954 * The seed buffer is invalid, so do the next 8 bytes, max...
955 */
956
957 offset = 0;
958
959 if ((count = line_end - line_ptr) > 8)
960 count = 8;
961
962 line_ptr += count;
963 }
964 else
965 {
966 /*
967 * The seed buffer is valid, so compare against it...
968 */
969
970 while (*line_ptr == *seed &&
971 line_ptr < line_end)
972 {
973 line_ptr ++;
974 seed ++;
975 }
976
977 if (line_ptr == line_end)
978 break;
979
980 offset = line_ptr - start;
981
982 /*
983 * Find up to 8 non-matching bytes...
984 */
985
986 start = line_ptr;
987 count = 0;
988 while (*line_ptr != *seed &&
989 line_ptr < line_end &&
990 count < 8)
991 {
992 line_ptr ++;
993 seed ++;
994 count ++;
995 }
996 }
997
998 /*
999 * Place mode 3 compression data in the buffer; see HP manuals
1000 * for details...
1001 */
1002
1003 if (offset >= 31)
1004 {
1005 /*
1006 * Output multi-byte offset...
1007 */
1008
1009 *comp_ptr++ = ((count - 1) << 5) | 31;
1010
1011 offset -= 31;
1012 while (offset >= 255)
1013 {
1014 *comp_ptr++ = 255;
1015 offset -= 255;
1016 }
1017
1018 *comp_ptr++ = offset;
1019 }
1020 else
1021 {
1022 /*
1023 * Output single-byte offset...
1024 */
1025
1026 *comp_ptr++ = ((count - 1) << 5) | offset;
1027 }
1028
1029 memcpy(comp_ptr, start, count);
1030 comp_ptr += count;
ef416fc2 1031 }
f7deaa1a 1032
f7deaa1a 1033 /*
1034 * Set the length of the data and write it...
1035 */
1036
355e94dc 1037 printf("\033*b%dW", (int)(comp_ptr - CompBuffer));
f7deaa1a 1038 fwrite(CompBuffer, comp_ptr - CompBuffer, 1, stdout);
1039
1040 /*
1041 * Save this line as a "seed" buffer for the next...
1042 */
1043
1044 memcpy(LastBuffer, line, length);
1045 LastSet = 1;
ef416fc2 1046}
1047
1048
1049/*
1050 * 'ZPLCompress()' - Output a run-length compression sequence.
1051 */
1052
1053void
1054ZPLCompress(char repeat_char, /* I - Character to repeat */
1055 int repeat_count) /* I - Number of repeated characters */
1056{
1057 if (repeat_count > 1)
1058 {
1059 /*
1060 * Print as many z's as possible - they are the largest denomination
1061 * representing 400 characters (zC stands for 400 adjacent C's)
1062 */
1063
1064 while (repeat_count >= 400)
1065 {
1066 putchar('z');
1067 repeat_count -= 400;
1068 }
1069
1070 /*
1071 * Then print 'g' through 'y' as multiples of 20 characters...
1072 */
1073
1074 if (repeat_count >= 20)
1075 {
1076 putchar('f' + repeat_count / 20);
1077 repeat_count %= 20;
1078 }
1079
1080 /*
1081 * Finally, print 'G' through 'Y' as 1 through 19 characters...
1082 */
1083
1084 if (repeat_count > 0)
1085 putchar('F' + repeat_count);
1086 }
1087
1088 /*
1089 * Then the character to be repeated...
1090 */
1091
1092 putchar(repeat_char);
1093}
1094
1095
1096/*
1097 * 'main()' - Main entry and processing of driver.
1098 */
1099
1100int /* O - Exit status */
1101main(int argc, /* I - Number of command-line arguments */
1102 char *argv[]) /* I - Command-line arguments */
1103{
1104 int fd; /* File descriptor */
1105 cups_raster_t *ras; /* Raster stream for printing */
839a51c8 1106 cups_page_header2_t header; /* Page header from file */
ef416fc2 1107 int y; /* Current line */
1108 ppd_file_t *ppd; /* PPD file */
1109 int num_options; /* Number of options */
1110 cups_option_t *options; /* Options */
839a51c8
MS
1111#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
1112 struct sigaction action; /* Actions for POSIX signals */
1113#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
ef416fc2 1114
1115
1116 /*
1117 * Make sure status messages are not buffered...
1118 */
1119
1120 setbuf(stderr, NULL);
1121
1122 /*
1123 * Check command-line...
1124 */
1125
1126 if (argc < 6 || argc > 7)
1127 {
1128 /*
1129 * We don't have the correct number of arguments; write an error message
1130 * and return.
1131 */
1132
c0e1af83 1133 fprintf(stderr, _("Usage: %s job-id user title copies options [file]\n"),
1134 argv[0]);
ef416fc2 1135 return (1);
1136 }
1137
1138 /*
1139 * Open the page stream...
1140 */
1141
1142 if (argc == 7)
1143 {
1144 if ((fd = open(argv[6], O_RDONLY)) == -1)
1145 {
1146 perror("ERROR: Unable to open raster file - ");
1147 sleep(1);
1148 return (1);
1149 }
1150 }
1151 else
1152 fd = 0;
1153
1154 ras = cupsRasterOpen(fd, CUPS_RASTER_READ);
1155
839a51c8
MS
1156 /*
1157 * Register a signal handler to eject the current page if the
1158 * job is cancelled.
1159 */
1160
1161 Canceled = 0;
1162
1163#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
1164 sigset(SIGTERM, CancelJob);
1165#elif defined(HAVE_SIGACTION)
1166 memset(&action, 0, sizeof(action));
1167
1168 sigemptyset(&action.sa_mask);
1169 action.sa_handler = CancelJob;
1170 sigaction(SIGTERM, &action, NULL);
1171#else
1172 signal(SIGTERM, CancelJob);
1173#endif /* HAVE_SIGSET */
1174
ef416fc2 1175 /*
1176 * Open the PPD file and apply options...
1177 */
1178
1179 num_options = cupsParseOptions(argv[5], 0, &options);
1180
1181 if ((ppd = ppdOpenFile(getenv("PPD"))) != NULL)
1182 {
1183 ppdMarkDefaults(ppd);
1184 cupsMarkOptions(ppd, num_options, options);
1185 }
1186
1187 /*
1188 * Initialize the print device...
1189 */
1190
1191 Setup(ppd);
1192
1193 /*
1194 * Process pages as needed...
1195 */
1196
839a51c8 1197 Page = 0;
ef416fc2 1198
839a51c8 1199 while (cupsRasterReadHeader2(ras, &header))
ef416fc2 1200 {
1201 /*
1202 * Write a status message with the page number and number of copies.
1203 */
1204
839a51c8
MS
1205 if (Canceled)
1206 break;
1207
ef416fc2 1208 Page ++;
1209
1210 fprintf(stderr, "PAGE: %d 1\n", Page);
1211
1212 /*
1213 * Start the page...
1214 */
1215
1216 StartPage(ppd, &header);
1217
1218 /*
1219 * Loop for each line on the page...
1220 */
1221
1222 for (y = 0; y < header.cupsHeight && !Canceled; y ++)
1223 {
1224 /*
1225 * Let the user know how far we have progressed...
1226 */
1227
839a51c8
MS
1228 if (Canceled)
1229 break;
1230
ef416fc2 1231 if ((y & 15) == 0)
c0e1af83 1232 fprintf(stderr, _("INFO: Printing page %d, %d%% complete...\n"), Page,
ef416fc2 1233 100 * y / header.cupsHeight);
1234
1235 /*
1236 * Read a line of graphics...
1237 */
1238
1239 if (cupsRasterReadPixels(ras, Buffer, header.cupsBytesPerLine) < 1)
1240 break;
1241
1242 /*
1243 * Write it to the printer...
1244 */
1245
1246 OutputLine(ppd, &header, y);
1247 }
1248
1249 /*
1250 * Eject the page...
1251 */
1252
1253 EndPage(ppd, &header);
1254
1255 if (Canceled)
1256 break;
1257 }
1258
1259 /*
1260 * Close the raster stream...
1261 */
1262
1263 cupsRasterClose(ras);
1264 if (fd != 0)
1265 close(fd);
1266
1267 /*
1268 * Close the PPD file and free the options...
1269 */
1270
1271 ppdClose(ppd);
1272 cupsFreeOptions(num_options, options);
1273
1274 /*
1275 * If no pages were printed, send an error message...
1276 */
1277
1278 if (Page == 0)
c0e1af83 1279 fputs(_("ERROR: No pages found!\n"), stderr);
ef416fc2 1280 else
c0e1af83 1281 fputs(_("INFO: Ready to print.\n"), stderr);
ef416fc2 1282
1283 return (Page == 0);
1284}
1285
1286
1287/*
b19ccc9e 1288 * End of "$Id: rastertolabel.c 7720 2008-07-11 22:46:21Z mike $".
ef416fc2 1289 */