blob: ffe69ce282f1a963951bb2d766914348c695b76d [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
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070025from . import misc, ttLib, cffLib
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040026
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040027
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040028class Row(object):
Behdad Esfahbod22849902013-09-03 18:26:29 -040029 def __init__(self, parent, index, key, value, font):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040030 self._parent = parent
31 self._index = index
32 self._key = key
33 self._value = value
Behdad Esfahbod22849902013-09-03 18:26:29 -040034 self._font = font
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040035
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070036 if isinstance(value, ttLib.TTFont):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040037 self._add_font(value)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040038 return
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040039
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040040 if not isinstance(value, basestring):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040041 # Try sequences
Behdad Esfahbod78b74942013-08-17 13:08:33 -040042 is_sequence = True
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040043 try:
44 len(value)
45 iter(value)
Behdad Esfahbod70132792013-08-17 12:31:27 -040046 # It's hard to differentiate list-type sequences
47 # from dict-type ones. Try fetching item 0.
48 value[0]
Behdad Esfahbod78b74942013-08-17 13:08:33 -040049 except TypeError:
50 is_sequence = False
51 except AttributeError:
52 is_sequence = False
53 except KeyError:
54 is_sequence = False
55 except IndexError:
56 is_sequence = False
57 if is_sequence:
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040058 self._add_list(key, value)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040059 return
Behdad Esfahbod70132792013-08-17 12:31:27 -040060 if hasattr(value, '__dict__'):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040061 self._add_object(key, value)
Behdad Esfahbod70132792013-08-17 12:31:27 -040062 return
63 if hasattr(value, 'items'):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040064 self._add_dict(key, value)
Behdad Esfahbod70132792013-08-17 12:31:27 -040065 return
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040066
Behdad Esfahbodaa55d752013-08-17 12:50:18 -040067 if isinstance(value, basestring):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040068 self._value_str = '"'+value+'"'
69 self._children = []
Behdad Esfahbodaa55d752013-08-17 12:50:18 -040070 return
71
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040072 # Everything else
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040073 self._children = []
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -040074
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040075 def _filter_items(self):
76 items = []
77 for k,v in self._items:
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070078 if isinstance(v, ttLib.TTFont):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040079 continue
80 if k in ['reader', 'file', 'tableTag', 'compileStatus', 'recurse']:
81 continue
82 if isinstance(k, basestring) and k[0] == '_':
83 continue
84 items.append((k,v))
85 self._items = items
86
87 def _add_font(self, font):
88 self._items = [(tag,font[tag]) for tag in font.keys()]
89
90 def _add_object(self, key, value):
91 # Make sure item is decompiled
92 try:
Behdad Esfahbodac10d812013-09-03 18:29:58 -040093 value["asdf"]
Behnam Esfahbod31547e02013-09-03 21:00:06 -070094 except (AttributeError, KeyError, ttLib.TTLibError):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040095 pass
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070096 if isinstance(value, ttLib.getTableModule('glyf').Glyph):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -040097 # Glyph type needs explicit expanding to be useful
Behdad Esfahbod22849902013-09-03 18:26:29 -040098 value.expand(self._font['glyf'])
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -070099 if isinstance(value, misc.psCharStrings.T2CharString):
Behdad Esfahbod453ceed2013-08-18 16:53:57 -0400100 try:
101 value.decompile()
102 except TypeError: # Subroutines can't be decompiled
103 pass
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -0700104 if isinstance(value, cffLib.BaseDict):
Behdad Esfahbode3314312013-08-18 16:40:18 -0400105 for k in value.rawDict.keys():
106 getattr(value, k)
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -0700107 if isinstance(value, cffLib.Index):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400108 # Load all items
109 for i in range(len(value)):
110 value[i]
111 # Discard offsets as should not be needed anymore
112 if hasattr(value, 'offsets'):
113 del value.offsets
114
115 self._value_str = value.__class__.__name__
116 self._items = sorted(value.__dict__.items())
117 self._filter_items()
118
119 def _add_dict(self, key, value):
120 self._value_str = '%s of %d items' % (value.__class__.__name__, len(value))
121 self._items = sorted(value.items())
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400122
123 def _add_list(self, key, value):
124 if len(value) and len(value) <= 32:
125 self._value_str = str(value)
126 else:
127 self._value_str = '%s of %d items' % (value.__class__.__name__,
128 len(value))
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400129 self._items = list(enumerate(value))
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400130
131 def __len__(self):
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400132 if hasattr(self, '_children'):
133 return len(self._children)
134 if hasattr(self, '_items'):
135 return len(self._items)
136 assert 0
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400137
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400138 def _ensure_children(self):
139 if hasattr(self, '_children'):
140 return
141 children = []
142 for i,(k,v) in enumerate(self._items):
Behdad Esfahbod22849902013-09-03 18:26:29 -0400143 children.append(Row(self, i, k, v, self._font))
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400144 self._children = children
145 del self._items
146
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400147 def __getitem__(self, n):
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400148 if n >= len(self):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400149 return None
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400150 if not hasattr(self, '_children'):
151 self._children = [None] * len(self)
152 c = self._children[n]
153 if c is None:
154 k,v = self._items[n]
Behdad Esfahbod22849902013-09-03 18:26:29 -0400155 c = self._children[n] = Row(self, n, k, v, self._font)
Behdad Esfahbod8623d022013-08-18 17:19:13 -0400156 self._items[n] = None
157 return c
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400158
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400159 def get_parent(self):
160 return self._parent
161
162 def get_index(self):
163 return self._index
164
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400165 def get_key(self):
166 return self._key
167
168 def get_value(self):
169 return self._value
170
171 def get_value_str(self):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400172 if hasattr(self,'_value_str'):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400173 return self._value_str
174 return str(self._value)
175
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400176class FontTreeModel(gtk.GenericTreeModel):
177
178 __gtype_name__ = 'FontTreeModel'
179
180 def __init__(self, font):
181 super(FontTreeModel, self).__init__()
182 self._columns = (str, str)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400183 self.font = font
Behdad Esfahbod22849902013-09-03 18:26:29 -0400184 self._root = Row(None, 0, "font", font, font)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400185
186 def on_get_flags(self):
187 return 0
188
189 def on_get_n_columns(self):
190 return len(self._columns)
191
192 def on_get_column_type(self, index):
193 return self._columns[index]
194
195 def on_get_iter(self, path):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400196 rowref = self._root
197 while path:
198 rowref = rowref[path[0]]
199 path = path[1:]
200 return rowref
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400201
202 def on_get_path(self, rowref):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400203 path = []
204 while rowref != self._root:
205 path.append(rowref.get_index())
206 rowref = rowref.get_parent()
207 path.reverse()
208 return tuple(path)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400209
210 def on_get_value(self, rowref, column):
211 if column == 0:
212 return rowref.get_key()
213 else:
214 return rowref.get_value_str()
215
216 def on_iter_next(self, rowref):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400217 return rowref.get_parent()[rowref.get_index() + 1]
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400218
219 def on_iter_children(self, rowref):
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400220 return rowref[0]
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400221
222 def on_iter_has_child(self, rowref):
223 return bool(len(rowref))
224
225 def on_iter_n_children(self, rowref):
226 return len(rowref)
227
228 def on_iter_nth_child(self, rowref, n):
229 if not rowref: rowref = self._root
Behdad Esfahbod62d1d242013-08-18 17:04:59 -0400230 return rowref[n]
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400231
232 def on_iter_parent(self, rowref):
233 return rowref.get_parent()
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400234
Behdad Esfahbod11581182013-08-29 18:30:26 -0400235class FontView:
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400236
Behdad Esfahbod75858f62013-08-19 13:01:45 -0400237 def _delete_event(self, widget, event, data=None):
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400238 gtk.main_quit()
239 return False
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400240
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400241 def __init__(self, fontfile):
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400242
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400243 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
Behdad Esfahbod11581182013-08-29 18:30:26 -0400244 self.window.set_title("%s - FontView" % fontfile)
Behdad Esfahbod75858f62013-08-19 13:01:45 -0400245 self.window.connect("delete_event", self._delete_event)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400246 self.window.set_size_request(400, 600)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400247
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400248 self.scrolled_window = gtk.ScrolledWindow()
249 self.window.add(self.scrolled_window)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400250
Behnam Esfahbod1a281ee2013-09-03 20:56:24 -0700251 self.font = ttLib.TTFont(fontfile)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400252 self.treemodel = FontTreeModel(self.font)
253 self.treeview = gtk.TreeView(self.treemodel)
254 #self.treeview.set_reorderable(True)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400255
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400256 for i in range(2):
Behnam Esfahbod25fc2682013-09-03 20:27:14 -0700257 col_name = ('Key', 'Value')[i]
258 col = gtk.TreeViewColumn(col_name)
259 col.set_sort_column_id(-1)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400260 self.treeview.append_column(col)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400261
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400262 cell = gtk.CellRendererText()
263 col.pack_start(cell, True)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400264 col.add_attribute(cell, 'text', i)
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400265
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400266 self.treeview.set_search_column(1)
267 self.scrolled_window.add(self.treeview)
268 self.window.show_all()
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400269
Behdad Esfahbod11581182013-08-29 18:30:26 -0400270def main(args):
271 if len(args) < 1:
272 print >>sys.stderr, "usage: pyftfontview font..."
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400273 sys.exit(1)
Behdad Esfahbod11581182013-08-29 18:30:26 -0400274 for arg in args:
Behdad Esfahbodac10d812013-09-03 18:29:58 -0400275 FontView(arg)
Behdad Esfahbod1d6e7262013-08-18 16:08:44 -0400276 gtk.main()
Behdad Esfahbodbe4809c2013-08-17 11:40:00 -0400277
278if __name__ == "__main__":
Behdad Esfahbod11581182013-08-29 18:30:26 -0400279 main(sys.argv[1:])