home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3.4.17 [SPARC, PA-RISC] / nextstep33_risc.iso / NextLibrary / TeX / tex / src / texview / virtualfont.c < prev   
Encoding:
C/C++ Source or Header  |  1992-08-20  |  7.0 KB  |  285 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 int str2cmp() ;
  14. extern TeXfontdesctype *TeXfontlist ;
  15. void free() ;
  16. /*
  17.  *   These are the external variables we use.
  18.  */
  19. #ifdef DEBUG
  20. extern integer debug_flag;
  21. #endif  /* DEBUG */
  22. extern int debugon ;
  23. long bytesleft ;
  24. quarterword *raster ;
  25. extern char *vfpath ;
  26. char errbuf[200] ;
  27. extern real conv ;
  28. extern real vconv ;
  29. extern real alpha ;
  30. extern integer dvimag ;
  31. extern int actualdpi ;
  32. extern real pxlconv ;
  33. extern char *nextstring, *maxstring ;
  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->name ;
  96.    n = d ;
  97.    while (*n++) ;
  98.    if (*d==0)
  99.       d = vfpath ;
  100.    (void)sprintf(name, "%s.vf", n) ;
  101.    if (vffile=search(d, name, "r"))
  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)
  111.       integer s ;
  112. {
  113.    register integer i, j, fn ;
  114.    register TeXfontdesctype *fp ;
  115.    register fontmaptype *cfnt ;
  116.    char *p ;
  117.    integer checksum, scaledsize, designsize ;
  118.  
  119.    fn = vfbyte() ;
  120.    checksum = vfquad() ;
  121.    scaledsize = vfquad() ;
  122.    designsize = vfquad() ;
  123.    i = vfbyte() ; j = vfbyte() ;
  124.    fp = (TeXfontdesctype *)malloc(sizeof(TeXfontdesctype) + i + j) ;
  125.    cfnt = (fontmaptype *)malloc(sizeof(fontmaptype)) ;
  126.    if (fp==NULL || cfnt==NULL)
  127.       error("! ran out of memory") ;
  128.    cfnt->fontnum = fn ;
  129.    fp->loaded = 0 ;
  130.    fp->checksum = checksum ;
  131.    fp->origssize = scaledsize ;
  132.    fp->scaledsize = scalewidth(s, scaledsize) ;
  133.    fp->designsize = (integer)(alpha * (real)designsize) ;
  134.    fp->thinspace = fp->scaledsize / 6 ;
  135.    fp->wanteddpi = pxlconv *
  136.                       (float)fp->scaledsize / (float)(fp->designsize) + 0.5 ;
  137. /*   (halfword)((float)dvimag*(float)fp->scaledsize*actualdpi/
  138.          ((float)fp->designsize*1000.0)+0.5) ; */
  139.    p = fp->name ;
  140.    for (; i>0; i--)
  141.       *p++ = vfbyte() ;
  142.    *p++ = 0 ;
  143.    for (; j>0; j--)
  144.       *p++ = vfbyte() ;
  145.    *p++ = 0 ;
  146.    if (debugon > 5)
  147.       printf("Defining virtual subfont %s %ld %ld\n", fp->name + 1,
  148.       fp->scaledsize, fp->wanteddpi) ; fflush(stdout) ;
  149. /*
  150.  *   We don't do this minor optimization, since the matching will be done
  151.  *   automatically by the loadfont routine.
  152.  *
  153.    for (fpp=TeXfontlist; fpp; fpp=fpp->next) {
  154.       if (debugon > 6)
  155.          printf("Comparing %s with %s\n", fp->name + 1, fpp->name + 1) ;
  156.       if (fp->wanteddpi==fpp->wanteddpi && str2cmp(fp->name, fpp->name)) {
  157.          free((char *)fp) ;
  158.          fp = fpp ;
  159.     if (debugon > 5) printf("Font was already known.\n") ;
  160.          goto alreadyknown ;
  161.       }
  162.    }
  163.    fp->next = TeXfontlist ;
  164.    TeXfontlist = fp ;
  165.  alreadyknown: */
  166.    cfnt->tdesc = fp ;
  167.    fp->next = TeXfontlist ;
  168.    TeXfontlist = fp ;
  169.    return (cfnt) ;
  170. }
  171.  
  172. /*
  173.  *   Now our virtualfont routine.
  174.  */
  175. Boolean
  176. virtualfont(tft)
  177. register TeXfontdesctype *tft ;
  178. {
  179.    register fontdesctype *curfnt ;
  180.    register shalfword i ;
  181.    register shalfword cmd ;
  182.    register integer k ;
  183.    register integer length ;
  184.    register shalfword cc ;
  185.    register chardesctype *cd ;
  186.    integer scaledsize = tft->scaledsize ;
  187.    register quarterword *tempr ;
  188.    fontmaptype *fm, *newf ;
  189.  
  190.    curfnt = tft->loaded ;
  191.    if (!vfopen(curfnt))
  192.       return (0) ;
  193. /*
  194.  *   We clear out some pointers:
  195.  */
  196.    for (i=0; i<256; i++) {
  197.       curfnt->chardesc[i].TFMwidth = 0 ;
  198.       curfnt->chardesc[i].pixelwidth = 0 ;
  199.    }
  200.    if (vfbyte()!=247)
  201.       badvf("expected pre") ;
  202.    if (vfbyte()!=202)
  203.       badvf("wrong id byte") ;
  204.    for(i=vfbyte(); i>0; i--)
  205.       (void)vfbyte() ;
  206.    k = vfquad() ;
  207.    if (k && tft->checksum)
  208.       if (k!=tft->checksum) {
  209.          (void)sprintf(errbuf,"Checksum mismatch in font %s", name) ;
  210.          error(errbuf) ;
  211.        }
  212.    k = (integer)(alpha * (real)vfquad()) ;
  213.    if (k > tft->designsize + 2 || k < tft->designsize - 2) {
  214.       (void)sprintf(errbuf,"Design size mismatch in font %s", name) ;
  215.       error(errbuf) ;
  216.    }
  217. /*
  218.  * Now we look for font definitions.
  219.  */
  220.    fm = NULL ;
  221.    while ((cmd=vfbyte())>=243) {
  222.       if (cmd!=243)
  223.          badvf("unexpected command in preamble") ;
  224.       newf = vfontdef(scaledsize) ;
  225.       if (fm)
  226.          fm->next = newf ;
  227.       else
  228.          curfnt->localfonts = newf ;
  229.       fm = newf ;
  230.       fm->next = NULL ; /* FIFO */
  231.    }
  232. /*
  233.  *   Now we get down to the serious business of reading character definitions.
  234.  */
  235.    do {
  236.       if (cmd==242) {
  237.          length = vfquad() + 2 ;
  238.          if (length<2) badvf("negative length packet") ;
  239.          if (length>65535) badvf("packet too long") ;
  240.          cc = vfquad() ;
  241.          if (cc<0 || cc>255) badvf("character code out of range") ;
  242.          cd = curfnt->chardesc + cc ;
  243.          cd->TFMwidth = vfquad() ; /* scaling comes later */
  244.       } else {
  245.          length = cmd + 2;
  246.          cc = vfbyte() ;
  247.          cd = curfnt->chardesc + cc ;
  248.          cd->TFMwidth = vftrio() ;
  249.       }
  250.       if (cd->TFMwidth > 0)
  251.          cd->pixelwidth = ((integer)(conv*scalewidth(cd->TFMwidth, scaledsize)+0.5)) ;
  252.       else
  253.          cd->pixelwidth = -((integer)(conv*scalewidth(-cd->TFMwidth, scaledsize)+0.5)) ;
  254.       if (bytesleft < length) {
  255.           if (length > MINCHUNK) {
  256.              tempr = (quarterword *)malloc((unsigned int)length) ;
  257.              bytesleft = 0 ;
  258.           } else {
  259.              raster = (quarterword *)malloc(RASTERCHUNK) ;
  260.              tempr = raster ;
  261.              bytesleft = RASTERCHUNK - length ;
  262.              raster += length ;
  263.          }
  264.          if (tempr == NULL)
  265.             error("! out of memory while allocating raster") ;
  266.       } else {
  267.          tempr = raster ;
  268.          bytesleft -= length ;
  269.          raster += length ;
  270.       }
  271.       cd->packptr = tempr ;
  272.       length -= 2 ;
  273.       *tempr++ = length / 256 ;
  274.       *tempr++ = length % 256 ;
  275.          for (; length>0; length--)
  276.             *tempr++ = vfbyte() ;
  277.       cmd = vfbyte() ;
  278.    } while (cmd < 243) ;
  279.    if (cmd != 248)
  280.       badvf("missing postamble") ;
  281.    (void)fclose(vffile) ;
  282.    curfnt->virtual = 1 ;
  283.    return (1) ;
  284. }
  285.