Debian Codegraph @

texk/web2c/pdftexdir/writet1.c

raw
1 /*
2 Copyright 1996-2023 Han The Thanh <[email protected]>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
17 #include "ptexlib.h"
18 #include <stdarg.h>
19 #include <string.h>
20
21 #define t1_log(str) tex_printf("%s",str)
22 #define get_length1() t1_length1 = t1_offset() - t1_save_offset
23 #define get_length2() t1_length2 = t1_offset() - t1_save_offset
24 #define get_length3() t1_length3 = fixedcontent? t1_offset() - t1_save_offset : 0
25 #define save_offset() t1_save_offset = t1_offset()
26
27 #define t1_open() open_input(&t1_file, kpse_type1_format, FOPEN_RBIN_MODE)
28 #define t1_close() xfclose(t1_file, cur_file_name)
29 #define t1_getchar() getc(t1_file)
30 #define t1_putchar fb_putchar
31 #define t1_offset fb_offset
32 #define t1_ungetchar(c) ungetc(c, t1_file)
33 #define t1_eof() feof(t1_file)
34
35 #define t1_prefix(s) str_prefix(t1_line_array, s)
36 #define t1_buf_prefix(s) str_prefix(t1_buf_array, s)
37 #define t1_suffix(s) str_suffix(t1_line_array, t1_line_ptr, s)
38 #define t1_buf_suffix(s) str_suffix(t1_buf_array, t1_buf_ptr, s)
39 #define t1_charstrings() strstr(t1_line_array, charstringname)
40 #define t1_subrs() t1_prefix("/Subrs")
41 #define t1_end_eexec() t1_suffix("mark currentfile closefile")
42 #define t1_cleartomark() t1_prefix("cleartomark")
43
44 #define enc_open() open_input(&enc_file, kpse_enc_format, FOPEN_RBIN_MODE)
45 #define enc_close() xfclose(enc_file, cur_file_name)
46 #define enc_getchar() getc(enc_file)
47 #define enc_eof() feof(enc_file)
48
49 #define valid_code(c) (c >= 0 && c < 256)
50 #define fixedcontent false /* false for pdfTeX, true for dvips */
51
52 static const char *standard_glyph_names[256] = {
53 /* 0x00 */
54 notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef,
55 notdef, notdef, notdef, notdef, notdef, notdef, notdef,
56 /* 0x10 */
57 notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef,
58 notdef, notdef, notdef, notdef, notdef, notdef, notdef,
59 /* 0x20 */
60 "space", "exclam", "quotedbl", "numbersign", "dollar", "percent",
61 "ampersand", "quoteright", "parenleft", "parenright", "asterisk",
62 "plus", "comma", "hyphen", "period", "slash",
63 /* 0x30 */
64 "zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
65 "nine", "colon", "semicolon", "less", "equal", "greater", "question",
66 /* 0x40 */
67 "at", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
68 "O",
69 /* 0x50 */
70 "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "bracketleft",
71 "backslash", "bracketright", "asciicircum", "underscore",
72 /* 0x60 */
73 "quoteleft", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
74 "m", "n", "o",
75 /* 0x70 */
76 "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "braceleft", "bar",
77 "braceright", "asciitilde", notdef,
78 /* 0x80 */
79 notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef,
80 notdef, notdef, notdef, notdef, notdef, notdef, notdef,
81 /* 0x90 */
82 notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef,
83 notdef, notdef, notdef, notdef, notdef, notdef, notdef,
84 /* 0xa0 */
85 notdef, "exclamdown", "cent", "sterling", "fraction", "yen", "florin",
86 "section", "currency", "quotesingle", "quotedblleft", "guillemotleft",
87 "guilsinglleft", "guilsinglright", "fi", "fl",
88 /* 0xb0 */
89 notdef, "endash", "dagger", "daggerdbl", "periodcentered", notdef,
90 "paragraph", "bullet", "quotesinglbase", "quotedblbase",
91 "quotedblright", "guillemotright", "ellipsis", "perthousand", notdef,
92 "questiondown",
93 /* 0xc0 */
94 notdef, "grave", "acute", "circumflex", "tilde", "macron", "breve",
95 "dotaccent", "dieresis", notdef,
96 "ring", "cedilla", notdef, "hungarumlaut", "ogonek", "caron",
97 /* 0xd0 */
98 "emdash", notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef,
99 notdef, notdef, notdef, notdef, notdef, notdef, notdef,
100 /* 0xe0 */
101 notdef, "AE", notdef, "ordfeminine", notdef, notdef, notdef, notdef,
102 "Lslash", "Oslash", "OE", "ordmasculine", notdef, notdef, notdef,
103 notdef,
104 /* 0xf0 */
105 notdef, "ae", notdef, notdef, notdef, "dotlessi", notdef, notdef, "lslash",
106 "oslash", "oe", "germandbls", notdef, notdef, notdef, notdef
107 };
108
109 integer t1_length1, t1_length2, t1_length3;
110 static integer t1_save_offset;
111 static integer t1_fontname_offset;
112 static fd_entry *fd_cur;
113
114 static char charstringname[] = "/CharStrings";
115
116 enum { ENC_STANDARD, ENC_BUILTIN } t1_encoding;
117
118 #define T1_BUF_SIZE 0x10
119 #define ENC_BUF_SIZE 0x1000
120
121 #define CS_HSTEM 1
122 #define CS_VSTEM 3
123 #define CS_VMOVETO 4
124 #define CS_RLINETO 5
125 #define CS_HLINETO 6
126 #define CS_VLINETO 7
127 #define CS_RRCURVETO 8
128 #define CS_CLOSEPATH 9
129 #define CS_CALLSUBR 10
130 #define CS_RETURN 11
131 #define CS_ESCAPE 12
132 #define CS_HSBW 13
133 #define CS_ENDCHAR 14
134 #define CS_RMOVETO 21
135 #define CS_HMOVETO 22
136 #define CS_VHCURVETO 30
137 #define CS_HVCURVETO 31
138 #define CS_1BYTE_MAX (CS_HVCURVETO + 1)
139
140 #define CS_DOTSECTION CS_1BYTE_MAX + 0
141 #define CS_VSTEM3 CS_1BYTE_MAX + 1
142 #define CS_HSTEM3 CS_1BYTE_MAX + 2
143 #define CS_SEAC CS_1BYTE_MAX + 6
144 #define CS_SBW CS_1BYTE_MAX + 7
145 #define CS_DIV CS_1BYTE_MAX + 12
146 #define CS_CALLOTHERSUBR CS_1BYTE_MAX + 16
147 #define CS_POP CS_1BYTE_MAX + 17
148 #define CS_SETCURRENTPOINT CS_1BYTE_MAX + 33
149 #define CS_2BYTE_MAX (CS_SETCURRENTPOINT + 1)
150 #define CS_MAX CS_2BYTE_MAX
151
152 typedef unsigned char byte;
153
154 typedef struct {
155 byte nargs; /* number of arguments */
156 boolean bottom; /* take arguments from bottom of stack? */
157 boolean clear; /* clear stack? */
158 boolean valid;
159 } cc_entry; /* CharString Command */
160
161 typedef struct {
162 char *name; /* glyph name (or notdef for Subrs entry) */
163 byte *data;
164 unsigned short len; /* length of the whole string */
165 unsigned short cslen; /* length of the encoded part of the string */
166 boolean used;
167 boolean valid;
168 } cs_entry;
169
170 static unsigned short t1_dr, t1_er;
171 static const unsigned short t1_c1 = 52845, t1_c2 = 22719;
172 static unsigned short t1_cslen;
173 static short t1_lenIV;
174 static char enc_line[ENC_BUF_SIZE];
175
176 /* define t1_line_ptr, t1_line_array & t1_line_limit */
177 typedef char t1_line_entry;
178 define_array(t1_line);
179
180 /* define t1_buf_ptr, t1_buf_array & t1_buf_limit */
181 typedef char t1_buf_entry;
182 define_array(t1_buf);
183
184 static int cs_start;
185
186 static cs_entry *cs_tab, *cs_ptr, *cs_notdef;
187 static char *cs_dict_start, *cs_dict_end;
188 static int cs_count, cs_size, cs_size_pos;
189
190 static cs_entry *subr_tab;
191 static char *subr_array_start, *subr_array_end;
192 static int subr_max, subr_size, subr_size_pos;
193
194 /* This list contains the begin/end tokens commonly used in the */
195 /* /Subrs array of a Type 1 font. */
196
197 static const char *cs_token_pairs_list[][2] = {
198 {" RD", "NP"},
199 {" -|", "|"},
200 {" RD", "noaccess put"},
201 {" -|", "noaccess put"},
202 {NULL, NULL}
203 };
204 static const char **cs_token_pair;
205
206 static boolean t1_pfa, t1_cs, t1_scan, t1_eexec_encrypt, t1_synthetic;
207 static int t1_in_eexec; /* 0 before eexec-encrypted, 1 during, 2 after */
208 static long t1_block_length;
209 static int last_hexbyte;
210 static FILE *t1_file;
211 static FILE *enc_file;
212
213 static void enc_getline(void)
214 {
215 char *p;
216 int c;
217 restart:
218 if (enc_eof())
219 pdftex_fail("unexpected end of file");
220 p = enc_line;
221 do {
222 c = enc_getchar();
223 append_char_to_buf(c, p, enc_line, ENC_BUF_SIZE);
224 } while (c != 10);
225 append_eol(p, enc_line, ENC_BUF_SIZE);
226 if (p - enc_line < 2 || *enc_line == '%')
227 goto restart;
228 }
229
230 /* read encoding from .enc file, return glyph_names array, or pdffail() */
231
232 char **load_enc_file(char *enc_name)
233 {
234 char buf[ENC_BUF_SIZE], *p, *r;
235 int i, names_count;
236 char **glyph_names;
237 set_cur_file_name(enc_name);
238 if (!enc_open()) {
239 pdftex_fail("cannot open encoding file for reading");
240 }
241 glyph_names = xtalloc(256, char *);
242 for (i = 0; i < 256; i++)
243 glyph_names[i] = notdef;
244 t1_log("{");
245 t1_log(cur_file_name = (char *) nameoffile + 1);
246 enc_getline();
247 if (*enc_line != '/' || (r = strchr(enc_line, '[')) == NULL) {
248 remove_eol(r, enc_line);
249 pdftex_fail
250 ("invalid encoding vector (a name or `[' missing): `%s'", enc_line);
251 }
252 names_count = 0;
253 r++; /* skip '[' */
254 skip(r, ' ');
255 for (;;) {
256 while (*r == '/') {
257 for (p = buf, r++;
258 *r != ' ' && *r != 10 && *r != ']' && *r != '/'; *p++ = *r++);
259 *p = 0;
260 skip(r, ' ');
261 if (names_count > 255)
262 pdftex_fail("encoding vector contains more than 256 names");
263 if (strcmp(buf, notdef) != 0)
264 glyph_names[names_count] = xstrdup(buf);
265 names_count++;
266 }
267 if (*r != 10 && *r != '%') {
268 if (str_prefix(r, "] def"))
269 goto done;
270 else {
271 remove_eol(r, enc_line);
272 pdftex_fail
273 ("invalid encoding vector: a name or `] def' expected: `%s'", enc_line);
274 }
275 }
276 enc_getline();
277 r = enc_line;
278 }
279 done:
280 enc_close();
281 t1_log("}");
282 cur_file_name = NULL;
283 return glyph_names;
284 }
285
286 static void t1_check_pfa(void)
287 {
288 const int c = t1_getchar();
289 t1_pfa = (c != 128) ? true : false;
290 t1_ungetchar(c);
291 }
292
293 static int t1_getbyte(void)
294 {
295 int c = t1_getchar();
296 if (t1_pfa)
297 return c;
298 if (t1_block_length == 0) {
299 if (c != 128)
300 pdftex_fail("invalid marker");
301 c = t1_getchar();
302 if (c == 3) {
303 while (!t1_eof())
304 t1_getchar();
305 return EOF;
306 }
307 t1_block_length = t1_getchar() & 0xff;
308 t1_block_length |= (t1_getchar() & 0xff) << 8;
309 t1_block_length |= (t1_getchar() & 0xff) << 16;
310 t1_block_length |= (t1_getchar() & 0xff) << 24;
311 c = t1_getchar();
312 }
313 t1_block_length--;
314 return c;
315 }
316
317 static int hexval(int c)
318 {
319 if (c >= 'A' && c <= 'F')
320 return c - 'A' + 10;
321 else if (c >= 'a' && c <= 'f')
322 return c - 'a' + 10;
323 else if (c >= '0' && c <= '9')
324 return c - '0';
325 else
326 return -1;
327 }
328
329 static byte edecrypt(byte cipher)
330 {
331 byte plain;
332 if (t1_pfa) {
333 while (cipher == 10 || cipher == 13)
334 cipher = t1_getbyte();
335 last_hexbyte = cipher = (hexval(cipher) << 4) + hexval(t1_getbyte());
336 }
337 plain = (cipher ^ (t1_dr >> 8));
338 t1_dr = (cipher + t1_dr) * t1_c1 + t1_c2;
339 return plain;
340 }
341
342 static byte cdecrypt(byte cipher, unsigned short *cr)
343 {
344 const byte plain = (cipher ^ (*cr >> 8));
345 *cr = (cipher + *cr) * t1_c1 + t1_c2;
346 return plain;
347 }
348
349 static byte eencrypt(byte plain)
350 {
351 const byte cipher = (plain ^ (t1_er >> 8));
352 t1_er = (cipher + t1_er) * t1_c1 + t1_c2;
353 return cipher;
354 }
355
356 static byte cencrypt(byte plain, unsigned short *cr)
357 {
358 const byte cipher = (plain ^ (*cr >> 8));
359 *cr = (cipher + *cr) * t1_c1 + t1_c2;
360 return cipher;
361 }
362
363 static char *eol(char *s)
364 {
365 char *p = strend(s);
366 if (p - s > 1 && p[-1] != 10) {
367 *p++ = 10;
368 *p = 0;
369 }
370 return p;
371 }
372
373 static float t1_scan_num(char *p, char **r)
374 {
375 float f;
376 skip(p, ' ');
377 if (sscanf(p, "%g", &f) != 1) {
378 remove_eol(p, t1_line_array);
379 pdftex_fail("a number expected: `%s'", t1_line_array);
380 }
381 if (r != NULL) {
382 for (; isdigit((unsigned char)*p) || *p == '.' ||
383 *p == 'e' || *p == 'E' || *p == '+' || *p == '-'; p++);
384 *r = p;
385 }
386 return f;
387 }
388
389 static boolean str_suffix(const char *begin_buf, const char *end_buf,
390 const char *s)
391 {
392 const char *s1 = end_buf - 1, *s2 = strend(s) - 1;
393 if (*s1 == 10)
394 s1--;
395 while (s1 >= begin_buf && s2 >= s) {
396 if (*s1-- != *s2--)
397 return false;
398 }
399 return s2 < s;
400 }
401
402 static void t1_getline(void)
403 {
404 int c, l, eexec_scan;
405 char *p;
406 static const char eexec_str[] = "currentfile eexec";
407 static int eexec_len = 17; /* strlen(eexec_str) */
408 restart:
409 if (t1_eof())
410 pdftex_fail("unexpected end of file");
411 t1_line_ptr = t1_line_array;
412 alloc_array(t1_line, 1, T1_BUF_SIZE);
413 t1_cslen = 0;
414 eexec_scan = 0;
415 c = t1_getbyte();
416 if (c == EOF)
417 goto exit;
418 while (!t1_eof()) {
419 if (t1_in_eexec == 1)
420 c = edecrypt((byte)c);
421 alloc_array(t1_line, 1, T1_BUF_SIZE);
422 append_char_to_buf(c, t1_line_ptr, t1_line_array, t1_line_limit);
423 if (t1_in_eexec == 0 && eexec_scan >= 0 && eexec_scan < eexec_len) {
424 if (t1_line_array[eexec_scan] == eexec_str[eexec_scan])
425 eexec_scan++;
426 else
427 eexec_scan = -1;
428 }
429 if (c == 10 || (t1_pfa && eexec_scan == eexec_len && c == 32))
430 break;
431 if (t1_cs && t1_cslen == 0 && (t1_line_ptr - t1_line_array > 4) &&
432 (t1_suffix(" RD ") || t1_suffix(" -| "))) {
433 p = t1_line_ptr - 5;
434 while (*p != ' ')
435 p--;
436 t1_cslen = l = t1_scan_num(p + 1, 0);
437 cs_start = t1_line_ptr - t1_line_array; /* cs_start is an index now */
438 alloc_array(t1_line, l, T1_BUF_SIZE);
439 while (l-- > 0)
440 *t1_line_ptr++ = edecrypt((byte)t1_getbyte());
441 }
442 c = t1_getbyte();
443 }
444 alloc_array(t1_line, 2, T1_BUF_SIZE); /* append_eol can append 2 chars */
445 append_eol(t1_line_ptr, t1_line_array, t1_line_limit);
446 if (t1_line_ptr - t1_line_array < 2)
447 goto restart;
448 if (eexec_scan == eexec_len)
449 t1_in_eexec = 1;
450 exit:
451 /* ensure that t1_buf_array has as much room as t1_line_array */
452 t1_buf_ptr = t1_buf_array;
453 alloc_array(t1_buf, t1_line_limit, t1_line_limit);
454 }
455
456 static void t1_putline(void)
457 {
458 char *p = t1_line_array;
459 if (t1_line_ptr - t1_line_array <= 1)
460 return;
461 if (t1_eexec_encrypt) {
462 while (p < t1_line_ptr)
463 t1_putchar(eencrypt(*p++));
464 } else
465 while (p < t1_line_ptr)
466 t1_putchar(*p++);
467 }
468
469 static void t1_puts(const char *s)
470 {
471 if (s != t1_line_array)
472 strcpy(t1_line_array, s);
473 t1_line_ptr = strend(t1_line_array);
474 t1_putline();
475 }
476
477 __attribute__ ((format(printf, 1, 2)))
478 static void t1_printf(const char *fmt, ...)
479 {
480 va_list args;
481 va_start(args, fmt);
482 vsprintf(t1_line_array, fmt, args);
483 t1_puts(t1_line_array);
484 va_end(args);
485 }
486
487 static void t1_init_params(const char *open_name_prefix)
488 {
489 t1_log(open_name_prefix);
490 t1_log(cur_file_name);
491 t1_lenIV = 4;
492 t1_dr = 55665;
493 t1_er = 55665;
494 t1_in_eexec = 0;
495 t1_cs = false;
496 t1_scan = true;
497 t1_synthetic = false;
498 t1_eexec_encrypt = false;
499 t1_block_length = 0;
500 t1_check_pfa();
501 }
502
503 static void t1_close_font_file(const char *close_name_suffix)
504 {
505 t1_log(close_name_suffix);
506 t1_close();
507 cur_file_name = NULL;
508 }
509
510 static void t1_check_block_len(boolean decrypt)
511 {
512 int l, c;
513 if (t1_block_length == 0)
514 return;
515 c = t1_getbyte();
516 if (decrypt)
517 c = edecrypt((byte)c);
518 l = t1_block_length;
519 if (!(l == 0 && (c == 10 || c == 13))) {
520 pdftex_fail("%i bytes more than expected", l + 1);
521 }
522 }
523
524 static void t1_start_eexec(void)
525 {
526 int i;
527 assert(is_included(fd_cur->fm));
528 get_length1();
529 save_offset();
530 if (!t1_pfa)
531 t1_check_block_len(false);
532 for (t1_line_ptr = t1_line_array, i = 0; i < 4; i++) {
533 edecrypt((byte)t1_getbyte());
534 *t1_line_ptr++ = 0;
535 }
536 t1_eexec_encrypt = true;
537 t1_putline(); /* to put the first four bytes */
538 }
539
540 static void t1_stop_eexec(void)
541 {
542 int c;
543 assert(is_included(fd_cur->fm));
544 get_length2();
545 save_offset();
546 t1_eexec_encrypt = false;
547 if (!t1_pfa)
548 t1_check_block_len(true);
549 else {
550 c = edecrypt((byte)t1_getbyte());
551 if (!(c == 10 || c == 13)) {
552 if (last_hexbyte == 0)
553 t1_puts("00");
554 else
555 pdftex_fail("unexpected data after eexec");
556 }
557 }
558 t1_cs = false;
559 t1_in_eexec = 2;
560 }
561
562 /* macros for various transforms; currently only slant and extend are used */
563
564 #define do_xshift(x,a) {x[4]+=a;}
565 #define do_yshift(x,a) {x[5]+=a;}
566 #define do_xscale(x,a) {x[0]*=a; x[2]*=a; x[4]*=a;}
567 #define do_yscale(x,a) {x[1]*=a; x[3]*=a; x[5]*=a;}
568 #define do_extend(x,a) {do_xscale(x,a);}
569 #define do_scale(x,a) {do_xscale(x,a); do_yscale(x,a);}
570 #define do_slant(x,a) {x[0]+=x[1]*(a); x[2]+=x[3]*(a); x[4]+=x[5]*(a);}
571 #define do_shear(x,a) {x[1]+=x[0]*(a); x[3]+=x[2]*(a); x[5]+=x[4]*(a);}
572 #define do_rotate(x,a) \
573 {float t, u=cos(a), v=sin(a); \
574 t =x[0]*u+x[1]*-v; \
575 x[1] =x[0]*v+x[1]* u; x[0]=t; \
576 t =x[2]*u+x[3]*-v; \
577 x[3] =x[2]*v+x[3]* u; x[2]=t; \
578 t =x[4]*u+x[5]*-v; \
579 x[5] =x[4]*v+x[5]* u; x[4]=t;}
580
581 static void t1_modify_fm(void)
582 {
583 /*
584 * font matrix is given as six numbers a0..a5, which stands for the matrix
585 *
586 * a0 a1 0
587 * M = a2 a3 0
588 * a4 a5 1
589 *
590 * ExtendFont is given as
591 *
592 * e 0 0
593 * E = 0 1 0
594 * 0 0 1
595 *
596 * SlantFont is given as
597 *
598 * 1 0 0
599 * S = s 1 0
600 * 0 0 1
601 *
602 * The slant transform must be done _before_ the extend transform
603 * for compatibility!
604 */
605 float a[6];
606 int i, c;
607 char *p, *q, *r;
608 if ((p = strchr(t1_line_array, '[')) == 0)
609 if ((p = strchr(t1_line_array, '{')) == 0) {
610 remove_eol(p, t1_line_array);
611 pdftex_fail("FontMatrix: an array expected: `%s'", t1_line_array);
612 }
613 c = *p++; /* save the character '[' resp. '{' */
614 strncpy(t1_buf_array, t1_line_array, (size_t) (p - t1_line_array));
615 r = t1_buf_array + (p - t1_line_array);
616 for (i = 0; i < 6; i++) {
617 a[i] = t1_scan_num(p, &q);
618 p = q;
619 }
620 if (fm_slant(fd_cur->fm) != 0)
621 do_slant(a, fm_slant(fd_cur->fm) * 1E-3);
622 if (fm_extend(fd_cur->fm) != 0)
623 do_extend(a, fm_extend(fd_cur->fm) * 1E-3);
624 for (i = 0; i < 6; i++) {
625 sprintf(r, "%g ", a[i]);
626 r = strend(r);
627 }
628 if (c == '[') {
629 while (*p != ']' && *p != 0)
630 p++;
631 } else {
632 while (*p != '}' && *p != 0)
633 p++;
634 }
635 if (*p == 0) {
636 remove_eol(p, t1_line_array);
637 pdftex_fail
638 ("FontMatrix: cannot find the corresponding character to '%c': `%s'",
639 c, t1_line_array);
640 }
641 strcpy(r, p);
642 strcpy(t1_line_array, t1_buf_array);
643 t1_line_ptr = eol(t1_line_array);
644 }
645
646 static void t1_modify_italic(void)
647 {
648 float a;
649 char *p, *r;
650 if (fm_slant(fd_cur->fm) == 0)
651 return;
652 p = strchr(t1_line_array, ' ');
653 strncpy(t1_buf_array, t1_line_array, (size_t) (p - t1_line_array + 1));
654 a = t1_scan_num(p + 1, &r);
655 a -= atan(fm_slant(fd_cur->fm) * 1E-3) * (180 / M_PI);
656 sprintf(t1_buf_array + (p - t1_line_array + 1), "%g", a);
657 strcpy(strend(t1_buf_array), r);
658 strcpy(t1_line_array, t1_buf_array);
659 t1_line_ptr = eol(t1_line_array);
660 fd_cur->font_dim[ITALIC_ANGLE_CODE].val = round(a);
661 fd_cur->font_dim[ITALIC_ANGLE_CODE].set = true;
662 }
663
664 static void t1_scan_keys(void)
665 {
666 int i, k;
667 char *p, *q, *r;
668 const key_entry *key;
669 if (fm_extend(fd_cur->fm) != 0 || fm_slant(fd_cur->fm) != 0) {
670 if (t1_prefix("/FontMatrix")) {
671 t1_modify_fm();
672 return;
673 }
674 if (t1_prefix("/ItalicAngle")) {
675 t1_modify_italic();
676 return;
677 }
678 }
679 if (t1_prefix("/FontType")) {
680 p = t1_line_array + strlen("FontType") + 1;
681 if ((i = t1_scan_num(p, 0)) != 1)
682 pdftex_fail("Type%d fonts unsupported by pdfTeX", i);
683 return;
684 }
685 for (key = font_key; key - font_key < FONT_KEYS_NUM; key++) {
686 if (key->t1name[0] != '\0' &&
687 str_prefix(t1_line_array + 1, key->t1name))
688 break;
689 }
690 if (key - font_key == FONT_KEYS_NUM)
691 return;
692 p = t1_line_array + strlen(key->t1name) + 1;
693 skip(p, ' ');
694 if ((k = key - font_key) == FONTNAME_CODE) {
695 if (*p != '/') {
696 remove_eol(p, t1_line_array);
697 pdftex_fail("a name expected: `%s'", t1_line_array);
698 }
699 r = ++p; /* skip the slash */
700 for (q = t1_buf_array; *p != ' ' && *p != 10; *q++ = *p++);
701 *q = 0;
702 if (fm_slant(fd_cur->fm) != 0) {
703 sprintf(q, "-Slant_%i", (int) fm_slant(fd_cur->fm));
704 q = strend(q);
705 }
706 if (fm_extend(fd_cur->fm) != 0) {
707 sprintf(q, "-Extend_%i", (int) fm_extend(fd_cur->fm));
708 }
709 xfree(fd_cur->fontname);
710 fd_cur->fontname = xstrdup(t1_buf_array);
711 /* at this moment we cannot call make_subset_tag() yet, as the encoding
712 * is not read; thus we mark the offset of the subset tag and write it
713 * later */
714 if (is_subsetted(fd_cur->fm)) {
715 assert(is_included(fd_cur->fm));
716 t1_fontname_offset = t1_offset() + (r - t1_line_array);
717 strcpy(t1_buf_array, p);
718 sprintf(r, "ABCDEF+%s%s", fd_cur->fontname, t1_buf_array);
719 t1_line_ptr = eol(r);
720 }
721 return;
722 }
723 if ((k == STEMV_CODE || k == FONTBBOX1_CODE)
724 && (*p == '[' || *p == '{'))
725 p++;
726 if (k == FONTBBOX1_CODE) {
727 for (i = 0; i < 4; i++, k++) {
728 fd_cur->font_dim[k].val = t1_scan_num(p, &r);
729 fd_cur->font_dim[k].set = true;
730 p = r;
731 }
732 return;
733 }
734 fd_cur->font_dim[k].val = t1_scan_num(p, 0);
735 fd_cur->font_dim[k].set = true;
736 }
737
738 static void t1_scan_param(void)
739 {
740 static const char *lenIV = "/lenIV";
741 if (!t1_scan || *t1_line_array != '/')
742 return;
743 if (t1_prefix(lenIV)) {
744 t1_lenIV = t1_scan_num(t1_line_array + strlen(lenIV), 0);
745 if (t1_lenIV < 0)
746 pdftex_fail("negative value of lenIV is not supported");
747 return;
748 }
749 t1_scan_keys();
750 }
751
752 static void copy_glyph_names(char **glyph_names, int a, int b)
753 {
754 if (glyph_names[b] != notdef) {
755 xfree(glyph_names[b]);
756 glyph_names[b] = notdef;
757 }
758 if (glyph_names[a] != notdef) {
759 glyph_names[b] = xstrdup(glyph_names[a]);
760 }
761 }
762
763 /* read encoding from Type1 font file, return glyph_names array, or pdffail() */
764
765 static char **t1_builtin_enc(void)
766 {
767 int i, a, b, c, counter = 0;
768 char *r, *p, **glyph_names;
769 /* At this moment "/Encoding" is the prefix of t1_line_array */
770 glyph_names = xtalloc(256, char *);
771 for (i = 0; i < 256; i++)
772 glyph_names[i] = notdef;
773 if (t1_suffix("def")) { /* predefined encoding */
774 if (sscanf(t1_line_array + strlen("/Encoding"), "%255s", t1_buf_array) == 1
775 && strcmp(t1_buf_array, "StandardEncoding") == 0) {
776 t1_encoding = ENC_STANDARD;
777 for (i = 0; i < 256; i++) {
778 if (standard_glyph_names[i] != notdef)
779 glyph_names[i] = xstrdup(standard_glyph_names[i]);
780 }
781 return glyph_names;
782 }
783 pdftex_fail("cannot subset font (unknown predefined encoding `%s')",
784 t1_buf_array);
785 }
786 /* At this moment "/Encoding" is the prefix of t1_line_array, and the encoding is
787 * not a predefined encoding.
788 *
789 * We have two possible forms of Encoding vector. The first case is
790 *
791 * /Encoding [/a /b /c...] readonly def
792 *
793 * and the second case can look like
794 *
795 * /Encoding 256 array 0 1 255 {1 index exch /.notdef put} for
796 * dup 0 /x put
797 * dup 1 /y put
798 * ...
799 * readonly def
800 */
801 t1_encoding = ENC_BUILTIN;
802 if (t1_prefix("/Encoding [") || t1_prefix("/Encoding[")) { /* the first case */
803 r = strchr(t1_line_array, '[') + 1;
804 skip(r, ' ');
805 for (;;) {
806 while (*r == '/') {
807 for (p = t1_buf_array, r++;
808 *r != 32 && *r != 10 && *r != ']' && *r != '/';
809 *p++ = *r++);
810 *p = 0;
811 skip(r, ' ');
812 if (counter > 255)
813 pdftex_fail("encoding vector contains more than 256 names");
814 if (strcmp(t1_buf_array, notdef) != 0)
815 glyph_names[counter] = xstrdup(t1_buf_array);
816 counter++;
817 }
818 if (*r != 10 && *r != '%') {
819 if (str_prefix(r, "] def") || str_prefix(r, "] readonly def"))
820 break;
821 else {
822 remove_eol(r, t1_line_array);
823 pdftex_fail
824 ("a name or `] def' or `] readonly def' expected: `%s'", t1_line_array);
825 }
826 }
827 t1_getline();
828 r = t1_line_array;
829 }
830 } else { /* the second case */
831 p = strchr(t1_line_array, 10);
832 for (;;) {
833 if (*p == 10) {
834 t1_getline();
835 p = t1_line_array;
836 }
837 /*
838 check for `dup <index> <glyph> put'
839 */
840 if (sscanf(p, "dup %i%255s put", &i, t1_buf_array) == 2 &&
841 *t1_buf_array == '/' && valid_code(i)) {
842 if (strcmp(t1_buf_array + 1, notdef) != 0)
843 glyph_names[i] = xstrdup(t1_buf_array + 1);
844 p = strstr(p, " put");
845 if (!p)
846 pdftex_fail("invalid pfb, no put found in dup");
847 p += strlen(" put");
848 skip(p, ' ');
849 }
850 /*
851 check for `dup dup <to> exch <from> get put'
852 */
853 else if (sscanf(p, "dup dup %i exch %i get put", &b, &a) == 2
854 && valid_code(a) && valid_code(b)) {
855 copy_glyph_names(glyph_names, a, b);
856 p = strstr(p, " get put");
857 if (!p)
858 pdftex_fail("invalid pfb, no get put found in dup dup");
859 p += strlen(" get put");
860 skip(p, ' ');
861 }
862 /*
863 check for `dup dup <from> <size> getinterval <to> exch putinterval'
864 */
865 else if (sscanf(p, "dup dup %i %i getinterval %i exch putinterval",
866 &a, &c, &b) == 3
867 && valid_code(a) && valid_code(b) && valid_code(c)) {
868 for (i = 0; i < c; i++)
869 copy_glyph_names(glyph_names, a + i, b + i);
870 p = strstr(p, " putinterval");
871 if (!p)
872 pdftex_fail("invalid pfb, no putinterval found in dup dup");
873 p += strlen(" putinterval");
874 skip(p, ' ');
875 }
876 /*
877 check for `def' or `readonly def'
878 */
879 else if ((p == t1_line_array || (p > t1_line_array && p[-1] == ' '))
880 && strcmp(p, "def\n") == 0)
881 return glyph_names;
882 /*
883 skip an unrecognizable word
884 */
885 else {
886 while (*p != ' ' && *p != 10)
887 p++;
888 skip(p, ' ');
889 }
890 }
891 }
892 return glyph_names;
893 }
894
895 static void t1_check_end(void)
896 {
897 if (t1_eof())
898 return;
899 t1_getline();
900 if (t1_prefix("{restore}"))
901 t1_putline();
902 }
903
904 static boolean t1_open_fontfile(const char *open_name_prefix)
905 {
906 ff_entry *ff;
907 ff = check_ff_exist(fd_cur->fm->ff_name, is_truetype(fd_cur->fm));
908 if (ff->ff_path != NULL) {
909 t1_file = xfopen(cur_file_name = ff->ff_path, FOPEN_RBIN_MODE);
910 recorder_record_input(ff->ff_path);
911 } else {
912 set_cur_file_name(fd_cur->fm->ff_name);
913 pdftex_fail("cannot open Type 1 font file for reading");
914 }
915 t1_init_params(open_name_prefix);
916 return true; /* font file found */
917 }
918
919 static void t1_include(void)
920 {
921 do {
922 t1_getline();
923 t1_scan_param();
924 t1_putline();
925 } while (t1_in_eexec == 0);
926 t1_start_eexec();
927 do {
928 t1_getline();
929 t1_scan_param();
930 t1_putline();
931 } while (!(t1_charstrings() || t1_subrs()));
932 t1_cs = true;
933 do {
934 t1_getline();
935 t1_putline();
936 } while (!t1_end_eexec());
937 t1_stop_eexec();
938 if (fixedcontent) { /* copy 512 zeros (not needed for PDF) */
939 do {
940 t1_getline();
941 t1_putline();
942 } while (!t1_cleartomark());
943 t1_check_end(); /* write "{restore}if" if found */
944 }
945 get_length3();
946 }
947
948 #define check_subr(subr) \
949 if (subr >= subr_size || subr < 0) \
950 pdftex_fail("Subrs array: entry index out of range (%i)", subr);
951
952 static const char **check_cs_token_pair(void)
953 {
954 const char **p = (const char **) cs_token_pairs_list;
955 for (; p[0] != NULL; ++p)
956 if (t1_buf_prefix(p[0]) && t1_buf_suffix(p[1]))
957 return p;
958 return NULL;
959 }
960
961 static void cs_store(boolean is_subr)
962 {
963 char *p;
964 cs_entry *ptr;
965 int subr;
966 for (p = t1_line_array, t1_buf_ptr = t1_buf_array; *p != ' ';
967 *t1_buf_ptr++ = *p++);
968 *t1_buf_ptr = 0;
969 if (is_subr) {
970 subr = t1_scan_num(p + 1, 0);
971 check_subr(subr);
972 ptr = subr_tab + subr;
973 } else {
974 ptr = cs_ptr++;
975 if (cs_ptr - cs_tab > cs_size)
976 pdftex_fail
977 ("CharStrings dict: more entries than dict size (%i)", cs_size);
978 if (strcmp(t1_buf_array + 1, notdef) == 0) /* skip the slash */
979 ptr->name = notdef;
980 else
981 ptr->name = xstrdup(t1_buf_array + 1);
982 }
983 /* copy " RD " + cs data to t1_buf_array */
984 memcpy(t1_buf_array, t1_line_array + cs_start - 4,
985 (unsigned) (t1_cslen + 4));
986 /* copy the end of cs data to t1_buf_array */
987 for (p = t1_line_array + cs_start + t1_cslen,
988 t1_buf_ptr = t1_buf_array + t1_cslen + 4;
989 *p != 10; *t1_buf_ptr++ = *p++);
990 *t1_buf_ptr++ = 10;
991 if (is_subr && cs_token_pair == NULL)
992 cs_token_pair = check_cs_token_pair();
993 ptr->len = t1_buf_ptr - t1_buf_array;
994 ptr->cslen = t1_cslen;
995 ptr->data = xtalloc(ptr->len, byte);
996 memcpy(ptr->data, t1_buf_array, ptr->len);
997 ptr->valid = true;
998 }
999
1000 #define store_subr() cs_store(true)
1001 #define store_cs() cs_store(false)
1002
1003 #define CC_STACK_SIZE 24
1004
1005 static integer cc_stack[CC_STACK_SIZE], *stack_ptr = cc_stack;
1006 static cc_entry cc_tab[CS_MAX];
1007 static boolean is_cc_init = false;
1008
1009 #define cc_pop(N) \
1010 if (stack_ptr - cc_stack < (N)) \
1011 stack_error(N); \
1012 stack_ptr -= N
1013
1014 #define stack_error(N) { \
1015 pdftex_fail("CharString: invalid access (%i) to stack (%i entries)", \
1016 (int) N, (int)(stack_ptr - cc_stack)); \
1017 goto cs_error; \
1018 }
1019
1020 /*
1021 static integer cc_get(integer index)
1022 {
1023 if (index < 0) {
1024 if (stack_ptr + index < cc_stack )
1025 stack_error(stack_ptr - cc_stack + index);
1026 return *(stack_ptr + index);
1027 }
1028 else {
1029 if (cc_stack + index >= stack_ptr)
1030 stack_error(index);
1031 return cc_stack[index];
1032 }
1033 }
1034 */
1035
1036 #define cc_get(N) ((N) < 0 ? *(stack_ptr + (N)) : *(cc_stack + (N)))
1037 #define cc_push(V) *stack_ptr++ = V
1038 #define cc_clear() stack_ptr = cc_stack
1039 #define set_cc(N, B, A, C) \
1040 cc_tab[N].nargs = A; \
1041 cc_tab[N].bottom = B; \
1042 cc_tab[N].clear = C; \
1043 cc_tab[N].valid = true
1044
1045 static void cc_init(void)
1046 {
1047 int i;
1048 if (is_cc_init)
1049 return;
1050 for (i = 0; i < CS_MAX; i++)
1051 cc_tab[i].valid = false;
1052 set_cc(CS_HSTEM, true, 2, true);
1053 set_cc(CS_VSTEM, true, 2, true);
1054 set_cc(CS_VMOVETO, true, 1, true);
1055 set_cc(CS_RLINETO, true, 2, true);
1056 set_cc(CS_HLINETO, true, 1, true);
1057 set_cc(CS_VLINETO, true, 1, true);
1058 set_cc(CS_RRCURVETO, true, 6, true);
1059 set_cc(CS_CLOSEPATH, false, 0, true);
1060 set_cc(CS_CALLSUBR, false, 1, false);
1061 set_cc(CS_RETURN, false, 0, false);
1062 /*
1063 set_cc(CS_ESCAPE, false, 0, false);
1064 */
1065 set_cc(CS_HSBW, true, 2, true);
1066 set_cc(CS_ENDCHAR, false, 0, true);
1067 set_cc(CS_RMOVETO, true, 2, true);
1068 set_cc(CS_HMOVETO, true, 1, true);
1069 set_cc(CS_VHCURVETO, true, 4, true);
1070 set_cc(CS_HVCURVETO, true, 4, true);
1071 set_cc(CS_DOTSECTION, false, 0, true);
1072 set_cc(CS_VSTEM3, true, 6, true);
1073 set_cc(CS_HSTEM3, true, 6, true);
1074 set_cc(CS_SEAC, true, 5, true);
1075 set_cc(CS_SBW, true, 4, true);
1076 set_cc(CS_DIV, false, 2, false);
1077 set_cc(CS_CALLOTHERSUBR, false, 0, false);
1078 set_cc(CS_POP, false, 0, false);
1079 set_cc(CS_SETCURRENTPOINT, true, 2, true);
1080 is_cc_init = true;
1081 }
1082
1083 #define cs_getchar() cdecrypt(*data++, &cr)
1084
1085 #define mark_subr(n) cs_mark(0, n)
1086 #define mark_cs(s) cs_mark(s, 0)
1087
1088 __attribute__ ((format(printf, 3, 4)))
1089 static void cs_fail(const char *cs_name, int subr, const char *fmt, ...)
1090 {
1091 char buf[SMALL_BUF_SIZE];
1092 va_list args;
1093 va_start(args, fmt);
1094 vsprintf(buf, fmt, args);
1095 va_end(args);
1096 if (cs_name == NULL)
1097 pdftex_fail("Subr (%i): %s", (int) subr, buf);
1098 else
1099 pdftex_fail("CharString (/%s): %s", cs_name, buf);
1100 }
1101
1102 /* fix a return-less subr by appending CS_RETURN */
1103 static void append_cs_return(cs_entry *ptr)
1104 {
1105 unsigned short cr;
1106 int i;
1107 byte *p, *q, *data, *new_data;
1108 assert(ptr != NULL && ptr->valid && ptr->used);
1109
1110 /* decrypt the cs data to t1_buf_array, append CS_RETURN */
1111 p = (byte *) t1_buf_array;
1112 data = ptr->data + 4;
1113 cr = 4330;
1114 for (i = 0; i < ptr->cslen; i++)
1115 *p++ = cs_getchar();
1116 *p = CS_RETURN;
1117
1118 /* encrypt the new cs data to new_data */
1119 new_data = xtalloc(ptr->len + 1, byte);
1120 memcpy(new_data, ptr->data, 4);
1121 p = new_data + 4;
1122 q = (byte *) t1_buf_array;
1123 cr = 4330;
1124 for (i = 0; i < ptr->cslen + 1; i++)
1125 *p++ = cencrypt(*q++, &cr);
1126 memcpy(p, ptr->data + 4 + ptr->cslen, ptr->len - ptr->cslen - 4);
1127
1128 /* update *ptr */
1129 xfree(ptr->data);
1130 ptr->data = new_data;
1131 ptr->len++;
1132 ptr->cslen++;
1133 }
1134
1135 static void cs_mark(const char *cs_name, int subr)
1136 {
1137 byte *data;
1138 int i, b, cs_len;
1139 int last_cmd = 0;
1140 integer a, a1, a2;
1141 unsigned short cr;
1142 static integer lastargOtherSubr3 = 3; /* the argument of last call to
1143 OtherSubrs[3] */
1144 cs_entry *ptr;
1145 cc_entry *cc;
1146 if (cs_name == NULL) {
1147 check_subr(subr);
1148 ptr = subr_tab + subr;
1149 if (!ptr->valid)
1150 return;
1151 } else {
1152 if (cs_notdef != NULL &&
1153 (cs_name == notdef || strcmp(cs_name, notdef) == 0))
1154 ptr = cs_notdef;
1155 else {
1156 for (ptr = cs_tab; ptr < cs_ptr; ptr++)
1157 if (strcmp(ptr->name, cs_name) == 0)
1158 break;
1159 if (ptr == cs_ptr) {
1160 pdftex_warn("glyph `%s' undefined", cs_name);
1161 return;
1162 }
1163 if (ptr->name == notdef)
1164 cs_notdef = ptr;
1165 }
1166 }
1167 /* only marked CharString entries and invalid entries can be skipped;
1168 valid marked subrs must be parsed to keep the stack in sync */
1169 if (!ptr->valid || (ptr->used && cs_name != NULL))
1170 return;
1171 ptr->used = true;
1172 cr = 4330;
1173 cs_len = ptr->cslen;
1174 data = ptr->data + 4;
1175 for (i = 0; i < t1_lenIV; i++, cs_len--)
1176 cs_getchar();
1177 while (cs_len > 0) {
1178 --cs_len;
1179 b = cs_getchar();
1180 if (b >= 32) {
1181 if (b <= 246)
1182 a = b - 139;
1183 else if (b <= 250) {
1184 --cs_len;
1185 a = ((b - 247) << 8) + 108 + cs_getchar();
1186 } else if (b <= 254) {
1187 --cs_len;
1188 a = -((b - 251) << 8) - 108 - cs_getchar();
1189 } else {
1190 cs_len -= 4;
1191 a = (cs_getchar() & 0xff) << 24;
1192 a |= (cs_getchar() & 0xff) << 16;
1193 a |= (cs_getchar() & 0xff) << 8;
1194 a |= (cs_getchar() & 0xff) << 0;
1195 if (sizeof(integer) > 4 && (a & 0x80000000))
1196 a |= ~0x7FFFFFFF;
1197 }
1198 cc_push(a);
1199 } else {
1200 if (b == CS_ESCAPE) {
1201 b = cs_getchar() + CS_1BYTE_MAX;
1202 cs_len--;
1203 }
1204 if (b >= CS_MAX) {
1205 cs_fail(cs_name, subr, "command value out of range: %i",
1206 (int) b);
1207 goto cs_error;
1208 }
1209 cc = cc_tab + b;
1210 if (!cc->valid) {
1211 cs_fail(cs_name, subr, "command not valid: %i", (int) b);
1212 goto cs_error;
1213 }
1214 if (cc->bottom) {
1215 if (stack_ptr - cc_stack < cc->nargs)
1216 cs_fail(cs_name, subr,
1217 "less arguments on stack (%i) than required (%i)",
1218 (int) (stack_ptr - cc_stack), (int) cc->nargs);
1219 else if (stack_ptr - cc_stack > cc->nargs)
1220 cs_fail(cs_name, subr,
1221 "more arguments on stack (%i) than required (%i)",
1222 (int) (stack_ptr - cc_stack), (int) cc->nargs);
1223 }
1224 last_cmd = b;
1225 switch (cc - cc_tab) {
1226 case CS_CALLSUBR:
1227 a1 = cc_get(-1);
1228 cc_pop(1);
1229 mark_subr(a1);
1230 if (!subr_tab[a1].valid) {
1231 cs_fail(cs_name, subr, "cannot call subr (%i)", (int) a1);
1232 goto cs_error;
1233 }
1234 break;
1235 case CS_DIV:
1236 cc_pop(2);
1237 cc_push(0);
1238 break;
1239 case CS_CALLOTHERSUBR:
1240 if (cc_get(-1) == 3)
1241 lastargOtherSubr3 = cc_get(-3);
1242 a1 = cc_get(-2) + 2;
1243 cc_pop(a1);
1244 break;
1245 case CS_POP:
1246 cc_push(lastargOtherSubr3);
1247 /* the only case when we care about the value being pushed onto
1248 stack is when POP follows CALLOTHERSUBR (changing hints by
1249 OtherSubrs[3])
1250 */
1251 break;
1252 case CS_SEAC:
1253 a1 = cc_get(3);
1254 a2 = cc_get(4);
1255 cc_clear();
1256 mark_cs(standard_glyph_names[a1]);
1257 mark_cs(standard_glyph_names[a2]);
1258 if (fd_cur->gl_tree != NULL) {
1259 /* base and accent characters are needed in CharSet */
1260 avl_probe(fd_cur->gl_tree,
1261 (void *) standard_glyph_names[a1]);
1262 avl_probe(fd_cur->gl_tree,
1263 (void *) standard_glyph_names[a2]);
1264 }
1265 break;
1266 default:
1267 if (cc->clear)
1268 cc_clear();
1269 }
1270 }
1271 }
1272 if (cs_name == NULL && last_cmd != CS_RETURN) {
1273 pdftex_warn("last command in subr `%i' is not a RETURN; "
1274 "I will add it now but please consider fixing the font",
1275 (int) subr);
1276 append_cs_return(ptr);
1277 }
1278 return;
1279 cs_error: /* an error occured during parsing */
1280 cc_clear();
1281 ptr->valid = false;
1282 ptr->used = false;
1283 }
1284
1285 /**********************************************************************/
1286 /* AVL search tree for glyph code by glyph name */
1287
1288 static int comp_t1_glyphs(const void *pa, const void *pb, void *p)
1289 {
1290 return strcmp(*((const char * const *) pa), *((const char * const *) pb));
1291 }
1292
1293 static struct avl_table *create_t1_glyph_tree(char **glyph_names)
1294 {
1295 int i;
1296 void **aa;
1297 static struct avl_table *gl_tree;
1298 gl_tree = avl_create(comp_t1_glyphs, NULL, &avl_xallocator);
1299 assert(gl_tree != NULL);
1300 for (i = 0; i < 256; i++) {
1301 if (glyph_names[i] != notdef &&
1302 (char **) avl_find(gl_tree, &glyph_names[i]) == NULL) {
1303 /* no strdup here, just point to the glyph_names array members */
1304 aa = avl_probe(gl_tree, &glyph_names[i]);
1305 assert(aa != NULL);
1306 }
1307 }
1308 return gl_tree;
1309 }
1310
1311 static void destroy_t1_glyph_tree(struct avl_table *gl_tree)
1312 {
1313 assert(gl_tree != NULL);
1314 avl_destroy(gl_tree, NULL);
1315 }
1316
1317 /**********************************************************************/
1318
1319 static void t1_subset_ascii_part(void)
1320 {
1321 int j, *p;
1322 char *glyph, **gg, **glyph_names;
1323 struct avl_table *gl_tree;
1324 struct avl_traverser t;
1325 void **aa;
1326 assert(fd_cur != NULL);
1327 assert(fd_cur->gl_tree != NULL);
1328 t1_getline();
1329 while (!t1_prefix("/Encoding")) {
1330 t1_scan_param();
1331 if (!(t1_prefix("/UniqueID")
1332 && !strncmp(t1_line_array + strlen(t1_line_array) -4, "def", 3)))
1333 t1_putline();
1334 t1_getline();
1335 }
1336 glyph_names = t1_builtin_enc();
1337 fd_cur->builtin_glyph_names = glyph_names;
1338 if (is_subsetted(fd_cur->fm)) {
1339 assert(is_included(fd_cur->fm));
1340 if (fd_cur->tx_tree != NULL) {
1341 /* take over collected non-reencoded characters from TeX */
1342 avl_t_init(&t, fd_cur->tx_tree);
1343 for (p = (int *) avl_t_first(&t, fd_cur->tx_tree); p != NULL;
1344 p = (int *) avl_t_next(&t)) {
1345 if ((char *) avl_find(fd_cur->gl_tree, glyph_names[*p]) == NULL) {
1346 glyph = xstrdup(glyph_names[*p]);
1347 aa = avl_probe(fd_cur->gl_tree, glyph);
1348 assert(aa != NULL);
1349 }
1350 }
1351 }
1352 make_subset_tag(fd_cur);
1353 assert(t1_fontname_offset != 0);
1354 strncpy(fb_array + t1_fontname_offset, fd_cur->subset_tag, 6);
1355 }
1356 /* now really all glyphs needed from this font are in the fd_cur->gl_tree */
1357 if (t1_encoding == ENC_STANDARD)
1358 t1_puts("/Encoding StandardEncoding def\n");
1359 else {
1360 t1_puts
1361 ("/Encoding 256 array\n0 1 255 {1 index exch /.notdef put} for\n");
1362 gl_tree = create_t1_glyph_tree(glyph_names);
1363 avl_t_init(&t, fd_cur->gl_tree);
1364 j = 0;
1365 for (glyph = (char *) avl_t_first(&t, fd_cur->gl_tree); glyph != NULL;
1366 glyph = (char *) avl_t_next(&t)) {
1367 if ((gg = (char **) avl_find(gl_tree, &glyph)) != NULL) {
1368 t1_printf("dup %i /%s put\n", (int) (gg - glyph_names), *gg);
1369 j++;
1370 }
1371 }
1372 destroy_t1_glyph_tree(gl_tree);
1373 if (j == 0)
1374 /* We didn't mark anything for the Encoding array. */
1375 /* We add "dup 0 /.notdef put" for compatibility */
1376 /* with Acrobat 5.0. */
1377 t1_puts("dup 0 /.notdef put\n");
1378 t1_puts("readonly def\n");
1379 }
1380 do {
1381 t1_getline();
1382 t1_scan_param();
1383 if (!t1_prefix("/UniqueID")) /* ignore UniqueID for subsetted fonts */
1384 t1_putline();
1385 } while (t1_in_eexec == 0);
1386 }
1387
1388 static void cs_init(void)
1389 {
1390 cs_ptr = cs_tab = NULL;
1391 cs_dict_start = cs_dict_end = NULL;
1392 cs_count = cs_size = cs_size_pos = 0;
1393 cs_token_pair = NULL;
1394 subr_tab = NULL;
1395 subr_array_start = subr_array_end = NULL;
1396 subr_max = subr_size = subr_size_pos = 0;
1397 }
1398
1399 static void init_cs_entry(cs_entry *cs)
1400 {
1401 cs->data = NULL;
1402 cs->name = NULL;
1403 cs->len = 0;
1404 cs->cslen = 0;
1405 cs->used = false;
1406 cs->valid = false;
1407 }
1408
1409 static void t1_read_subrs(void)
1410 {
1411 int i, s;
1412 cs_entry *ptr;
1413 t1_getline();
1414 while (!(t1_charstrings() || t1_subrs())) {
1415 t1_scan_param();
1416 if (!t1_prefix("/UniqueID")) /* ignore UniqueID for subsetted fonts */
1417 t1_putline();
1418 t1_getline();
1419 }
1420 found:
1421 t1_cs = true;
1422 t1_scan = false;
1423 if (!t1_subrs())
1424 return;
1425 subr_size_pos = strlen("/Subrs") + 1;
1426 /* subr_size_pos points to the number indicating dict size after "/Subrs" */
1427 subr_size = t1_scan_num(t1_line_array + subr_size_pos, 0);
1428 if (subr_size == 0) {
1429 while (!t1_charstrings())
1430 t1_getline();
1431 return;
1432 }
1433 subr_tab = xtalloc(subr_size, cs_entry);
1434 for (ptr = subr_tab; ptr - subr_tab < subr_size; ptr++)
1435 init_cs_entry(ptr);
1436 subr_array_start = xstrdup(t1_line_array);
1437 t1_getline();
1438 while (t1_cslen) {
1439 store_subr();
1440 t1_getline();
1441 }
1442 /* mark the first four entries without parsing */
1443 for (i = 0; i < subr_size && i < 4; i++)
1444 subr_tab[i].used = true;
1445 /* the end of the Subrs array might have more than one line so we need to
1446 concatnate them to subr_array_end. Unfortunately some fonts don't have
1447 the Subrs array followed by the CharStrings dict immediately (synthetic
1448 fonts). If we cannot find CharStrings in next POST_SUBRS_SCAN lines then
1449 we will treat the font as synthetic and ignore everything until next
1450 Subrs is found
1451 */
1452
1453 #define POST_SUBRS_SCAN 5
1454
1455 s = 0;
1456 *t1_buf_array = 0;
1457 for (i = 0; i < POST_SUBRS_SCAN; i++) {
1458 if (t1_charstrings())
1459 break;
1460 s += t1_line_ptr - t1_line_array;
1461 alloc_array(t1_buf, s, T1_BUF_SIZE);
1462 strcat(t1_buf_array, t1_line_array);
1463 t1_getline();
1464 }
1465 subr_array_end = xstrdup(t1_buf_array);
1466 if (i == POST_SUBRS_SCAN) { /* CharStrings not found;
1467 suppose synthetic font */
1468 for (ptr = subr_tab; ptr - subr_tab < subr_size; ptr++)
1469 if (ptr->valid)
1470 xfree(ptr->data);
1471 xfree(subr_tab);
1472 xfree(subr_array_start);
1473 xfree(subr_array_end);
1474 cs_init();
1475 t1_cs = false;
1476 t1_synthetic = true;
1477 while (!(t1_charstrings() || t1_subrs()))
1478 t1_getline();
1479 goto found;
1480 }
1481 }
1482
1483 #define t1_subr_flush() t1_flush_cs(true)
1484 #define t1_cs_flush() t1_flush_cs(false)
1485
1486 static void t1_flush_cs(boolean is_subr)
1487 {
1488 char *p;
1489 byte *r, *return_cs = NULL;
1490 cs_entry *tab, *end_tab, *ptr;
1491 char *start_line, *line_end;
1492 int count, size_pos;
1493 unsigned short cr, cs_len = 0; /* to avoid warning about uninitialized use of cs_len */
1494 if (is_subr) {
1495 start_line = subr_array_start;
1496 line_end = subr_array_end;
1497 size_pos = subr_size_pos;
1498 tab = subr_tab;
1499 count = subr_max + 1;
1500 end_tab = subr_tab + count;
1501 } else {
1502 start_line = cs_dict_start;
1503 line_end = cs_dict_end;
1504 size_pos = cs_size_pos;
1505 tab = cs_tab;
1506 end_tab = cs_ptr;
1507 count = cs_count;
1508 }
1509 t1_line_ptr = t1_line_array;
1510 for (p = start_line; p - start_line < size_pos;)
1511 *t1_line_ptr++ = *p++;
1512 while (isdigit((unsigned char)*p))
1513 p++;
1514 sprintf(t1_line_ptr, "%u", count);
1515 strcat(t1_line_ptr, p);
1516 t1_line_ptr = eol(t1_line_array);
1517 t1_putline();
1518
1519 /* create return_cs to replace unused subr's */
1520 if (is_subr) {
1521 cr = 4330;
1522 cs_len = 0;
1523 /* at this point we have t1_lenIV >= 0;
1524 * a negative value would be caught in t1_scan_param() */
1525 return_cs = xtalloc(t1_lenIV + 1, byte);
1526 for (cs_len = 0, r = return_cs; cs_len < t1_lenIV; cs_len++, r++)
1527 *r = cencrypt(0x00, &cr);
1528 *r = cencrypt(CS_RETURN, &cr);
1529 cs_len++;
1530 }
1531
1532 for (ptr = tab; ptr < end_tab; ptr++) {
1533 if (ptr->used) {
1534 if (is_subr)
1535 sprintf(t1_line_array, "dup %lu %u",
1536 (unsigned long) (ptr - tab), ptr->cslen);
1537 else
1538 sprintf(t1_line_array, "/%s %u", ptr->name, ptr->cslen);
1539 p = strend(t1_line_array);
1540 memcpy(p, ptr->data, ptr->len);
1541 t1_line_ptr = p + ptr->len;
1542 t1_putline();
1543 } else {
1544 /* replace unsused subr's by return_cs */
1545 if (is_subr) {
1546 sprintf(t1_line_array, "dup %lu %u%s ",
1547 (unsigned long) (ptr - tab), cs_len, cs_token_pair[0]);
1548 p = strend(t1_line_array);
1549 memcpy(p, return_cs, cs_len);
1550 t1_line_ptr = p + cs_len;
1551 t1_putline();
1552 sprintf(t1_line_array, " %s", cs_token_pair[1]);
1553 t1_line_ptr = eol(t1_line_array);
1554 t1_putline();
1555 }
1556 }
1557 xfree(ptr->data);
1558 if (ptr->name != notdef)
1559 xfree(ptr->name);
1560 }
1561 sprintf(t1_line_array, "%s", line_end);
1562 t1_line_ptr = eol(t1_line_array);
1563 t1_putline();
1564 if (is_subr)
1565 xfree(return_cs);
1566 xfree(tab);
1567 xfree(start_line);
1568 xfree(line_end);
1569 }
1570
1571 static void t1_mark_glyphs(void)
1572 {
1573 char *glyph;
1574 struct avl_traverser t;
1575 cs_entry *ptr;
1576 if (t1_synthetic || fd_cur->all_glyphs) { /* mark everything */
1577 if (cs_tab != NULL)
1578 for (ptr = cs_tab; ptr < cs_ptr; ptr++)
1579 if (ptr->valid)
1580 ptr->used = true;
1581 if (subr_tab != NULL) {
1582 for (ptr = subr_tab; ptr - subr_tab < subr_size; ptr++)
1583 if (ptr->valid)
1584 ptr->used = true;
1585 subr_max = subr_size - 1;
1586 }
1587 return;
1588 }
1589 mark_cs(notdef);
1590 avl_t_init(&t, fd_cur->gl_tree);
1591 for (glyph = (char *) avl_t_first(&t, fd_cur->gl_tree); glyph != NULL;
1592 glyph = (char *) avl_t_next(&t)) {
1593 mark_cs(glyph);
1594 }
1595 if (subr_tab != NULL)
1596 for (subr_max = -1, ptr = subr_tab; ptr - subr_tab < subr_size; ptr++)
1597 if (ptr->used && ptr - subr_tab > subr_max)
1598 subr_max = ptr - subr_tab;
1599 }
1600
1601 static void t1_check_unusual_charstring(void)
1602 {
1603 char *p = strstr(t1_line_array, charstringname) + strlen(charstringname);
1604 int i;
1605 /* if no number follows "/CharStrings", let's read the next line */
1606 if (sscanf(p, "%i", &i) != 1) {
1607 /* pdftex_warn("no number found after `%s', I assume it's on the next line",
1608 charstringname); */
1609 strcpy(t1_buf_array, t1_line_array);
1610
1611 /* t1_getline always appends EOL to t1_line_array; let's change it to
1612 * space before appending the next line
1613 */
1614 *(strend(t1_buf_array) - 1) = ' ';
1615
1616 t1_getline();
1617 alloc_array(t1_buf, strlen(t1_line_array)
1618 + (t1_buf_array ? strlen(t1_buf_array) + 1 : 0),
1619 T1_BUF_SIZE);
1620 strcat(t1_buf_array, t1_line_array);
1621 alloc_array(t1_line, strlen(t1_buf_array) + 1, T1_BUF_SIZE);
1622 strcpy(t1_line_array, t1_buf_array);
1623 t1_line_ptr = eol(t1_line_array);
1624 }
1625 }
1626
1627 static void t1_subset_charstrings(void)
1628 {
1629 cs_entry *ptr;
1630
1631 /* at this point t1_line_array contains "/CharStrings".
1632 when we hit a case like this:
1633 dup/CharStrings
1634 229 dict dup begin
1635 we read the next line and concatenate to t1_line_array before moving on
1636 */
1637 t1_check_unusual_charstring();
1638
1639 cs_size_pos = strstr(t1_line_array, charstringname)
1640 + strlen(charstringname) - t1_line_array + 1;
1641 /* cs_size_pos points to the number indicating
1642 dict size after "/CharStrings" */
1643 cs_size = t1_scan_num(t1_line_array + cs_size_pos, 0);
1644 cs_ptr = cs_tab = xtalloc(cs_size, cs_entry);
1645 for (ptr = cs_tab; ptr - cs_tab < cs_size; ptr++)
1646 init_cs_entry(ptr);
1647 cs_notdef = NULL;
1648 cs_dict_start = xstrdup(t1_line_array);
1649 t1_getline();
1650 while (t1_cslen) {
1651 store_cs();
1652 t1_getline();
1653 }
1654 cs_dict_end = xstrdup(t1_line_array);
1655 t1_mark_glyphs();
1656 if (subr_tab != NULL) {
1657 if (cs_token_pair == NULL)
1658 pdftex_fail
1659 ("This Type 1 font uses mismatched subroutine begin/end token pairs.");
1660 t1_subr_flush();
1661 }
1662 for (cs_count = 0, ptr = cs_tab; ptr < cs_ptr; ptr++)
1663 if (ptr->used)
1664 cs_count++;
1665 t1_cs_flush();
1666 }
1667
1668 static void t1_subset_end(void)
1669 {
1670 if (t1_synthetic) { /* copy to "dup /FontName get exch definefont pop" */
1671 while (!strstr(t1_line_array, "definefont")) {
1672 t1_getline();
1673 t1_putline();
1674 }
1675 while (!t1_end_eexec())
1676 t1_getline(); /* ignore the rest */
1677 t1_putline(); /* write "mark currentfile closefile" */
1678 } else
1679 while (!t1_end_eexec()) { /* copy to "mark currentfile closefile" */
1680 t1_getline();
1681 t1_putline();
1682 }
1683 t1_stop_eexec();
1684 if (fixedcontent) { /* copy 512 zeros (not needed for PDF) */
1685 while (!t1_cleartomark()) {
1686 t1_getline();
1687 t1_putline();
1688 }
1689 if (!t1_synthetic) /* don't check "{restore}if" for synthetic fonts */
1690 t1_check_end(); /* write "{restore}if" if found */
1691 }
1692 get_length3();
1693 }
1694
1695 void writet1(fd_entry *fd)
1696 {
1697 fd_cur = fd; /* fd_cur is global inside writet1.c */
1698 assert(fd_cur->fm != NULL);
1699 assert(is_type1(fd->fm));
1700 assert(is_included(fd->fm));
1701
1702 t1_save_offset = 0;
1703 if (!is_subsetted(fd_cur->fm)) { /* include entire font */
1704 if (!(fd->ff_found = t1_open_fontfile("<<")))
1705 return;
1706 t1_include();
1707 t1_close_font_file(">>");
1708 return;
1709 }
1710 /* partial downloading */
1711 if (!(fd->ff_found = t1_open_fontfile("<")))
1712 return;
1713 t1_subset_ascii_part();
1714 t1_start_eexec();
1715 cc_init();
1716 cs_init();
1717 t1_read_subrs();
1718 t1_subset_charstrings();
1719 t1_subset_end();
1720 t1_close_font_file(">");
1721 }
1722
1723 void t1_free(void)
1724 {
1725 xfree(t1_line_array);
1726 xfree(t1_buf_array);
1727 }