blob: 1da1962745ed9bac17c9b0fd0fc3d97e49b04a27 [file] [log] [blame]
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001# Copyright 2013 Google, Inc. All Rights Reserved.
2#
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04003# Google Author(s): Behdad Esfahbod
Behdad Esfahbod616d36e2013-08-13 20:02:59 -04004
5"""Python OpenType Layout Subsetter.
6
7Later grown into full OpenType subsetter, supporting all standard tables.
8"""
9
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040010import sys
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -040011import struct
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040012import time
Behdad Esfahbodd816b7f2013-08-16 16:18:40 -040013import array
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040014
Behdad Esfahbod46d260f2013-09-19 20:36:49 -040015from fontTools import ttLib
16from fontTools.ttLib.tables import otTables
17from fontTools.misc import psCharStrings
18from fontTools.pens import basePen
Behdad Esfahbod54660612013-07-21 18:16:55 -040019
Behdad Esfahbod54660612013-07-21 18:16:55 -040020
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040021def _add_method(*clazzes):
Behdad Esfahbod616d36e2013-08-13 20:02:59 -040022 """Returns a decorator function that adds a new method to one or
23 more classes."""
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040024 def wrapper(method):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040025 for clazz in clazzes:
26 assert clazz.__name__ != 'DefaultTable', 'Oops, table class not found.'
Behdad Esfahbode7a0d562013-08-16 10:56:30 -040027 assert not hasattr(clazz, method.func_name), \
Behdad Esfahbodd77f1572013-08-15 19:24:36 -040028 "Oops, class '%s' has method '%s'." % (clazz.__name__,
29 method.func_name)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040030 setattr(clazz, method.func_name, method)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040031 return None
32 return wrapper
Behdad Esfahbod54660612013-07-21 18:16:55 -040033
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040034def _uniq_sort(l):
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040035 return sorted(set(l))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -040036
Behdad Esfahbod42d4f2b2013-08-16 16:16:22 -040037def _set_update(s, *others):
38 # Jython's set.update only takes one other argument.
39 # Emulate real set.update...
40 for other in others:
41 s.update(other)
42
Behdad Esfahbod78661bb2013-07-23 10:23:42 -040043
Behdad Esfahbod46d260f2013-09-19 20:36:49 -040044@_add_method(otTables.Coverage)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040045def intersect(self, glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040046 "Returns ascending list of matching coverage values."
Behdad Esfahbod4734be52013-08-14 19:47:42 -040047 return [i for i,g in enumerate(self.glyphs) if g in glyphs]
Behdad Esfahbod610b0552013-07-23 14:52:18 -040048
Behdad Esfahbod46d260f2013-09-19 20:36:49 -040049@_add_method(otTables.Coverage)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040050def intersect_glyphs(self, glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040051 "Returns set of intersecting glyphs."
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040052 return set(g for g in self.glyphs if g in glyphs)
Behdad Esfahbod849d25c2013-08-12 19:24:24 -040053
Behdad Esfahbod46d260f2013-09-19 20:36:49 -040054@_add_method(otTables.Coverage)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040055def subset(self, glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040056 "Returns ascending list of remaining coverage values."
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040057 indices = self.intersect(glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040058 self.glyphs = [g for g in self.glyphs if g in glyphs]
59 return indices
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040060
Behdad Esfahbod46d260f2013-09-19 20:36:49 -040061@_add_method(otTables.Coverage)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040062def remap(self, coverage_map):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040063 "Remaps coverage."
64 self.glyphs = [self.glyphs[i] for i in coverage_map]
Behdad Esfahbod14374262013-08-08 22:26:49 -040065
Behdad Esfahbod46d260f2013-09-19 20:36:49 -040066@_add_method(otTables.ClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040067def intersect(self, glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040068 "Returns ascending list of matching class values."
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040069 return _uniq_sort(
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040070 ([0] if any(g not in self.classDefs for g in glyphs) else []) +
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040071 [v for g,v in self.classDefs.iteritems() if g in glyphs])
Behdad Esfahbodb8d55882013-07-23 22:17:39 -040072
Behdad Esfahbod46d260f2013-09-19 20:36:49 -040073@_add_method(otTables.ClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040074def intersect_class(self, glyphs, klass):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040075 "Returns set of glyphs matching class."
76 if klass == 0:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040077 return set(g for g in glyphs if g not in self.classDefs)
78 return set(g for g,v in self.classDefs.iteritems()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040079 if v == klass and g in glyphs)
Behdad Esfahbodb8d55882013-07-23 22:17:39 -040080
Behdad Esfahbod46d260f2013-09-19 20:36:49 -040081@_add_method(otTables.ClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040082def subset(self, glyphs, remap=False):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040083 "Returns ascending list of remaining classes."
Behdad Esfahbodd73f2252013-08-16 10:58:25 -040084 self.classDefs = dict((g,v) for g,v in self.classDefs.iteritems() if g in glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040085 # Note: while class 0 has the special meaning of "not matched",
86 # if no glyph will ever /not match/, we can optimize class 0 out too.
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040087 indices = _uniq_sort(
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040088 ([0] if any(g not in self.classDefs for g in glyphs) else []) +
Behdad Esfahbod4e5d9672013-08-14 19:49:53 -040089 self.classDefs.values())
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040090 if remap:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040091 self.remap(indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040092 return indices
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -040093
Behdad Esfahbod46d260f2013-09-19 20:36:49 -040094@_add_method(otTables.ClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040095def remap(self, class_map):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040096 "Remaps classes."
Behdad Esfahbodd73f2252013-08-16 10:58:25 -040097 self.classDefs = dict((g,class_map.index(v))
98 for g,v in self.classDefs.iteritems())
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040099
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400100@_add_method(otTables.SingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400101def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400102 if cur_glyphs == None: cur_glyphs = s.glyphs
103 if self.Format in [1, 2]:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400104 s.glyphs.update(v for g,v in self.mapping.iteritems() if g in cur_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400105 else:
106 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400107
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400108@_add_method(otTables.SingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400109def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400110 if self.Format in [1, 2]:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -0400111 self.mapping = dict((g,v) for g,v in self.mapping.iteritems()
112 if g in s.glyphs and v in s.glyphs)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400113 return bool(self.mapping)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400114 else:
115 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400116
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400117@_add_method(otTables.MultipleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400118def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400119 if cur_glyphs == None: cur_glyphs = s.glyphs
120 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400121 indices = self.Coverage.intersect(cur_glyphs)
Behdad Esfahboda9bfec12013-08-16 16:21:25 -0400122 _set_update(s.glyphs, *(self.Sequence[i].Substitute for i in indices))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400123 else:
124 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400125
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400126@_add_method(otTables.MultipleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400127def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400128 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400129 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400130 self.Sequence = [self.Sequence[i] for i in indices]
131 # Now drop rules generating glyphs we don't want
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400132 indices = [i for i,seq in enumerate(self.Sequence)
133 if all(sub in s.glyphs for sub in seq.Substitute)]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400134 self.Sequence = [self.Sequence[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400135 self.Coverage.remap(indices)
136 self.SequenceCount = len(self.Sequence)
137 return bool(self.SequenceCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400138 else:
139 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400140
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400141@_add_method(otTables.AlternateSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400142def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400143 if cur_glyphs == None: cur_glyphs = s.glyphs
144 if self.Format == 1:
Behdad Esfahbod42d4f2b2013-08-16 16:16:22 -0400145 _set_update(s.glyphs, *(vlist for g,vlist in self.alternates.iteritems()
146 if g in cur_glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400147 else:
148 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400149
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400150@_add_method(otTables.AlternateSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400151def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400152 if self.Format == 1:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -0400153 self.alternates = dict((g,vlist)
154 for g,vlist in self.alternates.iteritems()
155 if g in s.glyphs and
156 all(v in s.glyphs for v in vlist))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400157 return bool(self.alternates)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400158 else:
159 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400160
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400161@_add_method(otTables.LigatureSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400162def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400163 if cur_glyphs == None: cur_glyphs = s.glyphs
164 if self.Format == 1:
Behdad Esfahbod42d4f2b2013-08-16 16:16:22 -0400165 _set_update(s.glyphs, *([seq.LigGlyph for seq in seqs
166 if all(c in s.glyphs for c in seq.Component)]
167 for g,seqs in self.ligatures.iteritems()
168 if g in cur_glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400169 else:
170 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400171
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400172@_add_method(otTables.LigatureSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400173def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400174 if self.Format == 1:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -0400175 self.ligatures = dict((g,v) for g,v in self.ligatures.iteritems()
176 if g in s.glyphs)
177 self.ligatures = dict((g,[seq for seq in seqs
178 if seq.LigGlyph in s.glyphs and
179 all(c in s.glyphs for c in seq.Component)])
180 for g,seqs in self.ligatures.iteritems())
181 self.ligatures = dict((g,v) for g,v in self.ligatures.iteritems() if v)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400182 return bool(self.ligatures)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400183 else:
184 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400185
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400186@_add_method(otTables.ReverseChainSingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400187def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400188 if cur_glyphs == None: cur_glyphs = s.glyphs
189 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400190 indices = self.Coverage.intersect(cur_glyphs)
191 if(not indices or
192 not all(c.intersect(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400193 for c in self.LookAheadCoverage + self.BacktrackCoverage)):
194 return
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400195 s.glyphs.update(self.Substitute[i] for i in indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400196 else:
197 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400198
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400199@_add_method(otTables.ReverseChainSingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400200def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400201 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400202 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400203 self.Substitute = [self.Substitute[i] for i in indices]
204 # Now drop rules generating glyphs we don't want
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400205 indices = [i for i,sub in enumerate(self.Substitute)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400206 if sub in s.glyphs]
207 self.Substitute = [self.Substitute[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400208 self.Coverage.remap(indices)
209 self.GlyphCount = len(self.Substitute)
210 return bool(self.GlyphCount and
211 all(c.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400212 for c in self.LookAheadCoverage+self.BacktrackCoverage))
213 else:
214 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400215
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400216@_add_method(otTables.SinglePos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400217def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400218 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400219 return len(self.Coverage.subset(s.glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400220 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400221 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400222 self.Value = [self.Value[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400223 self.ValueCount = len(self.Value)
224 return bool(self.ValueCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400225 else:
226 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400227
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400228@_add_method(otTables.SinglePos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400229def prune_post_subset(self, options):
230 if not options.hinting:
231 # Drop device tables
232 self.ValueFormat &= ~0x00F0
233 return True
234
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400235@_add_method(otTables.PairPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400236def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400237 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400238 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400239 self.PairSet = [self.PairSet[i] for i in indices]
240 for p in self.PairSet:
241 p.PairValueRecord = [r for r in p.PairValueRecord
242 if r.SecondGlyph in s.glyphs]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400243 p.PairValueCount = len(p.PairValueRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400244 self.PairSet = [p for p in self.PairSet if p.PairValueCount]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400245 self.PairSetCount = len(self.PairSet)
246 return bool(self.PairSetCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400247 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400248 class1_map = self.ClassDef1.subset(s.glyphs, remap=True)
249 class2_map = self.ClassDef2.subset(s.glyphs, remap=True)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400250 self.Class1Record = [self.Class1Record[i] for i in class1_map]
251 for c in self.Class1Record:
252 c.Class2Record = [c.Class2Record[i] for i in class2_map]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400253 self.Class1Count = len(class1_map)
254 self.Class2Count = len(class2_map)
255 return bool(self.Class1Count and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400256 self.Class2Count and
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400257 self.Coverage.subset(s.glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400258 else:
259 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400260
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400261@_add_method(otTables.PairPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400262def prune_post_subset(self, options):
263 if not options.hinting:
264 # Drop device tables
265 self.ValueFormat1 &= ~0x00F0
266 self.ValueFormat2 &= ~0x00F0
267 return True
268
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400269@_add_method(otTables.CursivePos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400270def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400271 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400272 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400273 self.EntryExitRecord = [self.EntryExitRecord[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400274 self.EntryExitCount = len(self.EntryExitRecord)
275 return bool(self.EntryExitCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400276 else:
277 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400278
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400279@_add_method(otTables.Anchor)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400280def prune_hints(self):
281 # Drop device tables / contour anchor point
282 self.Format = 1
283
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400284@_add_method(otTables.CursivePos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400285def prune_post_subset(self, options):
286 if not options.hinting:
287 for rec in self.EntryExitRecord:
288 if rec.EntryAnchor: rec.EntryAnchor.prune_hints()
289 if rec.ExitAnchor: rec.ExitAnchor.prune_hints()
290 return True
291
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400292@_add_method(otTables.MarkBasePos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400293def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400294 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400295 mark_indices = self.MarkCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400296 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i]
297 for i in mark_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400298 self.MarkArray.MarkCount = len(self.MarkArray.MarkRecord)
299 base_indices = self.BaseCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400300 self.BaseArray.BaseRecord = [self.BaseArray.BaseRecord[i]
301 for i in base_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400302 self.BaseArray.BaseCount = len(self.BaseArray.BaseRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400303 # Prune empty classes
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400304 class_indices = _uniq_sort(v.Class for v in self.MarkArray.MarkRecord)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400305 self.ClassCount = len(class_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400306 for m in self.MarkArray.MarkRecord:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400307 m.Class = class_indices.index(m.Class)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400308 for b in self.BaseArray.BaseRecord:
309 b.BaseAnchor = [b.BaseAnchor[i] for i in class_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400310 return bool(self.ClassCount and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400311 self.MarkArray.MarkCount and
312 self.BaseArray.BaseCount)
313 else:
314 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400315
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400316@_add_method(otTables.MarkBasePos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400317def prune_post_subset(self, options):
318 if not options.hinting:
319 for m in self.MarkArray.MarkRecord:
Behdad Esfahbode1a010c2013-10-09 15:57:22 +0200320 if m.MarkAnchor:
321 m.MarkAnchor.prune_hints()
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400322 for b in self.BaseArray.BaseRecord:
323 for a in b.BaseAnchor:
Behdad Esfahbode1a010c2013-10-09 15:57:22 +0200324 if a:
325 a.prune_hints()
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400326 return True
327
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400328@_add_method(otTables.MarkLigPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400329def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400330 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400331 mark_indices = self.MarkCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400332 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i]
333 for i in mark_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400334 self.MarkArray.MarkCount = len(self.MarkArray.MarkRecord)
335 ligature_indices = self.LigatureCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400336 self.LigatureArray.LigatureAttach = [self.LigatureArray.LigatureAttach[i]
337 for i in ligature_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400338 self.LigatureArray.LigatureCount = len(self.LigatureArray.LigatureAttach)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400339 # Prune empty classes
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400340 class_indices = _uniq_sort(v.Class for v in self.MarkArray.MarkRecord)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400341 self.ClassCount = len(class_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400342 for m in self.MarkArray.MarkRecord:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400343 m.Class = class_indices.index(m.Class)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400344 for l in self.LigatureArray.LigatureAttach:
345 for c in l.ComponentRecord:
346 c.LigatureAnchor = [c.LigatureAnchor[i] for i in class_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400347 return bool(self.ClassCount and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400348 self.MarkArray.MarkCount and
349 self.LigatureArray.LigatureCount)
350 else:
351 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400352
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400353@_add_method(otTables.MarkLigPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400354def prune_post_subset(self, options):
355 if not options.hinting:
356 for m in self.MarkArray.MarkRecord:
Behdad Esfahbode1a010c2013-10-09 15:57:22 +0200357 if m.MarkAnchor:
358 m.MarkAnchor.prune_hints()
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400359 for l in self.LigatureArray.LigatureAttach:
360 for c in l.ComponentRecord:
361 for a in c.LigatureAnchor:
Behdad Esfahbode1a010c2013-10-09 15:57:22 +0200362 if a:
363 a.prune_hints()
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400364 return True
365
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400366@_add_method(otTables.MarkMarkPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400367def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400368 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400369 mark1_indices = self.Mark1Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400370 self.Mark1Array.MarkRecord = [self.Mark1Array.MarkRecord[i]
371 for i in mark1_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400372 self.Mark1Array.MarkCount = len(self.Mark1Array.MarkRecord)
373 mark2_indices = self.Mark2Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400374 self.Mark2Array.Mark2Record = [self.Mark2Array.Mark2Record[i]
375 for i in mark2_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400376 self.Mark2Array.MarkCount = len(self.Mark2Array.Mark2Record)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400377 # Prune empty classes
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400378 class_indices = _uniq_sort(v.Class for v in self.Mark1Array.MarkRecord)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400379 self.ClassCount = len(class_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400380 for m in self.Mark1Array.MarkRecord:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400381 m.Class = class_indices.index(m.Class)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400382 for b in self.Mark2Array.Mark2Record:
383 b.Mark2Anchor = [b.Mark2Anchor[i] for i in class_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400384 return bool(self.ClassCount and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400385 self.Mark1Array.MarkCount and
386 self.Mark2Array.MarkCount)
387 else:
388 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400389
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400390@_add_method(otTables.MarkMarkPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400391def prune_post_subset(self, options):
392 if not options.hinting:
393 # Drop device tables or contour anchor point
394 for m in self.Mark1Array.MarkRecord:
Behdad Esfahbode1a010c2013-10-09 15:57:22 +0200395 if m.MarkAnchor:
396 m.MarkAnchor.prune_hints()
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400397 for b in self.Mark2Array.Mark2Record:
Behdad Esfahbod0ec17d92013-09-15 18:30:41 -0400398 for m in b.Mark2Anchor:
Behdad Esfahbode1a010c2013-10-09 15:57:22 +0200399 if m:
400 m.prune_hints()
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400401 return True
402
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400403@_add_method(otTables.SingleSubst,
404 otTables.MultipleSubst,
405 otTables.AlternateSubst,
406 otTables.LigatureSubst,
407 otTables.ReverseChainSingleSubst,
408 otTables.SinglePos,
409 otTables.PairPos,
410 otTables.CursivePos,
411 otTables.MarkBasePos,
412 otTables.MarkLigPos,
413 otTables.MarkMarkPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400414def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400415 pass
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400416
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400417@_add_method(otTables.SingleSubst,
418 otTables.MultipleSubst,
419 otTables.AlternateSubst,
420 otTables.LigatureSubst,
421 otTables.ReverseChainSingleSubst,
422 otTables.SinglePos,
423 otTables.PairPos,
424 otTables.CursivePos,
425 otTables.MarkBasePos,
426 otTables.MarkLigPos,
427 otTables.MarkMarkPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400428def collect_lookups(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400429 return []
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400430
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400431@_add_method(otTables.SingleSubst,
432 otTables.MultipleSubst,
433 otTables.AlternateSubst,
434 otTables.LigatureSubst,
435 otTables.ContextSubst,
436 otTables.ChainContextSubst,
437 otTables.ReverseChainSingleSubst,
438 otTables.SinglePos,
439 otTables.PairPos,
440 otTables.CursivePos,
441 otTables.MarkBasePos,
442 otTables.MarkLigPos,
443 otTables.MarkMarkPos,
444 otTables.ContextPos,
445 otTables.ChainContextPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400446def prune_pre_subset(self, options):
447 return True
448
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400449@_add_method(otTables.SingleSubst,
450 otTables.MultipleSubst,
451 otTables.AlternateSubst,
452 otTables.LigatureSubst,
453 otTables.ReverseChainSingleSubst,
454 otTables.ContextSubst,
455 otTables.ChainContextSubst,
456 otTables.ContextPos,
457 otTables.ChainContextPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400458def prune_post_subset(self, options):
459 return True
460
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400461@_add_method(otTables.SingleSubst,
462 otTables.AlternateSubst,
463 otTables.ReverseChainSingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400464def may_have_non_1to1(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400465 return False
Behdad Esfahbodaeacc152013-08-12 20:24:33 -0400466
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400467@_add_method(otTables.MultipleSubst,
468 otTables.LigatureSubst,
469 otTables.ContextSubst,
470 otTables.ChainContextSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400471def may_have_non_1to1(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400472 return True
Behdad Esfahbodaeacc152013-08-12 20:24:33 -0400473
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400474@_add_method(otTables.ContextSubst,
475 otTables.ChainContextSubst,
476 otTables.ContextPos,
477 otTables.ChainContextPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400478def __classify_context(self):
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400479
Behdad Esfahbod3d4c4712013-08-13 20:10:17 -0400480 class ContextHelper(object):
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400481 def __init__(self, klass, Format):
482 if klass.__name__.endswith('Subst'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400483 Typ = 'Sub'
484 Type = 'Subst'
485 else:
486 Typ = 'Pos'
487 Type = 'Pos'
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400488 if klass.__name__.startswith('Chain'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400489 Chain = 'Chain'
490 else:
491 Chain = ''
492 ChainTyp = Chain+Typ
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400493
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400494 self.Typ = Typ
495 self.Type = Type
496 self.Chain = Chain
497 self.ChainTyp = ChainTyp
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400498
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400499 self.LookupRecord = Type+'LookupRecord'
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400500
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400501 if Format == 1:
502 Coverage = lambda r: r.Coverage
503 ChainCoverage = lambda r: r.Coverage
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400504 ContextData = lambda r:(None,)
505 ChainContextData = lambda r:(None, None, None)
506 RuleData = lambda r:(r.Input,)
507 ChainRuleData = lambda r:(r.Backtrack, r.Input, r.LookAhead)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400508 SetRuleData = None
509 ChainSetRuleData = None
510 elif Format == 2:
511 Coverage = lambda r: r.Coverage
512 ChainCoverage = lambda r: r.Coverage
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400513 ContextData = lambda r:(r.ClassDef,)
514 ChainContextData = lambda r:(r.LookAheadClassDef,
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400515 r.InputClassDef,
516 r.BacktrackClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400517 RuleData = lambda r:(r.Class,)
518 ChainRuleData = lambda r:(r.LookAhead, r.Input, r.Backtrack)
519 def SetRuleData(r, d):(r.Class,) = d
520 def ChainSetRuleData(r, d):(r.LookAhead, r.Input, r.Backtrack) = d
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400521 elif Format == 3:
522 Coverage = lambda r: r.Coverage[0]
523 ChainCoverage = lambda r: r.InputCoverage[0]
524 ContextData = None
525 ChainContextData = None
526 RuleData = lambda r: r.Coverage
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400527 ChainRuleData = lambda r:(r.LookAheadCoverage +
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400528 r.InputCoverage +
529 r.BacktrackCoverage)
530 SetRuleData = None
531 ChainSetRuleData = None
532 else:
533 assert 0, "unknown format: %s" % Format
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400534
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400535 if Chain:
536 self.Coverage = ChainCoverage
537 self.ContextData = ChainContextData
538 self.RuleData = ChainRuleData
539 self.SetRuleData = ChainSetRuleData
540 else:
541 self.Coverage = Coverage
542 self.ContextData = ContextData
543 self.RuleData = RuleData
544 self.SetRuleData = SetRuleData
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400545
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400546 if Format == 1:
547 self.Rule = ChainTyp+'Rule'
548 self.RuleCount = ChainTyp+'RuleCount'
549 self.RuleSet = ChainTyp+'RuleSet'
550 self.RuleSetCount = ChainTyp+'RuleSetCount'
551 self.Intersect = lambda glyphs, c, r: [r] if r in glyphs else []
552 elif Format == 2:
553 self.Rule = ChainTyp+'ClassRule'
554 self.RuleCount = ChainTyp+'ClassRuleCount'
555 self.RuleSet = ChainTyp+'ClassSet'
556 self.RuleSetCount = ChainTyp+'ClassSetCount'
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400557 self.Intersect = lambda glyphs, c, r: c.intersect_class(glyphs, r)
Behdad Esfahbod89987002013-07-23 23:07:42 -0400558
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400559 self.ClassDef = 'InputClassDef' if Chain else 'ClassDef'
Behdad Esfahbod98b60752013-10-14 17:49:19 +0200560 self.ClassDefIndex = 1 if Chain else 0
Behdad Esfahbod11763302013-08-14 15:33:08 -0400561 self.Input = 'Input' if Chain else 'Class'
Behdad Esfahbod27108392013-07-23 16:40:47 -0400562
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400563 if self.Format not in [1, 2, 3]:
Behdad Esfahbod318adc02013-08-13 20:09:28 -0400564 return None # Don't shoot the messenger; let it go
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400565 if not hasattr(self.__class__, "__ContextHelpers"):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400566 self.__class__.__ContextHelpers = {}
567 if self.Format not in self.__class__.__ContextHelpers:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400568 helper = ContextHelper(self.__class__, self.Format)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400569 self.__class__.__ContextHelpers[self.Format] = helper
570 return self.__class__.__ContextHelpers[self.Format]
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400571
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400572@_add_method(otTables.ContextSubst,
573 otTables.ChainContextSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400574def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400575 if cur_glyphs == None: cur_glyphs = s.glyphs
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400576 c = self.__classify_context()
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400577
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400578 indices = c.Coverage(self).intersect(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400579 if not indices:
580 return []
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400581 cur_glyphs = c.Coverage(self).intersect_glyphs(s.glyphs);
Behdad Esfahbod1d4fa132013-08-08 22:59:32 -0400582
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400583 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400584 ContextData = c.ContextData(self)
585 rss = getattr(self, c.RuleSet)
Behdad Esfahbod11174452013-11-18 20:20:49 -0500586 rssCount = getattr(self, c.RuleSetCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400587 for i in indices:
Behdad Esfahbod11174452013-11-18 20:20:49 -0500588 if i >= rssCount or not rss[i]: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400589 for r in getattr(rss[i], c.Rule):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400590 if not r: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400591 if all(all(c.Intersect(s.glyphs, cd, k) for k in klist)
592 for cd,klist in zip(ContextData, c.RuleData(r))):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400593 chaos = False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400594 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400595 if not ll: continue
596 seqi = ll.SequenceIndex
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400597 if chaos:
598 pos_glyphs = s.glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400599 else:
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400600 if seqi == 0:
601 pos_glyphs = set([c.Coverage(self).glyphs[i]])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400602 else:
Behdad Esfahbodd3fdcc72013-08-14 17:59:31 -0400603 pos_glyphs = set([r.Input[seqi - 1]])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400604 lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400605 chaos = chaos or lookup.may_have_non_1to1()
606 lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400607 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400608 ClassDef = getattr(self, c.ClassDef)
609 indices = ClassDef.intersect(cur_glyphs)
610 ContextData = c.ContextData(self)
611 rss = getattr(self, c.RuleSet)
Behdad Esfahbod11174452013-11-18 20:20:49 -0500612 rssCount = getattr(self, c.RuleSetCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400613 for i in indices:
Behdad Esfahbod11174452013-11-18 20:20:49 -0500614 if i >= rssCount or not rss[i]: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400615 for r in getattr(rss[i], c.Rule):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400616 if not r: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400617 if all(all(c.Intersect(s.glyphs, cd, k) for k in klist)
618 for cd,klist in zip(ContextData, c.RuleData(r))):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400619 chaos = False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400620 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400621 if not ll: continue
622 seqi = ll.SequenceIndex
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400623 if chaos:
624 pos_glyphs = s.glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400625 else:
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400626 if seqi == 0:
627 pos_glyphs = ClassDef.intersect_class(cur_glyphs, i)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400628 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400629 pos_glyphs = ClassDef.intersect_class(s.glyphs,
Behdad Esfahbod11763302013-08-14 15:33:08 -0400630 getattr(r, c.Input)[seqi - 1])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400631 lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400632 chaos = chaos or lookup.may_have_non_1to1()
633 lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400634 elif self.Format == 3:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400635 if not all(x.intersect(s.glyphs) for x in c.RuleData(self)):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400636 return []
637 r = self
638 chaos = False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400639 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400640 if not ll: continue
641 seqi = ll.SequenceIndex
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400642 if chaos:
643 pos_glyphs = s.glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400644 else:
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400645 if seqi == 0:
646 pos_glyphs = cur_glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400647 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400648 pos_glyphs = r.InputCoverage[seqi].intersect_glyphs(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400649 lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400650 chaos = chaos or lookup.may_have_non_1to1()
651 lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400652 else:
653 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod00776972013-07-23 15:33:00 -0400654
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400655@_add_method(otTables.ContextSubst,
656 otTables.ContextPos,
657 otTables.ChainContextSubst,
658 otTables.ChainContextPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400659def subset_glyphs(self, s):
660 c = self.__classify_context()
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400661
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400662 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400663 indices = self.Coverage.subset(s.glyphs)
664 rss = getattr(self, c.RuleSet)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400665 rss = [rss[i] for i in indices]
666 for rs in rss:
667 if not rs: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400668 ss = getattr(rs, c.Rule)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400669 ss = [r for r in ss
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400670 if r and all(all(g in s.glyphs for g in glist)
671 for glist in c.RuleData(r))]
672 setattr(rs, c.Rule, ss)
673 setattr(rs, c.RuleCount, len(ss))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400674 # Prune empty subrulesets
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400675 rss = [rs for rs in rss if rs and getattr(rs, c.Rule)]
676 setattr(self, c.RuleSet, rss)
677 setattr(self, c.RuleSetCount, len(rss))
678 return bool(rss)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400679 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400680 if not self.Coverage.subset(s.glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400681 return False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400682 ContextData = c.ContextData(self)
683 klass_maps = [x.subset(s.glyphs, remap=True) for x in ContextData]
Behdad Esfahbod98b60752013-10-14 17:49:19 +0200684
685 # Keep rulesets for class numbers that survived.
686 indices = klass_maps[c.ClassDefIndex]
687 rss = getattr(self, c.RuleSet)
688 rssCount = getattr(self, c.RuleSetCount)
689 rss = [rss[i] for i in indices if i < rssCount]
690 del rssCount
691 # Delete, but not renumber, unreachable rulesets.
692 indices = getattr(self, c.ClassDef).intersect(self.Coverage.glyphs)
693 rss = [rss if i in indices else None for i,rss in enumerate(rss)]
694 while rss and rss[-1] == None:
695 del rss[-1]
696
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400697 for rs in rss:
698 if not rs: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400699 ss = getattr(rs, c.Rule)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400700 ss = [r for r in ss
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400701 if r and all(all(k in klass_map for k in klist)
702 for klass_map,klist in zip(klass_maps, c.RuleData(r)))]
703 setattr(rs, c.Rule, ss)
704 setattr(rs, c.RuleCount, len(ss))
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400705
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400706 # Remap rule classes
707 for r in ss:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400708 c.SetRuleData(r, [[klass_map.index(k) for k in klist]
709 for klass_map,klist in zip(klass_maps, c.RuleData(r))])
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400710 return bool(rss)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400711 elif self.Format == 3:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400712 return all(x.subset(s.glyphs) for x in c.RuleData(self))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400713 else:
714 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400715
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400716@_add_method(otTables.ContextSubst,
717 otTables.ChainContextSubst,
718 otTables.ContextPos,
719 otTables.ChainContextPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400720def subset_lookups(self, lookup_indices):
721 c = self.__classify_context()
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400722
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400723 if self.Format in [1, 2]:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400724 for rs in getattr(self, c.RuleSet):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400725 if not rs: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400726 for r in getattr(rs, c.Rule):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400727 if not r: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400728 setattr(r, c.LookupRecord,
729 [ll for ll in getattr(r, c.LookupRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400730 if ll and ll.LookupListIndex in lookup_indices])
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400731 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400732 if not ll: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400733 ll.LookupListIndex = lookup_indices.index(ll.LookupListIndex)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400734 elif self.Format == 3:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400735 setattr(self, c.LookupRecord,
736 [ll for ll in getattr(self, c.LookupRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400737 if ll and ll.LookupListIndex in lookup_indices])
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400738 for ll in getattr(self, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400739 if not ll: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400740 ll.LookupListIndex = lookup_indices.index(ll.LookupListIndex)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400741 else:
742 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400743
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400744@_add_method(otTables.ContextSubst,
745 otTables.ChainContextSubst,
746 otTables.ContextPos,
747 otTables.ChainContextPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400748def collect_lookups(self):
749 c = self.__classify_context()
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400750
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400751 if self.Format in [1, 2]:
752 return [ll.LookupListIndex
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400753 for rs in getattr(self, c.RuleSet) if rs
754 for r in getattr(rs, c.Rule) if r
755 for ll in getattr(r, c.LookupRecord) if ll]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400756 elif self.Format == 3:
757 return [ll.LookupListIndex
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400758 for ll in getattr(self, c.LookupRecord) if ll]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400759 else:
760 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400761
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400762@_add_method(otTables.ExtensionSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400763def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400764 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400765 self.ExtSubTable.closure_glyphs(s, cur_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400766 else:
767 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400768
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400769@_add_method(otTables.ExtensionSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400770def may_have_non_1to1(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400771 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400772 return self.ExtSubTable.may_have_non_1to1()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400773 else:
774 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbodaeacc152013-08-12 20:24:33 -0400775
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400776@_add_method(otTables.ExtensionSubst,
777 otTables.ExtensionPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400778def prune_pre_subset(self, options):
779 if self.Format == 1:
780 return self.ExtSubTable.prune_pre_subset(options)
781 else:
782 assert 0, "unknown format: %s" % self.Format
783
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400784@_add_method(otTables.ExtensionSubst,
785 otTables.ExtensionPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400786def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400787 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400788 return self.ExtSubTable.subset_glyphs(s)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400789 else:
790 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400791
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400792@_add_method(otTables.ExtensionSubst,
793 otTables.ExtensionPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400794def prune_post_subset(self, options):
795 if self.Format == 1:
796 return self.ExtSubTable.prune_post_subset(options)
797 else:
798 assert 0, "unknown format: %s" % self.Format
799
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400800@_add_method(otTables.ExtensionSubst,
801 otTables.ExtensionPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400802def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400803 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400804 return self.ExtSubTable.subset_lookups(lookup_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400805 else:
806 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400807
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400808@_add_method(otTables.ExtensionSubst,
809 otTables.ExtensionPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400810def collect_lookups(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400811 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400812 return self.ExtSubTable.collect_lookups()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400813 else:
814 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400815
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400816@_add_method(otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400817def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400818 for st in self.SubTable:
819 if not st: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400820 st.closure_glyphs(s, cur_glyphs)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400821
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400822@_add_method(otTables.Lookup)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400823def prune_pre_subset(self, options):
824 ret = False
825 for st in self.SubTable:
826 if not st: continue
827 if st.prune_pre_subset(options): ret = True
828 return ret
829
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400830@_add_method(otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400831def subset_glyphs(self, s):
832 self.SubTable = [st for st in self.SubTable if st and st.subset_glyphs(s)]
833 self.SubTableCount = len(self.SubTable)
834 return bool(self.SubTableCount)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400835
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400836@_add_method(otTables.Lookup)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400837def prune_post_subset(self, options):
838 ret = False
839 for st in self.SubTable:
840 if not st: continue
841 if st.prune_post_subset(options): ret = True
842 return ret
843
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400844@_add_method(otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400845def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400846 for s in self.SubTable:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400847 s.subset_lookups(lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400848
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400849@_add_method(otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400850def collect_lookups(self):
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400851 return _uniq_sort(sum((st.collect_lookups() for st in self.SubTable
852 if st), []))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400853
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400854@_add_method(otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400855def may_have_non_1to1(self):
856 return any(st.may_have_non_1to1() for st in self.SubTable if st)
Behdad Esfahbodaeacc152013-08-12 20:24:33 -0400857
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400858@_add_method(otTables.LookupList)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400859def prune_pre_subset(self, options):
860 ret = False
861 for l in self.Lookup:
862 if not l: continue
863 if l.prune_pre_subset(options): ret = True
864 return ret
865
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400866@_add_method(otTables.LookupList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400867def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400868 "Returns the indices of nonempty lookups."
Behdad Esfahbod4734be52013-08-14 19:47:42 -0400869 return [i for i,l in enumerate(self.Lookup) if l and l.subset_glyphs(s)]
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400870
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400871@_add_method(otTables.LookupList)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400872def prune_post_subset(self, options):
873 ret = False
874 for l in self.Lookup:
875 if not l: continue
876 if l.prune_post_subset(options): ret = True
877 return ret
878
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400879@_add_method(otTables.LookupList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400880def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400881 self.Lookup = [self.Lookup[i] for i in lookup_indices
882 if i < self.LookupCount]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400883 self.LookupCount = len(self.Lookup)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400884 for l in self.Lookup:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400885 l.subset_lookups(lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400886
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400887@_add_method(otTables.LookupList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400888def closure_lookups(self, lookup_indices):
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400889 lookup_indices = _uniq_sort(lookup_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400890 recurse = lookup_indices
891 while True:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400892 recurse_lookups = sum((self.Lookup[i].collect_lookups()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400893 for i in recurse if i < self.LookupCount), [])
894 recurse_lookups = [l for l in recurse_lookups
895 if l not in lookup_indices and l < self.LookupCount]
896 if not recurse_lookups:
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400897 return _uniq_sort(lookup_indices)
898 recurse_lookups = _uniq_sort(recurse_lookups)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400899 lookup_indices.extend(recurse_lookups)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400900 recurse = recurse_lookups
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400901
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400902@_add_method(otTables.Feature)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400903def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400904 self.LookupListIndex = [l for l in self.LookupListIndex
905 if l in lookup_indices]
906 # Now map them.
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400907 self.LookupListIndex = [lookup_indices.index(l)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400908 for l in self.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400909 self.LookupCount = len(self.LookupListIndex)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400910 return self.LookupCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400911
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400912@_add_method(otTables.Feature)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400913def collect_lookups(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400914 return self.LookupListIndex[:]
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400915
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400916@_add_method(otTables.FeatureList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400917def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400918 "Returns the indices of nonempty features."
Behdad Esfahbod4734be52013-08-14 19:47:42 -0400919 feature_indices = [i for i,f in enumerate(self.FeatureRecord)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400920 if f.Feature.subset_lookups(lookup_indices)]
921 self.subset_features(feature_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400922 return feature_indices
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400923
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400924@_add_method(otTables.FeatureList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400925def collect_lookups(self, feature_indices):
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400926 return _uniq_sort(sum((self.FeatureRecord[i].Feature.collect_lookups()
927 for i in feature_indices
Behdad Esfahbod1ee298d2013-08-13 20:07:09 -0400928 if i < self.FeatureCount), []))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400929
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400930@_add_method(otTables.FeatureList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400931def subset_features(self, feature_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400932 self.FeatureRecord = [self.FeatureRecord[i] for i in feature_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400933 self.FeatureCount = len(self.FeatureRecord)
934 return bool(self.FeatureCount)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400935
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400936@_add_method(otTables.DefaultLangSys,
937 otTables.LangSys)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400938def subset_features(self, feature_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400939 if self.ReqFeatureIndex in feature_indices:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400940 self.ReqFeatureIndex = feature_indices.index(self.ReqFeatureIndex)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400941 else:
942 self.ReqFeatureIndex = 65535
943 self.FeatureIndex = [f for f in self.FeatureIndex if f in feature_indices]
944 # Now map them.
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400945 self.FeatureIndex = [feature_indices.index(f) for f in self.FeatureIndex
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400946 if f in feature_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400947 self.FeatureCount = len(self.FeatureIndex)
948 return bool(self.FeatureCount or self.ReqFeatureIndex != 65535)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400949
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400950@_add_method(otTables.DefaultLangSys,
951 otTables.LangSys)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400952def collect_features(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400953 feature_indices = self.FeatureIndex[:]
954 if self.ReqFeatureIndex != 65535:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400955 feature_indices.append(self.ReqFeatureIndex)
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400956 return _uniq_sort(feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400957
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400958@_add_method(otTables.Script)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400959def subset_features(self, feature_indices):
960 if(self.DefaultLangSys and
961 not self.DefaultLangSys.subset_features(feature_indices)):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400962 self.DefaultLangSys = None
963 self.LangSysRecord = [l for l in self.LangSysRecord
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400964 if l.LangSys.subset_features(feature_indices)]
965 self.LangSysCount = len(self.LangSysRecord)
966 return bool(self.LangSysCount or self.DefaultLangSys)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400967
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400968@_add_method(otTables.Script)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400969def collect_features(self):
970 feature_indices = [l.LangSys.collect_features() for l in self.LangSysRecord]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400971 if self.DefaultLangSys:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400972 feature_indices.append(self.DefaultLangSys.collect_features())
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400973 return _uniq_sort(sum(feature_indices, []))
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400974
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400975@_add_method(otTables.ScriptList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400976def subset_features(self, feature_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400977 self.ScriptRecord = [s for s in self.ScriptRecord
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400978 if s.Script.subset_features(feature_indices)]
979 self.ScriptCount = len(self.ScriptRecord)
980 return bool(self.ScriptCount)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400981
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400982@_add_method(otTables.ScriptList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400983def collect_features(self):
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400984 return _uniq_sort(sum((s.Script.collect_features()
985 for s in self.ScriptRecord), []))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400986
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400987@_add_method(ttLib.getTableClass('GSUB'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400988def closure_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400989 s.table = self.table
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400990 feature_indices = self.table.ScriptList.collect_features()
Behdad Esfahbod7e972472013-11-15 17:57:15 -0500991 if self.table.FeatureList:
992 lookup_indices = self.table.FeatureList.collect_lookups(feature_indices)
993 else:
994 lookup_indices = []
995 if self.table.LookupList:
996 while True:
997 orig_glyphs = s.glyphs.copy()
998 for i in lookup_indices:
999 if i >= self.table.LookupList.LookupCount: continue
1000 if not self.table.LookupList.Lookup[i]: continue
1001 self.table.LookupList.Lookup[i].closure_glyphs(s)
1002 if orig_glyphs == s.glyphs:
1003 break
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001004 del s.table
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001005
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001006@_add_method(ttLib.getTableClass('GSUB'),
1007 ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001008def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001009 s.glyphs = s.glyphs_gsubed
Behdad Esfahbod7e972472013-11-15 17:57:15 -05001010 if self.table.LookupList:
1011 lookup_indices = self.table.LookupList.subset_glyphs(s)
1012 else:
1013 lookup_indices = []
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001014 self.subset_lookups(lookup_indices)
1015 self.prune_lookups()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001016 return True
Behdad Esfahbod02b92062013-07-21 18:40:59 -04001017
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001018@_add_method(ttLib.getTableClass('GSUB'),
1019 ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001020def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001021 """Retrains specified lookups, then removes empty features, language
1022 systems, and scripts."""
Behdad Esfahbod7e972472013-11-15 17:57:15 -05001023 if self.table.LookupList:
1024 self.table.LookupList.subset_lookups(lookup_indices)
1025 if self.table.FeatureList:
1026 feature_indices = self.table.FeatureList.subset_lookups(lookup_indices)
1027 else:
1028 feature_indices = []
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001029 self.table.ScriptList.subset_features(feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -04001030
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001031@_add_method(ttLib.getTableClass('GSUB'),
1032 ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001033def prune_lookups(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001034 "Remove unreferenced lookups"
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001035 feature_indices = self.table.ScriptList.collect_features()
Behdad Esfahbod7e972472013-11-15 17:57:15 -05001036 if self.table.FeatureList:
1037 lookup_indices = self.table.FeatureList.collect_lookups(feature_indices)
1038 else:
1039 lookup_indices = []
1040 if self.table.LookupList:
1041 lookup_indices = self.table.LookupList.closure_lookups(lookup_indices)
1042 else:
1043 lookup_indices = []
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001044 self.subset_lookups(lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -04001045
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001046@_add_method(ttLib.getTableClass('GSUB'),
1047 ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001048def subset_feature_tags(self, feature_tags):
Behdad Esfahbod7e972472013-11-15 17:57:15 -05001049 if self.table.FeatureList:
1050 feature_indices = [i for i,f in
1051 enumerate(self.table.FeatureList.FeatureRecord)
1052 if f.FeatureTag in feature_tags]
1053 self.table.FeatureList.subset_features(feature_indices)
1054 else:
1055 feature_indices = []
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001056 self.table.ScriptList.subset_features(feature_indices)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -04001057
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001058@_add_method(ttLib.getTableClass('GSUB'),
1059 ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001060def prune_pre_subset(self, options):
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001061 if '*' not in options.layout_features:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001062 self.subset_feature_tags(options.layout_features)
1063 self.prune_lookups()
Behdad Esfahbod7e972472013-11-15 17:57:15 -05001064 if self.table.LookupList:
1065 self.table.LookupList.prune_pre_subset(options);
Behdad Esfahbodd77f1572013-08-15 19:24:36 -04001066 return True
1067
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001068@_add_method(ttLib.getTableClass('GSUB'),
1069 ttLib.getTableClass('GPOS'))
Behdad Esfahbodd77f1572013-08-15 19:24:36 -04001070def prune_post_subset(self, options):
Behdad Esfahbod9fe4eef2013-11-25 04:28:37 -05001071 table = self.table
1072 if table.ScriptList and not table.ScriptList.ScriptRecord:
1073 table.ScriptList = None
1074 if table.FeatureList and not table.FeatureList.FeatureRecord:
1075 table.FeatureList = None
1076 if table.LookupList:
1077 table.LookupList.prune_post_subset(options);
1078 if not table.LookupList.Lookup:
1079 table.LookupList = None
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001080 return True
Behdad Esfahbod356c42e2013-07-23 12:10:46 -04001081
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001082@_add_method(ttLib.getTableClass('GDEF'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001083def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001084 glyphs = s.glyphs_gsubed
1085 table = self.table
1086 if table.LigCaretList:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001087 indices = table.LigCaretList.Coverage.subset(glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001088 table.LigCaretList.LigGlyph = [table.LigCaretList.LigGlyph[i]
1089 for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001090 table.LigCaretList.LigGlyphCount = len(table.LigCaretList.LigGlyph)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001091 if table.MarkAttachClassDef:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001092 table.MarkAttachClassDef.classDefs = dict((g,v) for g,v in
1093 table.MarkAttachClassDef.
1094 classDefs.iteritems()
1095 if g in glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001096 if table.GlyphClassDef:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001097 table.GlyphClassDef.classDefs = dict((g,v) for g,v in
1098 table.GlyphClassDef.
1099 classDefs.iteritems()
1100 if g in glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001101 if table.AttachList:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001102 indices = table.AttachList.Coverage.subset(glyphs)
Behdad Esfahbod98769432013-11-19 14:40:57 -05001103 GlyphCount = table.AttachList.GlyphCount
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001104 table.AttachList.AttachPoint = [table.AttachList.AttachPoint[i]
Behdad Esfahbod98769432013-11-19 14:40:57 -05001105 for i in indices
1106 if i < GlyphCount]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001107 table.AttachList.GlyphCount = len(table.AttachList.AttachPoint)
Behdad Esfahbod5aea27d2013-11-25 04:19:42 -05001108 if hasattr(table, "MarkGlyphSetsDef") and table.MarkGlyphSetsDef:
1109 for coverage in table.MarkGlyphSetsDef.Coverage:
1110 coverage.subset(glyphs)
1111 table.MarkGlyphSetsDef.Coverage = [c for c in table.MarkGlyphSetsDef.Coverage if c.glyphs]
1112 return True
1113
1114@_add_method(ttLib.getTableClass('GDEF'))
1115def prune_post_subset(self, options):
1116 table = self.table
1117 if table.LigCaretList and not table.LigCaretList.LigGlyphCount:
1118 table.LigCaretList = None
1119 if table.MarkAttachClassDef and not table.MarkAttachClassDef.classDefs:
1120 table.MarkAttachClassDef = None
1121 if table.GlyphClassDef and not table.GlyphClassDef.classDefs:
1122 table.GlyphClassDef = None
1123 if table.AttachList and not table.AttachList.GlyphCount:
1124 table.AttachList = None
1125 if hasattr(table, "MarkGlyphSetsDef") and table.MarkGlyphSetsDef and not table.MarkGlyphSetsDef.Coverage:
1126 table.MarkGlyphSetsDef = None
1127 if table.Version == float(0x00010002)/0x10000:
1128 table.Version = 1.0
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001129 return bool(table.LigCaretList or
Behdad Esfahbod5aea27d2013-11-25 04:19:42 -05001130 table.MarkAttachClassDef or
1131 table.GlyphClassDef or
1132 table.AttachList or
1133 (table.Version >= float(0x00010002)/0x10000 and table.MarkGlyphSetsDef))
Behdad Esfahbodefb984a2013-07-21 22:26:16 -04001134
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001135@_add_method(ttLib.getTableClass('kern'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001136def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001137 # Prune unknown kern table types
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001138 self.kernTables = [t for t in self.kernTables if hasattr(t, 'kernTable')]
1139 return bool(self.kernTables)
Behdad Esfahbodd4e33a72013-07-24 18:51:05 -04001140
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001141@_add_method(ttLib.getTableClass('kern'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001142def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001143 glyphs = s.glyphs_gsubed
1144 for t in self.kernTables:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001145 t.kernTable = dict(((a,b),v) for (a,b),v in t.kernTable.iteritems()
1146 if a in glyphs and b in glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001147 self.kernTables = [t for t in self.kernTables if t.kernTable]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001148 return bool(self.kernTables)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -04001149
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001150@_add_method(ttLib.getTableClass('vmtx'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001151def subset_glyphs(self, s):
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001152 self.metrics = dict((g,v) for g,v in self.metrics.iteritems() if g in s.glyphs)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001153 return bool(self.metrics)
Behdad Esfahbodc7160442013-07-22 14:29:08 -04001154
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001155@_add_method(ttLib.getTableClass('hmtx'))
1156def subset_glyphs(self, s):
1157 self.metrics = dict((g,v) for g,v in self.metrics.iteritems() if g in s.glyphs)
1158 return True # Required table
1159
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001160@_add_method(ttLib.getTableClass('hdmx'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001161def subset_glyphs(self, s):
Denis Jacqueryebed5f612013-10-08 19:26:49 +01001162 self.hdmx = dict((sz,dict((g,v) for g,v in l.iteritems() if g in s.glyphs))
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001163 for sz,l in self.hdmx.iteritems())
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001164 return bool(self.hdmx)
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -04001165
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001166@_add_method(ttLib.getTableClass('VORG'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001167def subset_glyphs(self, s):
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001168 self.VOriginRecords = dict((g,v) for g,v in self.VOriginRecords.iteritems()
1169 if g in s.glyphs)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001170 self.numVertOriginYMetrics = len(self.VOriginRecords)
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001171 return True # Never drop; has default metrics
Behdad Esfahbode45d6af2013-07-22 15:29:17 -04001172
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001173@_add_method(ttLib.getTableClass('post'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001174def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001175 if not options.glyph_names:
1176 self.formatType = 3.0
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001177 return True # Required table
Behdad Esfahbod42648242013-07-23 12:56:06 -04001178
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001179@_add_method(ttLib.getTableClass('post'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001180def subset_glyphs(self, s):
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001181 self.extraNames = [] # This seems to do it
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001182 return True # Required table
Behdad Esfahbod653e9742013-07-22 15:17:12 -04001183
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001184@_add_method(ttLib.getTableModule('glyf').Glyph)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001185def remapComponentsFast(self, indices):
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001186 if not self.data or struct.unpack(">h", self.data[:2])[0] >= 0:
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001187 return # Not composite
Behdad Esfahbodd816b7f2013-08-16 16:18:40 -04001188 data = array.array("B", self.data)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001189 i = 10
1190 more = 1
1191 while more:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001192 flags =(data[i] << 8) | data[i+1]
1193 glyphID =(data[i+2] << 8) | data[i+3]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001194 # Remap
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001195 glyphID = indices.index(glyphID)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001196 data[i+2] = glyphID >> 8
1197 data[i+3] = glyphID & 0xFF
1198 i += 4
1199 flags = int(flags)
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -04001200
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04001201 if flags & 0x0001: i += 4 # ARG_1_AND_2_ARE_WORDS
Behdad Esfahbod574ce792013-08-13 20:51:44 -04001202 else: i += 2
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04001203 if flags & 0x0008: i += 2 # WE_HAVE_A_SCALE
1204 elif flags & 0x0040: i += 4 # WE_HAVE_AN_X_AND_Y_SCALE
1205 elif flags & 0x0080: i += 8 # WE_HAVE_A_TWO_BY_TWO
1206 more = flags & 0x0020 # MORE_COMPONENTS
1207
Behdad Esfahbodd816b7f2013-08-16 16:18:40 -04001208 self.data = data.tostring()
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -04001209
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001210@_add_method(ttLib.getTableClass('glyf'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001211def closure_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001212 decompose = s.glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001213 while True:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001214 components = set()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001215 for g in decompose:
1216 if g not in self.glyphs:
1217 continue
1218 gl = self.glyphs[g]
Behdad Esfahbod043108c2013-09-27 12:59:47 -04001219 for c in gl.getComponentNames(self):
Behdad Esfahbod626107c2013-09-20 14:10:31 -04001220 if c not in s.glyphs:
1221 components.add(c)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001222 components = set(c for c in components if c not in s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001223 if not components:
1224 break
1225 decompose = components
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001226 s.glyphs.update(components)
Behdad Esfahbodabb50a12013-07-23 12:58:37 -04001227
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001228@_add_method(ttLib.getTableClass('glyf'))
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001229def prune_pre_subset(self, options):
1230 if options.notdef_glyph and not options.notdef_outline:
1231 g = self[self.glyphOrder[0]]
1232 # Yay, easy!
1233 g.__dict__.clear()
1234 g.data = ""
1235 return True
1236
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001237@_add_method(ttLib.getTableClass('glyf'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001238def subset_glyphs(self, s):
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001239 self.glyphs = dict((g,v) for g,v in self.glyphs.iteritems() if g in s.glyphs)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001240 indices = [i for i,g in enumerate(self.glyphOrder) if g in s.glyphs]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001241 for v in self.glyphs.itervalues():
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001242 if hasattr(v, "data"):
1243 v.remapComponentsFast(indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001244 else:
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001245 pass # No need
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001246 self.glyphOrder = [g for g in self.glyphOrder if g in s.glyphs]
Behdad Esfahbodb69b6712013-08-29 18:17:31 -04001247 # Don't drop empty 'glyf' tables, otherwise 'loca' doesn't get subset.
1248 return True
Behdad Esfahbod861d9152013-07-22 16:47:24 -04001249
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001250@_add_method(ttLib.getTableClass('glyf'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001251def prune_post_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001252 if not options.hinting:
1253 for v in self.glyphs.itervalues():
Behdad Esfahbod626107c2013-09-20 14:10:31 -04001254 v.removeHinting()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001255 return True
Behdad Esfahboded98c612013-07-23 12:37:41 -04001256
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001257@_add_method(ttLib.getTableClass('CFF '))
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001258def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001259 cff = self.cff
Behdad Esfahbode0622072013-09-10 14:33:19 -04001260 # CFF table must have one font only
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001261 cff.fontNames = cff.fontNames[:1]
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001262
1263 if options.notdef_glyph and not options.notdef_outline:
1264 for fontname in cff.keys():
1265 font = cff[fontname]
1266 c,_ = font.CharStrings.getItemAndSelector('.notdef')
Behdad Esfahbod21582e92013-09-12 16:47:52 -04001267 # XXX we should preserve the glyph width
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001268 c.bytecode = '\x0e' # endchar
1269 c.program = None
1270
Behdad Esfahbod50f83ef2013-08-29 18:18:17 -04001271 return True # bool(cff.fontNames)
Behdad Esfahbod1a4e72e2013-08-13 15:46:37 -04001272
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001273@_add_method(ttLib.getTableClass('CFF '))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001274def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001275 cff = self.cff
1276 for fontname in cff.keys():
1277 font = cff[fontname]
1278 cs = font.CharStrings
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001279
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001280 # Load all glyphs
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001281 for g in font.charset:
1282 if g not in s.glyphs: continue
1283 c,sel = cs.getItemAndSelector(g)
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001284
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001285 if cs.charStringsAreIndexed:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001286 indices = [i for i,g in enumerate(font.charset) if g in s.glyphs]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001287 csi = cs.charStringsIndex
1288 csi.items = [csi.items[i] for i in indices]
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001289 csi.count = len(csi.items)
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001290 del csi.file, csi.offsets
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001291 if hasattr(font, "FDSelect"):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001292 sel = font.FDSelect
1293 sel.format = None
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001294 sel.gidArray = [sel.gidArray[i] for i in indices]
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001295 cs.charStrings = dict((g,indices.index(v))
1296 for g,v in cs.charStrings.iteritems()
1297 if g in s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001298 else:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001299 cs.charStrings = dict((g,v)
1300 for g,v in cs.charStrings.iteritems()
1301 if g in s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001302 font.charset = [g for g in font.charset if g in s.glyphs]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001303 font.numGlyphs = len(font.charset)
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001304
Behdad Esfahbod50f83ef2013-08-29 18:18:17 -04001305 return True # any(cff[fontname].numGlyphs for fontname in cff.keys())
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001306
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001307@_add_method(psCharStrings.T2CharString)
Behdad Esfahbodb4ea9532013-08-14 19:37:39 -04001308def subset_subroutines(self, subrs, gsubrs):
1309 p = self.program
Behdad Esfahbode0622072013-09-10 14:33:19 -04001310 assert len(p)
Behdad Esfahbod87c8c502013-08-16 14:44:09 -04001311 for i in xrange(1, len(p)):
Behdad Esfahbodb4ea9532013-08-14 19:37:39 -04001312 if p[i] == 'callsubr':
1313 assert type(p[i-1]) is int
1314 p[i-1] = subrs._used.index(p[i-1] + subrs._old_bias) - subrs._new_bias
1315 elif p[i] == 'callgsubr':
1316 assert type(p[i-1]) is int
1317 p[i-1] = gsubrs._used.index(p[i-1] + gsubrs._old_bias) - gsubrs._new_bias
1318
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001319@_add_method(psCharStrings.T2CharString)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001320def drop_hints(self):
1321 hints = self._hints
1322
1323 if hints.has_hint:
1324 self.program = self.program[hints.last_hint:]
Behdad Esfahbod285d7b82013-09-10 20:30:47 -04001325 if hasattr(self, 'width'):
1326 # Insert width back if needed
1327 if self.width != self.private.defaultWidthX:
Behdad Esfahbod99536852013-09-12 00:23:11 -04001328 self.program.insert(0, self.width - self.private.nominalWidthX)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001329
1330 if hints.has_hintmask:
1331 i = 0
1332 p = self.program
1333 while i < len(p):
1334 if p[i] in ['hintmask', 'cntrmask']:
1335 assert i + 1 <= len(p)
1336 del p[i:i+2]
1337 continue
1338 i += 1
1339
Behdad Esfahbod2a70f4a2013-10-28 15:18:07 +01001340 # TODO: we currently don't drop calls to "empty" subroutines.
1341
Behdad Esfahbode0622072013-09-10 14:33:19 -04001342 assert len(self.program)
1343
1344 del self._hints
1345
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001346class _MarkingT2Decompiler(psCharStrings.SimpleT2Decompiler):
Behdad Esfahbode0622072013-09-10 14:33:19 -04001347
1348 def __init__(self, localSubrs, globalSubrs):
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001349 psCharStrings.SimpleT2Decompiler.__init__(self,
1350 localSubrs,
1351 globalSubrs)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001352 for subrs in [localSubrs, globalSubrs]:
1353 if subrs and not hasattr(subrs, "_used"):
1354 subrs._used = set()
1355
1356 def op_callsubr(self, index):
1357 self.localSubrs._used.add(self.operandStack[-1]+self.localBias)
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001358 psCharStrings.SimpleT2Decompiler.op_callsubr(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001359
1360 def op_callgsubr(self, index):
1361 self.globalSubrs._used.add(self.operandStack[-1]+self.globalBias)
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001362 psCharStrings.SimpleT2Decompiler.op_callgsubr(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001363
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001364class _DehintingT2Decompiler(psCharStrings.SimpleT2Decompiler):
Behdad Esfahbode0622072013-09-10 14:33:19 -04001365
1366 class Hints:
1367 def __init__(self):
1368 # Whether calling this charstring produces any hint stems
1369 self.has_hint = False
1370 # Index to start at to drop all hints
1371 self.last_hint = 0
1372 # Index up to which we know more hints are possible. Only
1373 # relevant if status is 0 or 1.
1374 self.last_checked = 0
1375 # The status means:
1376 # 0: after dropping hints, this charstring is empty
1377 # 1: after dropping hints, there may be more hints continuing after this
1378 # 2: no more hints possible after this charstring
1379 self.status = 0
1380 # Has hintmask instructions; not recursive
1381 self.has_hintmask = False
1382 pass
1383
1384 def __init__(self, css, localSubrs, globalSubrs):
1385 self._css = css
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001386 psCharStrings.SimpleT2Decompiler.__init__(self,
1387 localSubrs,
1388 globalSubrs)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001389
1390 def execute(self, charString):
1391 old_hints = charString._hints if hasattr(charString, '_hints') else None
1392 charString._hints = self.Hints()
1393
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001394 psCharStrings.SimpleT2Decompiler.execute(self, charString)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001395
1396 hints = charString._hints
1397
1398 if hints.has_hint or hints.has_hintmask:
1399 self._css.add(charString)
1400
1401 if hints.status != 2:
1402 # Check from last_check, make sure we didn't have any operators.
1403 for i in xrange(hints.last_checked, len(charString.program) - 1):
1404 if type(charString.program[i]) == str:
1405 hints.status = 2
1406 break;
1407 else:
1408 hints.status = 1 # There's *something* here
1409 hints.last_checked = len(charString.program)
1410
1411 if old_hints:
1412 assert hints.__dict__ == old_hints.__dict__
1413
1414 def op_callsubr(self, index):
1415 subr = self.localSubrs[self.operandStack[-1]+self.localBias]
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001416 psCharStrings.SimpleT2Decompiler.op_callsubr(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001417 self.processSubr(index, subr)
1418
1419 def op_callgsubr(self, index):
1420 subr = self.globalSubrs[self.operandStack[-1]+self.globalBias]
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001421 psCharStrings.SimpleT2Decompiler.op_callgsubr(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001422 self.processSubr(index, subr)
1423
1424 def op_hstem(self, index):
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001425 psCharStrings.SimpleT2Decompiler.op_hstem(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001426 self.processHint(index)
1427 def op_vstem(self, index):
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001428 psCharStrings.SimpleT2Decompiler.op_vstem(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001429 self.processHint(index)
1430 def op_hstemhm(self, index):
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001431 psCharStrings.SimpleT2Decompiler.op_hstemhm(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001432 self.processHint(index)
1433 def op_vstemhm(self, index):
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001434 psCharStrings.SimpleT2Decompiler.op_vstemhm(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001435 self.processHint(index)
1436 def op_hintmask(self, index):
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001437 psCharStrings.SimpleT2Decompiler.op_hintmask(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001438 self.processHintmask(index)
1439 def op_cntrmask(self, index):
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001440 psCharStrings.SimpleT2Decompiler.op_cntrmask(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001441 self.processHintmask(index)
1442
1443 def processHintmask(self, index):
1444 cs = self.callingStack[-1]
1445 hints = cs._hints
1446 hints.has_hintmask = True
1447 if hints.status != 2 and hints.has_hint:
1448 # Check from last_check, see if we may be an implicit vstem
Behdad Esfahbod84763142013-09-10 19:00:48 -04001449 for i in xrange(hints.last_checked, index - 1):
Behdad Esfahbode0622072013-09-10 14:33:19 -04001450 if type(cs.program[i]) == str:
Behdad Esfahbod84763142013-09-10 19:00:48 -04001451 hints.status = 2
Behdad Esfahbode0622072013-09-10 14:33:19 -04001452 break;
Behdad Esfahbod84763142013-09-10 19:00:48 -04001453 if hints.status != 2:
Behdad Esfahbode0622072013-09-10 14:33:19 -04001454 # We are an implicit vstem
1455 hints.last_hint = index + 1
Behdad Esfahbod84763142013-09-10 19:00:48 -04001456 hints.status = 0
Behdad Esfahbod2a70f4a2013-10-28 15:18:07 +01001457 hints.last_checked = index + 1
Behdad Esfahbode0622072013-09-10 14:33:19 -04001458
1459 def processHint(self, index):
1460 cs = self.callingStack[-1]
1461 hints = cs._hints
1462 hints.has_hint = True
1463 hints.last_hint = index
1464 hints.last_checked = index
1465
1466 def processSubr(self, index, subr):
1467 cs = self.callingStack[-1]
1468 hints = cs._hints
1469 subr_hints = subr._hints
1470
1471 if subr_hints.has_hint:
Behdad Esfahbod285d7b82013-09-10 20:30:47 -04001472 if hints.status != 2:
1473 hints.has_hint = True
Behdad Esfahbod99536852013-09-12 00:23:11 -04001474 hints.last_checked = index
1475 hints.status = subr_hints.status
Behdad Esfahbod285d7b82013-09-10 20:30:47 -04001476 # Decide where to chop off from
1477 if subr_hints.status == 0:
Behdad Esfahbod99536852013-09-12 00:23:11 -04001478 hints.last_hint = index
Behdad Esfahbod285d7b82013-09-10 20:30:47 -04001479 else:
Behdad Esfahbod99536852013-09-12 00:23:11 -04001480 hints.last_hint = index - 2 # Leave the subr call in
Behdad Esfahbode0622072013-09-10 14:33:19 -04001481 else:
Behdad Esfahbod285d7b82013-09-10 20:30:47 -04001482 # In my understanding, this is a font bug. Ie. it has hint stems
1483 # *after* path construction. I've seen this in widespread fonts.
1484 # Best to ignore the hints I suppose...
1485 pass
1486 #assert 0
Behdad Esfahbode0622072013-09-10 14:33:19 -04001487 else:
1488 hints.status = max(hints.status, subr_hints.status)
1489 if hints.status != 2:
1490 # Check from last_check, make sure we didn't have
1491 # any operators.
1492 for i in xrange(hints.last_checked, index - 1):
1493 if type(cs.program[i]) == str:
1494 hints.status = 2
1495 break;
1496 hints.last_checked = index
Behdad Esfahbod2a70f4a2013-10-28 15:18:07 +01001497 if hints.status != 2:
1498 # Decide where to chop off from
1499 if subr_hints.status == 0:
1500 hints.last_hint = index
1501 else:
1502 hints.last_hint = index - 2 # Leave the subr call in
Behdad Esfahbode0622072013-09-10 14:33:19 -04001503
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001504@_add_method(ttLib.getTableClass('CFF '))
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001505def prune_post_subset(self, options):
1506 cff = self.cff
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001507 for fontname in cff.keys():
1508 font = cff[fontname]
1509 cs = font.CharStrings
1510
Behdad Esfahbode0622072013-09-10 14:33:19 -04001511
1512 #
Behdad Esfahbod3c20a132013-08-14 19:39:00 -04001513 # Drop unused FontDictionaries
Behdad Esfahbode0622072013-09-10 14:33:19 -04001514 #
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001515 if hasattr(font, "FDSelect"):
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001516 sel = font.FDSelect
1517 indices = _uniq_sort(sel.gidArray)
1518 sel.gidArray = [indices.index (ss) for ss in sel.gidArray]
1519 arr = font.FDArray
Behdad Esfahbod3c20a132013-08-14 19:39:00 -04001520 arr.items = [arr[i] for i in indices]
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001521 arr.count = len(arr.items)
1522 del arr.file, arr.offsets
Behdad Esfahbod1a4e72e2013-08-13 15:46:37 -04001523
Behdad Esfahbode0622072013-09-10 14:33:19 -04001524
1525 #
1526 # Drop hints if not needed
1527 #
1528 if not options.hinting:
1529
1530 #
1531 # This can be tricky, but doesn't have to. What we do is:
1532 #
1533 # - Run all used glyph charstrings and recurse into subroutines,
1534 # - For each charstring (including subroutines), if it has any
1535 # of the hint stem operators, we mark it as such. Upon returning,
1536 # for each charstring we note all the subroutine calls it makes
1537 # that (recursively) contain a stem,
1538 # - Dropping hinting then consists of the following two ops:
1539 # * Drop the piece of the program in each charstring before the
1540 # last call to a stem op or a stem-calling subroutine,
1541 # * Drop all hintmask operations.
1542 # - It's trickier... A hintmask right after hints and a few numbers
1543 # will act as an implicit vstemhm. As such, we track whether
1544 # we have seen any non-hint operators so far and do the right
1545 # thing, recursively... Good luck understanding that :(
1546 #
1547 css = set()
1548 for g in font.charset:
1549 c,sel = cs.getItemAndSelector(g)
1550 # Make sure it's decompiled. We want our "decompiler" to walk
1551 # the program, not the bytecode.
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001552 c.draw(basePen.NullPen())
Behdad Esfahbode0622072013-09-10 14:33:19 -04001553 subrs = getattr(c.private, "Subrs", [])
1554 decompiler = _DehintingT2Decompiler(css, subrs, c.globalSubrs)
1555 decompiler.execute(c)
1556 for charstring in css:
1557 charstring.drop_hints()
1558
Behdad Esfahbod16fc3232013-09-30 15:09:27 -04001559 # Drop font-wide hinting values
1560 all_privs = []
1561 if hasattr(font, 'FDSelect'):
1562 all_privs.extend(fd.Private for fd in font.FDArray)
1563 else:
1564 all_privs.append(font.Private)
1565 for priv in all_privs:
Behdad Esfahbod4d99d142013-10-28 13:15:08 +01001566 for k in ['BlueValues', 'OtherBlues', 'FamilyBlues', 'FamilyOtherBlues',
1567 'BlueScale', 'BlueShift', 'BlueFuzz',
1568 'StemSnapH', 'StemSnapV', 'StdHW', 'StdVW']:
Behdad Esfahbod16fc3232013-09-30 15:09:27 -04001569 if hasattr(priv, k):
1570 setattr(priv, k, None)
1571
Behdad Esfahbode0622072013-09-10 14:33:19 -04001572
1573 #
1574 # Renumber subroutines to remove unused ones
1575 #
1576
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001577 # Mark all used subroutines
1578 for g in font.charset:
1579 c,sel = cs.getItemAndSelector(g)
1580 subrs = getattr(c.private, "Subrs", [])
1581 decompiler = _MarkingT2Decompiler(subrs, c.globalSubrs)
1582 decompiler.execute(c)
1583
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001584 all_subrs = [font.GlobalSubrs]
Behdad Esfahbod83f1f5c2013-08-30 16:20:08 -04001585 if hasattr(font, 'FDSelect'):
Behdad Esfahbode0622072013-09-10 14:33:19 -04001586 all_subrs.extend(fd.Private.Subrs for fd in font.FDArray if hasattr(fd.Private, 'Subrs') and fd.Private.Subrs)
1587 elif hasattr(font.Private, 'Subrs') and font.Private.Subrs:
Behdad Esfahbodcbcaccf2013-08-30 16:21:38 -04001588 all_subrs.append(font.Private.Subrs)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001589
1590 subrs = set(subrs) # Remove duplicates
1591
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001592 # Prepare
1593 for subrs in all_subrs:
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001594 if not hasattr(subrs, '_used'):
1595 subrs._used = set()
1596 subrs._used = _uniq_sort(subrs._used)
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001597 subrs._old_bias = psCharStrings.calcSubrBias(subrs)
1598 subrs._new_bias = psCharStrings.calcSubrBias(subrs._used)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001599
Behdad Esfahboded107712013-08-14 19:54:13 -04001600 # Renumber glyph charstrings
1601 for g in font.charset:
1602 c,sel = cs.getItemAndSelector(g)
1603 subrs = getattr(c.private, "Subrs", [])
1604 c.subset_subroutines (subrs, font.GlobalSubrs)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001605
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001606 # Renumber subroutines themselves
1607 for subrs in all_subrs:
Behdad Esfahbode0622072013-09-10 14:33:19 -04001608
1609 if subrs == font.GlobalSubrs:
1610 if not hasattr(font, 'FDSelect') and hasattr(font.Private, 'Subrs'):
1611 local_subrs = font.Private.Subrs
Behdad Esfahbod83f1f5c2013-08-30 16:20:08 -04001612 else:
Behdad Esfahbode0622072013-09-10 14:33:19 -04001613 local_subrs = []
1614 else:
1615 local_subrs = subrs
1616
1617 subrs.items = [subrs.items[i] for i in subrs._used]
1618 subrs.count = len(subrs.items)
1619 del subrs.file
1620 if hasattr(subrs, 'offsets'):
1621 del subrs.offsets
1622
1623 for i in xrange (subrs.count):
Behdad Esfahbod83f1f5c2013-08-30 16:20:08 -04001624 subrs[i].subset_subroutines (local_subrs, font.GlobalSubrs)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001625
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001626 # Cleanup
1627 for subrs in all_subrs:
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001628 del subrs._used, subrs._old_bias, subrs._new_bias
1629
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001630 return True
Behdad Esfahbod2b677c82013-07-23 13:37:13 -04001631
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001632@_add_method(ttLib.getTableClass('cmap'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001633def closure_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001634 tables = [t for t in self.tables
1635 if t.platformID == 3 and t.platEncID in [1, 10]]
1636 for u in s.unicodes_requested:
1637 found = False
1638 for table in tables:
1639 if u in table.cmap:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001640 s.glyphs.add(table.cmap[u])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001641 found = True
1642 break
1643 if not found:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001644 s.log("No glyph for Unicode value %s; skipping." % u)
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04001645
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001646@_add_method(ttLib.getTableClass('cmap'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001647def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001648 if not options.legacy_cmap:
1649 # Drop non-Unicode / non-Symbol cmaps
1650 self.tables = [t for t in self.tables
1651 if t.platformID == 3 and t.platEncID in [0, 1, 10]]
1652 if not options.symbol_cmap:
1653 self.tables = [t for t in self.tables
1654 if t.platformID == 3 and t.platEncID in [1, 10]]
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001655 # TODO(behdad) Only keep one subtable?
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001656 # For now, drop format=0 which can't be subset_glyphs easily?
1657 self.tables = [t for t in self.tables if t.format != 0]
Behdad Esfahbodfd92d4c2013-09-19 19:43:09 -04001658 self.numSubTables = len(self.tables)
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001659 return True # Required table
Behdad Esfahbodabb50a12013-07-23 12:58:37 -04001660
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001661@_add_method(ttLib.getTableClass('cmap'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001662def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001663 s.glyphs = s.glyphs_cmaped
1664 for t in self.tables:
1665 # For reasons I don't understand I need this here
1666 # to force decompilation of the cmap format 14.
1667 try:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001668 getattr(t, "asdf")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001669 except AttributeError:
1670 pass
1671 if t.format == 14:
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001672 # TODO(behdad) XXX We drop all the default-UVS mappings(g==None).
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001673 t.uvsDict = dict((v,[(u,g) for u,g in l if g in s.glyphs])
1674 for v,l in t.uvsDict.iteritems())
1675 t.uvsDict = dict((v,l) for v,l in t.uvsDict.iteritems() if l)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001676 else:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001677 t.cmap = dict((u,g) for u,g in t.cmap.iteritems()
1678 if g in s.glyphs_requested or u in s.unicodes_requested)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001679 self.tables = [t for t in self.tables
Behdad Esfahbod4734be52013-08-14 19:47:42 -04001680 if (t.cmap if t.format != 14 else t.uvsDict)]
Behdad Esfahbodfd92d4c2013-09-19 19:43:09 -04001681 self.numSubTables = len(self.tables)
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001682 # TODO(behdad) Convert formats when needed.
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001683 # In particular, if we have a format=12 without non-BMP
1684 # characters, either drop format=12 one or convert it
1685 # to format=4 if there's not one.
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001686 return True # Required table
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001687
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001688@_add_method(ttLib.getTableClass('name'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001689def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001690 if '*' not in options.name_IDs:
1691 self.names = [n for n in self.names if n.nameID in options.name_IDs]
1692 if not options.name_legacy:
1693 self.names = [n for n in self.names
1694 if n.platformID == 3 and n.platEncID == 1]
1695 if '*' not in options.name_languages:
1696 self.names = [n for n in self.names if n.langID in options.name_languages]
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001697 return True # Required table
Behdad Esfahbod653e9742013-07-22 15:17:12 -04001698
Behdad Esfahbod8c646f62013-07-22 15:06:23 -04001699
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001700# TODO(behdad) OS/2 ulUnicodeRange / ulCodePageRange?
Behdad Esfahbod26560d22013-10-26 22:03:35 +02001701# TODO(behdad) Drop AAT tables.
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001702# TODO(behdad) Drop unneeded GSUB/GPOS Script/LangSys entries.
Behdad Esfahbod852e8a52013-08-29 18:19:22 -04001703# TODO(behdad) Drop empty GSUB/GPOS, and GDEF if no GSUB/GPOS left
1704# TODO(behdad) Drop GDEF subitems if unused by lookups
Behdad Esfahbod10195332013-08-14 19:55:24 -04001705# TODO(behdad) Avoid recursing too much (in GSUB/GPOS and in CFF)
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001706# TODO(behdad) Text direction considerations.
1707# TODO(behdad) Text script / language considerations.
Behdad Esfahbod8c486d82013-07-24 13:34:47 -04001708
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001709class Options(object):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001710
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001711 class UnknownOptionError(Exception):
1712 pass
Behdad Esfahbod26d9ee72013-08-13 16:55:01 -04001713
Behdad Esfahboda17743f2013-08-28 17:14:53 -04001714 _drop_tables_default = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', 'SVG ',
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001715 'PCLT', 'LTSH']
1716 _drop_tables_default += ['Feat', 'Glat', 'Gloc', 'Silf', 'Sill'] # Graphite
1717 _drop_tables_default += ['CBLC', 'CBDT', 'sbix', 'COLR', 'CPAL'] # Color
1718 _no_subset_tables_default = ['gasp', 'head', 'hhea', 'maxp', 'vhea', 'OS/2',
1719 'loca', 'name', 'cvt ', 'fpgm', 'prep']
1720 _hinting_tables_default = ['cvt ', 'fpgm', 'prep', 'hdmx', 'VDMX']
Behdad Esfahbod26d9ee72013-08-13 16:55:01 -04001721
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001722 # Based on HarfBuzz shapers
1723 _layout_features_groups = {
1724 # Default shaper
1725 'common': ['ccmp', 'liga', 'locl', 'mark', 'mkmk', 'rlig'],
1726 'horizontal': ['calt', 'clig', 'curs', 'kern', 'rclt'],
1727 'vertical': ['valt', 'vert', 'vkrn', 'vpal', 'vrt2'],
1728 'ltr': ['ltra', 'ltrm'],
1729 'rtl': ['rtla', 'rtlm'],
1730 # Complex shapers
1731 'arabic': ['init', 'medi', 'fina', 'isol', 'med2', 'fin2', 'fin3',
1732 'cswh', 'mset'],
1733 'hangul': ['ljmo', 'vjmo', 'tjmo'],
Behdad Esfahbod3977d3e2013-10-14 17:49:12 +02001734 'tibetan': ['abvs', 'blws', 'abvm', 'blwm'],
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001735 'indic': ['nukt', 'akhn', 'rphf', 'rkrf', 'pref', 'blwf', 'half',
1736 'abvf', 'pstf', 'cfar', 'vatu', 'cjct', 'init', 'pres',
1737 'abvs', 'blws', 'psts', 'haln', 'dist', 'abvm', 'blwm'],
1738 }
1739 _layout_features_default = _uniq_sort(sum(
1740 _layout_features_groups.itervalues(), []))
Behdad Esfahbod9eeeb4e2013-08-13 16:58:50 -04001741
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001742 drop_tables = _drop_tables_default
1743 no_subset_tables = _no_subset_tables_default
1744 hinting_tables = _hinting_tables_default
1745 layout_features = _layout_features_default
Behdad Esfahbodfe6bc4c2013-11-02 11:10:23 +00001746 hinting = True
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001747 glyph_names = False
1748 legacy_cmap = False
1749 symbol_cmap = False
1750 name_IDs = [1, 2] # Family and Style
1751 name_legacy = False
1752 name_languages = [0x0409] # English
Behdad Esfahbod04f3a192013-08-29 16:56:06 -04001753 notdef_glyph = True # gid0 for TrueType / .notdef for CFF
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001754 notdef_outline = False # No need for notdef to have an outline really
Behdad Esfahbod04f3a192013-08-29 16:56:06 -04001755 recommended_glyphs = False # gid1, gid2, gid3 for TrueType
Behdad Esfahbode911de12013-08-16 12:42:34 -04001756 recalc_bounds = False # Recalculate font bounding boxes
Behdad Esfahbod03d78da2013-08-29 16:42:00 -04001757 canonical_order = False # Order tables as recommended
Behdad Esfahbodc6c3bb82013-08-15 17:46:20 -04001758 flavor = None # May be 'woff'
Behdad Esfahbod9eeeb4e2013-08-13 16:58:50 -04001759
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001760 def __init__(self, **kwargs):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001761
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001762 self.set(**kwargs)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001763
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001764 def set(self, **kwargs):
1765 for k,v in kwargs.iteritems():
1766 if not hasattr(self, k):
Behdad Esfahbodac10d812013-09-03 18:29:58 -04001767 raise self.UnknownOptionError("Unknown option '%s'" % k)
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001768 setattr(self, k, v)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001769
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001770 def parse_opts(self, argv, ignore_unknown=False):
1771 ret = []
1772 opts = {}
1773 for a in argv:
1774 orig_a = a
1775 if not a.startswith('--'):
1776 ret.append(a)
1777 continue
1778 a = a[2:]
1779 i = a.find('=')
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001780 op = '='
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001781 if i == -1:
1782 if a.startswith("no-"):
1783 k = a[3:]
1784 v = False
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001785 else:
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001786 k = a
1787 v = True
1788 else:
1789 k = a[:i]
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001790 if k[-1] in "-+":
1791 op = k[-1]+'=' # Ops is '-=' or '+=' now.
1792 k = k[:-1]
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001793 v = a[i+1:]
1794 k = k.replace('-', '_')
1795 if not hasattr(self, k):
1796 if ignore_unknown == True or k in ignore_unknown:
1797 ret.append(orig_a)
1798 continue
1799 else:
1800 raise self.UnknownOptionError("Unknown option '%s'" % a)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001801
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001802 ov = getattr(self, k)
1803 if isinstance(ov, bool):
1804 v = bool(v)
1805 elif isinstance(ov, int):
1806 v = int(v)
1807 elif isinstance(ov, list):
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001808 vv = v.split(',')
1809 if vv == ['']:
1810 vv = []
Behdad Esfahbod87c8c502013-08-16 14:44:09 -04001811 vv = [int(x, 0) if len(x) and x[0] in "0123456789" else x for x in vv]
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001812 if op == '=':
1813 v = vv
1814 elif op == '+=':
1815 v = ov
1816 v.extend(vv)
1817 elif op == '-=':
1818 v = ov
1819 for x in vv:
1820 if x in v:
1821 v.remove(x)
1822 else:
1823 assert 0
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001824
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001825 opts[k] = v
1826 self.set(**opts)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001827
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001828 return ret
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001829
1830
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001831class Subsetter(object):
1832
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001833 def __init__(self, options=None, log=None):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001834
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001835 if not log:
1836 log = Logger()
1837 if not options:
1838 options = Options()
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001839
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001840 self.options = options
1841 self.log = log
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001842 self.unicodes_requested = set()
1843 self.glyphs_requested = set()
1844 self.glyphs = set()
Behdad Esfahbod3d513b72013-07-31 14:11:40 -04001845
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001846 def populate(self, glyphs=[], unicodes=[], text=""):
1847 self.unicodes_requested.update(unicodes)
1848 if isinstance(text, str):
1849 text = text.decode("utf8")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001850 for u in text:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001851 self.unicodes_requested.add(ord(u))
1852 self.glyphs_requested.update(glyphs)
1853 self.glyphs.update(glyphs)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -04001854
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04001855 def _prune_pre_subset(self, font):
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001856
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001857 for tag in font.keys():
1858 if tag == 'GlyphOrder': continue
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001859
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001860 if(tag in self.options.drop_tables or
1861 (tag in self.options.hinting_tables and not self.options.hinting)):
1862 self.log(tag, "dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001863 del font[tag]
1864 continue
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001865
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001866 clazz = ttLib.getTableClass(tag)
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001867
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001868 if hasattr(clazz, 'prune_pre_subset'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001869 table = font[tag]
Behdad Esfahbod010c5f92013-09-10 20:54:46 -04001870 self.log.lapse("load '%s'" % tag)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001871 retain = table.prune_pre_subset(self.options)
1872 self.log.lapse("prune '%s'" % tag)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001873 if not retain:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001874 self.log(tag, "pruned to empty; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001875 del font[tag]
1876 continue
1877 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001878 self.log(tag, "pruned")
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001879
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04001880 def _closure_glyphs(self, font):
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001881
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001882 self.glyphs = self.glyphs_requested.copy()
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001883
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001884 if 'cmap' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001885 font['cmap'].closure_glyphs(self)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001886 self.glyphs_cmaped = self.glyphs
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001887
Behdad Esfahbod04f3a192013-08-29 16:56:06 -04001888 if self.options.notdef_glyph:
1889 if 'glyf' in font:
1890 self.glyphs.add(font.getGlyphName(0))
1891 self.log("Added gid0 to subset")
1892 else:
1893 self.glyphs.add('.notdef')
1894 self.log("Added .notdef to subset")
1895 if self.options.recommended_glyphs:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001896 if 'glyf' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001897 for i in range(4):
1898 self.glyphs.add(font.getGlyphName(i))
1899 self.log("Added first four glyphs to subset")
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001900
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001901 if 'GSUB' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001902 self.log("Closing glyph list over 'GSUB': %d glyphs before" %
1903 len(self.glyphs))
1904 self.log.glyphs(self.glyphs, font=font)
1905 font['GSUB'].closure_glyphs(self)
1906 self.log("Closed glyph list over 'GSUB': %d glyphs after" %
1907 len(self.glyphs))
1908 self.log.glyphs(self.glyphs, font=font)
1909 self.log.lapse("close glyph list over 'GSUB'")
1910 self.glyphs_gsubed = self.glyphs.copy()
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001911
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001912 if 'glyf' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001913 self.log("Closing glyph list over 'glyf': %d glyphs before" %
1914 len(self.glyphs))
1915 self.log.glyphs(self.glyphs, font=font)
1916 font['glyf'].closure_glyphs(self)
1917 self.log("Closed glyph list over 'glyf': %d glyphs after" %
1918 len(self.glyphs))
1919 self.log.glyphs(self.glyphs, font=font)
1920 self.log.lapse("close glyph list over 'glyf'")
1921 self.glyphs_glyfed = self.glyphs.copy()
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001922
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001923 self.glyphs_all = self.glyphs.copy()
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04001924
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001925 self.log("Retaining %d glyphs: " % len(self.glyphs_all))
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001926
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04001927 def _subset_glyphs(self, font):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001928 for tag in font.keys():
1929 if tag == 'GlyphOrder': continue
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001930 clazz = ttLib.getTableClass(tag)
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001931
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001932 if tag in self.options.no_subset_tables:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001933 self.log(tag, "subsetting not needed")
1934 elif hasattr(clazz, 'subset_glyphs'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001935 table = font[tag]
1936 self.glyphs = self.glyphs_all
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001937 retain = table.subset_glyphs(self)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001938 self.glyphs = self.glyphs_all
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001939 self.log.lapse("subset '%s'" % tag)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001940 if not retain:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001941 self.log(tag, "subsetted to empty; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001942 del font[tag]
1943 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001944 self.log(tag, "subsetted")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001945 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001946 self.log(tag, "NOT subset; don't know how to subset; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001947 del font[tag]
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001948
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001949 glyphOrder = font.getGlyphOrder()
1950 glyphOrder = [g for g in glyphOrder if g in self.glyphs_all]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001951 font.setGlyphOrder(glyphOrder)
1952 font._buildReverseGlyphOrderDict()
1953 self.log.lapse("subset GlyphOrder")
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001954
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04001955 def _prune_post_subset(self, font):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001956 for tag in font.keys():
1957 if tag == 'GlyphOrder': continue
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001958 clazz = ttLib.getTableClass(tag)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001959 if hasattr(clazz, 'prune_post_subset'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001960 table = font[tag]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001961 retain = table.prune_post_subset(self.options)
1962 self.log.lapse("prune '%s'" % tag)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001963 if not retain:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001964 self.log(tag, "pruned to empty; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001965 del font[tag]
1966 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001967 self.log(tag, "pruned")
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001968
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001969 def subset(self, font):
Behdad Esfahbod756af492013-08-01 12:05:26 -04001970
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04001971 self._prune_pre_subset(font)
1972 self._closure_glyphs(font)
1973 self._subset_glyphs(font)
1974 self._prune_post_subset(font)
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001975
Behdad Esfahbod756af492013-08-01 12:05:26 -04001976
Behdad Esfahbod3d4c4712013-08-13 20:10:17 -04001977class Logger(object):
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001978
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001979 def __init__(self, verbose=False, xml=False, timing=False):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001980 self.verbose = verbose
1981 self.xml = xml
1982 self.timing = timing
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001983 self.last_time = self.start_time = time.time()
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001984
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001985 def parse_opts(self, argv):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001986 argv = argv[:]
1987 for v in ['verbose', 'xml', 'timing']:
1988 if "--"+v in argv:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001989 setattr(self, v, True)
1990 argv.remove("--"+v)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001991 return argv
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001992
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001993 def __call__(self, *things):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001994 if not self.verbose:
1995 return
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001996 print ' '.join(str(x) for x in things)
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001997
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001998 def lapse(self, *things):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001999 if not self.timing:
2000 return
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002001 new_time = time.time()
2002 print "Took %0.3fs to %s" %(new_time - self.last_time,
2003 ' '.join(str(x) for x in things))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002004 self.last_time = new_time
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04002005
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04002006 def glyphs(self, glyphs, font=None):
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002007 self("Names: ", sorted(glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002008 if font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002009 reverseGlyphMap = font.getReverseGlyphMap()
2010 self("Gids : ", sorted(reverseGlyphMap[g] for g in glyphs))
Behdad Esfahbodf5497842013-08-08 21:57:02 -04002011
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002012 def font(self, font, file=sys.stdout):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002013 if not self.xml:
2014 return
Behdad Esfahbod28fc4982013-09-18 19:01:16 -04002015 from fontTools.misc import xmlWriter
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002016 writer = xmlWriter.XMLWriter(file)
Behdad Esfahbod45a84602013-08-19 14:44:49 -04002017 font.disassembleInstructions = False # Work around ttLib bug
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002018 for tag in font.keys():
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002019 writer.begintag(tag)
2020 writer.newline()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002021 font[tag].toXML(writer, font)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002022 writer.endtag(tag)
2023 writer.newline()
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04002024
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002025
Behdad Esfahbod85da2682013-08-15 12:17:21 -04002026def load_font(fontFile,
Behdad Esfahbodadc47fd2013-08-15 18:29:25 -04002027 options,
Behdad Esfahbod85da2682013-08-15 12:17:21 -04002028 checkChecksums=False,
Behdad Esfahbod85da2682013-08-15 12:17:21 -04002029 dontLoadGlyphNames=False):
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002030
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04002031 font = ttLib.TTFont(fontFile,
Behdad Esfahbod45a84602013-08-19 14:44:49 -04002032 checkChecksums=checkChecksums,
2033 recalcBBoxes=options.recalc_bounds)
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002034
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002035 # Hack:
2036 #
2037 # If we don't need glyph names, change 'post' class to not try to
2038 # load them. It avoid lots of headache with broken fonts as well
2039 # as loading time.
2040 #
2041 # Ideally ttLib should provide a way to ask it to skip loading
2042 # glyph names. But it currently doesn't provide such a thing.
2043 #
Behdad Esfahbod85da2682013-08-15 12:17:21 -04002044 if dontLoadGlyphNames:
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04002045 post = ttLib.getTableClass('post')
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002046 saved = post.decode_format_2_0
2047 post.decode_format_2_0 = post.decode_format_3_0
2048 f = font['post']
2049 if f.formatType == 2.0:
2050 f.formatType = 3.0
2051 post.decode_format_2_0 = saved
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002052
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002053 return font
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002054
Behdad Esfahbode911de12013-08-16 12:42:34 -04002055def save_font(font, outfile, options):
Behdad Esfahbodc6c3bb82013-08-15 17:46:20 -04002056 if options.flavor and not hasattr(font, 'flavor'):
2057 raise Exception("fonttools version does not support flavors.")
2058 font.flavor = options.flavor
Behdad Esfahbode911de12013-08-16 12:42:34 -04002059 font.save(outfile, reorderTables=options.canonical_order)
Behdad Esfahbod41de4cc2013-08-15 12:09:55 -04002060
Behdad Esfahbodb69400f2013-08-29 18:40:53 -04002061def main(args):
Behdad Esfahbod610b0552013-07-23 14:52:18 -04002062
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002063 log = Logger()
2064 args = log.parse_opts(args)
Behdad Esfahbod4ae81712013-07-22 11:57:13 -04002065
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04002066 options = Options()
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002067 args = options.parse_opts(args, ignore_unknown=['text'])
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04002068
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002069 if len(args) < 2:
Behdad Esfahbodb69400f2013-08-29 18:40:53 -04002070 print >>sys.stderr, "usage: pyftsubset font-file glyph... [--text=ABC]... [--option=value]..."
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002071 sys.exit(1)
Behdad Esfahbod02b92062013-07-21 18:40:59 -04002072
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002073 fontfile = args[0]
2074 args = args[1:]
Behdad Esfahbod02b92062013-07-21 18:40:59 -04002075
Behdad Esfahbod85da2682013-08-15 12:17:21 -04002076 dontLoadGlyphNames =(not options.glyph_names and
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002077 all(any(g.startswith(p)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002078 for p in ['gid', 'glyph', 'uni', 'U+'])
2079 for g in args))
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002080
Behdad Esfahbodadc47fd2013-08-15 18:29:25 -04002081 font = load_font(fontfile, options, dontLoadGlyphNames=dontLoadGlyphNames)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002082 subsetter = Subsetter(options=options, log=log)
2083 log.lapse("load font")
Behdad Esfahbod02b92062013-07-21 18:40:59 -04002084
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002085 names = font.getGlyphNames()
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002086 log.lapse("loading glyph names")
Behdad Esfahbode7f5a892013-07-31 19:58:59 -04002087
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002088 glyphs = []
2089 unicodes = []
2090 text = ""
2091 for g in args:
Behdad Esfahbod2be33d92013-09-10 19:28:59 -04002092 if g == '*':
2093 glyphs.extend(font.getGlyphOrder())
2094 continue
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002095 if g in names:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002096 glyphs.append(g)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002097 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002098 if g.startswith('--text='):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002099 text += g[7:]
2100 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002101 if g.startswith('uni') or g.startswith('U+'):
2102 if g.startswith('uni') and len(g) > 3:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002103 g = g[3:]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002104 elif g.startswith('U+') and len(g) > 2:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002105 g = g[2:]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002106 u = int(g, 16)
2107 unicodes.append(u)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002108 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002109 if g.startswith('gid') or g.startswith('glyph'):
2110 if g.startswith('gid') and len(g) > 3:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002111 g = g[3:]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002112 elif g.startswith('glyph') and len(g) > 5:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002113 g = g[5:]
2114 try:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002115 glyphs.append(font.getGlyphName(int(g), requireReal=1))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002116 except ValueError:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002117 raise Exception("Invalid glyph identifier: %s" % g)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002118 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002119 raise Exception("Invalid glyph identifier: %s" % g)
2120 log.lapse("compile glyph list")
2121 log("Unicodes:", unicodes)
2122 log("Glyphs:", glyphs)
Behdad Esfahbod6df089a2013-07-31 19:27:14 -04002123
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002124 subsetter.populate(glyphs=glyphs, unicodes=unicodes, text=text)
2125 subsetter.subset(font)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -04002126
Behdad Esfahbod34426c12013-08-14 18:30:09 -04002127 outfile = fontfile + '.subset'
2128
Behdad Esfahbodc6c3bb82013-08-15 17:46:20 -04002129 save_font (font, outfile, options)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002130 log.lapse("compile and save font")
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04002131
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002132 log.last_time = log.start_time
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002133 log.lapse("make one with everything(TOTAL TIME)")
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04002134
Behdad Esfahbod34426c12013-08-14 18:30:09 -04002135 if log.verbose:
2136 import os
2137 log("Input font: %d bytes" % os.path.getsize(fontfile))
2138 log("Subset font: %d bytes" % os.path.getsize(outfile))
2139
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002140 log.font(font)
Behdad Esfahbod8c486d82013-07-24 13:34:47 -04002141
Behdad Esfahbodc56bf482013-08-13 20:13:33 -04002142 font.close()
2143
Behdad Esfahbod39a39ac2013-08-22 18:10:17 -04002144
2145__all__ = [
Behdad Esfahbodb69400f2013-08-29 18:40:53 -04002146 'Options',
2147 'Subsetter',
2148 'Logger',
2149 'load_font',
2150 'save_font',
2151 'main'
Behdad Esfahbod39a39ac2013-08-22 18:10:17 -04002152]
2153
Behdad Esfahbod8c486d82013-07-24 13:34:47 -04002154if __name__ == '__main__':
Behdad Esfahbodb69400f2013-08-29 18:40:53 -04002155 main(sys.argv[1:])