blob: 090cdb771f456c5cd7ffe292ad13575b742b78e5 [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 Esfahbod3e78b602013-11-27 05:52:25 -050016try:
17 basestring
18except NameError:
19 basestring = str
20
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040021
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040022class Row(object):
Behdad Esfahbod22849902013-09-03 18:26:29 -040023 def __init__(self, parent, index, key, value, font):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040024 self._parent = parent
25 self._index = index
26 self._key = key
27 self._value = value
Behdad Esfahbod22849902013-09-03 18:26:29 -040028 self._font = font
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040029
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070030 if isinstance(value, ttLib.TTFont):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040031 self._add_font(value)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040032 return
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040033
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040034 if not isinstance(value, basestring):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040035 # Try sequences
Behdad Esfahbod78b74942013-08-17 13:08:33 -040036 is_sequence = True
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040037 try:
38 len(value)
39 iter(value)
Behdad Esfahbod70132792013-08-17 12:31:27 -040040 # It's hard to differentiate list-type sequences
41 # from dict-type ones. Try fetching item 0.
42 value[0]
Behdad Esfahbod583ce632013-09-04 09:56:51 -040043 except (TypeError, AttributeError, KeyError, IndexError):
Behdad Esfahbod78b74942013-08-17 13:08:33 -040044 is_sequence = False
45 if is_sequence:
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040046 self._add_list(key, value)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040047 return
Behdad Esfahbod70132792013-08-17 12:31:27 -040048 if hasattr(value, '__dict__'):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040049 self._add_object(key, value)
Behdad Esfahbod70132792013-08-17 12:31:27 -040050 return
51 if hasattr(value, 'items'):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040052 self._add_dict(key, value)
Behdad Esfahbod70132792013-08-17 12:31:27 -040053 return
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040054
Behdad Esfahbodaa55d752013-08-17 12:50:18 -040055 if isinstance(value, basestring):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040056 self._value_str = '"'+value+'"'
57 self._children = []
Behdad Esfahbodaa55d752013-08-17 12:50:18 -040058 return
59
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040060 # Everything else
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040061 self._children = []
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040062
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040063 def _filter_items(self):
64 items = []
65 for k,v in self._items:
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070066 if isinstance(v, ttLib.TTFont):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040067 continue
68 if k in ['reader', 'file', 'tableTag', 'compileStatus', 'recurse']:
69 continue
70 if isinstance(k, basestring) and k[0] == '_':
71 continue
72 items.append((k,v))
73 self._items = items
74
75 def _add_font(self, font):
76 self._items = [(tag,font[tag]) for tag in font.keys()]
77
78 def _add_object(self, key, value):
79 # Make sure item is decompiled
80 try:
Behdad Esfahbodac10d812013-09-03 18:29:58 -040081 value["asdf"]
Behdad Esfahbod227a4952013-10-02 17:52:04 -040082 except (AttributeError, KeyError, TypeError, ttLib.TTLibError):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040083 pass
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070084 if isinstance(value, ttLib.getTableModule('glyf').Glyph):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040085 # Glyph type needs explicit expanding to be useful
Behdad Esfahbod22849902013-09-03 18:26:29 -040086 value.expand(self._font['glyf'])
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070087 if isinstance(value, misc.psCharStrings.T2CharString):
Behdad Esfahbod453ceed2013-08-18 16:53:57 -040088 try:
89 value.decompile()
90 except TypeError: # Subroutines can't be decompiled
91 pass
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070092 if isinstance(value, cffLib.BaseDict):
Behdad Esfahbode3314312013-08-18 16:40:18 -040093 for k in value.rawDict.keys():
94 getattr(value, k)
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070095 if isinstance(value, cffLib.Index):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040096 # Load all items
97 for i in range(len(value)):
98 value[i]
99 # Discard offsets as should not be needed anymore
100 if hasattr(value, 'offsets'):
101 del value.offsets
102
103 self._value_str = value.__class__.__name__
Behnam Esfahbodf7e0c672013-09-03 21:56:30 -0700104 if isinstance(value, ttLib.tables.DefaultTable.DefaultTable):
105 self._value_str += ' (%d Bytes)' % self._font.reader.tables[key].length
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400106 self._items = sorted(value.__dict__.items())
107 self._filter_items()
108
109 def _add_dict(self, key, value):
110 self._value_str = '%s of %d items' % (value.__class__.__name__, len(value))
111 self._items = sorted(value.items())
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400112
113 def _add_list(self, key, value):
114 if len(value) and len(value) <= 32:
115 self._value_str = str(value)
116 else:
117 self._value_str = '%s of %d items' % (value.__class__.__name__,
118 len(value))
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400119 self._items = list(enumerate(value))
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400120
121 def __len__(self):
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400122 if hasattr(self, '_children'):
123 return len(self._children)
124 if hasattr(self, '_items'):
125 return len(self._items)
126 assert 0
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400127
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400128 def _ensure_children(self):
129 if hasattr(self, '_children'):
130 return
131 children = []
132 for i,(k,v) in enumerate(self._items):
Behdad Esfahbod22849902013-09-03 18:26:29 -0400133 children.append(Row(self, i, k, v, self._font))
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400134 self._children = children
135 del self._items
136
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400137 def __getitem__(self, n):
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400138 if n >= len(self):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400139 return None
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400140 if not hasattr(self, '_children'):
141 self._children = [None] * len(self)
142 c = self._children[n]
143 if c is None:
144 k,v = self._items[n]
Behdad Esfahbod22849902013-09-03 18:26:29 -0400145 c = self._children[n] = Row(self, n, k, v, self._font)
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400146 self._items[n] = None
147 return c
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400148
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400149 def get_parent(self):
150 return self._parent
151
152 def get_index(self):
153 return self._index
154
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400155 def get_key(self):
156 return self._key
157
158 def get_value(self):
159 return self._value
160
161 def get_value_str(self):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400162 if hasattr(self,'_value_str'):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400163 return self._value_str
164 return str(self._value)
165
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400166class FontTreeModel(gtk.GenericTreeModel):
167
168 __gtype_name__ = 'FontTreeModel'
169
170 def __init__(self, font):
171 super(FontTreeModel, self).__init__()
172 self._columns = (str, str)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400173 self.font = font
Behdad Esfahbod22849902013-09-03 18:26:29 -0400174 self._root = Row(None, 0, "font", font, font)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400175
176 def on_get_flags(self):
177 return 0
178
179 def on_get_n_columns(self):
180 return len(self._columns)
181
182 def on_get_column_type(self, index):
183 return self._columns[index]
184
185 def on_get_iter(self, path):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400186 rowref = self._root
187 while path:
188 rowref = rowref[path[0]]
189 path = path[1:]
190 return rowref
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400191
192 def on_get_path(self, rowref):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400193 path = []
194 while rowref != self._root:
195 path.append(rowref.get_index())
196 rowref = rowref.get_parent()
197 path.reverse()
198 return tuple(path)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400199
200 def on_get_value(self, rowref, column):
201 if column == 0:
202 return rowref.get_key()
203 else:
204 return rowref.get_value_str()
205
206 def on_iter_next(self, rowref):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400207 return rowref.get_parent()[rowref.get_index() + 1]
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400208
209 def on_iter_children(self, rowref):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400210 return rowref[0]
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400211
212 def on_iter_has_child(self, rowref):
213 return bool(len(rowref))
214
215 def on_iter_n_children(self, rowref):
216 return len(rowref)
217
218 def on_iter_nth_child(self, rowref, n):
219 if not rowref: rowref = self._root
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400220 return rowref[n]
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400221
222 def on_iter_parent(self, rowref):
223 return rowref.get_parent()
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400224
Behdad Esfahbodcebcf172013-09-06 18:23:13 -0400225class Inspect:
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400226
Behdad Esfahbod75858f62013-08-19 13:01:45 -0400227 def _delete_event(self, widget, event, data=None):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400228 gtk.main_quit()
229 return False
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400230
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400231 def __init__(self, fontfile):
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400232
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400233 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
Behdad Esfahbodcebcf172013-09-06 18:23:13 -0400234 self.window.set_title("%s - pyftinspect" % fontfile)
Behdad Esfahbod75858f62013-08-19 13:01:45 -0400235 self.window.connect("delete_event", self._delete_event)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400236 self.window.set_size_request(400, 600)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400237
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400238 self.scrolled_window = gtk.ScrolledWindow()
239 self.window.add(self.scrolled_window)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400240
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -0700241 self.font = ttLib.TTFont(fontfile)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400242 self.treemodel = FontTreeModel(self.font)
243 self.treeview = gtk.TreeView(self.treemodel)
244 #self.treeview.set_reorderable(True)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400245
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400246 for i in range(2):
Behnam Esfahbod25fc2682013-09-03 20:27:14 -0700247 col_name = ('Key', 'Value')[i]
248 col = gtk.TreeViewColumn(col_name)
249 col.set_sort_column_id(-1)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400250 self.treeview.append_column(col)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400251
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400252 cell = gtk.CellRendererText()
253 col.pack_start(cell, True)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400254 col.add_attribute(cell, 'text', i)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400255
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400256 self.treeview.set_search_column(1)
257 self.scrolled_window.add(self.treeview)
258 self.window.show_all()
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400259
Behdad Esfahbod11581182013-08-29 18:30:26 -0400260def main(args):
261 if len(args) < 1:
Behdad Esfahbodd7e8d812013-11-27 04:57:17 -0500262 print("usage: pyftinspect font...")
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400263 sys.exit(1)
Behdad Esfahbod11581182013-08-29 18:30:26 -0400264 for arg in args:
Behdad Esfahbodcebcf172013-09-06 18:23:13 -0400265 Inspect(arg)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400266 gtk.main()
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400267
268if __name__ == "__main__":
Behdad Esfahbod11581182013-08-29 18:30:26 -0400269 main(sys.argv[1:])