blob: 9d0a9bc08222d9608efefb8b7b653a3cc2e85228 [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
Behnam Esfahbodf7e0c672013-09-03 21:56:30 -070024import array
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040025
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070026from . import misc, ttLib, 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
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070037 if isinstance(value, 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:
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070079 if isinstance(v, 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:
Behdad Esfahbodac10d812013-09-03 18:29:58 -040094 value["asdf"]
Behnam Esfahbod31547e02013-09-03 21:00:06 -070095 except (AttributeError, KeyError, ttLib.TTLibError):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040096 pass
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070097 if isinstance(value, ttLib.getTableModule('glyf').Glyph):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040098 # Glyph type needs explicit expanding to be useful
Behdad Esfahbod22849902013-09-03 18:26:29 -040099 value.expand(self._font['glyf'])
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -0700100 if isinstance(value, misc.psCharStrings.T2CharString):
Behdad Esfahbod453ceed2013-08-18 16:53:57 -0400101 try:
102 value.decompile()
103 except TypeError: # Subroutines can't be decompiled
104 pass
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -0700105 if isinstance(value, cffLib.BaseDict):
Behdad Esfahbode3314312013-08-18 16:40:18 -0400106 for k in value.rawDict.keys():
107 getattr(value, k)
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -0700108 if isinstance(value, cffLib.Index):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400109 # 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__
Behnam Esfahbodf7e0c672013-09-03 21:56:30 -0700117 if isinstance(value, ttLib.tables.DefaultTable.DefaultTable):
118 self._value_str += ' (%d Bytes)' % self._font.reader.tables[key].length
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400119 self._items = sorted(value.__dict__.items())
120 self._filter_items()
121
122 def _add_dict(self, key, value):
123 self._value_str = '%s of %d items' % (value.__class__.__name__, len(value))
124 self._items = sorted(value.items())
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400125
126 def _add_list(self, key, value):
127 if len(value) and len(value) <= 32:
128 self._value_str = str(value)
129 else:
130 self._value_str = '%s of %d items' % (value.__class__.__name__,
131 len(value))
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400132 self._items = list(enumerate(value))
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400133
134 def __len__(self):
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400135 if hasattr(self, '_children'):
136 return len(self._children)
137 if hasattr(self, '_items'):
138 return len(self._items)
139 assert 0
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400140
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400141 def _ensure_children(self):
142 if hasattr(self, '_children'):
143 return
144 children = []
145 for i,(k,v) in enumerate(self._items):
Behdad Esfahbod22849902013-09-03 18:26:29 -0400146 children.append(Row(self, i, k, v, self._font))
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400147 self._children = children
148 del self._items
149
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400150 def __getitem__(self, n):
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400151 if n >= len(self):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400152 return None
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400153 if not hasattr(self, '_children'):
154 self._children = [None] * len(self)
155 c = self._children[n]
156 if c is None:
157 k,v = self._items[n]
Behdad Esfahbod22849902013-09-03 18:26:29 -0400158 c = self._children[n] = Row(self, n, k, v, self._font)
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400159 self._items[n] = None
160 return c
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400161
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400162 def get_parent(self):
163 return self._parent
164
165 def get_index(self):
166 return self._index
167
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400168 def get_key(self):
169 return self._key
170
171 def get_value(self):
172 return self._value
173
174 def get_value_str(self):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400175 if hasattr(self,'_value_str'):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400176 return self._value_str
177 return str(self._value)
178
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400179class FontTreeModel(gtk.GenericTreeModel):
180
181 __gtype_name__ = 'FontTreeModel'
182
183 def __init__(self, font):
184 super(FontTreeModel, self).__init__()
185 self._columns = (str, str)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400186 self.font = font
Behdad Esfahbod22849902013-09-03 18:26:29 -0400187 self._root = Row(None, 0, "font", font, font)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400188
189 def on_get_flags(self):
190 return 0
191
192 def on_get_n_columns(self):
193 return len(self._columns)
194
195 def on_get_column_type(self, index):
196 return self._columns[index]
197
198 def on_get_iter(self, path):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400199 rowref = self._root
200 while path:
201 rowref = rowref[path[0]]
202 path = path[1:]
203 return rowref
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400204
205 def on_get_path(self, rowref):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400206 path = []
207 while rowref != self._root:
208 path.append(rowref.get_index())
209 rowref = rowref.get_parent()
210 path.reverse()
211 return tuple(path)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400212
213 def on_get_value(self, rowref, column):
214 if column == 0:
215 return rowref.get_key()
216 else:
217 return rowref.get_value_str()
218
219 def on_iter_next(self, rowref):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400220 return rowref.get_parent()[rowref.get_index() + 1]
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400221
222 def on_iter_children(self, rowref):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400223 return rowref[0]
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400224
225 def on_iter_has_child(self, rowref):
226 return bool(len(rowref))
227
228 def on_iter_n_children(self, rowref):
229 return len(rowref)
230
231 def on_iter_nth_child(self, rowref, n):
232 if not rowref: rowref = self._root
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400233 return rowref[n]
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400234
235 def on_iter_parent(self, rowref):
236 return rowref.get_parent()
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400237
Behdad Esfahbod11581182013-08-29 18:30:26 -0400238class FontView:
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400239
Behdad Esfahbod75858f62013-08-19 13:01:45 -0400240 def _delete_event(self, widget, event, data=None):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400241 gtk.main_quit()
242 return False
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400243
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400244 def __init__(self, fontfile):
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400245
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400246 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
Behdad Esfahbod11581182013-08-29 18:30:26 -0400247 self.window.set_title("%s - FontView" % fontfile)
Behdad Esfahbod75858f62013-08-19 13:01:45 -0400248 self.window.connect("delete_event", self._delete_event)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400249 self.window.set_size_request(400, 600)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400250
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400251 self.scrolled_window = gtk.ScrolledWindow()
252 self.window.add(self.scrolled_window)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400253
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -0700254 self.font = ttLib.TTFont(fontfile)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400255 self.treemodel = FontTreeModel(self.font)
256 self.treeview = gtk.TreeView(self.treemodel)
257 #self.treeview.set_reorderable(True)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400258
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400259 for i in range(2):
Behnam Esfahbod25fc2682013-09-03 20:27:14 -0700260 col_name = ('Key', 'Value')[i]
261 col = gtk.TreeViewColumn(col_name)
262 col.set_sort_column_id(-1)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400263 self.treeview.append_column(col)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400264
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400265 cell = gtk.CellRendererText()
266 col.pack_start(cell, True)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400267 col.add_attribute(cell, 'text', i)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400268
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400269 self.treeview.set_search_column(1)
270 self.scrolled_window.add(self.treeview)
271 self.window.show_all()
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400272
Behdad Esfahbod11581182013-08-29 18:30:26 -0400273def main(args):
274 if len(args) < 1:
275 print >>sys.stderr, "usage: pyftfontview font..."
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400276 sys.exit(1)
Behdad Esfahbod11581182013-08-29 18:30:26 -0400277 for arg in args:
Behdad Esfahbodac10d812013-09-03 18:29:58 -0400278 FontView(arg)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400279 gtk.main()
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400280
281if __name__ == "__main__":
Behdad Esfahbod11581182013-08-29 18:30:26 -0400282 main(sys.argv[1:])