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