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