blob: 9d9f35a6b653cc99b4b05970a4659b27e371a473 [file] [log] [blame]
Behdad Esfahbod54660612013-07-21 18:16:55 -04001#!/usr/bin/python
2
3# Python OpenType Layout Subsetter
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04004#
5# Copyright 2013 Google, Inc. All Rights Reserved.
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19# Google Author(s): Behdad Esfahbod
20#
Behdad Esfahbod54660612013-07-21 18:16:55 -040021
22import fontTools.ttx
Behdad Esfahbod54660612013-07-21 18:16:55 -040023
Behdad Esfahbod54660612013-07-21 18:16:55 -040024
Behdad Esfahbod02b92062013-07-21 18:40:59 -040025def add_method (*clazzes):
Behdad Esfahbod54660612013-07-21 18:16:55 -040026 def wrapper(method):
Behdad Esfahbod02b92062013-07-21 18:40:59 -040027 for clazz in clazzes:
28 setattr (clazz, method.func_name, method)
Behdad Esfahbod54660612013-07-21 18:16:55 -040029 return wrapper
30
Behdad Esfahbod78661bb2013-07-23 10:23:42 -040031def unique_sorted (l):
32 return sorted ({v:1 for v in l}.keys ())
33
34
Behdad Esfahbod54660612013-07-21 18:16:55 -040035@add_method(fontTools.ttLib.tables.otTables.Coverage)
Behdad Esfahbod610b0552013-07-23 14:52:18 -040036def intersect_glyphs (self, glyphs):
37 "Returns ascending list of matching coverage values."
38 return [i for (i,g) in enumerate (self.glyphs) if g in glyphs]
39
40@add_method(fontTools.ttLib.tables.otTables.Coverage)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -040041def subset_glyphs (self, glyphs):
Behdad Esfahbodd821ea02013-07-23 10:50:43 -040042 "Returns ascending list of remaining coverage values."
Behdad Esfahbod610b0552013-07-23 14:52:18 -040043 indices = self.intersect_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -040044 self.glyphs = [g for g in self.glyphs if g in glyphs]
45 return indices
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040046
Behdad Esfahbod54660612013-07-21 18:16:55 -040047@add_method(fontTools.ttLib.tables.otTables.ClassDef)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -040048def subset_glyphs (self, glyphs):
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -040049 "Returns ascending list of remaining classes."
Behdad Esfahbod54660612013-07-21 18:16:55 -040050 self.classDefs = {g:v for g,v in self.classDefs.items() if g in glyphs}
Behdad Esfahbod78661bb2013-07-23 10:23:42 -040051 return unique_sorted (self.classDefs.values ())
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -040052
53@add_method(fontTools.ttLib.tables.otTables.ClassDef)
54def remap (self, class_map):
55 "Remaps classes."
56 self.classDefs = {g:class_map.index (v) for g,v in self.classDefs.items()}
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040057
Behdad Esfahbod54660612013-07-21 18:16:55 -040058@add_method(fontTools.ttLib.tables.otTables.SingleSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -040059def closure_glyphs (self, glyphs, table):
60 if self.Format in [1, 2]:
61 return [v for g,v in self.mapping.items() if g in glyphs]
62 else:
63 assert 0, "unknown format: %s" % self.Format
64
65@add_method(fontTools.ttLib.tables.otTables.SingleSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -040066def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -040067 if self.Format in [1, 2]:
68 self.mapping = {g:v for g,v in self.mapping.items() if g in glyphs}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -040069 return bool (self.mapping)
Behdad Esfahbod54660612013-07-21 18:16:55 -040070 else:
71 assert 0, "unknown format: %s" % self.Format
72
73@add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -040074def closure_glyphs (self, glyphs, table):
75 if self.Format == 1:
76 indices = self.Coverage.intersect_glyphs (glyphs)
77 return sum ((self.Sequence[i].Substitute for i in indices), [])
78 else:
79 assert 0, "unknown format: %s" % self.Format
80
81@add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -040082def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -040083 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -040084 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -040085 self.Sequence = [self.Sequence[i] for i in indices]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040086 self.SequenceCount = len (self.Sequence)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -040087 return bool (self.SequenceCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -040088 else:
89 assert 0, "unknown format: %s" % self.Format
90
91@add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -040092def closure_glyphs (self, glyphs, table):
93 if self.Format == 1:
94 return sum ((v for g,v in self.alternates.items() if g in glyphs), [])
95 else:
96 assert 0, "unknown format: %s" % self.Format
97
98@add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -040099def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400100 if self.Format == 1:
101 self.alternates = {g:v for g,v in self.alternates.items() if g in glyphs}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400102 return bool (self.alternates)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400103 else:
104 assert 0, "unknown format: %s" % self.Format
105
106@add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400107def closure_glyphs (self, glyphs, table):
108 if self.Format == 1:
109 return sum (([seq.LigGlyph for seq in seqs if all(c in glyphs for c in seq.Component)]
110 for g,seqs in self.ligatures.items()), [])
111 else:
112 assert 0, "unknown format: %s" % self.Format
113
114@add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400115def subset_glyphs (self, glyphs):
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400116 if self.Format == 1:
117 self.ligatures = {g:v for g,v in self.ligatures.items() if g in glyphs}
118 self.ligatures = {g:[seq for seq in seqs if all(c in glyphs for c in seq.Component)]
119 for g,seqs in self.ligatures.items()}
120 self.ligatures = {g:v for g,v in self.ligatures.items() if v}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400121 return bool (self.ligatures)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400122 else:
123 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400124
Behdad Esfahbod54660612013-07-21 18:16:55 -0400125@add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400126def closure_glyphs (self, glyphs, table):
127 if self.Format == 1:
128 indices = self.Coverage.intersect_glyphs (glyphs)
129 if not indices or \
130 not all (c.intersect_glyphs (glyphs) for c in self.LookAheadCoverage + self.BacktrackCoverage):
131 return []
132 return [self.Substitute[i] for i in indices]
133 else:
134 assert 0, "unknown format: %s" % self.Format
135
136@add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400137def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400138 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400139 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400140 self.Substitute = [self.Substitute[i] for i in indices]
141 self.GlyphCount = len (self.Substitute)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400142 return bool (self.GlyphCount and all (c.subset_glyphs (glyphs) for c in self.LookAheadCoverage + self.BacktrackCoverage))
Behdad Esfahbod54660612013-07-21 18:16:55 -0400143 else:
144 assert 0, "unknown format: %s" % self.Format
145
146@add_method(fontTools.ttLib.tables.otTables.SinglePos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400147def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400148 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400149 return len (self.Coverage.subset_glyphs (glyphs))
Behdad Esfahbod54660612013-07-21 18:16:55 -0400150 elif self.Format == 2:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400151 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400152 self.Value = [self.Value[i] for i in indices]
153 self.ValueCount = len (self.Value)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400154 return bool (self.ValueCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400155 else:
156 assert 0, "unknown format: %s" % self.Format
157
158@add_method(fontTools.ttLib.tables.otTables.PairPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400159def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400160 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400161 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400162 self.PairSet = [self.PairSet[i] for i in indices]
163 for p in self.PairSet:
164 p.PairValueRecord = [r for r in p.PairValueRecord if r.SecondGlyph in glyphs]
165 p.PairValueCount = len (p.PairValueRecord)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400166 self.PairSet = [p for p in self.PairSet if p.PairValueCount]
Behdad Esfahbod54660612013-07-21 18:16:55 -0400167 self.PairSetCount = len (self.PairSet)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400168 return bool (self.PairSetCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400169 elif self.Format == 2:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400170 class1_map = self.ClassDef1.subset_glyphs (glyphs)
171 class2_map = self.ClassDef2.subset_glyphs (glyphs)
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -0400172 self.ClassDef1.remap (class1_map)
173 self.ClassDef2.remap (class2_map)
174 self.Class1Record = [self.Class1Record[i] for i in class1_map]
175 for c in self.Class1Record:
176 c.Class2Record = [c.Class2Record[i] for i in class2_map]
177 self.Class1Count = len (class1_map)
178 self.Class2Count = len (class2_map)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400179 return bool (self.Class1Count and self.Class2Count and self.Coverage.subset_glyphs (glyphs))
Behdad Esfahbod54660612013-07-21 18:16:55 -0400180 else:
181 assert 0, "unknown format: %s" % self.Format
182
183@add_method(fontTools.ttLib.tables.otTables.CursivePos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400184def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400185 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400186 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400187 self.EntryExitRecord = [self.EntryExitRecord[i] for i in indices]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400188 self.EntryExitCount = len (self.EntryExitRecord)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400189 return bool (self.EntryExitCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400190 else:
191 assert 0, "unknown format: %s" % self.Format
192
193@add_method(fontTools.ttLib.tables.otTables.MarkBasePos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400194def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400195 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400196 mark_indices = self.MarkCoverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400197 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices]
198 self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400199 base_indices = self.BaseCoverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400200 self.BaseArray.BaseRecord = [self.BaseArray.BaseRecord[i] for i in base_indices]
201 self.BaseArray.BaseCount = len (self.BaseArray.BaseRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400202 # Prune empty classes
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400203 class_indices = unique_sorted (v.Class for v in self.MarkArray.MarkRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400204 self.ClassCount = len (class_indices)
205 for m in self.MarkArray.MarkRecord:
206 m.Class = class_indices.index (m.Class)
207 for b in self.BaseArray.BaseRecord:
208 b.BaseAnchor = [b.BaseAnchor[i] for i in class_indices]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400209 return bool (self.ClassCount and self.MarkArray.MarkCount and self.BaseArray.BaseCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400210 else:
211 assert 0, "unknown format: %s" % self.Format
212
213@add_method(fontTools.ttLib.tables.otTables.MarkLigPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400214def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400215 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400216 mark_indices = self.MarkCoverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400217 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices]
218 self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400219 ligature_indices = self.LigatureCoverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400220 self.LigatureArray.LigatureAttach = [self.LigatureArray.LigatureAttach[i] for i in ligature_indices]
221 self.LigatureArray.LigatureCount = len (self.LigatureArray.LigatureAttach)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400222 # Prune empty classes
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400223 class_indices = unique_sorted (v.Class for v in self.MarkArray.MarkRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400224 self.ClassCount = len (class_indices)
225 for m in self.MarkArray.MarkRecord:
226 m.Class = class_indices.index (m.Class)
227 for l in self.LigatureArray.LigatureAttach:
228 for c in l.ComponentRecord:
229 c.LigatureAnchor = [c.LigatureAnchor[i] for i in class_indices]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400230 return bool (self.ClassCount and self.MarkArray.MarkCount and self.LigatureArray.LigatureCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400231 else:
232 assert 0, "unknown format: %s" % self.Format
233
234@add_method(fontTools.ttLib.tables.otTables.MarkMarkPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400235def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400236 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400237 mark1_indices = self.Mark1Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400238 self.Mark1Array.MarkRecord = [self.Mark1Array.MarkRecord[i] for i in mark1_indices]
239 self.Mark1Array.MarkCount = len (self.Mark1Array.MarkRecord)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400240 mark2_indices = self.Mark2Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400241 self.Mark2Array.Mark2Record = [self.Mark2Array.Mark2Record[i] for i in mark2_indices]
242 self.Mark2Array.MarkCount = len (self.Mark2Array.Mark2Record)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400243 # Prune empty classes
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400244 class_indices = unique_sorted (v.Class for v in self.Mark1Array.MarkRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400245 self.ClassCount = len (class_indices)
246 for m in self.Mark1Array.MarkRecord:
247 m.Class = class_indices.index (m.Class)
248 for b in self.Mark2Array.Mark2Record:
249 b.Mark2Anchor = [b.Mark2Anchor[i] for i in class_indices]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400250 return bool (self.ClassCount and self.Mark1Array.MarkCount and self.Mark2Array.MarkCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400251 else:
252 assert 0, "unknown format: %s" % self.Format
253
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400254@add_method(fontTools.ttLib.tables.otTables.SingleSubst,
255 fontTools.ttLib.tables.otTables.MultipleSubst,
256 fontTools.ttLib.tables.otTables.AlternateSubst,
257 fontTools.ttLib.tables.otTables.LigatureSubst,
258 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
259 fontTools.ttLib.tables.otTables.SinglePos,
260 fontTools.ttLib.tables.otTables.PairPos,
261 fontTools.ttLib.tables.otTables.CursivePos,
262 fontTools.ttLib.tables.otTables.MarkBasePos,
263 fontTools.ttLib.tables.otTables.MarkLigPos,
264 fontTools.ttLib.tables.otTables.MarkMarkPos)
265def subset_lookups (self, lookup_indices):
266 pass
267
268@add_method(fontTools.ttLib.tables.otTables.SingleSubst,
269 fontTools.ttLib.tables.otTables.MultipleSubst,
270 fontTools.ttLib.tables.otTables.AlternateSubst,
271 fontTools.ttLib.tables.otTables.LigatureSubst,
272 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
273 fontTools.ttLib.tables.otTables.SinglePos,
274 fontTools.ttLib.tables.otTables.PairPos,
275 fontTools.ttLib.tables.otTables.CursivePos,
276 fontTools.ttLib.tables.otTables.MarkBasePos,
277 fontTools.ttLib.tables.otTables.MarkLigPos,
278 fontTools.ttLib.tables.otTables.MarkMarkPos)
279def collect_lookups (self):
280 return []
281
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400282@add_method(fontTools.ttLib.tables.otTables.ContextSubst)
283def closure_glyphs (self, glyphs, table):
284 if self.Format == 1:
285 assert 0 # XXX
286 elif self.Format == 2:
287 assert 0 # XXX
288 elif self.Format == 3:
Behdad Esfahbod3dce28c2013-07-23 15:05:45 -0400289 if not all (c.intersect_glyphs (glyphs) for c in self.Coverage):
290 return []
291 return sum ((table.table.LookupList.Lookup[ll.LookupListIndex].closure_glyphs (glyphs, table) \
292 for ll in self.SubstLookupRecord), [])
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400293 else:
294 assert 0, "unknown format: %s" % self.Format
295
Behdad Esfahbod00776972013-07-23 15:33:00 -0400296@add_method(fontTools.ttLib.tables.otTables.ChainContextSubst)
297def closure_glyphs (self, glyphs, table):
298 if self.Format == 1:
299 return []
300 assert 0 # XXX
301 elif self.Format == 2:
302 assert 0 # XXX
303 elif self.Format == 3:
304 if not all (c.intersect_glyphs (glyphs) \
305 for c in self.InputCoverage + self.LookAheadCoverage + self.BacktrackCoverage):
306 return []
307 return sum ((table.table.LookupList.Lookup[ll.LookupListIndex].closure_glyphs (glyphs, table) \
308 for ll in self.SubstLookupRecord), [])
309 else:
310 assert 0, "unknown format: %s" % self.Format
311
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400312@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ContextPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400313def subset_glyphs (self, glyphs):
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400314 if self.Format == 1:
Behdad Esfahbod398d3892013-07-23 15:29:40 -0400315 # XXX Needs more work
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400316 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbodb7fef902013-07-21 22:48:08 -0400317 self.SubRuleSet = [self.SubRuleSet[i] for i in indices]
318 self.SubRuleSetCount = len (self.SubRuleSet)
319 for rs in self.SubRuleSet:
320 rs.SubRule = [r for r in rs.SubRule
321 if all (g in glyphs for g in r.Input)]
322 rs.SubRuleCount = len (rs.SubRule)
Behdad Esfahbod398d3892013-07-23 15:29:40 -0400323 # TODO Prune empty subrulesets
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400324 return bool (self.SubRuleSetCount)
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400325 elif self.Format == 2:
Behdad Esfahbod398d3892013-07-23 15:29:40 -0400326 # XXX Needs more work
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400327 return bool (self.Coverage.subset_glyphs (glyphs) and self.ClassDef.subset_glyphs (glyphs))
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400328 elif self.Format == 3:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400329 return all (c.subset_glyphs (glyphs) for c in self.Coverage)
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400330 else:
331 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400332
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400333@add_method(fontTools.ttLib.tables.otTables.ChainContextSubst, fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400334def subset_glyphs (self, glyphs):
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400335 if self.Format == 1:
Behdad Esfahbod398d3892013-07-23 15:29:40 -0400336 # XXX Needs more work
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400337 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbodb7fef902013-07-21 22:48:08 -0400338 self.ChainSubRuleSet = [self.ChainSubRuleSet[i] for i in indices]
339 self.ChainSubRuleSetCount = len (self.ChainSubRuleSet)
340 for rs in self.ChainSubRuleSet:
341 rs.ChainSubRule = [r for r in rs.ChainSubRule
342 if all (g in glyphs for g in r.Backtrack + r.Input + r.LookAhead)]
343 rs.ChainSubRuleCount = len (rs.ChainSubRule)
Behdad Esfahbod398d3892013-07-23 15:29:40 -0400344 # TODO Prune empty subrulesets
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400345 return self.ChainSubRuleSetCount
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400346 elif self.Format == 2:
Behdad Esfahbod398d3892013-07-23 15:29:40 -0400347 # XXX Needs more work
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400348 return self.Coverage.subset_glyphs (glyphs) and \
349 self.LookAheadClassDef.subset_glyphs (glyphs) and \
350 self.BacktrackClassDef.subset_glyphs (glyphs) and \
351 self.InputClassDef.subset_glyphs (glyphs)
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400352 elif self.Format == 3:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400353 return all (c.subset_glyphs (glyphs) for c in self.InputCoverage + self.LookAheadCoverage + self.BacktrackCoverage)
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400354 else:
355 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400356
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400357@add_method(fontTools.ttLib.tables.otTables.ContextSubst)
358def subset_lookups (self, lookup_indices):
359 if self.Format == 1:
360 for rs in self.SubRuleSet:
361 for r in rs.SubRule:
362 r.SubstLookupRecord = [ll for ll in r.SubstLookupRecord if ll.LookupListIndex in lookup_indices]
363 for ll in r.SubstLookupRecord:
364 ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
365 elif self.Format == 2:
366 assert 0 # XXX
367 elif self.Format == 3:
368 self.SubstLookupRecord = [ll for ll in self.SubstLookupRecord if ll.LookupListIndex in lookup_indices]
369 for ll in self.SubstLookupRecord:
370 ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
371 else:
372 assert 0, "unknown format: %s" % self.Format
373
374@add_method(fontTools.ttLib.tables.otTables.ChainContextSubst)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400375def subset_lookups (self, lookup_indices):
Behdad Esfahbod398d3892013-07-23 15:29:40 -0400376 if self.Format == 1:
377 for rs in self.ChainSubRuleSet:
378 for r in rs.ChainSubRule:
379 r.SubstLookupRecord = [ll for ll in r.SubstLookupRecord if ll.LookupListIndex in lookup_indices]
380 for ll in r.SubstLookupRecord:
381 ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
382 elif self.Format == 2:
383 assert 0 # XXX
384 elif self.Format == 3:
385 self.SubstLookupRecord = [ll for ll in self.SubstLookupRecord if ll.LookupListIndex in lookup_indices]
386 for ll in self.SubstLookupRecord:
387 ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
388 else:
389 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400390
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400391@add_method(fontTools.ttLib.tables.otTables.ContextPos)
392def subset_lookups (self, lookup_indices):
393 if self.Format == 1:
394 for rs in self.SubRuleSet:
395 for r in rs.SubRule:
396 r.PosLookupRecord = [ll for ll in r.PosLookupRecord if ll.LookupListIndex in lookup_indices]
397 for ll in r.PosLookupRecord:
398 ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
399 elif self.Format == 2:
400 assert 0 # XXX
401 elif self.Format == 3:
402 self.PosLookupRecord = [ll for ll in self.PosLookupRecord if ll.LookupListIndex in lookup_indices]
403 for ll in self.PosLookupRecord:
404 ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
405 else:
406 assert 0, "unknown format: %s" % self.Format
407
408@add_method(fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbodf2ecc9c2013-07-23 12:40:35 -0400409def subset_lookups (self, lookup_indices):
Behdad Esfahbod398d3892013-07-23 15:29:40 -0400410 if self.Format == 1:
411 for rs in self.ChainSubRuleSet:
412 for r in rs.ChainSubRule:
413 r.PosLookupRecord = [ll for ll in r.PosLookupRecord if ll.LookupListIndex in lookup_indices]
414 for ll in r.PosLookupRecord:
415 ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
416 elif self.Format == 2:
417 assert 0 # XXX
418 elif self.Format == 3:
419 self.PosLookupRecord = [ll for ll in self.PosLookupRecord if ll.LookupListIndex in lookup_indices]
420 for ll in self.PosLookupRecord:
421 ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
422 else:
423 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbodf2ecc9c2013-07-23 12:40:35 -0400424
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400425@add_method(fontTools.ttLib.tables.otTables.ContextSubst)
426def collect_lookups (self):
427 if self.Format == 1:
428 return [ll.LookupListIndex for rs in self.SubRuleSet for r in rs.SubRule for ll in r.SubstLookupRecord]
429 elif self.Format == 2:
430 assert 0 # XXX
431 elif self.Format == 3:
432 return [ll.LookupListIndex for ll in self.SubstLookupRecord]
433 else:
434 assert 0, "unknown format: %s" % self.Format
435
436@add_method(fontTools.ttLib.tables.otTables.ChainContextSubst)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400437def collect_lookups (self):
Behdad Esfahbod398d3892013-07-23 15:29:40 -0400438 if self.Format == 1:
439 return [ll.LookupListIndex for rs in self.ChainSubRuleSet for r in rs.ChainSubRule for ll in r.SubstLookupRecord]
440 elif self.Format == 2:
441 assert 0 # XXX
442 elif self.Format == 3:
443 return [ll.LookupListIndex for ll in self.SubstLookupRecord]
444 else:
445 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400446
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400447@add_method(fontTools.ttLib.tables.otTables.ContextPos)
448def collect_lookups (self):
449 if self.Format == 1:
450 return [ll.LookupListIndex for rs in self.SubRuleSet for r in rs.SubRule for ll in r.PosLookupRecord]
451 elif self.Format == 2:
452 assert 0 # XXX
453 elif self.Format == 3:
454 return [ll.LookupListIndex for ll in self.PosLookupRecord]
455 else:
456 assert 0, "unknown format: %s" % self.Format
457
458@add_method(fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbodf2ecc9c2013-07-23 12:40:35 -0400459def collect_lookups (self):
Behdad Esfahbod398d3892013-07-23 15:29:40 -0400460 if self.Format == 1:
461 return [ll.LookupListIndex for rs in self.ChainSubRuleSet for r in rs.ChainSubRule for ll in r.PosLookupRecord]
462 elif self.Format == 2:
463 assert 0 # XXX
464 elif self.Format == 3:
465 return [ll.LookupListIndex for ll in self.PosLookupRecord]
466 else:
467 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbodf2ecc9c2013-07-23 12:40:35 -0400468
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400469@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst)
470def closure_glyphs (self, glyphs, table):
471 if self.Format == 1:
472 return self.ExtSubTable.closure_glyphs (glyphs, table)
473 else:
474 assert 0, "unknown format: %s" % self.Format
475
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400476@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400477def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400478 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400479 return self.ExtSubTable.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400480 else:
481 assert 0, "unknown format: %s" % self.Format
482
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400483@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
484def subset_lookups (self, lookup_indices):
485 if self.Format == 1:
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400486 return self.ExtSubTable.subset_lookups (lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400487 else:
488 assert 0, "unknown format: %s" % self.Format
489
490@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
491def collect_lookups (self):
492 if self.Format == 1:
493 return self.ExtSubTable.collect_lookups ()
494 else:
495 assert 0, "unknown format: %s" % self.Format
496
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400497@add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400498def closure_glyphs (self, glyphs, table):
499 return sum ((s.closure_glyphs (glyphs, table) for s in self.SubTable), [])
500
501@add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400502def subset_glyphs (self, glyphs):
503 self.SubTable = [s for s in self.SubTable if s.subset_glyphs (glyphs)]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400504 self.SubTableCount = len (self.SubTable)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400505 return bool (self.SubTableCount)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400506
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400507@add_method(fontTools.ttLib.tables.otTables.Lookup)
508def subset_lookups (self, lookup_indices):
509 for s in self.SubTable:
510 s.subset_lookups (lookup_indices)
511
512@add_method(fontTools.ttLib.tables.otTables.Lookup)
513def collect_lookups (self):
514 return unique_sorted (sum ((s.collect_lookups () for s in self.SubTable), []))
515
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400516@add_method(fontTools.ttLib.tables.otTables.LookupList)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400517def subset_glyphs (self, glyphs):
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400518 "Returns the indices of nonempty lookups."
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400519 return [i for (i,l) in enumerate (self.Lookup) if l.subset_glyphs (glyphs)]
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400520
521@add_method(fontTools.ttLib.tables.otTables.LookupList)
522def subset_lookups (self, lookup_indices):
523 self.Lookup = [self.Lookup[i] for i in lookup_indices]
524 self.LookupCount = len (self.Lookup)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400525 for l in self.Lookup:
526 l.subset_lookups (lookup_indices)
527
528@add_method(fontTools.ttLib.tables.otTables.LookupList)
529def closure_lookups (self, lookup_indices):
Behdad Esfahbodbb7e2132013-07-23 13:48:35 -0400530 lookup_indices = unique_sorted (lookup_indices)
531 recurse = lookup_indices
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400532 while True:
Behdad Esfahbodbb7e2132013-07-23 13:48:35 -0400533 recurse_lookups = sum ((self.Lookup[i].collect_lookups () for i in recurse), [])
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400534 recurse_lookups = [l for l in recurse_lookups if l not in lookup_indices]
535 if not recurse_lookups:
Behdad Esfahbodbb7e2132013-07-23 13:48:35 -0400536 return unique_sorted (lookup_indices)
537 recurse_lookups = unique_sorted (recurse_lookups)
538 lookup_indices.extend (recurse_lookups)
539 recurse = recurse_lookups
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400540
541@add_method(fontTools.ttLib.tables.otTables.Feature)
542def subset_lookups (self, lookup_indices):
543 self.LookupListIndex = [l for l in self.LookupListIndex if l in lookup_indices]
544 # Now map them.
545 self.LookupListIndex = [lookup_indices.index (l) for l in self.LookupListIndex]
546 self.LookupCount = len (self.LookupListIndex)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400547 return self.LookupCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400548
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400549@add_method(fontTools.ttLib.tables.otTables.Feature)
550def collect_lookups (self):
551 return self.LookupListIndex[:]
552
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400553@add_method(fontTools.ttLib.tables.otTables.FeatureList)
554def subset_lookups (self, lookup_indices):
555 "Returns the indices of nonempty features."
556 feature_indices = [i for (i,f) in enumerate (self.FeatureRecord) if f.Feature.subset_lookups (lookup_indices)]
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400557 self.subset_features (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400558 return feature_indices
559
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400560@add_method(fontTools.ttLib.tables.otTables.FeatureList)
561def collect_lookups (self, feature_indices):
562 return unique_sorted (sum ((self.FeatureRecord[i].Feature.collect_lookups () for i in feature_indices
563 if i < self.FeatureCount), []))
564
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400565@add_method(fontTools.ttLib.tables.otTables.FeatureList)
566def subset_features (self, feature_indices):
567 self.FeatureRecord = [self.FeatureRecord[i] for i in feature_indices]
568 self.FeatureCount = len (self.FeatureRecord)
569 return bool (self.FeatureCount)
570
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400571@add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys)
572def subset_features (self, feature_indices):
Behdad Esfahbod69ce1502013-07-22 18:00:31 -0400573 if self.ReqFeatureIndex in feature_indices:
574 self.ReqFeatureIndex = feature_indices.index (self.ReqFeatureIndex)
575 else:
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400576 self.ReqFeatureIndex = 65535
577 self.FeatureIndex = [f for f in self.FeatureIndex if f in feature_indices]
Behdad Esfahbod69ce1502013-07-22 18:00:31 -0400578 # Now map them.
579 self.FeatureIndex = [feature_indices.index (f) for f in self.FeatureIndex if f in feature_indices]
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400580 self.FeatureCount = len (self.FeatureIndex)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400581 return bool (self.FeatureCount or self.ReqFeatureIndex != 65535)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400582
583@add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys)
584def collect_features (self):
585 feature_indices = self.FeatureIndex[:]
586 if self.ReqFeatureIndex != 65535:
587 feature_indices.append (self.ReqFeatureIndex)
588 return unique_sorted (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400589
590@add_method(fontTools.ttLib.tables.otTables.Script)
591def subset_features (self, feature_indices):
592 if self.DefaultLangSys and not self.DefaultLangSys.subset_features (feature_indices):
593 self.DefaultLangSys = None
594 self.LangSysRecord = [l for l in self.LangSysRecord if l.LangSys.subset_features (feature_indices)]
595 self.LangSysCount = len (self.LangSysRecord)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400596 return bool (self.LangSysCount or self.DefaultLangSys)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400597
598@add_method(fontTools.ttLib.tables.otTables.Script)
599def collect_features (self):
Behdad Esfahbod2307c8b2013-07-23 11:18:13 -0400600 feature_indices = [l.LangSys.collect_features () for l in self.LangSysRecord]
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400601 if self.DefaultLangSys:
602 feature_indices.append (self.DefaultLangSys.collect_features ())
603 return unique_sorted (sum (feature_indices, []))
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400604
605@add_method(fontTools.ttLib.tables.otTables.ScriptList)
606def subset_features (self, feature_indices):
607 self.ScriptRecord = [s for s in self.ScriptRecord if s.Script.subset_features (feature_indices)]
608 self.ScriptCount = len (self.ScriptRecord)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400609 return bool (self.ScriptCount)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400610
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400611@add_method(fontTools.ttLib.tables.otTables.ScriptList)
612def collect_features (self):
613 return unique_sorted (sum ((s.Script.collect_features () for s in self.ScriptRecord), []))
614
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400615@add_method(fontTools.ttLib.getTableClass('GSUB'))
616def closure_glyphs (self, glyphs):
617 feature_indices = self.table.ScriptList.collect_features ()
618 lookup_indices = self.table.FeatureList.collect_lookups (feature_indices)
619 glyphs = unique_sorted (glyphs)
620 while True:
621 additions = (sum ((self.table.LookupList.Lookup[i].closure_glyphs (glyphs, self) for i in lookup_indices), []))
622 additions = unique_sorted (g for g in additions if g not in glyphs)
623 if not additions:
624 return glyphs
625 glyphs.extend (additions)
626
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400627@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400628def subset_glyphs (self, glyphs):
629 lookup_indices = self.table.LookupList.subset_glyphs (glyphs)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400630 self.subset_lookups (lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400631 self.prune_lookups ()
632 return True
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400633
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400634@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400635def subset_lookups (self, lookup_indices):
636 "Retrains specified lookups, then removes empty features, language systems, and scripts."
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400637 self.table.LookupList.subset_lookups (lookup_indices)
638 feature_indices = self.table.FeatureList.subset_lookups (lookup_indices)
639 self.table.ScriptList.subset_features (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400640
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400641@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
642def prune_lookups (self):
643 "Remove unreferenced lookups"
644 feature_indices = self.table.ScriptList.collect_features ()
645 lookup_indices = self.table.FeatureList.collect_lookups (feature_indices)
646 lookup_indices = self.table.LookupList.closure_lookups (lookup_indices)
647 self.subset_lookups (lookup_indices)
648
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400649@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
650def subset_feature_tags (self, feature_tags):
651 feature_indices = [i for (i,f) in enumerate (self.table.FeatureList.FeatureRecord) if f.FeatureTag in feature_tags]
652 self.table.FeatureList.subset_features (feature_indices)
653 self.table.ScriptList.subset_features (feature_indices)
654
655@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400656def prune_pre_subset (self, options):
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400657 if options['layout-features'] and '*' not in options['layout-features']:
Behdad Esfahboded98c612013-07-23 12:37:41 -0400658 self.subset_feature_tags (options['layout-features'])
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400659 self.prune_lookups ()
660 return True
661
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400662@add_method(fontTools.ttLib.getTableClass('GDEF'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400663def subset_glyphs (self, glyphs):
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400664 table = self.table
665 if table.LigCaretList:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400666 indices = table.LigCaretList.Coverage.subset_glyphs (glyphs)
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400667 table.LigCaretList.LigGlyph = [table.LigCaretList.LigGlyph[i] for i in indices]
668 table.LigCaretList.LigGlyphCount = len (table.LigCaretList.LigGlyph)
669 if not table.LigCaretList.LigGlyphCount:
670 table.LigCaretList = None
671 if table.MarkAttachClassDef:
672 table.MarkAttachClassDef.classDefs = {g:v for g,v in table.MarkAttachClassDef.classDefs.items() if g in glyphs}
673 if not table.MarkAttachClassDef.classDefs:
674 table.MarkAttachClassDef = None
675 if table.GlyphClassDef:
676 table.GlyphClassDef.classDefs = {g:v for g,v in table.GlyphClassDef.classDefs.items() if g in glyphs}
677 if not table.GlyphClassDef.classDefs:
678 table.GlyphClassDef = None
679 if table.AttachList:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400680 indices = table.AttachList.Coverage.subset_glyphs (glyphs)
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400681 table.AttachList.AttachPoint = [table.AttachList.AttachPoint[i] for i in indices]
682 table.AttachList.GlyphCount = len (table.AttachList.AttachPoint)
683 if not table.AttachList.GlyphCount:
684 table.AttachList = None
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400685 return bool (table.LigCaretList or table.MarkAttachClassDef or table.GlyphClassDef or table.AttachList)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400686
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400687@add_method(fontTools.ttLib.getTableClass('kern'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400688def subset_glyphs (self, glyphs):
Behdad Esfahbod5270ec42013-07-22 12:57:02 -0400689 for t in self.kernTables:
690 t.kernTable = {(a,b):v for ((a,b),v) in t.kernTable.items() if a in glyphs and b in glyphs}
691 self.kernTables = [t for t in self.kernTables if t.kernTable]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400692 return bool (self.kernTables)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400693
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400694@add_method(fontTools.ttLib.getTableClass('hmtx'), fontTools.ttLib.getTableClass('vmtx'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400695def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400696 self.metrics = {g:v for g,v in self.metrics.items() if g in glyphs}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400697 return bool (self.metrics)
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400698
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400699@add_method(fontTools.ttLib.getTableClass('hdmx'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400700def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400701 self.hdmx = {s:{g:v for g,v in l.items() if g in glyphs} for (s,l) in self.hdmx.items()}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400702 return bool (self.hdmx)
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400703
Behdad Esfahbode45d6af2013-07-22 15:29:17 -0400704@add_method(fontTools.ttLib.getTableClass('VORG'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400705def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400706 self.VOriginRecords = {g:v for g,v in self.VOriginRecords.items() if g in glyphs}
Behdad Esfahbode45d6af2013-07-22 15:29:17 -0400707 self.numVertOriginYMetrics = len (self.VOriginRecords)
708 return True # Never drop; has default metrics
709
Behdad Esfahbod8c646f62013-07-22 15:06:23 -0400710@add_method(fontTools.ttLib.getTableClass('post'))
Behdad Esfahbod42648242013-07-23 12:56:06 -0400711def prune_pre_subset (self, glyphs):
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400712 if not options['glyph-names']:
Behdad Esfahbod42648242013-07-23 12:56:06 -0400713 self.formatType = 3.0
714 return True
715
716@add_method(fontTools.ttLib.getTableClass('post'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400717def subset_glyphs (self, glyphs):
Behdad Esfahbod42648242013-07-23 12:56:06 -0400718 self.extraNames = [] # This seems to do it
Behdad Esfahbodc9dec9d2013-07-23 10:28:47 -0400719 return True
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400720
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400721@add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400722def closure_glyphs (self, glyphs):
723 glyphs = unique_sorted (glyphs)
724 decompose = glyphs
725 # I don't know if component glyphs can be composite themselves.
726 # We handle them anyway.
727 while True:
728 components = []
729 for g in decompose:
730 gl = self[g]
731 if gl.isComposite ():
732 for c in gl.components:
733 if c.glyphName not in glyphs:
734 components.append (c.glyphName)
735 components = [c for c in components if c not in glyphs]
736 if not components:
737 return glyphs
738 decompose = unique_sorted (components)
739 glyphs.extend (components)
740
741@add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400742def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400743 self.glyphs = {g:v for g,v in self.glyphs.items() if g in glyphs}
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400744 self.glyphOrder = [g for g in self.glyphOrder if g in glyphs]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400745 return bool (self.glyphs)
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400746
Behdad Esfahboded98c612013-07-23 12:37:41 -0400747@add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400748def prune_post_subset (self, options):
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400749 if not options['hinting']:
Behdad Esfahboded98c612013-07-23 12:37:41 -0400750 for g in self.glyphs.values ():
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400751 g.expand (self)
752 g.program = fontTools.ttLib.tables.ttProgram.Program()
753 g.program.fromBytecode([])
Behdad Esfahboded98c612013-07-23 12:37:41 -0400754 return True
755
Behdad Esfahbod2b677c82013-07-23 13:37:13 -0400756@add_method(fontTools.ttLib.getTableClass('CFF '))
757def subset_glyphs (self, glyphs):
758 assert 0, "unimplemented"
759
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400760@add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400761def prune_pre_subset (self, options):
Behdad Esfahbodde4a15b2013-07-23 13:05:42 -0400762 if not options['legacy-cmap']:
763 # Drop non-Unicode / non-Symbol cmaps
764 self.tables = [t for t in self.tables if t.platformID == 3 and t.platEncID in [0, 1, 10]]
765 if not options['symbol-cmap']:
766 self.tables = [t for t in self.tables if t.platformID == 3 and t.platEncID in [1, 10]]
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400767 # TODO Only keep one subtable?
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400768 # For now, drop format=0 which can't be subset_glyphs easily?
769 self.tables = [t for t in self.tables if t.format != 0]
770 return bool (self.tables)
771
772@add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400773def subset_glyphs (self, glyphs):
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400774 for t in self.tables:
Behdad Esfahbod9453a362013-07-22 16:21:24 -0400775 # For reasons I don't understand I need this here
776 # to force decompilation of the cmap format 14.
777 try:
778 getattr (t, "asdf")
779 except AttributeError:
780 pass
Behdad Esfahbodb13d7902013-07-22 16:01:15 -0400781 if t.format == 14:
Behdad Esfahbod9453a362013-07-22 16:21:24 -0400782 # XXX We drop all the default-UVS mappings (g==None)
Behdad Esfahbodb13d7902013-07-22 16:01:15 -0400783 t.uvsDict = {v:[(u,g) for (u,g) in l if g in glyphs] for (v,l) in t.uvsDict.items()}
784 t.uvsDict = {v:l for (v,l) in t.uvsDict.items() if l}
785 else:
786 t.cmap = {u:g for (u,g) in t.cmap.items() if g in glyphs}
787 self.tables = [t for t in self.tables if (t.cmap if t.format != 14 else t.uvsDict)]
Behdad Esfahbod7e4bfc32013-07-22 18:47:32 -0400788 # XXX Convert formats when needed
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400789 return bool (self.tables)
790
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400791@add_method(fontTools.ttLib.getTableClass('name'))
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400792def prune_pre_subset (self, options):
Behdad Esfahbod20faeb02013-07-23 13:19:03 -0400793 if '*' not in options['name-IDs']:
794 self.names = [n for n in self.names if n.nameID in options['name-IDs']]
795 if not options['name-legacy']:
796 self.names = [n for n in self.names if n.platformID == 3 and n.platEncID == 1]
797 if '*' not in options['name-languages']:
798 self.names = [n for n in self.names if n.langID in options['name-languages']]
799 return True # Retain even if empty
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400800
Behdad Esfahbod8c646f62013-07-22 15:06:23 -0400801
Behdad Esfahbodbc25f162013-07-23 12:56:54 -0400802drop_tables_default = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', 'PCLT', 'LTSH']
Behdad Esfahbod103a12f2013-07-23 15:08:39 -0400803drop_tables_default += ['Feat', 'Glat', 'Gloc', 'Silf', 'Sill'] # Graphite
Behdad Esfahboded98c612013-07-23 12:37:41 -0400804no_subset_tables = ['gasp', 'head', 'hhea', 'maxp', 'vhea', 'OS/2', 'loca', 'name', 'cvt ', 'fpgm', 'prep']
Behdad Esfahbodbc25f162013-07-23 12:56:54 -0400805hinting_tables = ['cvt ', 'fpgm', 'prep', 'hdmx', 'VDMX']
Behdad Esfahbod29df0462013-07-23 11:05:25 -0400806
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400807# Based on HarfBuzz shapers
808layout_features_dict = {
809 # Default shaper
810 'common': ['ccmp', 'liga', 'locl', 'mark', 'mkmk', 'rlig'],
811 'horizontal': ['calt', 'clig', 'curs', 'kern', 'rclt'],
812 'vertical': ['valt', 'vert', 'vkrn', 'vpal', 'vrt2'],
813 'ltr': ['ltra', 'ltrm'],
814 'rtl': ['rtla', 'rtlm'],
815 # Complex shapers
816 'arabic': ['init', 'medi', 'fina', 'isol', 'med2', 'fin2', 'fin3'],
817 'hangul': ['ljmo', 'vjmo', 'tjmo'],
818 'tibetal': ['abvs', 'blws', 'abvm', 'blwm'],
819 'indic': ['nukt', 'akhn', 'rphf', 'rkrf', 'pref', 'blwf', 'half', 'abvf', 'pstf', 'cfar', 'vatu', 'cjct',
820 'init', 'pres', 'abvs', 'blws', 'psts', 'haln', 'dist', 'abvm', 'blwm'],
821}
822layout_features_all = unique_sorted (sum (layout_features_dict.values (), []))
823
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400824options_default = {
Behdad Esfahbodde4a15b2013-07-23 13:05:42 -0400825 'drop-tables': drop_tables_default,
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400826 'layout-features': layout_features_all,
827 'hinting': False,
828 'glyph-names': False,
Behdad Esfahbodde4a15b2013-07-23 13:05:42 -0400829 'legacy-cmap': False,
830 'symbol-cmap': False,
Behdad Esfahbod20faeb02013-07-23 13:19:03 -0400831 'name-IDs': [1, 2], # Family and Style
832 'name-legacy': False,
833 'name-languages': [0x0409], # English
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400834 'notdef': True,
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400835}
836
Behdad Esfahbod29df0462013-07-23 11:05:25 -0400837
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400838# TODO OS/2 ulUnicodeRange / ulCodePageRange?
Behdad Esfahbodf71267b2013-07-23 12:59:13 -0400839# TODO Drop unneeded GSUB/GPOS Script/LangSys entries
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400840# TODO Finish GSUB glyph closure
Behdad Esfahbod398d3892013-07-23 15:29:40 -0400841# TODO Avoid recursing too much
Behdad Esfahbode94aa0e2013-07-23 13:22:04 -0400842# TODO Text direction considerations
843# TODO Text script / language considerations
Behdad Esfahbod56ebd042013-07-22 13:02:24 -0400844
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400845if __name__ == '__main__':
846
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400847 import sys, time
848
849 start_time = time.time ()
850 last_time = start_time
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400851
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400852 verbose = False
853 if "--verbose" in sys.argv:
854 verbose = True
855 sys.argv.remove ("--verbose")
Behdad Esfahbod350a5272013-07-22 12:02:16 -0400856 xml = False
857 if "--xml" in sys.argv:
858 xml = True
859 sys.argv.remove ("--xml")
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400860 timing = False
861 if "--timing" in sys.argv:
862 timing = True
863 sys.argv.remove ("--timing")
864
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400865 options = options_default.copy ()
866
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400867 def lapse (what):
868 if not timing:
869 return
870 global last_time
871 new_time = time.time ()
872 print "Took %0.3fs to %s" % (new_time - last_time, what)
873 last_time = new_time
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400874
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400875 if len (sys.argv) < 3:
876 print >>sys.stderr, "usage: pyotlss.py font-file glyph..."
877 sys.exit (1)
878
879 fontfile = sys.argv[1]
880 glyphs = sys.argv[2:]
881
882 font = fontTools.ttx.TTFont (fontfile)
Behdad Esfahbod0f86ce92013-07-22 17:30:31 -0400883 font.disassembleInstructions = False
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400884 lapse ("load font")
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400885
886 names = font.getGlyphNames()
887 # Convert to glyph names
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400888 glyph_names = []
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400889 cmap_tables = None
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400890 for g in glyphs:
891 if g in names:
892 glyph_names.append (g)
893 continue
894 if g[:3] == 'uni':
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400895 if not cmap_tables:
896 cmap = font['cmap']
897 cmap_tables = [t for t in cmap.tables if t.platformID == 3 and t.platEncID in [1, 10]]
898 del cmap
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400899 found = False
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400900 u = int (g[3:], 16)
901 for table in cmap_tables:
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400902 if u in table.cmap:
903 glyph_names.append (table.cmap[u])
904 found = True
905 break
906 if not found:
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400907 if verbose:
908 print ("No glyph for Unicode value %s; skipping." % g)
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400909 continue
910 if g[:3] == 'gid':
911 g = g[3:]
912 elif g[:5] == 'glyph':
913 g = g[5:]
914 try:
915 glyph_names.append (font.getGlyphName (int (g)))
916 except ValueError:
917 raise Exception ("Invalid glyph identifier %s" % g)
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400918 del cmap_tables
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400919 glyphs = glyph_names
920 del glyph_names
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400921 lapse ("compile glyph list")
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400922
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400923 if options["notdef"]:
924 # Always include .notdef; anything else?
925 glyphs.append ('.notdef')
926 if verbose:
927 print "Added .notdef glyph"
928
929 glyphs_requested = glyphs
930 if 'GSUB' in font:
931 # XXX Do this after pruning!
932 if verbose:
933 print "Closing glyph list over 'GSUB'. %d glyphs before" % len (glyphs)
934 glyphs = font['GSUB'].closure_glyphs (glyphs)
935 if verbose:
936 print "Closed glyph list over 'GSUB'. %d glyphs after" % len (glyphs)
937 lapse ("close glyph list over 'GSUB'")
938 glyphs_gsubed = glyphs
939
Behdad Esfahbod2a784ac2013-07-22 17:00:36 -0400940 # Close over composite glyphs
941 if 'glyf' in font:
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -0400942 if verbose:
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400943 print "Closing glyph list over 'glyf'. %d glyphs before" % len (glyphs)
944 glyphs = font['glyf'].closure_glyphs (glyphs)
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -0400945 if verbose:
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400946 print "Closed glyph list over 'glyf'. %d glyphs after" % len (glyphs)
947 lapse ("close glyph list over 'glyf'")
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -0400948 else:
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400949 glyphs = glyphs
950 glyphs_glyfed = glyphs
951 glyphs_closed = glyphs
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -0400952 del glyphs
953
Behdad Esfahbodc9dec9d2013-07-23 10:28:47 -0400954 if verbose:
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -0400955 print "Retaining %d glyphs: " % len (glyphs_closed)
Behdad Esfahbod2a784ac2013-07-22 17:00:36 -0400956
Behdad Esfahbod350a5272013-07-22 12:02:16 -0400957 if xml:
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400958 import xmlWriter
959 writer = xmlWriter.XMLWriter (sys.stdout)
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400960
Behdad Esfahbod8842ce22013-07-22 13:01:33 -0400961 for tag in font.keys():
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400962
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400963 if tag == 'GlyphOrder':
964 continue
965
Behdad Esfahboded98c612013-07-23 12:37:41 -0400966 if tag in options['drop-tables'] or \
967 (not options['hinting'] and tag in hinting_tables):
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400968 if verbose:
969 print tag, "dropped."
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400970 del font[tag]
971 continue
972
Behdad Esfahbod8842ce22013-07-22 13:01:33 -0400973 clazz = fontTools.ttLib.getTableClass(tag)
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400974
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400975 if hasattr (clazz, 'prune_pre_subset'):
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400976 table = font[tag]
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400977 retain = table.prune_pre_subset (options)
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400978 lapse ("prune '%s'" % tag)
979 if not retain:
Behdad Esfahbod8b411f32013-07-23 11:24:20 -0400980 if verbose:
981 print tag, "pruned to empty; dropped."
982 del font[tag]
983 continue
984 else:
985 if verbose:
986 print tag, "pruned."
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400987
988 if tag in no_subset_tables:
989 if verbose:
990 print tag, "subsetting not needed."
Behdad Esfahbod96f47042013-07-23 12:21:34 -0400991 elif hasattr (clazz, 'subset_glyphs'):
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400992 table = font[tag]
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400993 if tag == 'cmap': # What else?
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -0400994 glyphs = glyphs_requested
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400995 elif tag in ['GSUB', 'GPOS', 'GDEF', 'cmap', 'kern', 'post']: # What else?
996 glyphs = glyphs_gsubed
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -0400997 else:
998 glyphs = glyphs_closed
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400999 retain = table.subset_glyphs (glyphs)
1000 lapse ("subset '%s'" % tag)
1001 if not retain:
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001002 if verbose:
1003 print tag, "subsetted to empty; dropped."
Behdad Esfahbod8b411f32013-07-23 11:24:20 -04001004 del font[tag]
1005 continue
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001006 else:
1007 if verbose:
1008 print tag, "subsetted."
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001009 del glyphs
Behdad Esfahbod4ae81712013-07-22 11:57:13 -04001010 else:
Behdad Esfahbod350a5272013-07-22 12:02:16 -04001011 if verbose:
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001012 print tag, "NOT subset; don't know how to subset."
1013 continue
1014
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -04001015 if hasattr (clazz, 'prune_post_subset'):
1016 table = font[tag]
1017 retain = table.prune_post_subset (options)
1018 lapse ("prune '%s'" % tag)
1019 if not retain:
1020 if verbose:
1021 print tag, "pruned to empty; dropped."
1022 del font[tag]
1023 continue
1024 else:
1025 if verbose:
1026 print tag, "pruned."
1027
Behdad Esfahbodc7160442013-07-22 14:29:08 -04001028 glyphOrder = font.getGlyphOrder()
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001029 glyphOrder = [g for g in glyphOrder if g in glyphs_closed]
Behdad Esfahbodc7160442013-07-22 14:29:08 -04001030 font.setGlyphOrder (glyphOrder)
1031 font._buildReverseGlyphOrderDict ()
Behdad Esfahbodb70b4982013-07-23 11:38:26 -04001032 lapse ("subset GlyphOrder")
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -04001033
Behdad Esfahbod0f86ce92013-07-22 17:30:31 -04001034 if xml:
1035 for tag in font.keys():
1036 writer.begintag (tag)
1037 writer.newline ()
1038 font[tag].toXML(writer, font)
1039 writer.endtag (tag)
1040 writer.newline ()
1041
Behdad Esfahbod77cda412013-07-22 11:46:50 -04001042 font.save (fontfile + '.subset')
Behdad Esfahbodb70b4982013-07-23 11:38:26 -04001043 lapse ("compile and save font")
1044
1045 last_time = start_time
1046 lapse ("make one with everything (TOTAL TIME)")