Debian Codegraph @

texk/dvipdfm-x/t1_load.c

raw
1 /* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
2
3 Copyright (C) 2002-2021 by Jin-Hwan Cho and Shunsaku Hirata,
4 the dvipdfmx project team.
5
6 Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28 #include <math.h>
29
30 #include "system.h"
31 #include "mfileio.h"
32 #include "mem.h"
33 #include "error.h"
34 #include "numbers.h"
35
36 #include "pdfobj.h"
37 #include "pdffont.h"
38
39 #include "pdfencoding.h"
40 #include "unicode.h"
41
42 #include "dpxutil.h"
43
44 #include "pst_obj.h"
45 #include "pst.h"
46
47 #include "cff_limits.h"
48 #include "cff_types.h"
49 #include "cff_dict.h"
50 #include "cff.h"
51
52 #include "t1_load.h"
53
54 /* Migrated from t1crypt */
55 #define T1_EEKEY 55665u
56 #define T1_CHARKEY 4330u
57
58 #if 0
59 /* We no longer need encryption. */
60 static unsigned short r = T1_EEKEY, c1 = 52845, c2 = 22719;
61
62 static unsigned char t1_encrypt (unsigned char plain)
63 {
64 unsigned char cipher;
65 cipher = (plain ^ (r >> 8));
66 r = (cipher + r) * c1 + c2;
67 return cipher;
68 }
69
70 static void t1_crypt_init (unsigned short key)
71 {
72 r = key;
73 }
74 #endif /* 0 */
75
76 static void
77 t1_decrypt (unsigned short key,
78 unsigned char *dst, const unsigned char *src,
79 int skip, int len)
80 {
81 len -= skip;
82 while (skip--)
83 key = (key + *src++) * 52845u + 22719u;
84 while (len--) {
85 unsigned char c = *src++;
86 *dst++ = (c ^ (key >> 8));
87 key = (key + c) * 52845u + 22719u;
88 }
89 }
90 /* T1CRYPT */
91
92 #define MATCH_NAME(t,n) (PST_NAMETYPE((t)) && (pst_length_of((t)) == strlen((n))) && !memcmp(pst_data_ptr((t)),(n),strlen((n))))
93 #define MATCH_OP(t,n) (PST_UNKNOWNTYPE((t)) && (pst_length_of((t)) == strlen((n))) && !memcmp(pst_data_ptr((t)),(n),strlen((n))))
94
95 #define RELEASE_TOK(t) if ((t) != NULL) {\
96 pst_release_obj((t));\
97 (t) = NULL;\
98 }
99
100 static char *
101 get_next_key (unsigned char **start, unsigned char *end)
102 {
103 char *key = NULL;
104 pst_obj *tok;
105
106 while (*start < end &&
107 (tok = pst_get_token(start, end)) != NULL) {
108 if (PST_NAMETYPE(tok)) {
109 key = (char *) pst_getSV(tok);
110 RELEASE_TOK(tok);
111 break;
112 }
113 RELEASE_TOK(tok);
114 }
115
116 return key;
117 }
118
119 static int
120 seek_operator (unsigned char **start, unsigned char *end, const char *op)
121 {
122 pst_obj *tok = NULL;
123
124 while (*start < end &&
125 (tok = pst_get_token(start, end)) != NULL) {
126 if (MATCH_OP(tok, op)) {
127 break;
128 }
129 RELEASE_TOK(tok);
130 }
131
132 if (tok == NULL)
133 return -1;
134 RELEASE_TOK(tok);
135
136 return 0;
137 }
138
139
140 static int
141 parse_svalue (unsigned char **start, unsigned char *end, char **value)
142 {
143 pst_obj *tok;
144
145 tok = pst_get_token(start, end);
146 if (tok == NULL)
147 return -1;
148 else if (PST_NAMETYPE(tok) || PST_STRINGTYPE(tok))
149 *value = (char *) pst_getSV(tok);
150 else {
151 RELEASE_TOK(tok);
152 return -1;
153 }
154 RELEASE_TOK(tok);
155
156 return 1;
157 }
158
159 static int
160 parse_bvalue (unsigned char **start, unsigned char *end, double *value)
161 {
162 pst_obj *tok;
163
164 tok = pst_get_token(start, end);
165 if (tok == NULL)
166 return -1;
167 else if (PST_BOOLEANTYPE(tok))
168 *value = (double) pst_getIV(tok);
169 else {
170 RELEASE_TOK(tok);
171 return -1;
172 }
173 RELEASE_TOK(tok);
174
175 return 1;
176 }
177
178 static int
179 parse_nvalue (unsigned char **start, unsigned char *end, double *value, int max)
180 {
181 int argn = 0;
182 pst_obj *tok;
183
184 tok = pst_get_token(start, end);
185 if (tok == NULL)
186 return -1;
187 /*
188 * All array elements must be numeric token. (ATM compatible)
189 */
190 if (PST_NUMBERTYPE(tok) && max > 0) {
191 value[0] = (double) pst_getRV(tok);
192 argn = 1;
193 } else if (PST_MARKTYPE(tok)) {
194 /* It does not distinguish '[' and '{'... */
195 RELEASE_TOK(tok);
196 while (*start < end &&
197 (tok = pst_get_token(start, end)) != NULL &&
198 PST_NUMBERTYPE(tok) && argn < max) {
199 value[argn++] = (double) pst_getRV(tok);
200 RELEASE_TOK(tok);
201 }
202 if (tok == NULL)
203 return -1;
204 if (!MATCH_OP(tok, "]") && !MATCH_OP(tok, "}")) {
205 argn = -1;
206 }
207 }
208 RELEASE_TOK(tok);
209
210 return argn;
211 }
212
213 static const char *const StandardEncoding[256] = {
214 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
215 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
216 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
217 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
218 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
219 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
220 ".notdef", ".notdef", "space", "exclam", "quotedbl",
221 "numbersign", "dollar", "percent", "ampersand", "quoteright",
222 "parenleft", "parenright", "asterisk", "plus", "comma",
223 "hyphen", "period", "slash", "zero", "one",
224 "two", "three", "four", "five", "six",
225 "seven", "eight", "nine", "colon", "semicolon",
226 "less", "equal", "greater", "question", "at",
227 "A", "B", "C", "D", "E",
228 "F", "G", "H", "I", "J",
229 "K", "L", "M", "N", "O",
230 "P", "Q", "R", "S", "T",
231 "U", "V", "W", "X", "Y",
232 "Z", "bracketleft", "backslash", "bracketright", "asciicircum",
233 "underscore", "quoteleft", "a", "b", "c",
234 "d", "e", "f", "g", "h",
235 "i", "j", "k", "l", "m",
236 "n", "o", "p", "q", "r",
237 "s", "t", "u", "v", "w",
238 "x", "y", "z", "braceleft", "bar",
239 "braceright", "asciitilde", ".notdef", ".notdef", ".notdef",
240 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
241 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
242 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
243 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
244 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
245 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
246 ".notdef", "exclamdown", "cent", "sterling", "fraction",
247 "yen", "florin", "section", "currency", "quotesingle",
248 "quotedblleft", "guillemotleft", "guilsinglleft", "guilsinglright", "fi",
249 "fl", ".notdef", "endash", "dagger", "daggerdbl",
250 "periodcentered", ".notdef", "paragraph", "bullet", "quotesinglbase",
251 "quotedblbase", "quotedblright", "guillemotright", "ellipsis", "perthousand",
252 ".notdef", "questiondown", ".notdef", "grave", "acute",
253 "circumflex", "tilde", "macron", "breve", "dotaccent",
254 "dieresis", ".notdef", "ring", "cedilla", ".notdef",
255 "hungarumlaut", "ogonek", "caron", "emdash", ".notdef",
256 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
257 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
258 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
259 "AE", ".notdef", "ordfeminine", ".notdef", ".notdef",
260 ".notdef", ".notdef", "Lslash", "Oslash", "OE",
261 "ordmasculine", ".notdef", ".notdef", ".notdef", ".notdef",
262 ".notdef", "ae", ".notdef", ".notdef", ".notdef",
263 "dotlessi", ".notdef", ".notdef", "lslash", "oslash",
264 "oe", "germandbls", ".notdef", ".notdef", ".notdef",
265 ".notdef"
266 };
267
268 static const char *const ISOLatin1Encoding[256] = {
269 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
270 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
271 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
272 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
273 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
274 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
275 ".notdef", ".notdef", "space", "exclam", "quotedbl",
276 "numbersign", "dollar", "percent", "ampersand", "quotesingle",
277 "parenleft", "parenright", "asterisk", "plus", "comma",
278 "hyphen", "period", "slash", "zero", "one",
279 "two", "three", "four", "five", "six",
280 "seven", "eight", "nine", "colon", "semicolon",
281 "less", "equal", "greater", "question", "at",
282 "A", "B", "C", "D", "E",
283 "F", "G", "H", "I", "J",
284 "K", "L", "M", "N", "O",
285 "P", "Q", "R", "S", "T",
286 "U", "V", "W", "X", "Y",
287 "Z", "bracketleft", "backslash", "bracketright", "asciicircum",
288 "underscore", "grave", "a", "b", "c",
289 "d", "e", "f", "g", "h",
290 "i", "j", "k", "l", "m",
291 "n", "o", "p", "q", "r",
292 "s", "t", "u", "v", "w",
293 "x", "y", "z", "braceleft", "bar",
294 "braceright", "asciitilde", ".notdef", ".notdef", ".notdef",
295 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
296 ".notdef", ".notdef", ".notdef", ".notdef", ".notdef",
297 ".notdef", ".notdef", ".notdef", ".notdef", "dotlessi",
298 "quoteleft", "quoteright", "circumflex", "tilde", "macron",
299 "breve", "dotaccent", "dieresis", ".notdef", "ring",
300 "cedilla", ".notdef", "hungarumlaut", "ogonek", "caron",
301 "space", "exclamdown", "cent", "sterling", "currency",
302 "yen", "brokenbar", "section", "dieresis", "copyright",
303 "ordfeminine", "guillemotleft", "logicalnot", "hyphen",
304 "registered",
305 "macron", "degree", "plusminus", "twosuperior", "threesuperior",
306 "acute", "mu", "paragraph", "periodcentered", "cedilla",
307 "onesuperior", "ordmasculine", "guillemotright", "onequarter",
308 "onehalf",
309 "threequarters", "questiondown", "Agrave", "Aacute", "Acircumflex",
310 "Atilde", "Adieresis", "Aring", "AE", "Ccedilla",
311 "Egrave", "Eacute", "Ecircumflex", "Edieresis", "Igrave",
312 "Iacute", "Icircumflex", "Idieresis", "Eth", "Ntilde",
313 "Ograve", "Oacute", "Ocircumflex", "Otilde", "Odieresis",
314 "multiply", "Oslash", "Ugrave", "Uacute", "Ucircumflex",
315 "Udieresis", "Yacute", "Thorn", "germandbls", "agrave",
316 "aacute", "acircumflex", "atilde", "adieresis", "aring",
317 "ae", "ccedilla", "egrave", "eacute", "ecircumflex",
318 "edieresis", "igrave", "iacute", "icircumflex", "idieresis",
319 "eth", "ntilde", "ograve", "oacute", "ocircumflex",
320 "otilde", "odieresis", "divide", "oslash", "ugrave",
321 "uacute", "ucircumflex", "udieresis", "yacute", "thorn",
322 "ydieresis"
323 };
324
325 /* Treat cases such as "dup num num getinterval num exch putinterval"
326 * or "dup num exch num get put"
327 */
328 static int
329 try_put_or_putinterval (char **enc_vec, unsigned char **start, unsigned char *end)
330 {
331 pst_obj *tok;
332 int i, num1, num2, num3;
333
334 tok = pst_get_token(start, end);
335 if (!tok || !PST_INTEGERTYPE(tok) ||
336 (num1 = pst_getIV(tok)) > 255 || num1 < 0) {
337 RELEASE_TOK(tok);
338 return -1;
339 }
340 RELEASE_TOK(tok);
341
342 tok = pst_get_token(start, end);
343 if (!tok) {
344 return -1;
345 } else if (MATCH_OP(tok, "exch")) {
346 /* dup num exch num get put */
347 RELEASE_TOK(tok);
348
349 tok = pst_get_token(start, end);
350 if (!tok || !PST_INTEGERTYPE(tok) ||
351 (num2 = pst_getIV(tok)) > 255 || num2 < 0) {
352 RELEASE_TOK(tok);
353 return -1;
354 }
355 RELEASE_TOK(tok);
356
357 tok = pst_get_token(start, end);
358 if (!MATCH_OP(tok, "get")) {
359 RELEASE_TOK(tok);
360 return -1;
361 }
362 RELEASE_TOK(tok);
363
364 tok = pst_get_token(start, end);
365 if (!MATCH_OP(tok, "put")) {
366 RELEASE_TOK(tok);
367 return -1;
368 }
369 RELEASE_TOK(tok);
370
371 if (enc_vec[num1])
372 RELEASE(enc_vec[num1]);
373 enc_vec[num1] = xstrdup(enc_vec[num2]);
374 } else if (PST_INTEGERTYPE(tok) &&
375 (num2 = pst_getIV(tok)) + num1 <= 255 && num2 >= 0) {
376 RELEASE_TOK(tok);
377
378 tok = pst_get_token(start, end);
379 if (!MATCH_OP(tok, "getinterval")) {
380 RELEASE_TOK(tok);
381 return -1;
382 }
383 RELEASE_TOK(tok);
384
385 tok = pst_get_token(start, end);
386 if (!tok || !PST_INTEGERTYPE(tok) ||
387 (num3 = pst_getIV(tok)) + num2 > 255 || num3 < 0) {
388 RELEASE_TOK(tok);
389 return -1;
390 }
391 RELEASE_TOK(tok);
392
393 tok = pst_get_token(start, end);
394 if (!MATCH_OP(tok, "exch")) {
395 RELEASE_TOK(tok);
396 return -1;
397 }
398 RELEASE_TOK(tok);
399
400 tok = pst_get_token(start, end);
401 if (!MATCH_OP(tok, "putinterval")) {
402 RELEASE_TOK(tok);
403 return -1;
404 }
405 RELEASE_TOK(tok);
406
407 for (i = 0; i < num2; i++) {
408 if (enc_vec[num1 + i]) { /* num1 + i < 256 here */
409 if (enc_vec[num3 + i]) { /* num3 + i < 256 here */
410 RELEASE(enc_vec[num3 + i]);
411 enc_vec[num3+i] = NULL;
412 }
413 enc_vec[num3 + i] = xstrdup(enc_vec[num1 + i]);
414 }
415 }
416 } else {
417 RELEASE_TOK(tok);
418 return -1;
419 }
420
421 return 0;
422 }
423
424 static int
425 parse_encoding (char **enc_vec, unsigned char **start, unsigned char *end)
426 {
427 pst_obj *tok;
428 int code;
429
430 /*
431 * StandardEncoding def
432 * or
433 * ISOLatin1Encoding def
434 * or
435 * 0 1 255 {1 index exch /.notdef put } for
436 * dup int name put
437 * ...
438 * [readonly] def
439 */
440 tok = pst_get_token(start, end);
441 if (MATCH_OP(tok, "StandardEncoding")) {
442 RELEASE_TOK(tok);
443 if (enc_vec) {
444 for (code = 0; code < 256; code++) {
445 if (StandardEncoding[code] &&
446 strcmp(StandardEncoding[code], ".notdef") != 0) {
447 enc_vec[code] = NEW(strlen(StandardEncoding[code])+1, char);
448 strcpy(enc_vec[code], StandardEncoding[code]);
449 } else {
450 enc_vec[code] = NULL;
451 }
452 }
453 }
454 } else if (MATCH_OP(tok, "ISOLatin1Encoding")) {
455 RELEASE_TOK(tok);
456 if (enc_vec) {
457 for (code = 0; code < 256; code++) {
458 if (ISOLatin1Encoding[code] &&
459 strcmp(ISOLatin1Encoding[code], ".notdef") != 0) {
460 enc_vec[code] = NEW(strlen(ISOLatin1Encoding[code])+1, char);
461 strcpy(enc_vec[code], ISOLatin1Encoding[code]);
462 } else {
463 enc_vec[code] = NULL;
464 }
465 }
466 }
467 } else if (MATCH_OP(tok, "ExpertEncoding")) {
468 RELEASE_TOK(tok);
469 if (enc_vec) {
470 WARN("ExpertEncoding not supported.");
471 RELEASE_TOK(tok);
472 return -1;
473 }
474 /*
475 * Not supported yet.
476 */
477 } else {
478 RELEASE_TOK(tok);
479 seek_operator(start, end, "array");
480 /*
481 * Pick all seaquences that matches "dup n /Name put" until
482 * occurrence of "def" or "readonly".
483 */
484 while (*start < end &&
485 (tok = pst_get_token(start, end)) != NULL) {
486 if (MATCH_OP(tok, "def") || MATCH_OP(tok, "readonly")) {
487 RELEASE_TOK(tok);
488 break;
489 } else if (!MATCH_OP(tok, "dup")) {
490 RELEASE_TOK(tok);
491 continue;
492 }
493 RELEASE_TOK(tok);
494
495 /* cmctt10.pfb for examples contains the following PS code
496 * dup num num getinterval num exch putinterval
497 * dup num exch num get put
498 */
499 tok = pst_get_token(start, end);
500 if (MATCH_OP(tok, "dup")) { /* possibly putinterval type */
501 if (enc_vec == NULL) {
502 WARN ("This kind of type1 fonts are not supported as native fonts.\n"
503 " They are supported if used with tfm fonts.\n");
504 } else {
505 try_put_or_putinterval(enc_vec, start, end);
506 }
507 RELEASE_TOK(tok)
508 continue;
509 } else if (!tok || !PST_INTEGERTYPE(tok) ||
510 (code = pst_getIV(tok)) > 255 || code < 0) {
511 RELEASE_TOK(tok);
512 continue;
513 }
514 RELEASE_TOK(tok);
515
516 tok = pst_get_token(start, end);
517 if (!tok || !PST_NAMETYPE(tok)) {
518 RELEASE_TOK(tok);
519 continue;
520 }
521 if (enc_vec) {
522 if (enc_vec[code])
523 RELEASE(enc_vec[code]);
524 enc_vec[code] = (char *) pst_getSV(tok);
525 }
526 RELEASE_TOK(tok);
527
528 tok = pst_get_token(start, end);
529 if (!MATCH_OP(tok, "put")) {
530 if (enc_vec[code]) {
531 RELEASE(enc_vec[code]);
532 enc_vec[code] = NULL;
533 }
534 RELEASE_TOK(tok);
535 continue;
536 }
537 RELEASE_TOK(tok);
538 }
539 }
540
541 return 0;
542 }
543
544 #ifndef CS_STR_LEN_MAX
545 #define CS_STR_LEN_MAX 65536UL
546 #endif
547 #ifndef CFF_GLYPH_MAX
548 #define CFF_GLYPH_MAX CFF_SID_MAX
549 #endif
550
551 static int
552 parse_subrs (cff_font *font,
553 unsigned char **start, unsigned char *end, int lenIV, int mode)
554 {
555 cff_index *subrs;
556 pst_obj *tok;
557 int i, count, offset, max_size;
558 int *offsets, *lengths;
559 card8 *data;
560
561 tok = pst_get_token(start, end);
562 if (!PST_INTEGERTYPE(tok) || pst_getIV(tok) < 0) {
563 WARN("Parsing Subrs failed.");
564 RELEASE_TOK(tok);
565 return -1;
566 }
567
568 count = pst_getIV(tok);
569 RELEASE_TOK(tok);
570
571 if (count == 0) {
572 font->subrs[0] = NULL;
573 return 0;
574 }
575
576 tok = pst_get_token(start, end);
577 if (!MATCH_OP(tok, "array")) {
578 RELEASE_TOK(tok);
579 return -1;
580 }
581 RELEASE_TOK(tok);
582
583 if (mode != 1) {
584 max_size = CS_STR_LEN_MAX;
585 data = NEW(max_size, card8);
586 offsets = NEW(count, int);
587 lengths = NEW(count, int);
588 memset(offsets, 0, sizeof(int)*count);
589 memset(lengths, 0, sizeof(int)*count);
590 } else {
591 max_size = 0;
592 data = NULL;
593 offsets = NULL;
594 lengths = NULL;
595 }
596
597 offset = 0;
598 /* dup subr# n-bytes RD n-binary-bytes NP */
599 for (i = 0; i < count;) {
600 int idx, len;
601
602 tok = pst_get_token(start, end);
603 if (!tok) {
604 if (data) RELEASE(data);
605 if (offsets) RELEASE(offsets);
606 if (lengths) RELEASE(lengths);
607 return -1;
608 } else if (MATCH_OP(tok, "ND") ||
609 MATCH_OP(tok, "|-") || MATCH_OP(tok, "def")) {
610 RELEASE_TOK(tok);
611 break;
612 } else if (!MATCH_OP(tok, "dup")) {
613 RELEASE_TOK(tok);
614 continue;
615 }
616 RELEASE_TOK(tok);
617
618 /* Found "dup" */
619 tok = pst_get_token(start, end);
620 if (!PST_INTEGERTYPE(tok) || pst_getIV(tok) < 0 ||
621 pst_getIV(tok) >= count) {
622 RELEASE_TOK(tok);
623 if (data) RELEASE(data);
624 if (offsets) RELEASE(offsets);
625 if (lengths) RELEASE(lengths);
626 return -1;
627 }
628 idx = pst_getIV(tok);
629 RELEASE_TOK(tok);
630
631 tok = pst_get_token(start, end);
632 if (!PST_INTEGERTYPE(tok) || pst_getIV(tok) < 0 ||
633 pst_getIV(tok) > CS_STR_LEN_MAX) {
634 RELEASE_TOK(tok);
635 return -1;
636 }
637 len = pst_getIV(tok);
638 RELEASE_TOK(tok);
639
640 tok = pst_get_token(start, end);
641 if (!MATCH_OP(tok, "RD") && !MATCH_OP(tok, "-|") &&
642 seek_operator(start, end, "readstring") < 0) {
643 RELEASE_TOK(tok);
644 if (data) RELEASE(data);
645 if (offsets) RELEASE(offsets);
646 if (lengths) RELEASE(lengths);
647 return -1;
648 }
649 RELEASE_TOK(tok);
650
651 *start += 1;
652 if (*start + len >= end) {
653 if (data) RELEASE(data);
654 if (offsets) RELEASE(offsets);
655 if (lengths) RELEASE(lengths);
656 return -1;
657 }
658 if (mode != 1) {
659 if (offset + len >= max_size) {
660 max_size += CS_STR_LEN_MAX;
661 data = RENEW(data, max_size, card8);
662 }
663 if (lenIV >= 0) {
664 t1_decrypt(T1_CHARKEY, data+offset, *start, lenIV, len);
665 offsets[idx] = offset;
666 offset += (lengths[idx] = len - lenIV);
667 } else if (len > 0) {
668 offsets[idx] = offset;
669 lengths[idx] = len;
670 memcpy(&data[offset], *start, len);
671 offset += len;
672 }
673 }
674 *start += len;
675 i++;
676 }
677
678 if (mode != 1) {
679 if (font->subrs[0] == NULL) {
680 subrs = font->subrs[0] = cff_new_index(count);
681 subrs->data = NEW(offset, card8);
682 offset = 0;
683 for (i = 0; i < count; i++) {
684 subrs->offset[i] = offset + 1;
685 if (lengths[i] > 0) {
686 memcpy(subrs->data + offset, data + offsets[i], lengths[i]);
687 offset += lengths[i];
688 }
689 }
690 subrs->offset[count] = offset + 1;
691 } else {
692 /* Adobe's OPO_____.PFB and OPBO____.PFB have two /Subrs dicts,
693 * and also have /CharStrings not followed by dicts.
694 * Simply ignores those data. By ChoF on 2009/04/08. */
695 WARN("Already found /Subrs; ignores the other /Subrs dicts.");
696 }
697 RELEASE(data);
698 RELEASE(offsets);
699 RELEASE(lengths);
700 }
701
702 return 0;
703 }
704
705 static int
706 parse_charstrings (cff_font *font,
707 unsigned char **start, unsigned char *end, int lenIV, int mode)
708 {
709 cff_index *charstrings;
710 cff_charsets *charset;
711 pst_obj *tok;
712 int i, count, have_notdef;
713 int max_size, offset;
714
715 /* /CharStrings n dict dup begin
716 * /GlyphName n-bytes RD -n-binary-bytes- ND
717 * ...
718 * end
719 * - stack - ... /CharStrings dict
720 */
721 tok = pst_get_token(start, end);
722 if (!PST_INTEGERTYPE(tok) ||
723 pst_getIV(tok) < 0 || pst_getIV(tok) > CFF_GLYPH_MAX) {
724 unsigned char *s = pst_getSV(tok);
725 WARN("Ignores non dict \"/CharStrings %s ...\"", s);
726 RELEASE(s);
727 RELEASE_TOK(tok);
728 return 0;
729 }
730 count = pst_getIV(tok);
731 RELEASE_TOK(tok);
732
733 if (mode != 1) {
734 charstrings = cff_new_index(count);
735 max_size = CS_STR_LEN_MAX;
736 charstrings->data = NEW(max_size, card8);
737 } else {
738 charstrings = NULL;
739 max_size = 0;
740 }
741 font->cstrings = charstrings;
742
743 charset = font->charsets = NEW(1, cff_charsets);
744 charset->format = 0;
745 charset->num_entries = count-1;
746 charset->data.glyphs = NEW(count-1, s_SID);
747 memset(charset->data.glyphs, 0, sizeof(s_SID)*(count-1));
748
749 offset = 0;
750 have_notdef = 0; /* .notdef must be at gid = 0 in CFF */
751
752 font->is_notdef_notzero = 0;
753 seek_operator(start, end, "begin");
754 for (i = 0; i < count; i++) {
755 char *glyph_name;
756 int len, gid, j;
757
758 /* BUG-20061126 (by ChoF):
759 * Some fonts (e.g., belleek/blsy.pfb) does not have the correct number
760 * of glyphs. Modify the codes even to work with these broken fonts.
761 */
762 tok = pst_get_token(start, end);
763 glyph_name = (char *)pst_getSV(tok);
764
765 if ((i == 0) && (glyph_name != NULL) && (strcmp (glyph_name, ".notdef") != 0))
766 font->is_notdef_notzero = 1;
767
768 if (PST_NAMETYPE(tok)) {
769 RELEASE_TOK(tok);
770 if (!glyph_name) {
771 return -1;
772 } else if (!strcmp(glyph_name, ".notdef")) {
773 gid = 0;
774 have_notdef = 1;
775 } else if (have_notdef) {
776 gid = i;
777 } else if (i == count - 1) {
778 WARN("No .notdef glyph???");
779 return -1;
780 } else {
781 gid = i+1;
782 }
783 } else if (PST_UNKNOWNTYPE(tok) && !strcmp(glyph_name, "end")) {
784 RELEASE_TOK(tok);
785 break;
786 } else {
787 RELEASE_TOK(tok);
788 return -1;
789 }
790
791 if (gid > 0)
792 charset->data.glyphs[gid-1] = cff_add_string(font, glyph_name, 0);
793 /*
794 * We don't care about duplicate strings here since
795 * later a subset font of this font will be generated.
796 */
797
798 RELEASE(glyph_name);
799
800 tok = pst_get_token(start, end);
801 if (!PST_INTEGERTYPE(tok) ||
802 pst_getIV(tok) < 0 || pst_getIV(tok) > CS_STR_LEN_MAX) {
803 RELEASE_TOK(tok);
804 return -1;
805 }
806 len = pst_getIV(tok);
807 RELEASE_TOK(tok);
808
809 tok = pst_get_token(start, end);
810 if (!MATCH_OP(tok, "RD") &&
811 !MATCH_OP(tok, "-|") &&
812 seek_operator(start, end, "readstring") < 0) {
813 RELEASE_TOK(tok);
814 return -1;
815 }
816 RELEASE_TOK(tok);
817
818 if (*start + len + 1 >= end) {
819 return -1;
820 }
821 if (mode != 1) {
822 if (offset + len >= max_size) {
823 max_size += MAX(len, CS_STR_LEN_MAX);
824 charstrings->data = RENEW(charstrings->data, max_size, card8);
825 }
826 if (gid == 0) {
827 if (lenIV >= 0) {
828 memmove(charstrings->data + len - lenIV, charstrings->data, offset);
829 for (j = 1; j <= i; j++) {
830 charstrings->offset[j] += len - lenIV;
831 }
832 } else {
833 memmove(charstrings->data + len, charstrings->data, offset);
834 for (j = 1; j <= i; j++) {
835 charstrings->offset[j] += len;
836 }
837 }
838 }
839 }
840
841 *start += 1;
842 if (mode != 1) {
843 if (lenIV >= 0) {
844 int offs = gid ? offset : 0;
845 charstrings->offset[gid] = offs + 1; /* start at 1 */
846 t1_decrypt(T1_CHARKEY, charstrings->data+offs, *start, lenIV, len);
847 offset += len - lenIV;
848 } else {
849 if (gid == 0) {
850 charstrings->offset[gid] = 1;
851 memcpy(&charstrings->data[0], *start, len);
852 } else {
853 charstrings->offset[gid] = offset + 1;
854 memcpy(&charstrings->data[offset], *start, len);
855 }
856 offset += len;
857 }
858 }
859 *start += len;
860
861 tok = pst_get_token(start, end);
862 if (!MATCH_OP(tok, "ND") && !MATCH_OP(tok, "|-")) {
863 RELEASE_TOK(tok);
864 return -1;
865 }
866 RELEASE_TOK(tok);
867 }
868 if (mode != 1)
869 charstrings->offset[count] = offset + 1;
870 font->num_glyphs = count;
871
872 return 0;
873 }
874
875 #define CHECK_ARGN_EQ(n) if (argn != (n)) {\
876 WARN("%d values expected but only %d read.", (n), argn);\
877 RELEASE(key);\
878 return -1;\
879 }
880 #define CHECK_ARGN_GE(n) if (argn < (n)) {\
881 WARN("%d values expected but only %d read.", (n), argn);\
882 RELEASE(key);\
883 return -1;\
884 }
885
886 #define MAX_ARGS 127
887 static int
888 parse_part2 (cff_font *font, unsigned char **start, unsigned char *end, int mode)
889 {
890 char *key;
891 double argv[MAX_ARGS];
892 int argn, lenIV = 4;
893
894 while (*start < end &&
895 (key = get_next_key(start, end)) != NULL) {
896 if (!strcmp(key, "Subrs")) {
897 /* levIV must appear before Subrs */
898 if (parse_subrs(font, start, end, lenIV, mode) < 0) {
899 RELEASE(key);
900 return -1;
901 }
902 } else if (!strcmp(key, "CharStrings")) {
903 if (parse_charstrings(font, start, end, lenIV, mode) < 0) {
904 RELEASE(key);
905 return -1;
906 }
907 } else if (!strcmp(key, "lenIV")) {
908 argn = parse_nvalue(start, end, argv, 1);
909 CHECK_ARGN_EQ(1);
910 lenIV = (int) argv[0];
911 } else if (!strcmp(key, "BlueValues") ||
912 !strcmp(key, "OtherBlues") ||
913 !strcmp(key, "FamilyBlues") ||
914 !strcmp(key, "FamilyOtherBlues") ||
915 !strcmp(key, "StemSnapH") ||
916 !strcmp(key, "StemSnapV")) {
917 /*
918 * Operand values are delta in CFF font dictionary encoding.
919 */
920 argn = parse_nvalue(start, end, argv, MAX_ARGS);
921 CHECK_ARGN_GE(0);
922 cff_dict_add(font->private[0], key, argn);
923 while (argn-- > 0) {
924 cff_dict_set(font->private[0], key, argn,
925 (argn == 0) ? argv[argn] : argv[argn] - argv[argn-1]);
926 }
927 } else if (!strcmp(key, "StdHW") ||
928 !strcmp(key, "StdVW") ||
929 !strcmp(key, "BlueScale") ||
930 !strcmp(key, "BlueShift") ||
931 !strcmp(key, "BlueFuzz") ||
932 !strcmp(key, "LanguageGroup") ||
933 !strcmp(key, "ExpansionFactor")) {
934 /*
935 * Value of StdHW and StdVW is described as an array in the
936 * Type 1 Font Specification but is a number in CFF format.
937 */
938 argn = parse_nvalue(start, end, argv, 1);
939 CHECK_ARGN_EQ(1);
940 cff_dict_add(font->private[0], key, 1);
941 cff_dict_set(font->private[0], key, 0, argv[0]);
942 } else if (!strcmp(key, "ForceBold")) {
943 argn = parse_bvalue(start, end, &(argv[0]));
944 CHECK_ARGN_EQ(1);
945 if (argv[0] != 0) {
946 cff_dict_add(font->private[0], key, 1);
947 cff_dict_set(font->private[0], key, 0, 1);
948 }
949 }
950 /*
951 * MinFeature, RndStemUp, UniqueID, Password ignored.
952 */
953 RELEASE(key);
954 }
955
956 return 0;
957 }
958
959 #ifndef TYPE1_NAME_LEN_MAX
960 #define TYPE1_NAME_LEN_MAX 127
961 #endif
962
963 static int
964 parse_part1 (cff_font *font, char **enc_vec,
965 unsigned char **start, unsigned char *end)
966 {
967 char *key, *strval;
968 double argv[MAX_ARGS];
969 int argn; /* Macro CHECK_ARGN_XX assume 'argn' is used. */
970
971 /*
972 * We skip PostScript code inserted before the beginning of
973 * font dictionary so that parser will not be confused with
974 * it. See LMRoman10-Regular (lmr10.pfb) for example.
975 */
976 if (seek_operator(start, end, "begin") < 0)
977 return -1;
978
979 while (*start < end &&
980 (key = get_next_key(start, end)) != NULL) {
981 if (!strcmp(key, "Encoding")) {
982 if (parse_encoding(enc_vec, start, end) < 0) {
983 RELEASE(key);
984 return -1;
985 }
986 } else if (!strcmp(key, "FontName")) {
987 argn = parse_svalue(start, end, &strval);
988 CHECK_ARGN_EQ(1);
989 if (strlen(strval) > TYPE1_NAME_LEN_MAX) {
990 WARN("FontName too long: %s (%d bytes)", strval, strlen(strval));
991 strval[TYPE1_NAME_LEN_MAX] = '\0';
992 }
993 cff_set_name(font, strval);
994 RELEASE(strval);
995 } else if (!strcmp(key, "FontType")) {
996 argn = parse_nvalue(start, end, argv, 1);
997 CHECK_ARGN_EQ(1);
998 if (argv[0] != 1.0) {
999 WARN("FontType %d not supported.", (int) argv[0]);
1000 RELEASE(key);
1001 return -1;
1002 }
1003 #if 0
1004 /* DISABLED:
1005 *
1006 * Subsetted font shouldn't have UniqueID.
1007 */
1008 } else if (!strcmp(key, "UniqueID")) {
1009 argn = parse_nvalue(start, end, argv, 1);
1010 CHECK_ARGN_EQ(1);
1011 cff_dict_add(font->topdict, key, 1);
1012 cff_dict_set(font->topdict, key, 0, argv[0]);
1013 #endif
1014 } else if (!strcmp(key, "ItalicAngle") ||
1015 !strcmp(key, "StrokeWidth") ||
1016 !strcmp(key, "PaintType")) {
1017 argn = parse_nvalue(start, end, argv, 1);
1018 CHECK_ARGN_EQ(1);
1019 if (argv[0] != 0.0) {
1020 #if 0
1021 /*
1022 * Positive value in Bitstream CharterBT-Italic ???
1023 */
1024 if (!strcmp(key, "ItalicAngle") && argv[0] > 0) {
1025 WARN("Positive ItalicAngle value: %g", argv[0]);
1026 argv[0] *= -1;
1027 }
1028 #endif
1029 cff_dict_add(font->topdict, key, 1);
1030 cff_dict_set(font->topdict, key, 0, argv[0]);
1031 }
1032 } else if (!strcmp(key, "UnderLinePosition") ||
1033 !strcmp(key, "UnderLineThickness")) {
1034 argn = parse_nvalue(start, end, argv, 1);
1035 CHECK_ARGN_EQ(1);
1036 cff_dict_add(font->topdict, key, 1);
1037 cff_dict_set(font->topdict, key, 0, argv[0]);
1038 } else if (!strcmp(key, "FontBBox")) {
1039 argn = parse_nvalue(start, end, argv, 4);
1040 CHECK_ARGN_EQ(4);
1041 cff_dict_add(font->topdict, key, 4);
1042 while (argn-- > 0) {
1043 cff_dict_set(font->topdict, key, argn, argv[argn]);
1044 }
1045 } else if (!strcmp(key, "FontMatrix")) {
1046 argn = parse_nvalue(start, end, argv, 6);
1047 CHECK_ARGN_EQ(6);
1048 if (argv[0] != 0.001 || argv[1] != 0.0 || argv[2] != 0.0 ||
1049 argv[3] != 0.001 || argv[4] != 0.0 || argv[5] != 0.0) {
1050 cff_dict_add(font->topdict, key, 6);
1051 while (argn-- > 0) {
1052 cff_dict_set(font->topdict, key, argn, argv[argn]);
1053 }
1054 }
1055 } else if (!strcmp(key, "version") || !strcmp(key, "Notice") ||
1056 !strcmp(key, "FullName") || !strcmp(key, "FamilyName") ||
1057 !strcmp(key, "Weight") || !strcmp(key, "Copyright")) {
1058 /*
1059 * FontInfo
1060 */
1061 argn = parse_svalue(start, end, &strval);
1062 CHECK_ARGN_EQ(1);
1063 {
1064 s_SID sid;
1065
1066 cff_dict_add(font->topdict, key, 1);
1067 if ((sid = cff_get_sid(font, strval)) == CFF_STRING_NOTDEF)
1068 sid = cff_add_string(font, strval, 0); /* FIXME */
1069 /*
1070 * We don't care about duplicate strings here since
1071 * later a subset font of this font will be generated.
1072 */
1073 cff_dict_set(font->topdict, key, 0, sid);
1074 }
1075 RELEASE(strval);
1076 } else if (!strcmp(key, "IsFixedPitch")) {
1077 argn = parse_bvalue(start, end, &(argv[0]));
1078 CHECK_ARGN_EQ(1);
1079 if (argv[0] != 0.0) {
1080 cff_dict_add(font->private[0], key, 1);
1081 cff_dict_set(font->private[0], key, 0, 1);
1082 }
1083 }
1084 RELEASE(key);
1085 }
1086
1087 return 0;
1088 }
1089
1090 int
1091 is_pfb (FILE *fp)
1092 {
1093 char sig[15];
1094 int i, ch;
1095
1096 rewind(fp);
1097 if ((ch = fgetc(fp)) != 128 ||
1098 (ch = fgetc (fp)) < 0 || ch > 3) {
1099 return 0;
1100 }
1101 for (i = 0; i < 4; i++) {
1102 if ((ch = fgetc(fp)) < 0) {
1103 return 0;
1104 }
1105 }
1106 for (i = 0; i < 14; i++) {
1107 if ((ch = fgetc(fp)) < 0) {
1108 return 0;
1109 }
1110 sig[i] = (char) ch;
1111 }
1112 if (!memcmp(sig, "%!PS-AdobeFont", 14) ||
1113 !memcmp(sig, "%!FontType1", 11)) {
1114 return 1;
1115 } else if (!memcmp(sig, "%!PS", 4)) {
1116 sig[14] = '\0';
1117 WARN("Ambiguous PostScript resource type: %s", sig);
1118 return 1;
1119 } else {
1120 WARN("Not a PFB font file?");
1121 return 0;
1122 }
1123 }
1124
1125
1126 #define PFB_SEG_TYPE_ASCII 1
1127 #define PFB_SEG_TYPE_BINARY 2
1128
1129 static unsigned char *
1130 get_pfb_segment (FILE *fp, int expected_type, int *length)
1131 {
1132 unsigned char *buffer;
1133 int bytesread;
1134
1135 buffer = NULL; bytesread = 0;
1136 for (;;) {
1137 int ch;
1138
1139 ch = fgetc(fp);
1140 if (ch < 0) {
1141 break;
1142 } else if (ch != 128) {
1143 ERROR("Not a pfb file?");
1144 }
1145 ch = fgetc(fp);
1146 if (ch < 0 || ch != expected_type) {
1147 seek_relative(fp, -2);
1148 break;
1149 }
1150 {
1151 int slen, rlen;
1152 int i;
1153
1154 slen = 0;
1155 for (i = 0; i < 4; i++) {
1156 if ((ch = fgetc(fp)) < 0) {
1157 if (buffer)
1158 RELEASE(buffer);
1159 return NULL;
1160 }
1161 slen = slen + (ch << (8*i));
1162 }
1163 buffer = RENEW(buffer, bytesread + slen, unsigned char);
1164 while (slen > 0) {
1165 rlen = fread(buffer + bytesread, sizeof(unsigned char), slen, fp);
1166 if (rlen < 0) {
1167 if (buffer)
1168 RELEASE(buffer);
1169 return NULL;
1170 }
1171 slen -= rlen;
1172 bytesread += rlen;
1173 }
1174 }
1175 }
1176 if (bytesread == 0) {
1177 ERROR("PFB segment length zero?");
1178 }
1179
1180 buffer = RENEW(buffer, bytesread+1, unsigned char);
1181 buffer[bytesread] = 0;
1182
1183 if (length)
1184 *length = bytesread;
1185 return buffer;
1186 }
1187
1188 const char *
1189 t1_get_standard_glyph (int code)
1190 {
1191 if (!StandardEncoding[code])
1192 return NULL;
1193
1194 return StandardEncoding[code];
1195 }
1196
1197 int
1198 t1_get_fontname (FILE *fp, char *fontname)
1199 {
1200 unsigned char *buffer, *start, *end;
1201 int length;
1202 char *key;
1203 int fn_found = 0;
1204
1205 rewind(fp);
1206 buffer = get_pfb_segment(fp, PFB_SEG_TYPE_ASCII, &length);
1207 if (buffer == NULL || length == 0)
1208 ERROR("Reading PFB (ASCII part) file failed.");
1209 start = buffer;
1210 end = buffer + length;
1211
1212 if (seek_operator(&start, end, "begin") < 0) {
1213 RELEASE(buffer);
1214 return -1;
1215 }
1216
1217 while (!fn_found && start < end &&
1218 (key = get_next_key(&start, end)) != NULL) {
1219 if (!strcmp(key, "FontName")) {
1220 char *strval;
1221 if (parse_svalue(&start, end, &strval) == 1) {
1222 if (strlen(strval) > TYPE1_NAME_LEN_MAX) {
1223 WARN("FontName \"%s\" too long. (%d bytes)", strval, strlen(strval));
1224 strval[TYPE1_NAME_LEN_MAX] = '\0';
1225 }
1226 strcpy(fontname, strval);
1227 RELEASE(strval);
1228 fn_found = 1;
1229 }
1230 }
1231 RELEASE(key);
1232 }
1233 RELEASE(buffer);
1234
1235 return 0;
1236 }
1237
1238 static void
1239 init_cff_font (cff_font *cff)
1240 {
1241 cff->stream = NULL;
1242 cff->filter = 0;
1243 cff->fontname = NULL;
1244 cff->index = 0;
1245 cff->flag = FONTTYPE_FONT;
1246
1247 cff->header.major = 1;
1248 cff->header.minor = 0;
1249 cff->header.hdr_size = 4;
1250 cff->header.offsize = 4;
1251 cff->name = cff_new_index(1);
1252 cff->topdict = cff_new_dict();
1253 cff->string = NULL;
1254 cff->gsubr = cff_new_index(0); /* No Global Subr */
1255 cff->encoding = NULL;
1256 cff->charsets = NULL;
1257 cff->fdselect = NULL;
1258 cff->cstrings = NULL;
1259 cff->fdarray = NULL;
1260 cff->private = NEW(1, cff_dict *);
1261 cff->private[0] = cff_new_dict();
1262 cff->subrs = NEW(1, cff_index *);
1263 cff->subrs[0] = NULL;
1264
1265 cff->offset = 0;
1266 cff->gsubr_offset = 0;
1267 cff->num_glyphs = 0;
1268 cff->num_fds = 1;
1269 cff->_string = cff_new_index(0);
1270 }
1271
1272 cff_font *
1273 t1_load_font (char **enc_vec, int mode, FILE *fp)
1274 {
1275 int length;
1276 cff_font *cff;
1277 unsigned char *buffer, *start, *end;
1278
1279 rewind(fp);
1280 /* ASCII section */
1281 buffer = get_pfb_segment(fp, PFB_SEG_TYPE_ASCII, &length);
1282 if (buffer == NULL || length == 0) {
1283 ERROR("Reading PFB (ASCII part) file failed.");
1284 return NULL;
1285 }
1286
1287 cff = NEW(1, cff_font);
1288 init_cff_font(cff);
1289
1290 start = buffer; end = buffer + length;
1291 if (parse_part1(cff, enc_vec, &start, end) < 0) {
1292 cff_close(cff);
1293 RELEASE(buffer);
1294 ERROR("Reading PFB (ASCII part) file failed.");
1295 return NULL;
1296 }
1297 RELEASE(buffer);
1298
1299 /* Binary section */
1300 buffer = get_pfb_segment(fp, PFB_SEG_TYPE_BINARY, &length);
1301 if (buffer == NULL || length == 0) {
1302 cff_close(cff);
1303 RELEASE(buffer);
1304 ERROR("Reading PFB (BINARY part) file failed.");
1305 return NULL;
1306 } else {
1307 t1_decrypt(T1_EEKEY, buffer, buffer, 0, length);
1308 }
1309 start = buffer + 4; end = buffer + length;
1310 if (parse_part2(cff, &start, end, mode) < 0) {
1311 cff_close(cff);
1312 RELEASE(buffer);
1313 ERROR("Reading PFB (BINARY part) file failed.");
1314 return NULL;
1315 }
1316 RELEASE(buffer);
1317
1318 cff_update_string(cff);
1319
1320 /* Remaining section ignored. */
1321
1322 return cff;
1323 }