charset.py 1.5 KB

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