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