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 | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 36 | def intersect_glyphs (self, glyphs): |
| 37 | "Returns ascending list of matching coverage values." |
| 38 | return [i for (i,g) in enumerate (self.glyphs) if g in glyphs] |
| 39 | |
| 40 | @add_method(fontTools.ttLib.tables.otTables.Coverage) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 41 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | d821ea0 | 2013-07-23 10:50:43 -0400 | [diff] [blame] | 42 | "Returns ascending list of remaining coverage values." |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 43 | indices = self.intersect_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 44 | self.glyphs = [g for g in self.glyphs if g in glyphs] |
| 45 | return indices |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 46 | |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 47 | @add_method(fontTools.ttLib.tables.otTables.ClassDef) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 48 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 4aa6ce3 | 2013-07-22 12:15:36 -0400 | [diff] [blame] | 49 | "Returns ascending list of remaining classes." |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 50 | 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] | 51 | return unique_sorted (self.classDefs.values ()) |
Behdad Esfahbod | 4aa6ce3 | 2013-07-22 12:15:36 -0400 | [diff] [blame] | 52 | |
| 53 | @add_method(fontTools.ttLib.tables.otTables.ClassDef) |
| 54 | def remap (self, class_map): |
| 55 | "Remaps classes." |
| 56 | 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] | 57 | |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 58 | @add_method(fontTools.ttLib.tables.otTables.SingleSubst) |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 59 | def closure_glyphs (self, glyphs, table): |
| 60 | if self.Format in [1, 2]: |
| 61 | return [v for g,v in self.mapping.items() if g in glyphs] |
| 62 | else: |
| 63 | assert 0, "unknown format: %s" % self.Format |
| 64 | |
| 65 | @add_method(fontTools.ttLib.tables.otTables.SingleSubst) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 66 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 67 | if self.Format in [1, 2]: |
| 68 | 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] | 69 | return bool (self.mapping) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 70 | else: |
| 71 | assert 0, "unknown format: %s" % self.Format |
| 72 | |
| 73 | @add_method(fontTools.ttLib.tables.otTables.MultipleSubst) |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 74 | def closure_glyphs (self, glyphs, table): |
| 75 | if self.Format == 1: |
| 76 | indices = self.Coverage.intersect_glyphs (glyphs) |
| 77 | return sum ((self.Sequence[i].Substitute for i in indices), []) |
| 78 | else: |
| 79 | assert 0, "unknown format: %s" % self.Format |
| 80 | |
| 81 | @add_method(fontTools.ttLib.tables.otTables.MultipleSubst) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 82 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 83 | if self.Format == 1: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 84 | indices = self.Coverage.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 85 | self.Sequence = [self.Sequence[i] for i in indices] |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 86 | self.SequenceCount = len (self.Sequence) |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 87 | return bool (self.SequenceCount) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 88 | else: |
| 89 | assert 0, "unknown format: %s" % self.Format |
| 90 | |
| 91 | @add_method(fontTools.ttLib.tables.otTables.AlternateSubst) |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 92 | def closure_glyphs (self, glyphs, table): |
| 93 | if self.Format == 1: |
| 94 | return sum ((v for g,v in self.alternates.items() if g in glyphs), []) |
| 95 | else: |
| 96 | assert 0, "unknown format: %s" % self.Format |
| 97 | |
| 98 | @add_method(fontTools.ttLib.tables.otTables.AlternateSubst) |
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: |
| 101 | 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] | 102 | return bool (self.alternates) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 103 | else: |
| 104 | assert 0, "unknown format: %s" % self.Format |
| 105 | |
| 106 | @add_method(fontTools.ttLib.tables.otTables.LigatureSubst) |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 107 | def closure_glyphs (self, glyphs, table): |
| 108 | if self.Format == 1: |
| 109 | return sum (([seq.LigGlyph for seq in seqs if all(c in glyphs for c in seq.Component)] |
| 110 | for g,seqs in self.ligatures.items()), []) |
| 111 | else: |
| 112 | assert 0, "unknown format: %s" % self.Format |
| 113 | |
| 114 | @add_method(fontTools.ttLib.tables.otTables.LigatureSubst) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 115 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 116 | if self.Format == 1: |
| 117 | self.ligatures = {g:v for g,v in self.ligatures.items() if g in glyphs} |
| 118 | self.ligatures = {g:[seq for seq in seqs if all(c in glyphs for c in seq.Component)] |
| 119 | for g,seqs in self.ligatures.items()} |
| 120 | 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] | 121 | return bool (self.ligatures) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 122 | else: |
| 123 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 124 | |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 125 | @add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst) |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 126 | def closure_glyphs (self, glyphs, table): |
| 127 | if self.Format == 1: |
| 128 | indices = self.Coverage.intersect_glyphs (glyphs) |
| 129 | if not indices or \ |
| 130 | not all (c.intersect_glyphs (glyphs) for c in self.LookAheadCoverage + self.BacktrackCoverage): |
| 131 | return [] |
| 132 | return [self.Substitute[i] for i in indices] |
| 133 | else: |
| 134 | assert 0, "unknown format: %s" % self.Format |
| 135 | |
| 136 | @add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 137 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 138 | if self.Format == 1: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 139 | indices = self.Coverage.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 140 | self.Substitute = [self.Substitute[i] for i in indices] |
| 141 | self.GlyphCount = len (self.Substitute) |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 142 | 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] | 143 | else: |
| 144 | assert 0, "unknown format: %s" % self.Format |
| 145 | |
| 146 | @add_method(fontTools.ttLib.tables.otTables.SinglePos) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 147 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 148 | if self.Format == 1: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 149 | return len (self.Coverage.subset_glyphs (glyphs)) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 150 | elif self.Format == 2: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 151 | indices = self.Coverage.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 152 | self.Value = [self.Value[i] for i in indices] |
| 153 | self.ValueCount = len (self.Value) |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 154 | return bool (self.ValueCount) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 155 | else: |
| 156 | assert 0, "unknown format: %s" % self.Format |
| 157 | |
| 158 | @add_method(fontTools.ttLib.tables.otTables.PairPos) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 159 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 160 | if self.Format == 1: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 161 | indices = self.Coverage.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 162 | self.PairSet = [self.PairSet[i] for i in indices] |
| 163 | for p in self.PairSet: |
| 164 | p.PairValueRecord = [r for r in p.PairValueRecord if r.SecondGlyph in glyphs] |
| 165 | p.PairValueCount = len (p.PairValueRecord) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 166 | self.PairSet = [p for p in self.PairSet if p.PairValueCount] |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 167 | self.PairSetCount = len (self.PairSet) |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 168 | return bool (self.PairSetCount) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 169 | elif self.Format == 2: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 170 | class1_map = self.ClassDef1.subset_glyphs (glyphs) |
| 171 | class2_map = self.ClassDef2.subset_glyphs (glyphs) |
Behdad Esfahbod | 4aa6ce3 | 2013-07-22 12:15:36 -0400 | [diff] [blame] | 172 | self.ClassDef1.remap (class1_map) |
| 173 | self.ClassDef2.remap (class2_map) |
| 174 | self.Class1Record = [self.Class1Record[i] for i in class1_map] |
| 175 | for c in self.Class1Record: |
| 176 | c.Class2Record = [c.Class2Record[i] for i in class2_map] |
| 177 | self.Class1Count = len (class1_map) |
| 178 | self.Class2Count = len (class2_map) |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 179 | 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] | 180 | else: |
| 181 | assert 0, "unknown format: %s" % self.Format |
| 182 | |
| 183 | @add_method(fontTools.ttLib.tables.otTables.CursivePos) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 184 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 185 | if self.Format == 1: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 186 | indices = self.Coverage.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 187 | self.EntryExitRecord = [self.EntryExitRecord[i] for i in indices] |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 188 | self.EntryExitCount = len (self.EntryExitRecord) |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 189 | return bool (self.EntryExitCount) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 190 | else: |
| 191 | assert 0, "unknown format: %s" % self.Format |
| 192 | |
| 193 | @add_method(fontTools.ttLib.tables.otTables.MarkBasePos) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 194 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 195 | if self.Format == 1: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 196 | mark_indices = self.MarkCoverage.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 197 | self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices] |
| 198 | self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 199 | base_indices = self.BaseCoverage.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 200 | self.BaseArray.BaseRecord = [self.BaseArray.BaseRecord[i] for i in base_indices] |
| 201 | self.BaseArray.BaseCount = len (self.BaseArray.BaseRecord) |
Behdad Esfahbod | c6396b7 | 2013-07-22 12:31:33 -0400 | [diff] [blame] | 202 | # Prune empty classes |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 203 | class_indices = unique_sorted (v.Class for v in self.MarkArray.MarkRecord) |
Behdad Esfahbod | c6396b7 | 2013-07-22 12:31:33 -0400 | [diff] [blame] | 204 | self.ClassCount = len (class_indices) |
| 205 | for m in self.MarkArray.MarkRecord: |
| 206 | m.Class = class_indices.index (m.Class) |
| 207 | for b in self.BaseArray.BaseRecord: |
| 208 | b.BaseAnchor = [b.BaseAnchor[i] for i in class_indices] |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 209 | return bool (self.ClassCount and self.MarkArray.MarkCount and self.BaseArray.BaseCount) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 210 | else: |
| 211 | assert 0, "unknown format: %s" % self.Format |
| 212 | |
| 213 | @add_method(fontTools.ttLib.tables.otTables.MarkLigPos) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 214 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 215 | if self.Format == 1: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 216 | mark_indices = self.MarkCoverage.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 217 | self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] for i in mark_indices] |
| 218 | self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 219 | ligature_indices = self.LigatureCoverage.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 220 | self.LigatureArray.LigatureAttach = [self.LigatureArray.LigatureAttach[i] for i in ligature_indices] |
| 221 | self.LigatureArray.LigatureCount = len (self.LigatureArray.LigatureAttach) |
Behdad Esfahbod | c6396b7 | 2013-07-22 12:31:33 -0400 | [diff] [blame] | 222 | # Prune empty classes |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 223 | class_indices = unique_sorted (v.Class for v in self.MarkArray.MarkRecord) |
Behdad Esfahbod | c6396b7 | 2013-07-22 12:31:33 -0400 | [diff] [blame] | 224 | self.ClassCount = len (class_indices) |
| 225 | for m in self.MarkArray.MarkRecord: |
| 226 | m.Class = class_indices.index (m.Class) |
| 227 | for l in self.LigatureArray.LigatureAttach: |
| 228 | for c in l.ComponentRecord: |
| 229 | c.LigatureAnchor = [c.LigatureAnchor[i] for i in class_indices] |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 230 | return bool (self.ClassCount and self.MarkArray.MarkCount and self.LigatureArray.LigatureCount) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 231 | else: |
| 232 | assert 0, "unknown format: %s" % self.Format |
| 233 | |
| 234 | @add_method(fontTools.ttLib.tables.otTables.MarkMarkPos) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 235 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 236 | if self.Format == 1: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 237 | mark1_indices = self.Mark1Coverage.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 238 | self.Mark1Array.MarkRecord = [self.Mark1Array.MarkRecord[i] for i in mark1_indices] |
| 239 | self.Mark1Array.MarkCount = len (self.Mark1Array.MarkRecord) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 240 | mark2_indices = self.Mark2Coverage.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 241 | self.Mark2Array.Mark2Record = [self.Mark2Array.Mark2Record[i] for i in mark2_indices] |
| 242 | self.Mark2Array.MarkCount = len (self.Mark2Array.Mark2Record) |
Behdad Esfahbod | c6396b7 | 2013-07-22 12:31:33 -0400 | [diff] [blame] | 243 | # Prune empty classes |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 244 | class_indices = unique_sorted (v.Class for v in self.Mark1Array.MarkRecord) |
Behdad Esfahbod | c6396b7 | 2013-07-22 12:31:33 -0400 | [diff] [blame] | 245 | self.ClassCount = len (class_indices) |
| 246 | for m in self.Mark1Array.MarkRecord: |
| 247 | m.Class = class_indices.index (m.Class) |
| 248 | for b in self.Mark2Array.Mark2Record: |
| 249 | b.Mark2Anchor = [b.Mark2Anchor[i] for i in class_indices] |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 250 | return bool (self.ClassCount and self.Mark1Array.MarkCount and self.Mark2Array.MarkCount) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 251 | else: |
| 252 | assert 0, "unknown format: %s" % self.Format |
| 253 | |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 254 | @add_method(fontTools.ttLib.tables.otTables.SingleSubst, |
| 255 | fontTools.ttLib.tables.otTables.MultipleSubst, |
| 256 | fontTools.ttLib.tables.otTables.AlternateSubst, |
| 257 | fontTools.ttLib.tables.otTables.LigatureSubst, |
| 258 | fontTools.ttLib.tables.otTables.ReverseChainSingleSubst, |
| 259 | fontTools.ttLib.tables.otTables.SinglePos, |
| 260 | fontTools.ttLib.tables.otTables.PairPos, |
| 261 | fontTools.ttLib.tables.otTables.CursivePos, |
| 262 | fontTools.ttLib.tables.otTables.MarkBasePos, |
| 263 | fontTools.ttLib.tables.otTables.MarkLigPos, |
| 264 | fontTools.ttLib.tables.otTables.MarkMarkPos) |
| 265 | def subset_lookups (self, lookup_indices): |
| 266 | pass |
| 267 | |
| 268 | @add_method(fontTools.ttLib.tables.otTables.SingleSubst, |
| 269 | fontTools.ttLib.tables.otTables.MultipleSubst, |
| 270 | fontTools.ttLib.tables.otTables.AlternateSubst, |
| 271 | fontTools.ttLib.tables.otTables.LigatureSubst, |
| 272 | fontTools.ttLib.tables.otTables.ReverseChainSingleSubst, |
| 273 | fontTools.ttLib.tables.otTables.SinglePos, |
| 274 | fontTools.ttLib.tables.otTables.PairPos, |
| 275 | fontTools.ttLib.tables.otTables.CursivePos, |
| 276 | fontTools.ttLib.tables.otTables.MarkBasePos, |
| 277 | fontTools.ttLib.tables.otTables.MarkLigPos, |
| 278 | fontTools.ttLib.tables.otTables.MarkMarkPos) |
| 279 | def collect_lookups (self): |
| 280 | return [] |
| 281 | |
Behdad Esfahbod | 44c2b3c | 2013-07-23 16:00:32 -0400 | [diff] [blame] | 282 | |
| 283 | @add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst, |
| 284 | fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos) |
| 285 | def __classify_context (self): |
Behdad Esfahbod | 6870f8a | 2013-07-23 16:18:30 -0400 | [diff] [blame] | 286 | class ContextContext: |
| 287 | def __init__ (self, lookup): |
Behdad Esfahbod | 6870f8a | 2013-07-23 16:18:30 -0400 | [diff] [blame] | 288 | if lookup.__class__.__name__.endswith ('Subst'): |
Behdad Esfahbod | 9e73572 | 2013-07-23 16:35:23 -0400 | [diff] [blame] | 289 | Typ = 'Sub' |
| 290 | Type = 'Subst' |
Behdad Esfahbod | 6870f8a | 2013-07-23 16:18:30 -0400 | [diff] [blame] | 291 | else: |
Behdad Esfahbod | 9e73572 | 2013-07-23 16:35:23 -0400 | [diff] [blame] | 292 | Typ = 'Pos' |
| 293 | Type = 'Pos' |
| 294 | if lookup.__class__.__name__.startswith ('Chain'): |
| 295 | Chain = 'Chain' |
| 296 | else: |
| 297 | Chain = '' |
| 298 | ChainTyp = Chain+Typ |
| 299 | |
| 300 | self.Typ = Typ |
| 301 | self.Type = Type |
| 302 | self.Chain = Chain |
| 303 | self.ChainTyp = ChainTyp |
| 304 | |
| 305 | self.LookupRecord = Type+'LookupRecord' |
| 306 | |
Behdad Esfahbod | 2710839 | 2013-07-23 16:40:47 -0400 | [diff] [blame] | 307 | # Format 1 |
Behdad Esfahbod | 9e73572 | 2013-07-23 16:35:23 -0400 | [diff] [blame] | 308 | self.Rule = ChainTyp+'Rule' |
| 309 | self.RuleCount = ChainTyp+'RuleCount' |
| 310 | self.RuleSet = ChainTyp+'RuleSet' |
| 311 | self.RuleSetCount = ChainTyp+'RuleSetCount' |
Behdad Esfahbod | 707a37a | 2013-07-23 21:08:26 -0400 | [diff] [blame] | 312 | def ContextData (r, Format): |
Behdad Esfahbod | 1ab2dbf | 2013-07-23 17:17:21 -0400 | [diff] [blame] | 313 | if Format == 1: |
| 314 | return r.Input |
| 315 | elif Format == 2: |
Behdad Esfahbod | cbba4a6 | 2013-07-23 17:27:18 -0400 | [diff] [blame] | 316 | return [r.ClassDef] |
Behdad Esfahbod | 1ab2dbf | 2013-07-23 17:17:21 -0400 | [diff] [blame] | 317 | elif Format == 3: |
| 318 | return r.Coverage |
| 319 | else: |
| 320 | assert 0, "unknown format: %s" % Format |
Behdad Esfahbod | 707a37a | 2013-07-23 21:08:26 -0400 | [diff] [blame] | 321 | def ChainContextData (r, Format): |
Behdad Esfahbod | 1ab2dbf | 2013-07-23 17:17:21 -0400 | [diff] [blame] | 322 | if Format == 1: |
| 323 | return r.Backtrack + r.Input + r.LookAhead |
| 324 | elif Format == 2: |
Behdad Esfahbod | cbba4a6 | 2013-07-23 17:27:18 -0400 | [diff] [blame] | 325 | return [r.LookAheadClassDef, r.BacktrackClassDef, r.InputClassDef] |
Behdad Esfahbod | 1ab2dbf | 2013-07-23 17:17:21 -0400 | [diff] [blame] | 326 | elif Format == 3: |
| 327 | return r.InputCoverage + r.LookAheadCoverage + r.BacktrackCoverage |
| 328 | else: |
| 329 | assert 0, "unknown format: %s" % Format |
| 330 | if Chain: |
Behdad Esfahbod | 707a37a | 2013-07-23 21:08:26 -0400 | [diff] [blame] | 331 | self.ContextData = ChainContextData |
Behdad Esfahbod | 1ab2dbf | 2013-07-23 17:17:21 -0400 | [diff] [blame] | 332 | else: |
Behdad Esfahbod | 707a37a | 2013-07-23 21:08:26 -0400 | [diff] [blame] | 333 | self.ContextData = ContextData |
Behdad Esfahbod | 9e73572 | 2013-07-23 16:35:23 -0400 | [diff] [blame] | 334 | |
Behdad Esfahbod | 2710839 | 2013-07-23 16:40:47 -0400 | [diff] [blame] | 335 | # Format 2 |
| 336 | self.ClassRule = ChainTyp+'ClassRule' |
| 337 | self.ClassRuleCount = ChainTyp+'ClassRuleCount' |
| 338 | self.ClassRuleSet = ChainTyp+'ClassSet' |
| 339 | self.ClassRuleSetCount = ChainTyp+'ClassSetCount' |
| 340 | |
Behdad Esfahbod | 6870f8a | 2013-07-23 16:18:30 -0400 | [diff] [blame] | 341 | if not hasattr (self.__class__, "__ContextContext"): |
| 342 | self.__class__.__ContextContext = ContextContext (self) |
| 343 | return self.__class__.__ContextContext |
Behdad Esfahbod | 44c2b3c | 2013-07-23 16:00:32 -0400 | [diff] [blame] | 344 | |
Behdad Esfahbod | f2b6d9c | 2013-07-23 17:31:54 -0400 | [diff] [blame] | 345 | @add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst) |
Behdad Esfahbod | 0077697 | 2013-07-23 15:33:00 -0400 | [diff] [blame] | 346 | def closure_glyphs (self, glyphs, table): |
Behdad Esfahbod | 1ab2dbf | 2013-07-23 17:17:21 -0400 | [diff] [blame] | 347 | c = self.__classify_context () |
| 348 | |
Behdad Esfahbod | 0077697 | 2013-07-23 15:33:00 -0400 | [diff] [blame] | 349 | if self.Format == 1: |
Behdad Esfahbod | eeca982 | 2013-07-23 17:42:17 -0400 | [diff] [blame] | 350 | indices = self.Coverage.intersect_glyphs (glyphs) |
| 351 | rss = getattr (self, c.RuleSet) |
| 352 | return sum ((table.table.LookupList.Lookup[ll.LookupListIndex].closure_glyphs (glyphs, table) \ |
| 353 | for i in indices \ |
| 354 | for r in getattr (rss[i], c.Rule) \ |
Behdad Esfahbod | 707a37a | 2013-07-23 21:08:26 -0400 | [diff] [blame] | 355 | if all (g in glyphs for g in c.ContextData (r, self.Format)) \ |
Behdad Esfahbod | eeca982 | 2013-07-23 17:42:17 -0400 | [diff] [blame] | 356 | for ll in getattr (r, c.LookupRecord) \ |
| 357 | ), []) |
Behdad Esfahbod | 0077697 | 2013-07-23 15:33:00 -0400 | [diff] [blame] | 358 | elif self.Format == 2: |
| 359 | assert 0 # XXX |
| 360 | elif self.Format == 3: |
Behdad Esfahbod | 707a37a | 2013-07-23 21:08:26 -0400 | [diff] [blame] | 361 | if not all (x.intersect_glyphs (glyphs) for x in c.ContextData (self, self.Format)): |
Behdad Esfahbod | 0077697 | 2013-07-23 15:33:00 -0400 | [diff] [blame] | 362 | return [] |
| 363 | return sum ((table.table.LookupList.Lookup[ll.LookupListIndex].closure_glyphs (glyphs, table) \ |
Behdad Esfahbod | f2b6d9c | 2013-07-23 17:31:54 -0400 | [diff] [blame] | 364 | for ll in getattr (self, c.LookupRecord)), []) |
Behdad Esfahbod | 0077697 | 2013-07-23 15:33:00 -0400 | [diff] [blame] | 365 | else: |
| 366 | assert 0, "unknown format: %s" % self.Format |
| 367 | |
Behdad Esfahbod | cbba4a6 | 2013-07-23 17:27:18 -0400 | [diff] [blame] | 368 | @add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ContextPos, |
| 369 | fontTools.ttLib.tables.otTables.ChainContextSubst, fontTools.ttLib.tables.otTables.ChainContextPos) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 370 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | d8c7e10 | 2013-07-23 17:07:06 -0400 | [diff] [blame] | 371 | c = self.__classify_context () |
| 372 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 373 | if self.Format == 1: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 374 | indices = self.Coverage.subset_glyphs (glyphs) |
Behdad Esfahbod | d8c7e10 | 2013-07-23 17:07:06 -0400 | [diff] [blame] | 375 | rss = getattr (self, c.RuleSet) |
| 376 | rss = [rss[i] for i in indices] |
| 377 | for rs in rss: |
| 378 | ss = getattr (rs, c.Rule) |
| 379 | ss = [r for r in ss \ |
Behdad Esfahbod | 707a37a | 2013-07-23 21:08:26 -0400 | [diff] [blame] | 380 | if all (g in glyphs for g in c.ContextData (r, self.Format))] |
Behdad Esfahbod | d8c7e10 | 2013-07-23 17:07:06 -0400 | [diff] [blame] | 381 | setattr (rs, c.Rule, ss) |
| 382 | setattr (rs, c.RuleCount, len (ss)) |
Behdad Esfahbod | cbba4a6 | 2013-07-23 17:27:18 -0400 | [diff] [blame] | 383 | # Prune empty subrulesets |
| 384 | rss = [rs for rs in rss if getattr (rs, c.Rule)] |
Behdad Esfahbod | d8c7e10 | 2013-07-23 17:07:06 -0400 | [diff] [blame] | 385 | setattr (self, c.RuleSet, rss) |
| 386 | setattr (self, c.RuleSetCount, len (rss)) |
| 387 | return bool (rss) |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 388 | elif self.Format == 2: |
Behdad Esfahbod | 0a21529 | 2013-07-23 17:47:18 -0400 | [diff] [blame] | 389 | # TODO Renumber classes then prune rules that can't apply |
| 390 | # But then I first need to find fonts that use this type. D'oh! |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 391 | return self.Coverage.subset_glyphs (glyphs) and \ |
Behdad Esfahbod | 707a37a | 2013-07-23 21:08:26 -0400 | [diff] [blame] | 392 | all (x.subset_glyphs (glyphs) for x in c.ContextData (self, self.Format)) |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 393 | elif self.Format == 3: |
Behdad Esfahbod | 707a37a | 2013-07-23 21:08:26 -0400 | [diff] [blame] | 394 | return all (x.subset_glyphs (glyphs) for x in c.ContextData (self, self.Format)) |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 395 | else: |
| 396 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 397 | |
Behdad Esfahbod | 44c2b3c | 2013-07-23 16:00:32 -0400 | [diff] [blame] | 398 | @add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst, |
| 399 | fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos) |
Behdad Esfahbod | 59dfc13 | 2013-07-23 15:39:20 -0400 | [diff] [blame] | 400 | def subset_lookups (self, lookup_indices): |
Behdad Esfahbod | 6870f8a | 2013-07-23 16:18:30 -0400 | [diff] [blame] | 401 | c = self.__classify_context () |
Behdad Esfahbod | 44c2b3c | 2013-07-23 16:00:32 -0400 | [diff] [blame] | 402 | |
Behdad Esfahbod | 59dfc13 | 2013-07-23 15:39:20 -0400 | [diff] [blame] | 403 | if self.Format == 1: |
Behdad Esfahbod | 9e73572 | 2013-07-23 16:35:23 -0400 | [diff] [blame] | 404 | for rs in getattr (self, c.RuleSet): |
| 405 | for r in getattr (rs, c.Rule): |
Behdad Esfahbod | 6870f8a | 2013-07-23 16:18:30 -0400 | [diff] [blame] | 406 | setattr (r, c.LookupRecord, [ll for ll in getattr (r, c.LookupRecord) \ |
| 407 | if ll.LookupListIndex in lookup_indices]) |
| 408 | for ll in getattr (r, c.LookupRecord): |
Behdad Esfahbod | 59dfc13 | 2013-07-23 15:39:20 -0400 | [diff] [blame] | 409 | ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex) |
| 410 | elif self.Format == 2: |
Behdad Esfahbod | 2710839 | 2013-07-23 16:40:47 -0400 | [diff] [blame] | 411 | for rs in getattr (self, c.ClassRuleSet): |
| 412 | for r in getattr (rs, c.ClassRule): |
| 413 | setattr (r, c.LookupRecord, [ll for ll in getattr (r, c.LookupRecord) \ |
| 414 | if ll.LookupListIndex in lookup_indices]) |
| 415 | for ll in getattr (r, c.LookupRecord): |
| 416 | ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex) |
Behdad Esfahbod | 59dfc13 | 2013-07-23 15:39:20 -0400 | [diff] [blame] | 417 | elif self.Format == 3: |
Behdad Esfahbod | 6870f8a | 2013-07-23 16:18:30 -0400 | [diff] [blame] | 418 | setattr (self, c.LookupRecord, [ll for ll in getattr (self, c.LookupRecord) \ |
| 419 | if ll.LookupListIndex in lookup_indices]) |
| 420 | for ll in getattr (self, c.LookupRecord): |
Behdad Esfahbod | 59dfc13 | 2013-07-23 15:39:20 -0400 | [diff] [blame] | 421 | ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex) |
| 422 | else: |
| 423 | assert 0, "unknown format: %s" % self.Format |
| 424 | |
Behdad Esfahbod | 44c2b3c | 2013-07-23 16:00:32 -0400 | [diff] [blame] | 425 | @add_method(fontTools.ttLib.tables.otTables.ContextSubst, fontTools.ttLib.tables.otTables.ChainContextSubst, |
| 426 | fontTools.ttLib.tables.otTables.ContextPos, fontTools.ttLib.tables.otTables.ChainContextPos) |
Behdad Esfahbod | 59dfc13 | 2013-07-23 15:39:20 -0400 | [diff] [blame] | 427 | def collect_lookups (self): |
Behdad Esfahbod | 6870f8a | 2013-07-23 16:18:30 -0400 | [diff] [blame] | 428 | c = self.__classify_context () |
Behdad Esfahbod | 44c2b3c | 2013-07-23 16:00:32 -0400 | [diff] [blame] | 429 | |
Behdad Esfahbod | 59dfc13 | 2013-07-23 15:39:20 -0400 | [diff] [blame] | 430 | if self.Format == 1: |
Behdad Esfahbod | 6870f8a | 2013-07-23 16:18:30 -0400 | [diff] [blame] | 431 | return [ll.LookupListIndex \ |
Behdad Esfahbod | 9e73572 | 2013-07-23 16:35:23 -0400 | [diff] [blame] | 432 | for rs in getattr (self, c.RuleSet) \ |
| 433 | for r in getattr (rs, c.Rule) \ |
Behdad Esfahbod | 6870f8a | 2013-07-23 16:18:30 -0400 | [diff] [blame] | 434 | for ll in getattr (r, c.LookupRecord)] |
Behdad Esfahbod | 59dfc13 | 2013-07-23 15:39:20 -0400 | [diff] [blame] | 435 | elif self.Format == 2: |
Behdad Esfahbod | 2710839 | 2013-07-23 16:40:47 -0400 | [diff] [blame] | 436 | return [ll.LookupListIndex \ |
| 437 | for rs in getattr (self, c.ClassRuleSet) \ |
| 438 | for r in getattr (rs, c.ClassRule) \ |
| 439 | for ll in getattr (r, c.LookupRecord)] |
Behdad Esfahbod | 59dfc13 | 2013-07-23 15:39:20 -0400 | [diff] [blame] | 440 | elif self.Format == 3: |
Behdad Esfahbod | 6870f8a | 2013-07-23 16:18:30 -0400 | [diff] [blame] | 441 | return [ll.LookupListIndex \ |
| 442 | for ll in getattr (self, c.LookupRecord)] |
Behdad Esfahbod | 59dfc13 | 2013-07-23 15:39:20 -0400 | [diff] [blame] | 443 | else: |
| 444 | assert 0, "unknown format: %s" % self.Format |
| 445 | |
Behdad Esfahbod | f2ecc9c | 2013-07-23 12:40:35 -0400 | [diff] [blame] | 446 | |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 447 | @add_method(fontTools.ttLib.tables.otTables.ExtensionSubst) |
| 448 | def closure_glyphs (self, glyphs, table): |
| 449 | if self.Format == 1: |
| 450 | return self.ExtSubTable.closure_glyphs (glyphs, table) |
| 451 | else: |
| 452 | assert 0, "unknown format: %s" % self.Format |
| 453 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 454 | @add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 455 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 456 | if self.Format == 1: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 457 | return self.ExtSubTable.subset_glyphs (glyphs) |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 458 | else: |
| 459 | assert 0, "unknown format: %s" % self.Format |
| 460 | |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 461 | @add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos) |
| 462 | def subset_lookups (self, lookup_indices): |
| 463 | if self.Format == 1: |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 464 | return self.ExtSubTable.subset_lookups (lookup_indices) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 465 | else: |
| 466 | assert 0, "unknown format: %s" % self.Format |
| 467 | |
| 468 | @add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, fontTools.ttLib.tables.otTables.ExtensionPos) |
| 469 | def collect_lookups (self): |
| 470 | if self.Format == 1: |
| 471 | return self.ExtSubTable.collect_lookups () |
| 472 | else: |
| 473 | assert 0, "unknown format: %s" % self.Format |
| 474 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 475 | @add_method(fontTools.ttLib.tables.otTables.Lookup) |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 476 | def closure_glyphs (self, glyphs, table): |
| 477 | return sum ((s.closure_glyphs (glyphs, table) for s in self.SubTable), []) |
| 478 | |
| 479 | @add_method(fontTools.ttLib.tables.otTables.Lookup) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 480 | def subset_glyphs (self, glyphs): |
| 481 | 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] | 482 | self.SubTableCount = len (self.SubTable) |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 483 | return bool (self.SubTableCount) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 484 | |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 485 | @add_method(fontTools.ttLib.tables.otTables.Lookup) |
| 486 | def subset_lookups (self, lookup_indices): |
| 487 | for s in self.SubTable: |
| 488 | s.subset_lookups (lookup_indices) |
| 489 | |
| 490 | @add_method(fontTools.ttLib.tables.otTables.Lookup) |
| 491 | def collect_lookups (self): |
| 492 | return unique_sorted (sum ((s.collect_lookups () for s in self.SubTable), [])) |
| 493 | |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 494 | @add_method(fontTools.ttLib.tables.otTables.LookupList) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 495 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 496 | "Returns the indices of nonempty lookups." |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 497 | 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] | 498 | |
| 499 | @add_method(fontTools.ttLib.tables.otTables.LookupList) |
| 500 | def subset_lookups (self, lookup_indices): |
| 501 | self.Lookup = [self.Lookup[i] for i in lookup_indices] |
| 502 | self.LookupCount = len (self.Lookup) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 503 | for l in self.Lookup: |
| 504 | l.subset_lookups (lookup_indices) |
| 505 | |
| 506 | @add_method(fontTools.ttLib.tables.otTables.LookupList) |
| 507 | def closure_lookups (self, lookup_indices): |
Behdad Esfahbod | bb7e213 | 2013-07-23 13:48:35 -0400 | [diff] [blame] | 508 | lookup_indices = unique_sorted (lookup_indices) |
| 509 | recurse = lookup_indices |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 510 | while True: |
Behdad Esfahbod | bb7e213 | 2013-07-23 13:48:35 -0400 | [diff] [blame] | 511 | recurse_lookups = sum ((self.Lookup[i].collect_lookups () for i in recurse), []) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 512 | recurse_lookups = [l for l in recurse_lookups if l not in lookup_indices] |
| 513 | if not recurse_lookups: |
Behdad Esfahbod | bb7e213 | 2013-07-23 13:48:35 -0400 | [diff] [blame] | 514 | return unique_sorted (lookup_indices) |
| 515 | recurse_lookups = unique_sorted (recurse_lookups) |
| 516 | lookup_indices.extend (recurse_lookups) |
| 517 | recurse = recurse_lookups |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 518 | |
| 519 | @add_method(fontTools.ttLib.tables.otTables.Feature) |
| 520 | def subset_lookups (self, lookup_indices): |
| 521 | self.LookupListIndex = [l for l in self.LookupListIndex if l in lookup_indices] |
| 522 | # Now map them. |
| 523 | self.LookupListIndex = [lookup_indices.index (l) for l in self.LookupListIndex] |
| 524 | self.LookupCount = len (self.LookupListIndex) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 525 | return self.LookupCount |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 526 | |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 527 | @add_method(fontTools.ttLib.tables.otTables.Feature) |
| 528 | def collect_lookups (self): |
| 529 | return self.LookupListIndex[:] |
| 530 | |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 531 | @add_method(fontTools.ttLib.tables.otTables.FeatureList) |
| 532 | def subset_lookups (self, lookup_indices): |
| 533 | "Returns the indices of nonempty features." |
| 534 | feature_indices = [i for (i,f) in enumerate (self.FeatureRecord) if f.Feature.subset_lookups (lookup_indices)] |
Behdad Esfahbod | 356c42e | 2013-07-23 12:10:46 -0400 | [diff] [blame] | 535 | self.subset_features (feature_indices) |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 536 | return feature_indices |
| 537 | |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 538 | @add_method(fontTools.ttLib.tables.otTables.FeatureList) |
| 539 | def collect_lookups (self, feature_indices): |
| 540 | return unique_sorted (sum ((self.FeatureRecord[i].Feature.collect_lookups () for i in feature_indices |
| 541 | if i < self.FeatureCount), [])) |
| 542 | |
Behdad Esfahbod | 356c42e | 2013-07-23 12:10:46 -0400 | [diff] [blame] | 543 | @add_method(fontTools.ttLib.tables.otTables.FeatureList) |
| 544 | def subset_features (self, feature_indices): |
| 545 | self.FeatureRecord = [self.FeatureRecord[i] for i in feature_indices] |
| 546 | self.FeatureCount = len (self.FeatureRecord) |
| 547 | return bool (self.FeatureCount) |
| 548 | |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 549 | @add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys) |
| 550 | def subset_features (self, feature_indices): |
Behdad Esfahbod | 69ce150 | 2013-07-22 18:00:31 -0400 | [diff] [blame] | 551 | if self.ReqFeatureIndex in feature_indices: |
| 552 | self.ReqFeatureIndex = feature_indices.index (self.ReqFeatureIndex) |
| 553 | else: |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 554 | self.ReqFeatureIndex = 65535 |
| 555 | 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] | 556 | # Now map them. |
| 557 | 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] | 558 | self.FeatureCount = len (self.FeatureIndex) |
Behdad Esfahbod | 356c42e | 2013-07-23 12:10:46 -0400 | [diff] [blame] | 559 | return bool (self.FeatureCount or self.ReqFeatureIndex != 65535) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 560 | |
| 561 | @add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, fontTools.ttLib.tables.otTables.LangSys) |
| 562 | def collect_features (self): |
| 563 | feature_indices = self.FeatureIndex[:] |
| 564 | if self.ReqFeatureIndex != 65535: |
| 565 | feature_indices.append (self.ReqFeatureIndex) |
| 566 | return unique_sorted (feature_indices) |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 567 | |
| 568 | @add_method(fontTools.ttLib.tables.otTables.Script) |
| 569 | def subset_features (self, feature_indices): |
| 570 | if self.DefaultLangSys and not self.DefaultLangSys.subset_features (feature_indices): |
| 571 | self.DefaultLangSys = None |
| 572 | self.LangSysRecord = [l for l in self.LangSysRecord if l.LangSys.subset_features (feature_indices)] |
| 573 | self.LangSysCount = len (self.LangSysRecord) |
Behdad Esfahbod | 356c42e | 2013-07-23 12:10:46 -0400 | [diff] [blame] | 574 | return bool (self.LangSysCount or self.DefaultLangSys) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 575 | |
| 576 | @add_method(fontTools.ttLib.tables.otTables.Script) |
| 577 | def collect_features (self): |
Behdad Esfahbod | 2307c8b | 2013-07-23 11:18:13 -0400 | [diff] [blame] | 578 | feature_indices = [l.LangSys.collect_features () for l in self.LangSysRecord] |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 579 | if self.DefaultLangSys: |
| 580 | feature_indices.append (self.DefaultLangSys.collect_features ()) |
| 581 | return unique_sorted (sum (feature_indices, [])) |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 582 | |
| 583 | @add_method(fontTools.ttLib.tables.otTables.ScriptList) |
| 584 | def subset_features (self, feature_indices): |
| 585 | self.ScriptRecord = [s for s in self.ScriptRecord if s.Script.subset_features (feature_indices)] |
| 586 | self.ScriptCount = len (self.ScriptRecord) |
Behdad Esfahbod | 356c42e | 2013-07-23 12:10:46 -0400 | [diff] [blame] | 587 | return bool (self.ScriptCount) |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 588 | |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 589 | @add_method(fontTools.ttLib.tables.otTables.ScriptList) |
| 590 | def collect_features (self): |
| 591 | return unique_sorted (sum ((s.Script.collect_features () for s in self.ScriptRecord), [])) |
| 592 | |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 593 | @add_method(fontTools.ttLib.getTableClass('GSUB')) |
| 594 | def closure_glyphs (self, glyphs): |
| 595 | feature_indices = self.table.ScriptList.collect_features () |
| 596 | lookup_indices = self.table.FeatureList.collect_lookups (feature_indices) |
| 597 | glyphs = unique_sorted (glyphs) |
| 598 | while True: |
| 599 | additions = (sum ((self.table.LookupList.Lookup[i].closure_glyphs (glyphs, self) for i in lookup_indices), [])) |
| 600 | additions = unique_sorted (g for g in additions if g not in glyphs) |
| 601 | if not additions: |
| 602 | return glyphs |
| 603 | glyphs.extend (additions) |
| 604 | |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 605 | @add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS')) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 606 | def subset_glyphs (self, glyphs): |
| 607 | lookup_indices = self.table.LookupList.subset_glyphs (glyphs) |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 608 | self.subset_lookups (lookup_indices) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 609 | self.prune_lookups () |
| 610 | return True |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 611 | |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 612 | @add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS')) |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 613 | def subset_lookups (self, lookup_indices): |
| 614 | "Retrains specified lookups, then removes empty features, language systems, and scripts." |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 615 | self.table.LookupList.subset_lookups (lookup_indices) |
| 616 | feature_indices = self.table.FeatureList.subset_lookups (lookup_indices) |
| 617 | self.table.ScriptList.subset_features (feature_indices) |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 618 | |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 619 | @add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS')) |
| 620 | def prune_lookups (self): |
| 621 | "Remove unreferenced lookups" |
| 622 | feature_indices = self.table.ScriptList.collect_features () |
| 623 | lookup_indices = self.table.FeatureList.collect_lookups (feature_indices) |
| 624 | lookup_indices = self.table.LookupList.closure_lookups (lookup_indices) |
| 625 | self.subset_lookups (lookup_indices) |
| 626 | |
Behdad Esfahbod | 356c42e | 2013-07-23 12:10:46 -0400 | [diff] [blame] | 627 | @add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS')) |
| 628 | def subset_feature_tags (self, feature_tags): |
| 629 | feature_indices = [i for (i,f) in enumerate (self.table.FeatureList.FeatureRecord) if f.FeatureTag in feature_tags] |
| 630 | self.table.FeatureList.subset_features (feature_indices) |
| 631 | self.table.ScriptList.subset_features (feature_indices) |
| 632 | |
| 633 | @add_method(fontTools.ttLib.getTableClass('GSUB'), fontTools.ttLib.getTableClass('GPOS')) |
Behdad Esfahbod | d7b6f8f | 2013-07-23 12:46:52 -0400 | [diff] [blame] | 634 | def prune_pre_subset (self, options): |
Behdad Esfahbod | 4091ec6 | 2013-07-23 13:02:51 -0400 | [diff] [blame] | 635 | if options['layout-features'] and '*' not in options['layout-features']: |
Behdad Esfahbod | ed98c61 | 2013-07-23 12:37:41 -0400 | [diff] [blame] | 636 | self.subset_feature_tags (options['layout-features']) |
Behdad Esfahbod | 356c42e | 2013-07-23 12:10:46 -0400 | [diff] [blame] | 637 | self.prune_lookups () |
| 638 | return True |
| 639 | |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 640 | @add_method(fontTools.ttLib.getTableClass('GDEF')) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 641 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 642 | table = self.table |
| 643 | if table.LigCaretList: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 644 | indices = table.LigCaretList.Coverage.subset_glyphs (glyphs) |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 645 | table.LigCaretList.LigGlyph = [table.LigCaretList.LigGlyph[i] for i in indices] |
| 646 | table.LigCaretList.LigGlyphCount = len (table.LigCaretList.LigGlyph) |
| 647 | if not table.LigCaretList.LigGlyphCount: |
| 648 | table.LigCaretList = None |
| 649 | if table.MarkAttachClassDef: |
| 650 | table.MarkAttachClassDef.classDefs = {g:v for g,v in table.MarkAttachClassDef.classDefs.items() if g in glyphs} |
| 651 | if not table.MarkAttachClassDef.classDefs: |
| 652 | table.MarkAttachClassDef = None |
| 653 | if table.GlyphClassDef: |
| 654 | table.GlyphClassDef.classDefs = {g:v for g,v in table.GlyphClassDef.classDefs.items() if g in glyphs} |
| 655 | if not table.GlyphClassDef.classDefs: |
| 656 | table.GlyphClassDef = None |
| 657 | if table.AttachList: |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 658 | indices = table.AttachList.Coverage.subset_glyphs (glyphs) |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 659 | table.AttachList.AttachPoint = [table.AttachList.AttachPoint[i] for i in indices] |
| 660 | table.AttachList.GlyphCount = len (table.AttachList.AttachPoint) |
| 661 | if not table.AttachList.GlyphCount: |
| 662 | table.AttachList = None |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 663 | 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] | 664 | |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 665 | @add_method(fontTools.ttLib.getTableClass('kern')) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 666 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 5270ec4 | 2013-07-22 12:57:02 -0400 | [diff] [blame] | 667 | for t in self.kernTables: |
| 668 | t.kernTable = {(a,b):v for ((a,b),v) in t.kernTable.items() if a in glyphs and b in glyphs} |
| 669 | self.kernTables = [t for t in self.kernTables if t.kernTable] |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 670 | return bool (self.kernTables) |
Behdad Esfahbod | efb984a | 2013-07-21 22:26:16 -0400 | [diff] [blame] | 671 | |
Behdad Esfahbod | 75e14fc | 2013-07-22 14:49:54 -0400 | [diff] [blame] | 672 | @add_method(fontTools.ttLib.getTableClass('hmtx'), fontTools.ttLib.getTableClass('vmtx')) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 673 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 674 | 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] | 675 | return bool (self.metrics) |
Behdad Esfahbod | c716044 | 2013-07-22 14:29:08 -0400 | [diff] [blame] | 676 | |
Behdad Esfahbod | 75e14fc | 2013-07-22 14:49:54 -0400 | [diff] [blame] | 677 | @add_method(fontTools.ttLib.getTableClass('hdmx')) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 678 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 679 | 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] | 680 | return bool (self.hdmx) |
Behdad Esfahbod | 75e14fc | 2013-07-22 14:49:54 -0400 | [diff] [blame] | 681 | |
Behdad Esfahbod | e45d6af | 2013-07-22 15:29:17 -0400 | [diff] [blame] | 682 | @add_method(fontTools.ttLib.getTableClass('VORG')) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 683 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 684 | self.VOriginRecords = {g:v for g,v in self.VOriginRecords.items() if g in glyphs} |
Behdad Esfahbod | e45d6af | 2013-07-22 15:29:17 -0400 | [diff] [blame] | 685 | self.numVertOriginYMetrics = len (self.VOriginRecords) |
| 686 | return True # Never drop; has default metrics |
| 687 | |
Behdad Esfahbod | 8c646f6 | 2013-07-22 15:06:23 -0400 | [diff] [blame] | 688 | @add_method(fontTools.ttLib.getTableClass('post')) |
Behdad Esfahbod | 4264824 | 2013-07-23 12:56:06 -0400 | [diff] [blame] | 689 | def prune_pre_subset (self, glyphs): |
Behdad Esfahbod | 4091ec6 | 2013-07-23 13:02:51 -0400 | [diff] [blame] | 690 | if not options['glyph-names']: |
Behdad Esfahbod | 4264824 | 2013-07-23 12:56:06 -0400 | [diff] [blame] | 691 | self.formatType = 3.0 |
| 692 | return True |
| 693 | |
| 694 | @add_method(fontTools.ttLib.getTableClass('post')) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 695 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 4264824 | 2013-07-23 12:56:06 -0400 | [diff] [blame] | 696 | self.extraNames = [] # This seems to do it |
Behdad Esfahbod | c9dec9d | 2013-07-23 10:28:47 -0400 | [diff] [blame] | 697 | return True |
Behdad Esfahbod | 653e974 | 2013-07-22 15:17:12 -0400 | [diff] [blame] | 698 | |
Behdad Esfahbod | 861d915 | 2013-07-22 16:47:24 -0400 | [diff] [blame] | 699 | @add_method(fontTools.ttLib.getTableClass('glyf')) |
Behdad Esfahbod | abb50a1 | 2013-07-23 12:58:37 -0400 | [diff] [blame] | 700 | def closure_glyphs (self, glyphs): |
| 701 | glyphs = unique_sorted (glyphs) |
| 702 | decompose = glyphs |
| 703 | # I don't know if component glyphs can be composite themselves. |
| 704 | # We handle them anyway. |
| 705 | while True: |
| 706 | components = [] |
| 707 | for g in decompose: |
| 708 | gl = self[g] |
| 709 | if gl.isComposite (): |
| 710 | for c in gl.components: |
| 711 | if c.glyphName not in glyphs: |
| 712 | components.append (c.glyphName) |
| 713 | components = [c for c in components if c not in glyphs] |
| 714 | if not components: |
| 715 | return glyphs |
| 716 | decompose = unique_sorted (components) |
| 717 | glyphs.extend (components) |
| 718 | |
| 719 | @add_method(fontTools.ttLib.getTableClass('glyf')) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 720 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 721 | self.glyphs = {g:v for g,v in self.glyphs.items() if g in glyphs} |
Behdad Esfahbod | 861d915 | 2013-07-22 16:47:24 -0400 | [diff] [blame] | 722 | self.glyphOrder = [g for g in self.glyphOrder if g in glyphs] |
Behdad Esfahbod | 4027dd8 | 2013-07-23 10:56:04 -0400 | [diff] [blame] | 723 | return bool (self.glyphs) |
Behdad Esfahbod | 861d915 | 2013-07-22 16:47:24 -0400 | [diff] [blame] | 724 | |
Behdad Esfahbod | ed98c61 | 2013-07-23 12:37:41 -0400 | [diff] [blame] | 725 | @add_method(fontTools.ttLib.getTableClass('glyf')) |
Behdad Esfahbod | d7b6f8f | 2013-07-23 12:46:52 -0400 | [diff] [blame] | 726 | def prune_post_subset (self, options): |
Behdad Esfahbod | 4091ec6 | 2013-07-23 13:02:51 -0400 | [diff] [blame] | 727 | if not options['hinting']: |
Behdad Esfahbod | ed98c61 | 2013-07-23 12:37:41 -0400 | [diff] [blame] | 728 | for g in self.glyphs.values (): |
Behdad Esfahbod | d7b6f8f | 2013-07-23 12:46:52 -0400 | [diff] [blame] | 729 | g.expand (self) |
| 730 | g.program = fontTools.ttLib.tables.ttProgram.Program() |
| 731 | g.program.fromBytecode([]) |
Behdad Esfahbod | ed98c61 | 2013-07-23 12:37:41 -0400 | [diff] [blame] | 732 | return True |
| 733 | |
Behdad Esfahbod | 2b677c8 | 2013-07-23 13:37:13 -0400 | [diff] [blame] | 734 | @add_method(fontTools.ttLib.getTableClass('CFF ')) |
| 735 | def subset_glyphs (self, glyphs): |
| 736 | assert 0, "unimplemented" |
| 737 | |
Behdad Esfahbod | 653e974 | 2013-07-22 15:17:12 -0400 | [diff] [blame] | 738 | @add_method(fontTools.ttLib.getTableClass('cmap')) |
Behdad Esfahbod | abb50a1 | 2013-07-23 12:58:37 -0400 | [diff] [blame] | 739 | def prune_pre_subset (self, options): |
Behdad Esfahbod | de4a15b | 2013-07-23 13:05:42 -0400 | [diff] [blame] | 740 | if not options['legacy-cmap']: |
| 741 | # Drop non-Unicode / non-Symbol cmaps |
| 742 | self.tables = [t for t in self.tables if t.platformID == 3 and t.platEncID in [0, 1, 10]] |
| 743 | if not options['symbol-cmap']: |
| 744 | self.tables = [t for t in self.tables if t.platformID == 3 and t.platEncID in [1, 10]] |
Behdad Esfahbod | abb50a1 | 2013-07-23 12:58:37 -0400 | [diff] [blame] | 745 | # TODO Only keep one subtable? |
Behdad Esfahbod | abb50a1 | 2013-07-23 12:58:37 -0400 | [diff] [blame] | 746 | # For now, drop format=0 which can't be subset_glyphs easily? |
| 747 | self.tables = [t for t in self.tables if t.format != 0] |
| 748 | return bool (self.tables) |
| 749 | |
| 750 | @add_method(fontTools.ttLib.getTableClass('cmap')) |
Behdad Esfahbod | 0716eb4 | 2013-07-23 10:47:03 -0400 | [diff] [blame] | 751 | def subset_glyphs (self, glyphs): |
Behdad Esfahbod | 653e974 | 2013-07-22 15:17:12 -0400 | [diff] [blame] | 752 | for t in self.tables: |
Behdad Esfahbod | 9453a36 | 2013-07-22 16:21:24 -0400 | [diff] [blame] | 753 | # For reasons I don't understand I need this here |
| 754 | # to force decompilation of the cmap format 14. |
| 755 | try: |
| 756 | getattr (t, "asdf") |
| 757 | except AttributeError: |
| 758 | pass |
Behdad Esfahbod | b13d790 | 2013-07-22 16:01:15 -0400 | [diff] [blame] | 759 | if t.format == 14: |
Behdad Esfahbod | 9453a36 | 2013-07-22 16:21:24 -0400 | [diff] [blame] | 760 | # XXX We drop all the default-UVS mappings (g==None) |
Behdad Esfahbod | b13d790 | 2013-07-22 16:01:15 -0400 | [diff] [blame] | 761 | t.uvsDict = {v:[(u,g) for (u,g) in l if g in glyphs] for (v,l) in t.uvsDict.items()} |
| 762 | t.uvsDict = {v:l for (v,l) in t.uvsDict.items() if l} |
| 763 | else: |
| 764 | t.cmap = {u:g for (u,g) in t.cmap.items() if g in glyphs} |
| 765 | 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] | 766 | # XXX Convert formats when needed |
Behdad Esfahbod | 61addb4 | 2013-07-23 11:03:49 -0400 | [diff] [blame] | 767 | return bool (self.tables) |
| 768 | |
Behdad Esfahbod | 61addb4 | 2013-07-23 11:03:49 -0400 | [diff] [blame] | 769 | @add_method(fontTools.ttLib.getTableClass('name')) |
Behdad Esfahbod | d7b6f8f | 2013-07-23 12:46:52 -0400 | [diff] [blame] | 770 | def prune_pre_subset (self, options): |
Behdad Esfahbod | 20faeb0 | 2013-07-23 13:19:03 -0400 | [diff] [blame] | 771 | if '*' not in options['name-IDs']: |
| 772 | self.names = [n for n in self.names if n.nameID in options['name-IDs']] |
| 773 | if not options['name-legacy']: |
| 774 | self.names = [n for n in self.names if n.platformID == 3 and n.platEncID == 1] |
| 775 | if '*' not in options['name-languages']: |
| 776 | self.names = [n for n in self.names if n.langID in options['name-languages']] |
| 777 | return True # Retain even if empty |
Behdad Esfahbod | 653e974 | 2013-07-22 15:17:12 -0400 | [diff] [blame] | 778 | |
Behdad Esfahbod | 8c646f6 | 2013-07-22 15:06:23 -0400 | [diff] [blame] | 779 | |
Behdad Esfahbod | bc25f16 | 2013-07-23 12:56:54 -0400 | [diff] [blame] | 780 | drop_tables_default = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', 'PCLT', 'LTSH'] |
Behdad Esfahbod | 103a12f | 2013-07-23 15:08:39 -0400 | [diff] [blame] | 781 | drop_tables_default += ['Feat', 'Glat', 'Gloc', 'Silf', 'Sill'] # Graphite |
Behdad Esfahbod | 38e852c | 2013-07-23 16:56:50 -0400 | [diff] [blame] | 782 | drop_tables_default += ['CBLC', 'CBDT', 'sbix', 'COLR', 'CPAL'] # Color |
Behdad Esfahbod | ed98c61 | 2013-07-23 12:37:41 -0400 | [diff] [blame] | 783 | no_subset_tables = ['gasp', 'head', 'hhea', 'maxp', 'vhea', 'OS/2', 'loca', 'name', 'cvt ', 'fpgm', 'prep'] |
Behdad Esfahbod | bc25f16 | 2013-07-23 12:56:54 -0400 | [diff] [blame] | 784 | hinting_tables = ['cvt ', 'fpgm', 'prep', 'hdmx', 'VDMX'] |
Behdad Esfahbod | 29df046 | 2013-07-23 11:05:25 -0400 | [diff] [blame] | 785 | |
Behdad Esfahbod | 356c42e | 2013-07-23 12:10:46 -0400 | [diff] [blame] | 786 | # Based on HarfBuzz shapers |
| 787 | layout_features_dict = { |
| 788 | # Default shaper |
| 789 | 'common': ['ccmp', 'liga', 'locl', 'mark', 'mkmk', 'rlig'], |
| 790 | 'horizontal': ['calt', 'clig', 'curs', 'kern', 'rclt'], |
| 791 | 'vertical': ['valt', 'vert', 'vkrn', 'vpal', 'vrt2'], |
| 792 | 'ltr': ['ltra', 'ltrm'], |
| 793 | 'rtl': ['rtla', 'rtlm'], |
| 794 | # Complex shapers |
| 795 | 'arabic': ['init', 'medi', 'fina', 'isol', 'med2', 'fin2', 'fin3'], |
| 796 | 'hangul': ['ljmo', 'vjmo', 'tjmo'], |
| 797 | 'tibetal': ['abvs', 'blws', 'abvm', 'blwm'], |
| 798 | 'indic': ['nukt', 'akhn', 'rphf', 'rkrf', 'pref', 'blwf', 'half', 'abvf', 'pstf', 'cfar', 'vatu', 'cjct', |
| 799 | 'init', 'pres', 'abvs', 'blws', 'psts', 'haln', 'dist', 'abvm', 'blwm'], |
| 800 | } |
| 801 | layout_features_all = unique_sorted (sum (layout_features_dict.values (), [])) |
| 802 | |
Behdad Esfahbod | 4091ec6 | 2013-07-23 13:02:51 -0400 | [diff] [blame] | 803 | options_default = { |
Behdad Esfahbod | de4a15b | 2013-07-23 13:05:42 -0400 | [diff] [blame] | 804 | 'drop-tables': drop_tables_default, |
Behdad Esfahbod | 4091ec6 | 2013-07-23 13:02:51 -0400 | [diff] [blame] | 805 | 'layout-features': layout_features_all, |
| 806 | 'hinting': False, |
| 807 | 'glyph-names': False, |
Behdad Esfahbod | de4a15b | 2013-07-23 13:05:42 -0400 | [diff] [blame] | 808 | 'legacy-cmap': False, |
| 809 | 'symbol-cmap': False, |
Behdad Esfahbod | 20faeb0 | 2013-07-23 13:19:03 -0400 | [diff] [blame] | 810 | 'name-IDs': [1, 2], # Family and Style |
| 811 | 'name-legacy': False, |
| 812 | 'name-languages': [0x0409], # English |
Behdad Esfahbod | e30ed12 | 2013-07-23 21:08:29 -0400 | [diff] [blame] | 813 | 'mandatory-glyphs': True, # First four for TrueType, .notdef for CFF |
Behdad Esfahbod | 4091ec6 | 2013-07-23 13:02:51 -0400 | [diff] [blame] | 814 | } |
| 815 | |
Behdad Esfahbod | 29df046 | 2013-07-23 11:05:25 -0400 | [diff] [blame] | 816 | |
Behdad Esfahbod | 75e14fc | 2013-07-22 14:49:54 -0400 | [diff] [blame] | 817 | # TODO OS/2 ulUnicodeRange / ulCodePageRange? |
Behdad Esfahbod | f71267b | 2013-07-23 12:59:13 -0400 | [diff] [blame] | 818 | # TODO Drop unneeded GSUB/GPOS Script/LangSys entries |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 819 | # TODO Finish GSUB glyph closure |
Behdad Esfahbod | 398d389 | 2013-07-23 15:29:40 -0400 | [diff] [blame] | 820 | # TODO Avoid recursing too much |
Behdad Esfahbod | e94aa0e | 2013-07-23 13:22:04 -0400 | [diff] [blame] | 821 | # TODO Text direction considerations |
| 822 | # TODO Text script / language considerations |
Behdad Esfahbod | 38e852c | 2013-07-23 16:56:50 -0400 | [diff] [blame] | 823 | # TODO Drop unknown tables |
Behdad Esfahbod | 56ebd04 | 2013-07-22 13:02:24 -0400 | [diff] [blame] | 824 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 825 | if __name__ == '__main__': |
| 826 | |
Behdad Esfahbod | b70b498 | 2013-07-23 11:38:26 -0400 | [diff] [blame] | 827 | import sys, time |
| 828 | |
| 829 | start_time = time.time () |
| 830 | last_time = start_time |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 831 | |
Behdad Esfahbod | 4ae8171 | 2013-07-22 11:57:13 -0400 | [diff] [blame] | 832 | verbose = False |
| 833 | if "--verbose" in sys.argv: |
| 834 | verbose = True |
| 835 | sys.argv.remove ("--verbose") |
Behdad Esfahbod | 350a527 | 2013-07-22 12:02:16 -0400 | [diff] [blame] | 836 | xml = False |
| 837 | if "--xml" in sys.argv: |
| 838 | xml = True |
| 839 | sys.argv.remove ("--xml") |
Behdad Esfahbod | b70b498 | 2013-07-23 11:38:26 -0400 | [diff] [blame] | 840 | timing = False |
| 841 | if "--timing" in sys.argv: |
| 842 | timing = True |
| 843 | sys.argv.remove ("--timing") |
| 844 | |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 845 | options = options_default.copy () |
| 846 | |
Behdad Esfahbod | b70b498 | 2013-07-23 11:38:26 -0400 | [diff] [blame] | 847 | def lapse (what): |
| 848 | if not timing: |
| 849 | return |
| 850 | global last_time |
| 851 | new_time = time.time () |
| 852 | print "Took %0.3fs to %s" % (new_time - last_time, what) |
| 853 | last_time = new_time |
Behdad Esfahbod | 4ae8171 | 2013-07-22 11:57:13 -0400 | [diff] [blame] | 854 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 855 | if len (sys.argv) < 3: |
| 856 | print >>sys.stderr, "usage: pyotlss.py font-file glyph..." |
| 857 | sys.exit (1) |
| 858 | |
| 859 | fontfile = sys.argv[1] |
| 860 | glyphs = sys.argv[2:] |
| 861 | |
| 862 | font = fontTools.ttx.TTFont (fontfile) |
Behdad Esfahbod | 0f86ce9 | 2013-07-22 17:30:31 -0400 | [diff] [blame] | 863 | font.disassembleInstructions = False |
Behdad Esfahbod | b70b498 | 2013-07-23 11:38:26 -0400 | [diff] [blame] | 864 | lapse ("load font") |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 865 | |
Behdad Esfahbod | e30ed12 | 2013-07-23 21:08:29 -0400 | [diff] [blame] | 866 | if options["mandatory-glyphs"]: |
| 867 | # Always include .notdef; anything else? |
| 868 | if 'glyf' in font: |
| 869 | glyphs.extend (['gid0', 'gid1', 'gid2', 'gid3']) |
| 870 | if verbose: |
| 871 | print "Added first four glyphs to subset" |
| 872 | else: |
| 873 | glyphs.append ('.notdef') |
| 874 | if verbose: |
| 875 | print "Added .notdef glyph to subset" |
| 876 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 877 | names = font.getGlyphNames() |
Behdad Esfahbod | 9bd59c4 | 2013-07-23 21:19:49 -0400 | [diff] [blame^] | 878 | lapse ("loading glyph names") |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 879 | # Convert to glyph names |
Behdad Esfahbod | 3f4b97e | 2013-07-23 15:04:25 -0400 | [diff] [blame] | 880 | glyph_names = [] |
Behdad Esfahbod | e36061e | 2013-07-23 15:13:00 -0400 | [diff] [blame] | 881 | cmap_tables = None |
Behdad Esfahbod | 3f4b97e | 2013-07-23 15:04:25 -0400 | [diff] [blame] | 882 | for g in glyphs: |
| 883 | if g in names: |
| 884 | glyph_names.append (g) |
| 885 | continue |
Behdad Esfahbod | e30ed12 | 2013-07-23 21:08:29 -0400 | [diff] [blame] | 886 | if g.startswith ('uni'): |
Behdad Esfahbod | e36061e | 2013-07-23 15:13:00 -0400 | [diff] [blame] | 887 | if not cmap_tables: |
| 888 | cmap = font['cmap'] |
| 889 | cmap_tables = [t for t in cmap.tables if t.platformID == 3 and t.platEncID in [1, 10]] |
| 890 | del cmap |
Behdad Esfahbod | 3f4b97e | 2013-07-23 15:04:25 -0400 | [diff] [blame] | 891 | found = False |
Behdad Esfahbod | e36061e | 2013-07-23 15:13:00 -0400 | [diff] [blame] | 892 | u = int (g[3:], 16) |
| 893 | for table in cmap_tables: |
Behdad Esfahbod | 3f4b97e | 2013-07-23 15:04:25 -0400 | [diff] [blame] | 894 | if u in table.cmap: |
| 895 | glyph_names.append (table.cmap[u]) |
| 896 | found = True |
| 897 | break |
| 898 | if not found: |
Behdad Esfahbod | e36061e | 2013-07-23 15:13:00 -0400 | [diff] [blame] | 899 | if verbose: |
| 900 | print ("No glyph for Unicode value %s; skipping." % g) |
Behdad Esfahbod | 3f4b97e | 2013-07-23 15:04:25 -0400 | [diff] [blame] | 901 | continue |
Behdad Esfahbod | e30ed12 | 2013-07-23 21:08:29 -0400 | [diff] [blame] | 902 | if g.startswith ('gid') or g.startswith ('glyph'): |
| 903 | if g.startswith ('gid'): |
| 904 | g = g[3:] |
| 905 | elif g.startswith ('glyph'): |
| 906 | g = g[5:] |
| 907 | try: |
| 908 | glyph_names.append (font.getGlyphName (int (g), requireReal=1)) |
| 909 | except ValueError: |
| 910 | raise Exception ("Invalid glyph identifier %s" % g) |
| 911 | continue |
| 912 | raise Exception ("Invalid glyph identifier %s" % g) |
Behdad Esfahbod | e36061e | 2013-07-23 15:13:00 -0400 | [diff] [blame] | 913 | del cmap_tables |
Behdad Esfahbod | e30ed12 | 2013-07-23 21:08:29 -0400 | [diff] [blame] | 914 | glyphs = unique_sorted (glyph_names) |
Behdad Esfahbod | 3f4b97e | 2013-07-23 15:04:25 -0400 | [diff] [blame] | 915 | del glyph_names |
Behdad Esfahbod | b70b498 | 2013-07-23 11:38:26 -0400 | [diff] [blame] | 916 | lapse ("compile glyph list") |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 917 | |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 918 | glyphs_requested = glyphs |
| 919 | if 'GSUB' in font: |
| 920 | # XXX Do this after pruning! |
| 921 | if verbose: |
Behdad Esfahbod | e30ed12 | 2013-07-23 21:08:29 -0400 | [diff] [blame] | 922 | print "Closing glyph list over 'GSUB': %d glyphs before" % len (glyphs) |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 923 | glyphs = font['GSUB'].closure_glyphs (glyphs) |
| 924 | if verbose: |
Behdad Esfahbod | e30ed12 | 2013-07-23 21:08:29 -0400 | [diff] [blame] | 925 | print "Closed glyph list over 'GSUB': %d glyphs after" % len (glyphs) |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 926 | lapse ("close glyph list over 'GSUB'") |
| 927 | glyphs_gsubed = glyphs |
| 928 | |
Behdad Esfahbod | 2a784ac | 2013-07-22 17:00:36 -0400 | [diff] [blame] | 929 | # Close over composite glyphs |
| 930 | if 'glyf' in font: |
Behdad Esfahbod | 0fe6a51 | 2013-07-23 11:17:35 -0400 | [diff] [blame] | 931 | if verbose: |
Behdad Esfahbod | e30ed12 | 2013-07-23 21:08:29 -0400 | [diff] [blame] | 932 | print "Closing glyph list over 'glyf': %d glyphs before" % len (glyphs) |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 933 | glyphs = font['glyf'].closure_glyphs (glyphs) |
Behdad Esfahbod | 0fe6a51 | 2013-07-23 11:17:35 -0400 | [diff] [blame] | 934 | if verbose: |
Behdad Esfahbod | e30ed12 | 2013-07-23 21:08:29 -0400 | [diff] [blame] | 935 | print "Closed glyph list over 'glyf': %d glyphs after" % len (glyphs) |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 936 | lapse ("close glyph list over 'glyf'") |
Behdad Esfahbod | 0fe6a51 | 2013-07-23 11:17:35 -0400 | [diff] [blame] | 937 | else: |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 938 | glyphs = glyphs |
| 939 | glyphs_glyfed = glyphs |
| 940 | glyphs_closed = glyphs |
Behdad Esfahbod | 0fe6a51 | 2013-07-23 11:17:35 -0400 | [diff] [blame] | 941 | del glyphs |
| 942 | |
Behdad Esfahbod | c9dec9d | 2013-07-23 10:28:47 -0400 | [diff] [blame] | 943 | if verbose: |
Behdad Esfahbod | 0fe6a51 | 2013-07-23 11:17:35 -0400 | [diff] [blame] | 944 | print "Retaining %d glyphs: " % len (glyphs_closed) |
Behdad Esfahbod | 2a784ac | 2013-07-22 17:00:36 -0400 | [diff] [blame] | 945 | |
Behdad Esfahbod | 350a527 | 2013-07-22 12:02:16 -0400 | [diff] [blame] | 946 | if xml: |
Behdad Esfahbod | 4ae8171 | 2013-07-22 11:57:13 -0400 | [diff] [blame] | 947 | import xmlWriter |
| 948 | writer = xmlWriter.XMLWriter (sys.stdout) |
Behdad Esfahbod | 4e214e4 | 2013-07-22 13:13:49 -0400 | [diff] [blame] | 949 | |
Behdad Esfahbod | 8842ce2 | 2013-07-22 13:01:33 -0400 | [diff] [blame] | 950 | for tag in font.keys(): |
Behdad Esfahbod | 4e214e4 | 2013-07-22 13:13:49 -0400 | [diff] [blame] | 951 | |
Behdad Esfahbod | c716044 | 2013-07-22 14:29:08 -0400 | [diff] [blame] | 952 | if tag == 'GlyphOrder': |
| 953 | continue |
| 954 | |
Behdad Esfahbod | ed98c61 | 2013-07-23 12:37:41 -0400 | [diff] [blame] | 955 | if tag in options['drop-tables'] or \ |
| 956 | (not options['hinting'] and tag in hinting_tables): |
Behdad Esfahbod | c716044 | 2013-07-22 14:29:08 -0400 | [diff] [blame] | 957 | if verbose: |
| 958 | print tag, "dropped." |
Behdad Esfahbod | 4e214e4 | 2013-07-22 13:13:49 -0400 | [diff] [blame] | 959 | del font[tag] |
| 960 | continue |
| 961 | |
Behdad Esfahbod | 8842ce2 | 2013-07-22 13:01:33 -0400 | [diff] [blame] | 962 | clazz = fontTools.ttLib.getTableClass(tag) |
Behdad Esfahbod | 4e214e4 | 2013-07-22 13:13:49 -0400 | [diff] [blame] | 963 | |
Behdad Esfahbod | d7b6f8f | 2013-07-23 12:46:52 -0400 | [diff] [blame] | 964 | if hasattr (clazz, 'prune_pre_subset'): |
Behdad Esfahbod | 61addb4 | 2013-07-23 11:03:49 -0400 | [diff] [blame] | 965 | table = font[tag] |
Behdad Esfahbod | d7b6f8f | 2013-07-23 12:46:52 -0400 | [diff] [blame] | 966 | retain = table.prune_pre_subset (options) |
Behdad Esfahbod | b70b498 | 2013-07-23 11:38:26 -0400 | [diff] [blame] | 967 | lapse ("prune '%s'" % tag) |
| 968 | if not retain: |
Behdad Esfahbod | 8b411f3 | 2013-07-23 11:24:20 -0400 | [diff] [blame] | 969 | if verbose: |
| 970 | print tag, "pruned to empty; dropped." |
| 971 | del font[tag] |
| 972 | continue |
| 973 | else: |
| 974 | if verbose: |
| 975 | print tag, "pruned." |
Behdad Esfahbod | 61addb4 | 2013-07-23 11:03:49 -0400 | [diff] [blame] | 976 | |
| 977 | if tag in no_subset_tables: |
| 978 | if verbose: |
| 979 | print tag, "subsetting not needed." |
Behdad Esfahbod | 96f4704 | 2013-07-23 12:21:34 -0400 | [diff] [blame] | 980 | elif hasattr (clazz, 'subset_glyphs'): |
Behdad Esfahbod | 61addb4 | 2013-07-23 11:03:49 -0400 | [diff] [blame] | 981 | table = font[tag] |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 982 | if tag == 'cmap': # What else? |
Behdad Esfahbod | 0fe6a51 | 2013-07-23 11:17:35 -0400 | [diff] [blame] | 983 | glyphs = glyphs_requested |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 984 | elif tag in ['GSUB', 'GPOS', 'GDEF', 'cmap', 'kern', 'post']: # What else? |
| 985 | glyphs = glyphs_gsubed |
Behdad Esfahbod | 0fe6a51 | 2013-07-23 11:17:35 -0400 | [diff] [blame] | 986 | else: |
| 987 | glyphs = glyphs_closed |
Behdad Esfahbod | b70b498 | 2013-07-23 11:38:26 -0400 | [diff] [blame] | 988 | retain = table.subset_glyphs (glyphs) |
| 989 | lapse ("subset '%s'" % tag) |
| 990 | if not retain: |
Behdad Esfahbod | 61addb4 | 2013-07-23 11:03:49 -0400 | [diff] [blame] | 991 | if verbose: |
| 992 | print tag, "subsetted to empty; dropped." |
Behdad Esfahbod | 8b411f3 | 2013-07-23 11:24:20 -0400 | [diff] [blame] | 993 | del font[tag] |
| 994 | continue |
Behdad Esfahbod | 61addb4 | 2013-07-23 11:03:49 -0400 | [diff] [blame] | 995 | else: |
| 996 | if verbose: |
| 997 | print tag, "subsetted." |
Behdad Esfahbod | 0fe6a51 | 2013-07-23 11:17:35 -0400 | [diff] [blame] | 998 | del glyphs |
Behdad Esfahbod | 4ae8171 | 2013-07-22 11:57:13 -0400 | [diff] [blame] | 999 | else: |
Behdad Esfahbod | 350a527 | 2013-07-22 12:02:16 -0400 | [diff] [blame] | 1000 | if verbose: |
Behdad Esfahbod | 61addb4 | 2013-07-23 11:03:49 -0400 | [diff] [blame] | 1001 | print tag, "NOT subset; don't know how to subset." |
| 1002 | continue |
| 1003 | |
Behdad Esfahbod | d7b6f8f | 2013-07-23 12:46:52 -0400 | [diff] [blame] | 1004 | if hasattr (clazz, 'prune_post_subset'): |
| 1005 | table = font[tag] |
| 1006 | retain = table.prune_post_subset (options) |
| 1007 | lapse ("prune '%s'" % tag) |
| 1008 | if not retain: |
| 1009 | if verbose: |
| 1010 | print tag, "pruned to empty; dropped." |
| 1011 | del font[tag] |
| 1012 | continue |
| 1013 | else: |
| 1014 | if verbose: |
| 1015 | print tag, "pruned." |
| 1016 | |
Behdad Esfahbod | c716044 | 2013-07-22 14:29:08 -0400 | [diff] [blame] | 1017 | glyphOrder = font.getGlyphOrder() |
Behdad Esfahbod | 0fe6a51 | 2013-07-23 11:17:35 -0400 | [diff] [blame] | 1018 | glyphOrder = [g for g in glyphOrder if g in glyphs_closed] |
Behdad Esfahbod | c716044 | 2013-07-22 14:29:08 -0400 | [diff] [blame] | 1019 | font.setGlyphOrder (glyphOrder) |
| 1020 | font._buildReverseGlyphOrderDict () |
Behdad Esfahbod | b70b498 | 2013-07-23 11:38:26 -0400 | [diff] [blame] | 1021 | lapse ("subset GlyphOrder") |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 1022 | |
Behdad Esfahbod | 0f86ce9 | 2013-07-22 17:30:31 -0400 | [diff] [blame] | 1023 | if xml: |
| 1024 | for tag in font.keys(): |
| 1025 | writer.begintag (tag) |
| 1026 | writer.newline () |
| 1027 | font[tag].toXML(writer, font) |
| 1028 | writer.endtag (tag) |
| 1029 | writer.newline () |
| 1030 | |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 1031 | font.save (fontfile + '.subset') |
Behdad Esfahbod | b70b498 | 2013-07-23 11:38:26 -0400 | [diff] [blame] | 1032 | lapse ("compile and save font") |
| 1033 | |
| 1034 | last_time = start_time |
| 1035 | lapse ("make one with everything (TOTAL TIME)") |