home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fonts 1 / freshfonts1.bin / bbs / programs / amiga / pastex13.lha / DVIPS / dvips5519.lha / dvips / finclude.c < prev    next >
C/C++ Source or Header  |  1993-08-12  |  9KB  |  332 lines

  1. /*
  2.  *  Code for allowing fonts to be used in PostScript files given via
  3.  *  `psfile=...'.
  4.  */
  5. #include "dvips.h" /* The copyright notice in that file is included too! */
  6. #include <ctype.h>
  7. #ifndef SYSV
  8. extern char *strtok() ; /* some systems don't have this in strings.h */
  9. #endif
  10. #ifdef VMS
  11. #define getname vms_getname
  12. #endif
  13.  
  14. double atof();
  15. /*
  16.  *   These are the external routines we call.
  17.  */
  18. extern fontdesctype *newfontdesc() ;
  19. extern fontdesctype *matchfont() ;
  20. extern Boolean prescanchar() ;
  21. extern Boolean preselectfont() ;
  22. extern void scout(), error() ;
  23. extern void stringend() ;
  24. extern void cmdout() ;
  25. extern void numout() ;
  26. extern void lfontout() ;
  27. extern char *newstring() ;
  28. extern FILE *search() ;
  29. /*
  30.  *   These are the external variables we access.
  31.  */
  32. extern fontdesctype *curfnt ;
  33. extern fontdesctype *fonthead ;
  34. extern integer fontmem ;
  35. extern fontdesctype *fonthd[MAXFONTHD] ;
  36. extern int nextfonthd ;
  37. extern char *nextstring ;
  38. extern char xdig[256] ;
  39. extern real conv ;
  40. extern integer pagecost ;
  41. extern int actualdpi ;
  42. extern integer mag ;
  43. extern Boolean includesfonts ;
  44. extern char *figpath ;
  45. /*
  46.  * Create a font descriptor for a font included in a psfile.  There will be
  47.  * no fontmaptype node for the resulting font descriptor until this font is
  48.  * encountered by fontdef() (if that ever happens).
  49.  */
  50. fontdesctype *
  51. ifontdef(name, area, scsize, dssize, scname)
  52. char *name, *scname, *area ;
  53. integer scsize, dssize ;
  54. {
  55.    fontdesctype *fp;
  56.  
  57.    fp = newfontdesc((integer)0, scsize, dssize, name, area);
  58.    fp->scalename = scname;
  59.    fp->next = fonthead ;
  60.    fonthead = fp ;
  61.    return fp;
  62. }
  63. /*
  64.  * When a font appears in an included psfile for the first time, this routine
  65.  * links it into the fonthd[] array.
  66.  */
  67. void
  68. setfamily(f)
  69. fontdesctype *f ;
  70. {
  71.    int i ;
  72.  
  73.    fontmem -= DICTITEMCOST;
  74.    for (i=0; i<nextfonthd; i++)
  75.       if (strcmp(f->name, fonthd[i]->name)==0
  76.             && strcmp(f->area, fonthd[i]->area)==0) {
  77.          f->nextsize = fonthd[i];
  78.          fonthd[i] = f;
  79.          return;
  80.       }
  81.    if (nextfonthd==MAXFONTHD)
  82.       error("! Too many fonts in included psfiles") ;
  83.    fontmem -= NAMECOST + strlen(f->name) + strlen(f->area) ;
  84.    fonthd[nextfonthd++] = f ;
  85.    f->nextsize = NULL ;
  86. }
  87. /*
  88.  * Convert file name s to a pair of new strings in the string pool.
  89.  * The first string is the original value of nextstring; the second
  90.  * string is the return value.
  91.  */
  92. char*
  93. getname(s)
  94. char *s ;
  95. {
  96.    char *a, *p, sav;
  97.  
  98.    a = NULL;
  99.    for (p=s; *p!=0; p++)
  100.       if (*p=='/')
  101.          a = p+1 ;
  102.    if (a==NULL) *nextstring++ = 0 ;
  103.    else {   sav = *a ;
  104.       *a = 0 ;
  105.       (void) newstring(s) ;
  106.       *a = sav ;
  107.       s = a ;
  108.    }
  109.    return newstring(s);
  110. }
  111. /*
  112.  * Mark character usage in *f based on the hexadicimal bitmap found in
  113.  * string s.  A two-digit offset separated by a colon gives the initial
  114.  * character code.  We have no way of knowing how many times each character
  115.  * is used or how many strings get created when showing the characters so
  116.  * we just estimate two usages per character and one string per pair of
  117.  * usages.
  118.  */
  119. void
  120. includechars(f, s)
  121. fontdesctype *f ;
  122. char *s ;
  123. {
  124.    int b, c, d ;
  125.    int l = strlen(s) ;
  126.  
  127.    if (l>0 && s[l-1]=='\n')
  128.       s[--l] = 0 ;
  129.    if (!isxdigit(s[0]) || !isxdigit(s[1]) || s[2]!=':'
  130.          || strspn(s+3,"0123456789ABCDEFabcdef") < l-3) {
  131.       fprintf(stderr, "%s\n", s) ;
  132.       error("Bad syntax in included font usage table") ;
  133.       return ;
  134.    }
  135.    c = (xdig[(int)(s[0])] << 4) + xdig[(int)(s[1])] ;
  136.    s += 2 ;
  137.    while (*++s) {
  138.       d = xdig[(int)*s] ;
  139.       for (b=8; b!=0; b>>=1) {
  140.          if ((d&b)!=0) {
  141.             pagecost ++ ;
  142.             (void) prescanchar(&f->chardesc[c]) ;
  143.          }
  144.          if (++c==256) return ;
  145.       }
  146.    }
  147. }
  148. /*
  149.  * String p should be start after the ":" in a font declaration of the form
  150. %*FONT: <tfm-name> <scaled-size> <design-size> <2-hex-digits>:<hex-string>
  151.  * where the sizes are floating-point numbers in units of PostScript points
  152.  * (TeX's "bp").  We update the data structures for the included font,
  153.  * charge fontmem for the VM used, and add to delchar if necessary.
  154.  * Note that the scaled size and the design size are multiplied by mag/1000.
  155.  * This is needed for the design size to undo the similar factor in conv since
  156.  * design sizes are not supposed to be affected by magnification.  Applying
  157.  * the magnification factor to the scaled size selects magnified fonts as is
  158.  * appropriate in the normal case where the included PostScript is scaled by
  159.  * mag/1000.  The definition of `fshow' in finclude.lpro unscales by `DVImag'
  160.  * to account for this.  We cannot change the font scaled size to account for
  161.  * options like `hscale=' because then the definition of `fshow' would have
  162.  * to change.
  163.  */
  164. void
  165. scan1fontcomment(p)
  166. char *p ;
  167. {
  168.    char *q, *name, *area;
  169.    char *scname;      /* location in buffer where we got scsize */
  170.    integer scsize, dssize;
  171.    fontdesctype *fptr;
  172.    real DVIperBP;
  173.  
  174.    DVIperBP = actualdpi/(72.0*conv) * (mag/1000.0);
  175.    p = strtok(p, " ");
  176.    if (p==NULL) return;
  177.    area = nextstring ;   /* tentatively in the string pool */
  178.    name = getname(p);
  179.    q = strtok((char *)0, " ");
  180.    if (p==NULL || (scsize=(integer)(atof(q)*DVIperBP))==0) {
  181.       fprintf(stderr, "%s\n",p);
  182.       error("No scaled size for included font");
  183.       nextstring = area ;   /* remove from string pool */
  184.       return;
  185.    }
  186.    scname = q;
  187.    q = strtok((char *)0, " ");
  188.    if (p==NULL || (dssize=(integer)(atof(q)*DVIperBP))==0) {
  189.       fprintf(stderr, "%s\n",p);
  190.       error("No design size for included font");
  191.       nextstring = area ;
  192.       return;
  193.    }
  194.    q = strtok((char *)0, " ");
  195.    fptr = matchfont(name, area, scsize, scname);
  196.    if (!fptr) {
  197.       fptr = ifontdef(name, area, scsize, dssize, newstring(scname));
  198.       (void) preselectfont(fptr);
  199.       setfamily(fptr);
  200.    } else {
  201.       nextstring = area;   /* remove from string pool */
  202.       (void) preselectfont(fptr);
  203.       if (fptr->scalename==NULL) {
  204.          fptr->scalename=newstring(scname);
  205.          setfamily(fptr);
  206.       }
  207.    }
  208.    includesfonts = 1;
  209.    fptr->psflag |= THISPAGE;
  210.    includechars(fptr, q);
  211. }
  212. /*
  213.  * Parse the arguments to a "%%VMusage" comment.  The Adobe Type 1 Font Format
  214.  * book specifies two arguments. This routine will accept one or two arguments;
  215.  * if there are two arguments we take the maximum.
  216.  */
  217. integer
  218. scanvm(p)
  219. char *p ;
  220. {
  221.    char* q;
  222.    integer vm, vmmax;
  223.    extern long atol() ;
  224.  
  225.    q = strtok(p, " ");
  226.    if (q==NULL) {
  227.       error("Missing data in VMusage comment");
  228.       return 0;
  229.    }
  230.    vmmax = atol(q);
  231.    q = strtok((char *)0, " ");
  232.    if (q!=NULL && (vm=atol(q))>vmmax)
  233.       vmmax = vm;
  234.    return vmmax;
  235. }
  236. /*
  237.  * Scan an initial sequence of comment lines looking for font and memory
  238.  * usage specifications.  This does not handle the "atend" construction.
  239.  */
  240. void
  241. scanfontcomments(filename)
  242. char* filename ;
  243. {
  244.    char p[500];
  245.    FILE *f;
  246.    integer truecost = pagecost ;
  247.    Boolean trueknown = 0 ;
  248.    fontdesctype *oldcf = curfnt;
  249.  
  250.    f = search(figpath, filename, READ) ;
  251.    if (f) {
  252.       while (fgets(p,500,f) && p[0]=='%' &&
  253.             (p[1]=='!' || p[1]=='%' || p[1]=='*'))
  254.          if (strncmp(p, "%*Font:", 7) == 0)
  255.             scan1fontcomment(p+7);
  256.          else if (strncmp(p, "%%VMusage:", 9) == 0) {
  257.             truecost += scanvm(p+10) ;
  258.             trueknown = 1 ;
  259.          }
  260.       if (trueknown)
  261.          pagecost = truecost ;
  262.       fclose(f) ;
  263.    }
  264.    curfnt = oldcf;
  265. }
  266. /*
  267.  * Is string s less than 30 characters long with no special characters
  268.  * that are not allowed in PostScript commands.
  269.  */
  270. Boolean
  271. okascmd(ss)
  272. char *ss ;
  273. {
  274.    register c = 0 ;
  275.    register char *s = ss ;
  276.  
  277.    while (*s)
  278.       if (*s<' ' || *s>126 || ++c==30)
  279.          return(0) ;
  280.    return(strcspn(ss,"()<>[]{}%/") == c) ;
  281. }
  282. /*
  283.  * Output font area and font name strings as a literal string
  284.  */
  285. void
  286. nameout(area, name)
  287. char *area, *name ;
  288. {
  289.    char buf[30] ;
  290.    char *s ;
  291.  
  292.    if (*area==0 && okascmd(name)) {
  293.       (void)sprintf(buf, "/%s", name) ;
  294.       cmdout(name);
  295.    } else {
  296.       for (s=area; *s; s++)
  297.          scout(*s) ;
  298.       for (s=name; *s; s++)
  299.          scout(*s) ;
  300.       stringend();
  301.       cmdout("cvn") ;
  302.    }
  303. }
  304. /*
  305.  *