blob: 5662619e2a5ec16fea003d5eb330351c21f32ff7 [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 Esfahbod4cf7a802013-07-24 16:08:35 -040029import struct
Behdad Esfahbod54660612013-07-21 18:16:55 -040030
Behdad Esfahbod54660612013-07-21 18:16:55 -040031
Behdad Esfahbod02b92062013-07-21 18:40:59 -040032def add_method (*clazzes):
Behdad Esfahbod54660612013-07-21 18:16:55 -040033 def wrapper(method):
Behdad Esfahbod02b92062013-07-21 18:40:59 -040034 for clazz in clazzes:
Behdad Esfahbodc0d59592013-07-24 14:41:47 -040035 assert clazz.__name__ != 'DefaultTable', 'Oops, table class not found.'
Behdad Esfahbod02b92062013-07-21 18:40:59 -040036 setattr (clazz, method.func_name, method)
Behdad Esfahbod54660612013-07-21 18:16:55 -040037 return wrapper
38
Behdad Esfahbod78661bb2013-07-23 10:23:42 -040039def unique_sorted (l):
Behdad Esfahbod2d9a0962013-07-31 13:33:31 -040040 return sorted (set (l))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -040041
Behdad Esfahbod97e17b82013-07-31 15:59:21 -040042def safeEval(data, eval=eval):
43 """A (kindof) safe replacement for eval."""
44 return eval(data, {"__builtins__":{}}, {})
45
Behdad Esfahbod78661bb2013-07-23 10:23:42 -040046
Behdad Esfahbod54660612013-07-21 18:16:55 -040047@add_method(fontTools.ttLib.tables.otTables.Coverage)
Behdad Esfahbod327dcc32013-07-31 13:50:51 -040048def intersect (self, glyphs):
Behdad Esfahbod610b0552013-07-23 14:52:18 -040049 "Returns ascending list of matching coverage values."
50 return [i for (i,g) in enumerate (self.glyphs) if g in glyphs]
51
52@add_method(fontTools.ttLib.tables.otTables.Coverage)
Behdad Esfahbod327dcc32013-07-31 13:50:51 -040053def subset (self, glyphs):
Behdad Esfahbodd821ea02013-07-23 10:50:43 -040054 "Returns ascending list of remaining coverage values."
Behdad Esfahbod327dcc32013-07-31 13:50:51 -040055 indices = self.intersect (glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -040056 self.glyphs = [g for g in self.glyphs if g in glyphs]
57 return indices
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040058
Behdad Esfahbod54660612013-07-21 18:16:55 -040059@add_method(fontTools.ttLib.tables.otTables.ClassDef)
Behdad Esfahbod327dcc32013-07-31 13:50:51 -040060def intersect (self, glyphs):
Behdad Esfahbodb8d55882013-07-23 22:17:39 -040061 "Returns ascending list of matching class values."
62 return unique_sorted (v for g,v in self.classDefs.items() if g in glyphs)
63
64@add_method(fontTools.ttLib.tables.otTables.ClassDef)
Behdad Esfahbod327dcc32013-07-31 13:50:51 -040065def intersects_class (self, glyphs, klass):
Behdad Esfahbodb8d55882013-07-23 22:17:39 -040066 "Returns true if any of glyphs has requested class."
Behdad Esfahbod0befd6b2013-08-05 22:47:14 -040067 if klass == 0:
68 if any (g not in self.classDefs.items() for g in glyphs):
69 return True
70 # Fall through
Behdad Esfahbodb8d55882013-07-23 22:17:39 -040071 return any (g in glyphs for g,v in self.classDefs.items() if v == klass)
72
73@add_method(fontTools.ttLib.tables.otTables.ClassDef)
Behdad Esfahbod327dcc32013-07-31 13:50:51 -040074def subset (self, glyphs, remap=False):
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -040075 "Returns ascending list of remaining classes."
Behdad Esfahbod54660612013-07-21 18:16:55 -040076 self.classDefs = {g:v for g,v in self.classDefs.items() if g in glyphs}
Behdad Esfahbodde71dca2013-07-24 12:40:54 -040077 indices = unique_sorted (self.classDefs.values ())
78 if remap:
79 self.remap (indices)
80 return indices
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -040081
82@add_method(fontTools.ttLib.tables.otTables.ClassDef)
83def remap (self, class_map):
84 "Remaps classes."
85 self.classDefs = {g:class_map.index (v) for g,v in self.classDefs.items()}
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040086
Behdad Esfahbod54660612013-07-21 18:16:55 -040087@add_method(fontTools.ttLib.tables.otTables.SingleSubst)
Behdad Esfahbod254442b2013-07-31 14:20:13 -040088def closure_glyphs (self, s):
Behdad Esfahbod610b0552013-07-23 14:52:18 -040089 if self.Format in [1, 2]:
Behdad Esfahbod254442b2013-07-31 14:20:13 -040090 return [v for g,v in self.mapping.items() if g in s.glyphs]
Behdad Esfahbod610b0552013-07-23 14:52:18 -040091 else:
92 assert 0, "unknown format: %s" % self.Format
93
94@add_method(fontTools.ttLib.tables.otTables.SingleSubst)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -040095def subset_glyphs (self, s):
Behdad Esfahbod54660612013-07-21 18:16:55 -040096 if self.Format in [1, 2]:
Behdad Esfahbod3d513b72013-07-31 14:11:40 -040097 self.mapping = {g:v for g,v in self.mapping.items() if g in s.glyphs}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -040098 return bool (self.mapping)
Behdad Esfahbod54660612013-07-21 18:16:55 -040099 else:
100 assert 0, "unknown format: %s" % self.Format
101
102@add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400103def closure_glyphs (self, s):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400104 if self.Format == 1:
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400105 indices = self.Coverage.intersect (s.glyphs)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400106 return sum ((self.Sequence[i].Substitute for i in indices), [])
107 else:
108 assert 0, "unknown format: %s" % self.Format
109
110@add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400111def subset_glyphs (self, s):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400112 if self.Format == 1:
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400113 indices = self.Coverage.subset (s.glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400114 self.Sequence = [self.Sequence[i] for i in indices]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400115 self.SequenceCount = len (self.Sequence)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400116 return bool (self.SequenceCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400117 else:
118 assert 0, "unknown format: %s" % self.Format
119
120@add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400121def closure_glyphs (self, s):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400122 if self.Format == 1:
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400123 return sum ((v for g,v in self.alternates.items() if g in s.glyphs), [])
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400124 else:
125 assert 0, "unknown format: %s" % self.Format
126
127@add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400128def subset_glyphs (self, s):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400129 if self.Format == 1:
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400130 self.alternates = {g:v for g,v in self.alternates.items() if g in s.glyphs}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400131 return bool (self.alternates)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400132 else:
133 assert 0, "unknown format: %s" % self.Format
134
135@add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400136def closure_glyphs (self, s):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400137 if self.Format == 1:
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400138 return sum (([seq.LigGlyph for seq in seqs if all(c in s.glyphs for c in seq.Component)]
139 for g,seqs in self.ligatures.items() if g in s.glyphs), [])
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400140 else:
141 assert 0, "unknown format: %s" % self.Format
142
143@add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400144def subset_glyphs (self, s):
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400145 if self.Format == 1:
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400146 self.ligatures = {g:v for g,v in self.ligatures.items() if g in s.glyphs}
147 self.ligatures = {g:[seq for seq in seqs if all(c in s.glyphs for c in seq.Component)]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400148 for g,seqs in self.ligatures.items()}
149 self.ligatures = {g:v for g,v in self.ligatures.items() if v}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400150 return bool (self.ligatures)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400151 else:
152 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400153
Behdad Esfahbod54660612013-07-21 18:16:55 -0400154@add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400155def closure_glyphs (self, s):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400156 if self.Format == 1:
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400157 indices = self.Coverage.intersect (s.glyphs)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400158 if not indices or \
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400159 not all (c.intersect (s.glyphs) for c in self.LookAheadCoverage + self.BacktrackCoverage):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400160 return []
161 return [self.Substitute[i] for i in indices]
162 else:
163 assert 0, "unknown format: %s" % self.Format
164
165@add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400166def subset_glyphs (self, s):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400167 if self.Format == 1:
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400168 indices = self.Coverage.subset (s.glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400169 self.Substitute = [self.Substitute[i] for i in indices]
170 self.GlyphCount = len (self.Substitute)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400171 return bool (self.GlyphCount and all (c.subset (s.glyphs) for c in self.LookAheadCoverage + self.BacktrackCoverage))
Behdad Esfahbod54660612013-07-21 18:16:55 -0400172 else:
173 assert 0, "unknown format: %s" % self.Format
174
175@add_method(fontTools.ttLib.tables.otTables.SinglePos)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400176def subset_glyphs (self, s):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400177 if self.Format == 1:
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400178 return len (self.Coverage.subset (s.glyphs))
Behdad Esfahbod54660612013-07-21 18:16:55 -0400179 elif self.Format == 2:
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400180 indices = self.Coverage.subset (s.glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400181 self.Value = [self.Value[i] for i in indices]
182 self.ValueCount = len (self.Value)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400183 return bool (self.ValueCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400184 else:
185 assert 0, "unknown format: %s" % self.Format
186
187@add_method(fontTools.ttLib.tables.otTables.PairPos)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400188def subset_glyphs (self, s):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400189 if self.Format == 1:
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400190 indices = self.Coverage.subset (s.glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400191 self.PairSet = [self.PairSet[i] for i in indices]
192 for p in self.PairSet:
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400193 p.PairValueRecord = [r for r in p.PairValueRecord if r.SecondGlyph in s.glyphs]
Behdad Esfahbod54660612013-07-21 18:16:55 -0400194 p.PairValueCount = len (p.PairValueRecord)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400195 self.PairSet = [p for p in self.PairSet if p.PairValueCount]
Behdad Esfahbod54660612013-07-21 18:16:55 -0400196 self.PairSetCount = len (self.PairSet)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400197 return bool (self.PairSetCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400198 elif self.Format == 2:
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400199 class1_map = self.ClassDef1.subset (s.glyphs, remap=True)
200 class2_map = self.ClassDef2.subset (s.glyphs, remap=True)
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -0400201 self.Class1Record = [self.Class1Record[i] for i in class1_map]
202 for c in self.Class1Record:
203 c.Class2Record = [c.Class2Record[i] for i in class2_map]
204 self.Class1Count = len (class1_map)
205 self.Class2Count = len (class2_map)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400206 return bool (self.Class1Count and self.Class2Count and self.Coverage.subset (s.glyphs))
Behdad Esfahbod54660612013-07-21 18:16:55 -0400207 else:
208 assert 0, "unknown format: %s" % self.Format
209
210@add_method(fontTools.ttLib.tables.otTables.CursivePos)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400211def subset_glyphs (self, s):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400212 if self.Format == 1:
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400213 indices = self.Coverage.subset (s.glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400214 self.EntryExitRecord = [self.EntryExitRecord[i] for i in indices]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400215 self.EntryExitCount = len (self.EntryExitRecord)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400216 return bool (self.EntryExitCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400217 else:
218 assert 0, "unknown format: %s" % self.Format
219
220@add_method(fontTools.ttLib.tables.otTables.MarkBasePos)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400221def subset_glyphs (self, s):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400222 if self.Format == 1:
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400223 mark_indices = self.MarkCoverage.subset (s.glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400224 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices]
225 self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400226 base_indices = self.BaseCoverage.subset (s.glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400227 self.BaseArray.BaseRecord = [self.BaseArray.BaseRecord[i] for i in base_indices]
228 self.BaseArray.BaseCount = len (self.BaseArray.BaseRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400229 # Prune empty classes
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400230 class_indices = unique_sorted (v.Class for v in self.MarkArray.MarkRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400231 self.ClassCount = len (class_indices)
232 for m in self.MarkArray.MarkRecord:
233 m.Class = class_indices.index (m.Class)
234 for b in self.BaseArray.BaseRecord:
235 b.BaseAnchor = [b.BaseAnchor[i] for i in class_indices]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400236 return bool (self.ClassCount and self.MarkArray.MarkCount and self.BaseArray.BaseCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400237 else:
238 assert 0, "unknown format: %s" % self.Format
239
240@add_method(fontTools.ttLib.tables.otTables.MarkLigPos)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400241def subset_glyphs (self, s):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400242 if self.Format == 1:
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400243 mark_indices = self.MarkCoverage.subset (s.glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400244 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices]
245 self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400246 ligature_indices = self.LigatureCoverage.subset (s.glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400247 self.LigatureArray.LigatureAttach = [self.LigatureArray.LigatureAttach[i] for i in ligature_indices]
248 self.LigatureArray.LigatureCount = len (self.LigatureArray.LigatureAttach)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400249 # Prune empty classes
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400250 class_indices = unique_sorted (v.Class for v in self.MarkArray.MarkRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400251 self.ClassCount = len (class_indices)
252 for m in self.MarkArray.MarkRecord:
253 m.Class = class_indices.index (m.Class)
254 for l in self.LigatureArray.LigatureAttach:
255 for c in l.ComponentRecord:
256 c.LigatureAnchor = [c.LigatureAnchor[i] for i in class_indices]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400257 return bool (self.ClassCount and self.MarkArray.MarkCount and self.LigatureArray.LigatureCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400258 else:
259 assert 0, "unknown format: %s" % self.Format
260
261@add_method(fontTools.ttLib.tables.otTables.MarkMarkPos)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400262def subset_glyphs (self, s):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400263 if self.Format == 1:
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400264 mark1_indices = self.Mark1Coverage.subset (s.glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400265 self.Mark1Array.MarkRecord = [self.Mark1Array.MarkRecord[i] for i in mark1_indices]
266 self.Mark1Array.MarkCount = len (self.Mark1Array.MarkRecord)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400267 mark2_indices = self.Mark2Coverage.subset (s.glyphs)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400268 self.Mark2Array.Mark2Record = [self.Mark2Array.Mark2Record[i] for i in mark2_indices]
269 self.Mark2Array.MarkCount = len (self.Mark2Array.Mark2Record)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400270 # Prune empty classes
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400271 class_indices = unique_sorted (v.Class for v in self.Mark1Array.MarkRecord)
Behdad Esfahbodc6396b72013-07-22 12:31:33 -0400272 self.ClassCount = len (class_indices)
273 for m in self.Mark1Array.MarkRecord:
274 m.Class = class_indices.index (m.Class)
275 for b in self.Mark2Array.Mark2Record:
276 b.Mark2Anchor = [b.Mark2Anchor[i] for i in class_indices]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400277 return bool (self.ClassCount and self.Mark1Array.MarkCount and self.Mark2Array.MarkCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400278 else:
279 assert 0, "unknown format: %s" % self.Format
280
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400281@add_method(fontTools.ttLib.tables.otTables.SingleSubst,
282 fontTools.ttLib.tables.otTables.MultipleSubst,
283 fontTools.ttLib.tables.otTables.AlternateSubst,
284 fontTools.ttLib.tables.otTables.LigatureSubst,
285 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
286 fontTools.ttLib.tables.otTables.SinglePos,
287 fontTools.ttLib.tables.otTables.PairPos,
288 fontTools.ttLib.tables.otTables.CursivePos,
289 fontTools.ttLib.tables.otTables.MarkBasePos,
290 fontTools.ttLib.tables.otTables.MarkLigPos,
291 fontTools.ttLib.tables.otTables.MarkMarkPos)
292def subset_lookups (self, lookup_indices):
293 pass
294
295@add_method(fontTools.ttLib.tables.otTables.SingleSubst,
296 fontTools.ttLib.tables.otTables.MultipleSubst,
297 fontTools.ttLib.tables.otTables.AlternateSubst,
298 fontTools.ttLib.tables.otTables.LigatureSubst,
299 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
300 fontTools.ttLib.tables.otTables.SinglePos,
301 fontTools.ttLib.tables.otTables.PairPos,
302 fontTools.ttLib.tables.otTables.CursivePos,
303 fontTools.ttLib.tables.otTables.MarkBasePos,
304 fontTools.ttLib.tables.otTables.MarkLigPos,
305 fontTools.ttLib.tables.otTables.MarkMarkPos)
306def collect_lookups (self):
307 return []
308
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400309@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst,
310 fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos)
311def __classify_context (self):
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400312
313 class ContextHelper:
314 def __init__ (self, klass, Format):
315 if klass.__name__.endswith ('Subst'):
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400316 Typ = 'Sub'
317 Type = 'Subst'
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400318 else:
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400319 Typ = 'Pos'
320 Type = 'Pos'
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400321 if klass.__name__.startswith ('Chain'):
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400322 Chain = 'Chain'
323 else:
324 Chain = ''
325 ChainTyp = Chain+Typ
326
327 self.Typ = Typ
328 self.Type = Type
329 self.Chain = Chain
330 self.ChainTyp = ChainTyp
331
332 self.LookupRecord = Type+'LookupRecord'
333
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400334 if Format == 1:
335 ContextData = None
336 ChainContextData = None
337 RuleData = lambda r: r.Input
338 ChainRuleData = lambda r: r.Backtrack + r.Input + r.LookAhead
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400339 SetRuleData = None
340 ChainSetRuleData = None
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400341 elif Format == 2:
Behdad Esfahbode3f20732013-07-24 11:26:43 -0400342 ContextData = lambda r: (r.ClassDef,)
343 ChainContextData = lambda r: (r.LookAheadClassDef, r.InputClassDef, r.BacktrackClassDef)
344 RuleData = lambda r: (r.Class,)
345 ChainRuleData = lambda r: (r.LookAhead, r.Input, r.Backtrack)
346 def SetRuleData (r, d): (r.Class,) = d
347 def ChainSetRuleData (r, d): (r.LookAhead, r.Input, r.Backtrack) = d
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400348 elif Format == 3:
349 ContextData = None
350 ChainContextData = None
351 RuleData = lambda r: r.Coverage
352 ChainRuleData = lambda r: r.LookAheadCoverage + r.InputCoverage + r.BacktrackCoverage
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400353 SetRuleData = None
354 ChainSetRuleData = None
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400355 else:
356 assert 0, "unknown format: %s" % Format
357
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400358 if Chain:
Behdad Esfahbod707a37a2013-07-23 21:08:26 -0400359 self.ContextData = ChainContextData
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400360 self.RuleData = ChainRuleData
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400361 self.SetRuleData = ChainSetRuleData
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400362 else:
Behdad Esfahbod707a37a2013-07-23 21:08:26 -0400363 self.ContextData = ContextData
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400364 self.RuleData = RuleData
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400365 self.SetRuleData = SetRuleData
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400366
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400367 if Format == 1:
368 self.Rule = ChainTyp+'Rule'
369 self.RuleCount = ChainTyp+'RuleCount'
370 self.RuleSet = ChainTyp+'RuleSet'
371 self.RuleSetCount = ChainTyp+'RuleSetCount'
372 elif Format == 2:
373 self.Rule = ChainTyp+'ClassRule'
374 self.RuleCount = ChainTyp+'ClassRuleCount'
375 self.RuleSet = ChainTyp+'ClassSet'
376 self.RuleSetCount = ChainTyp+'ClassSetCount'
Behdad Esfahbod89987002013-07-23 23:07:42 -0400377
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400378 self.ClassDef = 'InputClassDef' if Chain else 'ClassDef'
Behdad Esfahbod27108392013-07-23 16:40:47 -0400379
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400380 if self.Format not in [1, 2, 3]:
381 return None # Don't shoot the messenger; let it go
382 if not hasattr (self.__class__, "__ContextHelpers"):
383 self.__class__.__ContextHelpers = {}
384 if self.Format not in self.__class__.__ContextHelpers:
385 self.__class__.__ContextHelpers[self.Format] = ContextHelper (self.__class__, self.Format)
386 return self.__class__.__ContextHelpers[self.Format]
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400387
Behdad Esfahbodf2b6d9c2013-07-23 17:31:54 -0400388@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst)
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400389def closure_glyphs (self, s):
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400390 c = self.__classify_context ()
391
Behdad Esfahbod00776972013-07-23 15:33:00 -0400392 if self.Format == 1:
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400393 indices = self.Coverage.intersect (s.glyphs)
Behdad Esfahbodeeca9822013-07-23 17:42:17 -0400394 rss = getattr (self, c.RuleSet)
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400395 return sum ((s.table.LookupList.Lookup[ll.LookupListIndex].closure_glyphs (s) \
Behdad Esfahbodeeca9822013-07-23 17:42:17 -0400396 for i in indices \
397 for r in getattr (rss[i], c.Rule) \
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400398 if r and all (g in s.glyphs for g in c.RuleData (r)) \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400399 for ll in getattr (r, c.LookupRecord) if ll \
Behdad Esfahbodeeca9822013-07-23 17:42:17 -0400400 ), [])
Behdad Esfahbod00776972013-07-23 15:33:00 -0400401 elif self.Format == 2:
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400402 if not self.Coverage.intersect (s.glyphs):
Behdad Esfahbod31084302013-07-23 22:22:38 -0400403 return []
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400404 indices = getattr (self, c.ClassDef).intersect (s.glyphs)
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400405 rss = getattr (self, c.RuleSet)
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400406 return sum ((s.table.LookupList.Lookup[ll.LookupListIndex].closure_glyphs (s) \
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400407 for i in indices \
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400408 for r in getattr (rss[i], c.Rule) \
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400409 if r and all (cd.intersects_class (s.glyphs, k) \
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400410 for cd,k in zip (c.ContextData (self), c.RuleData (r))) \
Behdad Esfahbodb8d55882013-07-23 22:17:39 -0400411 for ll in getattr (r, c.LookupRecord) if ll \
412 ), [])
Behdad Esfahbod00776972013-07-23 15:33:00 -0400413 elif self.Format == 3:
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400414 if not all (x.intersect (s.glyphs) for x in c.RuleData (self)):
Behdad Esfahbod00776972013-07-23 15:33:00 -0400415 return []
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400416 return sum ((s.table.LookupList.Lookup[ll.LookupListIndex].closure_glyphs (s) \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400417 for ll in getattr (self, c.LookupRecord) if ll), [])
Behdad Esfahbod00776972013-07-23 15:33:00 -0400418 else:
419 assert 0, "unknown format: %s" % self.Format
420
Behdad Esfahbodcbba4a62013-07-23 17:27:18 -0400421@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ContextPos,
422 fontTools.ttLib.tables.otTables.ChainContextSubst, fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400423def subset_glyphs (self, s):
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400424 c = self.__classify_context ()
425
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400426 if self.Format == 1:
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400427 indices = self.Coverage.subset (s.glyphs)
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400428 rss = getattr (self, c.RuleSet)
429 rss = [rss[i] for i in indices]
430 for rs in rss:
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400431 if rs:
432 ss = getattr (rs, c.Rule)
433 ss = [r for r in ss \
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400434 if r and all (g in s.glyphs for g in c.RuleData (r))]
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400435 setattr (rs, c.Rule, ss)
436 setattr (rs, c.RuleCount, len (ss))
Behdad Esfahbodcbba4a62013-07-23 17:27:18 -0400437 # Prune empty subrulesets
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400438 rss = [rs for rs in rss if rs and getattr (rs, c.Rule)]
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400439 setattr (self, c.RuleSet, rss)
440 setattr (self, c.RuleSetCount, len (rss))
441 return bool (rss)
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400442 elif self.Format == 2:
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400443 if not self.Coverage.subset (s.glyphs):
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400444 return False
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400445 indices = getattr (self, c.ClassDef).intersect (s.glyphs)
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400446 rss = getattr (self, c.RuleSet)
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400447 rss = [rss[i] for i in indices]
448 ContextData = c.ContextData (self)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400449 klass_maps = [x.subset (s.glyphs, remap=True) for x in ContextData]
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400450 for rs in rss:
451 if rs:
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400452 ss = getattr (rs, c.Rule)
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400453 ss = [r for r in ss \
454 if r and all (k in klass_map \
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400455 for klass_map,k in zip (klass_maps, c.RuleData (r)))]
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400456 setattr (rs, c.Rule, ss)
457 setattr (rs, c.RuleCount, len (ss))
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400458
459 # Remap rule classes
460 for r in ss:
Behdad Esfahbod44fc6f62013-07-24 11:24:39 -0400461 c.SetRuleData (r, (klass_map.index (k) \
462 for klassmap,k in zip (klass_maps, c.RuleData (r))))
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400463 # Prune empty subrulesets
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400464 rss = [rs for rs in rss if rs and getattr (rs, c.Rule)]
465 setattr (self, c.RuleSet, rss)
466 setattr (self, c.RuleSetCount, len (rss))
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400467 return bool (rss)
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400468 elif self.Format == 3:
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400469 return all (x.subset (s.glyphs) for x in c.RuleData (self))
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400470 else:
471 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400472
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400473@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst,
474 fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400475def subset_lookups (self, lookup_indices):
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400476 c = self.__classify_context ()
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400477
Behdad Esfahbod1f573632013-07-23 23:04:43 -0400478 if self.Format in [1, 2]:
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400479 for rs in getattr (self, c.RuleSet):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400480 if rs:
481 for r in getattr (rs, c.Rule):
482 if r:
Behdad Esfahbod1f573632013-07-23 23:04:43 -0400483 setattr (r, c.LookupRecord, [ll for ll in getattr (r, c.LookupRecord) if ll \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400484 if ll.LookupListIndex in lookup_indices])
485 for ll in getattr (r, c.LookupRecord):
486 if ll:
487 ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400488 elif self.Format == 3:
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400489 setattr (self, c.LookupRecord, [ll for ll in getattr (self, c.LookupRecord) if ll \
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400490 if ll.LookupListIndex in lookup_indices])
491 for ll in getattr (self, c.LookupRecord):
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400492 if ll:
493 ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400494 else:
495 assert 0, "unknown format: %s" % self.Format
496
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400497@add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst,
498 fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400499def collect_lookups (self):
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400500 c = self.__classify_context ()
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400501
Behdad Esfahbod1f573632013-07-23 23:04:43 -0400502 if self.Format in [1, 2]:
Behdad Esfahbod27108392013-07-23 16:40:47 -0400503 return [ll.LookupListIndex \
Behdad Esfahbodbac31f52013-07-23 23:00:39 -0400504 for rs in getattr (self, c.RuleSet) if rs \
505 for r in getattr (rs, c.Rule) if r \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400506 for ll in getattr (r, c.LookupRecord) if ll]
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400507 elif self.Format == 3:
Behdad Esfahbod6870f8a2013-07-23 16:18:30 -0400508 return [ll.LookupListIndex \
Behdad Esfahbod7c225a62013-07-23 21:33:13 -0400509 for ll in getattr (self, c.LookupRecord) if ll]
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400510 else:
511 assert 0, "unknown format: %s" % self.Format
512
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400513@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst)
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400514def closure_glyphs (self, s):
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400515 if self.Format == 1:
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400516 return self.ExtSubTable.closure_glyphs (s)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400517 else:
518 assert 0, "unknown format: %s" % self.Format
519
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400520@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400521def subset_glyphs (self, s):
Behdad Esfahbod54660612013-07-21 18:16:55 -0400522 if self.Format == 1:
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400523 return self.ExtSubTable.subset_glyphs (s)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400524 else:
525 assert 0, "unknown format: %s" % self.Format
526
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400527@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
528def subset_lookups (self, lookup_indices):
529 if self.Format == 1:
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400530 return self.ExtSubTable.subset_lookups (lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400531 else:
532 assert 0, "unknown format: %s" % self.Format
533
534@add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos)
535def collect_lookups (self):
536 if self.Format == 1:
537 return self.ExtSubTable.collect_lookups ()
538 else:
539 assert 0, "unknown format: %s" % self.Format
540
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400541@add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400542def closure_glyphs (self, s):
543 return sum ((st.closure_glyphs (s) for st in self.SubTable if st), [])
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400544
545@add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400546def subset_glyphs (self, s):
547 self.SubTable = [st for st in self.SubTable if st and st.subset_glyphs (s)]
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400548 self.SubTableCount = len (self.SubTable)
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400549 return bool (self.SubTableCount)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400550
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400551@add_method(fontTools.ttLib.tables.otTables.Lookup)
552def subset_lookups (self, lookup_indices):
553 for s in self.SubTable:
554 s.subset_lookups (lookup_indices)
555
556@add_method(fontTools.ttLib.tables.otTables.Lookup)
557def collect_lookups (self):
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400558 return unique_sorted (sum ((st.collect_lookups () for st in self.SubTable if st), []))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400559
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400560@add_method(fontTools.ttLib.tables.otTables.LookupList)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400561def subset_glyphs (self, s):
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400562 "Returns the indices of nonempty lookups."
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400563 return [i for (i,l) in enumerate (self.Lookup) if l and l.subset_glyphs (s)]
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400564
565@add_method(fontTools.ttLib.tables.otTables.LookupList)
566def subset_lookups (self, lookup_indices):
Behdad Esfahbodafae8322013-07-24 18:57:06 -0400567 self.Lookup = [self.Lookup[i] for i in lookup_indices if i < self.LookupCount]
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400568 self.LookupCount = len (self.Lookup)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400569 for l in self.Lookup:
570 l.subset_lookups (lookup_indices)
571
572@add_method(fontTools.ttLib.tables.otTables.LookupList)
573def closure_lookups (self, lookup_indices):
Behdad Esfahbodbb7e2132013-07-23 13:48:35 -0400574 lookup_indices = unique_sorted (lookup_indices)
575 recurse = lookup_indices
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400576 while True:
Behdad Esfahbodafae8322013-07-24 18:57:06 -0400577 recurse_lookups = sum ((self.Lookup[i].collect_lookups () for i in recurse if i < self.LookupCount), [])
578 recurse_lookups = [l for l in recurse_lookups if l not in lookup_indices and l < self.LookupCount]
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400579 if not recurse_lookups:
Behdad Esfahbodbb7e2132013-07-23 13:48:35 -0400580 return unique_sorted (lookup_indices)
581 recurse_lookups = unique_sorted (recurse_lookups)
582 lookup_indices.extend (recurse_lookups)
583 recurse = recurse_lookups
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400584
585@add_method(fontTools.ttLib.tables.otTables.Feature)
586def subset_lookups (self, lookup_indices):
587 self.LookupListIndex = [l for l in self.LookupListIndex if l in lookup_indices]
588 # Now map them.
589 self.LookupListIndex = [lookup_indices.index (l) for l in self.LookupListIndex]
590 self.LookupCount = len (self.LookupListIndex)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400591 return self.LookupCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400592
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400593@add_method(fontTools.ttLib.tables.otTables.Feature)
594def collect_lookups (self):
595 return self.LookupListIndex[:]
596
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400597@add_method(fontTools.ttLib.tables.otTables.FeatureList)
598def subset_lookups (self, lookup_indices):
599 "Returns the indices of nonempty features."
600 feature_indices = [i for (i,f) in enumerate (self.FeatureRecord) if f.Feature.subset_lookups (lookup_indices)]
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400601 self.subset_features (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400602 return feature_indices
603
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400604@add_method(fontTools.ttLib.tables.otTables.FeatureList)
605def collect_lookups (self, feature_indices):
606 return unique_sorted (sum ((self.FeatureRecord[i].Feature.collect_lookups () for i in feature_indices
607 if i < self.FeatureCount), []))
608
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400609@add_method(fontTools.ttLib.tables.otTables.FeatureList)
610def subset_features (self, feature_indices):
611 self.FeatureRecord = [self.FeatureRecord[i] for i in feature_indices]
612 self.FeatureCount = len (self.FeatureRecord)
613 return bool (self.FeatureCount)
614
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400615@add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys)
616def subset_features (self, feature_indices):
Behdad Esfahbod69ce1502013-07-22 18:00:31 -0400617 if self.ReqFeatureIndex in feature_indices:
618 self.ReqFeatureIndex = feature_indices.index (self.ReqFeatureIndex)
619 else:
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400620 self.ReqFeatureIndex = 65535
621 self.FeatureIndex = [f for f in self.FeatureIndex if f in feature_indices]
Behdad Esfahbod69ce1502013-07-22 18:00:31 -0400622 # Now map them.
623 self.FeatureIndex = [feature_indices.index (f) for f in self.FeatureIndex if f in feature_indices]
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400624 self.FeatureCount = len (self.FeatureIndex)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400625 return bool (self.FeatureCount or self.ReqFeatureIndex != 65535)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400626
627@add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys)
628def collect_features (self):
629 feature_indices = self.FeatureIndex[:]
630 if self.ReqFeatureIndex != 65535:
631 feature_indices.append (self.ReqFeatureIndex)
632 return unique_sorted (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400633
634@add_method(fontTools.ttLib.tables.otTables.Script)
635def subset_features (self, feature_indices):
636 if self.DefaultLangSys and not self.DefaultLangSys.subset_features (feature_indices):
637 self.DefaultLangSys = None
638 self.LangSysRecord = [l for l in self.LangSysRecord if l.LangSys.subset_features (feature_indices)]
639 self.LangSysCount = len (self.LangSysRecord)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400640 return bool (self.LangSysCount or self.DefaultLangSys)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400641
642@add_method(fontTools.ttLib.tables.otTables.Script)
643def collect_features (self):
Behdad Esfahbod2307c8b2013-07-23 11:18:13 -0400644 feature_indices = [l.LangSys.collect_features () for l in self.LangSysRecord]
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400645 if self.DefaultLangSys:
646 feature_indices.append (self.DefaultLangSys.collect_features ())
647 return unique_sorted (sum (feature_indices, []))
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400648
649@add_method(fontTools.ttLib.tables.otTables.ScriptList)
650def subset_features (self, feature_indices):
651 self.ScriptRecord = [s for s in self.ScriptRecord if s.Script.subset_features (feature_indices)]
652 self.ScriptCount = len (self.ScriptRecord)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400653 return bool (self.ScriptCount)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400654
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400655@add_method(fontTools.ttLib.tables.otTables.ScriptList)
656def collect_features (self):
657 return unique_sorted (sum ((s.Script.collect_features () for s in self.ScriptRecord), []))
658
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400659@add_method(fontTools.ttLib.getTableClass('GSUB'))
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400660def closure_glyphs (self, s):
661 s.table = self.table
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400662 feature_indices = self.table.ScriptList.collect_features ()
663 lookup_indices = self.table.FeatureList.collect_lookups (feature_indices)
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400664 orig_glyphs = s.glyphs
665 glyphs = unique_sorted (s.glyphs)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400666 while True:
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400667 s.glyphs = glyphs
668 additions = (sum ((self.table.LookupList.Lookup[i].closure_glyphs (s) \
Behdad Esfahbodafae8322013-07-24 18:57:06 -0400669 for i in lookup_indices if i < self.table.LookupList.LookupCount), []))
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400670 additions = unique_sorted (g for g in additions if g not in glyphs)
671 if not additions:
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400672 s.glyphs = orig_glyphs
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400673 return glyphs
674 glyphs.extend (additions)
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400675 del s.table
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400676
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400677@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400678def subset_glyphs (self, s):
Behdad Esfahboda6dbb7a2013-07-31 19:53:57 -0400679 s.glyphs = s.glyphs_gsubed
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400680 lookup_indices = self.table.LookupList.subset_glyphs (s)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400681 self.subset_lookups (lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400682 self.prune_lookups ()
683 return True
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400684
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400685@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400686def subset_lookups (self, lookup_indices):
687 "Retrains specified lookups, then removes empty features, language systems, and scripts."
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400688 self.table.LookupList.subset_lookups (lookup_indices)
689 feature_indices = self.table.FeatureList.subset_lookups (lookup_indices)
690 self.table.ScriptList.subset_features (feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400691
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400692@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
693def prune_lookups (self):
694 "Remove unreferenced lookups"
695 feature_indices = self.table.ScriptList.collect_features ()
696 lookup_indices = self.table.FeatureList.collect_lookups (feature_indices)
697 lookup_indices = self.table.LookupList.closure_lookups (lookup_indices)
698 self.subset_lookups (lookup_indices)
699
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400700@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
701def subset_feature_tags (self, feature_tags):
702 feature_indices = [i for (i,f) in enumerate (self.table.FeatureList.FeatureRecord) if f.FeatureTag in feature_tags]
703 self.table.FeatureList.subset_features (feature_indices)
704 self.table.ScriptList.subset_features (feature_indices)
705
706@add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400707def prune_pre_subset (self, options):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -0400708 if options.layout_features and '*' not in options.layout_features:
709 self.subset_feature_tags (options.layout_features)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400710 self.prune_lookups ()
711 return True
712
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400713@add_method(fontTools.ttLib.getTableClass('GDEF'))
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400714def subset_glyphs (self, s):
Behdad Esfahboda6dbb7a2013-07-31 19:53:57 -0400715 glyphs = s.glyphs_gsubed
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400716 table = self.table
717 if table.LigCaretList:
Behdad Esfahboda6dbb7a2013-07-31 19:53:57 -0400718 indices = table.LigCaretList.Coverage.subset (glyphs)
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400719 table.LigCaretList.LigGlyph = [table.LigCaretList.LigGlyph[i] for i in indices]
720 table.LigCaretList.LigGlyphCount = len (table.LigCaretList.LigGlyph)
721 if not table.LigCaretList.LigGlyphCount:
722 table.LigCaretList = None
723 if table.MarkAttachClassDef:
Behdad Esfahboda6dbb7a2013-07-31 19:53:57 -0400724 table.MarkAttachClassDef.classDefs = {g:v for g,v in table.MarkAttachClassDef.classDefs.items() if g in glyphs}
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400725 if not table.MarkAttachClassDef.classDefs:
726 table.MarkAttachClassDef = None
727 if table.GlyphClassDef:
Behdad Esfahboda6dbb7a2013-07-31 19:53:57 -0400728 table.GlyphClassDef.classDefs = {g:v for g,v in table.GlyphClassDef.classDefs.items() if g in glyphs}
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400729 if not table.GlyphClassDef.classDefs:
730 table.GlyphClassDef = None
731 if table.AttachList:
Behdad Esfahboda6dbb7a2013-07-31 19:53:57 -0400732 indices = table.AttachList.Coverage.subset (glyphs)
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400733 table.AttachList.AttachPoint = [table.AttachList.AttachPoint[i] for i in indices]
734 table.AttachList.GlyphCount = len (table.AttachList.AttachPoint)
735 if not table.AttachList.GlyphCount:
736 table.AttachList = None
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400737 return bool (table.LigCaretList or table.MarkAttachClassDef or table.GlyphClassDef or table.AttachList)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400738
Behdad Esfahbodfd3923e2013-07-22 12:48:17 -0400739@add_method(fontTools.ttLib.getTableClass('kern'))
Behdad Esfahbodd4e33a72013-07-24 18:51:05 -0400740def prune_pre_subset (self, options):
741 # Prune unknown kern table types
742 self.kernTables = [t for t in self.kernTables if hasattr (t, 'kernTable')]
743 return bool (self.kernTables)
744
745@add_method(fontTools.ttLib.getTableClass('kern'))
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400746def subset_glyphs (self, s):
Behdad Esfahboda6dbb7a2013-07-31 19:53:57 -0400747 glyphs = s.glyphs_gsubed
Behdad Esfahbod5270ec42013-07-22 12:57:02 -0400748 for t in self.kernTables:
Behdad Esfahboda6dbb7a2013-07-31 19:53:57 -0400749 t.kernTable = {(a,b):v for ((a,b),v) in t.kernTable.items() if a in glyphs and b in glyphs}
Behdad Esfahbod5270ec42013-07-22 12:57:02 -0400750 self.kernTables = [t for t in self.kernTables if t.kernTable]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400751 return bool (self.kernTables)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400752
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400753@add_method(fontTools.ttLib.getTableClass('hmtx'), fontTools.ttLib.getTableClass('vmtx'))
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400754def subset_glyphs (self, s):
755 self.metrics = {g:v for g,v in self.metrics.items() if g in s.glyphs}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400756 return bool (self.metrics)
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400757
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400758@add_method(fontTools.ttLib.getTableClass('hdmx'))
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400759def subset_glyphs (self, s):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -0400760 self.hdmx = {sz:{g:v for g,v in l.items() if g in s.glyphs} for (sz,l) in self.hdmx.items()}
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400761 return bool (self.hdmx)
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400762
Behdad Esfahbode45d6af2013-07-22 15:29:17 -0400763@add_method(fontTools.ttLib.getTableClass('VORG'))
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400764def subset_glyphs (self, s):
765 self.VOriginRecords = {g:v for g,v in self.VOriginRecords.items() if g in s.glyphs}
Behdad Esfahbode45d6af2013-07-22 15:29:17 -0400766 self.numVertOriginYMetrics = len (self.VOriginRecords)
767 return True # Never drop; has default metrics
768
Behdad Esfahbod8c646f62013-07-22 15:06:23 -0400769@add_method(fontTools.ttLib.getTableClass('post'))
Behdad Esfahbod8c486d82013-07-24 13:34:47 -0400770def prune_pre_subset (self, options):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -0400771 if not options.glyph_names:
Behdad Esfahbod42648242013-07-23 12:56:06 -0400772 self.formatType = 3.0
773 return True
774
775@add_method(fontTools.ttLib.getTableClass('post'))
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400776def subset_glyphs (self, s):
Behdad Esfahbod42648242013-07-23 12:56:06 -0400777 self.extraNames = [] # This seems to do it
Behdad Esfahbodc9dec9d2013-07-23 10:28:47 -0400778 return True
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400779
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -0400780# Copied from _g_l_y_f.py
781ARG_1_AND_2_ARE_WORDS = 0x0001 # if set args are words otherwise they are bytes
782ARGS_ARE_XY_VALUES = 0x0002 # if set args are xy values, otherwise they are points
783ROUND_XY_TO_GRID = 0x0004 # for the xy values if above is true
784WE_HAVE_A_SCALE = 0x0008 # Sx = Sy, otherwise scale == 1.0
785NON_OVERLAPPING = 0x0010 # set to same value for all components (obsolete!)
786MORE_COMPONENTS = 0x0020 # indicates at least one more glyph after this one
787WE_HAVE_AN_X_AND_Y_SCALE = 0x0040 # Sx, Sy
788WE_HAVE_A_TWO_BY_TWO = 0x0080 # t00, t01, t10, t11
789WE_HAVE_INSTRUCTIONS = 0x0100 # instructions follow
790USE_MY_METRICS = 0x0200 # apply these metrics to parent glyph
791OVERLAP_COMPOUND = 0x0400 # used by Apple in GX fonts
792SCALED_COMPONENT_OFFSET = 0x0800 # composite designed to have the component offset scaled (designed for Apple)
793UNSCALED_COMPONENT_OFFSET = 0x1000 # composite designed not to have the component offset scaled (designed for MS)
794
795@add_method(fontTools.ttLib.getTableModule('glyf').Glyph)
796def getComponentNamesFast (self, glyfTable):
797 if struct.unpack(">h", self.data[:2])[0] >= 0:
798 return [] # Not composite
799 data = self.data
800 i = 10
801 components = []
802 more = 1
803 while more:
804 flags, glyphID = struct.unpack(">HH", data[i:i+4])
805 i += 4
806 flags = int(flags)
807 components.append (glyfTable.getGlyphName (int (glyphID)))
808
809 if flags & ARG_1_AND_2_ARE_WORDS: i += 4
810 else: i += 2
811 if flags & WE_HAVE_A_SCALE: i += 2
812 elif flags & WE_HAVE_AN_X_AND_Y_SCALE: i += 4
813 elif flags & WE_HAVE_A_TWO_BY_TWO: i += 8
814 more = flags & MORE_COMPONENTS
815 return components
816
817@add_method(fontTools.ttLib.getTableModule('glyf').Glyph)
818def remapComponentsFast (self, indices):
819 if struct.unpack(">h", self.data[:2])[0] >= 0:
820 return # Not composite
821 data = bytearray (self.data)
822 i = 10
823 more = 1
824 while more:
825 flags = (data[i] << 8) | data[i+1]
826 glyphID = (data[i+2] << 8) | data[i+3]
827 # Remap
828 glyphID = indices.index (glyphID)
829 data[i+2] = glyphID >> 8
830 data[i+3] = glyphID & 0xFF
831 i += 4
832 flags = int(flags)
833
834 if flags & ARG_1_AND_2_ARE_WORDS: i += 4
835 else: i += 2
836 if flags & WE_HAVE_A_SCALE: i += 2
837 elif flags & WE_HAVE_AN_X_AND_Y_SCALE: i += 4
838 elif flags & WE_HAVE_A_TWO_BY_TWO: i += 8
839 more = flags & MORE_COMPONENTS
840 self.data = str (data)
841
Behdad Esfahbod6ec88542013-07-24 16:52:47 -0400842@add_method(fontTools.ttLib.getTableModule('glyf').Glyph)
843def dropInstructionsFast (self):
Behdad Esfahbod6ec88542013-07-24 16:52:47 -0400844 numContours = struct.unpack(">h", self.data[:2])[0]
845 data = bytearray (self.data)
846 i = 10
847 if numContours >= 0:
848 i += 2 * numContours # endPtsOfContours
849 instructionLen = (data[i] << 8) | data[i+1]
850 # Zero it
851 data[i] = data [i+1] = 0
852 i += 2
Behdad Esfahbod0fb69882013-07-24 17:25:35 -0400853 if instructionLen:
854 # Splice it out
855 data = data[:i] + data[i+instructionLen:]
Behdad Esfahbod6ec88542013-07-24 16:52:47 -0400856 else:
857 more = 1
858 while more:
859 flags = (data[i] << 8) | data[i+1]
860 # Turn instruction flag off
861 flags &= ~WE_HAVE_INSTRUCTIONS
862 data[i+0] = flags >> 8
863 data[i+1] = flags & 0xFF
864 i += 4
865 flags = int(flags)
866
867 if flags & ARG_1_AND_2_ARE_WORDS: i += 4
868 else: i += 2
869 if flags & WE_HAVE_A_SCALE: i += 2
870 elif flags & WE_HAVE_AN_X_AND_Y_SCALE: i += 4
871 elif flags & WE_HAVE_A_TWO_BY_TWO: i += 8
872 more = flags & MORE_COMPONENTS
873 # Cut off
874 data = data[:i]
875 if len(data) % 4:
876 # add pad bytes
877 nPadBytes = 4 - (len(data) % 4)
878 for i in range (nPadBytes):
879 data.append (0)
880 self.data = str (data)
881
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400882@add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbod254442b2013-07-31 14:20:13 -0400883def closure_glyphs (self, s):
884 glyphs = unique_sorted (s.glyphs)
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400885 decompose = glyphs
886 # I don't know if component glyphs can be composite themselves.
887 # We handle them anyway.
888 while True:
889 components = []
890 for g in decompose:
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -0400891 if g not in self.glyphs:
Behdad Esfahbodf8c20e42013-07-23 23:13:23 -0400892 continue
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -0400893 gl = self.glyphs[g]
894 if hasattr (gl, "data"):
895 for c in gl.getComponentNamesFast (self):
896 if c not in glyphs:
897 components.append (c)
898 else:
899 # TTX seems to expand gid0..3 always
900 if gl.isComposite ():
901 for c in gl.components:
902 if c.glyphName not in glyphs:
903 components.append (c.glyphName)
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400904 components = [c for c in components if c not in glyphs]
905 if not components:
906 return glyphs
907 decompose = unique_sorted (components)
908 glyphs.extend (components)
909
910@add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400911def subset_glyphs (self, s):
912 self.glyphs = {g:v for g,v in self.glyphs.items() if g in s.glyphs}
913 indices = [i for i,g in enumerate (self.glyphOrder) if g in s.glyphs]
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -0400914 for v in self.glyphs.values ():
915 if hasattr (v, "data"):
916 v.remapComponentsFast (indices)
917 else:
918 pass # No need
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400919 self.glyphOrder = [g for g in self.glyphOrder if g in s.glyphs]
Behdad Esfahbod4027dd82013-07-23 10:56:04 -0400920 return bool (self.glyphs)
Behdad Esfahbod861d9152013-07-22 16:47:24 -0400921
Behdad Esfahboded98c612013-07-23 12:37:41 -0400922@add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400923def prune_post_subset (self, options):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -0400924 if not options.hinting:
Behdad Esfahbod6ec88542013-07-24 16:52:47 -0400925 for v in self.glyphs.values ():
926 if hasattr (v, "data"):
927 v.dropInstructionsFast ()
928 else:
929 v.program = fontTools.ttLib.tables.ttProgram.Program()
930 v.program.fromBytecode([])
Behdad Esfahboded98c612013-07-23 12:37:41 -0400931 return True
932
Behdad Esfahbod2b677c82013-07-23 13:37:13 -0400933@add_method(fontTools.ttLib.getTableClass('CFF '))
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400934def subset_glyphs (self, s):
Behdad Esfahbod2b677c82013-07-23 13:37:13 -0400935 assert 0, "unimplemented"
936
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400937@add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -0400938def closure_glyphs (self, s):
939 tables = [t for t in self.tables if t.platformID == 3 and t.platEncID in [1, 10]]
940 extra = []
Behdad Esfahbod98259f22013-07-31 20:16:24 -0400941 for u in s.unicodes_requested:
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -0400942 found = False
943 for table in tables:
944 if u in table.cmap:
945 extra.append (table.cmap[u])
946 found = True
947 break
948 if not found:
949 s.log ("No glyph for Unicode value %s; skipping." % u)
950 return extra
951
952@add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400953def prune_pre_subset (self, options):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -0400954 if not options.legacy_cmap:
Behdad Esfahbodde4a15b2013-07-23 13:05:42 -0400955 # Drop non-Unicode / non-Symbol cmaps
956 self.tables = [t for t in self.tables if t.platformID == 3 and t.platEncID in [0, 1, 10]]
Behdad Esfahbod97e17b82013-07-31 15:59:21 -0400957 if not options.symbol_cmap:
Behdad Esfahbodde4a15b2013-07-23 13:05:42 -0400958 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 -0400959 # TODO Only keep one subtable?
Behdad Esfahbodabb50a12013-07-23 12:58:37 -0400960 # For now, drop format=0 which can't be subset_glyphs easily?
961 self.tables = [t for t in self.tables if t.format != 0]
962 return bool (self.tables)
963
964@add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400965def subset_glyphs (self, s):
Behdad Esfahboda6dbb7a2013-07-31 19:53:57 -0400966 s.glyphs = s.glyphs_cmaped
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400967 for t in self.tables:
Behdad Esfahbod9453a362013-07-22 16:21:24 -0400968 # For reasons I don't understand I need this here
969 # to force decompilation of the cmap format 14.
970 try:
971 getattr (t, "asdf")
972 except AttributeError:
973 pass
Behdad Esfahbodb13d7902013-07-22 16:01:15 -0400974 if t.format == 14:
Behdad Esfahbod9453a362013-07-22 16:21:24 -0400975 # XXX We drop all the default-UVS mappings (g==None)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -0400976 t.uvsDict = {v:[(u,g) for (u,g) in l if g in s.glyphs] for (v,l) in t.uvsDict.items()}
Behdad Esfahbodb13d7902013-07-22 16:01:15 -0400977 t.uvsDict = {v:l for (v,l) in t.uvsDict.items() if l}
978 else:
Behdad Esfahbodc4eb3db2013-07-31 19:56:19 -0400979 t.cmap = {u:g for (u,g) in t.cmap.items() if g in s.glyphs_requested or u in s.unicodes_requested}
Behdad Esfahbodb13d7902013-07-22 16:01:15 -0400980 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 -0400981 # XXX Convert formats when needed
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400982 return bool (self.tables)
983
Behdad Esfahbod61addb42013-07-23 11:03:49 -0400984@add_method(fontTools.ttLib.getTableClass('name'))
Behdad Esfahbodd7b6f8f2013-07-23 12:46:52 -0400985def prune_pre_subset (self, options):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -0400986 if '*' not in options.name_IDs:
987 self.names = [n for n in self.names if n.nameID in options.name_IDs]
988 if not options.name_legacy:
Behdad Esfahbod20faeb02013-07-23 13:19:03 -0400989 self.names = [n for n in self.names if n.platformID == 3 and n.platEncID == 1]
Behdad Esfahbod97e17b82013-07-31 15:59:21 -0400990 if '*' not in options.name_languages:
991 self.names = [n for n in self.names if n.langID in options.name_languages]
Behdad Esfahbod20faeb02013-07-23 13:19:03 -0400992 return True # Retain even if empty
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400993
Behdad Esfahbod8c646f62013-07-22 15:06:23 -0400994
Behdad Esfahbodbc25f162013-07-23 12:56:54 -0400995drop_tables_default = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', 'PCLT', 'LTSH']
Behdad Esfahbod103a12f2013-07-23 15:08:39 -0400996drop_tables_default += ['Feat', 'Glat', 'Gloc', 'Silf', 'Sill'] # Graphite
Behdad Esfahbod38e852c2013-07-23 16:56:50 -0400997drop_tables_default += ['CBLC', 'CBDT', 'sbix', 'COLR', 'CPAL'] # Color
Behdad Esfahboded98c612013-07-23 12:37:41 -0400998no_subset_tables = ['gasp', 'head', 'hhea', 'maxp', 'vhea', 'OS/2', 'loca', 'name', 'cvt ', 'fpgm', 'prep']
Behdad Esfahbodbc25f162013-07-23 12:56:54 -0400999hinting_tables = ['cvt ', 'fpgm', 'prep', 'hdmx', 'VDMX']
Behdad Esfahbod29df0462013-07-23 11:05:25 -04001000
Behdad Esfahbod356c42e2013-07-23 12:10:46 -04001001# Based on HarfBuzz shapers
1002layout_features_dict = {
1003 # Default shaper
1004 'common': ['ccmp', 'liga', 'locl', 'mark', 'mkmk', 'rlig'],
1005 'horizontal': ['calt', 'clig', 'curs', 'kern', 'rclt'],
1006 'vertical': ['valt', 'vert', 'vkrn', 'vpal', 'vrt2'],
1007 'ltr': ['ltra', 'ltrm'],
1008 'rtl': ['rtla', 'rtlm'],
1009 # Complex shapers
Behdad Esfahbodf36b5a92013-08-04 16:54:46 -04001010 'arabic': ['init', 'medi', 'fina', 'isol', 'med2', 'fin2', 'fin3', 'cswh', 'mset'],
Behdad Esfahbod356c42e2013-07-23 12:10:46 -04001011 'hangul': ['ljmo', 'vjmo', 'tjmo'],
1012 'tibetal': ['abvs', 'blws', 'abvm', 'blwm'],
1013 'indic': ['nukt', 'akhn', 'rphf', 'rkrf', 'pref', 'blwf', 'half', 'abvf', 'pstf', 'cfar', 'vatu', 'cjct',
1014 'init', 'pres', 'abvs', 'blws', 'psts', 'haln', 'dist', 'abvm', 'blwm'],
1015}
1016layout_features_all = unique_sorted (sum (layout_features_dict.values (), []))
1017
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -04001018# TODO OS/2 ulUnicodeRange / ulCodePageRange?
Behdad Esfahbodf71267b2013-07-23 12:59:13 -04001019# TODO Drop unneeded GSUB/GPOS Script/LangSys entries
Behdad Esfahbod398d3892013-07-23 15:29:40 -04001020# TODO Avoid recursing too much
Behdad Esfahbode94aa0e2013-07-23 13:22:04 -04001021# TODO Text direction considerations
1022# TODO Text script / language considerations
Behdad Esfahbodb3ee60c2013-07-24 19:21:40 -04001023# TODO Drop unknown tables? Using DefaultTable.prune?
Behdad Esfahbod8c4f7cc2013-07-24 17:58:29 -04001024# TODO Drop GPOS Device records if not hinting?
Behdad Esfahbod56ebd042013-07-22 13:02:24 -04001025
Behdad Esfahbod8c486d82013-07-24 13:34:47 -04001026
Behdad Esfahbod3d513b72013-07-31 14:11:40 -04001027class Subsetter:
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001028
1029 class Options:
1030 drop_tables = drop_tables_default
1031 layout_features = layout_features_all
1032 hinting = False
1033 glyph_names = False
1034 legacy_cmap = False
1035 symbol_cmap = False
1036 name_IDs = [1, 2] # Family and Style
1037 name_legacy = False
1038 name_languages = [0x0409] # English
1039 mandatory_glyphs = True # First four for TrueType, .notdef for CFF
1040 recalc_bboxes = False # Slows us down
1041
1042 def __init__ (self, **kwargs):
1043
1044 self.set (**kwargs)
1045
1046 def set (self, **kwargs):
1047 for k,v in kwargs.items ():
1048 if not hasattr (self, k):
1049 raise Exception ("Unknown option '%s'" % k)
1050 setattr (self, k, v)
1051
1052 def parse_opts (self, argv, ignore_unknown=False):
1053 ret = []
1054 opts = {}
1055 for a in argv:
1056 if not a.startswith ('--'):
1057 ret.append (a)
1058 continue
1059 a = a[2:]
1060 i = a.find ('=')
1061 if i == -1:
1062 if a.startswith ("no-"):
1063 k = a[3:]
1064 v = False
1065 else:
1066 k = a
1067 v = True
1068 else:
1069 k = a[:i]
1070 v = a[i+1:]
1071 k = k.replace ('-', '_')
1072 if not hasattr (self, k):
1073 if ignore_unknown:
1074 ret.append (a)
1075 continue
1076 else:
1077 raise Exception ("Unknown option '%s'" % a)
1078
1079 ov = getattr (self, k)
1080 if isinstance (ov, bool):
1081 v = bool (v)
1082 elif isinstance (ov, int):
1083 v = int (v)
1084 elif isinstance (ov, list):
1085 v = v.split (',')
1086 v = [int (x, 0) if x[0] in range (10) else x for x in v]
1087
1088 opts[k] = v
1089 self.set (**opts)
1090
1091 return ret
1092
1093
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001094 def __init__ (self, font=None, options=None, log=None):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001095
1096 if isinstance (font, basestring):
1097 font = fontTools.ttx.TTFont (font)
1098 if not log:
1099 log = Logger()
1100 if not options:
1101 options = Options()
1102
Behdad Esfahbod88264a62013-07-31 14:45:13 -04001103 self.font = font
1104 self.options = options
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001105 self.log = log
Behdad Esfahbodc4eb3db2013-07-31 19:56:19 -04001106 self.unicodes_requested = set ()
1107 self.glyphs_requested = set ()
Behdad Esfahbod3d513b72013-07-31 14:11:40 -04001108
Behdad Esfahbod618c0862013-07-31 20:11:17 -04001109 def populate (self, glyphs=[], unicodes=[], text=""):
Behdad Esfahbodc4eb3db2013-07-31 19:56:19 -04001110 self.unicodes_requested.update (unicodes)
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001111 if isinstance (text, str):
Behdad Esfahbod618c0862013-07-31 20:11:17 -04001112 text = text.decode ("utf8")
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04001113 for u in text:
Behdad Esfahbod618c0862013-07-31 20:11:17 -04001114 self.unicodes_requested.add (ord (u))
Behdad Esfahbodc4eb3db2013-07-31 19:56:19 -04001115 self.glyphs_requested.update (glyphs)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -04001116
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001117 def pre_prune (self):
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001118
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001119 for tag in self.font.keys():
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001120 if tag == 'GlyphOrder': continue
1121
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001122 if tag in self.options.drop_tables or \
1123 (tag in hinting_tables and not self.options.hinting):
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001124 self.log (tag, "dropped")
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001125 del self.font[tag]
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001126 continue
1127
1128 clazz = fontTools.ttLib.getTableClass(tag)
1129
1130 if hasattr (clazz, 'prune_pre_subset'):
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001131 table = self.font[tag]
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001132 retain = table.prune_pre_subset (self.options)
1133 self.log.lapse ("prune '%s'" % tag)
1134 if not retain:
1135 self.log (tag, "pruned to empty; dropped")
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001136 del self.font[tag]
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001137 continue
1138 else:
1139 self.log (tag, "pruned")
1140
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001141 def closure_glyphs (self):
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001142
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001143 self.glyphs = self.glyphs_requested
1144
1145 if 'cmap' in self.font:
1146 extra_glyphs = self.font['cmap'].closure_glyphs (self)
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001147 self.glyph = self.glyphs.copy ()
1148 self.glyphs.update (extra_glyphs)
1149 self.glyphs_cmaped = self.glyphs
1150
1151 if self.options.mandatory_glyphs:
1152 self.glyphs = self.glyphs.copy ()
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001153 if 'glyf' in self.font:
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001154 for i in range (4):
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001155 self.glyphs.add (self.font.getGlyphName (i))
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001156 self.log ("Added first four glyphs to subset")
1157 else:
1158 self.glyphs.add ('.notdef')
1159 self.log ("Added .notdef glyph to subset")
1160
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001161 if 'GSUB' in self.font:
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04001162 self.log ("Closing glyph list over 'GSUB': %d glyphs before" % len (self.glyphs))
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001163 self.glyphs = set (self.font['GSUB'].closure_glyphs (self))
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04001164 self.log ("Closed glyph list over 'GSUB': %d glyphs after" % len (self.glyphs))
1165 self.log ("Glyphs:", self.glyphs)
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001166 self.log.lapse ("close glyph list over 'GSUB'")
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04001167 self.glyphs_gsubed = self.glyphs
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001168
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001169 if 'glyf' in self.font:
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04001170 self.log ("Closing glyph list over 'glyf': %d glyphs before" % len (self.glyphs))
1171 self.log ("Glyphs:", self.glyphs)
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001172 self.glyphs = set (self.font['glyf'].closure_glyphs (self))
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04001173 self.log ("Closed glyph list over 'glyf': %d glyphs after" % len (self.glyphs))
1174 self.log ("Glyphs:", self.glyphs)
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001175 self.log.lapse ("close glyph list over 'glyf'")
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04001176 self.glyphs_glyfed = self.glyphs
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001177
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04001178 self.glyphs_all = self.glyphs
1179
1180 self.log ("Retaining %d glyphs: " % len (self.glyphs_all))
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001181
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001182 def subset_glyphs (self):
1183 for tag in self.font.keys():
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001184 if tag == 'GlyphOrder': continue
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001185 clazz = fontTools.ttLib.getTableClass(tag)
1186
1187 if tag in no_subset_tables:
1188 self.log (tag, "subsetting not needed")
1189 elif hasattr (clazz, 'subset_glyphs'):
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001190 table = self.font[tag]
Behdad Esfahboda6dbb7a2013-07-31 19:53:57 -04001191 self.glyphs = self.glyphs_all
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001192 retain = table.subset_glyphs (self)
1193 self.log.lapse ("subset '%s'" % tag)
1194 if not retain:
1195 self.log (tag, "subsetted to empty; dropped")
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001196 del self.font[tag]
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001197 else:
1198 self.log (tag, "subsetted")
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001199 else:
1200 self.log (tag, "NOT subset; don't know how to subset")
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001201
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001202 glyphOrder = self.font.getGlyphOrder()
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001203 glyphOrder = [g for g in glyphOrder if g in self.glyphs_all]
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001204 self.font.setGlyphOrder (glyphOrder)
1205 self.font._buildReverseGlyphOrderDict ()
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001206 self.log.lapse ("subset GlyphOrder")
1207
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001208 def post_prune (self):
1209 for tag in self.font.keys():
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001210 if tag == 'GlyphOrder': continue
1211 clazz = fontTools.ttLib.getTableClass(tag)
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001212 if hasattr (clazz, 'prune_post_subset'):
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001213 table = self.font[tag]
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001214 retain = table.prune_post_subset (self.options)
1215 self.log.lapse ("prune '%s'" % tag)
1216 if not retain:
1217 self.log (tag, "pruned to empty; dropped")
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001218 del self.font[tag]
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001219 else:
1220 self.log (tag, "pruned")
1221
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001222 def subset (self, font):
1223
1224 self.font = font
Behdad Esfahbod756af492013-08-01 12:05:26 -04001225
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001226 self.font.recalcBBoxes = self.options.recalc_bboxes
1227
1228 self.pre_prune ()
1229 self.closure_glyphs ()
1230 self.subset_glyphs ()
1231 self.post_prune ()
1232
Behdad Esfahbod756af492013-08-01 12:05:26 -04001233 del self.font
1234
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001235import sys, time
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001236
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001237class Logger:
1238
1239 def __init__ (self, verbose=False, xml=False, timing=False):
1240 self.verbose = verbose
1241 self.xml = xml
1242 self.timing = timing
1243 self.last_time = self.start_time = time.time ()
1244
1245 def parse_opts (self, argv):
1246 argv = argv[:]
1247 for v in ['verbose', 'xml', 'timing']:
1248 if "--"+v in argv:
1249 setattr (self, v, True)
1250 argv.remove ("--"+v)
1251 return argv
1252
1253 def __call__ (self, *things):
1254 if not self.verbose:
1255 return
1256 print ' '.join (str (x) for x in things)
1257
1258 def lapse (self, *things):
1259 if not self.timing:
1260 return
1261 new_time = time.time ()
1262 print "Took %0.3fs to %s" % (new_time - self.last_time, ' '.join (str (x) for x in things))
1263 self.last_time = new_time
1264
1265 def font (self, font, file=sys.stdout):
1266 if not self.xml:
1267 return
1268 import xmlWriter, sys
1269 writer = xmlWriter.XMLWriter (file)
1270 font.disassembleInstructions = False # Work around ttx bug
1271 for tag in font.keys():
1272 writer.begintag (tag)
1273 writer.newline ()
1274 font[tag].toXML(writer, font)
1275 writer.endtag (tag)
1276 writer.newline ()
1277
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001278def main (args):
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001279
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001280 log = Logger ()
1281 args = log.parse_opts (args)
Behdad Esfahbod4ae81712013-07-22 11:57:13 -04001282
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001283 options = Subsetter.Options ()
1284 args = options.parse_opts (args)
1285
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001286 if len (args) < 2:
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001287 import sys
Behdad Esfahbod02b92062013-07-21 18:40:59 -04001288 print >>sys.stderr, "usage: pyotlss.py font-file glyph..."
1289 sys.exit (1)
1290
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001291 fontfile = args[0]
Behdad Esfahbode7f5a892013-07-31 19:58:59 -04001292 args = args[1:]
Behdad Esfahbod02b92062013-07-21 18:40:59 -04001293
Behdad Esfahbodb3ee60c2013-07-24 19:21:40 -04001294 # TODO Option for ignoreDecompileErrors?
Behdad Esfahbod88264a62013-07-31 14:45:13 -04001295 font = fontTools.ttx.TTFont (fontfile)
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001296 s = Subsetter (font=font, options=options, log=log)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001297 log.lapse ("load font")
Behdad Esfahbod02b92062013-07-21 18:40:59 -04001298
Behdad Esfahbod6df089a2013-07-31 19:27:14 -04001299 # Hack:
1300 #
1301 # If we don't need glyph names, change 'post' class to not try to
1302 # load them. It avoid lots of headache with broken fonts as well
1303 # as loading time.
1304 #
1305 # Ideally ttLib should provide a way to ask it to skip loading
1306 # glyph names. But it currently doesn't provide such a thing.
1307 #
1308 if not options.glyph_names \
1309 and all (any (g.startswith (p) for p in ['gid', 'glyph', 'uni']) \
Behdad Esfahbode7f5a892013-07-31 19:58:59 -04001310 for g in args):
Behdad Esfahbod6df089a2013-07-31 19:27:14 -04001311 post = fontTools.ttLib.getTableClass('post')
1312 saved = post.decode_format_2_0
1313 post.decode_format_2_0 = post.decode_format_3_0
1314 f = font['post']
1315 if f.formatType == 2.0:
1316 f.formatType = 3.0
1317 post.decode_format_2_0 = saved
1318 del post, saved, f
1319
1320 names = font.getGlyphNames()
1321 log.lapse ("loading glyph names")
Behdad Esfahbode7f5a892013-07-31 19:58:59 -04001322
1323 glyphs = []
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04001324 unicodes = []
Behdad Esfahbode7f5a892013-07-31 19:58:59 -04001325 for g in args:
Behdad Esfahbod6df089a2013-07-31 19:27:14 -04001326 if g in names:
Behdad Esfahbode7f5a892013-07-31 19:58:59 -04001327 glyphs.append (g)
Behdad Esfahbod6df089a2013-07-31 19:27:14 -04001328 continue
1329 if g.startswith ('uni') and len (g) > 3:
Behdad Esfahbod6df089a2013-07-31 19:27:14 -04001330 u = int (g[3:], 16)
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04001331 unicodes.append (u)
Behdad Esfahbod6df089a2013-07-31 19:27:14 -04001332 continue
1333 if g.startswith ('gid') or g.startswith ('glyph'):
1334 if g.startswith ('gid') and len (g) > 3:
1335 g = g[3:]
1336 elif g.startswith ('glyph') and len (g) > 5:
1337 g = g[5:]
1338 try:
Behdad Esfahbode7f5a892013-07-31 19:58:59 -04001339 glyphs.append (font.getGlyphName (int (g), requireReal=1))
Behdad Esfahbod6df089a2013-07-31 19:27:14 -04001340 except ValueError:
1341 raise Exception ("Invalid glyph identifier %s" % g)
1342 continue
1343 raise Exception ("Invalid glyph identifier %s" % g)
Behdad Esfahbod6df089a2013-07-31 19:27:14 -04001344 log.lapse ("compile glyph list")
Behdad Esfahbode7f5a892013-07-31 19:58:59 -04001345 log ("Unicodes:", unicodes)
Behdad Esfahbod6df089a2013-07-31 19:27:14 -04001346 log ("Glyphs:", glyphs)
1347
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04001348 s.populate (glyphs=glyphs, unicodes=unicodes)
1349 s.subset (font)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -04001350
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04001351 font.save (fontfile + '.subset')
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001352 log.lapse ("compile and save font")
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04001353
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001354 log.last_time = s.log.start_time
1355 log.lapse ("make one with everything (TOTAL TIME)")
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04001356
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001357 log.font (font)
Behdad Esfahbod8c486d82013-07-24 13:34:47 -04001358
1359if __name__ == '__main__':
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001360 import sys
1361 main (sys.argv[1:])