ftp.nice.ch/pub/next/text/tex/apps/TeXview-kp0.25.s.tar.gz#/TeXview-grey/virtualfont.c

This is virtualfont.c in view mode; [Download] [Up]

/*
 *   Here's the code to load a VF file into memory.
 *   Any resemblance between this file and loadfont.c is purely uncoincidental.
 */
#include "structures.h" /* The copyright notice in that file is included too! */
#include <kpathsea/tex-file.h>
/*
 *   These are the external routines we use.
 */
extern void makefont() ;
extern void error() ;
extern integer scalewidth() ;
extern FILE *search() ;
extern int str2cmp() ;
extern TeXfontdesctype *TeXfontlist ;
void free() ;
/*
 *   These are the external variables we use.
 */
#ifdef DEBUG
extern integer debug_flag;
#endif  /* DEBUG */
extern int debugon ;
long bytesleft ;
quarterword *raster ;
char errbuf[300] ;
extern real conv ;
extern real vconv ;
extern real alpha ;
extern integer dvimag ;
extern int actualdpi ;
extern real pxlconv ;
extern char *nextstring, *maxstring ;
/*
 *   We use malloc here.
 */
char *malloc() ;

/*
 *   Now we have some routines to get stuff from the VF file.
 *   Subroutine vfbyte returns the next byte.
 */

static FILE *vffile ;
static char name[300] ;
void
badvf(s)
   char *s ;
{
   (void)sprintf(errbuf,"! Bad VF file %s: %s",name,s) ;
   error(errbuf);
}

shalfword
vfbyte()
{
   register shalfword i ;

   if ((i=getc(vffile))==EOF)
      badvf("unexpected eof") ;
   return(i) ;
}

integer
vfquad()
{
   register integer i ;

   i = vfbyte() ;
   if (i > 127)
      i -= 256 ;
   i = i * 256 + vfbyte() ;
   i = i * 256 + vfbyte() ;
   i = i * 256 + vfbyte() ;
   return(i) ;
}

integer
vftrio()
{
   register integer i ;

   i = vfbyte() ;
   i = i * 256 + vfbyte() ;
   i = i * 256 + vfbyte() ;
   return(i) ;
}

Boolean
vfopen(fd)
        register fontdesctype *fd ;
{
   register char *d, *n ;

   d = fd->name ;
   n = d ;
   while (*n++) ;
//   if (*d==0)
//      d = vfpath ;
   (void)sprintf(name, "%s.vf", n) ;
   if (vffile=search(vfpath, name, "r"))
      return(1) ;
   return(0) ;
}

/*
 * The following routine is like fontdef, but for local fontdefs in VF files.
 */
fontmaptype *
vfontdef(s)
      integer s ;
{
   register integer i, j, fn ;
   register TeXfontdesctype *fp ;
   register fontmaptype *cfnt ;
   char *p ;
   integer checksum, scaledsize, designsize ;

   fn = vfbyte() ;
   checksum = vfquad() ;
   scaledsize = vfquad() ;
   designsize = vfquad() ;
   i = vfbyte() ; j = vfbyte() ;
   fp = (TeXfontdesctype *)malloc(sizeof(TeXfontdesctype) + i + j) ;
   cfnt = (fontmaptype *)malloc(sizeof(fontmaptype)) ;
   if (fp==NULL || cfnt==NULL)
      error("! ran out of memory") ;
   cfnt->fontnum = fn ;
   fp->loaded = 0 ;
   fp->checksum = checksum ;
   fp->origssize = scaledsize ;
   fp->scaledsize = scalewidth(s, scaledsize) ;
   fp->designsize = (integer)(alpha * (real)designsize) ;
   fp->thinspace = fp->scaledsize / 6 ;
   fp->wanteddpi = pxlconv *
                      (float)fp->scaledsize / (float)(fp->designsize) + 0.5 ;
/*   (halfword)((float)dvimag*(float)fp->scaledsize*actualdpi/
         ((float)fp->designsize*1000.0)+0.5) ; */
   p = fp->name ;
   for (; i>0; i--)
      *p++ = vfbyte() ;
   *p++ = 0 ;
   for (; j>0; j--)
      *p++ = vfbyte() ;
   *p++ = 0 ;
   if (debugon > 5)
      printf("Defining virtual subfont %s %ld %ld\n", fp->name + 1,
      fp->scaledsize, fp->wanteddpi) ; fflush(stdout) ;
/*
 *   We don't do this minor optimization, since the matching will be done
 *   automatically by the loadfont routine.
 *
   for (fpp=TeXfontlist; fpp; fpp=fpp->next) {
      if (debugon > 6)
         printf("Comparing %s with %s\n", fp->name + 1, fpp->name + 1) ;
      if (fp->wanteddpi==fpp->wanteddpi && str2cmp(fp->name, fpp->name)) {
         free((char *)fp) ;
         fp = fpp ;
    if (debugon > 5) printf("Font was already known.\n") ;
         goto alreadyknown ;
      }
   }
   fp->next = TeXfontlist ;
   TeXfontlist = fp ;
 alreadyknown: */
   cfnt->tdesc = fp ;
   fp->next = TeXfontlist ;
   TeXfontlist = fp ;
   return (cfnt) ;
}

