laserbar.c 4.4 KB

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