summaryrefslogtreecommitdiff
path: root/Build/source/texk/ps2pkm/encoding.c
blob: a55184a65b9ad3b979eaa6faf5a6ae07d2262d87 (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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* MODULE encoding (encoding.c)
 * interface:  typedef char *encoding[256];
 *             void getenc(
 *		      char **fontname, **encname,  
 *                         ( foundry name of font and name of this encoding )
 *                    encoding ev,      output encoding
 *                    int width[256]);  output widths
 *
 * depends on: MODULEs binary_table, strings
 *             <ctype.h>
 *             <stdio.h>
 *             <stdlib.h>
 *             <string.h>
 *
 * globals:    void fatal();
 *
 * Author:     Piet Tutelaers
 *             Version 1.3 (August 1992)
 *             Version 1.4 (December 1993)
 */

#include "basics.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *my_string(char *);
void remove_string();
void addcc(char *name, int charcode);
int getcc(char *name);
void initcc();

extern char *encfile, *afmfile;

/* return a pointer to first non space character after occurence of string t,
 * the scan starts at pointer s 
 */
static char *value_after(char *s, char *t)
{  int l;
   l = strlen(t);
   for (;;) {
      while (*s != *t && *s != '\n') s++;
      if (*s == '\n') return NULL;
      if (strncmp(s, t, l) == 0) break;
      s++;
   }
   s += l;
   while (isspace(*s)) s++;
   return s;
}

int decimal(char *s)
{  int sign = 1, d = 0;

   while (isspace(*s)) s++;
   if (*s == '-') { sign = -1; s++; }
   while (isdigit(*s)) { d = d * 10 + (*s - '0'); s++; }
   return sign*d;
}

/* Nextsymbol() reads lines from enc until a '[', '/' or a ']' is 
 * encountered. In that case pline points to the character following
 * this symbol otherwise a fatal error is generated.
 */

FILE *enc;

#define LINEBUF 1024
char line[LINEBUF], *pline = NULL;

char nextsymbol()
{
   for (;;) {
      if (pline == NULL) {
         if (fgets(line, LINEBUF-1, enc) == NULL)
            fatal("unexpected EOF while reading %s\n", encfile);
         pline = line;
      }
      if (*pline == '[' || *pline == ']' || *pline == '/') break;
      if (*pline == '%' || *pline == '\n' || *pline == '\0') 
      {  pline = NULL; 
         continue; 
      }
      pline++;
   }
   pline++; return *(pline-1);
}

char *nextpsname()
{  int i = 0;
   char the_nextname[MAXSTRLEN], *name;

   for (;;) {
      if (pline == NULL) {
         if (fgets(line, LINEBUF-1, enc) == NULL)
            fatal("unexpected EOF while reading %s\n", encfile);
         pline = line;
      }
      if (*pline == '%' || *pline == '\n' || *pline == '\0') 
      {  pline = NULL; 
         continue; 
      }
      if (isspace(*pline)) {
         pline++;
	 continue;
      }
      if (!isalpha(*pline)) fatal("invalid name in %s\n", encfile);
      /* pt981009: added '_' Staszek Wawrykiewicz <staw@gust.org.pl> */
      while (isalnum(*pline) || *pline == '-' || *pline == '_') {
	if (i > MAXSTRLEN-2)
	  fatal("name too long in %s (%s)\n", line, encfile);
	the_nextname[i++] = *pline++;
      }
      the_nextname[i] = '\0';
      name = malloc(i+1);
      if (name == NULL) fatal("Out of memory\n");
      strcpy(name, the_nextname);
      break;
   }
   /* pt981009: removed pline++ Staszek Wawrykiewicz <staw@gust.org.pl> */
   return name;
}

typedef char *encoding[256];

void getenc(char **fontname, char **encname, encoding ev, int width[256])
{  int i, len, SCMseen, Ccnt, wx, cc;
   FILE *afm;
   char *name, ns;
   
   for (i=0; i < 256; i++) { ev[i] = NULL; width[i] = -1000; }
   if (encfile) {
      enc = fopen(encfile, "r");
      if (enc == NULL) fatal("Can't open %s\n", encfile);
      if (nextsymbol() != '/') fatal("missing '/encoding' in %s\n", encfile);
      *encname = nextpsname();
      if (nextsymbol() != '[') fatal("missing '[' in %s\n", encfile);
      i = 0;
      while (i < 256 && (ns = nextsymbol()) == '/') {
         name = my_string(pline);
         if (strcmp(name, ".notdef") == 0) { 
            i++; remove_string();
            continue; 
         }
         addcc(name, i++);
      }
      if (i != 256) fatal("missing %d symbols in %s\n", 256-i, encfile);
      if (nextsymbol() != ']') fatal("missing ']' in %s\n", encfile);
      fclose(enc);
   }
   afm = fopen(afmfile, "r");
   if (afm == NULL) fatal("Can't open %s\n", afmfile);
   /*
    * Scan header of AFM file and take the FontName and EncodingScheme
    * (used for identification purposes in the PK postamble)
    * Stop after reading `StartCharMetrics'.
    */
   SCMseen = 0; Ccnt = 0;
   while (fgets(line, LINEBUF-1, afm)) {
      if (strncmp(line, "FontName", 8) == 0) { 
	 pline = value_after(line, "FontName"); 
	 len = strlen(pline);
	 if (*(pline+len-1) == '\n') {
	    *(pline+len-1) = '\0'; len--;
	 }
	 *fontname = malloc(len + 1);
	 if (*fontname == NULL) fatal("Out of memory\n");
         strcpy(*fontname, pline);
      }
      else if (encname == NULL && strncmp(line, "EncodingScheme", 8) == 0) { 
	 pline = value_after(line, "EncodingScheme"); 
	 len = strlen(pline);
	 if (*(pline+len-1) == '\n') {
	    *(pline+len-1) = '\0'; len--;
	 }
	 *encname = malloc(len + 1);
	 if (*encname == NULL) fatal("Out of memory\n");
	 strcpy(*encname, pline);
      }
      else if (strncmp(line, "StartCharMetrics", 16) == 0) {
         SCMseen = 1; break;
      }
   }
   if (SCMseen == 0) fatal("%s: no metrics info\n", afmfile);
   while (fgets(line, LINEBUF-1, afm)) {
      if (strncmp(line, "EndCharMetrics", 14) == 0) break;
      if (strncmp(line, "C ", 2) != 0) 
         fatal("%s: unexpected line\n", afmfile);

      /* Gracefully terminate when the AFM file is not valid.  Every line */
      /* in the AFM file should specify a "C"haracter, a "WX"idth, and    */
      /* a "N"ame. (ndw)
      */
      pline = value_after(line, "C"); 
      if (pline == NULL)
         fatal("\nBad char metric in %s (no char):\n\t %s\n", afmfile, line);
      cc = decimal(pline);
      pline = value_after(pline, "WX"); 
      if (pline == NULL)
         fatal("\nBad char metric in %s (no width):\n\t %s\n", afmfile, line);
      wx = decimal(pline);
      pline = value_after(pline, "N"); 
      if (pline == NULL)
         fatal("\nBad char metric in %s (no name):\n\t %s\n", afmfile, line);
      name = my_string(pline);
      
      if (encfile) {
         if ((i = getcc(name)) == -1) {
            remove_string();
            continue;
	 }
         do { /* allow more occurences per name */
            ev[i] = name; width[i] = wx;
            i = getcc(name);
         }
         while (i >= 0);
      }
      else if (cc >= 0 && cc <= 255) { /* ndw: 10/13/92 */
	 ev[cc] = name; width[cc] = wx;
      }
      else if (cc > 255)
	 msg("Char code %d ignored (out of range).\n", cc);
      Ccnt++;
   }
   if (Ccnt == 0) fatal("%s: no characters selected\n", afmfile);   
}

/* END MODULE encoding */

/* MODULE binary_table
 * depends on: MODULE string
 *             <stdlib.h>
 *             <string.h>
 */

/*
 * TUG has accepted an extended font layout (EC.enc) where the hyphen 
 * occupies two positions ('055 and '177) to handle exeptional hyphenation
 * situations (see TUGboat#11 1990 (no. 4) pp. 514).
 * To handle this special case we allow two charcodes per symbol.
 */
  
/*
 * Two functions for a binary search table. One to add a name and one 
 * to lookup a name. This table is used to store all encoding names and 
 * its corresponding charcode.
 */

struct Node
{ char *name;                    /* pointer to a PostScript string */
  short cc[2];                   /* its charcode's (maximum of two) */
  short left, right;
} node[256];

static int freenode = 0;
static short root = -1;

void addcc(char *name, int charcode)
{  short r, p, q;

   q = root;
   while (q != -1) {
      p = q; r = strcmp(name, node[p].name);
      if (r == 0) {
      	 if (node[p].cc[1] == -1) {
      	    node[p].cc[1] = charcode;
      	    return;
      	 }
      	 else 
      	    fatal("%s: more than two codes for %s (%d,%d,%d)\n", 
      	       encfile, name, node[p].cc[0], node[p].cc[1], charcode);
      }
      else if (r < 0) q=node[p].left;
      else q=node[p].right;
   }
   if (freenode == 256) 
      fatal("%s: too many PostScript names\n", encfile);
      /* not found hence insert */
   node[freenode].name = name;
   node[freenode].cc[0] = charcode;
   node[freenode].cc[1] = -1;
   node[freenode].left = node[freenode].right = -1;
   if (root == -1)
      root = freenode;
   else {
      if (r < 0) node[p].left=freenode;
      else node[p].right=freenode;
   }
   freenode++;
}

int getcc(char *name)
{  short r, p, q, cc;

   q = root;
   while (q != -1) {
      p = q; r = strcmp(name, node[p].name);
      if (r == 0) {
      	 cc = node[p].cc[0];
      	 node[p].cc[0] = node[p].cc[1];
      	 node[p].cc[1] = -1;
      	 return cc; 
      }
      else if (r < 0) q = node[p].left;
      else q = node[p].right;
   }
   return -1;
}

/* END MODULE binary_table */

/* MODULE string: 
 * depends on: <ctype.h>
 *             <string.h>
 */

/* String() reads a string starting at a given character pointer. 
 * After removing leading whitespace the resulting string (with possible
 * `.' and '_' characters) is stored in a stringpool after which a pointer 
 * to it is returned.
 */

#define POOLSIZE 10000
static char stringpool[POOLSIZE];
static int poolsize,      /* occupied space annex free index */
           lastpoolsize;  /* previous poolsize */

char *my_string(char *s)
{  int length; char *str;

   while (isspace(*s)) s++;
   str = s; length = 0;
   while (isalnum(*s) || *s == '.' || *s == '_') { length++; s++; }
   if (length == 0) return NULL;
   else {
      if (length + 1 > POOLSIZE - poolsize)
         fatal("Out of memory %s\n", s);
      strncpy(stringpool+poolsize, str, length);
      str = stringpool + poolsize;
      s = stringpool + poolsize + length;
      *s = '\0';
      lastpoolsize = poolsize;
      poolsize += length+1;
      return str; 
   }
}

/* remove last string from stringpool */
void remove_string()
{
   poolsize = lastpoolsize;
}

/* END MODULE string */