python-escpos/examples/codepage_tables.py

71 lines
1.3 KiB
Python
Raw Normal View History

2016-08-26 09:49:42 +00:00
"""Prints code page tables.
"""
2017-01-30 02:00:23 +00:00
2016-08-26 09:49:42 +00:00
import six
import sys
from escpos import printer
2021-10-30 16:15:22 +00:00
from escpos.constants import (
CODEPAGE_CHANGE,
ESC,
CTL_LF,
CTL_FF,
CTL_CR,
CTL_HT,
CTL_VT,
)
2016-08-26 09:49:42 +00:00
def main():
dummy = printer.Dummy()
2021-10-30 16:15:22 +00:00
dummy.hw("init")
2016-08-26 09:49:42 +00:00
2021-10-30 16:15:22 +00:00
for codepage in sys.argv[1:] or ["USA"]:
2016-08-26 09:49:42 +00:00
dummy.set(height=2, width=2)
2017-01-29 22:39:26 +00:00
dummy._raw(codepage + "\n\n\n")
2016-08-26 09:49:42 +00:00
print_codepage(dummy, codepage)
dummy._raw("\n\n")
dummy.cut()
2017-01-30 02:00:23 +00:00
print(dummy.output)
2016-08-26 09:49:42 +00:00
def print_codepage(printer, codepage):
if codepage.isdigit():
codepage = int(codepage)
printer._raw(CODEPAGE_CHANGE + six.int2byte(codepage))
printer._raw("after")
else:
printer.charcode(codepage)
sep = ""
# Table header
2021-10-30 16:15:22 +00:00
printer.set(font="b")
2017-01-29 22:39:26 +00:00
printer._raw(" {}\n".format(sep.join(map(lambda s: hex(s)[2:], range(0, 16)))))
2016-08-26 09:49:42 +00:00
printer.set()
# The table
2017-01-29 22:39:26 +00:00
for x in range(0, 16):
2016-08-26 09:49:42 +00:00
# First column
2021-10-30 16:15:22 +00:00
printer.set(font="b")
2017-01-29 22:39:26 +00:00
printer._raw("{} ".format(hex(x)[2:]))
2016-08-26 09:49:42 +00:00
printer.set()
2017-01-29 23:10:14 +00:00
for y in range(0, 16):
byte = six.int2byte(x * 16 + y)
2016-08-26 09:49:42 +00:00
if byte in (ESC, CTL_LF, CTL_FF, CTL_CR, CTL_HT, CTL_VT):
2021-10-30 16:15:22 +00:00
byte = " "
2016-08-26 09:49:42 +00:00
printer._raw(byte)
printer._raw(sep)
2021-10-30 16:15:22 +00:00
printer._raw("\n")
2016-08-26 09:49:42 +00:00
2017-01-29 23:10:14 +00:00
2021-10-30 16:15:22 +00:00
if __name__ == "__main__":
2017-01-29 23:10:14 +00:00
main()