blob: dbb2f0f4821754c2f9ba56c0d94b86a18e703a7b [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 Esfahbod75e14fc2013-07-22 14:49:54 -0400345@add_method(fontTools.ttLib.getTableClass('hmtx'), fontTools.ttLib.getTableClass('vmtx'))
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400346def 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 Esfahbod75e14fc2013-07-22 14:49:54 -0400350@add_method(fontTools.ttLib.getTableClass('hdmx'))
351def subset (self, glyphs):
352 self.hdmx = {s:{g:v for (g,v) in l.items() if g in glyphs} for (s,l) in self.hdmx.items()}
353
Behdad Esfahbode45d6af2013-07-22 15:29:17 -0400354@add_method(fontTools.ttLib.getTableClass('VORG'))
355def subset (self, glyphs):
356 self.VOriginRecords = {g:v for (g,v) in self.VOriginRecords.items() if g in glyphs}
357 self.numVertOriginYMetrics = len (self.VOriginRecords)
358 return True # Never drop; has default metrics
359
Behdad Esfahbod8c646f62013-07-22 15:06:23 -0400360@add_method(fontTools.ttLib.getTableClass('post'))
361def subset (self, glyphs):
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400362 return True # Just pass-through
363
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400364@add_method(fontTools.ttLib.getTableClass('glyf'))
365def subset (self, glyphs):
366 self.glyphs = {g:v for (g,v) in self.glyphs.items() if g in glyphs}
367 self.glyphOrder = [g for g in self.glyphOrder if g in glyphs]
368 return len (self.glyphs)
369
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400370@add_method(fontTools.ttLib.getTableClass('cmap'))
371def subset (self, glyphs):
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400372 for t in self.tables:
Behdad Esfahbod9453a362013-07-22 16:21:24 -0400373 # For reasons I don't understand I need this here
374 # to force decompilation of the cmap format 14.
375 try:
376 getattr (t, "asdf")
377 except AttributeError:
378 pass
Behdad Esfahbodb13d7902013-07-22 16:01:15 -0400379 if t.format == 14:
Behdad Esfahbod9453a362013-07-22 16:21:24 -0400380 # XXX We drop all the default-UVS mappings (g==None)
Behdad Esfahbodb13d7902013-07-22 16:01:15 -0400381 t.uvsDict = {v:[(u,g) for (u,g) in l if g in glyphs] for (v,l) in t.uvsDict.items()}
382 t.uvsDict = {v:l for (v,l) in t.uvsDict.items() if l}
383 else:
384 t.cmap = {u:g for (u,g) in t.cmap.items() if g in glyphs}
385 self.tables = [t for t in self.tables if (t.cmap if t.format != 14 else t.uvsDict)]
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400386 return len (self.tables)
387
Behdad Esfahbod8c646f62013-07-22 15:06:23 -0400388
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400389# TODO OS/2 ulUnicodeRange / ulCodePageRange?
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400390# TODO Drop unnecessary cmap subtables
391# TODO Drop unnecessary name entries
Behdad Esfahbod56ebd042013-07-22 13:02:24 -0400392
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400393if __name__ == '__main__':
394
395 import sys
396
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400397 verbose = False
398 if "--verbose" in sys.argv:
399 verbose = True
400 sys.argv.remove ("--verbose")
Behdad Esfahbod350a5272013-07-22 12:02:16 -0400401 xml = False
402 if "--xml" in sys.argv:
403 xml = True
404 sys.argv.remove ("--xml")
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400405
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400406 if len (sys.argv) < 3:
407 print >>sys.stderr, "usage: pyotlss.py font-file glyph..."
408 sys.exit (1)
409
410 fontfile = sys.argv[1]
411 glyphs = sys.argv[2:]
412
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400413 # Always include .notdef; anything else?
414 if '.notdef' not in glyphs:
415 glyphs.append ('.notdef')
416
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400417 font = fontTools.ttx.TTFont (fontfile)
418
419 names = font.getGlyphNames()
420 # Convert to glyph names
421 glyphs = [g if g in names else font.getGlyphName(int(g)) for g in glyphs]
422
Behdad Esfahbod350a5272013-07-22 12:02:16 -0400423 if xml:
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400424 import xmlWriter
425 writer = xmlWriter.XMLWriter (sys.stdout)
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400426
427 drop_tables = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', 'PCLT', 'LTSH']
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400428 noneed_tables = ['gasp', 'head', 'hhea', 'maxp', 'name', 'vhea', 'OS/2', 'VDMX', 'loca']
Behdad Esfahbodcd708852013-07-22 14:30:23 -0400429
Behdad Esfahbodb1e1ab62013-07-22 15:24:09 -0400430 # For now drop these
431 drop_tables += ['cvt ', 'fpgm', 'prep']
432
Behdad Esfahbod8842ce22013-07-22 13:01:33 -0400433 for tag in font.keys():
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400434
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400435 if tag == 'GlyphOrder':
436 continue
437
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400438 if tag in drop_tables:
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400439 if verbose:
440 print tag, "dropped."
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400441 del font[tag]
442 continue
443
Behdad Esfahbodcd708852013-07-22 14:30:23 -0400444 if tag in noneed_tables:
445 if verbose:
446 print tag, "intact."
447 continue
448
Behdad Esfahbod8842ce22013-07-22 13:01:33 -0400449 clazz = fontTools.ttLib.getTableClass(tag)
450 if 'subset' not in vars (clazz):
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400451 if verbose:
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400452 print tag, "skipped................"
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400453 continue
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400454
Behdad Esfahbod8842ce22013-07-22 13:01:33 -0400455 table = font[tag]
456 if not table.subset (glyphs):
Behdad Esfahbod9d02c2d2013-07-22 11:08:37 -0400457 del font[tag]
Behdad Esfahbod5270ec42013-07-22 12:57:02 -0400458 if verbose:
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400459 print tag, "subset empty; dropped."
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400460 else:
Behdad Esfahbod350a5272013-07-22 12:02:16 -0400461 if xml:
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400462 writer.begintag (tag)
463 writer.newline ()
464 font[tag].toXML(writer, font)
465 writer.endtag (tag)
466 writer.newline ()
Behdad Esfahbod350a5272013-07-22 12:02:16 -0400467 if verbose:
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400468 print tag, "subsetted."
469
470 glyphOrder = font.getGlyphOrder()
471 glyphOrder = [g for g in glyphOrder if g in glyphs]
472 font.setGlyphOrder (glyphOrder)
473 font._buildReverseGlyphOrderDict ()
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400474
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400475 font.save (fontfile + '.subset')