]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * "$Id: rastertolabel.c 6649 2007-07-11 21:46:42Z mike $" | |
3 | * | |
4 | * Label printer filter for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 2007 by Apple Inc. | |
7 | * Copyright 2001-2007 by Easy Software Products. | |
8 | * | |
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/". | |
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. | |
24 | * PCLCompress() - Output a PCL (mode 3) compressed line. | |
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> | |
35 | #include <cups/i18n.h> | |
36 | #include "raster.h" | |
37 | #include <stdlib.h> | |
38 | #include <unistd.h> | |
39 | #include <fcntl.h> | |
40 | #include <signal.h> | |
41 | ||
42 | ||
43 | /* | |
44 | * This driver filter currently supports Dymo, Intellitech, and Zebra | |
45 | * label printers. | |
46 | * | |
47 | * The Dymo portion of the driver has been tested with the 300, 330, | |
48 | * and 330 Turbo label printers; it may also work with other models. | |
49 | * The Dymo printers support printing at 136, 203, and 300 DPI. | |
50 | * | |
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. | |
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 | ||
71 | #define INTELLITECH_PCL 0x20 /* Intellitech PCL-based printers */ | |
72 | ||
73 | ||
74 | /* | |
75 | * Globals... | |
76 | */ | |
77 | ||
78 | unsigned char *Buffer; /* Output buffer */ | |
79 | unsigned char *CompBuffer; /* Compression buffer */ | |
80 | unsigned char *LastBuffer; /* Last buffer */ | |
81 | int LastSet; /* Number of repeat characters */ | |
82 | int 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 | ||
92 | void Setup(ppd_file_t *ppd); | |
93 | void StartPage(ppd_file_t *ppd, cups_page_header_t *header); | |
94 | void EndPage(ppd_file_t *ppd, cups_page_header_t *header); | |
95 | void CancelJob(int sig); | |
96 | void OutputLine(ppd_file_t *ppd, cups_page_header_t *header, int y); | |
97 | void PCLCompress(unsigned char *line, int length); | |
98 | void ZPLCompress(char repeat_char, int repeat_count); | |
99 | ||
100 | ||
101 | /* | |
102 | * 'Setup()' - Prepare the printer for printing. | |
103 | */ | |
104 | ||
105 | void | |
106 | Setup(ppd_file_t *ppd) /* I - PPD file */ | |
107 | { | |
108 | int i; /* Looping var */ | |
109 | ||
110 | ||
111 | /* | |
112 | * Get the model number from the PPD file... | |
113 | */ | |
114 | ||
115 | if (ppd) | |
116 | ModelNumber = ppd->model_number; | |
117 | ||
118 | /* | |
119 | * Initialize based on the model number... | |
120 | */ | |
121 | ||
122 | switch (ModelNumber) | |
123 | { | |
124 | case DYMO_3x0 : | |
125 | /* | |
126 | * Clear any remaining data... | |
127 | */ | |
128 | ||
129 | for (i = 0; i < 100; i ++) | |
130 | putchar(0x1b); | |
131 | ||
132 | /* | |
133 | * Reset the printer... | |
134 | */ | |
135 | ||
136 | fputs("\033@", stdout); | |
137 | break; | |
138 | ||
139 | case ZEBRA_EPL_LINE : | |
140 | break; | |
141 | ||
142 | case ZEBRA_EPL_PAGE : | |
143 | break; | |
144 | ||
145 | case ZEBRA_ZPL : | |
146 | break; | |
147 | ||
148 | case ZEBRA_CPCL : | |
149 | break; | |
150 | ||
151 | case INTELLITECH_PCL : | |
152 | /* | |
153 | * Send a PCL reset sequence. | |
154 | */ | |
155 | ||
156 | putchar(0x1b); | |
157 | putchar('E'); | |
158 | break; | |
159 | } | |
160 | } | |
161 | ||
162 | ||
163 | /* | |
164 | * 'StartPage()' - Start a page of graphics. | |
165 | */ | |
166 | ||
167 | void | |
168 | StartPage(ppd_file_t *ppd, /* I - PPD file */ | |
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 | ||
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); | |
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); | |
228 | ||
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 | ||
331 | printf("q%d\n", (header->cupsWidth + 7) & ~7); | |
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); | |
367 | printf("PAGE-WIDTH %d\r\n", header->cupsWidth); | |
368 | printf("PAGE-HEIGHT %d\r\n", header->cupsWidth); | |
369 | break; | |
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; | |
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 | ||
499 | void | |
500 | EndPage(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 | ||
663 | puts("^XZ"); | |
664 | ||
665 | /* | |
666 | * Free compression buffers... | |
667 | */ | |
668 | ||
669 | free(CompBuffer); | |
670 | free(LastBuffer); | |
671 | break; | |
672 | ||
673 | case ZEBRA_CPCL : | |
674 | /* | |
675 | * Set tear-off adjust position... | |
676 | */ | |
677 | ||
678 | if (header->AdvanceDistance != 1000) | |
679 | printf("PRESENT-AT %d 1\r\n", (int)header->AdvanceDistance); | |
680 | ||
681 | /* | |
682 | * Allow for reprinting after an error... | |
683 | */ | |
684 | ||
685 | if (ppdIsMarked(ppd, "zeErrorReprint", "Always")) | |
686 | puts("ON-OUT-OF-PAPER WAIT\r"); | |
687 | else if (ppdIsMarked(ppd, "zeErrorReprint", "Never")) | |
688 | puts("ON-OUT-OF-PAPER PURGE\r"); | |
689 | ||
690 | /* | |
691 | * Cut label? | |
692 | */ | |
693 | ||
694 | if (header->CutMedia) | |
695 | puts("CUT\r"); | |
696 | ||
697 | /* | |
698 | * Set darkness... | |
699 | */ | |
700 | ||
701 | if (header->cupsCompression > 0) | |
702 | printf("TONE %u\r\n", 2 * header->cupsCompression); | |
703 | ||
704 | /* | |
705 | * Set print rate... | |
706 | */ | |
707 | ||
708 | if ((choice = ppdFindMarkedChoice(ppd, "zePrintRate")) != NULL && | |
709 | strcmp(choice->choice, "Default")) | |
710 | { | |
711 | val = atoi(choice->choice); | |
712 | printf("SPEED %d\r\n", val); | |
713 | } | |
714 | ||
715 | /* | |
716 | * Print the label... | |
717 | */ | |
718 | ||
719 | if ((choice = ppdFindMarkedChoice(ppd, "zeMediaTracking")) == NULL || | |
720 | strcmp(choice->choice, "Continuous")) | |
721 | puts("FORM\r"); | |
722 | ||
723 | puts("PRINT\r"); | |
724 | break; | |
725 | ||
726 | case INTELLITECH_PCL : | |
727 | printf("\033*rB"); /* End GFX */ | |
728 | printf("\014"); /* Eject current page */ | |
729 | break; | |
730 | } | |
731 | ||
732 | fflush(stdout); | |
733 | ||
734 | /* | |
735 | * Unregister the signal handler... | |
736 | */ | |
737 | ||
738 | #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ | |
739 | sigset(SIGTERM, SIG_IGN); | |
740 | #elif defined(HAVE_SIGACTION) | |
741 | memset(&action, 0, sizeof(action)); | |
742 | ||
743 | sigemptyset(&action.sa_mask); | |
744 | action.sa_handler = SIG_IGN; | |
745 | sigaction(SIGTERM, &action, NULL); | |
746 | #else | |
747 | signal(SIGTERM, SIG_IGN); | |
748 | #endif /* HAVE_SIGSET */ | |
749 | ||
750 | /* | |
751 | * Free memory... | |
752 | */ | |
753 | ||
754 | free(Buffer); | |
755 | } | |
756 | ||
757 | ||
758 | /* | |
759 | * 'CancelJob()' - Cancel the current job... | |
760 | */ | |
761 | ||
762 | void | |
763 | CancelJob(int sig) /* I - Signal */ | |
764 | { | |
765 | /* | |
766 | * Tell the main loop to stop... | |
767 | */ | |
768 | ||
769 | (void)sig; | |
770 | ||
771 | Canceled = 1; | |
772 | } | |
773 | ||
774 | ||
775 | /* | |
776 | * 'OutputLine()' - Output a line of graphics... | |
777 | */ | |
778 | ||
779 | void | |
780 | OutputLine(ppd_file_t *ppd, /* I - PPD file */ | |
781 | cups_page_header_t *header, /* I - Page header */ | |
782 | int y) /* I - Line number */ | |
783 | { | |
784 | int i; /* Looping var */ | |
785 | unsigned char *ptr; /* Pointer into buffer */ | |
786 | unsigned char *compptr; /* Pointer into compression buffer */ | |
787 | char repeat_char; /* Repeated character */ | |
788 | int repeat_count; /* Number of repeated characters */ | |
789 | static const char *hex = "0123456789ABCDEF"; | |
790 | /* Hex digits */ | |
791 | ||
792 | ||
793 | switch (ModelNumber) | |
794 | { | |
795 | case DYMO_3x0 : | |
796 | /* | |
797 | * See if the line is blank; if not, write it to the printer... | |
798 | */ | |
799 | ||
800 | if (Buffer[0] || | |
801 | memcmp(Buffer, Buffer + 1, header->cupsBytesPerLine - 1)) | |
802 | { | |
803 | if (Feed) | |
804 | { | |
805 | while (Feed > 255) | |
806 | { | |
807 | printf("\033f\001%c", 255); | |
808 | Feed -= 255; | |
809 | } | |
810 | ||
811 | printf("\033f\001%c", Feed); | |
812 | Feed = 0; | |
813 | } | |
814 | ||
815 | putchar(0x16); | |
816 | fwrite(Buffer, header->cupsBytesPerLine, 1, stdout); | |
817 | fflush(stdout); | |
818 | ||
819 | #ifdef __sgi | |
820 | /* | |
821 | * This hack works around a bug in the IRIX serial port driver when | |
822 | * run at high baud rates (e.g. 115200 baud)... This results in | |
823 | * slightly slower label printing, but at least the labels come | |
824 | * out properly. | |
825 | */ | |
826 | ||
827 | sginap(1); | |
828 | #endif /* __sgi */ | |
829 | } | |
830 | else | |
831 | Feed ++; | |
832 | break; | |
833 | ||
834 | case ZEBRA_EPL_LINE : | |
835 | printf("\033g%03d", header->cupsBytesPerLine); | |
836 | fwrite(Buffer, 1, header->cupsBytesPerLine, stdout); | |
837 | fflush(stdout); | |
838 | break; | |
839 | ||
840 | case ZEBRA_EPL_PAGE : | |
841 | if (Buffer[0] || memcmp(Buffer, Buffer + 1, header->cupsBytesPerLine)) | |
842 | { | |
843 | printf("GW0,%d,%d,1\n", y, header->cupsBytesPerLine); | |
844 | for (i = header->cupsBytesPerLine, ptr = Buffer; i > 0; i --, ptr ++) | |
845 | putchar(~*ptr); | |
846 | putchar('\n'); | |
847 | fflush(stdout); | |
848 | } | |
849 | break; | |
850 | ||
851 | case ZEBRA_ZPL : | |
852 | /* | |
853 | * Determine if this row is the same as the previous line. | |
854 | * If so, output a ':' and return... | |
855 | */ | |
856 | ||
857 | if (LastSet) | |
858 | { | |
859 | if (!memcmp(Buffer, LastBuffer, header->cupsBytesPerLine)) | |
860 | { | |
861 | putchar(':'); | |
862 | return; | |
863 | } | |
864 | } | |
865 | ||
866 | /* | |
867 | * Convert the line to hex digits... | |
868 | */ | |
869 | ||
870 | for (ptr = Buffer, compptr = CompBuffer, i = header->cupsBytesPerLine; | |
871 | i > 0; | |
872 | i --, ptr ++) | |
873 | { | |
874 | *compptr++ = hex[*ptr >> 4]; | |
875 | *compptr++ = hex[*ptr & 15]; | |
876 | } | |
877 | ||
878 | *compptr = '\0'; | |
879 | ||
880 | /* | |
881 | * Run-length compress the graphics... | |
882 | */ | |
883 | ||
884 | for (compptr = CompBuffer + 1, repeat_char = CompBuffer[0], repeat_count = 1; | |
885 | *compptr; | |
886 | compptr ++) | |
887 | if (*compptr == repeat_char) | |
888 | repeat_count ++; | |
889 | else | |
890 | { | |
891 | ZPLCompress(repeat_char, repeat_count); | |
892 | repeat_char = *compptr; | |
893 | repeat_count = 1; | |
894 | } | |
895 | ||
896 | if (repeat_char == '0') | |
897 | { | |
898 | /* | |
899 | * Handle 0's on the end of the line... | |
900 | */ | |
901 | ||
902 | if (repeat_count & 1) | |
903 | { | |
904 | repeat_count --; | |
905 | putchar('0'); | |
906 | } | |
907 | ||
908 | if (repeat_count > 0) | |
909 | putchar(','); | |
910 | } | |
911 | else | |
912 | ZPLCompress(repeat_char, repeat_count); | |
913 | ||
914 | fflush(stdout); | |
915 | ||
916 | /* | |
917 | * Save this line for the next round... | |
918 | */ | |
919 | ||
920 | memcpy(LastBuffer, Buffer, header->cupsBytesPerLine); | |
921 | LastSet = 1; | |
922 | break; | |
923 | ||
924 | case ZEBRA_CPCL : | |
925 | if (Buffer[0] || memcmp(Buffer, Buffer + 1, header->cupsBytesPerLine)) | |
926 | { | |
927 | printf("CG %u 1 0 %d ", header->cupsBytesPerLine, y); | |
928 | fwrite(Buffer, 1, header->cupsBytesPerLine, stdout); | |
929 | puts("\r"); | |
930 | fflush(stdout); | |
931 | } | |
932 | break; | |
933 | ||
934 | case INTELLITECH_PCL : | |
935 | if (Buffer[0] || | |
936 | memcmp(Buffer, Buffer + 1, header->cupsBytesPerLine - 1)) | |
937 | { | |
938 | if (Feed) | |
939 | { | |
940 | printf("\033*b%dY", Feed); | |
941 | Feed = 0; | |
942 | LastSet = 0; | |
943 | } | |
944 | ||
945 | PCLCompress(Buffer, header->cupsBytesPerLine); | |
946 | } | |
947 | else | |
948 | Feed ++; | |
949 | break; | |
950 | } | |
951 | } | |
952 | ||
953 | ||
954 | /* | |
955 | * 'PCLCompress()' - Output a PCL (mode 3) compressed line. | |
956 | */ | |
957 | ||
958 | void | |
959 | PCLCompress(unsigned char *line, /* I - Line to compress */ | |
960 | int length) /* I - Length of line */ | |
961 | { | |
962 | unsigned char *line_ptr, /* Current byte pointer */ | |
963 | *line_end, /* End-of-line byte pointer */ | |
964 | *comp_ptr, /* Pointer into compression buffer */ | |
965 | *start, /* Start of compression sequence */ | |
966 | *seed; /* Seed buffer pointer */ | |
967 | int count, /* Count of bytes for output */ | |
968 | offset; /* Offset of bytes for output */ | |
969 | ||
970 | ||
971 | /* | |
972 | * Do delta-row compression... | |
973 | */ | |
974 | ||
975 | line_ptr = line; | |
976 | line_end = line + length; | |
977 | ||
978 | comp_ptr = CompBuffer; | |
979 | seed = LastBuffer; | |
980 | ||
981 | while (line_ptr < line_end) | |
982 | { | |
983 | /* | |
984 | * Find the next non-matching sequence... | |
985 | */ | |
986 | ||
987 | start = line_ptr; | |
988 | ||
989 | if (!LastSet) | |
990 | { | |
991 | /* | |
992 | * The seed buffer is invalid, so do the next 8 bytes, max... | |
993 | */ | |
994 | ||
995 | offset = 0; | |
996 | ||
997 | if ((count = line_end - line_ptr) > 8) | |
998 | count = 8; | |
999 | ||
1000 | line_ptr += count; | |
1001 | } | |
1002 | else | |
1003 | { | |
1004 | /* | |
1005 | * The seed buffer is valid, so compare against it... | |
1006 | */ | |
1007 | ||
1008 | while (*line_ptr == *seed && | |
1009 | line_ptr < line_end) | |
1010 | { | |
1011 | line_ptr ++; | |
1012 | seed ++; | |
1013 | } | |
1014 | ||
1015 | if (line_ptr == line_end) | |
1016 | break; | |
1017 | ||
1018 | offset = line_ptr - start; | |
1019 | ||
1020 | /* | |
1021 | * Find up to 8 non-matching bytes... | |
1022 | */ | |
1023 | ||
1024 | start = line_ptr; | |
1025 | count = 0; | |
1026 | while (*line_ptr != *seed && | |
1027 | line_ptr < line_end && | |
1028 | count < 8) | |
1029 | { | |
1030 | line_ptr ++; | |
1031 | seed ++; | |
1032 | count ++; | |
1033 | } | |
1034 | } | |
1035 | ||
1036 | /* | |
1037 | * Place mode 3 compression data in the buffer; see HP manuals | |
1038 | * for details... | |
1039 | */ | |
1040 | ||
1041 | if (offset >= 31) | |
1042 | { | |
1043 | /* | |
1044 | * Output multi-byte offset... | |
1045 | */ | |
1046 | ||
1047 | *comp_ptr++ = ((count - 1) << 5) | 31; | |
1048 | ||
1049 | offset -= 31; | |
1050 | while (offset >= 255) | |
1051 | { | |
1052 | *comp_ptr++ = 255; | |
1053 | offset -= 255; | |
1054 | } | |
1055 | ||
1056 | *comp_ptr++ = offset; | |
1057 | } | |
1058 | else | |
1059 | { | |
1060 | /* | |
1061 | * Output single-byte offset... | |
1062 | */ | |
1063 | ||
1064 | *comp_ptr++ = ((count - 1) << 5) | offset; | |
1065 | } | |
1066 | ||
1067 | memcpy(comp_ptr, start, count); | |
1068 | comp_ptr += count; | |
1069 | } | |
1070 | ||
1071 | line_ptr = CompBuffer; | |
1072 | line_end = comp_ptr; | |
1073 | ||
1074 | /* | |
1075 | * Set the length of the data and write it... | |
1076 | */ | |
1077 | ||
1078 | printf("\033*b%dW", comp_ptr - CompBuffer); | |
1079 | fwrite(CompBuffer, comp_ptr - CompBuffer, 1, stdout); | |
1080 | ||
1081 | /* | |
1082 | * Save this line as a "seed" buffer for the next... | |
1083 | */ | |
1084 | ||
1085 | memcpy(LastBuffer, line, length); | |
1086 | LastSet = 1; | |
1087 | } | |
1088 | ||
1089 | ||
1090 | /* | |
1091 | * 'ZPLCompress()' - Output a run-length compression sequence. | |
1092 | */ | |
1093 | ||
1094 | void | |
1095 | ZPLCompress(char repeat_char, /* I - Character to repeat */ | |
1096 | int repeat_count) /* I - Number of repeated characters */ | |
1097 | { | |
1098 | if (repeat_count > 1) | |
1099 | { | |
1100 | /* | |
1101 | * Print as many z's as possible - they are the largest denomination | |
1102 | * representing 400 characters (zC stands for 400 adjacent C's) | |
1103 | */ | |
1104 | ||
1105 | while (repeat_count >= 400) | |
1106 | { | |
1107 | putchar('z'); | |
1108 | repeat_count -= 400; | |
1109 | } | |
1110 | ||
1111 | /* | |
1112 | * Then print 'g' through 'y' as multiples of 20 characters... | |
1113 | */ | |
1114 | ||
1115 | if (repeat_count >= 20) | |
1116 | { | |
1117 | putchar('f' + repeat_count / 20); | |
1118 | repeat_count %= 20; | |
1119 | } | |
1120 | ||
1121 | /* | |
1122 | * Finally, print 'G' through 'Y' as 1 through 19 characters... | |
1123 | */ | |
1124 | ||
1125 | if (repeat_count > 0) | |
1126 | putchar('F' + repeat_count); | |
1127 | } | |
1128 | ||
1129 | /* | |
1130 | * Then the character to be repeated... | |
1131 | */ | |
1132 | ||
1133 | putchar(repeat_char); | |
1134 | } | |
1135 | ||
1136 | ||
1137 | /* | |
1138 | * 'main()' - Main entry and processing of driver. | |
1139 | */ | |
1140 | ||
1141 | int /* O - Exit status */ | |
1142 | main(int argc, /* I - Number of command-line arguments */ | |
1143 | char *argv[]) /* I - Command-line arguments */ | |
1144 | { | |
1145 | int fd; /* File descriptor */ | |
1146 | cups_raster_t *ras; /* Raster stream for printing */ | |
1147 | cups_page_header_t header; /* Page header from file */ | |
1148 | int y; /* Current line */ | |
1149 | ppd_file_t *ppd; /* PPD file */ | |
1150 | int num_options; /* Number of options */ | |
1151 | cups_option_t *options; /* Options */ | |
1152 | ||
1153 | ||
1154 | /* | |
1155 | * Make sure status messages are not buffered... | |
1156 | */ | |
1157 | ||
1158 | setbuf(stderr, NULL); | |
1159 | ||
1160 | /* | |
1161 | * Check command-line... | |
1162 | */ | |
1163 | ||
1164 | if (argc < 6 || argc > 7) | |
1165 | { | |
1166 | /* | |
1167 | * We don't have the correct number of arguments; write an error message | |
1168 | * and return. | |
1169 | */ | |
1170 | ||
1171 | fprintf(stderr, _("Usage: %s job-id user title copies options [file]\n"), | |
1172 | argv[0]); | |
1173 | return (1); | |
1174 | } | |
1175 | ||
1176 | /* | |
1177 | * Open the page stream... | |
1178 | */ | |
1179 | ||
1180 | if (argc == 7) | |
1181 | { | |
1182 | if ((fd = open(argv[6], O_RDONLY)) == -1) | |
1183 | { | |
1184 | perror("ERROR: Unable to open raster file - "); | |
1185 | sleep(1); | |
1186 | return (1); | |
1187 | } | |
1188 | } | |
1189 | else | |
1190 | fd = 0; | |
1191 | ||
1192 | ras = cupsRasterOpen(fd, CUPS_RASTER_READ); | |
1193 | ||
1194 | /* | |
1195 | * Open the PPD file and apply options... | |
1196 | */ | |
1197 | ||
1198 | num_options = cupsParseOptions(argv[5], 0, &options); | |
1199 | ||
1200 | if ((ppd = ppdOpenFile(getenv("PPD"))) != NULL) | |
1201 | { | |
1202 | ppdMarkDefaults(ppd); | |
1203 | cupsMarkOptions(ppd, num_options, options); | |
1204 | } | |
1205 | ||
1206 | /* | |
1207 | * Initialize the print device... | |
1208 | */ | |
1209 | ||
1210 | Setup(ppd); | |
1211 | ||
1212 | /* | |
1213 | * Process pages as needed... | |
1214 | */ | |
1215 | ||
1216 | Page = 0; | |
1217 | Canceled = 0; | |
1218 | ||
1219 | while (cupsRasterReadHeader(ras, &header)) | |
1220 | { | |
1221 | /* | |
1222 | * Write a status message with the page number and number of copies. | |
1223 | */ | |
1224 | ||
1225 | Page ++; | |
1226 | ||
1227 | fprintf(stderr, "PAGE: %d 1\n", Page); | |
1228 | ||
1229 | /* | |
1230 | * Start the page... | |
1231 | */ | |
1232 | ||
1233 | StartPage(ppd, &header); | |
1234 | ||
1235 | /* | |
1236 | * Loop for each line on the page... | |
1237 | */ | |
1238 | ||
1239 | for (y = 0; y < header.cupsHeight && !Canceled; y ++) | |
1240 | { | |
1241 | /* | |
1242 | * Let the user know how far we have progressed... | |
1243 | */ | |
1244 | ||
1245 | if ((y & 15) == 0) | |
1246 | fprintf(stderr, _("INFO: Printing page %d, %d%% complete...\n"), Page, | |
1247 | 100 * y / header.cupsHeight); | |
1248 | ||
1249 | /* | |
1250 | * Read a line of graphics... | |
1251 | */ | |
1252 | ||
1253 | if (cupsRasterReadPixels(ras, Buffer, header.cupsBytesPerLine) < 1) | |
1254 | break; | |
1255 | ||
1256 | /* | |
1257 | * Write it to the printer... | |
1258 | */ | |
1259 | ||
1260 | OutputLine(ppd, &header, y); | |
1261 | } | |
1262 | ||
1263 | /* | |
1264 | * Eject the page... | |
1265 | */ | |
1266 | ||
1267 | EndPage(ppd, &header); | |
1268 | ||
1269 | if (Canceled) | |
1270 | break; | |
1271 | } | |
1272 | ||
1273 | /* | |
1274 | * Close the raster stream... | |
1275 | */ | |
1276 | ||
1277 | cupsRasterClose(ras); | |
1278 | if (fd != 0) | |
1279 | close(fd); | |
1280 | ||
1281 | /* | |
1282 | * Close the PPD file and free the options... | |
1283 | */ | |
1284 | ||
1285 | ppdClose(ppd); | |
1286 | cupsFreeOptions(num_options, options); | |
1287 | ||
1288 | /* | |
1289 | * If no pages were printed, send an error message... | |
1290 | */ | |
1291 | ||
1292 | if (Page == 0) | |
1293 | fputs(_("ERROR: No pages found!\n"), stderr); | |
1294 | else | |
1295 | fputs(_("INFO: Ready to print.\n"), stderr); | |
1296 | ||
1297 | return (Page == 0); | |
1298 | } | |
1299 | ||
1300 | ||
1301 | /* | |
1302 | * End of "$Id: rastertolabel.c 6649 2007-07-11 21:46:42Z mike $". | |
1303 | */ |