writer and reader added

This commit is contained in:
Philipp Klaus
2015-12-12 13:53:33 +01:00
parent 8f699384e5
commit ecd970d846
2 changed files with 197 additions and 0 deletions
+77
View File
@@ -0,0 +1,77 @@
models = [
'QL-500',
'QL-550',
'QL-560',
'QL-570',
'QL-580N',
'QL-650TD'
'QL-700',
'QL-710W',
'QL-720NW',
'QL-1050',
'QL-1060N',
]
min_max_length_dots = {
'default': (295, 11811),
# 'QL-500', 'QL-550', 'QL-560', and 'QL-650TD'
# they all use the default.
'QL-1050': (295, 35433),
'QL-1060N': (295, 35433),
'QL-570': (150, 11811),
'QL-580N': (150, 11811),
'QL-700': (150, 11811),
'QL-710W': (150, 11811),
'QL-720NW': (150, 11811),
}
min_max_feed = {
'default': (35, 1500),
}
paper_dimensions = {
# the dimensions are given as (width, length)
# tape with length > 0: rectangular die-cut labels
# tape with length == 0: continuous length tape
# tape with length == -1: round die-cut labels
#ID: ( tape_size_mm, size_dots, printable size dots, right_margin_dots, restrict_printers)
257: ((12, 0), (142, 0), (106, 0), 29, []),
258: ((29, 0), (342, 0), (306, 0), 6, []),
264: ((38, 0), (449, 0), (413, 0), 12, []),
262: ((50, 0), (590, 0), (554, 0), 12, []),
261: ((54, 0), (636, 0), (590, 0), 0, []),
259: ((62, 0), (732, 0), (696, 0), 12, []),
260: ((102, 0), (1200, 0), (1164, 0), 12, ['QL-1060N', 'QL-1050']),
269: ((17, 54), (201, 636), (165, 566), 0, []),
270: ((17, 87), (201, 1026), (165, 956), 0, []),
370: ((23, 23), (272, 272), (202, 202), 42, []),
358: ((29, 42), (342, 495), (306, 425), 6, []),
271: ((29, 90), (342, 1061), (306, 991), 6, []),
272: ((38, 90), (449, 1061), (413, 991), 12, []),
367: ((39, 48), (461, 565), (425, 495), 6, []),
374: ((52, 29), (614, 341), (578, 271), 0, []),
274: ((62, 29), (732, 341), (696, 271), 12, []),
275: ((62, 100), (732, 1179), (696, 1109), 12, []),
365: ((102, 51), (1200, 596), (1164, 526), 12, ['QL-1060N', 'QL-1050']),
366: ((102, 152), (1200, 1804), (1164, 1660), 12, ['QL-1060N', 'QL-1050']),
362: ((12, -1), (142, 142), ( 94, 94), 113, []),
363: ((24, -1), (284, 284), (236, 236), 42, []),
273: ((58, -1), (688, 688), (618, 618), 51, []),
}
number_bytes_per_row = {
'default': 90,
'QL-1050': 162,
'QL-1060N': 162,
}
right_margin_addition = {
'default': 0,
'QL-1050': 44,
'QL-1060N': 44,
}
+120
View File
@@ -0,0 +1,120 @@
import struct
import packbits
import numpy as np
from devicedependent import models, \
min_max_feed, \
min_max_length_dots, \
paper_dimensions, \
number_bytes_per_row, \
right_margin_addition
class QLRaster(object):
def __init__(self, model='QL-500'):
if model not in models:
raise QLRasterUnknownModel()
self.model = model
self.data = b''
self._pquality = 1
self.page_number = 0
self.cut_at_end = True
self.dpi_600 = False
self.compression = True
def initialize(self):
self.page_number = 0
self.data += b'\x1B\x69\x61\x01' # mode setting (raster mode)
self.data += b'\x00' * 200
self.data += b'\x1B\x40' # init
self.data += b'\x1B\x69\x61\x01' # mode setting (raster mode)
@property
def mtype(self): return self._mtype
@property
def mwidth(self): return self._mwidth
@property
def mlength(self): return self._mlength
@property
def pquality(self): return self._pquality
@mtype.setter
def mtype(self, value):
self._mtype = bytes([value & 0xFF])
@mwidth.setter
def mwidth(self, value):
self._mwidth = bytes([value & 0xFF])
@mlength.setter
def mlength(self, value):
self._mlength = bytes([value & 0xFF])
@pquality.setter
def pquality(self, value):
self._pquality = bytes([value & 0x01])
def set_media_and_quality(self, rnumber):
self.data += b'\x1B\x69\x7A'
valid_flags = 0x80
valid_flags |= (self._mtype is not None) << 1
valid_flags |= (self._mwidth is not None) << 2
valid_flags |= (self._mlength is not None) << 3
valid_flags |= self._pquality << 6
self.data += bytes([valid_flags])
vals = [self._mtype, self._mwidth, self._mlength]
self.data += b''.join(b'\x00' if val is None else val for val in vals)
self.data += struct.pack('<L', rnumber)
self.data += bytes([self.page_number == 0])
self.data += b'\x00'
# INFO: media/quality (1B 69 7A) --> found! (payload: 8E 0A 3E 00 D2 00 00 00 00 00)
def set_autocut(self, autocut = False):
self.data += b'\x1B\x69\x4D'
self.data += bytes([autocut << 6])
def set_cut_every(self, n=1):
self.data += b'\x1B\x69\x41'
self.data += bytes([n & 0xFF])
def set_expanded_mode(self):
self.data += b'\x1B\x69\x4B'
flags = 0x00
flags |= self.cut_at_end << 3
flags |= self.dpi_600 << 6
self.data += bytes([flags])
def set_margins(self, dots=0x23):
self.data += b'\x1B\x69\x64'
self.data += struct.pack('<H', dots)
def set_compression(self, compression=True):
self.compression = compression
self.data += b'\x4D'
self.data += bytes([compression << 1])
def set_raster_data(self, np_array):
""" np_array: numpy array of 1-bit values """
np_array = np.fliplr(np_array)
for row in np_array:
self.data += b'\x67\x00'
row = bytes(np.packbits(row))
if self.compression:
row = packbits.encode(row)
self.data += bytes([len(row)])
self.data += row
def print(self, last_page=True):
if last_page:
self.data += b'\x1A'
else:
self.data += b'\x0C'
class QLRasterError(Exception): pass
class QLRasterUnknownModel(QLRasterError): pass