summaryrefslogtreecommitdiff
path: root/Build/source/texk/ttf2pk2/parse.c
blob: 873bf27f9fe0563a91d2f0486c82f2b6506f02e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
 *   parse.c
 *
 *   This file is part of the ttf2pk package.
 *
 *   Copyright 1997-1999, 2000 by
 *     Frederic Loyer <loyer@ensta.fr>
 *     Werner Lemberg <wl@gnu.org>
 */

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>         /* for size_t */
#include <ctype.h>
#include <string.h>

#include "ttf2tfm.h"
#include "errormsg.h"
#include "newobj.h"
#include "ligkern.h"
#include "texenc.h"
#include "parse.h"
#include "filesrch.h"


/*
 *   Here we get a token from the encoding file.  We parse just as much
 *   PostScript as we expect to find in an encoding file.  We allow
 *   commented lines and names like 0, .notdef, _foo_.  We do not allow
 *   //abc.
 *
 *   `bufferp' is a pointer to the current line; the offset of the beginning
 *   of the token to be parsed relative to `bufferp' will be returned in
 *   `offsetp'.  On the first call of gettoken() `init' must be set to 1 and
 *   to 0 on the following calls.
 *
 *   If `ignoreligkern' is `True', no LIGKERN data will be extracted from the
 *   encoding file.
 *
 *   Don't modify `bufferp'!
 *
 *   The memory management of `bufferp' will be done by gettoken() itself;
 *   nevertheless, it returns a pointer to the current token which should be
 *   freed after it has been used.
 */

static char *
gettoken(char **bufferp, size_t *offsetp, FILE *f, Font *fnt,
         Boolean ignoreligkern, Boolean init)
{
  char *p, *q;
  char tempchar;
  static char *curp;


  if (init)
    curp = NULL;

  while (1)
  {
    while (curp == NULL || *curp == '\0')
    {
      if (*bufferp)
        free(*bufferp);

      if (texlive_getline(bufferp, f) == False)
        oops("Premature end in encoding file.");

      curp = *bufferp;

      for (p = *bufferp; *p; p++)
        if (*p == '%')
        {
          if (ignoreligkern == False)
            checkligkern(p, fnt);
          *p = '\0';
          break;
        }
    }

    while (isspace(*curp))
      curp++;

    *offsetp = curp - *bufferp;

    if (*curp)
    {
      if (*curp == '[' || *curp == ']' ||
          *curp == '{' || *curp == '}')
        q = curp++;
      else if (*curp == '/' ||
               *curp == '-' || *curp == '_' || *curp == '.' ||
               ('0' <= *curp && *curp <= '9') ||
               ('a' <= *curp && *curp <= 'z') ||
               ('A' <= *curp && *curp <= 'Z'))
      {
        q = curp++;
        while (*curp == '-' || *curp == '_' || *curp == '.' ||
               ('0' <= *curp && *curp <= '9') ||
               ('a' <= *curp && *curp <= 'z') ||
               ('A' <= *curp && *curp <= 'Z'))
          curp++;
      }
      else
        q = curp;

      tempchar = *curp;
      *curp = '\0';
      p = newstring(q);
      *curp = tempchar;
      return p;
    }
  }
}


/*
 *   This routine reads in an encoding file, given the name.  It returns
 *   the final total structure.  It performs a number of consistency checks.
 */

