python-escpos/examples/codepage_tables.py

73 lines
1.4 KiB
Python
Raw Permalink Normal View History

"""Prints code page tables."""
2016-08-26 09:49:42 +00:00
2017-01-30 02:00:23 +00:00
2016-08-26 09:49:42 +00:00
import sys
import six
2016-08-26 09:49:42 +00:00
from escpos import printer
2021-10-30 16:15:22 +00:00
from escpos.constants import (
CODEPAGE_CHANGE,
CTL_CR,
CTL_FF,
2021-10-30 16:15:22 +00:00
CTL_HT,
CTL_LF,
2021-10-30 16:15:22 +00:00
CTL_VT,
ESC,
2021-10-30 16:15:22 +00:00
)
2016-08-26 09:49:42 +00:00
def main():
"""Init printer and print codepage tables."""
2016-08-26 09:49:42 +00:00
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):
"""Print a codepage."""
2016-08-26 09:49:42 +00:00
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()