Fix tabs behaviour.

The changes done in this commit should help with the open issues:
#5, #27 and #161.
The old implementation lacked the NUL char at the end of the command, as
defined on the Epson ESC/POS Reference Guide (see
https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=53
). Also, the horizontal tab control character (CTL_HT) shouldn't be
there.

This implementation allows setting up to 32 tabs with a given tab width.
Both values are checked to be in the valid ranges defined on the guide.
Also, the TabPosError exception text has been rewritten to define the
stated above.
This commit is contained in:
csoft2k 2017-07-24 23:29:16 +02:00
parent 8a1816476a
commit 8cdcf112e7
2 changed files with 10 additions and 7 deletions

View File

@ -689,7 +689,7 @@ class Escpos(object):
else:
raise ValueError("n must be betwen 0 and 255")
def control(self, ctl, pos=4):
def control(self, ctl, count=5, tab_size=8):
""" Feed control sequences
:param ctl: string for the following control sequences:
@ -700,7 +700,8 @@ class Escpos(object):
* HT *for Horizontal Tab*
* VT *for Vertical Tab*
:param pos: integer between 1 and 16, controls the horizontal tab position
:param count: integer between 1 and 32, controls the horizontal tab count. Defaults to 5.
:param tab_size: integer between 1 and 255, controls the horizontal tab size in characters. Defaults to 8
:raises: :py:exc:`~escpos.exceptions.TabPosError`
"""
# Set position
@ -711,13 +712,14 @@ class Escpos(object):
elif ctl.upper() == "CR":
self._raw(CTL_CR)
elif ctl.upper() == "HT":
if not (1 <= pos <= 16):
if not (0 <= count <= 32 and 1 <= tab_size <= 255 and count * tab_size < 256):
raise TabPosError()
else:
# Set tab positions
self._raw(CTL_SET_HT + six.int2byte(pos))
self._raw(CTL_HT)
self._raw(CTL_SET_HT)
for iterator in range(1, count):
self._raw(six.int2byte(iterator * tab_size))
self._raw(NUL)
elif ctl.upper() == "VT":
self._raw(CTL_VT)

View File

@ -150,7 +150,8 @@ class CashDrawerError(Error):
class TabPosError(Error):
""" Valid tab positions must be in the range 0 to 16.
""" Valid tab positions must be set by using from 1 to 32 tabs, and between 1 and 255 tab size values.
Both values multiplied must not exceed 255, since it is the maximum tab value.
This exception is raised by :py:meth:`escpos.escpos.Escpos.control`.
The returncode for this exception is `70`.