python-escpos/examples/codepage_tables.py

68 lines
1.5 KiB
Python
Raw Permalink Normal View History

2018-05-16 00:02:35 +00:00
# -*- coding: utf-8 -*-
2016-08-26 09:49:42 +00:00
"""Prints code page tables.
"""
2018-05-15 23:23:04 +00:00
from __future__ import absolute_import
from __future__ import division
2017-01-30 02:00:23 +00:00
from __future__ import print_function
2018-05-15 23:23:04 +00:00
from __future__ import unicode_literals
2017-01-30 02:00:23 +00:00
2016-08-26 09:49:42 +00:00
import sys
from escpos import printer
2018-05-15 23:23:04 +00:00
from escpos.constants import CODEPAGE_CHANGE, CTL_CR, CTL_FF, CTL_HT, CTL_LF, CTL_VT, ESC
import six
2016-08-26 09:49:42 +00:00
def main():
dummy = printer.Dummy()
dummy.hw('init')
for codepage in sys.argv[1:] or ['USA']:
dummy.set(height=2, width=2)
2018-05-15 23:09:48 +00:00
dummy._raw(codepage + '\n\n\n')
2016-08-26 09:49:42 +00:00
print_codepage(dummy, codepage)
2018-05-15 23:09:48 +00:00
dummy._raw('\n\n')
2016-08-26 09:49:42 +00:00
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))
2018-05-15 23:09:48 +00:00
printer._raw('after')
2016-08-26 09:49:42 +00:00
else:
printer.charcode(codepage)
2018-05-15 23:09:48 +00:00
sep = ''
2016-08-26 09:49:42 +00:00
# Table header
printer.set(font='b')
2018-05-15 23:09:48 +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(font='b')
2018-05-15 23:09:48 +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):
byte = ' '
printer._raw(byte)
printer._raw(sep)
printer._raw('\n')
2017-01-29 23:10:14 +00:00
if __name__ == '__main__':
2017-01-29 23:10:14 +00:00
main()