blob: dcd783c522f479b195cafe5a1ef64f541d179ca8 [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):
280 if self.ReqFeatureIndex not in feature_indices:
281 self.ReqFeatureIndex = 65535
282 self.FeatureIndex = [f for f in self.FeatureIndex if f in feature_indices]
283 self.FeatureCount = len (self.FeatureIndex)
284 return self.FeatureCount
285
286@add_method(fontTools.ttLib.tables.otTables.Script)
287def subset_features (self, feature_indices):
288 if self.DefaultLangSys and not self.DefaultLangSys.subset_features (feature_indices):
289 self.DefaultLangSys = None
290 self.LangSysRecord = [l for l in self.LangSysRecord if l.LangSys.subset_features (feature_indices)]
291 self.LangSysCount = len (self.LangSysRecord)
292 return self.LangSysCount
293
294@add_method(fontTools.ttLib.tables.otTables.ScriptList)
295def subset_features (self, feature_indices):
296 self.ScriptRecord = [s for s in self.ScriptRecord if s.Script.subset_features (feature_indices)]
297 self.ScriptCount = len (self.ScriptRecord)
298 return self.ScriptCount
299
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400300@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400301def subset (self, glyphs):
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400302 lookup_indices = self.table.LookupList.subset (glyphs)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400303 self.subset_lookups (lookup_indices)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400304 return True # Retain the possibly empty table
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400305
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400306@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400307def subset_lookups (self, lookup_indices):
308 "Retrains specified lookups, then removes empty features, language systems, and scripts."
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400309 self.table.LookupList.subset_lookups (lookup_indices)
310 feature_indices = self.table.FeatureList.subset_lookups (lookup_indices)
311 self.table.ScriptList.subset_features (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400312
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400313@add_method(fontTools.ttLib.getTableClass('GDEF'))
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400314def subset (self, glyphs):
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400315 table = self.table
316 if table.LigCaretList:
317 indices = table.LigCaretList.Coverage.subset (glyphs)
318 table.LigCaretList.LigGlyph = [table.LigCaretList.LigGlyph[i] for i in indices]
319 table.LigCaretList.LigGlyphCount = len (table.LigCaretList.LigGlyph)
320 if not table.LigCaretList.LigGlyphCount:
321 table.LigCaretList = None
322 if table.MarkAttachClassDef:
323 table.MarkAttachClassDef.classDefs = {g:v for g,v in table.MarkAttachClassDef.classDefs.items() if g in glyphs}
324 if not table.MarkAttachClassDef.classDefs:
325 table.MarkAttachClassDef = None
326 if table.GlyphClassDef:
327 table.GlyphClassDef.classDefs = {g:v for g,v in table.GlyphClassDef.classDefs.items() if g in glyphs}
328 if not table.GlyphClassDef.classDefs:
329 table.GlyphClassDef = None
330 if table.AttachList:
331 indices = table.AttachList.Coverage.subset (glyphs)
332 table.AttachList.AttachPoint = [table.AttachList.AttachPoint[i] for i in indices]
333 table.AttachList.GlyphCount = len (table.AttachList.AttachPoint)
334 if not table.AttachList.GlyphCount:
335 table.AttachList = None
336 return table.LigCaretList or table.MarkAttachClassDef or table.GlyphClassDef or table.AttachList
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400337
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400338@add_method(fontTools.ttLib.getTableClass('kern'))
339def subset (self, glyphs):
Behdad Esfahbod5270ec42013-07-22 12:57:02 -0400340 for t in self.kernTables:
341 t.kernTable = {(a,b):v for ((a,b),v) in t.kernTable.items() if a in glyphs and b in glyphs}
342 self.kernTables = [t for t in self.kernTables if t.kernTable]
343 return self.kernTables
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400344
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400345@add_method(fontTools.ttLib.getTableClass('hmtx'))
346def subset (self, glyphs):
347 self.metrics = {g:v for (g,v) in self.metrics.items() if g in glyphs}
348 return len (self.metrics)
349
Behdad Esfahbod56ebd042013-07-22 13:02:24 -0400350@add_method(fontTools.ttLib.getTableClass('gasp'),
351 fontTools.ttLib.getTableClass('head'),
352 fontTools.ttLib.getTableClass('hhea'),
353 fontTools.ttLib.getTableClass('name'),
354 fontTools.ttLib.getTableClass('vhea'),
355 fontTools.ttLib.getTableClass('OS/2'))
356def subset (self, glyphs):
357 # Nothing to do.
358 return True
359
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400360if __name__ == '__main__':
361
362 import sys
363
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400364 verbose = False
365 if "--verbose" in sys.argv:
366 verbose = True
367 sys.argv.remove ("--verbose")
Behdad Esfahbod350a5272013-07-22 12:02:16 -0400368 xml = False
369 if "--xml" in sys.argv:
370 xml = True
371 sys.argv.remove ("--xml")
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400372
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400373 if len (sys.argv) < 3:
374 print >>sys.stderr, "usage: pyotlss.py font-file glyph..."
375 sys.exit (1)
376
377 fontfile = sys.argv[1]
378 glyphs = sys.argv[2:]
379
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400380 # Always include .notdef; anything else?
381 if '.notdef' not in glyphs:
382 glyphs.append ('.notdef')
383
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400384 font = fontTools.ttx.TTFont (fontfile)
385
386 names = font.getGlyphNames()
387 # Convert to glyph names
388 glyphs = [g if g in names else font.getGlyphName(int(g)) for g in glyphs]
389
Behdad Esfahbod350a5272013-07-22 12:02:16 -0400390 if xml:
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400391 import xmlWriter
392 writer = xmlWriter.XMLWriter (sys.stdout)
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400393
394 drop_tables = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', 'PCLT', 'LTSH']
Behdad Esfahbod8842ce22013-07-22 13:01:33 -0400395 for tag in font.keys():
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400396
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400397 if tag == 'GlyphOrder':
398 continue
399
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400400 if tag in drop_tables:
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400401 if verbose:
402 print tag, "dropped."
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400403 del font[tag]
404 continue
405
Behdad Esfahbod8842ce22013-07-22 13:01:33 -0400406 clazz = fontTools.ttLib.getTableClass(tag)
407 if 'subset' not in vars (clazz):
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400408 if verbose:
409 print tag, "skipped."
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400410 continue
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400411
Behdad Esfahbod8842ce22013-07-22 13:01:33 -0400412 table = font[tag]
413 if not table.subset (glyphs):
Behdad Esfahbod9d02c2d2013-07-22 11:08:37 -0400414 del font[tag]
Behdad Esfahbod5270ec42013-07-22 12:57:02 -0400415 if verbose:
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400416 print tag, "subset empty; dropped."
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400417 else:
Behdad Esfahbod350a5272013-07-22 12:02:16 -0400418 if xml:
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400419 writer.begintag (tag)
420 writer.newline ()
421 font[tag].toXML(writer, font)
422 writer.endtag (tag)
423 writer.newline ()
Behdad Esfahbod350a5272013-07-22 12:02:16 -0400424 if verbose:
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400425 print tag, "subsetted."
426
427 glyphOrder = font.getGlyphOrder()
428 glyphOrder = [g for g in glyphOrder if g in glyphs]
429 font.setGlyphOrder (glyphOrder)
430 font._buildReverseGlyphOrderDict ()
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400431
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400432 font.save (fontfile + '.subset')