blob: a02f0adee7f09901926016c1887abe25b7d34e99 [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 Esfahbodde71dca2013-07-24 12:40:54 -040058def subset_glyphs (self, glyphs, remap=False):
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 Esfahbodde71dca2013-07-24 12:40:54 -040061 indices = unique_sorted (self.classDefs.values ())
62 if remap:
63 self.remap (indices)
64 return indices
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -040065
66@add_method(fontTools.ttLib.tables.otTables.ClassDef)
67def remap (self, class_map):
68 "Remaps classes."
69 self.classDefs = {g:class_map.index (v) for g,v in self.classDefs.items()}
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040070
Behdad Esfahbod54660612013-07-21 18:16:55 -040071@add_method(fontTools.ttLib.tables.otTables.SingleSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -040072def closure_glyphs (self, glyphs, table):
73 if self.Format in [1, 2]:
74 return [v for g,v in self.mapping.items() if g in glyphs]
75 else:
76 assert 0, "unknown format: %s" % self.Format
77
78@add_method(fontTools.ttLib.tables.otTables.SingleSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -040079def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -040080 if self.Format in [1, 2]:
81 self.mapping = {g:v for g,v in self.mapping.items() if g in glyphs}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -040082 return bool (self.mapping)
Behdad Esfahbod54660612013-07-21 18:16:55 -040083 else:
84 assert 0, "unknown format: %s" % self.Format
85
86@add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -040087def closure_glyphs (self, glyphs, table):
88 if self.Format == 1:
89 indices = self.Coverage.intersect_glyphs (glyphs)
90 return sum ((self.Sequence[i].Substitute for i in indices), [])
91 else:
92 assert 0, "unknown format: %s" % self.Format
93
94@add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -040095def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -040096 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -040097 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -040098 self.Sequence = [self.Sequence[i] for i in indices]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040099 self.SequenceCount = len (self.Sequence)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400100 return bool (self.SequenceCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400101 else:
102 assert 0, "unknown format: %s" % self.Format
103
104@add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400105def closure_glyphs (self, glyphs, table):
106 if self.Format == 1:
107 return sum ((v for g,v in self.alternates.items() if g in glyphs), [])
108 else:
109 assert 0, "unknown format: %s" % self.Format
110
111@add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400112def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400113 if self.Format == 1:
114 self.alternates = {g:v for g,v in self.alternates.items() if g in glyphs}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400115 return bool (self.alternates)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400116 else:
117 assert 0, "unknown format: %s" % self.Format
118
119@add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400120def closure_glyphs (self, glyphs, table):
121 if self.Format == 1:
122 return sum (([seq.LigGlyph for seq in seqs if all(c in glyphs for c in seq.Component)]
123 for g,seqs in self.ligatures.items()), [])
124 else:
125 assert 0, "unknown format: %s" % self.Format
126
127@add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400128def subset_glyphs (self, glyphs):
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400129 if self.Format == 1:
130 self.ligatures = {g:v for g,v in self.ligatures.items() if g in glyphs}
131 self.ligatures = {g:[seq for seq in seqs if all(c in glyphs for c in seq.Component)]
132 for g,seqs in self.ligatures.items()}
133 self.ligatures = {g:v for g,v in self.ligatures.items() if v}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400134 return bool (self.ligatures)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400135 else:
136 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400137
Behdad Esfahbod54660612013-07-21 18:16:55 -0400138@add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400139def closure_glyphs (self, glyphs, table):
140 if self.Format == 1:
141 indices = self.Coverage.intersect_glyphs (glyphs)
142 if not indices or \
143 not all (c.intersect_glyphs (glyphs) for c in self.LookAheadCoverage + self.BacktrackCoverage):
144 return []
145 return [self.Substitute[i] for i in indices]
146 else:
147 assert 0, "unknown format: %s" % self.Format
148
149@add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400150def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400151 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400152 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400153 self.Substitute = [self.Substitute[i] for i in indices]
154 self.GlyphCount = len (self.Substitute)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400155 return bool (self.GlyphCount and all (c.subset_glyphs (glyphs) for c in self.LookAheadCoverage + self.BacktrackCoverage))
Behdad Esfahbod54660612013-07-21 18:16:55 -0400156 else:
157 assert 0, "unknown format: %s" % self.Format
158
159@add_method(fontTools.ttLib.tables.otTables.SinglePos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400160def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400161 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400162 return len (self.Coverage.subset_glyphs (glyphs))
Behdad Esfahbod54660612013-07-21 18:16:55 -0400163 elif self.Format == 2:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400164 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400165 self.Value = [self.Value[i] for i in indices]
166 self.ValueCount = len (self.Value)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400167 return bool (self.ValueCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400168 else:
169 assert 0, "unknown format: %s" % self.Format
170
171@add_method(fontTools.ttLib.tables.otTables.PairPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400172def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400173 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400174 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400175 self.PairSet = [self.PairSet[i] for i in indices]
176 for p in self.PairSet:
177 p.PairValueRecord = [r for r in p.PairValueRecord if r.SecondGlyph in glyphs]
178 p.PairValueCount = len (p.PairValueRecord)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400179 self.PairSet = [p for p in self.PairSet if p.PairValueCount]
Behdad Esfahbod54660612013-07-21 18:16:55 -0400180 self.PairSetCount = len (self.PairSet)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400181 return bool (self.PairSetCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400182 elif self.Format == 2:
Behdad Esfahbodde71dca2013-07-24 12:40:54 -0400183 class1_map = self.ClassDef1.subset_glyphs (glyphs, remap=True)
184 class2_map = self.ClassDef2.subset_glyphs (glyphs, remap=True)
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -0400185 self.Class1Record = [self.Class1Record[i] for i in class1_map]
186 for c in self.Class1Record:
187 c.Class2Record = [c.Class2Record[i] for i in class2_map]
188 self.Class1Count = len (class1_map)
189 self.Class2Count = len (class2_map)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400190 return bool (self.Class1Count and self.Class2Count and self.Coverage.subset_glyphs (glyphs))
Behdad Esfahbod54660612013-07-21 18:16:55 -0400191 else:
192 assert 0, "unknown format: %s" % self.Format
193
194@add_method(fontTools.ttLib.tables.otTables.CursivePos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400195def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400196 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400197 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400198 self.EntryExitRecord = [self.EntryExitRecord[i] for i in indices]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400199 self.EntryExitCount = len (self.EntryExitRecord)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400200 return bool (self.EntryExitCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400201 else:
202 assert 0, "unknown format: %s" % self.Format
203
204@add_method(fontTools.ttLib.tables.otTables.MarkBasePos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400205def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400206 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400207 mark_indices = self.MarkCoverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400208 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices]
209 self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400210 base_indices = self.BaseCoverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400211 self.BaseArray.BaseRecord = [self.BaseArray.BaseRecord[i] for i in base_indices]
212 self.BaseArray.BaseCount = len (self.BaseArray.BaseRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400213 # Prune empty classes
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400214 class_indices = unique_sorted (v.Class for v in self.MarkArray.MarkRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400215 self.ClassCount = len (class_indices)
216 for m in self.MarkArray.MarkRecord:
217 m.Class = class_indices.index (m.Class)
218 for b in self.BaseArray.BaseRecord:
219 b.BaseAnchor = [b.BaseAnchor[i] for i in class_indices]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400220 return bool (self.ClassCount and self.MarkArray.MarkCount and self.BaseArray.BaseCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400221 else:
222 assert 0, "unknown format: %s" % self.Format
223
224@add_method(fontTools.ttLib.tables.otTables.MarkLigPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400225def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400226 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400227 mark_indices = self.MarkCoverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400228 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices]
229 self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400230 ligature_indices = self.LigatureCoverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400231 self.LigatureArray.LigatureAttach = [self.LigatureArray.LigatureAttach[i] for i in ligature_indices]
232 self.LigatureArray.LigatureCount = len (self.LigatureArray.LigatureAttach)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400233 # Prune empty classes
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400234 class_indices = unique_sorted (v.Class for v in self.MarkArray.MarkRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400235 self.ClassCount = len (class_indices)
236 for m in self.MarkArray.MarkRecord:
237 m.Class = class_indices.index (m.Class)
238 for l in self.LigatureArray.LigatureAttach:
239 for c in l.ComponentRecord:
240 c.LigatureAnchor = [c.LigatureAnchor[i] for i in class_indices]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400241 return bool (self.ClassCount and self.MarkArray.MarkCount and self.LigatureArray.LigatureCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400242 else:
243 assert 0, "unknown format: %s" % self.Format
244
245@add_method(fontTools.ttLib.tables.otTables.MarkMarkPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400246def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400247 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400248 mark1_indices = self.Mark1Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400249 self.Mark1Array.MarkRecord = [self.Mark1Array.MarkRecord[i] for i in mark1_indices]
250 self.Mark1Array.MarkCount = len (self.Mark1Array.MarkRecord)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400251 mark2_indices = self.Mark2Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400252 self.Mark2Array.Mark2Record = [self.Mark2Array.Mark2Record[i] for i in mark2_indices]
253 self.Mark2Array.MarkCount = len (self.Mark2Array.Mark2Record)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400254 # Prune empty classes
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400255 class_indices = unique_sorted (v.Class for v in self.Mark1Array.MarkRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400256 self.ClassCount = len (class_indices)
257 for m in self.Mark1Array.MarkRecord:
258 m.Class = class_indices.index (m.Class)
259 for b in self.Mark2Array.Mark2Record:
260 b.Mark2Anchor = [b.Mark2Anchor[i] for i in class_indices]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400261 return bool (self.ClassCount and self.Mark1Array.MarkCount and self.Mark2Array.MarkCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400262 else:
263 assert 0, "unknown format: %s" % self.Format
264
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400265@add_method(fontTools.ttLib.tables.otTables.SingleSubst,
266 fontTools.ttLib.tables.otTables.MultipleSubst,
267 fontTools.ttLib.tables.otTables.AlternateSubst,
268 fontTools.ttLib.tables.otTables.LigatureSubst,
269 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
270 fontTools.ttLib.tables.otTables.SinglePos,
271 fontTools.ttLib.tables.otTables.PairPos,
272 fontTools.ttLib.tables.otTables.CursivePos,
273 fontTools.ttLib.tables.otTables.MarkBasePos,
274 fontTools.ttLib.tables.otTables.MarkLigPos,
275 fontTools.ttLib.tables.otTables.MarkMarkPos)
276def subset_lookups (self, lookup_indices):
277 pass
278
279@add_method(fontTools.ttLib.tables.otTables.SingleSubst,
280 fontTools.ttLib.tables.otTables.MultipleSubst,
281 fontTools.ttLib.tables.otTables.AlternateSubst,
282 fontTools.ttLib.tables.otTables.LigatureSubst,
283 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
284 fontTools.ttLib.tables.otTables.SinglePos,
285 fontTools.ttLib.tables.otTables.PairPos,
286 fontTools.ttLib.tables.otTables.CursivePos,
287 fontTools.ttLib.tables.otTables.MarkBasePos,
288 fontTools.ttLib.tables.otTables.MarkLigPos,
289 fontTools.ttLib.tables.otTables.MarkMarkPos)
290def collect_lookups (self):
291 return []
292
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400293@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst,
294 fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos)
295def __classify_context (self):
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400296
297 class ContextHelper:
298 def __init__ (self, klass, Format):
299 if klass.__name__.endswith ('Subst'):
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400300 Typ = 'Sub'
301 Type = 'Subst'
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400302 else:
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400303 Typ = 'Pos'
304 Type = 'Pos'
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400305 if klass.__name__.startswith ('Chain'):
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400306 Chain = 'Chain'
307 else:
308 Chain = ''
309 ChainTyp = Chain+Typ
310
311 self.Typ = Typ
312 self.Type = Type
313 self.Chain = Chain
314 self.ChainTyp = ChainTyp
315
316 self.LookupRecord = Type+'LookupRecord'
317
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400318 if Format == 1:
319 ContextData = None
320 ChainContextData = None
321 RuleData = lambda r: r.Input
322 ChainRuleData = lambda r: r.Backtrack + r.Input + r.LookAhead
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400323 SetRuleData = None
324 ChainSetRuleData = None
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400325 elif Format == 2:
Behdad Esfahbode3f20732013-07-24 11:26:43 -0400326 ContextData = lambda r: (r.ClassDef,)
327 ChainContextData = lambda r: (r.LookAheadClassDef, r.InputClassDef, r.BacktrackClassDef)
328 RuleData = lambda r: (r.Class,)
329 ChainRuleData = lambda r: (r.LookAhead, r.Input, r.Backtrack)
330 def SetRuleData (r, d): (r.Class,) = d
331 def ChainSetRuleData (r, d): (r.LookAhead, r.Input, r.Backtrack) = d
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400332 elif Format == 3:
333 ContextData = None
334 ChainContextData = None
335 RuleData = lambda r: r.Coverage
336 ChainRuleData = lambda r: r.LookAheadCoverage + r.InputCoverage + r.BacktrackCoverage
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400337 SetRuleData = None
338 ChainSetRuleData = None
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400339 else:
340 assert 0, "unknown format: %s" % Format
341
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400342 if Chain:
Behdad Esfahbod707a37a2013-07-23 21:08:26 -0400343 self.ContextData = ChainContextData
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400344 self.RuleData = ChainRuleData
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400345 self.SetRuleData = ChainSetRuleData
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400346 else:
Behdad Esfahbod707a37a2013-07-23 21:08:26 -0400347 self.ContextData = ContextData
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400348 self.RuleData = RuleData
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400349 self.SetRuleData = SetRuleData
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400350
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400351 if Format == 1:
352 self.Rule = ChainTyp+'Rule'
353 self.RuleCount = ChainTyp+'RuleCount'
354 self.RuleSet = ChainTyp+'RuleSet'
355 self.RuleSetCount = ChainTyp+'RuleSetCount'
356 elif Format == 2:
357 self.Rule = ChainTyp+'ClassRule'
358 self.RuleCount = ChainTyp+'ClassRuleCount'
359 self.RuleSet = ChainTyp+'ClassSet'
360 self.RuleSetCount = ChainTyp+'ClassSetCount'
Behdad Esfahbod89987002013-07-23 23:07:42 -0400361
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400362 self.ClassDef = 'InputClassDef' if Chain else 'ClassDef'
Behdad Esfahbod27108392013-07-23 16:40:47 -0400363
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400364 if self.Format not in [1, 2, 3]:
365 return None # Don't shoot the messenger; let it go
366 if not hasattr (self.__class__, "__ContextHelpers"):
367 self.__class__.__ContextHelpers = {}
368 if self.Format not in self.__class__.__ContextHelpers:
369 self.__class__.__ContextHelpers[self.Format] = ContextHelper (self.__class__, self.Format)
370 return self.__class__.__ContextHelpers[self.Format]
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400371
Behdad Esfahbodf2b6d9c2013-07-23 17:31:54 -0400372@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst)
Behdad Esfahbod00776972013-07-23 15:33:00 -0400373def closure_glyphs (self, glyphs, table):
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400374 c = self.__classify_context ()
375
Behdad Esfahbod00776972013-07-23 15:33:00 -0400376 if self.Format == 1:
Behdad Esfahbodeeca9822013-07-23 17:42:17 -0400377 indices = self.Coverage.intersect_glyphs (glyphs)
378 rss = getattr (self, c.RuleSet)
379 return sum ((table.table.LookupList.Lookup[ll.LookupListIndex].closure_glyphs (glyphs, table) \
380 for i in indices \
381 for r in getattr (rss[i], c.Rule) \
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400382 if r and all (g in glyphs for g in c.RuleData (r)) \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400383 for ll in getattr (r, c.LookupRecord) if ll \
Behdad Esfahbodeeca9822013-07-23 17:42:17 -0400384 ), [])
Behdad Esfahbod00776972013-07-23 15:33:00 -0400385 elif self.Format == 2:
Behdad Esfahbod31084302013-07-23 22:22:38 -0400386 if not self.Coverage.intersect_glyphs (glyphs):
387 return []
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400388 indices = getattr (self, c.ClassDef).intersect_glyphs (glyphs)
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400389 rss = getattr (self, c.RuleSet)
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400390 return sum ((table.table.LookupList.Lookup[ll.LookupListIndex].closure_glyphs (glyphs, table) \
391 for i in indices \
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400392 for r in getattr (rss[i], c.Rule) \
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400393 if r and all (cd.intersects_glyphs_class (glyphs, k) \
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400394 for cd,k in zip (c.ContextData (self), c.RuleData (r))) \
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400395 for ll in getattr (r, c.LookupRecord) if ll \
396 ), [])
Behdad Esfahbod00776972013-07-23 15:33:00 -0400397 elif self.Format == 3:
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400398 if not all (x.intersect_glyphs (glyphs) for x in c.RuleData (self)):
Behdad Esfahbod00776972013-07-23 15:33:00 -0400399 return []
400 return sum ((table.table.LookupList.Lookup[ll.LookupListIndex].closure_glyphs (glyphs, table) \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400401 for ll in getattr (self, c.LookupRecord) if ll), [])
Behdad Esfahbod00776972013-07-23 15:33:00 -0400402 else:
403 assert 0, "unknown format: %s" % self.Format
404
Behdad Esfahbodcbba4a62013-07-23 17:27:18 -0400405@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ContextPos,
406 fontTools.ttLib.tables.otTables.ChainContextSubst, fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400407def subset_glyphs (self, glyphs):
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400408 c = self.__classify_context ()
409
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400410 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400411 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400412 rss = getattr (self, c.RuleSet)
413 rss = [rss[i] for i in indices]
414 for rs in rss:
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400415 if rs:
416 ss = getattr (rs, c.Rule)
417 ss = [r for r in ss \
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400418 if r and all (g in glyphs for g in c.RuleData (r))]
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400419 setattr (rs, c.Rule, ss)
420 setattr (rs, c.RuleCount, len (ss))
Behdad Esfahbodcbba4a62013-07-23 17:27:18 -0400421 # Prune empty subrulesets
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400422 rss = [rs for rs in rss if rs and getattr (rs, c.Rule)]
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400423 setattr (self, c.RuleSet, rss)
424 setattr (self, c.RuleSetCount, len (rss))
425 return bool (rss)
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400426 elif self.Format == 2:
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400427 if not self.Coverage.subset_glyphs (glyphs):
428 return False
429 indices = getattr (self, c.ClassDef).intersect_glyphs (glyphs)
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400430 rss = getattr (self, c.RuleSet)
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400431 rss = [rss[i] for i in indices]
432 ContextData = c.ContextData (self)
Behdad Esfahbodde71dca2013-07-24 12:40:54 -0400433 klass_maps = [x.subset_glyphs (glyphs, remap=True) for x in ContextData]
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400434 for rs in rss:
435 if rs:
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400436 ss = getattr (rs, c.Rule)
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400437 ss = [r for r in ss \
438 if r and all (k in klass_map \
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400439 for klass_map,k in zip (klass_maps, c.RuleData (r)))]
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400440 setattr (rs, c.Rule, ss)
441 setattr (rs, c.RuleCount, len (ss))
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400442
443 # Remap rule classes
444 for r in ss:
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400445 c.SetRuleData (r, (klass_map.index (k) \
446 for klassmap,k in zip (klass_maps, c.RuleData (r))))
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400447 # Prune empty subrulesets
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400448 rss = [rs for rs in rss if rs and getattr (rs, c.Rule)]
449 setattr (self, c.RuleSet, rss)
450 setattr (self, c.RuleSetCount, len (rss))
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400451 return bool (rss)
452
453 return all (x.subset_glyphs (glyphs) for x in c.ContextData (self))
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400454 elif self.Format == 3:
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400455 return all (x.subset_glyphs (glyphs) for x in c.RuleData (self))
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400456 else:
457 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400458
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400459@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst,
460 fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400461def subset_lookups (self, lookup_indices):
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400462 c = self.__classify_context ()
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400463
Behdad Esfahbod1f573632013-07-23 23:04:43 -0400464 if self.Format in [1, 2]:
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400465 for rs in getattr (self, c.RuleSet):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400466 if rs:
467 for r in getattr (rs, c.Rule):
468 if r:
Behdad Esfahbod1f573632013-07-23 23:04:43 -0400469 setattr (r, c.LookupRecord, [ll for ll in getattr (r, c.LookupRecord) if ll \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400470 if ll.LookupListIndex in lookup_indices])
471 for ll in getattr (r, c.LookupRecord):
472 if ll:
473 ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400474 elif self.Format == 3:
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400475 setattr (self, c.LookupRecord, [ll for ll in getattr (self, c.LookupRecord) if ll \
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400476 if ll.LookupListIndex in lookup_indices])
477 for ll in getattr (self, c.LookupRecord):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400478 if ll:
479 ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400480 else:
481 assert 0, "unknown format: %s" % self.Format
482
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400483@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst,
484 fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400485def collect_lookups (self):
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400486 c = self.__classify_context ()
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400487
Behdad Esfahbod1f573632013-07-23 23:04:43 -0400488 if self.Format in [1, 2]:
Behdad Esfahbod27108392013-07-23 16:40:47 -0400489 return [ll.LookupListIndex \
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400490 for rs in getattr (self, c.RuleSet) if rs \
491 for r in getattr (rs, c.Rule) if r \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400492 for ll in getattr (r, c.LookupRecord) if ll]
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400493 elif self.Format == 3:
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400494 return [ll.LookupListIndex \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400495 for ll in getattr (self, c.LookupRecord) if ll]
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400496 else:
497 assert 0, "unknown format: %s" % self.Format
498
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400499@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst)
500def closure_glyphs (self, glyphs, table):
501 if self.Format == 1:
502 return self.ExtSubTable.closure_glyphs (glyphs, table)
503 else:
504 assert 0, "unknown format: %s" % self.Format
505
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400506@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400507def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400508 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400509 return self.ExtSubTable.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400510 else:
511 assert 0, "unknown format: %s" % self.Format
512
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400513@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
514def subset_lookups (self, lookup_indices):
515 if self.Format == 1:
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400516 return self.ExtSubTable.subset_lookups (lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400517 else:
518 assert 0, "unknown format: %s" % self.Format
519
520@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
521def collect_lookups (self):
522 if self.Format == 1:
523 return self.ExtSubTable.collect_lookups ()
524 else:
525 assert 0, "unknown format: %s" % self.Format
526
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400527@add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400528def closure_glyphs (self, glyphs, table):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400529 return sum ((s.closure_glyphs (glyphs, table) for s in self.SubTable if s), [])
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400530
531@add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400532def subset_glyphs (self, glyphs):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400533 self.SubTable = [s for s in self.SubTable if s and s.subset_glyphs (glyphs)]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400534 self.SubTableCount = len (self.SubTable)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400535 return bool (self.SubTableCount)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400536
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400537@add_method(fontTools.ttLib.tables.otTables.Lookup)
538def subset_lookups (self, lookup_indices):
539 for s in self.SubTable:
540 s.subset_lookups (lookup_indices)
541
542@add_method(fontTools.ttLib.tables.otTables.Lookup)
543def collect_lookups (self):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400544 return unique_sorted (sum ((s.collect_lookups () for s in self.SubTable if s), []))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400545
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400546@add_method(fontTools.ttLib.tables.otTables.LookupList)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400547def subset_glyphs (self, glyphs):
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400548 "Returns the indices of nonempty lookups."
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400549 return [i for (i,l) in enumerate (self.Lookup) if l and l.subset_glyphs (glyphs)]
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400550
551@add_method(fontTools.ttLib.tables.otTables.LookupList)
552def subset_lookups (self, lookup_indices):
553 self.Lookup = [self.Lookup[i] for i in lookup_indices]
554 self.LookupCount = len (self.Lookup)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400555 for l in self.Lookup:
556 l.subset_lookups (lookup_indices)
557
558@add_method(fontTools.ttLib.tables.otTables.LookupList)
559def closure_lookups (self, lookup_indices):
Behdad Esfahbodbb7e2132013-07-23 13:48:35 -0400560 lookup_indices = unique_sorted (lookup_indices)
561 recurse = lookup_indices
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400562 while True:
Behdad Esfahbodbb7e2132013-07-23 13:48:35 -0400563 recurse_lookups = sum ((self.Lookup[i].collect_lookups () for i in recurse), [])
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400564 recurse_lookups = [l for l in recurse_lookups if l not in lookup_indices]
565 if not recurse_lookups:
Behdad Esfahbodbb7e2132013-07-23 13:48:35 -0400566 return unique_sorted (lookup_indices)
567 recurse_lookups = unique_sorted (recurse_lookups)
568 lookup_indices.extend (recurse_lookups)
569 recurse = recurse_lookups
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400570
571@add_method(fontTools.ttLib.tables.otTables.Feature)
572def subset_lookups (self, lookup_indices):
573 self.LookupListIndex = [l for l in self.LookupListIndex if l in lookup_indices]
574 # Now map them.
575 self.LookupListIndex = [lookup_indices.index (l) for l in self.LookupListIndex]
576 self.LookupCount = len (self.LookupListIndex)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400577 return self.LookupCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400578
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400579@add_method(fontTools.ttLib.tables.otTables.Feature)
580def collect_lookups (self):
581 return self.LookupListIndex[:]
582
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400583@add_method(fontTools.ttLib.tables.otTables.FeatureList)
584def subset_lookups (self, lookup_indices):
585 "Returns the indices of nonempty features."
586 feature_indices = [i for (i,f) in enumerate (self.FeatureRecord) if f.Feature.subset_lookups (lookup_indices)]
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400587 self.subset_features (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400588 return feature_indices
589
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400590@add_method(fontTools.ttLib.tables.otTables.FeatureList)
591def collect_lookups (self, feature_indices):
592 return unique_sorted (sum ((self.FeatureRecord[i].Feature.collect_lookups () for i in feature_indices
593 if i < self.FeatureCount), []))
594
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400595@add_method(fontTools.ttLib.tables.otTables.FeatureList)
596def subset_features (self, feature_indices):
597 self.FeatureRecord = [self.FeatureRecord[i] for i in feature_indices]
598 self.FeatureCount = len (self.FeatureRecord)
599 return bool (self.FeatureCount)
600
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400601@add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys)
602def subset_features (self, feature_indices):
Behdad Esfahbod69ce1502013-07-22 18:00:31 -0400603 if self.ReqFeatureIndex in feature_indices:
604 self.ReqFeatureIndex = feature_indices.index (self.ReqFeatureIndex)
605 else:
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400606 self.ReqFeatureIndex = 65535
607 self.FeatureIndex = [f for f in self.FeatureIndex if f in feature_indices]
Behdad Esfahbod69ce1502013-07-22 18:00:31 -0400608 # Now map them.
609 self.FeatureIndex = [feature_indices.index (f) for f in self.FeatureIndex if f in feature_indices]
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400610 self.FeatureCount = len (self.FeatureIndex)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400611 return bool (self.FeatureCount or self.ReqFeatureIndex != 65535)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400612
613@add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys)
614def collect_features (self):
615 feature_indices = self.FeatureIndex[:]
616 if self.ReqFeatureIndex != 65535:
617 feature_indices.append (self.ReqFeatureIndex)
618 return unique_sorted (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400619
620@add_method(fontTools.ttLib.tables.otTables.Script)
621def subset_features (self, feature_indices):
622 if self.DefaultLangSys and not self.DefaultLangSys.subset_features (feature_indices):
623 self.DefaultLangSys = None
624 self.LangSysRecord = [l for l in self.LangSysRecord if l.LangSys.subset_features (feature_indices)]
625 self.LangSysCount = len (self.LangSysRecord)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400626 return bool (self.LangSysCount or self.DefaultLangSys)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400627
628@add_method(fontTools.ttLib.tables.otTables.Script)
629def collect_features (self):
Behdad Esfahbod2307c8b2013-07-23 11:18:13 -0400630 feature_indices = [l.LangSys.collect_features () for l in self.LangSysRecord]
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400631 if self.DefaultLangSys:
632 feature_indices.append (self.DefaultLangSys.collect_features ())
633 return unique_sorted (sum (feature_indices, []))
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400634
635@add_method(fontTools.ttLib.tables.otTables.ScriptList)
636def subset_features (self, feature_indices):
637 self.ScriptRecord = [s for s in self.ScriptRecord if s.Script.subset_features (feature_indices)]
638 self.ScriptCount = len (self.ScriptRecord)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400639 return bool (self.ScriptCount)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400640
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400641@add_method(fontTools.ttLib.tables.otTables.ScriptList)
642def collect_features (self):
643 return unique_sorted (sum ((s.Script.collect_features () for s in self.ScriptRecord), []))
644
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400645@add_method(fontTools.ttLib.getTableClass('GSUB'))
646def closure_glyphs (self, glyphs):
647 feature_indices = self.table.ScriptList.collect_features ()
648 lookup_indices = self.table.FeatureList.collect_lookups (feature_indices)
649 glyphs = unique_sorted (glyphs)
650 while True:
651 additions = (sum ((self.table.LookupList.Lookup[i].closure_glyphs (glyphs, self) for i in lookup_indices), []))
652 additions = unique_sorted (g for g in additions if g not in glyphs)
653 if not additions:
654 return glyphs
655 glyphs.extend (additions)
656
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400657@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400658def subset_glyphs (self, glyphs):
659 lookup_indices = self.table.LookupList.subset_glyphs (glyphs)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400660 self.subset_lookups (lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400661 self.prune_lookups ()
662 return True
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400663
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400664@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400665def subset_lookups (self, lookup_indices):
666 "Retrains specified lookups, then removes empty features, language systems, and scripts."
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400667 self.table.LookupList.subset_lookups (lookup_indices)
668 feature_indices = self.table.FeatureList.subset_lookups (lookup_indices)
669 self.table.ScriptList.subset_features (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400670
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400671@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
672def prune_lookups (self):
673 "Remove unreferenced lookups"
674 feature_indices = self.table.ScriptList.collect_features ()
675 lookup_indices = self.table.FeatureList.collect_lookups (feature_indices)
676 lookup_indices = self.table.LookupList.closure_lookups (lookup_indices)
677 self.subset_lookups (lookup_indices)
678
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400679@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
680def subset_feature_tags (self, feature_tags):
681 feature_indices = [i for (i,f) in enumerate (self.table.FeatureList.FeatureRecord) if f.FeatureTag in feature_tags]
682 self.table.FeatureList.subset_features (feature_indices)
683 self.table.ScriptList.subset_features (feature_indices)
684
685@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400686def prune_pre_subset (self, options):
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400687 if options['layout-features'] and '*' not in options['layout-features']:
Behdad Esfahboded98c612013-07-23 12:37:41 -0400688 self.subset_feature_tags (options['layout-features'])
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400689 self.prune_lookups ()
690 return True
691
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400692@add_method(fontTools.ttLib.getTableClass('GDEF'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400693def subset_glyphs (self, glyphs):
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400694 table = self.table
695 if table.LigCaretList:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400696 indices = table.LigCaretList.Coverage.subset_glyphs (glyphs)
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400697 table.LigCaretList.LigGlyph = [table.LigCaretList.LigGlyph[i] for i in indices]
698 table.LigCaretList.LigGlyphCount = len (table.LigCaretList.LigGlyph)
699 if not table.LigCaretList.LigGlyphCount:
700 table.LigCaretList = None
701 if table.MarkAttachClassDef:
702 table.MarkAttachClassDef.classDefs = {g:v for g,v in table.MarkAttachClassDef.classDefs.items() if g in glyphs}
703 if not table.MarkAttachClassDef.classDefs:
704 table.MarkAttachClassDef = None
705 if table.GlyphClassDef:
706 table.GlyphClassDef.classDefs = {g:v for g,v in table.GlyphClassDef.classDefs.items() if g in glyphs}
707 if not table.GlyphClassDef.classDefs:
708 table.GlyphClassDef = None
709 if table.AttachList:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400710 indices = table.AttachList.Coverage.subset_glyphs (glyphs)
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400711 table.AttachList.AttachPoint = [table.AttachList.AttachPoint[i] for i in indices]
712 table.AttachList.GlyphCount = len (table.AttachList.AttachPoint)
713 if not table.AttachList.GlyphCount:
714 table.AttachList = None
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400715 return bool (table.LigCaretList or table.MarkAttachClassDef or table.GlyphClassDef or table.AttachList)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400716
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400717@add_method(fontTools.ttLib.getTableClass('kern'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400718def subset_glyphs (self, glyphs):
Behdad Esfahbod5270ec42013-07-22 12:57:02 -0400719 for t in self.kernTables:
720 t.kernTable = {(a,b):v for ((a,b),v) in t.kernTable.items() if a in glyphs and b in glyphs}
721 self.kernTables = [t for t in self.kernTables if t.kernTable]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400722 return bool (self.kernTables)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400723
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400724@add_method(fontTools.ttLib.getTableClass('hmtx'), fontTools.ttLib.getTableClass('vmtx'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400725def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400726 self.metrics = {g:v for g,v in self.metrics.items() if g in glyphs}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400727 return bool (self.metrics)
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400728
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400729@add_method(fontTools.ttLib.getTableClass('hdmx'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400730def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400731 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 -0400732 return bool (self.hdmx)
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400733
Behdad Esfahbode45d6af2013-07-22 15:29:17 -0400734@add_method(fontTools.ttLib.getTableClass('VORG'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400735def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400736 self.VOriginRecords = {g:v for g,v in self.VOriginRecords.items() if g in glyphs}
Behdad Esfahbode45d6af2013-07-22 15:29:17 -0400737 self.numVertOriginYMetrics = len (self.VOriginRecords)
738 return True # Never drop; has default metrics
739
Behdad Esfahbod8c646f62013-07-22 15:06:23 -0400740@add_method(fontTools.ttLib.getTableClass('post'))
Behdad Esfahbod8c486d82013-07-24 13:34:47 -0400741def prune_pre_subset (self, options):
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400742 if not options['glyph-names']:
Behdad Esfahbod42648242013-07-23 12:56:06 -0400743 self.formatType = 3.0
744 return True
745
746@add_method(fontTools.ttLib.getTableClass('post'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400747def subset_glyphs (self, glyphs):
Behdad Esfahbod42648242013-07-23 12:56:06 -0400748 self.extraNames = [] # This seems to do it
Behdad Esfahbodc9dec9d2013-07-23 10:28:47 -0400749 return True
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400750
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400751@add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400752def closure_glyphs (self, glyphs):
753 glyphs = unique_sorted (glyphs)
754 decompose = glyphs
755 # I don't know if component glyphs can be composite themselves.
756 # We handle them anyway.
757 while True:
758 components = []
759 for g in decompose:
Behdad Esfahbodf8c20e42013-07-23 23:13:23 -0400760 if g not in self:
761 continue
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400762 gl = self[g]
763 if gl.isComposite ():
764 for c in gl.components:
765 if c.glyphName not in glyphs:
766 components.append (c.glyphName)
767 components = [c for c in components if c not in glyphs]
768 if not components:
769 return glyphs
770 decompose = unique_sorted (components)
771 glyphs.extend (components)
772
773@add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400774def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400775 self.glyphs = {g:v for g,v in self.glyphs.items() if g in glyphs}
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400776 self.glyphOrder = [g for g in self.glyphOrder if g in glyphs]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400777 return bool (self.glyphs)
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400778
Behdad Esfahboded98c612013-07-23 12:37:41 -0400779@add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400780def prune_post_subset (self, options):
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400781 if not options['hinting']:
Behdad Esfahboded98c612013-07-23 12:37:41 -0400782 for g in self.glyphs.values ():
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400783 g.expand (self)
784 g.program = fontTools.ttLib.tables.ttProgram.Program()
785 g.program.fromBytecode([])
Behdad Esfahboded98c612013-07-23 12:37:41 -0400786 return True
787
Behdad Esfahbod2b677c82013-07-23 13:37:13 -0400788@add_method(fontTools.ttLib.getTableClass('CFF '))
789def subset_glyphs (self, glyphs):
790 assert 0, "unimplemented"
791
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400792@add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400793def prune_pre_subset (self, options):
Behdad Esfahbodde4a15b2013-07-23 13:05:42 -0400794 if not options['legacy-cmap']:
795 # Drop non-Unicode / non-Symbol cmaps
796 self.tables = [t for t in self.tables if t.platformID == 3 and t.platEncID in [0, 1, 10]]
797 if not options['symbol-cmap']:
798 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 -0400799 # TODO Only keep one subtable?
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400800 # For now, drop format=0 which can't be subset_glyphs easily?
801 self.tables = [t for t in self.tables if t.format != 0]
802 return bool (self.tables)
803
804@add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400805def subset_glyphs (self, glyphs):
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400806 for t in self.tables:
Behdad Esfahbod9453a362013-07-22 16:21:24 -0400807 # For reasons I don't understand I need this here
808 # to force decompilation of the cmap format 14.
809 try:
810 getattr (t, "asdf")
811 except AttributeError:
812 pass
Behdad Esfahbodb13d7902013-07-22 16:01:15 -0400813 if t.format == 14:
Behdad Esfahbod9453a362013-07-22 16:21:24 -0400814 # XXX We drop all the default-UVS mappings (g==None)
Behdad Esfahbodb13d7902013-07-22 16:01:15 -0400815 t.uvsDict = {v:[(u,g) for (u,g) in l if g in glyphs] for (v,l) in t.uvsDict.items()}
816 t.uvsDict = {v:l for (v,l) in t.uvsDict.items() if l}
817 else:
818 t.cmap = {u:g for (u,g) in t.cmap.items() if g in glyphs}
819 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 -0400820 # XXX Convert formats when needed
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400821 return bool (self.tables)
822
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400823@add_method(fontTools.ttLib.getTableClass('name'))
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400824def prune_pre_subset (self, options):
Behdad Esfahbod20faeb02013-07-23 13:19:03 -0400825 if '*' not in options['name-IDs']:
826 self.names = [n for n in self.names if n.nameID in options['name-IDs']]
827 if not options['name-legacy']:
828 self.names = [n for n in self.names if n.platformID == 3 and n.platEncID == 1]
829 if '*' not in options['name-languages']:
830 self.names = [n for n in self.names if n.langID in options['name-languages']]
831 return True # Retain even if empty
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400832
Behdad Esfahbod8c646f62013-07-22 15:06:23 -0400833
Behdad Esfahbodbc25f162013-07-23 12:56:54 -0400834drop_tables_default = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', 'PCLT', 'LTSH']
Behdad Esfahbod103a12f2013-07-23 15:08:39 -0400835drop_tables_default += ['Feat', 'Glat', 'Gloc', 'Silf', 'Sill'] # Graphite
Behdad Esfahbod38e852c2013-07-23 16:56:50 -0400836drop_tables_default += ['CBLC', 'CBDT', 'sbix', 'COLR', 'CPAL'] # Color
Behdad Esfahboded98c612013-07-23 12:37:41 -0400837no_subset_tables = ['gasp', 'head', 'hhea', 'maxp', 'vhea', 'OS/2', 'loca', 'name', 'cvt ', 'fpgm', 'prep']
Behdad Esfahbodbc25f162013-07-23 12:56:54 -0400838hinting_tables = ['cvt ', 'fpgm', 'prep', 'hdmx', 'VDMX']
Behdad Esfahbod29df0462013-07-23 11:05:25 -0400839
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400840# Based on HarfBuzz shapers
841layout_features_dict = {
842 # Default shaper
843 'common': ['ccmp', 'liga', 'locl', 'mark', 'mkmk', 'rlig'],
844 'horizontal': ['calt', 'clig', 'curs', 'kern', 'rclt'],
845 'vertical': ['valt', 'vert', 'vkrn', 'vpal', 'vrt2'],
846 'ltr': ['ltra', 'ltrm'],
847 'rtl': ['rtla', 'rtlm'],
848 # Complex shapers
849 'arabic': ['init', 'medi', 'fina', 'isol', 'med2', 'fin2', 'fin3'],
850 'hangul': ['ljmo', 'vjmo', 'tjmo'],
851 'tibetal': ['abvs', 'blws', 'abvm', 'blwm'],
852 'indic': ['nukt', 'akhn', 'rphf', 'rkrf', 'pref', 'blwf', 'half', 'abvf', 'pstf', 'cfar', 'vatu', 'cjct',
853 'init', 'pres', 'abvs', 'blws', 'psts', 'haln', 'dist', 'abvm', 'blwm'],
854}
855layout_features_all = unique_sorted (sum (layout_features_dict.values (), []))
856
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400857options_default = {
Behdad Esfahbodde4a15b2013-07-23 13:05:42 -0400858 'drop-tables': drop_tables_default,
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400859 'layout-features': layout_features_all,
860 'hinting': False,
861 'glyph-names': False,
Behdad Esfahbodde4a15b2013-07-23 13:05:42 -0400862 'legacy-cmap': False,
863 'symbol-cmap': False,
Behdad Esfahbod20faeb02013-07-23 13:19:03 -0400864 'name-IDs': [1, 2], # Family and Style
865 'name-legacy': False,
866 'name-languages': [0x0409], # English
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400867 'mandatory-glyphs': True, # First four for TrueType, .notdef for CFF
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400868}
869
Behdad Esfahbod29df0462013-07-23 11:05:25 -0400870
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400871# TODO OS/2 ulUnicodeRange / ulCodePageRange?
Behdad Esfahbodf71267b2013-07-23 12:59:13 -0400872# TODO Drop unneeded GSUB/GPOS Script/LangSys entries
Behdad Esfahbod398d3892013-07-23 15:29:40 -0400873# TODO Avoid recursing too much
Behdad Esfahbode94aa0e2013-07-23 13:22:04 -0400874# TODO Text direction considerations
875# TODO Text script / language considerations
Behdad Esfahbod38e852c2013-07-23 16:56:50 -0400876# TODO Drop unknown tables
Behdad Esfahbod56ebd042013-07-22 13:02:24 -0400877
Behdad Esfahbod8c486d82013-07-24 13:34:47 -0400878
879def main ():
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400880
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400881 import sys, time
Behdad Esfahbod8c486d82013-07-24 13:34:47 -0400882 global last_time
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400883
884 start_time = time.time ()
885 last_time = start_time
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400886
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400887 verbose = False
888 if "--verbose" in sys.argv:
889 verbose = True
890 sys.argv.remove ("--verbose")
Behdad Esfahbod350a5272013-07-22 12:02:16 -0400891 xml = False
892 if "--xml" in sys.argv:
893 xml = True
894 sys.argv.remove ("--xml")
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400895 timing = False
896 if "--timing" in sys.argv:
897 timing = True
898 sys.argv.remove ("--timing")
899
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400900 options = options_default.copy ()
901
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400902 def lapse (what):
903 if not timing:
904 return
905 global last_time
906 new_time = time.time ()
907 print "Took %0.3fs to %s" % (new_time - last_time, what)
908 last_time = new_time
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400909
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400910 if len (sys.argv) < 3:
911 print >>sys.stderr, "usage: pyotlss.py font-file glyph..."
912 sys.exit (1)
913
914 fontfile = sys.argv[1]
915 glyphs = sys.argv[2:]
916
917 font = fontTools.ttx.TTFont (fontfile)
Behdad Esfahbod0f86ce92013-07-22 17:30:31 -0400918 font.disassembleInstructions = False
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400919 lapse ("load font")
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400920
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400921 if options["mandatory-glyphs"]:
922 # Always include .notdef; anything else?
923 if 'glyf' in font:
924 glyphs.extend (['gid0', 'gid1', 'gid2', 'gid3'])
925 if verbose:
926 print "Added first four glyphs to subset"
927 else:
928 glyphs.append ('.notdef')
929 if verbose:
930 print "Added .notdef glyph to subset"
931
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400932 names = font.getGlyphNames()
Behdad Esfahbod9bd59c42013-07-23 21:19:49 -0400933 lapse ("loading glyph names")
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400934 # Convert to glyph names
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400935 glyph_names = []
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400936 cmap_tables = None
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400937 for g in glyphs:
938 if g in names:
939 glyph_names.append (g)
940 continue
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400941 if g.startswith ('uni') and len (g) > 3:
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400942 if not cmap_tables:
943 cmap = font['cmap']
944 cmap_tables = [t for t in cmap.tables if t.platformID == 3 and t.platEncID in [1, 10]]
945 del cmap
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400946 found = False
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400947 u = int (g[3:], 16)
948 for table in cmap_tables:
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400949 if u in table.cmap:
950 glyph_names.append (table.cmap[u])
951 found = True
952 break
953 if not found:
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400954 if verbose:
955 print ("No glyph for Unicode value %s; skipping." % g)
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400956 continue
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400957 if g.startswith ('gid') or g.startswith ('glyph'):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400958 if g.startswith ('gid') and len (g) > 3:
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400959 g = g[3:]
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400960 elif g.startswith ('glyph') and len (g) > 5:
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400961 g = g[5:]
962 try:
963 glyph_names.append (font.getGlyphName (int (g), requireReal=1))
964 except ValueError:
965 raise Exception ("Invalid glyph identifier %s" % g)
966 continue
967 raise Exception ("Invalid glyph identifier %s" % g)
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400968 del cmap_tables
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400969 glyphs = unique_sorted (glyph_names)
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400970 del glyph_names
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400971 lapse ("compile glyph list")
Behdad Esfahbod240d7e72013-07-23 23:12:06 -0400972 if verbose:
973 print "Glyphs:", glyphs
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400974
Behdad Esfahbodde71dca2013-07-24 12:40:54 -0400975
976 for tag in font.keys():
977 if tag == 'GlyphOrder': continue
978
979 if tag in options['drop-tables'] or \
980 (not options['hinting'] and tag in hinting_tables):
981 if verbose:
982 print tag, "dropped."
983 del font[tag]
984 continue
985
986 clazz = fontTools.ttLib.getTableClass(tag)
987
988 if hasattr (clazz, 'prune_pre_subset'):
989 table = font[tag]
990 retain = table.prune_pre_subset (options)
991 lapse ("prune '%s'" % tag)
992 if not retain:
993 if verbose:
994 print tag, "pruned to empty; dropped."
995 del font[tag]
996 continue
997 else:
998 if verbose:
999 print tag, "pruned."
1000
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001001 glyphs_requested = glyphs
1002 if 'GSUB' in font:
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001003 if verbose:
Behdad Esfahbode30ed122013-07-23 21:08:29 -04001004 print "Closing glyph list over 'GSUB': %d glyphs before" % len (glyphs)
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001005 glyphs = font['GSUB'].closure_glyphs (glyphs)
1006 if verbose:
Behdad Esfahbode30ed122013-07-23 21:08:29 -04001007 print "Closed glyph list over 'GSUB': %d glyphs after" % len (glyphs)
Behdad Esfahbod240d7e72013-07-23 23:12:06 -04001008 print "Glyphs:", glyphs
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001009 lapse ("close glyph list over 'GSUB'")
1010 glyphs_gsubed = glyphs
1011
Behdad Esfahbod2a784ac2013-07-22 17:00:36 -04001012 # Close over composite glyphs
1013 if 'glyf' in font:
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001014 if verbose:
Behdad Esfahbode30ed122013-07-23 21:08:29 -04001015 print "Closing glyph list over 'glyf': %d glyphs before" % len (glyphs)
Behdad Esfahbod240d7e72013-07-23 23:12:06 -04001016 print "Glyphs:", glyphs
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001017 glyphs = font['glyf'].closure_glyphs (glyphs)
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001018 if verbose:
Behdad Esfahbode30ed122013-07-23 21:08:29 -04001019 print "Closed glyph list over 'glyf': %d glyphs after" % len (glyphs)
Behdad Esfahbod240d7e72013-07-23 23:12:06 -04001020 print "Glyphs:", glyphs
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001021 lapse ("close glyph list over 'glyf'")
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001022 else:
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001023 glyphs = glyphs
1024 glyphs_glyfed = glyphs
1025 glyphs_closed = glyphs
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001026 del glyphs
1027
Behdad Esfahbodc9dec9d2013-07-23 10:28:47 -04001028 if verbose:
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001029 print "Retaining %d glyphs: " % len (glyphs_closed)
Behdad Esfahbod2a784ac2013-07-22 17:00:36 -04001030
Behdad Esfahbod8842ce22013-07-22 13:01:33 -04001031 for tag in font.keys():
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04001032 if tag == 'GlyphOrder': continue
Behdad Esfahbod4e214e42013-07-22 13:13:49 -04001033
Behdad Esfahbod8842ce22013-07-22 13:01:33 -04001034 clazz = fontTools.ttLib.getTableClass(tag)
Behdad Esfahbod4e214e42013-07-22 13:13:49 -04001035
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001036 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 Esfahbodde71dca2013-07-24 12:40:54 -04001082 font.save (fontfile + '.subset')
1083 lapse ("compile and save font")
1084
1085 last_time = start_time
1086 lapse ("make one with everything (TOTAL TIME)")
1087
Behdad Esfahbod0f86ce92013-07-22 17:30:31 -04001088 if xml:
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04001089 import xmlWriter
1090 writer = xmlWriter.XMLWriter (sys.stdout)
1091
Behdad Esfahbod0f86ce92013-07-22 17:30:31 -04001092 for tag in font.keys():
1093 writer.begintag (tag)
1094 writer.newline ()
1095 font[tag].toXML(writer, font)
1096 writer.endtag (tag)
1097 writer.newline ()
Behdad Esfahbod8c486d82013-07-24 13:34:47 -04001098
1099if __name__ == '__main__':
1100 main ()