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