plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

laserbar.c (4499B)


      1 /* laserbar -- filter to print barcodes on postscript printer */
      2 
      3 #define MAIN 1
      4 
      5 #define	LABEL	01
      6 #define	NFLAG	02
      7 #define	SFLAG	04
      8 
      9 #include <stdio.h>
     10 #include <ctype.h>
     11 
     12 static int code39[256] = {
     13 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     14 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     15 /*	sp    !     "     #     $     %     &     '	*/
     16 	0304, 0,    0,    0,    0250, 0052, 0,    0,
     17 /*	(     )     *     +     ,     -     -     /	*/
     18 	0,    0,    0224, 0212, 0,    0205, 0604, 0242,
     19 /*	0     1     2     3     4     5     6     7	*/
     20 	0064, 0441, 0141, 0540, 0061, 0460, 0160, 0045,
     21 /*	8     9     :     ;     <     =     >     ?	*/
     22 	0444, 0144, 0,    0,    0,    0,    0,    0,
     23 /*	@     A     B     C     D     E     F     G	*/
     24 	0,    0411, 0111, 0510, 0031, 0430, 0130, 0015,
     25 /*	H     I     J     K     L     M     N     O	*/
     26 	0414, 0114, 0034, 0403, 0103, 0502, 0023, 0422,
     27 /*	P     Q     R     S     T     U     V     W	*/
     28 	0122, 0007, 0406, 0106, 0026, 0601, 0301, 0700,
     29 /*	X     Y     Z     [     \     ]     ^     _	*/
     30 	0221, 0620, 0320, 0,    0,    0,    0,    0,
     31 /*	`     a     b     c     d     e     f     g	*/
     32 	0,    0411, 0111, 0510, 0031, 0430, 0130, 0015,
     33 /*	h     i     j     k     l     m     n     o	*/
     34 	0414, 0114, 0034, 0403, 0103, 0502, 0023, 0422,
     35 /*	p     q     r     s     t     u     v     w	*/
     36 	0122, 0007, 0406, 0106, 0026, 0601, 0301, 0700,
     37 /*	x     y     z     {     |     }     ~     del	*/
     38 	0221, 0620, 0320, 0,    0,    0,    0,    0,
     39 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     40 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     41 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     42 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     43 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     44 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     45 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     46 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
     47 };
     48 
     49 static void barprt();
     50 void laserbar();
     51 
     52 #ifdef MAIN
     53 
     54 main(argc, argv)
     55 char **argv;
     56 {
     57 	int c, flags = 0, error = 0;
     58 	double rotate = 0, xoffset = 0, yoffset = 0, xscale = 1, yscale = 1;
     59 	extern char *optarg;
     60 	extern int optind;
     61 	extern double atof();
     62 	extern void exit();
     63 
     64 	while ((c = getopt(argc, argv, "r:x:y:X:Y:lns")) != EOF) {
     65 		switch(c) {
     66 		    case 'r':
     67 			rotate = atof(optarg);
     68 			break;
     69 		    case 'x':
     70 			xoffset = atof(optarg);
     71 			break;
     72 		    case 'y':
     73 			yoffset = atof(optarg);
     74 			break;
     75 		    case 'X':
     76 			xscale = atof(optarg);
     77 			break;
     78 		    case 'Y':
     79 			yscale = atof(optarg);
     80 			break;
     81 		    case 'l':
     82 			flags |= LABEL;
     83 			break;
     84 		    case 'n':
     85 			flags |= NFLAG;
     86 			break;
     87 		    case 's':
     88 			flags |= SFLAG;
     89 			break;
     90 		    case '?':
     91 			++error;
     92 		}
     93 	}
     94 	if ((argc - optind) != 1)
     95 		++error;
     96 	if (error) {
     97 		(void) fprintf(stderr,
     98 "Usage: %s [-r rotate] [-x xoffset] [-y yoffset] [-X xscale] [-Y yscale] [-lns] string\n",
     99 		    *argv);
    100 		exit(1);
    101 	}
    102 	laserbar(stdout, argv[optind], rotate, xoffset, yoffset, xscale, yscale, flags);
    103 	return 0;
    104 }
    105 
    106 #endif /*MAIN*/
    107 
    108 static int right = 0;
    109 
    110 void
    111 laserbar(fp, str, rotate, xoffset, yoffset, xscale, yscale, flags)
    112 FILE *fp;
    113 char *str;
    114 double rotate, xoffset, yoffset, xscale, yscale;
    115 int flags;
    116 {
    117 	xoffset *= 72.;
    118 	yoffset *= 72.;
    119 	(void) fprintf(fp, "gsave %s\n", (flags & NFLAG) ? "newpath" : "");
    120 	if (xoffset || yoffset)
    121 		(void) fprintf(fp, "%f %f moveto\n", xoffset, yoffset);
    122 	if (xscale != 1 || yscale != 1)
    123 		(void) fprintf(fp, "%f %f scale\n", xscale, yscale);
    124 	if (rotate)
    125 		(void) fprintf(fp, "%f rotate\n", rotate);
    126 	(void) fputs("/Helvetica findfont 16 scalefont setfont\n", fp);
    127 	(void) fputs("/w { 0 rmoveto gsave 3 setlinewidth 0 -72 rlineto stroke grestore } def\n", fp);
    128 	(void) fputs("/n { 0 rmoveto gsave 1 setlinewidth 0 -72 rlineto stroke grestore } def\n", fp);
    129 	(void) fputs("/l { gsave 2 -88 rmoveto show grestore } def\n", fp);
    130 	barprt(fp, '*', 0);
    131 	while (*str)
    132 		barprt(fp, *(str++), (flags & LABEL));
    133 	barprt(fp, '*', 0);
    134 	(void) fprintf(fp, "%sgrestore\n", (flags & SFLAG) ? "showpage " : "");
    135 	right = 0;
    136 }
    137 
    138 static void
    139 barprt(fp, c, label)
    140 FILE *fp;
    141 int c, label;
    142 {
    143 	int i, mask, bar, wide;
    144 
    145 	if (!(i = code39[c]))
    146 		return;
    147 	if (islower(c))
    148 		c = toupper(c);
    149 	if (label)
    150 		(void) fprintf(fp, "(%c) l", c);
    151 	else
    152 		(void) fputs("     ", fp);
    153 	for (bar = 1, mask = 0400; mask; bar = 1 - bar, mask >>= 1) {
    154 		wide = mask & i;
    155 		if (bar) {
    156 			if (wide)
    157 				++right;
    158 			(void) fprintf(fp, " %d %s", right, wide ? "w" : "n");
    159 			right = (wide ? 2 : 1);
    160 		}
    161 		else
    162 			right += (wide ? 3 : 1);
    163 	}
    164 	(void) fputs("\n", fp);
    165 	++right;
    166 }