blob: c57e5702c8d29bf4777bdaf814ef30be148c3279 [file] [log] [blame]
Behdad Esfahbod75858f62013-08-19 13:01:45 -04001# Copyright 2013 Google, Inc. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0(the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#
15# Google Author(s): Behdad Esfahbod
16
Behdad Esfahbod11581182013-08-29 18:30:26 -040017"""GUI Font Viewer.
Behdad Esfahbod75858f62013-08-19 13:01:45 -040018"""
19
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040020import pygtk
21pygtk.require('2.0')
22import gtk
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040023import sys
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040024
Behdad Esfahbodaa736d82013-08-17 13:21:47 -040025import fontTools.ttLib
26import fontTools.cffLib
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040027
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040028
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040029class Row(object):
Behdad Esfahbod22849902013-09-03 18:26:29 -040030 def __init__(self, parent, index, key, value, font):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040031 self._parent = parent
32 self._index = index
33 self._key = key
34 self._value = value
Behdad Esfahbod22849902013-09-03 18:26:29 -040035 self._font = font
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040036
Behdad Esfahbodd711d4b2013-08-19 14:46:08 -040037 if isinstance(value, fontTools.ttLib.TTFont):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040038 self._add_font(value)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040039 return
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040040
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040041 if not isinstance(value, basestring):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040042 # Try sequences
Behdad Esfahbod78b74942013-08-17 13:08:33 -040043 is_sequence = True
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040044 try:
45 len(value)
46 iter(value)
Behdad Esfahbod70132792013-08-17 12:31:27 -040047 # It's hard to differentiate list-type sequences
48 # from dict-type ones. Try fetching item 0.
49 value[0]
Behdad Esfahbod78b74942013-08-17 13:08:33 -040050 except TypeError:
51 is_sequence = False
52 except AttributeError:
53 is_sequence = False
54 except KeyError:
55 is_sequence = False
56 except IndexError:
57 is_sequence = False
58 if is_sequence:
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040059 self._add_list(key, value)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040060 return
Behdad Esfahbod70132792013-08-17 12:31:27 -040061 if hasattr(value, '__dict__'):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040062 self._add_object(key, value)
Behdad Esfahbod70132792013-08-17 12:31:27 -040063 return
64 if hasattr(value, 'items'):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040065 self._add_dict(key, value)
Behdad Esfahbod70132792013-08-17 12:31:27 -040066 return
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040067
Behdad Esfahbodaa55d752013-08-17 12:50:18 -040068 if isinstance(value, basestring):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040069 self._value_str = '"'+value+'"'
70 self._children = []
Behdad Esfahbodaa55d752013-08-17 12:50:18 -040071 return
72
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040073 # Everything else
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040074 self._children = []
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040075
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040076 def _filter_items(self):
77 items = []
78 for k,v in self._items:
Behdad Esfahbodd711d4b2013-08-19 14:46:08 -040079 if isinstance(v, fontTools.ttLib.TTFont):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040080 continue
81 if k in ['reader', 'file', 'tableTag', 'compileStatus', 'recurse']:
82 continue
83 if isinstance(k, basestring) and k[0] == '_':
84 continue
85 items.append((k,v))
86 self._items = items
87
88 def _add_font(self, font):
89 self._items = [(tag,font[tag]) for tag in font.keys()]
90
91 def _add_object(self, key, value):
92 # Make sure item is decompiled
93 try:
94 getattr(value, "asdf")
95 except AttributeError:
96 pass
97 if isinstance(value, fontTools.ttLib.getTableModule('glyf').Glyph):
98 # Glyph type needs explicit expanding to be useful
Behdad Esfahbod22849902013-09-03 18:26:29 -040099 value.expand(self._font['glyf'])
Behdad Esfahbod453ceed2013-08-18 16:53:57 -0400100 if isinstance(value, fontTools.misc.psCharStrings.T2CharString):
101 try:
102 value.decompile()
103 except TypeError: # Subroutines can't be decompiled
104 pass
Behdad Esfahbode3314312013-08-18 16:40:18 -0400105 if isinstance(value, fontTools.cffLib.BaseDict):
106 for k in value.rawDict.keys():
107 getattr(value, k)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400108 if isinstance(value, fontTools.cffLib.Index):
109 # Load all items
110 for i in range(len(value)):
111 value[i]
112 # Discard offsets as should not be needed anymore
113 if hasattr(value, 'offsets'):
114 del value.offsets
115
116 self._value_str = value.__class__.__name__
117 self._items = sorted(value.__dict__.items())
118 self._filter_items()
119
120 def _add_dict(self, key, value):
121 self._value_str = '%s of %d items' % (value.__class__.__name__, len(value))
122 self._items = sorted(value.items())
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400123
124 def _add_list(self, key, value):
125 if len(value) and len(value) <= 32:
126 self._value_str = str(value)
127 else:
128 self._value_str = '%s of %d items' % (value.__class__.__name__,
129 len(value))
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400130 self._items = list(enumerate(value))
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400131
132 def __len__(self):
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400133 if hasattr(self, '_children'):
134 return len(self._children)
135 if hasattr(self, '_items'):
136 return len(self._items)
137 assert 0
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400138
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400139 def _ensure_children(self):
140 if hasattr(self, '_children'):
141 return
142 children = []
143 for i,(k,v) in enumerate(self._items):
Behdad Esfahbod22849902013-09-03 18:26:29 -0400144 children.append(Row(self, i, k, v, self._font))
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400145 self._children = children
146 del self._items
147
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400148 def __getitem__(self, n):
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400149 if n >= len(self):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400150 return None
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400151 if not hasattr(self, '_children'):
152 self._children = [None] * len(self)
153 c = self._children[n]
154 if c is None:
155 k,v = self._items[n]
Behdad Esfahbod22849902013-09-03 18:26:29 -0400156 c = self._children[n] = Row(self, n, k, v, self._font)
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400157 self._items[n] = None
158 return c
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400159
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400160 def get_parent(self):
161 return self._parent
162
163 def get_index(self):
164 return self._index
165
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400166 def get_key(self):
167 return self._key
168
169 def get_value(self):
170 return self._value
171
172 def get_value_str(self):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400173 if hasattr(self,'_value_str'):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400174 return self._value_str
175 return str(self._value)
176
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400177class FontTreeModel(gtk.GenericTreeModel):
178
179 __gtype_name__ = 'FontTreeModel'
180
181 def __init__(self, font):
182 super(FontTreeModel, self).__init__()
183 self._columns = (str, str)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400184 self.font = font
Behdad Esfahbod22849902013-09-03 18:26:29 -0400185 self._root = Row(None, 0, "font", font, font)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400186
187 def on_get_flags(self):
188 return 0
189
190 def on_get_n_columns(self):
191 return len(self._columns)
192
193 def on_get_column_type(self, index):
194 return self._columns[index]
195
196 def on_get_iter(self, path):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400197 rowref = self._root
198 while path:
199 rowref = rowref[path[0]]
200 path = path[1:]
201 return rowref
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400202
203 def on_get_path(self, rowref):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400204 path = []
205 while rowref != self._root:
206 path.append(rowref.get_index())
207 rowref = rowref.get_parent()
208 path.reverse()
209 return tuple(path)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400210
211 def on_get_value(self, rowref, column):
212 if column == 0:
213 return rowref.get_key()
214 else:
215 return rowref.get_value_str()
216
217 def on_iter_next(self, rowref):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400218 return rowref.get_parent()[rowref.get_index() + 1]
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400219
220 def on_iter_children(self, rowref):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400221 return rowref[0]
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400222
223 def on_iter_has_child(self, rowref):
224 return bool(len(rowref))
225
226 def on_iter_n_children(self, rowref):
227 return len(rowref)
228
229 def on_iter_nth_child(self, rowref, n):
230 if not rowref: rowref = self._root
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400231 return rowref[n]
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400232
233 def on_iter_parent(self, rowref):
234 return rowref.get_parent()
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400235
Behdad Esfahbod11581182013-08-29 18:30:26 -0400236class FontView:
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400237
Behdad Esfahbod75858f62013-08-19 13:01:45 -0400238 def _delete_event(self, widget, event, data=None):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400239 gtk.main_quit()
240 return False
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400241
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400242 def __init__(self, fontfile):
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400243
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400244 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
Behdad Esfahbod11581182013-08-29 18:30:26 -0400245 self.window.set_title("%s - FontView" % fontfile)
Behdad Esfahbod75858f62013-08-19 13:01:45 -0400246 self.window.connect("delete_event", self._delete_event)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400247 self.window.set_size_request(400, 600)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400248
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400249 self.scrolled_window = gtk.ScrolledWindow()
250 self.window.add(self.scrolled_window)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400251
Behdad Esfahbodd711d4b2013-08-19 14:46:08 -0400252 self.font = fontTools.ttLib.TTFont(fontfile)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400253 self.treemodel = FontTreeModel(self.font)
254 self.treeview = gtk.TreeView(self.treemodel)
255 #self.treeview.set_reorderable(True)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400256
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400257 for i in range(2):
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400258
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400259 col = gtk.TreeViewColumn('Column %d' % i)
Behdad Esfahbod9c64ded2013-08-17 13:40:12 -0400260
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400261 self.treeview.append_column(col)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400262
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400263 cell = gtk.CellRendererText()
264 col.pack_start(cell, True)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400265
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400266 col.add_attribute(cell, 'text', i)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400267
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400268 col.set_sort_column_id(i)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400269
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400270 self.treeview.set_search_column(1)
271 self.scrolled_window.add(self.treeview)
272 self.window.show_all()
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400273
Behdad Esfahbod11581182013-08-29 18:30:26 -0400274def main(args):
275 if len(args) < 1:
276 print >>sys.stderr, "usage: pyftfontview font..."
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400277 sys.exit(1)
Behdad Esfahbod11581182013-08-29 18:30:26 -0400278 for arg in args:
279 viewer = FontView(arg)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400280 gtk.main()
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400281
282if __name__ == "__main__":
Behdad Esfahbod11581182013-08-29 18:30:26 -0400283 main(sys.argv[1:])