summaryrefslogtreecommitdiff
path: root/ptbulk.py
blob: 583bb03c5bb84fe569428d9ee374cbacde5c271e (plain)
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
#!/usr/bin/env python
# vim: set expandtab ts=4:

import cairo, pango, pangocairo
import pttarget, ptlayout
from ptlayout import PTLText
import time, sys
import usb.core

layout = ptlayout.PTLHSeq()
layout.spacing = 0
layout.add(ptlayout.PTLHSpace()).spacing = 5
text = layout.add(ptlayout.PTLText())
layout.add(ptlayout.PTLHSpace()).spacing = 5

pttarget.PTUSB.scan()
print 'device list:'
for dev in pttarget.PTUSB.printers:
    print str(dev)
if len(pttarget.PTUSB.printers) != 1:
    print 'no idea which printer to use!'
    sys.exit(1)
dev = pttarget.PTUSB.printers[0]
dev.setup()
dev.refresh_status()
mtype = pttarget.media_types.get(dev.media_type, 'unknown (%02x)' % (dev.media_type))
msize =  '%dx%dmm' % (dev.media_width, dev.media_length) if dev.media_length != 0 else \
         '%dmm continuous tape' % (dev.media_width)
print 'Status: %s, %s %s' % (dev.get_errstr(), mtype, msize)

tape = dev.find_tape()

for line in sys.stdin.readlines():
    line = line.strip()

    # insert code here

    text.text = line

    w, h = layout.prep_size(tape.pixels)
    if w < 32:
        w = 32
    label_surface = cairo.ImageSurface(cairo.FORMAT_A1, w, tape.pixels)
    ctx = cairo.Context(label_surface)
    ctx.set_operator(cairo.OPERATOR_SOURCE)
    final_w = layout.render(ctx, tape.pixels)
    print 'text: "%s" label width: %d' % (line, final_w)

    data = pttarget.PTLabelData()
    data.addcairo(label_surface, offset = tape.offset)
    dev.send(data.get(True))