home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / unixtex-6.1b-src.tgz / tar.out / contrib / unixtex / dvipsk / finclude.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  9KB  |  331 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 <kpathsea/c-ctype.h>
  7. #ifdef VMS
  8. #define getname vms_getname
  9. #endif
  10.  
  11. #ifndef STDC_HEADERS
  12. double atof();
  13. #endif
  14.  
  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 error() ;
  23. extern void scout(), error() ;
  24. extern void stringend() ;
  25. extern void cmdout() ;
  26. extern void numout() ;
  27. extern void lfontout() ;
  28. extern char *newstring() ;
  29. extern FILE *search() ;
  30. /*
  31.  *   These are the external variables we access.
  32.  */
  33. extern fontdesctype *curfnt ;
  34. extern fontdesctype *fonthead ;
  35. extern integer fontmem ;
  36. extern fontdesctype *fonthd[MAXFONTHD] ;
  37. extern int nextfonthd ;
  38. extern char *nextstring ;
  39. extern char xdig[256] ;
  40. extern real conv ;
  41. extern integer pagecost ;
  42. extern int actualdpi ;
  43. extern integer mag ;
  44. extern Boolean includesfonts ;
  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.  
  224.    q = strtok(p, " ");
  225.    if (q==NULL) {
  226.       error("Missing data in VMusage comment");
  227.       return 0;
  228.    }
  229.    vmmax = atol(q);
  230.    q = strtok((char *)0, " ");
  231.    if (q!=NULL && (vm=atol(q))>vmmax)
  232.       vmmax = vm;
  233.    return vmmax;
  234. }
  235. /*
  236.  * Scan an initial sequence of comment lines looking for font and memory
  237.  * usage specifications.  This does not handle the "atend" construction.
  238.  */
  239. void
  240. scanfontcomments(filename)
  241. char* filename ;
  242. {
  243.    char p[500];
  244.    FILE *f;
  245.    integer truecost = pagecost ;
  246.    Boolean trueknown = 0 ;
  247.    fontdesctype *oldcf = curfnt;
  248.  
  249.    f = search(figpath, filename, READ) ;
  250.    if (f) {
  251.       while (fgets(p,500,f) && p[0]=='%' &&
  252.             (p[1]=='!' || p[1]=='%' || p[1]=='*'))
  253.          if (strncmp(p, "%*Font:", 7) == 0)
  254.             scan1fontcomment(p+7);
  255.          else if (strncmp(p, "%%VMusage:", 9) == 0) {
  256.             truecost += scanvm(p+10) ;
  257.             trueknown = 1 ;
  258.          }
  259.       if (trueknown)
  260.          pagecost = truecost ;
  261.       fclose(f) ;
  262.    }
  263.    curfnt = oldcf;
  264. }
  265. /*
  266.  * Is string s less than 30 characters long with no special characters
  267.  * that are not allowed in PostScript commands.
  268.  */
  269. Boolean
  270. okascmd(ss)
  271. char *ss ;
  272. {
  273.    register c = 0 ;
  274.    register char *s = ss ;
  275.  
  276.    while (*s)
  277.       if (*s<' ' || *s>126 || ++c==30)
  278.          return(0) ;
  279.    return(strcspn(ss,"()<>[]{}%/") == c) ;
  280. }
  281. /*
  282.  * Output font area and font name strings as a literal string
  283.  */
  284. void
  285. nameout(area, name)
  286. char *area, *name ;
  287. {
  288.    char buf[30] ;
  289.    char *s ;
  290.  
  291.    if (*area==0 && okascmd(name)) {
  292.       (void)sprintf(buf, "/%s", name) ;
  293.       cmdout(name);
  294.    } else {
  295.       for (s=area; *s; s++)
  296.          scout(*s) ;
  297.       for (s=name; *s; s++)
  298.          scout(*s) ;
  299.       stringend();
  300.       cmdout("cvn") ;
  301.    }
  302. }
  303. /*
  304.  * Output commands for defining a table of PostScript font identifiers for
  305.  * fonts used in included psfiles in the current section.
  306.  */
  307. void
  308. fonttableout()
  309. {
  310.    int i, k;
  311.    fontdesctype *f;
  312.  
  313.    for (i=0; i<nextfonthd; i++) {
  314.       for (f=fonthd[i]; f!=NULL; f=f->nextsize)
  315.          if (f->psflag==EXISTS) break;
  316.       if (f!=NULL) {
  317.          nameout(f->area, f->name);
  318.          k = 0;
  319.          do {   if (f->psflag==EXISTS) {
  320.                cmdout(f->scalename);
  321.                lfontout((int)f->psname);
  322.             }
  323.             f = f->nextsize;
  324.             k++;
  325.          } while (f!=NULL);
  326.          numout((integer)k);
  327.          cmdout("fstore");
  328.       }
  329.    }
  330. }
  331.