pdf_unicode.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "fitz-internal.h"
  2. #include "mupdf-internal.h"
  3. /* Load or synthesize ToUnicode map for fonts */
  4. void
  5. pdf_load_to_unicode(pdf_document *xref, pdf_font_desc *font,
  6. char **strings, char *collection, pdf_obj *cmapstm)
  7. {
  8. pdf_cmap *cmap;
  9. int cid;
  10. int ucsbuf[8];
  11. int ucslen;
  12. int i;
  13. fz_context *ctx = xref->ctx;
  14. if (pdf_is_stream(xref, pdf_to_num(cmapstm), pdf_to_gen(cmapstm)))
  15. {
  16. cmap = pdf_load_embedded_cmap(xref, cmapstm);
  17. font->to_unicode = pdf_new_cmap(ctx);
  18. for (i = 0; i < (strings ? 256 : 65536); i++)
  19. {
  20. cid = pdf_lookup_cmap(font->encoding, i);
  21. if (cid >= 0)
  22. {
  23. ucslen = pdf_lookup_cmap_full(cmap, i, ucsbuf);
  24. if (ucslen == 1)
  25. pdf_map_range_to_range(ctx, font->to_unicode, cid, cid, ucsbuf[0]);
  26. if (ucslen > 1)
  27. pdf_map_one_to_many(ctx, font->to_unicode, cid, ucsbuf, ucslen);
  28. }
  29. }
  30. pdf_sort_cmap(ctx, font->to_unicode);
  31. pdf_drop_cmap(ctx, cmap);
  32. font->size += pdf_cmap_size(ctx, font->to_unicode);
  33. }
  34. else if (collection)
  35. {
  36. if (!strcmp(collection, "Adobe-CNS1"))
  37. font->to_unicode = pdf_load_system_cmap(ctx, "Adobe-CNS1-UCS2");
  38. else if (!strcmp(collection, "Adobe-GB1"))
  39. font->to_unicode = pdf_load_system_cmap(ctx, "Adobe-GB1-UCS2");
  40. else if (!strcmp(collection, "Adobe-Japan1"))
  41. font->to_unicode = pdf_load_system_cmap(ctx, "Adobe-Japan1-UCS2");
  42. else if (!strcmp(collection, "Adobe-Korea1"))
  43. font->to_unicode = pdf_load_system_cmap(ctx, "Adobe-Korea1-UCS2");
  44. return;
  45. }
  46. if (strings)
  47. {
  48. /* TODO one-to-many mappings */
  49. font->cid_to_ucs_len = 256;
  50. font->cid_to_ucs = fz_malloc_array(ctx, 256, sizeof(unsigned short));
  51. font->size += 256 * sizeof(unsigned short);
  52. for (i = 0; i < 256; i++)
  53. {
  54. if (strings[i])
  55. font->cid_to_ucs[i] = pdf_lookup_agl(strings[i]);
  56. else
  57. font->cid_to_ucs[i] = '?';
  58. }
  59. }
  60. if (!font->to_unicode && !font->cid_to_ucs)
  61. {
  62. /* TODO: synthesize a ToUnicode if it's a freetype font with
  63. * cmap and/or post tables or if it has glyph names. */
  64. }
  65. }