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 | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 15 | def unique_sorted (l): |
| 16 | return sorted ({v:1 for v in l}.keys ()) |
| 17 | |
| 18 | |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 19 | # Subset |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 20 | |
| 21 | @add_method(fontTools.ttLib.tables.otTables.Coverage) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 22 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | d821ea0 | 2013-07-23 10:50:43 -0400 | [diff] [blame] | 23 | "Returns ascending list of remaining coverage values." |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 24 | indices = [i for (i,g) in enumerate (self.glyphs) if g in glyphs] |
| 25 | self.glyphs = [g for g in self.glyphs if g in glyphs] |
| 26 | return indices |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 27 | |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 28 | @add_method(fontTools.ttLib.tables.otTables.ClassDef) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 29 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 4aa6ce3 | 2013-07-22 12:15:36 -0400 | [diff] [blame] | 30 | "Returns ascending list of remaining classes." |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 31 | self.classDefs = {g:v for g,v in self.classDefs.items() if g in glyphs} |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 32 | return unique_sorted (self.classDefs.values ()) |
Behdad Esfahbod | 4aa6ce3 | 2013-07-22 12:15:36 -0400 | [diff] [blame] | 33 | |
| 34 | @add_method(fontTools.ttLib.tables.otTables.ClassDef) |
| 35 | def remap (self, class_map): |
| 36 | "Remaps classes." |
| 37 | 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] | 38 | |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 39 | @add_method(fontTools.ttLib.tables.otTables.SingleSubst) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 40 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 41 | if self.Format in [1, 2]: |
| 42 | self.mapping = {g:v for g,v in self.mapping.items() if g in glyphs} |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 43 | return bool (self.mapping) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 44 | else: |
| 45 | assert 0, "unknown format: %s" % self.Format |
| 46 | |
| 47 | @add_method(fontTools.ttLib.tables.otTables.MultipleSubst) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 48 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 49 | if self.Format == 1: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 50 | indices = self.Coverage.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 51 | self.Sequence = [self.Sequence[i] for i in indices] |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 52 | self.SequenceCount = len (self.Sequence) |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 53 | return bool (self.SequenceCount) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 54 | else: |
| 55 | assert 0, "unknown format: %s" % self.Format |
| 56 | |
| 57 | @add_method(fontTools.ttLib.tables.otTables.AlternateSubst) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 58 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 59 | if self.Format == 1: |
| 60 | self.alternates = {g:v for g,v in self.alternates.items() if g in glyphs} |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 61 | return bool (self.alternates) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 62 | else: |
| 63 | assert 0, "unknown format: %s" % self.Format |
| 64 | |
| 65 | @add_method(fontTools.ttLib.tables.otTables.LigatureSubst) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 66 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 67 | if self.Format == 1: |
| 68 | self.ligatures = {g:v for g,v in self.ligatures.items() if g in glyphs} |
| 69 | self.ligatures = {g:[seq for seq in seqs if all(c in glyphs for c in seq.Component)] |
| 70 | for g,seqs in self.ligatures.items()} |
| 71 | self.ligatures = {g:v for g,v in self.ligatures.items() if v} |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 72 | return bool (self.ligatures) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 73 | else: |
| 74 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 75 | |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 76 | @add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 77 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 78 | if self.Format == 1: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 79 | indices = self.Coverage.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 80 | self.Substitute = [self.Substitute[i] for i in indices] |
| 81 | self.GlyphCount = len (self.Substitute) |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 82 | return bool (self.GlyphCount and all (c.subset_glyphs (glyphs) for c in self.LookAheadCoverage + self.BacktrackCoverage)) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 83 | else: |
| 84 | assert 0, "unknown format: %s" % self.Format |
| 85 | |
| 86 | @add_method(fontTools.ttLib.tables.otTables.SinglePos) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 87 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 88 | if self.Format == 1: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 89 | return len (self.Coverage.subset_glyphs (glyphs)) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 90 | elif self.Format == 2: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 91 | indices = self.Coverage.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 92 | self.Value = [self.Value[i] for i in indices] |
| 93 | self.ValueCount = len (self.Value) |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 94 | return bool (self.ValueCount) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 95 | else: |
| 96 | assert 0, "unknown format: %s" % self.Format |
| 97 | |
| 98 | @add_method(fontTools.ttLib.tables.otTables.PairPos) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 99 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 100 | if self.Format == 1: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 101 | indices = self.Coverage.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 102 | self.PairSet = [self.PairSet[i] for i in indices] |
| 103 | for p in self.PairSet: |
| 104 | p.PairValueRecord = [r for r in p.PairValueRecord if r.SecondGlyph in glyphs] |
| 105 | p.PairValueCount = len (p.PairValueRecord) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 106 | self.PairSet = [p for p in self.PairSet if p.PairValueCount] |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 107 | self.PairSetCount = len (self.PairSet) |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 108 | return bool (self.PairSetCount) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 109 | elif self.Format == 2: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 110 | class1_map = self.ClassDef1.subset_glyphs (glyphs) |
| 111 | class2_map = self.ClassDef2.subset_glyphs (glyphs) |
Behdad Esfahbod | 4aa6ce3 | 2013-07-22 12:15:36 -0400 | [diff] [blame] | 112 | self.ClassDef1.remap (class1_map) |
| 113 | self.ClassDef2.remap (class2_map) |
| 114 | self.Class1Record = [self.Class1Record[i] for i in class1_map] |
| 115 | for c in self.Class1Record: |
| 116 | c.Class2Record = [c.Class2Record[i] for i in class2_map] |
| 117 | self.Class1Count = len (class1_map) |
| 118 | self.Class2Count = len (class2_map) |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 119 | return bool (self.Class1Count and self.Class2Count and self.Coverage.subset_glyphs (glyphs)) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 120 | else: |
| 121 | assert 0, "unknown format: %s" % self.Format |
| 122 | |
| 123 | @add_method(fontTools.ttLib.tables.otTables.CursivePos) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 124 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 125 | if self.Format == 1: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 126 | indices = self.Coverage.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 127 | self.EntryExitRecord = [self.EntryExitRecord[i] for i in indices] |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 128 | self.EntryExitCount = len (self.EntryExitRecord) |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 129 | return bool (self.EntryExitCount) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 130 | else: |
| 131 | assert 0, "unknown format: %s" % self.Format |
| 132 | |
| 133 | @add_method(fontTools.ttLib.tables.otTables.MarkBasePos) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 134 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 135 | if self.Format == 1: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 136 | mark_indices = self.MarkCoverage.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 137 | self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices] |
| 138 | self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 139 | base_indices = self.BaseCoverage.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 140 | self.BaseArray.BaseRecord = [self.BaseArray.BaseRecord[i] for i in base_indices] |
| 141 | self.BaseArray.BaseCount = len (self.BaseArray.BaseRecord) |
Behdad Esfahbod | c6396b7 | 2013-07-22 12:31:33 -0400 | [diff] [blame] | 142 | # Prune empty classes |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 143 | class_indices = unique_sorted (v.Class for v in self.MarkArray.MarkRecord) |
Behdad Esfahbod | c6396b7 | 2013-07-22 12:31:33 -0400 | [diff] [blame] | 144 | self.ClassCount = len (class_indices) |
| 145 | for m in self.MarkArray.MarkRecord: |
| 146 | m.Class = class_indices.index (m.Class) |
| 147 | for b in self.BaseArray.BaseRecord: |
| 148 | b.BaseAnchor = [b.BaseAnchor[i] for i in class_indices] |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 149 | return bool (self.ClassCount and self.MarkArray.MarkCount and self.BaseArray.BaseCount) |
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.MarkLigPos) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 154 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 155 | if self.Format == 1: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 156 | mark_indices = self.MarkCoverage.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 157 | self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices] |
| 158 | self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 159 | ligature_indices = self.LigatureCoverage.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 160 | self.LigatureArray.LigatureAttach = [self.LigatureArray.LigatureAttach[i] for i in ligature_indices] |
| 161 | self.LigatureArray.LigatureCount = len (self.LigatureArray.LigatureAttach) |
Behdad Esfahbod | c6396b7 | 2013-07-22 12:31:33 -0400 | [diff] [blame] | 162 | # Prune empty classes |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 163 | class_indices = unique_sorted (v.Class for v in self.MarkArray.MarkRecord) |
Behdad Esfahbod | c6396b7 | 2013-07-22 12:31:33 -0400 | [diff] [blame] | 164 | self.ClassCount = len (class_indices) |
| 165 | for m in self.MarkArray.MarkRecord: |
| 166 | m.Class = class_indices.index (m.Class) |
| 167 | for l in self.LigatureArray.LigatureAttach: |
| 168 | for c in l.ComponentRecord: |
| 169 | c.LigatureAnchor = [c.LigatureAnchor[i] for i in class_indices] |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 170 | return bool (self.ClassCount and self.MarkArray.MarkCount and self.LigatureArray.LigatureCount) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 171 | else: |
| 172 | assert 0, "unknown format: %s" % self.Format |
| 173 | |
| 174 | @add_method(fontTools.ttLib.tables.otTables.MarkMarkPos) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 175 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 176 | if self.Format == 1: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 177 | mark1_indices = self.Mark1Coverage.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 178 | self.Mark1Array.MarkRecord = [self.Mark1Array.MarkRecord[i] for i in mark1_indices] |
| 179 | self.Mark1Array.MarkCount = len (self.Mark1Array.MarkRecord) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 180 | mark2_indices = self.Mark2Coverage.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 181 | self.Mark2Array.Mark2Record = [self.Mark2Array.Mark2Record[i] for i in mark2_indices] |
| 182 | self.Mark2Array.MarkCount = len (self.Mark2Array.Mark2Record) |
Behdad Esfahbod | c6396b7 | 2013-07-22 12:31:33 -0400 | [diff] [blame] | 183 | # Prune empty classes |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 184 | class_indices = unique_sorted (v.Class for v in self.Mark1Array.MarkRecord) |
Behdad Esfahbod | c6396b7 | 2013-07-22 12:31:33 -0400 | [diff] [blame] | 185 | self.ClassCount = len (class_indices) |
| 186 | for m in self.Mark1Array.MarkRecord: |
| 187 | m.Class = class_indices.index (m.Class) |
| 188 | for b in self.Mark2Array.Mark2Record: |
| 189 | b.Mark2Anchor = [b.Mark2Anchor[i] for i in class_indices] |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 190 | return bool (self.ClassCount and self.Mark1Array.MarkCount and self.Mark2Array.MarkCount) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 191 | else: |
| 192 | assert 0, "unknown format: %s" % self.Format |
| 193 | |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 194 | @add_method(fontTools.ttLib.tables.otTables.SingleSubst, |
| 195 | fontTools.ttLib.tables.otTables.MultipleSubst, |
| 196 | fontTools.ttLib.tables.otTables.AlternateSubst, |
| 197 | fontTools.ttLib.tables.otTables.LigatureSubst, |
| 198 | fontTools.ttLib.tables.otTables.ReverseChainSingleSubst, |
| 199 | fontTools.ttLib.tables.otTables.SinglePos, |
| 200 | fontTools.ttLib.tables.otTables.PairPos, |
| 201 | fontTools.ttLib.tables.otTables.CursivePos, |
| 202 | fontTools.ttLib.tables.otTables.MarkBasePos, |
| 203 | fontTools.ttLib.tables.otTables.MarkLigPos, |
| 204 | fontTools.ttLib.tables.otTables.MarkMarkPos) |
| 205 | def subset_lookups (self, lookup_indices): |
| 206 | pass |
| 207 | |
| 208 | @add_method(fontTools.ttLib.tables.otTables.SingleSubst, |
| 209 | fontTools.ttLib.tables.otTables.MultipleSubst, |
| 210 | fontTools.ttLib.tables.otTables.AlternateSubst, |
| 211 | fontTools.ttLib.tables.otTables.LigatureSubst, |
| 212 | fontTools.ttLib.tables.otTables.ReverseChainSingleSubst, |
| 213 | fontTools.ttLib.tables.otTables.SinglePos, |
| 214 | fontTools.ttLib.tables.otTables.PairPos, |
| 215 | fontTools.ttLib.tables.otTables.CursivePos, |
| 216 | fontTools.ttLib.tables.otTables.MarkBasePos, |
| 217 | fontTools.ttLib.tables.otTables.MarkLigPos, |
| 218 | fontTools.ttLib.tables.otTables.MarkMarkPos) |
| 219 | def collect_lookups (self): |
| 220 | return [] |
| 221 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 222 | @add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ContextPos) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 223 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 224 | if self.Format == 1: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 225 | indices = self.Coverage.subset_glyphs (glyphs) |
Behdad Esfahbod | b7fef90 | 2013-07-21 22:48:08 -0400 | [diff] [blame] | 226 | self.SubRuleSet = [self.SubRuleSet[i] for i in indices] |
| 227 | self.SubRuleSetCount = len (self.SubRuleSet) |
| 228 | for rs in self.SubRuleSet: |
| 229 | rs.SubRule = [r for r in rs.SubRule |
| 230 | if all (g in glyphs for g in r.Input)] |
| 231 | rs.SubRuleCount = len (rs.SubRule) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 232 | # Prune empty subrulesets |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 233 | return bool (self.SubRuleSetCount) |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 234 | elif self.Format == 2: |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 235 | return bool (self.Coverage.subset_glyphs (glyphs) and self.ClassDef.subset_glyphs (glyphs)) |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 236 | elif self.Format == 3: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 237 | return all (c.subset_glyphs (glyphs) for c in self.Coverage) |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 238 | else: |
| 239 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 240 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 241 | @add_method(fontTools.ttLib.tables.otTables.ChainContextSubst, fontTools.ttLib.tables.otTables.ChainContextPos) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 242 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 243 | if self.Format == 1: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 244 | indices = self.Coverage.subset_glyphs (glyphs) |
Behdad Esfahbod | b7fef90 | 2013-07-21 22:48:08 -0400 | [diff] [blame] | 245 | self.ChainSubRuleSet = [self.ChainSubRuleSet[i] for i in indices] |
| 246 | self.ChainSubRuleSetCount = len (self.ChainSubRuleSet) |
| 247 | for rs in self.ChainSubRuleSet: |
| 248 | rs.ChainSubRule = [r for r in rs.ChainSubRule |
| 249 | if all (g in glyphs for g in r.Backtrack + r.Input + r.LookAhead)] |
| 250 | rs.ChainSubRuleCount = len (rs.ChainSubRule) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 251 | # Prune empty subrulesets |
| 252 | return self.ChainSubRuleSetCount |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 253 | elif self.Format == 2: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 254 | return self.Coverage.subset_glyphs (glyphs) and \ |
| 255 | self.LookAheadClassDef.subset_glyphs (glyphs) and \ |
| 256 | self.BacktrackClassDef.subset_glyphs (glyphs) and \ |
| 257 | self.InputClassDef.subset_glyphs (glyphs) |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 258 | elif self.Format == 3: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 259 | return all (c.subset_glyphs (glyphs) for c in self.InputCoverage + self.LookAheadCoverage + self.BacktrackCoverage) |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 260 | else: |
| 261 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 262 | |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 263 | @add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ContextPos, |
| 264 | fontTools.ttLib.tables.otTables.ChainContextSubst, fontTools.ttLib.tables.otTables.ChainContextPos) |
| 265 | def subset_lookups (self, lookup_indices): |
| 266 | self.SubstLookupRecord = [ll for ll in self.SubstLookupRecord if ll.LookupListIndex in lookup_indices] |
| 267 | for ll in self.SubstLookupRecord: |
| 268 | ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex) |
| 269 | |
| 270 | @add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ContextPos, |
| 271 | fontTools.ttLib.tables.otTables.ChainContextSubst, fontTools.ttLib.tables.otTables.ChainContextPos) |
| 272 | def collect_lookups (self): |
| 273 | return [ll.LookupListIndex for ll in self.SubstLookupRecord] |
| 274 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 275 | @add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 276 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 277 | if self.Format == 1: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 278 | return self.ExtSubTable.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 279 | else: |
| 280 | assert 0, "unknown format: %s" % self.Format |
| 281 | |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 282 | @add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos) |
| 283 | def subset_lookups (self, lookup_indices): |
| 284 | if self.Format == 1: |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 285 | self.ExtSubTable.subset_lookups (lookup_indices) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 286 | else: |
| 287 | assert 0, "unknown format: %s" % self.Format |
| 288 | |
| 289 | @add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos) |
| 290 | def collect_lookups (self): |
| 291 | if self.Format == 1: |
| 292 | return self.ExtSubTable.collect_lookups () |
| 293 | else: |
| 294 | assert 0, "unknown format: %s" % self.Format |
| 295 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 296 | @add_method(fontTools.ttLib.tables.otTables.Lookup) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 297 | def subset_glyphs (self, glyphs): |
| 298 | self.SubTable = [s for s in self.SubTable if s.subset_glyphs (glyphs)] |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 299 | self.SubTableCount = len (self.SubTable) |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 300 | return bool (self.SubTableCount) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 301 | |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 302 | @add_method(fontTools.ttLib.tables.otTables.Lookup) |
| 303 | def subset_lookups (self, lookup_indices): |
| 304 | for s in self.SubTable: |
| 305 | s.subset_lookups (lookup_indices) |
| 306 | |
| 307 | @add_method(fontTools.ttLib.tables.otTables.Lookup) |
| 308 | def collect_lookups (self): |
| 309 | return unique_sorted (sum ((s.collect_lookups () for s in self.SubTable), [])) |
| 310 | |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 311 | @add_method(fontTools.ttLib.tables.otTables.LookupList) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 312 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 313 | "Returns the indices of nonempty lookups." |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 314 | return [i for (i,l) in enumerate (self.Lookup) if l.subset_glyphs (glyphs)] |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 315 | |
| 316 | @add_method(fontTools.ttLib.tables.otTables.LookupList) |
| 317 | def subset_lookups (self, lookup_indices): |
| 318 | self.Lookup = [self.Lookup[i] for i in lookup_indices] |
| 319 | self.LookupCount = len (self.Lookup) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 320 | for l in self.Lookup: |
| 321 | l.subset_lookups (lookup_indices) |
| 322 | |
| 323 | @add_method(fontTools.ttLib.tables.otTables.LookupList) |
| 324 | def closure_lookups (self, lookup_indices): |
| 325 | while True: |
| 326 | recurse_lookups = sum ((self.Lookup[i].collect_lookups () for i in lookup_indices), []) |
| 327 | recurse_lookups = [l for l in recurse_lookups if l not in lookup_indices] |
| 328 | if not recurse_lookups: |
| 329 | return lookup_indices |
| 330 | lookup_indices = unique_sorted (lookup_indices + recurse_lookups) |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 331 | |
| 332 | @add_method(fontTools.ttLib.tables.otTables.Feature) |
| 333 | def subset_lookups (self, lookup_indices): |
| 334 | self.LookupListIndex = [l for l in self.LookupListIndex if l in lookup_indices] |
| 335 | # Now map them. |
| 336 | self.LookupListIndex = [lookup_indices.index (l) for l in self.LookupListIndex] |
| 337 | self.LookupCount = len (self.LookupListIndex) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 338 | return self.LookupCount |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 339 | |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 340 | @add_method(fontTools.ttLib.tables.otTables.Feature) |
| 341 | def collect_lookups (self): |
| 342 | return self.LookupListIndex[:] |
| 343 | |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 344 | @add_method(fontTools.ttLib.tables.otTables.FeatureList) |
| 345 | def subset_lookups (self, lookup_indices): |
| 346 | "Returns the indices of nonempty features." |
| 347 | feature_indices = [i for (i,f) in enumerate (self.FeatureRecord) if f.Feature.subset_lookups (lookup_indices)] |
| 348 | self.FeatureRecord = [self.FeatureRecord[i] for i in feature_indices] |
| 349 | self.FeatureCount = len (self.FeatureRecord) |
| 350 | return feature_indices |
| 351 | |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 352 | @add_method(fontTools.ttLib.tables.otTables.FeatureList) |
| 353 | def collect_lookups (self, feature_indices): |
| 354 | return unique_sorted (sum ((self.FeatureRecord[i].Feature.collect_lookups () for i in feature_indices |
| 355 | if i < self.FeatureCount), [])) |
| 356 | |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 357 | @add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys) |
| 358 | def subset_features (self, feature_indices): |
Behdad Esfahbod | 69ce150 | 2013-07-22 18:00:31 -0400 | [diff] [blame] | 359 | if self.ReqFeatureIndex in feature_indices: |
| 360 | self.ReqFeatureIndex = feature_indices.index (self.ReqFeatureIndex) |
| 361 | else: |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 362 | self.ReqFeatureIndex = 65535 |
| 363 | self.FeatureIndex = [f for f in self.FeatureIndex if f in feature_indices] |
Behdad Esfahbod | 69ce150 | 2013-07-22 18:00:31 -0400 | [diff] [blame] | 364 | # Now map them. |
| 365 | self.FeatureIndex = [feature_indices.index (f) for f in self.FeatureIndex if f in feature_indices] |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 366 | self.FeatureCount = len (self.FeatureIndex) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 367 | return self.FeatureCount or self.ReqFeatureIndex != 65535 |
| 368 | |
| 369 | @add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys) |
| 370 | def collect_features (self): |
| 371 | feature_indices = self.FeatureIndex[:] |
| 372 | if self.ReqFeatureIndex != 65535: |
| 373 | feature_indices.append (self.ReqFeatureIndex) |
| 374 | return unique_sorted (feature_indices) |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 375 | |
| 376 | @add_method(fontTools.ttLib.tables.otTables.Script) |
| 377 | def subset_features (self, feature_indices): |
| 378 | if self.DefaultLangSys and not self.DefaultLangSys.subset_features (feature_indices): |
| 379 | self.DefaultLangSys = None |
| 380 | self.LangSysRecord = [l for l in self.LangSysRecord if l.LangSys.subset_features (feature_indices)] |
| 381 | self.LangSysCount = len (self.LangSysRecord) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 382 | return self.LangSysCount or self.DefaultLangSys |
| 383 | |
| 384 | @add_method(fontTools.ttLib.tables.otTables.Script) |
| 385 | def collect_features (self): |
| 386 | feature_indices = [l.LangSys.collectFeatures () for l in self.LangSysRecord] |
| 387 | if self.DefaultLangSys: |
| 388 | feature_indices.append (self.DefaultLangSys.collect_features ()) |
| 389 | return unique_sorted (sum (feature_indices, [])) |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 390 | |
| 391 | @add_method(fontTools.ttLib.tables.otTables.ScriptList) |
| 392 | def subset_features (self, feature_indices): |
| 393 | self.ScriptRecord = [s for s in self.ScriptRecord if s.Script.subset_features (feature_indices)] |
| 394 | self.ScriptCount = len (self.ScriptRecord) |
| 395 | return self.ScriptCount |
| 396 | |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 397 | @add_method(fontTools.ttLib.tables.otTables.ScriptList) |
| 398 | def collect_features (self): |
| 399 | return unique_sorted (sum ((s.Script.collect_features () for s in self.ScriptRecord), [])) |
| 400 | |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 401 | @add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS')) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 402 | def subset_glyphs (self, glyphs): |
| 403 | lookup_indices = self.table.LookupList.subset_glyphs (glyphs) |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 404 | self.subset_lookups (lookup_indices) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 405 | self.prune_lookups () |
| 406 | return True |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 407 | |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 408 | @add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS')) |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 409 | def subset_lookups (self, lookup_indices): |
| 410 | "Retrains specified lookups, then removes empty features, language systems, and scripts." |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 411 | self.table.LookupList.subset_lookups (lookup_indices) |
| 412 | feature_indices = self.table.FeatureList.subset_lookups (lookup_indices) |
| 413 | self.table.ScriptList.subset_features (feature_indices) |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 414 | |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 415 | @add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS')) |
| 416 | def prune_lookups (self): |
| 417 | "Remove unreferenced lookups" |
| 418 | feature_indices = self.table.ScriptList.collect_features () |
| 419 | lookup_indices = self.table.FeatureList.collect_lookups (feature_indices) |
| 420 | lookup_indices = self.table.LookupList.closure_lookups (lookup_indices) |
| 421 | self.subset_lookups (lookup_indices) |
| 422 | |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 423 | @add_method(fontTools.ttLib.getTableClass('GDEF')) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 424 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 425 | table = self.table |
| 426 | if table.LigCaretList: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 427 | indices = table.LigCaretList.Coverage.subset_glyphs (glyphs) |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 428 | table.LigCaretList.LigGlyph = [table.LigCaretList.LigGlyph[i] for i in indices] |
| 429 | table.LigCaretList.LigGlyphCount = len (table.LigCaretList.LigGlyph) |
| 430 | if not table.LigCaretList.LigGlyphCount: |
| 431 | table.LigCaretList = None |
| 432 | if table.MarkAttachClassDef: |
| 433 | table.MarkAttachClassDef.classDefs = {g:v for g,v in table.MarkAttachClassDef.classDefs.items() if g in glyphs} |
| 434 | if not table.MarkAttachClassDef.classDefs: |
| 435 | table.MarkAttachClassDef = None |
| 436 | if table.GlyphClassDef: |
| 437 | table.GlyphClassDef.classDefs = {g:v for g,v in table.GlyphClassDef.classDefs.items() if g in glyphs} |
| 438 | if not table.GlyphClassDef.classDefs: |
| 439 | table.GlyphClassDef = None |
| 440 | if table.AttachList: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 441 | indices = table.AttachList.Coverage.subset_glyphs (glyphs) |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 442 | table.AttachList.AttachPoint = [table.AttachList.AttachPoint[i] for i in indices] |
| 443 | table.AttachList.GlyphCount = len (table.AttachList.AttachPoint) |
| 444 | if not table.AttachList.GlyphCount: |
| 445 | table.AttachList = None |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 446 | return bool (table.LigCaretList or table.MarkAttachClassDef or table.GlyphClassDef or table.AttachList) |
Behdad Esfahbod | efb984a | 2013-07-21 22:26:16 -0400 | [diff] [blame] | 447 | |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 448 | @add_method(fontTools.ttLib.getTableClass('kern')) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 449 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5270ec4 | 2013-07-22 12:57:02 -0400 | [diff] [blame] | 450 | for t in self.kernTables: |
| 451 | t.kernTable = {(a,b):v for ((a,b),v) in t.kernTable.items() if a in glyphs and b in glyphs} |
| 452 | self.kernTables = [t for t in self.kernTables if t.kernTable] |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 453 | return bool (self.kernTables) |
Behdad Esfahbod | efb984a | 2013-07-21 22:26:16 -0400 | [diff] [blame] | 454 | |
Behdad Esfahbod | 75e14fc | 2013-07-22 14:49:54 -0400 | [diff] [blame] | 455 | @add_method(fontTools.ttLib.getTableClass('hmtx'), fontTools.ttLib.getTableClass('vmtx')) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 456 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | c716044 | 2013-07-22 14:29:08 -0400 | [diff] [blame] | 457 | self.metrics = {g:v for (g,v) in self.metrics.items() if g in glyphs} |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 458 | return bool (self.metrics) |
Behdad Esfahbod | c716044 | 2013-07-22 14:29:08 -0400 | [diff] [blame] | 459 | |
Behdad Esfahbod | 75e14fc | 2013-07-22 14:49:54 -0400 | [diff] [blame] | 460 | @add_method(fontTools.ttLib.getTableClass('hdmx')) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 461 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 75e14fc | 2013-07-22 14:49:54 -0400 | [diff] [blame] | 462 | self.hdmx = {s:{g:v for (g,v) in l.items() if g in glyphs} for (s,l) in self.hdmx.items()} |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 463 | return bool (self.hdmx) |
Behdad Esfahbod | 75e14fc | 2013-07-22 14:49:54 -0400 | [diff] [blame] | 464 | |
Behdad Esfahbod | e45d6af | 2013-07-22 15:29:17 -0400 | [diff] [blame] | 465 | @add_method(fontTools.ttLib.getTableClass('VORG')) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 466 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | e45d6af | 2013-07-22 15:29:17 -0400 | [diff] [blame] | 467 | self.VOriginRecords = {g:v for (g,v) in self.VOriginRecords.items() if g in glyphs} |
| 468 | self.numVertOriginYMetrics = len (self.VOriginRecords) |
| 469 | return True # Never drop; has default metrics |
| 470 | |
Behdad Esfahbod | 8c646f6 | 2013-07-22 15:06:23 -0400 | [diff] [blame] | 471 | @add_method(fontTools.ttLib.getTableClass('post')) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 472 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 4c2b6eb | 2013-07-23 10:57:56 -0400 | [diff] [blame] | 473 | # TODO Drop names? How? |
Behdad Esfahbod | 209eb41 | 2013-07-22 18:54:36 -0400 | [diff] [blame] | 474 | self.extraNames = [] |
Behdad Esfahbod | c9dec9d | 2013-07-23 10:28:47 -0400 | [diff] [blame] | 475 | return True |
Behdad Esfahbod | 653e974 | 2013-07-22 15:17:12 -0400 | [diff] [blame] | 476 | |
Behdad Esfahbod | 861d915 | 2013-07-22 16:47:24 -0400 | [diff] [blame] | 477 | @add_method(fontTools.ttLib.getTableClass('glyf')) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 478 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 861d915 | 2013-07-22 16:47:24 -0400 | [diff] [blame] | 479 | self.glyphs = {g:v for (g,v) in self.glyphs.items() if g in glyphs} |
| 480 | self.glyphOrder = [g for g in self.glyphOrder if g in glyphs] |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 481 | return bool (self.glyphs) |
Behdad Esfahbod | 861d915 | 2013-07-22 16:47:24 -0400 | [diff] [blame] | 482 | |
Behdad Esfahbod | 653e974 | 2013-07-22 15:17:12 -0400 | [diff] [blame] | 483 | @add_method(fontTools.ttLib.getTableClass('cmap')) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 484 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 653e974 | 2013-07-22 15:17:12 -0400 | [diff] [blame] | 485 | for t in self.tables: |
Behdad Esfahbod | 9453a36 | 2013-07-22 16:21:24 -0400 | [diff] [blame] | 486 | # For reasons I don't understand I need this here |
| 487 | # to force decompilation of the cmap format 14. |
| 488 | try: |
| 489 | getattr (t, "asdf") |
| 490 | except AttributeError: |
| 491 | pass |
Behdad Esfahbod | b13d790 | 2013-07-22 16:01:15 -0400 | [diff] [blame] | 492 | if t.format == 14: |
Behdad Esfahbod | 9453a36 | 2013-07-22 16:21:24 -0400 | [diff] [blame] | 493 | # XXX We drop all the default-UVS mappings (g==None) |
Behdad Esfahbod | b13d790 | 2013-07-22 16:01:15 -0400 | [diff] [blame] | 494 | t.uvsDict = {v:[(u,g) for (u,g) in l if g in glyphs] for (v,l) in t.uvsDict.items()} |
| 495 | t.uvsDict = {v:l for (v,l) in t.uvsDict.items() if l} |
| 496 | else: |
| 497 | t.cmap = {u:g for (u,g) in t.cmap.items() if g in glyphs} |
| 498 | self.tables = [t for t in self.tables if (t.cmap if t.format != 14 else t.uvsDict)] |
Behdad Esfahbod | 7e4bfc3 | 2013-07-22 18:47:32 -0400 | [diff] [blame] | 499 | # XXX Convert formats when needed |
Behdad Esfahbod | 61addb4 | 2013-07-23 11:03:49 -0400 | [diff] [blame^] | 500 | return bool (self.tables) |
| 501 | |
| 502 | |
| 503 | @add_method(fontTools.ttLib.getTableClass('cmap')) |
| 504 | def prune (self, options): |
| 505 | # Drop non-Unicode / non-Symbol cmaps |
| 506 | # TODO Add option for this |
| 507 | # TODO Only keep one subtable? |
| 508 | self.tables = [t for t in self.tables if t.platformID == 3 and t.platEncID in [0, 1, 10]] |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 509 | # For now, drop format=0 which can't be subset_glyphs easily? |
Behdad Esfahbod | f3744d9 | 2013-07-22 17:04:04 -0400 | [diff] [blame] | 510 | self.tables = [t for t in self.tables if t.format != 0] |
Behdad Esfahbod | 61addb4 | 2013-07-23 11:03:49 -0400 | [diff] [blame^] | 511 | |
| 512 | @add_method(fontTools.ttLib.getTableClass('name')) |
| 513 | def prune (self, options): |
| 514 | # TODO Make sure something remains? |
| 515 | # TODO Add option for this. |
| 516 | # TODO Drop even more (license, etc)? / Drop completely? |
| 517 | self.names = [n for n in self.names if n.platformID == 3 and n.platEncID == 1 and n.langID == 0x0409] |
| 518 | return bool (self.names) |
Behdad Esfahbod | 653e974 | 2013-07-22 15:17:12 -0400 | [diff] [blame] | 519 | |
Behdad Esfahbod | 8c646f6 | 2013-07-22 15:06:23 -0400 | [diff] [blame] | 520 | |
Behdad Esfahbod | 75e14fc | 2013-07-22 14:49:54 -0400 | [diff] [blame] | 521 | # TODO OS/2 ulUnicodeRange / ulCodePageRange? |
Behdad Esfahbod | 1e3551a | 2013-07-22 18:03:10 -0400 | [diff] [blame] | 522 | # TODO Drop unneeded GSUB/GPOS entries |
| 523 | # TODO Move glyf component closure after GSUB/GPOS/etc |
Behdad Esfahbod | 56ebd04 | 2013-07-22 13:02:24 -0400 | [diff] [blame] | 524 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 525 | if __name__ == '__main__': |
| 526 | |
| 527 | import sys |
| 528 | |
Behdad Esfahbod | 4ae8171 | 2013-07-22 11:57:13 -0400 | [diff] [blame] | 529 | verbose = False |
| 530 | if "--verbose" in sys.argv: |
| 531 | verbose = True |
| 532 | sys.argv.remove ("--verbose") |
Behdad Esfahbod | 350a527 | 2013-07-22 12:02:16 -0400 | [diff] [blame] | 533 | xml = False |
| 534 | if "--xml" in sys.argv: |
| 535 | xml = True |
| 536 | sys.argv.remove ("--xml") |
Behdad Esfahbod | 4ae8171 | 2013-07-22 11:57:13 -0400 | [diff] [blame] | 537 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 538 | if len (sys.argv) < 3: |
| 539 | print >>sys.stderr, "usage: pyotlss.py font-file glyph..." |
| 540 | sys.exit (1) |
| 541 | |
| 542 | fontfile = sys.argv[1] |
| 543 | glyphs = sys.argv[2:] |
| 544 | |
Behdad Esfahbod | c716044 | 2013-07-22 14:29:08 -0400 | [diff] [blame] | 545 | # Always include .notdef; anything else? |
Behdad Esfahbod | 0f86ce9 | 2013-07-22 17:30:31 -0400 | [diff] [blame] | 546 | glyphs.append ('.notdef') |
Behdad Esfahbod | c716044 | 2013-07-22 14:29:08 -0400 | [diff] [blame] | 547 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 548 | font = fontTools.ttx.TTFont (fontfile) |
Behdad Esfahbod | 0f86ce9 | 2013-07-22 17:30:31 -0400 | [diff] [blame] | 549 | font.disassembleInstructions = False |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 550 | |
| 551 | names = font.getGlyphNames() |
| 552 | # Convert to glyph names |
| 553 | glyphs = [g if g in names else font.getGlyphName(int(g)) for g in glyphs] |
| 554 | |
Behdad Esfahbod | 2a784ac | 2013-07-22 17:00:36 -0400 | [diff] [blame] | 555 | # Close over composite glyphs |
| 556 | if 'glyf' in font: |
| 557 | glyf = font['glyf'] |
Behdad Esfahbod | c9dec9d | 2013-07-23 10:28:47 -0400 | [diff] [blame] | 558 | # XXX Rinse & repeat |
Behdad Esfahbod | 2a784ac | 2013-07-22 17:00:36 -0400 | [diff] [blame] | 559 | for g in glyphs: |
| 560 | gl = glyf[g] |
| 561 | if gl.isComposite (): |
| 562 | for c in gl.components: |
| 563 | if c.glyphName not in glyphs: |
| 564 | glyphs.append (c.glyphName) |
Behdad Esfahbod | c9dec9d | 2013-07-23 10:28:47 -0400 | [diff] [blame] | 565 | if verbose: |
| 566 | print "Retaining %d glyphs: " % len (glyphs), glyphs |
Behdad Esfahbod | 2a784ac | 2013-07-22 17:00:36 -0400 | [diff] [blame] | 567 | |
Behdad Esfahbod | 350a527 | 2013-07-22 12:02:16 -0400 | [diff] [blame] | 568 | if xml: |
Behdad Esfahbod | 4ae8171 | 2013-07-22 11:57:13 -0400 | [diff] [blame] | 569 | import xmlWriter |
| 570 | writer = xmlWriter.XMLWriter (sys.stdout) |
Behdad Esfahbod | 4e214e4 | 2013-07-22 13:13:49 -0400 | [diff] [blame] | 571 | |
Behdad Esfahbod | fc54662 | 2013-07-23 10:31:57 -0400 | [diff] [blame] | 572 | drop_tables = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', 'PCLT', 'LTSH', 'VDMX'] |
Behdad Esfahbod | 61addb4 | 2013-07-23 11:03:49 -0400 | [diff] [blame^] | 573 | no_subset_tables = ['gasp', 'head', 'hhea', 'maxp', 'vhea', 'OS/2', 'loca', 'name'] |
Behdad Esfahbod | cd70885 | 2013-07-22 14:30:23 -0400 | [diff] [blame] | 574 | |
Behdad Esfahbod | b1e1ab6 | 2013-07-22 15:24:09 -0400 | [diff] [blame] | 575 | # For now drop these |
| 576 | drop_tables += ['cvt ', 'fpgm', 'prep'] |
| 577 | |
Behdad Esfahbod | 61addb4 | 2013-07-23 11:03:49 -0400 | [diff] [blame^] | 578 | prune_options = [] |
| 579 | |
Behdad Esfahbod | 8842ce2 | 2013-07-22 13:01:33 -0400 | [diff] [blame] | 580 | for tag in font.keys(): |
Behdad Esfahbod | 4e214e4 | 2013-07-22 13:13:49 -0400 | [diff] [blame] | 581 | |
Behdad Esfahbod | c716044 | 2013-07-22 14:29:08 -0400 | [diff] [blame] | 582 | if tag == 'GlyphOrder': |
| 583 | continue |
| 584 | |
Behdad Esfahbod | 4e214e4 | 2013-07-22 13:13:49 -0400 | [diff] [blame] | 585 | if tag in drop_tables: |
Behdad Esfahbod | c716044 | 2013-07-22 14:29:08 -0400 | [diff] [blame] | 586 | if verbose: |
| 587 | print tag, "dropped." |
Behdad Esfahbod | 4e214e4 | 2013-07-22 13:13:49 -0400 | [diff] [blame] | 588 | del font[tag] |
| 589 | continue |
| 590 | |
Behdad Esfahbod | 8842ce2 | 2013-07-22 13:01:33 -0400 | [diff] [blame] | 591 | clazz = fontTools.ttLib.getTableClass(tag) |
Behdad Esfahbod | 4e214e4 | 2013-07-22 13:13:49 -0400 | [diff] [blame] | 592 | |
Behdad Esfahbod | 61addb4 | 2013-07-23 11:03:49 -0400 | [diff] [blame^] | 593 | if 'prune' in vars (clazz): |
| 594 | table = font[tag] |
| 595 | table.prune (prune_options) |
Behdad Esfahbod | 5270ec4 | 2013-07-22 12:57:02 -0400 | [diff] [blame] | 596 | if verbose: |
Behdad Esfahbod | 61addb4 | 2013-07-23 11:03:49 -0400 | [diff] [blame^] | 597 | print tag, "pruned." |
| 598 | |
| 599 | if tag in no_subset_tables: |
| 600 | if verbose: |
| 601 | print tag, "subsetting not needed." |
| 602 | elif 'subset_glyphs' in vars (clazz): |
| 603 | table = font[tag] |
| 604 | if not table.subset_glyphs (glyphs): |
| 605 | del font[tag] |
| 606 | if verbose: |
| 607 | print tag, "subsetted to empty; dropped." |
| 608 | else: |
| 609 | if verbose: |
| 610 | print tag, "subsetted." |
Behdad Esfahbod | 4ae8171 | 2013-07-22 11:57:13 -0400 | [diff] [blame] | 611 | else: |
Behdad Esfahbod | 350a527 | 2013-07-22 12:02:16 -0400 | [diff] [blame] | 612 | if verbose: |
Behdad Esfahbod | 61addb4 | 2013-07-23 11:03:49 -0400 | [diff] [blame^] | 613 | print tag, "NOT subset; don't know how to subset." |
| 614 | continue |
| 615 | |
Behdad Esfahbod | c716044 | 2013-07-22 14:29:08 -0400 | [diff] [blame] | 616 | |
| 617 | glyphOrder = font.getGlyphOrder() |
| 618 | glyphOrder = [g for g in glyphOrder if g in glyphs] |
| 619 | font.setGlyphOrder (glyphOrder) |
| 620 | font._buildReverseGlyphOrderDict () |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 621 | |
Behdad Esfahbod | 0f86ce9 | 2013-07-22 17:30:31 -0400 | [diff] [blame] | 622 | if xml: |
| 623 | for tag in font.keys(): |
| 624 | writer.begintag (tag) |
| 625 | writer.newline () |
| 626 | font[tag].toXML(writer, font) |
| 627 | writer.endtag (tag) |
| 628 | writer.newline () |
| 629 | |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 630 | font.save (fontfile + '.subset') |