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