home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / PRINTING / DVIPS54.ZIP / DVIPS / FONTDEF.C < prev    next >
C/C++ Source or Header  |  1990-11-25  |  5KB  |  172 lines

  1. /*
  2.  *  Stores the data from a font definition into the global data structures.
  3.  *  A routine skipnop is also included to handle nops and font definitions
  4.  *  between pages.
  5.  */
  6. #include "structures.h" /* The copyright notice in that file is included too! */
  7. /*
  8.  *   These are the external routines we call.
  9.  */
  10. extern shalfword dvibyte() ;
  11. extern integer signedquad() ;
  12. extern void error() ;
  13. /*
  14.  *   The external variables we use:
  15.  */
  16. extern char *nextstring, *maxstring ;
  17. extern integer mag ;
  18. extern fontdesctype *baseFonts[] ;
  19. #ifdef DEBUG
  20. extern integer debug_flag;
  21. #endif  /* DEBUG */
  22. extern int actualdpi ;
  23. extern real alpha ;
  24. extern fontmaptype *ffont ;
  25. extern fontdesctype *fonthead ;
  26. extern halfword dpicheck() ;
  27. extern char *malloc() ;
  28. extern integer fsizetol ;
  29. /*
  30.  * newfontdesc creates a new font descriptor with the given parameters and
  31.  * returns a pointer to the new object.
  32. */
  33. fontdesctype*
  34. newfontdesc(cksum, scsize, dssize, name, area)
  35. integer cksum, scsize, dssize ;
  36. char *name, *area ;
  37. {
  38.    register fontdesctype *fp ;
  39.  
  40.    fp = (fontdesctype *)malloc(sizeof(fontdesctype)) ;
  41.    if (fp == NULL)
  42.       error("! ran out of memory") ;
  43.    fp->psname = 0 ;
  44.    fp->loaded = 0 ;
  45.    fp->checksum = cksum ;
  46.    fp->scaledsize = scsize ;
  47.    fp->designsize = dssize ;
  48.    fp->thinspace = scsize / 6 ;
  49.    fp->scalename = NULL ;
  50.    fp->psflag = 0 ;
  51.    fp->name = name;
  52.    fp->area = area;
  53.    fp->resfont = NULL ;
  54.    fp->localfonts = NULL ;
  55.    fp->dpi = dpicheck((halfword)((float)mag*(float)fp->scaledsize*DPI/
  56.          ((float)fp->designsize*1000.0)+0.5)) ;
  57.    fp->loadeddpi = fp->dpi ;
  58. #ifdef DEBUG
  59.    if (dd(D_FONTS))
  60.       (void)fprintf(stderr,"Defining font (%s) %s at %.1fpt\n",
  61.          area, name, (real)scsize/(alpha*0x100000)) ;
  62. #endif /* DEBUG */
  63.    return fp ;
  64. }
  65. /*
  66.  * Try to find a font with a given name and approximate scaled size, returning
  67.  * NULL if unsuccessful.  If scname and the font's scalename are both
  68.  * non-NULL they must match exactly.  If both are NULL, scsize and the
  69.  * font's scaledsize come from the dvi file and should match exactly.
  70.  * Otherwise there can be some slop due to the inaccuracies of sizes
  71.  * read from an included psfile.
  72.  */
  73. fontdesctype *
  74. matchfont(name, area, scsize, scname)
  75. char *area, *name, *scname ;
  76. integer scsize ;
  77. {
  78.    register fontdesctype *fp;
  79.    register integer smin, smax;
  80.  
  81.    smin = scsize - fsizetol ;
  82.    smax = scsize + fsizetol ;
  83.    for (fp=fonthead; fp; fp=fp->next)
  84.       if (smin < fp->scaledsize && fp->scaledsize < smax &&
  85.             strcmp(name,fp->name)==0 && strcmp(area,fp->area)==0)
  86.          if (scname == NULL) {
  87.             if (fp->scalename!=NULL || scsize==fp->scaledsize)
  88.                break ;
  89.          } else {
  90.             if (fp->scalename==NULL || strcmp(scname,fp->scalename)==0)
  91.                break ;
  92.          }
  93. #ifdef DEBUG
  94.    if (dd(D_FONTS) && fp)
  95.       (void)fprintf(stderr,"(Already known.)\n") ;
  96. #endif /* DEBUG */
  97.    return fp;
  98. }
  99. /*
  100.  *   fontdef takes a font definition in the dvi file and loads the data
  101.  *   into its data structures.
  102.  */
  103. void
  104. fontdef(siz)
  105. int siz ;
  106. {
  107.    register integer i, j, fn ;
  108.    register fontdesctype *fp ;
  109.    register fontmaptype *cfnt ;
  110.    char *name, *area ;
  111.    integer cksum, scsize, dssize ;
  112.    extern void skipover() ;
  113.  
  114.    fn = dvibyte() ;
  115.    while (siz-- > 1)
  116.       fn = (fn << 8) + dvibyte() ;
  117.    for (cfnt=ffont; cfnt; cfnt = cfnt->next)
  118.       if (cfnt->fontnum == fn) goto alreadydefined ;
  119.    cfnt = (fontmaptype *)malloc(sizeof(fontmaptype)) ;
  120.    if (cfnt==NULL)
  121.       error("! ran out of memory") ;
  122.    cfnt->next = ffont ;
  123.    ffont = cfnt ;
  124.    cfnt->fontnum = fn ;
  125.    cksum = signedquad() ;
  126.    scsize = signedquad() ;
  127.    dssize = signedquad() ;
  128.    i = dvibyte() ; j = dvibyte() ;
  129.    if (nextstring + i + j > maxstring)
  130.       error("! out of string space") ;
  131.    area = nextstring ;
  132.    for (; i>0; i--)
  133.       *nextstring++ = dvibyte() ;
  134.    *nextstring++ = 0 ;
  135.    name = nextstring ;
  136.    for (; j>0; j--)
  137.       *nextstring++ = dvibyte() ;
  138.    *nextstring++ = 0 ;
  139.    fp = matchfont(name, area, scsize, NULL) ;
  140.    if (fp) {
  141.       nextstring = name ;
  142.       fp->checksum = cksum ;
  143.    } else {
  144.       fp = newfontdesc(cksum, scsize, dssize, name, area) ;
  145.       fp->next = fonthead ;
  146.       fonthead = fp ;
  147.    }
  148.    cfnt->desc = fp ;
  149.    if (fn < 256)
  150.       baseFonts[fn] = fp ;
  151.    return ;
  152. alreadydefined:
  153. /* A DVI file will not define a font twice; but we may be scanning
  154.  * a font definition twice because a new section has started,
  155.  * or because of collated copies. */
  156.       skipover(12) ;
  157.       skipover(dvibyte()+dvibyte()) ;
  158. }
  159.  
  160. /*
  161.  *   The next routine handles nops or font definitions between pages in a
  162.  *   dvi file.  Returns the first command that is not a nop or font definition.
  163.  */
  164. int
  165. skipnop()
  166. {
  167.   register int cmd ;
  168.   while ((cmd=dvibyte())==138||cmd==243)
  169.     if (cmd>=243 && cmd<=246) fontdef(cmd-242) ;
  170.   return(cmd) ;
  171. }
  172.