Debian Codegraph @

texk/ttf2pk2/parse.c

raw
1 /*
2 * parse.c
3 *
4 * This file is part of the ttf2pk package.
5 *
6 * Copyright 1997-1999, 2000 by
7 * Frederic Loyer <[email protected]>
8 * Werner Lemberg <[email protected]>
9 */
10
11 #ifdef HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <stddef.h> /* for size_t */
18 #include <ctype.h>
19 #include <string.h>
20
21 #include "ttf2tfm.h"
22 #include "errormsg.h"
23 #include "newobj.h"
24 #include "ligkern.h"
25 #include "texenc.h"
26 #include "parse.h"
27 #include "filesrch.h"
28
29
30 /*
31 * Here we get a token from the encoding file. We parse just as much
32 * PostScript as we expect to find in an encoding file. We allow
33 * commented lines and names like 0, .notdef, _foo_. We do not allow
34 * //abc.
35 *
36 * `bufferp' is a pointer to the current line; the offset of the beginning
37 * of the token to be parsed relative to `bufferp' will be returned in
38 * `offsetp'. On the first call of gettoken() `init' must be set to 1 and
39 * to 0 on the following calls.
40 *
41 * If `ignoreligkern' is `True', no LIGKERN data will be extracted from the
42 * encoding file.
43 *
44 * Don't modify `bufferp'!
45 *
46 * The memory management of `bufferp' will be done by gettoken() itself;
47 * nevertheless, it returns a pointer to the current token which should be
48 * freed after it has been used.
49 */
50
51 static char *
52 gettoken(char **bufferp, size_t *offsetp, FILE *f, Font *fnt,
53 Boolean ignoreligkern, Boolean init)
54 {
55 char *p, *q;
56 char tempchar;
57 static char *curp;
58
59
60 if (init)
61 curp = NULL;
62
63 while (1)
64 {
65 while (curp == NULL || *curp == '\0')
66 {
67 if (*bufferp)
68 free(*bufferp);
69
70 if (texlive_getline(bufferp, f) == False)
71 oops("Premature end in encoding file.");
72
73 curp = *bufferp;
74
75 for (p = *bufferp; *p; p++)
76 if (*p == '%')
77 {
78 if (ignoreligkern == False)
79 checkligkern(p, fnt);
80 *p = '\0';
81 break;
82 }
83 }
84
85 while (isspace((unsigned char)*curp))
86 curp++;
87
88 *offsetp = curp - *bufferp;
89
90 if (*curp)
91 {
92 if (*curp == '[' || *curp == ']' ||
93 *curp == '{' || *curp == '}')
94 q = curp++;
95 else if (*curp == '/' ||
96 *curp == '-' || *curp == '_' || *curp == '.' ||
97 ('0' <= *curp && *curp <= '9') ||
98 ('a' <= *curp && *curp <= 'z') ||
99 ('A' <= *curp && *curp <= 'Z'))
100 {
101 q = curp++;
102 while (*curp == '-' || *curp == '_' || *curp == '.' ||
103 ('0' <= *curp && *curp <= '9') ||
104 ('a' <= *curp && *curp <= 'z') ||
105 ('A' <= *curp && *curp <= 'Z'))
106 curp++;
107 }
108 else
109 q = curp;
110
111 tempchar = *curp;
112 *curp = '\0';
113 p = newstring(q);
114 *curp = tempchar;
115 return p;
116 }
117 }
118 }
119
120
121 /*
122 * This routine reads in an encoding file, given the name. It returns
123 * the final total structure. It performs a number of consistency checks.
124 */
125
126 encoding *
127 readencoding(char **enc, Font *fnt, Boolean ignoreligkern)
128 {
129 char *real_encname;
130 FILE *enc_file;
131 char *p, *q, c;
132 char *buffer;
133 char numbuf[11];
134 size_t offset;
135 int i;
136 long l;
137 encoding *e = (encoding *)mymalloc(sizeof (encoding));
138
139
140 if (enc && *enc)
141 {
142 real_encname = TeX_search_encoding_file(enc);
143 if (!real_encname)
144 oops("Cannot find encoding file `%s'.", *enc);
145
146 enc_file = fopen(real_encname, "rt");
147 if (enc_file == NULL)
148 oops("Cannot open encoding file `%s'.", real_encname);
149
150 buffer = NULL;
151 p = gettoken(&buffer, &offset, enc_file, fnt, ignoreligkern, True);
152 if (*p != '/' || p[1] == '\0')
153 boops(buffer, offset,
154 "First token in encoding must be literal encoding name.");
155 e->name = newstring(p + 1);
156 free(p);
157
158 p = gettoken(&buffer, &offset, enc_file, fnt, ignoreligkern, False);
159 if (strcmp(p, "["))
160 boops(buffer, offset,
161 "Second token in encoding must be mark ([) token.");
162 free(p);
163
164 for (i = 0; i < 256; i++)
165 {
166 p = gettoken(&buffer, &offset, enc_file, fnt, ignoreligkern, False);
167 if (*p != '/' || p[1] == 0)
168 boops(buffer, offset,
169 "Tokens 3 to 257 in encoding must be literal names.");
170
171 /* now we test for a generic code point resp. glyph index value */
172
173 c = p[2];
174 if (p[1] == '.' && (c == 'c' || c == 'g') && '0' <= p[3] && p[3] <= '9')
175 {
176 l = strtol(p + 3, &q, 0);
177 if (*q != '\0' || l < 0 || l > 0x16FFFF)
178 boops(buffer, offset, "Invalid encoding token.");
179 sprintf(numbuf, ".%c0x%lx", c, l);
180 e->vec[i] = newstring(numbuf);
181 }
182 else
183 e->vec[i] = newstring(p + 1);
184
185 free(p);
186 }
187
188 p = gettoken(&buffer, &offset, enc_file, fnt, ignoreligkern, False);
189 if (strcmp(p, "]"))
190 boops(buffer, offset,
191 "Token 258 in encoding must be make-array (]).");
192 free(p);
193
194 while (texlive_getline(&buffer, enc_file))
195 {
196 for (p = buffer; *p; p++)
197 if (*p == '%')
198 {
199 if (ignoreligkern == False)
200 checkligkern(p, fnt);
201 *p = '\0';
202 break;
203 }
204 }
205
206 fclose(enc_file);
207
208 if (ignoreligkern == False && fnt->sawligkern == False)
209 getligkerndefaults(fnt);
210 }
211 else
212 {
213 if (ignoreligkern == False)
214 {
215 e = &staticencoding;
216 getligkerndefaults(fnt);
217 }
218 else
219 e = NULL;
220 }
221
222 return e;
223 }
224
225
226 /*
227 * We scan a glyph replacement file.
228 * `%' is the comment character.
229 */
230
231 void
232 get_replacements(Font *fnt)
233 {
234 char *real_replacement_name;
235 FILE *replacement_file;
236 char *buffer = NULL, *oldbuffer = NULL;
237 char *p;
238 char *old_name, *new_name;
239 stringlist *sl;
240
241
242 if (!fnt->replacementname)
243 return;
244
245 real_replacement_name = TeX_search_replacement_file(&fnt->replacementname);
246 if (!real_replacement_name)
247 oops("Cannot find replacement file `%s'.", fnt->replacementname);
248
249 replacement_file = fopen(real_replacement_name, "rt");
250 if (replacement_file == NULL)
251 oops("Cannot open replacement file `%s'.", real_replacement_name);
252
253 while (texlive_getline(&buffer, replacement_file))
254 {
255 for (p = buffer; *p; p++)
256 if (*p == '%')
257 {
258 *p = '\0';
259 break;
260 }
261
262 if (oldbuffer)
263 free(oldbuffer);
264 oldbuffer = newstring(buffer);
265
266 p = buffer;
267
268 while (isspace((unsigned char)*p))
269 p++;
270 if (!*p)
271 continue;
272
273 old_name = p;
274
275 while (*p && !isspace((unsigned char)*p))
276 p++;
277 if (*p)
278 *p++ = '\0';
279
280 while (*p && isspace((unsigned char)*p))
281 p++;
282 if (!*p)
283 boops(oldbuffer, old_name - oldbuffer, "Replacement glyph missing.");
284
285 new_name = p;
286
287 while (*p && !isspace((unsigned char)*p))
288 p++;
289 if (*p)
290 *p++ = '\0';
291
292 while (*p && isspace((unsigned char)*p))
293 p++;
294 if (*p)
295 boops(oldbuffer, p - oldbuffer, "Invalid replacement syntax.");
296
297 sl = newstringlist();
298 sl->new_name = newstring(new_name);
299 sl->old_name = newstring(old_name);
300 sl->next = fnt->replacements;
301 fnt->replacements = sl;
302 }
303
304 fclose(replacement_file);
305 }
306
307
308 /* end */