forked from jpf/devhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_badge
executable file
·301 lines (259 loc) · 10.2 KB
/
print_badge
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
#!/usr/bin/env python
#
# print_badge
#
# Generates SHDH badges based on a JSON formatted input file.
# Version: Alpha
#
# Copyright (c) 2008, 2009 Joel Franusic, and Adam Marshall Smith
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# TODO:
# - Give helpful error messages when the configuration file is
# missing parts that we expect.
# - Add fancypants python documentation
# - Add a library or wrapper for CUPS, send the PNG directly to CUPS
# - Get the job ID from CUPS,
# wait for the CUPS job to complete before exiting,
# fail if the CUPS job fails
#
import joelgd as gd
#import gd
import os
import sys
import subprocess
import getopt
import unicodedata
import simplejson as json
# set the working directory to the actual path of the script
os.chdir(os.path.dirname(os.path.realpath(sys.argv[0])))
CONFIGURATION_FILE = './configuration/print_badge.config.json'
def print_image(file):
# NOTE: we print with -o fitplot because the pngs we create
# are 72dpi but the printer is 300dpi. We could also
# use imageMagick to change DPI:
# 'convert foo.png -density 300 foo2.png'
# Dymo note: the diskette labels we use have a printable area of:
# 2.10in x 2.54in @ 300 dpi, that's 630 x 762
print_file = ('lp -d DYMO-LabelWriter-400 '
'-o landscape -o fitplot'.split() + [file])
# print_file = ('lp -H test -d DYMO_LabelWriter_400_USB_1 '
# '-o landscape -o fitplot'.split() + [file])
# n:"no print", q:"quick view"
opts, args = getopt.getopt(sys.argv[1:], "nq")
if opts and '-n' in opts[0]:
print "Not executing command:", print_file
elif opts and '-q' in opts[0]:
try:
subprocess.call(['qiv', file])
except:
print "can't find quick image viewer: apt-get install qiv"
else:
subprocess.call(print_file)
def wrapstring(string, nlines):
if (nlines == 1):
return string
# find index of first wrapping point in string
opt_wrap_pt = len(string)/nlines
prev_space = string.rfind(' ', 0, opt_wrap_pt) # -1 if not found
next_space = string.find(' ', opt_wrap_pt) # -1 if not found
# choose the space that is closest to the optimal wrapping point
if (next_space > 0) and ((opt_wrap_pt - prev_space) >
(next_space - opt_wrap_pt)):
split_point = next_space
else:
split_point = prev_space
if split_point < 0:
return string
ns = "%s\n%s" % (string[:split_point],
wrapstring(string[split_point+1:], nlines - 1))
#print nlines,':',ns,';'
#print ns
return ns
# Shorthand for image positions.
# B = Bottom, T = Top, L = Left, R = Right, X = x, Y = y
(BL_X, BL_Y, BR_X, BR_Y, TL_X, TL_Y, TR_X, TR_Y) = (0, 1, 2, 3, 4, 5, 6, 7)
def get_ideal_text_size(im, width, height, font, text):
size = 100 # arbitrary initial value, scaled to fit below
print "get_bounding_rect(", font, size, 0, (0, 0), text
p = im.get_bounding_rect(font, size, 0, (0, 0), text)
print "get_bounding_rect results: ", p
width_ratio = (float(width) / float(p[BR_X] - p[BL_X]))
height_ratio = (float(height) / float(p[BR_Y] - p[TR_Y]))
return_size = size * min(width_ratio, height_ratio)
#print "\ntext:", text, "\nsize:", return_size
return return_size
# Implement next:
# - Margins: top, bottom, left, right
# - Alignment: left, right, center
def place_text(image, (x, y), item):
input = {}
input['x'] = x
input['y'] = y
input['wraptext'] = 0
(input['width'], input['height']) = image.size()
for val in ["x", "y", "width", "height", "wraptext"]:
try:
item[val]
except KeyError:
continue
if item[val]:
input[val] = item[val]
color = image.colorAllocate(item["color"])
bb_wid = input["width"]
bb_ht = input["height"]
font = item["font"]
text = item["string"]
if input['wraptext']:
best_size = -1
nlines = 1
# if the line of text is too wide, it'll be shrunk to fit.
# when we split it, the font size will increase until height
# becomes the dominating constraint, so we keep wrapping long as
# the font size keeps increasing
while 1:
wraptext = wrapstring(text, nlines)
size = get_ideal_text_size(image, bb_wid, bb_ht, font, wraptext)
if size <= best_size:
break
best_size = size
best_text = wraptext
nlines += 1
size = best_size
text = best_text
else:
size = get_ideal_text_size(image, bb_wid, bb_ht, font, text)
p = image.get_bounding_rect(font, size, 0, (0, 0), text)
y = p[TR_Y]*-1
str_x = input["x"]
str_y = input["y"] + y
rv = image.string_ttf(font, size, 0, (str_x, str_y), text, color)
return (input["x"], rv[BR_Y])
def make_qrcode_png(qrfile, fname, lname, email, note):
#print "qrmake: %s %s %s %s %s" % (qrfile,fname,lname,email,note)
# vcard format is too verbose...
# especially the rigorous version created by python's vobject.vCard()
#import vobject
#j = vobject.vCard()
#j.add('n')
#j.n.value = vobject.vcard.Name(given=fname, family=lname)
#j.add('fn')
#j.fn.value = fname + " " + lname
#j.add('email')
#j.email.value = email
#j.email.type_param = 'INTERNET'
#j.add('note')
#j.note.value = note
#bad_text = j.serialize()
## android's barcode reader app doesn't like commas to be escaped
#good_text = bad_text.replace("\\,", ",")
#print "qr.vc: %d, %s\n" % (len(good_text), good_text)
#subprocess.call(['qrencode', '--size=3', '-m0', '-o', qrfile , good_text])
#subprocess.call(['identify', qrfile])
mecard = "MECARD:N:%s,%s;EMAIL:%s;NOTE:%s;;" % (
lname.replace(',', ''),
fname.replace(',', ''),
email,
note.replace(';', ','))
subprocess.call(['qrencode', '--size=5', '-m0', '-o', qrfile, mecard])
print "qr.me: %d, %s\n" % (len(mecard), mecard)
subprocess.call(['identify', qrfile])
def main():
configuration_file = open(CONFIGURATION_FILE)
conf = json.load(configuration_file)
# read card data in from stdin
stdin = sys.stdin.read()
card = json.loads(stdin)
# merge in the card data we just got from STDIN
# into the default configuration
for key, value in card.iteritems():
if key in conf:
conf[key]["string"] = value
filename = "./completed/%s_shdh%s" % (card["key"], card["shdh_number"])
conf["badge"]["file"] = "%s.png" % filename
if 'qr_code' in conf["badge"]["design"]:
try:
qrfile = "%s.qrcode.png" % filename
make_qrcode_png(qrfile,
card['first_name'],
card['last_name'],
card['key'],
'SHDH %(shdh_number)s -- %(tags)s' % card)
conf["qr_code"]["file"] = qrfile
except:
message = ("oh well, no qr code... "
"try: 'apt-get install qrencode' \n python says: ")
print message, sys.exc_info()
try:
conf["badge"]["remove_accents"]
except KeyError:
conf["badge"]["remove_accents"] = None
os.environ["GDFONTPATH"] = conf["badge"]["font_path"]
im = gd.image((conf["badge"]["width"], conf["badge"]["height"]))
print im
im.colorAllocate(conf["badge"]["color"])
x, y = 0, 0
for item in conf["badge"]["design"]:
# There has to be a better way to test for if a variable is defined
try:
conf[item]["string"]
except KeyError:
conf[item]["string"] = None
try:
conf[item]["file"]
except KeyError:
conf[item]["file"] = None
if conf[item]["file"]:
temporary = gd.image(conf[item]["file"])
ix, iy = conf[item]["x"], conf[item]["y"]
try:
iw, ih = conf[item]["width"], conf[item]["height"]
(tw, th) = temporary.size()
if (tw > iw) or (th > ih):
#### it would be better to preserve the aspect ratio
print "ooops, sloppy shrinking of image in progress..."
temporary.copyResizedTo(im, (ix, iy), (0, 0), (iw, ih))
else:
newx = ix + ((iw - tw) / 2)
newy = iy + ((ih - th) / 2)
temporary.copyTo(im, (newx, newy))
except KeyError:
temporary.copyTo(im, (ix, iy))
elif conf[item]["string"]:
if conf["badge"]["remove_accents"]:
normalized = unicodedata.normalize('NFKD',
conf[item]["string"])
as_ascii = normalized.encode('ascii', 'ignore')
conf[item]["string"] = as_ascii
(x, y) = place_text(im, (x, y), conf[item])
else:
print "ERROR"
badge = open(conf["badge"]["file"], "w")
im.writePng(badge)
badge.close()
print_image(conf["badge"]["file"])
if __name__ == "__main__":
#t1 = time.time()
main()
#t2 = time.time()
#print 'took %0.3f ms' % ((t2-t1)*1000.0)