charset.py 4.0 KB

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