home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / fermiVogle.tar.Z / fermiVogle.tar / devel / hershey / src / h2v.c < prev    next >
C/C++ Source or Header  |  1996-02-07  |  4KB  |  192 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include "h2v.h"
  4.  
  5. /*
  6.  *    Read the raw hersh font data and write the binary font files
  7.  *    as specified in h2v.h or as specified in an index file.
  8.  */
  9.  
  10. extern char    *malloc();
  11.  
  12. HTAB    hersh[MAX_CHARS];
  13.  
  14. /*
  15.  * main driver - if argc > 2 we are creating a file from an
  16.  * index, otherwise use the table in h2v.h
  17.  */
  18. main(argc, argv)
  19.     int    argc;
  20.     char    **argv;
  21. {
  22.     FILE    *fp;
  23.     FTAB    table;
  24.     int    i;
  25.     
  26.     if (argc != 2 && argc != 4) {
  27.         fprintf(stderr, "Usage: h2v datafile [indexfile fontfile]\n");
  28.         exit(1);
  29.     }
  30.     if ((fp = fopen(argv[1], "r")) == NULL) {
  31.         fprintf(stderr, "h2v: can't open hersh data file %s\n", argv[1]);
  32.         exit(1);
  33.     }
  34.  
  35.     readdata(fp);
  36.  
  37.     if (argc == 4) {
  38.         readindex(argv[2], argv[3], &table);
  39.         writefont(&table);
  40.     } else
  41.         for (i = 0; i < sizeof(fonts) / sizeof(FTAB); i++)
  42.             writefont(&fonts[i]);
  43.  
  44.     exit(0);
  45. }
  46.  
  47. /*
  48.  *  readdata
  49.  *
  50.  *  Reads the raw hersh data
  51.  */
  52. readdata(fp)
  53.     FILE    *fp;
  54. {
  55.     int    i = 0, j, x, y;
  56.     int    charno, pairs;
  57.     char    buf[MAX_BUF];
  58.     
  59.     while (getcharacter(fp, &charno, &pairs, buf)) {
  60.         hersh[charno - 1].ch = malloc(2 * pairs + 1);
  61.         strcpy(hersh[charno - 1].ch, buf);
  62.         hersh[charno - 1].len = strlen(hersh[charno - 1].ch);
  63.     }
  64.  
  65.     fclose(fp);
  66. }
  67.  
  68. /*
  69.  *  readindex
  70.  * 
  71.  *  Read an index file into index tab.
  72.  */
  73. readindex(name, fname, tab)
  74.     char    *name, *fname;
  75.     FTAB    *tab;
  76. {
  77.     
  78.     FILE    *fp;
  79.     int    i;
  80.  
  81.     if ((fp = fopen(name, "r")) == NULL) {
  82.         fprintf(stderr, "h2v: can't open index file\n");
  83.         exit(1);
  84.     }
  85.  
  86.     tab->name = fname;
  87.  
  88.     i = 0;
  89.     while (fscanf(fp, "%d %d", &tab->ent[i], &tab->ent[i + 1]) == 2)
  90.         if ((i += 2) >= MAX_ENTS - 2) {
  91.             fprintf(stderr, "h2v: indexfile to big - increase MAX_ENTS\n");
  92.             exit(1);
  93.         }
  94.     
  95.     tab->ent[i] = 0;
  96.  
  97.     fclose(fp);
  98. }
  99.  
  100. /*
  101.  * writefont
  102.  *
  103.  *    output a font to file name based on font table tab
  104.  */
  105. writefont(tab)
  106.     FTAB    *tab;
  107. {
  108.     int    l ,f, j, j1, x, y, k1, k2, k;
  109.     short    i, nchars, asdecw[3]; 
  110.     short    start, end, nvects, fd;
  111.     char    *p;
  112.     HTAB    *curch;
  113.  
  114.     fprintf(stderr, "Font name: %s\n", tab->name);
  115.  
  116. #ifdef PC
  117.     if ((fd = open(tab->name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644)) < 0) {
  118. #else
  119.     if ((fd = open(tab->name, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) {
  120. #endif
  121.         fprintf(stderr, "Can't open output file: %s\n", tab->name);
  122.         exit(1); 
  123.     }
  124.  
  125.     asdecw[0] = asdecw[2] = -100;
  126.     asdecw[1] = 100;
  127.     nvects = nchars = 0;
  128.  
  129.     lseek(fd, (long)(5 * sizeof(short)), 0); /*  Leave room for stuff at top */
  130.  
  131.     for (i = 0; (start = tab->ent[i]) != 0; i += 2) {
  132.         end = tab->ent[i + 1];
  133. #ifdef DEBUG
  134.         fprintf(stderr, "Char: %d to %d\n", start, end);
  135. #endif
  136.         do {
  137.             curch = &hersh[start - 1];
  138.             nchars++;
  139.             if (curch->ch == (char *)NULL) {
  140.                 fprintf(stderr, "h2v: character %d not available\n", start);
  141.                 exit(1);
  142.             }
  143.             asdecw[2] = MAX(asdecw[2], curch->ch[1] - curch->ch[0]);
  144.  
  145. #ifdef DEBUG
  146.             fprintf(stderr, "Char: %d length %d\n", start, curch->len);
  147. #endif
  148.             for (p = &curch->ch[2]; *p; p++) {
  149.                 x = *p++;
  150.                 if (x != ' ') {
  151.                     asdecw[0] = MAX(asdecw[0], COORD(*p));
  152.                     asdecw[1] = MIN(asdecw[1], COORD(*p));
  153.                 }
  154.             }
  155.  
  156.             nvects += curch->len / 2;
  157.             if (write(fd, &curch->len, sizeof(short)) != sizeof(short)) {
  158.                 fprintf(stderr,"h2v: ERROR writing character length to file\n");
  159.                 exit(1);
  160.             }
  161.             if (write(fd, curch->ch, (unsigned)curch->len) != curch->len) {
  162.                 fprintf(stderr,"h2v: ERROR writing character data to file\n");
  163.                 exit(1);
  164.             }
  165.             start++;
  166.         } while (start <= end);
  167.     }
  168.  
  169. #ifdef DEBUG
  170.     fprintf(stderr,"nchars: %d, nvects: %d\n", nchars, nvects);
  171.     fprintf(stderr,"ascender: %d, decender: %d maxwidth: %d\n",
  172.         asdecw[0], asdecw[1], asdecw[2]);
  173. #endif
  174.  
  175.     lseek(fd, 0L, 0);
  176.     if (write(fd, &nchars, sizeof(nchars)) != sizeof(nchars)) {
  177.         fprintf(stderr,"Error writing to file\n");
  178.         exit(1);
  179.     }
  180.  
  181.     if (write(fd, &nvects, sizeof(nvects)) != sizeof(nvects)) {
  182.         fprintf(stderr,"Error writing to file\n");
  183.         exit(1);
  184.     }
  185.     if (write(fd, asdecw, sizeof(asdecw)) != sizeof(asdecw)) {
  186.         fprintf(stderr, "Error writing to file\n");
  187.         exit(1);
  188.     }
  189.  
  190.     close(fd);
  191. }
  192.