blob: d2735794521ffba68686c4a8e8ca595d910cd5d0 [file] [log] [blame]
Behdad Esfahbod75858f62013-08-19 13:01:45 -04001# Copyright 2013 Google, Inc. All Rights Reserved.
2#
Behdad Esfahbod75858f62013-08-19 13:01:45 -04003# Google Author(s): Behdad Esfahbod
4
Behdad Esfahbodcebcf172013-09-06 18:23:13 -04005"""GUI font inspector.
Behdad Esfahbod75858f62013-08-19 13:01:45 -04006"""
7
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -04008import pygtk
9pygtk.require('2.0')
10import gtk
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040011import sys
Behnam Esfahbodf7e0c672013-09-03 21:56:30 -070012import array
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040013
Behdad Esfahbodc195a6b2013-09-04 10:35:10 -040014from fontTools import misc, ttLib, cffLib
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040015
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040016
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040017class Row(object):
Behdad Esfahbod22849902013-09-03 18:26:29 -040018 def __init__(self, parent, index, key, value, font):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040019 self._parent = parent
20 self._index = index
21 self._key = key
22 self._value = value
Behdad Esfahbod22849902013-09-03 18:26:29 -040023 self._font = font
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040024
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070025 if isinstance(value, ttLib.TTFont):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040026 self._add_font(value)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040027 return
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040028
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040029 if not isinstance(value, basestring):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040030 # Try sequences
Behdad Esfahbod78b74942013-08-17 13:08:33 -040031 is_sequence = True
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040032 try:
33 len(value)
34 iter(value)
Behdad Esfahbod70132792013-08-17 12:31:27 -040035 # It's hard to differentiate list-type sequences
36 # from dict-type ones. Try fetching item 0.
37 value[0]
Behdad Esfahbod583ce632013-09-04 09:56:51 -040038 except (TypeError, AttributeError, KeyError, IndexError):
Behdad Esfahbod78b74942013-08-17 13:08:33 -040039 is_sequence = False
40 if is_sequence:
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040041 self._add_list(key, value)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040042 return
Behdad Esfahbod70132792013-08-17 12:31:27 -040043 if hasattr(value, '__dict__'):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040044 self._add_object(key, value)
Behdad Esfahbod70132792013-08-17 12:31:27 -040045 return
46 if hasattr(value, 'items'):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040047 self._add_dict(key, value)
Behdad Esfahbod70132792013-08-17 12:31:27 -040048 return
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040049
Behdad Esfahbodaa55d752013-08-17 12:50:18 -040050 if isinstance(value, basestring):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040051 self._value_str = '"'+value+'"'
52 self._children = []
Behdad Esfahbodaa55d752013-08-17 12:50:18 -040053 return
54
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040055 # Everything else
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040056 self._children = []
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040057
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040058 def _filter_items(self):
59 items = []
60 for k,v in self._items:
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070061 if isinstance(v, ttLib.TTFont):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040062 continue
63 if k in ['reader', 'file', 'tableTag', 'compileStatus', 'recurse']:
64 continue
65 if isinstance(k, basestring) and k[0] == '_':
66 continue
67 items.append((k,v))
68 self._items = items
69
70 def _add_font(self, font):
71 self._items = [(tag,font[tag]) for tag in font.keys()]
72
73 def _add_object(self, key, value):
74 # Make sure item is decompiled
75 try:
Behdad Esfahbodac10d812013-09-03 18:29:58 -040076 value["asdf"]
Behdad Esfahbod227a4952013-10-02 17:52:04 -040077 except (AttributeError, KeyError, TypeError, ttLib.TTLibError):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040078 pass
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070079 if isinstance(value, ttLib.getTableModule('glyf').Glyph):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040080 # Glyph type needs explicit expanding to be useful
Behdad Esfahbod22849902013-09-03 18:26:29 -040081 value.expand(self._font['glyf'])
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070082 if isinstance(value, misc.psCharStrings.T2CharString):
Behdad Esfahbod453ceed2013-08-18 16:53:57 -040083 try:
84 value.decompile()
85 except TypeError: # Subroutines can't be decompiled
86 pass
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070087 if isinstance(value, cffLib.BaseDict):
Behdad Esfahbode3314312013-08-18 16:40:18 -040088 for k in value.rawDict.keys():
89 getattr(value, k)
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070090 if isinstance(value, cffLib.Index):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040091 # Load all items
92 for i in range(len(value)):
93 value[i]
94 # Discard offsets as should not be needed anymore
95 if hasattr(value, 'offsets'):
96 del value.offsets
97
98 self._value_str = value.__class__.__name__
Behnam Esfahbodf7e0c672013-09-03 21:56:30 -070099 if isinstance(value, ttLib.tables.DefaultTable.DefaultTable):
100 self._value_str += ' (%d Bytes)' % self._font.reader.tables[key].length
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400101 self._items = sorted(value.__dict__.items())
102 self._filter_items()
103
104 def _add_dict(self, key, value):
105 self._value_str = '%s of %d items' % (value.__class__.__name__, len(value))
106 self._items = sorted(value.items())
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400107
108 def _add_list(self, key, value):
109 if len(value) and len(value) <= 32:
110 self._value_str = str(value)
111 else:
112 self._value_str = '%s of %d items' % (value.__class__.__name__,
113 len(value))
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400114 self._items = list(enumerate(value))
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400115
116 def __len__(self):
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400117 if hasattr(self, '_children'):
118 return len(self._children)
119 if hasattr(self, '_items'):
120 return len(self._items)
121 assert 0
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400122
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400123 def _ensure_children(self):
124 if hasattr(self, '_children'):
125 return
126 children = []
127 for i,(k,v) in enumerate(self._items):
Behdad Esfahbod22849902013-09-03 18:26:29 -0400128 children.append(Row(self, i, k, v, self._font))
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400129 self._children = children
130 del self._items
131
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400132 def __getitem__(self, n):
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400133 if n >= len(self):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400134 return None
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400135 if not hasattr(self, '_children'):
136 self._children = [None] * len(self)
137 c = self._children[n]
138 if c is None:
139 k,v = self._items[n]
Behdad Esfahbod22849902013-09-03 18:26:29 -0400140 c = self._children[n] = Row(self, n, k, v, self._font)
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400141 self._items[n] = None
142 return c
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400143
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400144 def get_parent(self):
145 return self._parent
146
147 def get_index(self):
148 return self._index
149
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400150 def get_key(self):
151 return self._key
152
153 def get_value(self):
154 return self._value
155
156 def get_value_str(self):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400157 if hasattr(self,'_value_str'):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400158 return self._value_str
159 return str(self._value)
160
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400161class FontTreeModel(gtk.GenericTreeModel):
162
163 __gtype_name__ = 'FontTreeModel'
164
165 def __init__(self, font):
166 super(FontTreeModel, self).__init__()
167 self._columns = (str, str)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400168 self.font = font
Behdad Esfahbod22849902013-09-03 18:26:29 -0400169 self._root = Row(None, 0, "font", font, font)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400170
171 def on_get_flags(self):
172 return 0
173
174 def on_get_n_columns(self):
175 return len(self._columns)
176
177 def on_get_column_type(self, index):
178 return self._columns[index]
179
180 def on_get_iter(self, path):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400181 rowref = self._root
182 while path:
183 rowref = rowref[path[0]]
184 path = path[1:]
185 return rowref
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400186
187 def on_get_path(self, rowref):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400188 path = []
189 while rowref != self._root:
190 path.append(rowref.get_index())
191 rowref = rowref.get_parent()
192 path.reverse()
193 return tuple(path)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400194
195 def on_get_value(self, rowref, column):
196 if column == 0:
197 return rowref.get_key()
198 else:
199 return rowref.get_value_str()
200
201 def on_iter_next(self, rowref):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400202 return rowref.get_parent()[rowref.get_index() + 1]
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400203
204 def on_iter_children(self, rowref):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400205 return rowref[0]
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400206
207 def on_iter_has_child(self, rowref):
208 return bool(len(rowref))
209
210 def on_iter_n_children(self, rowref):
211 return len(rowref)
212
213 def on_iter_nth_child(self, rowref, n):
214 if not rowref: rowref = self._root
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400215 return rowref[n]
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400216
217 def on_iter_parent(self, rowref):
218 return rowref.get_parent()
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400219
Behdad Esfahbodcebcf172013-09-06 18:23:13 -0400220class Inspect:
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400221
Behdad Esfahbod75858f62013-08-19 13:01:45 -0400222 def _delete_event(self, widget, event, data=None):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400223 gtk.main_quit()
224 return False
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400225
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400226 def __init__(self, fontfile):
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400227
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400228 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
Behdad Esfahbodcebcf172013-09-06 18:23:13 -0400229 self.window.set_title("%s - pyftinspect" % fontfile)
Behdad Esfahbod75858f62013-08-19 13:01:45 -0400230 self.window.connect("delete_event", self._delete_event)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400231 self.window.set_size_request(400, 600)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400232
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400233 self.scrolled_window = gtk.ScrolledWindow()
234 self.window.add(self.scrolled_window)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400235
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -0700236 self.font = ttLib.TTFont(fontfile)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400237 self.treemodel = FontTreeModel(self.font)
238 self.treeview = gtk.TreeView(self.treemodel)
239 #self.treeview.set_reorderable(True)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400240
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400241 for i in range(2):
Behnam Esfahbod25fc2682013-09-03 20:27:14 -0700242 col_name = ('Key', 'Value')[i]
243 col = gtk.TreeViewColumn(col_name)
244 col.set_sort_column_id(-1)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400245 self.treeview.append_column(col)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400246
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400247 cell = gtk.CellRendererText()
248 col.pack_start(cell, True)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400249 col.add_attribute(cell, 'text', i)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400250
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400251 self.treeview.set_search_column(1)
252 self.scrolled_window.add(self.treeview)
253 self.window.show_all()
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400254
Behdad Esfahbod11581182013-08-29 18:30:26 -0400255def main(args):
256 if len(args) < 1:
Khaled Hosny0e37f892013-09-12 11:13:39 +0200257 print >>sys.stderr, "usage: pyftinspect font..."
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400258 sys.exit(1)
Behdad Esfahbod11581182013-08-29 18:30:26 -0400259 for arg in args:
Behdad Esfahbodcebcf172013-09-06 18:23:13 -0400260 Inspect(arg)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400261 gtk.main()
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400262
263if __name__ == "__main__":
Behdad Esfahbod11581182013-08-29 18:30:26 -0400264 main(sys.argv[1:])