This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fontmonk.py
190 lines (157 loc) · 6.74 KB
/
fontmonk.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
#!/usr/bin/python
import gtk, pygtk, gobject, urllib, os, subprocess, re
gobject.threads_init()
class FontMonk:
# This list is taken from the FontForge man page.
font_exts = {
"bdf": "Glyph Bitmap Dist.",
"bin": "Mac Resource",
"otf": "OpenType",
"sfd": "Spline Font DB",
"pfa": "PS ASCII",
"pfb": "PS binary",
"svg": "SVG fonts",
"pk": "TeX bitmap",
"ttf": "TrueType",
"ttc": "TrueType",
"pcf": "X11 bitmap"
}
def g(self, id):
return self.b.get_object(id)
def __init__(self):
self.b = gtk.Builder()
self.b.add_from_file('ui.glade')
self.w = self.g('mainwin')
self.ph = self.g('prog_header')
self.ps = self.g('prog_subhead')
self.pl = self.g('prog_line')
self.pb = self.g('progress')
# Set up DND
self.dndsetup()
# Set up chooser
self.chooseboxsetup()
self.b.connect_signals(self)
self.w.show_all()
def dndsetup(self):
self.treeview = self.g('mainview')
# Model: Stock icon (shown), label (marked up, shown), path (not shown)
self.treeview.set_model(gtk.ListStore(str, str, str))
x = gtk.CellRendererText()
x.markup = True
self.treeview.append_column(gtk.TreeViewColumn('Icon', gtk.CellRendererPixbuf(), stock_id=0))
self.treeview.append_column(gtk.TreeViewColumn('URL', x, markup=1))
self.treeview.enable_model_drag_dest([
('MY_TREE_MODEL_ROW', gtk.TARGET_SAME_WIDGET, 0),
('text/plain', 0, 1),
('TEXT', 0, 2),
('STRING', 0, 3),
],
gtk.gdk.ACTION_DEFAULT)
self.treeview.get_selection().set_mode(gtk.SELECTION_MULTIPLE)
self.treeview.connect("drag_data_received", self.drag_data_received_data)
def chooseboxsetup(self):
self.ch = self.g('choosebox')
# Model: Label (shown), extension (not shown)
model = gtk.ListStore(str, str)
self.ch.set_model(model)
self.cell = gtk.CellRendererText()
self.ch.pack_start(self.cell, True)
self.ch.add_attribute(self.cell, 'text', 0)
fx = self.font_exts.items()
fx.sort()
# TODO: Could be prettier.
i = 0
for k, v in fx:
model.append([("Output %s (%s)" % (v, k)), k])
if (k == 'ttf'):
j = i
i += 1
self.ch.set_active(j)
def makescript(self):
# Thanks to Thomas Maier for the 'quick and dirty hack'
model = self.treeview.get_model()
self.count = len(model)
self.t = self.ch.get_model().get_value(self.ch.get_active_iter(), 1)
iter = model.get_iter_root()
s = ""
while iter:
v = model.get_value(iter, 2)
s += 'Print("Opening %s");Open("%s");Print("Saving %s.%s");Generate("%s.%s");' % (v, v, v, self.t, v, self.t)
iter = model.iter_next(iter)
self.runscript(s)
def runscript(self, s):
self.ph.set_markup('<b><big>Converting <span>%s</span> files...</big></b>' % self.count)
self.ps.set_markup('FontMonk is now converting your fonts into <span>%s</span>.' % self.t.upper())
p = subprocess.Popen('fontforge -lang=ff -c \'%s\' &' % s, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout_id = gobject.io_add_watch(p.stdout, gobject.IO_IN, self.gotline)
hup_id = gobject.io_add_watch(p.stdout, gobject.IO_HUP, self.hup)
def gotline(self, file, dunno):
newdata = file.read().split('\n')
if (newdata[-2]):
self.pl.set_text(str(newdata[-2]))
if (newdata[-1]):
self.pl.set_text(str(newdata[-1]))
self.pb.pulse()
return False
def hup(self, file, dunno):
self.pl.hide()
self.ps.set_text("Conversion finished.")
self.pb.set_fraction(1)
return False
# Callback handlers
def quit(self, widget):
gtk.main_quit()
def del_button_clicked_cb(self, widget):
sel = self.treeview.get_selection()
selection = self.treeview.get_selection()
model, selected = selection.get_selected_rows()
iters = [model.get_iter(path) for path in selected]
for iter in iters:
model.remove(iter)
def add_button_clicked_cb(self, widget):
diag = gtk.FileChooserDialog('Add a font file', None, gtk.FILE_CHOOSER_ACTION_OPEN,(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK))
diag.set_default_response(gtk.RESPONSE_OK)
response = diag.run()
if response == gtk.RESPONSE_OK:
self.addpath(diag.get_filename())
diag.destroy()
def exec_button_clicked_cb(self, widget):
self.pw = self.b.get_object('progresswin')
self.w.hide()
self.pw.show_all()
self.makescript()
def closebutton_clicked_cb(self, widget):
self.pw.hide()
self.w.show()
def clear_button_clicked_cb(self, widget):
self.treeview.get_model().clear()
def about_button_clicked_cb(self, widget):
self.b.get_object('adiag').show()
def adiag_hide(self, widget, dunno):
self.b.get_object('adiag').hide()
def addpath(self, path):
model = self.treeview.get_model()
fname = path.split(os.sep)[-1]
fext = fname.split('.')[-1]
if not os.path.isfile(path):
print data, "couldn't be turned into a path."
return
if not str(fext).lower() in self.font_exts:
print fname, "didn't match a font extension."
return
label = "%s <span color='gray'>%s</span>" % (path.split(os.sep)[-1], self.font_exts[str(fext).lower()])
model.append([gtk.STOCK_FILE, label, path])
def drag_data_received_data(self, treeview, context, x, y, selection,
info, etime):
model = treeview.get_model()
data = str(selection.data)
files = data.splitlines()
for file in files:
path = str(urllib.unquote(file)).replace('file://', '')
self.addpath(path)
return
def main():
fmgui = FontMonk()
gtk.main()
if __name__ == "__main__":
main()