blob: f6ac5925468e10ae64feb8cbd90583bcee4ec8f3 [file] [log] [blame]
Behdad Esfahbod54660612013-07-21 18:16:55 -04001#!/usr/bin/python
2
3# Python OpenType Layout Subsetter
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -04004# Written by: Behdad Esfahbod
Behdad Esfahbod54660612013-07-21 18:16:55 -04005
6import fontTools.ttx
Behdad Esfahbod54660612013-07-21 18:16:55 -04007
Behdad Esfahbod54660612013-07-21 18:16:55 -04008
Behdad Esfahbod02b92062013-07-21 18:40:59 -04009def add_method (*clazzes):
Behdad Esfahbod54660612013-07-21 18:16:55 -040010 def wrapper(method):
Behdad Esfahbod02b92062013-07-21 18:40:59 -040011 for clazz in clazzes:
12 setattr (clazz, method.func_name, method)
Behdad Esfahbod54660612013-07-21 18:16:55 -040013 return wrapper
14
Behdad Esfahbod54660612013-07-21 18:16:55 -040015# Subset
Behdad Esfahbod54660612013-07-21 18:16:55 -040016
17@add_method(fontTools.ttLib.tables.otTables.Coverage)
18def subset (self, glyphs):
19 indices = [i for (i,g) in enumerate (self.glyphs) if g in glyphs]
20 self.glyphs = [g for g in self.glyphs if g in glyphs]
21 return indices
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040022
23@add_method(fontTools.ttLib.tables.otTables.Coverage)
24def __nonzero__ (self):
25 return bool (self.glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -040026
27@add_method(fontTools.ttLib.tables.otTables.ClassDef)
28def subset (self, glyphs):
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -040029 "Returns ascending list of remaining classes."
Behdad Esfahbod54660612013-07-21 18:16:55 -040030 self.classDefs = {g:v for g,v in self.classDefs.items() if g in glyphs}
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -040031 return {v:1 for v in self.classDefs.values ()}.keys ()
32
33@add_method(fontTools.ttLib.tables.otTables.ClassDef)
34def remap (self, class_map):
35 "Remaps classes."
36 self.classDefs = {g:class_map.index (v) for g,v in self.classDefs.items()}
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040037
38@add_method(fontTools.ttLib.tables.otTables.ClassDef)
39def __nonzero__ (self):
40 return bool (self.classDefs)
Behdad Esfahbod54660612013-07-21 18:16:55 -040041
42@add_method(fontTools.ttLib.tables.otTables.SingleSubst)
43def subset (self, glyphs):
44 if self.Format in [1, 2]:
45 self.mapping = {g:v for g,v in self.mapping.items() if g in glyphs}
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040046 return len (self.mapping)
Behdad Esfahbod54660612013-07-21 18:16:55 -040047 else:
48 assert 0, "unknown format: %s" % self.Format
49
50@add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
51def subset (self, glyphs):
52 if self.Format == 1:
53 indices = self.Coverage.subset (glyphs)
54 self.Sequence = [self.Sequence[i] for i in indices]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040055 self.SequenceCount = len (self.Sequence)
56 return self.SequenceCount
Behdad Esfahbod54660612013-07-21 18:16:55 -040057 else:
58 assert 0, "unknown format: %s" % self.Format
59
60@add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
61def subset (self, glyphs):
62 if self.Format == 1:
63 self.alternates = {g:v for g,v in self.alternates.items() if g in glyphs}
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040064 return len (self.alternates)
Behdad Esfahbod54660612013-07-21 18:16:55 -040065 else:
66 assert 0, "unknown format: %s" % self.Format
67
68@add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
69def subset (self, glyphs):
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040070 if self.Format == 1:
71 self.ligatures = {g:v for g,v in self.ligatures.items() if g in glyphs}
72 self.ligatures = {g:[seq for seq in seqs if all(c in glyphs for c in seq.Component)]
73 for g,seqs in self.ligatures.items()}
74 self.ligatures = {g:v for g,v in self.ligatures.items() if v}
75 return len (self.ligatures)
76 else:
77 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -040078
Behdad Esfahbod54660612013-07-21 18:16:55 -040079@add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
80def subset (self, glyphs):
81 if self.Format == 1:
82 indices = self.Coverage.subset (glyphs)
83 self.Substitute = [self.Substitute[i] for i in indices]
84 self.GlyphCount = len (self.Substitute)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040085 return self.GlyphCount and all (c.subset (glyphs) for c in self.LookAheadCoverage + self.BacktrackCoverage)
Behdad Esfahbod54660612013-07-21 18:16:55 -040086 else:
87 assert 0, "unknown format: %s" % self.Format
88
89@add_method(fontTools.ttLib.tables.otTables.SinglePos)
90def subset (self, glyphs):
91 if self.Format == 1:
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040092 return len (self.Coverage.subset (glyphs))
Behdad Esfahbod54660612013-07-21 18:16:55 -040093 elif self.Format == 2:
94 indices = self.Coverage.subset (glyphs)
95 self.Value = [self.Value[i] for i in indices]
96 self.ValueCount = len (self.Value)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040097 return self.ValueCount
Behdad Esfahbod54660612013-07-21 18:16:55 -040098 else:
99 assert 0, "unknown format: %s" % self.Format
100
101@add_method(fontTools.ttLib.tables.otTables.PairPos)
102def subset (self, glyphs):
103 if self.Format == 1:
104 indices = self.Coverage.subset (glyphs)
105 self.PairSet = [self.PairSet[i] for i in indices]
106 for p in self.PairSet:
107 p.PairValueRecord = [r for r in p.PairValueRecord if r.SecondGlyph in glyphs]
108 p.PairValueCount = len (p.PairValueRecord)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400109 self.PairSet = [p for p in self.PairSet if p.PairValueCount]
Behdad Esfahbod54660612013-07-21 18:16:55 -0400110 self.PairSetCount = len (self.PairSet)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400111 return self.PairSetCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400112 elif self.Format == 2:
113 self.Coverage.subset (glyphs)
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -0400114 class1_map = self.ClassDef1.subset (glyphs)
115 class2_map = self.ClassDef2.subset (glyphs)
116 self.ClassDef1.remap (class1_map)
117 self.ClassDef2.remap (class2_map)
118 self.Class1Record = [self.Class1Record[i] for i in class1_map]
119 for c in self.Class1Record:
120 c.Class2Record = [c.Class2Record[i] for i in class2_map]
121 self.Class1Count = len (class1_map)
122 self.Class2Count = len (class2_map)
123 return self.Coverage and self.Class1Count and self.Class2Count
Behdad Esfahbod54660612013-07-21 18:16:55 -0400124 else:
125 assert 0, "unknown format: %s" % self.Format
126
127@add_method(fontTools.ttLib.tables.otTables.CursivePos)
128def subset (self, glyphs):
129 if self.Format == 1:
130 indices = self.Coverage.subset (glyphs)
131 self.EntryExitRecord = [self.EntryExitRecord[i] for i in indices]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400132 self.EntryExitCount = len (self.EntryExitRecord)
133 return self.EntryExitCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400134 else:
135 assert 0, "unknown format: %s" % self.Format
136
137@add_method(fontTools.ttLib.tables.otTables.MarkBasePos)
138def subset (self, glyphs):
139 if self.Format == 1:
140 mark_indices = self.MarkCoverage.subset (glyphs)
141 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices]
142 self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord)
143 base_indices = self.BaseCoverage.subset (glyphs)
144 self.BaseArray.BaseRecord = [self.BaseArray.BaseRecord[i] for i in base_indices]
145 self.BaseArray.BaseCount = len (self.BaseArray.BaseRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400146 # Prune empty classes
147 class_indices = {v.Class:1 for v in self.MarkArray.MarkRecord}.keys ()
148 self.ClassCount = len (class_indices)
149 for m in self.MarkArray.MarkRecord:
150 m.Class = class_indices.index (m.Class)
151 for b in self.BaseArray.BaseRecord:
152 b.BaseAnchor = [b.BaseAnchor[i] for i in class_indices]
153 return self.ClassCount and self.MarkArray.MarkCount and self.BaseArray.BaseCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400154 else:
155 assert 0, "unknown format: %s" % self.Format
156
157@add_method(fontTools.ttLib.tables.otTables.MarkLigPos)
158def subset (self, glyphs):
159 if self.Format == 1:
160 mark_indices = self.MarkCoverage.subset (glyphs)
161 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices]
162 self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord)
163 ligature_indices = self.LigatureCoverage.subset (glyphs)
164 self.LigatureArray.LigatureAttach = [self.LigatureArray.LigatureAttach[i] for i in ligature_indices]
165 self.LigatureArray.LigatureCount = len (self.LigatureArray.LigatureAttach)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400166 # Prune empty classes
167 class_indices = {v.Class:1 for v in self.MarkArray.MarkRecord}.keys ()
168 self.ClassCount = len (class_indices)
169 for m in self.MarkArray.MarkRecord:
170 m.Class = class_indices.index (m.Class)
171 for l in self.LigatureArray.LigatureAttach:
172 for c in l.ComponentRecord:
173 c.LigatureAnchor = [c.LigatureAnchor[i] for i in class_indices]
174 return self.ClassCount and self.MarkArray.MarkCount and self.LigatureArray.LigatureCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400175 else:
176 assert 0, "unknown format: %s" % self.Format
177
178@add_method(fontTools.ttLib.tables.otTables.MarkMarkPos)
179def subset (self, glyphs):
180 if self.Format == 1:
181 mark1_indices = self.Mark1Coverage.subset (glyphs)
182 self.Mark1Array.MarkRecord = [self.Mark1Array.MarkRecord[i] for i in mark1_indices]
183 self.Mark1Array.MarkCount = len (self.Mark1Array.MarkRecord)
184 mark2_indices = self.Mark2Coverage.subset (glyphs)
185 self.Mark2Array.Mark2Record = [self.Mark2Array.Mark2Record[i] for i in mark2_indices]
186 self.Mark2Array.MarkCount = len (self.Mark2Array.Mark2Record)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400187 # Prune empty classes
188 class_indices = {v.Class:1 for v in self.Mark1Array.MarkRecord}.keys ()
189 self.ClassCount = len (class_indices)
190 for m in self.Mark1Array.MarkRecord:
191 m.Class = class_indices.index (m.Class)
192 for b in self.Mark2Array.Mark2Record:
193 b.Mark2Anchor = [b.Mark2Anchor[i] for i in class_indices]
194 return self.ClassCount and self.Mark1Array.MarkCount and self.Mark2Array.MarkCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400195 else:
196 assert 0, "unknown format: %s" % self.Format
197
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400198@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ContextPos)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400199def subset (self, glyphs):
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400200 if self.Format == 1:
Behdad Esfahbodb7fef902013-07-21 22:48:08 -0400201 indices = self.Coverage.subset (glyphs)
202 self.SubRuleSet = [self.SubRuleSet[i] for i in indices]
203 self.SubRuleSetCount = len (self.SubRuleSet)
204 for rs in self.SubRuleSet:
205 rs.SubRule = [r for r in rs.SubRule
206 if all (g in glyphs for g in r.Input)]
207 rs.SubRuleCount = len (rs.SubRule)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400208 # Prune empty subrulesets
209 return self.SubRuleSetCount
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400210 elif self.Format == 2:
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400211 return self.Coverage.subset (glyphs) and self.ClassDef.subset (glyphs)
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400212 elif self.Format == 3:
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400213 return all (c.subset (glyphs) for c in self.Coverage)
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400214 else:
215 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400216
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400217@add_method(fontTools.ttLib.tables.otTables.ChainContextSubst, fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400218def subset (self, glyphs):
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400219 if self.Format == 1:
Behdad Esfahbodb7fef902013-07-21 22:48:08 -0400220 indices = self.Coverage.subset (glyphs)
221 self.ChainSubRuleSet = [self.ChainSubRuleSet[i] for i in indices]
222 self.ChainSubRuleSetCount = len (self.ChainSubRuleSet)
223 for rs in self.ChainSubRuleSet:
224 rs.ChainSubRule = [r for r in rs.ChainSubRule
225 if all (g in glyphs for g in r.Backtrack + r.Input + r.LookAhead)]
226 rs.ChainSubRuleCount = len (rs.ChainSubRule)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400227 # Prune empty subrulesets
228 return self.ChainSubRuleSetCount
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400229 elif self.Format == 2:
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400230 return self.Coverage.subset (glyphs) and \
231 self.LookAheadClassDef.subset (glyphs) and \
232 self.BacktrackClassDef.subset (glyphs) and \
233 self.InputClassDef.subset (glyphs)
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400234 elif self.Format == 3:
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400235 return all (c.subset (glyphs) for c in self.InputCoverage + self.LookAheadCoverage + self.BacktrackCoverage)
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400236 else:
237 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400238
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400239@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400240def subset (self, glyphs):
241 if self.Format == 1:
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400242 return self.ExtSubTable.subset (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400243 else:
244 assert 0, "unknown format: %s" % self.Format
245
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400246@add_method(fontTools.ttLib.tables.otTables.Lookup)
247def subset (self, glyphs):
Behdad Esfahbod1be53452013-07-21 22:52:15 -0400248 self.SubTable = [s for s in self.SubTable if s.subset (glyphs)]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400249 self.SubTableCount = len (self.SubTable)
250 return self.SubTableCount
251
252@add_method(fontTools.ttLib.tables.otTables.LookupList)
253def subset (self, glyphs):
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400254 "Returns the indices of nonempty lookups."
255 return [i for (i,l) in enumerate (self.Lookup) if l.subset (glyphs)]
256
257@add_method(fontTools.ttLib.tables.otTables.LookupList)
258def subset_lookups (self, lookup_indices):
259 self.Lookup = [self.Lookup[i] for i in lookup_indices]
260 self.LookupCount = len (self.Lookup)
261
262@add_method(fontTools.ttLib.tables.otTables.Feature)
263def subset_lookups (self, lookup_indices):
264 self.LookupListIndex = [l for l in self.LookupListIndex if l in lookup_indices]
265 # Now map them.
266 self.LookupListIndex = [lookup_indices.index (l) for l in self.LookupListIndex]
267 self.LookupCount = len (self.LookupListIndex)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400268 return self.LookupCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400269
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400270@add_method(fontTools.ttLib.tables.otTables.FeatureList)
271def subset_lookups (self, lookup_indices):
272 "Returns the indices of nonempty features."
273 feature_indices = [i for (i,f) in enumerate (self.FeatureRecord) if f.Feature.subset_lookups (lookup_indices)]
274 self.FeatureRecord = [self.FeatureRecord[i] for i in feature_indices]
275 self.FeatureCount = len (self.FeatureRecord)
276 return feature_indices
277
278@add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys)
279def subset_features (self, feature_indices):
Behdad Esfahbod69ce1502013-07-22 18:00:31 -0400280 if self.ReqFeatureIndex in feature_indices:
281 self.ReqFeatureIndex = feature_indices.index (self.ReqFeatureIndex)
282 else:
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400283 self.ReqFeatureIndex = 65535
284 self.FeatureIndex = [f for f in self.FeatureIndex if f in feature_indices]
Behdad Esfahbod69ce1502013-07-22 18:00:31 -0400285 # Now map them.
286 self.FeatureIndex = [feature_indices.index (f) for f in self.FeatureIndex if f in feature_indices]
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400287 self.FeatureCount = len (self.FeatureIndex)
288 return self.FeatureCount
289
290@add_method(fontTools.ttLib.tables.otTables.Script)
291def subset_features (self, feature_indices):
292 if self.DefaultLangSys and not self.DefaultLangSys.subset_features (feature_indices):
293 self.DefaultLangSys = None
294 self.LangSysRecord = [l for l in self.LangSysRecord if l.LangSys.subset_features (feature_indices)]
295 self.LangSysCount = len (self.LangSysRecord)
296 return self.LangSysCount
297
298@add_method(fontTools.ttLib.tables.otTables.ScriptList)
299def subset_features (self, feature_indices):
300 self.ScriptRecord = [s for s in self.ScriptRecord if s.Script.subset_features (feature_indices)]
301 self.ScriptCount = len (self.ScriptRecord)
302 return self.ScriptCount
303
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400304@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400305def subset (self, glyphs):
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400306 lookup_indices = self.table.LookupList.subset (glyphs)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400307 self.subset_lookups (lookup_indices)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400308 return True # Retain the possibly empty table
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400309
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400310@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400311def subset_lookups (self, lookup_indices):
312 "Retrains specified lookups, then removes empty features, language systems, and scripts."
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400313 self.table.LookupList.subset_lookups (lookup_indices)
314 feature_indices = self.table.FeatureList.subset_lookups (lookup_indices)
315 self.table.ScriptList.subset_features (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400316
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400317@add_method(fontTools.ttLib.getTableClass('GDEF'))
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400318def subset (self, glyphs):
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400319 table = self.table
320 if table.LigCaretList:
321 indices = table.LigCaretList.Coverage.subset (glyphs)
322 table.LigCaretList.LigGlyph = [table.LigCaretList.LigGlyph[i] for i in indices]
323 table.LigCaretList.LigGlyphCount = len (table.LigCaretList.LigGlyph)
324 if not table.LigCaretList.LigGlyphCount:
325 table.LigCaretList = None
326 if table.MarkAttachClassDef:
327 table.MarkAttachClassDef.classDefs = {g:v for g,v in table.MarkAttachClassDef.classDefs.items() if g in glyphs}
328 if not table.MarkAttachClassDef.classDefs:
329 table.MarkAttachClassDef = None
330 if table.GlyphClassDef:
331 table.GlyphClassDef.classDefs = {g:v for g,v in table.GlyphClassDef.classDefs.items() if g in glyphs}
332 if not table.GlyphClassDef.classDefs:
333 table.GlyphClassDef = None
334 if table.AttachList:
335 indices = table.AttachList.Coverage.subset (glyphs)
336 table.AttachList.AttachPoint = [table.AttachList.AttachPoint[i] for i in indices]
337 table.AttachList.GlyphCount = len (table.AttachList.AttachPoint)
338 if not table.AttachList.GlyphCount:
339 table.AttachList = None
340 return table.LigCaretList or table.MarkAttachClassDef or table.GlyphClassDef or table.AttachList
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400341
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400342@add_method(fontTools.ttLib.getTableClass('kern'))
343def subset (self, glyphs):
Behdad Esfahbod5270ec42013-07-22 12:57:02 -0400344 for t in self.kernTables:
345 t.kernTable = {(a,b):v for ((a,b),v) in t.kernTable.items() if a in glyphs and b in glyphs}
346 self.kernTables = [t for t in self.kernTables if t.kernTable]
347 return self.kernTables
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400348
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400349@add_method(fontTools.ttLib.getTableClass('hmtx'), fontTools.ttLib.getTableClass('vmtx'))
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400350def subset (self, glyphs):
351 self.metrics = {g:v for (g,v) in self.metrics.items() if g in glyphs}
352 return len (self.metrics)
353
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400354@add_method(fontTools.ttLib.getTableClass('hdmx'))
355def subset (self, glyphs):
356 self.hdmx = {s:{g:v for (g,v) in l.items() if g in glyphs} for (s,l) in self.hdmx.items()}
357
Behdad Esfahbode45d6af2013-07-22 15:29:17 -0400358@add_method(fontTools.ttLib.getTableClass('VORG'))
359def subset (self, glyphs):
360 self.VOriginRecords = {g:v for (g,v) in self.VOriginRecords.items() if g in glyphs}
361 self.numVertOriginYMetrics = len (self.VOriginRecords)
362 return True # Never drop; has default metrics
363
Behdad Esfahbod8c646f62013-07-22 15:06:23 -0400364@add_method(fontTools.ttLib.getTableClass('post'))
365def subset (self, glyphs):
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400366 return True # Just pass-through
367
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400368@add_method(fontTools.ttLib.getTableClass('glyf'))
369def subset (self, glyphs):
370 self.glyphs = {g:v for (g,v) in self.glyphs.items() if g in glyphs}
371 self.glyphOrder = [g for g in self.glyphOrder if g in glyphs]
372 return len (self.glyphs)
373
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400374@add_method(fontTools.ttLib.getTableClass('cmap'))
375def subset (self, glyphs):
Behdad Esfahbodbccf6b12013-07-22 18:43:13 -0400376 # Drop non-Unicode / non-Symbol cmaps
377 self.tables = [t for t in self.tables if t.platformID == 3 and t.platEncID in [0, 1, 10]]
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400378 for t in self.tables:
Behdad Esfahbod9453a362013-07-22 16:21:24 -0400379 # For reasons I don't understand I need this here
380 # to force decompilation of the cmap format 14.
381 try:
382 getattr (t, "asdf")
383 except AttributeError:
384 pass
Behdad Esfahbodb13d7902013-07-22 16:01:15 -0400385 if t.format == 14:
Behdad Esfahbod9453a362013-07-22 16:21:24 -0400386 # XXX We drop all the default-UVS mappings (g==None)
Behdad Esfahbodb13d7902013-07-22 16:01:15 -0400387 t.uvsDict = {v:[(u,g) for (u,g) in l if g in glyphs] for (v,l) in t.uvsDict.items()}
388 t.uvsDict = {v:l for (v,l) in t.uvsDict.items() if l}
389 else:
390 t.cmap = {u:g for (u,g) in t.cmap.items() if g in glyphs}
391 self.tables = [t for t in self.tables if (t.cmap if t.format != 14 else t.uvsDict)]
Behdad Esfahbodf3744d92013-07-22 17:04:04 -0400392 # Drop format=0 which can't be subset easily?
393 self.tables = [t for t in self.tables if t.format != 0]
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400394 return len (self.tables)
395
Behdad Esfahbod8c646f62013-07-22 15:06:23 -0400396
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400397# TODO OS/2 ulUnicodeRange / ulCodePageRange?
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400398# TODO Drop unnecessary name entries
Behdad Esfahbod0f86ce92013-07-22 17:30:31 -0400399# TODO Drop glyph names
Behdad Esfahbod1e3551a2013-07-22 18:03:10 -0400400# TODO Drop unneeded GSUB/GPOS entries
401# TODO Move glyf component closure after GSUB/GPOS/etc
Behdad Esfahbod56ebd042013-07-22 13:02:24 -0400402
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400403if __name__ == '__main__':
404
405 import sys
406
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400407 verbose = False
408 if "--verbose" in sys.argv:
409 verbose = True
410 sys.argv.remove ("--verbose")
Behdad Esfahbod350a5272013-07-22 12:02:16 -0400411 xml = False
412 if "--xml" in sys.argv:
413 xml = True
414 sys.argv.remove ("--xml")
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400415
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400416 if len (sys.argv) < 3:
417 print >>sys.stderr, "usage: pyotlss.py font-file glyph..."
418 sys.exit (1)
419
420 fontfile = sys.argv[1]
421 glyphs = sys.argv[2:]
422
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400423 # Always include .notdef; anything else?
Behdad Esfahbod0f86ce92013-07-22 17:30:31 -0400424 glyphs.append ('.notdef')
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400425
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400426 font = fontTools.ttx.TTFont (fontfile)
Behdad Esfahbod0f86ce92013-07-22 17:30:31 -0400427 font.disassembleInstructions = False
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400428
429 names = font.getGlyphNames()
430 # Convert to glyph names
431 glyphs = [g if g in names else font.getGlyphName(int(g)) for g in glyphs]
432
Behdad Esfahbod2a784ac2013-07-22 17:00:36 -0400433 # Close over composite glyphs
434 if 'glyf' in font:
435 glyf = font['glyf']
436 for g in glyphs:
437 gl = glyf[g]
438 if gl.isComposite ():
439 for c in gl.components:
440 if c.glyphName not in glyphs:
441 glyphs.append (c.glyphName)
442
Behdad Esfahbod350a5272013-07-22 12:02:16 -0400443 if xml:
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400444 import xmlWriter
445 writer = xmlWriter.XMLWriter (sys.stdout)
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400446
447 drop_tables = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', 'PCLT', 'LTSH']
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400448 noneed_tables = ['gasp', 'head', 'hhea', 'maxp', 'name', 'vhea', 'OS/2', 'VDMX', 'loca']
Behdad Esfahbodcd708852013-07-22 14:30:23 -0400449
Behdad Esfahbodb1e1ab62013-07-22 15:24:09 -0400450 # For now drop these
451 drop_tables += ['cvt ', 'fpgm', 'prep']
452
Behdad Esfahbod8842ce22013-07-22 13:01:33 -0400453 for tag in font.keys():
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400454
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400455 if tag == 'GlyphOrder':
456 continue
457
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400458 if tag in drop_tables:
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400459 if verbose:
460 print tag, "dropped."
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400461 del font[tag]
462 continue
463
Behdad Esfahbodcd708852013-07-22 14:30:23 -0400464 if tag in noneed_tables:
465 if verbose:
466 print tag, "intact."
467 continue
468
Behdad Esfahbod8842ce22013-07-22 13:01:33 -0400469 clazz = fontTools.ttLib.getTableClass(tag)
470 if 'subset' not in vars (clazz):
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400471 if verbose:
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400472 print tag, "skipped................"
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400473 continue
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400474
Behdad Esfahbod8842ce22013-07-22 13:01:33 -0400475 table = font[tag]
476 if not table.subset (glyphs):
Behdad Esfahbod9d02c2d2013-07-22 11:08:37 -0400477 del font[tag]
Behdad Esfahbod5270ec42013-07-22 12:57:02 -0400478 if verbose:
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400479 print tag, "subset empty; dropped."
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400480 else:
Behdad Esfahbod350a5272013-07-22 12:02:16 -0400481 if verbose:
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400482 print tag, "subsetted."
483
484 glyphOrder = font.getGlyphOrder()
485 glyphOrder = [g for g in glyphOrder if g in glyphs]
486 font.setGlyphOrder (glyphOrder)
487 font._buildReverseGlyphOrderDict ()
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400488
Behdad Esfahbod0f86ce92013-07-22 17:30:31 -0400489 if xml:
490 for tag in font.keys():
491 writer.begintag (tag)
492 writer.newline ()
493 font[tag].toXML(writer, font)
494 writer.endtag (tag)
495 writer.newline ()
496
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400497 font.save (fontfile + '.subset')