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