forked from probonopd/ptouch-770
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptouch-770-gui
141 lines (114 loc) · 4.53 KB
/
ptouch-770-gui
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
#!/usr/bin/env python3
from PIL import Image, ImageFont, ImageDraw
import os, sys, subprocess, argparse
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MyWindow(Gtk.ApplicationWindow):
def __init__(self, app):
Gtk.Window.__init__(self, title="Label", application=app)
self.lines = 0
self.entries = []
self.text = []
self.unprintable = 20
self.height = 84 # 12mm tape (tested)
self.rim = 5
self.font = "FreeSansBold.ttf"
self.set_border_width(10)
self.max_height = self.height + self.unprintable
self.outfile = "/tmp/out.pbm"
self.out = os.path.abspath(self.outfile)
self.invert = False
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
for i in range(6):
entry = Gtk.Entry()
entry.connect("changed", self.on_text_changed)
vbox.pack_start(entry, False, False, 0)
self.entries.append(entry)
fonts = ["FreeSans.ttf", "FreeSansBold.ttf"]
fonts_combo = Gtk.ComboBoxText()
fonts_combo.set_entry_text_column(0)
for currency in fonts:
fonts_combo.append_text(currency)
vbox.pack_start(fonts_combo, False, False, 0)
fonts_combo.connect("changed", self.on_fonts_combo_changed)
self.inverted = Gtk.CheckButton("Invert")
self.inverted.show()
vbox.pack_start(self.inverted, False, False, 0)
self.inverted.connect("toggled", self.on_button_toggled, "2")
self.image = Gtk.Image()
self.image.set_from_file(self.outfile)
vbox.pack_start(self.image, False, False, 0)
self.button = Gtk.Button.new_with_label("Print")
self.button.connect("clicked", self.on_click_me_clicked)
vbox.pack_start(self.button, False, False, 0)
self.add(vbox)
def on_fonts_combo_changed(self, combo):
tree_iter = combo.get_active_iter()
if tree_iter is not None:
model = combo.get_model()
row_id, name = model[tree_iter][:2]
print("Selected: ID=%s" % (row_id))
self.font = row_id
else:
entry = combo.get_child()
print("Entered: %s" % entry.get_text())
self.on_text_changed(None)
def on_button_toggled(self, button, name):
if button.get_active():
self.invert = True
else:
self.invert = False
print("Button", name, "was toggled")
self.on_text_changed(None)
def on_text_changed(self, entry):
self.lines = 0
self.text = []
# Count the number of lines
for entry in self.entries:
if (entry.get_text() != ""):
self.text.append(entry.get_text())
self.lines = self.lines + 1
fontsize = int(round(self.height / self.lines, 0))
font = ImageFont.truetype(self.font, fontsize)
# Determine the height in pixels based on letter "A"
# rather than the actual text so as to not have varying
# text heights just because some lines contain letters
# such as "A, g" that have a different height
text_unused_width, text_height = font.getsize("A")
# Determine the longest line
max_width = 0
for line in self.text:
text_width, text_unused_height = font.getsize(line)
width = text_width + self.rim * 2
if (width > max_width):
max_width = width
if(self.invert == True):
foreground = 255
background = 0
else:
foreground = 0
background = 255
image = Image.new("1", (max_width, self.max_height), background)
draw = ImageDraw.Draw(image)
text_x = self.rim
text_y = self.unprintable - text_height
for line in self.text:
text_y = text_y + text_height * 0.9
draw.text((text_x, text_y), line, fill=foreground, font=font)
image.save(self.out)
self.image.set_from_file(self.outfile)
def on_click_me_clicked(self, button):
tool = os.path.abspath(os.path.join(os.path.dirname(__file__), "ptouch-770-write"))
subprocess.run(["sudo", tool, self.out], check=True)
class MyApplication(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self)
def do_activate(self):
win = MyWindow(self)
win.show_all()
def do_startup(self):
Gtk.Application.do_startup(self)
app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)