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