/*
 *   Now our virtualfont routine.
 */
Boolean
virtualfont(tft)
register TeXfontdesctype *tft ;
{
   register fontdesctype *curfnt ;
   register shalfword i ;
   register shalfword cmd ;
   register integer k ;
   register integer length ;
   register shalfword cc ;
   register chardesctype *cd ;
   integer scaledsize = tft->scaledsize ;
   register quarterword *tempr ;
   fontmaptype *fm, *newf ;

   curfnt = tft->loaded ;
   if (!vfopen(curfnt))
      return (0) ;
/*
 *   We clear out some pointers:
 */
   for (i=0; i<256; i++) {
      curfnt->chardesc[i].TFMwidth = 0 ;
      curfnt->chardesc[i].pixelwidth = 0 ;
   }
   if (vfbyte()!=247)
      badvf("expected pre") ;
   if (vfbyte()!=202)
      badvf("wrong id byte") ;
   for(i=vfbyte(); i>0; i--)
      (void)vfbyte() ;
   k = vfquad() ;
   if (k && tft->checksum)
      if (k!=tft->checksum) {
         (void)sprintf(errbuf,"Checksum mismatch in font %s", name) ;
         error(errbuf) ;
       }
   k = (integer)(alpha * (real)vfquad()) ;
   if (k > tft->designsize + 2 || k < tft->designsize - 2) {
      (void)sprintf(errbuf,"Design size mismatch in font %s", name) ;
      error(errbuf) ;
   }
/*
 * Now we look for font definitions.
 */
   fm = NULL ;
   while ((cmd=vfbyte())>=243) {
      if (cmd!=243)
         badvf("unexpected command in preamble") ;
      newf = vfontdef(scaledsize) ;
      if (fm)
         fm->next = newf ;
      else
         curfnt->localfonts = newf ;
      fm = newf ;
      fm->next = NULL ; /* FIFO */
   }
/*
 *   Now we get down to the serious business of reading character definitions.
 */
   do {
      if (cmd==242) {
         length = vfquad() + 2 ;
         if (length<2) badvf("negative length packet") ;
         if (length>65535) badvf("packet too long") ;
         cc = vfquad() ;
         if (cc<0 || cc>255) badvf("character code out of range") ;
         cd = curfnt->chardesc + cc ;
         cd->TFMwidth = vfquad() ; /* scaling comes later */
      } else {
         length = cmd + 2;
         cc = vfbyte() ;
         cd = curfnt->chardesc + cc ;
         cd->TFMwidth = vftrio() ;
      }
      if (cd->TFMwidth > 0)
         cd->pixelwidth = ((integer)(conv*scalewidth(cd->TFMwidth, scaledsize)+0.5)) ;
      else
         cd->pixelwidth = -((integer)(conv*scalewidth(-cd->TFMwidth, scaledsize)+0.5)) ;
      if (bytesleft < length) {
          if (length > MINCHUNK) {
             tempr = (quarterword *)malloc((unsigned int)length) ;
             bytesleft = 0 ;
          } else {
             raster = (quarterword *)malloc(RASTERCHUNK) ;
             tempr = raster ;
             bytesleft = RASTERCHUNK - length ;
             raster += length ;
         }
         if (tempr == NULL)
            error("! out of memory while allocating raster") ;
      } else {
         tempr = raster ;
         bytesleft -= length ;
         raster += length ;
      }
      cd->packptr = tempr ;
      length -= 2 ;
      *tempr++ = length / 256 ;
      *tempr++ = length % 256 ;
         for (; length>0; length--)
            *tempr++ = vfbyte() ;
      cmd = vfbyte() ;
   } while (cmd < 243) ;
   if (cmd != 248)
      badvf("missing postamble") ;
   (void)fclose(vffile) ;
   curfnt->virtual = 1 ;
   return (1) ;
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.