blob: 5d15d5cf85486e72a252e2f0bab3094a3142e1e0 [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 Esfahbod44c2b3c2013-07-23 16:00:32 -0400282
283@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst,
284 fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos)
285def __classify_context (self):
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400286 class ContextContext:
287 def __init__ (self, lookup):
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400288 if lookup.__class__.__name__.endswith ('Subst'):
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400289 Typ = 'Sub'
290 Type = 'Subst'
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400291 else:
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400292 Typ = 'Pos'
293 Type = 'Pos'
294 if lookup.__class__.__name__.startswith ('Chain'):
295 Chain = 'Chain'
296 else:
297 Chain = ''
298 ChainTyp = Chain+Typ
299
300 self.Typ = Typ
301 self.Type = Type
302 self.Chain = Chain
303 self.ChainTyp = ChainTyp
304
305 self.LookupRecord = Type+'LookupRecord'
306
Behdad Esfahbod27108392013-07-23 16:40:47 -0400307 # Format 1
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400308 self.Rule = ChainTyp+'Rule'
309 self.RuleCount = ChainTyp+'RuleCount'
310 self.RuleSet = ChainTyp+'RuleSet'
311 self.RuleSetCount = ChainTyp+'RuleSetCount'
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400312 def ContextSequence (r, Format):
313 if Format == 1:
314 return r.Input
315 elif Format == 2:
Behdad Esfahbodcbba4a62013-07-23 17:27:18 -0400316 return [r.ClassDef]
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400317 elif Format == 3:
318 return r.Coverage
319 else:
320 assert 0, "unknown format: %s" % Format
321 def ChainContextSequence (r, Format):
322 if Format == 1:
323 return r.Backtrack + r.Input + r.LookAhead
324 elif Format == 2:
Behdad Esfahbodcbba4a62013-07-23 17:27:18 -0400325 return [r.LookAheadClassDef, r.BacktrackClassDef, r.InputClassDef]
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400326 elif Format == 3:
327 return r.InputCoverage + r.LookAheadCoverage + r.BacktrackCoverage
328 else:
329 assert 0, "unknown format: %s" % Format
330 if Chain:
331 self.ContextSequence = ChainContextSequence
332 else:
333 self.ContextSequence = ContextSequence
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400334
Behdad Esfahbod27108392013-07-23 16:40:47 -0400335 # Format 2
336 self.ClassRule = ChainTyp+'ClassRule'
337 self.ClassRuleCount = ChainTyp+'ClassRuleCount'
338 self.ClassRuleSet = ChainTyp+'ClassSet'
339 self.ClassRuleSetCount = ChainTyp+'ClassSetCount'
340
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400341 if not hasattr (self.__class__, "__ContextContext"):
342 self.__class__.__ContextContext = ContextContext (self)
343 return self.__class__.__ContextContext
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400344
Behdad Esfahbodf2b6d9c2013-07-23 17:31:54 -0400345@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst)
Behdad Esfahbod00776972013-07-23 15:33:00 -0400346def closure_glyphs (self, glyphs, table):
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400347 c = self.__classify_context ()
348
Behdad Esfahbod00776972013-07-23 15:33:00 -0400349 if self.Format == 1:
350 return []
351 assert 0 # XXX
352 elif self.Format == 2:
353 assert 0 # XXX
354 elif self.Format == 3:
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400355 if not all (x.intersect_glyphs (glyphs) for x in c.ContextSequence (self, self.Format)):
Behdad Esfahbod00776972013-07-23 15:33:00 -0400356 return []
357 return sum ((table.table.LookupList.Lookup[ll.LookupListIndex].closure_glyphs (glyphs, table) \
Behdad Esfahbodf2b6d9c2013-07-23 17:31:54 -0400358 for ll in getattr (self, c.LookupRecord)), [])
Behdad Esfahbod00776972013-07-23 15:33:00 -0400359 else:
360 assert 0, "unknown format: %s" % self.Format
361
Behdad Esfahbodcbba4a62013-07-23 17:27:18 -0400362@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ContextPos,
363 fontTools.ttLib.tables.otTables.ChainContextSubst, fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400364def subset_glyphs (self, glyphs):
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400365 c = self.__classify_context ()
366
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400367 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400368 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400369 rss = getattr (self, c.RuleSet)
370 rss = [rss[i] for i in indices]
371 for rs in rss:
372 ss = getattr (rs, c.Rule)
373 ss = [r for r in ss \
Behdad Esfahbodcbba4a62013-07-23 17:27:18 -0400374 if all (g in glyphs for g in c.ContextSequence (r, self.Format))]
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400375 setattr (rs, c.Rule, ss)
376 setattr (rs, c.RuleCount, len (ss))
Behdad Esfahbodcbba4a62013-07-23 17:27:18 -0400377 # Prune empty subrulesets
378 rss = [rs for rs in rss if getattr (rs, c.Rule)]
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400379 setattr (self, c.RuleSet, rss)
380 setattr (self, c.RuleSetCount, len (rss))
381 return bool (rss)
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400382 elif self.Format == 2:
Behdad Esfahbod828c70c2013-07-23 17:33:52 -0400383 # TODO Renumber classes then prune rules
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400384 return self.Coverage.subset_glyphs (glyphs) and \
Behdad Esfahbodcbba4a62013-07-23 17:27:18 -0400385 all (x.subset_glyphs (glyphs) for x in c.ContextSequence (self, self.Format))
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400386 elif self.Format == 3:
Behdad Esfahbodcbba4a62013-07-23 17:27:18 -0400387 return all (x.subset_glyphs (glyphs) for x in c.ContextSequence (self, self.Format))
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400388 else:
389 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400390
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400391@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst,
392 fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400393def subset_lookups (self, lookup_indices):
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400394 c = self.__classify_context ()
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400395
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400396 if self.Format == 1:
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400397 for rs in getattr (self, c.RuleSet):
398 for r in getattr (rs, c.Rule):
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400399 setattr (r, c.LookupRecord, [ll for ll in getattr (r, c.LookupRecord) \
400 if ll.LookupListIndex in lookup_indices])
401 for ll in getattr (r, c.LookupRecord):
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400402 ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
403 elif self.Format == 2:
Behdad Esfahbod27108392013-07-23 16:40:47 -0400404 for rs in getattr (self, c.ClassRuleSet):
405 for r in getattr (rs, c.ClassRule):
406 setattr (r, c.LookupRecord, [ll for ll in getattr (r, c.LookupRecord) \
407 if ll.LookupListIndex in lookup_indices])
408 for ll in getattr (r, c.LookupRecord):
409 ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400410 elif self.Format == 3:
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400411 setattr (self, c.LookupRecord, [ll for ll in getattr (self, c.LookupRecord) \
412 if ll.LookupListIndex in lookup_indices])
413 for ll in getattr (self, c.LookupRecord):
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400414 ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
415 else:
416 assert 0, "unknown format: %s" % self.Format
417
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400418@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst,
419 fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400420def collect_lookups (self):
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400421 c = self.__classify_context ()
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400422
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400423 if self.Format == 1:
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400424 return [ll.LookupListIndex \
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400425 for rs in getattr (self, c.RuleSet) \
426 for r in getattr (rs, c.Rule) \
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400427 for ll in getattr (r, c.LookupRecord)]
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400428 elif self.Format == 2:
Behdad Esfahbod27108392013-07-23 16:40:47 -0400429 return [ll.LookupListIndex \
430 for rs in getattr (self, c.ClassRuleSet) \
431 for r in getattr (rs, c.ClassRule) \
432 for ll in getattr (r, c.LookupRecord)]
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400433 elif self.Format == 3:
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400434 return [ll.LookupListIndex \
435 for ll in getattr (self, c.LookupRecord)]
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400436 else:
437 assert 0, "unknown format: %s" % self.Format
438
Behdad Esfahbodf2ecc9c2013-07-23 12:40:35 -0400439
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400440@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst)
441def closure_glyphs (self, glyphs, table):
442 if self.Format == 1:
443 return self.ExtSubTable.closure_glyphs (glyphs, table)
444 else:
445 assert 0, "unknown format: %s" % self.Format
446
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400447@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400448def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400449 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400450 return self.ExtSubTable.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400451 else:
452 assert 0, "unknown format: %s" % self.Format
453
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400454@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
455def subset_lookups (self, lookup_indices):
456 if self.Format == 1:
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400457 return self.ExtSubTable.subset_lookups (lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400458 else:
459 assert 0, "unknown format: %s" % self.Format
460
461@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
462def collect_lookups (self):
463 if self.Format == 1:
464 return self.ExtSubTable.collect_lookups ()
465 else:
466 assert 0, "unknown format: %s" % self.Format
467
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400468@add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400469def closure_glyphs (self, glyphs, table):
470 return sum ((s.closure_glyphs (glyphs, table) for s in self.SubTable), [])
471
472@add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400473def subset_glyphs (self, glyphs):
474 self.SubTable = [s for s in self.SubTable if s.subset_glyphs (glyphs)]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400475 self.SubTableCount = len (self.SubTable)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400476 return bool (self.SubTableCount)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400477
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400478@add_method(fontTools.ttLib.tables.otTables.Lookup)
479def subset_lookups (self, lookup_indices):
480 for s in self.SubTable:
481 s.subset_lookups (lookup_indices)
482
483@add_method(fontTools.ttLib.tables.otTables.Lookup)
484def collect_lookups (self):
485 return unique_sorted (sum ((s.collect_lookups () for s in self.SubTable), []))
486
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400487@add_method(fontTools.ttLib.tables.otTables.LookupList)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400488def subset_glyphs (self, glyphs):
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400489 "Returns the indices of nonempty lookups."
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400490 return [i for (i,l) in enumerate (self.Lookup) if l.subset_glyphs (glyphs)]
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400491
492@add_method(fontTools.ttLib.tables.otTables.LookupList)
493def subset_lookups (self, lookup_indices):
494 self.Lookup = [self.Lookup[i] for i in lookup_indices]
495 self.LookupCount = len (self.Lookup)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400496 for l in self.Lookup:
497 l.subset_lookups (lookup_indices)
498
499@add_method(fontTools.ttLib.tables.otTables.LookupList)
500def closure_lookups (self, lookup_indices):
Behdad Esfahbodbb7e2132013-07-23 13:48:35 -0400501 lookup_indices = unique_sorted (lookup_indices)
502 recurse = lookup_indices
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400503 while True:
Behdad Esfahbodbb7e2132013-07-23 13:48:35 -0400504 recurse_lookups = sum ((self.Lookup[i].collect_lookups () for i in recurse), [])
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400505 recurse_lookups = [l for l in recurse_lookups if l not in lookup_indices]
506 if not recurse_lookups:
Behdad Esfahbodbb7e2132013-07-23 13:48:35 -0400507 return unique_sorted (lookup_indices)
508 recurse_lookups = unique_sorted (recurse_lookups)
509 lookup_indices.extend (recurse_lookups)
510 recurse = recurse_lookups
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400511
512@add_method(fontTools.ttLib.tables.otTables.Feature)
513def subset_lookups (self, lookup_indices):
514 self.LookupListIndex = [l for l in self.LookupListIndex if l in lookup_indices]
515 # Now map them.
516 self.LookupListIndex = [lookup_indices.index (l) for l in self.LookupListIndex]
517 self.LookupCount = len (self.LookupListIndex)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400518 return self.LookupCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400519
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400520@add_method(fontTools.ttLib.tables.otTables.Feature)
521def collect_lookups (self):
522 return self.LookupListIndex[:]
523
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400524@add_method(fontTools.ttLib.tables.otTables.FeatureList)
525def subset_lookups (self, lookup_indices):
526 "Returns the indices of nonempty features."
527 feature_indices = [i for (i,f) in enumerate (self.FeatureRecord) if f.Feature.subset_lookups (lookup_indices)]
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400528 self.subset_features (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400529 return feature_indices
530
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400531@add_method(fontTools.ttLib.tables.otTables.FeatureList)
532def collect_lookups (self, feature_indices):
533 return unique_sorted (sum ((self.FeatureRecord[i].Feature.collect_lookups () for i in feature_indices
534 if i < self.FeatureCount), []))
535
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400536@add_method(fontTools.ttLib.tables.otTables.FeatureList)
537def subset_features (self, feature_indices):
538 self.FeatureRecord = [self.FeatureRecord[i] for i in feature_indices]
539 self.FeatureCount = len (self.FeatureRecord)
540 return bool (self.FeatureCount)
541
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400542@add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys)
543def subset_features (self, feature_indices):
Behdad Esfahbod69ce1502013-07-22 18:00:31 -0400544 if self.ReqFeatureIndex in feature_indices:
545 self.ReqFeatureIndex = feature_indices.index (self.ReqFeatureIndex)
546 else:
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400547 self.ReqFeatureIndex = 65535
548 self.FeatureIndex = [f for f in self.FeatureIndex if f in feature_indices]
Behdad Esfahbod69ce1502013-07-22 18:00:31 -0400549 # Now map them.
550 self.FeatureIndex = [feature_indices.index (f) for f in self.FeatureIndex if f in feature_indices]
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400551 self.FeatureCount = len (self.FeatureIndex)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400552 return bool (self.FeatureCount or self.ReqFeatureIndex != 65535)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400553
554@add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys)
555def collect_features (self):
556 feature_indices = self.FeatureIndex[:]
557 if self.ReqFeatureIndex != 65535:
558 feature_indices.append (self.ReqFeatureIndex)
559 return unique_sorted (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400560
561@add_method(fontTools.ttLib.tables.otTables.Script)
562def subset_features (self, feature_indices):
563 if self.DefaultLangSys and not self.DefaultLangSys.subset_features (feature_indices):
564 self.DefaultLangSys = None
565 self.LangSysRecord = [l for l in self.LangSysRecord if l.LangSys.subset_features (feature_indices)]
566 self.LangSysCount = len (self.LangSysRecord)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400567 return bool (self.LangSysCount or self.DefaultLangSys)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400568
569@add_method(fontTools.ttLib.tables.otTables.Script)
570def collect_features (self):
Behdad Esfahbod2307c8b2013-07-23 11:18:13 -0400571 feature_indices = [l.LangSys.collect_features () for l in self.LangSysRecord]
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400572 if self.DefaultLangSys:
573 feature_indices.append (self.DefaultLangSys.collect_features ())
574 return unique_sorted (sum (feature_indices, []))
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400575
576@add_method(fontTools.ttLib.tables.otTables.ScriptList)
577def subset_features (self, feature_indices):
578 self.ScriptRecord = [s for s in self.ScriptRecord if s.Script.subset_features (feature_indices)]
579 self.ScriptCount = len (self.ScriptRecord)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400580 return bool (self.ScriptCount)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400581
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400582@add_method(fontTools.ttLib.tables.otTables.ScriptList)
583def collect_features (self):
584 return unique_sorted (sum ((s.Script.collect_features () for s in self.ScriptRecord), []))
585
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400586@add_method(fontTools.ttLib.getTableClass('GSUB'))
587def closure_glyphs (self, glyphs):
588 feature_indices = self.table.ScriptList.collect_features ()
589 lookup_indices = self.table.FeatureList.collect_lookups (feature_indices)
590 glyphs = unique_sorted (glyphs)
591 while True:
592 additions = (sum ((self.table.LookupList.Lookup[i].closure_glyphs (glyphs, self) for i in lookup_indices), []))
593 additions = unique_sorted (g for g in additions if g not in glyphs)
594 if not additions:
595 return glyphs
596 glyphs.extend (additions)
597
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400598@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400599def subset_glyphs (self, glyphs):
600 lookup_indices = self.table.LookupList.subset_glyphs (glyphs)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400601 self.subset_lookups (lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400602 self.prune_lookups ()
603 return True
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400604
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400605@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400606def subset_lookups (self, lookup_indices):
607 "Retrains specified lookups, then removes empty features, language systems, and scripts."
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400608 self.table.LookupList.subset_lookups (lookup_indices)
609 feature_indices = self.table.FeatureList.subset_lookups (lookup_indices)
610 self.table.ScriptList.subset_features (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400611
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400612@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
613def prune_lookups (self):
614 "Remove unreferenced lookups"
615 feature_indices = self.table.ScriptList.collect_features ()
616 lookup_indices = self.table.FeatureList.collect_lookups (feature_indices)
617 lookup_indices = self.table.LookupList.closure_lookups (lookup_indices)
618 self.subset_lookups (lookup_indices)
619
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400620@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
621def subset_feature_tags (self, feature_tags):
622 feature_indices = [i for (i,f) in enumerate (self.table.FeatureList.FeatureRecord) if f.FeatureTag in feature_tags]
623 self.table.FeatureList.subset_features (feature_indices)
624 self.table.ScriptList.subset_features (feature_indices)
625
626@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400627def prune_pre_subset (self, options):
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400628 if options['layout-features'] and '*' not in options['layout-features']:
Behdad Esfahboded98c612013-07-23 12:37:41 -0400629 self.subset_feature_tags (options['layout-features'])
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400630 self.prune_lookups ()
631 return True
632
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400633@add_method(fontTools.ttLib.getTableClass('GDEF'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400634def subset_glyphs (self, glyphs):
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400635 table = self.table
636 if table.LigCaretList:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400637 indices = table.LigCaretList.Coverage.subset_glyphs (glyphs)
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400638 table.LigCaretList.LigGlyph = [table.LigCaretList.LigGlyph[i] for i in indices]
639 table.LigCaretList.LigGlyphCount = len (table.LigCaretList.LigGlyph)
640 if not table.LigCaretList.LigGlyphCount:
641 table.LigCaretList = None
642 if table.MarkAttachClassDef:
643 table.MarkAttachClassDef.classDefs = {g:v for g,v in table.MarkAttachClassDef.classDefs.items() if g in glyphs}
644 if not table.MarkAttachClassDef.classDefs:
645 table.MarkAttachClassDef = None
646 if table.GlyphClassDef:
647 table.GlyphClassDef.classDefs = {g:v for g,v in table.GlyphClassDef.classDefs.items() if g in glyphs}
648 if not table.GlyphClassDef.classDefs:
649 table.GlyphClassDef = None
650 if table.AttachList:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400651 indices = table.AttachList.Coverage.subset_glyphs (glyphs)
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400652 table.AttachList.AttachPoint = [table.AttachList.AttachPoint[i] for i in indices]
653 table.AttachList.GlyphCount = len (table.AttachList.AttachPoint)
654 if not table.AttachList.GlyphCount:
655 table.AttachList = None
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400656 return bool (table.LigCaretList or table.MarkAttachClassDef or table.GlyphClassDef or table.AttachList)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400657
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400658@add_method(fontTools.ttLib.getTableClass('kern'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400659def subset_glyphs (self, glyphs):
Behdad Esfahbod5270ec42013-07-22 12:57:02 -0400660 for t in self.kernTables:
661 t.kernTable = {(a,b):v for ((a,b),v) in t.kernTable.items() if a in glyphs and b in glyphs}
662 self.kernTables = [t for t in self.kernTables if t.kernTable]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400663 return bool (self.kernTables)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400664
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400665@add_method(fontTools.ttLib.getTableClass('hmtx'), fontTools.ttLib.getTableClass('vmtx'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400666def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400667 self.metrics = {g:v for g,v in self.metrics.items() if g in glyphs}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400668 return bool (self.metrics)
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400669
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400670@add_method(fontTools.ttLib.getTableClass('hdmx'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400671def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400672 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 -0400673 return bool (self.hdmx)
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400674
Behdad Esfahbode45d6af2013-07-22 15:29:17 -0400675@add_method(fontTools.ttLib.getTableClass('VORG'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400676def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400677 self.VOriginRecords = {g:v for g,v in self.VOriginRecords.items() if g in glyphs}
Behdad Esfahbode45d6af2013-07-22 15:29:17 -0400678 self.numVertOriginYMetrics = len (self.VOriginRecords)
679 return True # Never drop; has default metrics
680
Behdad Esfahbod8c646f62013-07-22 15:06:23 -0400681@add_method(fontTools.ttLib.getTableClass('post'))
Behdad Esfahbod42648242013-07-23 12:56:06 -0400682def prune_pre_subset (self, glyphs):
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400683 if not options['glyph-names']:
Behdad Esfahbod42648242013-07-23 12:56:06 -0400684 self.formatType = 3.0
685 return True
686
687@add_method(fontTools.ttLib.getTableClass('post'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400688def subset_glyphs (self, glyphs):
Behdad Esfahbod42648242013-07-23 12:56:06 -0400689 self.extraNames = [] # This seems to do it
Behdad Esfahbodc9dec9d2013-07-23 10:28:47 -0400690 return True
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400691
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400692@add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400693def closure_glyphs (self, glyphs):
694 glyphs = unique_sorted (glyphs)
695 decompose = glyphs
696 # I don't know if component glyphs can be composite themselves.
697 # We handle them anyway.
698 while True:
699 components = []
700 for g in decompose:
701 gl = self[g]
702 if gl.isComposite ():
703 for c in gl.components:
704 if c.glyphName not in glyphs:
705 components.append (c.glyphName)
706 components = [c for c in components if c not in glyphs]
707 if not components:
708 return glyphs
709 decompose = unique_sorted (components)
710 glyphs.extend (components)
711
712@add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400713def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400714 self.glyphs = {g:v for g,v in self.glyphs.items() if g in glyphs}
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400715 self.glyphOrder = [g for g in self.glyphOrder if g in glyphs]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400716 return bool (self.glyphs)
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400717
Behdad Esfahboded98c612013-07-23 12:37:41 -0400718@add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400719def prune_post_subset (self, options):
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400720 if not options['hinting']:
Behdad Esfahboded98c612013-07-23 12:37:41 -0400721 for g in self.glyphs.values ():
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400722 g.expand (self)
723 g.program = fontTools.ttLib.tables.ttProgram.Program()
724 g.program.fromBytecode([])
Behdad Esfahboded98c612013-07-23 12:37:41 -0400725 return True
726
Behdad Esfahbod2b677c82013-07-23 13:37:13 -0400727@add_method(fontTools.ttLib.getTableClass('CFF '))
728def subset_glyphs (self, glyphs):
729 assert 0, "unimplemented"
730
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400731@add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400732def prune_pre_subset (self, options):
Behdad Esfahbodde4a15b2013-07-23 13:05:42 -0400733 if not options['legacy-cmap']:
734 # Drop non-Unicode / non-Symbol cmaps
735 self.tables = [t for t in self.tables if t.platformID == 3 and t.platEncID in [0, 1, 10]]
736 if not options['symbol-cmap']:
737 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 -0400738 # TODO Only keep one subtable?
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400739 # For now, drop format=0 which can't be subset_glyphs easily?
740 self.tables = [t for t in self.tables if t.format != 0]
741 return bool (self.tables)
742
743@add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400744def subset_glyphs (self, glyphs):
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400745 for t in self.tables:
Behdad Esfahbod9453a362013-07-22 16:21:24 -0400746 # For reasons I don't understand I need this here
747 # to force decompilation of the cmap format 14.
748 try:
749 getattr (t, "asdf")
750 except AttributeError:
751 pass
Behdad Esfahbodb13d7902013-07-22 16:01:15 -0400752 if t.format == 14:
Behdad Esfahbod9453a362013-07-22 16:21:24 -0400753 # XXX We drop all the default-UVS mappings (g==None)
Behdad Esfahbodb13d7902013-07-22 16:01:15 -0400754 t.uvsDict = {v:[(u,g) for (u,g) in l if g in glyphs] for (v,l) in t.uvsDict.items()}
755 t.uvsDict = {v:l for (v,l) in t.uvsDict.items() if l}
756 else:
757 t.cmap = {u:g for (u,g) in t.cmap.items() if g in glyphs}
758 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 -0400759 # XXX Convert formats when needed
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400760 return bool (self.tables)
761
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400762@add_method(fontTools.ttLib.getTableClass('name'))
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400763def prune_pre_subset (self, options):
Behdad Esfahbod20faeb02013-07-23 13:19:03 -0400764 if '*' not in options['name-IDs']:
765 self.names = [n for n in self.names if n.nameID in options['name-IDs']]
766 if not options['name-legacy']:
767 self.names = [n for n in self.names if n.platformID == 3 and n.platEncID == 1]
768 if '*' not in options['name-languages']:
769 self.names = [n for n in self.names if n.langID in options['name-languages']]
770 return True # Retain even if empty
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400771
Behdad Esfahbod8c646f62013-07-22 15:06:23 -0400772
Behdad Esfahbodbc25f162013-07-23 12:56:54 -0400773drop_tables_default = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', 'PCLT', 'LTSH']
Behdad Esfahbod103a12f2013-07-23 15:08:39 -0400774drop_tables_default += ['Feat', 'Glat', 'Gloc', 'Silf', 'Sill'] # Graphite
Behdad Esfahbod38e852c2013-07-23 16:56:50 -0400775drop_tables_default += ['CBLC', 'CBDT', 'sbix', 'COLR', 'CPAL'] # Color
Behdad Esfahboded98c612013-07-23 12:37:41 -0400776no_subset_tables = ['gasp', 'head', 'hhea', 'maxp', 'vhea', 'OS/2', 'loca', 'name', 'cvt ', 'fpgm', 'prep']
Behdad Esfahbodbc25f162013-07-23 12:56:54 -0400777hinting_tables = ['cvt ', 'fpgm', 'prep', 'hdmx', 'VDMX']
Behdad Esfahbod29df0462013-07-23 11:05:25 -0400778
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400779# Based on HarfBuzz shapers
780layout_features_dict = {
781 # Default shaper
782 'common': ['ccmp', 'liga', 'locl', 'mark', 'mkmk', 'rlig'],
783 'horizontal': ['calt', 'clig', 'curs', 'kern', 'rclt'],
784 'vertical': ['valt', 'vert', 'vkrn', 'vpal', 'vrt2'],
785 'ltr': ['ltra', 'ltrm'],
786 'rtl': ['rtla', 'rtlm'],
787 # Complex shapers
788 'arabic': ['init', 'medi', 'fina', 'isol', 'med2', 'fin2', 'fin3'],
789 'hangul': ['ljmo', 'vjmo', 'tjmo'],
790 'tibetal': ['abvs', 'blws', 'abvm', 'blwm'],
791 'indic': ['nukt', 'akhn', 'rphf', 'rkrf', 'pref', 'blwf', 'half', 'abvf', 'pstf', 'cfar', 'vatu', 'cjct',
792 'init', 'pres', 'abvs', 'blws', 'psts', 'haln', 'dist', 'abvm', 'blwm'],
793}
794layout_features_all = unique_sorted (sum (layout_features_dict.values (), []))
795
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400796options_default = {
Behdad Esfahbodde4a15b2013-07-23 13:05:42 -0400797 'drop-tables': drop_tables_default,
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400798 'layout-features': layout_features_all,
799 'hinting': False,
800 'glyph-names': False,
Behdad Esfahbodde4a15b2013-07-23 13:05:42 -0400801 'legacy-cmap': False,
802 'symbol-cmap': False,
Behdad Esfahbod20faeb02013-07-23 13:19:03 -0400803 'name-IDs': [1, 2], # Family and Style
804 'name-legacy': False,
805 'name-languages': [0x0409], # English
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400806 'notdef': True,
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400807}
808
Behdad Esfahbod29df0462013-07-23 11:05:25 -0400809
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400810# TODO OS/2 ulUnicodeRange / ulCodePageRange?
Behdad Esfahbodf71267b2013-07-23 12:59:13 -0400811# TODO Drop unneeded GSUB/GPOS Script/LangSys entries
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400812# TODO Finish GSUB glyph closure
Behdad Esfahbod398d3892013-07-23 15:29:40 -0400813# TODO Avoid recursing too much
Behdad Esfahbode94aa0e2013-07-23 13:22:04 -0400814# TODO Text direction considerations
815# TODO Text script / language considerations
Behdad Esfahbod38e852c2013-07-23 16:56:50 -0400816# TODO Drop unknown tables
Behdad Esfahbode339ad82013-07-23 17:29:13 -0400817# TODO Add other three required TrueType glyphs (1,2,3)
Behdad Esfahbod56ebd042013-07-22 13:02:24 -0400818
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400819if __name__ == '__main__':
820
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400821 import sys, time
822
823 start_time = time.time ()
824 last_time = start_time
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400825
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400826 verbose = False
827 if "--verbose" in sys.argv:
828 verbose = True
829 sys.argv.remove ("--verbose")
Behdad Esfahbod350a5272013-07-22 12:02:16 -0400830 xml = False
831 if "--xml" in sys.argv:
832 xml = True
833 sys.argv.remove ("--xml")
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400834 timing = False
835 if "--timing" in sys.argv:
836 timing = True
837 sys.argv.remove ("--timing")
838
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400839 options = options_default.copy ()
840
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400841 def lapse (what):
842 if not timing:
843 return
844 global last_time
845 new_time = time.time ()
846 print "Took %0.3fs to %s" % (new_time - last_time, what)
847 last_time = new_time
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400848
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400849 if len (sys.argv) < 3:
850 print >>sys.stderr, "usage: pyotlss.py font-file glyph..."
851 sys.exit (1)
852
853 fontfile = sys.argv[1]
854 glyphs = sys.argv[2:]
855
856 font = fontTools.ttx.TTFont (fontfile)
Behdad Esfahbod0f86ce92013-07-22 17:30:31 -0400857 font.disassembleInstructions = False
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400858 lapse ("load font")
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400859
860 names = font.getGlyphNames()
861 # Convert to glyph names
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400862 glyph_names = []
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400863 cmap_tables = None
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400864 for g in glyphs:
865 if g in names:
866 glyph_names.append (g)
867 continue
868 if g[:3] == 'uni':
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400869 if not cmap_tables:
870 cmap = font['cmap']
871 cmap_tables = [t for t in cmap.tables if t.platformID == 3 and t.platEncID in [1, 10]]
872 del cmap
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400873 found = False
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400874 u = int (g[3:], 16)
875 for table in cmap_tables:
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400876 if u in table.cmap:
877 glyph_names.append (table.cmap[u])
878 found = True
879 break
880 if not found:
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400881 if verbose:
882 print ("No glyph for Unicode value %s; skipping." % g)
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400883 continue
884 if g[:3] == 'gid':
885 g = g[3:]
886 elif g[:5] == 'glyph':
887 g = g[5:]
888 try:
889 glyph_names.append (font.getGlyphName (int (g)))
890 except ValueError:
891 raise Exception ("Invalid glyph identifier %s" % g)
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400892 del cmap_tables
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400893 glyphs = glyph_names
894 del glyph_names
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400895 lapse ("compile glyph list")
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400896
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400897 if options["notdef"]:
898 # Always include .notdef; anything else?
899 glyphs.append ('.notdef')
900 if verbose:
901 print "Added .notdef glyph"
902
903 glyphs_requested = glyphs
904 if 'GSUB' in font:
905 # XXX Do this after pruning!
906 if verbose:
907 print "Closing glyph list over 'GSUB'. %d glyphs before" % len (glyphs)
908 glyphs = font['GSUB'].closure_glyphs (glyphs)
909 if verbose:
910 print "Closed glyph list over 'GSUB'. %d glyphs after" % len (glyphs)
911 lapse ("close glyph list over 'GSUB'")
912 glyphs_gsubed = glyphs
913
Behdad Esfahbod2a784ac2013-07-22 17:00:36 -0400914 # Close over composite glyphs
915 if 'glyf' in font:
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -0400916 if verbose:
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400917 print "Closing glyph list over 'glyf'. %d glyphs before" % len (glyphs)
918 glyphs = font['glyf'].closure_glyphs (glyphs)
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -0400919 if verbose:
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400920 print "Closed glyph list over 'glyf'. %d glyphs after" % len (glyphs)
921 lapse ("close glyph list over 'glyf'")
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -0400922 else:
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400923 glyphs = glyphs
924 glyphs_glyfed = glyphs
925 glyphs_closed = glyphs
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -0400926 del glyphs
927
Behdad Esfahbodc9dec9d2013-07-23 10:28:47 -0400928 if verbose:
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -0400929 print "Retaining %d glyphs: " % len (glyphs_closed)
Behdad Esfahbod2a784ac2013-07-22 17:00:36 -0400930
Behdad Esfahbod350a5272013-07-22 12:02:16 -0400931 if xml:
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400932 import xmlWriter
933 writer = xmlWriter.XMLWriter (sys.stdout)
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400934
Behdad Esfahbod8842ce22013-07-22 13:01:33 -0400935 for tag in font.keys():
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400936
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400937 if tag == 'GlyphOrder':
938 continue
939
Behdad Esfahboded98c612013-07-23 12:37:41 -0400940 if tag in options['drop-tables'] or \
941 (not options['hinting'] and tag in hinting_tables):
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400942 if verbose:
943 print tag, "dropped."
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400944 del font[tag]
945 continue
946
Behdad Esfahbod8842ce22013-07-22 13:01:33 -0400947 clazz = fontTools.ttLib.getTableClass(tag)
Behdad Esfahbod4e214e42013-07-22 13:13:49 -0400948
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400949 if hasattr (clazz, 'prune_pre_subset'):
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400950 table = font[tag]
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400951 retain = table.prune_pre_subset (options)
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400952 lapse ("prune '%s'" % tag)
953 if not retain:
Behdad Esfahbod8b411f32013-07-23 11:24:20 -0400954 if verbose:
955 print tag, "pruned to empty; dropped."
956 del font[tag]
957 continue
958 else:
959 if verbose:
960 print tag, "pruned."
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400961
962 if tag in no_subset_tables:
963 if verbose:
964 print tag, "subsetting not needed."
Behdad Esfahbod96f47042013-07-23 12:21:34 -0400965 elif hasattr (clazz, 'subset_glyphs'):
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400966 table = font[tag]
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400967 if tag == 'cmap': # What else?
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -0400968 glyphs = glyphs_requested
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400969 elif tag in ['GSUB', 'GPOS', 'GDEF', 'cmap', 'kern', 'post']: # What else?
970 glyphs = glyphs_gsubed
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -0400971 else:
972 glyphs = glyphs_closed
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400973 retain = table.subset_glyphs (glyphs)
974 lapse ("subset '%s'" % tag)
975 if not retain:
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400976 if verbose:
977 print tag, "subsetted to empty; dropped."
Behdad Esfahbod8b411f32013-07-23 11:24:20 -0400978 del font[tag]
979 continue
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400980 else:
981 if verbose:
982 print tag, "subsetted."
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -0400983 del glyphs
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400984 else:
Behdad Esfahbod350a5272013-07-22 12:02:16 -0400985 if verbose:
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400986 print tag, "NOT subset; don't know how to subset."
987 continue
988
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400989 if hasattr (clazz, 'prune_post_subset'):
990 table = font[tag]
991 retain = table.prune_post_subset (options)
992 lapse ("prune '%s'" % tag)
993 if not retain:
994 if verbose:
995 print tag, "pruned to empty; dropped."
996 del font[tag]
997 continue
998 else:
999 if verbose:
1000 print tag, "pruned."
1001
Behdad Esfahbodc7160442013-07-22 14:29:08 -04001002 glyphOrder = font.getGlyphOrder()
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001003 glyphOrder = [g for g in glyphOrder if g in glyphs_closed]
Behdad Esfahbodc7160442013-07-22 14:29:08 -04001004 font.setGlyphOrder (glyphOrder)
1005 font._buildReverseGlyphOrderDict ()
Behdad Esfahbodb70b4982013-07-23 11:38:26 -04001006 lapse ("subset GlyphOrder")
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -04001007
Behdad Esfahbod0f86ce92013-07-22 17:30:31 -04001008 if xml:
1009 for tag in font.keys():
1010 writer.begintag (tag)
1011 writer.newline ()
1012 font[tag].toXML(writer, font)
1013 writer.endtag (tag)
1014 writer.newline ()
1015
Behdad Esfahbod77cda412013-07-22 11:46:50 -04001016 font.save (fontfile + '.subset')
Behdad Esfahbodb70b4982013-07-23 11:38:26 -04001017 lapse ("compile and save font")
1018
1019 last_time = start_time
1020 lapse ("make one with everything (TOTAL TIME)")