home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume2 / ditdvi / fontfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-07  |  4.3 KB  |  225 lines

  1. #include    <stdio.h>
  2. #include    "ditdvi.h"
  3.  
  4. /*
  5. **    Routines to read ditroff DESC file.
  6. */
  7.  
  8. tdev    dev;
  9. int    halfwidth;
  10. short    *pstab;
  11. lookup    *specialtab;
  12. fontdes    fonts[NFONTS];
  13.  
  14. static char *smalloc(n)
  15.     int        n;
  16. /*
  17. **    Get memory, or die
  18. */
  19. {
  20.     register char    *p;
  21.     char        *malloc();
  22.  
  23.     if ((p = malloc(n)) == NULL)
  24.         fatal("No more memory\n");
  25.     return (p);
  26. }
  27.  
  28. int readfontdir()
  29. /*
  30. **    Read in the information from the DESC file, storing it in
  31. **    the font info array.
  32. */
  33. {
  34.     register int    i;
  35.     register char    *filedata, *chname;
  36.     register short    *chtab;
  37.     register FILE    *f;
  38.     char        fname[100];
  39.     char        *sprintf();
  40.  
  41.     (void)sprintf(fname, "%s/dev%s/DESC.out", FONTDIR, engine.s_ts_name);
  42.     if ((f = fopen(fname, "r")) == NULL)
  43.     {
  44.         perror(fname);
  45.         return (0);
  46.     }
  47.     if (fread((char *)&dev, sizeof(char), sizeof(tdev), f) != sizeof(tdev))
  48.         return (0);
  49.     halfwidth = dev.unitwidth / 2;
  50.     if (dev.nfonts >= NFONTS)
  51.         fatal("Font capacity of %d fonts exceeded, reconfigure\n", NFONTS - 1);
  52.     if (dev.nsizes >= NSIZES)
  53.         fatal("Pointsize capacity of %d sizes exceeded, reconfigure\n", NSIZES);
  54.  
  55.     for (i = 0; i < NFONTS; ++i)        /* zero descriptors */
  56.     {
  57.         register fontdes    *f;
  58.         register int        j;
  59.  
  60.         f = &fonts[i];
  61.         f->tfontp = (tfont *)0;
  62.         f->codetab = f->fitab = (uchar *)0;
  63.         for (j = 0; j < NSIZES; ++j)
  64.             f->texfont[j] = -1;    /* initially unloaded */
  65.     }
  66.  
  67.     filedata = smalloc(dev.filesize);
  68.     if (fread(filedata, sizeof(char), dev.filesize, f) != dev.filesize)
  69.         fatal("Short read on %s\n", fname);
  70.     (void)fclose(f);
  71.     pstab = (short *)filedata;
  72.     filedata += (dev.nsizes + 1) * sizeof(short);
  73.     chtab = (short *)filedata;
  74.     filedata += dev.nchtab * sizeof(short);
  75.     chname = filedata;
  76.     filedata += dev.lchname;
  77.     makespecialtab(dev.nchtab, chname, chtab);
  78. #ifdef    DEBUG_FONTS
  79.     dumptables();
  80. #endif    DEBUG_FONTS
  81.     for (i = 1; i <= dev.nfonts; i++)
  82.     {
  83.         register int        nw;
  84.  
  85.         fonts[i].tfontp = (tfont *)filedata;
  86.         nw = fonts[i].n_width;
  87.         filedata += sizeof(tfont);
  88.         fonts[i].widtab = (uchar *)filedata;
  89.         filedata += nw;
  90.         fonts[i].kerntab = (uchar *)filedata;
  91.         filedata += nw;
  92.         fonts[i].codetab = (uchar *)filedata;
  93.         filedata += nw;
  94.         fonts[i].fitab = (uchar *)filedata;
  95.         filedata += dev.nchtab + (128 - 32);
  96.     }
  97. #ifdef    DEBUG_FONTS
  98.     dumpfonts();
  99. #endif    DEBUG_FONTS
  100.     return (1);
  101. }
  102.  
  103. static int speccmp(l1, l2)
  104.     lookup        *l1, *l2;
  105. {
  106.     return (strcmp(l1->chname, l2->chname));
  107. }
  108.  
  109. makespecialtab(nspecials, strings, offsets)
  110.     int        nspecials;
  111.     char        *strings;
  112.     short        offsets[];
  113. {
  114.     register int    i;
  115.  
  116.     specialtab = (lookup *)smalloc(nspecials * sizeof(lookup));
  117.     for (i = 0; i < nspecials; ++i)
  118.     {
  119.         specialtab[i].ordinal = i;
  120.         specialtab[i].chname = strings + offsets[i];
  121.     }
  122.     qsort((char *)specialtab, nspecials, sizeof(lookup), speccmp);
  123. #ifdef    DEBUG_FONTS
  124.     for (i = 0; i < nspecials; ++i)
  125.         printf("%s %d\n", specialtab[i].chname, specialtab[i].ordinal);
  126. #endif    DEBUG_FONTS
  127. }
  128.  
  129. #ifdef    DEBUG_FONTS
  130. static dumptables()
  131. /*
  132. **    Print font size tables
  133. */
  134. {
  135.     register int        i;
  136.  
  137.     printf("Point size table\n");
  138.     for (i = 0; i <= dev.nsizes; ++i)
  139.     {
  140.         printf("%3d", pstab[i]);
  141.         if (i % 20 == 19)
  142.             putchar('\n');
  143.         else
  144.             putchar(' ');
  145.     }
  146.     putchar('\n');
  147.     printf("Special chars\n");
  148.     for (i = 0; i < dev.nchtab; ++i)
  149.     {
  150.         printf("%s", &chname[chtab[i]]);
  151.         if (i % 20 == 19)
  152.             putchar('\n');
  153.         else
  154.             putchar(' ');
  155.     }
  156.     putchar('\n');
  157. }
  158.  
  159. static dumpfonts()
  160. /*
  161. **    Print out a human readable representation of fonts for
  162. **    debugging purposes.
  163. */
  164. {
  165.     register int        i;
  166.  
  167.     for (i = 1; i < NFONTS; ++i)
  168.     {
  169.         if (fonts[i].tfontp != NULL)
  170.             dumponefont(i);
  171.     }
  172. }
  173.  
  174. static dumponefont(n)
  175.     int            n;
  176. /*
  177. **    Say all we know about this font
  178. */
  179. {
  180.     register tfont        *tf;
  181.     register uchar        *t;
  182.     register int        i, nw;
  183.  
  184.     tf = fonts[n].tfontp;
  185.     printf("%s (%s), %d entries,%s%s\n",
  186.         tf->namefont, tf->intname, (int)tf->nwfont,
  187.         tf->specfont ? " special" : " ",
  188.         tf->ligfont ? " ligatures" : " ");
  189.     nw = tf->nwfont;
  190.     t = fonts[n].widtab;
  191.     printf("Width table\n");
  192.     for (i = 0; i < nw; ++i)
  193.     {
  194.         printf("%3d", t[i]);
  195.         if (i % 20 == 19)
  196.             putchar('\n');
  197.         else
  198.             putchar(' ');
  199.     }
  200.     putchar('\n');
  201.     t = fonts[n].codetab;
  202.     printf("Code table\n");
  203.     for (i = 0; i < nw; ++i)
  204.     {
  205.         printf("%3d", t[i]);
  206.         if (i % 20 == 19)
  207.             putchar('\n');
  208.         else
  209.             putchar(' ');
  210.     }
  211.     putchar('\n');
  212.     t = fonts[n].fitab;
  213.     printf("Font index table\n");
  214.     for (i = 0; i < dev.nchtab; ++i)
  215.     {
  216.         printf("%3d", t[i]);
  217.         if (i % 20 == 19)
  218.             putchar('\n');
  219.         else
  220.             putchar(' ');
  221.     }
  222.     putchar('\n');
  223. }
  224. #endif    DEBUG_FONTS
  225.