From 9fbdbb3c7bc4753491e1d497940447096cd654c8 Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Thu, 28 Sep 2023 19:35:43 +0200 Subject: [PATCH] Fix charset conversion --- lang/lib/FontGen.py | 2 ++ lang/lib/charset.py | 23 +++++++++++++---------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lang/lib/FontGen.py b/lang/lib/FontGen.py index 1df3f4394..e37f70764 100644 --- a/lang/lib/FontGen.py +++ b/lang/lib/FontGen.py @@ -91,6 +91,8 @@ for index in range(len(FONT_TABLE)): CUSTOM_CHARS.update({chr(index + 0x80): FONT_TABLE[index].utf}) CUSTOM_CHARS.update(BUILTIN_CHARS) +INVERSE_CUSTOM_CHARS = {v: k for k, v in CUSTOM_CHARS.items()} + def generateLineInTable(index, chars): pixels = chars[FONT_TABLE[index].charListIndex]["PIXELS"].split(',') diff --git a/lang/lib/charset.py b/lang/lib/charset.py index 4e03fa38a..0171ea145 100644 --- a/lang/lib/charset.py +++ b/lang/lib/charset.py @@ -1,4 +1,4 @@ -from .FontGen import CUSTOM_CHARS +from .FontGen import CUSTOM_CHARS, INVERSE_CUSTOM_CHARS # Charaters to be remapped prior to source-encoding transformation # This transformation is applied to the translation prior to being converted to the final encoding, @@ -141,19 +141,22 @@ def translation_check(buf): valid_chars.add('\n') return _character_check(buf, valid_chars) - -def source_to_unicode(buf): - for src, dst in CUSTOM_CHARS.items(): - buf = buf.replace(src, dst) - return buf - def trans_replace(buf): for src, dst in TRANS_CHARS.items(): buf = buf.replace(src, dst) return buf +def source_to_unicode(buf): + buf = trans_replace(buf) + out = u'' + for c in buf: + out += CUSTOM_CHARS.get(c, c) + return out + def unicode_to_source(buf): buf = trans_replace(buf) - for dst, src in CUSTOM_CHARS.items(): - buf = buf.replace(src, dst) - return buf + out = '' + for c in buf: + out += INVERSE_CUSTOM_CHARS.get(c, c) + return out +