msx-font/msx2utf8.py

66 lines
1.9 KiB
Python
Executable File

#!/usr/bin/env python3
# -*- coding: utf8 -*-
import sys
from unicodedata import name
chars = (r""" !"#$%&'()*+,-./0123456789:;<=>?""" +
r"""@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_""" +
r"""`abcdefghijklmnopqrstuvwxyz{|}~ """ +
r"""ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒ""" +
r"""áíóúñѪº¿⌐¬½¼¡«»ÃãĨĩÕõŨũIJij¾∽◇‰¶§""" +
r"""▂▚▆▔◾▇▎▞▊▕▉▨▧▼▲▶◀⧗⧓▘▗▝▖▒Δǂω█▄▌▐▀""" +
r"""αβΓπΣσμτΦθΩδ∞φ∈∩≡±≥≤⌠⌡÷≈°∙‐√ⁿ²❚■""" +
r""" ☺☻♥♦♣♠·◘○◙♂♀♪♬☼╂┻┳┫┣╋┃━┏┓┗┛╳╱╲┿""")
def msxchars():
"""
yields tuples in the form (msx code, unicode character)
"""
for i in range(32):
yield (i, chr(i))
for i in range(32,256):
if i == 127:
yield (i, chr(i))
else:
yield (i, chars[i - 32])
for i in range(65,96):
yield (256 + i, chars[160 + i])
msx2uni = {}
uni2msx = {}
for m,c in msxchars():
if m in msx2uni:
raise ValueError("MSX code {} is duplicated".format(m))
msx2uni[m] = c
if c in uni2msx:
raise ValueError("Codepoint {} ({}) is duplicated"
.format(ord(c), c))
uni2msx[c] = m
if len(sys.argv) < 2:
while True:
b = sys.stdin.buffer.read(1)
if len(b) == 0:
break
code = b[0]
if code == 1:
c = sys.stdin.buffer.read(1)
if len(b) == 0:
sys.stdout.write('\x01')
break
code = 256 + c[0]
char = msx2uni[code]
sys.stdout.write(char)
if sys.argv[1] == '-l':
for m,c in msxchars():
if m < 32 or m == 127:
continue
try:
n = name(c)
except ValueError:
n = ""
print(m,ord(c),c,n)