encodingTools.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """fontTools.misc.encodingTools.py -- tools for working with OpenType encodings.
  2. """
  3. import fontTools.encodings.codecs
  4. # Map keyed by platformID, then platEncID, then possibly langID
  5. _encodingMap = {
  6. 0: { # Unicode
  7. 0: "utf_16_be",
  8. 1: "utf_16_be",
  9. 2: "utf_16_be",
  10. 3: "utf_16_be",
  11. 4: "utf_16_be",
  12. 5: "utf_16_be",
  13. 6: "utf_16_be",
  14. },
  15. 1: { # Macintosh
  16. # See
  17. # https://github.com/fonttools/fonttools/issues/236
  18. 0: { # Macintosh, platEncID==0, keyed by langID
  19. 15: "mac_iceland",
  20. 17: "mac_turkish",
  21. 18: "mac_croatian",
  22. 24: "mac_latin2",
  23. 25: "mac_latin2",
  24. 26: "mac_latin2",
  25. 27: "mac_latin2",
  26. 28: "mac_latin2",
  27. 36: "mac_latin2",
  28. 37: "mac_romanian",
  29. 38: "mac_latin2",
  30. 39: "mac_latin2",
  31. 40: "mac_latin2",
  32. Ellipsis: "mac_roman", # Other
  33. },
  34. 1: "x_mac_japanese_ttx",
  35. 2: "x_mac_trad_chinese_ttx",
  36. 3: "x_mac_korean_ttx",
  37. 6: "mac_greek",
  38. 7: "mac_cyrillic",
  39. 25: "x_mac_simp_chinese_ttx",
  40. 29: "mac_latin2",
  41. 35: "mac_turkish",
  42. 37: "mac_iceland",
  43. },
  44. 2: { # ISO
  45. 0: "ascii",
  46. 1: "utf_16_be",
  47. 2: "latin1",
  48. },
  49. 3: { # Microsoft
  50. 0: "utf_16_be",
  51. 1: "utf_16_be",
  52. 2: "shift_jis",
  53. 3: "gb2312",
  54. 4: "big5",
  55. 5: "euc_kr",
  56. 6: "johab",
  57. 10: "utf_16_be",
  58. },
  59. }
  60. def getEncoding(platformID, platEncID, langID, default=None):
  61. """Returns the Python encoding name for OpenType platformID/encodingID/langID
  62. triplet. If encoding for these values is not known, by default None is
  63. returned. That can be overriden by passing a value to the default argument.
  64. """
  65. encoding = _encodingMap.get(platformID, {}).get(platEncID, default)
  66. if isinstance(encoding, dict):
  67. encoding = encoding.get(langID, encoding[Ellipsis])
  68. return encoding