blob: 5502cc94f28c79f62d7ca72e731e1ac832db50a7 [file] [log] [blame]
Behdad Esfahbod54660612013-07-21 18:16:55 -04001#!/usr/bin/python
2
3# Python OpenType Layout Subsetter
4# Writte by: Behdad Esfahbod
5
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):
29 self.classDefs = {g:v for g,v in self.classDefs.items() if g in glyphs}
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040030 return len (self.classDefs)
31
32@add_method(fontTools.ttLib.tables.otTables.ClassDef)
33def __nonzero__ (self):
34 return bool (self.classDefs)
Behdad Esfahbod54660612013-07-21 18:16:55 -040035
36@add_method(fontTools.ttLib.tables.otTables.SingleSubst)
37def subset (self, glyphs):
38 if self.Format in [1, 2]:
39 self.mapping = {g:v for g,v in self.mapping.items() if g in glyphs}
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040040 return len (self.mapping)
Behdad Esfahbod54660612013-07-21 18:16:55 -040041 else:
42 assert 0, "unknown format: %s" % self.Format
43
44@add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
45def subset (self, glyphs):
46 if self.Format == 1:
47 indices = self.Coverage.subset (glyphs)
48 self.Sequence = [self.Sequence[i] for i in indices]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040049 self.SequenceCount = len (self.Sequence)
50 return self.SequenceCount
Behdad Esfahbod54660612013-07-21 18:16:55 -040051 else:
52 assert 0, "unknown format: %s" % self.Format
53
54@add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
55def subset (self, glyphs):
56 if self.Format == 1:
57 self.alternates = {g:v for g,v in self.alternates.items() if g in glyphs}
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040058 return len (self.alternates)
Behdad Esfahbod54660612013-07-21 18:16:55 -040059 else:
60 assert 0, "unknown format: %s" % self.Format
61
62@add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
63def subset (self, glyphs):
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040064 if self.Format == 1:
65 self.ligatures = {g:v for g,v in self.ligatures.items() if g in glyphs}
66 self.ligatures = {g:[seq for seq in seqs if all(c in glyphs for c in seq.Component)]
67 for g,seqs in self.ligatures.items()}
68 self.ligatures = {g:v for g,v in self.ligatures.items() if v}
69 return len (self.ligatures)
70 else:
71 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -040072
Behdad Esfahbod54660612013-07-21 18:16:55 -040073@add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
74def subset (self, glyphs):
75 if self.Format == 1:
76 indices = self.Coverage.subset (glyphs)
77 self.Substitute = [self.Substitute[i] for i in indices]
78 self.GlyphCount = len (self.Substitute)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040079 return self.GlyphCount and all (c.subset (glyphs) for c in self.LookAheadCoverage + self.BacktrackCoverage)
Behdad Esfahbod54660612013-07-21 18:16:55 -040080 else:
81 assert 0, "unknown format: %s" % self.Format
82
83@add_method(fontTools.ttLib.tables.otTables.SinglePos)
84def subset (self, glyphs):
85 if self.Format == 1:
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040086 return len (self.Coverage.subset (glyphs))
Behdad Esfahbod54660612013-07-21 18:16:55 -040087 elif self.Format == 2:
88 indices = self.Coverage.subset (glyphs)
89 self.Value = [self.Value[i] for i in indices]
90 self.ValueCount = len (self.Value)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040091 return self.ValueCount
Behdad Esfahbod54660612013-07-21 18:16:55 -040092 else:
93 assert 0, "unknown format: %s" % self.Format
94
95@add_method(fontTools.ttLib.tables.otTables.PairPos)
96def subset (self, glyphs):
97 if self.Format == 1:
98 indices = self.Coverage.subset (glyphs)
99 self.PairSet = [self.PairSet[i] for i in indices]
100 for p in self.PairSet:
101 p.PairValueRecord = [r for r in p.PairValueRecord if r.SecondGlyph in glyphs]
102 p.PairValueCount = len (p.PairValueRecord)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400103 self.PairSet = [p for p in self.PairSet if p.PairValueCount]
Behdad Esfahbod54660612013-07-21 18:16:55 -0400104 self.PairSetCount = len (self.PairSet)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400105 return self.PairSetCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400106 elif self.Format == 2:
107 self.Coverage.subset (glyphs)
108 self.ClassDef1.subset (glyphs)
109 self.ClassDef2.subset (glyphs)
110 # TODO Prune empty classes
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400111 return self.Coverage and self.ClassDef1 and self.ClassDef2
Behdad Esfahbod54660612013-07-21 18:16:55 -0400112 else:
113 assert 0, "unknown format: %s" % self.Format
114
115@add_method(fontTools.ttLib.tables.otTables.CursivePos)
116def subset (self, glyphs):
117 if self.Format == 1:
118 indices = self.Coverage.subset (glyphs)
119 self.EntryExitRecord = [self.EntryExitRecord[i] for i in indices]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400120 self.EntryExitCount = len (self.EntryExitRecord)
121 return self.EntryExitCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400122 else:
123 assert 0, "unknown format: %s" % self.Format
124
125@add_method(fontTools.ttLib.tables.otTables.MarkBasePos)
126def subset (self, glyphs):
127 if self.Format == 1:
128 mark_indices = self.MarkCoverage.subset (glyphs)
129 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices]
130 self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord)
131 base_indices = self.BaseCoverage.subset (glyphs)
132 self.BaseArray.BaseRecord = [self.BaseArray.BaseRecord[i] for i in base_indices]
133 self.BaseArray.BaseCount = len (self.BaseArray.BaseRecord)
134 # TODO Prune empty classes
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400135 return self.MarkArray.MarkCount and self.BaseArray.BaseCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400136 else:
137 assert 0, "unknown format: %s" % self.Format
138
139@add_method(fontTools.ttLib.tables.otTables.MarkLigPos)
140def subset (self, glyphs):
141 if self.Format == 1:
142 mark_indices = self.MarkCoverage.subset (glyphs)
143 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices]
144 self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord)
145 ligature_indices = self.LigatureCoverage.subset (glyphs)
146 self.LigatureArray.LigatureAttach = [self.LigatureArray.LigatureAttach[i] for i in ligature_indices]
147 self.LigatureArray.LigatureCount = len (self.LigatureArray.LigatureAttach)
148 # TODO Prune empty classes
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400149 return self.MarkArray.MarkCount and self.LigatureArray.LigatureCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400150 else:
151 assert 0, "unknown format: %s" % self.Format
152
153@add_method(fontTools.ttLib.tables.otTables.MarkMarkPos)
154def subset (self, glyphs):
155 if self.Format == 1:
156 mark1_indices = self.Mark1Coverage.subset (glyphs)
157 self.Mark1Array.MarkRecord = [self.Mark1Array.MarkRecord[i] for i in mark1_indices]
158 self.Mark1Array.MarkCount = len (self.Mark1Array.MarkRecord)
159 mark2_indices = self.Mark2Coverage.subset (glyphs)
160 self.Mark2Array.Mark2Record = [self.Mark2Array.Mark2Record[i] for i in mark2_indices]
161 self.Mark2Array.MarkCount = len (self.Mark2Array.Mark2Record)
162 # TODO Prune empty classes
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400163 return self.Mark1Array.MarkCount and self.Mark2Array.MarkCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400164 else:
165 assert 0, "unknown format: %s" % self.Format
166
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400167@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ContextPos)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400168def subset (self, glyphs):
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400169 if self.Format == 1:
Behdad Esfahbodb7fef902013-07-21 22:48:08 -0400170 indices = self.Coverage.subset (glyphs)
171 self.SubRuleSet = [self.SubRuleSet[i] for i in indices]
172 self.SubRuleSetCount = len (self.SubRuleSet)
173 for rs in self.SubRuleSet:
174 rs.SubRule = [r for r in rs.SubRule
175 if all (g in glyphs for g in r.Input)]
176 rs.SubRuleCount = len (rs.SubRule)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400177 # Prune empty subrulesets
178 return self.SubRuleSetCount
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400179 elif self.Format == 2:
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400180 return self.Coverage.subset (glyphs) and self.ClassDef.subset (glyphs)
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400181 elif self.Format == 3:
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400182 return all (c.subset (glyphs) for c in self.Coverage)
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400183 else:
184 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400185
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400186@add_method(fontTools.ttLib.tables.otTables.ChainContextSubst, fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400187def subset (self, glyphs):
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400188 if self.Format == 1:
Behdad Esfahbodb7fef902013-07-21 22:48:08 -0400189 indices = self.Coverage.subset (glyphs)
190 self.ChainSubRuleSet = [self.ChainSubRuleSet[i] for i in indices]
191 self.ChainSubRuleSetCount = len (self.ChainSubRuleSet)
192 for rs in self.ChainSubRuleSet:
193 rs.ChainSubRule = [r for r in rs.ChainSubRule
194 if all (g in glyphs for g in r.Backtrack + r.Input + r.LookAhead)]
195 rs.ChainSubRuleCount = len (rs.ChainSubRule)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400196 # Prune empty subrulesets
197 return self.ChainSubRuleSetCount
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400198 elif self.Format == 2:
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400199 return self.Coverage.subset (glyphs) and \
200 self.LookAheadClassDef.subset (glyphs) and \
201 self.BacktrackClassDef.subset (glyphs) and \
202 self.InputClassDef.subset (glyphs)
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400203 elif self.Format == 3:
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400204 return all (c.subset (glyphs) for c in self.InputCoverage + self.LookAheadCoverage + self.BacktrackCoverage)
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400205 else:
206 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400207
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400208@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400209def subset (self, glyphs):
210 if self.Format == 1:
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400211 return self.ExtSubTable.subset (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400212 else:
213 assert 0, "unknown format: %s" % self.Format
214
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400215@add_method(fontTools.ttLib.tables.otTables.Lookup)
216def subset (self, glyphs):
Behdad Esfahbod1be53452013-07-21 22:52:15 -0400217 self.SubTable = [s for s in self.SubTable if s.subset (glyphs)]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400218 self.SubTableCount = len (self.SubTable)
219 return self.SubTableCount
220
221@add_method(fontTools.ttLib.tables.otTables.LookupList)
222def subset (self, glyphs):
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400223 "Returns the indices of nonempty lookups."
224 return [i for (i,l) in enumerate (self.Lookup) if l.subset (glyphs)]
225
226@add_method(fontTools.ttLib.tables.otTables.LookupList)
227def subset_lookups (self, lookup_indices):
228 self.Lookup = [self.Lookup[i] for i in lookup_indices]
229 self.LookupCount = len (self.Lookup)
230
231@add_method(fontTools.ttLib.tables.otTables.Feature)
232def subset_lookups (self, lookup_indices):
233 self.LookupListIndex = [l for l in self.LookupListIndex if l in lookup_indices]
234 # Now map them.
235 self.LookupListIndex = [lookup_indices.index (l) for l in self.LookupListIndex]
236 self.LookupCount = len (self.LookupListIndex)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400237 return self.LookupCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400238
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400239@add_method(fontTools.ttLib.tables.otTables.FeatureList)
240def subset_lookups (self, lookup_indices):
241 "Returns the indices of nonempty features."
242 feature_indices = [i for (i,f) in enumerate (self.FeatureRecord) if f.Feature.subset_lookups (lookup_indices)]
243 self.FeatureRecord = [self.FeatureRecord[i] for i in feature_indices]
244 self.FeatureCount = len (self.FeatureRecord)
245 return feature_indices
246
247@add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys)
248def subset_features (self, feature_indices):
249 if self.ReqFeatureIndex not in feature_indices:
250 self.ReqFeatureIndex = 65535
251 self.FeatureIndex = [f for f in self.FeatureIndex if f in feature_indices]
252 self.FeatureCount = len (self.FeatureIndex)
253 return self.FeatureCount
254
255@add_method(fontTools.ttLib.tables.otTables.Script)
256def subset_features (self, feature_indices):
257 if self.DefaultLangSys and not self.DefaultLangSys.subset_features (feature_indices):
258 self.DefaultLangSys = None
259 self.LangSysRecord = [l for l in self.LangSysRecord if l.LangSys.subset_features (feature_indices)]
260 self.LangSysCount = len (self.LangSysRecord)
261 return self.LangSysCount
262
263@add_method(fontTools.ttLib.tables.otTables.ScriptList)
264def subset_features (self, feature_indices):
265 self.ScriptRecord = [s for s in self.ScriptRecord if s.Script.subset_features (feature_indices)]
266 self.ScriptCount = len (self.ScriptRecord)
267 return self.ScriptCount
268
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400269@add_method(fontTools.ttLib.tables.otTables.GSUB, fontTools.ttLib.tables.otTables.GPOS)
270def subset (self, glyphs):
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400271 lookup_indices = self.LookupList.subset (glyphs)
272 self.subset_lookups (lookup_indices)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400273 return True # Retain the possibly empty table
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400274
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400275@add_method(fontTools.ttLib.tables.otTables.GSUB, fontTools.ttLib.tables.otTables.GPOS)
276def subset_lookups (self, lookup_indices):
277 "Retrains specified lookups, then removes empty features, language systems, and scripts."
278 self.LookupList.subset_lookups (lookup_indices)
279 feature_indices = self.FeatureList.subset_lookups (lookup_indices)
280 self.ScriptList.subset_features (feature_indices)
281
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400282@add_method(fontTools.ttLib.tables.otTables.GDEF)
283def subset (self, glyphs):
284 if self.LigCaretList:
285 indices = self.LigCaretList.Coverage.subset (glyphs)
286 self.LigCaretList.LigGlyph = [self.LigCaretList.LigGlyph[i] for i in indices]
287 self.LigCaretList.LigGlyphCount = len (self.LigCaretList.LigGlyph)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400288 if not self.LigCaretList.LigGlyphCount:
289 self.LigCaretList = None
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400290 if self.MarkAttachClassDef:
291 self.MarkAttachClassDef.classDefs = {g:v for g,v in self.MarkAttachClassDef.classDefs.items() if g in glyphs}
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400292 if not self.MarkAttachClassDef.classDefs:
293 self.MarkAttachClassDef = None
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400294 if self.GlyphClassDef:
295 self.GlyphClassDef.classDefs = {g:v for g,v in self.GlyphClassDef.classDefs.items() if g in glyphs}
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400296 if not self.GlyphClassDef.classDefs:
297 self.GlyphClassDef = None
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400298 if self.AttachList:
299 indices = self.AttachList.Coverage.subset (glyphs)
300 self.AttachList.AttachPoint = [self.AttachList.AttachPoint[i] for i in indices]
301 self.AttachList.GlyphCount = len (self.AttachList.AttachPoint)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400302 if not self.AttachList.GlyphCount:
303 self.AttachList = None
304 return True # Retain the possibly empty table
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400305
306
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400307if __name__ == '__main__':
308
309 import sys
310
311 if len (sys.argv) < 3:
312 print >>sys.stderr, "usage: pyotlss.py font-file glyph..."
313 sys.exit (1)
314
315 fontfile = sys.argv[1]
316 glyphs = sys.argv[2:]
317
318 font = fontTools.ttx.TTFont (fontfile)
319
320 names = font.getGlyphNames()
321 # Convert to glyph names
322 glyphs = [g if g in names else font.getGlyphName(int(g)) for g in glyphs]
323
Behdad Esfahbod9d02c2d2013-07-22 11:08:37 -0400324 for tag in ['GDEF', 'GSUB', 'GPOS']:
325 if tag not in font:
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400326 continue
Behdad Esfahbod9d02c2d2013-07-22 11:08:37 -0400327 if not font[tag].table.subset (glyphs):
328 del font[tag]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400329
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400330 font.save (fontfile + '.subset')