Using booleans for handling text size

This commit is contained in:
Romain Porte 2017-05-15 18:54:54 +02:00 committed by Patrick Kanzler
parent c0b4d03692
commit a6e1d0df00
2 changed files with 26 additions and 23 deletions

View File

@ -422,7 +422,7 @@ class Escpos(object):
def set(self, align='left', font='a', bold=False, underline=0, width=1, def set(self, align='left', font='a', bold=False, underline=0, width=1,
height=1, density=9, invert=False, smooth=False, flip=False, height=1, density=9, invert=False, smooth=False, flip=False,
size='normal'): double_width=False, double_height=False, custom_size=False):
""" Set text properties by sending them to the printer """ Set text properties by sending them to the printer
:param align: horizontal position for text, possible values are: :param align: horizontal position for text, possible values are:
@ -433,50 +433,53 @@ class Escpos(object):
*default*: 'left' *default*: 'left'
:param size: size modifier for text, possible values are:
* 'normal'
* '2h' for double text height
* '2w' for double text width
* '2x' for double text height and width (doubles the text surface)
* 'custom' for custom text height and width
In this last case, see the width and height parameters.
*default*: 'normal'
:param font: font given as an index, a name, or one of the :param font: font given as an index, a name, or one of the
special values 'a' or 'b', referring to fonts 0 and 1. special values 'a' or 'b', referring to fonts 0 and 1.
:param bold: text in bold, *default*: False :param bold: text in bold, *default*: False
:param underline: underline mode for text, decimal range 0-2, *default*: 0 :param underline: underline mode for text, decimal range 0-2, *default*: 0
:param width: text width multiplier when size is set to custom, decimal range 1-8, *default*: 1 :param double_height: doubles the height of the text
:param height: text height multiplier when size is set to custom, decimal range 1-8, *default*: 1 :param double_width: doubles the width of the text
:param custom_size: uses custom size specified by width and height
parameters. Cannot be used with double_width or double_height.
:param width: text width multiplier when custom_size is used, decimal range 1-8, *default*: 1
:param height: text height multiplier when custom_size is used, decimal range 1-8, *default*: 1
:param density: print density, value from 0-8, if something else is supplied the density remains unchanged :param density: print density, value from 0-8, if something else is supplied the density remains unchanged
:param invert: True enables white on black printing, *default*: False :param invert: True enables white on black printing, *default*: False
:param smooth: True enables text smoothing. Effective on 4x4 size text and larger, *default*: False :param smooth: True enables text smoothing. Effective on 4x4 size text and larger, *default*: False
:param flip: True enables upside-down printing, *default*: False :param flip: True enables upside-down printing, *default*: False
:type font: str
:type invert: bool :type invert: bool
:type bold: bool :type bold: bool
:type underline: bool :type underline: bool
:type smooth: bool :type smooth: bool
:type flip: bool :type flip: bool
:type size: str :type custom_size: bool
:type double_width: bool
:type double_height: bool
:type align: str :type align: str
:type width: int :type width: int
:type height: int :type height: int
:type density: int :type density: int
""" """
if size in TXT_STYLE['size']: if custom_size:
self._raw(TXT_NORMAL)
self._raw(TXT_STYLE['size'][size])
elif size == 'custom':
if 1 <= width <= 8 and 1 <= height <= 8 and isinstance(width, int) and\ if 1 <= width <= 8 and 1 <= height <= 8 and isinstance(width, int) and\
isinstance(height, int): isinstance(height, int):
size_byte = TXT_STYLE['width'][width] + TXT_STYLE['height'][height] size_byte = TXT_STYLE['width'][width] + TXT_STYLE['height'][height]
self._raw(TXT_SIZE + six.int2byte(size_byte)) self._raw(TXT_SIZE + six.int2byte(size_byte))
else: else:
raise SetVariableError() raise SetVariableError()
else:
self._raw(TXT_NORMAL)
if double_width and double_height:
self._raw(TXT_STYLE['size']['2x'])
elif double_width:
self._raw(TXT_STYLE['size']['2w'])
elif double_height:
self._raw(TXT_STYLE['size']['2h'])
else:
self._raw(TXT_STYLE['size']['normal'])
self._raw(TXT_STYLE['flip'][flip]) self._raw(TXT_STYLE['flip'][flip])
self._raw(TXT_STYLE['smooth'][smooth]) self._raw(TXT_STYLE['smooth'][smooth])

View File

@ -33,7 +33,7 @@ def test_default_values():
def test_set_size_2h(): def test_set_size_2h():
instance = printer.Dummy() instance = printer.Dummy()
instance.set(size='2h') instance.set(double_height=True)
expected_sequence = ( expected_sequence = (
TXT_NORMAL, TXT_STYLE['size']['2h'], # Double height text size TXT_NORMAL, TXT_STYLE['size']['2h'], # Double height text size
@ -51,7 +51,7 @@ def test_set_size_2h():
def test_set_size_2w(): def test_set_size_2w():
instance = printer.Dummy() instance = printer.Dummy()
instance.set(size='2w') instance.set(double_width=True)
expected_sequence = ( expected_sequence = (
TXT_NORMAL, TXT_STYLE['size']['2w'], # Double width text size TXT_NORMAL, TXT_STYLE['size']['2w'], # Double width text size
@ -69,7 +69,7 @@ def test_set_size_2w():
def test_set_size_2x(): def test_set_size_2x():
instance = printer.Dummy() instance = printer.Dummy()
instance.set(size='2x') instance.set(double_height=True, double_width=True)
expected_sequence = ( expected_sequence = (
TXT_NORMAL, TXT_STYLE['size']['2x'], # Double text size TXT_NORMAL, TXT_STYLE['size']['2x'], # Double text size
@ -87,7 +87,7 @@ def test_set_size_2x():
def test_set_size_custom(): def test_set_size_custom():
instance = printer.Dummy() instance = printer.Dummy()
instance.set(size='custom', width=8, height=7) instance.set(custom_size=True, width=8, height=7)
expected_sequence = ( expected_sequence = (
TXT_SIZE, # Custom text size, no normal reset TXT_SIZE, # Custom text size, no normal reset