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)
24 char cmd[100], s[100];
27 if ((fin = fopen(unsharp(name), "r")) == NULL)
29 while (fscanf(fin, "%s", cmd) != EOF) {
30 if (strcmp(cmd, "res") == 0) {
31 fscanf(fin, "%d", &Inch);
32 } else if (strcmp(cmd, "hor") == 0) {
33 fscanf(fin, "%d", &Hor);
34 } else if (strcmp(cmd, "vert") == 0) {
35 fscanf(fin, "%d", &Vert);
36 } else if (strcmp(cmd, "unitwidth") == 0) {
37 fscanf(fin, "%d", &Unitwidth);
38 } else if (strcmp(cmd, "sizes") == 0) {
40 while (fscanf(fin, "%d", &v) != EOF && v != 0 && nsizes < MAXPS)
42 } else if (strcmp(cmd, "fonts") == 0) {
43 fscanf(fin, "%d", &nfonts);
44 for (i = 1; i <= nfonts; i++) {
46 fontlab[i] = PAIR(s[0], s[1]);
48 } else if (strcmp(cmd, "charset") == 0) { /* add any names */
49 while (fscanf(fin, "%s", s) != EOF)
50 chadd(s, Troffchar, Install);
54 just skip anything else */
61 static int checkfont(char *name)
62 { /* in case it's not really a font description file */
63 /* really paranoid, but consider \f. */
65 char buf[300], buf2[300];
68 if ((fp = fopen(unsharp(name), "r")) == NULL)
70 for (i = 1; i <= 10; i++) {
71 if (fgets(buf, sizeof buf, fp) == NULL)
73 sscanf(buf, "%s", buf2);
78 if (eq(buf2, "name") || eq(buf2, "fontname") ||
79 eq(buf2, "special") || eq(buf2, "charset")) {
90 getfont(char *name, int pos) /* create width tab for font */
93 Font *ftemp = &fonts[pos];
96 int i, nw, n, wid, kern, code, type;
97 char buf[100], ch[100], s1[100], s2[100], s3[100], cmd[300];
100 /* fprintf(stderr, "read font %s onto %d\n", name, pos); */
101 if (checkfont(name) == -1)
103 if ((fin = fopen(unsharp(name), "r")) == NULL)
105 for (i = 0; i < ALPHABET; i++)
106 chtemp[i] = chinit; /* zero out to begin with */
107 ftemp->specfont = ftemp->ligfont = 0;
108 ftemp->defaultwidth = ftemp->spacewidth = Inch * Unitwidth / 72 / 3; /* should be rounded */
109 while (fscanf(fin, "%s", cmd) != EOF) {
110 if (strcmp(cmd, "name") == 0)
111 fscanf(fin, "%s", ftemp->longname);
112 else if (strcmp(cmd, "special") == 0)
114 else if (strcmp(cmd, "ligatures") == 0) {
115 ftemp->ligfont = getlig(fin);
116 } else if (strcmp(cmd, "spacewidth") == 0) {
117 fscanf(fin, "%d", &ftemp->spacewidth);
118 } else if (strcmp(cmd, "defaultwidth") == 0) {
119 fscanf(fin, "%d", &ftemp->defaultwidth);
120 } else if (strcmp(cmd, "charset") == 0) {
124 while (fgets(buf, sizeof buf, fin) != NULL) {
125 sscanf(buf, "%s %s %s %s", ch, s1, s2, s3);
126 if (s1[0] != '"') { /* genuine new character */
127 sscanf(s1, "%d", &wid);
128 sscanf(s2, "%d", &kern);
129 code = strtol(s3, 0, 0); /* dec/oct/hex */
131 /* otherwise it's a synonym for prev character, */
132 /* so leave previous values intact */
135 /* decide what kind of alphabet it might come from here */
138 if (strlen(ch) == 1) { /* it's ascii */
139 n = ch[0]; /* origin includes non-graphics */
140 chtemp[n].num = ch[0];
141 } else if (ch[0] == '\\' && ch[1] == '0') {
142 n = strtol(ch+1, 0, 0); /* \0octal or \0xhex */
145 } else if (mbtowc(&wc, ch, strlen(ch)) > 1) {
146 chtemp[nw].num = chadd(ch, MBchar, Install);
151 if (strcmp(ch, "---") == 0) { /* no name */
152 sprintf(ch, "%d", code);
156 chtemp[nw].num = chadd(ch, type, Install);
161 chtemp[n].kern = kern;
162 chtemp[n].code = code;
163 /*fprintf(stderr, "font %2.2s char %4.4s num %3d wid %2d code %3d\n",
164 ftemp->longname, ch, n, wid, code);
172 chtemp[' '].wid = ftemp->spacewidth; /* width of space on this font */
175 free(ftemp->wp); /* god help us if this wasn't allocated */
176 ftemp->wp = (Chwid *) malloc(nw * sizeof(Chwid));
177 if (ftemp->wp == NULL)
179 for (i = 0; i < nw; i++)
180 ftemp->wp[i] = chtemp[i];
182 * printf("%d chars: ", nw);
183 * for (i = 0; i < nw; i++)
184 * if (ftemp->wp[i].num > 0 && ftemp->wp[i].num < ALPHABET) {
185 * printf("%c %d ", ftemp->wp[i].num, ftemp->wp[i].wid);
186 * else if (i >= ALPHABET)
187 * printf("%d (%s) %d ", ftemp->wp[i].num,
188 * chnames[ftemp->wp[i].num-ALPHABET], ftemp->wp[i].wid);
196 chadd(char *s, int type, int install) /* add s to global character name table; */
197 { /* or just look it up */
199 /* a temporary kludge: store the "type" as the first character */
200 /* of the string, so we can remember from whence it came */
205 /* fprintf(stderr, "into chadd %s %c %c\n", s, type, install); /* */
206 for (i = 0; i < nchnames; i++)
207 if (type == chnames[i][0] && eq(s, chnames[i]+1)) /* +1 since type at front */
209 /* fprintf(stderr, "i %d, nchnames %d\n", i, nchnames); /* */
210 if (i < nchnames) /* found same type and bytes at position i */
212 else if (install == Lookup) /* not found, and we were just looking */
215 chnames[nchnames] = p = (char *) malloc(strlen(s)+1+1); /* type + \0 */
217 ERROR "out of space adding character %s", s WARN;
220 if (nchnames >= NCHARS - ALPHABET) {
221 ERROR "out of table space adding character %s", s WARN;
224 strcpy(chnames[nchnames]+1, s);
225 chnames[nchnames][0] = type;
226 /* fprintf(stderr, "installed %c%s at %d\n", type, s, nchnames); /* */
227 return nchnames++ + ALPHABET;
230 char *chname(int n) /* return string for char with index n */
231 { /* includes type char at front, to be peeled off elsewhere */
232 if (n >= ALPHABET && n < nchnames + ALPHABET)
233 return chnames[n-ALPHABET];
239 getlig(FILE *fin) /* pick up ligature list */
245 while (fscanf(fin, "%s", temp) != EOF && strcmp(temp, "0") != 0) {
246 if (strcmp(temp, "fi") == 0)
248 else if (strcmp(temp, "fl") == 0)
250 else if (strcmp(temp, "ff") == 0)
252 else if (strcmp(temp, "ffi") == 0)
254 else if (strcmp(temp, "ffl") == 0)
257 fprintf(stderr, "illegal ligature %s ignored\n", temp);