blob: 875736dd1d708d45fc8d3ac8136db0e5d97c9ff0 [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 Esfahbod1ae29592014-01-14 15:07:50 +08008from __future__ import print_function, division, absolute_import
Behdad Esfahbodb21c9032013-11-27 17:27:22 -05009from fontTools.misc.py23 import *
10from fontTools import misc, ttLib, cffLib
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040011import pygtk
12pygtk.require('2.0')
13import gtk
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040014import sys
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040015
Behdad Esfahbod3e78b602013-11-27 05:52:25 -050016
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040017
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040018class Row(object):
Behdad Esfahbod22849902013-09-03 18:26:29 -040019 def __init__(self, parent, index, key, value, font):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040020 self._parent = parent
21 self._index = index
22 self._key = key
23 self._value = value
Behdad Esfahbod22849902013-09-03 18:26:29 -040024 self._font = font
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040025
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070026 if isinstance(value, ttLib.TTFont):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040027 self._add_font(value)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040028 return
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040029
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040030 if not isinstance(value, basestring):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040031 # Try sequences
Behdad Esfahbod78b74942013-08-17 13:08:33 -040032 is_sequence = True
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040033 try:
34 len(value)
35 iter(value)
Behdad Esfahbod70132792013-08-17 12:31:27 -040036 # It's hard to differentiate list-type sequences
37 # from dict-type ones. Try fetching item 0.
38 value[0]
Behdad Esfahbod583ce632013-09-04 09:56:51 -040039 except (TypeError, AttributeError, KeyError, IndexError):
Behdad Esfahbod78b74942013-08-17 13:08:33 -040040 is_sequence = False
41 if is_sequence:
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040042 self._add_list(key, value)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040043 return
Behdad Esfahbod70132792013-08-17 12:31:27 -040044 if hasattr(value, '__dict__'):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040045 self._add_object(key, value)
Behdad Esfahbod70132792013-08-17 12:31:27 -040046 return
47 if hasattr(value, 'items'):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040048 self._add_dict(key, value)
Behdad Esfahbod70132792013-08-17 12:31:27 -040049 return
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040050
Behdad Esfahbodaa55d752013-08-17 12:50:18 -040051 if isinstance(value, basestring):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040052 self._value_str = '"'+value+'"'
53 self._children = []
Behdad Esfahbodaa55d752013-08-17 12:50:18 -040054 return
55
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040056 # Everything else
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040057 self._children = []
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040058
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040059 def _filter_items(self):
60 items = []
61 for k,v in self._items:
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070062 if isinstance(v, ttLib.TTFont):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040063 continue
64 if k in ['reader', 'file', 'tableTag', 'compileStatus', 'recurse']:
65 continue
66 if isinstance(k, basestring) and k[0] == '_':
67 continue
68 items.append((k,v))
69 self._items = items
70
71 def _add_font(self, font):
72 self._items = [(tag,font[tag]) for tag in font.keys()]
73
74 def _add_object(self, key, value):
75 # Make sure item is decompiled
76 try:
Behdad Esfahbodac10d812013-09-03 18:29:58 -040077 value["asdf"]
Behdad Esfahbod227a4952013-10-02 17:52:04 -040078 except (AttributeError, KeyError, TypeError, ttLib.TTLibError):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040079 pass
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070080 if isinstance(value, ttLib.getTableModule('glyf').Glyph):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040081 # Glyph type needs explicit expanding to be useful
Behdad Esfahbod22849902013-09-03 18:26:29 -040082 value.expand(self._font['glyf'])
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070083 if isinstance(value, misc.psCharStrings.T2CharString):
Behdad Esfahbod453ceed2013-08-18 16:53:57 -040084 try:
85 value.decompile()
86 except TypeError: # Subroutines can't be decompiled
87 pass
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070088 if isinstance(value, cffLib.BaseDict):
Behdad Esfahbode3314312013-08-18 16:40:18 -040089 for k in value.rawDict.keys():
90 getattr(value, k)
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070091 if isinstance(value, cffLib.Index):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040092 # Load all items
93 for i in range(len(value)):
94 value[i]
95 # Discard offsets as should not be needed anymore
96 if hasattr(value, 'offsets'):
97 del value.offsets
98
99 self._value_str = value.__class__.__name__
Behnam Esfahbodf7e0c672013-09-03 21:56:30 -0700100 if isinstance(value, ttLib.tables.DefaultTable.DefaultTable):
101 self._value_str += ' (%d Bytes)' % self._font.reader.tables[key].length
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400102 self._items = sorted(value.__dict__.items())
103 self._filter_items()
104
105 def _add_dict(self, key, value):
106 self._value_str = '%s of %d items' % (value.__class__.__name__, len(value))
107 self._items = sorted(value.items())
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400108
109 def _add_list(self, key, value):
110 if len(value) and len(value) <= 32:
111 self._value_str = str(value)
112 else:
113 self._value_str = '%s of %d items' % (value.__class__.__name__,
114 len(value))
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400115 self._items = list(enumerate(value))
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400116
117 def __len__(self):
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400118 if hasattr(self, '_children'):
119 return len(self._children)
120 if hasattr(self, '_items'):
121 return len(self._items)
Behdad Esfahbod153ec402013-12-04 01:15:46 -0500122 assert False
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400123
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400124 def _ensure_children(self):
125 if hasattr(self, '_children'):
126 return
127 children = []
128 for i,(k,v) in enumerate(self._items):
Behdad Esfahbod22849902013-09-03 18:26:29 -0400129 children.append(Row(self, i, k, v, self._font))
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400130 self._children = children
131 del self._items
132
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400133 def __getitem__(self, n):
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400134 if n >= len(self):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400135 return None
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400136 if not hasattr(self, '_children'):
137 self._children = [None] * len(self)
138 c = self._children[n]
139 if c is None:
140 k,v = self._items[n]
Behdad Esfahbod22849902013-09-03 18:26:29 -0400141 c = self._children[n] = Row(self, n, k, v, self._font)
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400142 self._items[n] = None
143 return c
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400144
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400145 def get_parent(self):
146 return self._parent
147
148 def get_index(self):
149 return self._index
150
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400151 def get_key(self):
152 return self._key
153
154 def get_value(self):
155 return self._value
156
157 def get_value_str(self):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400158 if hasattr(self,'_value_str'):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400159 return self._value_str
160 return str(self._value)
161
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400162class FontTreeModel(gtk.GenericTreeModel):
163
164 __gtype_name__ = 'FontTreeModel'
165
166 def __init__(self, font):
167 super(FontTreeModel, self).__init__()
168 self._columns = (str, str)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400169 self.font = font
Behdad Esfahbod22849902013-09-03 18:26:29 -0400170 self._root = Row(None, 0, "font", font, font)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400171
172 def on_get_flags(self):
173 return 0
174
175 def on_get_n_columns(self):
176 return len(self._columns)
177
178 def on_get_column_type(self, index):
179 return self._columns[index]
180
181 def on_get_iter(self, path):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400182 rowref = self._root
183 while path:
184 rowref = rowref[path[0]]
185 path = path[1:]
186 return rowref
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400187
188 def on_get_path(self, rowref):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400189 path = []
190 while rowref != self._root:
191 path.append(rowref.get_index())
192 rowref = rowref.get_parent()
193 path.reverse()
194 return tuple(path)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400195
196 def on_get_value(self, rowref, column):
197 if column == 0:
198 return rowref.get_key()
199 else:
200 return rowref.get_value_str()
201
202 def on_iter_next(self, rowref):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400203 return rowref.get_parent()[rowref.get_index() + 1]
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400204
205 def on_iter_children(self, rowref):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400206 return rowref[0]
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400207
208 def on_iter_has_child(self, rowref):
209 return bool(len(rowref))
210
211 def on_iter_n_children(self, rowref):
212 return len(rowref)
213
214 def on_iter_nth_child(self, rowref, n):
215 if not rowref: rowref = self._root
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400216 return rowref[n]
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400217
218 def on_iter_parent(self, rowref):
219 return rowref.get_parent()
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400220
Behdad Esfahbodb475c032013-11-28 14:26:48 -0500221class Inspect(object):
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400222
Behdad Esfahbod75858f62013-08-19 13:01:45 -0400223 def _delete_event(self, widget, event, data=None):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400224 gtk.main_quit()
225 return False
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400226
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400227 def __init__(self, fontfile):
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400228
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400229 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
Behdad Esfahbodcebcf172013-09-06 18:23:13 -0400230 self.window.set_title("%s - pyftinspect" % fontfile)
Behdad Esfahbod75858f62013-08-19 13:01:45 -0400231 self.window.connect("delete_event", self._delete_event)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400232 self.window.set_size_request(400, 600)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400233
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400234 self.scrolled_window = gtk.ScrolledWindow()
235 self.window.add(self.scrolled_window)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400236
Behdad Esfahbod283fb262013-12-16 00:50:48 -0500237 self.font = ttLib.TTFont(fontfile, lazy=True)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400238 self.treemodel = FontTreeModel(self.font)
239 self.treeview = gtk.TreeView(self.treemodel)
240 #self.treeview.set_reorderable(True)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400241
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400242 for i in range(2):
Behnam Esfahbod25fc2682013-09-03 20:27:14 -0700243 col_name = ('Key', 'Value')[i]
244 col = gtk.TreeViewColumn(col_name)
245 col.set_sort_column_id(-1)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400246 self.treeview.append_column(col)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400247
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400248 cell = gtk.CellRendererText()
249 col.pack_start(cell, True)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400250 col.add_attribute(cell, 'text', i)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400251
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400252 self.treeview.set_search_column(1)
253 self.scrolled_window.add(self.treeview)
254 self.window.show_all()
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400255
Behdad Esfahbod11581182013-08-29 18:30:26 -0400256def main(args):
257 if len(args) < 1:
Behdad Esfahbodb21c9032013-11-27 17:27:22 -0500258 print("usage: pyftinspect font...", file=sys.stderr)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400259 sys.exit(1)
Behdad Esfahbod11581182013-08-29 18:30:26 -0400260 for arg in args:
Behdad Esfahbodcebcf172013-09-06 18:23:13 -0400261 Inspect(arg)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400262 gtk.main()
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400263
264if __name__ == "__main__":
Behdad Esfahbod11581182013-08-29 18:30:26 -0400265 main(sys.argv[1:])