home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume3 / bdftools / part01 / bdf2gf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-07  |  3.6 KB  |  164 lines

  1. #include    <stdio.h>
  2. #include    <sys/types.h>
  3.  
  4. char        fontname[100];
  5. int        pointsize, xres, yres;
  6. int        bbx, bby, offx, offy;
  7. int        nchars;
  8. u_char        *bitmaps[256] = { 0 };
  9.  
  10. fatal(m)
  11.     char        *m;
  12. {
  13.     (void)fprintf(stderr, "%s\n", m);
  14.     exit(1);
  15. }
  16.  
  17. char *xfgets(s, max, f)
  18.     char        *s;
  19.     int        max;
  20.     FILE        *f;
  21. {
  22.     register char    *r;
  23.  
  24.     do
  25.     {
  26.         if ((r = fgets(s, max, f)) == NULL)
  27.             break;
  28.     } while (strncmp("COMMENT ", s, 8) == 0);
  29.     return (r);
  30. }
  31.  
  32. /*
  33. **    Read line in and match against s.
  34. **    If mustmatch and none found, exit.
  35. **    If !mustmatch and none found, return 0.
  36. **    Else return 1.
  37. */
  38. int match(s, line, max, mustmatch)
  39.     char        *s, *line;
  40.     int        max, mustmatch;
  41. {
  42.     int        len = strlen(s);
  43.  
  44.     if (xfgets(line, max, stdin) == NULL)
  45.         fatal("Unexpected EOF on input");
  46.     if (strncmp(line, s, len) != 0)
  47.     {
  48.         if (mustmatch)
  49.         {
  50.             (void)fprintf(stderr, "Expected %s, got %s", s, line);
  51.             exit(1);
  52.         }
  53.         else
  54.             return (0);
  55.     }
  56.     return (1);
  57. }
  58.  
  59. readproperties(line, max)
  60.     char        *line;
  61.     int        max;
  62. {
  63.     int        nprops;
  64.  
  65.     if (sscanf(line, "STARTPROPERTIES %d", &nprops) != 1)
  66.         fatal("No property count");
  67.     while (nprops-- > 0)
  68.         if (xfgets(line, max, stdin) == NULL)
  69.             fatal("Unexpected EOF on input reading properties");
  70.     (void)match("ENDPROPERTIES", line, max, 1);
  71. }
  72.  
  73. u_char *readbitmap(line, max, width, height)
  74.     char        *line;
  75.     int        max;
  76.     int        width, height;
  77. {
  78.     register int    i, j, bytewidth;
  79.     register u_char    *p, *q;
  80.     unsigned    byte;
  81.     char        *malloc();
  82.  
  83.     (void)match("BITMAP", line, max, 1);
  84.     bytewidth = (width + 7) / 8;
  85.     if ((q = p = (u_char *)malloc(bytewidth * height)) == NULL)
  86.         fatal("Cannot allocate bitmap storage");
  87.     for (i = 0; i < height; ++i)
  88.     {
  89.         if (xfgets(line, max, stdin) == NULL)
  90.             fatal("Unexpected EOF on input reading bitmap");
  91.         for (j = 0; j < bytewidth; ++j)
  92.         {
  93.             if (sscanf(&line[j*2], "%02x", &byte) != 1)
  94.                 fatal("Cannot read hex pair");
  95.             *q++ = byte & 0xff;
  96.         }
  97.     }
  98.     (void)match("ENDCHAR", line, max, 1);
  99.     return (p);
  100. }
  101.  
  102. read1char(line, max)
  103.     char        *line;
  104.     int        max;
  105. {
  106.     int        charnum;
  107.     int        width, bbx, bby, offx, offy;
  108.     float        dx, dy;
  109.  
  110.     (void)match("STARTCHAR", line, max, 1);
  111.     (void)match("ENCODING", line, max, 1);
  112.     if (sscanf(line, "ENCODING %d", &charnum) != 1)
  113.         fatal("No position number for character");
  114.     (void)match("SWIDTH", line, max, 1);
  115.     if (sscanf(line, "SWIDTH %g %g", &dx, &dy) != 2)
  116.         fatal("No scalable width info for character");
  117.     (void)match("DWIDTH", line, max, 1);
  118.     if (sscanf(line, "DWIDTH %d", &width) != 1)
  119.         fatal("No width info for character");
  120.     (void)match("BBX", line, max, 1);
  121.     if (sscanf(line, "BBX %d %d %d %d", &bbx, &bby, &offx, &offy) != 4)
  122.         fatal("Insufficient bounding box info for character");
  123.     bitmaps[charnum % 0x100] = readbitmap(line, max, bbx, bby);
  124.     storeglyph(charnum, bbx, bby, offx, offy, dx, dy);
  125. }
  126.  
  127. readchars(line, max)
  128.     char        *line;
  129.     int        max;
  130. {
  131.     (void)match("CHARS", line, max, 1);
  132.     if (sscanf(line, "CHARS %d", &nchars) != 1)
  133.         fatal("No count of chars");
  134.     while (nchars-- > 0)
  135.         read1char(line, max);
  136. }
  137.  
  138. main(argc, argv)
  139.     int        argc;
  140.     char        *argv[];
  141. {
  142.     char        line[512];
  143.  
  144.     initialize(argc, argv);
  145.  
  146.     (void)match("STARTFONT 2.1", line, sizeof(line), 1);
  147.     while (!match("FONT", line, sizeof(line), 0))
  148.         ;
  149.     if (sscanf(line, "FONT %s", fontname) != 1)
  150.         fatal("No font name");
  151.     (void)match("SIZE", line, sizeof(line), 1);
  152.     if (sscanf(line, "SIZE %d %d %d", &pointsize, &xres, &yres) != 3)
  153.         fatal("Insufficient size info");
  154.     (void)match("FONTBOUNDINGBOX", line, sizeof(line), 1);
  155.     if (sscanf(line, "FONTBOUNDINGBOX %d %d %d %d", &bbx, &bby,
  156.         &offx, &offy) != 4)
  157.         fatal("Insufficient bounding box info");
  158.     if (match("STARTPROPERTIES", line, sizeof(line), 0))
  159.         readproperties(line, sizeof(line));
  160.     readchars(line, sizeof(line));
  161.     dumpfont(bitmaps);
  162.     exit(0);
  163. }
  164.