]> git.ipfire.org Git - thirdparty/cups.git/blame - filter/rastertolabel.c
Load cups into easysw/current.
[thirdparty/cups.git] / filter / rastertolabel.c
CommitLineData
ef416fc2 1/*
b86bc4cf 2 * "$Id: rastertolabel.c 6236 2007-02-05 21:04:04Z mike $"
ef416fc2 3 *
4 * Label printer filter for the Common UNIX Printing System (CUPS).
5 *
b86bc4cf 6 * Copyright 2001-2007 by Easy Software Products.
ef416fc2 7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * This file is subject to the Apple OS-Developed Software exception.
25 *
26 * Contents:
27 *
28 * Setup() - Prepare the printer for printing.
29 * StartPage() - Start a page of graphics.
30 * EndPage() - Finish a page of graphics.
31 * CancelJob() - Cancel the current job...
32 * OutputLine() - Output a line of graphics.
33 * ZPLCompress() - Output a run-length compression sequence.
34 * main() - Main entry and processing of driver.
35 */
36
37/*
38 * Include necessary headers...
39 */
40
41#include <cups/cups.h>
42#include <cups/string.h>
43#include "raster.h"
44#include <stdlib.h>
45#include <unistd.h>
46#include <fcntl.h>
47#include <signal.h>
48
49
50/*
f7faf1f5 51 * This driver filter currently supports Dymo and Zebra label printers.
ef416fc2 52 *
53 * The Dymo portion of the driver has been tested with the 300, 330,
f7faf1f5 54 * and 330 Turbo label printers; it may also work with older models.
ef416fc2 55 * The Dymo printers support printing at 136, 203, and 300 DPI.
56 *
f7faf1f5 57 * The Zebra portion of the driver has been tested with the LP-2844Z label
58 * printer; it may also work with other models. The driver supports EPL
59 * line mode, EPL page mode, ZPL, and CPCL as defined in Zebra's on-line
60 * developer documentation.
ef416fc2 61 */
62
63/*
64 * Model number constants...
65 */
66
67#define DYMO_3x0 0 /* Dymo Labelwriter 300/330/330 Turbo */
68
69#define ZEBRA_EPL_LINE 0x10 /* Zebra EPL line mode printers */
70#define ZEBRA_EPL_PAGE 0x11 /* Zebra EPL page mode printers */
71#define ZEBRA_ZPL 0x12 /* Zebra ZPL-based printers */
72#define ZEBRA_CPCL 0x13 /* Zebra CPCL-based printers */
73
74
75/*
76 * Globals...
77 */
78
79unsigned char *Buffer; /* Output buffer */
b86bc4cf 80unsigned char *CompBuffer; /* Compression buffer */
ef416fc2 81unsigned char *LastBuffer; /* Last buffer */
82int LastSet; /* Number of repeat characters */
83int ModelNumber, /* cupsModelNumber attribute */
84 Page, /* Current page */
85 Feed, /* Number of lines to skip */
86 Canceled; /* Non-zero if job is canceled */
87
88
89/*
90 * Prototypes...
91 */
92
93void Setup(ppd_file_t *ppd);
94void StartPage(ppd_file_t *ppd, cups_page_header_t *header);
95void EndPage(ppd_file_t *ppd, cups_page_header_t *header);
96void CancelJob(int sig);
97void OutputLine(ppd_file_t *ppd, cups_page_header_t *header, int y);
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;
150 }
151}
152
153
154/*
155 * 'StartPage()' - Start a page of graphics.
156 */
157
158void
e1d6a774 159StartPage(ppd_file_t *ppd, /* I - PPD file */
ef416fc2 160 cups_page_header_t *header) /* I - Page header */
161{
162 ppd_choice_t *choice; /* Marked choice */
163 int length; /* Actual label length */
164#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
165 struct sigaction action; /* Actions for POSIX signals */
166#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
167
168
e1d6a774 169 /*
170 * Show page device dictionary...
171 */
172
173 fprintf(stderr, "DEBUG: StartPage...\n");
174 fprintf(stderr, "DEBUG: MediaClass = \"%s\"\n", header->MediaClass);
175 fprintf(stderr, "DEBUG: MediaColor = \"%s\"\n", header->MediaColor);
176 fprintf(stderr, "DEBUG: MediaType = \"%s\"\n", header->MediaType);
177 fprintf(stderr, "DEBUG: OutputType = \"%s\"\n", header->OutputType);
178
179 fprintf(stderr, "DEBUG: AdvanceDistance = %d\n", header->AdvanceDistance);
180 fprintf(stderr, "DEBUG: AdvanceMedia = %d\n", header->AdvanceMedia);
181 fprintf(stderr, "DEBUG: Collate = %d\n", header->Collate);
182 fprintf(stderr, "DEBUG: CutMedia = %d\n", header->CutMedia);
183 fprintf(stderr, "DEBUG: Duplex = %d\n", header->Duplex);
184 fprintf(stderr, "DEBUG: HWResolution = [ %d %d ]\n", header->HWResolution[0],
185 header->HWResolution[1]);
186 fprintf(stderr, "DEBUG: ImagingBoundingBox = [ %d %d %d %d ]\n",
187 header->ImagingBoundingBox[0], header->ImagingBoundingBox[1],
188 header->ImagingBoundingBox[2], header->ImagingBoundingBox[3]);
189 fprintf(stderr, "DEBUG: InsertSheet = %d\n", header->InsertSheet);
190 fprintf(stderr, "DEBUG: Jog = %d\n", header->Jog);
191 fprintf(stderr, "DEBUG: LeadingEdge = %d\n", header->LeadingEdge);
192 fprintf(stderr, "DEBUG: Margins = [ %d %d ]\n", header->Margins[0],
193 header->Margins[1]);
194 fprintf(stderr, "DEBUG: ManualFeed = %d\n", header->ManualFeed);
195 fprintf(stderr, "DEBUG: MediaPosition = %d\n", header->MediaPosition);
196 fprintf(stderr, "DEBUG: MediaWeight = %d\n", header->MediaWeight);
197 fprintf(stderr, "DEBUG: MirrorPrint = %d\n", header->MirrorPrint);
198 fprintf(stderr, "DEBUG: NegativePrint = %d\n", header->NegativePrint);
199 fprintf(stderr, "DEBUG: NumCopies = %d\n", header->NumCopies);
200 fprintf(stderr, "DEBUG: Orientation = %d\n", header->Orientation);
201 fprintf(stderr, "DEBUG: OutputFaceUp = %d\n", header->OutputFaceUp);
202 fprintf(stderr, "DEBUG: PageSize = [ %d %d ]\n", header->PageSize[0],
203 header->PageSize[1]);
204 fprintf(stderr, "DEBUG: Separations = %d\n", header->Separations);
205 fprintf(stderr, "DEBUG: TraySwitch = %d\n", header->TraySwitch);
206 fprintf(stderr, "DEBUG: Tumble = %d\n", header->Tumble);
207 fprintf(stderr, "DEBUG: cupsWidth = %d\n", header->cupsWidth);
208 fprintf(stderr, "DEBUG: cupsHeight = %d\n", header->cupsHeight);
209 fprintf(stderr, "DEBUG: cupsMediaType = %d\n", header->cupsMediaType);
210 fprintf(stderr, "DEBUG: cupsBitsPerColor = %d\n", header->cupsBitsPerColor);
211 fprintf(stderr, "DEBUG: cupsBitsPerPixel = %d\n", header->cupsBitsPerPixel);
212 fprintf(stderr, "DEBUG: cupsBytesPerLine = %d\n", header->cupsBytesPerLine);
213 fprintf(stderr, "DEBUG: cupsColorOrder = %d\n", header->cupsColorOrder);
214 fprintf(stderr, "DEBUG: cupsColorSpace = %d\n", header->cupsColorSpace);
215 fprintf(stderr, "DEBUG: cupsCompression = %d\n", header->cupsCompression);
216
ef416fc2 217 /*
218 * Register a signal handler to eject the current page if the
219 * job is canceled.
220 */
221
222#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
223 sigset(SIGTERM, CancelJob);
224#elif defined(HAVE_SIGACTION)
225 memset(&action, 0, sizeof(action));
226
227 sigemptyset(&action.sa_mask);
228 action.sa_handler = CancelJob;
229 sigaction(SIGTERM, &action, NULL);
230#else
231 signal(SIGTERM, CancelJob);
232#endif /* HAVE_SIGSET */
233
234 switch (ModelNumber)
235 {
236 case DYMO_3x0 :
237 /*
238 * Setup printer/job attributes...
239 */
240
241 length = header->PageSize[1] * header->HWResolution[1] / 72;
242
243 printf("\033L%c%c", length >> 8, length);
244 printf("\033D%c", header->cupsBytesPerLine);
245
246 printf("\033%c", header->cupsCompression + 'c'); /* Darkness */
247 break;
248
249 case ZEBRA_EPL_LINE :
250 /*
251 * Set print rate...
252 */
253
254 if ((choice = ppdFindMarkedChoice(ppd, "zePrintRate")) != NULL &&
255 strcmp(choice->choice, "Default"))
256 printf("\033S%.0f", atof(choice->choice) * 2.0 - 2.0);
257
258 /*
259 * Set darkness...
260 */
261
262 if (header->cupsCompression > 0 && header->cupsCompression <= 100)
263 printf("\033D%d", 7 * header->cupsCompression / 100);
264
265 /*
266 * Set left margin to 0...
267 */
268
269 fputs("\033M01", stdout);
270
271 /*
272 * Start buffered output...
273 */
274
275 fputs("\033B", stdout);
276 break;
277
278 case ZEBRA_EPL_PAGE :
279 /*
280 * Start a new label...
281 */
282
283 puts("");
284 puts("N");
285
286 /*
287 * Set hardware options...
288 */
289
290 if (!strcmp(header->MediaType, "Direct"))
291 puts("OD");
292
293 /*
294 * Set print rate...
295 */
296
297 if ((choice = ppdFindMarkedChoice(ppd, "zePrintRate")) != NULL &&
298 strcmp(choice->choice, "Default"))
299 {
300 float val = atof(choice->choice);
301
302 if (val >= 3.0)
303 printf("S%.0f\n", val);
304 else
305 printf("S%.0f\n", val * 2.0 - 2.0);
306 }
307
308 /*
309 * Set darkness...
310 */
311
312 if (header->cupsCompression > 0 && header->cupsCompression <= 100)
313 printf("D%d\n", 15 * header->cupsCompression / 100);
314
315 /*
316 * Set label size...
317 */
318
319 printf("q%d\n", header->cupsWidth);
320 break;
321
322 case ZEBRA_ZPL :
323 /*
324 * Set darkness...
325 */
326
327 if (header->cupsCompression > 0 && header->cupsCompression <= 100)
328 printf("~SD%02d\n", 30 * header->cupsCompression / 100);
329
330 /*
331 * Start bitmap graphics...
332 */
333
334 printf("~DGR:CUPS.GRF,%d,%d,\n",
335 header->cupsHeight * header->cupsBytesPerLine,
336 header->cupsBytesPerLine);
337
338 /*
339 * Allocate compression buffers...
340 */
341
342 CompBuffer = malloc(2 * header->cupsBytesPerLine + 1);
343 LastBuffer = malloc(header->cupsBytesPerLine);
344 LastSet = 0;
345 break;
346
347 case ZEBRA_CPCL :
348 /*
349 * Start label...
350 */
351
352 printf("! 0 %u %u %u %u\r\n", header->HWResolution[0],
353 header->HWResolution[1], header->cupsHeight,
354 header->NumCopies);
8ca02f3c 355 printf("PAGE-WIDTH %d\r\n", header->cupsWidth);
356 printf("PAGE-HEIGHT %d\r\n", header->cupsWidth);
ef416fc2 357 break;
358 }
359
360 /*
361 * Allocate memory for a line of graphics...
362 */
363
364 Buffer = malloc(header->cupsBytesPerLine);
365 Feed = 0;
366}
367
368
369/*
370 * 'EndPage()' - Finish a page of graphics.
371 */
372
373void
374EndPage(ppd_file_t *ppd, /* I - PPD file */
375 cups_page_header_t *header) /* I - Page header */
376{
377 int val; /* Option value */
378 ppd_choice_t *choice; /* Marked choice */
379#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
380 struct sigaction action; /* Actions for POSIX signals */
381#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
382
383
384 switch (ModelNumber)
385 {
386 case DYMO_3x0 :
387 /*
388 * Eject the current page...
389 */
390
391 fputs("\033E", stdout);
392 break;
393
394 case ZEBRA_EPL_LINE :
395 /*
396 * End buffered output, eject the label...
397 */
398
399 fputs("\033E\014", stdout);
400 break;
401
402 case ZEBRA_EPL_PAGE :
403 /*
404 * Print the label...
405 */
406
407 puts("P1");
408 break;
409
410 case ZEBRA_ZPL :
411 if (Canceled)
412 {
413 /*
414 * Cancel bitmap download...
415 */
416
417 puts("~DN");
418 break;
419 }
420
421 /*
422 * Start label...
423 */
424
425 puts("^XA");
426
427 /*
428 * Set print rate...
429 */
430
431 if ((choice = ppdFindMarkedChoice(ppd, "zePrintRate")) != NULL &&
432 strcmp(choice->choice, "Default"))
433 {
434 val = atoi(choice->choice);
435 printf("^PR%d,%d,%d\n", val, val, val);
436 }
437
438 /*
439 * Put label home in default position (0,0)...
440 */
441
442 printf("^LH0,0\n");
443
444 /*
445 * Set media tracking...
446 */
447
448 if (ppdIsMarked(ppd, "zeMediaTracking", "Continuous"))
449 {
450 /*
451 * Add label length command for continuous...
452 */
453
454 printf("^LL%d\n", header->cupsHeight);
455 printf("^MNN\n");
456 }
457 else if (ppdIsMarked(ppd, "zeMediaTracking", "Web"))
458 printf("^MNY\n");
459 else if (ppdIsMarked(ppd, "zeMediaTracking", "Mark"))
460 printf("^MNM\n");
461
462 /*
463 * Set label top
464 */
465
466 if (header->cupsRowStep != 200)
467 printf("^LT%u\n", header->cupsRowStep);
468
469 /*
470 * Set media type...
471 */
472
473 if (!strcmp(header->MediaType, "Thermal"))
474 printf("^MTT\n");
475 else if (!strcmp(header->MediaType, "Direct"))
476 printf("^MTD\n");
477
478 /*
479 * Set print mode...
480 */
481
482 if ((choice = ppdFindMarkedChoice(ppd, "zePrintMode")) != NULL &&
483 strcmp(choice->choice, "Saved"))
484 {
485 printf("^MM");
486
487 if (!strcmp(choice->choice, "Tear"))
488 printf("T,Y\n");
489 else if (!strcmp(choice->choice, "Peel"))
490 printf("P,Y\n");
491 else if (!strcmp(choice->choice, "Rewind"))
492 printf("R,Y\n");
493 else if (!strcmp(choice->choice, "Applicator"))
494 printf("A,Y\n");
495 else
496 printf("C,Y\n");
497 }
498
499 /*
500 * Set tear-off adjust position...
501 */
502
503 if (header->AdvanceDistance != 1000)
504 {
505 if ((int)header->AdvanceDistance < 0)
506 printf("~TA%04d\n", (int)header->AdvanceDistance);
507 else
508 printf("~TA%03d\n", (int)header->AdvanceDistance);
509 }
510
511 /*
512 * Allow for reprinting after an error...
513 */
514
515 if (ppdIsMarked(ppd, "zeErrorReprint", "Always"))
516 printf("^JZY\n");
517 else if (ppdIsMarked(ppd, "zeErrorReprint", "Never"))
518 printf("^JZN\n");
519
520 /*
521 * Print multiple copies
522 */
523
524 if (header->NumCopies > 1)
525 printf("^PQ%d, 0, 0, N\n", header->NumCopies);
526
527 /*
528 * Display the label image...
529 */
530
531 puts("^FO0,0^XGR:CUPS.GRF,1,1^FS");
532
533 /*
534 * End the label and eject...
535 */
536
537 puts("^XZ");
538
539 /*
540 * Free compression buffers...
541 */
542
543 free(CompBuffer);
544 free(LastBuffer);
545 break;
546
547 case ZEBRA_CPCL :
548 /*
549 * Set tear-off adjust position...
550 */
551
552 if (header->AdvanceDistance != 1000)
553 printf("PRESENT-AT %d 1\r\n", (int)header->AdvanceDistance);
554
555 /*
556 * Allow for reprinting after an error...
557 */
558
559 if (ppdIsMarked(ppd, "zeErrorReprint", "Always"))
560 puts("ON-OUT-OF-PAPER WAIT\r");
561 else if (ppdIsMarked(ppd, "zeErrorReprint", "Never"))
562 puts("ON-OUT-OF-PAPER PURGE\r");
563
564 /*
565 * Cut label?
566 */
567
568 if (header->CutMedia)
569 puts("CUT\r");
570
571 /*
572 * Set darkness...
573 */
574
575 if (header->cupsCompression > 0)
576 printf("TONE %u\r\n", 2 * header->cupsCompression);
577
578 /*
579 * Set print rate...
580 */
581
582 if ((choice = ppdFindMarkedChoice(ppd, "zePrintRate")) != NULL &&
583 strcmp(choice->choice, "Default"))
584 {
585 val = atoi(choice->choice);
586 printf("SPEED %d\r\n", val);
587 }
588
589 /*
590 * Print the label...
591 */
592
8ca02f3c 593 if ((choice = ppdFindMarkedChoice(ppd, "zeMediaTracking")) == NULL ||
594 strcmp(choice->choice, "Continuous"))
595 puts("FORM\r");
596
ef416fc2 597 puts("PRINT\r");
598 break;
599 }
600
601 fflush(stdout);
602
603 /*
604 * Unregister the signal handler...
605 */
606
607#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
608 sigset(SIGTERM, SIG_IGN);
609#elif defined(HAVE_SIGACTION)
610 memset(&action, 0, sizeof(action));
611
612 sigemptyset(&action.sa_mask);
613 action.sa_handler = SIG_IGN;
614 sigaction(SIGTERM, &action, NULL);
615#else
616 signal(SIGTERM, SIG_IGN);
617#endif /* HAVE_SIGSET */
618
619 /*
620 * Free memory...
621 */
622
623 free(Buffer);
624}
625
626
627/*
628 * 'CancelJob()' - Cancel the current job...
629 */
630
631void
632CancelJob(int sig) /* I - Signal */
633{
634 /*
635 * Tell the main loop to stop...
636 */
637
638 (void)sig;
639
640 Canceled = 1;
641}
642
643
644/*
645 * 'OutputLine()' - Output a line of graphics...
646 */
647
648void
649OutputLine(ppd_file_t *ppd, /* I - PPD file */
650 cups_page_header_t *header, /* I - Page header */
651 int y) /* I - Line number */
652{
653 int i; /* Looping var */
654 unsigned char *ptr; /* Pointer into buffer */
ed486911 655 unsigned char *compptr; /* Pointer into compression buffer */
ef416fc2 656 char repeat_char; /* Repeated character */
657 int repeat_count; /* Number of repeated characters */
658 static const char *hex = "0123456789ABCDEF";
659 /* Hex digits */
660
661
662 switch (ModelNumber)
663 {
664 case DYMO_3x0 :
665 /*
666 * See if the line is blank; if not, write it to the printer...
667 */
668
669 if (Buffer[0] ||
670 memcmp(Buffer, Buffer + 1, header->cupsBytesPerLine - 1))
671 {
672 if (Feed)
673 {
674 while (Feed > 255)
675 {
676 printf("\033f\001%c", 255);
677 Feed -= 255;
678 }
679
680 printf("\033f\001%c", Feed);
681 Feed = 0;
682 }
683
684 putchar(0x16);
685 fwrite(Buffer, header->cupsBytesPerLine, 1, stdout);
686 fflush(stdout);
687
688#ifdef __sgi
689 /*
690 * This hack works around a bug in the IRIX serial port driver when
691 * run at high baud rates (e.g. 115200 baud)... This results in
692 * slightly slower label printing, but at least the labels come
693 * out properly.
694 */
695
696 sginap(1);
697#endif /* __sgi */
698 }
699 else
700 Feed ++;
701 break;
702
703 case ZEBRA_EPL_LINE :
704 printf("\033g%03d", header->cupsBytesPerLine);
705 fwrite(Buffer, 1, header->cupsBytesPerLine, stdout);
706 fflush(stdout);
707 break;
708
709 case ZEBRA_EPL_PAGE :
710 if (Buffer[0] || memcmp(Buffer, Buffer + 1, header->cupsBytesPerLine))
711 {
712 printf("GW0,%d,%d,1\n", y, header->cupsBytesPerLine);
713 for (i = header->cupsBytesPerLine, ptr = Buffer; i > 0; i --, ptr ++)
714 putchar(~*ptr);
715 putchar('\n');
716 fflush(stdout);
717 }
718 break;
719
720 case ZEBRA_ZPL :
721 /*
722 * Determine if this row is the same as the previous line.
723 * If so, output a ':' and return...
724 */
725
726 if (LastSet)
727 {
728 if (!memcmp(Buffer, LastBuffer, header->cupsBytesPerLine))
729 {
730 putchar(':');
731 return;
732 }
733 }
734
735 /*
736 * Convert the line to hex digits...
737 */
738
739 for (ptr = Buffer, compptr = CompBuffer, i = header->cupsBytesPerLine;
740 i > 0;
741 i --, ptr ++)
742 {
743 *compptr++ = hex[*ptr >> 4];
744 *compptr++ = hex[*ptr & 15];
745 }
746
747 *compptr = '\0';
748
749 /*
750 * Run-length compress the graphics...
751 */
752
b86bc4cf 753 for (compptr = CompBuffer + 1, repeat_char = CompBuffer[0], repeat_count = 1;
ef416fc2 754 *compptr;
755 compptr ++)
756 if (*compptr == repeat_char)
757 repeat_count ++;
758 else
759 {
760 ZPLCompress(repeat_char, repeat_count);
761 repeat_char = *compptr;
762 repeat_count = 1;
763 }
764
765 if (repeat_char == '0')
766 {
767 /*
768 * Handle 0's on the end of the line...
769 */
770
771 if (repeat_count & 1)
b86bc4cf 772 {
773 repeat_count --;
ef416fc2 774 putchar('0');
b86bc4cf 775 }
ef416fc2 776
b86bc4cf 777 if (repeat_count > 0)
778 putchar(',');
ef416fc2 779 }
780 else
781 ZPLCompress(repeat_char, repeat_count);
782
b86bc4cf 783 fflush(stdout);
784
ef416fc2 785 /*
786 * Save this line for the next round...
787 */
788
789 memcpy(LastBuffer, Buffer, header->cupsBytesPerLine);
790 LastSet = 1;
791 break;
792
793 case ZEBRA_CPCL :
794 if (Buffer[0] || memcmp(Buffer, Buffer + 1, header->cupsBytesPerLine))
795 {
796 printf("CG %u 1 0 %d ", header->cupsBytesPerLine, y);
797 fwrite(Buffer, 1, header->cupsBytesPerLine, stdout);
798 puts("\r");
799 fflush(stdout);
800 }
801 break;
802 }
803}
804
805
806/*
807 * 'ZPLCompress()' - Output a run-length compression sequence.
808 */
809
810void
811ZPLCompress(char repeat_char, /* I - Character to repeat */
812 int repeat_count) /* I - Number of repeated characters */
813{
814 if (repeat_count > 1)
815 {
816 /*
817 * Print as many z's as possible - they are the largest denomination
818 * representing 400 characters (zC stands for 400 adjacent C's)
819 */
820
821 while (repeat_count >= 400)
822 {
823 putchar('z');
824 repeat_count -= 400;
825 }
826
827 /*
828 * Then print 'g' through 'y' as multiples of 20 characters...
829 */
830
831 if (repeat_count >= 20)
832 {
833 putchar('f' + repeat_count / 20);
834 repeat_count %= 20;
835 }
836
837 /*
838 * Finally, print 'G' through 'Y' as 1 through 19 characters...
839 */
840
841 if (repeat_count > 0)
842 putchar('F' + repeat_count);
843 }
844
845 /*
846 * Then the character to be repeated...
847 */
848
849 putchar(repeat_char);
850}
851
852
853/*
854 * 'main()' - Main entry and processing of driver.
855 */
856
857int /* O - Exit status */
858main(int argc, /* I - Number of command-line arguments */
859 char *argv[]) /* I - Command-line arguments */
860{
861 int fd; /* File descriptor */
862 cups_raster_t *ras; /* Raster stream for printing */
863 cups_page_header_t header; /* Page header from file */
864 int y; /* Current line */
865 ppd_file_t *ppd; /* PPD file */
866 int num_options; /* Number of options */
867 cups_option_t *options; /* Options */
868
869
870 /*
871 * Make sure status messages are not buffered...
872 */
873
874 setbuf(stderr, NULL);
875
876 /*
877 * Check command-line...
878 */
879
880 if (argc < 6 || argc > 7)
881 {
882 /*
883 * We don't have the correct number of arguments; write an error message
884 * and return.
885 */
886
b86bc4cf 887 fputs("ERROR: rastertolabel job-id user title copies options [file]\n", stderr);
ef416fc2 888 return (1);
889 }
890
891 /*
892 * Open the page stream...
893 */
894
895 if (argc == 7)
896 {
897 if ((fd = open(argv[6], O_RDONLY)) == -1)
898 {
899 perror("ERROR: Unable to open raster file - ");
900 sleep(1);
901 return (1);
902 }
903 }
904 else
905 fd = 0;
906
907 ras = cupsRasterOpen(fd, CUPS_RASTER_READ);
908
909 /*
910 * Open the PPD file and apply options...
911 */
912
913 num_options = cupsParseOptions(argv[5], 0, &options);
914
915 if ((ppd = ppdOpenFile(getenv("PPD"))) != NULL)
916 {
917 ppdMarkDefaults(ppd);
918 cupsMarkOptions(ppd, num_options, options);
919 }
920
921 /*
922 * Initialize the print device...
923 */
924
925 Setup(ppd);
926
927 /*
928 * Process pages as needed...
929 */
930
931 Page = 0;
932 Canceled = 0;
933
934 while (cupsRasterReadHeader(ras, &header))
935 {
936 /*
937 * Write a status message with the page number and number of copies.
938 */
939
940 Page ++;
941
942 fprintf(stderr, "PAGE: %d 1\n", Page);
943
944 /*
945 * Start the page...
946 */
947
948 StartPage(ppd, &header);
949
950 /*
951 * Loop for each line on the page...
952 */
953
954 for (y = 0; y < header.cupsHeight && !Canceled; y ++)
955 {
956 /*
957 * Let the user know how far we have progressed...
958 */
959
960 if ((y & 15) == 0)
961 fprintf(stderr, "INFO: Printing page %d, %d%% complete...\n", Page,
962 100 * y / header.cupsHeight);
963
964 /*
965 * Read a line of graphics...
966 */
967
968 if (cupsRasterReadPixels(ras, Buffer, header.cupsBytesPerLine) < 1)
969 break;
970
971 /*
972 * Write it to the printer...
973 */
974
975 OutputLine(ppd, &header, y);
976 }
977
978 /*
979 * Eject the page...
980 */
981
982 EndPage(ppd, &header);
983
984 if (Canceled)
985 break;
986 }
987
988 /*
989 * Close the raster stream...
990 */
991
992 cupsRasterClose(ras);
993 if (fd != 0)
994 close(fd);
995
996 /*
997 * Close the PPD file and free the options...
998 */
999
1000 ppdClose(ppd);
1001 cupsFreeOptions(num_options, options);
1002
1003 /*
1004 * If no pages were printed, send an error message...
1005 */
1006
1007 if (Page == 0)
1008 fputs("ERROR: No pages found!\n", stderr);
1009 else
1010 fputs("INFO: Ready to print.\n", stderr);
1011
1012 return (Page == 0);
1013}
1014
1015
1016/*
b86bc4cf 1017 * End of "$Id: rastertolabel.c 6236 2007-02-05 21:04:04Z mike $".
ef416fc2 1018 */