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