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