blob: 317e439237e2c8892cc42cdd07f4a0a41e39f9ca [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 Esfahbodb8d55882013-07-23 22:17:39 -040048def intersect_glyphs (self, glyphs):
49 "Returns ascending list of matching class values."
50 return unique_sorted (v for g,v in self.classDefs.items() if g in glyphs)
51
52@add_method(fontTools.ttLib.tables.otTables.ClassDef)
53def intersects_glyphs_class (self, glyphs, klass):
54 "Returns true if any of glyphs has requested class."
55 return any (g in glyphs for g,v in self.classDefs.items() if v == klass)
56
57@add_method(fontTools.ttLib.tables.otTables.ClassDef)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -040058def subset_glyphs (self, glyphs):
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -040059 "Returns ascending list of remaining classes."
Behdad Esfahbod54660612013-07-21 18:16:55 -040060 self.classDefs = {g:v for g,v in self.classDefs.items() if g in glyphs}
Behdad Esfahbod78661bb2013-07-23 10:23:42 -040061 return unique_sorted (self.classDefs.values ())
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -040062
63@add_method(fontTools.ttLib.tables.otTables.ClassDef)
64def remap (self, class_map):
65 "Remaps classes."
66 self.classDefs = {g:class_map.index (v) for g,v in self.classDefs.items()}
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040067
Behdad Esfahbod54660612013-07-21 18:16:55 -040068@add_method(fontTools.ttLib.tables.otTables.SingleSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -040069def closure_glyphs (self, glyphs, table):
70 if self.Format in [1, 2]:
71 return [v for g,v in self.mapping.items() if g in glyphs]
72 else:
73 assert 0, "unknown format: %s" % self.Format
74
75@add_method(fontTools.ttLib.tables.otTables.SingleSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -040076def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -040077 if self.Format in [1, 2]:
78 self.mapping = {g:v for g,v in self.mapping.items() if g in glyphs}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -040079 return bool (self.mapping)
Behdad Esfahbod54660612013-07-21 18:16:55 -040080 else:
81 assert 0, "unknown format: %s" % self.Format
82
83@add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -040084def closure_glyphs (self, glyphs, table):
85 if self.Format == 1:
86 indices = self.Coverage.intersect_glyphs (glyphs)
87 return sum ((self.Sequence[i].Substitute for i in indices), [])
88 else:
89 assert 0, "unknown format: %s" % self.Format
90
91@add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -040092def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -040093 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -040094 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -040095 self.Sequence = [self.Sequence[i] for i in indices]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040096 self.SequenceCount = len (self.Sequence)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -040097 return bool (self.SequenceCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -040098 else:
99 assert 0, "unknown format: %s" % self.Format
100
101@add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400102def closure_glyphs (self, glyphs, table):
103 if self.Format == 1:
104 return sum ((v for g,v in self.alternates.items() if g in glyphs), [])
105 else:
106 assert 0, "unknown format: %s" % self.Format
107
108@add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400109def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400110 if self.Format == 1:
111 self.alternates = {g:v for g,v in self.alternates.items() if g in glyphs}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400112 return bool (self.alternates)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400113 else:
114 assert 0, "unknown format: %s" % self.Format
115
116@add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400117def closure_glyphs (self, glyphs, table):
118 if self.Format == 1:
119 return sum (([seq.LigGlyph for seq in seqs if all(c in glyphs for c in seq.Component)]
120 for g,seqs in self.ligatures.items()), [])
121 else:
122 assert 0, "unknown format: %s" % self.Format
123
124@add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400125def subset_glyphs (self, glyphs):
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400126 if self.Format == 1:
127 self.ligatures = {g:v for g,v in self.ligatures.items() if g in glyphs}
128 self.ligatures = {g:[seq for seq in seqs if all(c in glyphs for c in seq.Component)]
129 for g,seqs in self.ligatures.items()}
130 self.ligatures = {g:v for g,v in self.ligatures.items() if v}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400131 return bool (self.ligatures)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400132 else:
133 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400134
Behdad Esfahbod54660612013-07-21 18:16:55 -0400135@add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400136def closure_glyphs (self, glyphs, table):
137 if self.Format == 1:
138 indices = self.Coverage.intersect_glyphs (glyphs)
139 if not indices or \
140 not all (c.intersect_glyphs (glyphs) for c in self.LookAheadCoverage + self.BacktrackCoverage):
141 return []
142 return [self.Substitute[i] for i in indices]
143 else:
144 assert 0, "unknown format: %s" % self.Format
145
146@add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
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 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400150 self.Substitute = [self.Substitute[i] for i in indices]
151 self.GlyphCount = len (self.Substitute)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400152 return bool (self.GlyphCount and all (c.subset_glyphs (glyphs) for c in self.LookAheadCoverage + self.BacktrackCoverage))
Behdad Esfahbod54660612013-07-21 18:16:55 -0400153 else:
154 assert 0, "unknown format: %s" % self.Format
155
156@add_method(fontTools.ttLib.tables.otTables.SinglePos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400157def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400158 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400159 return len (self.Coverage.subset_glyphs (glyphs))
Behdad Esfahbod54660612013-07-21 18:16:55 -0400160 elif self.Format == 2:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400161 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400162 self.Value = [self.Value[i] for i in indices]
163 self.ValueCount = len (self.Value)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400164 return bool (self.ValueCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400165 else:
166 assert 0, "unknown format: %s" % self.Format
167
168@add_method(fontTools.ttLib.tables.otTables.PairPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400169def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400170 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400171 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400172 self.PairSet = [self.PairSet[i] for i in indices]
173 for p in self.PairSet:
174 p.PairValueRecord = [r for r in p.PairValueRecord if r.SecondGlyph in glyphs]
175 p.PairValueCount = len (p.PairValueRecord)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400176 self.PairSet = [p for p in self.PairSet if p.PairValueCount]
Behdad Esfahbod54660612013-07-21 18:16:55 -0400177 self.PairSetCount = len (self.PairSet)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400178 return bool (self.PairSetCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400179 elif self.Format == 2:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400180 class1_map = self.ClassDef1.subset_glyphs (glyphs)
181 class2_map = self.ClassDef2.subset_glyphs (glyphs)
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -0400182 self.ClassDef1.remap (class1_map)
183 self.ClassDef2.remap (class2_map)
184 self.Class1Record = [self.Class1Record[i] for i in class1_map]
185 for c in self.Class1Record:
186 c.Class2Record = [c.Class2Record[i] for i in class2_map]
187 self.Class1Count = len (class1_map)
188 self.Class2Count = len (class2_map)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400189 return bool (self.Class1Count and self.Class2Count and self.Coverage.subset_glyphs (glyphs))
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.CursivePos)
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 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400197 self.EntryExitRecord = [self.EntryExitRecord[i] for i in indices]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400198 self.EntryExitCount = len (self.EntryExitRecord)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400199 return bool (self.EntryExitCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400200 else:
201 assert 0, "unknown format: %s" % self.Format
202
203@add_method(fontTools.ttLib.tables.otTables.MarkBasePos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400204def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400205 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400206 mark_indices = self.MarkCoverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400207 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices]
208 self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400209 base_indices = self.BaseCoverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400210 self.BaseArray.BaseRecord = [self.BaseArray.BaseRecord[i] for i in base_indices]
211 self.BaseArray.BaseCount = len (self.BaseArray.BaseRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400212 # Prune empty classes
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400213 class_indices = unique_sorted (v.Class for v in self.MarkArray.MarkRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400214 self.ClassCount = len (class_indices)
215 for m in self.MarkArray.MarkRecord:
216 m.Class = class_indices.index (m.Class)
217 for b in self.BaseArray.BaseRecord:
218 b.BaseAnchor = [b.BaseAnchor[i] for i in class_indices]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400219 return bool (self.ClassCount and self.MarkArray.MarkCount and self.BaseArray.BaseCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400220 else:
221 assert 0, "unknown format: %s" % self.Format
222
223@add_method(fontTools.ttLib.tables.otTables.MarkLigPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400224def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400225 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400226 mark_indices = self.MarkCoverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400227 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices]
228 self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400229 ligature_indices = self.LigatureCoverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400230 self.LigatureArray.LigatureAttach = [self.LigatureArray.LigatureAttach[i] for i in ligature_indices]
231 self.LigatureArray.LigatureCount = len (self.LigatureArray.LigatureAttach)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400232 # Prune empty classes
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400233 class_indices = unique_sorted (v.Class for v in self.MarkArray.MarkRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400234 self.ClassCount = len (class_indices)
235 for m in self.MarkArray.MarkRecord:
236 m.Class = class_indices.index (m.Class)
237 for l in self.LigatureArray.LigatureAttach:
238 for c in l.ComponentRecord:
239 c.LigatureAnchor = [c.LigatureAnchor[i] for i in class_indices]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400240 return bool (self.ClassCount and self.MarkArray.MarkCount and self.LigatureArray.LigatureCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400241 else:
242 assert 0, "unknown format: %s" % self.Format
243
244@add_method(fontTools.ttLib.tables.otTables.MarkMarkPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400245def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400246 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400247 mark1_indices = self.Mark1Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400248 self.Mark1Array.MarkRecord = [self.Mark1Array.MarkRecord[i] for i in mark1_indices]
249 self.Mark1Array.MarkCount = len (self.Mark1Array.MarkRecord)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400250 mark2_indices = self.Mark2Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400251 self.Mark2Array.Mark2Record = [self.Mark2Array.Mark2Record[i] for i in mark2_indices]
252 self.Mark2Array.MarkCount = len (self.Mark2Array.Mark2Record)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400253 # Prune empty classes
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400254 class_indices = unique_sorted (v.Class for v in self.Mark1Array.MarkRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400255 self.ClassCount = len (class_indices)
256 for m in self.Mark1Array.MarkRecord:
257 m.Class = class_indices.index (m.Class)
258 for b in self.Mark2Array.Mark2Record:
259 b.Mark2Anchor = [b.Mark2Anchor[i] for i in class_indices]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400260 return bool (self.ClassCount and self.Mark1Array.MarkCount and self.Mark2Array.MarkCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400261 else:
262 assert 0, "unknown format: %s" % self.Format
263
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400264@add_method(fontTools.ttLib.tables.otTables.SingleSubst,
265 fontTools.ttLib.tables.otTables.MultipleSubst,
266 fontTools.ttLib.tables.otTables.AlternateSubst,
267 fontTools.ttLib.tables.otTables.LigatureSubst,
268 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
269 fontTools.ttLib.tables.otTables.SinglePos,
270 fontTools.ttLib.tables.otTables.PairPos,
271 fontTools.ttLib.tables.otTables.CursivePos,
272 fontTools.ttLib.tables.otTables.MarkBasePos,
273 fontTools.ttLib.tables.otTables.MarkLigPos,
274 fontTools.ttLib.tables.otTables.MarkMarkPos)
275def subset_lookups (self, lookup_indices):
276 pass
277
278@add_method(fontTools.ttLib.tables.otTables.SingleSubst,
279 fontTools.ttLib.tables.otTables.MultipleSubst,
280 fontTools.ttLib.tables.otTables.AlternateSubst,
281 fontTools.ttLib.tables.otTables.LigatureSubst,
282 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
283 fontTools.ttLib.tables.otTables.SinglePos,
284 fontTools.ttLib.tables.otTables.PairPos,
285 fontTools.ttLib.tables.otTables.CursivePos,
286 fontTools.ttLib.tables.otTables.MarkBasePos,
287 fontTools.ttLib.tables.otTables.MarkLigPos,
288 fontTools.ttLib.tables.otTables.MarkMarkPos)
289def collect_lookups (self):
290 return []
291
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400292@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst,
293 fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos)
294def __classify_context (self):
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400295
296 class ContextHelper:
297 def __init__ (self, klass, Format):
298 if klass.__name__.endswith ('Subst'):
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400299 Typ = 'Sub'
300 Type = 'Subst'
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400301 else:
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400302 Typ = 'Pos'
303 Type = 'Pos'
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400304 if klass.__name__.startswith ('Chain'):
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400305 Chain = 'Chain'
306 else:
307 Chain = ''
308 ChainTyp = Chain+Typ
309
310 self.Typ = Typ
311 self.Type = Type
312 self.Chain = Chain
313 self.ChainTyp = ChainTyp
314
315 self.LookupRecord = Type+'LookupRecord'
316
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400317 if Format == 1:
318 ContextData = None
319 ChainContextData = None
320 RuleData = lambda r: r.Input
321 ChainRuleData = lambda r: r.Backtrack + r.Input + r.LookAhead
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400322 SetRuleData = None
323 ChainSetRuleData = None
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400324 elif Format == 2:
Behdad Esfahbode3f20732013-07-24 11:26:43 -0400325 ContextData = lambda r: (r.ClassDef,)
326 ChainContextData = lambda r: (r.LookAheadClassDef, r.InputClassDef, r.BacktrackClassDef)
327 RuleData = lambda r: (r.Class,)
328 ChainRuleData = lambda r: (r.LookAhead, r.Input, r.Backtrack)
329 def SetRuleData (r, d): (r.Class,) = d
330 def ChainSetRuleData (r, d): (r.LookAhead, r.Input, r.Backtrack) = d
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400331 elif Format == 3:
332 ContextData = None
333 ChainContextData = None
334 RuleData = lambda r: r.Coverage
335 ChainRuleData = lambda r: r.LookAheadCoverage + r.InputCoverage + r.BacktrackCoverage
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400336 SetRuleData = None
337 ChainSetRuleData = None
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400338 else:
339 assert 0, "unknown format: %s" % Format
340
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400341 if Chain:
Behdad Esfahbod707a37a2013-07-23 21:08:26 -0400342 self.ContextData = ChainContextData
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400343 self.RuleData = ChainRuleData
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400344 self.SetRuleData = ChainSetRuleData
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400345 else:
Behdad Esfahbod707a37a2013-07-23 21:08:26 -0400346 self.ContextData = ContextData
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400347 self.RuleData = RuleData
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400348 self.SetRuleData = SetRuleData
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400349
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400350 if Format == 1:
351 self.Rule = ChainTyp+'Rule'
352 self.RuleCount = ChainTyp+'RuleCount'
353 self.RuleSet = ChainTyp+'RuleSet'
354 self.RuleSetCount = ChainTyp+'RuleSetCount'
355 elif Format == 2:
356 self.Rule = ChainTyp+'ClassRule'
357 self.RuleCount = ChainTyp+'ClassRuleCount'
358 self.RuleSet = ChainTyp+'ClassSet'
359 self.RuleSetCount = ChainTyp+'ClassSetCount'
Behdad Esfahbod89987002013-07-23 23:07:42 -0400360
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400361 self.ClassDef = 'InputClassDef' if Chain else 'ClassDef'
Behdad Esfahbod27108392013-07-23 16:40:47 -0400362
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400363 if self.Format not in [1, 2, 3]:
364 return None # Don't shoot the messenger; let it go
365 if not hasattr (self.__class__, "__ContextHelpers"):
366 self.__class__.__ContextHelpers = {}
367 if self.Format not in self.__class__.__ContextHelpers:
368 self.__class__.__ContextHelpers[self.Format] = ContextHelper (self.__class__, self.Format)
369 return self.__class__.__ContextHelpers[self.Format]
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400370
Behdad Esfahbodf2b6d9c2013-07-23 17:31:54 -0400371@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst)
Behdad Esfahbod00776972013-07-23 15:33:00 -0400372def closure_glyphs (self, glyphs, table):
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400373 c = self.__classify_context ()
374
Behdad Esfahbod00776972013-07-23 15:33:00 -0400375 if self.Format == 1:
Behdad Esfahbodeeca9822013-07-23 17:42:17 -0400376 indices = self.Coverage.intersect_glyphs (glyphs)
377 rss = getattr (self, c.RuleSet)
378 return sum ((table.table.LookupList.Lookup[ll.LookupListIndex].closure_glyphs (glyphs, table) \
379 for i in indices \
380 for r in getattr (rss[i], c.Rule) \
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400381 if r and all (g in glyphs for g in c.RuleData (r)) \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400382 for ll in getattr (r, c.LookupRecord) if ll \
Behdad Esfahbodeeca9822013-07-23 17:42:17 -0400383 ), [])
Behdad Esfahbod00776972013-07-23 15:33:00 -0400384 elif self.Format == 2:
Behdad Esfahbod31084302013-07-23 22:22:38 -0400385 if not self.Coverage.intersect_glyphs (glyphs):
386 return []
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400387 indices = getattr (self, c.ClassDef).intersect_glyphs (glyphs)
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400388 rss = getattr (self, c.RuleSet)
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400389 return sum ((table.table.LookupList.Lookup[ll.LookupListIndex].closure_glyphs (glyphs, table) \
390 for i in indices \
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400391 for r in getattr (rss[i], c.Rule) \
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400392 if r and all (cd.intersects_glyphs_class (glyphs, k) \
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400393 for cd,k in zip (c.ContextData (self), c.RuleData (r))) \
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400394 for ll in getattr (r, c.LookupRecord) if ll \
395 ), [])
Behdad Esfahbod00776972013-07-23 15:33:00 -0400396 elif self.Format == 3:
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400397 if not all (x.intersect_glyphs (glyphs) for x in c.RuleData (self)):
Behdad Esfahbod00776972013-07-23 15:33:00 -0400398 return []
399 return sum ((table.table.LookupList.Lookup[ll.LookupListIndex].closure_glyphs (glyphs, table) \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400400 for ll in getattr (self, c.LookupRecord) if ll), [])
Behdad Esfahbod00776972013-07-23 15:33:00 -0400401 else:
402 assert 0, "unknown format: %s" % self.Format
403
Behdad Esfahbodcbba4a62013-07-23 17:27:18 -0400404@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ContextPos,
405 fontTools.ttLib.tables.otTables.ChainContextSubst, fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400406def subset_glyphs (self, glyphs):
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400407 c = self.__classify_context ()
408
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400409 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400410 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400411 rss = getattr (self, c.RuleSet)
412 rss = [rss[i] for i in indices]
413 for rs in rss:
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400414 if rs:
415 ss = getattr (rs, c.Rule)
416 ss = [r for r in ss \
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400417 if r and all (g in glyphs for g in c.RuleData (r))]
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400418 setattr (rs, c.Rule, ss)
419 setattr (rs, c.RuleCount, len (ss))
Behdad Esfahbodcbba4a62013-07-23 17:27:18 -0400420 # Prune empty subrulesets
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400421 rss = [rs for rs in rss if rs and getattr (rs, c.Rule)]
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400422 setattr (self, c.RuleSet, rss)
423 setattr (self, c.RuleSetCount, len (rss))
424 return bool (rss)
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400425 elif self.Format == 2:
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400426 if not self.Coverage.subset_glyphs (glyphs):
427 return False
428 indices = getattr (self, c.ClassDef).intersect_glyphs (glyphs)
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400429 rss = getattr (self, c.RuleSet)
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400430 rss = [rss[i] for i in indices]
431 ContextData = c.ContextData (self)
432 klass_maps = [x.subset_glyphs (glyphs) for x in ContextData]
433 for cd,m in zip (ContextData, klass_maps):
434 cd.remap (m)
435 for rs in rss:
436 if rs:
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400437 ss = getattr (rs, c.Rule)
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400438 ss = [r for r in ss \
439 if r and all (k in klass_map \
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400440 for klass_map,k in zip (klass_maps, c.RuleData (r)))]
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400441 setattr (rs, c.Rule, ss)
442 setattr (rs, c.RuleCount, len (ss))
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400443
444 # Remap rule classes
445 for r in ss:
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400446 c.SetRuleData (r, (klass_map.index (k) \
447 for klassmap,k in zip (klass_maps, c.RuleData (r))))
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400448 # Prune empty subrulesets
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400449 rss = [rs for rs in rss if rs and getattr (rs, c.Rule)]
450 setattr (self, c.RuleSet, rss)
451 setattr (self, c.RuleSetCount, len (rss))
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400452 return bool (rss)
453
454 return all (x.subset_glyphs (glyphs) for x in c.ContextData (self))
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400455 elif self.Format == 3:
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400456 return all (x.subset_glyphs (glyphs) for x in c.RuleData (self))
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400457 else:
458 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400459
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400460@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst,
461 fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400462def subset_lookups (self, lookup_indices):
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400463 c = self.__classify_context ()
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400464
Behdad Esfahbod1f573632013-07-23 23:04:43 -0400465 if self.Format in [1, 2]:
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400466 for rs in getattr (self, c.RuleSet):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400467 if rs:
468 for r in getattr (rs, c.Rule):
469 if r:
Behdad Esfahbod1f573632013-07-23 23:04:43 -0400470 setattr (r, c.LookupRecord, [ll for ll in getattr (r, c.LookupRecord) if ll \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400471 if ll.LookupListIndex in lookup_indices])
472 for ll in getattr (r, c.LookupRecord):
473 if ll:
474 ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400475 elif self.Format == 3:
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400476 setattr (self, c.LookupRecord, [ll for ll in getattr (self, c.LookupRecord) if ll \
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400477 if ll.LookupListIndex in lookup_indices])
478 for ll in getattr (self, c.LookupRecord):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400479 if ll:
480 ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400481 else:
482 assert 0, "unknown format: %s" % self.Format
483
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400484@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst,
485 fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400486def collect_lookups (self):
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400487 c = self.__classify_context ()
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400488
Behdad Esfahbod1f573632013-07-23 23:04:43 -0400489 if self.Format in [1, 2]:
Behdad Esfahbod27108392013-07-23 16:40:47 -0400490 return [ll.LookupListIndex \
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400491 for rs in getattr (self, c.RuleSet) if rs \
492 for r in getattr (rs, c.Rule) if r \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400493 for ll in getattr (r, c.LookupRecord) if ll]
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400494 elif self.Format == 3:
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400495 return [ll.LookupListIndex \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400496 for ll in getattr (self, c.LookupRecord) if ll]
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400497 else:
498 assert 0, "unknown format: %s" % self.Format
499
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400500@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst)
501def closure_glyphs (self, glyphs, table):
502 if self.Format == 1:
503 return self.ExtSubTable.closure_glyphs (glyphs, table)
504 else:
505 assert 0, "unknown format: %s" % self.Format
506
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400507@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400508def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400509 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400510 return self.ExtSubTable.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400511 else:
512 assert 0, "unknown format: %s" % self.Format
513
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400514@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
515def subset_lookups (self, lookup_indices):
516 if self.Format == 1:
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400517 return self.ExtSubTable.subset_lookups (lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400518 else:
519 assert 0, "unknown format: %s" % self.Format
520
521@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
522def collect_lookups (self):
523 if self.Format == 1:
524 return self.ExtSubTable.collect_lookups ()
525 else:
526 assert 0, "unknown format: %s" % self.Format
527
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400528@add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400529def closure_glyphs (self, glyphs, table):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400530 return sum ((s.closure_glyphs (glyphs, table) for s in self.SubTable if s), [])
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400531
532@add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400533def subset_glyphs (self, glyphs):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400534 self.SubTable = [s for s in self.SubTable if s and s.subset_glyphs (glyphs)]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400535 self.SubTableCount = len (self.SubTable)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400536 return bool (self.SubTableCount)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400537
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400538@add_method(fontTools.ttLib.tables.otTables.Lookup)
539def subset_lookups (self, lookup_indices):
540 for s in self.SubTable:
541 s.subset_lookups (lookup_indices)
542
543@add_method(fontTools.ttLib.tables.otTables.Lookup)
544def collect_lookups (self):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400545 return unique_sorted (sum ((s.collect_lookups () for s in self.SubTable if s), []))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400546
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400547@add_method(fontTools.ttLib.tables.otTables.LookupList)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400548def subset_glyphs (self, glyphs):
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400549 "Returns the indices of nonempty lookups."
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400550 return [i for (i,l) in enumerate (self.Lookup) if l and l.subset_glyphs (glyphs)]
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400551
552@add_method(fontTools.ttLib.tables.otTables.LookupList)
553def subset_lookups (self, lookup_indices):
554 self.Lookup = [self.Lookup[i] for i in lookup_indices]
555 self.LookupCount = len (self.Lookup)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400556 for l in self.Lookup:
557 l.subset_lookups (lookup_indices)
558
559@add_method(fontTools.ttLib.tables.otTables.LookupList)
560def closure_lookups (self, lookup_indices):
Behdad Esfahbodbb7e2132013-07-23 13:48:35 -0400561 lookup_indices = unique_sorted (lookup_indices)
562 recurse = lookup_indices
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400563 while True:
Behdad Esfahbodbb7e2132013-07-23 13:48:35 -0400564 recurse_lookups = sum ((self.Lookup[i].collect_lookups () for i in recurse), [])
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400565 recurse_lookups = [l for l in recurse_lookups if l not in lookup_indices]
566 if not recurse_lookups:
Behdad Esfahbodbb7e2132013-07-23 13:48:35 -0400567 return unique_sorted (lookup_indices)
568 recurse_lookups = unique_sorted (recurse_lookups)
569 lookup_indices.extend (recurse_lookups)
570 recurse = recurse_lookups
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400571
572@add_method(fontTools.ttLib.tables.otTables.Feature)
573def subset_lookups (self, lookup_indices):
574 self.LookupListIndex = [l for l in self.LookupListIndex if l in lookup_indices]
575 # Now map them.
576 self.LookupListIndex = [lookup_indices.index (l) for l in self.LookupListIndex]
577 self.LookupCount = len (self.LookupListIndex)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400578 return self.LookupCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400579
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400580@add_method(fontTools.ttLib.tables.otTables.Feature)
581def collect_lookups (self):
582 return self.LookupListIndex[:]
583
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400584@add_method(fontTools.ttLib.tables.otTables.FeatureList)
585def subset_lookups (self, lookup_indices):
586 "Returns the indices of nonempty features."
587 feature_indices = [i for (i,f) in enumerate (self.FeatureRecord) if f.Feature.subset_lookups (lookup_indices)]
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400588 self.subset_features (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400589 return feature_indices
590
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400591@add_method(fontTools.ttLib.tables.otTables.FeatureList)
592def collect_lookups (self, feature_indices):
593 return unique_sorted (sum ((self.FeatureRecord[i].Feature.collect_lookups () for i in feature_indices
594 if i < self.FeatureCount), []))
595
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400596@add_method(fontTools.ttLib.tables.otTables.FeatureList)
597def subset_features (self, feature_indices):
598 self.FeatureRecord = [self.FeatureRecord[i] for i in feature_indices]
599 self.FeatureCount = len (self.FeatureRecord)
600 return bool (self.FeatureCount)
601
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400602@add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys)
603def subset_features (self, feature_indices):
Behdad Esfahbod69ce1502013-07-22 18:00:31 -0400604 if self.ReqFeatureIndex in feature_indices:
605 self.ReqFeatureIndex = feature_indices.index (self.ReqFeatureIndex)
606 else:
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400607 self.ReqFeatureIndex = 65535
608 self.FeatureIndex = [f for f in self.FeatureIndex if f in feature_indices]
Behdad Esfahbod69ce1502013-07-22 18:00:31 -0400609 # Now map them.
610 self.FeatureIndex = [feature_indices.index (f) for f in self.FeatureIndex if f in feature_indices]
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400611 self.FeatureCount = len (self.FeatureIndex)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400612 return bool (self.FeatureCount or self.ReqFeatureIndex != 65535)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400613
614@add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys)
615def collect_features (self):
616 feature_indices = self.FeatureIndex[:]
617 if self.ReqFeatureIndex != 65535:
618 feature_indices.append (self.ReqFeatureIndex)
619 return unique_sorted (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400620
621@add_method(fontTools.ttLib.tables.otTables.Script)
622def subset_features (self, feature_indices):
623 if self.DefaultLangSys and not self.DefaultLangSys.subset_features (feature_indices):
624 self.DefaultLangSys = None
625 self.LangSysRecord = [l for l in self.LangSysRecord if l.LangSys.subset_features (feature_indices)]
626 self.LangSysCount = len (self.LangSysRecord)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400627 return bool (self.LangSysCount or self.DefaultLangSys)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400628
629@add_method(fontTools.ttLib.tables.otTables.Script)
630def collect_features (self):
Behdad Esfahbod2307c8b2013-07-23 11:18:13 -0400631 feature_indices = [l.LangSys.collect_features () for l in self.LangSysRecord]
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400632 if self.DefaultLangSys:
633 feature_indices.append (self.DefaultLangSys.collect_features ())
634 return unique_sorted (sum (feature_indices, []))
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400635
636@add_method(fontTools.ttLib.tables.otTables.ScriptList)
637def subset_features (self, feature_indices):
638 self.ScriptRecord = [s for s in self.ScriptRecord if s.Script.subset_features (feature_indices)]
639 self.ScriptCount = len (self.ScriptRecord)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400640 return bool (self.ScriptCount)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400641
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400642@add_method(fontTools.ttLib.tables.otTables.ScriptList)
643def collect_features (self):
644 return unique_sorted (sum ((s.Script.collect_features () for s in self.ScriptRecord), []))
645
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400646@add_method(fontTools.ttLib.getTableClass('GSUB'))
647def closure_glyphs (self, glyphs):
648 feature_indices = self.table.ScriptList.collect_features ()
649 lookup_indices = self.table.FeatureList.collect_lookups (feature_indices)
650 glyphs = unique_sorted (glyphs)
651 while True:
652 additions = (sum ((self.table.LookupList.Lookup[i].closure_glyphs (glyphs, self) for i in lookup_indices), []))
653 additions = unique_sorted (g for g in additions if g not in glyphs)
654 if not additions:
655 return glyphs
656 glyphs.extend (additions)
657
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400658@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400659def subset_glyphs (self, glyphs):
660 lookup_indices = self.table.LookupList.subset_glyphs (glyphs)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400661 self.subset_lookups (lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400662 self.prune_lookups ()
663 return True
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400664
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400665@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400666def subset_lookups (self, lookup_indices):
667 "Retrains specified lookups, then removes empty features, language systems, and scripts."
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400668 self.table.LookupList.subset_lookups (lookup_indices)
669 feature_indices = self.table.FeatureList.subset_lookups (lookup_indices)
670 self.table.ScriptList.subset_features (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400671
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400672@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
673def prune_lookups (self):
674 "Remove unreferenced lookups"
675 feature_indices = self.table.ScriptList.collect_features ()
676 lookup_indices = self.table.FeatureList.collect_lookups (feature_indices)
677 lookup_indices = self.table.LookupList.closure_lookups (lookup_indices)
678 self.subset_lookups (lookup_indices)
679
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400680@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
681def subset_feature_tags (self, feature_tags):
682 feature_indices = [i for (i,f) in enumerate (self.table.FeatureList.FeatureRecord) if f.FeatureTag in feature_tags]
683 self.table.FeatureList.subset_features (feature_indices)
684 self.table.ScriptList.subset_features (feature_indices)
685
686@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400687def prune_pre_subset (self, options):
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400688 if options['layout-features'] and '*' not in options['layout-features']:
Behdad Esfahboded98c612013-07-23 12:37:41 -0400689 self.subset_feature_tags (options['layout-features'])
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400690 self.prune_lookups ()
691 return True
692
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400693@add_method(fontTools.ttLib.getTableClass('GDEF'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400694def subset_glyphs (self, glyphs):
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400695 table = self.table
696 if table.LigCaretList:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400697 indices = table.LigCaretList.Coverage.subset_glyphs (glyphs)
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400698 table.LigCaretList.LigGlyph = [table.LigCaretList.LigGlyph[i] for i in indices]
699 table.LigCaretList.LigGlyphCount = len (table.LigCaretList.LigGlyph)
700 if not table.LigCaretList.LigGlyphCount:
701 table.LigCaretList = None
702 if table.MarkAttachClassDef:
703 table.MarkAttachClassDef.classDefs = {g:v for g,v in table.MarkAttachClassDef.classDefs.items() if g in glyphs}
704 if not table.MarkAttachClassDef.classDefs:
705 table.MarkAttachClassDef = None
706 if table.GlyphClassDef:
707 table.GlyphClassDef.classDefs = {g:v for g,v in table.GlyphClassDef.classDefs.items() if g in glyphs}
708 if not table.GlyphClassDef.classDefs:
709 table.GlyphClassDef = None
710 if table.AttachList:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400711 indices = table.AttachList.Coverage.subset_glyphs (glyphs)
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400712 table.AttachList.AttachPoint = [table.AttachList.AttachPoint[i] for i in indices]
713 table.AttachList.GlyphCount = len (table.AttachList.AttachPoint)
714 if not table.AttachList.GlyphCount:
715 table.AttachList = None
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400716 return bool (table.LigCaretList or table.MarkAttachClassDef or table.GlyphClassDef or table.AttachList)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400717
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400718@add_method(fontTools.ttLib.getTableClass('kern'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400719def subset_glyphs (self, glyphs):
Behdad Esfahbod5270ec42013-07-22 12:57:02 -0400720 for t in self.kernTables:
721 t.kernTable = {(a,b):v for ((a,b),v) in t.kernTable.items() if a in glyphs and b in glyphs}
722 self.kernTables = [t for t in self.kernTables if t.kernTable]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400723 return bool (self.kernTables)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400724
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400725@add_method(fontTools.ttLib.getTableClass('hmtx'), fontTools.ttLib.getTableClass('vmtx'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400726def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400727 self.metrics = {g:v for g,v in self.metrics.items() if g in glyphs}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400728 return bool (self.metrics)
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400729
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400730@add_method(fontTools.ttLib.getTableClass('hdmx'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400731def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400732 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 -0400733 return bool (self.hdmx)
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400734
Behdad Esfahbode45d6af2013-07-22 15:29:17 -0400735@add_method(fontTools.ttLib.getTableClass('VORG'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400736def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400737 self.VOriginRecords = {g:v for g,v in self.VOriginRecords.items() if g in glyphs}
Behdad Esfahbode45d6af2013-07-22 15:29:17 -0400738 self.numVertOriginYMetrics = len (self.VOriginRecords)
739 return True # Never drop; has default metrics
740
Behdad Esfahbod8c646f62013-07-22 15:06:23 -0400741@add_method(fontTools.ttLib.getTableClass('post'))
Behdad Esfahbod42648242013-07-23 12:56:06 -0400742def prune_pre_subset (self, glyphs):
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400743 if not options['glyph-names']:
Behdad Esfahbod42648242013-07-23 12:56:06 -0400744 self.formatType = 3.0
745 return True
746
747@add_method(fontTools.ttLib.getTableClass('post'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400748def subset_glyphs (self, glyphs):
Behdad Esfahbod42648242013-07-23 12:56:06 -0400749 self.extraNames = [] # This seems to do it
Behdad Esfahbodc9dec9d2013-07-23 10:28:47 -0400750 return True
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400751
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400752@add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400753def closure_glyphs (self, glyphs):
754 glyphs = unique_sorted (glyphs)
755 decompose = glyphs
756 # I don't know if component glyphs can be composite themselves.
757 # We handle them anyway.
758 while True:
759 components = []
760 for g in decompose:
Behdad Esfahbodf8c20e42013-07-23 23:13:23 -0400761 if g not in self:
762 continue
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400763 gl = self[g]
764 if gl.isComposite ():
765 for c in gl.components:
766 if c.glyphName not in glyphs:
767 components.append (c.glyphName)
768 components = [c for c in components if c not in glyphs]
769 if not components:
770 return glyphs
771 decompose = unique_sorted (components)
772 glyphs.extend (components)
773
774@add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400775def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400776 self.glyphs = {g:v for g,v in self.glyphs.items() if g in glyphs}
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400777 self.glyphOrder = [g for g in self.glyphOrder if g in glyphs]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400778 return bool (self.glyphs)
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400779
Behdad Esfahboded98c612013-07-23 12:37:41 -0400780@add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400781def prune_post_subset (self, options):
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400782 if not options['hinting']:
Behdad Esfahboded98c612013-07-23 12:37:41 -0400783 for g in self.glyphs.values ():
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400784 g.expand (self)
785 g.program = fontTools.ttLib.tables.ttProgram.Program()
786 g.program.fromBytecode([])
Behdad Esfahboded98c612013-07-23 12:37:41 -0400787 return True
788
Behdad Esfahbod2b677c82013-07-23 13:37:13 -0400789@add_method(fontTools.ttLib.getTableClass('CFF '))
790def subset_glyphs (self, glyphs):
791 assert 0, "unimplemented"
792
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400793@add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400794def prune_pre_subset (self, options):
Behdad Esfahbodde4a15b2013-07-23 13:05:42 -0400795 if not options['legacy-cmap']:
796 # Drop non-Unicode / non-Symbol cmaps
797 self.tables = [t for t in self.tables if t.platformID == 3 and t.platEncID in [0, 1, 10]]
798 if not options['symbol-cmap']:
799 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 -0400800 # TODO Only keep one subtable?
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400801 # For now, drop format=0 which can't be subset_glyphs easily?
802 self.tables = [t for t in self.tables if t.format != 0]
803 return bool (self.tables)
804
805@add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400806def subset_glyphs (self, glyphs):
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400807 for t in self.tables:
Behdad Esfahbod9453a362013-07-22 16:21:24 -0400808 # For reasons I don't understand I need this here
809 # to force decompilation of the cmap format 14.
810 try:
811 getattr (t, "asdf")
812 except AttributeError:
813 pass
Behdad Esfahbodb13d7902013-07-22 16:01:15 -0400814 if t.format == 14:
Behdad Esfahbod9453a362013-07-22 16:21:24 -0400815 # XXX We drop all the default-UVS mappings (g==None)
Behdad Esfahbodb13d7902013-07-22 16:01:15 -0400816 t.uvsDict = {v:[(u,g) for (u,g) in l if g in glyphs] for (v,l) in t.uvsDict.items()}
817 t.uvsDict = {v:l for (v,l) in t.uvsDict.items() if l}
818 else:
819 t.cmap = {u:g for (u,g) in t.cmap.items() if g in glyphs}
820 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 -0400821 # XXX Convert formats when needed
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400822 return bool (self.tables)
823
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400824@add_method(fontTools.ttLib.getTableClass('name'))
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400825def prune_pre_subset (self, options):
Behdad Esfahbod20faeb02013-07-23 13:19:03 -0400826 if '*' not in options['name-IDs']:
827 self.names = [n for n in self.names if n.nameID in options['name-IDs']]
828 if not options['name-legacy']:
829 self.names = [n for n in self.names if n.platformID == 3 and n.platEncID == 1]
830 if '*' not in options['name-languages']:
831 self.names = [n for n in self.names if n.langID in options['name-languages']]
832 return True # Retain even if empty
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400833
Behdad Esfahbod8c646f62013-07-22 15:06:23 -0400834
Behdad Esfahbodbc25f162013-07-23 12:56:54 -0400835drop_tables_default = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', 'PCLT', 'LTSH']
Behdad Esfahbod103a12f2013-07-23 15:08:39 -0400836drop_tables_default += ['Feat', 'Glat', 'Gloc', 'Silf', 'Sill'] # Graphite
Behdad Esfahbod38e852c2013-07-23 16:56:50 -0400837drop_tables_default += ['CBLC', 'CBDT', 'sbix', 'COLR', 'CPAL'] # Color
Behdad Esfahboded98c612013-07-23 12:37:41 -0400838no_subset_tables = ['gasp', 'head', 'hhea', 'maxp', 'vhea', 'OS/2', 'loca', 'name', 'cvt ', 'fpgm', 'prep']
Behdad Esfahbodbc25f162013-07-23 12:56:54 -0400839hinting_tables = ['cvt ', 'fpgm', 'prep', 'hdmx', 'VDMX']
Behdad Esfahbod29df0462013-07-23 11:05:25 -0400840
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400841# Based on HarfBuzz shapers
842layout_features_dict = {
843 # Default shaper
844 'common': ['ccmp', 'liga', 'locl', 'mark', 'mkmk', 'rlig'],
845 'horizontal': ['calt', 'clig', 'curs', 'kern', 'rclt'],
846 'vertical': ['valt', 'vert', 'vkrn', 'vpal', 'vrt2'],
847 'ltr': ['ltra', 'ltrm'],
848 'rtl': ['rtla', 'rtlm'],
849 # Complex shapers
850 'arabic': ['init', 'medi', 'fina', 'isol', 'med2', 'fin2', 'fin3'],
851 'hangul': ['ljmo', 'vjmo', 'tjmo'],
852 'tibetal': ['abvs', 'blws', 'abvm', 'blwm'],
853 'indic': ['nukt', 'akhn', 'rphf', 'rkrf', 'pref', 'blwf', 'half', 'abvf', 'pstf', 'cfar', 'vatu', 'cjct',
854 'init', 'pres', 'abvs', 'blws', 'psts', 'haln', 'dist', 'abvm', 'blwm'],
855}
856layout_features_all = unique_sorted (sum (layout_features_dict.values (), []))
857
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400858options_default = {
Behdad Esfahbodde4a15b2013-07-23 13:05:42 -0400859 'drop-tables': drop_tables_default,
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400860 'layout-features': layout_features_all,
861 'hinting': False,
862 'glyph-names': False,
Behdad Esfahbodde4a15b2013-07-23 13:05:42 -0400863 'legacy-cmap': False,
864 'symbol-cmap': False,
Behdad Esfahbod20faeb02013-07-23 13:19:03 -0400865 'name-IDs': [1, 2], # Family and Style
866 'name-legacy': False,
867 'name-languages': [0x0409], # English
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400868 'mandatory-glyphs': True, # First four for TrueType, .notdef for CFF
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400869}
870
Behdad Esfahbod29df0462013-07-23 11:05:25 -0400871
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400872# TODO OS/2 ulUnicodeRange / ulCodePageRange?
Behdad Esfahbodf71267b2013-07-23 12:59:13 -0400873# TODO Drop unneeded GSUB/GPOS Script/LangSys entries
Behdad Esfahbod398d3892013-07-23 15:29:40 -0400874# TODO Avoid recursing too much
Behdad Esfahbode94aa0e2013-07-23 13:22:04 -0400875# TODO Text direction considerations
876# TODO Text script / language considerations
Behdad Esfahbod38e852c2013-07-23 16:56:50 -0400877# TODO Drop unknown tables
Behdad Esfahbod56ebd042013-07-22 13:02:24 -0400878
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400879if __name__ == '__main__':
880
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400881 import sys, time
882
883 start_time = time.time ()
884 last_time = start_time
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400885
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400886 verbose = False
887 if "--verbose" in sys.argv:
888 verbose = True
889 sys.argv.remove ("--verbose")
Behdad Esfahbod350a5272013-07-22 12:02:16 -0400890 xml = False
891 if "--xml" in sys.argv:
892 xml = True
893 sys.argv.remove ("--xml")
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400894 timing = False
895 if "--timing" in sys.argv:
896 timing = True
897 sys.argv.remove ("--timing")
898
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400899 options = options_default.copy ()
900
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400901 def lapse (what):
902 if not timing:
903 return
904 global last_time
905 new_time = time.time ()
906 print "Took %0.3fs to %s" % (new_time - last_time, what)
907 last_time = new_time
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400908
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400909 if len (sys.argv) < 3:
910 print >>sys.stderr, "usage: pyotlss.py font-file glyph..."
911 sys.exit (1)
912
913 fontfile = sys.argv[1]
914 glyphs = sys.argv[2:]
915
916 font = fontTools.ttx.TTFont (fontfile)
Behdad Esfahbod0f86ce92013-07-22 17:30:31 -0400917 font.disassembleInstructions = False
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400918 lapse ("load font")
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400919
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400920 if options["mandatory-glyphs"]:
921 # Always include .notdef; anything else?
922 if 'glyf' in font:
923 glyphs.extend (['gid0', 'gid1', 'gid2', 'gid3'])
924 if verbose:
925 print "Added first four glyphs to subset"
926 else:
927 glyphs.append ('.notdef')
928 if verbose:
929 print "Added .notdef glyph to subset"
930
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400931 names = font.getGlyphNames()
Behdad Esfahbod9bd59c42013-07-23 21:19:49 -0400932 lapse ("loading glyph names")
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400933 # Convert to glyph names
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400934 glyph_names = []
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400935 cmap_tables = None
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400936 for g in glyphs:
937 if g in names:
938 glyph_names.append (g)
939 continue
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400940 if g.startswith ('uni') and len (g) > 3:
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400941 if not cmap_tables:
942 cmap = font['cmap']
943 cmap_tables = [t for t in cmap.tables if t.platformID == 3 and t.platEncID in [1, 10]]
944 del cmap
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400945 found = False
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400946 u = int (g[3:], 16)
947 for table in cmap_tables:
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400948 if u in table.cmap:
949 glyph_names.append (table.cmap[u])
950 found = True
951 break
952 if not found:
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400953 if verbose:
954 print ("No glyph for Unicode value %s; skipping." % g)
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400955 continue
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400956 if g.startswith ('gid') or g.startswith ('glyph'):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400957 if g.startswith ('gid') and len (g) > 3:
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400958 g = g[3:]
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400959 elif g.startswith ('glyph') and len (g) > 5:
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400960 g = g[5:]
961 try:
962 glyph_names.append (font.getGlyphName (int (g), requireReal=1))
963 except ValueError:
964 raise Exception ("Invalid glyph identifier %s" % g)
965 continue
966 raise Exception ("Invalid glyph identifier %s" % g)
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400967 del cmap_tables
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400968 glyphs = unique_sorted (glyph_names)
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400969 del glyph_names
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400970 lapse ("compile glyph list")
Behdad Esfahbod240d7e72013-07-23 23:12:06 -0400971 if verbose:
972 print "Glyphs:", glyphs
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400973
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400974 glyphs_requested = glyphs
975 if 'GSUB' in font:
976 # XXX Do this after pruning!
977 if verbose:
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400978 print "Closing glyph list over 'GSUB': %d glyphs before" % len (glyphs)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400979 glyphs = font['GSUB'].closure_glyphs (glyphs)
980 if verbose:
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400981 print "Closed glyph list over 'GSUB': %d glyphs after" % len (glyphs)
Behdad Esfahbod240d7e72013-07-23 23:12:06 -0400982 print "Glyphs:", glyphs
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400983 lapse ("close glyph list over 'GSUB'")
984 glyphs_gsubed = glyphs
985
Behdad Esfahbod2a784ac2013-07-22 17:00:36 -0400986 # Close over composite glyphs
987 if 'glyf' in font:
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -0400988 if verbose:
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400989 print "Closing glyph list over 'glyf': %d glyphs before" % len (glyphs)
Behdad Esfahbod240d7e72013-07-23 23:12:06 -0400990 print "Glyphs:", glyphs
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400991 glyphs = font['glyf'].closure_glyphs (glyphs)
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -0400992 if verbose:
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400993 print "Closed glyph list over 'glyf': %d glyphs after" % len (glyphs)
Behdad Esfahbod240d7e72013-07-23 23:12:06 -0400994 print "Glyphs:", glyphs
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400995 lapse ("close glyph list over 'glyf'")
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -0400996 else:
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400997 glyphs = glyphs
998 glyphs_glyfed = glyphs
999 glyphs_closed = glyphs
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001000 del glyphs
1001
Behdad Esfahbodc9dec9d2013-07-23 10:28:47 -04001002 if verbose:
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001003 print "Retaining %d glyphs: " % len (glyphs_closed)
Behdad Esfahbod2a784ac2013-07-22 17:00:36 -04001004
Behdad Esfahbod350a5272013-07-22 12:02:16 -04001005 if xml:
Behdad Esfahbod4ae81712013-07-22 11:57:13 -04001006 import xmlWriter
1007 writer = xmlWriter.XMLWriter (sys.stdout)
Behdad Esfahbod4e214e42013-07-22 13:13:49 -04001008
Behdad Esfahbod8842ce22013-07-22 13:01:33 -04001009 for tag in font.keys():
Behdad Esfahbod4e214e42013-07-22 13:13:49 -04001010
Behdad Esfahbodc7160442013-07-22 14:29:08 -04001011 if tag == 'GlyphOrder':
1012 continue
1013
Behdad Esfahboded98c612013-07-23 12:37:41 -04001014 if tag in options['drop-tables'] or \
1015 (not options['hinting'] and tag in hinting_tables):
Behdad Esfahbodc7160442013-07-22 14:29:08 -04001016 if verbose:
1017 print tag, "dropped."
Behdad Esfahbod4e214e42013-07-22 13:13:49 -04001018 del font[tag]
1019 continue
1020
Behdad Esfahbod8842ce22013-07-22 13:01:33 -04001021 clazz = fontTools.ttLib.getTableClass(tag)
Behdad Esfahbod4e214e42013-07-22 13:13:49 -04001022
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -04001023 if hasattr (clazz, 'prune_pre_subset'):
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001024 table = font[tag]
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -04001025 retain = table.prune_pre_subset (options)
Behdad Esfahbodb70b4982013-07-23 11:38:26 -04001026 lapse ("prune '%s'" % tag)
1027 if not retain:
Behdad Esfahbod8b411f32013-07-23 11:24:20 -04001028 if verbose:
1029 print tag, "pruned to empty; dropped."
1030 del font[tag]
1031 continue
1032 else:
1033 if verbose:
1034 print tag, "pruned."
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001035
1036 if tag in no_subset_tables:
1037 if verbose:
1038 print tag, "subsetting not needed."
Behdad Esfahbod96f47042013-07-23 12:21:34 -04001039 elif hasattr (clazz, 'subset_glyphs'):
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001040 table = font[tag]
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001041 if tag == 'cmap': # What else?
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001042 glyphs = glyphs_requested
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001043 elif tag in ['GSUB', 'GPOS', 'GDEF', 'cmap', 'kern', 'post']: # What else?
1044 glyphs = glyphs_gsubed
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001045 else:
1046 glyphs = glyphs_closed
Behdad Esfahbodb70b4982013-07-23 11:38:26 -04001047 retain = table.subset_glyphs (glyphs)
1048 lapse ("subset '%s'" % tag)
1049 if not retain:
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001050 if verbose:
1051 print tag, "subsetted to empty; dropped."
Behdad Esfahbod8b411f32013-07-23 11:24:20 -04001052 del font[tag]
1053 continue
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001054 else:
1055 if verbose:
1056 print tag, "subsetted."
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001057 del glyphs
Behdad Esfahbod4ae81712013-07-22 11:57:13 -04001058 else:
Behdad Esfahbod350a5272013-07-22 12:02:16 -04001059 if verbose:
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001060 print tag, "NOT subset; don't know how to subset."
1061 continue
1062
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -04001063 if hasattr (clazz, 'prune_post_subset'):
1064 table = font[tag]
1065 retain = table.prune_post_subset (options)
1066 lapse ("prune '%s'" % tag)
1067 if not retain:
1068 if verbose:
1069 print tag, "pruned to empty; dropped."
1070 del font[tag]
1071 continue
1072 else:
1073 if verbose:
1074 print tag, "pruned."
1075
Behdad Esfahbodc7160442013-07-22 14:29:08 -04001076 glyphOrder = font.getGlyphOrder()
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001077 glyphOrder = [g for g in glyphOrder if g in glyphs_closed]
Behdad Esfahbodc7160442013-07-22 14:29:08 -04001078 font.setGlyphOrder (glyphOrder)
1079 font._buildReverseGlyphOrderDict ()
Behdad Esfahbodb70b4982013-07-23 11:38:26 -04001080 lapse ("subset GlyphOrder")
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -04001081
Behdad Esfahbod0f86ce92013-07-22 17:30:31 -04001082 if xml:
1083 for tag in font.keys():
1084 writer.begintag (tag)
1085 writer.newline ()
1086 font[tag].toXML(writer, font)
1087 writer.endtag (tag)
1088 writer.newline ()
1089
Behdad Esfahbod77cda412013-07-22 11:46:50 -04001090 font.save (fontfile + '.subset')
Behdad Esfahbodb70b4982013-07-23 11:38:26 -04001091 lapse ("compile and save font")
1092
1093 last_time = start_time
1094 lapse ("make one with everything (TOTAL TIME)")