home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d7xx / d795 / pstools.lha / PSTools / PSTools1.lha / source / virtualfont.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-14  |  6.5 KB  |  271 lines

  1. /*
  2.  *   Here's the code to load a VF file into memory.
  3.  *   Any resemblance between this file and loadfont.c is purely uncoincidental.
  4.  */
  5. #include "structures.h" /* The copyright notice in that file is included too! */
  6. /*
  7.  *   These are the external routines we use.
  8.  */
  9. extern void makefont() ;
  10. extern void error() ;
  11. extern integer scalewidth() ;
  12. extern FILE *search() ;
  13. extern fontdesctype *newfontdesc() ;
  14. extern fontdesctype *matchfont() ;
  15. /*
  16.  *   These are the external variables we use.
  17.  */
  18. #ifdef DEBUG
  19. extern integer debug_flag;
  20. #endif  /* DEBUG */
  21. extern long bytesleft ;
  22. extern quarterword *raster ;
  23. extern char *vfpath ;
  24. extern char errbuf[200] ;
  25. extern real conv ;
  26. extern real vconv ;
  27. extern real alpha ;
  28. extern integer mag ;
  29. extern int actualdpi ;
  30. extern int vactualdpi ;
  31. extern char *nextstring, *maxstring ;
  32. extern fontdesctype *fonthead ;
  33. extern real alpha ;
  34.  
  35. /*
  36.  *   Now we have some routines to get stuff from the VF file.
  37.  *   Subroutine vfbyte returns the next byte.
  38.  */
  39.  
  40. static FILE *vffile ;
  41. static char name[50] ;
  42. void
  43. badvf(s)
  44.    char *s ;
  45. {
  46.    (void)sprintf(errbuf,"! Bad VF file %s: %s",name,s) ;
  47.    error(errbuf);
  48. }
  49.  
  50. shalfword
  51. vfbyte()
  52. {
  53.    register shalfword i ;
  54.  
  55.    if ((i=getc(vffile))==EOF)
  56.       badvf("unexpected eof") ;
  57.    return(i) ;
  58. }
  59.  
  60. integer
  61. vfquad()
  62. {
  63.    register integer i ;
  64.  
  65.    i = vfbyte() ;
  66.    if (i > 127)
  67.       i -= 256 ;
  68.    i = i * 256 + vfbyte() ;
  69.    i = i * 256 + vfbyte() ;
  70.    i = i * 256 + vfbyte() ;
  71.    return(i) ;
  72. }
  73.  
  74. integer
  75. vftrio()
  76. {
  77.    register integer i ;
  78.  
  79.    i = vfbyte() ;
  80.    i = i * 256 + vfbyte() ;
  81.    i = i * 256 + vfbyte() ;
  82.    return(i) ;
  83. }
  84.  
  85. Boolean
  86. vfopen(fd)
  87.         register fontdesctype *fd ;
  88. {
  89.    register char *d, *n ;
  90.  
  91.    d = fd->area ;
  92.    n = fd->name ;
  93.    if (*d==0)
  94.       d = vfpath ;
  95.    (void)sprintf(name, "%s.vf", n) ;
  96.    if (vffile=search(d, name, READBIN))
  97.       return(1) ;
  98.    return(0) ;
  99. }
  100.  
  101. /*
  102.  * The following routine is like fontdef, but for local fontdefs in VF files.
  103.  */
  104. fontmaptype *
  105. vfontdef(s, siz)
  106.       integer s ;
  107.     int siz ;
  108. {
  109.    register integer i, j, fn ;
  110.    register fontdesctype *fp ;
  111.    register fontmaptype *cfnt ;
  112.    char *name, *area ;
  113.    integer cksum, scsize, dssize ;
  114.  
  115.    fn = vfbyte() ;
  116.    while (siz-- > 1)
  117.       fn = (fn << 8) + vfbyte() ;
  118.    cfnt = (fontmaptype *)malloc(sizeof(fontmaptype)) ;
  119.    if (cfnt==NULL)
  120.       error("! ran out of memory") ;
  121.    cfnt->fontnum = fn ;
  122.    cksum = vfquad() ;
  123.    scsize = scalewidth(s, vfquad()) ;
  124.    dssize = (integer)(alpha * (real)vfquad()) ;
  125.    i = vfbyte() ; j = vfbyte() ;
  126.    if (nextstring + i + j > maxstring)
  127.       error("! out of string space") ;
  128.    area = nextstring ;
  129.    for (; i>0; i--)
  130.       *nextstring++ = vfbyte() ;
  131.    *nextstring++ = 0 ;
  132.    name = nextstring ;
  133.    for (; j>0; j--)
  134.       *nextstring++ = vfbyte() ;
  135.    *nextstring++ = 0 ;
  136.    fp = matchfont(name, area, scsize, (char *)0) ;
  137.    if (fp) {
  138.       nextstring = name ;
  139.       fp->checksum = cksum ;
  140.    } else {
  141.       fp = newfontdesc(cksum, scsize, dssize, name, area) ;
  142.       fp->next = fonthead ;
  143.       fonthead = fp ;
  144.    }
  145.    cfnt->desc = fp ;
  146.    return (cfnt) ;
  147. }
  148.  
  149. /*
  150.  *   Now our virtualfont routine.
  151.  */
  152. Boolean
  153. virtualfont(curfnt)
  154.         register fontdesctype *curfnt ;
  155. {
  156.    register shalfword i ;
  157.    register shalfword cmd ;
  158.    register integer k ;
  159.    register integer length ;
  160.    register shalfword cc ;
  161.    register chardesctype *cd ;
  162.    integer scaledsize = curfnt->scaledsize ;
  163.    register quarterword *tempr ;
  164.    fontmaptype *fm, *newf ;
  165.  
  166.    if (!vfopen(curfnt))
  167.       return (0) ;
  168. #ifdef DEBUG
  169.    if (dd(D_FONTS))
  170.       (void)fprintf(stderr,"Loading virtual font %s at %.1fpt\n",
  171.          name, (real)scaledsize/(alpha*0x100000)) ;
  172. #endif /* DEBUG */
  173.  
  174. /*
  175.  *   We clear out some pointers:
  176.  */
  177.    for (i=0; i<256; i++) {
  178.       curfnt->chardesc[i].TFMwidth = 0 ;
  179.       curfnt->chardesc[i].packptr = NULL ;
  180.       curfnt->chardesc[i].pixelwidth = 0 ;
  181.       curfnt->chardesc[i].flags = 0 ;
  182.    }
  183.    if (vfbyte()!=247)
  184.       badvf("expected pre") ;
  185.    if (vfbyte()!=202)
  186.       badvf("wrong id byte") ;
  187.    for(i=vfbyte(); i>0; i--)
  188.       (void)vfbyte() ;
  189.    k = vfquad() ;
  190.    if (k && curfnt->checksum)
  191.       if (k!=curfnt->checksum) {
  192.          (void)sprintf(errbuf,"Checksum mismatch in font %s", name) ;
  193.          error(errbuf) ;
  194.        }
  195.    k = (integer)(alpha * (real)vfquad()) ;
  196.    if (k > curfnt->designsize + 2 || k < curfnt->designsize - 2) {
  197.       (void)sprintf(errbuf,"Design size mismatch in font %s", name) ;
  198.       error(errbuf) ;
  199.    }
  200. /*
  201.  * Now we look for font definitions.
  202.  */
  203.    fm = NULL ;
  204.    while ((cmd=vfbyte())>=243) {
  205.       if (cmd>246)
  206.          badvf("unexpected command in preamble") ;
  207.       newf = vfontdef(scaledsize, cmd-242) ;
  208.       if (fm)
  209.          fm->next = newf ;
  210.       else curfnt->localfonts = newf ;
  211.       fm = newf ;
  212.       fm->next = NULL ; /* FIFO */
  213.    }
  214. /*
  215.  *   Now we get down to the serious business of reading character definitions.
  216.  */
  217.    do {
  218.       if (cmd==242) {
  219.          length = vfquad() + 2 ;
  220.          if (length<2) badvf("negative length packet") ;
  221.          if (length>65535) badvf("packet too long") ;
  222.          cc = vfquad() ;
  223.          if (cc<0 || cc>255) badvf("character code out of range") ;
  224.          cd = curfnt->chardesc + cc ;
  225.          cd->TFMwidth = scalewidth(vfquad(), scaledsize) ;
  226.       } else {
  227.          length = cmd + 2;
  228.          cc = vfbyte() ;
  229.          cd = curfnt->chardesc + cc ;
  230.          cd->TFMwidth = scalewidth(vftrio(), scaledsize) ;
  231.       }
  232.       cd->pixelwidth = ((integer)(conv*cd->TFMwidth+0.5)) ;
  233.       cd->flags = EXISTS ;
  234.       if (bytesleft < length) {
  235. #ifdef DEBUG
  236.           if (dd(D_FONTS))
  237.              (void)fprintf(stderr,
  238.                    "Allocating new raster memory (%d req, %d left)\n",
  239.                                 length, bytesleft) ;
  240. #endif /* DEBUG */
  241.           if (length > MINCHUNK) {
  242.              tempr = (quarterword *)malloc((unsigned int)length) ;
  243.              bytesleft = 0 ;
  244.           } else {
  245.              raster = (quarterword *)malloc(RASTERCHUNK) ;
  246.              tempr = raster ;
  247.              bytesleft = RASTERCHUNK - length ;
  248.              raster += length ;
  249.          }
  250.          if (tempr == NULL)
  251.             error("! out of memory while allocating raster") ;
  252.       } else {
  253.          tempr = raster ;
  254.          bytesleft -= length ;
  255.          raster += length ;
  256.       }
  257.       cd->packptr = tempr ;
  258.       length -= 2 ;
  259.       *tempr++ = length / 256 ;
  260.       *tempr++ = length % 256 ;
  261.          for (; length>0; length--)
  262.             *tempr++ = vfbyte() ;
  263.       cmd = vfbyte() ;
  264.    } while (cmd < 243) ;
  265.    if (cmd != 248)
  266.       badvf("missing postamble") ;
  267.    (void)fclose(vffile) ;
  268.    curfnt->loaded = 2 ;
  269.    return (1) ;
  270. }
  271.