home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / PRINTING / DVIPS386.ZIP / AFM2TFM.C < prev    next >
C/C++ Source or Header  |  1991-11-03  |  36KB  |  1,287 lines

  1. /*
  2.  *   This program converts AFM files to TeX TFM files, and optionally
  3.  *   to TeX VPL files that retain all kerning and ligature information.
  4.  *   Both files make the characters not normally encoded by TeX available
  5.  *   by character codes greater than 127.
  6.  */
  7.  
  8. /*   (Modified by Don Knuth from Tom Rokicki's pre-VPL version.) */
  9.  
  10. #include <stdio.h>
  11. #ifdef SYSV
  12. #include <string.h>
  13. #else
  14. #ifdef VMS
  15. #include <string.h>
  16. #else
  17. #include <strings.h>
  18. #endif
  19. #endif
  20. #include <math.h>
  21.  
  22. #ifdef MSDOS
  23. #define WRITEBIN "wb"
  24. #else
  25. #define WRITEBIN "w"
  26. #endif
  27.  
  28. char *texencoding[] = {
  29.    "Gamma", "Delta", "Theta", "Lambda", "Xi", "Pi", "Sigma",
  30.    "Upsilon", "Phi", "Psi", "Omega", "arrowup", "arrowdown", "quotesingle",
  31.    "exclamdown", "questiondown", "dotlessi", "dotlessj", "grave", "acute",
  32.    "caron", "breve", "macron", "ring", "cedilla", "germandbls", "ae", "oe",
  33.    "oslash", "AE", "OE", "Oslash", "space", "exclam", "quotedbl", "numbersign",
  34.    "dollar", "percent", "ampersand", "quoteright", "parenleft", "parenright",
  35.    "asterisk", "plus", "comma", "hyphen", "period", "slash", "zero", "one",
  36.    "two", "three", "four", "five", "six", "seven", "eight", "nine", "colon",
  37.    "semicolon", "less", "equal", "greater", "question", "at", "A", "B", "C",
  38.    "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
  39.    "S", "T", "U", "V", "W", "X", "Y", "Z", "bracketleft", "backslash",
  40.    "bracketright", "circumflex", "underscore", "quoteleft", "a", "b", "c", "d",
  41.    "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
  42.    "t", "u", "v", "w", "x", "y", "z", "braceleft", "bar", "braceright",
  43.    "tilde", "dieresis" } ;
  44. /*
  45.  *   The above layout corresponds to TeX Typewriter Type and is compatible
  46.  *   with TeX Text because the position of ligatures is immaterial.
  47.  */
  48.  
  49. /*
  50.  *   This is what we store Adobe data in.
  51.  */
  52. struct adobeinfo {
  53.    struct adobeinfo *next ;
  54.    int adobenum, texnum, width ;
  55.    char *adobename ;
  56.    int llx, lly, urx, ury ;
  57.    struct lig *ligs ;
  58.    struct kern *kerns ;
  59.    struct pcc *pccs ;
  60.    int wptr, hptr, dptr, iptr ;
  61. } *adobechars, *adobeptrs[256], *texptrs[256],
  62.   *uppercase[256], *lowercase[256] ;
  63. struct lig {
  64.    struct lig *next ;
  65.    char *succ, *sub ;
  66. } ;
  67. struct kern {
  68.    struct kern *next ;
  69.    char *succ ;
  70.    int delta ;
  71. } ;
  72. struct pcc {
  73.    struct pcc *next ;
  74.    char * partname ;
  75.    int xoffset, yoffset ;
  76. } ;
  77.  
  78. FILE *afmin, *vplout, *tfmout ;
  79. char inname[200], outname[200] ; /* names of input and output files */
  80. char buffer[255]; /* input buffer (modified while parsing) */
  81. char obuffer[255] ; /* unmodified copy of input buffer */
  82. char *param ; /* current position in input buffer */
  83. char *fontname = "Unknown" ;
  84. char *codingscheme = "Unspecified" ;
  85. float italicangle = 0.0 ;
  86. char fixedpitch ;
  87. char makevpl ;
  88. int xheight = 400 ;
  89. int fontspace ;
  90. int bc, ec ;
  91. long cksum ;
  92. float efactor = 1.0, slant = 0.0 ;
  93. double newslant ;
  94. char titlebuf[100] ;
  95.  
  96. void
  97. error(s)
  98. register char *s ;
  99. {
  100.    extern void exit() ;
  101.  
  102.    (void)fprintf(stderr, "%s\n", s) ;
  103.    if (obuffer[0]) {
  104.       (void)fprintf(stderr, "%s\n", obuffer) ;
  105.       while (param > buffer) {
  106.          (void)fprintf(stderr, " ") ;
  107.          param-- ;
  108.       }
  109.       (void)fprintf(stderr, "^\n") ;
  110.    }
  111.    if (*s == '!')
  112.       exit(1) ;
  113. }
  114.  
  115. int
  116. transform(x,y)
  117.    register int x,y ;
  118. {
  119.    register double acc ;
  120.    acc = efactor * x + slant *y ;
  121.    return (int)(acc>=0? acc+0.5 : acc-0.5 ) ;
  122. }
  123.  
  124. int
  125. getline() {
  126.    register char *p ;
  127.    register int c ;
  128.  
  129.    param = buffer ;
  130.    for (p=buffer; (c=getc(afmin)) != EOF && c != 10;)
  131.       *p++ = c ;
  132.    *p = 0 ;
  133.    (void)strcpy(obuffer, buffer) ;
  134.    if (p == buffer && c == EOF)
  135.       return(0) ;
  136.    else
  137.       return(1) ;
  138. }
  139.  
  140. char *interesting[] = { "FontName", "ItalicAngle", "IsFixedPitch",
  141.    "XHeight", "C", "KPX", "CC", "EncodingScheme", NULL} ; 
  142. #define FontName (0)
  143. #define ItalicAngle (1)
  144. #define IsFixedPitch (2)
  145. #define XHeight (3)
  146. #define C (4)
  147. #define KPX (5)
  148. #define CC (6)
  149. #define EncodingScheme (7)
  150. #define NONE (-1)
  151. int
  152. interest(s)
  153. char *s ;
  154. {
  155.    register char **p ;
  156.    register int n ;
  157.  
  158.    for (p=interesting, n=0; *p; p++, n++)
  159.       if (strcmp(s, *p)==0)
  160.          return(n) ;
  161.    return(NONE) ;
  162. }
  163.  
  164. char *
  165. mymalloc(len)
  166. int len ;
  167. {
  168.    register char *p ;
  169. /*   extern char *malloc() ; */
  170.    extern void* malloc(unsigned) ;
  171.  
  172.    p = malloc((unsigned)len) ;
  173.    if (p==NULL)
  174.       error("! out of memory") ;
  175.    return(p) ;
  176. }
  177.  
  178. char *
  179. paramnewstring() {
  180.    register char *p, *q ;
  181.  
  182.    p = param ;
  183.    while (*p > ' ')
  184.       p++ ;
  185.    q = mymalloc((int)(p-param+1)) ;
  186.    if (*p != 0)
  187.       *p++ = 0 ;
  188.    (void)strcpy(q, param) ;
  189.    while (*p && *p <= ' ')
  190.       p++ ;
  191.    param = p ;
  192.    return(q) ;
  193. }
  194.  
  195. char *
  196. paramstring() {
  197.    register char *p, *q ;
  198.  
  199.    p = param ;
  200.    while (*p > ' ')
  201.       p++ ;
  202.    q = param ;
  203.    if (*p != 0)
  204.       *p++ = 0 ;
  205.    while (*p && *p <= ' ')
  206.       p++ ;
  207.    param = p ;
  208.    return(q) ;
  209. }
  210.  
  211. int
  212. paramnum() {
  213.    register char *p ;
  214.    int i ;
  215.  
  216.    p = paramstring() ;
  217.    if (sscanf(p, "%d", &i) != 1)
  218.       error("! integer expected") ;
  219.    return(i) ;
  220. }
  221.  
  222. float
  223. paramfloat() {
  224.    register char *p ;
  225.    float i ;
  226.  
  227.    p = paramstring() ;
  228.    if (sscanf(p, "%f", &i) != 1)
  229.       error("! number expected") ;
  230.    return(i) ;
  231. }
  232.  
  233. struct adobeinfo *
  234. newchar() {
  235.    register struct adobeinfo *ai ;
  236.  
  237.    ai = (struct adobeinfo *)mymalloc(sizeof(struct adobeinfo)) ;
  238.    ai->adobenum = -1 ;
  239.    ai->texnum = -1 ;
  240.    ai->width = -1 ;
  241.    ai->adobename = NULL ;
  242.    ai->llx = -1 ;
  243.    ai->lly = -1 ;
  244.    ai->urx = -1 ;
  245.    ai->ury = -1 ;
  246.    ai->ligs = NULL ;
  247.    ai->kerns = NULL ;
  248.    ai->pccs = NULL ;
  249.    ai->next = adobechars ;
  250.    adobechars = ai ;
  251.    return(ai) ;
  252. }
  253.  
  254. struct kern *
  255. newkern() {
  256.    register struct kern *nk ;
  257.  
  258.    nk = (struct kern *)mymalloc(sizeof(struct kern)) ;
  259.    nk->next = NULL ;
  260.    nk->succ = NULL ;
  261.    nk->delta = 0 ;
  262.    return(nk) ;
  263. }
  264.  
  265. struct pcc *
  266. newpcc() {
  267.    register struct pcc *np ;
  268.  
  269.    np = (struct pcc *)mymalloc(sizeof(struct pcc)) ;
  270.    np->next = NULL ;
  271.    np->partname = NULL ;
  272.    np->xoffset = 0 ;
  273.    np->yoffset = 0 ;
  274.    return(np) ;
  275. }
  276.  
  277. struct lig *
  278. newlig() {
  279.    register struct lig *nl ;
  280.  
  281.    nl = (struct lig *)mymalloc(sizeof(struct lig)) ;
  282.    nl->next = NULL ;
  283.    nl->succ = NULL ;
  284.    nl->sub = NULL ;
  285.    return(nl) ;
  286. }
  287.  
  288. void
  289. expect(s)
  290. char *s ;
  291. {
  292.    if (strcmp(paramstring(), s) != 0) {
  293.       (void)fprintf(stderr, "%s expected: ", s) ;
  294.       error("! syntax error") ;
  295.    }
  296. }
  297.  
  298. void
  299. handlechar() { /* an input line beginning with C */
  300.    register struct adobeinfo *ai ;
  301.    register struct lig *nl ;
  302.  
  303.    ai = newchar() ;
  304.    ai->adobenum = paramnum() ;
  305.    expect(";") ;
  306.    expect("WX") ;
  307.    ai->width = transform(paramnum(),0) ;
  308.    if (ai->adobenum >= 0) {
  309.       adobeptrs[ai->adobenum] = ai ;
  310.       cksum = (cksum<<1) ^ ai->width ;
  311.    }
  312.    expect(";") ;
  313.    expect("N") ;
  314.    ai->adobename = paramnewstring() ;
  315.    expect(";") ;
  316.    expect("B") ;
  317.    ai->llx = paramnum() ;
  318.    ai->lly = paramnum() ;
  319.    ai->llx = transform(ai->llx, ai->lly) ;
  320.    ai->urx = paramnum() ;
  321.    ai->ury = paramnum() ;
  322.    ai->urx = transform(ai->urx, ai->ury) ;
  323.    expect(";") ;
  324. /* Now look for ligatures (which aren't present in fixedpitch fonts) */
  325.    while (*param == 'L') {
  326.       expect("L") ;
  327.       nl = newlig() ;
  328.       nl->succ = paramnewstring() ;
  329.       nl->sub = paramnewstring() ;
  330.       nl->next = ai->ligs ;
  331.       ai->ligs = nl ;
  332.       expect(";") ;
  333.    }
  334.    if (strcmp(ai->adobename, "space")==0) {
  335.       fontspace = ai->width ;
  336.       nl = newlig() ;        /* space will act as zero-width Polish crossbar */
  337.       nl->succ = "l" ;       /* when used by plain TeX's \l or \L macros */
  338.       nl->sub = "lslash" ;
  339.       nl->next = ai->ligs ;
  340.       ai->ligs = nl ;
  341.       nl = newlig() ;
  342.       nl->succ = "L" ;
  343.       nl->sub = "Lslash" ;
  344.       nl->next = ai->ligs ;
  345.       ai->ligs = nl ;
  346.    } else if (strcmp(ai->adobename, "question")==0) {
  347.       nl = newlig() ;
  348.       nl->succ = "quoteleft" ;
  349.       nl->sub = "questiondown" ;
  350.       nl->next = ai->ligs ;
  351.       ai->ligs = nl ;
  352.    } else if (strcmp(ai->adobename, "exclam")==0) {
  353.       nl = newlig() ;
  354.       nl->succ = "quoteleft" ;
  355.       nl->sub = "exclamdown" ;
  356.       nl->next = ai->ligs ;
  357.       ai->ligs = nl ;
  358.    } else if (! fixedpitch) {
  359.       if (strcmp(ai->adobename, "hyphen")==0) {
  360.          nl = newlig() ;
  361.          nl->succ = "hyphen" ;
  362.          nl->sub = "endash" ;
  363.          nl->next = ai->ligs ;
  364.          ai->ligs = nl ;
  365.       } else if (strcmp(ai->adobename, "endash")==0) {
  366.          nl = newlig() ;
  367.          nl->succ = "hyphen" ;
  368.          nl->sub = "emdash" ;
  369.          nl->next = ai->ligs ;
  370.          ai->ligs = nl ;
  371.       } else if (strcmp(ai->adobename, "quoteleft")==0) {
  372.          nl = newlig() ;
  373.          nl->succ = "quoteleft" ;
  374.          nl->sub = "quotedblleft" ;
  375.          nl->next = ai->ligs ;
  376.          ai->ligs = nl ;
  377.       } else if (strcmp(ai->adobename, "quoteright")==0) {
  378.          nl = newlig() ;
  379.          nl->succ = "quoteright" ;
  380.          nl->sub = "quotedblright" ;
  381.          nl->next = ai->ligs ;
  382.          ai->ligs = nl ;
  383.       }
  384.    }
  385. }
  386.  
  387. struct adobeinfo *
  388. findadobe(p)
  389. char *p ;
  390. {
  391.    register struct adobeinfo *ai ;
  392.  
  393.    for (ai=adobechars; ai; ai = ai->next)
  394.       if (strcmp(p, ai->adobename)==0)
  395.          return(ai) ;
  396.    return(NULL) ;
  397. }
  398.  
  399. /* We ignore kerns before and after space characters, because (1) TeX
  400.  * is using the space only for Polish ligatures, and (2) TeX's
  401.  * boundarychar mechanisms are not oriented to kerns (they apply
  402.  * to both spaces and punctuation) so we don't want to use them. */
  403. void
  404. handlekern() { /* an input line beginning with KPX */
  405.    register struct adobeinfo *ai ;
  406.    register char *p ;
  407.    register struct kern *nk ;
  408.  
  409.    p = paramstring() ;
  410.    if (strcmp(p,"space")==0) return ;
  411.    ai = findadobe(p) ;
  412.    if (ai == NULL)
  413.       error("! kern char not found") ;
  414.    if (ai->adobenum<'0' || ai->adobenum>'9') {
  415. /* Ignore kerns after digits because they would mess up our nice tables */
  416.       nk = newkern() ;
  417.       nk->succ = paramnewstring() ;
  418.       if (strcmp(nk->succ,"space")==0) return ;
  419.       nk->delta = transform(paramnum(),0) ;
  420.       nk->next = ai->kerns ;
  421.       ai->kerns = nk ;
  422.     }
  423. }
  424.  
  425. void
  426. handleconstruct() { /* an input line beginning with CC */
  427.    register struct adobeinfo *ai ;
  428.    register char *p ;
  429.    register struct pcc *np ;
  430.    register int n ;
  431.    struct pcc *npp = NULL;
  432.  
  433.    p = paramstring() ;
  434.    ai = findadobe(p) ;
  435.    if (ai == NULL)
  436.       error("! composite character name not found") ;
  437.    n = paramnum() ;
  438.    expect(";") ;
  439.    while (n--) {
  440.       if (strcmp(paramstring(),"PCC") != 0) return ;
  441.         /* maybe I should expect("PCC") instead, but I'm playing it safe */
  442.       np = newpcc() ;
  443.       np->partname = paramnewstring() ;
  444.       if (findadobe(np->partname)==NULL) return ;
  445.       np->xoffset = paramnum() ;
  446.       np->yoffset = paramnum() ;
  447.       np->xoffset = transform(np->xoffset, np->yoffset) ;
  448.       if (npp) npp->next = np ;
  449.       else ai->pccs = np ;
  450.       npp = np ;
  451.       expect(";") ;
  452.    }
  453. }
  454.  
  455. void
  456. makeaccentligs() {
  457.    register struct adobeinfo *ai, *aci ;
  458.    register char *p ;
  459.    register struct lig *nl ;
  460.    for (ai=adobechars; ai; ai=ai->next) {
  461.       p = ai->adobename ;
  462.       if (strlen(p)>2)
  463.          if ((aci=findadobe(p+1)) && (aci->adobenum > 127)) {
  464.             nl = newlig() ;
  465.             nl->succ = mymalloc(2) ;
  466.             *(nl->succ + 1) = 0 ;
  467.             *(nl->succ) = *p ;
  468.             nl->sub = ai->adobename ;
  469.             nl->next = aci->ligs ;
  470.             aci->ligs = nl ;
  471.          }
  472.    }
  473. }
  474.  
  475. void
  476. readadobe() {
  477.    while (getline()) {
  478.       switch(interest(paramstring())) {
  479. case FontName:
  480.          fontname = paramnewstring() ;
  481.          break ;
  482. case EncodingScheme:
  483.          codingscheme = paramnewstring() ;
  484.          break ;
  485. case ItalicAngle:
  486.          italicangle = paramfloat() ;
  487.          break ;
  488. case IsFixedPitch:
  489.          if (*param == 't' || *param == 'T')
  490.             fixedpitch = 1 ;
  491.          else
  492.             fixedpitch = 0 ;
  493.          break ;
  494. case XHeight:
  495.          xheight = paramnum() ;
  496.          break ;
  497. case C:
  498.          handlechar() ;
  499.          break ;
  500. case KPX:
  501.          handlekern() ;
  502.          break ;
  503. case CC:
  504.          handleconstruct() ;
  505.          break ;
  506. default:
  507.          break ;
  508.       }
  509.    }
  510.    makeaccentligs() ;
  511. }
  512.  
  513. void
  514. assignchars() {
  515.    register char **p ;
  516.    register int i ;
  517.    register struct adobeinfo *ai ;
  518.    int nextfree = 128 ;
  519.  
  520. /*
  521.  *   First, we assign all those that match perfectly.
  522.  */
  523.    for (i=0, p=texencoding; i<128; i++, p++)
  524.       if ((ai=findadobe(*p)) && ai->adobenum >= 0) {
  525.          ai->texnum = i ;
  526.          texptrs[i] = ai ;
  527.       }
  528. /*
  529.  *   Next, we assign all the others whose codes are above 127, retaining
  530.  *   the adobe positions. */
  531.    for (ai=adobechars; ai; ai=ai->next)
  532.       if (ai->adobenum > 127 && ai->texnum<0) {
  533.          ai->texnum = ai->adobenum ;
  534.          texptrs[ai->adobenum] = ai ;
  535.       }
  536. /*   Finally, we map all remaining characters into free locations beginning
  537.  *   with 128, if we know how to construct those characters. */
  538.    for (ai=adobechars; ai; ai=ai->next)
  539.       if (ai->texnum<0 && (ai->adobenum>=0 || ai->pccs !=NULL)) {
  540.          while (texptrs[nextfree]) {
  541.             nextfree=(nextfree+1)&255 ;
  542.             if (nextfree==128) return ; /* all slots full */
  543.          }
  544.          ai->texnum = nextfree ;
  545.          texptrs[nextfree] = ai ;
  546.       }
  547. }
  548.  
  549. void
  550. upmap() { /* Compute uppercase mapping, when making a small caps font */
  551.    register struct adobeinfo *ai, *Ai ;
  552.    register char *p, *q ;
  553.    register struct pcc *np, *nq ;
  554.    char lwr[50] ;
  555.  
  556.    for (Ai=adobechars; Ai; Ai=Ai->next) {
  557.       p = Ai->adobename ;
  558.       if (*p>='A' && *p<='Z') {
  559.          q = lwr ;
  560.          for (; *p; p++)
  561.             *q++ = ((*p>='A' && *p<='Z') ? *p+32 : *p) ;
  562.          *q = 0;
  563.          if (ai=findadobe(lwr)) {
  564.             if (ai->texnum>=0) uppercase[ai->texnum] = Ai ;
  565.             if (Ai->texnum>=0) lowercase[Ai->texnum] = ai ;
  566.          }
  567.       }
  568.    }
  569. /* Note that, contrary to the normal true/false conventions,
  570.  * uppercase[i] is NULL and lowercase[i] is non-NULL when i is the
  571.  * ASCII code of an uppercase letter; and vice versa for lowercase letters */
  572.  
  573.    if (ai=findadobe("germandbls"))
  574.       if (Ai=findadobe("S")) { /* we also construct SS */
  575.          uppercase[ai->texnum] = ai ;
  576.          ai->width = Ai->width << 1 ;
  577.          ai->llx = Ai->llx ;
  578.          ai->lly = Ai->lly ;
  579.          ai->urx = Ai->width + Ai->urx ;
  580.          ai->ury = Ai->ury ;
  581.          ai->kerns = Ai->kerns ;
  582.          np = newpcc() ;
  583.          np->partname = "S" ;
  584.          nq = newpcc() ;
  585.          nq->partname = "S" ;
  586.          nq->xoffset = Ai->width ;
  587.          np->next = nq ;   
  588.          ai->pccs = np ;
  589.       }
  590.    if (ai=findadobe("dotlessi"))
  591.       uppercase[ai->texnum] = findadobe("I") ;
  592.    if (ai=findadobe("dotlessj"))
  593.       uppercase[ai->texnum] = findadobe("J") ;
  594. }
  595. /* The logic above seems to work well enough, but it leaves useless characters
  596.  * like `fi' and `fl' in the font if they were present initially,
  597.  * and it omits characters like `dotlessj' if they are absent initially */
  598.  
  599. /* Now we turn to computing the TFM file */
  600.  
  601. int lf, lh, nw, nh, nd, ni, nl, nk, ne, np ;
  602.  
  603. void
  604. write16(what)
  605. register short what ;
  606. {
  607.    (void)fputc(what >> 8, tfmout) ;
  608.    (void)fputc(what & 255, tfmout) ;
  609. }
  610.  
  611. void
  612. writearr(p, n)
  613. register long *p ;
  614. register int n ;
  615. {
  616.    while (n) {
  617.       write16((short)(*p >> 16)) ;
  618.       write16((short)(*p & 65535)) ;
  619.       p++ ;
  620.       n-- ;
  621.    }
  622. }
  623.  
  624. void
  625. makebcpl(p, s, n)
  626. register long *p ;
  627. register char *s ;
  628. register int n ;
  629. {
  630.    register long t ;
  631.    register long sc ;
  632.  
  633.    if (strlen(s) < n)
  634.       n = strlen(s) ;
  635.    t = ((long)n) << 24 ;
  636.    sc = 16 ;
  637.    while (n > 0) {
  638.       t |= ((long)(*(unsigned char *)s++)) << sc ;
  639.       sc -= 8 ;
  640.       if (sc < 0) {
  641.          *p++ = t ;
  642.          t = 0 ;
  643.          sc = 24 ;
  644.       }
  645.       n-- ;
  646.    }
  647.    *p++ = t ;
  648. }
  649.  
  650. int source[257] ;
  651. int unsort[257] ;
  652.  
  653. /*
  654.  *   Next we need a routine to reduce the number of distinct dimensions
  655.  *   in a TFM file. Given an array what[0]...what[oldn-1], we want to
  656.  *   group its elements into newn clusters, in such a way that the maximum
  657.  *   difference between elements of a cluster is as small as possible.
  658.  *   Furthermore, what[0]=0, and this value must remain in a cluster by
  659.  *   itself. Data such as `0 4 6 7 9' with newn=3 shows that an iterative
  660.  *   scheme in which 6 is first clustered with 7 will not work. So we
  661.  *   borrow a neat algorithm from METAFONT to find the true optimum.
  662.  *   Memory location what[oldn] is set to 0x7fffffffL for convenience.
  663.  */
  664. long nextd ; /* smallest value that will give a different mincover */
  665. int
  666. mincover(what,d) /* tells how many clusters result, given max difference d */
  667. register long d ;
  668. long *what ;
  669. {
  670.    register int m ;
  671.    register long l ;
  672.    register long *p ;
  673.  
  674.    nextd = 0x7fffffffL ;
  675.    p = what+1 ;
  676.    m = 1 ;
  677.    while (*p<0x7fffffffL) {
  678.       m++ ;
  679.       l = *p ;
  680.       while (*++p <= l+d) ;
  681.       if (*p-l < nextd) nextd = *p-l ;
  682.    }
  683.    return (m) ;
  684. }
  685.  
  686. void
  687. remap(what, oldn, newn)
  688. long *what ;
  689. int oldn, newn ;
  690. {
  691.    register int i, j ;
  692.    register long d, l ;
  693.  
  694.    what[oldn] = 0x7fffffffL ;
  695.    for (i=oldn-1; i>0; i--) {
  696.       d = what[i] ;
  697.       for (j=i; what[j+1]<d; j++) {
  698.          what[j] = what[j+1] ;
  699.          source[j] = source[j+1] ;
  700.       }
  701.       what[j] = d ;
  702.       source[j] = i ;
  703.    } /* Tom, don't let me ever catch you using bubblesort again! -- Don */
  704.  
  705.    i = mincover(what, 0L) ;
  706.    d = nextd ;
  707.    while (mincover(what,d+d)>newn) d += d ;
  708.    while (mincover(what,d)>newn) d = nextd ;
  709.  
  710.    i = 1 ;
  711.    j = 0 ;
  712.    while (i<oldn) {
  713.       j++ ;
  714.       l = what[i] ;
  715.       unsort[source[i]] = j ;
  716.       while (what[++i] <= l+d) {
  717.          unsort[source[i]] = j ;
  718.          if (i-j == oldn-newn) d = 0 ;
  719.       }
  720.       what[j] = (l+what[i-1])/2 ;
  721.    }
  722. }
  723.  
  724. /*
  725.  *   The next routine simply scales something.
  726.  *   Input is in 1000ths of an em.  Output is in FIXFACTORths of 1000.
  727.  */
  728. #define FIXFACTOR (0x100000L) /* 2^{20}, the unit fixnum */
  729. long
  730. scale(what)
  731. long what ;
  732. {
  733.    return(((what / 1000) << 20) +
  734.           (((what % 1000) << 20) + 500) / 1000) ;
  735. }
  736.  
  737. long *header, *charinfo, *width, *height, *depth, *ligkern, *kern, *tparam,
  738.      *italic ;
  739. long tfmdata[10000] ;
  740.  
  741. void
  742. buildtfm() {
  743.    register int i, j ;
  744.    register struct adobeinfo *ai ;
  745.    double tan() ;
  746.  
  747.    header = tfmdata ;
  748.    header[0] = cksum ;
  749.    header[1] = 0xa00000 ; /* 10pt design size */
  750.    makebcpl(header+2, codingscheme, 39) ;
  751.    makebcpl(header+12, fontname, 19) ;
  752.    lh = 17 ;
  753.    charinfo = header + lh ;
  754.  
  755.    for (i=0; i<256 && adobeptrs[i]==NULL; i++) ;
  756.    bc = i ;
  757.    for (i=255; i>=0 && adobeptrs[i]==NULL; i--) ;
  758.    ec = i;
  759.    if (ec < bc)
  760.       error("! no Adobe characters") ;
  761.  
  762.    width = charinfo + (ec - bc + 1) ;
  763.    width[0] = 0 ;
  764.    nw++ ;
  765.    for (i=bc; i<=ec; i++)
  766.       if (ai=adobeptrs[i]) {
  767.          width[nw]=ai->width ;
  768.          for (j=1; width[j]!=ai->width; j++) ;
  769.          ai->wptr = j ;
  770.          if (j==nw)
  771.             nw++ ;
  772.       }
  773.    if (nw>256)
  774.       error("! 256 chars with different widths") ;
  775.    depth = width + nw ;
  776.    depth[0] = 0 ;
  777.    nd = 1 ;
  778.    for (i=bc; i<=ec; i++)
  779.       if (ai=adobeptrs[i]) {
  780.          depth[nd] = -ai->lly ;
  781.          for (j=0; depth[j]!=-ai->lly; j++) ;
  782.          ai->dptr = j ;
  783.          if (j==nd)
  784.             nd++ ;
  785.       }
  786.    if (nd > 16) {
  787.       remap(depth, nd, 16) ;
  788.       nd = 16 ;
  789.       for (i=bc; i<=ec; i++)
  790.          if (ai=adobeptrs[i])
  791.             ai->dptr = unsort[ai->dptr] ;
  792.    }
  793.    height = depth + nd ;
  794.    height[0] = 0 ;
  795.    nh = 1 ;
  796.    for (i=bc; i<=ec; i++)
  797.       if (ai=adobeptrs[i]) {
  798.          height[nh]=ai->ury ;
  799.          for (j=0; height[j]!=ai->ury; j++) ;
  800.          ai->hptr = j ;
  801.          if (j==nh)
  802.             nh++ ;
  803.       }
  804.    if (nh > 16) {
  805.       remap(height, nh, 16) ;
  806.       nh = 16 ;
  807.       for (i=bc; i<=ec; i++)
  808.          if (ai=adobeptrs[i])
  809.             ai->hptr = unsort[ai->hptr] ;
  810.    }
  811.    italic  = height + nh ;
  812.    italic[0] = 0 ;
  813.    ni = 1 ;
  814.    for (i=bc; i<=ec; i++)
  815.       if (ai=adobeptrs[i]) {
  816.          italic[ni] = ai->urx - ai->width ;
  817.          if (italic[ni]<0)
  818.             italic[ni] = 0 ;
  819.          for (j=0; italic[j]!=italic[ni]; j++) ;
  820.          ai->iptr = j ;
  821.          if (j==ni)
  822.             ni++ ;
  823.       }
  824.    if (ni > 64) {
  825.       remap(italic, ni, 64) ;
  826.       ni = 64 ;
  827.       for (i=bc; i<=ec; i++)
  828.          if (ai=adobeptrs[i])
  829.             ai->iptr = unsort[ai->iptr] ;
  830.    }
  831.  
  832.    for (i=bc; i<=ec; i++)
  833.       if (ai=adobeptrs[i])
  834.          charinfo[i-bc] = ((long)(ai->wptr)<<24) +
  835.                            ((long)(ai->hptr)<<20) +
  836.                             ((long)(ai->dptr)<<16) +
  837.                              ((long)(ai->iptr)<<10) ;
  838.  
  839.    ligkern = italic + ni ;
  840.    nl = 0 ; /* ligatures and kerns omitted from raw Adobe font */
  841.    kern = ligkern + nl ;
  842.    nk = 0 ;
  843.  
  844.    newslant = (double)slant - efactor * tan(italicangle*(3.1415926535/180.0)) ;
  845.    tparam = kern + nk ;
  846.    tparam[0] = (long)(FIXFACTOR * newslant + 0.5) ;
  847.    tparam[1] = scale((long)(fontspace*efactor+0.5)) ;
  848.    tparam[2] = (fixedpitch ? 0 : scale((long)(300*efactor+0.5))) ;
  849.    tparam[3] = (fixedpitch ? 0 : scale((long)(100*efactor+0.5))) ;
  850.    tparam[4] = scale((long)xheight) ;
  851.    tparam[5] = scale((long)(1000*efactor+0.5)) ;
  852.    np = 6 ;
  853. }
  854.  
  855. void
  856. writesarr(what, len)
  857. long *what ;
  858. int len ;
  859. {
  860.    register long *p ;
  861.    int i ;
  862.  
  863.    p = what ;
  864.    i = len ;
  865.    while (i) {
  866.       *p = scale(*p) ;
  867.       p++ ;
  868.       i-- ;
  869.    }
  870.    writearr(what, len) ;
  871. }
  872.  
  873. void
  874. writetfm() {
  875.    lf = 6 + lh + (ec - bc + 1) + nw + nh + nd + ni + nl + nk + ne + np ;
  876.    write16(lf) ;
  877.    write16(lh) ;
  878.    write16(bc) ;
  879.    write16(ec) ;
  880.    write16(nw) ;
  881.    write16(nh) ;
  882.    write16(nd) ;
  883.    write16(ni) ;
  884.    write16(nl) ;
  885.    write16(nk) ;
  886.    write16(ne) ;
  887.    write16(np) ;
  888.    writearr(header, lh) ;
  889.    writearr(charinfo, ec-bc+1) ;
  890.    writesarr(width, nw) ;
  891.    writesarr(height, nh) ;
  892.    writesarr(depth, nd) ;
  893.    writesarr(italic, ni) ;
  894.    writearr(ligkern, nl) ;
  895.    writesarr(kern, nk) ;
  896.    writearr(tparam, np) ;
  897. }
  898.  
  899. /* OK, the TFM file is done! Now for our next trick, the VPL file. */
  900.  
  901. /* For TeX we want to compute a character height that works properly
  902.  * with accents. The following list of accents doesn't need to be complete. */
  903. char *accents[] = { "acute", "tilde", "caron", "dieresis", NULL} ;
  904. int
  905. texheight(ai)
  906. register struct adobeinfo *ai ;
  907. {
  908.    register char **p;
  909.    register struct adobeinfo *aci, *acci ;
  910.    if (*(ai->adobename + 1)) return (ai->ury) ; /* that was the simple case */
  911.    for (p=accents; *p; p++)  /* otherwise we look for accented letters */
  912.       if (aci=findadobe(*p)) {
  913.          strcpy(buffer,ai->adobename) ;
  914.          strcat(buffer,*p) ;
  915.          if (acci=findadobe(buffer)) return (acci->ury - aci->ury + xheight) ;
  916.       }
  917.    return (ai->ury) ;
  918. }
  919.  
  920. /* modified tgr to eliminate varargs problems */
  921.  
  922. #define vout(s)  fprintf(vplout, s)
  923. int level ; /* the depth of parenthesis nesting in VPL file being written */
  924.  
  925. void vlevout() {
  926.    register int l = level ;
  927.    while (l--) vout("   ") ;
  928. }
  929.  
  930. void vlevnlout() {
  931.    vout("\n") ;
  932.    vlevout() ;
  933. }
  934.  
  935. #define voutln(str) {fprintf(vplout,"%s\n",str);vlevout();}
  936. /*
  937. void
  938. voutln(s)
  939. char *s ;
  940. {
  941.    register char l ;
  942.    fprintf(vplout,"%s\n", s) ;
  943.    for (l=level; l; l--) vout("   ") ;
  944. }
  945. */
  946. #define voutln2(f,s) {fprintf(vplout,f,s);vlevnlout();}
  947. /*
  948. void
  949. voutln2(f,s)
  950. char *f, *s ;
  951. {
  952.    register char l ;
  953.    (void) sprintf(buffer, f, s) ;
  954.    fprintf(vplout,"%s\n", buffer) ;
  955.    for (l=level; l; l--) vout("   ") ;
  956. }
  957. */
  958. #define voutln3(f,a,b) {fprintf(vplout,f,a,b);vlevnlout();}
  959. /*
  960. void
  961. voutln3(f,s1,s2)
  962. char *f, *s1, *s2 ;
  963. {
  964.    register char l ;
  965.    (void) sprintf(buffer, f, s1, s2) ;
  966.    fprintf(vplout,"%s\n", buffer) ;
  967.    for (l=level; l; l--) vout("   ") ;
  968. }
  969. */
  970. void
  971. vleft()
  972. {
  973.    level++ ;
  974.    vout("(") ;
  975. }
  976.  
  977. void
  978. vright()
  979. {
  980.    level-- ;
  981.    voutln(")") ;
  982. }
  983.  
  984. char vcharbuf[6] ;
  985. char *vchar(c)
  986. int c ;
  987. {
  988.    if ((c>='0' && c<='9')||(c>='A' && c<='Z')||(c>='a' && c<='z'))
  989.       (void) sprintf(vcharbuf,"C %c", c) ;
  990.    else (void) sprintf(vcharbuf,"O %o", c) ;
  991.    return (vcharbuf) ;
  992. }
  993.  
  994. void
  995. writevpl()
  996. {
  997.    register int i ;
  998.    register struct adobeinfo *ai ;
  999.    register struct lig *nlig ;
  1000.    register struct kern *nkern ;
  1001.    register struct pcc *npcc ;
  1002.    struct adobeinfo *asucc, *asub, *api ;
  1003.    int xoff, yoff, ht ;
  1004.    char unlabeled ;
  1005.    double tan() ;
  1006.  
  1007.    voutln2("(VTITLE Created by %s)", titlebuf) ;
  1008.    voutln("(COMMENT Please edit that VTITLE if you edit this file)") ;
  1009.    (void)sprintf(obuffer, "TeX-%s%s%s%s", outname,
  1010.       (efactor==1.0? "" : "-E"), (slant==0.0? "" : "-S"),
  1011.                  (makevpl==1? "" : "-CSC")) ;
  1012.    if (strlen(obuffer)>19) { /* too long, will retain first 9 and last 10 */
  1013.       register char *p, *q ;
  1014.       for (p = &obuffer[9], q = &obuffer[strlen(obuffer)-10] ; p<&obuffer[19];
  1015.               p++, q++) *p = *q ;
  1016.       obuffer[19] = '\0' ;
  1017.    }
  1018.    voutln2("(FAMILY %s)" , obuffer) ;
  1019.    voutln2("(CODINGSCHEME TeX text + %s)", codingscheme) ;
  1020.    voutln("(DESIGNSIZE R 10.0)") ;
  1021.    voutln("(DESIGNUNITS R 1000)") ;
  1022.    voutln("(COMMENT DESIGNSIZE (1 em) IS IN POINTS)") ;
  1023.    voutln("(COMMENT OTHER DIMENSIONS ARE MULTIPLES OF DESIGNSIZE/1000)") ;
  1024.    voutln2("(CHECKSUM O %lo)",cksum ^ 0xffffffff) ;
  1025.    vleft() ; voutln("FONTDIMEN") ;
  1026.    if (newslant)
  1027.       voutln2("(SLANT R %f)", newslant) ;
  1028.    voutln2("(SPACE D %d)", transform(fontspace,0)) ;
  1029.    if (! fixedpitch) {
  1030.       voutln2("(STRETCH D %d)", transform(200,0)) ;
  1031.       voutln2("(SHRINK D %d)", transform(100,0)) ;
  1032.    }
  1033.    voutln2("(XHEIGHT D %d)", xheight) ;
  1034.    voutln2("(QUAD D %d)", transform(1000,0)) ;
  1035.    voutln2("(EXTRASPACE D %d)", transform(fixedpitch ? fontspace : 111, 0)) ;
  1036.    vright() ;
  1037.    vleft() ; voutln("MAPFONT D 0");
  1038.    voutln2("(FONTNAME %s)", outname) ;
  1039.    voutln2("(FONTCHECKSUM O %lo)", cksum) ;
  1040.    vright() ;
  1041.    if (makevpl>1) {
  1042.       vleft() ; voutln("MAPFONT D 1");
  1043.       voutln2("(FONTNAME %s)", outname) ;
  1044.       voutln("(FONTAT D 800)") ;
  1045.       voutln2("(FONTCHECKSUM O %lo)", cksum) ;
  1046.       vright() ;
  1047.    }
  1048.  
  1049.    for (i=0; i<256 && texptrs[i]==NULL; i++) ;
  1050.    bc = i ;
  1051.    for (i=255; i>=0 && texptrs[i]==NULL; i--) ;
  1052.    ec = i;
  1053.  
  1054.    vleft() ; voutln("LIGTABLE") ;
  1055.    for (i=bc; i<=ec; i++)
  1056.       if (ai=texptrs[i]) {
  1057.          unlabeled = 1 ;
  1058.          if (uppercase[i]==NULL) /* omit ligatures from smallcap lowercase */
  1059.             for (nlig=ai->ligs; nlig; nlig=nlig->next)
  1060.                if (asucc=findadobe(nlig->succ))
  1061.                   if (asucc->texnum>=0)
  1062.                      if (asub=findadobe(nlig->sub))
  1063.                         if (asub->texnum>=0) {
  1064.                            if (unlabeled) {
  1065.                               voutln2("(LABEL %s)", vchar(i)) ;
  1066.                               unlabeled = 0 ;
  1067.                            }
  1068.                            voutln3("(LIG %s O %o)", vchar(asucc->texnum),
  1069.                                 asub->texnum) ;
  1070.                         }
  1071.          for (nkern = (uppercase[i] ? uppercase[i]->kerns : ai->kerns);
  1072.                     nkern; nkern=nkern->next)
  1073.             if (asucc=findadobe(nkern->succ))
  1074.                if (asucc->texnum>=0)
  1075.                   if (uppercase[asucc->texnum]==NULL) {
  1076.                      if (unlabeled) {
  1077.                         voutln2("(LABEL %s)", vchar(i)) ;
  1078.                         unlabeled = 0 ;
  1079.                      }
  1080.                      if (uppercase[i]) {
  1081.                         if (lowercase[asucc->texnum]) {
  1082.                            voutln3("(KRN %s R %.1f)",
  1083.                                  vchar(lowercase[asucc->texnum]->texnum),
  1084.                                  0.8*nkern->delta) ;
  1085.                         } else voutln3("(KRN %s R %.1f)",
  1086.                                  vchar(asucc->texnum), 0.8*nkern->delta) ;
  1087.                      } else {
  1088.                         voutln3("(KRN %s R %d)", vchar(asucc->texnum),
  1089.                                 nkern->delta) ;
  1090.                         if (lowercase[asucc->texnum])
  1091.                            if (lowercase[asucc->texnum]->texnum>=0)
  1092.                               voutln3("(KRN %s R %.1f)",
  1093.                                 vchar(lowercase[asucc->texnum]->texnum),
  1094.                                 0.8*nkern->delta) ;
  1095.                      }
  1096.                   }
  1097.          if (! unlabeled) voutln("(STOP)") ;
  1098.       }
  1099.    vright() ;
  1100.  
  1101.    for (i=bc; i<=ec; i++)
  1102.       if (ai=texptrs[i]) {
  1103.          vleft() ; fprintf(vplout, "CHARACTER %s", vchar(i)) ;
  1104.          if (*vcharbuf=='C') {
  1105.             voutln("") ;
  1106.          } else
  1107.             voutln2(" (comment %s)", ai->adobename) ;
  1108.          if (uppercase[i]) {
  1109.             ai=uppercase[i] ;
  1110.             voutln2("(CHARWD R %.1f)", 0.8 * (ai->width)) ;
  1111.             if (ht=texheight(ai)) voutln2("(CHARHT R %.1f)", 0.8 * ht) ;
  1112.             if (ai->lly) voutln2("(CHARDP R %.1f)", -0.8 * ai->lly) ;
  1113.             if (ai->urx > ai->width)
  1114.                voutln2("(CHARIC R %.1f)", 0.8 * (ai->urx - ai->width)) ;
  1115.          } else {
  1116.             voutln2("(CHARWD R %d)", ai->width) ;
  1117.             if (ht=texheight(ai)) voutln2("(CHARHT R %d)", ht) ;
  1118.             if (ai->lly) voutln2("(CHARDP R %d)", -ai->lly) ;
  1119.             if (ai->urx > ai->width)
  1120.                voutln2("(CHARIC R %d)", ai->urx - ai->width) ;
  1121.          }
  1122.          if (ai->adobenum != i || uppercase[i]) {
  1123.             vleft() ; voutln("MAP") ;
  1124.             if (uppercase[i]) voutln("(SELECTFONT D 1)") ;
  1125.             if (ai->pccs) {
  1126.                xoff = 0 ; yoff = 0 ;
  1127.                for (npcc = ai->pccs; npcc; npcc=npcc->next)
  1128.                   if (api=findadobe(npcc->partname))
  1129.                      if (api->texnum>=0) {
  1130.                         if (npcc->xoffset != xoff) {
  1131.                            if (uppercase[i]) {
  1132.                               voutln2("(MOVERIGHT R %.1f)",
  1133.                                       0.8 * (npcc->xoffset - xoff)) ;
  1134.                            } else voutln2("(MOVERIGHT R %d)",
  1135.                                       npcc->xoffset - xoff) ;
  1136.                            xoff = npcc->xoffset ;
  1137.                         }
  1138.                         if (npcc->yoffset != yoff) {
  1139.                            if (uppercase[i]) {
  1140.                               voutln2("(MOVEUP R %.1f)",
  1141.                                       0.8 * (npcc->yoffset - yoff)) ;
  1142.                            } else voutln2("(MOVEUP R %d)",
  1143.                                       npcc->yoffset - yoff) ;
  1144.                            yoff = npcc->yoffset ;
  1145.                         }
  1146.                         voutln2("(SETCHAR O %o)", api->adobenum) ;
  1147.                         xoff += texptrs[api->texnum]->width ;
  1148.                      }
  1149.             } else voutln2("(SETCHAR O %o)", ai->adobenum) ;
  1150.             vright() ;
  1151.          }
  1152.          vright() ;
  1153.       }
  1154.    if (level) error("! I forgot to match the parentheses") ;
  1155. }
  1156.  
  1157. void
  1158. openfiles(argc, argv)
  1159. int argc ;
  1160. char *argv[] ;
  1161. {
  1162.    register int lastext ;
  1163.    register int i ;
  1164.    extern void exit() ;
  1165.    tfmout = (FILE *)NULL ;
  1166.  
  1167.    if (argc == 1) {
  1168.       (void)printf("afm2tfm 5.36, Copyright 1990 by Radical Eye Software\n") ;
  1169.       (void)
  1170.         printf("Usage: afm2tfm foo[.afm] [-v|-V bar[.vpl]] [foo[.tfm]]\n") ;
  1171.      exit(0) ;
  1172.    }
  1173.  
  1174.    (void)sprintf(titlebuf, "%s %s", argv[0], argv[1]) ;
  1175.    (void)strcpy(inname, argv[1]) ;
  1176.    lastext = -1 ;
  1177.    for (i=0; inname[i]; i++)
  1178.       if (inname[i] == '.')
  1179.          lastext = i ;
  1180.       else if (inname[i] == '/' || inname[i] == ':')
  1181.          lastext = -1 ;
  1182.    if (lastext == -1) (void)strcat(inname, ".afm") ;
  1183.    if ((afmin=fopen(inname, "r"))==NULL)
  1184.       error("! can't open afm input file") ;
  1185.  
  1186.    while (argc>3 && *argv[2]=='-') {
  1187.       switch (argv[2][1]) {
  1188. case 'V': makevpl++ ;
  1189. case 'v': makevpl++ ;
  1190.          (void)strcpy(outname, argv[3]) ;
  1191.          lastext = -1 ;
  1192.          for (i=0; outname[i]; i++)
  1193.             if (outname[i] == '.')
  1194.                lastext = i ;
  1195.             else if (outname[i] == '/' || outname[i] == ':')
  1196.                lastext = -1 ;
  1197.          if (lastext == -1) (void)strcat(outname, ".vpl") ;
  1198.          if ((vplout=fopen(outname, WRITEBIN))==NULL)
  1199.             error("! can't open vpl output file") ;
  1200.          break ;
  1201. case 'e': if (sscanf(argv[3], "%f", &efactor)==0 || efactor<0.01)
  1202.             error("! Bad extension factor") ;
  1203.          break ;
  1204. case 's': if (sscanf(argv[3], "%f", &slant)==0)
  1205.             error("! Bad slant parameter") ;
  1206.          break ;
  1207. case 't':  /* get tfm name if present */
  1208.          (void)strcpy(outname, argv[3]) ;
  1209.          lastext = -1 ;
  1210.          for (i=0; outname[i]; i++)
  1211.             if (outname[i] == '.')
  1212.                lastext = i ;
  1213.             else if (outname[i] == '/' || outname[i] == ':')
  1214.                     lastext = -1 ;
  1215.         if (lastext == -1) {
  1216.            lastext = strlen(outname) ;
  1217.            (void)strcat(outname, ".tfm") ;
  1218.         }
  1219.         if ((tfmout=fopen(outname, WRITEBIN))==NULL)
  1220.            error("! can't open tfm output file") ;
  1221.         break ;
  1222. default: (void)fprintf(stderr, "Unknown option %s %s will be ignored.\n",
  1223.                          argv[2], argv[3]) ;
  1224.       }
  1225.       (void)sprintf(titlebuf, "%s %s %s", titlebuf, argv[2], argv[3]) ;
  1226.       argv += 2 ;
  1227.       argc -= 2 ;
  1228.    }
  1229.  
  1230.    if (argc>3 || (argc==3 && *argv[2]=='-'))
  1231.       error("! Usage: afm2tfm foo[.afm] [-v|-V bar[.vpl]] [foo[.tfm]]") ;
  1232.          
  1233.    if (argc == 2) (void)strcpy(outname, inname) ;
  1234.    else (void)strcpy(outname, argv[2]) ;
  1235.  
  1236.    lastext = -1 ;
  1237.    for (i=0; outname[i]; i++)
  1238.       if (outname[i] == '.')
  1239.          lastext = i ;
  1240.       else if (outname[i] == '/' || outname[i] == ':' || outname[i] == '\\')
  1241.          lastext = -1 ;
  1242.    if (argc == 2) {
  1243.       outname[lastext] = 0 ;
  1244.       lastext = -1 ;
  1245.    }
  1246.    if (lastext == -1) {
  1247.       lastext = strlen(outname) ;
  1248.       (void)strcat(outname, ".tfm") ;
  1249.    }
  1250.    if (tfmout == NULL && (tfmout=fopen(outname, WRITEBIN))==NULL)
  1251.       error("! can't open tfm output file") ;
  1252.    outname[lastext] = 0 ;
  1253. /*
  1254.  *   Now we strip off any directory information, so we only use the
  1255.  *   base name in the vf file.  We accept any of /, :, or \ as directory
  1256.  *   delimiters, so none of these are available for use inside the
  1257.  *   base name; this shouldn't be a problem.
  1258.  */
  1259.    for (i=0, lastext=0; outname[i]; i++)
  1260.       if (outname[i] == '/' || outname[i] == ':' || outname[i] == '\\')
  1261.          lastext = i + 1 ;
  1262.    if (lastext)
  1263.       strcpy(outname, outname + lastext) ;
  1264. }
  1265.  
  1266. #ifndef VMS
  1267. void
  1268. #endif
  1269. main(argc, argv)
  1270. int argc ;
  1271. char *argv[] ;
  1272. {
  1273.    extern void exit() ;
  1274.  
  1275.    openfiles(argc, argv) ;
  1276.    readadobe() ;
  1277.    buildtfm() ;
  1278.    writetfm() ;
  1279.    if (makevpl) {
  1280.       assignchars() ;
  1281.       if (makevpl>1) upmap() ;
  1282.       writevpl() ;
  1283.    }
  1284.    exit(0) ;
  1285.    /*NOTREACHED*/
  1286. }
  1287.