home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / GLEN / FONTFI.ZIP / FFILTERS.H < prev    next >
Text File  |  1987-10-14  |  5KB  |  226 lines

  1. /**********************************
  2.  * FFILTERS.H
  3.  *
  4.  * Basic Font Filtering Scheme
  5.  *
  6.  * James Bumgardner 1987
  7.  ***********************************/
  8.  
  9. #define SIZE_LIMIT    152
  10. #define ESC        27
  11.  
  12. struct {
  13.     int fil26;
  14.     char nul1,font_type;
  15.     int nul2,base_line,width,height;
  16.     char orient,fixed;
  17.     int symset,pitch,points,nul3;
  18.     char nul4,style,weight,type_face;
  19. } font_header;
  20.  
  21. struct {
  22.     char fil4,nul1,fil14,cil1;
  23.     char orient,nul2;
  24.     int left_offset,top_offset,width,height,delta_x;
  25. } char_header;
  26.  
  27. char ibuf[2000],*bitmap = &ibuf[sizeof(char_header)];
  28.  
  29. char pmap[2][SIZE_LIMIT][SIZE_LIMIT];
  30.  
  31. FILE *ifile,*ofile;
  32.  
  33. main(argc,argv)
  34. char *argv[];
  35. {
  36.     int total_bytes,horiz_bytes,true_width,arg;
  37.  
  38.     if (argc < 3)
  39.         erxit("Please Specify an Input and Output File");
  40.  
  41.     if (argc >= 4)    arg = atoi(argv[3]);
  42.     else        arg = 0;    
  43.     
  44.     if ((ifile = fopen(argv[1],"r")) == NULL)
  45.         erxit("Can't open Input File\n");
  46.     if ((ofile = fopen(argv[2],"w")) == NULL)
  47.         erxit("Can't open Output File\n");
  48.  
  49.     if ((total_bytes = get_font_header()) == EOF)
  50.         erxit("Invalid Font File");
  51.     process_font_header(arg);
  52.     write_font_header(total_bytes);
  53.         
  54.     printf("Processing  ");
  55.     while ((total_bytes = get_char_header()) != EOF)
  56.     {
  57.         fix_header();
  58.  
  59.         horiz_bytes = (total_bytes-sizeof(char_header))/char_header.height;
  60.  
  61.         translate_bitmap(char_header.height,horiz_bytes);
  62.  
  63.         true_width = horiz_bytes << 3;
  64.         process_char(char_header.height,true_width,arg);
  65.  
  66.         horiz_bytes = (char_header.width-1 >> 3)+1;
  67.         translate_pmap(char_header.height,horiz_bytes);
  68.  
  69.         total_bytes = sizeof(char_header)+ horiz_bytes * char_header.height;
  70.         unfix_header();
  71.         fprintf(ofile,"\033(s%dW",total_bytes);
  72.         fwrite(ibuf,total_bytes,1,ofile);
  73.     }
  74. }
  75.  
  76. translate_bitmap(h,w)
  77. {
  78.     int y,x;
  79.     char *p,*op;
  80.     unsigned char byt,bit;
  81.     p = bitmap;
  82.     for (y = 0; y < h; ++y) {
  83.         op = pmap[0][y];
  84.         for (x = 0; x < w; ++x) {
  85.             byt = *p++;
  86.             for (bit = 0x80; bit; bit >>= 1)
  87.             {
  88.                 if (byt & bit)        *op++ = 1;
  89.                 else            *op++ = 0;
  90.             }
  91.         }
  92.     }
  93. }
  94.  
  95. translate_pmap(h,w)
  96. {
  97.     int y,x,rx;
  98.     char *p;
  99.     unsigned char byt,bit;
  100.     p = bitmap;
  101.     for (y = 0; y < h; ++y) {
  102.         rx = 0;
  103.         for (x = 0; x < w; ++x) {
  104.             byt = 0;
  105.             for (bit = 0x80; bit; bit >>= 1)
  106.             {
  107.                 if (pmap[1][y][rx])    byt |= bit;
  108.                 ++rx;
  109.             }
  110.             *p++ = byt;
  111.         }
  112.  
  113.     }
  114. }
  115.  
  116. get_font_header()
  117. {
  118.     unsigned int tmp;
  119.     int c;
  120.     char *p;
  121.     
  122.     while ((c = getc(ifile)) != EOF && c != ESC)
  123.         ;
  124.     if (c == EOF)
  125.         erxit("Invalid Font File");
  126.     tmp = getw(ifile);
  127.  
  128.     if (tmp == ')s'    )
  129.     {
  130.         tmp = get_number();
  131.         fread(ibuf,tmp,1,ifile);
  132.         memcpy(&font_header,ibuf,sizeof(font_header));
  133.         font_header.base_line = iswap(font_header.base_line);
  134.         font_header.width = iswap(font_header.width);
  135.         font_header.height = iswap(font_header.height);
  136.         if (font_header.width > SIZE_LIMIT ||
  137.             font_header.height > SIZE_LIMIT)
  138.         {
  139.             fprintf(stderr,"Characters are too big\n");
  140.             return(EOF);
  141.         }
  142.         return(tmp);
  143.     }
  144.     return(EOF);    
  145. }
  146.  
  147. write_font_header(t)
  148. {
  149.     fprintf(ofile,"\033)s%dW",t);
  150.     font_header.base_line = iswap(font_header.base_line);
  151.     font_header.width = iswap(font_header.width);
  152.     font_header.height = iswap(font_header.height);
  153.     memcpy(ibuf,&font_header,sizeof(font_header));
  154.     fwrite(ibuf,t,1,ofile);
  155. }
  156.  
  157. get_char_header()
  158. {
  159.     int tmp;
  160.     int c,cno;
  161.     while ((c = getc(ifile)) != EOF && c != ESC)
  162.         ;
  163.     if (c == EOF)        return(EOF);
  164.     tmp = getw(ifile);
  165.  
  166.     if (tmp != '*c')    return(EOF);
  167.     cno = get_number();
  168.  
  169.     printf("\b%c",cno);
  170.     fprintf(ofile,"\033*c%dE",cno);
  171.  
  172.     c = getc(ifile);        /* skip escape */
  173.     tmp = getw(ifile);
  174.     if (tmp != '(s')    return(EOF);
  175.  
  176.     tmp = get_number();
  177.  
  178.     fread(ibuf,tmp,1,ifile);
  179.     return(tmp);
  180. }
  181.  
  182. fix_header()
  183. {
  184.     memcpy(&char_header,ibuf,sizeof(char_header));
  185.     char_header.width = iswap(char_header.width);
  186.     char_header.height = iswap(char_header.height);
  187.     char_header.delta_x = iswap(char_header.delta_x);
  188.     char_header.left_offset = iswap(char_header.left_offset);
  189.     char_header.top_offset = iswap(char_header.top_offset);
  190. }
  191.  
  192. unfix_header()
  193. {
  194.     char_header.width = iswap(char_header.width);
  195.     char_header.height = iswap(char_header.height);
  196.     char_header.delta_x = iswap(char_header.delta_x);
  197.     char_header.left_offset = iswap(char_header.left_offset);
  198.     char_header.top_offset = iswap(char_header.top_offset);
  199.     memcpy(ibuf,&char_header,sizeof(char_header));
  200. }
  201.  
  202. iswap(n)
  203. unsigned int n;
  204. {
  205.     return(n >> 8 | n << 8);
  206. }
  207.  
  208. get_number()
  209. {
  210.     char *p;
  211.     int c;
  212.     p = ibuf;
  213.     while ((c = getc(ifile)) != EOF && isdigit(c))
  214.         *p++ = c;
  215.     *p = 0;
  216.     return(atoi(ibuf));
  217. }
  218.  
  219. erxit(msg)
  220. char *msg;
  221. {
  222.     fprintf(stderr,msg);
  223.     exit();
  224. }
  225.  
  226.