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