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 int readutf8(int *dst
, char *src
);
90 int 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];
99 /* fprintf(stderr, "read font %s onto %d\n", name, pos); */
100 if (checkfont(name
) == -1)
102 if ((fin
= fopen(name
, "r")) == NULL
)
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)
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) {
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 */
144 } else if (readutf8(&wc
, ch
) > 1) {
145 chtemp
[nw
].num
= chadd(ch
, MBchar
, Install
);
150 if (strcmp(ch
, "---") == 0) { /* no name */
151 sprintf(ch
, "%d", code
);
155 chtemp
[nw
].num
= chadd(ch
, type
, Install
);
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);
171 chtemp
[' '].wid
= ftemp
->spacewidth
; /* width of space on this font */
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
)
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);
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 */
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 */
207 /* fprintf(stderr, "i %d, nchnames %d\n", i, nchnames); /* */
208 if (i
< nchnames
) /* found same type and bytes at position i */
210 else if (install
== Lookup
) /* not found, and we were just looking */
213 chnames
[nchnames
] = p
= (char *) malloc(strlen(s
)+1+1); /* type + \0 */
215 ERROR
"out of space adding character %s", s WARN
;
218 if (nchnames
>= NCHARS
- ALPHABET
) {
219 ERROR
"out of table space adding character %s", s WARN
;
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
];
236 getlig(FILE *fin
) /* pick up ligature list */
242 while (fscanf(fin
, "%s", temp
) != EOF
&& strcmp(temp
, "0") != 0) {
243 if (strcmp(temp
, "fi") == 0)
245 else if (strcmp(temp
, "fl") == 0)
247 else if (strcmp(temp
, "ff") == 0)
249 else if (strcmp(temp
, "ffi") == 0)
251 else if (strcmp(temp
, "ffl") == 0)