mkdev: remove devutf/C*
[troff.git] / troff / t11.c
blob68dc2a6edf25f4a09fd51f377098425b37a6fff0
1 #include "tdef.h"
2 #include "fns.h"
3 #include "ext.h"
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)
20 getdesc(char *name)
22 FILE *fin;
23 char cmd[100], s[100];
24 int i, v;
26 if ((fin = fopen(name, "r")) == NULL)
27 return -1;
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) {
38 nsizes = 0;
39 while (fscanf(fin, "%d", &v) != EOF && v != 0 && nsizes < MAXPS)
40 pstab[nsizes++] = v;
41 } else if (strcmp(cmd, "fonts") == 0) {
42 fscanf(fin, "%d", &nfonts);
43 for (i = 1; i <= nfonts; i++) {
44 fscanf(fin, "%s", s);
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);
50 break;
52 /* else
53 just skip anything else */
54 skipline(fin);
56 fclose(fin);
57 return 1;
60 static int checkfont(char *name)
61 { /* in case it's not really a font description file */
62 /* really paranoid, but consider \f. */
63 FILE *fp;
64 char buf[300], buf2[300];
65 int i, status = -1;
67 if ((fp = fopen(name, "r")) == NULL)
68 return -1;
69 for (i = 1; i <= 10; i++) {
70 if (fgets(buf, sizeof buf, fp) == NULL)
71 break;
72 sscanf(buf, "%s", buf2);
73 if (buf2[0] == '#') {
74 i--;
75 continue;
77 if (eq(buf2, "name") || eq(buf2, "fontname") ||
78 eq(buf2, "special") || eq(buf2, "charset")) {
79 status = 1;
80 break;
83 fclose(fp);
84 return status;
88 int readutf8(int *dst, char *src);
90 int getfont(char *name, int pos) /* create width tab for font */
92 FILE *fin;
93 Font *ftemp = &fonts[pos];
94 Chwid chtemp[MAXCH];
95 static Chwid chinit;
96 int i, nw, n, wid, kern, code, type;
97 char buf[100], ch[100], s1[100], s2[100], s3[100], cmd[300];
99 /* fprintf(stderr, "read font %s onto %d\n", name, pos); */
100 if (checkfont(name) == -1)
101 return -1;
102 if ((fin = fopen(name, "r")) == NULL)
103 return -1;
104 for (i = 0; i < ALPHABET; i++)
105 chtemp[i] = chinit; /* zero out to begin with */
106 ftemp->specfont = ftemp->ligfont = 0;
107 ftemp->defaultwidth = ftemp->spacewidth = Inch * Unitwidth / 72 / 3; /* should be rounded */
108 while (fscanf(fin, "%s", cmd) != EOF) {
109 if (strcmp(cmd, "name") == 0)
110 fscanf(fin, "%s", ftemp->longname);
111 else if (strcmp(cmd, "special") == 0)
112 ftemp->specfont = 1;
113 else if (strcmp(cmd, "ligatures") == 0) {
114 ftemp->ligfont = getlig(fin);
115 } else if (strcmp(cmd, "spacewidth") == 0) {
116 fscanf(fin, "%d", &ftemp->spacewidth);
117 } else if (strcmp(cmd, "defaultwidth") == 0) {
118 fscanf(fin, "%d", &ftemp->defaultwidth);
119 } else if (strcmp(cmd, "charset") == 0) {
120 int wc;
121 skipline(fin);
122 nw = ALPHABET;
123 while (fgets(buf, sizeof buf, fin) != NULL) {
124 sscanf(buf, "%s %s %s %s", ch, s1, s2, s3);
125 if (s1[0] != '"') { /* genuine new character */
126 sscanf(s1, "%d", &wid);
127 sscanf(s2, "%d", &kern);
128 code = strtol(s3, 0, 0); /* dec/oct/hex */
130 /* otherwise it's a synonym for prev character, */
131 /* so leave previous values intact */
134 /* decide what kind of alphabet it might come from here */
137 if (strlen(ch) == 1) { /* it's ascii */
138 n = ch[0]; /* origin includes non-graphics */
139 chtemp[n].num = ch[0];
140 } else if (ch[0] == '\\' && ch[1] == '0') {
141 n = strtol(ch+1, 0, 0); /* \0octal or \0xhex */
142 chtemp[n].num = n;
143 #ifdef UNICODE
144 } else if (readutf8(&wc, ch) > 1) {
145 chtemp[nw].num = chadd(ch, MBchar, Install);
146 n = nw;
147 nw++;
148 #endif /*UNICODE*/
149 } else {
150 if (strcmp(ch, "---") == 0) { /* no name */
151 sprintf(ch, "%d", code);
152 type = Number;
153 } else
154 type = Troffchar;
155 chtemp[nw].num = chadd(ch, type, Install);
156 n = nw;
157 nw++;
159 chtemp[n].wid = wid;
160 chtemp[n].kern = kern;
161 chtemp[n].code = code;
162 /*fprintf(stderr, "font %2.2s char %4.4s num %3d wid %2d code %3d\n",
163 ftemp->longname, ch, n, wid, code);
166 break;
168 skipline(fin);
170 fclose(fin);
171 chtemp[' '].wid = ftemp->spacewidth; /* width of space on this font */
172 ftemp->nchars = nw;
173 if (ftemp->wp)
174 free(ftemp->wp); /* god help us if this wasn't allocated */
175 ftemp->wp = (Chwid *) malloc(nw * sizeof(Chwid));
176 if (ftemp->wp == NULL)
177 return -1;
178 for (i = 0; i < nw; i++)
179 ftemp->wp[i] = chtemp[i];
181 * printf("%d chars: ", nw);
182 * for (i = 0; i < nw; i++)
183 * if (ftemp->wp[i].num > 0 && ftemp->wp[i].num < ALPHABET) {
184 * printf("%c %d ", ftemp->wp[i].num, ftemp->wp[i].wid);
185 * else if (i >= ALPHABET)
186 * printf("%d (%s) %d ", ftemp->wp[i].num,
187 * chnames[ftemp->wp[i].num-ALPHABET], ftemp->wp[i].wid);
189 * printf("\n");
191 return 1;
194 chadd(char *s, int type, int install) /* add s to global character name table; */
195 { /* or just look it up */
197 /* a temporary kludge: store the "type" as the first character */
198 /* of the string, so we can remember from whence it came */
200 char *p;
201 int i;
203 /* fprintf(stderr, "into chadd %s %c %c\n", s, type, install); /* */
204 for (i = 0; i < nchnames; i++)
205 if (type == chnames[i][0] && eq(s, chnames[i]+1)) /* +1 since type at front */
206 break;
207 /* fprintf(stderr, "i %d, nchnames %d\n", i, nchnames); /* */
208 if (i < nchnames) /* found same type and bytes at position i */
209 return ALPHABET + i;
210 else if (install == Lookup) /* not found, and we were just looking */
211 return -1;
213 chnames[nchnames] = p = (char *) malloc(strlen(s)+1+1); /* type + \0 */
214 if (p == NULL) {
215 ERROR "out of space adding character %s", s WARN;
216 return LEFTHAND;
218 if (nchnames >= NCHARS - ALPHABET) {
219 ERROR "out of table space adding character %s", s WARN;
220 return LEFTHAND;
222 strcpy(chnames[nchnames]+1, s);
223 chnames[nchnames][0] = type;
224 /* fprintf(stderr, "installed %c%s at %d\n", type, s, nchnames); /* */
225 return nchnames++ + ALPHABET;
228 char *chname(int n) /* return string for char with index n */
229 { /* includes type char at front, to be peeled off elsewhere */
230 if (n >= ALPHABET && n < nchnames + ALPHABET)
231 return chnames[n-ALPHABET];
232 else
233 return "";
236 getlig(FILE *fin) /* pick up ligature list */
238 int lig;
239 char temp[200];
241 lig = 0;
242 while (fscanf(fin, "%s", temp) != EOF && strcmp(temp, "0") != 0) {
243 if (strcmp(temp, "fi") == 0)
244 lig |= LFI;
245 else if (strcmp(temp, "fl") == 0)
246 lig |= LFL;
247 else if (strcmp(temp, "ff") == 0)
248 lig |= LFF;
249 else if (strcmp(temp, "ffi") == 0)
250 lig |= LFFI;
251 else if (strcmp(temp, "ffl") == 0)
252 lig |= LFFL;
254 return lig;