pdf_cmap_parse.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. #include "fitz-internal.h"
  2. #include "mupdf-internal.h"
  3. /*
  4. * CMap parser
  5. */
  6. enum
  7. {
  8. TOK_USECMAP = PDF_NUM_TOKENS,
  9. TOK_BEGIN_CODESPACE_RANGE,
  10. TOK_END_CODESPACE_RANGE,
  11. TOK_BEGIN_BF_CHAR,
  12. TOK_END_BF_CHAR,
  13. TOK_BEGIN_BF_RANGE,
  14. TOK_END_BF_RANGE,
  15. TOK_BEGIN_CID_CHAR,
  16. TOK_END_CID_CHAR,
  17. TOK_BEGIN_CID_RANGE,
  18. TOK_END_CID_RANGE,
  19. TOK_END_CMAP
  20. };
  21. static int
  22. pdf_cmap_token_from_keyword(char *key)
  23. {
  24. if (!strcmp(key, "usecmap")) return TOK_USECMAP;
  25. if (!strcmp(key, "begincodespacerange")) return TOK_BEGIN_CODESPACE_RANGE;
  26. if (!strcmp(key, "endcodespacerange")) return TOK_END_CODESPACE_RANGE;
  27. if (!strcmp(key, "beginbfchar")) return TOK_BEGIN_BF_CHAR;
  28. if (!strcmp(key, "endbfchar")) return TOK_END_BF_CHAR;
  29. if (!strcmp(key, "beginbfrange")) return TOK_BEGIN_BF_RANGE;
  30. if (!strcmp(key, "endbfrange")) return TOK_END_BF_RANGE;
  31. if (!strcmp(key, "begincidchar")) return TOK_BEGIN_CID_CHAR;
  32. if (!strcmp(key, "endcidchar")) return TOK_END_CID_CHAR;
  33. if (!strcmp(key, "begincidrange")) return TOK_BEGIN_CID_RANGE;
  34. if (!strcmp(key, "endcidrange")) return TOK_END_CID_RANGE;
  35. if (!strcmp(key, "endcmap")) return TOK_END_CMAP;
  36. return PDF_TOK_KEYWORD;
  37. }
  38. static int
  39. pdf_code_from_string(char *buf, int len)
  40. {
  41. int a = 0;
  42. while (len--)
  43. a = (a << 8) | *(unsigned char *)buf++;
  44. return a;
  45. }
  46. static int
  47. pdf_lex_cmap(fz_stream *file, pdf_lexbuf *buf)
  48. {
  49. pdf_token tok = pdf_lex(file, buf);
  50. if (tok == PDF_TOK_KEYWORD)
  51. tok = pdf_cmap_token_from_keyword(buf->scratch);
  52. return tok;
  53. }
  54. static void
  55. pdf_parse_cmap_name(fz_context *ctx, pdf_cmap *cmap, fz_stream *file, pdf_lexbuf *buf)
  56. {
  57. pdf_token tok;
  58. tok = pdf_lex_cmap(file, buf);
  59. if (tok == PDF_TOK_NAME)
  60. fz_strlcpy(cmap->cmap_name, buf->scratch, sizeof(cmap->cmap_name));
  61. else
  62. fz_warn(ctx, "expected name after CMapName in cmap");
  63. }
  64. static void
  65. pdf_parse_wmode(fz_context *ctx, pdf_cmap *cmap, fz_stream *file, pdf_lexbuf *buf)
  66. {
  67. pdf_token tok;
  68. tok = pdf_lex_cmap(file, buf);
  69. if (tok == PDF_TOK_INT)
  70. pdf_set_cmap_wmode(ctx, cmap, buf->i);
  71. else
  72. fz_warn(ctx, "expected integer after WMode in cmap");
  73. }
  74. static void
  75. pdf_parse_codespace_range(fz_context *ctx, pdf_cmap *cmap, fz_stream *file, pdf_lexbuf *buf)
  76. {
  77. pdf_token tok;
  78. int lo, hi;
  79. while (1)
  80. {
  81. tok = pdf_lex_cmap(file, buf);
  82. if (tok == TOK_END_CODESPACE_RANGE)
  83. return;
  84. else if (tok == PDF_TOK_STRING)
  85. {
  86. lo = pdf_code_from_string(buf->scratch, buf->len);
  87. tok = pdf_lex_cmap(file, buf);
  88. if (tok == PDF_TOK_STRING)
  89. {
  90. hi = pdf_code_from_string(buf->scratch, buf->len);
  91. pdf_add_codespace(ctx, cmap, lo, hi, buf->len);
  92. }
  93. else break;
  94. }
  95. else break;
  96. }
  97. fz_throw(ctx, "expected string or endcodespacerange");
  98. }
  99. static void
  100. pdf_parse_cid_range(fz_context *ctx, pdf_cmap *cmap, fz_stream *file, pdf_lexbuf *buf)
  101. {
  102. pdf_token tok;
  103. int lo, hi, dst;
  104. while (1)
  105. {
  106. tok = pdf_lex_cmap(file, buf);
  107. if (tok == TOK_END_CID_RANGE)
  108. return;
  109. else if (tok != PDF_TOK_STRING)
  110. fz_throw(ctx, "expected string or endcidrange");
  111. lo = pdf_code_from_string(buf->scratch, buf->len);
  112. tok = pdf_lex_cmap(file, buf);
  113. if (tok != PDF_TOK_STRING)
  114. fz_throw(ctx, "expected string");
  115. hi = pdf_code_from_string(buf->scratch, buf->len);
  116. tok = pdf_lex_cmap(file, buf);
  117. if (tok != PDF_TOK_INT)
  118. fz_throw(ctx, "expected integer");
  119. dst = buf->i;
  120. pdf_map_range_to_range(ctx, cmap, lo, hi, dst);
  121. }
  122. }
  123. static void
  124. pdf_parse_cid_char(fz_context *ctx, pdf_cmap *cmap, fz_stream *file, pdf_lexbuf *buf)
  125. {
  126. pdf_token tok;
  127. int src, dst;
  128. while (1)
  129. {
  130. tok = pdf_lex_cmap(file, buf);
  131. if (tok == TOK_END_CID_CHAR)
  132. return;
  133. else if (tok != PDF_TOK_STRING)
  134. fz_throw(ctx, "expected string or endcidchar");
  135. src = pdf_code_from_string(buf->scratch, buf->len);
  136. tok = pdf_lex_cmap(file, buf);
  137. if (tok != PDF_TOK_INT)
  138. fz_throw(ctx, "expected integer");
  139. dst = buf->i;
  140. pdf_map_range_to_range(ctx, cmap, src, src, dst);
  141. }
  142. }
  143. static void
  144. pdf_parse_bf_range_array(fz_context *ctx, pdf_cmap *cmap, fz_stream *file, pdf_lexbuf *buf, int lo, int hi)
  145. {
  146. pdf_token tok;
  147. int dst[256];
  148. int i;
  149. while (1)
  150. {
  151. tok = pdf_lex_cmap(file, buf);
  152. if (tok == PDF_TOK_CLOSE_ARRAY)
  153. return;
  154. /* Note: does not handle [ /Name /Name ... ] */
  155. else if (tok != PDF_TOK_STRING)
  156. fz_throw(ctx, "expected string or ]");
  157. if (buf->len / 2)
  158. {
  159. int len = fz_mini(buf->len / 2, nelem(dst));
  160. for (i = 0; i < len; i++)
  161. dst[i] = pdf_code_from_string(&buf->scratch[i * 2], 2);
  162. pdf_map_one_to_many(ctx, cmap, lo, dst, buf->len / 2);
  163. }
  164. lo ++;
  165. }
  166. }
  167. static void
  168. pdf_parse_bf_range(fz_context *ctx, pdf_cmap *cmap, fz_stream *file, pdf_lexbuf *buf)
  169. {
  170. pdf_token tok;
  171. int lo, hi, dst;
  172. while (1)
  173. {
  174. tok = pdf_lex_cmap(file, buf);
  175. if (tok == TOK_END_BF_RANGE)
  176. return;
  177. else if (tok != PDF_TOK_STRING)
  178. fz_throw(ctx, "expected string or endbfrange");
  179. lo = pdf_code_from_string(buf->scratch, buf->len);
  180. tok = pdf_lex_cmap(file, buf);
  181. if (tok != PDF_TOK_STRING)
  182. fz_throw(ctx, "expected string");
  183. hi = pdf_code_from_string(buf->scratch, buf->len);
  184. if (lo < 0 || lo > 65535 || hi < 0 || hi > 65535 || lo > hi)
  185. {
  186. fz_warn(ctx, "bf_range limits out of range in cmap %s", cmap->cmap_name);
  187. return;
  188. }
  189. tok = pdf_lex_cmap(file, buf);
  190. if (tok == PDF_TOK_STRING)
  191. {
  192. if (buf->len == 2)
  193. {
  194. dst = pdf_code_from_string(buf->scratch, buf->len);
  195. pdf_map_range_to_range(ctx, cmap, lo, hi, dst);
  196. }
  197. else
  198. {
  199. int dststr[256];
  200. int i;
  201. if (buf->len / 2)
  202. {
  203. int len = fz_mini(buf->len / 2, nelem(dststr));
  204. for (i = 0; i < len; i++)
  205. dststr[i] = pdf_code_from_string(&buf->scratch[i * 2], 2);
  206. while (lo <= hi)
  207. {
  208. dststr[i-1] ++;
  209. pdf_map_one_to_many(ctx, cmap, lo, dststr, i);
  210. lo ++;
  211. }
  212. }
  213. }
  214. }
  215. else if (tok == PDF_TOK_OPEN_ARRAY)
  216. {
  217. pdf_parse_bf_range_array(ctx, cmap, file, buf, lo, hi);
  218. }
  219. else
  220. {
  221. fz_throw(ctx, "expected string or array or endbfrange");
  222. }
  223. }
  224. }
  225. static void
  226. pdf_parse_bf_char(fz_context *ctx, pdf_cmap *cmap, fz_stream *file, pdf_lexbuf *buf)
  227. {
  228. pdf_token tok;
  229. int dst[256];
  230. int src;
  231. int i;
  232. while (1)
  233. {
  234. tok = pdf_lex_cmap(file, buf);
  235. if (tok == TOK_END_BF_CHAR)
  236. return;
  237. else if (tok != PDF_TOK_STRING)
  238. fz_throw(ctx, "expected string or endbfchar");
  239. src = pdf_code_from_string(buf->scratch, buf->len);
  240. tok = pdf_lex_cmap(file, buf);
  241. /* Note: does not handle /dstName */
  242. if (tok != PDF_TOK_STRING)
  243. fz_throw(ctx, "expected string");
  244. if (buf->len / 2)
  245. {
  246. int len = fz_mini(buf->len / 2, nelem(dst));
  247. for (i = 0; i < len; i++)
  248. dst[i] = pdf_code_from_string(&buf->scratch[i * 2], 2);
  249. pdf_map_one_to_many(ctx, cmap, src, dst, i);
  250. }
  251. }
  252. }
  253. pdf_cmap *
  254. pdf_load_cmap(fz_context *ctx, fz_stream *file)
  255. {
  256. pdf_cmap *cmap;
  257. char key[64];
  258. pdf_lexbuf buf;
  259. pdf_token tok;
  260. const char *where;
  261. pdf_lexbuf_init(ctx, &buf, PDF_LEXBUF_SMALL);
  262. cmap = pdf_new_cmap(ctx);
  263. strcpy(key, ".notdef");
  264. fz_var(where);
  265. fz_try(ctx)
  266. {
  267. while (1)
  268. {
  269. where = "";
  270. tok = pdf_lex_cmap(file, &buf);
  271. if (tok == PDF_TOK_EOF || tok == TOK_END_CMAP)
  272. break;
  273. else if (tok == PDF_TOK_NAME)
  274. {
  275. if (!strcmp(buf.scratch, "CMapName"))
  276. {
  277. where = " after CMapName";
  278. pdf_parse_cmap_name(ctx, cmap, file, &buf);
  279. }
  280. else if (!strcmp(buf.scratch, "WMode"))
  281. {
  282. where = " after WMode";
  283. pdf_parse_wmode(ctx, cmap, file, &buf);
  284. }
  285. else
  286. fz_strlcpy(key, buf.scratch, sizeof key);
  287. }
  288. else if (tok == TOK_USECMAP)
  289. {
  290. fz_strlcpy(cmap->usecmap_name, key, sizeof(cmap->usecmap_name));
  291. }
  292. else if (tok == TOK_BEGIN_CODESPACE_RANGE)
  293. {
  294. where = " codespacerange";
  295. pdf_parse_codespace_range(ctx, cmap, file, &buf);
  296. }
  297. else if (tok == TOK_BEGIN_BF_CHAR)
  298. {
  299. where = " bfchar";
  300. pdf_parse_bf_char(ctx, cmap, file, &buf);
  301. }
  302. else if (tok == TOK_BEGIN_CID_CHAR)
  303. {
  304. where = " cidchar";
  305. pdf_parse_cid_char(ctx, cmap, file, &buf);
  306. }
  307. else if (tok == TOK_BEGIN_BF_RANGE)
  308. {
  309. where = " bfrange";
  310. pdf_parse_bf_range(ctx, cmap, file, &buf);
  311. }
  312. else if (tok == TOK_BEGIN_CID_RANGE)
  313. {
  314. where = "cidrange";
  315. pdf_parse_cid_range(ctx, cmap, file, &buf);
  316. }
  317. /* ignore everything else */
  318. }
  319. pdf_sort_cmap(ctx, cmap);
  320. }
  321. fz_always(ctx)
  322. {
  323. pdf_lexbuf_fin(&buf);
  324. }
  325. fz_catch(ctx)
  326. {
  327. pdf_drop_cmap(ctx, cmap);
  328. fz_throw(ctx, "syntaxerror in cmap%s", where);
  329. }
  330. return cmap;
  331. }