encoding *
readencoding(char **enc, Font *fnt, Boolean ignoreligkern)
{
  char *real_encname;
  FILE *enc_file;
  char *p, *q, c;
  char *buffer;
  char numbuf[11];
  size_t offset;
  int i;
  long l;
  encoding *e = (encoding *)mymalloc(sizeof (encoding));


  if (enc && *enc)
  {
    real_encname = TeX_search_encoding_file(enc);
    if (!real_encname)
      oops("Cannot find encoding file `%s'.", *enc);

    enc_file = fopen(real_encname, "rt");
    if (enc_file == NULL)
      oops("Cannot open encoding file `%s'.", real_encname);

    buffer = NULL;
    p = gettoken(&buffer, &offset, enc_file, fnt, ignoreligkern, True);
    if (*p != '/' || p[1] == '\0')
      boops(buffer, offset,
        "First token in encoding must be literal encoding name.");
    e->name = newstring(p + 1);
    free(p);

    p = gettoken(&buffer, &offset, enc_file, fnt, ignoreligkern, False);
    if (strcmp(p, "["))
      boops(buffer, offset,
        "Second token in encoding must be mark ([) token.");
    free(p);

    for (i = 0; i < 256; i++)
    {
      p = gettoken(&buffer, &offset, enc_file, fnt, ignoreligkern, False);
      if (*p != '/' || p[1] == 0)
        boops(buffer, offset,
          "Tokens 3 to 257 in encoding must be literal names.");

      /* now we test for a generic code point resp. glyph index value */

      c = p[2];
      if (p[1] == '.' && (c == 'c' || c == 'g') && '0' <= p[3] && p[3] <= '9')
      {
        l = strtol(p + 3, &q, 0);
        if (*q != '\0' || l < 0 || l > 0x16FFFF)
          boops(buffer, offset, "Invalid encoding token.");
        sprintf(numbuf, ".%c0x%lx", c, l);
        e->vec[i] = newstring(numbuf);
      }
      else
        e->vec[i] = newstring(p + 1);

      free(p);
    }

    p = gettoken(&buffer, &offset, enc_file, fnt, ignoreligkern, False);
    if (strcmp(p, "]"))
      boops(buffer, offset,
        "Token 258 in encoding must be make-array (]).");
    free(p);

    while (texlive_getline(&buffer, enc_file))
    {
      for (p = buffer; *p; p++)
        if (*p == '%')
        {
          if (ignoreligkern == False)
            checkligkern(p, fnt);
          *p = '\0';
          break;
        }
    }

    fclose(enc_file);

    if (ignoreligkern == False && fnt->sawligkern == False)
      getligkerndefaults(fnt);
  }
  else
  {
    if (ignoreligkern == False)
    {
      e = &staticencoding;
      getligkerndefaults(fnt);
    }
    else
      e = NULL;
  }

  return e;
}


/*
 *   We scan a glyph replacement file.
 *   `%' is the comment character.
 */

void
get_replacements(Font *fnt)
{
  char *real_replacement_name;
  FILE *replacement_file;
  char *buffer = NULL, *oldbuffer = NULL;
  char *p;
  char *old_name, *new_name;
  stringlist *sl;


  if (!fnt->replacementname)
    return;

  real_replacement_name = TeX_search_replacement_file(&fnt->replacementname);
  if (!real_replacement_name)
    oops("Cannot find replacement file `%s'.", fnt->replacementname);

  replacement_file = fopen(real_replacement_name, "rt");
  if (replacement_file == NULL)
    oops("Cannot open replacement file `%s'.", real_replacement_name);

  while (texlive_getline(&buffer, replacement_file))
  {
    for (p = buffer; *p; p++)
      if (*p == '%')
      {
        *p = '\0';
        break;
      }

    if (oldbuffer)
      free(oldbuffer);
    oldbuffer = newstring(buffer);

    p = buffer;

    while (isspace(*p))
      p++;
    if (!*p)
      continue;

    old_name = p;

    while (*p && !isspace(*p))
      p++;
    if (*p)
      *p++ = '\0';      

    while (*p && isspace(*p))
      p++;
    if (!*p)
      boops(oldbuffer, old_name - oldbuffer, "Replacement glyph missing.");

    new_name = p;

    while (*p && !isspace(*p))
      p++;
    if (*p)
      *p++ = '\0';      

    while (*p && isspace(*p))
      p++;
    if (*p)
      boops(oldbuffer, p - oldbuffer, "Invalid replacement syntax.");

    sl = newstringlist();
    sl->new_name = newstring(new_name);
    sl->old_name = newstring(old_name);
    sl->next = fnt->replacements;
    fnt->replacements = sl;
  }

  fclose(replacement_file);
}


/* end */