def __repr__(self): """ Build and return a text entity package :return: str representing entity """ # Start with packet, then add length of packet out = "" out += int_to_hexstring(self.vertical, little_endian=True, number_of_hex_digits=3) out += int_to_hexstring(self.horizontal, little_endian=False, number_of_hex_digits=3) out += int_to_hexstring(self.draw_style, little_endian=False, number_of_hex_digits=2) out += int_to_hexstring(self.font_style, little_endian=False, number_of_hex_digits=2) out += utf8_to_utf16hexstring(self.text) length = int_to_hexstring(int(len(out) / 2), little_endian=False, number_of_hex_digits=2) out = length + out return out
def __str__(self): """ Build a human readable packet :return: str """ out = "Entity Text package\n" out += "Part\t\t\t\t\tLength\tData\n" length = len(self.__repr__()) - 2 out += "DataLengthSegment\t\t2\t\t%s (%d*2=%d bytes)\n" % ( int_to_hexstring(int(length / 2), little_endian=False, number_of_hex_digits=2), int(length / 2), length) out += "Vertical\t\t\t\t3\t\t%s (%d)\n" % (int_to_hexstring( self.vertical, little_endian=True, number_of_hex_digits=3), self.vertical) out += "Horizontal\t\t\t\t3\t\t%s (%d)\n" % (int_to_hexstring( self.horizontal, little_endian=False, number_of_hex_digits=3), self.horizontal) if self.draw_style in DrawStyles: out += "DrawStyle\t\t\t\t2\t\t%s (%s)\n" % (int_to_hexstring( self.draw_style, little_endian=False, number_of_hex_digits=2), DrawStyles[self.draw_style]) else: out += "DrawStyle\t\t\t\t2\t\t%s (Unknown)\n" % int_to_hexstring( self.draw_style, little_endian=False, number_of_hex_digits=2) if self.font_style in FontStyles: out += "FontStyle\t\t\t\t2\t\t%s (%s)\n" % (int_to_hexstring( self.font_style, little_endian=False, number_of_hex_digits=2), FontStyles[self.font_style]) else: out += "FontStyle\t\t\t\t2\t\t%s (Unknown)\n" % int_to_hexstring( self.font_style, little_endian=False, number_of_hex_digits=2) out += "Text\t\t\t\t\t\t\t%s (%s)" % (utf8_to_utf16hexstring( self.text), self.text) return out
def test_utf8_to_utf16hexstring(self): self.assertEqual("0041", utf8_to_utf16hexstring("A")) self.assertEqual("00410061", utf8_to_utf16hexstring("Aa")) self.assertEqual("0059006F0020006D0061006D006D00610021", utf8_to_utf16hexstring("Yo mamma!")) self.assertEqual("003100320033003400350036003700380039", utf8_to_utf16hexstring("123456789")) self.assertEqual("0041002000E500E400F600200042", utf8_to_utf16hexstring("A åäö B")) self.assertEqual("0041002000C500C400D600200042", utf8_to_utf16hexstring("A ÅÄÖ B")) self.assertEqual("003D007B007D005B005D00250026", utf8_to_utf16hexstring("={}[]%&"))
def __init__(self, raw="", vertical=0, horizontal=0, draw_style=0, font_style=0, text=""): # Barcodes that is supported now, some other check digits etc. might be needed self.supported_barcodes = [ FontStylesInv['Barcode EAN13'], FontStylesInv['Barcode 128'], FontStylesInv['Barcode EAN13 Double Size'], FontStylesInv['Barcode 128 Double Size'] ] if len(raw) > 0: self._raw = raw self.length = hexstring_to_int(self._raw[0:2], little_endian=False) * 2 if len(self._raw) != self.length + 2: raise Exception( f'Barcode packet length field does not match up with actual size, this string is {len(self._raw)} Packet: {self._raw}' ) self.vertical = hexstring_to_int(self._raw[2:5], little_endian=True) self.horizontal = hexstring_to_int(self._raw[5:8], little_endian=False) self.draw_style = hexstring_to_int(self._raw[8:10], little_endian=False) self.font_style = hexstring_to_int(self._raw[10:12], little_endian=False) rawtext = self._raw[12:] if self.font_style in [ FontStylesInv['Barcode 128'], FontStylesInv['Barcode 128 Double Size'] ]: self.text = rawtext[ 4: -8] # Skip first start code, and last check digit and stop if self.font_style in [ FontStylesInv['Barcode EAN13'], FontStylesInv['Barcode EAN13 Double Size'] ]: if len(rawtext) != (1 * 4) + (13 * 4) + (2 * 4): raise Exception( "Can't read EAN 13, expected length to be 1 start + 13 digits + 2 stop." ) self.text = rawtext[ 4: -12] # Skip first start code, and last check digit and stop self.text = utf16hexstring_to_utf8(self.text) else: self.length = 0 self.vertical = vertical self.horizontal = horizontal self.draw_style = draw_style self.font_style = font_style self.text = text self.prefix = "0088" # Start Code B 0088(UTF-16 HTS), also used by EAN 13 if self.font_style not in self.supported_barcodes: raise Exception("Only Barcode 128 and EAN13 is supported.") # Barcode 128 if self.font_style in [ FontStylesInv['Barcode 128'], FontStylesInv['Barcode 128 Double Size'] ]: if len(self.text) < 1: raise Exception("Barcode 128 musbe more than 0 cahracters.") if not validate_barcode128b_characters(self.text): raise Exception( "Only characters belonging to Barcode 128 B is valid.") self.suffix = utf8_to_utf16hexstring( calculate_barcode128b_check_digit(self.text)) self.suffix += "008A" # Stop 008A(UTF-16 VTS) # EAN 13 if self.font_style in [ FontStylesInv['Barcode EAN13'], FontStylesInv['Barcode EAN13 Double Size'] ]: if len(self.text) != 12: raise Exception( "EAN 13 must bra 12 digits, the 13th is check digit and it will be calculated automatically." ) if not validate_ean13_characters(self.text): raise Exception("EAN 13 can only contain digits.") self.suffix = utf8_to_utf16hexstring( calculate_ean13_check_digit(self.text)) self.suffix += "003D" # Hardcoded for EAN13? self.suffix += "008A" # Stop 008A(UTF-16 VTS)