mirror of
https://github.com/python-escpos/python-escpos
synced 2025-09-13 09:09:58 +00:00
Merge remote-tracking branch 'faulab/refactoring-and-documentation'
This commit is contained in:
193
escpos/escpos.py
193
escpos/escpos.py
@@ -19,17 +19,39 @@ import binascii
|
||||
from .constants import *
|
||||
from .exceptions import *
|
||||
|
||||
class Escpos:
|
||||
from abc import ABCMeta, abstractmethod # abstract base class support
|
||||
|
||||
class Escpos(object):
|
||||
""" ESC/POS Printer object """
|
||||
device = None
|
||||
__metaclass__ = ABCMeta
|
||||
device = None
|
||||
|
||||
|
||||
def __init__(self, columns=32):
|
||||
""" Initialize ESCPOS Printer
|
||||
|
||||
:param columns: Text columns used by the printer. Defaults to 32."""
|
||||
self.columns = columns
|
||||
|
||||
def _check_image_size(self, size):
|
||||
""" Check and fix the size of the image to 32 bits """
|
||||
@abstractmethod
|
||||
def _raw(self, msg):
|
||||
""" Sends raw data to the printer
|
||||
|
||||
This function has to be individually implemented by the implementations.
|
||||
:param msg: message string to be sent to the printer
|
||||
"""
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def _check_image_size(size):
|
||||
""" Check and fix the size of the image to 32 bits
|
||||
|
||||
:param size: size of the image
|
||||
:returns: tuple of image borders
|
||||
:rtype: (int, int)
|
||||
"""
|
||||
if size % 32 == 0:
|
||||
return (0, 0)
|
||||
return 0, 0
|
||||
else:
|
||||
image_border = 32 - (size % 32)
|
||||
if (image_border % 2) == 0:
|
||||
@@ -37,41 +59,46 @@ class Escpos:
|
||||
else:
|
||||
return (round(image_border / 2), round((image_border / 2) + 1))
|
||||
|
||||
|
||||
def _print_image(self, line, size):
|
||||
""" Print formatted image """
|
||||
""" Print formatted image
|
||||
|
||||
:param line:
|
||||
:param size:
|
||||
"""
|
||||
i = 0
|
||||
cont = 0
|
||||
buffer = ""
|
||||
pbuffer = ""
|
||||
|
||||
self._raw(S_RASTER_N)
|
||||
buffer = "%02X%02X%02X%02X" % (((size[0]/size[1])/8), 0, size[1]&0xff, size[1]>>8)
|
||||
self._raw(binascii.unhexlify(buffer))
|
||||
buffer = ""
|
||||
pbuffer = "%02X%02X%02X%02X" % (((size[0]/size[1])/8), 0, size[1] & 0xff, size[1] >> 8)
|
||||
self._raw(binascii.unhexlify(pbuffer))
|
||||
pbuffer = ""
|
||||
|
||||
while i < len(line):
|
||||
hex_string = int(line[i:i+8],2)
|
||||
buffer += "%02X" % hex_string
|
||||
hex_string = int(line[i:i+8], 2)
|
||||
pbuffer += "%02X" % hex_string
|
||||
i += 8
|
||||
cont += 1
|
||||
if cont % 4 == 0:
|
||||
self._raw(binascii.unhexlify(buffer))
|
||||
buffer = ""
|
||||
self._raw(binascii.unhexlify(pbuffer))
|
||||
pbuffer = ""
|
||||
cont = 0
|
||||
|
||||
|
||||
def _convert_image(self, im):
|
||||
""" Parse image and prepare it to a printable format """
|
||||
pixels = []
|
||||
pix_line = ""
|
||||
im_left = ""
|
||||
im_right = ""
|
||||
switch = 0
|
||||
img_size = [ 0, 0 ]
|
||||
""" Parse image and prepare it to a printable format
|
||||
|
||||
:param im: image data
|
||||
:raises: ImageSizeError
|
||||
"""
|
||||
pixels = []
|
||||
pix_line = ""
|
||||
im_left = ""
|
||||
im_right = ""
|
||||
switch = 0
|
||||
img_size = [0, 0]
|
||||
|
||||
if im.size[0] > 512:
|
||||
print ("WARNING: Image is wider than 512 and could be truncated at print time ")
|
||||
print ("WARNING: Image is wider than 512 and could be truncated at print time ")
|
||||
if im.size[1] > 0xffff:
|
||||
raise ImageSizeError()
|
||||
|
||||
@@ -91,7 +118,7 @@ class Escpos:
|
||||
im_color = (RGB[0] + RGB[1] + RGB[2])
|
||||
im_pattern = "1X0"
|
||||
pattern_len = len(im_pattern)
|
||||
switch = (switch - 1 ) * (-1)
|
||||
switch = (switch - 1) * (-1)
|
||||
for x in range(pattern_len):
|
||||
if im_color <= (255 * 3 / pattern_len * (x+1)):
|
||||
if im_pattern[x] == "X":
|
||||
@@ -99,7 +126,7 @@ class Escpos:
|
||||
else:
|
||||
pix_line += im_pattern[x]
|
||||
break
|
||||
elif im_color > (255 * 3 / pattern_len * pattern_len) and im_color <= (255 * 3):
|
||||
elif (255 * 3 / pattern_len * pattern_len) < im_color <= (255 * 3):
|
||||
pix_line += im_pattern[-1]
|
||||
break
|
||||
pix_line += im_right
|
||||
@@ -107,9 +134,11 @@ class Escpos:
|
||||
|
||||
self._print_image(pix_line, img_size)
|
||||
|
||||
def image(self, path_img):
|
||||
""" Open image file
|
||||
|
||||
def image(self,path_img):
|
||||
""" Open image file """
|
||||
:param path_img: path to image
|
||||
"""
|
||||
im_open = Image.open(path_img)
|
||||
|
||||
# Remove the alpha channel on transparent images
|
||||
@@ -155,9 +184,11 @@ class Escpos:
|
||||
temp = 0
|
||||
self._raw(binascii.unhexlify(bytes(buf, "ascii")))
|
||||
|
||||
def qr(self, text):
|
||||
""" Print QR Code for the provided string
|
||||
|
||||
def qr(self,text):
|
||||
""" Print QR Code for the provided string """
|
||||
:param text: text to generate a QR-Code from
|
||||
"""
|
||||
qr_code = qrcode.QRCode(version=4, box_size=4, border=1)
|
||||
qr_code.add_data(text)
|
||||
qr_code.make(fit=True)
|
||||
@@ -167,9 +198,14 @@ class Escpos:
|
||||
# Convert the RGB image in printable image
|
||||
self._convert_image(im)
|
||||
|
||||
def charcode(self, code):
|
||||
""" Set Character Code Table
|
||||
|
||||
def charcode(self,code):
|
||||
""" Set Character Code Table """
|
||||
Sends the control sequence from constants.py to the printer with :py:meth:`escpos.printer._raw()`.
|
||||
|
||||
:param code: Name of CharCode
|
||||
:raises: CharCodeError
|
||||
"""
|
||||
if code.upper() == "USA":
|
||||
self._raw(CHARCODE_PC437)
|
||||
elif code.upper() == "JIS":
|
||||
@@ -188,8 +224,8 @@ class Escpos:
|
||||
self._raw(CHARCODE_GREEK)
|
||||
elif code.upper() == "HEBREW":
|
||||
self._raw(CHARCODE_HEBREW)
|
||||
elif code.upper() == "LATVIAN":
|
||||
self._raw(CHARCODE_PC755)
|
||||
# elif code.upper() == "LATVIAN": # this is not listed in the constants
|
||||
# self._raw(CHARCODE_PC755)
|
||||
elif code.upper() == "WPC1252":
|
||||
self._raw(CHARCODE_PC1252)
|
||||
elif code.upper() == "CIRILLIC2":
|
||||
@@ -216,23 +252,32 @@ class Escpos:
|
||||
raise CharCodeError()
|
||||
|
||||
def barcode(self, code, bc, width, height, pos, font):
|
||||
""" Print Barcode """
|
||||
""" Print Barcode
|
||||
|
||||
:param code: data for barcode
|
||||
:param bc: barcode format, see constants.py
|
||||
:param width: barcode width, has to be between 1 and 255
|
||||
:param height: barcode height, has to be between 2 and 6
|
||||
:param pos: position of text in barcode, default when nothing supplied is below
|
||||
:param font: select font, default is font A
|
||||
:raises: BarcodeSizeError, BarcodeTypeError, BarcodeCodeError
|
||||
"""
|
||||
# Align Bar Code()
|
||||
self._raw(TXT_ALIGN_CT)
|
||||
# Height
|
||||
if height >=2 or height <=6:
|
||||
if height >= 2 or height <= 6:
|
||||
self._raw(BARCODE_HEIGHT)
|
||||
else:
|
||||
raise BarcodeSizeError()
|
||||
# Width
|
||||
if width >= 1 or width <=255:
|
||||
if width >= 1 or width <= 255:
|
||||
self._raw(BARCODE_WIDTH)
|
||||
else:
|
||||
raise BarcodeSizeError()
|
||||
# Font
|
||||
if font.upper() == "B":
|
||||
self._raw(BARCODE_FONT_B)
|
||||
else: # DEFAULT FONT: A
|
||||
else: # DEFAULT FONT: A
|
||||
self._raw(BARCODE_FONT_A)
|
||||
# Position
|
||||
if pos.upper() == "OFF":
|
||||
@@ -264,11 +309,15 @@ class Escpos:
|
||||
if code:
|
||||
self._raw(code)
|
||||
else:
|
||||
raise exception.BarcodeCodeError()
|
||||
|
||||
raise BarcodeCodeError()
|
||||
|
||||
def text(self, txt):
|
||||
""" Print alpha-numeric text """
|
||||
""" Print alpha-numeric text
|
||||
|
||||
The text has to be encoded in the currently selected codepage.
|
||||
:param txt: text to be printed
|
||||
:raises: TextError
|
||||
"""
|
||||
if txt:
|
||||
self._raw(txt)
|
||||
else:
|
||||
@@ -279,8 +328,16 @@ class Escpos:
|
||||
colCount = self.columns if columns == None else columns
|
||||
self.text(textwrap.fill(txt, colCount))
|
||||
|
||||
def set(self, align='left', font='a', type='normal', width=1, height=1, density=9):
|
||||
""" Set text properties """
|
||||
def set(self, align='left', font='a', text_type='normal', width=1, height=1, density=9):
|
||||
""" Set text properties by sending them to the printer
|
||||
|
||||
:param align: alignment of text
|
||||
:param font: font A or B
|
||||
:param text_type: add bold or underlined
|
||||
:param width: text width, normal or double width
|
||||
:param height: text height, normal or double height
|
||||
:param density: print density
|
||||
"""
|
||||
# Width
|
||||
if height == 2 and width == 2:
|
||||
self._raw(TXT_NORMAL)
|
||||
@@ -291,25 +348,25 @@ class Escpos:
|
||||
elif width == 2 and height != 2:
|
||||
self._raw(TXT_NORMAL)
|
||||
self._raw(TXT_2WIDTH)
|
||||
else: # DEFAULT SIZE: NORMAL
|
||||
else: # DEFAULT SIZE: NORMAL
|
||||
self._raw(TXT_NORMAL)
|
||||
# Type
|
||||
if type.upper() == "B":
|
||||
if text_type.upper() == "B":
|
||||
self._raw(TXT_BOLD_ON)
|
||||
self._raw(TXT_UNDERL_OFF)
|
||||
elif type.upper() == "U":
|
||||
elif text_type.upper() == "U":
|
||||
self._raw(TXT_BOLD_OFF)
|
||||
self._raw(TXT_UNDERL_ON)
|
||||
elif type.upper() == "U2":
|
||||
elif text_type.upper() == "U2":
|
||||
self._raw(TXT_BOLD_OFF)
|
||||
self._raw(TXT_UNDERL2_ON)
|
||||
elif type.upper() == "BU":
|
||||
elif text_type.upper() == "BU":
|
||||
self._raw(TXT_BOLD_ON)
|
||||
self._raw(TXT_UNDERL_ON)
|
||||
elif type.upper() == "BU2":
|
||||
elif text_type.upper() == "BU2":
|
||||
self._raw(TXT_BOLD_ON)
|
||||
self._raw(TXT_UNDERL2_ON)
|
||||
elif type.upper == "NORMAL":
|
||||
elif text_type.upper == "NORMAL":
|
||||
self._raw(TXT_BOLD_OFF)
|
||||
self._raw(TXT_UNDERL_OFF)
|
||||
# Font
|
||||
@@ -343,23 +400,29 @@ class Escpos:
|
||||
self._raw(PD_P37)
|
||||
elif density == 8:
|
||||
self._raw(PD_P50)
|
||||
else:# DEFAULT: DOES NOTHING
|
||||
else: # DEFAULT: DOES NOTHING
|
||||
pass
|
||||
|
||||
|
||||
def cut(self, mode=''):
|
||||
""" Cut paper """
|
||||
""" Cut paper
|
||||
|
||||
:param mode: set to 'PART' for a partial cut
|
||||
"""
|
||||
# Fix the size between last line and cut
|
||||
# TODO: handle this with a line feed
|
||||
self._raw("\n\n\n\n\n\n")
|
||||
if mode.upper() == "PART":
|
||||
self._raw(PAPER_PART_CUT)
|
||||
else: # DEFAULT MODE: FULL CUT
|
||||
else: # DEFAULT MODE: FULL CUT
|
||||
self._raw(PAPER_FULL_CUT)
|
||||
|
||||
|
||||
def cashdraw(self, pin):
|
||||
""" Send pulse to kick the cash drawer """
|
||||
""" Send pulse to kick the cash drawer
|
||||
|
||||
Kick cash drawer on pin 2 or pin 5.
|
||||
:param pin: pin number
|
||||
:raises: CashDrawerError
|
||||
"""
|
||||
if pin == 2:
|
||||
self._raw(CD_KICK_2)
|
||||
elif pin == 5:
|
||||
@@ -367,26 +430,30 @@ class Escpos:
|
||||
else:
|
||||
raise CashDrawerError()
|
||||
|
||||
|
||||
def hw(self, hw):
|
||||
""" Hardware operations """
|
||||
""" Hardware operations
|
||||
|
||||
:param hw: hardware action
|
||||
"""
|
||||
if hw.upper() == "INIT":
|
||||
self._raw(HW_INIT)
|
||||
elif hw.upper() == "SELECT":
|
||||
self._raw(HW_SELECT)
|
||||
elif hw.upper() == "RESET":
|
||||
self._raw(HW_RESET)
|
||||
else: # DEFAULT: DOES NOTHING
|
||||
else: # DEFAULT: DOES NOTHING
|
||||
pass
|
||||
|
||||
|
||||
def control(self, ctl, pos=4):
|
||||
""" Feed control sequences """
|
||||
""" Feed control sequences
|
||||
|
||||
:raises: TabPosError
|
||||
"""
|
||||
# Set tab positions
|
||||
if pos < 1 or pos > 16:
|
||||
raise TabError()
|
||||
raise TabPosError()
|
||||
else:
|
||||
self._raw("".join([CTL_SET_HT,hex(pos)]))
|
||||
self._raw("".join([CTL_SET_HT, hex(pos)]))
|
||||
# Set position
|
||||
if ctl.upper() == "LF":
|
||||
self._raw(CTL_LF)
|
||||
|
Reference in New Issue
Block a user