home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1655 / pbmtopk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  23.9 KB  |  1,041 lines

  1. /*
  2.   pbmtopk, adapted from "pxtopk.c by tomas rokicki" by AJCD 1/8/90
  3.   
  4.   compile with: cc -o pbmtopk pbmtopk.c -lm -lpbm
  5.   */
  6.  
  7. #include <stdio.h>
  8. #include <pbm.h>
  9. #include <math.h>
  10. #include <ctype.h>
  11.  
  12. #define MAXPKCHAR 256
  13. #define MAXOPTLINE 200
  14. #define MAXWIDTHTAB 256
  15. #define MAXHEIGHTTAB 16
  16. #define MAXDEPTHTAB 16
  17. #define MAXITALICTAB 64
  18. #define MAXPARAMS 30
  19. #define NAMELENGTH 80
  20.  
  21. #define true (1)
  22. #define false (0)
  23. #define chr(a) a
  24.   
  25. #define round(a) ((int)(a+.5))
  26. #define fixword(d) ((int)aint((double)((d)*1048576)))
  27. #define unfixword(f) ((double)(f) / 1048576)
  28. #define fixrange(f) ((f) < 16777216 && (f) > -16777216)
  29. #define designunits(p) ((p)*72.27/(double)resolution/unfixword(designsize))
  30.  
  31. /* character flags: in order of appearance in option files. */
  32. #define XOFFSET     1
  33. #define YOFFSET     2
  34. #define HORZESC     4
  35. #define VERTESC     8
  36. #define TFMWIDTH   16
  37. #define TFMHEIGHT  32
  38. #define TFMDEPTH   64
  39. #define TFMITALIC 128
  40.  
  41. typedef int integer ;
  42. typedef char quarterword ;
  43. typedef char boolean ;
  44. typedef quarterword ASCIIcode ;
  45. typedef quarterword eightbits ;
  46. typedef FILE *bytefile ;
  47. typedef unsigned char byte ;
  48.  
  49. integer resolution, designsize ;
  50. char *filename[MAXPKCHAR] ;
  51.  
  52. integer xoffset[MAXPKCHAR] ;
  53. integer yoffset[MAXPKCHAR] ;
  54. integer horzesc[MAXPKCHAR] ;
  55. integer vertesc[MAXPKCHAR] ;
  56.  
  57. byte tfmindex[MAXPKCHAR] ;
  58. byte hgtindex[MAXPKCHAR] ;
  59. byte depindex[MAXPKCHAR] ;
  60. byte italindex[MAXPKCHAR] ;
  61. byte charflags[MAXPKCHAR] ;
  62.  
  63. bit **bitmap ;
  64. integer smallestch, largestch ;
  65. integer emwidth ;
  66. integer checksum ;
  67. char *codingscheme = "GRAPHIC" ;
  68. char *familyname = "PBM" ;
  69.  
  70. integer widthtab[MAXWIDTHTAB] ; /* TFM widths */
  71. integer numwidth ;      /* number of entries in width table */
  72. integer heighttab[MAXHEIGHTTAB] ;
  73. integer numheight ;
  74. integer depthtab[MAXDEPTHTAB] ;
  75. integer numdepth ;
  76. integer italictab[MAXITALICTAB] ;
  77. integer numitalic ;
  78. integer parameters[MAXPARAMS] ;
  79. integer numparam ;
  80.  
  81. ASCIIcode xord[128] ;
  82. char xchr[256] ;
  83. bytefile tfmfile, pkfile ;
  84. char tfmname[NAMELENGTH+1], pkname[NAMELENGTH+1] ;
  85. integer pkloc ;
  86. integer bitweight ;
  87. integer outputbyte ;
  88. integer car ;
  89. integer hppp ;
  90. integer width ;
  91. integer height ;
  92. integer power[32] ;
  93.  
  94. integer
  95. compute_checksum()
  96. {
  97.   /*
  98.     begin
  99.       c0:=bc; c1:=ec; c2:=bc; c3:=ec;
  100.       for c:=bc to ec do if char_wd[c]>0 then
  101.     begin
  102.       temp_width:=memory[char_wd[c]];
  103.       if design_units<>unity then
  104.         temp_width:=round((temp_width/design_units)*1048576.0);
  105.       temp_width:=temp_width + (c+4)*@'20000000; {this should be positive}
  106.       c0:=(c0+c0+temp_width) mod 255;
  107.       c1:=(c1+c1+temp_width) mod 253;
  108.       c2:=(c2+c2+temp_width) mod 251;
  109.       c3:=(c3+c3+temp_width) mod 247;
  110.     end;
  111.       header_bytes[check_sum_loc]:=c0;
  112.       header_bytes[check_sum_loc+1]:=c1;
  113.       header_bytes[check_sum_loc+2]:=c2;
  114.       header_bytes[check_sum_loc+3]:=c3;
  115.     end
  116.     */
  117. }
  118.  
  119. #define add_tfmwidth(v) (add_tfmtable(widthtab, &numwidth, v, MAXWIDTHTAB,\
  120.                       "TFM width"))
  121. #define add_tfmheight(v) (add_tfmtable(heighttab, &numheight, v, MAXHEIGHTTAB,\
  122.                        "TFM height"))
  123. #define add_tfmdepth(v) (add_tfmtable(depthtab, &numdepth, v, MAXDEPTHTAB,\
  124.                       "TFM depth"))
  125. #define add_tfmitalic(v) (add_tfmtable(italictab, &numitalic, v, MAXITALICTAB,\
  126.                        "Italic correction"))
  127. byte
  128. add_tfmtable(table, count, value, max, name)
  129.      integer *table, *count, value, max;
  130.      char *name;
  131. {
  132.   integer i;
  133.   for (i = 0; i < *count; i++) /* search for value in tfm table */
  134.     if (table[i] == value) return (byte)i;
  135.   if (*count >= max) {
  136.     fprintf(stderr, "Too many values in %s table!\n", name) ;
  137.     jumpout();
  138.   }
  139.   if (!fixrange(value)) {
  140.     fprintf(stderr, " %s %f for char %d out of range!\n",
  141.         name, unfixword(value), car);
  142.     jumpout();
  143.   }
  144.   table[*count] = value ;
  145.   return (*count)++ ;
  146. }
  147.  
  148. add_suffix(name, suffix)
  149.      char *name, *suffix ;
  150. {
  151.   int haveext = 0;
  152.   if (name && strcmp(name, "-")) {
  153.     while (*name) {
  154.       if (*name == '/') haveext = 0 ;
  155.       else if (*name == '.') haveext = 1 ;
  156.       name++ ;
  157.     }
  158.     if (!haveext) {
  159.       *name++ = '.';
  160.       strcpy(name,suffix) ;
  161.     }
  162.   }
  163. }
  164.  
  165. initialize()
  166. {
  167.   integer i ;
  168.   fprintf(stderr, "This is PBMtoPK, C Version 2.3\n") ;
  169.   for (i = 0 ; i <= 31 ; i ++) xchr[i] = '?' ;
  170.   xchr[32] = ' ' ;
  171.   xchr[33] = '!' ;
  172.   xchr[34] = '"' ;
  173.   xchr[35] = '#' ;
  174.   xchr[36] = '$' ;
  175.   xchr[37] = '%' ;
  176.   xchr[38] = '&' ;
  177.   xchr[39] = '\'' ;
  178.   xchr[40] = '(' ;
  179.   xchr[41] = ')' ;
  180.   xchr[42] = '*' ;
  181.   xchr[43] = '+' ;
  182.   xchr[44] = ',' ;
  183.   xchr[45] = '-' ;
  184.   xchr[46] = '.' ;
  185.   xchr[47] = '/' ;
  186.   xchr[48] = '0' ;
  187.   xchr[49] = '1' ;
  188.   xchr[50] = '2' ;
  189.   xchr[51] = '3' ;
  190.   xchr[52] = '4' ;
  191.   xchr[53] = '5' ;
  192.   xchr[54] = '6' ;
  193.   xchr[55] = '7' ;
  194.   xchr[56] = '8' ;
  195.   xchr[57] = '9' ;
  196.   xchr[58] = ':' ;
  197.   xchr[59] = ';' ;
  198.   xchr[60] = '<' ;
  199.   xchr[61] = '=' ;
  200.   xchr[62] = '>' ;
  201.   xchr[63] = '?' ;
  202.   xchr[64] = '@' ;
  203.   xchr[65] = 'A' ;
  204.   xchr[66] = 'B' ;
  205.   xchr[67] = 'C' ;
  206.   xchr[68] = 'D' ;
  207.   xchr[69] = 'E' ;
  208.   xchr[70] = 'F' ;
  209.   xchr[71] = 'G' ;
  210.   xchr[72] = 'H' ;
  211.   xchr[73] = 'I' ;
  212.   xchr[74] = 'J' ;
  213.   xchr[75] = 'K' ;
  214.   xchr[76] = 'L' ;
  215.   xchr[77] = 'M' ;
  216.   xchr[78] = 'N' ;
  217.   xchr[79] = 'O' ;
  218.   xchr[80] = 'P' ;
  219.   xchr[81] = 'Q' ;
  220.   xchr[82] = 'R' ;
  221.   xchr[83] = 'S' ;
  222.   xchr[84] = 'T' ;
  223.   xchr[85] = 'U' ;
  224.   xchr[86] = 'V' ;
  225.   xchr[87] = 'W' ;
  226.   xchr[88] = 'X' ;
  227.   xchr[89] = 'Y' ;
  228.   xchr[90] = 'Z' ;
  229.   xchr[91] = '[' ;
  230.   xchr[92] = '\\' ;
  231.   xchr[93] = ']' ;
  232.   xchr[94] = '^' ;
  233.   xchr[95] = '_' ;
  234.   xchr[96] = '`' ;
  235.   xchr[97] = 'a' ;
  236.   xchr[98] = 'b' ;
  237.   xchr[99] = 'c' ;
  238.   xchr[100] = 'd' ;
  239.   xchr[101] = 'e' ;
  240.   xchr[102] = 'f' ;
  241.   xchr[103] = 'g' ;
  242.   xchr[104] = 'h' ;
  243.   xchr[105] = 'i' ;
  244.   xchr[106] = 'j' ;
  245.   xchr[107] = 'k' ;
  246.   xchr[108] = 'l' ;
  247.   xchr[109] = 'm' ;
  248.   xchr[110] = 'n' ;
  249.   xchr[111] = 'o' ;
  250.   xchr[112] = 'p' ;
  251.   xchr[113] = 'q' ;
  252.   xchr[114] = 'r' ;
  253.   xchr[115] = 's' ;
  254.   xchr[116] = 't' ;
  255.   xchr[117] = 'u' ;
  256.   xchr[118] = 'v' ;
  257.   xchr[119] = 'w' ;
  258.   xchr[120] = 'x' ;
  259.   xchr[121] = 'y' ;
  260.   xchr[122] = 'z' ;
  261.   xchr[123] = '{' ;
  262.   xchr[124] = '|' ;
  263.   xchr[125] = '}' ;
  264.   xchr[126] = '~' ;
  265.   for (i = 127 ; i <= 255 ; i ++) xchr[i] = '?' ;
  266.   for (i = 0 ; i <= 127 ; i ++) xord[chr(i)] = 32 ;
  267.   for (i = 32 ; i <= 126 ; i ++) xord[xchr[i]] = i ;
  268.   for (i = 0 ; i < 32 ; i++) power[i] = 1 << i ;
  269.   for (i = 0; i < MAXPKCHAR; i++) {
  270.     filename[i] = NULL;
  271.     charflags[i] = 0;
  272.   }
  273.   pkloc = 0 ;
  274.   designsize = fixword(1.0) ;
  275.   numparam = 0;
  276.   widthtab[0] = heighttab[0] = depthtab[0] = italictab[0] = 0 ;
  277.   numwidth = numheight = numdepth = numitalic = 1;
  278.   smallestch = MAXPKCHAR ;
  279.   largestch = -1 ;
  280.   emwidth = 0 ;
  281. }
  282.  
  283. jumpout()
  284. {
  285.   exit(1) ;
  286. }
  287.  
  288. pkbyte(b)
  289.      integer b ;
  290. {
  291.   if (b < 0) b = b + 256 ;
  292.   putc(b, pkfile) ;
  293.   pkloc++ ;
  294. }
  295.  
  296. pkhalfword(a)
  297.      integer a ;
  298. {
  299.   if (a < 0) a = a + 65536 ;
  300.   pkbyte(a >> 8) ;
  301.   pkbyte(a & 255) ;
  302. }
  303.  
  304. pkthreebytes(a)
  305.      integer a ;
  306. {
  307.   pkbyte((a>>16) & 255) ;
  308.   pkbyte((a>>8) & 255) ;
  309.   pkbyte(a & 255) ;
  310. }
  311.  
  312. pkword(a)
  313.      integer a ;
  314. {
  315.   pkbyte((a>>24) & 255) ;
  316.   pkbyte((a>>16) & 255) ;
  317.   pkbyte((a>>8) & 255) ;
  318.   pkbyte(a & 255) ;
  319. }
  320.  
  321. pknyb(a)
  322.      integer a ;
  323. {
  324.   if (bitweight == 16) {
  325.     outputbyte = (a<<4) ;
  326.     bitweight = 1 ;
  327.   } else {
  328.     pkbyte(outputbyte + a) ;
  329.     bitweight = 16 ;
  330.   }
  331. }
  332.  
  333. writepreamble()
  334. {
  335.   integer i ;
  336.   char *comment = "PBMtoPK 2.3 output" ;
  337.   
  338.   pkbyte(247) ;
  339.   pkbyte(89) ;
  340.   pkbyte(strlen(comment)) ;
  341.   for (i = 0 ; i < strlen(comment); i++) pkbyte(xord[comment[i]]) ;
  342.   pkword(designsize) ;
  343.   pkword(checksum) ; /* checksum; calculate if possible */
  344.   pkword(hppp) ;
  345.   pkword(hppp) ;
  346. }
  347.  
  348. writepostamble()
  349. {
  350.   pkbyte(245) ;
  351.   while ((pkloc % 4 != 0)) pkbyte(246) ;
  352.   fprintf(stderr, "%d bytes written to packed file.\n", pkloc) ;
  353. }
  354.  
  355. tfmbyte(b)
  356.      integer b ;
  357. {
  358.   if (b < 0) b = b + 256 ;
  359.   putc(b, tfmfile) ;
  360. }
  361.  
  362. tfmhalfword(a)
  363.      integer a ;
  364. {
  365.   if (a < 0) a = a + 65536 ;
  366.   tfmbyte(a >> 8) ;
  367.   tfmbyte(a & 255) ;
  368. }
  369.  
  370. tfmword(a)
  371.      integer a ;
  372. {
  373.   tfmbyte((a>>24) & 255) ;
  374.   tfmbyte((a>>16) & 255) ;
  375.   tfmbyte((a>>8) & 255) ;
  376.   tfmbyte(a & 255) ;
  377. }
  378.  
  379. writetfmfile()
  380. {
  381.   integer totallength ;
  382.   integer headersize = 17;
  383.   integer i ;
  384.  
  385.   if (smallestch < 0 || largestch >= MAXPKCHAR) {
  386.     fprintf(stderr, " No characters read!\n") ;
  387.     jumpout() ;
  388.   }
  389.   if (numparam < 7) /* set default parameters */
  390.     switch (numparam) {
  391.     case 0: /* slant */
  392.       parameters[numparam++] = 0 ;
  393.     case 1: /* space */
  394.       parameters[numparam++] = fixword(designunits(emwidth/3.0));
  395.     case 2: /* space_stretch */
  396.       parameters[numparam++] = fixword(unfixword(parameters[1])/2.0) ;
  397.     case 3: /* space_shrink */
  398.       parameters[numparam++] = fixword(unfixword(parameters[1])/3.0) ;
  399.     case 4: /* x_height */
  400.       parameters[numparam++] = fixword(0.45);
  401.     case 5: /* quad */
  402.       parameters[numparam++] = fixword(designunits(emwidth)) ;
  403.     case 6: /* extra_space */
  404.       parameters[numparam++] = fixword(unfixword(parameters[1])/3.0) ;
  405.     }
  406.   totallength = 6 + headersize + (largestch+1-smallestch) +
  407.     numwidth + numheight + numdepth + numitalic + numparam ;
  408.   /* lengths */
  409.   tfmhalfword(totallength) ;
  410.   tfmhalfword(headersize) ;
  411.   tfmhalfword(smallestch) ;
  412.   tfmhalfword(largestch) ;
  413.   tfmhalfword(numwidth) ;
  414.   tfmhalfword(numheight) ;
  415.   tfmhalfword(numdepth) ;
  416.   tfmhalfword(numitalic) ;
  417.   tfmhalfword(0) ; /* lig/kern table */
  418.   tfmhalfword(0) ; /* kern table */
  419.   tfmhalfword(0) ; /* extensible char table */
  420.   tfmhalfword(numparam) ;
  421.   /* header */
  422.   tfmword(checksum) ;
  423.   tfmword(designsize) ;
  424.   if (strlen(codingscheme) > 39) tfmbyte(39) ;
  425.   else tfmbyte(strlen(codingscheme)) ;
  426.   for (i = 0; i < 39; i++)
  427.     if (*codingscheme) tfmbyte(xord[*codingscheme++]) ;
  428.     else tfmbyte(0) ;
  429.   if (strlen(familyname) > 19) tfmbyte(19) ;
  430.   else tfmbyte(strlen(familyname)) ;
  431.   for (i = 0; i < 19; i++)
  432.     if (*familyname) tfmbyte(xord[*familyname++]) ;
  433.     else tfmbyte(0) ;
  434.   /* char_info */
  435.   for (car = smallestch; car <= largestch; car++)
  436.     if (filename[car]) {
  437.       tfmbyte(tfmindex[car]) ;
  438.       tfmbyte((hgtindex[car]<<4) + depindex[car]) ;
  439.       tfmbyte(italindex[car]<<2) ;
  440.       tfmbyte(0) ;
  441.     } else tfmword(0) ;
  442.   /* width */
  443.   for (i = 0; i < numwidth; i++) tfmword(widthtab[i]) ;
  444.   /* height */
  445.   for (i = 0; i < numheight; i++) tfmword(heighttab[i]) ;
  446.   /* depth */
  447.   for (i = 0; i < numdepth; i++) tfmword(depthtab[i]) ;
  448.   /* italic */
  449.   for (i = 0; i < numitalic; i++) tfmword(italictab[i]) ;
  450.   /* no lig_kern, kern, or exten */
  451.   /* param */
  452.   for (i = 0; i < numparam; i++)
  453.     if (i && (!fixrange(parameters[i]))) {
  454.       fprintf(stderr, " Parameter %d out of range (-p)!\n", i);
  455.       jumpout();
  456.     }
  457.     else tfmword(parameters[i]) ;
  458.   fprintf(stderr, "%d bytes written to tfm file.\n", totallength*4) ;
  459. }
  460.  
  461. readcharacter()
  462. {
  463.   FILE *fp;
  464.  
  465.   if (!strcmp(filename[car], "-")) fp = stdin;
  466.   else if ((fp = fopen(filename[car], "r")) == NULL) {
  467.     fprintf(stderr, " Can't open pbm file %s!\n", filename[car]);
  468.     jumpout();
  469.   }
  470.   bitmap = pbm_readpbm(fp, &width, &height) ;
  471.   if (fp != stdin) fclose(fp) ;
  472.   
  473.   if ((charflags[car] & HORZESC) == 0) horzesc[car] = width ;
  474.   if ((charflags[car] & VERTESC) == 0) vertesc[car] = 0;
  475.   if ((charflags[car] & XOFFSET) == 0) xoffset[car] = 0;
  476.   if ((charflags[car] & YOFFSET) == 0) yoffset[car] = height-1;
  477.   if ((charflags[car] & TFMWIDTH) == 0)
  478.     tfmindex[car] = add_tfmwidth(fixword(designunits(width)));
  479.   if ((charflags[car] & TFMHEIGHT) == 0)
  480.     hgtindex[car] = add_tfmheight(fixword(designunits(yoffset[car]+1)));
  481.   if ((charflags[car] & TFMDEPTH) == 0)
  482.     depindex[car] = add_tfmdepth(fixword(designunits(height-1-yoffset[car])));
  483.   if ((charflags[car] & TFMITALIC) == 0) italindex[car] = 0;
  484.  
  485.   if (car < smallestch) smallestch = car;
  486.   if (car > largestch) largestch = car;
  487.   if (width > emwidth) emwidth = width ;
  488. }
  489.  
  490. int
  491. equal(row1, row2)
  492.      bit *row1, *row2 ;
  493. {
  494.   integer i ;
  495.   
  496.   for (i = 0; i < width; i++)
  497.     if (row1[i] != row2[i]) return (0) ;
  498.   return(1) ;
  499. }
  500.  
  501. shipcharacter()
  502. {
  503.   integer compsize ;
  504.   integer i, j, k ;
  505.   bit *zerorow, *onesrow ;
  506.   integer *repeatptr, *bitcounts ;
  507.   integer count ;
  508.   integer test ;
  509.   integer curptr, rowptr ;
  510.   integer bitval ;
  511.   integer repeatflag ;
  512.   integer colptr ;
  513.   integer currepeat ;
  514.   integer dynf ;
  515.   integer deriv[14] ;
  516.   integer bcompsize ;
  517.   boolean firston ;
  518.   integer flagbyte ;
  519.   boolean state ;
  520.   boolean on ;
  521.   integer hbit ;
  522.   integer pbit ;
  523.   boolean ron, son ;
  524.   integer rcount, scount ;
  525.   integer ri, si ;
  526.   integer max2 ;
  527.   integer predpkloc ;
  528.   integer buff ;
  529.  
  530.   integer tfwid = widthtab[tfmindex[car]] ;
  531.   integer hesc = horzesc[car] ;
  532.   integer vesc = vertesc[car] ;
  533.   integer xoff = xoffset[car] ;
  534.   integer yoff = yoffset[car] ;
  535.  
  536.   zerorow = pbm_allocrow(width) ;
  537.   onesrow = pbm_allocrow(width) ;
  538.   repeatptr =
  539.     (integer *)malloc((unsigned int)((height+1)*sizeof(integer))) ;
  540.   bitcounts =
  541.     (integer *)malloc((unsigned int)((height*width)*sizeof(integer))) ;
  542.   for (i = 0 ; i < width ; i++) {
  543.     zerorow[i] = PBM_WHITE ;
  544.     onesrow[i] = PBM_BLACK ;
  545.   }
  546.   for (i=0; i < height; i++) {
  547.     if (equal(bitmap[i], zerorow))
  548.       repeatptr[i] = 0 ;
  549.     else if (equal(bitmap[i], onesrow)) 
  550.       repeatptr[i] = 0 ;
  551.     else if (i + 1 < height && equal(bitmap[i],bitmap[i+1]))
  552.       repeatptr[i] = 1 ;
  553.     else
  554.       repeatptr[i] = 0 ;
  555.   }
  556.   i = 0 ;
  557.   while (i < height) {
  558.     k = i ;
  559.     while (repeatptr[k] == 1) k++ ;
  560.     repeatptr[i] = k - i ;
  561.     i = k + 1 ;
  562.   }
  563.   repeatptr[i] = 0 ;
  564.   colptr = width - 1 ;
  565.   repeatflag = currepeat = curptr = count = rowptr = 0 ;
  566.   test = PBM_WHITE ;
  567.   do {
  568.     colptr++ ;
  569.     if (colptr == width) {
  570.       colptr = 0 ;
  571.       rowptr = currepeat ;
  572.       if (repeatptr[currepeat] > 0) {
  573.     repeatflag = repeatptr[currepeat] ;
  574.     currepeat += repeatflag ;
  575.     rowptr += repeatflag ;
  576.       }
  577.       currepeat++ ;
  578.     }
  579.     if (rowptr >= height) bitval = -1 ;
  580.     else bitval = bitmap[rowptr][colptr] ;
  581.     if (bitval == test) count++ ;
  582.     else {
  583.       bitcounts[curptr++] = count ;
  584.       if (curptr+3 >= height*width) {
  585.     fprintf(stderr, " Out of memory while saving character counts!\n");
  586.     jumpout() ;
  587.       }
  588.       count = 1 ;
  589.       test = bitval ;
  590.       if (repeatflag > 0) {
  591.     bitcounts[curptr++] = -repeatflag ;
  592.     repeatflag = 0 ;
  593.       }
  594.     }
  595.   } while (test != -1) ;
  596.   bitcounts[curptr] = 0 ;
  597.   bitcounts[curptr + 1] = 0 ;
  598.   for (i = 1 ; i <= 13 ; i ++) deriv[i] = 0 ;
  599.   i = firston = (bitcounts[0] == 0) ;
  600.   compsize = 0 ;
  601.   while (bitcounts[i] != 0) {
  602.     j = bitcounts[i] ;
  603.     if (j == -1) compsize++ ;
  604.     else {
  605.       if (j < 0) {
  606.     compsize++ ;
  607.     j = -j ;
  608.       }
  609.       if (j < 209) compsize += 2 ;
  610.       else {
  611.     k = j - 193 ;
  612.     while (k >= 16) {
  613.       k >>= 4 ;
  614.       compsize += 2 ;
  615.     }
  616.     compsize++ ;
  617.       }
  618.       if (j < 14) (deriv[j])-- ;
  619.       else if (j < 209) (deriv[(223 - j) / 15])++ ;
  620.       else {
  621.     k = 16 ;
  622.     while (((k<<4) < j + 3)) k <<= 4 ;
  623.     if (j - k <= 192)
  624.       deriv[(207 - j + k) / 15] += 2 ;
  625.       }
  626.     }
  627.     i++ ;
  628.   }
  629.   bcompsize = compsize ;
  630.   dynf = 0 ;
  631.   for (i = 1 ; i <= 13 ; i ++) {
  632.     compsize += deriv[i] ;
  633.     if (compsize <= bcompsize) {
  634.       bcompsize = compsize ;
  635.       dynf = i ;
  636.     }
  637.   }
  638.   compsize = ((bcompsize + 1)>>1) ;
  639.   if ((compsize > ((height*width+7)>>3)) || (height*width == 0)) {
  640.     compsize = ((height*width+7)>>3) ;
  641.     dynf = 14 ;
  642.   }
  643.   flagbyte = (dynf<<4) ;
  644.   if (firston) flagbyte |= 8 ;
  645.   if ((tfwid > 16777215) || (tfwid < 0) || (hesc < 0) || (vesc != 0) ||
  646.       (compsize > 196579) || (width > 65535) || (height > 65535) ||
  647.       (xoff > 32767) || (yoff > 32767) || (xoff < -32768) || (yoff < -32768)) {
  648.     flagbyte |= 7 ;
  649.     pkbyte(flagbyte) ;
  650.     compsize += 28 ;
  651.     pkword(compsize) ;
  652.     pkword(car) ;
  653.     predpkloc = pkloc + compsize ;
  654.     pkword(tfwid) ;
  655.     pkword(hesc<<16) ;
  656.     pkword(vesc<<16) ;
  657.     pkword(width) ;
  658.     pkword(height) ;
  659.     pkword(xoff) ;
  660.     pkword(yoff) ;
  661.   } else if ((hesc > 255) || (width > 255) || (height > 255) ||
  662.        (xoff > 127) || (yoff > 127) || (xoff < -128) ||
  663.        (yoff < -128) || (compsize > 1016)) {
  664.     compsize += 13 ;
  665.     flagbyte += (compsize>>16) + 4 ;
  666.     pkbyte(flagbyte) ;
  667.     pkhalfword(compsize & 65535) ;
  668.     pkbyte(car) ;
  669.     predpkloc = pkloc + compsize ;
  670.     pkthreebytes(tfwid) ;
  671.     pkhalfword(hesc) ;
  672.     pkhalfword(width) ;
  673.     pkhalfword(height) ;
  674.     pkhalfword(xoff) ;
  675.     pkhalfword(yoff) ;
  676.   } else {
  677.     compsize += 8 ;
  678.     flagbyte = flagbyte + (compsize>>8) ;
  679.     pkbyte(flagbyte) ;
  680.     pkbyte(compsize & 255) ;
  681.     pkbyte(car) ;
  682.     predpkloc = pkloc + compsize ;
  683.     pkthreebytes(tfwid) ;
  684.     pkbyte(hesc) ;
  685.     pkbyte(width) ;
  686.     pkbyte(height) ;
  687.     pkbyte(xoff) ;
  688.     pkbyte(yoff) ;
  689.   }
  690.   if (dynf != 14) {
  691.     bitweight = 16 ;
  692.     max2 = 208 - 15 * dynf ;
  693.     i = firston ;
  694.     while (bitcounts[i] != 0) {
  695.       j = bitcounts[i] ;
  696.       if (j == - 1) pknyb(15) ;
  697.       else {
  698.     if (j < 0) {
  699.       pknyb(14) ;
  700.       j = -j ;
  701.     }
  702.     if (j <= dynf) pknyb(j) ;
  703.     else if (j <= max2) {
  704.       j -= dynf + 1 ;
  705.       pknyb((j >> 4) + dynf + 1) ;
  706.       pknyb((j & 15)) ;
  707.     } else {
  708.       j -= max2 - 15 ;
  709.       k = 16 ;
  710.       while (k <= j) {
  711.         k <<= 4 ;
  712.         pknyb(0) ;
  713.       }
  714.       while (k > 1) {
  715.         k >>= 4 ;
  716.         pknyb(j / k) ;
  717.         j = j % k ;
  718.       }
  719.     }
  720.       }
  721.       i++ ;
  722.     }
  723.     if (bitweight != 16) pkbyte(outputbyte) ;
  724.   } else {
  725.     buff = 0 ;
  726.     pbit = 8 ;
  727.     i = firston ;
  728.     hbit = width ;
  729.     on = ! firston ;
  730.     state = false ;
  731.     count = repeatflag = 0 ;
  732.     while ((bitcounts[i] != 0) || state || (count > 0)) {
  733.       if (state) {
  734.     count = rcount ;
  735.     i = ri ;
  736.     on = ron ;
  737.     repeatflag-- ;
  738.       } else {
  739.     rcount = count ;
  740.     ri = i ;
  741.     ron = on ;
  742.       }
  743.       do {
  744.     if (count == 0) {
  745.       if (bitcounts[i] < 0) {
  746.         if (! state) repeatflag = -bitcounts[i] ;
  747.         i++ ;
  748.       }
  749.       count = bitcounts[i] ;
  750.       i++ ;
  751.       on = !on ;
  752.     }
  753.     if ((count >= pbit) && (pbit < hbit)) {
  754.       if (on) buff += power[pbit] - 1 ;
  755.       pkbyte(buff) ;
  756.       buff = 0 ;
  757.       hbit -= pbit ;
  758.       count -= pbit ;
  759.       pbit = 8 ;
  760.     } else if ((count < pbit) && (count < hbit)) {
  761.       if (on) buff += power[pbit] - power[pbit - count] ;
  762.       pbit -=  count ;
  763.       hbit -= count ;
  764.       count = 0 ;
  765.     } else {
  766.       if (on) buff += power[pbit] - power[pbit - hbit] ;
  767.       count -= hbit ;
  768.       pbit -= hbit ;
  769.       hbit = width ;
  770.       if (pbit == 0) {
  771.         pkbyte(buff) ;
  772.         buff = 0 ;
  773.         pbit = 8 ;
  774.       }
  775.     }
  776.       } while (hbit != width) ;
  777.       if (state && (repeatflag == 0)) {
  778.     count = scount ;
  779.     i = si ;
  780.     on = son ;
  781.     state = false ;
  782.       } else if (! state && (repeatflag > 0)) {
  783.     scount = count ;
  784.     si = i ;
  785.     son = on ;
  786.     state = true ;
  787.       }
  788.     }
  789.     if (pbit != 8) pkbyte(buff) ;
  790.   }
  791.   if (predpkloc != pkloc) {
  792.     fprintf(stderr, " Bad predicted character length: character %d!\n", car);
  793.     jumpout() ;
  794.   }
  795.   pbm_freerow(zerorow); 
  796.   pbm_freerow(onesrow); 
  797.   free((char *)repeatptr);
  798.   free((char *)bitcounts);
  799. }
  800.  
  801. usage()
  802. {
  803.   fprintf(stderr, " Usage: pbmtopk pkfile[.pk] tfmfile[.tfm] dpi\n") ;
  804.   fprintf(stderr, "                [-s designsize] [-p num param...]\n");
  805.   fprintf(stderr, "                [-C codingscheme ] [-F family]\n");
  806.   fprintf(stderr, "                [-c num | <char>]...\n");
  807.   fprintf(stderr, " <char> is:     [-W tfmwidth] [-H tfmheight] [-D tfmdepth]\n");
  808.   fprintf(stderr, "                [-I ital_corr] [-h horiz] [-v vert]\n") ;
  809.   fprintf(stderr, "                [-x xoffset] [-y yoffset] file\n") ;
  810.   fprintf(stderr, " or:            -f optfile\n") ;
  811.   jumpout() ;
  812. }
  813.  
  814. checkchar()
  815. {
  816.   if (car < 0 || car >= MAXPKCHAR) {
  817.     fprintf(stderr, " Character must be in range 0 to %d (-c)!\n",
  818.         MAXPKCHAR-1) ;
  819.     jumpout() ;
  820.   }
  821. }
  822.  
  823. optionfile(name)
  824.      char *name ;
  825. {
  826.   FILE *fp ;
  827.   char buffer[MAXOPTLINE] ;
  828.   
  829.   if (!strcmp(name, "-")) fp = stdin ;
  830.   else if ((fp = fopen(name, "r")) == NULL) {
  831.     fprintf(stderr, " Can't open option file %s!\n", name) ;
  832.     jumpout() ;
  833.   }
  834.   while (!feof(fp)) {
  835.     char *here = buffer;
  836.  
  837.     if (fgets(buffer, MAXOPTLINE, fp) == NULL) break ;
  838.     while (isspace(*here)) here++ ;
  839.     if (*here && *here != '%' && *here != '#') {
  840.       char str[NAMELENGTH] ;
  841.       integer i, n;
  842.       
  843.       checkchar() ;
  844.       if (sscanf(here, "%s%n", str, &n) != 1) {
  845.     fprintf(stderr, "Bad option file line %s!\n", buffer) ;
  846.     jumpout() ;
  847.       }
  848.       filename[car] =
  849.     (char *)malloc((unsigned int)(sizeof(char)*(strlen(str)+1))) ;
  850.       strcpy(filename[car], str) ;
  851.       for (i = 1; i < 256; i<<=1) {
  852.     here += n;
  853.     if (sscanf(here, "%s%n", str, &n) != 1) break ;
  854.     if (strcmp(str, "*")) {
  855.       charflags[car] |= i ;
  856.       switch (i) {
  857.       case XOFFSET:
  858.         xoffset[car] = atoi(str) ;
  859.         break ;
  860.       case YOFFSET:
  861.         yoffset[car] = atoi(str) ;
  862.         break ;
  863.       case HORZESC:
  864.         horzesc[car] = atoi(str) ;
  865.         break ;
  866.       case VERTESC:
  867.         vertesc[car] = atoi(str) ;
  868.         break ;
  869.       case TFMWIDTH:
  870.         tfmindex[car] = add_tfmwidth(fixword(atof(str))) ;
  871.         break ;
  872.       case TFMHEIGHT:
  873.         hgtindex[car] = add_tfmheight(fixword(atof(str))) ;
  874.         break ;
  875.       case TFMDEPTH:
  876.         depindex[car] = add_tfmdepth(fixword(atof(str))) ;
  877.         break ;
  878.       case TFMITALIC:
  879.         italindex[car] = add_tfmitalic(fixword(atof(str))) ;
  880.         break ;
  881.       }
  882.     }
  883.       }
  884.       car++ ;
  885.     }
  886.   }
  887.   if (fp != stdin) fclose(fp) ;
  888. }
  889.  
  890. dialog(gargc, gargv)
  891.      int gargc ;
  892.      char **gargv ;
  893. {
  894.   integer i, hesc, vesc, xoff, yoff, tfwid, tfdep, tfhgt, tfital ;
  895.   byte flags ;
  896.   
  897.   if (--gargc < 1) usage() ;
  898.   strcpy(pkname, *++gargv) ;
  899.   add_suffix(pkname, "pk") ;
  900.   
  901.   if (--gargc < 1) usage() ;
  902.   strcpy(tfmname, *++gargv) ;
  903.   add_suffix(tfmname, "tfm") ;
  904.   
  905.   if (--gargc < 1) usage() ;
  906.   resolution = atoi(*++gargv) ;
  907.   if (resolution < 1 || resolution > 32767) {
  908.     fprintf(stderr, " Unlikely resolution %d dpi!\n", resolution);
  909.     jumpout();
  910.   }
  911.   
  912.   car = flags = hesc = vesc = xoff = yoff = tfwid = 0;
  913.   while (++gargv, --gargc) {
  914.     if (gargv[0][0] == '-' && gargv[0][1]) {
  915.       char c, *p;
  916.       c = gargv[0][1] ;
  917.       if (gargv[0][2]) p = *gargv + 2 ;
  918.       else if (++gargv, --gargc) p = *gargv ;
  919.       else usage() ;
  920.       switch (c) {
  921.       case 'C':
  922.     codingscheme = p;
  923.     break ;
  924.       case 'F':
  925.     familyname = p;
  926.     break ;
  927.       case 'c':
  928.     car = atoi(p) ;
  929.     break ;
  930.       case 's':
  931.     designsize = fixword(atof(p));
  932.     if (designsize < 1048576) {
  933.       fprintf(stderr, " Design size %f out of range!\n",
  934.           unfixword(designsize));
  935.       jumpout() ;
  936.     }
  937.       case 'h':
  938.     hesc = atoi(p) ;
  939.     flags |= HORZESC ;
  940.     break ;
  941.       case 'v':
  942.     vesc = atoi(p) ;
  943.     flags |= VERTESC ;
  944.     break ;
  945.       case 'x':
  946.     xoff = atoi(p) ;
  947.     flags |= XOFFSET ;
  948.     break ;
  949.       case 'y':
  950.     yoff = atoi(p) ;
  951.     flags |= YOFFSET ;
  952.     break ;
  953.       case 'W':
  954.     tfwid = fixword(atof(p)) ;
  955.     flags |= TFMWIDTH ;
  956.     break ;
  957.       case 'H':
  958.     tfhgt = fixword(atof(p)) ;
  959.     flags |= TFMHEIGHT ;
  960.     break ;
  961.       case 'D':
  962.     tfdep = fixword(atof(p)) ;
  963.     flags |= TFMDEPTH ;
  964.     break ;
  965.       case 'I':
  966.     tfital = fixword(atof(p)) ;
  967.     flags |= TFMITALIC ;
  968.     break ;
  969.       case 'f':
  970.     optionfile(p) ;
  971.     break ;
  972.       case 'p':
  973.     numparam = atoi(p);
  974.     if (numparam < 1 || numparam > 30) {
  975.       fprintf(stderr, " Parameter count %d out of range!\n", numparam);
  976.       jumpout();
  977.     }
  978.     for (i=0; i<numparam; i++)
  979.       if (++gargv,--gargc) parameters[i] = fixword(atof(*gargv)) ;
  980.       else {
  981.         fprintf(stderr, " Not enough parameters (-p)!\n");
  982.         jumpout() ;
  983.       }
  984.     break ;
  985.       default:
  986.     usage() ;
  987.       }
  988.     } else  {
  989.       checkchar() ;
  990.       if (flags & TFMWIDTH)
  991.     tfmindex[car] = add_tfmwidth(tfwid);
  992.       if (flags & TFMDEPTH)
  993.     depindex[car] = add_tfmdepth(tfdep);
  994.       if (flags & TFMHEIGHT)
  995.     hgtindex[car] = add_tfmheight(tfhgt);
  996.       if (flags & TFMITALIC)
  997.     italindex[car] = add_tfmitalic(tfital);
  998.       horzesc[car] = hesc ;
  999.       vertesc[car] = vesc ;
  1000.       xoffset[car] = xoff ;
  1001.       yoffset[car] = yoff ;
  1002.       filename[car] = *gargv ;
  1003.       charflags[car] = flags ;
  1004.       car++ ;
  1005.       flags = 0;
  1006.     }
  1007.   }
  1008. }
  1009.  
  1010. main(argc, argv)
  1011.      int argc ;
  1012.      char *argv[] ;
  1013. {
  1014.   initialize() ;
  1015.   dialog(argc, argv) ;
  1016.   hppp = round((resolution<<16) / 72.27) ;
  1017.   add_suffix(pkname, "pk") ;
  1018.   add_suffix(tfmname, "tfm") ;
  1019.   if (!strcmp(pkname, "-")) pkfile = stdout;
  1020.   else if ((pkfile = fopen(pkname, "w")) == NULL) {
  1021.     fprintf(stderr, " Can't open PK file %s!\n", pkname);
  1022.     jumpout() ;
  1023.   }
  1024.   if (!strcmp(tfmname, "-")) tfmfile = stdout ;
  1025.   else if ((tfmfile = fopen(tfmname, "w")) == NULL) {
  1026.     fprintf(stderr, " Can't open TFM file %s!\n", tfmname);
  1027.     jumpout();
  1028.   }
  1029.   writepreamble() ;
  1030.   for (car = 0 ; car < MAXPKCHAR ; car++)
  1031.     if (filename[car]) {
  1032.       readcharacter() ;
  1033.       shipcharacter() ;
  1034.     }
  1035.   writepostamble() ;
  1036.   writetfmfile() ;
  1037.   if (pkfile != stdout) fclose(pkfile) ;
  1038.   if (tfmfile != stdout) fclose(tfmfile) ;
  1039. }
  1040.  
  1041.