blob: 8da9a210d088e34c5b945b3781be23084a810aa3 [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):
223 for l in self.Lookup:
224 l.subset (glyphs)
225 return self.LookupCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400226
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400227@add_method(fontTools.ttLib.tables.otTables.GSUB, fontTools.ttLib.tables.otTables.GPOS)
228def subset (self, glyphs):
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400229 self.LookupList.subset (glyphs)
230 # TODO Prune featurelists
231 #print vars (self.ScriptList.ScriptRecord[0].Script.LangSysRecord[0].LangSys)
232 #self.ScriptList.ScriptCount = len (self.ScriptList.ScriptRecord)
233 #self.FeatureList.FeatureCount = len (self.FeatureList.FeatureRecord)
234 return True # Retain the possibly empty table
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400235
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400236@add_method(fontTools.ttLib.tables.otTables.GDEF)
237def subset (self, glyphs):
238 if self.LigCaretList:
239 indices = self.LigCaretList.Coverage.subset (glyphs)
240 self.LigCaretList.LigGlyph = [self.LigCaretList.LigGlyph[i] for i in indices]
241 self.LigCaretList.LigGlyphCount = len (self.LigCaretList.LigGlyph)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400242 if not self.LigCaretList.LigGlyphCount:
243 self.LigCaretList = None
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400244 if self.MarkAttachClassDef:
245 self.MarkAttachClassDef.classDefs = {g:v for g,v in self.MarkAttachClassDef.classDefs.items() if g in glyphs}
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400246 if not self.MarkAttachClassDef.classDefs:
247 self.MarkAttachClassDef = None
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400248 if self.GlyphClassDef:
249 self.GlyphClassDef.classDefs = {g:v for g,v in self.GlyphClassDef.classDefs.items() if g in glyphs}
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400250 if not self.GlyphClassDef.classDefs:
251 self.GlyphClassDef = None
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400252 if self.AttachList:
253 indices = self.AttachList.Coverage.subset (glyphs)
254 self.AttachList.AttachPoint = [self.AttachList.AttachPoint[i] for i in indices]
255 self.AttachList.GlyphCount = len (self.AttachList.AttachPoint)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400256 if not self.AttachList.GlyphCount:
257 self.AttachList = None
258 return True # Retain the possibly empty table
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400259
260
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400261if __name__ == '__main__':
262
263 import sys
264
265 if len (sys.argv) < 3:
266 print >>sys.stderr, "usage: pyotlss.py font-file glyph..."
267 sys.exit (1)
268
269 fontfile = sys.argv[1]
270 glyphs = sys.argv[2:]
271
272 font = fontTools.ttx.TTFont (fontfile)
273
274 names = font.getGlyphNames()
275 # Convert to glyph names
276 glyphs = [g if g in names else font.getGlyphName(int(g)) for g in glyphs]
277
Behdad Esfahbod9d02c2d2013-07-22 11:08:37 -0400278 for tag in ['GDEF', 'GSUB', 'GPOS']:
279 if tag not in font:
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400280 continue
Behdad Esfahbod9d02c2d2013-07-22 11:08:37 -0400281 if not font[tag].table.subset (glyphs):
282 del font[tag]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400283
284 font.save (fontfile + '.subset.ttf')