python-escpos/src/escpos/katakana.py

102 lines
2.4 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""Helpers to encode Japanese characters.
I doubt that this currently works correctly.
"""
try:
import jaconv
except ImportError:
jaconv = None
def encode_katakana(text):
"""I don't think this quite works yet."""
encoded = []
for char in text:
if jaconv:
# try to convert japanese text to half-katakanas
char = jaconv.z2h(jaconv.hira2kata(char))
# TODO: "the conversion may result in multiple characters"
# If that really can happen (I am not really shure), than the string would have to be split and every single
# character has to passed through the following lines.
if char in TXT_ENC_KATAKANA_MAP:
encoded.append(TXT_ENC_KATAKANA_MAP[char])
else:
2017-01-29 23:10:14 +00:00
# TODO doesn't this discard all that is not in the map? Can we be sure that the input does contain only
# encodable characters? We could at least throw an exception if encoding is not possible.
pass
return b"".join(encoded)
TXT_ENC_KATAKANA_MAP = {
# Maps UTF-8 Katakana symbols to KATAKANA Page Codes
# TODO: has this really to be hardcoded?
# Half-Width Katakanas
2021-10-30 16:15:22 +00:00
"": b"\xa1",
"": b"\xa2",
"": b"\xa3",
"": b"\xa4",
"": b"\xa5",
"": b"\xa6",
"": b"\xa7",
"": b"\xa8",
"": b"\xa9",
"": b"\xaa",
"": b"\xab",
"": b"\xac",
"": b"\xad",
"": b"\xae",
"": b"\xaf",
"": b"\xb0",
"": b"\xb1",
"": b"\xb2",
"": b"\xb3",
"": b"\xb4",
"": b"\xb5",
"": b"\xb6",
"": b"\xb7",
"": b"\xb8",
"": b"\xb9",
"": b"\xba",
"": b"\xbb",
"": b"\xbc",
"": b"\xbd",
"": b"\xbe",
"ソ": b"\xbf",
"": b"\xc0",
"": b"\xc1",
"": b"\xc2",
"": b"\xc3",
"": b"\xc4",
"": b"\xc5",
"": b"\xc6",
"": b"\xc7",
"": b"\xc8",
"": b"\xc9",
"": b"\xca",
"": b"\xcb",
"": b"\xcc",
"": b"\xcd",
"": b"\xce",
"": b"\xcf",
"": b"\xd0",
"": b"\xd1",
"": b"\xd2",
"": b"\xd3",
"": b"\xd4",
"": b"\xd5",
"": b"\xd6",
"": b"\xd7",
"": b"\xd8",
"": b"\xd9",
"": b"\xda",
"": b"\xdb",
"": b"\xdc",
"": b"\xdd",
"": b"\xde",
"": b"\xdf",
}