blob: bfc5f9b497611be3a5c1adaa43863fb6b031ce5e [file] [log] [blame]
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001# Copyright 2013 Google, Inc. All Rights Reserved.
2#
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04003# Licensed under the Apache License, Version 2.0(the "License");
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04004# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#
15# Google Author(s): Behdad Esfahbod
Behdad Esfahbod616d36e2013-08-13 20:02:59 -040016
17"""Python OpenType Layout Subsetter.
18
19Later grown into full OpenType subsetter, supporting all standard tables.
20"""
21
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040022import sys
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -040023import struct
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040024import time
Behdad Esfahbodd816b7f2013-08-16 16:18:40 -040025import array
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040026
Behdad Esfahbod45a84602013-08-19 14:44:49 -040027import fontTools.ttLib
28import fontTools.ttLib.tables
29import fontTools.ttLib.tables.otTables
30import fontTools.cffLib
31import fontTools.misc.psCharStrings
Behdad Esfahbod285d7b82013-09-10 20:30:47 -040032import fontTools.pens.basePen
Behdad Esfahbod54660612013-07-21 18:16:55 -040033
Behdad Esfahbod54660612013-07-21 18:16:55 -040034
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040035def _add_method(*clazzes):
Behdad Esfahbod616d36e2013-08-13 20:02:59 -040036 """Returns a decorator function that adds a new method to one or
37 more classes."""
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040038 def wrapper(method):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040039 for clazz in clazzes:
40 assert clazz.__name__ != 'DefaultTable', 'Oops, table class not found.'
Behdad Esfahbode7a0d562013-08-16 10:56:30 -040041 assert not hasattr(clazz, method.func_name), \
Behdad Esfahbodd77f1572013-08-15 19:24:36 -040042 "Oops, class '%s' has method '%s'." % (clazz.__name__,
43 method.func_name)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040044 setattr(clazz, method.func_name, method)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040045 return None
46 return wrapper
Behdad Esfahbod54660612013-07-21 18:16:55 -040047
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040048def _uniq_sort(l):
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040049 return sorted(set(l))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -040050
Behdad Esfahbod42d4f2b2013-08-16 16:16:22 -040051def _set_update(s, *others):
52 # Jython's set.update only takes one other argument.
53 # Emulate real set.update...
54 for other in others:
55 s.update(other)
56
Behdad Esfahbod78661bb2013-07-23 10:23:42 -040057
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040058@_add_method(fontTools.ttLib.tables.otTables.Coverage)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040059def intersect(self, glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040060 "Returns ascending list of matching coverage values."
Behdad Esfahbod4734be52013-08-14 19:47:42 -040061 return [i for i,g in enumerate(self.glyphs) if g in glyphs]
Behdad Esfahbod610b0552013-07-23 14:52:18 -040062
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040063@_add_method(fontTools.ttLib.tables.otTables.Coverage)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040064def intersect_glyphs(self, glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040065 "Returns set of intersecting glyphs."
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040066 return set(g for g in self.glyphs if g in glyphs)
Behdad Esfahbod849d25c2013-08-12 19:24:24 -040067
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040068@_add_method(fontTools.ttLib.tables.otTables.Coverage)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040069def subset(self, glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040070 "Returns ascending list of remaining coverage values."
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040071 indices = self.intersect(glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040072 self.glyphs = [g for g in self.glyphs if g in glyphs]
73 return indices
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040074
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040075@_add_method(fontTools.ttLib.tables.otTables.Coverage)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040076def remap(self, coverage_map):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040077 "Remaps coverage."
78 self.glyphs = [self.glyphs[i] for i in coverage_map]
Behdad Esfahbod14374262013-08-08 22:26:49 -040079
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040080@_add_method(fontTools.ttLib.tables.otTables.ClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040081def intersect(self, glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040082 "Returns ascending list of matching class values."
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040083 return _uniq_sort(
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040084 ([0] if any(g not in self.classDefs for g in glyphs) else []) +
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040085 [v for g,v in self.classDefs.iteritems() if g in glyphs])
Behdad Esfahbodb8d55882013-07-23 22:17:39 -040086
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040087@_add_method(fontTools.ttLib.tables.otTables.ClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040088def intersect_class(self, glyphs, klass):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040089 "Returns set of glyphs matching class."
90 if klass == 0:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040091 return set(g for g in glyphs if g not in self.classDefs)
92 return set(g for g,v in self.classDefs.iteritems()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040093 if v == klass and g in glyphs)
Behdad Esfahbodb8d55882013-07-23 22:17:39 -040094
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040095@_add_method(fontTools.ttLib.tables.otTables.ClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040096def subset(self, glyphs, remap=False):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040097 "Returns ascending list of remaining classes."
Behdad Esfahbodd73f2252013-08-16 10:58:25 -040098 self.classDefs = dict((g,v) for g,v in self.classDefs.iteritems() if g in glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040099 # Note: while class 0 has the special meaning of "not matched",
100 # if no glyph will ever /not match/, we can optimize class 0 out too.
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400101 indices = _uniq_sort(
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400102 ([0] if any(g not in self.classDefs for g in glyphs) else []) +
Behdad Esfahbod4e5d9672013-08-14 19:49:53 -0400103 self.classDefs.values())
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400104 if remap:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400105 self.remap(indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400106 return indices
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -0400107
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400108@_add_method(fontTools.ttLib.tables.otTables.ClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400109def remap(self, class_map):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400110 "Remaps classes."
Behdad Esfahbodd73f2252013-08-16 10:58:25 -0400111 self.classDefs = dict((g,class_map.index(v))
112 for g,v in self.classDefs.iteritems())
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400113
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400114@_add_method(fontTools.ttLib.tables.otTables.SingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400115def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400116 if cur_glyphs == None: cur_glyphs = s.glyphs
117 if self.Format in [1, 2]:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400118 s.glyphs.update(v for g,v in self.mapping.iteritems() if g in cur_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400119 else:
120 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400121
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400122@_add_method(fontTools.ttLib.tables.otTables.SingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400123def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400124 if self.Format in [1, 2]:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -0400125 self.mapping = dict((g,v) for g,v in self.mapping.iteritems()
126 if g in s.glyphs and v in s.glyphs)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400127 return bool(self.mapping)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400128 else:
129 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400130
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400131@_add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400132def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400133 if cur_glyphs == None: cur_glyphs = s.glyphs
134 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400135 indices = self.Coverage.intersect(cur_glyphs)
Behdad Esfahboda9bfec12013-08-16 16:21:25 -0400136 _set_update(s.glyphs, *(self.Sequence[i].Substitute for i in indices))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400137 else:
138 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400139
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400140@_add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400141def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400142 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400143 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400144 self.Sequence = [self.Sequence[i] for i in indices]
145 # Now drop rules generating glyphs we don't want
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400146 indices = [i for i,seq in enumerate(self.Sequence)
147 if all(sub in s.glyphs for sub in seq.Substitute)]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400148 self.Sequence = [self.Sequence[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400149 self.Coverage.remap(indices)
150 self.SequenceCount = len(self.Sequence)
151 return bool(self.SequenceCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400152 else:
153 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400154
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400155@_add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400156def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400157 if cur_glyphs == None: cur_glyphs = s.glyphs
158 if self.Format == 1:
Behdad Esfahbod42d4f2b2013-08-16 16:16:22 -0400159 _set_update(s.glyphs, *(vlist for g,vlist in self.alternates.iteritems()
160 if g in cur_glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400161 else:
162 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400163
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400164@_add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400165def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400166 if self.Format == 1:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -0400167 self.alternates = dict((g,vlist)
168 for g,vlist in self.alternates.iteritems()
169 if g in s.glyphs and
170 all(v in s.glyphs for v in vlist))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400171 return bool(self.alternates)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400172 else:
173 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400174
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400175@_add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400176def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400177 if cur_glyphs == None: cur_glyphs = s.glyphs
178 if self.Format == 1:
Behdad Esfahbod42d4f2b2013-08-16 16:16:22 -0400179 _set_update(s.glyphs, *([seq.LigGlyph for seq in seqs
180 if all(c in s.glyphs for c in seq.Component)]
181 for g,seqs in self.ligatures.iteritems()
182 if g in cur_glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400183 else:
184 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400185
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400186@_add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400187def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400188 if self.Format == 1:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -0400189 self.ligatures = dict((g,v) for g,v in self.ligatures.iteritems()
190 if g in s.glyphs)
191 self.ligatures = dict((g,[seq for seq in seqs
192 if seq.LigGlyph in s.glyphs and
193 all(c in s.glyphs for c in seq.Component)])
194 for g,seqs in self.ligatures.iteritems())
195 self.ligatures = dict((g,v) for g,v in self.ligatures.iteritems() if v)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400196 return bool(self.ligatures)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400197 else:
198 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400199
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400200@_add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400201def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400202 if cur_glyphs == None: cur_glyphs = s.glyphs
203 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400204 indices = self.Coverage.intersect(cur_glyphs)
205 if(not indices or
206 not all(c.intersect(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400207 for c in self.LookAheadCoverage + self.BacktrackCoverage)):
208 return
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400209 s.glyphs.update(self.Substitute[i] for i in indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400210 else:
211 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400212
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400213@_add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400214def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400215 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400216 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400217 self.Substitute = [self.Substitute[i] for i in indices]
218 # Now drop rules generating glyphs we don't want
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400219 indices = [i for i,sub in enumerate(self.Substitute)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400220 if sub in s.glyphs]
221 self.Substitute = [self.Substitute[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400222 self.Coverage.remap(indices)
223 self.GlyphCount = len(self.Substitute)
224 return bool(self.GlyphCount and
225 all(c.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400226 for c in self.LookAheadCoverage+self.BacktrackCoverage))
227 else:
228 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400229
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400230@_add_method(fontTools.ttLib.tables.otTables.SinglePos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400231def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400232 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400233 return len(self.Coverage.subset(s.glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400234 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400235 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400236 self.Value = [self.Value[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400237 self.ValueCount = len(self.Value)
238 return bool(self.ValueCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400239 else:
240 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400241
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400242@_add_method(fontTools.ttLib.tables.otTables.SinglePos)
243def prune_post_subset(self, options):
244 if not options.hinting:
245 # Drop device tables
246 self.ValueFormat &= ~0x00F0
247 return True
248
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400249@_add_method(fontTools.ttLib.tables.otTables.PairPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400250def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400251 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400252 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400253 self.PairSet = [self.PairSet[i] for i in indices]
254 for p in self.PairSet:
255 p.PairValueRecord = [r for r in p.PairValueRecord
256 if r.SecondGlyph in s.glyphs]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400257 p.PairValueCount = len(p.PairValueRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400258 self.PairSet = [p for p in self.PairSet if p.PairValueCount]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400259 self.PairSetCount = len(self.PairSet)
260 return bool(self.PairSetCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400261 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400262 class1_map = self.ClassDef1.subset(s.glyphs, remap=True)
263 class2_map = self.ClassDef2.subset(s.glyphs, remap=True)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400264 self.Class1Record = [self.Class1Record[i] for i in class1_map]
265 for c in self.Class1Record:
266 c.Class2Record = [c.Class2Record[i] for i in class2_map]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400267 self.Class1Count = len(class1_map)
268 self.Class2Count = len(class2_map)
269 return bool(self.Class1Count and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400270 self.Class2Count and
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400271 self.Coverage.subset(s.glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400272 else:
273 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400274
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400275@_add_method(fontTools.ttLib.tables.otTables.PairPos)
276def prune_post_subset(self, options):
277 if not options.hinting:
278 # Drop device tables
279 self.ValueFormat1 &= ~0x00F0
280 self.ValueFormat2 &= ~0x00F0
281 return True
282
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400283@_add_method(fontTools.ttLib.tables.otTables.CursivePos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400284def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400285 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400286 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400287 self.EntryExitRecord = [self.EntryExitRecord[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400288 self.EntryExitCount = len(self.EntryExitRecord)
289 return bool(self.EntryExitCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400290 else:
291 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400292
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400293@_add_method(fontTools.ttLib.tables.otTables.Anchor)
294def prune_hints(self):
295 # Drop device tables / contour anchor point
296 self.Format = 1
297
298@_add_method(fontTools.ttLib.tables.otTables.CursivePos)
299def prune_post_subset(self, options):
300 if not options.hinting:
301 for rec in self.EntryExitRecord:
302 if rec.EntryAnchor: rec.EntryAnchor.prune_hints()
303 if rec.ExitAnchor: rec.ExitAnchor.prune_hints()
304 return True
305
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400306@_add_method(fontTools.ttLib.tables.otTables.MarkBasePos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400307def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400308 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400309 mark_indices = self.MarkCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400310 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i]
311 for i in mark_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400312 self.MarkArray.MarkCount = len(self.MarkArray.MarkRecord)
313 base_indices = self.BaseCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400314 self.BaseArray.BaseRecord = [self.BaseArray.BaseRecord[i]
315 for i in base_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400316 self.BaseArray.BaseCount = len(self.BaseArray.BaseRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400317 # Prune empty classes
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400318 class_indices = _uniq_sort(v.Class for v in self.MarkArray.MarkRecord)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400319 self.ClassCount = len(class_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400320 for m in self.MarkArray.MarkRecord:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400321 m.Class = class_indices.index(m.Class)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400322 for b in self.BaseArray.BaseRecord:
323 b.BaseAnchor = [b.BaseAnchor[i] for i in class_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400324 return bool(self.ClassCount and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400325 self.MarkArray.MarkCount and
326 self.BaseArray.BaseCount)
327 else:
328 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400329
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400330@_add_method(fontTools.ttLib.tables.otTables.MarkBasePos)
331def prune_post_subset(self, options):
332 if not options.hinting:
333 for m in self.MarkArray.MarkRecord:
334 m.MarkAnchor.prune_hints()
335 for b in self.BaseArray.BaseRecord:
336 for a in b.BaseAnchor:
337 a.prune_hints()
338 return True
339
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400340@_add_method(fontTools.ttLib.tables.otTables.MarkLigPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400341def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400342 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400343 mark_indices = self.MarkCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400344 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i]
345 for i in mark_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400346 self.MarkArray.MarkCount = len(self.MarkArray.MarkRecord)
347 ligature_indices = self.LigatureCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400348 self.LigatureArray.LigatureAttach = [self.LigatureArray.LigatureAttach[i]
349 for i in ligature_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400350 self.LigatureArray.LigatureCount = len(self.LigatureArray.LigatureAttach)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400351 # Prune empty classes
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400352 class_indices = _uniq_sort(v.Class for v in self.MarkArray.MarkRecord)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400353 self.ClassCount = len(class_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400354 for m in self.MarkArray.MarkRecord:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400355 m.Class = class_indices.index(m.Class)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400356 for l in self.LigatureArray.LigatureAttach:
357 for c in l.ComponentRecord:
358 c.LigatureAnchor = [c.LigatureAnchor[i] for i in class_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400359 return bool(self.ClassCount and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400360 self.MarkArray.MarkCount and
361 self.LigatureArray.LigatureCount)
362 else:
363 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400364
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400365@_add_method(fontTools.ttLib.tables.otTables.MarkLigPos)
366def prune_post_subset(self, options):
367 if not options.hinting:
368 for m in self.MarkArray.MarkRecord:
369 m.MarkAnchor.prune_hints()
370 for l in self.LigatureArray.LigatureAttach:
371 for c in l.ComponentRecord:
372 for a in c.LigatureAnchor:
373 a.prune_hints()
374 return True
375
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400376@_add_method(fontTools.ttLib.tables.otTables.MarkMarkPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400377def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400378 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400379 mark1_indices = self.Mark1Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400380 self.Mark1Array.MarkRecord = [self.Mark1Array.MarkRecord[i]
381 for i in mark1_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400382 self.Mark1Array.MarkCount = len(self.Mark1Array.MarkRecord)
383 mark2_indices = self.Mark2Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400384 self.Mark2Array.Mark2Record = [self.Mark2Array.Mark2Record[i]
385 for i in mark2_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400386 self.Mark2Array.MarkCount = len(self.Mark2Array.Mark2Record)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400387 # Prune empty classes
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400388 class_indices = _uniq_sort(v.Class for v in self.Mark1Array.MarkRecord)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400389 self.ClassCount = len(class_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400390 for m in self.Mark1Array.MarkRecord:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400391 m.Class = class_indices.index(m.Class)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400392 for b in self.Mark2Array.Mark2Record:
393 b.Mark2Anchor = [b.Mark2Anchor[i] for i in class_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400394 return bool(self.ClassCount and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400395 self.Mark1Array.MarkCount and
396 self.Mark2Array.MarkCount)
397 else:
398 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400399
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400400@_add_method(fontTools.ttLib.tables.otTables.MarkMarkPos)
401def prune_post_subset(self, options):
402 if not options.hinting:
403 # Drop device tables or contour anchor point
404 for m in self.Mark1Array.MarkRecord:
405 m.MarkAnchor.prune_hints()
406 for b in self.Mark2Array.Mark2Record:
Behdad Esfahbod0ec17d92013-09-15 18:30:41 -0400407 for m in b.Mark2Anchor:
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400408 m.prune_hints()
409 return True
410
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400411@_add_method(fontTools.ttLib.tables.otTables.SingleSubst,
412 fontTools.ttLib.tables.otTables.MultipleSubst,
413 fontTools.ttLib.tables.otTables.AlternateSubst,
414 fontTools.ttLib.tables.otTables.LigatureSubst,
415 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
416 fontTools.ttLib.tables.otTables.SinglePos,
417 fontTools.ttLib.tables.otTables.PairPos,
418 fontTools.ttLib.tables.otTables.CursivePos,
419 fontTools.ttLib.tables.otTables.MarkBasePos,
420 fontTools.ttLib.tables.otTables.MarkLigPos,
421 fontTools.ttLib.tables.otTables.MarkMarkPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400422def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400423 pass
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400424
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400425@_add_method(fontTools.ttLib.tables.otTables.SingleSubst,
426 fontTools.ttLib.tables.otTables.MultipleSubst,
427 fontTools.ttLib.tables.otTables.AlternateSubst,
428 fontTools.ttLib.tables.otTables.LigatureSubst,
429 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
430 fontTools.ttLib.tables.otTables.SinglePos,
431 fontTools.ttLib.tables.otTables.PairPos,
432 fontTools.ttLib.tables.otTables.CursivePos,
433 fontTools.ttLib.tables.otTables.MarkBasePos,
434 fontTools.ttLib.tables.otTables.MarkLigPos,
435 fontTools.ttLib.tables.otTables.MarkMarkPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400436def collect_lookups(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400437 return []
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400438
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400439@_add_method(fontTools.ttLib.tables.otTables.SingleSubst,
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400440 fontTools.ttLib.tables.otTables.MultipleSubst,
441 fontTools.ttLib.tables.otTables.AlternateSubst,
442 fontTools.ttLib.tables.otTables.LigatureSubst,
443 fontTools.ttLib.tables.otTables.ContextSubst,
444 fontTools.ttLib.tables.otTables.ChainContextSubst,
445 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
446 fontTools.ttLib.tables.otTables.SinglePos,
447 fontTools.ttLib.tables.otTables.PairPos,
448 fontTools.ttLib.tables.otTables.CursivePos,
449 fontTools.ttLib.tables.otTables.MarkBasePos,
450 fontTools.ttLib.tables.otTables.MarkLigPos,
451 fontTools.ttLib.tables.otTables.MarkMarkPos,
452 fontTools.ttLib.tables.otTables.ContextPos,
453 fontTools.ttLib.tables.otTables.ChainContextPos)
454def prune_pre_subset(self, options):
455 return True
456
457@_add_method(fontTools.ttLib.tables.otTables.SingleSubst,
458 fontTools.ttLib.tables.otTables.MultipleSubst,
459 fontTools.ttLib.tables.otTables.AlternateSubst,
460 fontTools.ttLib.tables.otTables.LigatureSubst,
461 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
462 fontTools.ttLib.tables.otTables.ContextSubst,
463 fontTools.ttLib.tables.otTables.ChainContextSubst,
464 fontTools.ttLib.tables.otTables.ContextPos,
465 fontTools.ttLib.tables.otTables.ChainContextPos)
466def prune_post_subset(self, options):
467 return True
468
469@_add_method(fontTools.ttLib.tables.otTables.SingleSubst,
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400470 fontTools.ttLib.tables.otTables.AlternateSubst,
471 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400472def may_have_non_1to1(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400473 return False
Behdad Esfahbodaeacc152013-08-12 20:24:33 -0400474
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400475@_add_method(fontTools.ttLib.tables.otTables.MultipleSubst,
476 fontTools.ttLib.tables.otTables.LigatureSubst,
477 fontTools.ttLib.tables.otTables.ContextSubst,
478 fontTools.ttLib.tables.otTables.ChainContextSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400479def may_have_non_1to1(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400480 return True
Behdad Esfahbodaeacc152013-08-12 20:24:33 -0400481
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400482@_add_method(fontTools.ttLib.tables.otTables.ContextSubst,
483 fontTools.ttLib.tables.otTables.ChainContextSubst,
484 fontTools.ttLib.tables.otTables.ContextPos,
485 fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400486def __classify_context(self):
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400487
Behdad Esfahbod3d4c4712013-08-13 20:10:17 -0400488 class ContextHelper(object):
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400489 def __init__(self, klass, Format):
490 if klass.__name__.endswith('Subst'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400491 Typ = 'Sub'
492 Type = 'Subst'
493 else:
494 Typ = 'Pos'
495 Type = 'Pos'
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400496 if klass.__name__.startswith('Chain'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400497 Chain = 'Chain'
498 else:
499 Chain = ''
500 ChainTyp = Chain+Typ
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400501
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400502 self.Typ = Typ
503 self.Type = Type
504 self.Chain = Chain
505 self.ChainTyp = ChainTyp
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400506
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400507 self.LookupRecord = Type+'LookupRecord'
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400508
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400509 if Format == 1:
510 Coverage = lambda r: r.Coverage
511 ChainCoverage = lambda r: r.Coverage
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400512 ContextData = lambda r:(None,)
513 ChainContextData = lambda r:(None, None, None)
514 RuleData = lambda r:(r.Input,)
515 ChainRuleData = lambda r:(r.Backtrack, r.Input, r.LookAhead)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400516 SetRuleData = None
517 ChainSetRuleData = None
518 elif Format == 2:
519 Coverage = lambda r: r.Coverage
520 ChainCoverage = lambda r: r.Coverage
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400521 ContextData = lambda r:(r.ClassDef,)
522 ChainContextData = lambda r:(r.LookAheadClassDef,
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400523 r.InputClassDef,
524 r.BacktrackClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400525 RuleData = lambda r:(r.Class,)
526 ChainRuleData = lambda r:(r.LookAhead, r.Input, r.Backtrack)
527 def SetRuleData(r, d):(r.Class,) = d
528 def ChainSetRuleData(r, d):(r.LookAhead, r.Input, r.Backtrack) = d
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400529 elif Format == 3:
530 Coverage = lambda r: r.Coverage[0]
531 ChainCoverage = lambda r: r.InputCoverage[0]
532 ContextData = None
533 ChainContextData = None
534 RuleData = lambda r: r.Coverage
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400535 ChainRuleData = lambda r:(r.LookAheadCoverage +
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400536 r.InputCoverage +
537 r.BacktrackCoverage)
538 SetRuleData = None
539 ChainSetRuleData = None
540 else:
541 assert 0, "unknown format: %s" % Format
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400542
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400543 if Chain:
544 self.Coverage = ChainCoverage
545 self.ContextData = ChainContextData
546 self.RuleData = ChainRuleData
547 self.SetRuleData = ChainSetRuleData
548 else:
549 self.Coverage = Coverage
550 self.ContextData = ContextData
551 self.RuleData = RuleData
552 self.SetRuleData = SetRuleData
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400553
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400554 if Format == 1:
555 self.Rule = ChainTyp+'Rule'
556 self.RuleCount = ChainTyp+'RuleCount'
557 self.RuleSet = ChainTyp+'RuleSet'
558 self.RuleSetCount = ChainTyp+'RuleSetCount'
559 self.Intersect = lambda glyphs, c, r: [r] if r in glyphs else []
560 elif Format == 2:
561 self.Rule = ChainTyp+'ClassRule'
562 self.RuleCount = ChainTyp+'ClassRuleCount'
563 self.RuleSet = ChainTyp+'ClassSet'
564 self.RuleSetCount = ChainTyp+'ClassSetCount'
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400565 self.Intersect = lambda glyphs, c, r: c.intersect_class(glyphs, r)
Behdad Esfahbod89987002013-07-23 23:07:42 -0400566
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400567 self.ClassDef = 'InputClassDef' if Chain else 'ClassDef'
Behdad Esfahbod11763302013-08-14 15:33:08 -0400568 self.Input = 'Input' if Chain else 'Class'
Behdad Esfahbod27108392013-07-23 16:40:47 -0400569
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400570 if self.Format not in [1, 2, 3]:
Behdad Esfahbod318adc02013-08-13 20:09:28 -0400571 return None # Don't shoot the messenger; let it go
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400572 if not hasattr(self.__class__, "__ContextHelpers"):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400573 self.__class__.__ContextHelpers = {}
574 if self.Format not in self.__class__.__ContextHelpers:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400575 helper = ContextHelper(self.__class__, self.Format)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400576 self.__class__.__ContextHelpers[self.Format] = helper
577 return self.__class__.__ContextHelpers[self.Format]
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400578
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400579@_add_method(fontTools.ttLib.tables.otTables.ContextSubst,
580 fontTools.ttLib.tables.otTables.ChainContextSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400581def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400582 if cur_glyphs == None: cur_glyphs = s.glyphs
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400583 c = self.__classify_context()
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400584
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400585 indices = c.Coverage(self).intersect(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400586 if not indices:
587 return []
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400588 cur_glyphs = c.Coverage(self).intersect_glyphs(s.glyphs);
Behdad Esfahbod1d4fa132013-08-08 22:59:32 -0400589
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400590 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400591 ContextData = c.ContextData(self)
592 rss = getattr(self, c.RuleSet)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400593 for i in indices:
594 if not rss[i]: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400595 for r in getattr(rss[i], c.Rule):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400596 if not r: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400597 if all(all(c.Intersect(s.glyphs, cd, k) for k in klist)
598 for cd,klist in zip(ContextData, c.RuleData(r))):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400599 chaos = False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400600 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400601 if not ll: continue
602 seqi = ll.SequenceIndex
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400603 if chaos:
604 pos_glyphs = s.glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400605 else:
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400606 if seqi == 0:
607 pos_glyphs = set([c.Coverage(self).glyphs[i]])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400608 else:
Behdad Esfahbodd3fdcc72013-08-14 17:59:31 -0400609 pos_glyphs = set([r.Input[seqi - 1]])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400610 lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400611 chaos = chaos or lookup.may_have_non_1to1()
612 lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400613 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400614 ClassDef = getattr(self, c.ClassDef)
615 indices = ClassDef.intersect(cur_glyphs)
616 ContextData = c.ContextData(self)
617 rss = getattr(self, c.RuleSet)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400618 for i in indices:
619 if not rss[i]: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400620 for r in getattr(rss[i], c.Rule):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400621 if not r: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400622 if all(all(c.Intersect(s.glyphs, cd, k) for k in klist)
623 for cd,klist in zip(ContextData, c.RuleData(r))):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400624 chaos = False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400625 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400626 if not ll: continue
627 seqi = ll.SequenceIndex
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400628 if chaos:
629 pos_glyphs = s.glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400630 else:
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400631 if seqi == 0:
632 pos_glyphs = ClassDef.intersect_class(cur_glyphs, i)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400633 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400634 pos_glyphs = ClassDef.intersect_class(s.glyphs,
Behdad Esfahbod11763302013-08-14 15:33:08 -0400635 getattr(r, c.Input)[seqi - 1])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400636 lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400637 chaos = chaos or lookup.may_have_non_1to1()
638 lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400639 elif self.Format == 3:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400640 if not all(x.intersect(s.glyphs) for x in c.RuleData(self)):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400641 return []
642 r = self
643 chaos = False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400644 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400645 if not ll: continue
646 seqi = ll.SequenceIndex
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400647 if chaos:
648 pos_glyphs = s.glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400649 else:
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400650 if seqi == 0:
651 pos_glyphs = cur_glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400652 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400653 pos_glyphs = r.InputCoverage[seqi].intersect_glyphs(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400654 lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400655 chaos = chaos or lookup.may_have_non_1to1()
656 lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400657 else:
658 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod00776972013-07-23 15:33:00 -0400659
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400660@_add_method(fontTools.ttLib.tables.otTables.ContextSubst,
661 fontTools.ttLib.tables.otTables.ContextPos,
662 fontTools.ttLib.tables.otTables.ChainContextSubst,
663 fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400664def subset_glyphs(self, s):
665 c = self.__classify_context()
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400666
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400667 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400668 indices = self.Coverage.subset(s.glyphs)
669 rss = getattr(self, c.RuleSet)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400670 rss = [rss[i] for i in indices]
671 for rs in rss:
672 if not rs: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400673 ss = getattr(rs, c.Rule)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400674 ss = [r for r in ss
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400675 if r and all(all(g in s.glyphs for g in glist)
676 for glist in c.RuleData(r))]
677 setattr(rs, c.Rule, ss)
678 setattr(rs, c.RuleCount, len(ss))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400679 # Prune empty subrulesets
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400680 rss = [rs for rs in rss if rs and getattr(rs, c.Rule)]
681 setattr(self, c.RuleSet, rss)
682 setattr(self, c.RuleSetCount, len(rss))
683 return bool(rss)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400684 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400685 if not self.Coverage.subset(s.glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400686 return False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400687 indices = getattr(self, c.ClassDef).subset(self.Coverage.glyphs,
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400688 remap=False)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400689 rss = getattr(self, c.RuleSet)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400690 rss = [rss[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400691 ContextData = c.ContextData(self)
692 klass_maps = [x.subset(s.glyphs, remap=True) for x in ContextData]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400693 for rs in rss:
694 if not rs: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400695 ss = getattr(rs, c.Rule)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400696 ss = [r for r in ss
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400697 if r and all(all(k in klass_map for k in klist)
698 for klass_map,klist in zip(klass_maps, c.RuleData(r)))]
699 setattr(rs, c.Rule, ss)
700 setattr(rs, c.RuleCount, len(ss))
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400701
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400702 # Remap rule classes
703 for r in ss:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400704 c.SetRuleData(r, [[klass_map.index(k) for k in klist]
705 for klass_map,klist in zip(klass_maps, c.RuleData(r))])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400706 # Prune empty subrulesets
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400707 rss = [rs for rs in rss if rs and getattr(rs, c.Rule)]
708 setattr(self, c.RuleSet, rss)
709 setattr(self, c.RuleSetCount, len(rss))
710 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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400716@_add_method(fontTools.ttLib.tables.otTables.ContextSubst,
717 fontTools.ttLib.tables.otTables.ChainContextSubst,
718 fontTools.ttLib.tables.otTables.ContextPos,
719 fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400744@_add_method(fontTools.ttLib.tables.otTables.ContextSubst,
745 fontTools.ttLib.tables.otTables.ChainContextSubst,
746 fontTools.ttLib.tables.otTables.ContextPos,
747 fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400762@_add_method(fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400769@_add_method(fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400776@_add_method(fontTools.ttLib.tables.otTables.ExtensionSubst,
777 fontTools.ttLib.tables.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
784@_add_method(fontTools.ttLib.tables.otTables.ExtensionSubst,
785 fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400792@_add_method(fontTools.ttLib.tables.otTables.ExtensionSubst,
793 fontTools.ttLib.tables.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
800@_add_method(fontTools.ttLib.tables.otTables.ExtensionSubst,
801 fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400808@_add_method(fontTools.ttLib.tables.otTables.ExtensionSubst,
809 fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400816@_add_method(fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400822@_add_method(fontTools.ttLib.tables.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
830@_add_method(fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400836@_add_method(fontTools.ttLib.tables.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
844@_add_method(fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400849@_add_method(fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400854@_add_method(fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400858@_add_method(fontTools.ttLib.tables.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
866@_add_method(fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400871@_add_method(fontTools.ttLib.tables.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
879@_add_method(fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400887@_add_method(fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400902@_add_method(fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400912@_add_method(fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400916@_add_method(fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400924@_add_method(fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400930@_add_method(fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400936@_add_method(fontTools.ttLib.tables.otTables.DefaultLangSys,
937 fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400950@_add_method(fontTools.ttLib.tables.otTables.DefaultLangSys,
951 fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400958@_add_method(fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400968@_add_method(fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400975@_add_method(fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400982@_add_method(fontTools.ttLib.tables.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 Esfahbod22f5cfc2013-08-13 20:25:37 -0400987@_add_method(fontTools.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()
991 lookup_indices = self.table.FeatureList.collect_lookups(feature_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400992 while True:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400993 orig_glyphs = s.glyphs.copy()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400994 for i in lookup_indices:
995 if i >= self.table.LookupList.LookupCount: continue
996 if not self.table.LookupList.Lookup[i]: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400997 self.table.LookupList.Lookup[i].closure_glyphs(s)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400998 if orig_glyphs == s.glyphs:
999 break
1000 del s.table
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001001
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001002@_add_method(fontTools.ttLib.getTableClass('GSUB'),
1003 fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001004def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001005 s.glyphs = s.glyphs_gsubed
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001006 lookup_indices = self.table.LookupList.subset_glyphs(s)
1007 self.subset_lookups(lookup_indices)
1008 self.prune_lookups()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001009 return True
Behdad Esfahbod02b92062013-07-21 18:40:59 -04001010
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001011@_add_method(fontTools.ttLib.getTableClass('GSUB'),
1012 fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001013def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001014 """Retrains specified lookups, then removes empty features, language
1015 systems, and scripts."""
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001016 self.table.LookupList.subset_lookups(lookup_indices)
1017 feature_indices = self.table.FeatureList.subset_lookups(lookup_indices)
1018 self.table.ScriptList.subset_features(feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -04001019
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001020@_add_method(fontTools.ttLib.getTableClass('GSUB'),
1021 fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001022def prune_lookups(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001023 "Remove unreferenced lookups"
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001024 feature_indices = self.table.ScriptList.collect_features()
1025 lookup_indices = self.table.FeatureList.collect_lookups(feature_indices)
1026 lookup_indices = self.table.LookupList.closure_lookups(lookup_indices)
1027 self.subset_lookups(lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -04001028
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001029@_add_method(fontTools.ttLib.getTableClass('GSUB'),
1030 fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001031def subset_feature_tags(self, feature_tags):
Behdad Esfahbod4734be52013-08-14 19:47:42 -04001032 feature_indices = [i for i,f in
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001033 enumerate(self.table.FeatureList.FeatureRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001034 if f.FeatureTag in feature_tags]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001035 self.table.FeatureList.subset_features(feature_indices)
1036 self.table.ScriptList.subset_features(feature_indices)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -04001037
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001038@_add_method(fontTools.ttLib.getTableClass('GSUB'),
1039 fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001040def prune_pre_subset(self, options):
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001041 if '*' not in options.layout_features:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001042 self.subset_feature_tags(options.layout_features)
1043 self.prune_lookups()
Behdad Esfahbodd77f1572013-08-15 19:24:36 -04001044 self.table.LookupList.prune_pre_subset(options);
1045 return True
1046
1047@_add_method(fontTools.ttLib.getTableClass('GSUB'),
1048 fontTools.ttLib.getTableClass('GPOS'))
1049def prune_post_subset(self, options):
1050 self.table.LookupList.prune_post_subset(options);
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001051 return True
Behdad Esfahbod356c42e2013-07-23 12:10:46 -04001052
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001053@_add_method(fontTools.ttLib.getTableClass('GDEF'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001054def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001055 glyphs = s.glyphs_gsubed
1056 table = self.table
1057 if table.LigCaretList:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001058 indices = table.LigCaretList.Coverage.subset(glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001059 table.LigCaretList.LigGlyph = [table.LigCaretList.LigGlyph[i]
1060 for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001061 table.LigCaretList.LigGlyphCount = len(table.LigCaretList.LigGlyph)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001062 if not table.LigCaretList.LigGlyphCount:
1063 table.LigCaretList = None
1064 if table.MarkAttachClassDef:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001065 table.MarkAttachClassDef.classDefs = dict((g,v) for g,v in
1066 table.MarkAttachClassDef.
1067 classDefs.iteritems()
1068 if g in glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001069 if not table.MarkAttachClassDef.classDefs:
1070 table.MarkAttachClassDef = None
1071 if table.GlyphClassDef:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001072 table.GlyphClassDef.classDefs = dict((g,v) for g,v in
1073 table.GlyphClassDef.
1074 classDefs.iteritems()
1075 if g in glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001076 if not table.GlyphClassDef.classDefs:
1077 table.GlyphClassDef = None
1078 if table.AttachList:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001079 indices = table.AttachList.Coverage.subset(glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001080 table.AttachList.AttachPoint = [table.AttachList.AttachPoint[i]
1081 for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001082 table.AttachList.GlyphCount = len(table.AttachList.AttachPoint)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001083 if not table.AttachList.GlyphCount:
1084 table.AttachList = None
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001085 return bool(table.LigCaretList or
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001086 table.MarkAttachClassDef or
1087 table.GlyphClassDef or
1088 table.AttachList)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -04001089
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001090@_add_method(fontTools.ttLib.getTableClass('kern'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001091def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001092 # Prune unknown kern table types
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001093 self.kernTables = [t for t in self.kernTables if hasattr(t, 'kernTable')]
1094 return bool(self.kernTables)
Behdad Esfahbodd4e33a72013-07-24 18:51:05 -04001095
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001096@_add_method(fontTools.ttLib.getTableClass('kern'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001097def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001098 glyphs = s.glyphs_gsubed
1099 for t in self.kernTables:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001100 t.kernTable = dict(((a,b),v) for (a,b),v in t.kernTable.iteritems()
1101 if a in glyphs and b in glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001102 self.kernTables = [t for t in self.kernTables if t.kernTable]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001103 return bool(self.kernTables)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -04001104
Behdad Esfahbode7a0d562013-08-16 10:56:30 -04001105@_add_method(fontTools.ttLib.getTableClass('vmtx'),
1106 fontTools.ttLib.getTableClass('hmtx'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001107def subset_glyphs(self, s):
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001108 self.metrics = dict((g,v) for g,v in self.metrics.iteritems() if g in s.glyphs)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001109 return bool(self.metrics)
Behdad Esfahbodc7160442013-07-22 14:29:08 -04001110
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001111@_add_method(fontTools.ttLib.getTableClass('hdmx'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001112def subset_glyphs(self, s):
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001113 self.hdmx = dict((sz,_dict((g,v) for g,v in l.iteritems() if g in s.glyphs))
1114 for sz,l in self.hdmx.iteritems())
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001115 return bool(self.hdmx)
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -04001116
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001117@_add_method(fontTools.ttLib.getTableClass('VORG'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001118def subset_glyphs(self, s):
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001119 self.VOriginRecords = dict((g,v) for g,v in self.VOriginRecords.iteritems()
1120 if g in s.glyphs)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001121 self.numVertOriginYMetrics = len(self.VOriginRecords)
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001122 return True # Never drop; has default metrics
Behdad Esfahbode45d6af2013-07-22 15:29:17 -04001123
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001124@_add_method(fontTools.ttLib.getTableClass('post'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001125def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001126 if not options.glyph_names:
1127 self.formatType = 3.0
1128 return True
Behdad Esfahbod42648242013-07-23 12:56:06 -04001129
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001130@_add_method(fontTools.ttLib.getTableClass('post'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001131def subset_glyphs(self, s):
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001132 self.extraNames = [] # This seems to do it
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001133 return True
Behdad Esfahbod653e9742013-07-22 15:17:12 -04001134
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001135@_add_method(fontTools.ttLib.getTableModule('glyf').Glyph)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001136def getComponentNamesFast(self, glyfTable):
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001137 if not self.data or struct.unpack(">h", self.data[:2])[0] >= 0:
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001138 return [] # Not composite
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001139 data = self.data
1140 i = 10
1141 components = []
1142 more = 1
1143 while more:
1144 flags, glyphID = struct.unpack(">HH", data[i:i+4])
1145 i += 4
1146 flags = int(flags)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001147 components.append(glyfTable.getGlyphName(int(glyphID)))
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -04001148
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04001149 if flags & 0x0001: i += 4 # ARG_1_AND_2_ARE_WORDS
Behdad Esfahbod574ce792013-08-13 20:51:44 -04001150 else: i += 2
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04001151 if flags & 0x0008: i += 2 # WE_HAVE_A_SCALE
1152 elif flags & 0x0040: i += 4 # WE_HAVE_AN_X_AND_Y_SCALE
1153 elif flags & 0x0080: i += 8 # WE_HAVE_A_TWO_BY_TWO
1154 more = flags & 0x0020 # MORE_COMPONENTS
1155
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001156 return components
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -04001157
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001158@_add_method(fontTools.ttLib.getTableModule('glyf').Glyph)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001159def remapComponentsFast(self, indices):
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001160 if not self.data or struct.unpack(">h", self.data[:2])[0] >= 0:
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001161 return # Not composite
Behdad Esfahbodd816b7f2013-08-16 16:18:40 -04001162 data = array.array("B", self.data)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001163 i = 10
1164 more = 1
1165 while more:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001166 flags =(data[i] << 8) | data[i+1]
1167 glyphID =(data[i+2] << 8) | data[i+3]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001168 # Remap
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001169 glyphID = indices.index(glyphID)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001170 data[i+2] = glyphID >> 8
1171 data[i+3] = glyphID & 0xFF
1172 i += 4
1173 flags = int(flags)
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -04001174
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04001175 if flags & 0x0001: i += 4 # ARG_1_AND_2_ARE_WORDS
Behdad Esfahbod574ce792013-08-13 20:51:44 -04001176 else: i += 2
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04001177 if flags & 0x0008: i += 2 # WE_HAVE_A_SCALE
1178 elif flags & 0x0040: i += 4 # WE_HAVE_AN_X_AND_Y_SCALE
1179 elif flags & 0x0080: i += 8 # WE_HAVE_A_TWO_BY_TWO
1180 more = flags & 0x0020 # MORE_COMPONENTS
1181
Behdad Esfahbodd816b7f2013-08-16 16:18:40 -04001182 self.data = data.tostring()
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -04001183
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001184@_add_method(fontTools.ttLib.getTableModule('glyf').Glyph)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001185def dropInstructionsFast(self):
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001186 if not self.data:
1187 return
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001188 numContours = struct.unpack(">h", self.data[:2])[0]
Behdad Esfahbodd816b7f2013-08-16 16:18:40 -04001189 data = array.array("B", self.data)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001190 i = 10
1191 if numContours >= 0:
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001192 i += 2 * numContours # endPtsOfContours
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001193 instructionLen =(data[i] << 8) | data[i+1]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001194 # Zero it
1195 data[i] = data [i+1] = 0
1196 i += 2
1197 if instructionLen:
1198 # Splice it out
1199 data = data[:i] + data[i+instructionLen:]
1200 else:
1201 more = 1
1202 while more:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001203 flags =(data[i] << 8) | data[i+1]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001204 # Turn instruction flag off
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04001205 flags &= ~0x0100 # WE_HAVE_INSTRUCTIONS
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001206 data[i+0] = flags >> 8
1207 data[i+1] = flags & 0xFF
1208 i += 4
1209 flags = int(flags)
Behdad Esfahbod6ec88542013-07-24 16:52:47 -04001210
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04001211 if flags & 0x0001: i += 4 # ARG_1_AND_2_ARE_WORDS
Behdad Esfahbod574ce792013-08-13 20:51:44 -04001212 else: i += 2
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04001213 if flags & 0x0008: i += 2 # WE_HAVE_A_SCALE
1214 elif flags & 0x0040: i += 4 # WE_HAVE_AN_X_AND_Y_SCALE
1215 elif flags & 0x0080: i += 8 # WE_HAVE_A_TWO_BY_TWO
1216 more = flags & 0x0020 # MORE_COMPONENTS
1217
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001218 # Cut off
1219 data = data[:i]
1220 if len(data) % 4:
1221 # add pad bytes
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001222 nPadBytes = 4 -(len(data) % 4)
1223 for i in range(nPadBytes):
1224 data.append(0)
Behdad Esfahbodd816b7f2013-08-16 16:18:40 -04001225 self.data = data.tostring()
Behdad Esfahbod6ec88542013-07-24 16:52:47 -04001226
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001227@_add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001228def closure_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001229 decompose = s.glyphs
1230 # I don't know if component glyphs can be composite themselves.
1231 # We handle them anyway.
1232 while True:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001233 components = set()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001234 for g in decompose:
1235 if g not in self.glyphs:
1236 continue
1237 gl = self.glyphs[g]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001238 if hasattr(gl, "data"):
1239 for c in gl.getComponentNamesFast(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001240 if c not in s.glyphs:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001241 components.add(c)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001242 else:
1243 # TTX seems to expand gid0..3 always
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001244 if gl.isComposite():
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001245 for c in gl.components:
1246 if c.glyphName not in s.glyphs:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001247 components.add(c.glyphName)
1248 components = set(c for c in components if c not in s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001249 if not components:
1250 break
1251 decompose = components
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001252 s.glyphs.update(components)
Behdad Esfahbodabb50a12013-07-23 12:58:37 -04001253
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001254@_add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001255def prune_pre_subset(self, options):
1256 if options.notdef_glyph and not options.notdef_outline:
1257 g = self[self.glyphOrder[0]]
1258 # Yay, easy!
1259 g.__dict__.clear()
1260 g.data = ""
1261 return True
1262
1263@_add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001264def subset_glyphs(self, s):
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001265 self.glyphs = dict((g,v) for g,v in self.glyphs.iteritems() if g in s.glyphs)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001266 indices = [i for i,g in enumerate(self.glyphOrder) if g in s.glyphs]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001267 for v in self.glyphs.itervalues():
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001268 if hasattr(v, "data"):
1269 v.remapComponentsFast(indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001270 else:
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001271 pass # No need
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001272 self.glyphOrder = [g for g in self.glyphOrder if g in s.glyphs]
Behdad Esfahbodb69b6712013-08-29 18:17:31 -04001273 # Don't drop empty 'glyf' tables, otherwise 'loca' doesn't get subset.
1274 return True
Behdad Esfahbod861d9152013-07-22 16:47:24 -04001275
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001276@_add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001277def prune_post_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001278 if not options.hinting:
1279 for v in self.glyphs.itervalues():
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001280 if hasattr(v, "data"):
1281 v.dropInstructionsFast()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001282 else:
1283 v.program = fontTools.ttLib.tables.ttProgram.Program()
1284 v.program.fromBytecode([])
1285 return True
Behdad Esfahboded98c612013-07-23 12:37:41 -04001286
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001287@_add_method(fontTools.ttLib.getTableClass('CFF '))
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001288def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001289 cff = self.cff
Behdad Esfahbode0622072013-09-10 14:33:19 -04001290 # CFF table must have one font only
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001291 cff.fontNames = cff.fontNames[:1]
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001292
1293 if options.notdef_glyph and not options.notdef_outline:
1294 for fontname in cff.keys():
1295 font = cff[fontname]
1296 c,_ = font.CharStrings.getItemAndSelector('.notdef')
Behdad Esfahbod21582e92013-09-12 16:47:52 -04001297 # XXX we should preserve the glyph width
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001298 c.bytecode = '\x0e' # endchar
1299 c.program = None
1300
Behdad Esfahbod50f83ef2013-08-29 18:18:17 -04001301 return True # bool(cff.fontNames)
Behdad Esfahbod1a4e72e2013-08-13 15:46:37 -04001302
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001303@_add_method(fontTools.ttLib.getTableClass('CFF '))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001304def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001305 cff = self.cff
1306 for fontname in cff.keys():
1307 font = cff[fontname]
1308 cs = font.CharStrings
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001309
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001310 # Load all glyphs
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001311 for g in font.charset:
1312 if g not in s.glyphs: continue
1313 c,sel = cs.getItemAndSelector(g)
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001314
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001315 if cs.charStringsAreIndexed:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001316 indices = [i for i,g in enumerate(font.charset) if g in s.glyphs]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001317 csi = cs.charStringsIndex
1318 csi.items = [csi.items[i] for i in indices]
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001319 csi.count = len(csi.items)
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001320 del csi.file, csi.offsets
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001321 if hasattr(font, "FDSelect"):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001322 sel = font.FDSelect
1323 sel.format = None
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001324 sel.gidArray = [sel.gidArray[i] for i in indices]
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001325 cs.charStrings = dict((g,indices.index(v))
1326 for g,v in cs.charStrings.iteritems()
1327 if g in s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001328 else:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001329 cs.charStrings = dict((g,v)
1330 for g,v in cs.charStrings.iteritems()
1331 if g in s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001332 font.charset = [g for g in font.charset if g in s.glyphs]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001333 font.numGlyphs = len(font.charset)
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001334
Behdad Esfahbod50f83ef2013-08-29 18:18:17 -04001335 return True # any(cff[fontname].numGlyphs for fontname in cff.keys())
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001336
Behdad Esfahbodb4ea9532013-08-14 19:37:39 -04001337@_add_method(fontTools.misc.psCharStrings.T2CharString)
1338def subset_subroutines(self, subrs, gsubrs):
1339 p = self.program
Behdad Esfahbode0622072013-09-10 14:33:19 -04001340 assert len(p)
Behdad Esfahbod87c8c502013-08-16 14:44:09 -04001341 for i in xrange(1, len(p)):
Behdad Esfahbodb4ea9532013-08-14 19:37:39 -04001342 if p[i] == 'callsubr':
1343 assert type(p[i-1]) is int
1344 p[i-1] = subrs._used.index(p[i-1] + subrs._old_bias) - subrs._new_bias
1345 elif p[i] == 'callgsubr':
1346 assert type(p[i-1]) is int
1347 p[i-1] = gsubrs._used.index(p[i-1] + gsubrs._old_bias) - gsubrs._new_bias
1348
Behdad Esfahbode0622072013-09-10 14:33:19 -04001349@_add_method(fontTools.misc.psCharStrings.T2CharString)
1350def drop_hints(self):
1351 hints = self._hints
1352
1353 if hints.has_hint:
1354 self.program = self.program[hints.last_hint:]
Behdad Esfahbod285d7b82013-09-10 20:30:47 -04001355 if hasattr(self, 'width'):
1356 # Insert width back if needed
1357 if self.width != self.private.defaultWidthX:
Behdad Esfahbod99536852013-09-12 00:23:11 -04001358 self.program.insert(0, self.width - self.private.nominalWidthX)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001359
1360 if hints.has_hintmask:
1361 i = 0
1362 p = self.program
1363 while i < len(p):
1364 if p[i] in ['hintmask', 'cntrmask']:
1365 assert i + 1 <= len(p)
1366 del p[i:i+2]
1367 continue
1368 i += 1
1369
1370 assert len(self.program)
1371
1372 del self._hints
1373
1374class _MarkingT2Decompiler(fontTools.misc.psCharStrings.SimpleT2Decompiler):
1375
1376 def __init__(self, localSubrs, globalSubrs):
1377 fontTools.misc.psCharStrings.SimpleT2Decompiler.__init__(self,
1378 localSubrs,
1379 globalSubrs)
1380 for subrs in [localSubrs, globalSubrs]:
1381 if subrs and not hasattr(subrs, "_used"):
1382 subrs._used = set()
1383
1384 def op_callsubr(self, index):
1385 self.localSubrs._used.add(self.operandStack[-1]+self.localBias)
1386 fontTools.misc.psCharStrings.SimpleT2Decompiler.op_callsubr(self, index)
1387
1388 def op_callgsubr(self, index):
1389 self.globalSubrs._used.add(self.operandStack[-1]+self.globalBias)
1390 fontTools.misc.psCharStrings.SimpleT2Decompiler.op_callgsubr(self, index)
1391
1392class _DehintingT2Decompiler(fontTools.misc.psCharStrings.SimpleT2Decompiler):
1393
1394 class Hints:
1395 def __init__(self):
1396 # Whether calling this charstring produces any hint stems
1397 self.has_hint = False
1398 # Index to start at to drop all hints
1399 self.last_hint = 0
1400 # Index up to which we know more hints are possible. Only
1401 # relevant if status is 0 or 1.
1402 self.last_checked = 0
1403 # The status means:
1404 # 0: after dropping hints, this charstring is empty
1405 # 1: after dropping hints, there may be more hints continuing after this
1406 # 2: no more hints possible after this charstring
1407 self.status = 0
1408 # Has hintmask instructions; not recursive
1409 self.has_hintmask = False
1410 pass
1411
1412 def __init__(self, css, localSubrs, globalSubrs):
1413 self._css = css
1414 fontTools.misc.psCharStrings.SimpleT2Decompiler.__init__(self,
1415 localSubrs,
1416 globalSubrs)
1417
1418 def execute(self, charString):
1419 old_hints = charString._hints if hasattr(charString, '_hints') else None
1420 charString._hints = self.Hints()
1421
1422 fontTools.misc.psCharStrings.SimpleT2Decompiler.execute(self, charString)
1423
1424 hints = charString._hints
1425
1426 if hints.has_hint or hints.has_hintmask:
1427 self._css.add(charString)
1428
1429 if hints.status != 2:
1430 # Check from last_check, make sure we didn't have any operators.
1431 for i in xrange(hints.last_checked, len(charString.program) - 1):
1432 if type(charString.program[i]) == str:
1433 hints.status = 2
1434 break;
1435 else:
1436 hints.status = 1 # There's *something* here
1437 hints.last_checked = len(charString.program)
1438
1439 if old_hints:
1440 assert hints.__dict__ == old_hints.__dict__
1441
1442 def op_callsubr(self, index):
1443 subr = self.localSubrs[self.operandStack[-1]+self.localBias]
1444 fontTools.misc.psCharStrings.SimpleT2Decompiler.op_callsubr(self, index)
1445 self.processSubr(index, subr)
1446
1447 def op_callgsubr(self, index):
1448 subr = self.globalSubrs[self.operandStack[-1]+self.globalBias]
1449 fontTools.misc.psCharStrings.SimpleT2Decompiler.op_callgsubr(self, index)
1450 self.processSubr(index, subr)
1451
1452 def op_hstem(self, index):
1453 fontTools.misc.psCharStrings.SimpleT2Decompiler.op_hstem(self, index)
1454 self.processHint(index)
1455 def op_vstem(self, index):
1456 fontTools.misc.psCharStrings.SimpleT2Decompiler.op_vstem(self, index)
1457 self.processHint(index)
1458 def op_hstemhm(self, index):
1459 fontTools.misc.psCharStrings.SimpleT2Decompiler.op_hstemhm(self, index)
1460 self.processHint(index)
1461 def op_vstemhm(self, index):
1462 fontTools.misc.psCharStrings.SimpleT2Decompiler.op_vstemhm(self, index)
1463 self.processHint(index)
1464 def op_hintmask(self, index):
1465 fontTools.misc.psCharStrings.SimpleT2Decompiler.op_hintmask(self, index)
1466 self.processHintmask(index)
1467 def op_cntrmask(self, index):
1468 fontTools.misc.psCharStrings.SimpleT2Decompiler.op_cntrmask(self, index)
1469 self.processHintmask(index)
1470
1471 def processHintmask(self, index):
1472 cs = self.callingStack[-1]
1473 hints = cs._hints
1474 hints.has_hintmask = True
1475 if hints.status != 2 and hints.has_hint:
1476 # Check from last_check, see if we may be an implicit vstem
Behdad Esfahbod84763142013-09-10 19:00:48 -04001477 for i in xrange(hints.last_checked, index - 1):
Behdad Esfahbode0622072013-09-10 14:33:19 -04001478 if type(cs.program[i]) == str:
Behdad Esfahbod84763142013-09-10 19:00:48 -04001479 hints.status = 2
Behdad Esfahbode0622072013-09-10 14:33:19 -04001480 break;
Behdad Esfahbod84763142013-09-10 19:00:48 -04001481 if hints.status != 2:
Behdad Esfahbode0622072013-09-10 14:33:19 -04001482 # We are an implicit vstem
1483 hints.last_hint = index + 1
Behdad Esfahbod84763142013-09-10 19:00:48 -04001484 hints.status = 0
1485 hints.last_checked = index + 1
Behdad Esfahbode0622072013-09-10 14:33:19 -04001486
1487 def processHint(self, index):
1488 cs = self.callingStack[-1]
1489 hints = cs._hints
1490 hints.has_hint = True
1491 hints.last_hint = index
1492 hints.last_checked = index
1493
1494 def processSubr(self, index, subr):
1495 cs = self.callingStack[-1]
1496 hints = cs._hints
1497 subr_hints = subr._hints
1498
1499 if subr_hints.has_hint:
Behdad Esfahbod285d7b82013-09-10 20:30:47 -04001500 if hints.status != 2:
1501 hints.has_hint = True
Behdad Esfahbod99536852013-09-12 00:23:11 -04001502 hints.last_checked = index
1503 hints.status = subr_hints.status
Behdad Esfahbod285d7b82013-09-10 20:30:47 -04001504 # Decide where to chop off from
1505 if subr_hints.status == 0:
Behdad Esfahbod99536852013-09-12 00:23:11 -04001506 hints.last_hint = index
Behdad Esfahbod285d7b82013-09-10 20:30:47 -04001507 else:
Behdad Esfahbod99536852013-09-12 00:23:11 -04001508 hints.last_hint = index - 2 # Leave the subr call in
Behdad Esfahbode0622072013-09-10 14:33:19 -04001509 else:
Behdad Esfahbod285d7b82013-09-10 20:30:47 -04001510 # In my understanding, this is a font bug. Ie. it has hint stems
1511 # *after* path construction. I've seen this in widespread fonts.
1512 # Best to ignore the hints I suppose...
1513 pass
1514 #assert 0
Behdad Esfahbode0622072013-09-10 14:33:19 -04001515 else:
1516 hints.status = max(hints.status, subr_hints.status)
1517 if hints.status != 2:
1518 # Check from last_check, make sure we didn't have
1519 # any operators.
1520 for i in xrange(hints.last_checked, index - 1):
1521 if type(cs.program[i]) == str:
1522 hints.status = 2
1523 break;
1524 hints.last_checked = index
1525
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001526@_add_method(fontTools.ttLib.getTableClass('CFF '))
1527def prune_post_subset(self, options):
1528 cff = self.cff
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001529 for fontname in cff.keys():
1530 font = cff[fontname]
1531 cs = font.CharStrings
1532
Behdad Esfahbode0622072013-09-10 14:33:19 -04001533
1534 #
Behdad Esfahbod3c20a132013-08-14 19:39:00 -04001535 # Drop unused FontDictionaries
Behdad Esfahbode0622072013-09-10 14:33:19 -04001536 #
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001537 if hasattr(font, "FDSelect"):
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001538 sel = font.FDSelect
1539 indices = _uniq_sort(sel.gidArray)
1540 sel.gidArray = [indices.index (ss) for ss in sel.gidArray]
1541 arr = font.FDArray
Behdad Esfahbod3c20a132013-08-14 19:39:00 -04001542 arr.items = [arr[i] for i in indices]
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001543 arr.count = len(arr.items)
1544 del arr.file, arr.offsets
Behdad Esfahbod1a4e72e2013-08-13 15:46:37 -04001545
Behdad Esfahbode0622072013-09-10 14:33:19 -04001546
1547 #
1548 # Drop hints if not needed
1549 #
1550 if not options.hinting:
1551
1552 #
1553 # This can be tricky, but doesn't have to. What we do is:
1554 #
1555 # - Run all used glyph charstrings and recurse into subroutines,
1556 # - For each charstring (including subroutines), if it has any
1557 # of the hint stem operators, we mark it as such. Upon returning,
1558 # for each charstring we note all the subroutine calls it makes
1559 # that (recursively) contain a stem,
1560 # - Dropping hinting then consists of the following two ops:
1561 # * Drop the piece of the program in each charstring before the
1562 # last call to a stem op or a stem-calling subroutine,
1563 # * Drop all hintmask operations.
1564 # - It's trickier... A hintmask right after hints and a few numbers
1565 # will act as an implicit vstemhm. As such, we track whether
1566 # we have seen any non-hint operators so far and do the right
1567 # thing, recursively... Good luck understanding that :(
1568 #
1569 css = set()
1570 for g in font.charset:
1571 c,sel = cs.getItemAndSelector(g)
1572 # Make sure it's decompiled. We want our "decompiler" to walk
1573 # the program, not the bytecode.
Behdad Esfahbod285d7b82013-09-10 20:30:47 -04001574 c.draw(fontTools.pens.basePen.NullPen())
Behdad Esfahbode0622072013-09-10 14:33:19 -04001575 subrs = getattr(c.private, "Subrs", [])
1576 decompiler = _DehintingT2Decompiler(css, subrs, c.globalSubrs)
1577 decompiler.execute(c)
1578 for charstring in css:
1579 charstring.drop_hints()
1580
1581
1582 #
1583 # Renumber subroutines to remove unused ones
1584 #
1585
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001586 # Mark all used subroutines
1587 for g in font.charset:
1588 c,sel = cs.getItemAndSelector(g)
1589 subrs = getattr(c.private, "Subrs", [])
1590 decompiler = _MarkingT2Decompiler(subrs, c.globalSubrs)
1591 decompiler.execute(c)
1592
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001593 all_subrs = [font.GlobalSubrs]
Behdad Esfahbod83f1f5c2013-08-30 16:20:08 -04001594 if hasattr(font, 'FDSelect'):
Behdad Esfahbode0622072013-09-10 14:33:19 -04001595 all_subrs.extend(fd.Private.Subrs for fd in font.FDArray if hasattr(fd.Private, 'Subrs') and fd.Private.Subrs)
1596 elif hasattr(font.Private, 'Subrs') and font.Private.Subrs:
Behdad Esfahbodcbcaccf2013-08-30 16:21:38 -04001597 all_subrs.append(font.Private.Subrs)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001598
1599 subrs = set(subrs) # Remove duplicates
1600
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001601 # Prepare
1602 for subrs in all_subrs:
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001603 if not hasattr(subrs, '_used'):
1604 subrs._used = set()
1605 subrs._used = _uniq_sort(subrs._used)
1606 subrs._old_bias = fontTools.misc.psCharStrings.calcSubrBias(subrs)
1607 subrs._new_bias = fontTools.misc.psCharStrings.calcSubrBias(subrs._used)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001608
Behdad Esfahboded107712013-08-14 19:54:13 -04001609 # Renumber glyph charstrings
1610 for g in font.charset:
1611 c,sel = cs.getItemAndSelector(g)
1612 subrs = getattr(c.private, "Subrs", [])
1613 c.subset_subroutines (subrs, font.GlobalSubrs)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001614
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001615 # Renumber subroutines themselves
1616 for subrs in all_subrs:
Behdad Esfahbode0622072013-09-10 14:33:19 -04001617
1618 if subrs == font.GlobalSubrs:
1619 if not hasattr(font, 'FDSelect') and hasattr(font.Private, 'Subrs'):
1620 local_subrs = font.Private.Subrs
Behdad Esfahbod83f1f5c2013-08-30 16:20:08 -04001621 else:
Behdad Esfahbode0622072013-09-10 14:33:19 -04001622 local_subrs = []
1623 else:
1624 local_subrs = subrs
1625
1626 subrs.items = [subrs.items[i] for i in subrs._used]
1627 subrs.count = len(subrs.items)
1628 del subrs.file
1629 if hasattr(subrs, 'offsets'):
1630 del subrs.offsets
1631
1632 for i in xrange (subrs.count):
Behdad Esfahbod83f1f5c2013-08-30 16:20:08 -04001633 subrs[i].subset_subroutines (local_subrs, font.GlobalSubrs)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001634
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001635 # Cleanup
1636 for subrs in all_subrs:
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001637 del subrs._used, subrs._old_bias, subrs._new_bias
1638
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001639 return True
Behdad Esfahbod2b677c82013-07-23 13:37:13 -04001640
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001641@_add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001642def closure_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001643 tables = [t for t in self.tables
1644 if t.platformID == 3 and t.platEncID in [1, 10]]
1645 for u in s.unicodes_requested:
1646 found = False
1647 for table in tables:
1648 if u in table.cmap:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001649 s.glyphs.add(table.cmap[u])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001650 found = True
1651 break
1652 if not found:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001653 s.log("No glyph for Unicode value %s; skipping." % u)
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04001654
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001655@_add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001656def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001657 if not options.legacy_cmap:
1658 # Drop non-Unicode / non-Symbol cmaps
1659 self.tables = [t for t in self.tables
1660 if t.platformID == 3 and t.platEncID in [0, 1, 10]]
1661 if not options.symbol_cmap:
1662 self.tables = [t for t in self.tables
1663 if t.platformID == 3 and t.platEncID in [1, 10]]
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001664 # TODO(behdad) Only keep one subtable?
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001665 # For now, drop format=0 which can't be subset_glyphs easily?
1666 self.tables = [t for t in self.tables if t.format != 0]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001667 return bool(self.tables)
Behdad Esfahbodabb50a12013-07-23 12:58:37 -04001668
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001669@_add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001670def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001671 s.glyphs = s.glyphs_cmaped
1672 for t in self.tables:
1673 # For reasons I don't understand I need this here
1674 # to force decompilation of the cmap format 14.
1675 try:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001676 getattr(t, "asdf")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001677 except AttributeError:
1678 pass
1679 if t.format == 14:
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001680 # TODO(behdad) XXX We drop all the default-UVS mappings(g==None).
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001681 t.uvsDict = dict((v,[(u,g) for u,g in l if g in s.glyphs])
1682 for v,l in t.uvsDict.iteritems())
1683 t.uvsDict = dict((v,l) for v,l in t.uvsDict.iteritems() if l)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001684 else:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001685 t.cmap = dict((u,g) for u,g in t.cmap.iteritems()
1686 if g in s.glyphs_requested or u in s.unicodes_requested)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001687 self.tables = [t for t in self.tables
Behdad Esfahbod4734be52013-08-14 19:47:42 -04001688 if (t.cmap if t.format != 14 else t.uvsDict)]
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001689 # TODO(behdad) Convert formats when needed.
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001690 # In particular, if we have a format=12 without non-BMP
1691 # characters, either drop format=12 one or convert it
1692 # to format=4 if there's not one.
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001693 return bool(self.tables)
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001694
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001695@_add_method(fontTools.ttLib.getTableClass('name'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001696def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001697 if '*' not in options.name_IDs:
1698 self.names = [n for n in self.names if n.nameID in options.name_IDs]
1699 if not options.name_legacy:
1700 self.names = [n for n in self.names
1701 if n.platformID == 3 and n.platEncID == 1]
1702 if '*' not in options.name_languages:
1703 self.names = [n for n in self.names if n.langID in options.name_languages]
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001704 return True # Retain even if empty
Behdad Esfahbod653e9742013-07-22 15:17:12 -04001705
Behdad Esfahbod8c646f62013-07-22 15:06:23 -04001706
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001707# TODO(behdad) OS/2 ulUnicodeRange / ulCodePageRange?
1708# TODO(behdad) Drop unneeded GSUB/GPOS Script/LangSys entries.
Behdad Esfahbod852e8a52013-08-29 18:19:22 -04001709# TODO(behdad) Drop empty GSUB/GPOS, and GDEF if no GSUB/GPOS left
1710# TODO(behdad) Drop GDEF subitems if unused by lookups
Behdad Esfahbod10195332013-08-14 19:55:24 -04001711# TODO(behdad) Avoid recursing too much (in GSUB/GPOS and in CFF)
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001712# TODO(behdad) Text direction considerations.
1713# TODO(behdad) Text script / language considerations.
Behdad Esfahbod21582e92013-09-12 16:47:52 -04001714# TODO(behdad) Option to drop hmtx for CFF?
Behdad Esfahbod56ebd042013-07-22 13:02:24 -04001715
Behdad Esfahbod8c486d82013-07-24 13:34:47 -04001716
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001717class Options(object):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001718
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001719 class UnknownOptionError(Exception):
1720 pass
Behdad Esfahbod26d9ee72013-08-13 16:55:01 -04001721
Behdad Esfahboda17743f2013-08-28 17:14:53 -04001722 _drop_tables_default = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', 'SVG ',
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001723 'PCLT', 'LTSH']
1724 _drop_tables_default += ['Feat', 'Glat', 'Gloc', 'Silf', 'Sill'] # Graphite
1725 _drop_tables_default += ['CBLC', 'CBDT', 'sbix', 'COLR', 'CPAL'] # Color
1726 _no_subset_tables_default = ['gasp', 'head', 'hhea', 'maxp', 'vhea', 'OS/2',
1727 'loca', 'name', 'cvt ', 'fpgm', 'prep']
1728 _hinting_tables_default = ['cvt ', 'fpgm', 'prep', 'hdmx', 'VDMX']
Behdad Esfahbod26d9ee72013-08-13 16:55:01 -04001729
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001730 # Based on HarfBuzz shapers
1731 _layout_features_groups = {
1732 # Default shaper
1733 'common': ['ccmp', 'liga', 'locl', 'mark', 'mkmk', 'rlig'],
1734 'horizontal': ['calt', 'clig', 'curs', 'kern', 'rclt'],
1735 'vertical': ['valt', 'vert', 'vkrn', 'vpal', 'vrt2'],
1736 'ltr': ['ltra', 'ltrm'],
1737 'rtl': ['rtla', 'rtlm'],
1738 # Complex shapers
1739 'arabic': ['init', 'medi', 'fina', 'isol', 'med2', 'fin2', 'fin3',
1740 'cswh', 'mset'],
1741 'hangul': ['ljmo', 'vjmo', 'tjmo'],
1742 'tibetal': ['abvs', 'blws', 'abvm', 'blwm'],
1743 'indic': ['nukt', 'akhn', 'rphf', 'rkrf', 'pref', 'blwf', 'half',
1744 'abvf', 'pstf', 'cfar', 'vatu', 'cjct', 'init', 'pres',
1745 'abvs', 'blws', 'psts', 'haln', 'dist', 'abvm', 'blwm'],
1746 }
1747 _layout_features_default = _uniq_sort(sum(
1748 _layout_features_groups.itervalues(), []))
Behdad Esfahbod9eeeb4e2013-08-13 16:58:50 -04001749
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001750 drop_tables = _drop_tables_default
1751 no_subset_tables = _no_subset_tables_default
1752 hinting_tables = _hinting_tables_default
1753 layout_features = _layout_features_default
1754 hinting = False
1755 glyph_names = False
1756 legacy_cmap = False
1757 symbol_cmap = False
1758 name_IDs = [1, 2] # Family and Style
1759 name_legacy = False
1760 name_languages = [0x0409] # English
Behdad Esfahbod04f3a192013-08-29 16:56:06 -04001761 notdef_glyph = True # gid0 for TrueType / .notdef for CFF
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001762 notdef_outline = False # No need for notdef to have an outline really
Behdad Esfahbod04f3a192013-08-29 16:56:06 -04001763 recommended_glyphs = False # gid1, gid2, gid3 for TrueType
Behdad Esfahbode911de12013-08-16 12:42:34 -04001764 recalc_bounds = False # Recalculate font bounding boxes
Behdad Esfahbod03d78da2013-08-29 16:42:00 -04001765 canonical_order = False # Order tables as recommended
Behdad Esfahbodc6c3bb82013-08-15 17:46:20 -04001766 flavor = None # May be 'woff'
Behdad Esfahbod9eeeb4e2013-08-13 16:58:50 -04001767
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001768 def __init__(self, **kwargs):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001769
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001770 self.set(**kwargs)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001771
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001772 def set(self, **kwargs):
1773 for k,v in kwargs.iteritems():
1774 if not hasattr(self, k):
Behdad Esfahbodac10d812013-09-03 18:29:58 -04001775 raise self.UnknownOptionError("Unknown option '%s'" % k)
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001776 setattr(self, k, v)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001777
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001778 def parse_opts(self, argv, ignore_unknown=False):
1779 ret = []
1780 opts = {}
1781 for a in argv:
1782 orig_a = a
1783 if not a.startswith('--'):
1784 ret.append(a)
1785 continue
1786 a = a[2:]
1787 i = a.find('=')
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001788 op = '='
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001789 if i == -1:
1790 if a.startswith("no-"):
1791 k = a[3:]
1792 v = False
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001793 else:
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001794 k = a
1795 v = True
1796 else:
1797 k = a[:i]
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001798 if k[-1] in "-+":
1799 op = k[-1]+'=' # Ops is '-=' or '+=' now.
1800 k = k[:-1]
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001801 v = a[i+1:]
1802 k = k.replace('-', '_')
1803 if not hasattr(self, k):
1804 if ignore_unknown == True or k in ignore_unknown:
1805 ret.append(orig_a)
1806 continue
1807 else:
1808 raise self.UnknownOptionError("Unknown option '%s'" % a)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001809
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001810 ov = getattr(self, k)
1811 if isinstance(ov, bool):
1812 v = bool(v)
1813 elif isinstance(ov, int):
1814 v = int(v)
1815 elif isinstance(ov, list):
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001816 vv = v.split(',')
1817 if vv == ['']:
1818 vv = []
Behdad Esfahbod87c8c502013-08-16 14:44:09 -04001819 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 -04001820 if op == '=':
1821 v = vv
1822 elif op == '+=':
1823 v = ov
1824 v.extend(vv)
1825 elif op == '-=':
1826 v = ov
1827 for x in vv:
1828 if x in v:
1829 v.remove(x)
1830 else:
1831 assert 0
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001832
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001833 opts[k] = v
1834 self.set(**opts)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001835
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001836 return ret
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001837
1838
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001839class Subsetter(object):
1840
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001841 def __init__(self, options=None, log=None):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001842
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001843 if not log:
1844 log = Logger()
1845 if not options:
1846 options = Options()
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001847
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001848 self.options = options
1849 self.log = log
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001850 self.unicodes_requested = set()
1851 self.glyphs_requested = set()
1852 self.glyphs = set()
Behdad Esfahbod3d513b72013-07-31 14:11:40 -04001853
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001854 def populate(self, glyphs=[], unicodes=[], text=""):
1855 self.unicodes_requested.update(unicodes)
1856 if isinstance(text, str):
1857 text = text.decode("utf8")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001858 for u in text:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001859 self.unicodes_requested.add(ord(u))
1860 self.glyphs_requested.update(glyphs)
1861 self.glyphs.update(glyphs)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -04001862
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04001863 def _prune_pre_subset(self, font):
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001864
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001865 for tag in font.keys():
1866 if tag == 'GlyphOrder': continue
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001867
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001868 if(tag in self.options.drop_tables or
1869 (tag in self.options.hinting_tables and not self.options.hinting)):
1870 self.log(tag, "dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001871 del font[tag]
1872 continue
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001873
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001874 clazz = fontTools.ttLib.getTableClass(tag)
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001875
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001876 if hasattr(clazz, 'prune_pre_subset'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001877 table = font[tag]
Behdad Esfahbod010c5f92013-09-10 20:54:46 -04001878 self.log.lapse("load '%s'" % tag)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001879 retain = table.prune_pre_subset(self.options)
1880 self.log.lapse("prune '%s'" % tag)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001881 if not retain:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001882 self.log(tag, "pruned to empty; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001883 del font[tag]
1884 continue
1885 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001886 self.log(tag, "pruned")
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001887
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04001888 def _closure_glyphs(self, font):
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001889
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001890 self.glyphs = self.glyphs_requested.copy()
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001891
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001892 if 'cmap' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001893 font['cmap'].closure_glyphs(self)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001894 self.glyphs_cmaped = self.glyphs
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001895
Behdad Esfahbod04f3a192013-08-29 16:56:06 -04001896 if self.options.notdef_glyph:
1897 if 'glyf' in font:
1898 self.glyphs.add(font.getGlyphName(0))
1899 self.log("Added gid0 to subset")
1900 else:
1901 self.glyphs.add('.notdef')
1902 self.log("Added .notdef to subset")
1903 if self.options.recommended_glyphs:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001904 if 'glyf' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001905 for i in range(4):
1906 self.glyphs.add(font.getGlyphName(i))
1907 self.log("Added first four glyphs to subset")
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001908
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001909 if 'GSUB' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001910 self.log("Closing glyph list over 'GSUB': %d glyphs before" %
1911 len(self.glyphs))
1912 self.log.glyphs(self.glyphs, font=font)
1913 font['GSUB'].closure_glyphs(self)
1914 self.log("Closed glyph list over 'GSUB': %d glyphs after" %
1915 len(self.glyphs))
1916 self.log.glyphs(self.glyphs, font=font)
1917 self.log.lapse("close glyph list over 'GSUB'")
1918 self.glyphs_gsubed = self.glyphs.copy()
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001919
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001920 if 'glyf' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001921 self.log("Closing glyph list over 'glyf': %d glyphs before" %
1922 len(self.glyphs))
1923 self.log.glyphs(self.glyphs, font=font)
1924 font['glyf'].closure_glyphs(self)
1925 self.log("Closed glyph list over 'glyf': %d glyphs after" %
1926 len(self.glyphs))
1927 self.log.glyphs(self.glyphs, font=font)
1928 self.log.lapse("close glyph list over 'glyf'")
1929 self.glyphs_glyfed = self.glyphs.copy()
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001930
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001931 self.glyphs_all = self.glyphs.copy()
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04001932
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001933 self.log("Retaining %d glyphs: " % len(self.glyphs_all))
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001934
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04001935 def _subset_glyphs(self, font):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001936 for tag in font.keys():
1937 if tag == 'GlyphOrder': continue
1938 clazz = fontTools.ttLib.getTableClass(tag)
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001939
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001940 if tag in self.options.no_subset_tables:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001941 self.log(tag, "subsetting not needed")
1942 elif hasattr(clazz, 'subset_glyphs'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001943 table = font[tag]
1944 self.glyphs = self.glyphs_all
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001945 retain = table.subset_glyphs(self)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001946 self.glyphs = self.glyphs_all
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001947 self.log.lapse("subset '%s'" % tag)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001948 if not retain:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001949 self.log(tag, "subsetted to empty; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001950 del font[tag]
1951 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001952 self.log(tag, "subsetted")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001953 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001954 self.log(tag, "NOT subset; don't know how to subset; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001955 del font[tag]
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001956
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001957 glyphOrder = font.getGlyphOrder()
1958 glyphOrder = [g for g in glyphOrder if g in self.glyphs_all]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001959 font.setGlyphOrder(glyphOrder)
1960 font._buildReverseGlyphOrderDict()
1961 self.log.lapse("subset GlyphOrder")
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001962
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04001963 def _prune_post_subset(self, font):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001964 for tag in font.keys():
1965 if tag == 'GlyphOrder': continue
1966 clazz = fontTools.ttLib.getTableClass(tag)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001967 if hasattr(clazz, 'prune_post_subset'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001968 table = font[tag]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001969 retain = table.prune_post_subset(self.options)
1970 self.log.lapse("prune '%s'" % tag)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001971 if not retain:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001972 self.log(tag, "pruned to empty; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001973 del font[tag]
1974 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001975 self.log(tag, "pruned")
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001976
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001977 def subset(self, font):
Behdad Esfahbod756af492013-08-01 12:05:26 -04001978
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04001979 self._prune_pre_subset(font)
1980 self._closure_glyphs(font)
1981 self._subset_glyphs(font)
1982 self._prune_post_subset(font)
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001983
Behdad Esfahbod756af492013-08-01 12:05:26 -04001984
Behdad Esfahbod3d4c4712013-08-13 20:10:17 -04001985class Logger(object):
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001986
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001987 def __init__(self, verbose=False, xml=False, timing=False):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001988 self.verbose = verbose
1989 self.xml = xml
1990 self.timing = timing
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001991 self.last_time = self.start_time = time.time()
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001992
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001993 def parse_opts(self, argv):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001994 argv = argv[:]
1995 for v in ['verbose', 'xml', 'timing']:
1996 if "--"+v in argv:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001997 setattr(self, v, True)
1998 argv.remove("--"+v)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001999 return argv
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04002000
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002001 def __call__(self, *things):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002002 if not self.verbose:
2003 return
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002004 print ' '.join(str(x) for x in things)
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04002005
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002006 def lapse(self, *things):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002007 if not self.timing:
2008 return
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002009 new_time = time.time()
2010 print "Took %0.3fs to %s" %(new_time - self.last_time,
2011 ' '.join(str(x) for x in things))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002012 self.last_time = new_time
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04002013
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04002014 def glyphs(self, glyphs, font=None):
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002015 self("Names: ", sorted(glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002016 if font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002017 reverseGlyphMap = font.getReverseGlyphMap()
2018 self("Gids : ", sorted(reverseGlyphMap[g] for g in glyphs))
Behdad Esfahbodf5497842013-08-08 21:57:02 -04002019
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002020 def font(self, font, file=sys.stdout):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002021 if not self.xml:
2022 return
Behdad Esfahbod28fc4982013-09-18 19:01:16 -04002023 from fontTools.misc import xmlWriter
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002024 writer = xmlWriter.XMLWriter(file)
Behdad Esfahbod45a84602013-08-19 14:44:49 -04002025 font.disassembleInstructions = False # Work around ttLib bug
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002026 for tag in font.keys():
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002027 writer.begintag(tag)
2028 writer.newline()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002029 font[tag].toXML(writer, font)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002030 writer.endtag(tag)
2031 writer.newline()
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04002032
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002033
Behdad Esfahbod85da2682013-08-15 12:17:21 -04002034def load_font(fontFile,
Behdad Esfahbodadc47fd2013-08-15 18:29:25 -04002035 options,
Behdad Esfahbod85da2682013-08-15 12:17:21 -04002036 checkChecksums=False,
Behdad Esfahbod85da2682013-08-15 12:17:21 -04002037 dontLoadGlyphNames=False):
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002038
Behdad Esfahbod45a84602013-08-19 14:44:49 -04002039 font = fontTools.ttLib.TTFont(fontFile,
2040 checkChecksums=checkChecksums,
2041 recalcBBoxes=options.recalc_bounds)
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002042
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002043 # Hack:
2044 #
2045 # If we don't need glyph names, change 'post' class to not try to
2046 # load them. It avoid lots of headache with broken fonts as well
2047 # as loading time.
2048 #
2049 # Ideally ttLib should provide a way to ask it to skip loading
2050 # glyph names. But it currently doesn't provide such a thing.
2051 #
Behdad Esfahbod85da2682013-08-15 12:17:21 -04002052 if dontLoadGlyphNames:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002053 post = fontTools.ttLib.getTableClass('post')
2054 saved = post.decode_format_2_0
2055 post.decode_format_2_0 = post.decode_format_3_0
2056 f = font['post']
2057 if f.formatType == 2.0:
2058 f.formatType = 3.0
2059 post.decode_format_2_0 = saved
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002060
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002061 return font
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002062
Behdad Esfahbode911de12013-08-16 12:42:34 -04002063def save_font(font, outfile, options):
Behdad Esfahbodc6c3bb82013-08-15 17:46:20 -04002064 if options.flavor and not hasattr(font, 'flavor'):
2065 raise Exception("fonttools version does not support flavors.")
2066 font.flavor = options.flavor
Behdad Esfahbode911de12013-08-16 12:42:34 -04002067 font.save(outfile, reorderTables=options.canonical_order)
Behdad Esfahbod41de4cc2013-08-15 12:09:55 -04002068
Behdad Esfahbodb69400f2013-08-29 18:40:53 -04002069def main(args):
Behdad Esfahbod610b0552013-07-23 14:52:18 -04002070
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002071 log = Logger()
2072 args = log.parse_opts(args)
Behdad Esfahbod4ae81712013-07-22 11:57:13 -04002073
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04002074 options = Options()
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002075 args = options.parse_opts(args, ignore_unknown=['text'])
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04002076
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002077 if len(args) < 2:
Behdad Esfahbodb69400f2013-08-29 18:40:53 -04002078 print >>sys.stderr, "usage: pyftsubset font-file glyph... [--text=ABC]... [--option=value]..."
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002079 sys.exit(1)
Behdad Esfahbod02b92062013-07-21 18:40:59 -04002080
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002081 fontfile = args[0]
2082 args = args[1:]
Behdad Esfahbod02b92062013-07-21 18:40:59 -04002083
Behdad Esfahbod85da2682013-08-15 12:17:21 -04002084 dontLoadGlyphNames =(not options.glyph_names and
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002085 all(any(g.startswith(p)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002086 for p in ['gid', 'glyph', 'uni', 'U+'])
2087 for g in args))
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002088
Behdad Esfahbodadc47fd2013-08-15 18:29:25 -04002089 font = load_font(fontfile, options, dontLoadGlyphNames=dontLoadGlyphNames)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002090 subsetter = Subsetter(options=options, log=log)
2091 log.lapse("load font")
Behdad Esfahbod02b92062013-07-21 18:40:59 -04002092
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002093 names = font.getGlyphNames()
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002094 log.lapse("loading glyph names")
Behdad Esfahbode7f5a892013-07-31 19:58:59 -04002095
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002096 glyphs = []
2097 unicodes = []
2098 text = ""
2099 for g in args:
Behdad Esfahbod2be33d92013-09-10 19:28:59 -04002100 if g == '*':
2101 glyphs.extend(font.getGlyphOrder())
2102 continue
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002103 if g in names:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002104 glyphs.append(g)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002105 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002106 if g.startswith('--text='):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002107 text += g[7:]
2108 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002109 if g.startswith('uni') or g.startswith('U+'):
2110 if g.startswith('uni') 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('U+') and len(g) > 2:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002113 g = g[2:]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002114 u = int(g, 16)
2115 unicodes.append(u)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002116 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002117 if g.startswith('gid') or g.startswith('glyph'):
2118 if g.startswith('gid') and len(g) > 3:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002119 g = g[3:]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002120 elif g.startswith('glyph') and len(g) > 5:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002121 g = g[5:]
2122 try:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002123 glyphs.append(font.getGlyphName(int(g), requireReal=1))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002124 except ValueError:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002125 raise Exception("Invalid glyph identifier: %s" % g)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002126 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002127 raise Exception("Invalid glyph identifier: %s" % g)
2128 log.lapse("compile glyph list")
2129 log("Unicodes:", unicodes)
2130 log("Glyphs:", glyphs)
Behdad Esfahbod6df089a2013-07-31 19:27:14 -04002131
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002132 subsetter.populate(glyphs=glyphs, unicodes=unicodes, text=text)
2133 subsetter.subset(font)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -04002134
Behdad Esfahbod34426c12013-08-14 18:30:09 -04002135 outfile = fontfile + '.subset'
2136
Behdad Esfahbodc6c3bb82013-08-15 17:46:20 -04002137 save_font (font, outfile, options)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002138 log.lapse("compile and save font")
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04002139
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002140 log.last_time = log.start_time
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002141 log.lapse("make one with everything(TOTAL TIME)")
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04002142
Behdad Esfahbod34426c12013-08-14 18:30:09 -04002143 if log.verbose:
2144 import os
2145 log("Input font: %d bytes" % os.path.getsize(fontfile))
2146 log("Subset font: %d bytes" % os.path.getsize(outfile))
2147
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002148 log.font(font)
Behdad Esfahbod8c486d82013-07-24 13:34:47 -04002149
Behdad Esfahbodc56bf482013-08-13 20:13:33 -04002150 font.close()
2151
Behdad Esfahbod39a39ac2013-08-22 18:10:17 -04002152
2153__all__ = [
Behdad Esfahbodb69400f2013-08-29 18:40:53 -04002154 'Options',
2155 'Subsetter',
2156 'Logger',
2157 'load_font',
2158 'save_font',
2159 'main'
Behdad Esfahbod39a39ac2013-08-22 18:10:17 -04002160]
2161
Behdad Esfahbod8c486d82013-07-24 13:34:47 -04002162if __name__ == '__main__':
Behdad Esfahbodb69400f2013-08-29 18:40:53 -04002163 main(sys.argv[1:])