Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | # Python OpenType Layout Subsetter |
Behdad Esfahbod | 4aa6ce3 | 2013-07-22 12:15:36 -0400 | [diff] [blame] | 4 | # Written by: Behdad Esfahbod |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 5 | |
| 6 | import fontTools.ttx |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 7 | |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 8 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 9 | def add_method (*clazzes): |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 10 | def wrapper(method): |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 11 | for clazz in clazzes: |
| 12 | setattr (clazz, method.func_name, method) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 13 | return wrapper |
| 14 | |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 15 | # Subset |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 16 | |
| 17 | @add_method(fontTools.ttLib.tables.otTables.Coverage) |
| 18 | def subset (self, glyphs): |
| 19 | indices = [i for (i,g) in enumerate (self.glyphs) if g in glyphs] |
| 20 | self.glyphs = [g for g in self.glyphs if g in glyphs] |
| 21 | return indices |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 22 | |
| 23 | @add_method(fontTools.ttLib.tables.otTables.Coverage) |
| 24 | def __nonzero__ (self): |
| 25 | return bool (self.glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 26 | |
| 27 | @add_method(fontTools.ttLib.tables.otTables.ClassDef) |
| 28 | def subset (self, glyphs): |
Behdad Esfahbod | 4aa6ce3 | 2013-07-22 12:15:36 -0400 | [diff] [blame] | 29 | "Returns ascending list of remaining classes." |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 30 | self.classDefs = {g:v for g,v in self.classDefs.items() if g in glyphs} |
Behdad Esfahbod | 4aa6ce3 | 2013-07-22 12:15:36 -0400 | [diff] [blame] | 31 | return {v:1 for v in self.classDefs.values ()}.keys () |
| 32 | |
| 33 | @add_method(fontTools.ttLib.tables.otTables.ClassDef) |
| 34 | def remap (self, class_map): |
| 35 | "Remaps classes." |
| 36 | self.classDefs = {g:class_map.index (v) for g,v in self.classDefs.items()} |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 37 | |
| 38 | @add_method(fontTools.ttLib.tables.otTables.ClassDef) |
| 39 | def __nonzero__ (self): |
| 40 | return bool (self.classDefs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 41 | |
| 42 | @add_method(fontTools.ttLib.tables.otTables.SingleSubst) |
| 43 | def subset (self, glyphs): |
| 44 | if self.Format in [1, 2]: |
| 45 | self.mapping = {g:v for g,v in self.mapping.items() if g in glyphs} |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 46 | return len (self.mapping) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 47 | else: |
| 48 | assert 0, "unknown format: %s" % self.Format |
| 49 | |
| 50 | @add_method(fontTools.ttLib.tables.otTables.MultipleSubst) |
| 51 | def subset (self, glyphs): |
| 52 | if self.Format == 1: |
| 53 | indices = self.Coverage.subset (glyphs) |
| 54 | self.Sequence = [self.Sequence[i] for i in indices] |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 55 | self.SequenceCount = len (self.Sequence) |
| 56 | return self.SequenceCount |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 57 | else: |
| 58 | assert 0, "unknown format: %s" % self.Format |
| 59 | |
| 60 | @add_method(fontTools.ttLib.tables.otTables.AlternateSubst) |
| 61 | def subset (self, glyphs): |
| 62 | if self.Format == 1: |
| 63 | self.alternates = {g:v for g,v in self.alternates.items() if g in glyphs} |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 64 | return len (self.alternates) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 65 | else: |
| 66 | assert 0, "unknown format: %s" % self.Format |
| 67 | |
| 68 | @add_method(fontTools.ttLib.tables.otTables.LigatureSubst) |
| 69 | def subset (self, glyphs): |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 70 | if self.Format == 1: |
| 71 | self.ligatures = {g:v for g,v in self.ligatures.items() if g in glyphs} |
| 72 | self.ligatures = {g:[seq for seq in seqs if all(c in glyphs for c in seq.Component)] |
| 73 | for g,seqs in self.ligatures.items()} |
| 74 | self.ligatures = {g:v for g,v in self.ligatures.items() if v} |
| 75 | return len (self.ligatures) |
| 76 | else: |
| 77 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 78 | |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 79 | @add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst) |
| 80 | def subset (self, glyphs): |
| 81 | if self.Format == 1: |
| 82 | indices = self.Coverage.subset (glyphs) |
| 83 | self.Substitute = [self.Substitute[i] for i in indices] |
| 84 | self.GlyphCount = len (self.Substitute) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 85 | return self.GlyphCount and all (c.subset (glyphs) for c in self.LookAheadCoverage + self.BacktrackCoverage) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 86 | else: |
| 87 | assert 0, "unknown format: %s" % self.Format |
| 88 | |
| 89 | @add_method(fontTools.ttLib.tables.otTables.SinglePos) |
| 90 | def subset (self, glyphs): |
| 91 | if self.Format == 1: |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 92 | return len (self.Coverage.subset (glyphs)) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 93 | elif self.Format == 2: |
| 94 | indices = self.Coverage.subset (glyphs) |
| 95 | self.Value = [self.Value[i] for i in indices] |
| 96 | self.ValueCount = len (self.Value) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 97 | return self.ValueCount |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 98 | else: |
| 99 | assert 0, "unknown format: %s" % self.Format |
| 100 | |
| 101 | @add_method(fontTools.ttLib.tables.otTables.PairPos) |
| 102 | def subset (self, glyphs): |
| 103 | if self.Format == 1: |
| 104 | indices = self.Coverage.subset (glyphs) |
| 105 | self.PairSet = [self.PairSet[i] for i in indices] |
| 106 | for p in self.PairSet: |
| 107 | p.PairValueRecord = [r for r in p.PairValueRecord if r.SecondGlyph in glyphs] |
| 108 | p.PairValueCount = len (p.PairValueRecord) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 109 | self.PairSet = [p for p in self.PairSet if p.PairValueCount] |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 110 | self.PairSetCount = len (self.PairSet) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 111 | return self.PairSetCount |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 112 | elif self.Format == 2: |
| 113 | self.Coverage.subset (glyphs) |
Behdad Esfahbod | 4aa6ce3 | 2013-07-22 12:15:36 -0400 | [diff] [blame] | 114 | class1_map = self.ClassDef1.subset (glyphs) |
| 115 | class2_map = self.ClassDef2.subset (glyphs) |
| 116 | self.ClassDef1.remap (class1_map) |
| 117 | self.ClassDef2.remap (class2_map) |
| 118 | self.Class1Record = [self.Class1Record[i] for i in class1_map] |
| 119 | for c in self.Class1Record: |
| 120 | c.Class2Record = [c.Class2Record[i] for i in class2_map] |
| 121 | self.Class1Count = len (class1_map) |
| 122 | self.Class2Count = len (class2_map) |
| 123 | return self.Coverage and self.Class1Count and self.Class2Count |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 124 | else: |
| 125 | assert 0, "unknown format: %s" % self.Format |
| 126 | |
| 127 | @add_method(fontTools.ttLib.tables.otTables.CursivePos) |
| 128 | def subset (self, glyphs): |
| 129 | if self.Format == 1: |
| 130 | indices = self.Coverage.subset (glyphs) |
| 131 | self.EntryExitRecord = [self.EntryExitRecord[i] for i in indices] |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 132 | self.EntryExitCount = len (self.EntryExitRecord) |
| 133 | return self.EntryExitCount |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 134 | else: |
| 135 | assert 0, "unknown format: %s" % self.Format |
| 136 | |
| 137 | @add_method(fontTools.ttLib.tables.otTables.MarkBasePos) |
| 138 | def subset (self, glyphs): |
| 139 | if self.Format == 1: |
| 140 | mark_indices = self.MarkCoverage.subset (glyphs) |
| 141 | self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices] |
| 142 | self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord) |
| 143 | base_indices = self.BaseCoverage.subset (glyphs) |
| 144 | self.BaseArray.BaseRecord = [self.BaseArray.BaseRecord[i] for i in base_indices] |
| 145 | self.BaseArray.BaseCount = len (self.BaseArray.BaseRecord) |
Behdad Esfahbod | c6396b7 | 2013-07-22 12:31:33 -0400 | [diff] [blame] | 146 | # Prune empty classes |
| 147 | class_indices = {v.Class:1 for v in self.MarkArray.MarkRecord}.keys () |
| 148 | self.ClassCount = len (class_indices) |
| 149 | for m in self.MarkArray.MarkRecord: |
| 150 | m.Class = class_indices.index (m.Class) |
| 151 | for b in self.BaseArray.BaseRecord: |
| 152 | b.BaseAnchor = [b.BaseAnchor[i] for i in class_indices] |
| 153 | return self.ClassCount and self.MarkArray.MarkCount and self.BaseArray.BaseCount |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 154 | else: |
| 155 | assert 0, "unknown format: %s" % self.Format |
| 156 | |
| 157 | @add_method(fontTools.ttLib.tables.otTables.MarkLigPos) |
| 158 | def subset (self, glyphs): |
| 159 | if self.Format == 1: |
| 160 | mark_indices = self.MarkCoverage.subset (glyphs) |
| 161 | self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices] |
| 162 | self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord) |
| 163 | ligature_indices = self.LigatureCoverage.subset (glyphs) |
| 164 | self.LigatureArray.LigatureAttach = [self.LigatureArray.LigatureAttach[i] for i in ligature_indices] |
| 165 | self.LigatureArray.LigatureCount = len (self.LigatureArray.LigatureAttach) |
Behdad Esfahbod | c6396b7 | 2013-07-22 12:31:33 -0400 | [diff] [blame] | 166 | # Prune empty classes |
| 167 | class_indices = {v.Class:1 for v in self.MarkArray.MarkRecord}.keys () |
| 168 | self.ClassCount = len (class_indices) |
| 169 | for m in self.MarkArray.MarkRecord: |
| 170 | m.Class = class_indices.index (m.Class) |
| 171 | for l in self.LigatureArray.LigatureAttach: |
| 172 | for c in l.ComponentRecord: |
| 173 | c.LigatureAnchor = [c.LigatureAnchor[i] for i in class_indices] |
| 174 | return self.ClassCount and self.MarkArray.MarkCount and self.LigatureArray.LigatureCount |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 175 | else: |
| 176 | assert 0, "unknown format: %s" % self.Format |
| 177 | |
| 178 | @add_method(fontTools.ttLib.tables.otTables.MarkMarkPos) |
| 179 | def subset (self, glyphs): |
| 180 | if self.Format == 1: |
| 181 | mark1_indices = self.Mark1Coverage.subset (glyphs) |
| 182 | self.Mark1Array.MarkRecord = [self.Mark1Array.MarkRecord[i] for i in mark1_indices] |
| 183 | self.Mark1Array.MarkCount = len (self.Mark1Array.MarkRecord) |
| 184 | mark2_indices = self.Mark2Coverage.subset (glyphs) |
| 185 | self.Mark2Array.Mark2Record = [self.Mark2Array.Mark2Record[i] for i in mark2_indices] |
| 186 | self.Mark2Array.MarkCount = len (self.Mark2Array.Mark2Record) |
Behdad Esfahbod | c6396b7 | 2013-07-22 12:31:33 -0400 | [diff] [blame] | 187 | # Prune empty classes |
| 188 | class_indices = {v.Class:1 for v in self.Mark1Array.MarkRecord}.keys () |
| 189 | self.ClassCount = len (class_indices) |
| 190 | for m in self.Mark1Array.MarkRecord: |
| 191 | m.Class = class_indices.index (m.Class) |
| 192 | for b in self.Mark2Array.Mark2Record: |
| 193 | b.Mark2Anchor = [b.Mark2Anchor[i] for i in class_indices] |
| 194 | return self.ClassCount and self.Mark1Array.MarkCount and self.Mark2Array.MarkCount |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 195 | else: |
| 196 | assert 0, "unknown format: %s" % self.Format |
| 197 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 198 | @add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ContextPos) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 199 | def subset (self, glyphs): |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 200 | if self.Format == 1: |
Behdad Esfahbod | b7fef90 | 2013-07-21 22:48:08 -0400 | [diff] [blame] | 201 | indices = self.Coverage.subset (glyphs) |
| 202 | self.SubRuleSet = [self.SubRuleSet[i] for i in indices] |
| 203 | self.SubRuleSetCount = len (self.SubRuleSet) |
| 204 | for rs in self.SubRuleSet: |
| 205 | rs.SubRule = [r for r in rs.SubRule |
| 206 | if all (g in glyphs for g in r.Input)] |
| 207 | rs.SubRuleCount = len (rs.SubRule) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 208 | # Prune empty subrulesets |
| 209 | return self.SubRuleSetCount |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 210 | elif self.Format == 2: |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 211 | return self.Coverage.subset (glyphs) and self.ClassDef.subset (glyphs) |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 212 | elif self.Format == 3: |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 213 | return all (c.subset (glyphs) for c in self.Coverage) |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 214 | else: |
| 215 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 216 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 217 | @add_method(fontTools.ttLib.tables.otTables.ChainContextSubst, fontTools.ttLib.tables.otTables.ChainContextPos) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 218 | def subset (self, glyphs): |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 219 | if self.Format == 1: |
Behdad Esfahbod | b7fef90 | 2013-07-21 22:48:08 -0400 | [diff] [blame] | 220 | indices = self.Coverage.subset (glyphs) |
| 221 | self.ChainSubRuleSet = [self.ChainSubRuleSet[i] for i in indices] |
| 222 | self.ChainSubRuleSetCount = len (self.ChainSubRuleSet) |
| 223 | for rs in self.ChainSubRuleSet: |
| 224 | rs.ChainSubRule = [r for r in rs.ChainSubRule |
| 225 | if all (g in glyphs for g in r.Backtrack + r.Input + r.LookAhead)] |
| 226 | rs.ChainSubRuleCount = len (rs.ChainSubRule) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 227 | # Prune empty subrulesets |
| 228 | return self.ChainSubRuleSetCount |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 229 | elif self.Format == 2: |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 230 | return self.Coverage.subset (glyphs) and \ |
| 231 | self.LookAheadClassDef.subset (glyphs) and \ |
| 232 | self.BacktrackClassDef.subset (glyphs) and \ |
| 233 | self.InputClassDef.subset (glyphs) |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 234 | elif self.Format == 3: |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 235 | return all (c.subset (glyphs) for c in self.InputCoverage + self.LookAheadCoverage + self.BacktrackCoverage) |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 236 | else: |
| 237 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 238 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 239 | @add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 240 | def subset (self, glyphs): |
| 241 | if self.Format == 1: |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 242 | return self.ExtSubTable.subset (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 243 | else: |
| 244 | assert 0, "unknown format: %s" % self.Format |
| 245 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 246 | @add_method(fontTools.ttLib.tables.otTables.Lookup) |
| 247 | def subset (self, glyphs): |
Behdad Esfahbod | 1be5345 | 2013-07-21 22:52:15 -0400 | [diff] [blame] | 248 | self.SubTable = [s for s in self.SubTable if s.subset (glyphs)] |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 249 | self.SubTableCount = len (self.SubTable) |
| 250 | return self.SubTableCount |
| 251 | |
| 252 | @add_method(fontTools.ttLib.tables.otTables.LookupList) |
| 253 | def subset (self, glyphs): |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 254 | "Returns the indices of nonempty lookups." |
| 255 | return [i for (i,l) in enumerate (self.Lookup) if l.subset (glyphs)] |
| 256 | |
| 257 | @add_method(fontTools.ttLib.tables.otTables.LookupList) |
| 258 | def subset_lookups (self, lookup_indices): |
| 259 | self.Lookup = [self.Lookup[i] for i in lookup_indices] |
| 260 | self.LookupCount = len (self.Lookup) |
| 261 | |
| 262 | @add_method(fontTools.ttLib.tables.otTables.Feature) |
| 263 | def subset_lookups (self, lookup_indices): |
| 264 | self.LookupListIndex = [l for l in self.LookupListIndex if l in lookup_indices] |
| 265 | # Now map them. |
| 266 | self.LookupListIndex = [lookup_indices.index (l) for l in self.LookupListIndex] |
| 267 | self.LookupCount = len (self.LookupListIndex) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 268 | return self.LookupCount |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 269 | |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 270 | @add_method(fontTools.ttLib.tables.otTables.FeatureList) |
| 271 | def subset_lookups (self, lookup_indices): |
| 272 | "Returns the indices of nonempty features." |
| 273 | feature_indices = [i for (i,f) in enumerate (self.FeatureRecord) if f.Feature.subset_lookups (lookup_indices)] |
| 274 | self.FeatureRecord = [self.FeatureRecord[i] for i in feature_indices] |
| 275 | self.FeatureCount = len (self.FeatureRecord) |
| 276 | return feature_indices |
| 277 | |
| 278 | @add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys) |
| 279 | def subset_features (self, feature_indices): |
| 280 | if self.ReqFeatureIndex not in feature_indices: |
| 281 | self.ReqFeatureIndex = 65535 |
| 282 | self.FeatureIndex = [f for f in self.FeatureIndex if f in feature_indices] |
| 283 | self.FeatureCount = len (self.FeatureIndex) |
| 284 | return self.FeatureCount |
| 285 | |
| 286 | @add_method(fontTools.ttLib.tables.otTables.Script) |
| 287 | def subset_features (self, feature_indices): |
| 288 | if self.DefaultLangSys and not self.DefaultLangSys.subset_features (feature_indices): |
| 289 | self.DefaultLangSys = None |
| 290 | self.LangSysRecord = [l for l in self.LangSysRecord if l.LangSys.subset_features (feature_indices)] |
| 291 | self.LangSysCount = len (self.LangSysRecord) |
| 292 | return self.LangSysCount |
| 293 | |
| 294 | @add_method(fontTools.ttLib.tables.otTables.ScriptList) |
| 295 | def subset_features (self, feature_indices): |
| 296 | self.ScriptRecord = [s for s in self.ScriptRecord if s.Script.subset_features (feature_indices)] |
| 297 | self.ScriptCount = len (self.ScriptRecord) |
| 298 | return self.ScriptCount |
| 299 | |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 300 | @add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS')) |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 301 | def subset (self, glyphs): |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 302 | lookup_indices = self.table.LookupList.subset (glyphs) |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 303 | self.subset_lookups (lookup_indices) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 304 | return True # Retain the possibly empty table |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 305 | |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 306 | @add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS')) |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 307 | def subset_lookups (self, lookup_indices): |
| 308 | "Retrains specified lookups, then removes empty features, language systems, and scripts." |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 309 | self.table.LookupList.subset_lookups (lookup_indices) |
| 310 | feature_indices = self.table.FeatureList.subset_lookups (lookup_indices) |
| 311 | self.table.ScriptList.subset_features (feature_indices) |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 312 | |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 313 | @add_method(fontTools.ttLib.getTableClass('GDEF')) |
Behdad Esfahbod | efb984a | 2013-07-21 22:26:16 -0400 | [diff] [blame] | 314 | def subset (self, glyphs): |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 315 | table = self.table |
| 316 | if table.LigCaretList: |
| 317 | indices = table.LigCaretList.Coverage.subset (glyphs) |
| 318 | table.LigCaretList.LigGlyph = [table.LigCaretList.LigGlyph[i] for i in indices] |
| 319 | table.LigCaretList.LigGlyphCount = len (table.LigCaretList.LigGlyph) |
| 320 | if not table.LigCaretList.LigGlyphCount: |
| 321 | table.LigCaretList = None |
| 322 | if table.MarkAttachClassDef: |
| 323 | table.MarkAttachClassDef.classDefs = {g:v for g,v in table.MarkAttachClassDef.classDefs.items() if g in glyphs} |
| 324 | if not table.MarkAttachClassDef.classDefs: |
| 325 | table.MarkAttachClassDef = None |
| 326 | if table.GlyphClassDef: |
| 327 | table.GlyphClassDef.classDefs = {g:v for g,v in table.GlyphClassDef.classDefs.items() if g in glyphs} |
| 328 | if not table.GlyphClassDef.classDefs: |
| 329 | table.GlyphClassDef = None |
| 330 | if table.AttachList: |
| 331 | indices = table.AttachList.Coverage.subset (glyphs) |
| 332 | table.AttachList.AttachPoint = [table.AttachList.AttachPoint[i] for i in indices] |
| 333 | table.AttachList.GlyphCount = len (table.AttachList.AttachPoint) |
| 334 | if not table.AttachList.GlyphCount: |
| 335 | table.AttachList = None |
| 336 | return table.LigCaretList or table.MarkAttachClassDef or table.GlyphClassDef or table.AttachList |
Behdad Esfahbod | efb984a | 2013-07-21 22:26:16 -0400 | [diff] [blame] | 337 | |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 338 | @add_method(fontTools.ttLib.getTableClass('kern')) |
| 339 | def subset (self, glyphs): |
Behdad Esfahbod | 5270ec4 | 2013-07-22 12:57:02 -0400 | [diff] [blame] | 340 | for t in self.kernTables: |
| 341 | t.kernTable = {(a,b):v for ((a,b),v) in t.kernTable.items() if a in glyphs and b in glyphs} |
| 342 | self.kernTables = [t for t in self.kernTables if t.kernTable] |
| 343 | return self.kernTables |
Behdad Esfahbod | efb984a | 2013-07-21 22:26:16 -0400 | [diff] [blame] | 344 | |
Behdad Esfahbod | c716044 | 2013-07-22 14:29:08 -0400 | [diff] [blame^] | 345 | @add_method(fontTools.ttLib.getTableClass('hmtx')) |
| 346 | def subset (self, glyphs): |
| 347 | self.metrics = {g:v for (g,v) in self.metrics.items() if g in glyphs} |
| 348 | return len (self.metrics) |
| 349 | |
Behdad Esfahbod | 56ebd04 | 2013-07-22 13:02:24 -0400 | [diff] [blame] | 350 | @add_method(fontTools.ttLib.getTableClass('gasp'), |
| 351 | fontTools.ttLib.getTableClass('head'), |
| 352 | fontTools.ttLib.getTableClass('hhea'), |
| 353 | fontTools.ttLib.getTableClass('name'), |
| 354 | fontTools.ttLib.getTableClass('vhea'), |
| 355 | fontTools.ttLib.getTableClass('OS/2')) |
| 356 | def subset (self, glyphs): |
| 357 | # Nothing to do. |
| 358 | return True |
| 359 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 360 | if __name__ == '__main__': |
| 361 | |
| 362 | import sys |
| 363 | |
Behdad Esfahbod | 4ae8171 | 2013-07-22 11:57:13 -0400 | [diff] [blame] | 364 | verbose = False |
| 365 | if "--verbose" in sys.argv: |
| 366 | verbose = True |
| 367 | sys.argv.remove ("--verbose") |
Behdad Esfahbod | 350a527 | 2013-07-22 12:02:16 -0400 | [diff] [blame] | 368 | xml = False |
| 369 | if "--xml" in sys.argv: |
| 370 | xml = True |
| 371 | sys.argv.remove ("--xml") |
Behdad Esfahbod | 4ae8171 | 2013-07-22 11:57:13 -0400 | [diff] [blame] | 372 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 373 | if len (sys.argv) < 3: |
| 374 | print >>sys.stderr, "usage: pyotlss.py font-file glyph..." |
| 375 | sys.exit (1) |
| 376 | |
| 377 | fontfile = sys.argv[1] |
| 378 | glyphs = sys.argv[2:] |
| 379 | |
Behdad Esfahbod | c716044 | 2013-07-22 14:29:08 -0400 | [diff] [blame^] | 380 | # Always include .notdef; anything else? |
| 381 | if '.notdef' not in glyphs: |
| 382 | glyphs.append ('.notdef') |
| 383 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 384 | font = fontTools.ttx.TTFont (fontfile) |
| 385 | |
| 386 | names = font.getGlyphNames() |
| 387 | # Convert to glyph names |
| 388 | glyphs = [g if g in names else font.getGlyphName(int(g)) for g in glyphs] |
| 389 | |
Behdad Esfahbod | 350a527 | 2013-07-22 12:02:16 -0400 | [diff] [blame] | 390 | if xml: |
Behdad Esfahbod | 4ae8171 | 2013-07-22 11:57:13 -0400 | [diff] [blame] | 391 | import xmlWriter |
| 392 | writer = xmlWriter.XMLWriter (sys.stdout) |
Behdad Esfahbod | 4e214e4 | 2013-07-22 13:13:49 -0400 | [diff] [blame] | 393 | |
| 394 | drop_tables = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', 'PCLT', 'LTSH'] |
Behdad Esfahbod | 8842ce2 | 2013-07-22 13:01:33 -0400 | [diff] [blame] | 395 | for tag in font.keys(): |
Behdad Esfahbod | 4e214e4 | 2013-07-22 13:13:49 -0400 | [diff] [blame] | 396 | |
Behdad Esfahbod | c716044 | 2013-07-22 14:29:08 -0400 | [diff] [blame^] | 397 | if tag == 'GlyphOrder': |
| 398 | continue |
| 399 | |
Behdad Esfahbod | 4e214e4 | 2013-07-22 13:13:49 -0400 | [diff] [blame] | 400 | if tag in drop_tables: |
Behdad Esfahbod | c716044 | 2013-07-22 14:29:08 -0400 | [diff] [blame^] | 401 | if verbose: |
| 402 | print tag, "dropped." |
Behdad Esfahbod | 4e214e4 | 2013-07-22 13:13:49 -0400 | [diff] [blame] | 403 | del font[tag] |
| 404 | continue |
| 405 | |
Behdad Esfahbod | 8842ce2 | 2013-07-22 13:01:33 -0400 | [diff] [blame] | 406 | clazz = fontTools.ttLib.getTableClass(tag) |
| 407 | if 'subset' not in vars (clazz): |
Behdad Esfahbod | c716044 | 2013-07-22 14:29:08 -0400 | [diff] [blame^] | 408 | if verbose: |
| 409 | print tag, "skipped." |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 410 | continue |
Behdad Esfahbod | 4e214e4 | 2013-07-22 13:13:49 -0400 | [diff] [blame] | 411 | |
Behdad Esfahbod | 8842ce2 | 2013-07-22 13:01:33 -0400 | [diff] [blame] | 412 | table = font[tag] |
| 413 | if not table.subset (glyphs): |
Behdad Esfahbod | 9d02c2d | 2013-07-22 11:08:37 -0400 | [diff] [blame] | 414 | del font[tag] |
Behdad Esfahbod | 5270ec4 | 2013-07-22 12:57:02 -0400 | [diff] [blame] | 415 | if verbose: |
Behdad Esfahbod | c716044 | 2013-07-22 14:29:08 -0400 | [diff] [blame^] | 416 | print tag, "subset empty; dropped." |
Behdad Esfahbod | 4ae8171 | 2013-07-22 11:57:13 -0400 | [diff] [blame] | 417 | else: |
Behdad Esfahbod | 350a527 | 2013-07-22 12:02:16 -0400 | [diff] [blame] | 418 | if xml: |
Behdad Esfahbod | 4ae8171 | 2013-07-22 11:57:13 -0400 | [diff] [blame] | 419 | writer.begintag (tag) |
| 420 | writer.newline () |
| 421 | font[tag].toXML(writer, font) |
| 422 | writer.endtag (tag) |
| 423 | writer.newline () |
Behdad Esfahbod | 350a527 | 2013-07-22 12:02:16 -0400 | [diff] [blame] | 424 | if verbose: |
Behdad Esfahbod | c716044 | 2013-07-22 14:29:08 -0400 | [diff] [blame^] | 425 | print tag, "subsetted." |
| 426 | |
| 427 | glyphOrder = font.getGlyphOrder() |
| 428 | glyphOrder = [g for g in glyphOrder if g in glyphs] |
| 429 | font.setGlyphOrder (glyphOrder) |
| 430 | font._buildReverseGlyphOrderDict () |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 431 | |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 432 | font.save (fontfile + '.subset') |