forked from sammyfung/badgeprint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabelprint.py
331 lines (297 loc) · 15.1 KB
/
labelprint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# -*- coding:utf8 -*-
# labelprint.py by Sammy Fung <[email protected]>
import logging, subprocess, re, os
from django.conf import settings
from PIL import Image, ImageDraw, ImageFont
from brother_ql.devicedependent import label_type_specs
from brother_ql.devicedependent import ENDLESS_LABEL, DIE_CUT_LABEL, ROUND_DIE_CUT_LABEL
from brother_ql import BrotherQLRaster, create_label
from brother_ql.backends import backend_factory, guess_backend
logger = logging.getLogger(__name__)
BACKEND_CLASS = None
BACKEND_STRING_DESCR = None
def get_label_context(first_name, last_name, company, default_label_size):
# For labels in English, it uses font 'Open Sans'.
# For labels in non-English, it assumes Traditional Chinese font is required,
# it uses font 'Noto Sans CJK TC'.
context = {
'first_name': first_name,
'last_name': last_name,
'name': "%s %s"%(first_name, last_name),
'company': company,
'font_size': int(90),
'company_font_size': int(40),
'font_family': 'Open Sans',
'font_style': 'Regular',
# 'font_family': ('Noto Sans CJK TC', 'Noto Sans CJK TC Medium'),
# 'font_style': ('Medium', 'Regular'),
'label_size': default_label_size,
'margin': int(10),
'threshold': int(70),
'align': 'center',
'orientation': 'standard',
'margin_top': float(24)/100.,
'margin_bottom': float(45)/100.,
'margin_left': float(35)/100.,
'margin_right': float(35)/100.,
}
context['margin_top'] = int(context['font_size']*context['margin_top'])
context['margin_bottom'] = int(context['font_size']*context['margin_bottom'])
context['margin_left'] = int(context['font_size']*context['margin_left'])
context['margin_right'] = int(context['font_size']*context['margin_right'])
def get_font_path(font_family, font_style):
try:
if font_family is None:
font_family = DEFAULT_FONT['family']
font_style = DEFAULT_FONT['style']
if font_style is None:
font_style = 'Regular'
font_path = FONTS[font_family][font_style]
except KeyError:
raise LookupError("Couln't find the font & style")
return font_path
context['font_path'] = get_font_path(context['font_family'], context['font_style'])
# Use Chinese font if first name is not starting with A-Z
if not(re.search('^[A-Za-z0-9,.()\-/ ]*$', last_name)):
context['name'] = "%s%s" % (first_name, last_name)
context['font_path'] = '/badgeprint/static/fonts/NotoSansCJKtc-Medium.otf'
if not(re.search('^[A-Za-z0-9,.()\-/ ]*$', first_name)):
context['name'] = "%s%s" % (last_name, first_name)
context['font_path'] = '/badgeprint/static/fonts/NotoSansCJKtc-Medium.otf'
if not (re.search('^[A-Za-z0-9,.()\-/ ]*$', company)):
context['font_path'] = '/badgeprint/static/fonts/NotoSansCJKtc-Medium.otf'
def get_label_dimensions(label_size):
try:
ls = label_type_specs[context['label_size']]
except KeyError:
raise LookupError("Unknown label_size")
return ls['dots_printable']
width, height = get_label_dimensions(context['label_size'])
if height > width: width, height = height, width
if context['orientation'] == 'rotated': height, width = width, height
context['width'], context['height'] = width, height
return context
# Brother Label DK-11209: 62x29 (696x271px)
def create_label_im_62x29(**kwargs):
label_type = label_type_specs[kwargs['label_size']]['kind']
kwargs['font_path'] = kwargs['font_path'][1:]
im_font = ImageFont.truetype(kwargs['font_path'], kwargs['font_size'])
company_font = ImageFont.truetype(kwargs['font_path'], kwargs['company_font_size'])
im = Image.new('L', (20, 20), 'white')
draw = ImageDraw.Draw(im)
company_textsize = draw.multiline_textsize(kwargs['company'], font=company_font)
textsize = draw.multiline_textsize(kwargs['name'], font=im_font)
# Label DK-11209 is 696x271px
if textsize[0] > 696:
kwargs['name'] = "%s\n%s"%(kwargs['first_name'], kwargs['last_name'])
textsize = draw.multiline_textsize(kwargs['name'], font=im_font)
width, height = kwargs['width'], kwargs['height']
if kwargs['orientation'] == 'standard':
if label_type in (ENDLESS_LABEL,):
height = textsize[1] + company_textsize[1] + kwargs['margin_top'] + kwargs['margin_bottom']
elif kwargs['orientation'] == 'rotated':
if label_type in (ENDLESS_LABEL,):
width = textsize[0] + company_textsize[0] + kwargs['margin_left'] + kwargs['margin_right']
im = Image.new('L', (width, height), 'white')
draw = ImageDraw.Draw(im)
if kwargs['orientation'] == 'standard':
if label_type in (DIE_CUT_LABEL, ROUND_DIE_CUT_LABEL):
vertical_offset = (height - textsize[1] - company_textsize[1] - 10)//2
vertical_offset += (kwargs['margin_top'] - kwargs['margin_bottom'])//2
else:
vertical_offset = kwargs['margin_top']
horizontal_offset = max((width - textsize[0])//2, 0)
elif kwargs['orientation'] == 'rotated':
vertical_offset = (height - textsize[1])//2
vertical_offset += (kwargs['margin_top'] - kwargs['margin_bottom'])//2
if label_type in (DIE_CUT_LABEL, ROUND_DIE_CUT_LABEL):
horizontal_offset = max((width - textsize[0])//2, 0)
else:
horizontal_offset = kwargs['margin_left']
offset = horizontal_offset, vertical_offset
draw.multiline_text(offset, kwargs['name'], (0), font=im_font, align=kwargs['align'])
company_vertical_offset = vertical_offset + textsize[1] + 20
company_horizontal_offset = max((width - company_textsize[0]) // 2, 0)
company_offset = company_horizontal_offset, company_vertical_offset
draw.multiline_text(company_offset, kwargs['company'], (0), font=company_font, align=kwargs['align'])
return im
# Brother Label DK-11202: 62x100 (696x1109px)
def create_label_im_62x100(**kwargs):
label_type = label_type_specs[kwargs['label_size']]['kind']
kwargs['font_path'] = kwargs['font_path'][1:]
kwargs['font_path'] = re.sub('"','', kwargs['font_path'])
im_font = ImageFont.truetype(kwargs['font_path'], kwargs['font_size'], encoding="utf-8")
company_font = ImageFont.truetype(kwargs['font_path'], kwargs['company_font_size'])
im = Image.new('L', (20, 20), 'white')
draw = ImageDraw.Draw(im)
company_textsize = draw.multiline_textsize(kwargs['company'], font=company_font)
textsize = draw.multiline_textsize(kwargs['name'], font=im_font)
# Label DK-11209 is 696x1109px
if textsize[0] > 696:
kwargs['name'] = "%s\n%s"%(kwargs['first_name'], kwargs['last_name'])
textsize = draw.multiline_textsize(kwargs['name'], font=im_font)
width, height = kwargs['width'], kwargs['height']
if kwargs['orientation'] == 'standard':
if label_type in (ENDLESS_LABEL,):
height = textsize[1] + company_textsize[1] + kwargs['margin_top'] + kwargs['margin_bottom']
elif kwargs['orientation'] == 'rotated':
if label_type in (ENDLESS_LABEL,):
width = textsize[0] + company_textsize[0] + kwargs['margin_left'] + kwargs['margin_right']
im = Image.new('L', (width, height), 'white')
draw = ImageDraw.Draw(im)
if kwargs['orientation'] == 'standard':
if label_type in (DIE_CUT_LABEL, ROUND_DIE_CUT_LABEL):
vertical_offset = (height - textsize[1] - company_textsize[1] - 10)//2
vertical_offset += (kwargs['margin_top'] - kwargs['margin_bottom'])//2
else:
vertical_offset = kwargs['margin_top']
horizontal_offset = max((width - textsize[0])//2, 0)
elif kwargs['orientation'] == 'rotated':
vertical_offset = (height - textsize[1])//2
vertical_offset += (kwargs['margin_top'] - kwargs['margin_bottom'])//2
if label_type in (DIE_CUT_LABEL, ROUND_DIE_CUT_LABEL):
horizontal_offset = max((width - textsize[0])//2, 0)
else:
horizontal_offset = kwargs['margin_left']
offset = horizontal_offset, vertical_offset
draw.multiline_text(offset, kwargs['name'], (0), font=im_font, align=kwargs['align'])
company_vertical_offset = vertical_offset + textsize[1] + 20
company_horizontal_offset = max((width - company_textsize[0]) // 2, 0)
company_offset = company_horizontal_offset, company_vertical_offset
draw.multiline_text(company_offset, kwargs['company'], (0), font=company_font, align=kwargs['align'])
draw.line((3, 10, width-3, 10), fill=0, width=3)
if re.search('^type-', kwargs['label_tpl']):
if re.search('^type-non-', kwargs['label_tpl']):
label_tpl = re.sub('^type-non-', '', kwargs['label_tpl'])
ticket_type = kwargs['ticket_type']
print("%s %s"%(label_tpl , ticket_type))
if label_tpl != ticket_type:
ticket_type = ticket_type.upper()
ticket_type_font = ImageFont.truetype(kwargs['font_path'], 60)
ticket_type_textsize = draw.multiline_textsize(ticket_type, font=ticket_type_font)
ticket_type_vertical_offset = height - ticket_type_textsize[1] - 20
ticket_type_horizontal_offset = max((width - ticket_type_textsize[0]) // 2, 0)
ticket_type_offset = ticket_type_horizontal_offset, ticket_type_vertical_offset
draw.multiline_text(ticket_type_offset, ticket_type, (0), font=ticket_type_font, align=kwargs['align'])
line_height = height - 20 - ticket_type_textsize[1]/2 + 8
draw.line((3, line_height, ticket_type_horizontal_offset-10, line_height), fill=0, width=3)
draw.line((ticket_type_horizontal_offset+ticket_type_textsize[0]+10, line_height, width-3, line_height), fill=0, width=3)
else:
draw.line((3, height-10, width-3, height-10),fill=0, width=3)
im.show()
else:
eventname_font = ImageFont.truetype(kwargs['font_path'], 26)
eventname = kwargs['event_name']
eventname_textsize = draw.multiline_textsize(eventname, font=eventname_font)
eventname_vertical_offset = height - eventname_textsize[1] - 20
eventname_horizontal_offset = max((width - eventname_textsize[0]) // 2, 0)
eventname_offset = eventname_horizontal_offset, eventname_vertical_offset
draw.multiline_text(eventname_offset, eventname, (0), font=eventname_font, align=kwargs['align'])
if kwargs['logo'] != None and kwargs['logo'] != '':
logo = Image.open(kwargs['logo'], 'r')
logo_width, logo_height = logo.size
logo_offset = int((width - logo_width)/2), 20
im.paste(logo, logo_offset)
return im
def print_text(**data):
global DEBUG, FONTS, DEFAULT_FONT, MODEL, BACKEND_CLASS, BACKEND_STRING_DESCR, DEFAULT_ORIENTATION, DEFAULT_LABEL_SIZE
BACKEND_STRING_DESCR = data['printer_uri'] # "tcp://192.168.0.10:9100"
font_folder = "./static/fonts"
selected_backend = guess_backend(BACKEND_STRING_DESCR)
BACKEND_CLASS = backend_factory(selected_backend)['backend_class']
MODEL = data['printer_model'] # "QL-720NW"
DEFAULT_LABEL_SIZE = data['label_size'] # "62x100"
DEFAULT_ORIENTATION = data['orientation'] # "rotated"
FONTS = get_fonts()
if font_folder:
FONTS.update(get_fonts(font_folder))
status = False
try:
context = get_label_context(data['first_name'], data['last_name'], data['company'], data['label_size'])
except LookupError as e:
return status
if context['name'] is None:
return status
if context['company'] is None:
context['company'] = ''
if context['last_name'] is None:
context['name'] = context['first_name']
context['event_name'] = data['event_name']
context['logo'] = data['logo']
context['label_tpl'] = data['label_tpl']
context['ticket_type'] = data['ticket_type']
im = eval('create_label_im_' + data['label_size'])(**context)
if data['debug']:
data['image'] = im
try:
im.save(settings.MEDIA_ROOT + '/cache/label-%s_%s.png'%(data['first_name'], data['last_name']))
except FileNotFoundError:
os.mkdir(settings.MEDIA_ROOT + '/cache')
im.save(settings.MEDIA_ROOT + '/cache/label-%s_%s.png'%(data['first_name'], data['last_name']))
else:
qlr = BrotherQLRaster(MODEL)
rotate = 0 if data['orientation'] == 'standard' else 90
if context['label_size'] == '62x29':
rotate = 0
create_label(qlr, im, context['label_size'], threshold=context['threshold'], cut=True, rotate=rotate)
try:
be = BACKEND_CLASS(BACKEND_STRING_DESCR)
be.write(qlr.data)
be.dispose()
del be
except Exception as e:
return status
status = True
return status
def get_fonts(folder=None):
"""
Scan a folder (or the system) for .ttf / .otf fonts and
return a dictionary of the structure family -> style -> file path
"""
fonts = {}
if folder:
cmd = ['fc-scan', '--format', '"%{file}:%{family}:style=%{style}\n"', folder]
else:
cmd = ['fc-list', ':', 'file', 'family', 'style']
for line in subprocess.check_output(cmd).decode('utf-8').split("\n"):
logger.debug(line)
line.strip()
if not line: continue
if 'otf' not in line and 'ttf' not in line: continue
parts = line.split(':')
path = parts[0]
families = parts[1].strip().split(',')
styles = parts[2].split('=')[1].split(',')
if len(families) == 1 and len(styles) > 1:
families = [families[0]] * len(styles)
elif len(families) > 1 and len(styles) == 1:
styles = [styles[0]] * len(families)
if len(families) != len(styles):
logger.debug("Problem with this font: " + line)
continue
for i in range(len(families)):
try: fonts[families[i]]
except: fonts[families[i]] = dict()
fonts[families[i]][styles[i]] = path
logger.debug("Added this font: " + str((families[i], styles[i], path)))
return fonts
def label_print():
global DEBUG, FONTS, DEFAULT_FONT, MODEL, BACKEND_CLASS, BACKEND_STRING_DESCR, DEFAULT_ORIENTATION, DEFAULT_LABEL_SIZE
printer = "tcp://192.168.11.106:9100"
model = "QL-720NW"
# Default label size is "62x100" (DK-11202) which use "rotated" in orientation.
# If label size in "62x29" (DK-11209), use "standard" in orientation.
default_label_size = "62x100"
default_orientation = "rotated"
font_folder = "./static/fonts"
selected_backend = guess_backend(printer)
BACKEND_CLASS = backend_factory(selected_backend)['backend_class']
BACKEND_STRING_DESCR = printer
MODEL = model
#if default_label_size not in label_sizes:
#parser.error("Invalid --default-label-size. Please choose on of the following:\n:" + " ".join(label_sizes))
DEFAULT_LABEL_SIZE = default_label_size
DEFAULT_ORIENTATION = default_orientation
FONTS = get_fonts()
if font_folder:
FONTS.update(get_fonts(font_folder))