You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.4 KiB

4 years ago
  1. import re
  2. from .psparser import PSLiteral
  3. from .glyphlist import glyphname2unicode
  4. from .latin_enc import ENCODING
  5. STRIP_NAME = re.compile(r'[0-9]+')
  6. def name2unicode(name):
  7. """Converts Adobe glyph names to Unicode numbers."""
  8. if name in glyphname2unicode:
  9. return glyphname2unicode[name]
  10. m = STRIP_NAME.search(name)
  11. if not m: raise KeyError(name)
  12. return chr(int(m.group(0)))
  13. class EncodingDB:
  14. std2unicode = {}
  15. mac2unicode = {}
  16. win2unicode = {}
  17. pdf2unicode = {}
  18. for (name,std,mac,win,pdf) in ENCODING:
  19. c = name2unicode(name)
  20. if std: std2unicode[std] = c
  21. if mac: mac2unicode[mac] = c
  22. if win: win2unicode[win] = c
  23. if pdf: pdf2unicode[pdf] = c
  24. encodings = {
  25. 'StandardEncoding': std2unicode,
  26. 'MacRomanEncoding': mac2unicode,
  27. 'WinAnsiEncoding': win2unicode,
  28. 'PDFDocEncoding': pdf2unicode,
  29. }
  30. @classmethod
  31. def get_encoding(cls, name, diff=None):
  32. cid2unicode = cls.encodings.get(name, cls.std2unicode)
  33. if diff:
  34. cid2unicode = cid2unicode.copy()
  35. cid = 0
  36. for x in diff:
  37. if isinstance(x, int):
  38. cid = x
  39. elif isinstance(x, PSLiteral):
  40. try:
  41. cid2unicode[cid] = name2unicode(x.name)
  42. except KeyError:
  43. pass
  44. cid += 1
  45. return cid2unicode