Debian Codegraph @

texk/xdvipsk/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 #ifndef XDVIPSK
18 #include "dvips.h"
19 #else
20 #include "xdvips.h" /* The copyright notice in that file is included too! */
21 #include "lua.h"
22 #include "lauxlib.h"
23 #include "lualib.h"
24 #include "glyphmap.h"
25 #include "mem.h"
26 #include "unicode.h"
27 #endif /* XDVIPSK */
28 /*
29 * The external declarations:
30 */
31 #include "protos.h"
32 #include "ptexmac.h"
33 #undef fm_extend
34 #define fm_extend(f) 0
35 #undef fm_slant
36 #define fm_slant(f) 0
37 #undef is_reencoded
38 #define is_reencoded(f) (cur_enc_name != NULL)
39 #undef is_subsetted
40 #define is_subsetted(f) true
41 #undef is_included
42 #define is_included(f) true
43 #undef set_cur_file_name
44 #define set_cur_file_name(s) cur_file_name = s
45 #define external_enc() ext_glyph_names
46 #define full_file_name() cur_file_name
47 #define is_used_char(c) (grid[c] == 1)
48 #define end_last_eexec_line() \
49 hexline_length = HEXLINE_WIDTH; \
50 end_hexline(); \
51 t1_eexec_encrypt = false
52 #define t1_scan_only()
53 #ifndef XDVIPSK
54 #define t1_scan_keys()
55 #endif /* XDVIPSK */
56 #define embed_all_glyphs(tex_font) false
57 #undef pdfmovechars
58 #ifdef SHIFTLOWCHARS
59 #define pdfmovechars shiftlowchars
60 #define t1_char(c) T1Char(c)
61 #else /* SHIFTLOWCHARS */
62 #define t1_char(c) c
63 #define pdfmovechars 0
64 #endif /* SHIFTLOWCHARS */
65 #define extra_charset() dvips_extra_charset
66 #define make_subset_tag(a, b)
67 #define update_subset_tag()
68
69 static char *dvips_extra_charset;
70 static char *cur_file_name;
71 static char *cur_enc_name;
72 static unsigned char *grid;
73 static char *ext_glyph_names[256];
74 static char print_buf[PRINTF_BUF_SIZE];
75 static int hexline_length;
76 static char notdef[] = ".notdef";
77 static size_t last_ptr_index;
78
79 #include <stdarg.h>
80
81 #define t1_log(str)
82 #define get_length1()
83 #define get_length2()
84 #define get_length3()
85 #define save_offset()
86
87 #define t1_open() \
88 ((t1_file = search(type1path, cur_file_name, FOPEN_RBIN_MODE)) != NULL)
89 #define t1_close() xfclose(t1_file, cur_file_name)
90 #define t1_getchar() getc(t1_file)
91 #ifdef XDVIPSK
92 #define t1_putchar(c) { char ch = c; if (!read_encoding_only) fputc(ch, bitfile); }
93 #else
94 #define t1_putchar(c) fputc(c, bitfile)
95 #endif /* XDVIPSK */
96 #define t1_ungetchar(c) ungetc(c, t1_file)
97 #define t1_eof() feof(t1_file)
98
99 #define str_prefix(s1, s2) (strncmp(s1, s2, strlen(s2)) == 0)
100 #define t1_prefix(s) str_prefix(t1_line_array, s)
101 #define t1_buf_prefix(s) str_prefix(t1_buf_array, s)
102 #define t1_suffix(s) str_suffix(t1_line_array, t1_line_ptr, s)
103 #define t1_buf_suffix(s) str_suffix(t1_buf_array, t1_buf_ptr, s)
104 #define t1_charstrings() strstr(t1_line_array, charstringname)
105 #define t1_subrs() t1_prefix("/Subrs")
106 #define t1_end_eexec() t1_suffix("mark currentfile closefile")
107 #define t1_cleartomark() t1_prefix("cleartomark")
108
109 #define enc_open() \
110 ((enc_file = search(encpath, cur_file_name, FOPEN_RBIN_MODE)) != NULL)
111 #define enc_close() xfclose(enc_file, cur_file_name)
112 #define enc_getchar() getc(enc_file)
113 #define enc_eof() feof(enc_file)
114
115 #define valid_code(c) (c >= 0 && c < 256)
116 #define fixedcontent true /* false for pdfTeX, true for dvips */
117
118 static const char *standard_glyph_names[256] = {
119 /* 0x00 */
120 notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef,
121 notdef, notdef, notdef, notdef, notdef, notdef, notdef,
122 /* 0x10 */
123 notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef,
124 notdef, notdef, notdef, notdef, notdef, notdef, notdef,
125 /* 0x20 */
126 "space", "exclam", "quotedbl", "numbersign", "dollar", "percent",
127 "ampersand", "quoteright", "parenleft", "parenright", "asterisk",
128 "plus", "comma", "hyphen", "period", "slash",
129 /* 0x30 */
130 "zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
131 "nine", "colon", "semicolon", "less", "equal", "greater", "question",
132 /* 0x40 */
133 "at", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
134 "O",
135 /* 0x50 */
136 "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "bracketleft",
137 "backslash", "bracketright", "asciicircum", "underscore",
138 /* 0x60 */
139 "quoteleft", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
140 "m", "n", "o",
141 /* 0x70 */
142 "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "braceleft", "bar",
143 "braceright", "asciitilde", notdef,
144 /* 0x80 */
145 notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef,
146 notdef, notdef, notdef, notdef, notdef, notdef, notdef,
147 /* 0x90 */
148 notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef,
149 notdef, notdef, notdef, notdef, notdef, notdef, notdef,
150 /* 0xa0 */
151 notdef, "exclamdown", "cent", "sterling", "fraction", "yen", "florin",
152 "section", "currency", "quotesingle", "quotedblleft", "guillemotleft",
153 "guilsinglleft", "guilsinglright", "fi", "fl",
154 /* 0xb0 */
155 notdef, "endash", "dagger", "daggerdbl", "periodcentered", notdef,
156 "paragraph", "bullet", "quotesinglbase", "quotedblbase",
157 "quotedblright", "guillemotright", "ellipsis", "perthousand", notdef,
158 "questiondown",
159 /* 0xc0 */
160 notdef, "grave", "acute", "circumflex", "tilde", "macron", "breve",
161 "dotaccent", "dieresis", notdef,
162 "ring", "cedilla", notdef, "hungarumlaut", "ogonek", "caron",
163 /* 0xd0 */
164 "emdash", notdef, notdef, notdef, notdef, notdef, notdef, notdef, notdef,
165 notdef, notdef, notdef, notdef, notdef, notdef, notdef,
166 /* 0xe0 */
167 notdef, "AE", notdef, "ordfeminine", notdef, notdef, notdef, notdef,
168 "Lslash", "Oslash", "OE", "ordmasculine", notdef, notdef, notdef,
169 notdef,
170 /* 0xf0 */
171 notdef, "ae", notdef, notdef, notdef, "dotlessi", notdef, notdef, "lslash",
172 "oslash", "oe", "germandbls", notdef, notdef, notdef, notdef
173 };
174
175 char **t1_glyph_names;
176 char *t1_builtin_glyph_names[256];
177 static boolean read_encoding_only;
178
179 static char charstringname[] = "/CharStrings";
180
181 enum { ENC_STANDARD, ENC_BUILTIN } t1_encoding;
182
183 #define T1_BUF_SIZE 0x10
184 #define ENC_BUF_SIZE 0x1000
185 #ifdef XDVIPSK
186 #define FONTINFO_KEY "/FontInfo "
187 static char* g2u_buf = NULL;
188 #endif /* XDVIPSK */
189 #define CS_HSTEM 1
190 #define CS_VSTEM 3
191 #define CS_VMOVETO 4
192 #define CS_RLINETO 5
193 #define CS_HLINETO 6
194 #define CS_VLINETO 7
195 #define CS_RRCURVETO 8
196 #define CS_CLOSEPATH 9
197 #define CS_CALLSUBR 10
198 #define CS_RETURN 11
199 #define CS_ESCAPE 12
200 #define CS_HSBW 13
201 #define CS_ENDCHAR 14
202 #define CS_RMOVETO 21
203 #define CS_HMOVETO 22
204 #define CS_VHCURVETO 30
205 #define CS_HVCURVETO 31
206 #define CS_1BYTE_MAX (CS_HVCURVETO + 1)
207
208 #define CS_DOTSECTION CS_1BYTE_MAX + 0
209 #define CS_VSTEM3 CS_1BYTE_MAX + 1
210 #define CS_HSTEM3 CS_1BYTE_MAX + 2
211 #define CS_SEAC CS_1BYTE_MAX + 6
212 #define CS_SBW CS_1BYTE_MAX + 7
213 #define CS_DIV CS_1BYTE_MAX + 12
214 #define CS_CALLOTHERSUBR CS_1BYTE_MAX + 16
215 #define CS_POP CS_1BYTE_MAX + 17
216 #define CS_SETCURRENTPOINT CS_1BYTE_MAX + 33
217 #define CS_2BYTE_MAX (CS_SETCURRENTPOINT + 1)
218 #define CS_MAX CS_2BYTE_MAX
219
220 typedef unsigned char byte;
221
222 typedef struct {
223 byte nargs; /* number of arguments */
224 boolean bottom; /* take arguments from bottom of stack? */
225 boolean clear; /* clear stack? */
226 boolean valid;
227 } cc_entry; /* CharString Command */
228
229 typedef struct {
230 char *name; /* glyph name (or notdef for Subrs entry) */
231 byte *data;
232 unsigned short len; /* length of the whole string */
233 unsigned short cslen; /* length of the encoded part of the string */
234 boolean used;
235 boolean valid;
236 } cs_entry;
237
238 static unsigned short t1_dr, t1_er;
239 static const unsigned short t1_c1 = 52845, t1_c2 = 22719;
240 static unsigned short t1_cslen;
241 static short t1_lenIV;
242 static char enc_line[ENC_BUF_SIZE];
243
244 /* define t1_line_ptr, t1_line_array & t1_line_limit */
245 typedef char t1_line_entry;
246 define_array(t1_line);
247
248 /* define t1_buf_ptr, t1_buf_array & t1_buf_limit */
249 typedef char t1_buf_entry;
250 define_array(t1_buf);
251 #ifdef XDVIPSK
252 /* additional buffer for copied and renamed glyphs */
253 typedef char t1_add_entry;
254 define_array(t1_add);
255 /* temporary buffer for t1 line manipulations */
256 typedef char t1_wrk_entry;
257 define_array(t1_wrk);
258 #endif
259
260 static int cs_start;
261
262 static cs_entry *cs_tab, *cs_ptr, *cs_notdef;
263 static char *cs_dict_start, *cs_dict_end;
264 static int cs_count, cs_size, cs_size_pos;
265
266 static cs_entry *subr_tab;
267 static char *subr_array_start, *subr_array_end;
268 static int subr_max, subr_size, subr_size_pos;
269
270 /* This list contains the begin/end tokens commonly used in the */
271 /* /Subrs array of a Type 1 font. */
272
273 static const char *cs_token_pairs_list[][2] = {
274 {" RD", "NP"},
275 {" -|", "|"},
276 {" RD", "noaccess put"},
277 {" -|", "noaccess put"},
278 {NULL, NULL}
279 };
280 static const char **cs_token_pair;
281
282 static boolean t1_pfa, t1_cs, t1_scan, t1_eexec_encrypt, t1_synthetic;
283 static int t1_in_eexec; /* 0 before eexec-encrypted, 1 during, 2 after */
284 static long t1_block_length;
285 static int last_hexbyte;
286 static FILE *t1_file;
287 static FILE *enc_file;
288
289 #ifdef XDVIPSK
290 const char* err_file;
291 int err_line;
292
293 void printf_pr(const char* fmt, ...)
294 {
295 #ifdef TRACE_PR /* add __FILE__ and __LINE__ to warnings and errors */
296 char *fmt_cpy = NULL;
297 int ix;
298 #endif
299 const char *err_fname = NULL;
300 va_list args;
301 va_start(args, fmt);
302
303 assert(fmt);
304
305 if (prettycolumn)
306 {
307 fprintf(stderr, "\n");
308 prettycolumn = 0;
309 }
310
311 #ifdef TRACE_PR
312 if (strstr(fmt, "Error") || strstr(fmt, "Warning"))
313 {
314 fmt_cpy = strdup(fmt);
315 assert(fmt_cpy);
316
317 ix = strlen(fmt_cpy);
318 if (ix > 0)
319 {
320 ix--;
321 if (fmt_cpy[ix] == '\n')
322 fmt_cpy[ix] = '\0';
323 }
324
325 fmt = fmt_cpy;
326 }
327 #endif
328
329 #if defined(KPATHSEA) && defined(WIN32)
330 win32_vfprintf(stderr, fmt, args);
331 #else
332 vfprintf(stderr, fmt, args);
333 #endif
334
335 #ifdef TRACE_PR
336 if (fmt_cpy)
337 {
338 free(fmt_cpy);
339 free(extract_fname(err_file, &err_fname));
340 fprintf(stderr, " (%s %d)\n", err_fname, err_line);
341 }
342 #endif
343
344 va_end(args);
345 }
346
347 void load_touni_file(const char *map_fname, boolean overwrite, const char *pfb_name, int verbose)
348 {
349 FILE* map_file = NULL;
350 char* map_buf = NULL;
351 char* map_buf_ptr = NULL;
352 size_t map_buf_len = 0;
353 static char map_line[ENC_BUF_SIZE + 1];
354 char *pfb_name_lwr;
355
356 assert(map_fname);
357 map_file = search_safe(G2U_FORMAT, map_fname, FOPEN_R_MODE, verbose > 0);
358 if (map_file)
359 {
360 init_map_buf(&map_buf, &map_buf_ptr, &map_buf_len);
361 assert(map_buf);
362 append_map_buf(&map_buf, &map_buf_ptr, &map_buf_len,
363 "function get_map()\n"
364 );
365 while (fgets(map_line, ENC_BUF_SIZE, map_file))
366 append_map_buf(&map_buf, &map_buf_ptr, &map_buf_len, map_line);
367 if (ferror(map_file))
368 {
369 PRINTF_PR("Error: File %.500s read error.\n", map_fname);
370 }
371 fclose(map_file);
372 append_map_buf(&map_buf, &map_buf_ptr, &map_buf_len,
373 "end\n"
374 "load_touni(get_map(), "
375 );
376 append_map_buf(&map_buf, &map_buf_ptr, &map_buf_len, overwrite? "true" : "false");
377 append_map_buf(&map_buf, &map_buf_ptr, &map_buf_len, ", \'");
378 if (pfb_name)
379 {
380 pfb_name_lwr = strdup(pfb_name);
381 assert(pfb_name_lwr);
382 strlwr(pfb_name_lwr);
383 append_map_buf(&map_buf, &map_buf_ptr, &map_buf_len, pfb_name_lwr);
384 free(pfb_name_lwr);
385 }
386 append_map_buf(&map_buf, &map_buf_ptr, &map_buf_len, "\')\n");
387 if (luaL_dostring(L, map_buf))
388 {
389 PRINTF_PR("Error: Lua script %.500s execution error: %.500s.\n", map_fname, lua_tostring(L, -1));
390 lua_pop(L, 1);
391 }
392 free(map_buf);
393 }
394 else
395 if (verbose > 1)
396 {
397 PRINTF_PR("Error: File %.500s not found.\n", map_fname);
398 }
399 }
400
401 unsigned int *get_glname_tounicode(const char *glyph_name, const char *font_name, const char *prefix, boolean exact, int *p_ucnt, boolean *p_needs_g2u)
402 {
403 unsigned int *tounicode = NULL;
404 int ii;
405 char full_glyph_name[FULL_GLYPH_NAME_LEN + 1 + 1]; /* strlen("/") */
406 int error;
407
408 assert(glyph_name);
409 assert(p_ucnt);
410
411 full_glyph_name[0] = '\0';
412 if (prefix && font_name)
413 {
414 strcat(full_glyph_name, prefix);
415 strncat(full_glyph_name, font_name, TEX_NAME_LEN);
416 full_glyph_name[TEX_NAME_LEN + 4] = '\0'; /* strlen("pfb:") */
417 strcat(full_glyph_name, "/");
418 strlwr(full_glyph_name);
419 }
420 strncat(full_glyph_name, glyph_name, GLYPH_NAME_LEN);
421 full_glyph_name[FULL_GLYPH_NAME_LEN] = '\0';
422
423 lua_getglobal(L, "get_glname_tounicode");
424 lua_pushstring(L, full_glyph_name);
425 lua_pushboolean(L, exact);
426 error = lua_pcall(L, 2, 2, 0);
427 if (error)
428 {
429 PRINTF_PR("Lua function get_glname_tounicode() execution error: %d - %s\n", error, lua_tostring(L, -1));
430 lua_pop(L, 1);
431 }
432 else
433 {
434 if (p_needs_g2u)
435 *p_needs_g2u = lua_toboolean(L, -1);
436 lua_pop(L, 1);
437
438 lua_len(L, -1);
439 *p_ucnt = lua_tointeger(L, -1);
440 lua_pop(L, 1);
441 if (*p_ucnt)
442 {
443 tounicode = NEW(*p_ucnt, unsigned int);
444 assert(tounicode);
445 for (ii = 0; ii < *p_ucnt; ii++)
446 {
447 lua_pushinteger(L, ii + 1);
448 lua_gettable(L, -2);
449 tounicode[ii] = lua_tointeger(L, -1);
450 lua_pop(L, 1);
451 }
452 }
453 lua_pop(L, 1);
454 }
455
456 return (tounicode);
457 }
458
459 char *get_glname_substitute(const char *glyph_name, const char *font_name, const char *prefix, boolean exact, boolean *p_needs_g2u)
460 {
461 char full_glyph_name[FULL_GLYPH_NAME_LEN + 1 + 1]; /* strlen("/") */
462 const char *glyph_subst = NULL;
463 char *ret_str = NULL;
464 int error;
465
466 assert(glyph_name);
467
468 full_glyph_name[0] = '\0';
469 if (font_name && prefix)
470 {
471 strcat(full_glyph_name, prefix);
472 strncat(full_glyph_name, font_name, TEX_NAME_LEN);
473 full_glyph_name[TEX_NAME_LEN + 4] = '\0'; /* strlen("pfb:") */
474 strcat(full_glyph_name, "/");
475 strlwr(full_glyph_name);
476 }
477 strncat(full_glyph_name, glyph_name, GLYPH_NAME_LEN);
478 full_glyph_name[FULL_GLYPH_NAME_LEN] = '\0';
479
480 lua_getglobal(L, "get_glname_subst");
481 lua_pushstring(L, full_glyph_name);
482 lua_pushboolean(L, exact);
483 error = lua_pcall(L, 2, 2, 0);
484 if (error)
485 {
486 PRINTF_PR("Lua function get_glname_subst() execution error: %d - %s\n", error, lua_tostring(L, -1));
487 glyph_subst = NULL;
488 lua_pop(L, 1);
489 }
490 else
491 {
492 if (p_needs_g2u)
493 *p_needs_g2u = lua_toboolean(L, -1);
494 lua_pop(L, 1);
495
496 glyph_subst = lua_tostring(L, -1);
497 if (glyph_subst && (glyph_subst[0] == '\0'))
498 glyph_subst = NULL;
499 if (glyph_subst)
500 {
501 ret_str = strdup(glyph_subst);
502 assert(ret_str);
503 }
504 lua_pop(L, 1);
505 }
506 return (ret_str);
507 }
508
509 char *extract_fname(const char *fpath, const char **fname_ptr_p)
510 {
511 char *font_name = NULL;
512 char* path_ptr;
513 const char *fname_ptr;
514
515 assert(fpath);
516
517 fname_ptr = fpath;
518 while (TRUE)
519 {
520 path_ptr = strchr(fname_ptr, DIR_SEP);
521 if (!path_ptr)
522 path_ptr = strchr(fname_ptr, '/');
523 if (!path_ptr)
524 path_ptr = strchr(fname_ptr, '\\');
525 if (path_ptr)
526 fname_ptr = path_ptr + 1;
527 else
528 break;
529 }
530 if (fname_ptr_p)
531 *fname_ptr_p = fname_ptr;
532
533 font_name = strdup(fname_ptr);
534 assert(font_name);
535 path_ptr = strchr(font_name, '.');
536 if (path_ptr)
537 *path_ptr = '\0';
538
539 return(font_name);
540 }
541
542 static void redir_glyphtounicode_tfm_2_pfb(void)
543 {
544 fontmaptype *cfnt;
545 char *pfb_name = NULL;
546 char *tfm_name = NULL;
547 char *font_file = NULL;
548 int error;
549
550 for (cfnt = ffont; cfnt; cfnt = cfnt->next)
551 if (cfnt->desc && cfnt->desc->resfont && ((font_file = cfnt->desc->resfont->Fontfile) != NULL) && ((tfm_name = cfnt->desc->resfont->TeXname) != NULL) && strstr(font_file, PFB_EXT))
552 {
553 tfm_name = strdup(tfm_name);
554 assert(tfm_name);
555 strlwr(tfm_name);
556
557 pfb_name = extract_fname(font_file, NULL);
558 assert(pfb_name);
559 strlwr(pfb_name);
560
561 lua_getglobal(L, "add_tfm_2_pfb");
562 lua_pushstring(L, tfm_name);
563 lua_pushstring(L, pfb_name);
564 error = lua_pcall(L, 2, 0, 0);
565 if (error)
566 {
567 PRINTF_PR("Lua function add_tfm_2_pfb() execution error: %d - %s\n", error, lua_tostring(L, -1));
568 lua_pop(L, 1);
569 }
570
571 free(tfm_name);
572 free(pfb_name);
573 }
574
575 lua_getglobal(L, "encode_tfm_2_pfb");
576 error = lua_pcall(L, 0, 0, 0);
577 if (error)
578 {
579 PRINTF_PR("Lua function encode_tfm_2_pfb() execution error: %d - %s\n", error, lua_tostring(L, -1));
580 lua_pop(L, 1);
581 }
582 }
583
584 /* checks, whether there was already an attempt to load glyph unicode map of the font specified
585 pfb_name -- font file name without an extension */
586 static boolean is_g2u_loaded(const char *pfb_name)
587 {
588 boolean ret_val = FALSE;
589 int error;
590 char *pfb_name_lwr;
591
592 assert(pfb_name);
593 pfb_name_lwr = strdup(pfb_name);
594 assert(pfb_name_lwr);
595 strlwr(pfb_name_lwr);
596
597 lua_getglobal(L, "is_g2u_loaded");
598 lua_pushstring(L, pfb_name_lwr);
599 error = lua_pcall(L, 1, 1, 0);
600 if (error)
601 {
602 PRINTF_PR("Lua function is_g2u_loaded() execution error: %d - %s\n", error, lua_tostring(L, -1));
603 }
604 else
605 ret_val = lua_toboolean(L, -1);
606 lua_pop(L, 1);
607
608 free(pfb_name_lwr);
609
610 return(ret_val);
611 }
612 #endif /* XDVIPSK */
613 static void pdftex_fail(const char *fmt, ...)
614 {
615 va_list args;
616 va_start(args, fmt);
617 #ifdef XDVIPSK
618 char buf[1024];
619
620 PRINTF_PR("%s", "Error: module writet1");
621 #else
622 fputs("\nError: module writet1", stderr);
623 #endif /* XDVIPSK */
624 if (cur_file_name)
625 fprintf_str(stderr, " (file %s)", cur_file_name);
626 fputs(": ", stderr);
627 vsprintf(print_buf, fmt, args);
628 fputs_str(print_buf, stderr);
629 fputs("\n ==> Fatal error occurred, the output PS file is not finished!\n", stderr);
630 va_end(args);
631 #ifndef XDVIPSK
632 exit(-1);
633 #else
634 sprintf(buf, "! Module writet1 (file %s) %s", cur_file_name, print_buf);
635 writelogrecord(buf);
636 dvips_exit(-1);
637 #endif /* XDVIPSK */
638 }
639
640 static void pdftex_warn(const char *fmt, ...)
641 {
642 va_list args;
643 va_start(args, fmt);
644 #ifdef XDVIPSK
645 char buf[1024];
646
647 PRINTF_PR("%s", "Warning: module writet1 of xdvips");
648 #else
649 fputs("\nWarning: module writet1 of dvips", stderr);
650 #endif /* XDVIPSK */
651 if (cur_file_name)
652 fprintf_str(stderr, " (file %s)", cur_file_name);
653 fputs(": ", stderr);
654 vsprintf(print_buf, fmt, args);
655 fputs_str(print_buf, stderr);
656 fputs("\n", stderr);
657 va_end(args);
658 #ifdef XDVIPSK
659 sprintf(buf, "Module writet1 of xdvips (file %s) %s", cur_file_name, print_buf);
660 writelogrecord(buf);
661 #endif /* XDVIPSK */
662 }
663
664 #define HEXLINE_WIDTH 64
665
666 static void end_hexline(void)
667 {
668 if (hexline_length == HEXLINE_WIDTH) {
669 #ifdef XDVIPSK
670 if (!read_encoding_only)
671 #endif /* XDVIPSK */
672 fputs("\n", bitfile);
673 hexline_length = 0;
674 }
675 }
676
677 static void t1_outhex(byte b)
678 {
679 static const char *hexdigits = "0123456789ABCDEF";
680 t1_putchar(hexdigits[b/16]);
681 t1_putchar(hexdigits[b%16]);
682 hexline_length += 2;
683 end_hexline();
684 }
685
686
687 static void enc_getline(void)
688 {
689 char *p;
690 int c;
691 restart:
692 if (enc_eof())
693 pdftex_fail("unexpected end of file");
694 p = enc_line;
695 do {
696 c = enc_getchar();
697 append_char_to_buf(c, p, enc_line, ENC_BUF_SIZE);
698 } while (c != 10);
699 append_eol(p, enc_line, ENC_BUF_SIZE);
700 if (p - enc_line < 2 || *enc_line == '%')
701 goto restart;
702 }
703
704 /* read encoding from .enc file, return glyph_names array, or pdffail() */
705
706 char **load_enc_file(char *enc_name)
707 {
708 char buf[ENC_BUF_SIZE], *p, *r;
709 int i, names_count;
710 char **glyph_names;
711 #ifdef XDVIPSK
712 enc_name = get_alias_fname(enc_name);
713 #endif /* XDVIPSK */
714 set_cur_file_name(enc_name);
715 glyph_names = (char **) mymalloc(256 * sizeof(char *));
716 for (i = 0; i < 256; i++)
717 glyph_names[i] = notdef;
718 if (!enc_open()) {
719 pdftex_warn("cannot open encoding file for reading");
720 cur_file_name = NULL;
721 return glyph_names;
722 }
723 t1_log("{");
724 t1_log(cur_file_name = full_file_name());
725 enc_getline();
726 if (*enc_line != '/' || (r = strchr(enc_line, '[')) == NULL) {
727 remove_eol(r, enc_line);
728 pdftex_fail
729 ("invalid encoding vector (a name or `[' missing): `%s'", enc_line);
730 }
731 names_count = 0;
732 r++; /* skip '[' */
733 skip(r, ' ');
734 for (;;) {
735 while (*r == '/') {
736 for (p = buf, r++;
737 *r != ' ' && *r != 10 && *r != ']' && *r != '/'; *p++ = *r++);
738 *p = 0;
739 skip(r, ' ');
740 if (names_count > 255)
741 pdftex_fail("encoding vector contains more than 256 names");
742 if (strcmp(buf, notdef) != 0)
743 glyph_names[names_count] = xstrdup(buf);
744 names_count++;
745 }
746 if (*r != 10 && *r != '%') {
747 if (str_prefix(r, "] def"))
748 goto done;
749 else {
750 remove_eol(r, enc_line);
751 pdftex_fail
752 ("invalid encoding vector: a name or `] def' expected: `%s'", enc_line);
753 }
754 }
755 enc_getline();
756 r = enc_line;
757 }
758 done:
759 enc_close();
760 t1_log("}");
761 cur_file_name = NULL;
762 return glyph_names;
763 }
764
765 static void t1_check_pfa(void)
766 {
767 const int c = t1_getchar();
768 t1_pfa = (c != 128) ? true : false;
769 t1_ungetchar(c);
770 }
771
772 static int t1_getbyte(void)
773 {
774 int c = t1_getchar();
775 if (t1_pfa)
776 return c;
777 if (t1_block_length == 0) {
778 if (c != 128)
779 pdftex_fail("invalid marker");
780 c = t1_getchar();
781 if (c == 3) {
782 while (!t1_eof())
783 t1_getchar();
784 return EOF;
785 }
786 t1_block_length = t1_getchar() & 0xff;
787 t1_block_length |= (t1_getchar() & 0xff) << 8;
788 t1_block_length |= (t1_getchar() & 0xff) << 16;
789 t1_block_length |= (t1_getchar() & 0xff) << 24;
790 c = t1_getchar();
791 }
792 t1_block_length--;
793 return c;
794 }
795
796 static int hexval(int c)
797 {
798 if (c >= 'A' && c <= 'F')
799 return c - 'A' + 10;
800 else if (c >= 'a' && c <= 'f')
801 return c - 'a' + 10;
802 else if (c >= '0' && c <= '9')
803 return c - '0';
804 else
805 return -1;
806 }
807
808 static byte edecrypt(byte cipher)
809 {
810 byte plain;
811 if (t1_pfa) {
812 while (cipher == 10 || cipher == 13)
813 cipher = t1_getbyte();
814 last_hexbyte = cipher = (hexval(cipher) << 4) + hexval(t1_getbyte());
815 }
816 plain = (cipher ^ (t1_dr >> 8));
817 t1_dr = (cipher + t1_dr) * t1_c1 + t1_c2;
818 return plain;
819 }
820
821 static byte cdecrypt(byte cipher, unsigned short *cr)
822 {
823 const byte plain = (cipher ^ (*cr >> 8));
824 *cr = (cipher + *cr) * t1_c1 + t1_c2;
825 return plain;
826 }
827
828 static byte eencrypt(byte plain)
829 {
830 const byte cipher = (plain ^ (t1_er >> 8));
831 t1_er = (cipher + t1_er) * t1_c1 + t1_c2;
832 return cipher;
833 }
834
835 static byte cencrypt(byte plain, unsigned short *cr)
836 {
837 const byte cipher = (plain ^ (*cr >> 8));
838 *cr = (cipher + *cr) * t1_c1 + t1_c2;
839 return cipher;
840 }
841
842 static char *eol(char *s)
843 {
844 char *p = strend(s);
845 if (p - s > 1 && p[-1] != 10) {
846 *p++ = 10;
847 *p = 0;
848 }
849 return p;
850 }
851
852 static float t1_scan_num(char *p, char **r)
853 {
854 float f;
855 skip(p, ' ');
856 if (sscanf(p, "%g", &f) != 1) {
857 remove_eol(p, t1_line_array);
858 pdftex_fail("a number expected: `%s'", t1_line_array);
859 }
860 if (r != NULL) {
861 for (; isdigit((unsigned char)*p) || *p == '.' ||
862 *p == 'e' || *p == 'E' || *p == '+' || *p == '-'; p++);
863 *r = p;
864 }
865 return f;
866 }
867
868 static boolean str_suffix(const char *begin_buf, const char *end_buf,
869 const char *s)
870 {
871 const char *s1 = end_buf - 1, *s2 = strend(s) - 1;
872 if (*s1 == 10)
873 s1--;
874 while (s1 >= begin_buf && s2 >= s) {
875 if (*s1-- != *s2--)
876 return false;
877 }
878 return s2 < s;
879 }
880
881 static void t1_getline(void)
882 {
883 int c, l, eexec_scan;
884 char *p;
885 static const char eexec_str[] = "currentfile eexec";
886 static int eexec_len = 17; /* strlen(eexec_str) */
887 restart:
888 if (t1_eof())
889 pdftex_fail("unexpected end of file");
890 t1_line_ptr = t1_line_array;
891 alloc_array(t1_line, 1, T1_BUF_SIZE);
892 t1_cslen = 0;
893 eexec_scan = 0;
894 c = t1_getbyte();
895 if (c == EOF)
896 goto exit;
897 while (!t1_eof()) {
898 if (t1_in_eexec == 1)
899 c = edecrypt((byte)c);
900 alloc_array(t1_line, 1, T1_BUF_SIZE);
901 append_char_to_buf(c, t1_line_ptr, t1_line_array, t1_line_limit);
902 if (t1_in_eexec == 0 && eexec_scan >= 0 && eexec_scan < eexec_len) {
903 if (t1_line_array[eexec_scan] == eexec_str[eexec_scan])
904 eexec_scan++;
905 else
906 eexec_scan = -1;
907 }
908 if (c == 10 || (t1_pfa && eexec_scan == eexec_len && c == 32))
909 break;
910 if (t1_cs && t1_cslen == 0 && (t1_line_ptr - t1_line_array > 4) &&
911 (t1_suffix(" RD ") || t1_suffix(" -| "))) {
912 p = t1_line_ptr - 5;
913 while (*p != ' ')
914 p--;
915 t1_cslen = l = t1_scan_num(p + 1, 0);
916 cs_start = t1_line_ptr - t1_line_array; /* cs_start is an index now */
917 alloc_array(t1_line, l, T1_BUF_SIZE);
918 while (l-- > 0)
919 *t1_line_ptr++ = edecrypt((byte)t1_getbyte());
920 }
921 c = t1_getbyte();
922 }
923 alloc_array(t1_line, 2, T1_BUF_SIZE); /* append_eol can append 2 chars */
924 append_eol(t1_line_ptr, t1_line_array, t1_line_limit);
925 if (t1_line_ptr - t1_line_array < 2)
926 goto restart;
927 if (eexec_scan == eexec_len)
928 t1_in_eexec = 1;
929 exit:
930 /* ensure that t1_buf_array has as much room as t1_line_array */
931 t1_buf_ptr = t1_buf_array;
932 alloc_array(t1_buf, t1_line_limit, t1_line_limit);
933 }
934
935 #ifdef XDVIPSK
936 #ifdef GLYPH_CVT_TO_UNI
937 char *make_uni_glyph_name(unsigned int *tounicode, int ucnt)
938 {
939 char *uni_glyph_subst = NULL;
940 unsigned long *uni32_arr = NULL;
941 int uni32_cnt;
942
943 assert(tounicode)
944
945 uni32_arr = utf16_2_uni32(tounicode, ucnt, &uni32_cnt);
946 if (uni32_arr)
947 {
948 if (uni32_cnt == 1)
949 {
950 uni_glyph_subst = malloc(FULL_GLYPH_NAME_LEN + 1);
951 assert(uni_glyph_subst);
952 strcpy(uni_glyph_subst, "u");
953 sprintf(uni_glyph_subst + 1, "%04X", uni32_arr[0]);
954 }
955 RELEASE(uni32_arr);
956 uni32_arr = NULL;
957 }
958
959 return(uni_glyph_subst);
960 }
961 #endif
962 char *glyph_name_cvt(const char *glyph_name, const char *font_name, boolean *p_needs_g2u)
963 {
964 char *glyph_subst = NULL;
965 #ifdef GLYPH_CVT_TO_UNI
966 unsigned int *tounicode = NULL;
967 int ucnt;
968 #endif
969
970 if (p_needs_g2u)
971 *p_needs_g2u = TRUE;
972
973 glyph_subst = get_glname_substitute(glyph_name, font_name, "pfb:", FALSE, p_needs_g2u);
974 #ifdef GLYPH_CVT_TO_UNI
975 if (!glyph_subst)
976 {
977 tounicode = get_glname_tounicode(glyph_name, font_name, "pfb:", TRUE, 0, &ucnt, NULL);
978 if (tounicode)
979 {
980 glyph_subst = make_uni_glyph_name(tounicode, ucnt);
981 if (p_needs_g2u)
982 *p_needs_g2u = FALSE;
983
984 RELEASE(tounicode);
985 }
986 }
987 #endif
988 return (glyph_subst);
989 }
990
991 static void rename_t1_glyph_name(const char *glyph_name, const char *glyph_subst, char **p_t1_array, char **p_t1_ptr, size_t *p_t1_limit, char *gl_pt)
992 {
993 char *from_pt, *to_pt, *limit_pt;
994 int shift;
995 size_t last_gl_pt_index;
996
997 assert(p_t1_array);
998 assert(p_t1_ptr);
999 assert(p_t1_limit);
1000
1001 shift = strlen(glyph_subst) - strlen(glyph_name);
1002 if (*p_t1_ptr - *p_t1_array + shift + 2 > *p_t1_limit)
1003 {
1004 last_gl_pt_index = gl_pt - *p_t1_array;
1005 if (*p_t1_array == t1_line_array)
1006 {
1007 alloc_array(t1_line, shift + 2, T1_BUF_SIZE);
1008 }
1009 else
1010 {
1011 alloc_array(t1_add, shift + 2, T1_BUF_SIZE);
1012 }
1013 assert(*p_t1_array);
1014 gl_pt = *p_t1_array + last_gl_pt_index;
1015 }
1016 if (*p_t1_ptr - *p_t1_array + shift + 2 <= *p_t1_limit)
1017 {
1018 if (shift > 0)
1019 {
1020 from_pt = *p_t1_ptr + 2;
1021 to_pt = from_pt + shift;
1022 limit_pt = gl_pt + strlen(glyph_name);
1023 while (from_pt > limit_pt)
1024 *(--to_pt) = *(--from_pt);
1025 }
1026 else if (shift < 0)
1027 {
1028 from_pt = gl_pt + strlen(glyph_name);
1029 to_pt = from_pt + shift;
1030 while (from_pt <= *p_t1_ptr)
1031 *to_pt++ = *from_pt++;
1032 }
1033 *p_t1_ptr += shift;
1034 memcpy(gl_pt, glyph_subst, strlen(glyph_subst));
1035 }
1036 else
1037 {
1038 PRINTF_PR("Error: Type1 line buffer overflow, file %s: %s\n", cur_file_name, glyph_name);
1039 }
1040 }
1041 #endif /* XDVIPSK */
1042 static void t1_putline(void)
1043 {
1044 char *p = t1_line_array;
1045 #ifdef XDVIPSK
1046 char *from_pt, *to_pt, *glyph_subst, *pfb_name;
1047 char glyph_name[FULL_GLYPH_NAME_LEN + 1];
1048 const char *font_file = NULL;
1049 char *prev_t1_add_ptr;
1050 int ll;
1051
1052 ll = t1_line_ptr - t1_line_array;
1053 t1_wrk_ptr = t1_wrk_array;
1054 alloc_array(t1_wrk, ll + 1, T1_BUF_SIZE);
1055 assert(t1_wrk_array);
1056 memcpy(t1_wrk_array, t1_line_array, ll);
1057 t1_wrk_array[ll] = '\0';
1058
1059 /* /ampersand 208 -| ... */
1060 t1_wrk_ptr = strstr(t1_wrk_array, " -| ");
1061 if (t1_wrk_ptr)
1062 *t1_wrk_ptr = '\0';
1063 /* /minus 30 RD ... */
1064 t1_wrk_ptr = strstr(t1_wrk_array, " RD ");
1065 if (t1_wrk_ptr)
1066 *t1_wrk_ptr = '\0';
1067
1068 t1_wrk_ptr = strchr(t1_wrk_array, '/');
1069 if (t1_wrk_ptr)
1070 {
1071 if (curfnt && curfnt->resfont && ((font_file = curfnt->resfont->Fontfile) != NULL) && strstr(font_file, PFB_EXT))
1072 {
1073 pfb_name = extract_fname(font_file, NULL);
1074 assert(pfb_name);
1075 t1_wrk_ptr++;
1076 from_pt = t1_wrk_ptr;
1077 to_pt = glyph_name;
1078 while (*from_pt && (*from_pt != ' ') && (to_pt < glyph_name + FULL_GLYPH_NAME_LEN))
1079 *to_pt++ = *from_pt++;
1080 *to_pt = '\0';
1081 glyph_subst = glyph_name_cvt(glyph_name, pfb_name, NULL);
1082 if (glyph_subst)
1083 {
1084 if (strncmp(t1_line_array, "dup ", 4) == 0) /* strlen("dup ") */
1085 {
1086 /* Encodings are renamed directly to the file, for example, in the file t1xttsc.pfb (t1xttsc.pfa)
1087 dup 102 /f put
1088 renamed to
1089 dup 102 /uA730 put */
1090 rename_t1_glyph_name(glyph_name, glyph_subst, &t1_line_array, &t1_line_ptr, &t1_line_limit, t1_line_array + (t1_wrk_ptr - t1_wrk_array));
1091 }
1092 else
1093 {
1094 /* Glyph definition records like
1095 /fi 17 RD <CharString encoded binary bytes> ND
1096 (t1xttsc.pfa, for example) should be preserved with their intact names for
1097 possible references through the PostScript glyph definition command seac,
1098 for example, like in the t1xttsc.pfb (decoded t1xttsc.pfa)
1099 /fi {
1100 66 1050 hsbw
1101 89 548 0 102 105 seac
1102 endchar
1103 } ND
1104 102 and 105 are indices of /f and /i respectively.
1105 Renamed glyph definitions are collected into the t1_add_array for the postponed
1106 output at the end of the glyph definition section. */
1107 alloc_array(t1_add, t1_line_ptr - t1_line_array, T1_BUF_SIZE);
1108 assert(t1_add_array);
1109 memcpy(t1_add_ptr, t1_line_array, t1_line_ptr - t1_line_array);
1110 prev_t1_add_ptr = t1_add_ptr;
1111 t1_add_ptr += t1_line_ptr - t1_line_array;
1112 rename_t1_glyph_name(glyph_name, glyph_subst, &t1_add_array, &t1_add_ptr, &t1_add_limit, prev_t1_add_ptr + (t1_wrk_ptr - t1_wrk_array));
1113 }
1114 free(glyph_subst);
1115 }
1116 free(pfb_name);
1117 }
1118 }
1119
1120 #endif /* XDVIPSK */
1121 if (t1_line_ptr - t1_line_array <= 1)
1122 return;
1123 #ifdef XDVIPSK
1124 p = t1_line_array; /* t1_line_array could be reallocated in rename_t1_line_glyph_name() */
1125 if ((strncmp(p, "%!", 2) == 0) || (strncmp(p, "%%", 2) == 0))
1126 return;
1127 #endif /* XDVIPSK */
1128 if (t1_eexec_encrypt) {
1129 while (p < t1_line_ptr)
1130 t1_outhex(eencrypt(*p++)); /* dvips outputs hex, unlike pdftex */
1131 } else
1132 while (p < t1_line_ptr)
1133 t1_putchar(*p++);
1134 }
1135
1136 static void t1_puts(const char *s)
1137 {
1138 if (s != t1_line_array)
1139 strcpy(t1_line_array, s);
1140 t1_line_ptr = strend(t1_line_array);
1141 t1_putline();
1142 }
1143
1144 static void t1_printf(const char *fmt, ...)
1145 {
1146 va_list args;
1147 va_start(args, fmt);
1148 vsprintf(t1_line_array, fmt, args);
1149 t1_puts(t1_line_array);
1150 va_end(args);
1151 }
1152
1153 static void t1_init_params(const char *open_name_prefix)
1154 {
1155 t1_log(open_name_prefix);
1156 t1_log(cur_file_name);
1157 t1_lenIV = 4;
1158 t1_dr = 55665;
1159 t1_er = 55665;
1160 t1_in_eexec = 0;
1161 t1_cs = false;
1162 t1_scan = true;
1163 t1_synthetic = false;
1164 t1_eexec_encrypt = false;
1165 t1_block_length = 0;
1166 t1_check_pfa();
1167 }
1168
1169 static void t1_close_font_file(const char *close_name_suffix)
1170 {
1171 t1_log(close_name_suffix);
1172 t1_close();
1173 #ifndef XDVIPSK
1174 cur_file_name = NULL;
1175 #endif /* XDVIPSK */
1176 }
1177
1178 static void t1_check_block_len(boolean decrypt)
1179 {
1180 int l, c;
1181 if (t1_block_length == 0)
1182 return;
1183 c = t1_getbyte();
1184 if (decrypt)
1185 c = edecrypt((byte)c);
1186 l = t1_block_length;
1187 if (!(l == 0 && (c == 10 || c == 13))) {
1188 pdftex_warn("%i bytes more than expected were ignored", l + 1);
1189 while (l-- > 0)
1190 t1_getbyte();
1191 }
1192 }
1193
1194 static void t1_start_eexec(void)
1195 {
1196 int i;
1197 if (is_included(fm_cur)) {
1198 get_length1();
1199 save_offset();
1200 }
1201 if (!t1_pfa)
1202 t1_check_block_len(false);
1203 for (t1_line_ptr = t1_line_array, i = 0; i < 4; i++) {
1204 edecrypt((byte)t1_getbyte());
1205 *t1_line_ptr++ = 0;
1206 }
1207 t1_eexec_encrypt = true;
1208 if (is_included(fm_cur))
1209 t1_putline(); /* to put the first four bytes */
1210 }
1211
1212 static void t1_stop_eexec(void)
1213 {
1214 int c;
1215 if (is_included(fm_cur)) {
1216 get_length2();
1217 save_offset();
1218 }
1219 end_last_eexec_line();
1220 if (!t1_pfa)
1221 t1_check_block_len(true);
1222 else {
1223 c = edecrypt((byte)t1_getbyte());
1224 if (!(c == 10 || c == 13)) {
1225 if (last_hexbyte == 0)
1226 t1_puts("00");
1227 else
1228 pdftex_warn("unexpected data after eexec");
1229 }
1230 }
1231 t1_cs = false;
1232 t1_in_eexec = 2;
1233 }
1234
1235 #ifdef XDVIPSK
1236 FILE *search_safe(kpse_file_format_type format, const char *file_name, const char *mode, boolean verbose)
1237 {
1238 char *realnameoffile_cpy = NULL;
1239 FILE *ret_file = NULL;
1240
1241 if (realnameoffile)
1242 {
1243 realnameoffile_cpy = strdup(realnameoffile);
1244 assert(realnameoffile_cpy);
1245 }
1246 ret_file = search(format, file_name, mode);
1247 if (ret_file && realnameoffile && verbose)
1248 {
1249 if (strlen(realnameoffile) + prettycolumn > STDOUTSIZE) {
1250 fprintf(stderr, "\n");
1251 prettycolumn = 0;
1252 }
1253 fprintf_str(stderr, "<%s>", realnameoffile);
1254 prettycolumn += strlen(realnameoffile) + 2;
1255 }
1256 if (realnameoffile_cpy)
1257 {
1258 if (realnameoffile)
1259 free(realnameoffile);
1260 realnameoffile = realnameoffile_cpy;
1261 }
1262 return(ret_file);
1263 }
1264
1265 void parse_g2u(const char *pfb_fname)
1266 {
1267 char *map_fname = NULL;
1268 char *extptr;
1269 FILE *map_file = NULL;
1270 static char map_fline[ENC_BUF_SIZE + 2]; /* extra byte for double zero terminators */
1271 static char map_line[ENC_BUF_SIZE + 1];
1272 char *tok_ptr, *val_ptr, *tok_end;
1273 char *val_cpy = NULL;
1274 char hex_val[8 + 1];
1275 int ii;
1276 unsigned long ucode;
1277
1278 assert(pfb_fname);
1279 map_fname = strdup(pfb_fname);
1280 assert(map_fname);
1281 extptr = strstr(map_fname, PFB_EXT);
1282
1283 if (extptr)
1284 {
1285 *extptr = '\0';
1286 if (!is_g2u_loaded(map_fname))
1287 {
1288 /* loading .lua table for unicode encoding */
1289 char *pfb_name = strdup(map_fname);
1290 assert(pfb_name);
1291 assert(strlen(LUA_EXT) <= strlen(PFB_EXT));
1292 strcpy(extptr, LUA_EXT);
1293 load_touni_file(map_fname, FALSE, pfb_name, 1);
1294 free(pfb_name);
1295
1296 /* parsing .g2u file */
1297 assert(strlen(G2U_EXT) <= strlen(PFB_EXT));
1298 strcpy(extptr, G2U_EXT);
1299 map_file = search_safe(G2U_FORMAT, map_fname, FOPEN_R_MODE, TRUE);
1300 *extptr = '\0';
1301 if (map_file)
1302 {
1303 while (fgets(map_fline, ENC_BUF_SIZE, map_file))
1304 {
1305 /* doubling the terminating zero for the last token */
1306 map_fline[strlen(map_fline) + 1] = '\0';
1307 tok_ptr = strchr(map_fline, '/');
1308 if (tok_ptr)
1309 {
1310 tok_ptr++;
1311 if (strstr(tok_ptr, "GlyphNames2Unicode") != tok_ptr)
1312 {
1313 tok_ptr = strtok(tok_ptr, " ");
1314 val_ptr = tok_ptr + strlen(tok_ptr) + 1;
1315 val_cpy = strdup(val_ptr);
1316 assert(val_cpy);
1317
1318 val_ptr = val_cpy;
1319 strcpy(map_line, "load_touni({['");
1320
1321 strcat(map_line, "pfb:");
1322 ii = strlen(map_line);
1323 tok_end = map_line + ii;
1324 strncpy(tok_end, map_fname, ENC_BUF_SIZE - ii - 20);
1325 map_line[ENC_BUF_SIZE] = '\0';
1326 strlwr(tok_end);
1327 strcat(map_line, "/");
1328
1329 strncat(map_line, tok_ptr, ENC_BUF_SIZE - strlen(map_line) - 20);
1330 map_line[ENC_BUF_SIZE] = '\0';
1331 strcat(map_line, "'] = {");
1332 while (*val_ptr == ' ')
1333 val_ptr++;
1334 if (*val_ptr == '<')
1335 {
1336 val_ptr++;
1337 tok_end = strchr(val_ptr, '>');
1338 if (tok_end)
1339 {
1340 *tok_end = '\0';
1341 while (*val_ptr && (strlen(map_line) < ENC_BUF_SIZE - 20))
1342 {
1343 if (strlen(val_ptr) < 8)
1344 {
1345 if (sscanf(val_ptr, "%lx", &ucode) == 1)
1346 sprintf(map_line + strlen(map_line), "0x%04lX", ucode);
1347 else
1348 {
1349 PRINTF_PR("Error: Unrecognized format of the file %s: %s\n", map_fname, val_ptr);
1350 }
1351 break;
1352 }
1353 else
1354 {
1355 strncpy(hex_val, val_ptr, 4);
1356 hex_val[4] = '\0';
1357 if (sscanf(hex_val, "%lx", &ucode) == 1)
1358 sprintf(map_line + strlen(map_line), "0x%04lX, ", ucode);
1359 else
1360 {
1361 PRINTF_PR("Error: Unrecognized format of the file %s: %s\n", map_fname, val_ptr);
1362 }
1363 val_ptr += 4;
1364 }
1365 }
1366 if (strlen(map_line) >= ENC_BUF_SIZE - 20)
1367 {
1368 PRINTF_PR("Error: Map line buffer overflow, file %s: %s\n", map_fname, val_ptr);
1369 }
1370 }
1371 else
1372 {
1373 PRINTF_PR("Error: Unrecognized format of the file %s: %s\n", map_fname, val_ptr);
1374 }
1375 }
1376 else
1377 {
1378 if (sscanf(val_ptr, "%lu", &ucode) == 1)
1379 sprintf(map_line + strlen(map_line), "0x%04lX", ucode);
1380 else
1381 {
1382 PRINTF_PR("Error: Unrecognized format of the file %s: %s\n", map_fname, val_ptr);
1383 }
1384 }
1385 strcat(map_line, "}}, false, \'\')");
1386 if (luaL_dostring(L, map_line))
1387 {
1388 PRINTF_PR("Error: Lua script %.500s execution error: %.500s.\n", map_line, lua_tostring(L, -1));
1389 lua_pop(L, 1);
1390 }
1391
1392 free(val_cpy);
1393 val_cpy = NULL;
1394 }
1395 }
1396 }
1397 if (ferror(map_file))
1398 {
1399 PRINTF_PR("Error: File %s%s read error.\n", map_fname, G2U_EXT);
1400 }
1401 fclose(map_file);
1402 }
1403
1404 /* loading internal unicode encoding map of particular font */
1405 lua_getglobal(L, "deploy_font_glyph_map");
1406 lua_pushstring(L, map_fname);
1407 if (lua_pcall(L, 1, 0, 0))
1408 {
1409 PRINTF_PR("Error: Lua function deploy_font_glyph_map(\'%s\') execution error: %s.\n", map_fname, lua_tostring(L, -1));
1410 lua_pop(L, 1);
1411 }
1412 }
1413 }
1414 free(map_fname);
1415 map_fname = NULL;
1416 }
1417
1418 static void append_glyph_to_g2u_map_buf(const char *ps_name, const char *pfb_name, const char *glyph_name, boolean *p_empty_g2u, char **p_g2u_buf_ptr, size_t *p_g2u_buf_len)
1419 {
1420 char *glyph_subst = NULL;
1421 unsigned int *tounicode = NULL;
1422 int ucnt;
1423 boolean needs_g2u_tounicode, needs_g2u_cvt;
1424 static char map_line[ENC_BUF_SIZE + 1];
1425 char hex_val[8 + 1];
1426 int ii;
1427
1428 assert(ps_name);
1429 assert(pfb_name);
1430 assert(glyph_name);
1431 assert(p_empty_g2u);
1432 assert(p_g2u_buf_ptr);
1433 assert(p_g2u_buf_len);
1434
1435 /* check whether the glyph allready processed */
1436 tounicode = get_glname_tounicode(glyph_name, pfb_name, "processed:", TRUE, &ucnt, NULL);
1437 if (!tounicode)
1438 {
1439 /* "pfb:msbm10/A" */
1440 tounicode = get_glname_tounicode(glyph_name, pfb_name, "pfb:", FALSE, &ucnt, &needs_g2u_tounicode);
1441 if (tounicode)
1442 {
1443 glyph_subst = glyph_name_cvt(glyph_name, pfb_name, &needs_g2u_cvt);
1444 if (!glyph_subst)
1445 glyph_subst = strdup(glyph_name);
1446 assert(glyph_subst);
1447 /* if (needs_g2u_tounicode && needs_g2u_cvt) */
1448 {
1449 *p_empty_g2u = FALSE;
1450 append_map_buf(&g2u_buf, p_g2u_buf_ptr, p_g2u_buf_len, " /");
1451 append_map_buf(&g2u_buf, p_g2u_buf_ptr, p_g2u_buf_len, glyph_subst);
1452 append_map_buf(&g2u_buf, p_g2u_buf_ptr, p_g2u_buf_len, " <");
1453 for (ii = 0; ii < ucnt; ii++)
1454 {
1455 sprintf(hex_val, "%04X", tounicode[ii]);
1456 append_map_buf(&g2u_buf, p_g2u_buf_ptr, p_g2u_buf_len, hex_val);
1457 }
1458 append_map_buf(&g2u_buf, p_g2u_buf_ptr, p_g2u_buf_len, ">\n");
1459 }
1460 free(glyph_subst);
1461 glyph_subst = NULL;
1462
1463 /* mark processed glyphs */
1464 strcpy(map_line, "load_touni({['");
1465 strcat(map_line, "processed:");
1466 strncat(map_line, pfb_name, ENC_BUF_SIZE - strlen(map_line) - 20);
1467 map_line[ENC_BUF_SIZE] = '\0';
1468 strlwr(map_line);
1469 strcat(map_line, "/");
1470 strncat(map_line, glyph_name, ENC_BUF_SIZE - strlen(map_line) - 20);
1471 map_line[ENC_BUF_SIZE] = '\0';
1472 strcat(map_line, "'] = {");
1473 for (ii = 0; ii < ucnt; ii++)
1474 {
1475 if (strlen(map_line) >= ENC_BUF_SIZE - 20 - 4)
1476 {
1477 PRINTF_PR("Error: Map line buffer overflow, font %s, glyph %s\n", ps_name, glyph_name);
1478 break;
1479 }
1480 sprintf(hex_val, "0x%04X, ", tounicode[ii]);
1481 strncat(map_line, hex_val, ENC_BUF_SIZE - strlen(map_line) - 20);
1482 }
1483 strcat(map_line, "}}, false, \'\')");
1484
1485 if (luaL_dostring(L, map_line))
1486 {
1487 PRINTF_PR("Error: Lua script %.500s execution error: %.500s.\n", map_line, lua_tostring(L, -1));
1488 lua_pop(L, 1);
1489 }
1490 }
1491 }
1492 if (tounicode)
1493 RELEASE(tounicode);
1494 }
1495
1496 static void t1_scan_keys(boolean encoding_only)
1497 {
1498 char *g2u_buf_ptr = NULL;
1499 size_t g2u_buf_len = 0;
1500 char *ps_name = NULL;
1501 char *font_file = NULL;
1502 char *pfb_name = NULL;
1503 char *pfb_full_name = NULL;
1504 int charcode;
1505 const char *glyph_name = NULL;
1506 char glyph_name_buf[GLYPH_NAME_LEN + 1];
1507 char *glyph_name_ptr;
1508 charusetype_ref *cu_ref = NULL;
1509 charusetype_entry *cu_head = NULL;
1510 charusetype_entry *cu_entry;
1511 char **enc;
1512 const UsedMapElem *usedchars;
1513 char *pars = NULL;
1514 char *rest = NULL;
1515 char *rest_cpy = NULL;
1516 size_t rest_len;
1517 int dict_size = 0;
1518 int num_pars;
1519 boolean empty_g2u = TRUE;
1520 FT_Error ft_error = FT_Err_Ok;
1521 FT_Face ft_face = NULL;
1522 FT_Long gid;
1523
1524 if (t1_line_array && (strstr(t1_line_array, FONTINFO_KEY) == t1_line_array)) // "/FontInfo 9 dict dup begin"
1525 {
1526 if (g2u_buf)
1527 free(g2u_buf);
1528 g2u_buf = NULL;
1529
1530 if (cur_file_name)
1531 parse_g2u(cur_file_name);
1532
1533 if (!encoding_only)
1534 {
1535 if (curfnt && curfnt->resfont && ((ps_name = curfnt->resfont->PSname) != NULL) && ((font_file = curfnt->resfont->Fontfile) != NULL) && strstr(font_file, PFB_EXT))
1536 {
1537 pfb_name = extract_fname(font_file, NULL);
1538 assert(pfb_name);
1539
1540 init_map_buf(&g2u_buf, &g2u_buf_ptr, &g2u_buf_len);
1541 append_map_buf(&g2u_buf, &g2u_buf_ptr, &g2u_buf_len, "/GlyphNames2Unicode <<\n");
1542
1543 if ((!t1_partialdownload) || (!curfnt->resfont->partialdownload))
1544 {
1545 pfb_full_name = kpse_find_file(font_file, kpse_type1_format, TRUE);
1546 if (pfb_full_name)
1547 {
1548 ft_error = FT_New_Face(ft_lib, pfb_full_name, 0, &ft_face);
1549 if (!ft_error)
1550 {
1551 for (gid = 0; gid < ft_face->num_glyphs; gid++)
1552 {
1553 ft_error = FT_Get_Glyph_Name(ft_face, gid, glyph_name_buf, GLYPH_NAME_LEN);
1554 if (ft_error)
1555 {
1556 PRINTF_PR("Error: Glyph %ld of the font %s has no name.\n", gid, ps_name); /* TODO: ft_error evaluation */
1557 }
1558 else
1559 append_glyph_to_g2u_map_buf(ps_name, pfb_name, glyph_name_buf, &empty_g2u, &g2u_buf_ptr, &g2u_buf_len);
1560 }
1561 FT_Done_Face(ft_face);
1562 }
1563 else
1564 {
1565 PRINTF_PR("Error: Unable to open font %s\n", font_file); /* TODO: ft_error evaluation */
1566 }
1567 free(pfb_full_name);
1568 pfb_full_name = NULL;
1569 }
1570 }
1571 else
1572 {
1573 cu_ref = lookup_charused(ps_name, curfnt->resfont->otftype);
1574 if (cu_ref)
1575 cu_head = cu_ref->head;
1576 if (cu_head)
1577 {
1578 cu_entry = cu_head;
1579 while (cu_entry)
1580 {
1581 usedchars = NULL;
1582 if (cu_entry->charused_ptr)
1583 usedchars = cu_entry->charused_ptr->bitmap;
1584 enc = NULL;
1585 if (cu_entry->charused_ptr && cu_entry->charused_ptr->fd && cu_entry->charused_ptr->fd->resfont)
1586 {
1587 if (cu_entry->charused_ptr->fd->resfont->Vectfile)
1588 enc = getEncoding(cu_entry->charused_ptr->fd->resfont->Vectfile, NULL);
1589 else
1590 {
1591 if (t1_glyph_names)
1592 enc = getEncoding(ps_name, t1_glyph_names);
1593 else
1594 enc = NULL;
1595 }
1596 cu_entry->charused_ptr->fd->resfont->enc = enc;
1597 }
1598 if (usedchars && enc)
1599 for (charcode = 0; charcode < 256; charcode++)
1600 {
1601 glyph_name = enc[charcode];
1602 if (glyph_name && (IS_USED_CHAR(usedchars, charcode)))
1603 {
1604 /* Acrobat Distiller needs base glyphs of dot sufficed variants included as well */
1605 strncpy(glyph_name_buf, glyph_name, GLYPH_NAME_LEN);
1606 glyph_name_buf[GLYPH_NAME_LEN] = '\0';
1607 glyph_name_ptr = strchr(glyph_name_buf, '.');
1608 if (glyph_name_ptr)
1609 {
1610 *glyph_name_ptr = '\0';
1611 append_glyph_to_g2u_map_buf(ps_name, pfb_name, glyph_name_buf, &empty_g2u, &g2u_buf_ptr, &g2u_buf_len);
1612 }
1613 append_glyph_to_g2u_map_buf(ps_name, pfb_name, glyph_name, &empty_g2u, &g2u_buf_ptr, &g2u_buf_len);
1614 }
1615 }
1616 cu_entry = cu_entry->next;
1617 }
1618 }
1619 }
1620 append_map_buf(&g2u_buf, &g2u_buf_ptr, &g2u_buf_len, ">> def\n");
1621
1622 free(pfb_name);
1623 }
1624
1625 if (g2u_buf && empty_g2u)
1626 {
1627 free(g2u_buf);
1628 g2u_buf = NULL;
1629 }
1630
1631 /* incrementing PS dict size by one */
1632 if (g2u_buf)
1633 {
1634 pars = t1_line_array + strlen(FONTINFO_KEY);
1635 while (*pars == ' ')
1636 pars++;
1637 num_pars = sscanf(pars, "%d", &dict_size);
1638 if (num_pars == 1)
1639 {
1640 rest = pars;
1641 while (*rest != ' ')
1642 rest++;
1643 rest_len = strlen(rest);
1644 rest_cpy = strdup(rest);
1645 if (!rest_cpy) error("Out of memory");
1646 if (strlen(t1_line_array) >= t1_line_limit - 1) // incrementing dict size could increase current t1 line length max by one
1647 {
1648 PRINTF_PR("Error: File %s line too long, /FontInfo dict size will be not increased.\n", cur_file_name ? cur_file_name : "");
1649 }
1650 else
1651 {
1652 sprintf(pars, "%d", dict_size + 1);
1653 pars += strlen(pars);
1654 strcpy(pars, rest_cpy);
1655 t1_line_ptr = pars + rest_len; // t1_line_array + strlen(t1_line_array);
1656 }
1657 free(rest_cpy);
1658 rest_cpy = NULL;
1659 }
1660 }
1661 }
1662 }
1663 }
1664
1665 static void t1_scan_param(boolean encoding_only)
1666 #else
1667 static void t1_scan_param(void)
1668 #endif /* XDVIPSK */
1669 {
1670 static const char *lenIV = "/lenIV";
1671 #ifdef XDVIPSK
1672 if (g2u_buf && bitfile && t1_line_array && (strstr(t1_line_array, "end ") == t1_line_array) && strstr(t1_line_array, " def")) // "end readonly def"
1673 {
1674 if (!read_encoding_only)
1675 fputs(g2u_buf, bitfile);
1676 free(g2u_buf);
1677 g2u_buf = NULL;
1678
1679 if (ferror(bitfile))
1680 {
1681 PRINTF_PR("%s", "Error: Output file write error.\n");
1682 }
1683 }
1684 #endif /* XDVIPSK */
1685 if (!t1_scan || *t1_line_array != '/')
1686 return;
1687 if (t1_prefix(lenIV)) {
1688 t1_lenIV = t1_scan_num(t1_line_array + strlen(lenIV), 0);
1689 if (t1_lenIV < 0)
1690 pdftex_fail("negative value of lenIV is not supported");
1691 return;
1692 }
1693 #ifdef XDVIPSK
1694 t1_scan_keys(encoding_only);
1695 #else
1696 t1_scan_keys();
1697 #endif /* XDVIPSK */
1698 }
1699
1700 static void copy_glyph_names(char **glyph_names, int a, int b)
1701 {
1702 if (glyph_names[b] != notdef) {
1703 xfree(glyph_names[b]);
1704 glyph_names[b] = notdef;
1705 }
1706 if (glyph_names[a] != notdef) {
1707 glyph_names[b] = xstrdup(glyph_names[a]);
1708 }
1709 }
1710
1711 /* read encoding from Type1 font file, return glyph_names array, or pdffail() */
1712
1713 static char **t1_builtin_enc(void)
1714 {
1715 int i, a, b, c, counter = 0;
1716 char *r, *p, **glyph_names;
1717 /* At this moment "/Encoding" is the prefix of t1_line_array */
1718 glyph_names = t1_builtin_glyph_names;
1719 for (i = 0; i < 256; i++)
1720 glyph_names[i] = notdef;
1721 if (t1_suffix("def")) { /* predefined encoding */
1722 if (sscanf(t1_line_array + strlen("/Encoding"), "%255s", t1_buf_array) == 1
1723 && strcmp(t1_buf_array, "StandardEncoding") == 0) {
1724 t1_encoding = ENC_STANDARD;
1725 for (i = 0; i < 256; i++) {
1726 if (standard_glyph_names[i] != notdef)
1727 glyph_names[i] = xstrdup(standard_glyph_names[i]);
1728 }
1729 return glyph_names;
1730 }
1731 pdftex_fail("cannot subset font (unknown predefined encoding `%s')",
1732 t1_buf_array);
1733 }
1734 /* At this moment "/Encoding" is the prefix of t1_line_array, and the encoding is
1735 * not a predefined encoding.
1736 *
1737 * We have two possible forms of Encoding vector. The first case is
1738 *
1739 * /Encoding [/a /b /c...] readonly def
1740 *
1741 * and the second case can look like
1742 *
1743 * /Encoding 256 array 0 1 255 {1 index exch /.notdef put} for
1744 * dup 0 /x put
1745 * dup 1 /y put
1746 * ...
1747 * readonly def
1748 */
1749 t1_encoding = ENC_BUILTIN;
1750 if (t1_prefix("/Encoding [") || t1_prefix("/Encoding[")) { /* the first case */
1751 r = strchr(t1_line_array, '[') + 1;
1752 skip(r, ' ');
1753 for (;;) {
1754 while (*r == '/') {
1755 for (p = t1_buf_array, r++;
1756 *r != 32 && *r != 10 && *r != ']' && *r != '/';
1757 *p++ = *r++);
1758 *p = 0;
1759 skip(r, ' ');
1760 if (counter > 255)
1761 pdftex_fail("encoding vector contains more than 256 names");
1762 if (strcmp(t1_buf_array, notdef) != 0)
1763 glyph_names[counter] = xstrdup(t1_buf_array);
1764 counter++;
1765 }
1766 if (*r != 10 && *r != '%') {
1767 if (str_prefix(r, "] def") || str_prefix(r, "] readonly def"))
1768 break;
1769 else {
1770 remove_eol(r, t1_line_array);
1771 pdftex_fail
1772 ("a name or `] def' or `] readonly def' expected: `%s'", t1_line_array);
1773 }
1774 }
1775 t1_getline();
1776 r = t1_line_array;
1777 }
1778 } else { /* the second case */
1779 p = strchr(t1_line_array, 10);
1780 for (;;) {
1781 if (*p == 10) {
1782 t1_getline();
1783 p = t1_line_array;
1784 }
1785 /*
1786 check for `dup <index> <glyph> put'
1787 */
1788 if (sscanf(p, "dup %i%255s put", &i, t1_buf_array) == 2 &&
1789 *t1_buf_array == '/' && valid_code(i)) {
1790 if (strcmp(t1_buf_array + 1, notdef) != 0)
1791 glyph_names[i] = xstrdup(t1_buf_array + 1);
1792 p = strstr(p, " put");
1793 if (!p)
1794 pdftex_fail("invalid pfb, no put found in dup");
1795 p += strlen(" put");
1796 skip(p, ' ');
1797 }
1798 /*
1799 check for `dup dup <to> exch <from> get put'
1800 */
1801 else if (sscanf(p, "dup dup %i exch %i get put", &b, &a) == 2
1802 && valid_code(a) && valid_code(b)) {
1803 copy_glyph_names(glyph_names, a, b);
1804 p = strstr(p, " get put");
1805 if (!p)
1806 pdftex_fail("invalid pfb, no get put found in dup dup");
1807 p += strlen(" get put");
1808 skip(p, ' ');
1809 }
1810 /*
1811 check for `dup dup <from> <size> getinterval <to> exch putinterval'
1812 */
1813 else if (sscanf(p, "dup dup %i %i getinterval %i exch putinterval",
1814 &a, &c, &b) == 3
1815 && valid_code(a) && valid_code(b) && valid_code(c)) {
1816 for (i = 0; i < c; i++)
1817 copy_glyph_names(glyph_names, a + i, b + i);
1818 p = strstr(p, " putinterval");
1819 if (!p)
1820 pdftex_fail("invalid pfb, no putinterval found in dup dup");
1821 p += strlen(" putinterval");
1822 skip(p, ' ');
1823 }
1824 /*
1825 check for `def' or `readonly def'
1826 */
1827 else if ((p == t1_line_array || (p > t1_line_array && p[-1] == ' '))
1828 && strcmp(p, "def\n") == 0)
1829 return glyph_names;
1830 /*
1831 skip an unrecognizable word
1832 */
1833 else {
1834 while (*p != ' ' && *p != 10)
1835 p++;
1836 skip(p, ' ');
1837 }
1838 }
1839 }
1840 return glyph_names;
1841 }
1842
1843 static void t1_check_end(void)
1844 {
1845 if (t1_eof())
1846 return;
1847 t1_getline();
1848 if (t1_prefix("{restore}"))
1849 t1_putline();
1850 }
1851
1852 static boolean t1_open_fontfile(const char *open_name_prefix)
1853 {
1854 if (!t1_open()) {
1855 char *msg = concat ("! Couldn't find font file ", cur_file_name);
1856 error(msg);
1857 }
1858 t1_init_params(open_name_prefix);
1859 return true; /* font file found */
1860 }
1861
1862 #define t1_include()
1863
1864 #define check_subr(subr) \
1865 if (subr >= subr_size || subr < 0) \
1866 pdftex_fail("Subrs array: entry index out of range (%i)", subr);
1867
1868 static const char **check_cs_token_pair(void)
1869 {
1870 const char **p = (const char **) cs_token_pairs_list;
1871 for (; p[0] != NULL; ++p)
1872 if (t1_buf_prefix(p[0]) && t1_buf_suffix(p[1]))
1873 return p;
1874 return NULL;
1875 }
1876
1877 static void cs_store(boolean is_subr)
1878 {
1879 char *p;
1880 cs_entry *ptr;
1881 int subr;
1882 for (p = t1_line_array, t1_buf_ptr = t1_buf_array; *p != ' ';
1883 *t1_buf_ptr++ = *p++);
1884 *t1_buf_ptr = 0;
1885 if (is_subr) {
1886 subr = t1_scan_num(p + 1, 0);
1887 check_subr(subr);
1888 ptr = subr_tab + subr;
1889 } else {
1890 ptr = cs_ptr++;
1891 if (cs_ptr - cs_tab > cs_size)
1892 pdftex_fail
1893 ("CharStrings dict: more entries than dict size (%i)", cs_size);
1894 if (strcmp(t1_buf_array + 1, notdef) == 0) /* skip the slash */
1895 ptr->name = notdef;
1896 else
1897 ptr->name = xstrdup(t1_buf_array + 1);
1898 }
1899 /* copy " RD " + cs data to t1_buf_array */
1900 memcpy(t1_buf_array, t1_line_array + cs_start - 4,
1901 (unsigned) (t1_cslen + 4));
1902 /* copy the end of cs data to t1_buf_array */
1903 for (p = t1_line_array + cs_start + t1_cslen,
1904 t1_buf_ptr = t1_buf_array + t1_cslen + 4;
1905 *p != 10; *t1_buf_ptr++ = *p++);
1906 *t1_buf_ptr++ = 10;
1907 if (is_subr && cs_token_pair == NULL)
1908 cs_token_pair = check_cs_token_pair();
1909 ptr->len = t1_buf_ptr - t1_buf_array;
1910 ptr->cslen = t1_cslen;
1911 ptr->data = xtalloc(ptr->len, byte);
1912 memcpy(ptr->data, t1_buf_array, ptr->len);
1913 ptr->valid = true;
1914 }
1915
1916 #define store_subr() cs_store(true)
1917 #define store_cs() cs_store(false)
1918
1919 #define CC_STACK_SIZE 24
1920
1921 static integer cc_stack[CC_STACK_SIZE], *stack_ptr = cc_stack;
1922 static cc_entry cc_tab[CS_MAX];
1923 static boolean is_cc_init = false;
1924
1925 #define cc_pop(N) \
1926 if (stack_ptr - cc_stack < (N)) \
1927 stack_error(N); \
1928 stack_ptr -= N
1929
1930 #define stack_error(N) { \
1931 pdftex_fail("CharString: invalid access (%i) to stack (%i entries)", \
1932 (int) N, (int)(stack_ptr - cc_stack)); \
1933 goto cs_error; \
1934 }
1935
1936 /*
1937 static integer cc_get(integer index)
1938 {
1939 if (index < 0) {
1940 if (stack_ptr + index < cc_stack )
1941 stack_error(stack_ptr - cc_stack + index);
1942 return *(stack_ptr + index);
1943 }
1944 else {
1945 if (cc_stack + index >= stack_ptr)
1946 stack_error(index);
1947 return cc_stack[index];
1948 }
1949 }
1950 */
1951
1952 #define cc_get(N) ((N) < 0 ? *(stack_ptr + (N)) : *(cc_stack + (N)))
1953 #define cc_push(V) *stack_ptr++ = V
1954 #define cc_clear() stack_ptr = cc_stack
1955 #define set_cc(N, B, A, C) \
1956 cc_tab[N].nargs = A; \
1957 cc_tab[N].bottom = B; \
1958 cc_tab[N].clear = C; \
1959 cc_tab[N].valid = true
1960
1961 static void cc_init(void)
1962 {
1963 int i;
1964 if (is_cc_init)
1965 return;
1966 for (i = 0; i < CS_MAX; i++)
1967 cc_tab[i].valid = false;
1968 set_cc(CS_HSTEM, true, 2, true);
1969 set_cc(CS_VSTEM, true, 2, true);
1970 set_cc(CS_VMOVETO, true, 1, true);
1971 set_cc(CS_RLINETO, true, 2, true);
1972 set_cc(CS_HLINETO, true, 1, true);
1973 set_cc(CS_VLINETO, true, 1, true);
1974 set_cc(CS_RRCURVETO, true, 6, true);
1975 set_cc(CS_CLOSEPATH, false, 0, true);
1976 set_cc(CS_CALLSUBR, false, 1, false);
1977 set_cc(CS_RETURN, false, 0, false);
1978 /*
1979 set_cc(CS_ESCAPE, false, 0, false);
1980 */
1981 set_cc(CS_HSBW, true, 2, true);
1982 set_cc(CS_ENDCHAR, false, 0, true);
1983 set_cc(CS_RMOVETO, true, 2, true);
1984 set_cc(CS_HMOVETO, true, 1, true);
1985 set_cc(CS_VHCURVETO, true, 4, true);
1986 set_cc(CS_HVCURVETO, true, 4, true);
1987 set_cc(CS_DOTSECTION, false, 0, true);
1988 set_cc(CS_VSTEM3, true, 6, true);
1989 set_cc(CS_HSTEM3, true, 6, true);
1990 set_cc(CS_SEAC, true, 5, true);
1991 set_cc(CS_SBW, true, 4, true);
1992 set_cc(CS_DIV, false, 2, false);
1993 set_cc(CS_CALLOTHERSUBR, false, 0, false);
1994 set_cc(CS_POP, false, 0, false);
1995 set_cc(CS_SETCURRENTPOINT, true, 2, true);
1996 is_cc_init = true;
1997 }
1998
1999 #define cs_getchar() cdecrypt(*data++, &cr)
2000
2001 #define mark_subr(n) cs_mark(0, n)
2002 #define mark_cs(s) cs_mark(s, 0)
2003
2004 static void cs_fail(const char *cs_name, int subr, const char *fmt, ...)
2005 {
2006 char buf[SMALL_BUF_SIZE];
2007 va_list args;
2008 va_start(args, fmt);
2009 vsprintf(buf, fmt, args);
2010 va_end(args);
2011 if (cs_name == NULL)
2012 pdftex_warn("Subr (%i): %s", (int) subr, buf);
2013 else
2014 pdftex_warn("CharString (/%s): %s", cs_name, buf);
2015 }
2016
2017 /* fix a return-less subr by appending CS_RETURN */
2018 static void append_cs_return(cs_entry *ptr)
2019 {
2020 unsigned short cr;
2021 int i;
2022 byte *p, *q, *data, *new_data;
2023 assert(ptr != NULL && ptr->valid && ptr->used);
2024
2025 /* decrypt the cs data to t1_buf_array, append CS_RETURN */
2026 p = (byte *) t1_buf_array;
2027 data = ptr->data + 4;
2028 cr = 4330;
2029 for (i = 0; i < ptr->cslen; i++)
2030 *p++ = cs_getchar();
2031 *p = CS_RETURN;
2032
2033 /* encrypt the new cs data to new_data */
2034 new_data = xtalloc(ptr->len + 1, byte);
2035 memcpy(new_data, ptr->data, 4);
2036 p = new_data + 4;
2037 q = (byte *) t1_buf_array;
2038 cr = 4330;
2039 for (i = 0; i < ptr->cslen + 1; i++)
2040 *p++ = cencrypt(*q++, &cr);
2041 memcpy(p, ptr->data + 4 + ptr->cslen, ptr->len - ptr->cslen - 4);
2042
2043 /* update *ptr */
2044 xfree(ptr->data);
2045 ptr->data = new_data;
2046 ptr->len++;
2047 ptr->cslen++;
2048 }
2049
2050 static void cs_mark(const char *cs_name, int subr)
2051 {
2052 byte *data;
2053 int i, b, cs_len;
2054 int last_cmd = 0;
2055 integer a, a1, a2;
2056 unsigned short cr;
2057 static integer lastargOtherSubr3 = 3; /* the argument of last call to
2058 OtherSubrs[3] */
2059 cs_entry *ptr;
2060 cc_entry *cc;
2061 if (cs_name == NULL) {
2062 check_subr(subr);
2063 ptr = subr_tab + subr;
2064 if (!ptr->valid)
2065 return;
2066 } else {
2067 if (cs_notdef != NULL &&
2068 (cs_name == notdef || strcmp(cs_name, notdef) == 0))
2069 ptr = cs_notdef;
2070 else {
2071 for (ptr = cs_tab; ptr < cs_ptr; ptr++)
2072 if (strcmp(ptr->name, cs_name) == 0)
2073 break;
2074 if (ptr == cs_ptr) {
2075 pdftex_warn("glyph `%s' undefined", cs_name);
2076 return;
2077 }
2078 if (ptr->name == notdef)
2079 cs_notdef = ptr;
2080 }
2081 }
2082 /* only marked CharString entries and invalid entries can be skipped;
2083 valid marked subrs must be parsed to keep the stack in sync */
2084 if (!ptr->valid || (ptr->used && cs_name != NULL))
2085 return;
2086 ptr->used = true;
2087 cr = 4330;
2088 cs_len = ptr->cslen;
2089 data = ptr->data + 4;
2090 for (i = 0; i < t1_lenIV; i++, cs_len--)
2091 cs_getchar();
2092 while (cs_len > 0) {
2093 --cs_len;
2094 b = cs_getchar();
2095 if (b >= 32) {
2096 if (b <= 246)
2097 a = b - 139;
2098 else if (b <= 250) {
2099 --cs_len;
2100 a = ((b - 247) << 8) + 108 + cs_getchar();
2101 } else if (b <= 254) {
2102 --cs_len;
2103 a = -((b - 251) << 8) - 108 - cs_getchar();
2104 } else {
2105 cs_len -= 4;
2106 a = (cs_getchar() & 0xff) << 24;
2107 a |= (cs_getchar() & 0xff) << 16;
2108 a |= (cs_getchar() & 0xff) << 8;
2109 a |= (cs_getchar() & 0xff) << 0;
2110 if (sizeof(integer) > 4 && (a & 0x80000000))
2111 a |= ~0x7FFFFFFF;
2112 }
2113 cc_push(a);
2114 } else {
2115 if (b == CS_ESCAPE) {
2116 b = cs_getchar() + CS_1BYTE_MAX;
2117 cs_len--;
2118 }
2119 if (b >= CS_MAX) {
2120 cs_fail(cs_name, subr, "command value out of range: %i",
2121 (int) b);
2122 goto cs_error;
2123 }
2124 cc = cc_tab + b;
2125 if (!cc->valid) {
2126 cs_fail(cs_name, subr, "command not valid: %i", (int) b);
2127 goto cs_error;
2128 }
2129 if (cc->bottom) {
2130 if (stack_ptr - cc_stack < cc->nargs)
2131 cs_fail(cs_name, subr,
2132 "less arguments on stack (%i) than required (%i)",
2133 (int) (stack_ptr - cc_stack), (int) cc->nargs);
2134 else if (stack_ptr - cc_stack > cc->nargs)
2135 cs_fail(cs_name, subr,
2136 "more arguments on stack (%i) than required (%i)",
2137 (int) (stack_ptr - cc_stack), (int) cc->nargs);
2138 }
2139 last_cmd = b;
2140 switch (cc - cc_tab) {
2141 case CS_CALLSUBR:
2142 a1 = cc_get(-1);
2143 cc_pop(1);
2144 mark_subr(a1);
2145 if (!subr_tab[a1].valid) {
2146 cs_fail(cs_name, subr, "cannot call subr (%i)", (int) a1);
2147 goto cs_error;
2148 }
2149 break;
2150 case CS_DIV:
2151 cc_pop(2);
2152 cc_push(0);
2153 break;
2154 case CS_CALLOTHERSUBR:
2155 if (cc_get(-1) == 3)
2156 lastargOtherSubr3 = cc_get(-3);
2157 a1 = cc_get(-2) + 2;
2158 cc_pop(a1);
2159 break;
2160 case CS_POP:
2161 cc_push(lastargOtherSubr3);
2162 /* the only case when we care about the value being pushed onto
2163 stack is when POP follows CALLOTHERSUBR (changing hints by
2164 OtherSubrs[3])
2165 */
2166 break;
2167 case CS_SEAC:
2168 a1 = cc_get(3);
2169 a2 = cc_get(4);
2170 cc_clear();
2171 mark_cs(standard_glyph_names[a1]);
2172 mark_cs(standard_glyph_names[a2]);
2173 break;
2174 default:
2175 if (cc->clear)
2176 cc_clear();
2177 }
2178 }
2179 }
2180 if (cs_name == NULL && last_cmd != CS_RETURN) {
2181 pdftex_warn("last command in subr `%i' is not a RETURN; "
2182 "I will add it now but please consider fixing the font",
2183 (int) subr);
2184 append_cs_return(ptr);
2185 }
2186 return;
2187 cs_error: /* an error occured during parsing */
2188 cc_clear();
2189 ptr->valid = false;
2190 ptr->used = false;
2191 }
2192
2193 #ifdef XDVIPSK
2194 static void t1_subset_ascii_part(boolean encoding_only)
2195 #else
2196 static void t1_subset_ascii_part(void)
2197 #endif /* XDVIPSK */
2198 {
2199 int i, j;
2200 t1_getline();
2201 while (!t1_prefix("/Encoding")) {
2202 #ifdef XDVIPSK
2203 t1_scan_param(encoding_only);
2204 #else
2205 t1_scan_param();
2206 #endif /* XDVIPSK */
2207 if (!(t1_prefix("/UniqueID")
2208 && !strncmp(t1_line_array + strlen(t1_line_array) -4, "def", 3)))
2209 t1_putline();
2210 t1_getline();
2211 }
2212 if (is_reencoded(fm_cur))
2213 t1_glyph_names = external_enc();
2214 else
2215 t1_glyph_names = t1_builtin_enc();
2216 if (is_included(fm_cur) && is_subsetted(fm_cur)) {
2217 make_subset_tag(fm_cur, t1_glyph_names);
2218 update_subset_tag();
2219 }
2220 if (t1_encoding == ENC_STANDARD)
2221 t1_puts("/Encoding StandardEncoding def\n");
2222 else {
2223 t1_puts
2224 ("/Encoding 256 array\n0 1 255 {1 index exch /.notdef put} for\n");
2225 for (i = 0, j = 0; i < 256; i++) {
2226 if (is_used_char(i) && t1_glyph_names[i] != notdef) {
2227 j++;
2228 t1_printf("dup %i /%s put\n", (int)t1_char(i), t1_glyph_names[i]);
2229 }
2230 }
2231 if (j == 0)
2232 /* We didn't mark anything for the Encoding array. */
2233 /* We add "dup 0 /.notdef put" for compatibility */
2234 /* with Acrobat 5.0. */
2235 t1_puts("dup 0 /.notdef put\n");
2236 t1_puts("readonly def\n");
2237 }
2238 do {
2239 t1_getline();
2240 #ifdef XDVIPSK
2241 t1_scan_param(encoding_only);
2242 #else
2243 t1_scan_param();
2244 #endif /* XDVIPSK */
2245 if (!t1_prefix("/UniqueID")) /* ignore UniqueID for subsetted fonts */
2246 t1_putline();
2247 } while (t1_in_eexec == 0);
2248 }
2249
2250 static void cs_init(void)
2251 {
2252 cs_ptr = cs_tab = NULL;
2253 cs_dict_start = cs_dict_end = NULL;
2254 cs_count = cs_size = cs_size_pos = 0;
2255 cs_token_pair = NULL;
2256 subr_tab = NULL;
2257 subr_array_start = subr_array_end = NULL;
2258 subr_max = subr_size = subr_size_pos = 0;
2259 }
2260
2261 static void init_cs_entry(cs_entry *cs)
2262 {
2263 cs->data = NULL;
2264 cs->name = NULL;
2265 cs->len = 0;
2266 cs->cslen = 0;
2267 cs->used = false;
2268 cs->valid = false;
2269 }
2270
2271 static void t1_read_subrs(void)
2272 {
2273 int i, s;
2274 cs_entry *ptr;
2275 t1_getline();
2276 while (!(t1_charstrings() || t1_subrs())) {
2277 #ifdef XDVIPSK
2278 t1_scan_param(FALSE);
2279 #else
2280 t1_scan_param();
2281 #endif /* XDVIPSK */
2282 t1_putline();
2283 t1_getline();
2284 }
2285 found:
2286 t1_cs = true;
2287 t1_scan = false;
2288 if (!t1_subrs())
2289 return;
2290 subr_size_pos = strlen("/Subrs") + 1;
2291 /* subr_size_pos points to the number indicating dict size after "/Subrs" */
2292 subr_size = t1_scan_num(t1_line_array + subr_size_pos, 0);
2293 if (subr_size == 0) {
2294 while (!t1_charstrings())
2295 t1_getline();
2296 return;
2297 }
2298 subr_tab = xtalloc(subr_size, cs_entry);
2299 for (ptr = subr_tab; ptr - subr_tab < subr_size; ptr++)
2300 init_cs_entry(ptr);
2301 subr_array_start = xstrdup(t1_line_array);
2302 t1_getline();
2303 while (t1_cslen) {
2304 store_subr();
2305 t1_getline();
2306 }
2307 /* mark the first four entries without parsing */
2308 for (i = 0; i < subr_size && i < 4; i++)
2309 subr_tab[i].used = true;
2310 /* the end of the Subrs array might have more than one line so we need to
2311 concatnate them to subr_array_end. Unfortunately some fonts don't have
2312 the Subrs array followed by the CharStrings dict immediately (synthetic
2313 fonts). If we cannot find CharStrings in next POST_SUBRS_SCAN lines then
2314 we will treat the font as synthetic and ignore everything until next
2315 Subrs is found
2316 */
2317
2318 #define POST_SUBRS_SCAN 5
2319
2320 s = 0;
2321 *t1_buf_array = 0;
2322 for (i = 0; i < POST_SUBRS_SCAN; i++) {
2323 if (t1_charstrings())
2324 break;
2325 s += t1_line_ptr - t1_line_array;
2326 alloc_array(t1_buf, s, T1_BUF_SIZE);
2327 strcat(t1_buf_array, t1_line_array);
2328 t1_getline();
2329 }
2330 subr_array_end = xstrdup(t1_buf_array);
2331 if (i == POST_SUBRS_SCAN) { /* CharStrings not found;
2332 suppose synthetic font */
2333 for (ptr = subr_tab; ptr - subr_tab < subr_size; ptr++)
2334 if (ptr->valid)
2335 xfree(ptr->data);
2336 xfree(subr_tab);
2337 xfree(subr_array_start);
2338 xfree(subr_array_end);
2339 cs_init();
2340 t1_cs = false;
2341 t1_synthetic = true;
2342 while (!(t1_charstrings() || t1_subrs()))
2343 t1_getline();
2344 goto found;
2345 }
2346 }
2347
2348 #define t1_subr_flush() t1_flush_cs(true)
2349 #define t1_cs_flush() t1_flush_cs(false)
2350
2351 static void t1_flush_cs(boolean is_subr)
2352 {
2353 char *p;
2354 byte *r, *return_cs = NULL;
2355 cs_entry *tab, *end_tab, *ptr;
2356 char *start_line, *line_end;
2357 int count, size_pos;
2358 unsigned short cr, cs_len = 0; /* to avoid warning about uninitialized use of cs_len */
2359 if (is_subr) {
2360 start_line = subr_array_start;
2361 line_end = subr_array_end;
2362 size_pos = subr_size_pos;
2363 tab = subr_tab;
2364 count = subr_max + 1;
2365 end_tab = subr_tab + count;
2366 } else {
2367 start_line = cs_dict_start;
2368 line_end = cs_dict_end;
2369 size_pos = cs_size_pos;
2370 tab = cs_tab;
2371 end_tab = cs_ptr;
2372 count = cs_count;
2373 }
2374 t1_line_ptr = t1_line_array;
2375 for (p = start_line; p - start_line < size_pos;)
2376 *t1_line_ptr++ = *p++;
2377 while (isdigit((unsigned char)*p))
2378 p++;
2379 sprintf(t1_line_ptr, "%u", count);
2380 strcat(t1_line_ptr, p);
2381 t1_line_ptr = eol(t1_line_array);
2382 t1_putline();
2383
2384 /* create return_cs to replace unused subr's */
2385 if (is_subr) {
2386 cr = 4330;
2387 cs_len = 0;
2388 /* at this point we have t1_lenIV >= 0;
2389 * a negative value would be caught in t1_scan_param() */
2390 return_cs = xtalloc(t1_lenIV + 1, byte);
2391 for (cs_len = 0, r = return_cs; cs_len < t1_lenIV; cs_len++, r++)
2392 *r = cencrypt(0x00, &cr);
2393 *r = cencrypt(CS_RETURN, &cr);
2394 cs_len++;
2395 }
2396
2397 for (ptr = tab; ptr < end_tab; ptr++) {
2398 if (ptr->used) {
2399 if (is_subr)
2400 sprintf(t1_line_array, "dup %lu %u",
2401 (unsigned long) (ptr - tab), ptr->cslen);
2402 else
2403 sprintf(t1_line_array, "/%s %u", ptr->name, ptr->cslen);
2404 p = strend(t1_line_array);
2405 memcpy(p, ptr->data, ptr->len);
2406 t1_line_ptr = p + ptr->len;
2407 t1_putline();
2408 } else {
2409 /* replace unsused subr's by return_cs */
2410 if (is_subr) {
2411 sprintf(t1_line_array, "dup %lu %u%s ",
2412 (unsigned long) (ptr - tab), cs_len, cs_token_pair[0]);
2413 p = strend(t1_line_array);
2414 memcpy(p, return_cs, cs_len);
2415 t1_line_ptr = p + cs_len;
2416 t1_putline();
2417 sprintf(t1_line_array, " %s", cs_token_pair[1]);
2418 t1_line_ptr = eol(t1_line_array);
2419 t1_putline();
2420 }
2421 }
2422 xfree(ptr->data);
2423 if (ptr->name != notdef)
2424 xfree(ptr->name);
2425 }
2426 #ifdef XDVIPSK
2427 if (t1_add_array)
2428 {
2429 p = t1_add_array;
2430 if (t1_eexec_encrypt)
2431 {
2432 while (p < t1_add_ptr)
2433 t1_outhex(eencrypt(*p++)); /* dvips outputs hex, unlike pdftex */
2434 }
2435 else
2436 {
2437 while (p < t1_add_ptr)
2438 t1_putchar(*p++);
2439 }
2440 free(t1_add_array);
2441 t1_add_array = NULL;
2442 }
2443 #endif
2444 sprintf(t1_line_array, "%s", line_end);
2445 t1_line_ptr = eol(t1_line_array);
2446 t1_putline();
2447 if (is_subr)
2448 xfree(return_cs);
2449 xfree(tab);
2450 xfree(start_line);
2451 xfree(line_end);
2452 }
2453
2454 #ifndef XDVIPSK
2455 static void t1_mark_glyphs(void)
2456 #else
2457 static void t1_mark_glyphs(boolean fullfont)
2458 #endif /* XDVIPSK */
2459 {
2460 int i;
2461 char *charset = extra_charset();
2462 char *g, *s, *r;
2463 cs_entry *ptr;
2464 #ifndef XDVIPSK
2465 if (t1_synthetic || embed_all_glyphs(tex_font)) { /* mark everything */
2466 #else
2467 if (t1_synthetic || embed_all_glyphs(tex_font) || fullfont) { /* mark everything */
2468 #endif /* XDVIPSK */
2469 if (cs_tab != NULL)
2470 for (ptr = cs_tab; ptr < cs_ptr; ptr++)
2471 if (ptr->valid)
2472 ptr->used = true;
2473 if (subr_tab != NULL) {
2474 for (ptr = subr_tab; ptr - subr_tab < subr_size; ptr++)
2475 if (ptr->valid)
2476 ptr->used = true;
2477 subr_max = subr_size - 1;
2478 }
2479 return;
2480 }
2481 mark_cs(notdef);
2482 for (i = 0; i < 256; i++)
2483 if (is_used_char(i)) {
2484 if (t1_glyph_names[i] == notdef)
2485 pdftex_warn("character %i is mapped to %s", i, notdef);
2486 else
2487 mark_cs(t1_glyph_names[i]);
2488 }
2489 if (charset == NULL)
2490 goto set_subr_max;
2491 g = s = charset + 1; /* skip the first '/' */
2492 r = strend(g);
2493 while (g < r) {
2494 while (*s != '/' && s < r)
2495 s++;
2496 *s = 0; /* terminate g by rewriting '/' to 0 */
2497 mark_cs(g);
2498 g = s + 1;
2499 }
2500 set_subr_max:
2501 if (subr_tab != NULL)
2502 for (subr_max = -1, ptr = subr_tab; ptr - subr_tab < subr_size; ptr++)
2503 if (ptr->used && ptr - subr_tab > subr_max)
2504 subr_max = ptr - subr_tab;
2505 }
2506
2507 static void t1_check_unusual_charstring(void)
2508 {
2509 char *p = strstr(t1_line_array, charstringname) + strlen(charstringname);
2510 int i;
2511 /* if no number follows "/CharStrings", let's read the next line */
2512 if (sscanf(p, "%i", &i) != 1) {
2513 /* pdftex_warn("no number found after `%s', I assume it's on the next line",
2514 charstringname); */
2515 strcpy(t1_buf_array, t1_line_array);
2516
2517 /* t1_getline always appends EOL to t1_line_array; let's change it to
2518 * space before appending the next line
2519 */
2520 *(strend(t1_buf_array) - 1) = ' ';
2521
2522 t1_getline();
2523 alloc_array(t1_buf, strlen(t1_line_array)
2524 + (t1_buf_array ? strlen(t1_buf_array) + 1 : 0),
2525 T1_BUF_SIZE);
2526 strcat(t1_buf_array, t1_line_array);
2527 alloc_array(t1_line, strlen(t1_buf_array) + 1, T1_BUF_SIZE);
2528 strcpy(t1_line_array, t1_buf_array);
2529 t1_line_ptr = eol(t1_line_array);
2530 }
2531 }
2532
2533 #ifndef XDVIPSK
2534 static void t1_subset_charstrings(void)
2535 #else
2536 static void t1_subset_charstrings(boolean fullfont)
2537 #endif /* XDVIPSK */
2538 {
2539 cs_entry *ptr;
2540
2541 /* at this point t1_line_array contains "/CharStrings".
2542 when we hit a case like this:
2543 dup/CharStrings
2544 229 dict dup begin
2545 we read the next line and concatenate to t1_line_array before moving on
2546 */
2547 t1_check_unusual_charstring();
2548
2549 cs_size_pos = strstr(t1_line_array, charstringname)
2550 + strlen(charstringname) - t1_line_array + 1;
2551 /* cs_size_pos points to the number indicating
2552 dict size after "/CharStrings" */
2553 cs_size = t1_scan_num(t1_line_array + cs_size_pos, 0);
2554 cs_ptr = cs_tab = xtalloc(cs_size, cs_entry);
2555 for (ptr = cs_tab; ptr - cs_tab < cs_size; ptr++)
2556 init_cs_entry(ptr);
2557 cs_notdef = NULL;
2558 cs_dict_start = xstrdup(t1_line_array);
2559 t1_getline();
2560 while (t1_cslen) {
2561 store_cs();
2562 t1_getline();
2563 }
2564 cs_dict_end = xstrdup(t1_line_array);
2565 #ifndef XDVIPSK
2566 t1_mark_glyphs();
2567 #else
2568 t1_mark_glyphs(fullfont);
2569 #endif /* XDVIPSK */
2570 if (subr_tab != NULL) {
2571 if (cs_token_pair == NULL)
2572 pdftex_fail
2573 ("This Type 1 font uses mismatched subroutine begin/end token pairs.");
2574 t1_subr_flush();
2575 }
2576 for (cs_count = 0, ptr = cs_tab; ptr < cs_ptr; ptr++)
2577 if (ptr->used)
2578 cs_count++;
2579 t1_cs_flush();
2580 }
2581
2582 static void t1_subset_end(void)
2583 {
2584 if (t1_synthetic) { /* copy to "dup /FontName get exch definefont pop" */
2585 while (!strstr(t1_line_array, "definefont")) {
2586 t1_getline();
2587 t1_putline();
2588 }
2589 while (!t1_end_eexec())
2590 t1_getline(); /* ignore the rest */
2591 t1_putline(); /* write "mark currentfile closefile" */
2592 } else
2593 while (!t1_end_eexec()) { /* copy to "mark currentfile closefile" */
2594 t1_getline();
2595 t1_putline();
2596 }
2597 t1_stop_eexec();
2598 if (fixedcontent) { /* copy 512 zeros (not needed for PDF) */
2599 while (!t1_cleartomark()) {
2600 t1_getline();
2601 t1_putline();
2602 }
2603 if (!t1_synthetic) /* don't check "{restore}if" for synthetic fonts */
2604 t1_check_end(); /* write "{restore}if" if found */
2605 }
2606 get_length3();
2607 }
2608
2609 #ifdef XDVIPSK
2610 static void writet1(boolean encoding_only)
2611 #else
2612 static void writet1(void)
2613 #endif /* XDVIPSK */
2614 {
2615 #ifdef XDVIPSK
2616 read_encoding_only = encoding_only;
2617 #else
2618 read_encoding_only = false;
2619 #endif /* XDVIPSK */
2620 if (!is_included(fm_cur)) { /* scan parameters from font file */
2621 if (!t1_open_fontfile("{"))
2622 return;
2623 t1_scan_only();
2624 t1_close_font_file("}");
2625 return;
2626 }
2627 if (!is_subsetted(fm_cur)) { /* include entire font */
2628 if (!t1_open_fontfile("<<"))
2629 return;
2630 t1_include();
2631 t1_close_font_file(">>");
2632 return;
2633 }
2634 /* partial downloading */
2635 if (!t1_open_fontfile("<"))
2636 return;
2637 #ifdef XDVIPSK
2638 t1_subset_ascii_part(encoding_only);
2639 if (!read_encoding_only)
2640 {
2641 #else
2642 t1_subset_ascii_part();
2643 #endif /* XDVIPSK */
2644 t1_start_eexec();
2645 cc_init();
2646 cs_init();
2647 t1_read_subrs();
2648 #ifndef XDVIPSK
2649 t1_subset_charstrings();
2650 #else
2651 t1_subset_charstrings(0);
2652 #endif /* XDVIPSK */
2653 t1_subset_end();
2654 #ifdef XDVIPSK
2655 }
2656 #endif /* XDVIPSK */
2657 t1_close_font_file(">");
2658 }
2659
2660 #ifdef XDVIPSK
2661 boolean t1_write_full(char *fontfile, unsigned char *g)
2662 {
2663 read_encoding_only = false;
2664 grid = g;
2665 cur_file_name = fontfile;
2666 hexline_length = 0;
2667 dvips_extra_charset = NULL;
2668 if (!t1_open_fontfile("<"))
2669 return 0;
2670 #ifdef XDVIPSK
2671 redir_glyphtounicode_tfm_2_pfb();
2672 t1_subset_ascii_part(FALSE);
2673 #else
2674 t1_subset_ascii_part();
2675 #endif /* XDVIPSK */
2676 t1_start_eexec();
2677 cc_init();
2678 cs_init();
2679 t1_read_subrs();
2680 t1_subset_charstrings(1);
2681 t1_subset_end();
2682 t1_close_font_file(">");
2683 cur_file_name = NULL;
2684 return 1;
2685 }
2686 #endif /* XDVIPSK */
2687
2688 boolean t1_subset_2(char *fontfile, unsigned char *g, char *extraGlyphs)
2689 {
2690 int i;
2691 for (i = 0; i < 256; i++)
2692 ext_glyph_names[i] = (char*) notdef;
2693 grid = g;
2694 cur_file_name = fontfile;
2695 hexline_length = 0;
2696 dvips_extra_charset = extraGlyphs;
2697 #ifdef XDVIPSK
2698 t1_glyph_names = NULL;
2699 writet1(true); /* just scanning the t1_glyph_names and parsing all glyphtounicode_add sources */
2700 redir_glyphtounicode_tfm_2_pfb();
2701 writet1(false);
2702 cur_file_name = NULL;
2703 #else
2704 writet1();
2705 #endif /* XDVIPSK */
2706 for (i = 0; i < 256; i++)
2707 if (ext_glyph_names[i] != notdef)
2708 free(ext_glyph_names[i]);
2709 return 1; /* note: there *is* no unsuccessful return */
2710 }