blob: 450e0050f1ff9b3cc0ec273a67f42cbced5522c4 [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:
Behdad Esfahbodc0d59592013-07-24 14:41:47 -040034 assert clazz.__name__ != 'DefaultTable', 'Oops, table class not found.'
Behdad Esfahbod02b92062013-07-21 18:40:59 -040035 setattr (clazz, method.func_name, method)
Behdad Esfahbod54660612013-07-21 18:16:55 -040036 return wrapper
37
Behdad Esfahbod78661bb2013-07-23 10:23:42 -040038def unique_sorted (l):
39 return sorted ({v:1 for v in l}.keys ())
40
41
Behdad Esfahbod54660612013-07-21 18:16:55 -040042@add_method(fontTools.ttLib.tables.otTables.Coverage)
Behdad Esfahbod610b0552013-07-23 14:52:18 -040043def intersect_glyphs (self, glyphs):
44 "Returns ascending list of matching coverage values."
45 return [i for (i,g) in enumerate (self.glyphs) if g in glyphs]
46
47@add_method(fontTools.ttLib.tables.otTables.Coverage)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -040048def subset_glyphs (self, glyphs):
Behdad Esfahbodd821ea02013-07-23 10:50:43 -040049 "Returns ascending list of remaining coverage values."
Behdad Esfahbod610b0552013-07-23 14:52:18 -040050 indices = self.intersect_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -040051 self.glyphs = [g for g in self.glyphs if g in glyphs]
52 return indices
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040053
Behdad Esfahbod54660612013-07-21 18:16:55 -040054@add_method(fontTools.ttLib.tables.otTables.ClassDef)
Behdad Esfahbodb8d55882013-07-23 22:17:39 -040055def intersect_glyphs (self, glyphs):
56 "Returns ascending list of matching class values."
57 return unique_sorted (v for g,v in self.classDefs.items() if g in glyphs)
58
59@add_method(fontTools.ttLib.tables.otTables.ClassDef)
60def intersects_glyphs_class (self, glyphs, klass):
61 "Returns true if any of glyphs has requested class."
62 return any (g in glyphs for g,v in self.classDefs.items() if v == klass)
63
64@add_method(fontTools.ttLib.tables.otTables.ClassDef)
Behdad Esfahbodde71dca2013-07-24 12:40:54 -040065def subset_glyphs (self, glyphs, remap=False):
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -040066 "Returns ascending list of remaining classes."
Behdad Esfahbod54660612013-07-21 18:16:55 -040067 self.classDefs = {g:v for g,v in self.classDefs.items() if g in glyphs}
Behdad Esfahbodde71dca2013-07-24 12:40:54 -040068 indices = unique_sorted (self.classDefs.values ())
69 if remap:
70 self.remap (indices)
71 return indices
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -040072
73@add_method(fontTools.ttLib.tables.otTables.ClassDef)
74def remap (self, class_map):
75 "Remaps classes."
76 self.classDefs = {g:class_map.index (v) for g,v in self.classDefs.items()}
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040077
Behdad Esfahbod54660612013-07-21 18:16:55 -040078@add_method(fontTools.ttLib.tables.otTables.SingleSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -040079def closure_glyphs (self, glyphs, table):
80 if self.Format in [1, 2]:
81 return [v for g,v in self.mapping.items() if g in glyphs]
82 else:
83 assert 0, "unknown format: %s" % self.Format
84
85@add_method(fontTools.ttLib.tables.otTables.SingleSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -040086def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -040087 if self.Format in [1, 2]:
88 self.mapping = {g:v for g,v in self.mapping.items() if g in glyphs}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -040089 return bool (self.mapping)
Behdad Esfahbod54660612013-07-21 18:16:55 -040090 else:
91 assert 0, "unknown format: %s" % self.Format
92
93@add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -040094def closure_glyphs (self, glyphs, table):
95 if self.Format == 1:
96 indices = self.Coverage.intersect_glyphs (glyphs)
97 return sum ((self.Sequence[i].Substitute for i in indices), [])
98 else:
99 assert 0, "unknown format: %s" % self.Format
100
101@add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400102def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400103 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400104 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400105 self.Sequence = [self.Sequence[i] for i in indices]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400106 self.SequenceCount = len (self.Sequence)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400107 return bool (self.SequenceCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400108 else:
109 assert 0, "unknown format: %s" % self.Format
110
111@add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400112def closure_glyphs (self, glyphs, table):
113 if self.Format == 1:
114 return sum ((v for g,v in self.alternates.items() if g in glyphs), [])
115 else:
116 assert 0, "unknown format: %s" % self.Format
117
118@add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400119def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400120 if self.Format == 1:
121 self.alternates = {g:v for g,v in self.alternates.items() if g in glyphs}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400122 return bool (self.alternates)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400123 else:
124 assert 0, "unknown format: %s" % self.Format
125
126@add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400127def closure_glyphs (self, glyphs, table):
128 if self.Format == 1:
129 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 -0400130 for g,seqs in self.ligatures.items() if g in glyphs), [])
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400131 else:
132 assert 0, "unknown format: %s" % self.Format
133
134@add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400135def subset_glyphs (self, glyphs):
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400136 if self.Format == 1:
137 self.ligatures = {g:v for g,v in self.ligatures.items() if g in glyphs}
138 self.ligatures = {g:[seq for seq in seqs if all(c in glyphs for c in seq.Component)]
139 for g,seqs in self.ligatures.items()}
140 self.ligatures = {g:v for g,v in self.ligatures.items() if v}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400141 return bool (self.ligatures)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400142 else:
143 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400144
Behdad Esfahbod54660612013-07-21 18:16:55 -0400145@add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400146def closure_glyphs (self, glyphs, table):
147 if self.Format == 1:
148 indices = self.Coverage.intersect_glyphs (glyphs)
149 if not indices or \
150 not all (c.intersect_glyphs (glyphs) for c in self.LookAheadCoverage + self.BacktrackCoverage):
151 return []
152 return [self.Substitute[i] for i in indices]
153 else:
154 assert 0, "unknown format: %s" % self.Format
155
156@add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400157def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400158 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400159 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400160 self.Substitute = [self.Substitute[i] for i in indices]
161 self.GlyphCount = len (self.Substitute)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400162 return bool (self.GlyphCount and all (c.subset_glyphs (glyphs) for c in self.LookAheadCoverage + self.BacktrackCoverage))
Behdad Esfahbod54660612013-07-21 18:16:55 -0400163 else:
164 assert 0, "unknown format: %s" % self.Format
165
166@add_method(fontTools.ttLib.tables.otTables.SinglePos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400167def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400168 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400169 return len (self.Coverage.subset_glyphs (glyphs))
Behdad Esfahbod54660612013-07-21 18:16:55 -0400170 elif self.Format == 2:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400171 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400172 self.Value = [self.Value[i] for i in indices]
173 self.ValueCount = len (self.Value)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400174 return bool (self.ValueCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400175 else:
176 assert 0, "unknown format: %s" % self.Format
177
178@add_method(fontTools.ttLib.tables.otTables.PairPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400179def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400180 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400181 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400182 self.PairSet = [self.PairSet[i] for i in indices]
183 for p in self.PairSet:
184 p.PairValueRecord = [r for r in p.PairValueRecord if r.SecondGlyph in glyphs]
185 p.PairValueCount = len (p.PairValueRecord)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400186 self.PairSet = [p for p in self.PairSet if p.PairValueCount]
Behdad Esfahbod54660612013-07-21 18:16:55 -0400187 self.PairSetCount = len (self.PairSet)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400188 return bool (self.PairSetCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400189 elif self.Format == 2:
Behdad Esfahbodde71dca2013-07-24 12:40:54 -0400190 class1_map = self.ClassDef1.subset_glyphs (glyphs, remap=True)
191 class2_map = self.ClassDef2.subset_glyphs (glyphs, remap=True)
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -0400192 self.Class1Record = [self.Class1Record[i] for i in class1_map]
193 for c in self.Class1Record:
194 c.Class2Record = [c.Class2Record[i] for i in class2_map]
195 self.Class1Count = len (class1_map)
196 self.Class2Count = len (class2_map)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400197 return bool (self.Class1Count and self.Class2Count and self.Coverage.subset_glyphs (glyphs))
Behdad Esfahbod54660612013-07-21 18:16:55 -0400198 else:
199 assert 0, "unknown format: %s" % self.Format
200
201@add_method(fontTools.ttLib.tables.otTables.CursivePos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400202def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400203 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400204 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400205 self.EntryExitRecord = [self.EntryExitRecord[i] for i in indices]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400206 self.EntryExitCount = len (self.EntryExitRecord)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400207 return bool (self.EntryExitCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400208 else:
209 assert 0, "unknown format: %s" % self.Format
210
211@add_method(fontTools.ttLib.tables.otTables.MarkBasePos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400212def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400213 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400214 mark_indices = self.MarkCoverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400215 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices]
216 self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400217 base_indices = self.BaseCoverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400218 self.BaseArray.BaseRecord = [self.BaseArray.BaseRecord[i] for i in base_indices]
219 self.BaseArray.BaseCount = len (self.BaseArray.BaseRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400220 # Prune empty classes
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400221 class_indices = unique_sorted (v.Class for v in self.MarkArray.MarkRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400222 self.ClassCount = len (class_indices)
223 for m in self.MarkArray.MarkRecord:
224 m.Class = class_indices.index (m.Class)
225 for b in self.BaseArray.BaseRecord:
226 b.BaseAnchor = [b.BaseAnchor[i] for i in class_indices]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400227 return bool (self.ClassCount and self.MarkArray.MarkCount and self.BaseArray.BaseCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400228 else:
229 assert 0, "unknown format: %s" % self.Format
230
231@add_method(fontTools.ttLib.tables.otTables.MarkLigPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400232def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400233 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400234 mark_indices = self.MarkCoverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400235 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices]
236 self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400237 ligature_indices = self.LigatureCoverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400238 self.LigatureArray.LigatureAttach = [self.LigatureArray.LigatureAttach[i] for i in ligature_indices]
239 self.LigatureArray.LigatureCount = len (self.LigatureArray.LigatureAttach)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400240 # Prune empty classes
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400241 class_indices = unique_sorted (v.Class for v in self.MarkArray.MarkRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400242 self.ClassCount = len (class_indices)
243 for m in self.MarkArray.MarkRecord:
244 m.Class = class_indices.index (m.Class)
245 for l in self.LigatureArray.LigatureAttach:
246 for c in l.ComponentRecord:
247 c.LigatureAnchor = [c.LigatureAnchor[i] for i in class_indices]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400248 return bool (self.ClassCount and self.MarkArray.MarkCount and self.LigatureArray.LigatureCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400249 else:
250 assert 0, "unknown format: %s" % self.Format
251
252@add_method(fontTools.ttLib.tables.otTables.MarkMarkPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400253def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400254 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400255 mark1_indices = self.Mark1Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400256 self.Mark1Array.MarkRecord = [self.Mark1Array.MarkRecord[i] for i in mark1_indices]
257 self.Mark1Array.MarkCount = len (self.Mark1Array.MarkRecord)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400258 mark2_indices = self.Mark2Coverage.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400259 self.Mark2Array.Mark2Record = [self.Mark2Array.Mark2Record[i] for i in mark2_indices]
260 self.Mark2Array.MarkCount = len (self.Mark2Array.Mark2Record)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400261 # Prune empty classes
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400262 class_indices = unique_sorted (v.Class for v in self.Mark1Array.MarkRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400263 self.ClassCount = len (class_indices)
264 for m in self.Mark1Array.MarkRecord:
265 m.Class = class_indices.index (m.Class)
266 for b in self.Mark2Array.Mark2Record:
267 b.Mark2Anchor = [b.Mark2Anchor[i] for i in class_indices]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400268 return bool (self.ClassCount and self.Mark1Array.MarkCount and self.Mark2Array.MarkCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400269 else:
270 assert 0, "unknown format: %s" % self.Format
271
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400272@add_method(fontTools.ttLib.tables.otTables.SingleSubst,
273 fontTools.ttLib.tables.otTables.MultipleSubst,
274 fontTools.ttLib.tables.otTables.AlternateSubst,
275 fontTools.ttLib.tables.otTables.LigatureSubst,
276 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
277 fontTools.ttLib.tables.otTables.SinglePos,
278 fontTools.ttLib.tables.otTables.PairPos,
279 fontTools.ttLib.tables.otTables.CursivePos,
280 fontTools.ttLib.tables.otTables.MarkBasePos,
281 fontTools.ttLib.tables.otTables.MarkLigPos,
282 fontTools.ttLib.tables.otTables.MarkMarkPos)
283def subset_lookups (self, lookup_indices):
284 pass
285
286@add_method(fontTools.ttLib.tables.otTables.SingleSubst,
287 fontTools.ttLib.tables.otTables.MultipleSubst,
288 fontTools.ttLib.tables.otTables.AlternateSubst,
289 fontTools.ttLib.tables.otTables.LigatureSubst,
290 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
291 fontTools.ttLib.tables.otTables.SinglePos,
292 fontTools.ttLib.tables.otTables.PairPos,
293 fontTools.ttLib.tables.otTables.CursivePos,
294 fontTools.ttLib.tables.otTables.MarkBasePos,
295 fontTools.ttLib.tables.otTables.MarkLigPos,
296 fontTools.ttLib.tables.otTables.MarkMarkPos)
297def collect_lookups (self):
298 return []
299
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400300@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst,
301 fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos)
302def __classify_context (self):
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400303
304 class ContextHelper:
305 def __init__ (self, klass, Format):
306 if klass.__name__.endswith ('Subst'):
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400307 Typ = 'Sub'
308 Type = 'Subst'
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400309 else:
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400310 Typ = 'Pos'
311 Type = 'Pos'
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400312 if klass.__name__.startswith ('Chain'):
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400313 Chain = 'Chain'
314 else:
315 Chain = ''
316 ChainTyp = Chain+Typ
317
318 self.Typ = Typ
319 self.Type = Type
320 self.Chain = Chain
321 self.ChainTyp = ChainTyp
322
323 self.LookupRecord = Type+'LookupRecord'
324
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400325 if Format == 1:
326 ContextData = None
327 ChainContextData = None
328 RuleData = lambda r: r.Input
329 ChainRuleData = lambda r: r.Backtrack + r.Input + r.LookAhead
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400330 SetRuleData = None
331 ChainSetRuleData = None
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400332 elif Format == 2:
Behdad Esfahbode3f20732013-07-24 11:26:43 -0400333 ContextData = lambda r: (r.ClassDef,)
334 ChainContextData = lambda r: (r.LookAheadClassDef, r.InputClassDef, r.BacktrackClassDef)
335 RuleData = lambda r: (r.Class,)
336 ChainRuleData = lambda r: (r.LookAhead, r.Input, r.Backtrack)
337 def SetRuleData (r, d): (r.Class,) = d
338 def ChainSetRuleData (r, d): (r.LookAhead, r.Input, r.Backtrack) = d
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400339 elif Format == 3:
340 ContextData = None
341 ChainContextData = None
342 RuleData = lambda r: r.Coverage
343 ChainRuleData = lambda r: r.LookAheadCoverage + r.InputCoverage + r.BacktrackCoverage
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400344 SetRuleData = None
345 ChainSetRuleData = None
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400346 else:
347 assert 0, "unknown format: %s" % Format
348
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400349 if Chain:
Behdad Esfahbod707a37a2013-07-23 21:08:26 -0400350 self.ContextData = ChainContextData
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400351 self.RuleData = ChainRuleData
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400352 self.SetRuleData = ChainSetRuleData
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400353 else:
Behdad Esfahbod707a37a2013-07-23 21:08:26 -0400354 self.ContextData = ContextData
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400355 self.RuleData = RuleData
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400356 self.SetRuleData = SetRuleData
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400357
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400358 if Format == 1:
359 self.Rule = ChainTyp+'Rule'
360 self.RuleCount = ChainTyp+'RuleCount'
361 self.RuleSet = ChainTyp+'RuleSet'
362 self.RuleSetCount = ChainTyp+'RuleSetCount'
363 elif Format == 2:
364 self.Rule = ChainTyp+'ClassRule'
365 self.RuleCount = ChainTyp+'ClassRuleCount'
366 self.RuleSet = ChainTyp+'ClassSet'
367 self.RuleSetCount = ChainTyp+'ClassSetCount'
Behdad Esfahbod89987002013-07-23 23:07:42 -0400368
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400369 self.ClassDef = 'InputClassDef' if Chain else 'ClassDef'
Behdad Esfahbod27108392013-07-23 16:40:47 -0400370
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400371 if self.Format not in [1, 2, 3]:
372 return None # Don't shoot the messenger; let it go
373 if not hasattr (self.__class__, "__ContextHelpers"):
374 self.__class__.__ContextHelpers = {}
375 if self.Format not in self.__class__.__ContextHelpers:
376 self.__class__.__ContextHelpers[self.Format] = ContextHelper (self.__class__, self.Format)
377 return self.__class__.__ContextHelpers[self.Format]
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400378
Behdad Esfahbodf2b6d9c2013-07-23 17:31:54 -0400379@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst)
Behdad Esfahbod00776972013-07-23 15:33:00 -0400380def closure_glyphs (self, glyphs, table):
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400381 c = self.__classify_context ()
382
Behdad Esfahbod00776972013-07-23 15:33:00 -0400383 if self.Format == 1:
Behdad Esfahbodeeca9822013-07-23 17:42:17 -0400384 indices = self.Coverage.intersect_glyphs (glyphs)
385 rss = getattr (self, c.RuleSet)
386 return sum ((table.table.LookupList.Lookup[ll.LookupListIndex].closure_glyphs (glyphs, table) \
387 for i in indices \
388 for r in getattr (rss[i], c.Rule) \
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400389 if r and all (g in glyphs for g in c.RuleData (r)) \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400390 for ll in getattr (r, c.LookupRecord) if ll \
Behdad Esfahbodeeca9822013-07-23 17:42:17 -0400391 ), [])
Behdad Esfahbod00776972013-07-23 15:33:00 -0400392 elif self.Format == 2:
Behdad Esfahbod31084302013-07-23 22:22:38 -0400393 if not self.Coverage.intersect_glyphs (glyphs):
394 return []
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400395 indices = getattr (self, c.ClassDef).intersect_glyphs (glyphs)
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400396 rss = getattr (self, c.RuleSet)
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400397 return sum ((table.table.LookupList.Lookup[ll.LookupListIndex].closure_glyphs (glyphs, table) \
398 for i in indices \
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400399 for r in getattr (rss[i], c.Rule) \
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400400 if r and all (cd.intersects_glyphs_class (glyphs, k) \
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400401 for cd,k in zip (c.ContextData (self), c.RuleData (r))) \
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400402 for ll in getattr (r, c.LookupRecord) if ll \
403 ), [])
Behdad Esfahbod00776972013-07-23 15:33:00 -0400404 elif self.Format == 3:
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400405 if not all (x.intersect_glyphs (glyphs) for x in c.RuleData (self)):
Behdad Esfahbod00776972013-07-23 15:33:00 -0400406 return []
407 return sum ((table.table.LookupList.Lookup[ll.LookupListIndex].closure_glyphs (glyphs, table) \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400408 for ll in getattr (self, c.LookupRecord) if ll), [])
Behdad Esfahbod00776972013-07-23 15:33:00 -0400409 else:
410 assert 0, "unknown format: %s" % self.Format
411
Behdad Esfahbodcbba4a62013-07-23 17:27:18 -0400412@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ContextPos,
413 fontTools.ttLib.tables.otTables.ChainContextSubst, fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400414def subset_glyphs (self, glyphs):
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400415 c = self.__classify_context ()
416
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400417 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400418 indices = self.Coverage.subset_glyphs (glyphs)
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400419 rss = getattr (self, c.RuleSet)
420 rss = [rss[i] for i in indices]
421 for rs in rss:
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400422 if rs:
423 ss = getattr (rs, c.Rule)
424 ss = [r for r in ss \
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400425 if r and all (g in glyphs for g in c.RuleData (r))]
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400426 setattr (rs, c.Rule, ss)
427 setattr (rs, c.RuleCount, len (ss))
Behdad Esfahbodcbba4a62013-07-23 17:27:18 -0400428 # Prune empty subrulesets
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400429 rss = [rs for rs in rss if rs and getattr (rs, c.Rule)]
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400430 setattr (self, c.RuleSet, rss)
431 setattr (self, c.RuleSetCount, len (rss))
432 return bool (rss)
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400433 elif self.Format == 2:
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400434 if not self.Coverage.subset_glyphs (glyphs):
435 return False
436 indices = getattr (self, c.ClassDef).intersect_glyphs (glyphs)
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400437 rss = getattr (self, c.RuleSet)
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400438 rss = [rss[i] for i in indices]
439 ContextData = c.ContextData (self)
Behdad Esfahbodde71dca2013-07-24 12:40:54 -0400440 klass_maps = [x.subset_glyphs (glyphs, remap=True) for x in ContextData]
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400441 for rs in rss:
442 if rs:
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400443 ss = getattr (rs, c.Rule)
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400444 ss = [r for r in ss \
445 if r and all (k in klass_map \
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400446 for klass_map,k in zip (klass_maps, c.RuleData (r)))]
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400447 setattr (rs, c.Rule, ss)
448 setattr (rs, c.RuleCount, len (ss))
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400449
450 # Remap rule classes
451 for r in ss:
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400452 c.SetRuleData (r, (klass_map.index (k) \
453 for klassmap,k in zip (klass_maps, c.RuleData (r))))
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400454 # Prune empty subrulesets
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400455 rss = [rs for rs in rss if rs and getattr (rs, c.Rule)]
456 setattr (self, c.RuleSet, rss)
457 setattr (self, c.RuleSetCount, len (rss))
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400458 return bool (rss)
459
460 return all (x.subset_glyphs (glyphs) for x in c.ContextData (self))
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400461 elif self.Format == 3:
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400462 return all (x.subset_glyphs (glyphs) for x in c.RuleData (self))
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400463 else:
464 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400465
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400466@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst,
467 fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400468def subset_lookups (self, lookup_indices):
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400469 c = self.__classify_context ()
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400470
Behdad Esfahbod1f573632013-07-23 23:04:43 -0400471 if self.Format in [1, 2]:
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400472 for rs in getattr (self, c.RuleSet):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400473 if rs:
474 for r in getattr (rs, c.Rule):
475 if r:
Behdad Esfahbod1f573632013-07-23 23:04:43 -0400476 setattr (r, c.LookupRecord, [ll for ll in getattr (r, c.LookupRecord) if ll \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400477 if ll.LookupListIndex in lookup_indices])
478 for ll in getattr (r, c.LookupRecord):
479 if ll:
480 ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400481 elif self.Format == 3:
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400482 setattr (self, c.LookupRecord, [ll for ll in getattr (self, c.LookupRecord) if ll \
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400483 if ll.LookupListIndex in lookup_indices])
484 for ll in getattr (self, c.LookupRecord):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400485 if ll:
486 ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400487 else:
488 assert 0, "unknown format: %s" % self.Format
489
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400490@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst,
491 fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400492def collect_lookups (self):
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400493 c = self.__classify_context ()
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400494
Behdad Esfahbod1f573632013-07-23 23:04:43 -0400495 if self.Format in [1, 2]:
Behdad Esfahbod27108392013-07-23 16:40:47 -0400496 return [ll.LookupListIndex \
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400497 for rs in getattr (self, c.RuleSet) if rs \
498 for r in getattr (rs, c.Rule) if r \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400499 for ll in getattr (r, c.LookupRecord) if ll]
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400500 elif self.Format == 3:
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400501 return [ll.LookupListIndex \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400502 for ll in getattr (self, c.LookupRecord) if ll]
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400503 else:
504 assert 0, "unknown format: %s" % self.Format
505
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400506@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst)
507def closure_glyphs (self, glyphs, table):
508 if self.Format == 1:
509 return self.ExtSubTable.closure_glyphs (glyphs, table)
510 else:
511 assert 0, "unknown format: %s" % self.Format
512
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400513@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400514def subset_glyphs (self, glyphs):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400515 if self.Format == 1:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400516 return self.ExtSubTable.subset_glyphs (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400517 else:
518 assert 0, "unknown format: %s" % self.Format
519
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400520@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
521def subset_lookups (self, lookup_indices):
522 if self.Format == 1:
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400523 return self.ExtSubTable.subset_lookups (lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400524 else:
525 assert 0, "unknown format: %s" % self.Format
526
527@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
528def collect_lookups (self):
529 if self.Format == 1:
530 return self.ExtSubTable.collect_lookups ()
531 else:
532 assert 0, "unknown format: %s" % self.Format
533
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400534@add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400535def closure_glyphs (self, glyphs, table):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400536 return sum ((s.closure_glyphs (glyphs, table) for s in self.SubTable if s), [])
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400537
538@add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400539def subset_glyphs (self, glyphs):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400540 self.SubTable = [s for s in self.SubTable if s and s.subset_glyphs (glyphs)]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400541 self.SubTableCount = len (self.SubTable)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400542 return bool (self.SubTableCount)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400543
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400544@add_method(fontTools.ttLib.tables.otTables.Lookup)
545def subset_lookups (self, lookup_indices):
546 for s in self.SubTable:
547 s.subset_lookups (lookup_indices)
548
549@add_method(fontTools.ttLib.tables.otTables.Lookup)
550def collect_lookups (self):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400551 return unique_sorted (sum ((s.collect_lookups () for s in self.SubTable if s), []))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400552
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400553@add_method(fontTools.ttLib.tables.otTables.LookupList)
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400554def subset_glyphs (self, glyphs):
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400555 "Returns the indices of nonempty lookups."
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400556 return [i for (i,l) in enumerate (self.Lookup) if l and l.subset_glyphs (glyphs)]
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400557
558@add_method(fontTools.ttLib.tables.otTables.LookupList)
559def subset_lookups (self, lookup_indices):
560 self.Lookup = [self.Lookup[i] for i in lookup_indices]
561 self.LookupCount = len (self.Lookup)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400562 for l in self.Lookup:
563 l.subset_lookups (lookup_indices)
564
565@add_method(fontTools.ttLib.tables.otTables.LookupList)
566def closure_lookups (self, lookup_indices):
Behdad Esfahbodbb7e2132013-07-23 13:48:35 -0400567 lookup_indices = unique_sorted (lookup_indices)
568 recurse = lookup_indices
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400569 while True:
Behdad Esfahbodbb7e2132013-07-23 13:48:35 -0400570 recurse_lookups = sum ((self.Lookup[i].collect_lookups () for i in recurse), [])
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400571 recurse_lookups = [l for l in recurse_lookups if l not in lookup_indices]
572 if not recurse_lookups:
Behdad Esfahbodbb7e2132013-07-23 13:48:35 -0400573 return unique_sorted (lookup_indices)
574 recurse_lookups = unique_sorted (recurse_lookups)
575 lookup_indices.extend (recurse_lookups)
576 recurse = recurse_lookups
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400577
578@add_method(fontTools.ttLib.tables.otTables.Feature)
579def subset_lookups (self, lookup_indices):
580 self.LookupListIndex = [l for l in self.LookupListIndex if l in lookup_indices]
581 # Now map them.
582 self.LookupListIndex = [lookup_indices.index (l) for l in self.LookupListIndex]
583 self.LookupCount = len (self.LookupListIndex)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400584 return self.LookupCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400585
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400586@add_method(fontTools.ttLib.tables.otTables.Feature)
587def collect_lookups (self):
588 return self.LookupListIndex[:]
589
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400590@add_method(fontTools.ttLib.tables.otTables.FeatureList)
591def subset_lookups (self, lookup_indices):
592 "Returns the indices of nonempty features."
593 feature_indices = [i for (i,f) in enumerate (self.FeatureRecord) if f.Feature.subset_lookups (lookup_indices)]
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400594 self.subset_features (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400595 return feature_indices
596
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400597@add_method(fontTools.ttLib.tables.otTables.FeatureList)
598def collect_lookups (self, feature_indices):
599 return unique_sorted (sum ((self.FeatureRecord[i].Feature.collect_lookups () for i in feature_indices
600 if i < self.FeatureCount), []))
601
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400602@add_method(fontTools.ttLib.tables.otTables.FeatureList)
603def subset_features (self, feature_indices):
604 self.FeatureRecord = [self.FeatureRecord[i] for i in feature_indices]
605 self.FeatureCount = len (self.FeatureRecord)
606 return bool (self.FeatureCount)
607
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400608@add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys)
609def subset_features (self, feature_indices):
Behdad Esfahbod69ce1502013-07-22 18:00:31 -0400610 if self.ReqFeatureIndex in feature_indices:
611 self.ReqFeatureIndex = feature_indices.index (self.ReqFeatureIndex)
612 else:
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400613 self.ReqFeatureIndex = 65535
614 self.FeatureIndex = [f for f in self.FeatureIndex if f in feature_indices]
Behdad Esfahbod69ce1502013-07-22 18:00:31 -0400615 # Now map them.
616 self.FeatureIndex = [feature_indices.index (f) for f in self.FeatureIndex if f in feature_indices]
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400617 self.FeatureCount = len (self.FeatureIndex)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400618 return bool (self.FeatureCount or self.ReqFeatureIndex != 65535)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400619
620@add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys)
621def collect_features (self):
622 feature_indices = self.FeatureIndex[:]
623 if self.ReqFeatureIndex != 65535:
624 feature_indices.append (self.ReqFeatureIndex)
625 return unique_sorted (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400626
627@add_method(fontTools.ttLib.tables.otTables.Script)
628def subset_features (self, feature_indices):
629 if self.DefaultLangSys and not self.DefaultLangSys.subset_features (feature_indices):
630 self.DefaultLangSys = None
631 self.LangSysRecord = [l for l in self.LangSysRecord if l.LangSys.subset_features (feature_indices)]
632 self.LangSysCount = len (self.LangSysRecord)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400633 return bool (self.LangSysCount or self.DefaultLangSys)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400634
635@add_method(fontTools.ttLib.tables.otTables.Script)
636def collect_features (self):
Behdad Esfahbod2307c8b2013-07-23 11:18:13 -0400637 feature_indices = [l.LangSys.collect_features () for l in self.LangSysRecord]
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400638 if self.DefaultLangSys:
639 feature_indices.append (self.DefaultLangSys.collect_features ())
640 return unique_sorted (sum (feature_indices, []))
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400641
642@add_method(fontTools.ttLib.tables.otTables.ScriptList)
643def subset_features (self, feature_indices):
644 self.ScriptRecord = [s for s in self.ScriptRecord if s.Script.subset_features (feature_indices)]
645 self.ScriptCount = len (self.ScriptRecord)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400646 return bool (self.ScriptCount)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400647
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400648@add_method(fontTools.ttLib.tables.otTables.ScriptList)
649def collect_features (self):
650 return unique_sorted (sum ((s.Script.collect_features () for s in self.ScriptRecord), []))
651
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400652@add_method(fontTools.ttLib.getTableClass('GSUB'))
653def closure_glyphs (self, glyphs):
654 feature_indices = self.table.ScriptList.collect_features ()
655 lookup_indices = self.table.FeatureList.collect_lookups (feature_indices)
656 glyphs = unique_sorted (glyphs)
657 while True:
658 additions = (sum ((self.table.LookupList.Lookup[i].closure_glyphs (glyphs, self) for i in lookup_indices), []))
659 additions = unique_sorted (g for g in additions if g not in glyphs)
660 if not additions:
661 return glyphs
662 glyphs.extend (additions)
663
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400664@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400665def subset_glyphs (self, glyphs):
666 lookup_indices = self.table.LookupList.subset_glyphs (glyphs)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400667 self.subset_lookups (lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400668 self.prune_lookups ()
669 return True
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400670
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400671@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400672def subset_lookups (self, lookup_indices):
673 "Retrains specified lookups, then removes empty features, language systems, and scripts."
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400674 self.table.LookupList.subset_lookups (lookup_indices)
675 feature_indices = self.table.FeatureList.subset_lookups (lookup_indices)
676 self.table.ScriptList.subset_features (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400677
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400678@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
679def prune_lookups (self):
680 "Remove unreferenced lookups"
681 feature_indices = self.table.ScriptList.collect_features ()
682 lookup_indices = self.table.FeatureList.collect_lookups (feature_indices)
683 lookup_indices = self.table.LookupList.closure_lookups (lookup_indices)
684 self.subset_lookups (lookup_indices)
685
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400686@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
687def subset_feature_tags (self, feature_tags):
688 feature_indices = [i for (i,f) in enumerate (self.table.FeatureList.FeatureRecord) if f.FeatureTag in feature_tags]
689 self.table.FeatureList.subset_features (feature_indices)
690 self.table.ScriptList.subset_features (feature_indices)
691
692@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400693def prune_pre_subset (self, options):
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400694 if options['layout-features'] and '*' not in options['layout-features']:
Behdad Esfahboded98c612013-07-23 12:37:41 -0400695 self.subset_feature_tags (options['layout-features'])
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400696 self.prune_lookups ()
697 return True
698
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400699@add_method(fontTools.ttLib.getTableClass('GDEF'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400700def subset_glyphs (self, glyphs):
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400701 table = self.table
702 if table.LigCaretList:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400703 indices = table.LigCaretList.Coverage.subset_glyphs (glyphs)
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400704 table.LigCaretList.LigGlyph = [table.LigCaretList.LigGlyph[i] for i in indices]
705 table.LigCaretList.LigGlyphCount = len (table.LigCaretList.LigGlyph)
706 if not table.LigCaretList.LigGlyphCount:
707 table.LigCaretList = None
708 if table.MarkAttachClassDef:
709 table.MarkAttachClassDef.classDefs = {g:v for g,v in table.MarkAttachClassDef.classDefs.items() if g in glyphs}
710 if not table.MarkAttachClassDef.classDefs:
711 table.MarkAttachClassDef = None
712 if table.GlyphClassDef:
713 table.GlyphClassDef.classDefs = {g:v for g,v in table.GlyphClassDef.classDefs.items() if g in glyphs}
714 if not table.GlyphClassDef.classDefs:
715 table.GlyphClassDef = None
716 if table.AttachList:
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400717 indices = table.AttachList.Coverage.subset_glyphs (glyphs)
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400718 table.AttachList.AttachPoint = [table.AttachList.AttachPoint[i] for i in indices]
719 table.AttachList.GlyphCount = len (table.AttachList.AttachPoint)
720 if not table.AttachList.GlyphCount:
721 table.AttachList = None
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400722 return bool (table.LigCaretList or table.MarkAttachClassDef or table.GlyphClassDef or table.AttachList)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400723
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400724@add_method(fontTools.ttLib.getTableClass('kern'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400725def subset_glyphs (self, glyphs):
Behdad Esfahbod5270ec42013-07-22 12:57:02 -0400726 for t in self.kernTables:
727 t.kernTable = {(a,b):v for ((a,b),v) in t.kernTable.items() if a in glyphs and b in glyphs}
728 self.kernTables = [t for t in self.kernTables if t.kernTable]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400729 return bool (self.kernTables)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400730
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400731@add_method(fontTools.ttLib.getTableClass('hmtx'), fontTools.ttLib.getTableClass('vmtx'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400732def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400733 self.metrics = {g:v for g,v in self.metrics.items() if g in glyphs}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400734 return bool (self.metrics)
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400735
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400736@add_method(fontTools.ttLib.getTableClass('hdmx'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400737def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400738 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 -0400739 return bool (self.hdmx)
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400740
Behdad Esfahbode45d6af2013-07-22 15:29:17 -0400741@add_method(fontTools.ttLib.getTableClass('VORG'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400742def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400743 self.VOriginRecords = {g:v for g,v in self.VOriginRecords.items() if g in glyphs}
Behdad Esfahbode45d6af2013-07-22 15:29:17 -0400744 self.numVertOriginYMetrics = len (self.VOriginRecords)
745 return True # Never drop; has default metrics
746
Behdad Esfahbod8c646f62013-07-22 15:06:23 -0400747@add_method(fontTools.ttLib.getTableClass('post'))
Behdad Esfahbod8c486d82013-07-24 13:34:47 -0400748def prune_pre_subset (self, options):
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400749 if not options['glyph-names']:
Behdad Esfahbod42648242013-07-23 12:56:06 -0400750 self.formatType = 3.0
751 return True
752
753@add_method(fontTools.ttLib.getTableClass('post'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400754def subset_glyphs (self, glyphs):
Behdad Esfahbod42648242013-07-23 12:56:06 -0400755 self.extraNames = [] # This seems to do it
Behdad Esfahbodc9dec9d2013-07-23 10:28:47 -0400756 return True
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400757
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400758@add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400759def closure_glyphs (self, glyphs):
760 glyphs = unique_sorted (glyphs)
761 decompose = glyphs
762 # I don't know if component glyphs can be composite themselves.
763 # We handle them anyway.
764 while True:
765 components = []
766 for g in decompose:
Behdad Esfahbodf8c20e42013-07-23 23:13:23 -0400767 if g not in self:
768 continue
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400769 gl = self[g]
770 if gl.isComposite ():
771 for c in gl.components:
772 if c.glyphName not in glyphs:
773 components.append (c.glyphName)
774 components = [c for c in components if c not in glyphs]
775 if not components:
776 return glyphs
777 decompose = unique_sorted (components)
778 glyphs.extend (components)
779
780@add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400781def subset_glyphs (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400782 self.glyphs = {g:v for g,v in self.glyphs.items() if g in glyphs}
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400783 self.glyphOrder = [g for g in self.glyphOrder if g in glyphs]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400784 return bool (self.glyphs)
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400785
Behdad Esfahboded98c612013-07-23 12:37:41 -0400786@add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400787def prune_post_subset (self, options):
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400788 if not options['hinting']:
Behdad Esfahboded98c612013-07-23 12:37:41 -0400789 for g in self.glyphs.values ():
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400790 g.expand (self)
791 g.program = fontTools.ttLib.tables.ttProgram.Program()
792 g.program.fromBytecode([])
Behdad Esfahboded98c612013-07-23 12:37:41 -0400793 return True
794
Behdad Esfahbod2b677c82013-07-23 13:37:13 -0400795@add_method(fontTools.ttLib.getTableClass('CFF '))
796def subset_glyphs (self, glyphs):
797 assert 0, "unimplemented"
798
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400799@add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400800def prune_pre_subset (self, options):
Behdad Esfahbodde4a15b2013-07-23 13:05:42 -0400801 if not options['legacy-cmap']:
802 # Drop non-Unicode / non-Symbol cmaps
803 self.tables = [t for t in self.tables if t.platformID == 3 and t.platEncID in [0, 1, 10]]
804 if not options['symbol-cmap']:
805 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 -0400806 # TODO Only keep one subtable?
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400807 # For now, drop format=0 which can't be subset_glyphs easily?
808 self.tables = [t for t in self.tables if t.format != 0]
809 return bool (self.tables)
810
811@add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbod0716eb42013-07-23 10:47:03 -0400812def subset_glyphs (self, glyphs):
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400813 for t in self.tables:
Behdad Esfahbod9453a362013-07-22 16:21:24 -0400814 # For reasons I don't understand I need this here
815 # to force decompilation of the cmap format 14.
816 try:
817 getattr (t, "asdf")
818 except AttributeError:
819 pass
Behdad Esfahbodb13d7902013-07-22 16:01:15 -0400820 if t.format == 14:
Behdad Esfahbod9453a362013-07-22 16:21:24 -0400821 # XXX We drop all the default-UVS mappings (g==None)
Behdad Esfahbodb13d7902013-07-22 16:01:15 -0400822 t.uvsDict = {v:[(u,g) for (u,g) in l if g in glyphs] for (v,l) in t.uvsDict.items()}
823 t.uvsDict = {v:l for (v,l) in t.uvsDict.items() if l}
824 else:
825 t.cmap = {u:g for (u,g) in t.cmap.items() if g in glyphs}
826 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 -0400827 # XXX Convert formats when needed
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400828 return bool (self.tables)
829
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400830@add_method(fontTools.ttLib.getTableClass('name'))
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400831def prune_pre_subset (self, options):
Behdad Esfahbod20faeb02013-07-23 13:19:03 -0400832 if '*' not in options['name-IDs']:
833 self.names = [n for n in self.names if n.nameID in options['name-IDs']]
834 if not options['name-legacy']:
835 self.names = [n for n in self.names if n.platformID == 3 and n.platEncID == 1]
836 if '*' not in options['name-languages']:
837 self.names = [n for n in self.names if n.langID in options['name-languages']]
838 return True # Retain even if empty
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400839
Behdad Esfahbod8c646f62013-07-22 15:06:23 -0400840
Behdad Esfahbodbc25f162013-07-23 12:56:54 -0400841drop_tables_default = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', 'PCLT', 'LTSH']
Behdad Esfahbod103a12f2013-07-23 15:08:39 -0400842drop_tables_default += ['Feat', 'Glat', 'Gloc', 'Silf', 'Sill'] # Graphite
Behdad Esfahbod38e852c2013-07-23 16:56:50 -0400843drop_tables_default += ['CBLC', 'CBDT', 'sbix', 'COLR', 'CPAL'] # Color
Behdad Esfahboded98c612013-07-23 12:37:41 -0400844no_subset_tables = ['gasp', 'head', 'hhea', 'maxp', 'vhea', 'OS/2', 'loca', 'name', 'cvt ', 'fpgm', 'prep']
Behdad Esfahbodbc25f162013-07-23 12:56:54 -0400845hinting_tables = ['cvt ', 'fpgm', 'prep', 'hdmx', 'VDMX']
Behdad Esfahbod29df0462013-07-23 11:05:25 -0400846
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400847# Based on HarfBuzz shapers
848layout_features_dict = {
849 # Default shaper
850 'common': ['ccmp', 'liga', 'locl', 'mark', 'mkmk', 'rlig'],
851 'horizontal': ['calt', 'clig', 'curs', 'kern', 'rclt'],
852 'vertical': ['valt', 'vert', 'vkrn', 'vpal', 'vrt2'],
853 'ltr': ['ltra', 'ltrm'],
854 'rtl': ['rtla', 'rtlm'],
855 # Complex shapers
856 'arabic': ['init', 'medi', 'fina', 'isol', 'med2', 'fin2', 'fin3'],
857 'hangul': ['ljmo', 'vjmo', 'tjmo'],
858 'tibetal': ['abvs', 'blws', 'abvm', 'blwm'],
859 'indic': ['nukt', 'akhn', 'rphf', 'rkrf', 'pref', 'blwf', 'half', 'abvf', 'pstf', 'cfar', 'vatu', 'cjct',
860 'init', 'pres', 'abvs', 'blws', 'psts', 'haln', 'dist', 'abvm', 'blwm'],
861}
862layout_features_all = unique_sorted (sum (layout_features_dict.values (), []))
863
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400864options_default = {
Behdad Esfahbodde4a15b2013-07-23 13:05:42 -0400865 'drop-tables': drop_tables_default,
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400866 'layout-features': layout_features_all,
867 'hinting': False,
868 'glyph-names': False,
Behdad Esfahbodde4a15b2013-07-23 13:05:42 -0400869 'legacy-cmap': False,
870 'symbol-cmap': False,
Behdad Esfahbod20faeb02013-07-23 13:19:03 -0400871 'name-IDs': [1, 2], # Family and Style
872 'name-legacy': False,
873 'name-languages': [0x0409], # English
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400874 'mandatory-glyphs': True, # First four for TrueType, .notdef for CFF
Behdad Esfahbod4091ec62013-07-23 13:02:51 -0400875}
876
Behdad Esfahbod29df0462013-07-23 11:05:25 -0400877
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400878# TODO OS/2 ulUnicodeRange / ulCodePageRange?
Behdad Esfahbodf71267b2013-07-23 12:59:13 -0400879# TODO Drop unneeded GSUB/GPOS Script/LangSys entries
Behdad Esfahbod398d3892013-07-23 15:29:40 -0400880# TODO Avoid recursing too much
Behdad Esfahbode94aa0e2013-07-23 13:22:04 -0400881# TODO Text direction considerations
882# TODO Text script / language considerations
Behdad Esfahbod38e852c2013-07-23 16:56:50 -0400883# TODO Drop unknown tables
Behdad Esfahbod56ebd042013-07-22 13:02:24 -0400884
Behdad Esfahbod8c486d82013-07-24 13:34:47 -0400885
886def main ():
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400887
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400888 import sys, time
Behdad Esfahbod8c486d82013-07-24 13:34:47 -0400889 global last_time
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400890
891 start_time = time.time ()
892 last_time = start_time
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400893
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400894 verbose = False
895 if "--verbose" in sys.argv:
896 verbose = True
897 sys.argv.remove ("--verbose")
Behdad Esfahbod350a5272013-07-22 12:02:16 -0400898 xml = False
899 if "--xml" in sys.argv:
900 xml = True
901 sys.argv.remove ("--xml")
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400902 timing = False
903 if "--timing" in sys.argv:
904 timing = True
905 sys.argv.remove ("--timing")
906
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400907 options = options_default.copy ()
908
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400909 def lapse (what):
910 if not timing:
911 return
912 global last_time
913 new_time = time.time ()
914 print "Took %0.3fs to %s" % (new_time - last_time, what)
915 last_time = new_time
Behdad Esfahbod4ae81712013-07-22 11:57:13 -0400916
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400917 if len (sys.argv) < 3:
918 print >>sys.stderr, "usage: pyotlss.py font-file glyph..."
919 sys.exit (1)
920
921 fontfile = sys.argv[1]
922 glyphs = sys.argv[2:]
923
924 font = fontTools.ttx.TTFont (fontfile)
Behdad Esfahbod0f86ce92013-07-22 17:30:31 -0400925 font.disassembleInstructions = False
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400926 lapse ("load font")
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400927
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400928 if options["mandatory-glyphs"]:
929 # Always include .notdef; anything else?
930 if 'glyf' in font:
931 glyphs.extend (['gid0', 'gid1', 'gid2', 'gid3'])
932 if verbose:
933 print "Added first four glyphs to subset"
934 else:
935 glyphs.append ('.notdef')
936 if verbose:
937 print "Added .notdef glyph to subset"
938
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400939 names = font.getGlyphNames()
Behdad Esfahbod9bd59c42013-07-23 21:19:49 -0400940 lapse ("loading glyph names")
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400941 # Convert to glyph names
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400942 glyph_names = []
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400943 cmap_tables = None
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400944 for g in glyphs:
945 if g in names:
946 glyph_names.append (g)
947 continue
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400948 if g.startswith ('uni') and len (g) > 3:
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400949 if not cmap_tables:
950 cmap = font['cmap']
951 cmap_tables = [t for t in cmap.tables if t.platformID == 3 and t.platEncID in [1, 10]]
952 del cmap
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400953 found = False
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400954 u = int (g[3:], 16)
955 for table in cmap_tables:
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400956 if u in table.cmap:
957 glyph_names.append (table.cmap[u])
958 found = True
959 break
960 if not found:
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400961 if verbose:
962 print ("No glyph for Unicode value %s; skipping." % g)
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400963 continue
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400964 if g.startswith ('gid') or g.startswith ('glyph'):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400965 if g.startswith ('gid') and len (g) > 3:
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400966 g = g[3:]
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400967 elif g.startswith ('glyph') and len (g) > 5:
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400968 g = g[5:]
969 try:
970 glyph_names.append (font.getGlyphName (int (g), requireReal=1))
971 except ValueError:
972 raise Exception ("Invalid glyph identifier %s" % g)
973 continue
974 raise Exception ("Invalid glyph identifier %s" % g)
Behdad Esfahbode36061e2013-07-23 15:13:00 -0400975 del cmap_tables
Behdad Esfahbode30ed122013-07-23 21:08:29 -0400976 glyphs = unique_sorted (glyph_names)
Behdad Esfahbod3f4b97e2013-07-23 15:04:25 -0400977 del glyph_names
Behdad Esfahbodb70b4982013-07-23 11:38:26 -0400978 lapse ("compile glyph list")
Behdad Esfahbod240d7e72013-07-23 23:12:06 -0400979 if verbose:
980 print "Glyphs:", glyphs
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400981
Behdad Esfahbodde71dca2013-07-24 12:40:54 -0400982
983 for tag in font.keys():
984 if tag == 'GlyphOrder': continue
985
986 if tag in options['drop-tables'] or \
Behdad Esfahbodc0d59592013-07-24 14:41:47 -0400987 (tag in hinting_tables and not options['hinting']):
Behdad Esfahbodde71dca2013-07-24 12:40:54 -0400988 if verbose:
989 print tag, "dropped."
990 del font[tag]
991 continue
992
993 clazz = fontTools.ttLib.getTableClass(tag)
994
995 if hasattr (clazz, 'prune_pre_subset'):
996 table = font[tag]
997 retain = table.prune_pre_subset (options)
998 lapse ("prune '%s'" % tag)
999 if not retain:
1000 if verbose:
1001 print tag, "pruned to empty; dropped."
1002 del font[tag]
1003 continue
1004 else:
1005 if verbose:
1006 print tag, "pruned."
1007
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001008 glyphs_requested = glyphs
1009 if 'GSUB' in font:
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001010 if verbose:
Behdad Esfahbode30ed122013-07-23 21:08:29 -04001011 print "Closing glyph list over 'GSUB': %d glyphs before" % len (glyphs)
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001012 glyphs = font['GSUB'].closure_glyphs (glyphs)
1013 if verbose:
Behdad Esfahbode30ed122013-07-23 21:08:29 -04001014 print "Closed glyph list over 'GSUB': %d glyphs after" % len (glyphs)
Behdad Esfahbod240d7e72013-07-23 23:12:06 -04001015 print "Glyphs:", glyphs
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001016 lapse ("close glyph list over 'GSUB'")
1017 glyphs_gsubed = glyphs
1018
Behdad Esfahbod2a784ac2013-07-22 17:00:36 -04001019 # Close over composite glyphs
1020 if 'glyf' in font:
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001021 if verbose:
Behdad Esfahbode30ed122013-07-23 21:08:29 -04001022 print "Closing glyph list over 'glyf': %d glyphs before" % len (glyphs)
Behdad Esfahbod240d7e72013-07-23 23:12:06 -04001023 print "Glyphs:", glyphs
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001024 glyphs = font['glyf'].closure_glyphs (glyphs)
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001025 if verbose:
Behdad Esfahbode30ed122013-07-23 21:08:29 -04001026 print "Closed glyph list over 'glyf': %d glyphs after" % len (glyphs)
Behdad Esfahbod240d7e72013-07-23 23:12:06 -04001027 print "Glyphs:", glyphs
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001028 lapse ("close glyph list over 'glyf'")
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001029 else:
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001030 glyphs = glyphs
1031 glyphs_glyfed = glyphs
1032 glyphs_closed = glyphs
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001033 del glyphs
1034
Behdad Esfahbodc9dec9d2013-07-23 10:28:47 -04001035 if verbose:
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001036 print "Retaining %d glyphs: " % len (glyphs_closed)
Behdad Esfahbod2a784ac2013-07-22 17:00:36 -04001037
Behdad Esfahbod8842ce22013-07-22 13:01:33 -04001038 for tag in font.keys():
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04001039 if tag == 'GlyphOrder': continue
Behdad Esfahbod4e214e42013-07-22 13:13:49 -04001040
Behdad Esfahbod8842ce22013-07-22 13:01:33 -04001041 clazz = fontTools.ttLib.getTableClass(tag)
Behdad Esfahbod4e214e42013-07-22 13:13:49 -04001042
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001043 if tag in no_subset_tables:
1044 if verbose:
1045 print tag, "subsetting not needed."
Behdad Esfahbod96f47042013-07-23 12:21:34 -04001046 elif hasattr (clazz, 'subset_glyphs'):
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001047 table = font[tag]
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001048 if tag == 'cmap': # What else?
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001049 glyphs = glyphs_requested
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001050 elif tag in ['GSUB', 'GPOS', 'GDEF', 'cmap', 'kern', 'post']: # What else?
1051 glyphs = glyphs_gsubed
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001052 else:
1053 glyphs = glyphs_closed
Behdad Esfahbodb70b4982013-07-23 11:38:26 -04001054 retain = table.subset_glyphs (glyphs)
1055 lapse ("subset '%s'" % tag)
1056 if not retain:
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001057 if verbose:
1058 print tag, "subsetted to empty; dropped."
Behdad Esfahbod8b411f32013-07-23 11:24:20 -04001059 del font[tag]
1060 continue
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001061 else:
1062 if verbose:
1063 print tag, "subsetted."
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001064 del glyphs
Behdad Esfahbod4ae81712013-07-22 11:57:13 -04001065 else:
Behdad Esfahbod350a5272013-07-22 12:02:16 -04001066 if verbose:
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001067 print tag, "NOT subset; don't know how to subset."
1068 continue
1069
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -04001070 if hasattr (clazz, 'prune_post_subset'):
1071 table = font[tag]
1072 retain = table.prune_post_subset (options)
1073 lapse ("prune '%s'" % tag)
1074 if not retain:
1075 if verbose:
1076 print tag, "pruned to empty; dropped."
1077 del font[tag]
1078 continue
1079 else:
1080 if verbose:
1081 print tag, "pruned."
1082
Behdad Esfahbodc7160442013-07-22 14:29:08 -04001083 glyphOrder = font.getGlyphOrder()
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001084 glyphOrder = [g for g in glyphOrder if g in glyphs_closed]
Behdad Esfahbodc7160442013-07-22 14:29:08 -04001085 font.setGlyphOrder (glyphOrder)
1086 font._buildReverseGlyphOrderDict ()
Behdad Esfahbodb70b4982013-07-23 11:38:26 -04001087 lapse ("subset GlyphOrder")
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -04001088
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04001089 font.save (fontfile + '.subset')
1090 lapse ("compile and save font")
1091
1092 last_time = start_time
1093 lapse ("make one with everything (TOTAL TIME)")
1094
Behdad Esfahbod0f86ce92013-07-22 17:30:31 -04001095 if xml:
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04001096 import xmlWriter
1097 writer = xmlWriter.XMLWriter (sys.stdout)
1098
Behdad Esfahbod0f86ce92013-07-22 17:30:31 -04001099 for tag in font.keys():
1100 writer.begintag (tag)
1101 writer.newline ()
1102 font[tag].toXML(writer, font)
1103 writer.endtag (tag)
1104 writer.newline ()
Behdad Esfahbod8c486d82013-07-24 13:34:47 -04001105
1106if __name__ == '__main__':
1107 main ()