from typing import TextIO from sys import exit def convert_to_single_line(in_file: str, out_file: str): with open(in_file, 'r', encoding='utf8') as in_stream: lines = in_stream.readlines() with open(out_file, 'w', encoding='utf8') as out_stream: for i in range(0, len(lines), 4): for ch in lines[i].strip(): if ch != ' ': generate_line(out_stream, ch, lines[i+2].strip(), lines[i+3].strip()) def generate_line(out_stream: TextIO, ch: str, dots: str, comment: str): unicode_dots = dot_string_to_unicode(dots) if unicode_dots=='': print(f"ERROR: comment is {comment}") exit() out_stream.write(f""" - "{ch}": [t: "{unicode_dots}"] # {f'0x{ord(ch):04X}'} ({comment})\n""") def dot_string_to_unicode(dots: str) -> str: dots = dots.split('-') answer = '' for ch in dots: answer += dots_to_unicode( ch.replace('0d', '0') ) return answer def dots_to_unicode(dots: str) -> str: try: dots = int(dots) except: print(f"dots='{dots}' can't be converted to an int") return '' answer = 0 while dots > 0: digit = dots % 10 dots = (dots-digit)//10 answer += pow(2, digit-1) return chr(0x2800+answer) convert_to_single_line("cmu-chars.txt", "out")