Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | # Python OpenType Layout Subsetter |
| 4 | # Writte by: Behdad Esfahbod |
| 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): |
| 29 | self.classDefs = {g:v for g,v in self.classDefs.items() if g in glyphs} |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 30 | return len (self.classDefs) |
| 31 | |
| 32 | @add_method(fontTools.ttLib.tables.otTables.ClassDef) |
| 33 | def __nonzero__ (self): |
| 34 | return bool (self.classDefs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 35 | |
| 36 | @add_method(fontTools.ttLib.tables.otTables.SingleSubst) |
| 37 | def subset (self, glyphs): |
| 38 | if self.Format in [1, 2]: |
| 39 | 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] | 40 | return len (self.mapping) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 41 | else: |
| 42 | assert 0, "unknown format: %s" % self.Format |
| 43 | |
| 44 | @add_method(fontTools.ttLib.tables.otTables.MultipleSubst) |
| 45 | def subset (self, glyphs): |
| 46 | if self.Format == 1: |
| 47 | indices = self.Coverage.subset (glyphs) |
| 48 | self.Sequence = [self.Sequence[i] for i in indices] |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 49 | self.SequenceCount = len (self.Sequence) |
| 50 | return self.SequenceCount |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 51 | else: |
| 52 | assert 0, "unknown format: %s" % self.Format |
| 53 | |
| 54 | @add_method(fontTools.ttLib.tables.otTables.AlternateSubst) |
| 55 | def subset (self, glyphs): |
| 56 | if self.Format == 1: |
| 57 | 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] | 58 | return len (self.alternates) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 59 | else: |
| 60 | assert 0, "unknown format: %s" % self.Format |
| 61 | |
| 62 | @add_method(fontTools.ttLib.tables.otTables.LigatureSubst) |
| 63 | def subset (self, glyphs): |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 64 | if self.Format == 1: |
| 65 | self.ligatures = {g:v for g,v in self.ligatures.items() if g in glyphs} |
| 66 | self.ligatures = {g:[seq for seq in seqs if all(c in glyphs for c in seq.Component)] |
| 67 | for g,seqs in self.ligatures.items()} |
| 68 | self.ligatures = {g:v for g,v in self.ligatures.items() if v} |
| 69 | return len (self.ligatures) |
| 70 | else: |
| 71 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 72 | |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 73 | @add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst) |
| 74 | def subset (self, glyphs): |
| 75 | if self.Format == 1: |
| 76 | indices = self.Coverage.subset (glyphs) |
| 77 | self.Substitute = [self.Substitute[i] for i in indices] |
| 78 | self.GlyphCount = len (self.Substitute) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 79 | 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] | 80 | else: |
| 81 | assert 0, "unknown format: %s" % self.Format |
| 82 | |
| 83 | @add_method(fontTools.ttLib.tables.otTables.SinglePos) |
| 84 | def subset (self, glyphs): |
| 85 | if self.Format == 1: |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 86 | return len (self.Coverage.subset (glyphs)) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 87 | elif self.Format == 2: |
| 88 | indices = self.Coverage.subset (glyphs) |
| 89 | self.Value = [self.Value[i] for i in indices] |
| 90 | self.ValueCount = len (self.Value) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 91 | return self.ValueCount |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 92 | else: |
| 93 | assert 0, "unknown format: %s" % self.Format |
| 94 | |
| 95 | @add_method(fontTools.ttLib.tables.otTables.PairPos) |
| 96 | def subset (self, glyphs): |
| 97 | if self.Format == 1: |
| 98 | indices = self.Coverage.subset (glyphs) |
| 99 | self.PairSet = [self.PairSet[i] for i in indices] |
| 100 | for p in self.PairSet: |
| 101 | p.PairValueRecord = [r for r in p.PairValueRecord if r.SecondGlyph in glyphs] |
| 102 | p.PairValueCount = len (p.PairValueRecord) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 103 | self.PairSet = [p for p in self.PairSet if p.PairValueCount] |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 104 | self.PairSetCount = len (self.PairSet) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 105 | return self.PairSetCount |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 106 | elif self.Format == 2: |
| 107 | self.Coverage.subset (glyphs) |
| 108 | self.ClassDef1.subset (glyphs) |
| 109 | self.ClassDef2.subset (glyphs) |
| 110 | # TODO Prune empty classes |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 111 | return self.Coverage and self.ClassDef1 and self.ClassDef2 |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 112 | else: |
| 113 | assert 0, "unknown format: %s" % self.Format |
| 114 | |
| 115 | @add_method(fontTools.ttLib.tables.otTables.CursivePos) |
| 116 | def subset (self, glyphs): |
| 117 | if self.Format == 1: |
| 118 | indices = self.Coverage.subset (glyphs) |
| 119 | self.EntryExitRecord = [self.EntryExitRecord[i] for i in indices] |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 120 | self.EntryExitCount = len (self.EntryExitRecord) |
| 121 | return self.EntryExitCount |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 122 | else: |
| 123 | assert 0, "unknown format: %s" % self.Format |
| 124 | |
| 125 | @add_method(fontTools.ttLib.tables.otTables.MarkBasePos) |
| 126 | def subset (self, glyphs): |
| 127 | if self.Format == 1: |
| 128 | mark_indices = self.MarkCoverage.subset (glyphs) |
| 129 | self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices] |
| 130 | self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord) |
| 131 | base_indices = self.BaseCoverage.subset (glyphs) |
| 132 | self.BaseArray.BaseRecord = [self.BaseArray.BaseRecord[i] for i in base_indices] |
| 133 | self.BaseArray.BaseCount = len (self.BaseArray.BaseRecord) |
| 134 | # TODO Prune empty classes |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 135 | return self.MarkArray.MarkCount and self.BaseArray.BaseCount |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 136 | else: |
| 137 | assert 0, "unknown format: %s" % self.Format |
| 138 | |
| 139 | @add_method(fontTools.ttLib.tables.otTables.MarkLigPos) |
| 140 | def subset (self, glyphs): |
| 141 | if self.Format == 1: |
| 142 | mark_indices = self.MarkCoverage.subset (glyphs) |
| 143 | self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices] |
| 144 | self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord) |
| 145 | ligature_indices = self.LigatureCoverage.subset (glyphs) |
| 146 | self.LigatureArray.LigatureAttach = [self.LigatureArray.LigatureAttach[i] for i in ligature_indices] |
| 147 | self.LigatureArray.LigatureCount = len (self.LigatureArray.LigatureAttach) |
| 148 | # TODO Prune empty classes |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 149 | return self.MarkArray.MarkCount and self.LigatureArray.LigatureCount |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 150 | else: |
| 151 | assert 0, "unknown format: %s" % self.Format |
| 152 | |
| 153 | @add_method(fontTools.ttLib.tables.otTables.MarkMarkPos) |
| 154 | def subset (self, glyphs): |
| 155 | if self.Format == 1: |
| 156 | mark1_indices = self.Mark1Coverage.subset (glyphs) |
| 157 | self.Mark1Array.MarkRecord = [self.Mark1Array.MarkRecord[i] for i in mark1_indices] |
| 158 | self.Mark1Array.MarkCount = len (self.Mark1Array.MarkRecord) |
| 159 | mark2_indices = self.Mark2Coverage.subset (glyphs) |
| 160 | self.Mark2Array.Mark2Record = [self.Mark2Array.Mark2Record[i] for i in mark2_indices] |
| 161 | self.Mark2Array.MarkCount = len (self.Mark2Array.Mark2Record) |
| 162 | # TODO Prune empty classes |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 163 | return self.Mark1Array.MarkCount and self.Mark2Array.MarkCount |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 164 | else: |
| 165 | assert 0, "unknown format: %s" % self.Format |
| 166 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 167 | @add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ContextPos) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 168 | def subset (self, glyphs): |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 169 | if self.Format == 1: |
Behdad Esfahbod | b7fef90 | 2013-07-21 22:48:08 -0400 | [diff] [blame] | 170 | indices = self.Coverage.subset (glyphs) |
| 171 | self.SubRuleSet = [self.SubRuleSet[i] for i in indices] |
| 172 | self.SubRuleSetCount = len (self.SubRuleSet) |
| 173 | for rs in self.SubRuleSet: |
| 174 | rs.SubRule = [r for r in rs.SubRule |
| 175 | if all (g in glyphs for g in r.Input)] |
| 176 | rs.SubRuleCount = len (rs.SubRule) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 177 | # Prune empty subrulesets |
| 178 | return self.SubRuleSetCount |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 179 | elif self.Format == 2: |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 180 | return self.Coverage.subset (glyphs) and self.ClassDef.subset (glyphs) |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 181 | elif self.Format == 3: |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 182 | return all (c.subset (glyphs) for c in self.Coverage) |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 183 | else: |
| 184 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 185 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 186 | @add_method(fontTools.ttLib.tables.otTables.ChainContextSubst, fontTools.ttLib.tables.otTables.ChainContextPos) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 187 | def subset (self, glyphs): |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 188 | if self.Format == 1: |
Behdad Esfahbod | b7fef90 | 2013-07-21 22:48:08 -0400 | [diff] [blame] | 189 | indices = self.Coverage.subset (glyphs) |
| 190 | self.ChainSubRuleSet = [self.ChainSubRuleSet[i] for i in indices] |
| 191 | self.ChainSubRuleSetCount = len (self.ChainSubRuleSet) |
| 192 | for rs in self.ChainSubRuleSet: |
| 193 | rs.ChainSubRule = [r for r in rs.ChainSubRule |
| 194 | if all (g in glyphs for g in r.Backtrack + r.Input + r.LookAhead)] |
| 195 | rs.ChainSubRuleCount = len (rs.ChainSubRule) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 196 | # Prune empty subrulesets |
| 197 | return self.ChainSubRuleSetCount |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 198 | elif self.Format == 2: |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 199 | return self.Coverage.subset (glyphs) and \ |
| 200 | self.LookAheadClassDef.subset (glyphs) and \ |
| 201 | self.BacktrackClassDef.subset (glyphs) and \ |
| 202 | self.InputClassDef.subset (glyphs) |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 203 | elif self.Format == 3: |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 204 | 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] | 205 | else: |
| 206 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 207 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 208 | @add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 209 | def subset (self, glyphs): |
| 210 | if self.Format == 1: |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 211 | return self.ExtSubTable.subset (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 212 | else: |
| 213 | assert 0, "unknown format: %s" % self.Format |
| 214 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 215 | @add_method(fontTools.ttLib.tables.otTables.Lookup) |
| 216 | def subset (self, glyphs): |
Behdad Esfahbod | 1be5345 | 2013-07-21 22:52:15 -0400 | [diff] [blame] | 217 | self.SubTable = [s for s in self.SubTable if s.subset (glyphs)] |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 218 | self.SubTableCount = len (self.SubTable) |
| 219 | return self.SubTableCount |
| 220 | |
| 221 | @add_method(fontTools.ttLib.tables.otTables.LookupList) |
| 222 | def subset (self, glyphs): |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame^] | 223 | "Returns the indices of nonempty lookups." |
| 224 | return [i for (i,l) in enumerate (self.Lookup) if l.subset (glyphs)] |
| 225 | |
| 226 | @add_method(fontTools.ttLib.tables.otTables.LookupList) |
| 227 | def subset_lookups (self, lookup_indices): |
| 228 | self.Lookup = [self.Lookup[i] for i in lookup_indices] |
| 229 | self.LookupCount = len (self.Lookup) |
| 230 | |
| 231 | @add_method(fontTools.ttLib.tables.otTables.Feature) |
| 232 | def subset_lookups (self, lookup_indices): |
| 233 | self.LookupListIndex = [l for l in self.LookupListIndex if l in lookup_indices] |
| 234 | # Now map them. |
| 235 | self.LookupListIndex = [lookup_indices.index (l) for l in self.LookupListIndex] |
| 236 | self.LookupCount = len (self.LookupListIndex) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 237 | return self.LookupCount |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 238 | |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame^] | 239 | @add_method(fontTools.ttLib.tables.otTables.FeatureList) |
| 240 | def subset_lookups (self, lookup_indices): |
| 241 | "Returns the indices of nonempty features." |
| 242 | feature_indices = [i for (i,f) in enumerate (self.FeatureRecord) if f.Feature.subset_lookups (lookup_indices)] |
| 243 | self.FeatureRecord = [self.FeatureRecord[i] for i in feature_indices] |
| 244 | self.FeatureCount = len (self.FeatureRecord) |
| 245 | return feature_indices |
| 246 | |
| 247 | @add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys) |
| 248 | def subset_features (self, feature_indices): |
| 249 | if self.ReqFeatureIndex not in feature_indices: |
| 250 | self.ReqFeatureIndex = 65535 |
| 251 | self.FeatureIndex = [f for f in self.FeatureIndex if f in feature_indices] |
| 252 | self.FeatureCount = len (self.FeatureIndex) |
| 253 | return self.FeatureCount |
| 254 | |
| 255 | @add_method(fontTools.ttLib.tables.otTables.Script) |
| 256 | def subset_features (self, feature_indices): |
| 257 | if self.DefaultLangSys and not self.DefaultLangSys.subset_features (feature_indices): |
| 258 | self.DefaultLangSys = None |
| 259 | self.LangSysRecord = [l for l in self.LangSysRecord if l.LangSys.subset_features (feature_indices)] |
| 260 | self.LangSysCount = len (self.LangSysRecord) |
| 261 | return self.LangSysCount |
| 262 | |
| 263 | @add_method(fontTools.ttLib.tables.otTables.ScriptList) |
| 264 | def subset_features (self, feature_indices): |
| 265 | self.ScriptRecord = [s for s in self.ScriptRecord if s.Script.subset_features (feature_indices)] |
| 266 | self.ScriptCount = len (self.ScriptRecord) |
| 267 | return self.ScriptCount |
| 268 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 269 | @add_method(fontTools.ttLib.tables.otTables.GSUB, fontTools.ttLib.tables.otTables.GPOS) |
| 270 | def subset (self, glyphs): |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame^] | 271 | lookup_indices = self.LookupList.subset (glyphs) |
| 272 | self.subset_lookups (lookup_indices) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 273 | return True # Retain the possibly empty table |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 274 | |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame^] | 275 | @add_method(fontTools.ttLib.tables.otTables.GSUB, fontTools.ttLib.tables.otTables.GPOS) |
| 276 | def subset_lookups (self, lookup_indices): |
| 277 | "Retrains specified lookups, then removes empty features, language systems, and scripts." |
| 278 | self.LookupList.subset_lookups (lookup_indices) |
| 279 | feature_indices = self.FeatureList.subset_lookups (lookup_indices) |
| 280 | self.ScriptList.subset_features (feature_indices) |
| 281 | |
Behdad Esfahbod | efb984a | 2013-07-21 22:26:16 -0400 | [diff] [blame] | 282 | @add_method(fontTools.ttLib.tables.otTables.GDEF) |
| 283 | def subset (self, glyphs): |
| 284 | if self.LigCaretList: |
| 285 | indices = self.LigCaretList.Coverage.subset (glyphs) |
| 286 | self.LigCaretList.LigGlyph = [self.LigCaretList.LigGlyph[i] for i in indices] |
| 287 | self.LigCaretList.LigGlyphCount = len (self.LigCaretList.LigGlyph) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 288 | if not self.LigCaretList.LigGlyphCount: |
| 289 | self.LigCaretList = None |
Behdad Esfahbod | efb984a | 2013-07-21 22:26:16 -0400 | [diff] [blame] | 290 | if self.MarkAttachClassDef: |
| 291 | self.MarkAttachClassDef.classDefs = {g:v for g,v in self.MarkAttachClassDef.classDefs.items() if g in glyphs} |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 292 | if not self.MarkAttachClassDef.classDefs: |
| 293 | self.MarkAttachClassDef = None |
Behdad Esfahbod | efb984a | 2013-07-21 22:26:16 -0400 | [diff] [blame] | 294 | if self.GlyphClassDef: |
| 295 | self.GlyphClassDef.classDefs = {g:v for g,v in self.GlyphClassDef.classDefs.items() if g in glyphs} |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 296 | if not self.GlyphClassDef.classDefs: |
| 297 | self.GlyphClassDef = None |
Behdad Esfahbod | efb984a | 2013-07-21 22:26:16 -0400 | [diff] [blame] | 298 | if self.AttachList: |
| 299 | indices = self.AttachList.Coverage.subset (glyphs) |
| 300 | self.AttachList.AttachPoint = [self.AttachList.AttachPoint[i] for i in indices] |
| 301 | self.AttachList.GlyphCount = len (self.AttachList.AttachPoint) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 302 | if not self.AttachList.GlyphCount: |
| 303 | self.AttachList = None |
| 304 | return True # Retain the possibly empty table |
Behdad Esfahbod | efb984a | 2013-07-21 22:26:16 -0400 | [diff] [blame] | 305 | |
| 306 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 307 | if __name__ == '__main__': |
| 308 | |
| 309 | import sys |
| 310 | |
| 311 | if len (sys.argv) < 3: |
| 312 | print >>sys.stderr, "usage: pyotlss.py font-file glyph..." |
| 313 | sys.exit (1) |
| 314 | |
| 315 | fontfile = sys.argv[1] |
| 316 | glyphs = sys.argv[2:] |
| 317 | |
| 318 | font = fontTools.ttx.TTFont (fontfile) |
| 319 | |
| 320 | names = font.getGlyphNames() |
| 321 | # Convert to glyph names |
| 322 | glyphs = [g if g in names else font.getGlyphName(int(g)) for g in glyphs] |
| 323 | |
Behdad Esfahbod | 9d02c2d | 2013-07-22 11:08:37 -0400 | [diff] [blame] | 324 | for tag in ['GDEF', 'GSUB', 'GPOS']: |
| 325 | if tag not in font: |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 326 | continue |
Behdad Esfahbod | 9d02c2d | 2013-07-22 11:08:37 -0400 | [diff] [blame] | 327 | if not font[tag].table.subset (glyphs): |
| 328 | del font[tag] |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 329 | |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame^] | 330 | font.save (fontfile + '.subset') |