blob: 8280b2ecdd1fd19d6677adf1f9c8bd89390386fc [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 Esfahbod54660612013-07-21 18:16:55 -040032
Behdad Esfahbod54660612013-07-21 18:16:55 -040033
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040034def _add_method(*clazzes):
Behdad Esfahbod616d36e2013-08-13 20:02:59 -040035 """Returns a decorator function that adds a new method to one or
36 more classes."""
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040037 def wrapper(method):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040038 for clazz in clazzes:
39 assert clazz.__name__ != 'DefaultTable', 'Oops, table class not found.'
Behdad Esfahbode7a0d562013-08-16 10:56:30 -040040 assert not hasattr(clazz, method.func_name), \
Behdad Esfahbodd77f1572013-08-15 19:24:36 -040041 "Oops, class '%s' has method '%s'." % (clazz.__name__,
42 method.func_name)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040043 setattr(clazz, method.func_name, method)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040044 return None
45 return wrapper
Behdad Esfahbod54660612013-07-21 18:16:55 -040046
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040047def _uniq_sort(l):
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040048 return sorted(set(l))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -040049
Behdad Esfahbod42d4f2b2013-08-16 16:16:22 -040050def _set_update(s, *others):
51 # Jython's set.update only takes one other argument.
52 # Emulate real set.update...
53 for other in others:
54 s.update(other)
55
Behdad Esfahbod78661bb2013-07-23 10:23:42 -040056
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040057@_add_method(fontTools.ttLib.tables.otTables.Coverage)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040058def intersect(self, glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040059 "Returns ascending list of matching coverage values."
Behdad Esfahbod4734be52013-08-14 19:47:42 -040060 return [i for i,g in enumerate(self.glyphs) if g in glyphs]
Behdad Esfahbod610b0552013-07-23 14:52:18 -040061
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040062@_add_method(fontTools.ttLib.tables.otTables.Coverage)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040063def intersect_glyphs(self, glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040064 "Returns set of intersecting glyphs."
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040065 return set(g for g in self.glyphs if g in glyphs)
Behdad Esfahbod849d25c2013-08-12 19:24:24 -040066
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040067@_add_method(fontTools.ttLib.tables.otTables.Coverage)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040068def subset(self, glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040069 "Returns ascending list of remaining coverage values."
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040070 indices = self.intersect(glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040071 self.glyphs = [g for g in self.glyphs if g in glyphs]
72 return indices
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040073
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040074@_add_method(fontTools.ttLib.tables.otTables.Coverage)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040075def remap(self, coverage_map):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040076 "Remaps coverage."
77 self.glyphs = [self.glyphs[i] for i in coverage_map]
Behdad Esfahbod14374262013-08-08 22:26:49 -040078
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040079@_add_method(fontTools.ttLib.tables.otTables.ClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040080def intersect(self, glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040081 "Returns ascending list of matching class values."
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040082 return _uniq_sort(
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040083 ([0] if any(g not in self.classDefs for g in glyphs) else []) +
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040084 [v for g,v in self.classDefs.iteritems() if g in glyphs])
Behdad Esfahbodb8d55882013-07-23 22:17:39 -040085
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040086@_add_method(fontTools.ttLib.tables.otTables.ClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040087def intersect_class(self, glyphs, klass):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040088 "Returns set of glyphs matching class."
89 if klass == 0:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040090 return set(g for g in glyphs if g not in self.classDefs)
91 return set(g for g,v in self.classDefs.iteritems()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040092 if v == klass and g in glyphs)
Behdad Esfahbodb8d55882013-07-23 22:17:39 -040093
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040094@_add_method(fontTools.ttLib.tables.otTables.ClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040095def subset(self, glyphs, remap=False):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040096 "Returns ascending list of remaining classes."
Behdad Esfahbodd73f2252013-08-16 10:58:25 -040097 self.classDefs = dict((g,v) for g,v in self.classDefs.iteritems() if g in glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040098 # Note: while class 0 has the special meaning of "not matched",
99 # if no glyph will ever /not match/, we can optimize class 0 out too.
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400100 indices = _uniq_sort(
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400101 ([0] if any(g not in self.classDefs for g in glyphs) else []) +
Behdad Esfahbod4e5d9672013-08-14 19:49:53 -0400102 self.classDefs.values())
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400103 if remap:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400104 self.remap(indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400105 return indices
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -0400106
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400107@_add_method(fontTools.ttLib.tables.otTables.ClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400108def remap(self, class_map):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400109 "Remaps classes."
Behdad Esfahbodd73f2252013-08-16 10:58:25 -0400110 self.classDefs = dict((g,class_map.index(v))
111 for g,v in self.classDefs.iteritems())
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400112
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400113@_add_method(fontTools.ttLib.tables.otTables.SingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400114def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400115 if cur_glyphs == None: cur_glyphs = s.glyphs
116 if self.Format in [1, 2]:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400117 s.glyphs.update(v for g,v in self.mapping.iteritems() if g in cur_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400118 else:
119 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400120
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400121@_add_method(fontTools.ttLib.tables.otTables.SingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400122def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400123 if self.Format in [1, 2]:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -0400124 self.mapping = dict((g,v) for g,v in self.mapping.iteritems()
125 if g in s.glyphs and v in s.glyphs)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400126 return bool(self.mapping)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400127 else:
128 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400129
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400130@_add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400131def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400132 if cur_glyphs == None: cur_glyphs = s.glyphs
133 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400134 indices = self.Coverage.intersect(cur_glyphs)
Behdad Esfahboda9bfec12013-08-16 16:21:25 -0400135 _set_update(s.glyphs, *(self.Sequence[i].Substitute for i in indices))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400136 else:
137 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400138
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400139@_add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400140def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400141 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400142 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400143 self.Sequence = [self.Sequence[i] for i in indices]
144 # Now drop rules generating glyphs we don't want
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400145 indices = [i for i,seq in enumerate(self.Sequence)
146 if all(sub in s.glyphs for sub in seq.Substitute)]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400147 self.Sequence = [self.Sequence[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400148 self.Coverage.remap(indices)
149 self.SequenceCount = len(self.Sequence)
150 return bool(self.SequenceCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400151 else:
152 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400153
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400154@_add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400155def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400156 if cur_glyphs == None: cur_glyphs = s.glyphs
157 if self.Format == 1:
Behdad Esfahbod42d4f2b2013-08-16 16:16:22 -0400158 _set_update(s.glyphs, *(vlist for g,vlist in self.alternates.iteritems()
159 if g in cur_glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400160 else:
161 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400162
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400163@_add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400164def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400165 if self.Format == 1:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -0400166 self.alternates = dict((g,vlist)
167 for g,vlist in self.alternates.iteritems()
168 if g in s.glyphs and
169 all(v in s.glyphs for v in vlist))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400170 return bool(self.alternates)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400171 else:
172 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400173
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400174@_add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400175def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400176 if cur_glyphs == None: cur_glyphs = s.glyphs
177 if self.Format == 1:
Behdad Esfahbod42d4f2b2013-08-16 16:16:22 -0400178 _set_update(s.glyphs, *([seq.LigGlyph for seq in seqs
179 if all(c in s.glyphs for c in seq.Component)]
180 for g,seqs in self.ligatures.iteritems()
181 if g in cur_glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400182 else:
183 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400184
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400185@_add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400186def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400187 if self.Format == 1:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -0400188 self.ligatures = dict((g,v) for g,v in self.ligatures.iteritems()
189 if g in s.glyphs)
190 self.ligatures = dict((g,[seq for seq in seqs
191 if seq.LigGlyph in s.glyphs and
192 all(c in s.glyphs for c in seq.Component)])
193 for g,seqs in self.ligatures.iteritems())
194 self.ligatures = dict((g,v) for g,v in self.ligatures.iteritems() if v)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400195 return bool(self.ligatures)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400196 else:
197 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400198
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400199@_add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400200def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400201 if cur_glyphs == None: cur_glyphs = s.glyphs
202 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400203 indices = self.Coverage.intersect(cur_glyphs)
204 if(not indices or
205 not all(c.intersect(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400206 for c in self.LookAheadCoverage + self.BacktrackCoverage)):
207 return
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400208 s.glyphs.update(self.Substitute[i] for i in indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400209 else:
210 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400211
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400212@_add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400213def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400214 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400215 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400216 self.Substitute = [self.Substitute[i] for i in indices]
217 # Now drop rules generating glyphs we don't want
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400218 indices = [i for i,sub in enumerate(self.Substitute)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400219 if sub in s.glyphs]
220 self.Substitute = [self.Substitute[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400221 self.Coverage.remap(indices)
222 self.GlyphCount = len(self.Substitute)
223 return bool(self.GlyphCount and
224 all(c.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400225 for c in self.LookAheadCoverage+self.BacktrackCoverage))
226 else:
227 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400228
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400229@_add_method(fontTools.ttLib.tables.otTables.SinglePos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400230def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400231 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400232 return len(self.Coverage.subset(s.glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400233 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400234 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400235 self.Value = [self.Value[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400236 self.ValueCount = len(self.Value)
237 return bool(self.ValueCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400238 else:
239 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400240
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400241@_add_method(fontTools.ttLib.tables.otTables.SinglePos)
242def prune_post_subset(self, options):
243 if not options.hinting:
244 # Drop device tables
245 self.ValueFormat &= ~0x00F0
246 return True
247
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400248@_add_method(fontTools.ttLib.tables.otTables.PairPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400249def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400250 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400251 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400252 self.PairSet = [self.PairSet[i] for i in indices]
253 for p in self.PairSet:
254 p.PairValueRecord = [r for r in p.PairValueRecord
255 if r.SecondGlyph in s.glyphs]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400256 p.PairValueCount = len(p.PairValueRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400257 self.PairSet = [p for p in self.PairSet if p.PairValueCount]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400258 self.PairSetCount = len(self.PairSet)
259 return bool(self.PairSetCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400260 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400261 class1_map = self.ClassDef1.subset(s.glyphs, remap=True)
262 class2_map = self.ClassDef2.subset(s.glyphs, remap=True)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400263 self.Class1Record = [self.Class1Record[i] for i in class1_map]
264 for c in self.Class1Record:
265 c.Class2Record = [c.Class2Record[i] for i in class2_map]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400266 self.Class1Count = len(class1_map)
267 self.Class2Count = len(class2_map)
268 return bool(self.Class1Count and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400269 self.Class2Count and
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400270 self.Coverage.subset(s.glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400271 else:
272 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400273
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400274@_add_method(fontTools.ttLib.tables.otTables.PairPos)
275def prune_post_subset(self, options):
276 if not options.hinting:
277 # Drop device tables
278 self.ValueFormat1 &= ~0x00F0
279 self.ValueFormat2 &= ~0x00F0
280 return True
281
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400282@_add_method(fontTools.ttLib.tables.otTables.CursivePos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400283def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400284 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400285 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400286 self.EntryExitRecord = [self.EntryExitRecord[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400287 self.EntryExitCount = len(self.EntryExitRecord)
288 return bool(self.EntryExitCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400289 else:
290 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400291
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400292@_add_method(fontTools.ttLib.tables.otTables.Anchor)
293def prune_hints(self):
294 # Drop device tables / contour anchor point
295 self.Format = 1
296
297@_add_method(fontTools.ttLib.tables.otTables.CursivePos)
298def prune_post_subset(self, options):
299 if not options.hinting:
300 for rec in self.EntryExitRecord:
301 if rec.EntryAnchor: rec.EntryAnchor.prune_hints()
302 if rec.ExitAnchor: rec.ExitAnchor.prune_hints()
303 return True
304
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400305@_add_method(fontTools.ttLib.tables.otTables.MarkBasePos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400306def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400307 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400308 mark_indices = self.MarkCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400309 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i]
310 for i in mark_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400311 self.MarkArray.MarkCount = len(self.MarkArray.MarkRecord)
312 base_indices = self.BaseCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400313 self.BaseArray.BaseRecord = [self.BaseArray.BaseRecord[i]
314 for i in base_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400315 self.BaseArray.BaseCount = len(self.BaseArray.BaseRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400316 # Prune empty classes
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400317 class_indices = _uniq_sort(v.Class for v in self.MarkArray.MarkRecord)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400318 self.ClassCount = len(class_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400319 for m in self.MarkArray.MarkRecord:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400320 m.Class = class_indices.index(m.Class)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400321 for b in self.BaseArray.BaseRecord:
322 b.BaseAnchor = [b.BaseAnchor[i] for i in class_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400323 return bool(self.ClassCount and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400324 self.MarkArray.MarkCount and
325 self.BaseArray.BaseCount)
326 else:
327 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400328
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400329@_add_method(fontTools.ttLib.tables.otTables.MarkBasePos)
330def prune_post_subset(self, options):
331 if not options.hinting:
332 for m in self.MarkArray.MarkRecord:
333 m.MarkAnchor.prune_hints()
334 for b in self.BaseArray.BaseRecord:
335 for a in b.BaseAnchor:
336 a.prune_hints()
337 return True
338
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400339@_add_method(fontTools.ttLib.tables.otTables.MarkLigPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400340def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400341 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400342 mark_indices = self.MarkCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400343 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i]
344 for i in mark_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400345 self.MarkArray.MarkCount = len(self.MarkArray.MarkRecord)
346 ligature_indices = self.LigatureCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400347 self.LigatureArray.LigatureAttach = [self.LigatureArray.LigatureAttach[i]
348 for i in ligature_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400349 self.LigatureArray.LigatureCount = len(self.LigatureArray.LigatureAttach)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400350 # Prune empty classes
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400351 class_indices = _uniq_sort(v.Class for v in self.MarkArray.MarkRecord)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400352 self.ClassCount = len(class_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400353 for m in self.MarkArray.MarkRecord:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400354 m.Class = class_indices.index(m.Class)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400355 for l in self.LigatureArray.LigatureAttach:
356 for c in l.ComponentRecord:
357 c.LigatureAnchor = [c.LigatureAnchor[i] for i in class_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400358 return bool(self.ClassCount and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400359 self.MarkArray.MarkCount and
360 self.LigatureArray.LigatureCount)
361 else:
362 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400363
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400364@_add_method(fontTools.ttLib.tables.otTables.MarkLigPos)
365def prune_post_subset(self, options):
366 if not options.hinting:
367 for m in self.MarkArray.MarkRecord:
368 m.MarkAnchor.prune_hints()
369 for l in self.LigatureArray.LigatureAttach:
370 for c in l.ComponentRecord:
371 for a in c.LigatureAnchor:
372 a.prune_hints()
373 return True
374
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400375@_add_method(fontTools.ttLib.tables.otTables.MarkMarkPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400376def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400377 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400378 mark1_indices = self.Mark1Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400379 self.Mark1Array.MarkRecord = [self.Mark1Array.MarkRecord[i]
380 for i in mark1_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400381 self.Mark1Array.MarkCount = len(self.Mark1Array.MarkRecord)
382 mark2_indices = self.Mark2Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400383 self.Mark2Array.Mark2Record = [self.Mark2Array.Mark2Record[i]
384 for i in mark2_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400385 self.Mark2Array.MarkCount = len(self.Mark2Array.Mark2Record)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400386 # Prune empty classes
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400387 class_indices = _uniq_sort(v.Class for v in self.Mark1Array.MarkRecord)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400388 self.ClassCount = len(class_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400389 for m in self.Mark1Array.MarkRecord:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400390 m.Class = class_indices.index(m.Class)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400391 for b in self.Mark2Array.Mark2Record:
392 b.Mark2Anchor = [b.Mark2Anchor[i] for i in class_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400393 return bool(self.ClassCount and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400394 self.Mark1Array.MarkCount and
395 self.Mark2Array.MarkCount)
396 else:
397 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400398
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400399@_add_method(fontTools.ttLib.tables.otTables.MarkMarkPos)
400def prune_post_subset(self, options):
401 if not options.hinting:
402 # Drop device tables or contour anchor point
403 for m in self.Mark1Array.MarkRecord:
404 m.MarkAnchor.prune_hints()
405 for b in self.Mark2Array.Mark2Record:
406 for m in rec.Mark2Anchor:
407 m.prune_hints()
408 return True
409
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400410@_add_method(fontTools.ttLib.tables.otTables.SingleSubst,
411 fontTools.ttLib.tables.otTables.MultipleSubst,
412 fontTools.ttLib.tables.otTables.AlternateSubst,
413 fontTools.ttLib.tables.otTables.LigatureSubst,
414 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
415 fontTools.ttLib.tables.otTables.SinglePos,
416 fontTools.ttLib.tables.otTables.PairPos,
417 fontTools.ttLib.tables.otTables.CursivePos,
418 fontTools.ttLib.tables.otTables.MarkBasePos,
419 fontTools.ttLib.tables.otTables.MarkLigPos,
420 fontTools.ttLib.tables.otTables.MarkMarkPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400421def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400422 pass
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400423
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400424@_add_method(fontTools.ttLib.tables.otTables.SingleSubst,
425 fontTools.ttLib.tables.otTables.MultipleSubst,
426 fontTools.ttLib.tables.otTables.AlternateSubst,
427 fontTools.ttLib.tables.otTables.LigatureSubst,
428 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
429 fontTools.ttLib.tables.otTables.SinglePos,
430 fontTools.ttLib.tables.otTables.PairPos,
431 fontTools.ttLib.tables.otTables.CursivePos,
432 fontTools.ttLib.tables.otTables.MarkBasePos,
433 fontTools.ttLib.tables.otTables.MarkLigPos,
434 fontTools.ttLib.tables.otTables.MarkMarkPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400435def collect_lookups(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400436 return []
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400437
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400438@_add_method(fontTools.ttLib.tables.otTables.SingleSubst,
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400439 fontTools.ttLib.tables.otTables.MultipleSubst,
440 fontTools.ttLib.tables.otTables.AlternateSubst,
441 fontTools.ttLib.tables.otTables.LigatureSubst,
442 fontTools.ttLib.tables.otTables.ContextSubst,
443 fontTools.ttLib.tables.otTables.ChainContextSubst,
444 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
445 fontTools.ttLib.tables.otTables.SinglePos,
446 fontTools.ttLib.tables.otTables.PairPos,
447 fontTools.ttLib.tables.otTables.CursivePos,
448 fontTools.ttLib.tables.otTables.MarkBasePos,
449 fontTools.ttLib.tables.otTables.MarkLigPos,
450 fontTools.ttLib.tables.otTables.MarkMarkPos,
451 fontTools.ttLib.tables.otTables.ContextPos,
452 fontTools.ttLib.tables.otTables.ChainContextPos)
453def prune_pre_subset(self, options):
454 return True
455
456@_add_method(fontTools.ttLib.tables.otTables.SingleSubst,
457 fontTools.ttLib.tables.otTables.MultipleSubst,
458 fontTools.ttLib.tables.otTables.AlternateSubst,
459 fontTools.ttLib.tables.otTables.LigatureSubst,
460 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
461 fontTools.ttLib.tables.otTables.ContextSubst,
462 fontTools.ttLib.tables.otTables.ChainContextSubst,
463 fontTools.ttLib.tables.otTables.ContextPos,
464 fontTools.ttLib.tables.otTables.ChainContextPos)
465def prune_post_subset(self, options):
466 return True
467
468@_add_method(fontTools.ttLib.tables.otTables.SingleSubst,
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400469 fontTools.ttLib.tables.otTables.AlternateSubst,
470 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400471def may_have_non_1to1(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400472 return False
Behdad Esfahbodaeacc152013-08-12 20:24:33 -0400473
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400474@_add_method(fontTools.ttLib.tables.otTables.MultipleSubst,
475 fontTools.ttLib.tables.otTables.LigatureSubst,
476 fontTools.ttLib.tables.otTables.ContextSubst,
477 fontTools.ttLib.tables.otTables.ChainContextSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400478def may_have_non_1to1(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400479 return True
Behdad Esfahbodaeacc152013-08-12 20:24:33 -0400480
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400481@_add_method(fontTools.ttLib.tables.otTables.ContextSubst,
482 fontTools.ttLib.tables.otTables.ChainContextSubst,
483 fontTools.ttLib.tables.otTables.ContextPos,
484 fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400485def __classify_context(self):
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400486
Behdad Esfahbod3d4c4712013-08-13 20:10:17 -0400487 class ContextHelper(object):
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400488 def __init__(self, klass, Format):
489 if klass.__name__.endswith('Subst'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400490 Typ = 'Sub'
491 Type = 'Subst'
492 else:
493 Typ = 'Pos'
494 Type = 'Pos'
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400495 if klass.__name__.startswith('Chain'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400496 Chain = 'Chain'
497 else:
498 Chain = ''
499 ChainTyp = Chain+Typ
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400500
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400501 self.Typ = Typ
502 self.Type = Type
503 self.Chain = Chain
504 self.ChainTyp = ChainTyp
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400505
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400506 self.LookupRecord = Type+'LookupRecord'
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400507
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400508 if Format == 1:
509 Coverage = lambda r: r.Coverage
510 ChainCoverage = lambda r: r.Coverage
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400511 ContextData = lambda r:(None,)
512 ChainContextData = lambda r:(None, None, None)
513 RuleData = lambda r:(r.Input,)
514 ChainRuleData = lambda r:(r.Backtrack, r.Input, r.LookAhead)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400515 SetRuleData = None
516 ChainSetRuleData = None
517 elif Format == 2:
518 Coverage = lambda r: r.Coverage
519 ChainCoverage = lambda r: r.Coverage
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400520 ContextData = lambda r:(r.ClassDef,)
521 ChainContextData = lambda r:(r.LookAheadClassDef,
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400522 r.InputClassDef,
523 r.BacktrackClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400524 RuleData = lambda r:(r.Class,)
525 ChainRuleData = lambda r:(r.LookAhead, r.Input, r.Backtrack)
526 def SetRuleData(r, d):(r.Class,) = d
527 def ChainSetRuleData(r, d):(r.LookAhead, r.Input, r.Backtrack) = d
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400528 elif Format == 3:
529 Coverage = lambda r: r.Coverage[0]
530 ChainCoverage = lambda r: r.InputCoverage[0]
531 ContextData = None
532 ChainContextData = None
533 RuleData = lambda r: r.Coverage
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400534 ChainRuleData = lambda r:(r.LookAheadCoverage +
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400535 r.InputCoverage +
536 r.BacktrackCoverage)
537 SetRuleData = None
538 ChainSetRuleData = None
539 else:
540 assert 0, "unknown format: %s" % Format
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400541
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400542 if Chain:
543 self.Coverage = ChainCoverage
544 self.ContextData = ChainContextData
545 self.RuleData = ChainRuleData
546 self.SetRuleData = ChainSetRuleData
547 else:
548 self.Coverage = Coverage
549 self.ContextData = ContextData
550 self.RuleData = RuleData
551 self.SetRuleData = SetRuleData
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400552
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400553 if Format == 1:
554 self.Rule = ChainTyp+'Rule'
555 self.RuleCount = ChainTyp+'RuleCount'
556 self.RuleSet = ChainTyp+'RuleSet'
557 self.RuleSetCount = ChainTyp+'RuleSetCount'
558 self.Intersect = lambda glyphs, c, r: [r] if r in glyphs else []
559 elif Format == 2:
560 self.Rule = ChainTyp+'ClassRule'
561 self.RuleCount = ChainTyp+'ClassRuleCount'
562 self.RuleSet = ChainTyp+'ClassSet'
563 self.RuleSetCount = ChainTyp+'ClassSetCount'
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400564 self.Intersect = lambda glyphs, c, r: c.intersect_class(glyphs, r)
Behdad Esfahbod89987002013-07-23 23:07:42 -0400565
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400566 self.ClassDef = 'InputClassDef' if Chain else 'ClassDef'
Behdad Esfahbod11763302013-08-14 15:33:08 -0400567 self.Input = 'Input' if Chain else 'Class'
Behdad Esfahbod27108392013-07-23 16:40:47 -0400568
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400569 if self.Format not in [1, 2, 3]:
Behdad Esfahbod318adc02013-08-13 20:09:28 -0400570 return None # Don't shoot the messenger; let it go
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400571 if not hasattr(self.__class__, "__ContextHelpers"):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400572 self.__class__.__ContextHelpers = {}
573 if self.Format not in self.__class__.__ContextHelpers:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400574 helper = ContextHelper(self.__class__, self.Format)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400575 self.__class__.__ContextHelpers[self.Format] = helper
576 return self.__class__.__ContextHelpers[self.Format]
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400577
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400578@_add_method(fontTools.ttLib.tables.otTables.ContextSubst,
579 fontTools.ttLib.tables.otTables.ChainContextSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400580def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400581 if cur_glyphs == None: cur_glyphs = s.glyphs
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400582 c = self.__classify_context()
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400583
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400584 indices = c.Coverage(self).intersect(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400585 if not indices:
586 return []
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400587 cur_glyphs = c.Coverage(self).intersect_glyphs(s.glyphs);
Behdad Esfahbod1d4fa132013-08-08 22:59:32 -0400588
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400589 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400590 ContextData = c.ContextData(self)
591 rss = getattr(self, c.RuleSet)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400592 for i in indices:
593 if not rss[i]: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400594 for r in getattr(rss[i], c.Rule):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400595 if not r: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400596 if all(all(c.Intersect(s.glyphs, cd, k) for k in klist)
597 for cd,klist in zip(ContextData, c.RuleData(r))):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400598 chaos = False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400599 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400600 if not ll: continue
601 seqi = ll.SequenceIndex
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400602 if chaos:
603 pos_glyphs = s.glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400604 else:
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400605 if seqi == 0:
606 pos_glyphs = set([c.Coverage(self).glyphs[i]])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400607 else:
Behdad Esfahbodd3fdcc72013-08-14 17:59:31 -0400608 pos_glyphs = set([r.Input[seqi - 1]])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400609 lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400610 chaos = chaos or lookup.may_have_non_1to1()
611 lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400612 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400613 ClassDef = getattr(self, c.ClassDef)
614 indices = ClassDef.intersect(cur_glyphs)
615 ContextData = c.ContextData(self)
616 rss = getattr(self, c.RuleSet)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400617 for i in indices:
618 if not rss[i]: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400619 for r in getattr(rss[i], c.Rule):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400620 if not r: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400621 if all(all(c.Intersect(s.glyphs, cd, k) for k in klist)
622 for cd,klist in zip(ContextData, c.RuleData(r))):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400623 chaos = False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400624 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400625 if not ll: continue
626 seqi = ll.SequenceIndex
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400627 if chaos:
628 pos_glyphs = s.glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400629 else:
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400630 if seqi == 0:
631 pos_glyphs = ClassDef.intersect_class(cur_glyphs, i)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400632 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400633 pos_glyphs = ClassDef.intersect_class(s.glyphs,
Behdad Esfahbod11763302013-08-14 15:33:08 -0400634 getattr(r, c.Input)[seqi - 1])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400635 lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400636 chaos = chaos or lookup.may_have_non_1to1()
637 lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400638 elif self.Format == 3:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400639 if not all(x.intersect(s.glyphs) for x in c.RuleData(self)):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400640 return []
641 r = self
642 chaos = False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400643 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400644 if not ll: continue
645 seqi = ll.SequenceIndex
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400646 if chaos:
647 pos_glyphs = s.glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400648 else:
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400649 if seqi == 0:
650 pos_glyphs = cur_glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400651 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400652 pos_glyphs = r.InputCoverage[seqi].intersect_glyphs(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400653 lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400654 chaos = chaos or lookup.may_have_non_1to1()
655 lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400656 else:
657 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod00776972013-07-23 15:33:00 -0400658
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400659@_add_method(fontTools.ttLib.tables.otTables.ContextSubst,
660 fontTools.ttLib.tables.otTables.ContextPos,
661 fontTools.ttLib.tables.otTables.ChainContextSubst,
662 fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400663def subset_glyphs(self, s):
664 c = self.__classify_context()
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400665
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400666 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400667 indices = self.Coverage.subset(s.glyphs)
668 rss = getattr(self, c.RuleSet)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400669 rss = [rss[i] for i in indices]
670 for rs in rss:
671 if not rs: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400672 ss = getattr(rs, c.Rule)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400673 ss = [r for r in ss
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400674 if r and all(all(g in s.glyphs for g in glist)
675 for glist in c.RuleData(r))]
676 setattr(rs, c.Rule, ss)
677 setattr(rs, c.RuleCount, len(ss))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400678 # Prune empty subrulesets
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400679 rss = [rs for rs in rss if rs and getattr(rs, c.Rule)]
680 setattr(self, c.RuleSet, rss)
681 setattr(self, c.RuleSetCount, len(rss))
682 return bool(rss)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400683 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400684 if not self.Coverage.subset(s.glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400685 return False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400686 indices = getattr(self, c.ClassDef).subset(self.Coverage.glyphs,
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400687 remap=False)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400688 rss = getattr(self, c.RuleSet)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400689 rss = [rss[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400690 ContextData = c.ContextData(self)
691 klass_maps = [x.subset(s.glyphs, remap=True) for x in ContextData]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400692 for rs in rss:
693 if not rs: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400694 ss = getattr(rs, c.Rule)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400695 ss = [r for r in ss
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400696 if r and all(all(k in klass_map for k in klist)
697 for klass_map,klist in zip(klass_maps, c.RuleData(r)))]
698 setattr(rs, c.Rule, ss)
699 setattr(rs, c.RuleCount, len(ss))
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400700
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400701 # Remap rule classes
702 for r in ss:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400703 c.SetRuleData(r, [[klass_map.index(k) for k in klist]
704 for klass_map,klist in zip(klass_maps, c.RuleData(r))])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400705 # Prune empty subrulesets
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400706 rss = [rs for rs in rss if rs and getattr(rs, c.Rule)]
707 setattr(self, c.RuleSet, rss)
708 setattr(self, c.RuleSetCount, len(rss))
709 return bool(rss)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400710 elif self.Format == 3:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400711 return all(x.subset(s.glyphs) for x in c.RuleData(self))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400712 else:
713 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400714
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400715@_add_method(fontTools.ttLib.tables.otTables.ContextSubst,
716 fontTools.ttLib.tables.otTables.ChainContextSubst,
717 fontTools.ttLib.tables.otTables.ContextPos,
718 fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400719def subset_lookups(self, lookup_indices):
720 c = self.__classify_context()
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400721
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400722 if self.Format in [1, 2]:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400723 for rs in getattr(self, c.RuleSet):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400724 if not rs: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400725 for r in getattr(rs, c.Rule):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400726 if not r: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400727 setattr(r, c.LookupRecord,
728 [ll for ll in getattr(r, c.LookupRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400729 if ll and ll.LookupListIndex in lookup_indices])
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400730 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400731 if not ll: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400732 ll.LookupListIndex = lookup_indices.index(ll.LookupListIndex)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400733 elif self.Format == 3:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400734 setattr(self, c.LookupRecord,
735 [ll for ll in getattr(self, c.LookupRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400736 if ll and ll.LookupListIndex in lookup_indices])
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400737 for ll in getattr(self, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400738 if not ll: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400739 ll.LookupListIndex = lookup_indices.index(ll.LookupListIndex)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400740 else:
741 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400742
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400743@_add_method(fontTools.ttLib.tables.otTables.ContextSubst,
744 fontTools.ttLib.tables.otTables.ChainContextSubst,
745 fontTools.ttLib.tables.otTables.ContextPos,
746 fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400747def collect_lookups(self):
748 c = self.__classify_context()
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400749
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400750 if self.Format in [1, 2]:
751 return [ll.LookupListIndex
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400752 for rs in getattr(self, c.RuleSet) if rs
753 for r in getattr(rs, c.Rule) if r
754 for ll in getattr(r, c.LookupRecord) if ll]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400755 elif self.Format == 3:
756 return [ll.LookupListIndex
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400757 for ll in getattr(self, c.LookupRecord) if ll]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400758 else:
759 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400760
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400761@_add_method(fontTools.ttLib.tables.otTables.ExtensionSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400762def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400763 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400764 self.ExtSubTable.closure_glyphs(s, cur_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400765 else:
766 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400767
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400768@_add_method(fontTools.ttLib.tables.otTables.ExtensionSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400769def may_have_non_1to1(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400770 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400771 return self.ExtSubTable.may_have_non_1to1()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400772 else:
773 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbodaeacc152013-08-12 20:24:33 -0400774
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400775@_add_method(fontTools.ttLib.tables.otTables.ExtensionSubst,
776 fontTools.ttLib.tables.otTables.ExtensionPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400777def prune_pre_subset(self, options):
778 if self.Format == 1:
779 return self.ExtSubTable.prune_pre_subset(options)
780 else:
781 assert 0, "unknown format: %s" % self.Format
782
783@_add_method(fontTools.ttLib.tables.otTables.ExtensionSubst,
784 fontTools.ttLib.tables.otTables.ExtensionPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400785def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400786 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400787 return self.ExtSubTable.subset_glyphs(s)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400788 else:
789 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400790
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400791@_add_method(fontTools.ttLib.tables.otTables.ExtensionSubst,
792 fontTools.ttLib.tables.otTables.ExtensionPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400793def prune_post_subset(self, options):
794 if self.Format == 1:
795 return self.ExtSubTable.prune_post_subset(options)
796 else:
797 assert 0, "unknown format: %s" % self.Format
798
799@_add_method(fontTools.ttLib.tables.otTables.ExtensionSubst,
800 fontTools.ttLib.tables.otTables.ExtensionPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400801def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400802 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400803 return self.ExtSubTable.subset_lookups(lookup_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400804 else:
805 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400806
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400807@_add_method(fontTools.ttLib.tables.otTables.ExtensionSubst,
808 fontTools.ttLib.tables.otTables.ExtensionPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400809def collect_lookups(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400810 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400811 return self.ExtSubTable.collect_lookups()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400812 else:
813 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400814
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400815@_add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400816def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400817 for st in self.SubTable:
818 if not st: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400819 st.closure_glyphs(s, cur_glyphs)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400820
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400821@_add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400822def prune_pre_subset(self, options):
823 ret = False
824 for st in self.SubTable:
825 if not st: continue
826 if st.prune_pre_subset(options): ret = True
827 return ret
828
829@_add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400830def subset_glyphs(self, s):
831 self.SubTable = [st for st in self.SubTable if st and st.subset_glyphs(s)]
832 self.SubTableCount = len(self.SubTable)
833 return bool(self.SubTableCount)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400834
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400835@_add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400836def prune_post_subset(self, options):
837 ret = False
838 for st in self.SubTable:
839 if not st: continue
840 if st.prune_post_subset(options): ret = True
841 return ret
842
843@_add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400844def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400845 for s in self.SubTable:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400846 s.subset_lookups(lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400847
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400848@_add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400849def collect_lookups(self):
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400850 return _uniq_sort(sum((st.collect_lookups() for st in self.SubTable
851 if st), []))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400852
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400853@_add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400854def may_have_non_1to1(self):
855 return any(st.may_have_non_1to1() for st in self.SubTable if st)
Behdad Esfahbodaeacc152013-08-12 20:24:33 -0400856
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400857@_add_method(fontTools.ttLib.tables.otTables.LookupList)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400858def prune_pre_subset(self, options):
859 ret = False
860 for l in self.Lookup:
861 if not l: continue
862 if l.prune_pre_subset(options): ret = True
863 return ret
864
865@_add_method(fontTools.ttLib.tables.otTables.LookupList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400866def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400867 "Returns the indices of nonempty lookups."
Behdad Esfahbod4734be52013-08-14 19:47:42 -0400868 return [i for i,l in enumerate(self.Lookup) if l and l.subset_glyphs(s)]
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400869
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400870@_add_method(fontTools.ttLib.tables.otTables.LookupList)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400871def prune_post_subset(self, options):
872 ret = False
873 for l in self.Lookup:
874 if not l: continue
875 if l.prune_post_subset(options): ret = True
876 return ret
877
878@_add_method(fontTools.ttLib.tables.otTables.LookupList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400879def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400880 self.Lookup = [self.Lookup[i] for i in lookup_indices
881 if i < self.LookupCount]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400882 self.LookupCount = len(self.Lookup)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400883 for l in self.Lookup:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400884 l.subset_lookups(lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400885
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400886@_add_method(fontTools.ttLib.tables.otTables.LookupList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400887def closure_lookups(self, lookup_indices):
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400888 lookup_indices = _uniq_sort(lookup_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400889 recurse = lookup_indices
890 while True:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400891 recurse_lookups = sum((self.Lookup[i].collect_lookups()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400892 for i in recurse if i < self.LookupCount), [])
893 recurse_lookups = [l for l in recurse_lookups
894 if l not in lookup_indices and l < self.LookupCount]
895 if not recurse_lookups:
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400896 return _uniq_sort(lookup_indices)
897 recurse_lookups = _uniq_sort(recurse_lookups)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400898 lookup_indices.extend(recurse_lookups)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400899 recurse = recurse_lookups
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400900
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400901@_add_method(fontTools.ttLib.tables.otTables.Feature)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400902def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400903 self.LookupListIndex = [l for l in self.LookupListIndex
904 if l in lookup_indices]
905 # Now map them.
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400906 self.LookupListIndex = [lookup_indices.index(l)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400907 for l in self.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400908 self.LookupCount = len(self.LookupListIndex)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400909 return self.LookupCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400910
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400911@_add_method(fontTools.ttLib.tables.otTables.Feature)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400912def collect_lookups(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400913 return self.LookupListIndex[:]
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400914
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400915@_add_method(fontTools.ttLib.tables.otTables.FeatureList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400916def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400917 "Returns the indices of nonempty features."
Behdad Esfahbod4734be52013-08-14 19:47:42 -0400918 feature_indices = [i for i,f in enumerate(self.FeatureRecord)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400919 if f.Feature.subset_lookups(lookup_indices)]
920 self.subset_features(feature_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400921 return feature_indices
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400922
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400923@_add_method(fontTools.ttLib.tables.otTables.FeatureList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400924def collect_lookups(self, feature_indices):
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400925 return _uniq_sort(sum((self.FeatureRecord[i].Feature.collect_lookups()
926 for i in feature_indices
Behdad Esfahbod1ee298d2013-08-13 20:07:09 -0400927 if i < self.FeatureCount), []))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400928
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400929@_add_method(fontTools.ttLib.tables.otTables.FeatureList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400930def subset_features(self, feature_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400931 self.FeatureRecord = [self.FeatureRecord[i] for i in feature_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400932 self.FeatureCount = len(self.FeatureRecord)
933 return bool(self.FeatureCount)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400934
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400935@_add_method(fontTools.ttLib.tables.otTables.DefaultLangSys,
936 fontTools.ttLib.tables.otTables.LangSys)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400937def subset_features(self, feature_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400938 if self.ReqFeatureIndex in feature_indices:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400939 self.ReqFeatureIndex = feature_indices.index(self.ReqFeatureIndex)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400940 else:
941 self.ReqFeatureIndex = 65535
942 self.FeatureIndex = [f for f in self.FeatureIndex if f in feature_indices]
943 # Now map them.
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400944 self.FeatureIndex = [feature_indices.index(f) for f in self.FeatureIndex
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400945 if f in feature_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400946 self.FeatureCount = len(self.FeatureIndex)
947 return bool(self.FeatureCount or self.ReqFeatureIndex != 65535)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400948
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400949@_add_method(fontTools.ttLib.tables.otTables.DefaultLangSys,
950 fontTools.ttLib.tables.otTables.LangSys)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400951def collect_features(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400952 feature_indices = self.FeatureIndex[:]
953 if self.ReqFeatureIndex != 65535:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400954 feature_indices.append(self.ReqFeatureIndex)
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400955 return _uniq_sort(feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400956
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400957@_add_method(fontTools.ttLib.tables.otTables.Script)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400958def subset_features(self, feature_indices):
959 if(self.DefaultLangSys and
960 not self.DefaultLangSys.subset_features(feature_indices)):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400961 self.DefaultLangSys = None
962 self.LangSysRecord = [l for l in self.LangSysRecord
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400963 if l.LangSys.subset_features(feature_indices)]
964 self.LangSysCount = len(self.LangSysRecord)
965 return bool(self.LangSysCount or self.DefaultLangSys)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400966
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400967@_add_method(fontTools.ttLib.tables.otTables.Script)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400968def collect_features(self):
969 feature_indices = [l.LangSys.collect_features() for l in self.LangSysRecord]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400970 if self.DefaultLangSys:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400971 feature_indices.append(self.DefaultLangSys.collect_features())
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400972 return _uniq_sort(sum(feature_indices, []))
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400973
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400974@_add_method(fontTools.ttLib.tables.otTables.ScriptList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400975def subset_features(self, feature_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400976 self.ScriptRecord = [s for s in self.ScriptRecord
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400977 if s.Script.subset_features(feature_indices)]
978 self.ScriptCount = len(self.ScriptRecord)
979 return bool(self.ScriptCount)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400980
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400981@_add_method(fontTools.ttLib.tables.otTables.ScriptList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400982def collect_features(self):
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400983 return _uniq_sort(sum((s.Script.collect_features()
984 for s in self.ScriptRecord), []))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400985
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400986@_add_method(fontTools.ttLib.getTableClass('GSUB'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400987def closure_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400988 s.table = self.table
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400989 feature_indices = self.table.ScriptList.collect_features()
990 lookup_indices = self.table.FeatureList.collect_lookups(feature_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400991 while True:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400992 orig_glyphs = s.glyphs.copy()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400993 for i in lookup_indices:
994 if i >= self.table.LookupList.LookupCount: continue
995 if not self.table.LookupList.Lookup[i]: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400996 self.table.LookupList.Lookup[i].closure_glyphs(s)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400997 if orig_glyphs == s.glyphs:
998 break
999 del s.table
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001000
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001001@_add_method(fontTools.ttLib.getTableClass('GSUB'),
1002 fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001003def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001004 s.glyphs = s.glyphs_gsubed
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001005 lookup_indices = self.table.LookupList.subset_glyphs(s)
1006 self.subset_lookups(lookup_indices)
1007 self.prune_lookups()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001008 return True
Behdad Esfahbod02b92062013-07-21 18:40:59 -04001009
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001010@_add_method(fontTools.ttLib.getTableClass('GSUB'),
1011 fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001012def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001013 """Retrains specified lookups, then removes empty features, language
1014 systems, and scripts."""
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001015 self.table.LookupList.subset_lookups(lookup_indices)
1016 feature_indices = self.table.FeatureList.subset_lookups(lookup_indices)
1017 self.table.ScriptList.subset_features(feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -04001018
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001019@_add_method(fontTools.ttLib.getTableClass('GSUB'),
1020 fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001021def prune_lookups(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001022 "Remove unreferenced lookups"
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001023 feature_indices = self.table.ScriptList.collect_features()
1024 lookup_indices = self.table.FeatureList.collect_lookups(feature_indices)
1025 lookup_indices = self.table.LookupList.closure_lookups(lookup_indices)
1026 self.subset_lookups(lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -04001027
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001028@_add_method(fontTools.ttLib.getTableClass('GSUB'),
1029 fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001030def subset_feature_tags(self, feature_tags):
Behdad Esfahbod4734be52013-08-14 19:47:42 -04001031 feature_indices = [i for i,f in
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001032 enumerate(self.table.FeatureList.FeatureRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001033 if f.FeatureTag in feature_tags]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001034 self.table.FeatureList.subset_features(feature_indices)
1035 self.table.ScriptList.subset_features(feature_indices)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -04001036
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001037@_add_method(fontTools.ttLib.getTableClass('GSUB'),
1038 fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001039def prune_pre_subset(self, options):
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001040 if '*' not in options.layout_features:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001041 self.subset_feature_tags(options.layout_features)
1042 self.prune_lookups()
Behdad Esfahbodd77f1572013-08-15 19:24:36 -04001043 self.table.LookupList.prune_pre_subset(options);
1044 return True
1045
1046@_add_method(fontTools.ttLib.getTableClass('GSUB'),
1047 fontTools.ttLib.getTableClass('GPOS'))
1048def prune_post_subset(self, options):
1049 self.table.LookupList.prune_post_subset(options);
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001050 return True
Behdad Esfahbod356c42e2013-07-23 12:10:46 -04001051
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001052@_add_method(fontTools.ttLib.getTableClass('GDEF'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001053def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001054 glyphs = s.glyphs_gsubed
1055 table = self.table
1056 if table.LigCaretList:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001057 indices = table.LigCaretList.Coverage.subset(glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001058 table.LigCaretList.LigGlyph = [table.LigCaretList.LigGlyph[i]
1059 for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001060 table.LigCaretList.LigGlyphCount = len(table.LigCaretList.LigGlyph)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001061 if not table.LigCaretList.LigGlyphCount:
1062 table.LigCaretList = None
1063 if table.MarkAttachClassDef:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001064 table.MarkAttachClassDef.classDefs = dict((g,v) for g,v in
1065 table.MarkAttachClassDef.
1066 classDefs.iteritems()
1067 if g in glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001068 if not table.MarkAttachClassDef.classDefs:
1069 table.MarkAttachClassDef = None
1070 if table.GlyphClassDef:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001071 table.GlyphClassDef.classDefs = dict((g,v) for g,v in
1072 table.GlyphClassDef.
1073 classDefs.iteritems()
1074 if g in glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001075 if not table.GlyphClassDef.classDefs:
1076 table.GlyphClassDef = None
1077 if table.AttachList:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001078 indices = table.AttachList.Coverage.subset(glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001079 table.AttachList.AttachPoint = [table.AttachList.AttachPoint[i]
1080 for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001081 table.AttachList.GlyphCount = len(table.AttachList.AttachPoint)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001082 if not table.AttachList.GlyphCount:
1083 table.AttachList = None
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001084 return bool(table.LigCaretList or
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001085 table.MarkAttachClassDef or
1086 table.GlyphClassDef or
1087 table.AttachList)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -04001088
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001089@_add_method(fontTools.ttLib.getTableClass('kern'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001090def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001091 # Prune unknown kern table types
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001092 self.kernTables = [t for t in self.kernTables if hasattr(t, 'kernTable')]
1093 return bool(self.kernTables)
Behdad Esfahbodd4e33a72013-07-24 18:51:05 -04001094
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001095@_add_method(fontTools.ttLib.getTableClass('kern'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001096def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001097 glyphs = s.glyphs_gsubed
1098 for t in self.kernTables:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001099 t.kernTable = dict(((a,b),v) for (a,b),v in t.kernTable.iteritems()
1100 if a in glyphs and b in glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001101 self.kernTables = [t for t in self.kernTables if t.kernTable]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001102 return bool(self.kernTables)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -04001103
Behdad Esfahbode7a0d562013-08-16 10:56:30 -04001104@_add_method(fontTools.ttLib.getTableClass('vmtx'),
1105 fontTools.ttLib.getTableClass('hmtx'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001106def subset_glyphs(self, s):
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001107 self.metrics = dict((g,v) for g,v in self.metrics.iteritems() if g in s.glyphs)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001108 return bool(self.metrics)
Behdad Esfahbodc7160442013-07-22 14:29:08 -04001109
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001110@_add_method(fontTools.ttLib.getTableClass('hdmx'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001111def subset_glyphs(self, s):
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001112 self.hdmx = dict((sz,_dict((g,v) for g,v in l.iteritems() if g in s.glyphs))
1113 for sz,l in self.hdmx.iteritems())
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001114 return bool(self.hdmx)
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -04001115
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001116@_add_method(fontTools.ttLib.getTableClass('VORG'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001117def subset_glyphs(self, s):
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001118 self.VOriginRecords = dict((g,v) for g,v in self.VOriginRecords.iteritems()
1119 if g in s.glyphs)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001120 self.numVertOriginYMetrics = len(self.VOriginRecords)
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001121 return True # Never drop; has default metrics
Behdad Esfahbode45d6af2013-07-22 15:29:17 -04001122
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001123@_add_method(fontTools.ttLib.getTableClass('post'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001124def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001125 if not options.glyph_names:
1126 self.formatType = 3.0
1127 return True
Behdad Esfahbod42648242013-07-23 12:56:06 -04001128
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001129@_add_method(fontTools.ttLib.getTableClass('post'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001130def subset_glyphs(self, s):
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001131 self.extraNames = [] # This seems to do it
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001132 return True
Behdad Esfahbod653e9742013-07-22 15:17:12 -04001133
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001134@_add_method(fontTools.ttLib.getTableModule('glyf').Glyph)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001135def getComponentNamesFast(self, glyfTable):
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001136 if not self.data or struct.unpack(">h", self.data[:2])[0] >= 0:
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001137 return [] # Not composite
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001138 data = self.data
1139 i = 10
1140 components = []
1141 more = 1
1142 while more:
1143 flags, glyphID = struct.unpack(">HH", data[i:i+4])
1144 i += 4
1145 flags = int(flags)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001146 components.append(glyfTable.getGlyphName(int(glyphID)))
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -04001147
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04001148 if flags & 0x0001: i += 4 # ARG_1_AND_2_ARE_WORDS
Behdad Esfahbod574ce792013-08-13 20:51:44 -04001149 else: i += 2
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04001150 if flags & 0x0008: i += 2 # WE_HAVE_A_SCALE
1151 elif flags & 0x0040: i += 4 # WE_HAVE_AN_X_AND_Y_SCALE
1152 elif flags & 0x0080: i += 8 # WE_HAVE_A_TWO_BY_TWO
1153 more = flags & 0x0020 # MORE_COMPONENTS
1154
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001155 return components
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -04001156
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001157@_add_method(fontTools.ttLib.getTableModule('glyf').Glyph)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001158def remapComponentsFast(self, indices):
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001159 if not self.data or struct.unpack(">h", self.data[:2])[0] >= 0:
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001160 return # Not composite
Behdad Esfahbodd816b7f2013-08-16 16:18:40 -04001161 data = array.array("B", self.data)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001162 i = 10
1163 more = 1
1164 while more:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001165 flags =(data[i] << 8) | data[i+1]
1166 glyphID =(data[i+2] << 8) | data[i+3]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001167 # Remap
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001168 glyphID = indices.index(glyphID)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001169 data[i+2] = glyphID >> 8
1170 data[i+3] = glyphID & 0xFF
1171 i += 4
1172 flags = int(flags)
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -04001173
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04001174 if flags & 0x0001: i += 4 # ARG_1_AND_2_ARE_WORDS
Behdad Esfahbod574ce792013-08-13 20:51:44 -04001175 else: i += 2
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04001176 if flags & 0x0008: i += 2 # WE_HAVE_A_SCALE
1177 elif flags & 0x0040: i += 4 # WE_HAVE_AN_X_AND_Y_SCALE
1178 elif flags & 0x0080: i += 8 # WE_HAVE_A_TWO_BY_TWO
1179 more = flags & 0x0020 # MORE_COMPONENTS
1180
Behdad Esfahbodd816b7f2013-08-16 16:18:40 -04001181 self.data = data.tostring()
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -04001182
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001183@_add_method(fontTools.ttLib.getTableModule('glyf').Glyph)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001184def dropInstructionsFast(self):
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001185 if not self.data:
1186 return
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001187 numContours = struct.unpack(">h", self.data[:2])[0]
Behdad Esfahbodd816b7f2013-08-16 16:18:40 -04001188 data = array.array("B", self.data)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001189 i = 10
1190 if numContours >= 0:
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001191 i += 2 * numContours # endPtsOfContours
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001192 instructionLen =(data[i] << 8) | data[i+1]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001193 # Zero it
1194 data[i] = data [i+1] = 0
1195 i += 2
1196 if instructionLen:
1197 # Splice it out
1198 data = data[:i] + data[i+instructionLen:]
1199 else:
1200 more = 1
1201 while more:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001202 flags =(data[i] << 8) | data[i+1]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001203 # Turn instruction flag off
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04001204 flags &= ~0x0100 # WE_HAVE_INSTRUCTIONS
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001205 data[i+0] = flags >> 8
1206 data[i+1] = flags & 0xFF
1207 i += 4
1208 flags = int(flags)
Behdad Esfahbod6ec88542013-07-24 16:52:47 -04001209
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04001210 if flags & 0x0001: i += 4 # ARG_1_AND_2_ARE_WORDS
Behdad Esfahbod574ce792013-08-13 20:51:44 -04001211 else: i += 2
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04001212 if flags & 0x0008: i += 2 # WE_HAVE_A_SCALE
1213 elif flags & 0x0040: i += 4 # WE_HAVE_AN_X_AND_Y_SCALE
1214 elif flags & 0x0080: i += 8 # WE_HAVE_A_TWO_BY_TWO
1215 more = flags & 0x0020 # MORE_COMPONENTS
1216
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001217 # Cut off
1218 data = data[:i]
1219 if len(data) % 4:
1220 # add pad bytes
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001221 nPadBytes = 4 -(len(data) % 4)
1222 for i in range(nPadBytes):
1223 data.append(0)
Behdad Esfahbodd816b7f2013-08-16 16:18:40 -04001224 self.data = data.tostring()
Behdad Esfahbod6ec88542013-07-24 16:52:47 -04001225
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001226@_add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001227def closure_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001228 decompose = s.glyphs
1229 # I don't know if component glyphs can be composite themselves.
1230 # We handle them anyway.
1231 while True:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001232 components = set()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001233 for g in decompose:
1234 if g not in self.glyphs:
1235 continue
1236 gl = self.glyphs[g]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001237 if hasattr(gl, "data"):
1238 for c in gl.getComponentNamesFast(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001239 if c not in s.glyphs:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001240 components.add(c)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001241 else:
1242 # TTX seems to expand gid0..3 always
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001243 if gl.isComposite():
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001244 for c in gl.components:
1245 if c.glyphName not in s.glyphs:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001246 components.add(c.glyphName)
1247 components = set(c for c in components if c not in s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001248 if not components:
1249 break
1250 decompose = components
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001251 s.glyphs.update(components)
Behdad Esfahbodabb50a12013-07-23 12:58:37 -04001252
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001253@_add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001254def prune_pre_subset(self, options):
1255 if options.notdef_glyph and not options.notdef_outline:
1256 g = self[self.glyphOrder[0]]
1257 # Yay, easy!
1258 g.__dict__.clear()
1259 g.data = ""
1260 return True
1261
1262@_add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001263def subset_glyphs(self, s):
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001264 self.glyphs = dict((g,v) for g,v in self.glyphs.iteritems() if g in s.glyphs)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001265 indices = [i for i,g in enumerate(self.glyphOrder) if g in s.glyphs]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001266 for v in self.glyphs.itervalues():
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001267 if hasattr(v, "data"):
1268 v.remapComponentsFast(indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001269 else:
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001270 pass # No need
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001271 self.glyphOrder = [g for g in self.glyphOrder if g in s.glyphs]
Behdad Esfahbodb69b6712013-08-29 18:17:31 -04001272 # Don't drop empty 'glyf' tables, otherwise 'loca' doesn't get subset.
1273 return True
Behdad Esfahbod861d9152013-07-22 16:47:24 -04001274
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001275@_add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001276def prune_post_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001277 if not options.hinting:
1278 for v in self.glyphs.itervalues():
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001279 if hasattr(v, "data"):
1280 v.dropInstructionsFast()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001281 else:
1282 v.program = fontTools.ttLib.tables.ttProgram.Program()
1283 v.program.fromBytecode([])
1284 return True
Behdad Esfahboded98c612013-07-23 12:37:41 -04001285
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001286@_add_method(fontTools.ttLib.getTableClass('CFF '))
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001287def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001288 cff = self.cff
1289 # CFF table should have one font only
1290 cff.fontNames = cff.fontNames[:1]
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001291
1292 if options.notdef_glyph and not options.notdef_outline:
1293 for fontname in cff.keys():
1294 font = cff[fontname]
1295 c,_ = font.CharStrings.getItemAndSelector('.notdef')
1296 c.bytecode = '\x0e' # endchar
1297 c.program = None
1298
Behdad Esfahbod50f83ef2013-08-29 18:18:17 -04001299 return True # bool(cff.fontNames)
Behdad Esfahbod1a4e72e2013-08-13 15:46:37 -04001300
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001301@_add_method(fontTools.ttLib.getTableClass('CFF '))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001302def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001303 cff = self.cff
1304 for fontname in cff.keys():
1305 font = cff[fontname]
1306 cs = font.CharStrings
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001307
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001308 # Load all glyphs
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001309 for g in font.charset:
1310 if g not in s.glyphs: continue
1311 c,sel = cs.getItemAndSelector(g)
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001312
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001313 if cs.charStringsAreIndexed:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001314 indices = [i for i,g in enumerate(font.charset) if g in s.glyphs]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001315 csi = cs.charStringsIndex
1316 csi.items = [csi.items[i] for i in indices]
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001317 csi.count = len(csi.items)
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001318 del csi.file, csi.offsets
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001319 if hasattr(font, "FDSelect"):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001320 sel = font.FDSelect
1321 sel.format = None
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001322 sel.gidArray = [sel.gidArray[i] for i in indices]
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001323 cs.charStrings = dict((g,indices.index(v))
1324 for g,v in cs.charStrings.iteritems()
1325 if g in s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001326 else:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001327 cs.charStrings = dict((g,v)
1328 for g,v in cs.charStrings.iteritems()
1329 if g in s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001330 font.charset = [g for g in font.charset if g in s.glyphs]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001331 font.numGlyphs = len(font.charset)
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001332
Behdad Esfahbod50f83ef2013-08-29 18:18:17 -04001333 return True # any(cff[fontname].numGlyphs for fontname in cff.keys())
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001334
Behdad Esfahbodb4ea9532013-08-14 19:37:39 -04001335@_add_method(fontTools.misc.psCharStrings.T2CharString)
1336def subset_subroutines(self, subrs, gsubrs):
1337 p = self.program
Behdad Esfahbod87c8c502013-08-16 14:44:09 -04001338 for i in xrange(1, len(p)):
Behdad Esfahbodb4ea9532013-08-14 19:37:39 -04001339 if p[i] == 'callsubr':
1340 assert type(p[i-1]) is int
1341 p[i-1] = subrs._used.index(p[i-1] + subrs._old_bias) - subrs._new_bias
1342 elif p[i] == 'callgsubr':
1343 assert type(p[i-1]) is int
1344 p[i-1] = gsubrs._used.index(p[i-1] + gsubrs._old_bias) - gsubrs._new_bias
1345
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001346@_add_method(fontTools.ttLib.getTableClass('CFF '))
1347def prune_post_subset(self, options):
1348 cff = self.cff
1349
1350 class _MarkingT2Decompiler(fontTools.misc.psCharStrings.SimpleT2Decompiler):
1351
1352 def __init__(self, localSubrs, globalSubrs):
1353 fontTools.misc.psCharStrings.SimpleT2Decompiler.__init__(self,
1354 localSubrs,
1355 globalSubrs)
1356 for subrs in [localSubrs, globalSubrs]:
1357 if subrs and not hasattr(subrs, "_used"):
1358 subrs._used = set()
1359
1360 def op_callsubr(self, index):
1361 self.localSubrs._used.add(self.operandStack[-1]+self.localBias)
1362 fontTools.misc.psCharStrings.SimpleT2Decompiler.op_callsubr(self, index)
1363
1364 def op_callgsubr(self, index):
1365 self.globalSubrs._used.add(self.operandStack[-1]+self.globalBias)
1366 fontTools.misc.psCharStrings.SimpleT2Decompiler.op_callgsubr(self, index)
1367
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001368 class _NonrecursingT2Decompiler(fontTools.misc.psCharStrings.SimpleT2Decompiler):
1369
1370 def __init__(self, localSubrs, globalSubrs):
1371 fontTools.misc.psCharStrings.SimpleT2Decompiler.__init__(self,
1372 localSubrs,
1373 globalSubrs)
1374
1375 def op_callsubr(self, index):
1376 self.pop()
1377
1378 def op_callgsubr(self, index):
1379 self.pop()
1380
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001381 for fontname in cff.keys():
1382 font = cff[fontname]
1383 cs = font.CharStrings
1384
Behdad Esfahbod3c20a132013-08-14 19:39:00 -04001385 # Drop unused FontDictionaries
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001386 if hasattr(font, "FDSelect"):
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001387 sel = font.FDSelect
1388 indices = _uniq_sort(sel.gidArray)
1389 sel.gidArray = [indices.index (ss) for ss in sel.gidArray]
1390 arr = font.FDArray
Behdad Esfahbod3c20a132013-08-14 19:39:00 -04001391 arr.items = [arr[i] for i in indices]
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001392 arr.count = len(arr.items)
1393 del arr.file, arr.offsets
Behdad Esfahbod1a4e72e2013-08-13 15:46:37 -04001394
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001395 # Mark all used subroutines
1396 for g in font.charset:
1397 c,sel = cs.getItemAndSelector(g)
1398 subrs = getattr(c.private, "Subrs", [])
1399 decompiler = _MarkingT2Decompiler(subrs, c.globalSubrs)
1400 decompiler.execute(c)
1401
1402 # Renumber subroutines to remove unused ones
1403 all_subrs = [font.GlobalSubrs]
Behdad Esfahbod83f1f5c2013-08-30 16:20:08 -04001404 if hasattr(font, 'FDSelect'):
1405 all_subrs.extend(fd.Private.Subrs for fd in font.FDArray if hasattr(fd.Private, 'Subrs'))
Behdad Esfahbodcbcaccf2013-08-30 16:21:38 -04001406 else:
1407 all_subrs.append(font.Private.Subrs)
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001408 # Prepare
1409 for subrs in all_subrs:
1410 if not subrs: continue
1411 if not hasattr(subrs, '_used'):
1412 subrs._used = set()
1413 subrs._used = _uniq_sort(subrs._used)
1414 subrs._old_bias = fontTools.misc.psCharStrings.calcSubrBias(subrs)
1415 subrs._new_bias = fontTools.misc.psCharStrings.calcSubrBias(subrs._used)
Behdad Esfahboded107712013-08-14 19:54:13 -04001416 # Renumber glyph charstrings
1417 for g in font.charset:
1418 c,sel = cs.getItemAndSelector(g)
1419 subrs = getattr(c.private, "Subrs", [])
1420 c.subset_subroutines (subrs, font.GlobalSubrs)
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001421 # Renumber subroutines themselves
1422 for subrs in all_subrs:
1423 if not subrs: continue
1424 decompiler = _NonrecursingT2Decompiler(subrs, font.GlobalSubrs)
Behdad Esfahbod87c8c502013-08-16 14:44:09 -04001425 for i in xrange (subrs.count):
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001426 if i not in subrs._used: continue
1427 decompiler.reset()
1428 decompiler.execute(subrs[i])
Behdad Esfahbod83f1f5c2013-08-30 16:20:08 -04001429 if subrs == font.GlobalSubrs:
1430 if not hasattr(font, 'FDSelect'):
1431 local_subrs = font.Private.Subrs
1432 else:
1433 local_subrs = []
1434 else:
1435 local_subrs = subrs
1436 subrs[i].subset_subroutines (local_subrs, font.GlobalSubrs)
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001437 # Cleanup
1438 for subrs in all_subrs:
1439 if not subrs: continue
1440 subrs.items = [subrs.items[i] for i in subrs._used]
1441 del subrs.file, subrs.offsets
1442 del subrs._used, subrs._old_bias, subrs._new_bias
1443
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001444 if not options.hinting:
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001445 pass # TODO(behdad) Drop hints
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001446
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001447 return True
Behdad Esfahbod2b677c82013-07-23 13:37:13 -04001448
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001449@_add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001450def closure_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001451 tables = [t for t in self.tables
1452 if t.platformID == 3 and t.platEncID in [1, 10]]
1453 for u in s.unicodes_requested:
1454 found = False
1455 for table in tables:
1456 if u in table.cmap:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001457 s.glyphs.add(table.cmap[u])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001458 found = True
1459 break
1460 if not found:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001461 s.log("No glyph for Unicode value %s; skipping." % u)
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04001462
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001463@_add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001464def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001465 if not options.legacy_cmap:
1466 # Drop non-Unicode / non-Symbol cmaps
1467 self.tables = [t for t in self.tables
1468 if t.platformID == 3 and t.platEncID in [0, 1, 10]]
1469 if not options.symbol_cmap:
1470 self.tables = [t for t in self.tables
1471 if t.platformID == 3 and t.platEncID in [1, 10]]
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001472 # TODO(behdad) Only keep one subtable?
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001473 # For now, drop format=0 which can't be subset_glyphs easily?
1474 self.tables = [t for t in self.tables if t.format != 0]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001475 return bool(self.tables)
Behdad Esfahbodabb50a12013-07-23 12:58:37 -04001476
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001477@_add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001478def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001479 s.glyphs = s.glyphs_cmaped
1480 for t in self.tables:
1481 # For reasons I don't understand I need this here
1482 # to force decompilation of the cmap format 14.
1483 try:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001484 getattr(t, "asdf")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001485 except AttributeError:
1486 pass
1487 if t.format == 14:
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001488 # TODO(behdad) XXX We drop all the default-UVS mappings(g==None).
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001489 t.uvsDict = dict((v,[(u,g) for u,g in l if g in s.glyphs])
1490 for v,l in t.uvsDict.iteritems())
1491 t.uvsDict = dict((v,l) for v,l in t.uvsDict.iteritems() if l)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001492 else:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001493 t.cmap = dict((u,g) for u,g in t.cmap.iteritems()
1494 if g in s.glyphs_requested or u in s.unicodes_requested)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001495 self.tables = [t for t in self.tables
Behdad Esfahbod4734be52013-08-14 19:47:42 -04001496 if (t.cmap if t.format != 14 else t.uvsDict)]
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001497 # TODO(behdad) Convert formats when needed.
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001498 # In particular, if we have a format=12 without non-BMP
1499 # characters, either drop format=12 one or convert it
1500 # to format=4 if there's not one.
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001501 return bool(self.tables)
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001502
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -04001503@_add_method(fontTools.ttLib.getTableClass('name'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001504def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001505 if '*' not in options.name_IDs:
1506 self.names = [n for n in self.names if n.nameID in options.name_IDs]
1507 if not options.name_legacy:
1508 self.names = [n for n in self.names
1509 if n.platformID == 3 and n.platEncID == 1]
1510 if '*' not in options.name_languages:
1511 self.names = [n for n in self.names if n.langID in options.name_languages]
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001512 return True # Retain even if empty
Behdad Esfahbod653e9742013-07-22 15:17:12 -04001513
Behdad Esfahbod8c646f62013-07-22 15:06:23 -04001514
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001515# TODO(behdad) OS/2 ulUnicodeRange / ulCodePageRange?
1516# TODO(behdad) Drop unneeded GSUB/GPOS Script/LangSys entries.
Behdad Esfahbod852e8a52013-08-29 18:19:22 -04001517# TODO(behdad) Drop empty GSUB/GPOS, and GDEF if no GSUB/GPOS left
1518# TODO(behdad) Drop GDEF subitems if unused by lookups
Behdad Esfahbod10195332013-08-14 19:55:24 -04001519# TODO(behdad) Avoid recursing too much (in GSUB/GPOS and in CFF)
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001520# TODO(behdad) Text direction considerations.
1521# TODO(behdad) Text script / language considerations.
Behdad Esfahbod56ebd042013-07-22 13:02:24 -04001522
Behdad Esfahbod8c486d82013-07-24 13:34:47 -04001523
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001524class Options(object):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001525
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001526 class UnknownOptionError(Exception):
1527 pass
Behdad Esfahbod26d9ee72013-08-13 16:55:01 -04001528
Behdad Esfahboda17743f2013-08-28 17:14:53 -04001529 _drop_tables_default = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', 'SVG ',
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001530 'PCLT', 'LTSH']
1531 _drop_tables_default += ['Feat', 'Glat', 'Gloc', 'Silf', 'Sill'] # Graphite
1532 _drop_tables_default += ['CBLC', 'CBDT', 'sbix', 'COLR', 'CPAL'] # Color
1533 _no_subset_tables_default = ['gasp', 'head', 'hhea', 'maxp', 'vhea', 'OS/2',
1534 'loca', 'name', 'cvt ', 'fpgm', 'prep']
1535 _hinting_tables_default = ['cvt ', 'fpgm', 'prep', 'hdmx', 'VDMX']
Behdad Esfahbod26d9ee72013-08-13 16:55:01 -04001536
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001537 # Based on HarfBuzz shapers
1538 _layout_features_groups = {
1539 # Default shaper
1540 'common': ['ccmp', 'liga', 'locl', 'mark', 'mkmk', 'rlig'],
1541 'horizontal': ['calt', 'clig', 'curs', 'kern', 'rclt'],
1542 'vertical': ['valt', 'vert', 'vkrn', 'vpal', 'vrt2'],
1543 'ltr': ['ltra', 'ltrm'],
1544 'rtl': ['rtla', 'rtlm'],
1545 # Complex shapers
1546 'arabic': ['init', 'medi', 'fina', 'isol', 'med2', 'fin2', 'fin3',
1547 'cswh', 'mset'],
1548 'hangul': ['ljmo', 'vjmo', 'tjmo'],
1549 'tibetal': ['abvs', 'blws', 'abvm', 'blwm'],
1550 'indic': ['nukt', 'akhn', 'rphf', 'rkrf', 'pref', 'blwf', 'half',
1551 'abvf', 'pstf', 'cfar', 'vatu', 'cjct', 'init', 'pres',
1552 'abvs', 'blws', 'psts', 'haln', 'dist', 'abvm', 'blwm'],
1553 }
1554 _layout_features_default = _uniq_sort(sum(
1555 _layout_features_groups.itervalues(), []))
Behdad Esfahbod9eeeb4e2013-08-13 16:58:50 -04001556
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001557 drop_tables = _drop_tables_default
1558 no_subset_tables = _no_subset_tables_default
1559 hinting_tables = _hinting_tables_default
1560 layout_features = _layout_features_default
1561 hinting = False
1562 glyph_names = False
1563 legacy_cmap = False
1564 symbol_cmap = False
1565 name_IDs = [1, 2] # Family and Style
1566 name_legacy = False
1567 name_languages = [0x0409] # English
Behdad Esfahbod04f3a192013-08-29 16:56:06 -04001568 notdef_glyph = True # gid0 for TrueType / .notdef for CFF
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001569 notdef_outline = False # No need for notdef to have an outline really
Behdad Esfahbod04f3a192013-08-29 16:56:06 -04001570 recommended_glyphs = False # gid1, gid2, gid3 for TrueType
Behdad Esfahbode911de12013-08-16 12:42:34 -04001571 recalc_bounds = False # Recalculate font bounding boxes
Behdad Esfahbod03d78da2013-08-29 16:42:00 -04001572 canonical_order = False # Order tables as recommended
Behdad Esfahbodc6c3bb82013-08-15 17:46:20 -04001573 flavor = None # May be 'woff'
Behdad Esfahbod9eeeb4e2013-08-13 16:58:50 -04001574
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001575 def __init__(self, **kwargs):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001576
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001577 self.set(**kwargs)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001578
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001579 def set(self, **kwargs):
1580 for k,v in kwargs.iteritems():
1581 if not hasattr(self, k):
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001582 raise self.UnknownOptionError("Unknown option '%s'" % a)
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001583 setattr(self, k, v)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001584
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001585 def parse_opts(self, argv, ignore_unknown=False):
1586 ret = []
1587 opts = {}
1588 for a in argv:
1589 orig_a = a
1590 if not a.startswith('--'):
1591 ret.append(a)
1592 continue
1593 a = a[2:]
1594 i = a.find('=')
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001595 op = '='
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001596 if i == -1:
1597 if a.startswith("no-"):
1598 k = a[3:]
1599 v = False
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001600 else:
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001601 k = a
1602 v = True
1603 else:
1604 k = a[:i]
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001605 if k[-1] in "-+":
1606 op = k[-1]+'=' # Ops is '-=' or '+=' now.
1607 k = k[:-1]
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001608 v = a[i+1:]
1609 k = k.replace('-', '_')
1610 if not hasattr(self, k):
1611 if ignore_unknown == True or k in ignore_unknown:
1612 ret.append(orig_a)
1613 continue
1614 else:
1615 raise self.UnknownOptionError("Unknown option '%s'" % a)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001616
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001617 ov = getattr(self, k)
1618 if isinstance(ov, bool):
1619 v = bool(v)
1620 elif isinstance(ov, int):
1621 v = int(v)
1622 elif isinstance(ov, list):
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001623 vv = v.split(',')
1624 if vv == ['']:
1625 vv = []
Behdad Esfahbod87c8c502013-08-16 14:44:09 -04001626 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 -04001627 if op == '=':
1628 v = vv
1629 elif op == '+=':
1630 v = ov
1631 v.extend(vv)
1632 elif op == '-=':
1633 v = ov
1634 for x in vv:
1635 if x in v:
1636 v.remove(x)
1637 else:
1638 assert 0
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001639
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001640 opts[k] = v
1641 self.set(**opts)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001642
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001643 return ret
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001644
1645
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001646class Subsetter(object):
1647
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001648 def __init__(self, options=None, log=None):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001649
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001650 if not log:
1651 log = Logger()
1652 if not options:
1653 options = Options()
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001654
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001655 self.options = options
1656 self.log = log
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001657 self.unicodes_requested = set()
1658 self.glyphs_requested = set()
1659 self.glyphs = set()
Behdad Esfahbod3d513b72013-07-31 14:11:40 -04001660
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001661 def populate(self, glyphs=[], unicodes=[], text=""):
1662 self.unicodes_requested.update(unicodes)
1663 if isinstance(text, str):
1664 text = text.decode("utf8")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001665 for u in text:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001666 self.unicodes_requested.add(ord(u))
1667 self.glyphs_requested.update(glyphs)
1668 self.glyphs.update(glyphs)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -04001669
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04001670 def _prune_pre_subset(self, font):
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001671
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001672 for tag in font.keys():
1673 if tag == 'GlyphOrder': continue
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001674
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001675 if(tag in self.options.drop_tables or
1676 (tag in self.options.hinting_tables and not self.options.hinting)):
1677 self.log(tag, "dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001678 del font[tag]
1679 continue
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001680
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001681 clazz = fontTools.ttLib.getTableClass(tag)
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001682
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001683 if hasattr(clazz, 'prune_pre_subset'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001684 table = font[tag]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001685 retain = table.prune_pre_subset(self.options)
1686 self.log.lapse("prune '%s'" % tag)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001687 if not retain:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001688 self.log(tag, "pruned to empty; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001689 del font[tag]
1690 continue
1691 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001692 self.log(tag, "pruned")
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001693
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04001694 def _closure_glyphs(self, font):
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001695
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001696 self.glyphs = self.glyphs_requested.copy()
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001697
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001698 if 'cmap' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001699 font['cmap'].closure_glyphs(self)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001700 self.glyphs_cmaped = self.glyphs
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001701
Behdad Esfahbod04f3a192013-08-29 16:56:06 -04001702 if self.options.notdef_glyph:
1703 if 'glyf' in font:
1704 self.glyphs.add(font.getGlyphName(0))
1705 self.log("Added gid0 to subset")
1706 else:
1707 self.glyphs.add('.notdef')
1708 self.log("Added .notdef to subset")
1709 if self.options.recommended_glyphs:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001710 if 'glyf' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001711 for i in range(4):
1712 self.glyphs.add(font.getGlyphName(i))
1713 self.log("Added first four glyphs to subset")
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001714
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001715 if 'GSUB' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001716 self.log("Closing glyph list over 'GSUB': %d glyphs before" %
1717 len(self.glyphs))
1718 self.log.glyphs(self.glyphs, font=font)
1719 font['GSUB'].closure_glyphs(self)
1720 self.log("Closed glyph list over 'GSUB': %d glyphs after" %
1721 len(self.glyphs))
1722 self.log.glyphs(self.glyphs, font=font)
1723 self.log.lapse("close glyph list over 'GSUB'")
1724 self.glyphs_gsubed = self.glyphs.copy()
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001725
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001726 if 'glyf' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001727 self.log("Closing glyph list over 'glyf': %d glyphs before" %
1728 len(self.glyphs))
1729 self.log.glyphs(self.glyphs, font=font)
1730 font['glyf'].closure_glyphs(self)
1731 self.log("Closed glyph list over 'glyf': %d glyphs after" %
1732 len(self.glyphs))
1733 self.log.glyphs(self.glyphs, font=font)
1734 self.log.lapse("close glyph list over 'glyf'")
1735 self.glyphs_glyfed = self.glyphs.copy()
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001736
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001737 self.glyphs_all = self.glyphs.copy()
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04001738
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001739 self.log("Retaining %d glyphs: " % len(self.glyphs_all))
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001740
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04001741 def _subset_glyphs(self, font):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001742 for tag in font.keys():
1743 if tag == 'GlyphOrder': continue
1744 clazz = fontTools.ttLib.getTableClass(tag)
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001745
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001746 if tag in self.options.no_subset_tables:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001747 self.log(tag, "subsetting not needed")
1748 elif hasattr(clazz, 'subset_glyphs'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001749 table = font[tag]
1750 self.glyphs = self.glyphs_all
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001751 retain = table.subset_glyphs(self)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001752 self.glyphs = self.glyphs_all
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001753 self.log.lapse("subset '%s'" % tag)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001754 if not retain:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001755 self.log(tag, "subsetted to empty; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001756 del font[tag]
1757 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001758 self.log(tag, "subsetted")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001759 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001760 self.log(tag, "NOT subset; don't know how to subset; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001761 del font[tag]
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001762
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001763 glyphOrder = font.getGlyphOrder()
1764 glyphOrder = [g for g in glyphOrder if g in self.glyphs_all]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001765 font.setGlyphOrder(glyphOrder)
1766 font._buildReverseGlyphOrderDict()
1767 self.log.lapse("subset GlyphOrder")
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001768
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04001769 def _prune_post_subset(self, font):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001770 for tag in font.keys():
1771 if tag == 'GlyphOrder': continue
1772 clazz = fontTools.ttLib.getTableClass(tag)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001773 if hasattr(clazz, 'prune_post_subset'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001774 table = font[tag]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001775 retain = table.prune_post_subset(self.options)
1776 self.log.lapse("prune '%s'" % tag)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001777 if not retain:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001778 self.log(tag, "pruned to empty; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001779 del font[tag]
1780 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001781 self.log(tag, "pruned")
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001782
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001783 def subset(self, font):
Behdad Esfahbod756af492013-08-01 12:05:26 -04001784
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04001785 self._prune_pre_subset(font)
1786 self._closure_glyphs(font)
1787 self._subset_glyphs(font)
1788 self._prune_post_subset(font)
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001789
Behdad Esfahbod756af492013-08-01 12:05:26 -04001790
Behdad Esfahbod3d4c4712013-08-13 20:10:17 -04001791class Logger(object):
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001792
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001793 def __init__(self, verbose=False, xml=False, timing=False):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001794 self.verbose = verbose
1795 self.xml = xml
1796 self.timing = timing
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001797 self.last_time = self.start_time = time.time()
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001798
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001799 def parse_opts(self, argv):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001800 argv = argv[:]
1801 for v in ['verbose', 'xml', 'timing']:
1802 if "--"+v in argv:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001803 setattr(self, v, True)
1804 argv.remove("--"+v)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001805 return argv
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001806
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001807 def __call__(self, *things):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001808 if not self.verbose:
1809 return
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001810 print ' '.join(str(x) for x in things)
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001811
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001812 def lapse(self, *things):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001813 if not self.timing:
1814 return
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001815 new_time = time.time()
1816 print "Took %0.3fs to %s" %(new_time - self.last_time,
1817 ' '.join(str(x) for x in things))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001818 self.last_time = new_time
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001819
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04001820 def glyphs(self, glyphs, font=None):
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001821 self("Names: ", sorted(glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001822 if font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001823 reverseGlyphMap = font.getReverseGlyphMap()
1824 self("Gids : ", sorted(reverseGlyphMap[g] for g in glyphs))
Behdad Esfahbodf5497842013-08-08 21:57:02 -04001825
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001826 def font(self, font, file=sys.stdout):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001827 if not self.xml:
1828 return
1829 import xmlWriter
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001830 writer = xmlWriter.XMLWriter(file)
Behdad Esfahbod45a84602013-08-19 14:44:49 -04001831 font.disassembleInstructions = False # Work around ttLib bug
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001832 for tag in font.keys():
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001833 writer.begintag(tag)
1834 writer.newline()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001835 font[tag].toXML(writer, font)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001836 writer.endtag(tag)
1837 writer.newline()
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001838
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04001839
Behdad Esfahbod85da2682013-08-15 12:17:21 -04001840def load_font(fontFile,
Behdad Esfahbodadc47fd2013-08-15 18:29:25 -04001841 options,
Behdad Esfahbod85da2682013-08-15 12:17:21 -04001842 checkChecksums=False,
Behdad Esfahbod85da2682013-08-15 12:17:21 -04001843 dontLoadGlyphNames=False):
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04001844
Behdad Esfahbod45a84602013-08-19 14:44:49 -04001845 font = fontTools.ttLib.TTFont(fontFile,
1846 checkChecksums=checkChecksums,
1847 recalcBBoxes=options.recalc_bounds)
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04001848
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001849 # Hack:
1850 #
1851 # If we don't need glyph names, change 'post' class to not try to
1852 # load them. It avoid lots of headache with broken fonts as well
1853 # as loading time.
1854 #
1855 # Ideally ttLib should provide a way to ask it to skip loading
1856 # glyph names. But it currently doesn't provide such a thing.
1857 #
Behdad Esfahbod85da2682013-08-15 12:17:21 -04001858 if dontLoadGlyphNames:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001859 post = fontTools.ttLib.getTableClass('post')
1860 saved = post.decode_format_2_0
1861 post.decode_format_2_0 = post.decode_format_3_0
1862 f = font['post']
1863 if f.formatType == 2.0:
1864 f.formatType = 3.0
1865 post.decode_format_2_0 = saved
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04001866
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001867 return font
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04001868
Behdad Esfahbode911de12013-08-16 12:42:34 -04001869def save_font(font, outfile, options):
Behdad Esfahbodc6c3bb82013-08-15 17:46:20 -04001870 if options.flavor and not hasattr(font, 'flavor'):
1871 raise Exception("fonttools version does not support flavors.")
1872 font.flavor = options.flavor
Behdad Esfahbode911de12013-08-16 12:42:34 -04001873 font.save(outfile, reorderTables=options.canonical_order)
Behdad Esfahbod41de4cc2013-08-15 12:09:55 -04001874
Behdad Esfahbodb69400f2013-08-29 18:40:53 -04001875def main(args):
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001876
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001877 log = Logger()
1878 args = log.parse_opts(args)
Behdad Esfahbod4ae81712013-07-22 11:57:13 -04001879
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04001880 options = Options()
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001881 args = options.parse_opts(args, ignore_unknown=['text'])
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001882
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001883 if len(args) < 2:
Behdad Esfahbodb69400f2013-08-29 18:40:53 -04001884 print >>sys.stderr, "usage: pyftsubset font-file glyph... [--text=ABC]... [--option=value]..."
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001885 sys.exit(1)
Behdad Esfahbod02b92062013-07-21 18:40:59 -04001886
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001887 fontfile = args[0]
1888 args = args[1:]
Behdad Esfahbod02b92062013-07-21 18:40:59 -04001889
Behdad Esfahbod85da2682013-08-15 12:17:21 -04001890 dontLoadGlyphNames =(not options.glyph_names and
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001891 all(any(g.startswith(p)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001892 for p in ['gid', 'glyph', 'uni', 'U+'])
1893 for g in args))
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04001894
Behdad Esfahbodadc47fd2013-08-15 18:29:25 -04001895 font = load_font(fontfile, options, dontLoadGlyphNames=dontLoadGlyphNames)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001896 subsetter = Subsetter(options=options, log=log)
1897 log.lapse("load font")
Behdad Esfahbod02b92062013-07-21 18:40:59 -04001898
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001899 names = font.getGlyphNames()
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001900 log.lapse("loading glyph names")
Behdad Esfahbode7f5a892013-07-31 19:58:59 -04001901
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001902 glyphs = []
1903 unicodes = []
1904 text = ""
1905 for g in args:
1906 if g in names:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001907 glyphs.append(g)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001908 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001909 if g.startswith('--text='):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001910 text += g[7:]
1911 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001912 if g.startswith('uni') or g.startswith('U+'):
1913 if g.startswith('uni') and len(g) > 3:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001914 g = g[3:]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001915 elif g.startswith('U+') and len(g) > 2:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001916 g = g[2:]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001917 u = int(g, 16)
1918 unicodes.append(u)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001919 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001920 if g.startswith('gid') or g.startswith('glyph'):
1921 if g.startswith('gid') and len(g) > 3:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001922 g = g[3:]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001923 elif g.startswith('glyph') and len(g) > 5:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001924 g = g[5:]
1925 try:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001926 glyphs.append(font.getGlyphName(int(g), requireReal=1))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001927 except ValueError:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001928 raise Exception("Invalid glyph identifier: %s" % g)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001929 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001930 raise Exception("Invalid glyph identifier: %s" % g)
1931 log.lapse("compile glyph list")
1932 log("Unicodes:", unicodes)
1933 log("Glyphs:", glyphs)
Behdad Esfahbod6df089a2013-07-31 19:27:14 -04001934
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001935 subsetter.populate(glyphs=glyphs, unicodes=unicodes, text=text)
1936 subsetter.subset(font)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -04001937
Behdad Esfahbod34426c12013-08-14 18:30:09 -04001938 outfile = fontfile + '.subset'
1939
Behdad Esfahbodc6c3bb82013-08-15 17:46:20 -04001940 save_font (font, outfile, options)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001941 log.lapse("compile and save font")
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04001942
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001943 log.last_time = log.start_time
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001944 log.lapse("make one with everything(TOTAL TIME)")
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04001945
Behdad Esfahbod34426c12013-08-14 18:30:09 -04001946 if log.verbose:
1947 import os
1948 log("Input font: %d bytes" % os.path.getsize(fontfile))
1949 log("Subset font: %d bytes" % os.path.getsize(outfile))
1950
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001951 log.font(font)
Behdad Esfahbod8c486d82013-07-24 13:34:47 -04001952
Behdad Esfahbodc56bf482013-08-13 20:13:33 -04001953 font.close()
1954
Behdad Esfahbod39a39ac2013-08-22 18:10:17 -04001955
1956__all__ = [
Behdad Esfahbodb69400f2013-08-29 18:40:53 -04001957 'Options',
1958 'Subsetter',
1959 'Logger',
1960 'load_font',
1961 'save_font',
1962 'main'
Behdad Esfahbod39a39ac2013-08-22 18:10:17 -04001963]
1964
Behdad Esfahbod8c486d82013-07-24 13:34:47 -04001965if __name__ == '__main__':
Behdad Esfahbodb69400f2013-08-29 18:40:53 -04001966 main(sys.argv[1:])