From 8d00e63b87fcdf9ef303506db9dc2c46a458838a Mon Sep 17 00:00:00 2001 From: Dean Rispin Date: Tue, 1 Mar 2016 10:05:52 -0800 Subject: [PATCH 1/3] Add width/height multiplier support, upsidedown text and text smoothing --- doc/user/methods.rst | 6 ++++-- escpos/constants.py | 21 +++++++++++++++++++++ escpos/escpos.py | 25 ++++++++++++++++++------- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/doc/user/methods.rst b/doc/user/methods.rst index 10f4fb8..247d9cc 100644 --- a/doc/user/methods.rst +++ b/doc/user/methods.rst @@ -65,8 +65,8 @@ text("text") Prints raw text. Raises ``TextError`` exception. -set("align", "font", "type", width, height, invert) -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +set("align", "font", "type", width, height, invert, smooth, flip) +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Set text properties. * ``align`` set horizontal position for text, the possible values are: @@ -80,6 +80,8 @@ Set text properties. * ``width`` is a numeric value, 1 is for regular size, and 2 is twice the standard size. *Default*: 1 * ``height`` is a numeric value, 1 is for regular size and 2 is twice the standard size. *Default*: 1 * ``invert`` is a boolean value, True enables white on black printing. *Default*: False +* ``smooth`` is a boolean value, True enables text smoothing. *Default*: False +* ``flip`` is a boolean value, True enables upside-down text. *Default*: False cut("mode") ^^^^^^^^^^^ diff --git a/escpos/constants.py b/escpos/constants.py index 59a8853..d1835a4 100644 --- a/escpos/constants.py +++ b/escpos/constants.py @@ -50,6 +50,27 @@ PAPER_PART_CUT = _CUT_PAPER('\x01') # Partial cut paper # Text format # TODO: Acquire the "ESC/POS Application Programming Guide for Paper Roll # Printers" and tidy up this stuff too. +TXT_FLIP_ON = ESC + '\x7b\x01' +TXT_FLIP_OFF = ESC + '\x7b\x00' +TXT_SMOOTH_ON = GS + '\x62\x01' +TXT_SMOOTH_OFF = GS + '\x62\x00' +TXT_SIZE = GS + '!' +TXT_WIDTH = {1: 0x00, + 2: 0x10, + 3: 0x20, + 4: 0x30, + 5: 0x40, + 6: 0x50, + 7: 0x60, + 8: 0x70} +TXT_HEIGHT = {1: 0x00, + 2: 0x01, + 3: 0x02, + 4: 0x03, + 5: 0x04, + 6: 0x05, + 7: 0x06, + 8: 0x07} TXT_NORMAL = ESC + '!\x00' # Normal text TXT_2HEIGHT = ESC + '!\x10' # Double height text TXT_2WIDTH = ESC + '!\x20' # Double width text diff --git a/escpos/escpos.py b/escpos/escpos.py index 21eb452..64b31fe 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -451,7 +451,7 @@ class Escpos(object): colCount = self.columns if columns is None else columns self.text(textwrap.fill(txt, colCount)) - def set(self, align='left', font='a', text_type='normal', width=1, height=1, density=9, invert=False): + def set(self, align='left', font='a', text_type='normal', width=1, height=1, density=9, invert=False, smooth=False, flip=False): """ Set text properties by sending them to the printer :param align: horizontal position for text, possible values are: @@ -466,32 +466,43 @@ class Escpos(object): * B for bold * U for underlined - * B2 for bold, version 2 * U2 for underlined, version 2 * BU for bold and underlined * BU2 for bold and underlined, version 2 * NORMAL for normal text *default*: NORMAL - :param width: text width, normal (1) or double width (2), *default*: 1 - :param height: text height, normal (1) or double height (2), *default*: 1 + :param width: text width multiplier, decimal range 1-8, *default*: 1 + :param height: text height multiplier, decimal range 1-8, *default*: 1 :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 smooth: True enables text smoothing. Effective on 4x4 size text and larger, *default*: False + :param flip: True enables upside-down printing, *default*: False :type invert: bool """ # Width if height == 2 and width == 2: self._raw(TXT_NORMAL) self._raw(TXT_4SQUARE) - elif height == 2 and width != 2: + elif height == 2 and width == 1: self._raw(TXT_NORMAL) self._raw(TXT_2HEIGHT) - elif width == 2 and height != 2: + elif width == 2 and height == 1: self._raw(TXT_NORMAL) self._raw(TXT_2WIDTH) - else: # DEFAULT SIZE: NORMAL + elif width == 1 and height == 1: self._raw(TXT_NORMAL) + else: + self._raw(TXT_SIZE + chr(TXT_WIDTH[width] + TXT_HEIGHT[height])) # Type + if flip == True: + self._raw(TXT_FLIP_ON) + else: + self._raw(TXT_FLIP_OFF) + if smooth == True: + self._raw(TXT_SMOOTH_ON) + else: + self._raw(TXT_SMOOTH_OFF) if text_type.upper() == "B": self._raw(TXT_BOLD_ON) self._raw(TXT_UNDERL_OFF) From e48755f7d03f0da70f100bce328794bd1001961d Mon Sep 17 00:00:00 2001 From: Dean Rispin Date: Tue, 1 Mar 2016 16:25:38 -0800 Subject: [PATCH 2/3] Add error checking on width and height fields: --- escpos/escpos.py | 8 ++++++-- escpos/exceptions.py | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/escpos/escpos.py b/escpos/escpos.py index 64b31fe..1b90b91 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -492,17 +492,21 @@ class Escpos(object): self._raw(TXT_2WIDTH) elif width == 1 and height == 1: self._raw(TXT_NORMAL) - else: + elif width >= 1 and width <= 8 and height >= 1 and height <= 8 and isinstance(width, int) and isinstance(height, int): self._raw(TXT_SIZE + chr(TXT_WIDTH[width] + TXT_HEIGHT[height])) - # Type + else: + raise SetVariableError() + # Upside down if flip == True: self._raw(TXT_FLIP_ON) else: self._raw(TXT_FLIP_OFF) + # Smoothing if smooth == True: self._raw(TXT_SMOOTH_ON) else: self._raw(TXT_SMOOTH_OFF) + # Type if text_type.upper() == "B": self._raw(TXT_BOLD_ON) self._raw(TXT_UNDERL_OFF) diff --git a/escpos/exceptions.py b/escpos/exceptions.py index e91ed5e..56a30a4 100644 --- a/escpos/exceptions.py +++ b/escpos/exceptions.py @@ -12,6 +12,7 @@ Result/Exit codes: - `70` = Invalid number of tab positions :py:exc:`~escpos.exceptions.TabPosError` - `80` = Invalid char code :py:exc:`~escpos.exceptions.CharCodeError` - `90` = USB device not found :py:exc:`~escpos.exceptions.USBNotFoundError` + - `100` = Set variable out of range :py:exc:`~escpos.exceptions.SetVariableError` :author: `Manuel F Martinez `_ and others :organization: Bashlinux and `python-escpos `_ @@ -167,3 +168,18 @@ class USBNotFoundError(Error): def __str__(self): return "USB device not found" + + +class SetVariableError(Error): + """ A set method variable was out of range + + Check set variables against minimum and maximum values + Ths returncode for this exception is `100`. + """ + def __init__(self, msg=""): + Error.__init__(self, msg) + self.msg = msg + self.resultcode = 100 + + def __str__(self): + return "Set variable out of range" From 870144aac6ca03ed3b0a823a13c5190567196072 Mon Sep 17 00:00:00 2001 From: Dean Rispin Date: Tue, 1 Mar 2016 16:34:21 -0800 Subject: [PATCH 3/3] Simplify variable checking on width and height values --- escpos/escpos.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/escpos/escpos.py b/escpos/escpos.py index 1b90b91..8944633 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -492,7 +492,7 @@ class Escpos(object): self._raw(TXT_2WIDTH) elif width == 1 and height == 1: self._raw(TXT_NORMAL) - elif width >= 1 and width <= 8 and height >= 1 and height <= 8 and isinstance(width, int) and isinstance(height, int): + elif 1 <= width <= 8 and 1 <= height <= 8 and isinstance(width, int) and isinstance(height, int): self._raw(TXT_SIZE + chr(TXT_WIDTH[width] + TXT_HEIGHT[height])) else: raise SetVariableError()