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