Debian Codegraph @

texk/web2c/luatexdir/font/writet1.c

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