PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/TeXmacs-1.0.7.11-src/src/Plugins/Pdf/dvipdfmx/t1_load.c

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