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