charset.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Mapping from LCD source encoding to unicode characters
  2. CUSTOM_CHARS = {
  3. '\x06': '⏬',
  4. '\x04': '🔃',
  5. '\xe4': 'µ',
  6. '\xdf': '°',
  7. '\xe1': 'ä',
  8. '\xe4': 'μ',
  9. '\xef': 'ö',
  10. '\xf5': 'ü',
  11. }
  12. # Charaters to be remapped prior to source-encoding transformation
  13. # This transformation is applied to the translation prior to being converted to the final encoding,
  14. # and maps UTF8 to UTF8. It replaces unavailable symbols in the translation to a close
  15. # representation in the source encoding.
  16. TRANS_CHARS = {
  17. 'Ä': 'ä',
  18. 'Å': 'A',
  19. 'Ö': 'ö',
  20. 'Ü': 'ü',
  21. 'å': 'a',
  22. 'æ': 'ä',
  23. 'ø': 'ö',
  24. 'ß': 'ss',
  25. }
  26. def _character_check(buf, valid_chars):
  27. for c in buf:
  28. if (not c.isascii() or not c.isprintable()) and c not in valid_chars:
  29. return c
  30. return None
  31. def source_check(buf):
  32. valid_chars = set(CUSTOM_CHARS.values())
  33. valid_chars.add('\n')
  34. return _character_check(buf, valid_chars)
  35. def translation_check(buf):
  36. valid_chars = set(CUSTOM_CHARS.keys())
  37. valid_chars.add('\n')
  38. return _character_check(buf, valid_chars)
  39. def source_to_unicode(buf):
  40. for src, dst in CUSTOM_CHARS.items():
  41. buf = buf.replace(src, dst)
  42. return buf
  43. def trans_replace(buf):
  44. for src, dst in TRANS_CHARS.items():
  45. buf = buf.replace(src, dst)
  46. return buf
  47. def unicode_to_source(buf):
  48. buf = trans_replace(buf)
  49. for dst, src in CUSTOM_CHARS.items():
  50. buf = buf.replace(src, dst)
  51. return buf