Debian Codegraph @

texk/ps2pk/encoding.c

raw
1 /* MODULE encoding (encoding.c)
2 * interface: typedef char *encoding[256];
3 * void getenc(
4 * char **fontname, **encname,
5 * ( foundry name of font and name of this encoding )
6 * encoding ev, output encoding
7 * int width[256]); output widths
8 *
9 * depends on: MODULEs binary_table, strings
10 * <ctype.h>
11 * <stdio.h>
12 * <stdlib.h>
13 * <string.h>
14 *
15 * globals: void fatal();
16 *
17 * Author: Piet Tutelaers
18 * (see README for license)
19 */
20
21 #include "types.h"
22 #include "basics.h"
23 #include <ctype.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 static char *my_string(char *);
29 static void remove_string(void);
30 static void addcc(char *name, int charcode);
31 static int getcc(char *name);
32
33 /* return a pointer to first non space character after occurence of string t,
34 * the scan starts at pointer s
35 */
36 static char *value_after(char *s, const char *t)
37 { int l;
38 l = strlen(t);
39 for (;;) {
40 while (*s != *t && *s != '\n') s++;
41 if (*s == '\n') return NULL;
42 if (strncmp(s, t, l) == 0) break;
43 s++;
44 }
45 s += l;
46 while (isspace((unsigned char)*s)) s++;
47 return s;
48 }
49
50 static int decimal(char *s)
51 { int sign = 1, d = 0;
52
53 while (isspace((unsigned char)*s)) s++;
54 if (*s == '-') { sign = -1; s++; }
55 while (isdigit((unsigned char)*s)) { d = d * 10 + (*s - '0'); s++; }
56 return sign*d;
57 }
58
59 /* Nextsymbol() reads lines from enc until a '[', '/' or a ']' is
60 * encountered. In that case pline points to the character following
61 * this symbol otherwise a fatal error is generated.
62 */
63
64 FILE *enc;
65
66 #define LINEBUF 1024
67 char line[LINEBUF], *pline = NULL;
68
69 static char nextsymbol(void)
70 {
71 for (;;) {
72 if (pline == NULL) {
73 if (fgets(line, LINEBUF-1, enc) == NULL)
74 fatal("unexpected EOF while reading %s\n", encfile);
75 pline = line;
76 }
77 if (*pline == '[' || *pline == ']' || *pline == '/') break;
78 if (*pline == '%' || *pline == '\n' || *pline == '\0')
79 { pline = NULL;
80 continue;
81 }
82 pline++;
83 }
84 pline++; return *(pline-1);
85 }
86
87 static char *nextpsname(void)
88 { int i = 0;
89 char the_nextname[MAXSTRLEN], *name;
90
91 for (;;) {
92 if (pline == NULL) {
93 if (fgets(line, LINEBUF-1, enc) == NULL)
94 fatal("unexpected EOF while reading %s\n", encfile);
95 pline = line;
96 }
97 if (*pline == '%' || *pline == '\n' || *pline == '\0')
98 { pline = NULL;
99 continue;
100 }
101 if (isspace((unsigned char)*pline)) {
102 pline++;
103 continue;
104 }
105 if (!isalpha((unsigned char)*pline)) fatal("invalid name in %s\n", encfile);
106 /* pt981009: added '_' Staszek Wawrykiewicz <[email protected]> */
107 while (isalnum((unsigned char)*pline) || *pline == '-' || *pline == '_') {
108 if (i > MAXSTRLEN-2)
109 fatal("name too long in %s (%s)\n", line, encfile);
110 the_nextname[i++] = *pline++;
111 }
112 the_nextname[i] = '\0';
113 name = malloc(i+1);
114 if (name == NULL) fatal("Out of memory\n");
115 strcpy(name, the_nextname);
116 break;
117 }
118 /* pt981009: removed pline++ Staszek Wawrykiewicz <[email protected]> */
119 return name;
120 }
121
122 void getenc(char **fontname, char **encname, encoding ev, int width[256])
123 { int i, len, SCMseen, Ccnt, wx, cc;
124 FILE *afm;
125 char *name, ns;
126
127 for (i=0; i < 256; i++) { ev[i] = NULL; width[i] = -1000; }
128 if (encfile) {
129 enc = fopen(encfile, "r");
130 if (enc == NULL) fatal("Can't open %s\n", encfile);
131 if (nextsymbol() != '/') fatal("missing '/encoding' in %s\n", encfile);
132 *encname = nextpsname();
133 if (nextsymbol() != '[') fatal("missing '[' in %s\n", encfile);
134 i = 0;
135 while (i < 256 && (ns = nextsymbol()) == '/') {
136 name = my_string(pline);
137 if (strcmp(name, ".notdef") == 0) {
138 i++; remove_string();
139 continue;
140 }
141 addcc(name, i++);
142 }
143 if (i != 256) fatal("missing %d symbols in %s\n", 256-i, encfile);
144 if (nextsymbol() != ']') fatal("missing ']' in %s\n", encfile);
145 fclose(enc);
146 }
147 afm = fopen(afmfile, "r");
148 if (afm == NULL) fatal("Can't open %s\n", afmfile);
149 /*
150 * Scan header of AFM file and take the FontName and EncodingScheme
151 * (used for identification purposes in the PK postamble)
152 * Stop after reading `StartCharMetrics'.
153 */
154 SCMseen = 0; Ccnt = 0;
155 while (fgets(line, LINEBUF-1, afm)) {
156 if (strncmp(line, "FontName", 8) == 0) {
157 pline = value_after(line, "FontName");
158 len = strlen(pline);
159 if (*(pline+len-1) == '\n') {
160 *(pline+len-1) = '\0'; len--;
161 }
162 *fontname = malloc(len + 1);
163 if (*fontname == NULL) fatal("Out of memory\n");
164 strcpy(*fontname, pline);
165 }
166 else if (encname == NULL && strncmp(line, "EncodingScheme", 8) == 0) {
167 pline = value_after(line, "EncodingScheme");
168 len = strlen(pline);
169 if (*(pline+len-1) == '\n') {
170 *(pline+len-1) = '\0'; len--;
171 }
172 *encname = malloc(len + 1);
173 if (*encname == NULL) fatal("Out of memory\n");
174 strcpy(*encname, pline);
175 }
176 else if (strncmp(line, "StartCharMetrics", 16) == 0) {
177 SCMseen = 1; break;
178 }
179 }
180 if (SCMseen == 0) fatal("%s: no metrics info\n", afmfile);
181 while (fgets(line, LINEBUF-1, afm)) {
182 if (strncmp(line, "EndCharMetrics", 14) == 0) break;
183 if (strncmp(line, "C ", 2) != 0)
184 fatal("%s: unexpected line\n", afmfile);
185
186 /* Gracefully terminate when the AFM file is not valid. Every line */
187 /* in the AFM file should specify a "C"haracter, a "WX"idth, and */
188 /* a "N"ame. (ndw)
189 */
190 pline = value_after(line, "C");
191 if (pline == NULL)
192 fatal("\nBad char metric in %s (no char):\n\t %s\n", afmfile, line);
193 cc = decimal(pline);
194 pline = value_after(pline, "WX");
195 if (pline == NULL)
196 fatal("\nBad char metric in %s (no width):\n\t %s\n", afmfile, line);
197 wx = decimal(pline);
198 pline = value_after(pline, "N");
199 if (pline == NULL)
200 fatal("\nBad char metric in %s (no name):\n\t %s\n", afmfile, line);
201 name = my_string(pline);
202
203 if (encfile) {
204 if ((i = getcc(name)) == -1) {
205 remove_string();
206 continue;
207 }
208 do { /* allow more occurences per name */
209 ev[i] = name; width[i] = wx;
210 i = getcc(name);
211 }
212 while (i >= 0);
213 }
214 else if (cc >= 0 && cc <= 255) { /* ndw: 10/13/92 */
215 ev[cc] = name; width[cc] = wx;
216 }
217 else if (cc > 255)
218 msg("Char code %d ignored (out of range).\n", cc);
219 Ccnt++;
220 }
221 if (Ccnt == 0) fatal("%s: no characters selected\n", afmfile);
222 }
223
224 /* END MODULE encoding */
225
226 /* MODULE binary_table
227 * depends on: MODULE string
228 * <stdlib.h>
229 * <string.h>
230 */
231
232 /*
233 * TUG has accepted an extended font layout (EC.enc) where the hyphen
234 * occupies two positions ('055 and '177) to handle exeptional hyphenation
235 * situations (see TUGboat#11 1990 (no. 4) pp. 514).
236 * To handle this special case we allow two charcodes per symbol.
237 */
238
239 /*
240 * Two functions for a binary search table. One to add a name and one
241 * to lookup a name. This table is used to store all encoding names and
242 * its corresponding charcode.
243 */
244
245 struct Node
246 { char *name; /* pointer to a PostScript string */
247 short cc[2]; /* its charcode's (maximum of two) */
248 short left, right;
249 } node[256];
250
251 static int freenode = 0;
252 static short root = -1;
253
254 static void addcc(char *name, int charcode)
255 {
256 short p = 0, q;
257 int r = 0;
258
259 q = root;
260 while (q != -1) {
261 p = q; r = strcmp(name, node[p].name);
262 if (r == 0) {
263 if (node[p].cc[1] == -1) {
264 node[p].cc[1] = charcode;
265 return;
266 }
267 else
268 fatal("%s: more than two codes for %s (%d,%d,%d)\n",
269 encfile, name, node[p].cc[0], node[p].cc[1], charcode);
270 }
271 else if (r < 0) q=node[p].left;
272 else q=node[p].right;
273 }
274 if (freenode == 256)
275 fatal("%s: too many PostScript names\n", encfile);
276 /* not found hence insert */
277 node[freenode].name = name;
278 node[freenode].cc[0] = charcode;
279 node[freenode].cc[1] = -1;
280 node[freenode].left = node[freenode].right = -1;
281 if (root == -1)
282 root = freenode;
283 else {
284 if (r < 0) node[p].left=freenode;
285 else node[p].right=freenode;
286 }
287 freenode++;
288 }
289
290 static int getcc(char *name)
291 { short r, p, q, cc;
292
293 q = root;
294 while (q != -1) {
295 p = q; r = strcmp(name, node[p].name);
296 if (r == 0) {
297 cc = node[p].cc[0];
298 node[p].cc[0] = node[p].cc[1];
299 node[p].cc[1] = -1;
300 return cc;
301 }
302 else if (r < 0) q = node[p].left;
303 else q = node[p].right;
304 }
305 return -1;
306 }
307
308 /* END MODULE binary_table */
309
310 /* MODULE string:
311 * depends on: <ctype.h>
312 * <string.h>
313 */
314
315 /* String() reads a string starting at a given character pointer.
316 * After removing leading whitespace the resulting string (with possible
317 * `.' and '_' characters) is stored in a stringpool after which a pointer
318 * to it is returned.
319 */
320
321 #define POOLSIZE 10000
322 static char stringpool[POOLSIZE];
323 static int poolsize, /* occupied space annex free index */
324 lastpoolsize; /* previous poolsize */
325
326 static char *my_string(char *s)
327 { int length; char *str;
328
329 while (isspace((unsigned char)*s)) s++;
330 str = s; length = 0;
331 while (isalnum((unsigned char)*s) || *s == '.' || *s == '_') { length++; s++; }
332 if (length == 0) return NULL;
333 else {
334 if (length + 1 > POOLSIZE - poolsize)
335 fatal("Out of memory %s\n", s);
336 strncpy(stringpool+poolsize, str, length);
337 str = stringpool + poolsize;
338 s = stringpool + poolsize + length;
339 *s = '\0';
340 lastpoolsize = poolsize;
341 poolsize += length+1;
342 return str;
343 }
344 }
345
346 /* remove last string from stringpool */
347 static void remove_string(void)
348 {
349 poolsize = lastpoolsize;
350 }
351
352 /* END MODULE string */
353