msx2utf8.py 1.9 KB

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