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