blob: 024c72d4be6bcf04597d201e6ea4a4e26caf0da5 [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
Behdad Esfahbodfa3bc5e2013-07-24 14:37:58 -040022# Try running on PyPy
23try:
24 import numpypy
25except ImportError:
26 pass
27
Behdad Esfahbod54660612013-07-21 18:16:55 -040028import fontTools.ttx
Behdad Esfahbod54660612013-07-21 18:16:55 -040029
Behdad Esfahbod54660612013-07-21 18:16:55 -040030
Behdad Esfahbod02b92062013-07-21 18:40:59 -040031def add_method (*clazzes):
Behdad Esfahbod54660612013-07-21 18:16:55 -040032 def wrapper(method):
Behdad Esfahbod02b92062013-07-21 18:40:59 -040033 for clazz in clazzes:
34 setattr (clazz, method.func_name, method)
Behdad Esfahbod54660612013-07-21 18:16:55 -040035 return wrapper
36
Behdad Esfahbod78661bb2013-07-23 10:23:42 -040037def unique_sorted (l):
38 return sorted ({v:1 for v in l}.keys ())
39
40
Behdad Esfahbod54660612013-07-21 18:16:55 -040041@add_method(fontTools.ttLib.tables.otTables.Coverage)
Behdad Esfahbod610b0552013-07-23 14:52:18 -040042def intersect_glyphs (self, glyphs):
43 "Returns ascending list of matching coverage values."
44 return [i for (i,g) in enumerate (self.glyphs) if g in glyphs]
45
46@add_method(fontTools.ttLib.tables.otTables.Coverage)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -040047def subset_glyphs (self, glyphs):
Behdad Esfahbodd821ea02013-07-23 10:50:43 -040048 "Returns ascending list of remaining coverage values."
Behdad Esfahbod610b0552013-07-23 14:52:18 -040049 indices = self.intersect_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -040050 self.glyphs = [g for g in self.glyphs if g in glyphs]
51 return indices
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040052
Behdad Esfahbod54660612013-07-21 18:16:55 -040053@add_method(fontTools.ttLib.tables.otTables.ClassDef)
Behdad Esfahbodb8d55882013-07-23 22:17:39 -040054def intersect_glyphs (self, glyphs):
55 "Returns ascending list of matching class values."
56 return unique_sorted (v for g,v in self.classDefs.items() if g in glyphs)
57
58@add_method(fontTools.ttLib.tables.otTables.ClassDef)
59def intersects_glyphs_class (self, glyphs, klass):
60 "Returns true if any of glyphs has requested class."
61 return any (g in glyphs for g,v in self.classDefs.items() if v == klass)
62
63@add_method(fontTools.ttLib.tables.otTables.ClassDef)
Behdad Esfahbodde71dca2013-07-24 12:40:54 -040064def subset_glyphs (self, glyphs, remap=False):
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -040065 "Returns ascending list of remaining classes."
Behdad Esfahbod54660612013-07-21 18:16:55 -040066 self.classDefs = {g:v for g,v in self.classDefs.items() if g in glyphs}
Behdad Esfahbodde71dca2013-07-24 12:40:54 -040067 indices = unique_sorted (self.classDefs.values ())
68 if remap:
69 self.remap (indices)
70 return indices
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -040071
72@add_method(fontTools.ttLib.tables.otTables.ClassDef)
73def remap (self, class_map):
74 "Remaps classes."
75 self.classDefs = {g:class_map.index (v) for g,v in self.classDefs.items()}
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040076
Behdad Esfahbod54660612013-07-21 18:16:55 -040077@add_method(fontTools.ttLib.tables.otTables.SingleSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -040078def closure_glyphs (self, glyphs, table):
79 if self.Format in [1, 2]:
80 return [v for g,v in self.mapping.items() if g in glyphs]
81 else:
82 assert 0, "unknown format: %s" % self.Format
83
84@add_method(fontTools.ttLib.tables.otTables.SingleSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -040085def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -040086 if self.Format in [1, 2]:
87 self.mapping = {g:v for g,v in self.mapping.items() if g in glyphs}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -040088 return bool (self.mapping)
Behdad Esfahbod54660612013-07-21 18:16:55 -040089 else:
90 assert 0, "unknown format: %s" % self.Format
91
92@add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -040093def closure_glyphs (self, glyphs, table):
94 if self.Format == 1:
95 indices = self.Coverage.intersect_glyphs (glyphs)
96 return sum ((self.Sequence[i].Substitute for i in indices), [])
97 else:
98 assert 0, "unknown format: %s" % self.Format
99
100@add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400101def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400102 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400103 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400104 self.Sequence = [self.Sequence[i] for i in indices]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400105 self.SequenceCount = len (self.Sequence)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400106 return bool (self.SequenceCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400107 else:
108 assert 0, "unknown format: %s" % self.Format
109
110@add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400111def closure_glyphs (self, glyphs, table):
112 if self.Format == 1:
113 return sum ((v for g,v in self.alternates.items() if g in glyphs), [])
114 else:
115 assert 0, "unknown format: %s" % self.Format
116
117@add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400118def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400119 if self.Format == 1:
120 self.alternates = {g:v for g,v in self.alternates.items() if g in glyphs}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400121 return bool (self.alternates)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400122 else:
123 assert 0, "unknown format: %s" % self.Format
124
125@add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400126def closure_glyphs (self, glyphs, table):
127 if self.Format == 1:
128 return sum (([seq.LigGlyph for seq in seqs if all(c in glyphs for c in seq.Component)]
Behdad Esfahbodaf2117f2013-07-24 13:43:44 -0400129 for g,seqs in self.ligatures.items() if g in glyphs), [])
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400130 else:
131 assert 0, "unknown format: %s" % self.Format
132
133@add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400134def subset_glyphs (self, glyphs):
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400135 if self.Format == 1:
136 self.ligatures = {g:v for g,v in self.ligatures.items() if g in glyphs}
137 self.ligatures = {g:[seq for seq in seqs if all(c in glyphs for c in seq.Component)]
138 for g,seqs in self.ligatures.items()}
139 self.ligatures = {g:v for g,v in self.ligatures.items() if v}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400140 return bool (self.ligatures)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400141 else:
142 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400143
Behdad Esfahbod54660612013-07-21 18:16:55 -0400144@add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400145def closure_glyphs (self, glyphs, table):
146 if self.Format == 1:
147 indices = self.Coverage.intersect_glyphs (glyphs)
148 if not indices or \
149 not all (c.intersect_glyphs (glyphs) for c in self.LookAheadCoverage + self.BacktrackCoverage):
150 return []
151 return [self.Substitute[i] for i in indices]
152 else:
153 assert 0, "unknown format: %s" % self.Format
154
155@add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400156def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400157 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400158 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400159 self.Substitute = [self.Substitute[i] for i in indices]
160 self.GlyphCount = len (self.Substitute)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400161 return bool (self.GlyphCount and all (c.subset_glyphs (glyphs) for c in self.LookAheadCoverage + self.BacktrackCoverage))
Behdad Esfahbod54660612013-07-21 18:16:55 -0400162 else:
163 assert 0, "unknown format: %s" % self.Format
164
165@add_method(fontTools.ttLib.tables.otTables.SinglePos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400166def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400167 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400168 return len (self.Coverage.subset_glyphs (glyphs))
Behdad Esfahbod54660612013-07-21 18:16:55 -0400169 elif self.Format == 2:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400170 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400171 self.Value = [self.Value[i] for i in indices]
172 self.ValueCount = len (self.Value)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400173 return bool (self.ValueCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400174 else:
175 assert 0, "unknown format: %s" % self.Format
176
177@add_method(fontTools.ttLib.tables.otTables.PairPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400178def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400179 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400180 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400181 self.PairSet = [self.PairSet[i] for i in indices]
182 for p in self.PairSet:
183 p.PairValueRecord = [r for r in p.PairValueRecord if r.SecondGlyph in glyphs]
184 p.PairValueCount = len (p.PairValueRecord)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400185 self.PairSet = [p for p in self.PairSet if p.PairValueCount]
Behdad Esfahbod54660612013-07-21 18:16:55 -0400186 self.PairSetCount = len (self.PairSet)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400187 return bool (self.PairSetCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400188 elif self.Format == 2:
Behdad Esfahbodde71dca2013-07-24 12:40:54 -0400189 class1_map = self.ClassDef1.subset_glyphs (glyphs, remap=True)
190 class2_map = self.ClassDef2.subset_glyphs (glyphs, remap=True)
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -0400191 self.Class1Record = [self.Class1Record[i] for i in class1_map]
192 for c in self.Class1Record:
193 c.Class2Record = [c.Class2Record[i] for i in class2_map]
194 self.Class1Count = len (class1_map)
195 self.Class2Count = len (class2_map)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400196 return bool (self.Class1Count and self.Class2Count and self.Coverage.subset_glyphs (glyphs))
Behdad Esfahbod54660612013-07-21 18:16:55 -0400197 else:
198 assert 0, "unknown format: %s" % self.Format
199
200@add_method(fontTools.ttLib.tables.otTables.CursivePos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400201def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400202 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400203 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400204 self.EntryExitRecord = [self.EntryExitRecord[i] for i in indices]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400205 self.EntryExitCount = len (self.EntryExitRecord)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400206 return bool (self.EntryExitCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400207 else:
208 assert 0, "unknown format: %s" % self.Format
209
210@add_method(fontTools.ttLib.tables.otTables.MarkBasePos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400211def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400212 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400213 mark_indices = self.MarkCoverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400214 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices]
215 self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400216 base_indices = self.BaseCoverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400217 self.BaseArray.BaseRecord = [self.BaseArray.BaseRecord[i] for i in base_indices]
218 self.BaseArray.BaseCount = len (self.BaseArray.BaseRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400219 # Prune empty classes
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400220 class_indices = unique_sorted (v.Class for v in self.MarkArray.MarkRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400221 self.ClassCount = len (class_indices)
222 for m in self.MarkArray.MarkRecord:
223 m.Class = class_indices.index (m.Class)
224 for b in self.BaseArray.BaseRecord:
225 b.BaseAnchor = [b.BaseAnchor[i] for i in class_indices]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400226 return bool (self.ClassCount and self.MarkArray.MarkCount and self.BaseArray.BaseCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400227 else:
228 assert 0, "unknown format: %s" % self.Format
229
230@add_method(fontTools.ttLib.tables.otTables.MarkLigPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400231def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400232 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400233 mark_indices = self.MarkCoverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400234 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices]
235 self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400236 ligature_indices = self.LigatureCoverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400237 self.LigatureArray.LigatureAttach = [self.LigatureArray.LigatureAttach[i] for i in ligature_indices]
238 self.LigatureArray.LigatureCount = len (self.LigatureArray.LigatureAttach)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400239 # Prune empty classes
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400240 class_indices = unique_sorted (v.Class for v in self.MarkArray.MarkRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400241 self.ClassCount = len (class_indices)
242 for m in self.MarkArray.MarkRecord:
243 m.Class = class_indices.index (m.Class)
244 for l in self.LigatureArray.LigatureAttach:
245 for c in l.ComponentRecord:
246 c.LigatureAnchor = [c.LigatureAnchor[i] for i in class_indices]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400247 return bool (self.ClassCount and self.MarkArray.MarkCount and self.LigatureArray.LigatureCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400248 else:
249 assert 0, "unknown format: %s" % self.Format
250
251@add_method(fontTools.ttLib.tables.otTables.MarkMarkPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400252def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400253 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400254 mark1_indices = self.Mark1Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400255 self.Mark1Array.MarkRecord = [self.Mark1Array.MarkRecord[i] for i in mark1_indices]
256 self.Mark1Array.MarkCount = len (self.Mark1Array.MarkRecord)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400257 mark2_indices = self.Mark2Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400258 self.Mark2Array.Mark2Record = [self.Mark2Array.Mark2Record[i] for i in mark2_indices]
259 self.Mark2Array.MarkCount = len (self.Mark2Array.Mark2Record)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400260 # Prune empty classes
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400261 class_indices = unique_sorted (v.Class for v in self.Mark1Array.MarkRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400262 self.ClassCount = len (class_indices)
263 for m in self.Mark1Array.MarkRecord:
264 m.Class = class_indices.index (m.Class)
265 for b in self.Mark2Array.Mark2Record:
266 b.Mark2Anchor = [b.Mark2Anchor[i] for i in class_indices]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400267 return bool (self.ClassCount and self.Mark1Array.MarkCount and self.Mark2Array.MarkCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400268 else:
269 assert 0, "unknown format: %s" % self.Format
270
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400271@add_method(fontTools.ttLib.tables.otTables.SingleSubst,
272 fontTools.ttLib.tables.otTables.MultipleSubst,
273 fontTools.ttLib.tables.otTables.AlternateSubst,
274 fontTools.ttLib.tables.otTables.LigatureSubst,
275 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
276 fontTools.ttLib.tables.otTables.SinglePos,
277 fontTools.ttLib.tables.otTables.PairPos,
278 fontTools.ttLib.tables.otTables.CursivePos,
279 fontTools.ttLib.tables.otTables.MarkBasePos,
280 fontTools.ttLib.tables.otTables.MarkLigPos,
281 fontTools.ttLib.tables.otTables.MarkMarkPos)
282def subset_lookups (self, lookup_indices):
283 pass
284
285@add_method(fontTools.ttLib.tables.otTables.SingleSubst,
286 fontTools.ttLib.tables.otTables.MultipleSubst,
287 fontTools.ttLib.tables.otTables.AlternateSubst,
288 fontTools.ttLib.tables.otTables.LigatureSubst,
289 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
290 fontTools.ttLib.tables.otTables.SinglePos,
291 fontTools.ttLib.tables.otTables.PairPos,
292 fontTools.ttLib.tables.otTables.CursivePos,
293 fontTools.ttLib.tables.otTables.MarkBasePos,
294 fontTools.ttLib.tables.otTables.MarkLigPos,
295 fontTools.ttLib.tables.otTables.MarkMarkPos)
296def collect_lookups (self):
297 return []
298
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400299@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst,
300 fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos)
301def __classify_context (self):
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400302
303 class ContextHelper:
304 def __init__ (self, klass, Format):
305 if klass.__name__.endswith ('Subst'):
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400306 Typ = 'Sub'
307 Type = 'Subst'
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400308 else:
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400309 Typ = 'Pos'
310 Type = 'Pos'
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400311 if klass.__name__.startswith ('Chain'):
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400312 Chain = 'Chain'
313 else:
314 Chain = ''
315 ChainTyp = Chain+Typ
316
317 self.Typ = Typ
318 self.Type = Type
319 self.Chain = Chain
320 self.ChainTyp = ChainTyp
321
322 self.LookupRecord = Type+'LookupRecord'
323
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400324 if Format == 1:
325 ContextData = None
326 ChainContextData = None
327 RuleData = lambda r: r.Input
328 ChainRuleData = lambda r: r.Backtrack + r.Input + r.LookAhead
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400329 SetRuleData = None
330 ChainSetRuleData = None
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400331 elif Format == 2:
Behdad Esfahbode3f20732013-07-24 11:26:43 -0400332 ContextData = lambda r: (r.ClassDef,)
333 ChainContextData = lambda r: (r.LookAheadClassDef, r.InputClassDef, r.BacktrackClassDef)
334 RuleData = lambda r: (r.Class,)
335 ChainRuleData = lambda r: (r.LookAhead, r.Input, r.Backtrack)
336 def SetRuleData (r, d): (r.Class,) = d
337 def ChainSetRuleData (r, d): (r.LookAhead, r.Input, r.Backtrack) = d
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400338 elif Format == 3:
339 ContextData = None
340 ChainContextData = None
341 RuleData = lambda r: r.Coverage
342 ChainRuleData = lambda r: r.LookAheadCoverage + r.InputCoverage + r.BacktrackCoverage
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400343 SetRuleData = None
344 ChainSetRuleData = None
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400345 else:
346 assert 0, "unknown format: %s" % Format
347
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400348 if Chain:
Behdad Esfahbod707a37a2013-07-23 21:08:26 -0400349 self.ContextData = ChainContextData
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400350 self.RuleData = ChainRuleData
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400351 self.SetRuleData = ChainSetRuleData
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400352 else:
Behdad Esfahbod707a37a2013-07-23 21:08:26 -0400353 self.ContextData = ContextData
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400354 self.RuleData = RuleData
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400355 self.SetRuleData = SetRuleData
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400356
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400357 if Format == 1:
358 self.Rule = ChainTyp+'Rule'
359 self.RuleCount = ChainTyp+'RuleCount'
360 self.RuleSet = ChainTyp+'RuleSet'
361 self.RuleSetCount = ChainTyp+'RuleSetCount'
362 elif Format == 2:
363 self.Rule = ChainTyp+'ClassRule'
364 self.RuleCount = ChainTyp+'ClassRuleCount'
365 self.RuleSet = ChainTyp+'ClassSet'
366 self.RuleSetCount = ChainTyp+'ClassSetCount'
Behdad Esfahbod89987002013-07-23 23:07:42 -0400367
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400368 self.ClassDef = 'InputClassDef' if Chain else 'ClassDef'
Behdad Esfahbod27108392013-07-23 16:40:47 -0400369
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400370 if self.Format not in [1, 2, 3]:
371 return None # Don't shoot the messenger; let it go
372 if not hasattr (self.__class__, "__ContextHelpers"):
373 self.__class__.__ContextHelpers = {}
374 if self.Format not in self.__class__.__ContextHelpers:
375 self.__class__.__ContextHelpers[self.Format] = ContextHelper (self.__class__, self.Format)
376 return self.__class__.__ContextHelpers[self.Format]
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400377
Behdad Esfahbodf2b6d9c2013-07-23 17:31:54 -0400378@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst)
Behdad Esfahbod00776972013-07-23 15:33:00 -0400379def closure_glyphs (self, glyphs, table):
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400380 c = self.__classify_context ()
381
Behdad Esfahbod00776972013-07-23 15:33:00 -0400382 if self.Format == 1:
Behdad Esfahbodeeca9822013-07-23 17:42:17 -0400383 indices = self.Coverage.intersect_glyphs (glyphs)
384 rss = getattr (self, c.RuleSet)
385 return sum ((table.table.LookupList.Lookup[ll.LookupListIndex].closure_glyphs (glyphs, table) \
386 for i in indices \
387 for r in getattr (rss[i], c.Rule) \
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400388 if r and all (g in glyphs for g in c.RuleData (r)) \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400389 for ll in getattr (r, c.LookupRecord) if ll \
Behdad Esfahbodeeca9822013-07-23 17:42:17 -0400390 ), [])
Behdad Esfahbod00776972013-07-23 15:33:00 -0400391 elif self.Format == 2:
Behdad Esfahbod31084302013-07-23 22:22:38 -0400392 if not self.Coverage.intersect_glyphs (glyphs):
393 return []
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400394 indices = getattr (self, c.ClassDef).intersect_glyphs (glyphs)
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400395 rss = getattr (self, c.RuleSet)
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400396 return sum ((table.table.LookupList.Lookup[ll.LookupListIndex].closure_glyphs (glyphs, table) \
397 for i in indices \
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400398 for r in getattr (rss[i], c.Rule) \
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400399 if r and all (cd.intersects_glyphs_class (glyphs, k) \
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400400 for cd,k in zip (c.ContextData (self), c.RuleData (r))) \
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400401 for ll in getattr (r, c.LookupRecord) if ll \
402 ), [])
Behdad Esfahbod00776972013-07-23 15:33:00 -0400403 elif self.Format == 3:
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400404 if not all (x.intersect_glyphs (glyphs) for x in c.RuleData (self)):
Behdad Esfahbod00776972013-07-23 15:33:00 -0400405 return []
406 return sum ((table.table.LookupList.Lookup[ll.LookupListIndex].closure_glyphs (glyphs, table) \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400407 for ll in getattr (self, c.LookupRecord) if ll), [])
Behdad Esfahbod00776972013-07-23 15:33:00 -0400408 else:
409 assert 0, "unknown format: %s" % self.Format
410
Behdad Esfahbodcbba4a62013-07-23 17:27:18 -0400411@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ContextPos,
412 fontTools.ttLib.tables.otTables.ChainContextSubst, fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400413def subset_glyphs (self, glyphs):
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400414 c = self.__classify_context ()
415
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400416 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400417 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400418 rss = getattr (self, c.RuleSet)
419 rss = [rss[i] for i in indices]
420 for rs in rss:
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400421 if rs:
422 ss = getattr (rs, c.Rule)
423 ss = [r for r in ss \
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400424 if r and all (g in glyphs for g in c.RuleData (r))]
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400425 setattr (rs, c.Rule, ss)
426 setattr (rs, c.RuleCount, len (ss))
Behdad Esfahbodcbba4a62013-07-23 17:27:18 -0400427 # Prune empty subrulesets
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400428 rss = [rs for rs in rss if rs and getattr (rs, c.Rule)]
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400429 setattr (self, c.RuleSet, rss)
430 setattr (self, c.RuleSetCount, len (rss))
431 return bool (rss)
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400432 elif self.Format == 2:
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400433 if not self.Coverage.subset_glyphs (glyphs):
434 return False
435 indices = getattr (self, c.ClassDef).intersect_glyphs (glyphs)
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400436 rss = getattr (self, c.RuleSet)
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400437 rss = [rss[i] for i in indices]
438 ContextData = c.ContextData (self)
Behdad Esfahbodde71dca2013-07-24 12:40:54 -0400439 klass_maps = [x.subset_glyphs (glyphs, remap=True) for x in ContextData]
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400440 for rs in rss:
441 if rs:
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400442 ss = getattr (rs, c.Rule)
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400443 ss = [r for r in ss \
444 if r and all (k in klass_map \
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400445 for klass_map,k in zip (klass_maps, c.RuleData (r)))]
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400446 setattr (rs, c.Rule, ss)
447 setattr (rs, c.RuleCount, len (ss))
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400448
449 # Remap rule classes
450 for r in ss:
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400451 c.SetRuleData (r, (klass_map.index (k) \
452 for klassmap,k in zip (klass_maps, c.RuleData (r))))
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400453 # Prune empty subrulesets
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400454 rss = [rs for rs in rss if rs and getattr (rs, c.Rule)]
455 setattr (self, c.RuleSet, rss)
456 setattr (self, c.RuleSetCount, len (rss))
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400457 return bool (rss)
458
459 return all (x.subset_glyphs (glyphs) for x in c.ContextData (self))
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400460 elif self.Format == 3:
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400461 return all (x.subset_glyphs (glyphs) for x in c.RuleData (self))
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400462 else:
463 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400464
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400465@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst,
466 fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400467def subset_lookups (self, lookup_indices):
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400468 c = self.__classify_context ()
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400469
Behdad Esfahbod1f573632013-07-23 23:04:43 -0400470 if self.Format in [1, 2]:
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400471 for rs in getattr (self, c.RuleSet):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400472 if rs:
473 for r in getattr (rs, c.Rule):
474 if r:
Behdad Esfahbod1f573632013-07-23 23:04:43 -0400475 setattr (r, c.LookupRecord, [ll for ll in getattr (r, c.LookupRecord) if ll \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400476 if ll.LookupListIndex in lookup_indices])
477 for ll in getattr (r, c.LookupRecord):
478 if ll:
479 ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400480 elif self.Format == 3:
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400481 setattr (self, c.LookupRecord, [ll for ll in getattr (self, c.LookupRecord) if ll \
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400482 if ll.LookupListIndex in lookup_indices])
483 for ll in getattr (self, c.LookupRecord):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400484 if ll:
485 ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400486 else:
487 assert 0, "unknown format: %s" % self.Format
488
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400489@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst,
490 fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400491def collect_lookups (self):
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400492 c = self.__classify_context ()
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400493
Behdad Esfahbod1f573632013-07-23 23:04:43 -0400494 if self.Format in [1, 2]:
Behdad Esfahbod27108392013-07-23 16:40:47 -0400495 return [ll.LookupListIndex \
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400496 for rs in getattr (self, c.RuleSet) if rs \
497 for r in getattr (rs, c.Rule) if r \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400498 for ll in getattr (r, c.LookupRecord) if ll]
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400499 elif self.Format == 3:
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400500 return [ll.LookupListIndex \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400501 for ll in getattr (self, c.LookupRecord) if ll]
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400502 else:
503 assert 0, "unknown format: %s" % self.Format
504
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400505@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst)
506def closure_glyphs (self, glyphs, table):
507 if self.Format == 1:
508 return self.ExtSubTable.closure_glyphs (glyphs, table)
509 else:
510 assert 0, "unknown format: %s" % self.Format
511
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400512@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400513def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400514 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400515 return self.ExtSubTable.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400516 else:
517 assert 0, "unknown format: %s" % self.Format
518
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400519@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
520def subset_lookups (self, lookup_indices):
521 if self.Format == 1:
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400522 return self.ExtSubTable.subset_lookups (lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400523 else:
524 assert 0, "unknown format: %s" % self.Format
525
526@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
527def collect_lookups (self):
528 if self.Format == 1:
529 return self.ExtSubTable.collect_lookups ()
530 else:
531 assert 0, "unknown format: %s" % self.Format
532
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400533@add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400534def closure_glyphs (self, glyphs, table):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400535 return sum ((s.closure_glyphs (glyphs, table) for s in self.SubTable if s), [])
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400536
537@add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400538def subset_glyphs (self, glyphs):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400539 self.SubTable = [s for s in self.SubTable if s and s.subset_glyphs (glyphs)]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400540 self.SubTableCount = len (self.SubTable)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400541 return bool (self.SubTableCount)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400542
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400543@add_method(fontTools.ttLib.tables.otTables.Lookup)
544def subset_lookups (self, lookup_indices):
545 for s in self.SubTable:
546 s.subset_lookups (lookup_indices)
547
548@add_method(fontTools.ttLib.tables.otTables.Lookup)
549def collect_lookups (self):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400550 return unique_sorted (sum ((s.collect_lookups () for s in self.SubTable if s), []))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400551
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400552@add_method(fontTools.ttLib.tables.otTables.LookupList)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400553def subset_glyphs (self, glyphs):
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400554 "Returns the indices of nonempty lookups."
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400555 return [i for (i,l) in enumerate (self.Lookup) if l and l.subset_glyphs (glyphs)]
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400556
557@add_method(fontTools.ttLib.tables.otTables.LookupList)
558def subset_lookups (self, lookup_indices):
559 self.Lookup = [self.Lookup[i] for i in lookup_indices]
560 self.LookupCount = len (self.Lookup)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400561 for l in self.Lookup:
562 l.subset_lookups (lookup_indices)
563
564@add_method(fontTools.ttLib.tables.otTables.LookupList)
565def closure_lookups (self, lookup_indices):
Behdad Esfahbodbb7e2132013-07-23 13:48:35 -0400566 lookup_indices = unique_sorted (lookup_indices)
567 recurse = lookup_indices
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400568 while True:
Behdad Esfahbodbb7e2132013-07-23 13:48:35 -0400569 recurse_lookups = sum ((self.Lookup[i].collect_lookups () for i in recurse), [])
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400570 recurse_lookups = [l for l in recurse_lookups if l not in lookup_indices]
571 if not recurse_lookups:
Behdad Esfahbodbb7e2132013-07-23 13:48:35 -0400572 return unique_sorted (lookup_indices)
573 recurse_lookups = unique_sorted (recurse_lookups)
574 lookup_indices.extend (recurse_lookups)
575 recurse = recurse_lookups
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400576
577@add_method(fontTools.ttLib.tables.otTables.Feature)
578def subset_lookups (self, lookup_indices):
579 self.LookupListIndex = [l for l in self.LookupListIndex if l in lookup_indices]
580 # Now map them.
581 self.LookupListIndex = [lookup_indices.index (l) for l in self.LookupListIndex]
582 self.LookupCount = len (self.LookupListIndex)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400583 return self.LookupCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400584
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400585@add_method(fontTools.ttLib.tables.otTables.Feature)
586def collect_lookups (self):
587 return self.LookupListIndex[:]
588
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400589@add_method(fontTools.ttLib.tables.otTables.FeatureList)
590def subset_lookups (self, lookup_indices):
591 "Returns the indices of nonempty features."
592 feature_indices = [i for (i,f) in enumerate (self.FeatureRecord) if f.Feature.subset_lookups (lookup_indices)]
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400593 self.subset_features (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400594 return feature_indices
595
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400596@add_method(fontTools.ttLib.tables.otTables.FeatureList)
597def collect_lookups (self, feature_indices):
598 return unique_sorted (sum ((self.FeatureRecord[i].Feature.collect_lookups () for i in feature_indices
599 if i < self.FeatureCount), []))
600
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400601@add_method(fontTools.ttLib.tables.otTables.FeatureList)
602def subset_features (self, feature_indices):
603 self.FeatureRecord = [self.FeatureRecord[i] for i in feature_indices]
604 self.FeatureCount = len (self.FeatureRecord)
605 return bool (self.FeatureCount)
606
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400607@add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys)
608def subset_features (self, feature_indices):
Behdad Esfahbod69ce1502013-07-22 18:00:31 -0400609 if self.ReqFeatureIndex in feature_indices:
610 self.ReqFeatureIndex = feature_indices.index (self.ReqFeatureIndex)
611 else:
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400612 self.ReqFeatureIndex = 65535
613 self.FeatureIndex = [f for f in self.FeatureIndex if f in feature_indices]
Behdad Esfahbod69ce1502013-07-22 18:00:31 -0400614 # Now map them.
615 self.FeatureIndex = [feature_indices.index (f) for f in self.FeatureIndex if f in feature_indices]
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400616 self.FeatureCount = len (self.FeatureIndex)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400617 return bool (self.FeatureCount or self.ReqFeatureIndex != 65535)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400618
619@add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys)
620def collect_features (self):
621 feature_indices = self.FeatureIndex[:]
622 if self.ReqFeatureIndex != 65535:
623 feature_indices.append (self.ReqFeatureIndex)
624 return unique_sorted (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400625
626@add_method(fontTools.ttLib.tables.otTables.Script)
627def subset_features (self, feature_indices):
628 if self.DefaultLangSys and not self.DefaultLangSys.subset_features (feature_indices):
629 self.DefaultLangSys = None
630 self.LangSysRecord = [l for l in self.LangSysRecord if l.LangSys.subset_features (feature_indices)]
631 self.LangSysCount = len (self.LangSysRecord)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400632 return bool (self.LangSysCount or self.DefaultLangSys)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400633
634@add_method(fontTools.ttLib.tables.otTables.Script)
635def collect_features (self):
Behdad Esfahbod2307c8b2013-07-23 11:18:13 -0400636 feature_indices = [l.LangSys.collect_features () for l in self.LangSysRecord]
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400637 if self.DefaultLangSys:
638 feature_indices.append (self.DefaultLangSys.collect_features ())
639 return unique_sorted (sum (feature_indices, []))
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400640
641@add_method(fontTools.ttLib.tables.otTables.ScriptList)
642def subset_features (self, feature_indices):
643 self.ScriptRecord = [s for s in self.ScriptRecord if s.Script.subset_features (feature_indices)]
644 self.ScriptCount = len (self.ScriptRecord)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400645 return bool (self.ScriptCount)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400646
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400647@add_method(fontTools.ttLib.tables.otTables.ScriptList)
648def collect_features (self):
649 return unique_sorted (sum ((s.Script.collect_features () for s in self.ScriptRecord), []))
650
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400651@add_method(fontTools.ttLib.getTableClass('GSUB'))
652def closure_glyphs (self, glyphs):
653 feature_indices = self.table.ScriptList.collect_features ()
654 lookup_indices = self.table.FeatureList.collect_lookups (feature_indices)
655 glyphs = unique_sorted (glyphs)
656 while True:
657 additions = (sum ((self.table.LookupList.Lookup[i].closure_glyphs (glyphs, self) for i in lookup_indices), []))
658 additions = unique_sorted (g for g in additions if g not in glyphs)
659 if not additions:
660 return glyphs
661 glyphs.extend (additions)
662
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400663@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400664def subset_glyphs (self, glyphs):
665 lookup_indices = self.table.LookupList.subset_glyphs (glyphs)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400666 self.subset_lookups (lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400667 self.prune_lookups ()
668 return True
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400669
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400670@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400671def subset_lookups (self, lookup_indices):
672 "Retrains specified lookups, then removes empty features, language systems, and scripts."
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400673 self.table.LookupList.subset_lookups (lookup_indices)
674 feature_indices = self.table.FeatureList.subset_lookups (lookup_indices)
675 self.table.ScriptList.subset_features (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400676
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400677@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
678def prune_lookups (self):
679 "Remove unreferenced lookups"
680 feature_indices = self.table.ScriptList.collect_features ()
681 lookup_indices = self.table.FeatureList.collect_lookups (feature_indices)
682 lookup_indices = self.table.LookupList.closure_lookups (lookup_indices)
683 self.subset_lookups (lookup_indices)
684
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400685@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
686def subset_feature_tags (self, feature_tags):
687 feature_indices = [i for (i,f) in enumerate (self.table.FeatureList.FeatureRecord) if f.FeatureTag in feature_tags]
688 self.table.FeatureList.subset_features (feature_indices)
689 self.table.ScriptList.subset_features (feature_indices)
690
691@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400692def prune_pre_subset (self, options):
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400693 if options['layout-features'] and '*' not in options['layout-features']:
Behdad Esfahboded98c612013-07-23 12:37:41 -0400694 self.subset_feature_tags (options['layout-features'])
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400695 self.prune_lookups ()
696 return True
697
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400698@add_method(fontTools.ttLib.getTableClass('GDEF'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400699def subset_glyphs (self, glyphs):
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400700 table = self.table
701 if table.LigCaretList:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400702 indices = table.LigCaretList.Coverage.subset_glyphs (glyphs)
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400703 table.LigCaretList.LigGlyph = [table.LigCaretList.LigGlyph[i] for i in indices]
704 table.LigCaretList.LigGlyphCount = len (table.LigCaretList.LigGlyph)
705 if not table.LigCaretList.LigGlyphCount:
706 table.LigCaretList = None
707 if table.MarkAttachClassDef:
708 table.MarkAttachClassDef.classDefs = {g:v for g,v in table.MarkAttachClassDef.classDefs.items() if g in glyphs}
709 if not table.MarkAttachClassDef.classDefs:
710 table.MarkAttachClassDef = None
711 if table.GlyphClassDef:
712 table.GlyphClassDef.classDefs = {g:v for g,v in table.GlyphClassDef.classDefs.items() if g in glyphs}
713 if not table.GlyphClassDef.classDefs:
714 table.GlyphClassDef = None
715 if table.AttachList:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400716 indices = table.AttachList.Coverage.subset_glyphs (glyphs)
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400717 table.AttachList.AttachPoint = [table.AttachList.AttachPoint[i] for i in indices]
718 table.AttachList.GlyphCount = len (table.AttachList.AttachPoint)
719 if not table.AttachList.GlyphCount:
720 table.AttachList = None
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400721 return bool (table.LigCaretList or table.MarkAttachClassDef or table.GlyphClassDef or table.AttachList)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400722
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400723@add_method(fontTools.ttLib.getTableClass('kern'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400724def subset_glyphs (self, glyphs):
Behdad Esfahbod5270ec42013-07-22 12:57:02 -0400725 for t in self.kernTables:
726 t.kernTable = {(a,b):v for ((a,b),v) in t.kernTable.items() if a in glyphs and b in glyphs}
727 self.kernTables = [t for t in self.kernTables if t.kernTable]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400728 return bool (self.kernTables)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400729
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400730@add_method(fontTools.ttLib.getTableClass('hmtx'), fontTools.ttLib.getTableClass('vmtx'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400731def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400732 self.metrics = {g:v for g,v in self.metrics.items() if g in glyphs}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400733 return bool (self.metrics)
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400734
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400735@add_method(fontTools.ttLib.getTableClass('hdmx'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400736def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400737 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 -0400738 return bool (self.hdmx)
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400739
Behdad Esfahbode45d6af2013-07-22 15:29:17 -0400740@add_method(fontTools.ttLib.getTableClass('VORG'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400741def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400742 self.VOriginRecords = {g:v for g,v in self.VOriginRecords.items() if g in glyphs}
Behdad Esfahbode45d6af2013-07-22 15:29:17 -0400743 self.numVertOriginYMetrics = len (self.VOriginRecords)
744 return True # Never drop; has default metrics
745
Behdad Esfahbod8c646f62013-07-22 15:06:23 -0400746@add_method(fontTools.ttLib.getTableClass('post'))
Behdad Esfahbod8c486d82013-07-24 13:34:47 -0400747def prune_pre_subset (self, options):
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400748 if not options['glyph-names']:
Behdad Esfahbod42648242013-07-23 12:56:06 -0400749 self.formatType = 3.0
750 return True
751
752@add_method(fontTools.ttLib.getTableClass('post'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400753def subset_glyphs (self, glyphs):
Behdad Esfahbod42648242013-07-23 12:56:06 -0400754 self.extraNames = [] # This seems to do it
Behdad Esfahbodc9dec9d2013-07-23 10:28:47 -0400755 return True
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400756
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400757@add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400758def closure_glyphs (self, glyphs):
759 glyphs = unique_sorted (glyphs)
760 decompose = glyphs
761 # I don't know if component glyphs can be composite themselves.
762 # We handle them anyway.
763 while True:
764 components = []
765 for g in decompose:
Behdad Esfahbodf8c20e42013-07-23 23:13:23 -0400766 if g not in self:
767 continue
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400768 gl = self[g]
769 if gl.isComposite ():
770 for c in gl.components:
771 if c.glyphName not in glyphs:
772 components.append (c.glyphName)
773 components = [c for c in components if c not in glyphs]
774 if not components:
775 return glyphs
776 decompose = unique_sorted (components)
777 glyphs.extend (components)
778
779@add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400780def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400781 self.glyphs = {g:v for g,v in self.glyphs.items() if g in glyphs}
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400782 self.glyphOrder = [g for g in self.glyphOrder if g in glyphs]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400783 return bool (self.glyphs)
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400784
Behdad Esfahboded98c612013-07-23 12:37:41 -0400785@add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400786def prune_post_subset (self, options):
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400787 if not options['hinting']:
Behdad Esfahboded98c612013-07-23 12:37:41 -0400788 for g in self.glyphs.values ():
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400789 g.expand (self)
790 g.program = fontTools.ttLib.tables.ttProgram.Program()
791 g.program.fromBytecode([])
Behdad Esfahboded98c612013-07-23 12:37:41 -0400792 return True
793
Behdad Esfahbod2b677c82013-07-23 13:37:13 -0400794@add_method(fontTools.ttLib.getTableClass('CFF '))
795def subset_glyphs (self, glyphs):
796 assert 0, "unimplemented"
797
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400798@add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400799def prune_pre_subset (self, options):
Behdad Esfahbodde4a15b2013-07-23 13:05:42 -0400800 if not options['legacy-cmap']:
801 # Drop non-Unicode / non-Symbol cmaps
802 self.tables = [t for t in self.tables if t.platformID == 3 and t.platEncID in [0, 1, 10]]
803 if not options['symbol-cmap']:
804 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 -0400805 # TODO Only keep one subtable?
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400806 # For now, drop format=0 which can't be subset_glyphs easily?
807 self.tables = [t for t in self.tables if t.format != 0]
808 return bool (self.tables)
809
810@add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400811def subset_glyphs (self, glyphs):
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400812 for t in self.tables:
Behdad Esfahbod9453a362013-07-22 16:21:24 -0400813 # For reasons I don't understand I need this here
814 # to force decompilation of the cmap format 14.
815 try:
816 getattr (t, "asdf")
817 except AttributeError:
818 pass
Behdad Esfahbodb13d7902013-07-22 16:01:15 -0400819 if t.format == 14:
Behdad Esfahbod9453a362013-07-22 16:21:24 -0400820 # XXX We drop all the default-UVS mappings (g==None)
Behdad Esfahbodb13d7902013-07-22 16:01:15 -0400821 t.uvsDict = {v:[(u,g) for (u,g) in l if g in glyphs] for (v,l) in t.uvsDict.items()}
822 t.uvsDict = {v:l for (v,l) in t.uvsDict.items() if l}
823 else:
824 t.cmap = {u:g for (u,g) in t.cmap.items() if g in glyphs}
825 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 -0400826 # XXX Convert formats when needed
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400827 return bool (self.tables)
828
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400829@add_method(fontTools.ttLib.getTableClass('name'))
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400830def prune_pre_subset (self, options):
Behdad Esfahbod20faeb02013-07-23 13:19:03 -0400831 if '*' not in options['name-IDs']:
832 self.names = [n for n in self.names if n.nameID in options['name-IDs']]
833 if not options['name-legacy']:
834 self.names = [n for n in self.names if n.platformID == 3 and n.platEncID == 1]
835 if '*' not in options['name-languages']:
836 self.names = [n for n in self.names if n.langID in options['name-languages']]
837 return True # Retain even if empty
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400838
Behdad Esfahbod8c646f62013-07-22 15:06:23 -0400839
Behdad Esfahbodbc25f162013-07-23 12:56:54 -0400840drop_tables_default = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', 'PCLT', 'LTSH']
Behdad Esfahbod103a12f2013-07-23 15:08:39 -0400841drop_tables_default += ['Feat', 'Glat', 'Gloc', 'Silf', 'Sill'] # Graphite
Behdad Esfahbod38e852c2013-07-23 16:56:50 -0400842drop_tables_default += ['CBLC', 'CBDT', 'sbix', 'COLR', 'CPAL'] # Color
Behdad Esfahboded98c612013-07-23 12:37:41 -0400843no_subset_tables = ['gasp', 'head', 'hhea', 'maxp', 'vhea', 'OS/2', 'loca', 'name', 'cvt ', 'fpgm', 'prep']
Behdad Esfahbodbc25f162013-07-23 12:56:54 -0400844hinting_tables = ['cvt ', 'fpgm', 'prep', 'hdmx', 'VDMX']
Behdad Esfahbod29df0462013-07-23 11:05:25 -0400845
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400846# Based on HarfBuzz shapers
847layout_features_dict = {
848 # Default shaper
849 'common': ['ccmp', 'liga', 'locl', 'mark', 'mkmk', 'rlig'],
850 'horizontal': ['calt', 'clig', 'curs', 'kern', 'rclt'],
851 'vertical': ['valt', 'vert', 'vkrn', 'vpal', 'vrt2'],
852 'ltr': ['ltra', 'ltrm'],
853 'rtl': ['rtla', 'rtlm'],
854 # Complex shapers
855 'arabic': ['init', 'medi', 'fina', 'isol', 'med2', 'fin2', 'fin3'],
856 'hangul': ['ljmo', 'vjmo', 'tjmo'],
857 'tibetal': ['abvs', 'blws', 'abvm', 'blwm'],
858 'indic': ['nukt', 'akhn', 'rphf', 'rkrf', 'pref', 'blwf', 'half', 'abvf', 'pstf', 'cfar', 'vatu', 'cjct',
859 'init', 'pres', 'abvs', 'blws', 'psts', 'haln', 'dist', 'abvm', 'blwm'],
860}
861layout_features_all = unique_sorted (sum (layout_features_dict.values (), []))
862
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400863options_default = {
Behdad Esfahbodde4a15b2013-07-23 13:05:42 -0400864 'drop-tables': drop_tables_default,
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400865 'layout-features': layout_features_all,
866 'hinting': False,
867 'glyph-names': False,
Behdad Esfahbodde4a15b2013-07-23 13:05:42 -0400868 'legacy-cmap': False,
869 'symbol-cmap': False,
Behdad Esfahbod20faeb02013-07-23 13:19:03 -0400870 'name-IDs': [1, 2], # Family and Style
871 'name-legacy': False,
872 'name-languages': [0x0409], # English
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400873 'mandatory-glyphs': True, # First four for TrueType, .notdef for CFF
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400874}
875
Behdad Esfahbod29df0462013-07-23 11:05:25 -0400876
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400877# TODO OS/2 ulUnicodeRange / ulCodePageRange?
Behdad Esfahbodf71267b2013-07-23 12:59:13 -0400878# TODO Drop unneeded GSUB/GPOS Script/LangSys entries
Behdad Esfahbod398d3892013-07-23 15:29:40 -0400879# TODO Avoid recursing too much
Behdad Esfahbode94aa0e2013-07-23 13:22:04 -0400880# TODO Text direction considerations
881# TODO Text script / language considerations
Behdad Esfahbod38e852c2013-07-23 16:56:50 -0400882# TODO Drop unknown tables
Behdad Esfahbod56ebd042013-07-22 13:02:24 -0400883
Behdad Esfahbod8c486d82013-07-24 13:34:47 -0400884
885def main ():
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400886
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400887 import sys, time
Behdad Esfahbod8c486d82013-07-24 13:34:47 -0400888 global last_time
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400889
890 start_time = time.time ()
891 last_time = start_time
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400892
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400893 verbose = False
894 if "--verbose" in sys.argv:
895 verbose = True
896 sys.argv.remove ("--verbose")
Behdad Esfahbod350a5272013-07-22 12:02:16 -0400897 xml = False
898 if "--xml" in sys.argv:
899 xml = True
900 sys.argv.remove ("--xml")
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400901 timing = False
902 if "--timing" in sys.argv:
903 timing = True
904 sys.argv.remove ("--timing")
905
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400906 options = options_default.copy ()
907
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400908 def lapse (what):
909 if not timing:
910 return
911 global last_time
912 new_time = time.time ()
913 print "Took %0.3fs to %s" % (new_time - last_time, what)
914 last_time = new_time
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400915
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400916 if len (sys.argv) < 3:
917 print >>sys.stderr, "usage: pyotlss.py font-file glyph..."
918 sys.exit (1)
919
920 fontfile = sys.argv[1]
921 glyphs = sys.argv[2:]
922
923 font = fontTools.ttx.TTFont (fontfile)
Behdad Esfahbod0f86ce92013-07-22 17:30:31 -0400924 font.disassembleInstructions = False
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400925 lapse ("load font")
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400926
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400927 if options["mandatory-glyphs"]:
928 # Always include .notdef; anything else?
929 if 'glyf' in font:
930 glyphs.extend (['gid0', 'gid1', 'gid2', 'gid3'])
931 if verbose:
932 print "Added first four glyphs to subset"
933 else:
934 glyphs.append ('.notdef')
935 if verbose:
936 print "Added .notdef glyph to subset"
937
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400938 names = font.getGlyphNames()
Behdad Esfahbod9bd59c42013-07-23 21:19:49 -0400939 lapse ("loading glyph names")
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400940 # Convert to glyph names
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400941 glyph_names = []
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400942 cmap_tables = None
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400943 for g in glyphs:
944 if g in names:
945 glyph_names.append (g)
946 continue
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400947 if g.startswith ('uni') and len (g) > 3:
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400948 if not cmap_tables:
949 cmap = font['cmap']
950 cmap_tables = [t for t in cmap.tables if t.platformID == 3 and t.platEncID in [1, 10]]
951 del cmap
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400952 found = False
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400953 u = int (g[3:], 16)
954 for table in cmap_tables:
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400955 if u in table.cmap:
956 glyph_names.append (table.cmap[u])
957 found = True
958 break
959 if not found:
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400960 if verbose:
961 print ("No glyph for Unicode value %s; skipping." % g)
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400962 continue
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400963 if g.startswith ('gid') or g.startswith ('glyph'):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400964 if g.startswith ('gid') and len (g) > 3:
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400965 g = g[3:]
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400966 elif g.startswith ('glyph') and len (g) > 5:
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400967 g = g[5:]
968 try:
969 glyph_names.append (font.getGlyphName (int (g), requireReal=1))
970 except ValueError:
971 raise Exception ("Invalid glyph identifier %s" % g)
972 continue
973 raise Exception ("Invalid glyph identifier %s" % g)
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400974 del cmap_tables
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400975 glyphs = unique_sorted (glyph_names)
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400976 del glyph_names
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400977 lapse ("compile glyph list")
Behdad Esfahbod240d7e72013-07-23 23:12:06 -0400978 if verbose:
979 print "Glyphs:", glyphs
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400980
Behdad Esfahbodde71dca2013-07-24 12:40:54 -0400981
982 for tag in font.keys():
983 if tag == 'GlyphOrder': continue
984
985 if tag in options['drop-tables'] or \
986 (not options['hinting'] and tag in hinting_tables):
987 if verbose:
988 print tag, "dropped."
989 del font[tag]
990 continue
991
992 clazz = fontTools.ttLib.getTableClass(tag)
993
994 if hasattr (clazz, 'prune_pre_subset'):
995 table = font[tag]
996 retain = table.prune_pre_subset (options)
997 lapse ("prune '%s'" % tag)
998 if not retain:
999 if verbose:
1000 print tag, "pruned to empty; dropped."
1001 del font[tag]
1002 continue
1003 else:
1004 if verbose:
1005 print tag, "pruned."
1006
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001007 glyphs_requested = glyphs
1008 if 'GSUB' in font:
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001009 if verbose:
Behdad Esfahbode30ed122013-07-23 21:08:29 -04001010 print "Closing glyph list over 'GSUB': %d glyphs before" % len (glyphs)
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001011 glyphs = font['GSUB'].closure_glyphs (glyphs)
1012 if verbose:
Behdad Esfahbode30ed122013-07-23 21:08:29 -04001013 print "Closed glyph list over 'GSUB': %d glyphs after" % len (glyphs)
Behdad Esfahbod240d7e72013-07-23 23:12:06 -04001014 print "Glyphs:", glyphs
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001015 lapse ("close glyph list over 'GSUB'")
1016 glyphs_gsubed = glyphs
1017
Behdad Esfahbod2a784ac2013-07-22 17:00:36 -04001018 # Close over composite glyphs
1019 if 'glyf' in font:
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001020 if verbose:
Behdad Esfahbode30ed122013-07-23 21:08:29 -04001021 print "Closing glyph list over 'glyf': %d glyphs before" % len (glyphs)
Behdad Esfahbod240d7e72013-07-23 23:12:06 -04001022 print "Glyphs:", glyphs
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001023 glyphs = font['glyf'].closure_glyphs (glyphs)
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001024 if verbose:
Behdad Esfahbode30ed122013-07-23 21:08:29 -04001025 print "Closed glyph list over 'glyf': %d glyphs after" % len (glyphs)
Behdad Esfahbod240d7e72013-07-23 23:12:06 -04001026 print "Glyphs:", glyphs
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001027 lapse ("close glyph list over 'glyf'")
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001028 else:
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001029 glyphs = glyphs
1030 glyphs_glyfed = glyphs
1031 glyphs_closed = glyphs
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001032 del glyphs
1033
Behdad Esfahbodc9dec9d2013-07-23 10:28:47 -04001034 if verbose:
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001035 print "Retaining %d glyphs: " % len (glyphs_closed)
Behdad Esfahbod2a784ac2013-07-22 17:00:36 -04001036
Behdad Esfahbod8842ce22013-07-22 13:01:33 -04001037 for tag in font.keys():
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04001038 if tag == 'GlyphOrder': continue
Behdad Esfahbod4e214e42013-07-22 13:13:49 -04001039
Behdad Esfahbod8842ce22013-07-22 13:01:33 -04001040 clazz = fontTools.ttLib.getTableClass(tag)
Behdad Esfahbod4e214e42013-07-22 13:13:49 -04001041
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001042 if tag in no_subset_tables:
1043 if verbose:
1044 print tag, "subsetting not needed."
Behdad Esfahbod96f47042013-07-23 12:21:34 -04001045 elif hasattr (clazz, 'subset_glyphs'):
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001046 table = font[tag]
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001047 if tag == 'cmap': # What else?
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001048 glyphs = glyphs_requested
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001049 elif tag in ['GSUB', 'GPOS', 'GDEF', 'cmap', 'kern', 'post']: # What else?
1050 glyphs = glyphs_gsubed
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001051 else:
1052 glyphs = glyphs_closed
Behdad Esfahbodb70b4982013-07-23 11:38:26 -04001053 retain = table.subset_glyphs (glyphs)
1054 lapse ("subset '%s'" % tag)
1055 if not retain:
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001056 if verbose:
1057 print tag, "subsetted to empty; dropped."
Behdad Esfahbod8b411f32013-07-23 11:24:20 -04001058 del font[tag]
1059 continue
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001060 else:
1061 if verbose:
1062 print tag, "subsetted."
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001063 del glyphs
Behdad Esfahbod4ae81712013-07-22 11:57:13 -04001064 else:
Behdad Esfahbod350a5272013-07-22 12:02:16 -04001065 if verbose:
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001066 print tag, "NOT subset; don't know how to subset."
1067 continue
1068
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -04001069 if hasattr (clazz, 'prune_post_subset'):
1070 table = font[tag]
1071 retain = table.prune_post_subset (options)
1072 lapse ("prune '%s'" % tag)
1073 if not retain:
1074 if verbose:
1075 print tag, "pruned to empty; dropped."
1076 del font[tag]
1077 continue
1078 else:
1079 if verbose:
1080 print tag, "pruned."
1081
Behdad Esfahbodc7160442013-07-22 14:29:08 -04001082 glyphOrder = font.getGlyphOrder()
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001083 glyphOrder = [g for g in glyphOrder if g in glyphs_closed]
Behdad Esfahbodc7160442013-07-22 14:29:08 -04001084 font.setGlyphOrder (glyphOrder)
1085 font._buildReverseGlyphOrderDict ()
Behdad Esfahbodb70b4982013-07-23 11:38:26 -04001086 lapse ("subset GlyphOrder")
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -04001087
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04001088 font.save (fontfile + '.subset')
1089 lapse ("compile and save font")
1090
1091 last_time = start_time
1092 lapse ("make one with everything (TOTAL TIME)")
1093
Behdad Esfahbod0f86ce92013-07-22 17:30:31 -04001094 if xml:
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04001095 import xmlWriter
1096 writer = xmlWriter.XMLWriter (sys.stdout)
1097
Behdad Esfahbod0f86ce92013-07-22 17:30:31 -04001098 for tag in font.keys():
1099 writer.begintag (tag)
1100 writer.newline ()
1101 font[tag].toXML(writer, font)
1102 writer.endtag (tag)
1103 writer.newline ()
Behdad Esfahbod8c486d82013-07-24 13:34:47 -04001104
1105if __name__ == '__main__':
1106 main ()