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