Debian Codegraph @

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