charset.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # Mapping from LCD source encoding to unicode characters
  2. CUSTOM_CHARS = {
  3. '\x06': '⏬',
  4. '\x04': '🔃',
  5. '\xdf': '°',
  6. '\xe1': 'ä',
  7. '\xe4': 'µ', #on keyboard AltGr+m it is \xC2\xB5
  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. # sources
  16. # https://en.wikipedia.org/wiki/Czech_orthography
  17. # https://en.wikipedia.org/wiki/German_orthography
  18. # https://en.wikipedia.org/wiki/French_orthography
  19. # https://en.wikipedia.org/wiki/Spanish_orthography
  20. # https://en.wikipedia.org/wiki/Italian_orthography
  21. # https://en.wikipedia.org/wiki/Polish_alphabet
  22. # https://en.wikipedia.org/wiki/Dutch_orthography
  23. # https://en.wikipedia.org/wiki/Romanian_alphabet
  24. # https://en.wikipedia.org/wiki/Hungarian_alphabet
  25. # https://en.wikipedia.org/wiki/Gaj%27s_Latin_alphabet
  26. # https://en.wikipedia.org/wiki/Slovak_orthography
  27. # https://en.wikipedia.org/wiki/Swedish_alphabet
  28. # https://en.wikipedia.org/wiki/Norwegian_orthography
  29. TRANS_CHARS = {
  30. 'á': 'a', #cz,fr,es,hu,sk
  31. 'Á': 'A', #cz,fr,hu,sk
  32. 'à': 'a', #fr,it
  33. 'À': 'A', #fr,it
  34. 'â': 'a', #fr,ro
  35. 'Â': 'A', #ro
  36. 'Ä': 'ä', #de,sv,no,sk
  37. 'å': 'a', #sv,no
  38. 'Å': 'A', #sv,no
  39. 'æ': 'ä', #sv,no
  40. 'ą': 'a', #pl
  41. 'Ą': 'A', #pl
  42. 'ă': 'a', #ro
  43. 'Ă': 'A', #ro
  44. 'ć': 'c', #pl,hr
  45. 'Ć': 'C', #pl,hr
  46. 'ç': 'c', #fr,nl
  47. 'č': 'c', #cz,hr,sk
  48. 'Č': 'C', #cz,hr,sk
  49. 'ď': 'd', #cz,sk
  50. 'Ď': 'D', #cz,sk
  51. 'đ': 'd', #hr
  52. 'Đ': 'D', #hr
  53. 'é': 'e', #cz,fr,es,it,nl,hu,sk
  54. 'É': 'E', #cz,fr,it,hu,sk
  55. 'è': 'e', #fr,it,nl
  56. 'È': 'E', #fr,it
  57. 'ê': 'e', #fr,nl
  58. 'ě': 'e', #cz
  59. 'ë': 'e', #fr
  60. 'Ě': 'E', #cz
  61. 'ę': 'e', #pl
  62. 'Ę': 'E', #pl
  63. 'í': 'i', #cz,es,it,sk
  64. 'Í': 'I', #cz,it,sk
  65. 'î': 'i', #fr,ro
  66. 'Î': 'I', #ro
  67. 'ĺ': 'l', #sk
  68. 'Ĺ': 'L', #sk
  69. 'ł': 'l', #pl
  70. 'Ł': 'L', #pl
  71. 'ľ': 'l', #sk
  72. 'Ľ': 'L', #sk
  73. 'ń': 'n', #pl
  74. 'Ń': 'N', #pl
  75. 'ň': 'n', #cz,sk
  76. 'Ň': 'N', #cz,sk
  77. 'ñ': 'n', #es,nl
  78. 'ó': 'o', #cz,es,pl,hu,sk
  79. 'Ó': 'O', #cz,pl,hu,sk
  80. 'ò': 'o', #it
  81. 'Ò': 'O', #it
  82. 'ô': 'o', #fr,nl,sk
  83. 'Ô': 'O', #sk
  84. 'œ': 'o', #fr
  85. 'ø': 'ö', #sv,no
  86. 'Ø': 'ö', #sv,no
  87. 'Ö': 'ö', #de,sv,no,hu
  88. 'ő': 'o', #hu
  89. 'Ő': 'O', #hu
  90. 'ŕ': 'r', #sk
  91. 'Ŕ': 'R', #sk
  92. 'ř': 'r', #cz
  93. 'Ř': 'R', #cz
  94. 'ś': 's', #pl
  95. 'Ś': 's', #pl
  96. 'š': 's', #cz,hr,sk
  97. 'Š': 'S', #cz,hr,sk
  98. 'ș': 's', #ro
  99. 'Ș': 'S', #ro
  100. 'ß': 'ss',#de
  101. 'ť': 't', #cz,sk
  102. 'Ť': 'T', #cz,sk
  103. 'ț': 't', #ro
  104. 'Ț': 'T', #ro
  105. 'ú': 'u', #cz,es,hu,sk
  106. 'Ú': 'U', #cz,hu,sk
  107. 'ù': 'u', #it
  108. 'Ù': 'U', #it
  109. 'û': 'u', #fr
  110. 'Ü': 'ü', #de,hu
  111. 'ů': 'u', #cz
  112. 'Ů': 'U', #cz
  113. 'ű': 'u', #hu
  114. 'Ű': 'U', #hu
  115. 'ý': 'y', #cz,sk
  116. 'Ý': 'Y', #cz,sk
  117. 'ÿ': 'y', #fr
  118. 'ź': 'z', #pl
  119. 'Ź': 'Z', #pl
  120. 'ž': 'z', #cz,hr,sk
  121. 'Ž': 'z', #cz,hr,sk
  122. 'ż': 'z', #pl
  123. 'Ż': 'Z', #pl
  124. '¿': '', #es
  125. '¡': '', #es
  126. }
  127. def _character_check(buf, valid_chars):
  128. for c in buf:
  129. if (not c.isascii() or not c.isprintable()) and c not in valid_chars:
  130. return c
  131. return None
  132. def source_check(buf):
  133. valid_chars = set(CUSTOM_CHARS.values())
  134. valid_chars.add('\n')
  135. return _character_check(buf, valid_chars)
  136. def translation_check(buf):
  137. valid_chars = set(CUSTOM_CHARS.keys())
  138. valid_chars.add('\n')
  139. return _character_check(buf, valid_chars)
  140. def source_to_unicode(buf):
  141. for src, dst in CUSTOM_CHARS.items():
  142. buf = buf.replace(src, dst)
  143. return buf
  144. def trans_replace(buf):
  145. for src, dst in TRANS_CHARS.items():
  146. buf = buf.replace(src, dst)
  147. return buf
  148. def unicode_to_source(buf):
  149. buf = trans_replace(buf)
  150. for dst, src in CUSTOM_CHARS.items():
  151. buf = buf.replace(src, dst)
  152. return buf