home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fonts 1 / freshfonts1.bin / bbs / programs / amiga / pastex13.lha / DVIPS / dvips5519.lha / dvips / afm2tfm.c < prev    next >
C/C++ Source or Header  |  1993-08-12  |  53KB  |  1,886 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. /*   VM/CMS port by J. Hafner (hafner@almaden.ibm.com), based on
  10.  *   the port by Alessio Guglielmi (guglielmi@ipisnsib.bitnet)
  11.  *   and Marco Prevedelli (prevedelli@ipisnsva.bitnet).
  12.  *   This port is still in test state.  No guarantees.
  13.  *   11/3/92: more corrections to VM/CMS port. Now it looks correct
  14.  *   and will be supported by J. Hafner.
  15.  *
  16. */
  17.  
  18. #include <stdio.h>
  19. #if defined(SYSV) || defined(VMS) || defined(__THINK__) || defined(MSDOS)
  20. #include <string.h>
  21. #else
  22. #include <strings.h>
  23. #endif
  24. #include <math.h>
  25.  
  26. /* JLH: added these to make the code easier to read and remove some
  27.    ascii<->ebcdic dependencies */
  28. #define ASCII_A 65
  29. #define ASCII_Z 90
  30. #define ASCII_a 97
  31. #define ASCII_z 122
  32. #define ASCII_0 48
  33. #define ASCII_9 57
  34.  
  35. #ifdef VMCMS
  36. #define interesting lookstr  /* for 8 character truncation conflicts */
  37. #include "dvipscms.h"
  38. extern FILE *cmsfopen() ;
  39. extern char ebcdic2ascii[] ;
  40. extern char ascii2ebcdic[] ;
  41. #ifdef fopen
  42. #undef fopen
  43. #endif
  44. #define fopen cmsfopen
  45. #endif
  46.  
  47. #if defined MSDOS || defined OS2
  48. #define WRITEBIN "wb"
  49. #else
  50. #ifdef VMCMS
  51. #define WRITEBIN "wb, lrecl=1024, recfm=f"
  52. #else
  53. #define WRITEBIN "w"
  54. #endif
  55. #endif
  56.  
  57. #ifdef __TURBOC__
  58. #define SMALLMALLOC
  59. #endif
  60.  
  61. struct encoding {
  62.    char *name ;
  63.    char *vec[256] ;
  64. } ;
  65. struct encoding staticencoding = {
  66.   "TeX text",
  67.   {"Gamma", "Delta", "Theta", "Lambda", "Xi", "Pi", "Sigma",
  68.    "Upsilon", "Phi", "Psi", "Omega", "arrowup", "arrowdown", "quotesingle",
  69.    "exclamdown", "questiondown", "dotlessi", "dotlessj", "grave", "acute",
  70.    "caron", "breve", "macron", "ring", "cedilla", "germandbls", "ae", "oe",
  71.    "oslash", "AE", "OE", "Oslash", "space", "exclam", "quotedbl", "numbersign",
  72.    "dollar", "percent", "ampersand", "quoteright", "parenleft", "parenright",
  73.    "asterisk", "plus", "comma", "hyphen", "period", "slash", "zero", "one",
  74.    "two", "three", "four", "five", "six", "seven", "eight", "nine", "colon",
  75.    "semicolon", "less", "equal", "greater", "question", "at", "A", "B", "C",
  76.    "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
  77.    "S", "T", "U", "V", "W", "X", "Y", "Z", "bracketleft", "backslash",
  78.    "bracketright", "circumflex", "underscore", "quoteleft", "a", "b", "c", "d",
  79.    "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
  80.    "t", "u", "v", "w", "x", "y", "z", "braceleft", "bar", "braceright",
  81.    "tilde", "dieresis", "", "", "", "", "", "", "", "", "", "", "", "", "",
  82.     "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  83.     "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  84.     "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  85.     "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  86.     "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  87.     "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  88.     "", "", "", "", "", "", "" } } ;
  89. /*
  90.  *   It's easier to put this in static storage and parse it as we go
  91.  *   than to build the structures ourselves.
  92.  */
  93. char *staticligkern[] = {
  94.    "% LIGKERN space l =: lslash ; space L =: Lslash ;",
  95.    "% LIGKERN question quoteleft =: questiondown ;",
  96.    "% LIGKERN exclam quoteleft =: exclamdown ;",
  97.    "% LIGKERN hyphen hyphen =: endash ; endash hyphen =: emdash ;",
  98.    "% LIGKERN quoteleft quoteleft =: quotedblleft ;",
  99.    "% LIGKERN quoteright quoteright =: quotedblright ;",
  100.    "% LIGKERN space {} * ; * {} space ; zero {} * ; * {} zero ;",
  101.    "% LIGKERN one {} * ; * {} one ; two {} * ; * {} two ;",
  102.    "% LIGKERN three {} * ; * {} three ; four {} * ; * {} four ;",
  103.    "% LIGKERN five {} * ; * {} five ; six {} * ; * {} six ;",
  104.    "% LIGKERN seven {} * ; * {} seven ; eight {} * ; * {} eight ;",
  105.    "% LIGKERN nine {} * ; * {} nine ;",
  106. /*
  107.  *   These next are only included for deficient afm files that
  108.  *   have the lig characters but not the lig commands.
  109.  */
  110.    "% LIGKERN f i =: fi ; f l =: fl ; f f =: ff ; ff i =: ffi ;",
  111.    "% LIGKERN ff l =: ffl ;",
  112.    0 } ;
  113. /*
  114.  *   The above layout corresponds to TeX Typewriter Type and is compatible
  115.  *   with TeX Text because the position of ligatures is immaterial.
  116.  */
  117. struct encoding *outencoding = 0 ;
  118. struct encoding *inencoding = 0 ;
  119. char *outenname, *inenname ;/* the file names for input and output encodings */
  120. int boundarychar = -1 ;     /* the boundary character */
  121. int ignoreligkern ;         /* do we look at ligkern info in the encoding? */
  122. /*
  123.  *   This is what we store Adobe data in.
  124.  */
  125. struct adobeinfo {
  126.    struct adobeinfo *next ;
  127.    int adobenum, texnum, width ;
  128.    char *adobename ;
  129.    int llx, lly, urx, ury ;
  130.    struct lig *ligs ;
  131.    struct kern *kerns ;
  132.    struct pcc *pccs ;
  133.    int wptr, hptr, dptr, iptr ;
  134. } *adobechars, *adobeptrs[256], *texptrs[256],
  135.   *uppercase[256], *lowercase[256] ;
  136. int nexttex[256] ; /* for characters encoded multiple times in output */
  137. /*
  138.  *   These are the eight ligature ops, in VPL terms and in METAFONT terms.
  139.  */
  140. char *vplligops[] = {
  141.    "LIG", "/LIG", "/LIG>", "LIG/", "LIG/>", "/LIG/", "/LIG/>", "/LIG/>>", 0
  142. } ;
  143. char *encligops[] = {
  144.    "=:", "|=:", "|=:>", "=:|", "=:|>", "|=:|", "|=:|>", "|=:|>>", 0
  145. } ;
  146. struct lig {
  147.    struct lig *next ;
  148.    char *succ, *sub ;
  149.    short op, boundleft ;
  150. } ;
  151. struct kern {
  152.    struct kern *next ;
  153.    char *succ ;
  154.    int delta ;
  155. } ;
  156. struct pcc {
  157.    struct pcc *next ;
  158.    char * partname ;
  159.    int xoffset, yoffset ;
  160. } ;
  161.  
  162. FILE *afmin, *vplout, *tfmout ;
  163. char inname[200], outname[200] ; /* names of input and output files */
  164. char buffer[255]; /* input buffer (modified while parsing) */
  165. char obuffer[255] ; /* unmodified copy of input buffer */
  166. char *param ; /* current position in input buffer */
  167. char *fontname = "Unknown" ;
  168. char *codingscheme = "Unspecified" ;
  169. #ifdef VMCMS
  170. char *ebfontname ;
  171. char *ebcodingscheme ;
  172. #endif
  173. float italicangle = 0.0 ;
  174. char fixedpitch ;
  175. char makevpl ;
  176. char pedantic ;
  177. int xheight = 400 ;
  178. int fontspace ;
  179. int bc, ec ;
  180. long cksum ;
  181. float efactor = 1.0, slant = 0.0 ;
  182. float capheight = 0.8 ;
  183. char *efactorparam, *slantparam ;
  184. double newslant ;
  185. char titlebuf[500] ;
  186.  
  187. void
  188. error(s)
  189. register char *s ;
  190. {
  191.    extern void exit() ;
  192.  
  193.    (void)fprintf(stderr, "%s\n", s) ;
  194.    if (obuffer[0]) {
  195.       (void)fprintf(stderr, "%s\n", obuffer) ;
  196.       while (param > buffer) {
  197.          (void)fprintf(stderr, " ") ;
  198.          param-- ;
  199.       }
  200.       (void)fprintf(stderr, "^\n") ;
  201.    }
  202.    if (*s == '!')
  203.       exit(1) ;
  204. }
  205.  
  206. int
  207. transform(x,y)
  208.    register int x,y ;
  209. {
  210.    register double acc ;
  211.    acc = efactor * x + slant *y ;
  212.    return (int)(acc>=0? acc+0.5 : acc-0.5 ) ;
  213. }
  214.  
  215. int
  216. getline() {
  217.    register char *p ;
  218.    register int c ;
  219.  
  220.    param = buffer ;
  221.    for (p=buffer; (c=getc(afmin)) != EOF && c != '\n';)/* changed 10 to '\n' */
  222.       *p++ = c ;
  223.    *p = 0 ;
  224.    (void)strcpy(obuffer, buffer) ;
  225.    if (p == buffer && c == EOF)
  226.       return(0) ;
  227.    else
  228.       return(1) ;
  229. }
  230.  
  231. char *interesting[] = { "FontName", "ItalicAngle", "IsFixedPitch",
  232.    "XHeight", "C", "KPX", "CC", "EncodingScheme", NULL} ;
  233. #define FontName (0)
  234. #define ItalicAngle (1)
  235. #define IsFixedPitch (2)
  236. #define XHeight (3)
  237. #define C (4)
  238. #define KPX (5)
  239. #define CC (6)
  240. #define EncodingScheme (7)
  241. #define NONE (-1)
  242. int
  243. interest(s)
  244. char *s ;
  245. {
  246.    register char **p ;
  247.    register int n ;
  248.  
  249.    for (p=interesting, n=0; *p; p++, n++)
  250.       if (strcmp(s, *p)==0)
  251.          return(n) ;
  252.    return(NONE) ;
  253. }
  254.  
  255. char *
  256. mymalloc(len)
  257. unsigned long len ;
  258. {
  259.    register char *p ;
  260.    int i ;
  261. #ifndef IBM6000
  262. #if defined MSDOS || defined OS2
  263.    extern void *malloc() ;
  264. #else
  265.    extern char *malloc() ;
  266. #endif
  267. #endif
  268.  
  269. #ifdef SMALLMALLOC
  270.    if (len > 65500L)
  271.       error("! can't allocate more than 64K!") ;
  272. #endif
  273.    p = malloc((unsigned)len) ;
  274.    if (p==NULL)
  275.       error("! out of memory") ;
  276.    for (i=0; i<len; i++)
  277.       p[i] = 0 ;
  278.    return(p) ;
  279. }
  280.  
  281. char *
  282. newstring(s)
  283. char *s ;
  284. {
  285.    char *q = mymalloc((unsigned lon