5 #define MAXCH NCHARS /* maximum number of global char names */
6 char *chnames[MAXCH]; /* chnames[n-ALPHABET] -> name of char n */
7 int nchnames; /* number of Cxy names currently seen */
9 #define MAXPS 100 /* max number of point sizes */
10 int pstab[MAXPS]; /* point sizes */
11 int nsizes; /* number in DESC */
13 Font fonts[MAXFONTS+1]; /* font info + ptr to width info */
16 #define skipline(f) while (getc(f) != '\n')
18 #define eq(s1, s2) (strcmp(s1, s2) == 0)
23 char cmd[100], s[100];
26 if ((fin = fopen(name, "r")) == NULL)
28 while (fscanf(fin, "%s", cmd) != EOF) {
29 if (strcmp(cmd, "res") == 0) {
30 fscanf(fin, "%d", &Inch);
31 } else if (strcmp(cmd, "hor") == 0) {
32 fscanf(fin, "%d", &Hor);
33 } else if (strcmp(cmd, "vert") == 0) {
34 fscanf(fin, "%d", &Vert);
35 } else if (strcmp(cmd, "unitwidth") == 0) {
36 fscanf(fin, "%d", &Unitwidth);
37 } else if (strcmp(cmd, "sizes") == 0) {
39 while (fscanf(fin, "%d", &v) != EOF && v != 0 && nsizes < MAXPS)
41 } else if (strcmp(cmd, "fonts") == 0) {
42 fscanf(fin, "%d", &nfonts);
43 for (i = 1; i <= nfonts; i++) {
45 fontlab[i] = PAIR(s[0], s[1]);
47 } else if (strcmp(cmd, "charset") == 0) { /* add any names */
48 while (fscanf(fin, "%s", s) != EOF)
49 chadd(s, Troffchar, Install);
53 just skip anything else */
60 static int checkfont(char *name)
61 { /* in case it's not really a font description file */
62 /* really paranoid, but consider \f. */
64 char buf[300], buf2[300];
67 if ((fp = fopen(name, "r")) == NULL)
69 for (i = 1; i <= 10; i++) {
70 if (fgets(buf, sizeof buf, fp) == NULL)
72 sscanf(buf, "%s", buf2);
77 if (eq(buf2, "name") || eq(buf2, "fontname") ||
78 eq(buf2, "special") || eq(buf2, "charset")) {
88 getfont(char *name, int pos) /* create width tab for font */
91 Font *ftemp = &fonts[pos];
94 int i, nw, n, wid, kern, code, type;
95 char buf[100], ch[100], s1[100], s2[100], s3[100], cmd[300];
97 /* fprintf(stderr, "read font %s onto %d\n", name, pos); */
98 if (checkfont(name) == -1)
100 if ((fin = fopen(name, "r")) == NULL)
102 for (i = 0; i < ALPHABET; i++)
103 chtemp[i] = chinit; /* zero out to begin with */
104 ftemp->specfont = ftemp->ligfont = 0;
105 ftemp->defaultwidth = ftemp->spacewidth = Inch * Unitwidth / 72 / 3; /* should be rounded */
106 while (fscanf(fin, "%s", cmd) != EOF) {
107 if (strcmp(cmd, "name") == 0)
108 fscanf(fin, "%s", ftemp->longname);
109 else if (strcmp(cmd, "special") == 0)
111 else if (strcmp(cmd, "ligatures") == 0) {
112 ftemp->ligfont = getlig(fin);
113 } else if (strcmp(cmd, "spacewidth") == 0) {
114 fscanf(fin, "%d", &ftemp->spacewidth);
115 } else if (strcmp(cmd, "defaultwidth") == 0) {
116 fscanf(fin, "%d", &ftemp->defaultwidth);
117 } else if (strcmp(cmd, "charset") == 0) {
121 while (fgets(buf, sizeof buf, fin) != NULL) {
122 sscanf(buf, "%s %s %s %s", ch, s1, s2, s3);
123 if (s1[0] != '"') { /* genuine new character */
124 sscanf(s1, "%d", &wid);
125 sscanf(s2, "%d", &kern);
126 code = strtol(s3, 0, 0); /* dec/oct/hex */
128 /* otherwise it's a synonym for prev character, */
129 /* so leave previous values intact */
132 /* decide what kind of alphabet it might come from here */
135 if (strlen(ch) == 1) { /* it's ascii */
136 n = ch[0]; /* origin includes non-graphics */
137 chtemp[n].num = ch[0];
138 } else if (ch[0] == '\\' && ch[1] == '0') {
139 n = strtol(ch+1, 0, 0); /* \0octal or \0xhex */
142 } else if (mbtowc(&wc, ch, strlen(ch)) > 1) {
143 chtemp[nw].num = chadd(ch, MBchar, Install);
148 if (strcmp(ch, "---") == 0) { /* no name */
149 sprintf(ch, "%d", code);
153 chtemp[nw].num = chadd(ch, type, Install);
158 chtemp[n].kern = kern;
159 chtemp[n].code = code;
160 /*fprintf(stderr, "font %2.2s char %4.4s num %3d wid %2d code %3d\n",
161 ftemp->longname, ch, n, wid, code);
169 chtemp[' '].wid = ftemp->spacewidth; /* width of space on this font */
172 free(ftemp->wp); /* god help us if this wasn't allocated */
173 ftemp->wp = (Chwid *) malloc(nw * sizeof(Chwid));
174 if (ftemp->wp == NULL)
176 for (i = 0; i < nw; i++)
177 ftemp->wp[i] = chtemp[i];
179 * printf("%d chars: ", nw);
180 * for (i = 0; i < nw; i++)
181 * if (ftemp->wp[i].num > 0 && ftemp->wp[i].num < ALPHABET) {
182 * printf("%c %d ", ftemp->wp[i].num, ftemp->wp[i].wid);
183 * else if (i >= ALPHABET)
184 * printf("%d (%s) %d ", ftemp->wp[i].num,
185 * chnames[ftemp->wp[i].num-ALPHABET], ftemp->wp[i].wid);
192 chadd(char *s, int type, int install) /* add s to global character name table; */
193 { /* or just look it up */
195 /* a temporary kludge: store the "type" as the first character */
196 /* of the string, so we can remember from whence it came */
201 /* fprintf(stderr, "into chadd %s %c %c\n", s, type, install); /* */
202 for (i = 0; i < nchnames; i++)
203 if (type == chnames[i][0] && eq(s, chnames[i]+1)) /* +1 since type at front */
205 /* fprintf(stderr, "i %d, nchnames %d\n", i, nchnames); /* */
206 if (i < nchnames) /* found same type and bytes at position i */
208 else if (install == Lookup) /* not found, and we were just looking */
211 chnames[nchnames] = p = (char *) malloc(strlen(s)+1+1); /* type + \0 */
213 ERROR "out of space adding character %s", s WARN;
216 if (nchnames >= NCHARS - ALPHABET) {
217 ERROR "out of table space adding character %s", s WARN;
220 strcpy(chnames[nchnames]+1, s);
221 chnames[nchnames][0] = type;
222 /* fprintf(stderr, "installed %c%s at %d\n", type, s, nchnames); /* */
223 return nchnames++ + ALPHABET;
226 char *chname(int n) /* return string for char with index n */
227 { /* includes type char at front, to be peeled off elsewhere */
228 if (n >= ALPHABET && n < nchnames + ALPHABET)
229 return chnames[n-ALPHABET];
234 getlig(FILE *fin) /* pick up ligature list */
240 while (fscanf(fin, "%s", temp) != EOF && strcmp(temp, "0") != 0) {
241 if (strcmp(temp, "fi") == 0)
243 else if (strcmp(temp, "fl") == 0)
245 else if (strcmp(temp, "ff") == 0)
247 else if (strcmp(temp, "ffi") == 0)
249 else if (strcmp(temp, "ffl") == 0)
252 fprintf(stderr, "illegal ligature %s ignored\n", temp);