python-escpos/examples/codepage_tables.py

60 lines
1.3 KiB
Python
Raw Normal View History

2016-08-26 09:49:42 +00:00
"""Prints code page tables.
"""
import six
import sys
from escpos import printer
from escpos.constants import *
def main():
dummy = printer.Dummy()
dummy.hw('init')
for codepage in sys.argv[1:] or ['USA']:
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()
print dummy.output
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
printer.set(text_type='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
printer.set(text_type='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()
for y in range(0,16):
byte = six.int2byte(x*16+y)
if byte in (ESC, CTL_LF, CTL_FF, CTL_CR, CTL_HT, CTL_VT):
byte = ' '
printer._raw(byte)
printer._raw(sep)
printer._raw('\n')
if __name__ == '__main__':
main()