Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 1 | #!/usr/bin/python |
Behdad Esfahbod | db6d2e9 | 2013-08-13 12:42:12 -0400 | [diff] [blame] | 2 | # |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 3 | # Python OpenType Layout Subsetter |
Behdad Esfahbod | db6d2e9 | 2013-08-13 12:42:12 -0400 | [diff] [blame] | 4 | # Later grown into a full OpenType subsetter... |
Behdad Esfahbod | 0fe6a51 | 2013-07-23 11:17:35 -0400 | [diff] [blame] | 5 | # |
| 6 | # Copyright 2013 Google, Inc. All Rights Reserved. |
| 7 | # |
| 8 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | # you may not use this file except in compliance with the License. |
| 10 | # You may obtain a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, software |
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | # See the License for the specific language governing permissions and |
| 18 | # limitations under the License. |
| 19 | # |
| 20 | # Google Author(s): Behdad Esfahbod |
| 21 | # |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 22 | |
Behdad Esfahbod | fa3bc5e | 2013-07-24 14:37:58 -0400 | [diff] [blame] | 23 | # Try running on PyPy |
| 24 | try: |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 25 | import numpypy |
Behdad Esfahbod | fa3bc5e | 2013-07-24 14:37:58 -0400 | [diff] [blame] | 26 | except ImportError: |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 27 | pass |
Behdad Esfahbod | fa3bc5e | 2013-07-24 14:37:58 -0400 | [diff] [blame] | 28 | |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 29 | import fontTools.ttx |
Behdad Esfahbod | 4cf7a80 | 2013-07-24 16:08:35 -0400 | [diff] [blame] | 30 | import struct |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 31 | |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 32 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 33 | def add_method (*clazzes): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 34 | """A decorator-returning function to add a new method to one or |
| 35 | more classes.""" |
| 36 | def wrapper (method): |
| 37 | for clazz in clazzes: |
| 38 | assert clazz.__name__ != 'DefaultTable', 'Oops, table class not found.' |
| 39 | setattr (clazz, method.func_name, method) |
| 40 | return None |
| 41 | return wrapper |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 42 | |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 43 | def unique_sorted (l): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 44 | return sorted (set (l)) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 45 | |
| 46 | |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 47 | @add_method(fontTools.ttLib.tables.otTables.Coverage) |
Behdad Esfahbod | 327dcc3 | 2013-07-31 13:50:51 -0400 | [diff] [blame] | 48 | def intersect (self, glyphs): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 49 | "Returns ascending list of matching coverage values." |
| 50 | return [i for (i,g) in enumerate (self.glyphs) if g in glyphs] |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 51 | |
| 52 | @add_method(fontTools.ttLib.tables.otTables.Coverage) |
Behdad Esfahbod | 849d25c | 2013-08-12 19:24:24 -0400 | [diff] [blame] | 53 | def intersect_glyphs (self, glyphs): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 54 | "Returns set of intersecting glyphs." |
| 55 | return set (g for g in self.glyphs if g in glyphs) |
Behdad Esfahbod | 849d25c | 2013-08-12 19:24:24 -0400 | [diff] [blame] | 56 | |
| 57 | @add_method(fontTools.ttLib.tables.otTables.Coverage) |
Behdad Esfahbod | 327dcc3 | 2013-07-31 13:50:51 -0400 | [diff] [blame] | 58 | def subset (self, glyphs): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 59 | "Returns ascending list of remaining coverage values." |
| 60 | indices = self.intersect (glyphs) |
| 61 | self.glyphs = [g for g in self.glyphs if g in glyphs] |
| 62 | return indices |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 63 | |
Behdad Esfahbod | 1437426 | 2013-08-08 22:26:49 -0400 | [diff] [blame] | 64 | @add_method(fontTools.ttLib.tables.otTables.Coverage) |
| 65 | def remap (self, coverage_map): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 66 | "Remaps coverage." |
| 67 | self.glyphs = [self.glyphs[i] for i in coverage_map] |
Behdad Esfahbod | 1437426 | 2013-08-08 22:26:49 -0400 | [diff] [blame] | 68 | |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 69 | @add_method(fontTools.ttLib.tables.otTables.ClassDef) |
Behdad Esfahbod | 327dcc3 | 2013-07-31 13:50:51 -0400 | [diff] [blame] | 70 | def intersect (self, glyphs): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 71 | "Returns ascending list of matching class values." |
| 72 | return unique_sorted ( |
| 73 | ([0] if any (g not in self.classDefs for g in glyphs) else []) + |
| 74 | [v for g,v in self.classDefs.iteritems() if g in glyphs]) |
Behdad Esfahbod | b8d5588 | 2013-07-23 22:17:39 -0400 | [diff] [blame] | 75 | |
| 76 | @add_method(fontTools.ttLib.tables.otTables.ClassDef) |
Behdad Esfahbod | 849d25c | 2013-08-12 19:24:24 -0400 | [diff] [blame] | 77 | def intersect_class (self, glyphs, klass): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 78 | "Returns set of glyphs matching class." |
| 79 | if klass == 0: |
| 80 | return set (g for g in glyphs if g not in self.classDefs) |
| 81 | return set (g for g,v in self.classDefs.iteritems() |
| 82 | if v == klass and g in glyphs) |
Behdad Esfahbod | b8d5588 | 2013-07-23 22:17:39 -0400 | [diff] [blame] | 83 | |
| 84 | @add_method(fontTools.ttLib.tables.otTables.ClassDef) |
Behdad Esfahbod | 327dcc3 | 2013-07-31 13:50:51 -0400 | [diff] [blame] | 85 | def subset (self, glyphs, remap=False): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 86 | "Returns ascending list of remaining classes." |
| 87 | self.classDefs = {g:v for g,v in self.classDefs.iteritems() if g in glyphs} |
| 88 | # Note: while class 0 has the special meaning of "not matched", |
| 89 | # if no glyph will ever /not match/, we can optimize class 0 out too. |
| 90 | indices = unique_sorted ( |
| 91 | ([0] if any (g not in self.classDefs for g in glyphs) else []) + |
| 92 | self.classDefs.itervalues()) |
| 93 | if remap: |
| 94 | self.remap (indices) |
| 95 | return indices |
Behdad Esfahbod | 4aa6ce3 | 2013-07-22 12:15:36 -0400 | [diff] [blame] | 96 | |
| 97 | @add_method(fontTools.ttLib.tables.otTables.ClassDef) |
| 98 | def remap (self, class_map): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 99 | "Remaps classes." |
| 100 | self.classDefs = {g:class_map.index (v) |
| 101 | for g,v in self.classDefs.iteritems()} |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 102 | |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 103 | @add_method(fontTools.ttLib.tables.otTables.SingleSubst) |
Behdad Esfahbod | 1d4fa13 | 2013-08-08 22:59:32 -0400 | [diff] [blame] | 104 | def closure_glyphs (self, s, cur_glyphs=None): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 105 | if cur_glyphs == None: cur_glyphs = s.glyphs |
| 106 | if self.Format in [1, 2]: |
| 107 | s.glyphs.update (v for g,v in self.mapping.iteritems() if g in cur_glyphs) |
| 108 | else: |
| 109 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 110 | |
| 111 | @add_method(fontTools.ttLib.tables.otTables.SingleSubst) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 112 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 113 | if self.Format in [1, 2]: |
| 114 | self.mapping = {g:v for g,v in self.mapping.iteritems() |
| 115 | if g in s.glyphs and v in s.glyphs} |
| 116 | return bool (self.mapping) |
| 117 | else: |
| 118 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 119 | |
| 120 | @add_method(fontTools.ttLib.tables.otTables.MultipleSubst) |
Behdad Esfahbod | 1d4fa13 | 2013-08-08 22:59:32 -0400 | [diff] [blame] | 121 | def closure_glyphs (self, s, cur_glyphs=None): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 122 | if cur_glyphs == None: cur_glyphs = s.glyphs |
| 123 | if self.Format == 1: |
| 124 | indices = self.Coverage.intersect (cur_glyphs) |
| 125 | s.glyphs.update (*(self.Sequence[i].Substitute for i in indices)) |
| 126 | else: |
| 127 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 128 | |
| 129 | @add_method(fontTools.ttLib.tables.otTables.MultipleSubst) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 130 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 131 | if self.Format == 1: |
| 132 | indices = self.Coverage.subset (s.glyphs) |
| 133 | self.Sequence = [self.Sequence[i] for i in indices] |
| 134 | # Now drop rules generating glyphs we don't want |
| 135 | indices = [i for i,seq in enumerate (self.Sequence) |
| 136 | if all (sub in s.glyphs for sub in seq.Substitute)] |
| 137 | self.Sequence = [self.Sequence[i] for i in indices] |
| 138 | self.Coverage.remap (indices) |
| 139 | self.SequenceCount = len (self.Sequence) |
| 140 | return bool (self.SequenceCount) |
| 141 | else: |
| 142 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 143 | |
| 144 | @add_method(fontTools.ttLib.tables.otTables.AlternateSubst) |
Behdad Esfahbod | 1d4fa13 | 2013-08-08 22:59:32 -0400 | [diff] [blame] | 145 | def closure_glyphs (self, s, cur_glyphs=None): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 146 | if cur_glyphs == None: cur_glyphs = s.glyphs |
| 147 | if self.Format == 1: |
| 148 | s.glyphs.update (*(vlist for g,vlist in self.alternates.iteritems() |
| 149 | if g in cur_glyphs)) |
| 150 | else: |
| 151 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 152 | |
| 153 | @add_method(fontTools.ttLib.tables.otTables.AlternateSubst) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 154 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 155 | if self.Format == 1: |
| 156 | self.alternates = {g:vlist for g,vlist in self.alternates.iteritems() |
| 157 | if g in s.glyphs and all (v in s.glyphs for v in vlist)} |
| 158 | return bool (self.alternates) |
| 159 | else: |
| 160 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 161 | |
| 162 | @add_method(fontTools.ttLib.tables.otTables.LigatureSubst) |
Behdad Esfahbod | 1d4fa13 | 2013-08-08 22:59:32 -0400 | [diff] [blame] | 163 | def closure_glyphs (self, s, cur_glyphs=None): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 164 | if cur_glyphs == None: cur_glyphs = s.glyphs |
| 165 | if self.Format == 1: |
| 166 | s.glyphs.update (*([seq.LigGlyph for seq in seqs |
| 167 | if all (c in s.glyphs for c in seq.Component)] |
| 168 | for g,seqs in self.ligatures.iteritems() |
| 169 | if g in cur_glyphs)) |
| 170 | else: |
| 171 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 172 | |
| 173 | @add_method(fontTools.ttLib.tables.otTables.LigatureSubst) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 174 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 175 | if self.Format == 1: |
| 176 | self.ligatures = {g:v for g,v in self.ligatures.iteritems() |
| 177 | if g in s.glyphs} |
| 178 | self.ligatures = {g:[seq for seq in seqs |
| 179 | if seq.LigGlyph in s.glyphs and |
| 180 | all (c in s.glyphs for c in seq.Component)] |
| 181 | for g,seqs in self.ligatures.iteritems()} |
| 182 | self.ligatures = {g:v for g,v in self.ligatures.iteritems() if v} |
| 183 | return bool (self.ligatures) |
| 184 | else: |
| 185 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 186 | |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 187 | @add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst) |
Behdad Esfahbod | 1d4fa13 | 2013-08-08 22:59:32 -0400 | [diff] [blame] | 188 | def closure_glyphs (self, s, cur_glyphs=None): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 189 | if cur_glyphs == None: cur_glyphs = s.glyphs |
| 190 | if self.Format == 1: |
| 191 | indices = self.Coverage.intersect (cur_glyphs) |
| 192 | if (not indices or |
| 193 | not all (c.intersect (s.glyphs) |
| 194 | for c in self.LookAheadCoverage + self.BacktrackCoverage)): |
| 195 | return |
| 196 | s.glyphs.update (self.Substitute[i] for i in indices) |
| 197 | else: |
| 198 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 199 | |
| 200 | @add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 201 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 202 | if self.Format == 1: |
| 203 | indices = self.Coverage.subset (s.glyphs) |
| 204 | self.Substitute = [self.Substitute[i] for i in indices] |
| 205 | # Now drop rules generating glyphs we don't want |
| 206 | indices = [i for i,sub in enumerate (self.Substitute) |
| 207 | if sub in s.glyphs] |
| 208 | self.Substitute = [self.Substitute[i] for i in indices] |
| 209 | self.Coverage.remap (indices) |
| 210 | self.GlyphCount = len (self.Substitute) |
| 211 | return bool (self.GlyphCount and |
| 212 | all (c.subset (s.glyphs) |
| 213 | for c in self.LookAheadCoverage+self.BacktrackCoverage)) |
| 214 | else: |
| 215 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 216 | |
| 217 | @add_method(fontTools.ttLib.tables.otTables.SinglePos) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 218 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 219 | if self.Format == 1: |
| 220 | return len (self.Coverage.subset (s.glyphs)) |
| 221 | elif self.Format == 2: |
| 222 | indices = self.Coverage.subset (s.glyphs) |
| 223 | self.Value = [self.Value[i] for i in indices] |
| 224 | self.ValueCount = len (self.Value) |
| 225 | return bool (self.ValueCount) |
| 226 | else: |
| 227 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 228 | |
| 229 | @add_method(fontTools.ttLib.tables.otTables.PairPos) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 230 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 231 | if self.Format == 1: |
| 232 | indices = self.Coverage.subset (s.glyphs) |
| 233 | self.PairSet = [self.PairSet[i] for i in indices] |
| 234 | for p in self.PairSet: |
| 235 | p.PairValueRecord = [r for r in p.PairValueRecord |
| 236 | if r.SecondGlyph in s.glyphs] |
| 237 | p.PairValueCount = len (p.PairValueRecord) |
| 238 | self.PairSet = [p for p in self.PairSet if p.PairValueCount] |
| 239 | self.PairSetCount = len (self.PairSet) |
| 240 | return bool (self.PairSetCount) |
| 241 | elif self.Format == 2: |
| 242 | class1_map = self.ClassDef1.subset (s.glyphs, remap=True) |
| 243 | class2_map = self.ClassDef2.subset (s.glyphs, remap=True) |
| 244 | self.Class1Record = [self.Class1Record[i] for i in class1_map] |
| 245 | for c in self.Class1Record: |
| 246 | c.Class2Record = [c.Class2Record[i] for i in class2_map] |
| 247 | self.Class1Count = len (class1_map) |
| 248 | self.Class2Count = len (class2_map) |
| 249 | return bool (self.Class1Count and |
| 250 | self.Class2Count and |
| 251 | self.Coverage.subset (s.glyphs)) |
| 252 | else: |
| 253 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 254 | |
| 255 | @add_method(fontTools.ttLib.tables.otTables.CursivePos) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 256 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 257 | if self.Format == 1: |
| 258 | indices = self.Coverage.subset (s.glyphs) |
| 259 | self.EntryExitRecord = [self.EntryExitRecord[i] for i in indices] |
| 260 | self.EntryExitCount = len (self.EntryExitRecord) |
| 261 | return bool (self.EntryExitCount) |
| 262 | else: |
| 263 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 264 | |
| 265 | @add_method(fontTools.ttLib.tables.otTables.MarkBasePos) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 266 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 267 | if self.Format == 1: |
| 268 | mark_indices = self.MarkCoverage.subset (s.glyphs) |
| 269 | self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] |
| 270 | for i in mark_indices] |
| 271 | self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord) |
| 272 | base_indices = self.BaseCoverage.subset (s.glyphs) |
| 273 | self.BaseArray.BaseRecord = [self.BaseArray.BaseRecord[i] |
| 274 | for i in base_indices] |
| 275 | self.BaseArray.BaseCount = len (self.BaseArray.BaseRecord) |
| 276 | # Prune empty classes |
| 277 | class_indices = unique_sorted (v.Class for v in self.MarkArray.MarkRecord) |
| 278 | self.ClassCount = len (class_indices) |
| 279 | for m in self.MarkArray.MarkRecord: |
| 280 | m.Class = class_indices.index (m.Class) |
| 281 | for b in self.BaseArray.BaseRecord: |
| 282 | b.BaseAnchor = [b.BaseAnchor[i] for i in class_indices] |
| 283 | return bool (self.ClassCount and |
| 284 | self.MarkArray.MarkCount and |
| 285 | self.BaseArray.BaseCount) |
| 286 | else: |
| 287 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 288 | |
| 289 | @add_method(fontTools.ttLib.tables.otTables.MarkLigPos) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 290 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 291 | if self.Format == 1: |
| 292 | mark_indices = self.MarkCoverage.subset (s.glyphs) |
| 293 | self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i] |
| 294 | for i in mark_indices] |
| 295 | self.MarkArray.MarkCount = len (self.MarkArray.MarkRecord) |
| 296 | ligature_indices = self.LigatureCoverage.subset (s.glyphs) |
| 297 | self.LigatureArray.LigatureAttach = [self.LigatureArray.LigatureAttach[i] |
| 298 | for i in ligature_indices] |
| 299 | self.LigatureArray.LigatureCount = len (self.LigatureArray.LigatureAttach) |
| 300 | # Prune empty classes |
| 301 | class_indices = unique_sorted (v.Class for v in self.MarkArray.MarkRecord) |
| 302 | self.ClassCount = len (class_indices) |
| 303 | for m in self.MarkArray.MarkRecord: |
| 304 | m.Class = class_indices.index (m.Class) |
| 305 | for l in self.LigatureArray.LigatureAttach: |
| 306 | for c in l.ComponentRecord: |
| 307 | c.LigatureAnchor = [c.LigatureAnchor[i] for i in class_indices] |
| 308 | return bool (self.ClassCount and |
| 309 | self.MarkArray.MarkCount and |
| 310 | self.LigatureArray.LigatureCount) |
| 311 | else: |
| 312 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 313 | |
| 314 | @add_method(fontTools.ttLib.tables.otTables.MarkMarkPos) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 315 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 316 | if self.Format == 1: |
| 317 | mark1_indices = self.Mark1Coverage.subset (s.glyphs) |
| 318 | self.Mark1Array.MarkRecord = [self.Mark1Array.MarkRecord[i] |
| 319 | for i in mark1_indices] |
| 320 | self.Mark1Array.MarkCount = len (self.Mark1Array.MarkRecord) |
| 321 | mark2_indices = self.Mark2Coverage.subset (s.glyphs) |
| 322 | self.Mark2Array.Mark2Record = [self.Mark2Array.Mark2Record[i] |
| 323 | for i in mark2_indices] |
| 324 | self.Mark2Array.MarkCount = len (self.Mark2Array.Mark2Record) |
| 325 | # Prune empty classes |
| 326 | class_indices = unique_sorted (v.Class for v in self.Mark1Array.MarkRecord) |
| 327 | self.ClassCount = len (class_indices) |
| 328 | for m in self.Mark1Array.MarkRecord: |
| 329 | m.Class = class_indices.index (m.Class) |
| 330 | for b in self.Mark2Array.Mark2Record: |
| 331 | b.Mark2Anchor = [b.Mark2Anchor[i] for i in class_indices] |
| 332 | return bool (self.ClassCount and |
| 333 | self.Mark1Array.MarkCount and |
| 334 | self.Mark2Array.MarkCount) |
| 335 | else: |
| 336 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 337 | |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 338 | @add_method(fontTools.ttLib.tables.otTables.SingleSubst, |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 339 | fontTools.ttLib.tables.otTables.MultipleSubst, |
| 340 | fontTools.ttLib.tables.otTables.AlternateSubst, |
| 341 | fontTools.ttLib.tables.otTables.LigatureSubst, |
| 342 | fontTools.ttLib.tables.otTables.ReverseChainSingleSubst, |
| 343 | fontTools.ttLib.tables.otTables.SinglePos, |
| 344 | fontTools.ttLib.tables.otTables.PairPos, |
| 345 | fontTools.ttLib.tables.otTables.CursivePos, |
| 346 | fontTools.ttLib.tables.otTables.MarkBasePos, |
| 347 | fontTools.ttLib.tables.otTables.MarkLigPos, |
| 348 | fontTools.ttLib.tables.otTables.MarkMarkPos) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 349 | def subset_lookups (self, lookup_indices): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 350 | pass |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 351 | |
| 352 | @add_method(fontTools.ttLib.tables.otTables.SingleSubst, |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 353 | fontTools.ttLib.tables.otTables.MultipleSubst, |
| 354 | fontTools.ttLib.tables.otTables.AlternateSubst, |
| 355 | fontTools.ttLib.tables.otTables.LigatureSubst, |
| 356 | fontTools.ttLib.tables.otTables.ReverseChainSingleSubst, |
| 357 | fontTools.ttLib.tables.otTables.SinglePos, |
| 358 | fontTools.ttLib.tables.otTables.PairPos, |
| 359 | fontTools.ttLib.tables.otTables.CursivePos, |
| 360 | fontTools.ttLib.tables.otTables.MarkBasePos, |
| 361 | fontTools.ttLib.tables.otTables.MarkLigPos, |
| 362 | fontTools.ttLib.tables.otTables.MarkMarkPos) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 363 | def collect_lookups (self): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 364 | return [] |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 365 | |
Behdad Esfahbod | aeacc15 | 2013-08-12 20:24:33 -0400 | [diff] [blame] | 366 | @add_method(fontTools.ttLib.tables.otTables.SingleSubst, |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 367 | fontTools.ttLib.tables.otTables.AlternateSubst, |
| 368 | fontTools.ttLib.tables.otTables.ReverseChainSingleSubst) |
Behdad Esfahbod | aeacc15 | 2013-08-12 20:24:33 -0400 | [diff] [blame] | 369 | def may_have_non_1to1 (self): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 370 | return False |
Behdad Esfahbod | aeacc15 | 2013-08-12 20:24:33 -0400 | [diff] [blame] | 371 | |
| 372 | @add_method(fontTools.ttLib.tables.otTables.MultipleSubst, |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 373 | fontTools.ttLib.tables.otTables.LigatureSubst, |
| 374 | fontTools.ttLib.tables.otTables.ContextSubst, |
| 375 | fontTools.ttLib.tables.otTables.ChainContextSubst) |
Behdad Esfahbod | aeacc15 | 2013-08-12 20:24:33 -0400 | [diff] [blame] | 376 | def may_have_non_1to1 (self): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 377 | return True |
Behdad Esfahbod | aeacc15 | 2013-08-12 20:24:33 -0400 | [diff] [blame] | 378 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 379 | @add_method(fontTools.ttLib.tables.otTables.ContextSubst, |
| 380 | fontTools.ttLib.tables.otTables.ChainContextSubst, |
| 381 | fontTools.ttLib.tables.otTables.ContextPos, |
| 382 | fontTools.ttLib.tables.otTables.ChainContextPos) |
Behdad Esfahbod | 44c2b3c | 2013-07-23 16:00:32 -0400 | [diff] [blame] | 383 | def __classify_context (self): |
Behdad Esfahbod | b178dca | 2013-07-23 22:51:50 -0400 | [diff] [blame] | 384 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 385 | class ContextHelper: |
| 386 | def __init__ (self, klass, Format): |
| 387 | if klass.__name__.endswith ('Subst'): |
| 388 | Typ = 'Sub' |
| 389 | Type = 'Subst' |
| 390 | else: |
| 391 | Typ = 'Pos' |
| 392 | Type = 'Pos' |
| 393 | if klass.__name__.startswith ('Chain'): |
| 394 | Chain = 'Chain' |
| 395 | else: |
| 396 | Chain = '' |
| 397 | ChainTyp = Chain+Typ |
Behdad Esfahbod | 9e73572 | 2013-07-23 16:35:23 -0400 | [diff] [blame] | 398 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 399 | self.Typ = Typ |
| 400 | self.Type = Type |
| 401 | self.Chain = Chain |
| 402 | self.ChainTyp = ChainTyp |
Behdad Esfahbod | 9e73572 | 2013-07-23 16:35:23 -0400 | [diff] [blame] | 403 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 404 | self.LookupRecord = Type+'LookupRecord' |
Behdad Esfahbod | 9e73572 | 2013-07-23 16:35:23 -0400 | [diff] [blame] | 405 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 406 | if Format == 1: |
| 407 | Coverage = lambda r: r.Coverage |
| 408 | ChainCoverage = lambda r: r.Coverage |
| 409 | ContextData = lambda r: (None,) |
| 410 | ChainContextData = lambda r: (None, None, None) |
| 411 | RuleData = lambda r: (r.Input,) |
| 412 | ChainRuleData = lambda r: (r.Backtrack, r.Input, r.LookAhead) |
| 413 | SetRuleData = None |
| 414 | ChainSetRuleData = None |
| 415 | elif Format == 2: |
| 416 | Coverage = lambda r: r.Coverage |
| 417 | ChainCoverage = lambda r: r.Coverage |
| 418 | ContextData = lambda r: (r.ClassDef,) |
| 419 | ChainContextData = lambda r: (r.LookAheadClassDef, |
| 420 | r.InputClassDef, |
| 421 | r.BacktrackClassDef) |
| 422 | RuleData = lambda r: (r.Class,) |
| 423 | ChainRuleData = lambda r: (r.LookAhead, r.Input, r.Backtrack) |
| 424 | def SetRuleData (r, d): (r.Class,) = d |
| 425 | def ChainSetRuleData (r, d): (r.LookAhead, r.Input, r.Backtrack) = d |
| 426 | elif Format == 3: |
| 427 | Coverage = lambda r: r.Coverage[0] |
| 428 | ChainCoverage = lambda r: r.InputCoverage[0] |
| 429 | ContextData = None |
| 430 | ChainContextData = None |
| 431 | RuleData = lambda r: r.Coverage |
| 432 | ChainRuleData = lambda r: (r.LookAheadCoverage + |
| 433 | r.InputCoverage + |
| 434 | r.BacktrackCoverage) |
| 435 | SetRuleData = None |
| 436 | ChainSetRuleData = None |
| 437 | else: |
| 438 | assert 0, "unknown format: %s" % Format |
Behdad Esfahbod | 452ab6c | 2013-07-23 22:57:43 -0400 | [diff] [blame] | 439 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 440 | if Chain: |
| 441 | self.Coverage = ChainCoverage |
| 442 | self.ContextData = ChainContextData |
| 443 | self.RuleData = ChainRuleData |
| 444 | self.SetRuleData = ChainSetRuleData |
| 445 | else: |
| 446 | self.Coverage = Coverage |
| 447 | self.ContextData = ContextData |
| 448 | self.RuleData = RuleData |
| 449 | self.SetRuleData = SetRuleData |
Behdad Esfahbod | 9e73572 | 2013-07-23 16:35:23 -0400 | [diff] [blame] | 450 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 451 | if Format == 1: |
| 452 | self.Rule = ChainTyp+'Rule' |
| 453 | self.RuleCount = ChainTyp+'RuleCount' |
| 454 | self.RuleSet = ChainTyp+'RuleSet' |
| 455 | self.RuleSetCount = ChainTyp+'RuleSetCount' |
| 456 | self.Intersect = lambda glyphs, c, r: [r] if r in glyphs else [] |
| 457 | elif Format == 2: |
| 458 | self.Rule = ChainTyp+'ClassRule' |
| 459 | self.RuleCount = ChainTyp+'ClassRuleCount' |
| 460 | self.RuleSet = ChainTyp+'ClassSet' |
| 461 | self.RuleSetCount = ChainTyp+'ClassSetCount' |
| 462 | self.Intersect = lambda glyphs, c, r: c.intersect_class (glyphs, r) |
Behdad Esfahbod | 8998700 | 2013-07-23 23:07:42 -0400 | [diff] [blame] | 463 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 464 | self.ClassDef = 'InputClassDef' if Chain else 'ClassDef' |
Behdad Esfahbod | 2710839 | 2013-07-23 16:40:47 -0400 | [diff] [blame] | 465 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 466 | if self.Format not in [1, 2, 3]: |
| 467 | return None # Don't shoot the messenger; let it go |
| 468 | if not hasattr (self.__class__, "__ContextHelpers"): |
| 469 | self.__class__.__ContextHelpers = {} |
| 470 | if self.Format not in self.__class__.__ContextHelpers: |
| 471 | helper = ContextHelper (self.__class__, self.Format) |
| 472 | self.__class__.__ContextHelpers[self.Format] = helper |
| 473 | return self.__class__.__ContextHelpers[self.Format] |
Behdad Esfahbod | 44c2b3c | 2013-07-23 16:00:32 -0400 | [diff] [blame] | 474 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 475 | @add_method(fontTools.ttLib.tables.otTables.ContextSubst, |
| 476 | fontTools.ttLib.tables.otTables.ChainContextSubst) |
Behdad Esfahbod | 1d4fa13 | 2013-08-08 22:59:32 -0400 | [diff] [blame] | 477 | def closure_glyphs (self, s, cur_glyphs=None): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 478 | if cur_glyphs == None: cur_glyphs = s.glyphs |
| 479 | c = self.__classify_context () |
Behdad Esfahbod | 1ab2dbf | 2013-07-23 17:17:21 -0400 | [diff] [blame] | 480 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 481 | indices = c.Coverage (self).intersect (s.glyphs) |
| 482 | if not indices: |
| 483 | return [] |
| 484 | cur_glyphs = c.Coverage (self).intersect_glyphs (s.glyphs); |
Behdad Esfahbod | 1d4fa13 | 2013-08-08 22:59:32 -0400 | [diff] [blame] | 485 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 486 | if self.Format == 1: |
| 487 | ContextData = c.ContextData (self) |
| 488 | rss = getattr (self, c.RuleSet) |
| 489 | for i in indices: |
| 490 | if not rss[i]: continue |
| 491 | for r in getattr (rss[i], c.Rule): |
| 492 | if not r: continue |
| 493 | if all (all (c.Intersect (s.glyphs, cd, k) for k in klist) |
| 494 | for cd,klist in zip (ContextData, c.RuleData (r))): |
| 495 | chaos = False |
| 496 | for ll in getattr (r, c.LookupRecord): |
| 497 | if not ll: continue |
| 498 | seqi = ll.SequenceIndex |
| 499 | if seqi == 0: |
| 500 | pos_glyphs = set (c.Coverage (self).glyphs[i]) |
| 501 | else: |
| 502 | if chaos: |
| 503 | pos_glyphs = s.glyphs |
| 504 | else: |
| 505 | pos_glyphs = set (r.Input[seqi - 1]) |
| 506 | lookup = s.table.LookupList.Lookup[ll.LookupListIndex] |
| 507 | chaos = chaos or lookup.may_have_non_1to1 () |
| 508 | lookup.closure_glyphs (s, cur_glyphs=pos_glyphs) |
| 509 | elif self.Format == 2: |
| 510 | ClassDef = getattr (self, c.ClassDef) |
| 511 | indices = ClassDef.intersect (cur_glyphs) |
| 512 | ContextData = c.ContextData (self) |
| 513 | rss = getattr (self, c.RuleSet) |
| 514 | for i in indices: |
| 515 | if not rss[i]: continue |
| 516 | for r in getattr (rss[i], c.Rule): |
| 517 | if not r: continue |
| 518 | if all (all (c.Intersect (s.glyphs, cd, k) for k in klist) |
| 519 | for cd,klist in zip (ContextData, c.RuleData (r))): |
| 520 | chaos = False |
| 521 | for ll in getattr (r, c.LookupRecord): |
| 522 | if not ll: continue |
| 523 | seqi = ll.SequenceIndex |
| 524 | if seqi == 0: |
| 525 | pos_glyphs = ClassDef.intersect_class (cur_glyphs, i) |
| 526 | else: |
| 527 | if chaos: |
| 528 | pos_glyphs = s.glyphs |
| 529 | else: |
| 530 | pos_glyphs = ClassDef.intersect_class (s.glyphs, |
| 531 | r.Input[seqi - 1]) |
| 532 | lookup = s.table.LookupList.Lookup[ll.LookupListIndex] |
| 533 | chaos = chaos or lookup.may_have_non_1to1 () |
| 534 | lookup.closure_glyphs (s, cur_glyphs=pos_glyphs) |
| 535 | elif self.Format == 3: |
| 536 | if not all (x.intersect (s.glyphs) for x in c.RuleData (self)): |
| 537 | return [] |
| 538 | r = self |
| 539 | chaos = False |
| 540 | for ll in getattr (r, c.LookupRecord): |
| 541 | if not ll: continue |
| 542 | seqi = ll.SequenceIndex |
| 543 | if seqi == 0: |
| 544 | pos_glyphs = cur_glyphs |
| 545 | else: |
| 546 | if chaos: |
| 547 | pos_glyphs = s.glyphs |
| 548 | else: |
| 549 | pos_glyphs = r.InputCoverage[seqi].intersect_glyphs (s.glyphs) |
| 550 | lookup = s.table.LookupList.Lookup[ll.LookupListIndex] |
| 551 | chaos = chaos or lookup.may_have_non_1to1 () |
| 552 | lookup.closure_glyphs (s, cur_glyphs=pos_glyphs) |
| 553 | else: |
| 554 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 0077697 | 2013-07-23 15:33:00 -0400 | [diff] [blame] | 555 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 556 | @add_method(fontTools.ttLib.tables.otTables.ContextSubst, |
| 557 | fontTools.ttLib.tables.otTables.ContextPos, |
| 558 | fontTools.ttLib.tables.otTables.ChainContextSubst, |
| 559 | fontTools.ttLib.tables.otTables.ChainContextPos) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 560 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 561 | c = self.__classify_context () |
Behdad Esfahbod | d8c7e10 | 2013-07-23 17:07:06 -0400 | [diff] [blame] | 562 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 563 | if self.Format == 1: |
| 564 | indices = self.Coverage.subset (s.glyphs) |
| 565 | rss = getattr (self, c.RuleSet) |
| 566 | rss = [rss[i] for i in indices] |
| 567 | for rs in rss: |
| 568 | if not rs: continue |
| 569 | ss = getattr (rs, c.Rule) |
| 570 | ss = [r for r in ss |
| 571 | if r and all (all (g in s.glyphs for g in glist) |
| 572 | for glist in c.RuleData (r))] |
| 573 | setattr (rs, c.Rule, ss) |
| 574 | setattr (rs, c.RuleCount, len (ss)) |
| 575 | # Prune empty subrulesets |
| 576 | rss = [rs for rs in rss if rs and getattr (rs, c.Rule)] |
| 577 | setattr (self, c.RuleSet, rss) |
| 578 | setattr (self, c.RuleSetCount, len (rss)) |
| 579 | return bool (rss) |
| 580 | elif self.Format == 2: |
| 581 | if not self.Coverage.subset (s.glyphs): |
| 582 | return False |
| 583 | indices = getattr (self, c.ClassDef).subset (self.Coverage.glyphs, |
| 584 | remap=False) |
| 585 | rss = getattr (self, c.RuleSet) |
| 586 | rss = [rss[i] for i in indices] |
| 587 | ContextData = c.ContextData (self) |
| 588 | klass_maps = [x.subset (s.glyphs, remap=True) for x in ContextData] |
| 589 | for rs in rss: |
| 590 | if not rs: continue |
| 591 | ss = getattr (rs, c.Rule) |
| 592 | ss = [r for r in ss |
| 593 | if r and all (all (k in klass_map for k in klist) |
| 594 | for klass_map,klist in zip (klass_maps, c.RuleData (r)))] |
| 595 | setattr (rs, c.Rule, ss) |
| 596 | setattr (rs, c.RuleCount, len (ss)) |
Behdad Esfahbod | e9a3bd6 | 2013-07-23 22:41:11 -0400 | [diff] [blame] | 597 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 598 | # Remap rule classes |
| 599 | for r in ss: |
| 600 | c.SetRuleData (r, [[klass_map.index (k) for k in klist] |
| 601 | for klass_map,klist in zip (klass_maps, c.RuleData (r))]) |
| 602 | # Prune empty subrulesets |
| 603 | rss = [rs for rs in rss if rs and getattr (rs, c.Rule)] |
| 604 | setattr (self, c.RuleSet, rss) |
| 605 | setattr (self, c.RuleSetCount, len (rss)) |
| 606 | return bool (rss) |
| 607 | elif self.Format == 3: |
| 608 | return all (x.subset (s.glyphs) for x in c.RuleData (self)) |
| 609 | else: |
| 610 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 611 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 612 | @add_method(fontTools.ttLib.tables.otTables.ContextSubst, |
| 613 | fontTools.ttLib.tables.otTables.ChainContextSubst, |
| 614 | fontTools.ttLib.tables.otTables.ContextPos, |
| 615 | fontTools.ttLib.tables.otTables.ChainContextPos) |
Behdad Esfahbod | 59dfc13 | 2013-07-23 15:39:20 -0400 | [diff] [blame] | 616 | def subset_lookups (self, lookup_indices): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 617 | c = self.__classify_context () |
Behdad Esfahbod | 44c2b3c | 2013-07-23 16:00:32 -0400 | [diff] [blame] | 618 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 619 | if self.Format in [1, 2]: |
| 620 | for rs in getattr (self, c.RuleSet): |
| 621 | if not rs: continue |
| 622 | for r in getattr (rs, c.Rule): |
| 623 | if not r: continue |
| 624 | setattr (r, c.LookupRecord, |
| 625 | [ll for ll in getattr (r, c.LookupRecord) |
| 626 | if ll and ll.LookupListIndex in lookup_indices]) |
| 627 | for ll in getattr (r, c.LookupRecord): |
| 628 | if not ll: continue |
| 629 | ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex) |
| 630 | elif self.Format == 3: |
| 631 | setattr (self, c.LookupRecord, |
| 632 | [ll for ll in getattr (self, c.LookupRecord) |
| 633 | if ll and ll.LookupListIndex in lookup_indices]) |
| 634 | for ll in getattr (self, c.LookupRecord): |
| 635 | if not ll: continue |
| 636 | ll.LookupListIndex = lookup_indices.index (ll.LookupListIndex) |
| 637 | else: |
| 638 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 59dfc13 | 2013-07-23 15:39:20 -0400 | [diff] [blame] | 639 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 640 | @add_method(fontTools.ttLib.tables.otTables.ContextSubst, |
| 641 | fontTools.ttLib.tables.otTables.ChainContextSubst, |
| 642 | fontTools.ttLib.tables.otTables.ContextPos, |
| 643 | fontTools.ttLib.tables.otTables.ChainContextPos) |
Behdad Esfahbod | 59dfc13 | 2013-07-23 15:39:20 -0400 | [diff] [blame] | 644 | def collect_lookups (self): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 645 | c = self.__classify_context () |
Behdad Esfahbod | 44c2b3c | 2013-07-23 16:00:32 -0400 | [diff] [blame] | 646 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 647 | if self.Format in [1, 2]: |
| 648 | return [ll.LookupListIndex |
| 649 | for rs in getattr (self, c.RuleSet) if rs |
| 650 | for r in getattr (rs, c.Rule) if r |
| 651 | for ll in getattr (r, c.LookupRecord) if ll] |
| 652 | elif self.Format == 3: |
| 653 | return [ll.LookupListIndex |
| 654 | for ll in getattr (self, c.LookupRecord) if ll] |
| 655 | else: |
| 656 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 59dfc13 | 2013-07-23 15:39:20 -0400 | [diff] [blame] | 657 | |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 658 | @add_method(fontTools.ttLib.tables.otTables.ExtensionSubst) |
Behdad Esfahbod | 1d4fa13 | 2013-08-08 22:59:32 -0400 | [diff] [blame] | 659 | def closure_glyphs (self, s, cur_glyphs=None): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 660 | if self.Format == 1: |
| 661 | self.ExtSubTable.closure_glyphs (s, cur_glyphs) |
| 662 | else: |
| 663 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 664 | |
Behdad Esfahbod | aeacc15 | 2013-08-12 20:24:33 -0400 | [diff] [blame] | 665 | @add_method(fontTools.ttLib.tables.otTables.ExtensionSubst) |
| 666 | def may_have_non_1to1 (self): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 667 | if self.Format == 1: |
| 668 | return self.ExtSubTable.may_have_non_1to1 () |
| 669 | else: |
| 670 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | aeacc15 | 2013-08-12 20:24:33 -0400 | [diff] [blame] | 671 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 672 | @add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, |
| 673 | fontTools.ttLib.tables.otTables.ExtensionPos) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 674 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 675 | if self.Format == 1: |
| 676 | return self.ExtSubTable.subset_glyphs (s) |
| 677 | else: |
| 678 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 679 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 680 | @add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, |
| 681 | fontTools.ttLib.tables.otTables.ExtensionPos) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 682 | def subset_lookups (self, lookup_indices): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 683 | if self.Format == 1: |
| 684 | return self.ExtSubTable.subset_lookups (lookup_indices) |
| 685 | else: |
| 686 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 687 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 688 | @add_method(fontTools.ttLib.tables.otTables.ExtensionSubst, |
| 689 | fontTools.ttLib.tables.otTables.ExtensionPos) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 690 | def collect_lookups (self): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 691 | if self.Format == 1: |
| 692 | return self.ExtSubTable.collect_lookups () |
| 693 | else: |
| 694 | assert 0, "unknown format: %s" % self.Format |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 695 | |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 696 | @add_method(fontTools.ttLib.tables.otTables.Lookup) |
Behdad Esfahbod | 1d4fa13 | 2013-08-08 22:59:32 -0400 | [diff] [blame] | 697 | def closure_glyphs (self, s, cur_glyphs=None): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 698 | for st in self.SubTable: |
| 699 | if not st: continue |
| 700 | st.closure_glyphs (s, cur_glyphs) |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 701 | |
| 702 | @add_method(fontTools.ttLib.tables.otTables.Lookup) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 703 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 704 | self.SubTable = [st for st in self.SubTable if st and st.subset_glyphs (s)] |
| 705 | self.SubTableCount = len (self.SubTable) |
| 706 | return bool (self.SubTableCount) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 707 | |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 708 | @add_method(fontTools.ttLib.tables.otTables.Lookup) |
| 709 | def subset_lookups (self, lookup_indices): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 710 | for s in self.SubTable: |
| 711 | s.subset_lookups (lookup_indices) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 712 | |
| 713 | @add_method(fontTools.ttLib.tables.otTables.Lookup) |
| 714 | def collect_lookups (self): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 715 | return unique_sorted (sum ((st.collect_lookups () for st in self.SubTable |
| 716 | if st), [])) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 717 | |
Behdad Esfahbod | aeacc15 | 2013-08-12 20:24:33 -0400 | [diff] [blame] | 718 | @add_method(fontTools.ttLib.tables.otTables.Lookup) |
| 719 | def may_have_non_1to1 (self): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 720 | return any (st.may_have_non_1to1 () for st in self.SubTable if st) |
Behdad Esfahbod | aeacc15 | 2013-08-12 20:24:33 -0400 | [diff] [blame] | 721 | |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 722 | @add_method(fontTools.ttLib.tables.otTables.LookupList) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 723 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 724 | "Returns the indices of nonempty lookups." |
| 725 | return [i for (i,l) in enumerate (self.Lookup) if l and l.subset_glyphs (s)] |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 726 | |
| 727 | @add_method(fontTools.ttLib.tables.otTables.LookupList) |
| 728 | def subset_lookups (self, lookup_indices): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 729 | self.Lookup = [self.Lookup[i] for i in lookup_indices |
| 730 | if i < self.LookupCount] |
| 731 | self.LookupCount = len (self.Lookup) |
| 732 | for l in self.Lookup: |
| 733 | l.subset_lookups (lookup_indices) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 734 | |
| 735 | @add_method(fontTools.ttLib.tables.otTables.LookupList) |
| 736 | def closure_lookups (self, lookup_indices): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 737 | lookup_indices = unique_sorted (lookup_indices) |
| 738 | recurse = lookup_indices |
| 739 | while True: |
| 740 | recurse_lookups = sum ((self.Lookup[i].collect_lookups () |
| 741 | for i in recurse if i < self.LookupCount), []) |
| 742 | recurse_lookups = [l for l in recurse_lookups |
| 743 | if l not in lookup_indices and l < self.LookupCount] |
| 744 | if not recurse_lookups: |
| 745 | return unique_sorted (lookup_indices) |
| 746 | recurse_lookups = unique_sorted (recurse_lookups) |
| 747 | lookup_indices.extend (recurse_lookups) |
| 748 | recurse = recurse_lookups |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 749 | |
| 750 | @add_method(fontTools.ttLib.tables.otTables.Feature) |
| 751 | def subset_lookups (self, lookup_indices): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 752 | self.LookupListIndex = [l for l in self.LookupListIndex |
| 753 | if l in lookup_indices] |
| 754 | # Now map them. |
| 755 | self.LookupListIndex = [lookup_indices.index (l) |
| 756 | for l in self.LookupListIndex] |
| 757 | self.LookupCount = len (self.LookupListIndex) |
| 758 | return self.LookupCount |
Behdad Esfahbod | 5466061 | 2013-07-21 18:16:55 -0400 | [diff] [blame] | 759 | |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 760 | @add_method(fontTools.ttLib.tables.otTables.Feature) |
| 761 | def collect_lookups (self): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 762 | return self.LookupListIndex[:] |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 763 | |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 764 | @add_method(fontTools.ttLib.tables.otTables.FeatureList) |
| 765 | def subset_lookups (self, lookup_indices): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 766 | "Returns the indices of nonempty features." |
| 767 | feature_indices = [i for (i,f) in enumerate (self.FeatureRecord) |
| 768 | if f.Feature.subset_lookups (lookup_indices)] |
| 769 | self.subset_features (feature_indices) |
| 770 | return feature_indices |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 771 | |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 772 | @add_method(fontTools.ttLib.tables.otTables.FeatureList) |
| 773 | def collect_lookups (self, feature_indices): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 774 | return unique_sorted (sum ((self.FeatureRecord[i].Feature.collect_lookups () |
| 775 | for i in feature_indices |
| 776 | if i < self.FeatureCount), [])) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 777 | |
Behdad Esfahbod | 356c42e | 2013-07-23 12:10:46 -0400 | [diff] [blame] | 778 | @add_method(fontTools.ttLib.tables.otTables.FeatureList) |
| 779 | def subset_features (self, feature_indices): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 780 | self.FeatureRecord = [self.FeatureRecord[i] for i in feature_indices] |
| 781 | self.FeatureCount = len (self.FeatureRecord) |
| 782 | return bool (self.FeatureCount) |
Behdad Esfahbod | 356c42e | 2013-07-23 12:10:46 -0400 | [diff] [blame] | 783 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 784 | @add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, |
| 785 | fontTools.ttLib.tables.otTables.LangSys) |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 786 | def subset_features (self, feature_indices): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 787 | if self.ReqFeatureIndex in feature_indices: |
| 788 | self.ReqFeatureIndex = feature_indices.index (self.ReqFeatureIndex) |
| 789 | else: |
| 790 | self.ReqFeatureIndex = 65535 |
| 791 | self.FeatureIndex = [f for f in self.FeatureIndex if f in feature_indices] |
| 792 | # Now map them. |
| 793 | self.FeatureIndex = [feature_indices.index (f) for f in self.FeatureIndex |
| 794 | if f in feature_indices] |
| 795 | self.FeatureCount = len (self.FeatureIndex) |
| 796 | return bool (self.FeatureCount or self.ReqFeatureIndex != 65535) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 797 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 798 | @add_method(fontTools.ttLib.tables.otTables.DefaultLangSys, |
| 799 | fontTools.ttLib.tables.otTables.LangSys) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 800 | def collect_features (self): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 801 | feature_indices = self.FeatureIndex[:] |
| 802 | if self.ReqFeatureIndex != 65535: |
| 803 | feature_indices.append (self.ReqFeatureIndex) |
| 804 | return unique_sorted (feature_indices) |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 805 | |
| 806 | @add_method(fontTools.ttLib.tables.otTables.Script) |
| 807 | def subset_features (self, feature_indices): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 808 | if (self.DefaultLangSys and |
| 809 | not self.DefaultLangSys.subset_features (feature_indices)): |
| 810 | self.DefaultLangSys = None |
| 811 | self.LangSysRecord = [l for l in self.LangSysRecord |
| 812 | if l.LangSys.subset_features (feature_indices)] |
| 813 | self.LangSysCount = len (self.LangSysRecord) |
| 814 | return bool (self.LangSysCount or self.DefaultLangSys) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 815 | |
| 816 | @add_method(fontTools.ttLib.tables.otTables.Script) |
| 817 | def collect_features (self): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 818 | feature_indices = [l.LangSys.collect_features () for l in self.LangSysRecord] |
| 819 | if self.DefaultLangSys: |
| 820 | feature_indices.append (self.DefaultLangSys.collect_features ()) |
| 821 | return unique_sorted (sum (feature_indices, [])) |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 822 | |
| 823 | @add_method(fontTools.ttLib.tables.otTables.ScriptList) |
| 824 | def subset_features (self, feature_indices): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 825 | self.ScriptRecord = [s for s in self.ScriptRecord |
| 826 | if s.Script.subset_features (feature_indices)] |
| 827 | self.ScriptCount = len (self.ScriptRecord) |
| 828 | return bool (self.ScriptCount) |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 829 | |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 830 | @add_method(fontTools.ttLib.tables.otTables.ScriptList) |
| 831 | def collect_features (self): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 832 | return unique_sorted (sum ((s.Script.collect_features () |
| 833 | for s in self.ScriptRecord), [])) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 834 | |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 835 | @add_method(fontTools.ttLib.getTableClass('GSUB')) |
Behdad Esfahbod | 254442b | 2013-07-31 14:20:13 -0400 | [diff] [blame] | 836 | def closure_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 837 | s.table = self.table |
| 838 | feature_indices = self.table.ScriptList.collect_features () |
| 839 | lookup_indices = self.table.FeatureList.collect_lookups (feature_indices) |
| 840 | while True: |
| 841 | orig_glyphs = s.glyphs.copy () |
| 842 | for i in lookup_indices: |
| 843 | if i >= self.table.LookupList.LookupCount: continue |
| 844 | if not self.table.LookupList.Lookup[i]: continue |
| 845 | self.table.LookupList.Lookup[i].closure_glyphs (s) |
| 846 | if orig_glyphs == s.glyphs: |
| 847 | break |
| 848 | del s.table |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 849 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 850 | @add_method(fontTools.ttLib.getTableClass('GSUB'), |
| 851 | fontTools.ttLib.getTableClass('GPOS')) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 852 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 853 | s.glyphs = s.glyphs_gsubed |
| 854 | lookup_indices = self.table.LookupList.subset_glyphs (s) |
| 855 | self.subset_lookups (lookup_indices) |
| 856 | self.prune_lookups () |
| 857 | return True |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 858 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 859 | @add_method(fontTools.ttLib.getTableClass('GSUB'), |
| 860 | fontTools.ttLib.getTableClass('GPOS')) |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 861 | def subset_lookups (self, lookup_indices): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 862 | """Retrains specified lookups, then removes empty features, language |
| 863 | systems, and scripts.""" |
| 864 | self.table.LookupList.subset_lookups (lookup_indices) |
| 865 | feature_indices = self.table.FeatureList.subset_lookups (lookup_indices) |
| 866 | self.table.ScriptList.subset_features (feature_indices) |
Behdad Esfahbod | 77cda41 | 2013-07-22 11:46:50 -0400 | [diff] [blame] | 867 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 868 | @add_method(fontTools.ttLib.getTableClass('GSUB'), |
| 869 | fontTools.ttLib.getTableClass('GPOS')) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 870 | def prune_lookups (self): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 871 | "Remove unreferenced lookups" |
| 872 | feature_indices = self.table.ScriptList.collect_features () |
| 873 | lookup_indices = self.table.FeatureList.collect_lookups (feature_indices) |
| 874 | lookup_indices = self.table.LookupList.closure_lookups (lookup_indices) |
| 875 | self.subset_lookups (lookup_indices) |
Behdad Esfahbod | 78661bb | 2013-07-23 10:23:42 -0400 | [diff] [blame] | 876 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 877 | @add_method(fontTools.ttLib.getTableClass('GSUB'), |
| 878 | fontTools.ttLib.getTableClass('GPOS')) |
Behdad Esfahbod | 356c42e | 2013-07-23 12:10:46 -0400 | [diff] [blame] | 879 | def subset_feature_tags (self, feature_tags): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 880 | feature_indices = [i for (i,f) in |
| 881 | enumerate (self.table.FeatureList.FeatureRecord) |
| 882 | if f.FeatureTag in feature_tags] |
| 883 | self.table.FeatureList.subset_features (feature_indices) |
| 884 | self.table.ScriptList.subset_features (feature_indices) |
Behdad Esfahbod | 356c42e | 2013-07-23 12:10:46 -0400 | [diff] [blame] | 885 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 886 | @add_method(fontTools.ttLib.getTableClass('GSUB'), |
| 887 | fontTools.ttLib.getTableClass('GPOS')) |
Behdad Esfahbod | d7b6f8f | 2013-07-23 12:46:52 -0400 | [diff] [blame] | 888 | def prune_pre_subset (self, options): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 889 | if options.layout_features and '*' not in options.layout_features: |
| 890 | self.subset_feature_tags (options.layout_features) |
| 891 | self.prune_lookups () |
| 892 | return True |
Behdad Esfahbod | 356c42e | 2013-07-23 12:10:46 -0400 | [diff] [blame] | 893 | |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 894 | @add_method(fontTools.ttLib.getTableClass('GDEF')) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 895 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 896 | glyphs = s.glyphs_gsubed |
| 897 | table = self.table |
| 898 | if table.LigCaretList: |
| 899 | indices = table.LigCaretList.Coverage.subset (glyphs) |
| 900 | table.LigCaretList.LigGlyph = [table.LigCaretList.LigGlyph[i] |
| 901 | for i in indices] |
| 902 | table.LigCaretList.LigGlyphCount = len (table.LigCaretList.LigGlyph) |
| 903 | if not table.LigCaretList.LigGlyphCount: |
| 904 | table.LigCaretList = None |
| 905 | if table.MarkAttachClassDef: |
| 906 | table.MarkAttachClassDef.classDefs = {g:v for g,v in |
| 907 | table.MarkAttachClassDef.classDefs.iteritems() |
| 908 | if g in glyphs} |
| 909 | if not table.MarkAttachClassDef.classDefs: |
| 910 | table.MarkAttachClassDef = None |
| 911 | if table.GlyphClassDef: |
| 912 | table.GlyphClassDef.classDefs = {g:v for g,v in |
| 913 | table.GlyphClassDef.classDefs.iteritems() |
| 914 | if g in glyphs} |
| 915 | if not table.GlyphClassDef.classDefs: |
| 916 | table.GlyphClassDef = None |
| 917 | if table.AttachList: |
| 918 | indices = table.AttachList.Coverage.subset (glyphs) |
| 919 | table.AttachList.AttachPoint = [table.AttachList.AttachPoint[i] |
| 920 | for i in indices] |
| 921 | table.AttachList.GlyphCount = len (table.AttachList.AttachPoint) |
| 922 | if not table.AttachList.GlyphCount: |
| 923 | table.AttachList = None |
| 924 | return bool (table.LigCaretList or |
| 925 | table.MarkAttachClassDef or |
| 926 | table.GlyphClassDef or |
| 927 | table.AttachList) |
Behdad Esfahbod | efb984a | 2013-07-21 22:26:16 -0400 | [diff] [blame] | 928 | |
Behdad Esfahbod | fd3923e | 2013-07-22 12:48:17 -0400 | [diff] [blame] | 929 | @add_method(fontTools.ttLib.getTableClass('kern')) |
Behdad Esfahbod | d4e33a7 | 2013-07-24 18:51:05 -0400 | [diff] [blame] | 930 | def prune_pre_subset (self, options): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 931 | # Prune unknown kern table types |
| 932 | self.kernTables = [t for t in self.kernTables if hasattr (t, 'kernTable')] |
| 933 | return bool (self.kernTables) |
Behdad Esfahbod | d4e33a7 | 2013-07-24 18:51:05 -0400 | [diff] [blame] | 934 | |
| 935 | @add_method(fontTools.ttLib.getTableClass('kern')) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 936 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 937 | glyphs = s.glyphs_gsubed |
| 938 | for t in self.kernTables: |
| 939 | t.kernTable = {(a,b):v for ((a,b),v) in t.kernTable.iteritems() |
| 940 | if a in glyphs and b in glyphs} |
| 941 | self.kernTables = [t for t in self.kernTables if t.kernTable] |
| 942 | return bool (self.kernTables) |
Behdad Esfahbod | efb984a | 2013-07-21 22:26:16 -0400 | [diff] [blame] | 943 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 944 | @add_method(fontTools.ttLib.getTableClass('hmtx'), |
| 945 | fontTools.ttLib.getTableClass('vmtx')) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 946 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 947 | self.metrics = {g:v for g,v in self.metrics.iteritems() if g in s.glyphs} |
| 948 | return bool (self.metrics) |
Behdad Esfahbod | c716044 | 2013-07-22 14:29:08 -0400 | [diff] [blame] | 949 | |
Behdad Esfahbod | 75e14fc | 2013-07-22 14:49:54 -0400 | [diff] [blame] | 950 | @add_method(fontTools.ttLib.getTableClass('hdmx')) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 951 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 952 | self.hdmx = {sz:{g:v for g,v in l.iteritems() if g in s.glyphs} |
| 953 | for (sz,l) in self.hdmx.iteritems()} |
| 954 | return bool (self.hdmx) |
Behdad Esfahbod | 75e14fc | 2013-07-22 14:49:54 -0400 | [diff] [blame] | 955 | |
Behdad Esfahbod | e45d6af | 2013-07-22 15:29:17 -0400 | [diff] [blame] | 956 | @add_method(fontTools.ttLib.getTableClass('VORG')) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 957 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 958 | self.VOriginRecords = {g:v for g,v in self.VOriginRecords.iteritems() |
| 959 | if g in s.glyphs} |
| 960 | self.numVertOriginYMetrics = len (self.VOriginRecords) |
| 961 | return True # Never drop; has default metrics |
Behdad Esfahbod | e45d6af | 2013-07-22 15:29:17 -0400 | [diff] [blame] | 962 | |
Behdad Esfahbod | 8c646f6 | 2013-07-22 15:06:23 -0400 | [diff] [blame] | 963 | @add_method(fontTools.ttLib.getTableClass('post')) |
Behdad Esfahbod | 8c486d8 | 2013-07-24 13:34:47 -0400 | [diff] [blame] | 964 | def prune_pre_subset (self, options): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 965 | if not options.glyph_names: |
| 966 | self.formatType = 3.0 |
| 967 | return True |
Behdad Esfahbod | 4264824 | 2013-07-23 12:56:06 -0400 | [diff] [blame] | 968 | |
| 969 | @add_method(fontTools.ttLib.getTableClass('post')) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 970 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 971 | self.extraNames = [] # This seems to do it |
| 972 | return True |
Behdad Esfahbod | 653e974 | 2013-07-22 15:17:12 -0400 | [diff] [blame] | 973 | |
Behdad Esfahbod | 4cf7a80 | 2013-07-24 16:08:35 -0400 | [diff] [blame] | 974 | # Copied from _g_l_y_f.py |
| 975 | ARG_1_AND_2_ARE_WORDS = 0x0001 # if set args are words otherwise they are bytes |
| 976 | ARGS_ARE_XY_VALUES = 0x0002 # if set args are xy values, otherwise they are points |
| 977 | ROUND_XY_TO_GRID = 0x0004 # for the xy values if above is true |
| 978 | WE_HAVE_A_SCALE = 0x0008 # Sx = Sy, otherwise scale == 1.0 |
| 979 | NON_OVERLAPPING = 0x0010 # set to same value for all components (obsolete!) |
| 980 | MORE_COMPONENTS = 0x0020 # indicates at least one more glyph after this one |
| 981 | WE_HAVE_AN_X_AND_Y_SCALE = 0x0040 # Sx, Sy |
| 982 | WE_HAVE_A_TWO_BY_TWO = 0x0080 # t00, t01, t10, t11 |
| 983 | WE_HAVE_INSTRUCTIONS = 0x0100 # instructions follow |
| 984 | USE_MY_METRICS = 0x0200 # apply these metrics to parent glyph |
| 985 | OVERLAP_COMPOUND = 0x0400 # used by Apple in GX fonts |
| 986 | SCALED_COMPONENT_OFFSET = 0x0800 # composite designed to have the component offset scaled (designed for Apple) |
| 987 | UNSCALED_COMPONENT_OFFSET = 0x1000 # composite designed not to have the component offset scaled (designed for MS) |
| 988 | |
| 989 | @add_method(fontTools.ttLib.getTableModule('glyf').Glyph) |
| 990 | def getComponentNamesFast (self, glyfTable): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 991 | if struct.unpack(">h", self.data[:2])[0] >= 0: |
| 992 | return [] # Not composite |
| 993 | data = self.data |
| 994 | i = 10 |
| 995 | components = [] |
| 996 | more = 1 |
| 997 | while more: |
| 998 | flags, glyphID = struct.unpack(">HH", data[i:i+4]) |
| 999 | i += 4 |
| 1000 | flags = int(flags) |
| 1001 | components.append (glyfTable.getGlyphName (int (glyphID))) |
Behdad Esfahbod | 4cf7a80 | 2013-07-24 16:08:35 -0400 | [diff] [blame] | 1002 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1003 | if flags & ARG_1_AND_2_ARE_WORDS: i += 4 |
| 1004 | else: i += 2 |
| 1005 | if flags & WE_HAVE_A_SCALE: i += 2 |
| 1006 | elif flags & WE_HAVE_AN_X_AND_Y_SCALE: i += 4 |
| 1007 | elif flags & WE_HAVE_A_TWO_BY_TWO: i += 8 |
| 1008 | more = flags & MORE_COMPONENTS |
| 1009 | return components |
Behdad Esfahbod | 4cf7a80 | 2013-07-24 16:08:35 -0400 | [diff] [blame] | 1010 | |
| 1011 | @add_method(fontTools.ttLib.getTableModule('glyf').Glyph) |
| 1012 | def remapComponentsFast (self, indices): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1013 | if struct.unpack(">h", self.data[:2])[0] >= 0: |
| 1014 | return # Not composite |
| 1015 | data = bytearray (self.data) |
| 1016 | i = 10 |
| 1017 | more = 1 |
| 1018 | while more: |
| 1019 | flags = (data[i] << 8) | data[i+1] |
| 1020 | glyphID = (data[i+2] << 8) | data[i+3] |
| 1021 | # Remap |
| 1022 | glyphID = indices.index (glyphID) |
| 1023 | data[i+2] = glyphID >> 8 |
| 1024 | data[i+3] = glyphID & 0xFF |
| 1025 | i += 4 |
| 1026 | flags = int(flags) |
Behdad Esfahbod | 4cf7a80 | 2013-07-24 16:08:35 -0400 | [diff] [blame] | 1027 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1028 | if flags & ARG_1_AND_2_ARE_WORDS: i += 4 |
| 1029 | else: i += 2 |
| 1030 | if flags & WE_HAVE_A_SCALE: i += 2 |
| 1031 | elif flags & WE_HAVE_AN_X_AND_Y_SCALE: i += 4 |
| 1032 | elif flags & WE_HAVE_A_TWO_BY_TWO: i += 8 |
| 1033 | more = flags & MORE_COMPONENTS |
| 1034 | self.data = str (data) |
Behdad Esfahbod | 4cf7a80 | 2013-07-24 16:08:35 -0400 | [diff] [blame] | 1035 | |
Behdad Esfahbod | 6ec8854 | 2013-07-24 16:52:47 -0400 | [diff] [blame] | 1036 | @add_method(fontTools.ttLib.getTableModule('glyf').Glyph) |
| 1037 | def dropInstructionsFast (self): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1038 | numContours = struct.unpack(">h", self.data[:2])[0] |
| 1039 | data = bytearray (self.data) |
| 1040 | i = 10 |
| 1041 | if numContours >= 0: |
| 1042 | i += 2 * numContours # endPtsOfContours |
| 1043 | instructionLen = (data[i] << 8) | data[i+1] |
| 1044 | # Zero it |
| 1045 | data[i] = data [i+1] = 0 |
| 1046 | i += 2 |
| 1047 | if instructionLen: |
| 1048 | # Splice it out |
| 1049 | data = data[:i] + data[i+instructionLen:] |
| 1050 | else: |
| 1051 | more = 1 |
| 1052 | while more: |
| 1053 | flags = (data[i] << 8) | data[i+1] |
| 1054 | # Turn instruction flag off |
| 1055 | flags &= ~WE_HAVE_INSTRUCTIONS |
| 1056 | data[i+0] = flags >> 8 |
| 1057 | data[i+1] = flags & 0xFF |
| 1058 | i += 4 |
| 1059 | flags = int(flags) |
Behdad Esfahbod | 6ec8854 | 2013-07-24 16:52:47 -0400 | [diff] [blame] | 1060 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1061 | if flags & ARG_1_AND_2_ARE_WORDS: i += 4 |
| 1062 | else: i += 2 |
| 1063 | if flags & WE_HAVE_A_SCALE: i += 2 |
| 1064 | elif flags & WE_HAVE_AN_X_AND_Y_SCALE: i += 4 |
| 1065 | elif flags & WE_HAVE_A_TWO_BY_TWO: i += 8 |
| 1066 | more = flags & MORE_COMPONENTS |
| 1067 | # Cut off |
| 1068 | data = data[:i] |
| 1069 | if len(data) % 4: |
| 1070 | # add pad bytes |
| 1071 | nPadBytes = 4 - (len(data) % 4) |
| 1072 | for i in range (nPadBytes): |
| 1073 | data.append (0) |
| 1074 | self.data = str (data) |
Behdad Esfahbod | 6ec8854 | 2013-07-24 16:52:47 -0400 | [diff] [blame] | 1075 | |
Behdad Esfahbod | 861d915 | 2013-07-22 16:47:24 -0400 | [diff] [blame] | 1076 | @add_method(fontTools.ttLib.getTableClass('glyf')) |
Behdad Esfahbod | 254442b | 2013-07-31 14:20:13 -0400 | [diff] [blame] | 1077 | def closure_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1078 | decompose = s.glyphs |
| 1079 | # I don't know if component glyphs can be composite themselves. |
| 1080 | # We handle them anyway. |
| 1081 | while True: |
| 1082 | components = set () |
| 1083 | for g in decompose: |
| 1084 | if g not in self.glyphs: |
| 1085 | continue |
| 1086 | gl = self.glyphs[g] |
| 1087 | if hasattr (gl, "data"): |
| 1088 | for c in gl.getComponentNamesFast (self): |
| 1089 | if c not in s.glyphs: |
| 1090 | components.add (c) |
| 1091 | else: |
| 1092 | # TTX seems to expand gid0..3 always |
| 1093 | if gl.isComposite (): |
| 1094 | for c in gl.components: |
| 1095 | if c.glyphName not in s.glyphs: |
| 1096 | components.add (c.glyphName) |
| 1097 | components = set (c for c in components if c not in s.glyphs) |
| 1098 | if not components: |
| 1099 | break |
| 1100 | decompose = components |
| 1101 | s.glyphs.update (components) |
Behdad Esfahbod | abb50a1 | 2013-07-23 12:58:37 -0400 | [diff] [blame] | 1102 | |
| 1103 | @add_method(fontTools.ttLib.getTableClass('glyf')) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 1104 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1105 | self.glyphs = {g:v for g,v in self.glyphs.iteritems() if g in s.glyphs} |
| 1106 | indices = [i for i,g in enumerate (self.glyphOrder) if g in s.glyphs] |
| 1107 | for v in self.glyphs.itervalues(): |
| 1108 | if hasattr (v, "data"): |
| 1109 | v.remapComponentsFast (indices) |
| 1110 | else: |
| 1111 | pass # No need |
| 1112 | self.glyphOrder = [g for g in self.glyphOrder if g in s.glyphs] |
| 1113 | return bool (self.glyphs) |
Behdad Esfahbod | 861d915 | 2013-07-22 16:47:24 -0400 | [diff] [blame] | 1114 | |
Behdad Esfahbod | ed98c61 | 2013-07-23 12:37:41 -0400 | [diff] [blame] | 1115 | @add_method(fontTools.ttLib.getTableClass('glyf')) |
Behdad Esfahbod | d7b6f8f | 2013-07-23 12:46:52 -0400 | [diff] [blame] | 1116 | def prune_post_subset (self, options): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1117 | if not options.hinting: |
| 1118 | for v in self.glyphs.itervalues(): |
| 1119 | if hasattr (v, "data"): |
| 1120 | v.dropInstructionsFast () |
| 1121 | else: |
| 1122 | v.program = fontTools.ttLib.tables.ttProgram.Program() |
| 1123 | v.program.fromBytecode([]) |
| 1124 | return True |
Behdad Esfahbod | ed98c61 | 2013-07-23 12:37:41 -0400 | [diff] [blame] | 1125 | |
Behdad Esfahbod | 2b677c8 | 2013-07-23 13:37:13 -0400 | [diff] [blame] | 1126 | @add_method(fontTools.ttLib.getTableClass('CFF ')) |
Behdad Esfahbod | 1a4e72e | 2013-08-13 15:46:37 -0400 | [diff] [blame] | 1127 | def prune_pre_subset (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1128 | cff = self.cff |
| 1129 | # CFF table should have one font only |
| 1130 | cff.fontNames = cff.fontNames[:1] |
| 1131 | return bool (cff.fontNames) |
Behdad Esfahbod | 1a4e72e | 2013-08-13 15:46:37 -0400 | [diff] [blame] | 1132 | |
| 1133 | @add_method(fontTools.ttLib.getTableClass('CFF ')) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 1134 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1135 | cff = self.cff |
| 1136 | for fontname in cff.keys(): |
| 1137 | font = cff[fontname] |
| 1138 | cs = font.CharStrings |
| 1139 | if cs.charStringsAreIndexed: |
| 1140 | indices = [i for i,g in enumerate (font.charset) if g in s.glyphs] |
| 1141 | # Load all glyphs |
| 1142 | for g in font.charset: |
| 1143 | if g not in s.glyphs: continue |
| 1144 | cs.getItemAndSelector (g) |
| 1145 | csi = cs.charStringsIndex |
| 1146 | csi.items = [csi.items[i] for i in indices] |
| 1147 | csi.offsets = [] # Don't need it; loaded all glyphs |
| 1148 | if hasattr (font, "FDSelect"): |
| 1149 | sel = font.FDSelect |
| 1150 | sel.format = None |
| 1151 | sel.gidArray = [font.FDSelect.gidArray[i] for i in indices] |
| 1152 | cs.charStrings = {g:indices.index (v) |
| 1153 | for g,v in cs.charStrings.iteritems() |
| 1154 | if g in s.glyphs} |
| 1155 | else: |
| 1156 | cs.charStrings = {g:v |
| 1157 | for g,v in cs.charStrings.iteritems() |
| 1158 | if g in s.glyphs} |
| 1159 | font.charset = [g for g in font.charset if g in s.glyphs] |
| 1160 | font.numGlyphs = len (font.charset) |
| 1161 | return any (cff[fontname].numGlyphs for fontname in cff.keys()) |
Behdad Esfahbod | 1a4e72e | 2013-08-13 15:46:37 -0400 | [diff] [blame] | 1162 | |
| 1163 | @add_method(fontTools.ttLib.getTableClass('glyf')) |
| 1164 | def prune_post_subset (self, options): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1165 | if not options.hinting: |
| 1166 | pass # Drop hints |
| 1167 | return True |
Behdad Esfahbod | 2b677c8 | 2013-07-23 13:37:13 -0400 | [diff] [blame] | 1168 | |
Behdad Esfahbod | 653e974 | 2013-07-22 15:17:12 -0400 | [diff] [blame] | 1169 | @add_method(fontTools.ttLib.getTableClass('cmap')) |
Behdad Esfahbod | 8c8ff45 | 2013-07-31 19:47:37 -0400 | [diff] [blame] | 1170 | def closure_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1171 | tables = [t for t in self.tables |
| 1172 | if t.platformID == 3 and t.platEncID in [1, 10]] |
| 1173 | for u in s.unicodes_requested: |
| 1174 | found = False |
| 1175 | for table in tables: |
| 1176 | if u in table.cmap: |
| 1177 | s.glyphs.add (table.cmap[u]) |
| 1178 | found = True |
| 1179 | break |
| 1180 | if not found: |
| 1181 | s.log ("No glyph for Unicode value %s; skipping." % u) |
Behdad Esfahbod | 8c8ff45 | 2013-07-31 19:47:37 -0400 | [diff] [blame] | 1182 | |
| 1183 | @add_method(fontTools.ttLib.getTableClass('cmap')) |
Behdad Esfahbod | abb50a1 | 2013-07-23 12:58:37 -0400 | [diff] [blame] | 1184 | def prune_pre_subset (self, options): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1185 | if not options.legacy_cmap: |
| 1186 | # Drop non-Unicode / non-Symbol cmaps |
| 1187 | self.tables = [t for t in self.tables |
| 1188 | if t.platformID == 3 and t.platEncID in [0, 1, 10]] |
| 1189 | if not options.symbol_cmap: |
| 1190 | self.tables = [t for t in self.tables |
| 1191 | if t.platformID == 3 and t.platEncID in [1, 10]] |
| 1192 | # TODO Only keep one subtable? |
| 1193 | # For now, drop format=0 which can't be subset_glyphs easily? |
| 1194 | self.tables = [t for t in self.tables if t.format != 0] |
| 1195 | return bool (self.tables) |
Behdad Esfahbod | abb50a1 | 2013-07-23 12:58:37 -0400 | [diff] [blame] | 1196 | |
| 1197 | @add_method(fontTools.ttLib.getTableClass('cmap')) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 1198 | def subset_glyphs (self, s): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1199 | s.glyphs = s.glyphs_cmaped |
| 1200 | for t in self.tables: |
| 1201 | # For reasons I don't understand I need this here |
| 1202 | # to force decompilation of the cmap format 14. |
| 1203 | try: |
| 1204 | getattr (t, "asdf") |
| 1205 | except AttributeError: |
| 1206 | pass |
| 1207 | if t.format == 14: |
| 1208 | # XXX We drop all the default-UVS mappings (g==None) |
| 1209 | t.uvsDict = {v:[(u,g) for (u,g) in l if g in s.glyphs] |
| 1210 | for (v,l) in t.uvsDict.iteritems()} |
| 1211 | t.uvsDict = {v:l for (v,l) in t.uvsDict.iteritems() if l} |
| 1212 | else: |
| 1213 | t.cmap = {u:g for (u,g) in t.cmap.iteritems() |
| 1214 | if g in s.glyphs_requested or u in s.unicodes_requested} |
| 1215 | self.tables = [t for t in self.tables |
| 1216 | if (t.cmap if t.format != 14 else t.uvsDict)] |
| 1217 | # XXX Convert formats when needed |
| 1218 | # In particular, if we have a format=12 without non-BMP |
| 1219 | # characters, either drop format=12 one or convert it |
| 1220 | # to format=4 if there's not one. |
| 1221 | return bool (self.tables) |
Behdad Esfahbod | 61addb4 | 2013-07-23 11:03:49 -0400 | [diff] [blame] | 1222 | |
Behdad Esfahbod | 61addb4 | 2013-07-23 11:03:49 -0400 | [diff] [blame] | 1223 | @add_method(fontTools.ttLib.getTableClass('name')) |
Behdad Esfahbod | d7b6f8f | 2013-07-23 12:46:52 -0400 | [diff] [blame] | 1224 | def prune_pre_subset (self, options): |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1225 | if '*' not in options.name_IDs: |
| 1226 | self.names = [n for n in self.names if n.nameID in options.name_IDs] |
| 1227 | if not options.name_legacy: |
| 1228 | self.names = [n for n in self.names |
| 1229 | if n.platformID == 3 and n.platEncID == 1] |
| 1230 | if '*' not in options.name_languages: |
| 1231 | self.names = [n for n in self.names if n.langID in options.name_languages] |
| 1232 | return True # Retain even if empty |
Behdad Esfahbod | 653e974 | 2013-07-22 15:17:12 -0400 | [diff] [blame] | 1233 | |
Behdad Esfahbod | 8c646f6 | 2013-07-22 15:06:23 -0400 | [diff] [blame] | 1234 | |
Behdad Esfahbod | 75e14fc | 2013-07-22 14:49:54 -0400 | [diff] [blame] | 1235 | # TODO OS/2 ulUnicodeRange / ulCodePageRange? |
Behdad Esfahbod | f71267b | 2013-07-23 12:59:13 -0400 | [diff] [blame] | 1236 | # TODO Drop unneeded GSUB/GPOS Script/LangSys entries |
Behdad Esfahbod | 398d389 | 2013-07-23 15:29:40 -0400 | [diff] [blame] | 1237 | # TODO Avoid recursing too much |
Behdad Esfahbod | e94aa0e | 2013-07-23 13:22:04 -0400 | [diff] [blame] | 1238 | # TODO Text direction considerations |
| 1239 | # TODO Text script / language considerations |
Behdad Esfahbod | b3ee60c | 2013-07-24 19:21:40 -0400 | [diff] [blame] | 1240 | # TODO Drop unknown tables? Using DefaultTable.prune? |
Behdad Esfahbod | 8c4f7cc | 2013-07-24 17:58:29 -0400 | [diff] [blame] | 1241 | # TODO Drop GPOS Device records if not hinting? |
Behdad Esfahbod | 93e2636 | 2013-08-09 14:22:48 -0400 | [diff] [blame] | 1242 | # TODO Move font name loading hack to Subsetter? |
Behdad Esfahbod | 56ebd04 | 2013-07-22 13:02:24 -0400 | [diff] [blame] | 1243 | |
Behdad Esfahbod | 8c486d8 | 2013-07-24 13:34:47 -0400 | [diff] [blame] | 1244 | |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 1245 | class Subsetter: |
Behdad Esfahbod | 97e17b8 | 2013-07-31 15:59:21 -0400 | [diff] [blame] | 1246 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1247 | class Options: |
Behdad Esfahbod | 26d9ee7 | 2013-08-13 16:55:01 -0400 | [diff] [blame] | 1248 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1249 | class UnknownOptionError (Exception): |
| 1250 | pass |
Behdad Esfahbod | 26d9ee7 | 2013-08-13 16:55:01 -0400 | [diff] [blame] | 1251 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1252 | drop_tables_default = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', |
| 1253 | 'PCLT', 'LTSH'] |
| 1254 | drop_tables_default += ['Feat', 'Glat', 'Gloc', 'Silf', 'Sill'] # Graphite |
| 1255 | drop_tables_default += ['CBLC', 'CBDT', 'sbix', 'COLR', 'CPAL'] # Color |
| 1256 | no_subset_tables_default = ['gasp', 'head', 'hhea', 'maxp', 'vhea', 'OS/2', |
| 1257 | 'loca', 'name', 'cvt ', 'fpgm', 'prep'] |
| 1258 | hinting_tables_default = ['cvt ', 'fpgm', 'prep', 'hdmx', 'VDMX'] |
Behdad Esfahbod | 9eeeb4e | 2013-08-13 16:58:50 -0400 | [diff] [blame] | 1259 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1260 | # Based on HarfBuzz shapers |
| 1261 | layout_features_groups = { |
| 1262 | # Default shaper |
| 1263 | 'common': ['ccmp', 'liga', 'locl', 'mark', 'mkmk', 'rlig'], |
| 1264 | 'horizontal': ['calt', 'clig', 'curs', 'kern', 'rclt'], |
| 1265 | 'vertical': ['valt', 'vert', 'vkrn', 'vpal', 'vrt2'], |
| 1266 | 'ltr': ['ltra', 'ltrm'], |
| 1267 | 'rtl': ['rtla', 'rtlm'], |
| 1268 | # Complex shapers |
| 1269 | 'arabic': ['init', 'medi', 'fina', 'isol', 'med2', 'fin2', 'fin3', |
| 1270 | 'cswh', 'mset'], |
| 1271 | 'hangul': ['ljmo', 'vjmo', 'tjmo'], |
| 1272 | 'tibetal': ['abvs', 'blws', 'abvm', 'blwm'], |
| 1273 | 'indic': ['nukt', 'akhn', 'rphf', 'rkrf', 'pref', 'blwf', 'half', |
| 1274 | 'abvf', 'pstf', 'cfar', 'vatu', 'cjct', 'init', 'pres', |
| 1275 | 'abvs', 'blws', 'psts', 'haln', 'dist', 'abvm', 'blwm'], |
| 1276 | } |
| 1277 | layout_features_default = unique_sorted (sum ( |
| 1278 | layout_features_groups.itervalues(), [])) |
Behdad Esfahbod | 9eeeb4e | 2013-08-13 16:58:50 -0400 | [diff] [blame] | 1279 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1280 | drop_tables = drop_tables_default |
| 1281 | no_subset_tables = no_subset_tables_default |
| 1282 | hinting_tables = hinting_tables_default |
| 1283 | layout_features = layout_features_default |
| 1284 | hinting = False |
| 1285 | glyph_names = False |
| 1286 | legacy_cmap = False |
| 1287 | symbol_cmap = False |
| 1288 | name_IDs = [1, 2] # Family and Style |
| 1289 | name_legacy = False |
| 1290 | name_languages = [0x0409] # English |
| 1291 | mandatory_glyphs = True # First four for TrueType, .notdef for CFF |
| 1292 | recalc_bboxes = False # Slows us down |
Behdad Esfahbod | 97e17b8 | 2013-07-31 15:59:21 -0400 | [diff] [blame] | 1293 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1294 | def __init__ (self, **kwargs): |
Behdad Esfahbod | 97e17b8 | 2013-07-31 15:59:21 -0400 | [diff] [blame] | 1295 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1296 | self.set (**kwargs) |
Behdad Esfahbod | 97e17b8 | 2013-07-31 15:59:21 -0400 | [diff] [blame] | 1297 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1298 | def set (self, **kwargs): |
| 1299 | for k,v in kwargs.iteritems(): |
| 1300 | if not hasattr (self, k): |
| 1301 | raise self.UnknownOptionError ("Unknown option '%s'" % k) |
| 1302 | setattr (self, k, v) |
Behdad Esfahbod | 97e17b8 | 2013-07-31 15:59:21 -0400 | [diff] [blame] | 1303 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1304 | def parse_opts (self, argv, ignore_unknown=False): |
| 1305 | ret = [] |
| 1306 | opts = {} |
| 1307 | for a in argv: |
| 1308 | orig_a = a |
| 1309 | if not a.startswith ('--'): |
| 1310 | ret.append (a) |
| 1311 | continue |
| 1312 | a = a[2:] |
| 1313 | i = a.find ('=') |
| 1314 | if i == -1: |
| 1315 | if a.startswith ("no-"): |
| 1316 | k = a[3:] |
| 1317 | v = False |
| 1318 | else: |
| 1319 | k = a |
| 1320 | v = True |
| 1321 | else: |
| 1322 | k = a[:i] |
| 1323 | v = a[i+1:] |
| 1324 | k = k.replace ('-', '_') |
| 1325 | if not hasattr (self, k): |
| 1326 | if ignore_unknown == True or k in ignore_unknown: |
| 1327 | ret.append (orig_a) |
| 1328 | continue |
| 1329 | else: |
| 1330 | raise self.UnknownOptionError ("Unknown option '%s'" % a) |
Behdad Esfahbod | 97e17b8 | 2013-07-31 15:59:21 -0400 | [diff] [blame] | 1331 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1332 | ov = getattr (self, k) |
| 1333 | if isinstance (ov, bool): |
| 1334 | v = bool (v) |
| 1335 | elif isinstance (ov, int): |
| 1336 | v = int (v) |
| 1337 | elif isinstance (ov, list): |
| 1338 | v = v.split (',') |
| 1339 | v = [int (x, 0) if x[0] in range (10) else x for x in v] |
Behdad Esfahbod | 97e17b8 | 2013-07-31 15:59:21 -0400 | [diff] [blame] | 1340 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1341 | opts[k] = v |
| 1342 | self.set (**opts) |
Behdad Esfahbod | 97e17b8 | 2013-07-31 15:59:21 -0400 | [diff] [blame] | 1343 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1344 | return ret |
Behdad Esfahbod | 97e17b8 | 2013-07-31 15:59:21 -0400 | [diff] [blame] | 1345 | |
| 1346 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1347 | def __init__ (self, options=None, log=None): |
Behdad Esfahbod | 97e17b8 | 2013-07-31 15:59:21 -0400 | [diff] [blame] | 1348 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1349 | if not log: |
| 1350 | log = Logger() |
| 1351 | if not options: |
| 1352 | options = Options() |
Behdad Esfahbod | 97e17b8 | 2013-07-31 15:59:21 -0400 | [diff] [blame] | 1353 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1354 | self.options = options |
| 1355 | self.log = log |
| 1356 | self.unicodes_requested = set () |
| 1357 | self.glyphs_requested = set () |
| 1358 | self.glyphs = set () |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 1359 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1360 | def populate (self, glyphs=[], unicodes=[], text=""): |
| 1361 | self.unicodes_requested.update (unicodes) |
| 1362 | if isinstance (text, str): |
| 1363 | text = text.decode ("utf8") |
| 1364 | for u in text: |
| 1365 | self.unicodes_requested.add (ord (u)) |
| 1366 | self.glyphs_requested.update (glyphs) |
| 1367 | self.glyphs.update (glyphs) |
Behdad Esfahbod | 3d513b7 | 2013-07-31 14:11:40 -0400 | [diff] [blame] | 1368 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1369 | def pre_prune (self, font): |
Behdad Esfahbod | 063a2db | 2013-07-31 15:22:02 -0400 | [diff] [blame] | 1370 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1371 | for tag in font.keys(): |
| 1372 | if tag == 'GlyphOrder': continue |
Behdad Esfahbod | 063a2db | 2013-07-31 15:22:02 -0400 | [diff] [blame] | 1373 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1374 | if (tag in self.options.drop_tables or |
| 1375 | (tag in self.options.hinting_tables and not self.options.hinting)): |
| 1376 | self.log (tag, "dropped") |
| 1377 | del font[tag] |
| 1378 | continue |
Behdad Esfahbod | 063a2db | 2013-07-31 15:22:02 -0400 | [diff] [blame] | 1379 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1380 | clazz = fontTools.ttLib.getTableClass(tag) |
Behdad Esfahbod | 063a2db | 2013-07-31 15:22:02 -0400 | [diff] [blame] | 1381 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1382 | if hasattr (clazz, 'prune_pre_subset'): |
| 1383 | table = font[tag] |
| 1384 | retain = table.prune_pre_subset (self.options) |
| 1385 | self.log.lapse ("prune '%s'" % tag) |
| 1386 | if not retain: |
| 1387 | self.log (tag, "pruned to empty; dropped") |
| 1388 | del font[tag] |
| 1389 | continue |
| 1390 | else: |
| 1391 | self.log (tag, "pruned") |
Behdad Esfahbod | 063a2db | 2013-07-31 15:22:02 -0400 | [diff] [blame] | 1392 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1393 | def closure_glyphs (self, font): |
Behdad Esfahbod | 2fb90e2 | 2013-07-31 20:04:08 -0400 | [diff] [blame] | 1394 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1395 | self.glyphs = self.glyphs_requested.copy () |
Behdad Esfahbod | 98259f2 | 2013-07-31 20:16:24 -0400 | [diff] [blame] | 1396 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1397 | if 'cmap' in font: |
| 1398 | font['cmap'].closure_glyphs (self) |
| 1399 | self.glyphs_cmaped = self.glyphs |
Behdad Esfahbod | 2fb90e2 | 2013-07-31 20:04:08 -0400 | [diff] [blame] | 1400 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1401 | if self.options.mandatory_glyphs: |
| 1402 | if 'glyf' in font: |
| 1403 | for i in range (4): |
| 1404 | self.glyphs.add (font.getGlyphName (i)) |
| 1405 | self.log ("Added first four glyphs to subset") |
| 1406 | else: |
| 1407 | self.glyphs.add ('.notdef') |
| 1408 | self.log ("Added .notdef glyph to subset") |
Behdad Esfahbod | 2fb90e2 | 2013-07-31 20:04:08 -0400 | [diff] [blame] | 1409 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1410 | if 'GSUB' in font: |
| 1411 | self.log ("Closing glyph list over 'GSUB': %d glyphs before" % |
| 1412 | len (self.glyphs)) |
| 1413 | self.log.glyphs (self.glyphs, font=font) |
| 1414 | font['GSUB'].closure_glyphs (self) |
| 1415 | self.log ("Closed glyph list over 'GSUB': %d glyphs after" % |
| 1416 | len (self.glyphs)) |
| 1417 | self.log.glyphs (self.glyphs, font=font) |
| 1418 | self.log.lapse ("close glyph list over 'GSUB'") |
| 1419 | self.glyphs_gsubed = self.glyphs.copy () |
Behdad Esfahbod | 063a2db | 2013-07-31 15:22:02 -0400 | [diff] [blame] | 1420 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1421 | if 'glyf' in font: |
| 1422 | self.log ("Closing glyph list over 'glyf': %d glyphs before" % |
| 1423 | len (self.glyphs)) |
| 1424 | self.log.glyphs (self.glyphs, font=font) |
| 1425 | font['glyf'].closure_glyphs (self) |
| 1426 | self.log ("Closed glyph list over 'glyf': %d glyphs after" % |
| 1427 | len (self.glyphs)) |
| 1428 | self.log.glyphs (self.glyphs, font=font) |
| 1429 | self.log.lapse ("close glyph list over 'glyf'") |
| 1430 | self.glyphs_glyfed = self.glyphs.copy () |
Behdad Esfahbod | 063a2db | 2013-07-31 15:22:02 -0400 | [diff] [blame] | 1431 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1432 | self.glyphs_all = self.glyphs.copy () |
Behdad Esfahbod | 8c8ff45 | 2013-07-31 19:47:37 -0400 | [diff] [blame] | 1433 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1434 | self.log ("Retaining %d glyphs: " % len (self.glyphs_all)) |
Behdad Esfahbod | 063a2db | 2013-07-31 15:22:02 -0400 | [diff] [blame] | 1435 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1436 | def subset_glyphs (self, font): |
| 1437 | for tag in font.keys(): |
| 1438 | if tag == 'GlyphOrder': continue |
| 1439 | clazz = fontTools.ttLib.getTableClass(tag) |
Behdad Esfahbod | 063a2db | 2013-07-31 15:22:02 -0400 | [diff] [blame] | 1440 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1441 | if tag in self.options.no_subset_tables: |
| 1442 | self.log (tag, "subsetting not needed") |
| 1443 | elif hasattr (clazz, 'subset_glyphs'): |
| 1444 | table = font[tag] |
| 1445 | self.glyphs = self.glyphs_all |
| 1446 | retain = table.subset_glyphs (self) |
| 1447 | self.glyphs = self.glyphs_all |
| 1448 | self.log.lapse ("subset '%s'" % tag) |
| 1449 | if not retain: |
| 1450 | self.log (tag, "subsetted to empty; dropped") |
| 1451 | del font[tag] |
| 1452 | else: |
| 1453 | self.log (tag, "subsetted") |
| 1454 | else: |
| 1455 | self.log (tag, "NOT subset; don't know how to subset; dropped") |
| 1456 | del font[tag] |
Behdad Esfahbod | 063a2db | 2013-07-31 15:22:02 -0400 | [diff] [blame] | 1457 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1458 | glyphOrder = font.getGlyphOrder() |
| 1459 | glyphOrder = [g for g in glyphOrder if g in self.glyphs_all] |
| 1460 | font.setGlyphOrder (glyphOrder) |
| 1461 | font._buildReverseGlyphOrderDict () |
| 1462 | self.log.lapse ("subset GlyphOrder") |
Behdad Esfahbod | 2fb90e2 | 2013-07-31 20:04:08 -0400 | [diff] [blame] | 1463 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1464 | def post_prune (self, font): |
| 1465 | for tag in font.keys(): |
| 1466 | if tag == 'GlyphOrder': continue |
| 1467 | clazz = fontTools.ttLib.getTableClass(tag) |
| 1468 | if hasattr (clazz, 'prune_post_subset'): |
| 1469 | table = font[tag] |
| 1470 | retain = table.prune_post_subset (self.options) |
| 1471 | self.log.lapse ("prune '%s'" % tag) |
| 1472 | if not retain: |
| 1473 | self.log (tag, "pruned to empty; dropped") |
| 1474 | del font[tag] |
| 1475 | else: |
| 1476 | self.log (tag, "pruned") |
Behdad Esfahbod | 063a2db | 2013-07-31 15:22:02 -0400 | [diff] [blame] | 1477 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1478 | def subset (self, font): |
Behdad Esfahbod | 756af49 | 2013-08-01 12:05:26 -0400 | [diff] [blame] | 1479 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1480 | font.recalcBBoxes = self.options.recalc_bboxes |
Behdad Esfahbod | 98259f2 | 2013-07-31 20:16:24 -0400 | [diff] [blame] | 1481 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1482 | self.pre_prune (font) |
| 1483 | self.closure_glyphs (font) |
| 1484 | self.subset_glyphs (font) |
| 1485 | self.post_prune (font) |
Behdad Esfahbod | 98259f2 | 2013-07-31 20:16:24 -0400 | [diff] [blame] | 1486 | |
Behdad Esfahbod | 756af49 | 2013-08-01 12:05:26 -0400 | [diff] [blame] | 1487 | |
Behdad Esfahbod | df3d757 | 2013-07-31 15:03:43 -0400 | [diff] [blame] | 1488 | import sys, time |
Behdad Esfahbod | 063a2db | 2013-07-31 15:22:02 -0400 | [diff] [blame] | 1489 | |
Behdad Esfahbod | df3d757 | 2013-07-31 15:03:43 -0400 | [diff] [blame] | 1490 | class Logger: |
| 1491 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1492 | def __init__ (self, verbose=False, xml=False, timing=False): |
| 1493 | self.verbose = verbose |
| 1494 | self.xml = xml |
| 1495 | self.timing = timing |
| 1496 | self.last_time = self.start_time = time.time () |
Behdad Esfahbod | df3d757 | 2013-07-31 15:03:43 -0400 | [diff] [blame] | 1497 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1498 | def parse_opts (self, argv): |
| 1499 | argv = argv[:] |
| 1500 | for v in ['verbose', 'xml', 'timing']: |
| 1501 | if "--"+v in argv: |
| 1502 | setattr (self, v, True) |
| 1503 | argv.remove ("--"+v) |
| 1504 | return argv |
Behdad Esfahbod | df3d757 | 2013-07-31 15:03:43 -0400 | [diff] [blame] | 1505 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1506 | def __call__ (self, *things): |
| 1507 | if not self.verbose: |
| 1508 | return |
| 1509 | print ' '.join (str (x) for x in things) |
Behdad Esfahbod | df3d757 | 2013-07-31 15:03:43 -0400 | [diff] [blame] | 1510 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1511 | def lapse (self, *things): |
| 1512 | if not self.timing: |
| 1513 | return |
| 1514 | new_time = time.time () |
| 1515 | print "Took %0.3fs to %s" % (new_time - self.last_time, |
| 1516 | ' '.join (str (x) for x in things)) |
| 1517 | self.last_time = new_time |
Behdad Esfahbod | df3d757 | 2013-07-31 15:03:43 -0400 | [diff] [blame] | 1518 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1519 | def glyphs (self, glyphs, glyph_names=True, font=None): |
| 1520 | self ("Names: ", sorted (glyphs)) |
| 1521 | if font: |
| 1522 | reverseGlyphMap = font.getReverseGlyphMap () |
| 1523 | self ("Gids : ", sorted (reverseGlyphMap[g] for g in glyphs)) |
Behdad Esfahbod | f549784 | 2013-08-08 21:57:02 -0400 | [diff] [blame] | 1524 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1525 | def font (self, font, file=sys.stdout): |
| 1526 | if not self.xml: |
| 1527 | return |
| 1528 | import xmlWriter |
| 1529 | writer = xmlWriter.XMLWriter (file) |
| 1530 | font.disassembleInstructions = False # Work around ttx bug |
| 1531 | for tag in font.keys(): |
| 1532 | writer.begintag (tag) |
| 1533 | writer.newline () |
| 1534 | font[tag].toXML(writer, font) |
| 1535 | writer.endtag (tag) |
| 1536 | writer.newline () |
Behdad Esfahbod | df3d757 | 2013-07-31 15:03:43 -0400 | [diff] [blame] | 1537 | |
Behdad Esfahbod | f6b668e | 2013-08-13 12:20:59 -0400 | [diff] [blame] | 1538 | |
| 1539 | def load_font (fontfile, dont_load_glyph_names=False): |
| 1540 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1541 | # TODO Option for ignoreDecompileErrors? |
Behdad Esfahbod | f6b668e | 2013-08-13 12:20:59 -0400 | [diff] [blame] | 1542 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1543 | font = fontTools.ttx.TTFont (fontfile) |
Behdad Esfahbod | f6b668e | 2013-08-13 12:20:59 -0400 | [diff] [blame] | 1544 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1545 | # Hack: |
| 1546 | # |
| 1547 | # If we don't need glyph names, change 'post' class to not try to |
| 1548 | # load them. It avoid lots of headache with broken fonts as well |
| 1549 | # as loading time. |
| 1550 | # |
| 1551 | # Ideally ttLib should provide a way to ask it to skip loading |
| 1552 | # glyph names. But it currently doesn't provide such a thing. |
| 1553 | # |
| 1554 | if dont_load_glyph_names: |
| 1555 | post = fontTools.ttLib.getTableClass('post') |
| 1556 | saved = post.decode_format_2_0 |
| 1557 | post.decode_format_2_0 = post.decode_format_3_0 |
| 1558 | f = font['post'] |
| 1559 | if f.formatType == 2.0: |
| 1560 | f.formatType = 3.0 |
| 1561 | post.decode_format_2_0 = saved |
Behdad Esfahbod | f6b668e | 2013-08-13 12:20:59 -0400 | [diff] [blame] | 1562 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1563 | return font |
Behdad Esfahbod | f6b668e | 2013-08-13 12:20:59 -0400 | [diff] [blame] | 1564 | |
| 1565 | |
Behdad Esfahbod | 97e17b8 | 2013-07-31 15:59:21 -0400 | [diff] [blame] | 1566 | def main (args): |
Behdad Esfahbod | 610b055 | 2013-07-23 14:52:18 -0400 | [diff] [blame] | 1567 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1568 | log = Logger () |
| 1569 | args = log.parse_opts (args) |
Behdad Esfahbod | 4ae8171 | 2013-07-22 11:57:13 -0400 | [diff] [blame] | 1570 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1571 | options = Subsetter.Options () |
| 1572 | args = options.parse_opts (args, ignore_unknown=['text']) |
Behdad Esfahbod | 97e17b8 | 2013-07-31 15:59:21 -0400 | [diff] [blame] | 1573 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1574 | if len (args) < 2: |
| 1575 | print >>sys.stderr, "usage: pyotlss.py font-file glyph..." |
| 1576 | sys.exit (1) |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 1577 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1578 | fontfile = args[0] |
| 1579 | args = args[1:] |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 1580 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1581 | dont_load_glyph_names = (not options.glyph_names and |
| 1582 | all (any (g.startswith (p) |
| 1583 | for p in ['gid', 'glyph', 'uni', 'U+']) |
| 1584 | for g in args)) |
Behdad Esfahbod | f6b668e | 2013-08-13 12:20:59 -0400 | [diff] [blame] | 1585 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1586 | font = load_font (fontfile, dont_load_glyph_names=dont_load_glyph_names) |
| 1587 | subsetter = Subsetter (options=options, log=log) |
| 1588 | log.lapse ("load font") |
Behdad Esfahbod | 02b9206 | 2013-07-21 18:40:59 -0400 | [diff] [blame] | 1589 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1590 | names = font.getGlyphNames() |
| 1591 | log.lapse ("loading glyph names") |
Behdad Esfahbod | e7f5a89 | 2013-07-31 19:58:59 -0400 | [diff] [blame] | 1592 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1593 | glyphs = [] |
| 1594 | unicodes = [] |
| 1595 | text = "" |
| 1596 | for g in args: |
| 1597 | if g in names: |
| 1598 | glyphs.append (g) |
| 1599 | continue |
| 1600 | if g.startswith ('--text='): |
| 1601 | text += g[7:] |
| 1602 | continue |
| 1603 | if g.startswith ('uni') or g.startswith ('U+'): |
| 1604 | if g.startswith ('uni') and len (g) > 3: |
| 1605 | g = g[3:] |
| 1606 | elif g.startswith ('U+') and len (g) > 2: |
| 1607 | g = g[2:] |
| 1608 | u = int (g, 16) |
| 1609 | unicodes.append (u) |
| 1610 | continue |
| 1611 | if g.startswith ('gid') or g.startswith ('glyph'): |
| 1612 | if g.startswith ('gid') and len (g) > 3: |
| 1613 | g = g[3:] |
| 1614 | elif g.startswith ('glyph') and len (g) > 5: |
| 1615 | g = g[5:] |
| 1616 | try: |
| 1617 | glyphs.append (font.getGlyphName (int (g), requireReal=1)) |
| 1618 | except ValueError: |
| 1619 | raise Exception ("Invalid glyph identifier: %s" % g) |
| 1620 | continue |
| 1621 | raise Exception ("Invalid glyph identifier: %s" % g) |
| 1622 | log.lapse ("compile glyph list") |
| 1623 | log ("Unicodes:", unicodes) |
| 1624 | log ("Glyphs:", glyphs) |
Behdad Esfahbod | 6df089a | 2013-07-31 19:27:14 -0400 | [diff] [blame] | 1625 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1626 | subsetter.populate (glyphs=glyphs, unicodes=unicodes, text=text) |
| 1627 | subsetter.subset (font) |
Behdad Esfahbod | d1d41bc | 2013-07-21 23:15:32 -0400 | [diff] [blame] | 1628 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1629 | font.save (fontfile + '.subset') |
| 1630 | log.lapse ("compile and save font") |
Behdad Esfahbod | de71dca | 2013-07-24 12:40:54 -0400 | [diff] [blame] | 1631 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1632 | log.last_time = log.start_time |
| 1633 | log.lapse ("make one with everything (TOTAL TIME)") |
Behdad Esfahbod | de71dca | 2013-07-24 12:40:54 -0400 | [diff] [blame] | 1634 | |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1635 | log.font (font) |
Behdad Esfahbod | 8c486d8 | 2013-07-24 13:34:47 -0400 | [diff] [blame] | 1636 | |
| 1637 | if __name__ == '__main__': |
Behdad Esfahbod | 9e856ea | 2013-08-13 19:50:38 -0400 | [diff] [blame^] | 1638 | main (sys.argv[1:]) |