Fix tabs behaviour (#238)

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-26 10:01:08 +02:00 committed by Patrick Kanzler
parent 9e47ff2505
commit 82c67aa646
2 changed files with 13 additions and 8 deletions

View File

@ -33,7 +33,7 @@ from .constants import LINESPACING_FUNCS, LINESPACING_RESET
from .constants import LINE_DISPLAY_OPEN, LINE_DISPLAY_CLEAR, LINE_DISPLAY_CLOSE from .constants import LINE_DISPLAY_OPEN, LINE_DISPLAY_CLEAR, LINE_DISPLAY_CLOSE
from .constants import CD_KICK_DEC_SEQUENCE, CD_KICK_5, CD_KICK_2, PAPER_FULL_CUT, PAPER_PART_CUT from .constants import CD_KICK_DEC_SEQUENCE, CD_KICK_5, CD_KICK_2, PAPER_FULL_CUT, PAPER_PART_CUT
from .constants import HW_RESET, HW_SELECT, HW_INIT from .constants import HW_RESET, HW_SELECT, HW_INIT
from .constants import CTL_VT, CTL_HT, CTL_CR, CTL_FF, CTL_LF, CTL_SET_HT, PANEL_BUTTON_OFF, PANEL_BUTTON_ON from .constants import CTL_VT, CTL_CR, CTL_FF, CTL_LF, CTL_SET_HT, PANEL_BUTTON_OFF, PANEL_BUTTON_ON
from .constants import TXT_STYLE from .constants import TXT_STYLE
from .constants import RT_STATUS_ONLINE, RT_MASK_ONLINE from .constants import RT_STATUS_ONLINE, RT_MASK_ONLINE
@ -695,7 +695,7 @@ class Escpos(object):
else: else:
raise ValueError("n must be betwen 0 and 255") 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 """ Feed control sequences
:param ctl: string for the following control sequences: :param ctl: string for the following control sequences:
@ -706,7 +706,8 @@ class Escpos(object):
* HT *for Horizontal Tab* * HT *for Horizontal Tab*
* VT *for Vertical 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` :raises: :py:exc:`~escpos.exceptions.TabPosError`
""" """
# Set position # Set position
@ -717,13 +718,16 @@ class Escpos(object):
elif ctl.upper() == "CR": elif ctl.upper() == "CR":
self._raw(CTL_CR) self._raw(CTL_CR)
elif ctl.upper() == "HT": 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() raise TabPosError()
else: else:
# Set tab positions # Set tab positions
self._raw(CTL_SET_HT + six.int2byte(pos)) self._raw(CTL_SET_HT)
for iterator in range(1, count):
self._raw(CTL_HT) self._raw(six.int2byte(iterator * tab_size))
self._raw(NUL)
elif ctl.upper() == "VT": elif ctl.upper() == "VT":
self._raw(CTL_VT) self._raw(CTL_VT)

View File

@ -150,7 +150,8 @@ class CashDrawerError(Error):
class TabPosError(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`. This exception is raised by :py:meth:`escpos.escpos.Escpos.control`.
The returncode for this exception is `70`. The returncode for this exception is `70`.