home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_200 / 287_03 / tex2grad.c < prev    next >
Text File  |  1989-05-25  |  8KB  |  247 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <fcntl.h>
  4. #include <sys\types.h>
  5. #include <io.h>
  6. #include <sys\stat.h>
  7.  
  8. /*==============================================================*
  9.  *                                                              *
  10.  *    This is a program to convert a TEX pixel file (1002 PXL   *
  11.  *    file) to a GRAD font file. Input and output file name     *
  12.  *    can be the same but not recommended.                      *
  13.  *                                                              *
  14.  *    Character position 32 (0x20, ' ') in GRAD is forced to    *
  15.  *    be a space and width equal to 10/16 of the design size.   *
  16.  *    If it is a TeleType (TT) font, you may have to change     *
  17.  *    the width of the space character to become the same as    *
  18.  *    other characters. The magnification field (the last but   *
  19.  *    7 to last but 4 bytes) must contain the number of dot per *
  20.  *    inch the pixel file is for times 5. For 300dpi pixel      *
  21.  *    the field should contain 1500.                            *
  22.  *                                                              *
  23.  *    If you know about TEX pixel file, you should not have     *
  24.  *    any problem understanding the above paragraph.            *
  25.  *    If you don't know about TEX, it is no use looking at      *
  26.  *    this program.                                             *
  27.  *                                                              *
  28.  *==============================================================*/
  29.  
  30. #define TRUE 1
  31. #define FALSE 0
  32. #define ERROR (-1)
  33.  
  34. #define TOTAL_PATSIZE 32768     /* Maximum pattern size for TEX font file */
  35.  
  36. long get4();
  37. int get2();
  38.  
  39. int infile, outfile;
  40.  
  41. char infilename[64];    /* store input file name */
  42. char outfilename[64];   /* store output file name */
  43.  
  44. unsigned char pattern[TOTAL_PATSIZE];
  45.  
  46. long pxlid,dirloc,designsize,mag,checksum,designwidth;
  47. unsigned char designheight;
  48.  
  49. int dir_pixwid[128], dir_pixhgt[128];
  50. int dir_leftmargin[128], dir_topline[128];
  51. long dir_ptr[128];
  52. long dir_tfm[128];
  53.  
  54. int dir_bpr[128];
  55.  
  56. unsigned char buffer[32];
  57.  
  58. main(argc, argv)
  59.  
  60. int argc;
  61. char *argv[];
  62.  
  63. {
  64.         int loop1, total, dpi;
  65.         long pos;
  66.  
  67.  
  68.         if (argc!=3) {
  69.             printf("Usage: tex2grad <input-PXL> [<output-FON>]\n");
  70.             exit(1);
  71.         }
  72.  
  73.         strcpy(infilename,argv[1]);
  74.         strcpy(outfilename,argv[2]);
  75.  
  76.         if ((infile=open(infilename,O_RDONLY+O_BINARY)) == ERROR) {
  77.              printf("Can't open input PXL file '%s' for reading.\n",infilename);
  78.              exit(1);
  79.           }
  80.  
  81.         printf("Reading packed PXL file '%s'.\n",infilename);
  82.  
  83. /* Begin by checking the pxlIDs. */
  84.         pxlid = get4(infile, 0L, SEEK_SET );
  85.  
  86.         if (pxlid!=1002) {
  87.             printf("Invalid PXL-ID in PXL file, should be 1002: %ld\n",pxlid);
  88.             exit(1);
  89.         }
  90.  
  91.         pxlid = get4(infile, -4L, SEEK_END );
  92.  
  93.         if (pxlid!=1002) {
  94.             printf("Invalid PXL-ID at end of PXL file, should be 1002: %ld\n"
  95.                       ,pxlid);
  96.             exit(1);
  97.         }
  98.  
  99. /* Get the rest of the "trailer" information */
  100.         dirloc = get4(infile, -8L,  SEEK_END); 
  101.         if (dirloc>TOTAL_PATSIZE) {
  102.             printf("Requires %ld bytes for pattern information\n",dirloc);
  103.             exit(1);
  104.         }
  105.         designsize = get4(infile, -12L, SEEK_END);
  106.         mag = get4(infile, -16L, SEEK_END); 
  107.         dpi = mag / 5;
  108.         designwidth=((designsize>>4) * dpi / 0x48452L);
  109.         designheight=(unsigned char) ((designwidth + 0x08) >> 4);
  110.         checksum = get4(infile, -20L, SEEK_END); 
  111.         printf("dirloc=%ld, designsize=%ld, mag=%ld, checksum=%ld\n"
  112.                     ,dirloc, designsize, mag, checksum);
  113.         pos = dirloc;
  114.         for (loop1=0; loop1<=127; loop1++) {
  115.             dir_pixwid[loop1] = get2(infile, pos+0, SEEK_SET);
  116.             dir_pixhgt[loop1] = get2(infile, pos+2, SEEK_SET);
  117.             dir_leftmargin[loop1] = -get2(infile, pos+4, SEEK_SET);
  118.             if ((dir_topline[loop1] = get2(infile, pos+6, SEEK_SET)) < 0) {
  119.                 dir_topline[loop1] = 0;
  120.             }
  121.             dir_ptr[loop1] = get4(infile, pos+8, SEEK_SET);
  122.             dir_tfm[loop1] = get4(infile, pos+12, SEEK_SET);
  123.  
  124.             if (dir_ptr[loop1]==0) {
  125.                 dir_bpr[loop1]=0;
  126.             } else {
  127.                 dir_bpr[loop1]=(dir_pixwid[loop1] + 7) >> 3;
  128.             }
  129. /*            printf("loop1=%d, wid=%d, hgt=%d, ptr=%ld\n"
  130.                  ,loop1,dir_pixwid[loop1],dir_pixhgt[loop1],dir_ptr[loop1]); */
  131.             pos+=16;
  132.         }
  133.  
  134. /* Next read in the pattern information */
  135.  
  136.         get(infile, 0L, SEEK_SET, pattern, dirloc);
  137.         close(infile);
  138.  
  139. /* OK.  Now write the unpacked output file */
  140.  
  141.         if ((outfile=open(outfilename,O_WRONLY+O_CREAT+O_TRUNC+O_BINARY,
  142.                             S_IREAD | S_IWRITE))
  143.                 == ERROR) {
  144.             printf("Can't open FON file '%s' for writing.\n",outfilename);
  145.             exit(1);
  146.             }
  147.                                       
  148.         printf("Writing data to FON file '%s'.\n",outfilename);
  149.  
  150.         buffer[0]=51;
  151.         for (loop1=1; loop1<15; loop1++) buffer[loop1]=' ';
  152.         for (loop1=15; loop1<32; loop1++) buffer[loop1]=0;
  153.         buffer[16]=1;
  154.         buffer[17]=128;
  155.         buffer[19]=0;
  156.         buffer[24]=0x7f;
  157.         write(outfile, buffer, 32);
  158.  
  159.         for (loop1=0; loop1<=127; loop1++) {
  160.             if (loop1==' ') {   /* force character 0x20 as a space */
  161.                                 /* width is 9/16 of design size    */
  162.                 buffer[0]=(unsigned char ) ((designwidth*9+128) >> 8);
  163.                 buffer[1]=buffer[2]=buffer[3]=buffer[4]=0;
  164.                 dir_ptr[loop1]=0;
  165.             } else {
  166.                 buffer[0]=(unsigned char)
  167.                         ((designwidth * (dir_tfm[loop1] >> 4) + 0x80000) >> 20);
  168.                 buffer[1]=dir_pixwid[loop1];
  169.                 buffer[2]=dir_pixhgt[loop1];
  170.                 buffer[3]=dir_leftmargin[loop1];
  171.                 buffer[4]=dir_topline[loop1];
  172.             }
  173.             buffer[5]=designheight;
  174.             write(outfile, buffer, 6);
  175.         }
  176.         for (loop1=0; loop1<=127; loop1++) {
  177.             pos = dir_ptr[loop1];
  178.             if (pos==0) continue;
  179.  
  180.             total = dir_bpr[loop1] * dir_pixhgt[loop1];
  181.             if (write(outfile,&pattern[pos],total) != total) {
  182.                 printf("Write error\n");
  183.                 exit(1);
  184.             }
  185.         }
  186.  
  187.         close(outfile);
  188.  
  189. }
  190.  
  191.  
  192. /* 
  193.   Here is a function to read data from arbitrary position in a PXL file.
  194. */
  195. get(fd, pos, origin, buf, n)
  196. int fd, n, origin;
  197. long pos; 
  198. char *buf;
  199. {
  200.         int rc;
  201.  
  202.         lseek(fd, pos, origin);  /* get to proper position */
  203.         if ( (rc = read(fd,buf,n)) < n ) {
  204.             printf("Bad formatted PXL file at %d. Program aborted.\n",rc);
  205.             exit(1);
  206.         }
  207. }
  208.  
  209. long getn( fd, n )
  210. int fd;
  211. int n;
  212. {
  213.         long num;
  214.         char c;
  215.  
  216.         num = 0;
  217.         while( n-- ) {
  218.             if( read( fd, &c, 1 ) < 1 ) {
  219.                 printf( "Bad PXL file.  Program aborted!\n" );
  220.                 exit(1);
  221.             }
  222.             num <<= 8;
  223.             num |= c;
  224.         }
  225.  
  226.  
  227.         return( num );
  228. }
  229.  
  230. long get4( fd, pos, origin )
  231. int fd, origin;
  232. long pos;
  233. {
  234.         lseek(fd, pos, origin);  /* get to proper position */
  235.         return( getn( fd, 4 ) );
  236. }
  237.  
  238. get2( fd, pos, origin )
  239. int fd, origin;
  240. long pos;
  241. {
  242.         lseek(fd, pos, origin);  /* get to proper position */
  243.         return( (int) getn( fd, 2 ) );
  244. }
  245.  
  246.  
  247.