blob: 910ad5679ab7c66dfdaf24c05f009e8a7f363db5 [file] [log] [blame]
Behdad Esfahbod54660612013-07-21 18:16:55 -04001#!/usr/bin/python
Behdad Esfahbod616d36e2013-08-13 20:02:59 -04002
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04003# Copyright 2013 Google, Inc. All Rights Reserved.
4#
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04005# Licensed under the Apache License, Version 2.0(the "License");
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04006# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17# Google Author(s): Behdad Esfahbod
Behdad Esfahbod616d36e2013-08-13 20:02:59 -040018
19"""Python OpenType Layout Subsetter.
20
21Later grown into full OpenType subsetter, supporting all standard tables.
22"""
23
Behdad Esfahbod54660612013-07-21 18:16:55 -040024
Behdad Esfahbodfa3bc5e2013-07-24 14:37:58 -040025# Try running on PyPy
26try:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040027 import numpypy
Behdad Esfahbodfa3bc5e2013-07-24 14:37:58 -040028except ImportError:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040029 pass
Behdad Esfahbodfa3bc5e2013-07-24 14:37:58 -040030
Behdad Esfahbod54660612013-07-21 18:16:55 -040031import fontTools.ttx
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -040032import struct
Behdad Esfahbod54660612013-07-21 18:16:55 -040033
Behdad Esfahbod54660612013-07-21 18:16:55 -040034
Behdad Esfahbod616d36e2013-08-13 20:02:59 -040035def __add_method(*clazzes):
36 """Returns a decorator function that adds a new method to one or
37 more classes."""
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040038 def wrapper(method):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040039 for clazz in clazzes:
40 assert clazz.__name__ != 'DefaultTable', 'Oops, table class not found.'
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040041 setattr(clazz, method.func_name, method)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040042 return None
43 return wrapper
Behdad Esfahbod54660612013-07-21 18:16:55 -040044
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040045def unique_sorted(l):
46 return sorted(set(l))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -040047
48
Behdad Esfahbod616d36e2013-08-13 20:02:59 -040049@__add_method(fontTools.ttLib.tables.otTables.Coverage)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040050def intersect(self, glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040051 "Returns ascending list of matching coverage values."
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040052 return [i for(i,g) in enumerate(self.glyphs) if g in glyphs]
Behdad Esfahbod610b0552013-07-23 14:52:18 -040053
Behdad Esfahbod616d36e2013-08-13 20:02:59 -040054@__add_method(fontTools.ttLib.tables.otTables.Coverage)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040055def intersect_glyphs(self, glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040056 "Returns set of intersecting glyphs."
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040057 return set(g for g in self.glyphs if g in glyphs)
Behdad Esfahbod849d25c2013-08-12 19:24:24 -040058
Behdad Esfahbod616d36e2013-08-13 20:02:59 -040059@__add_method(fontTools.ttLib.tables.otTables.Coverage)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040060def subset(self, glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040061 "Returns ascending list of remaining coverage values."
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040062 indices = self.intersect(glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040063 self.glyphs = [g for g in self.glyphs if g in glyphs]
64 return indices
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040065
Behdad Esfahbod616d36e2013-08-13 20:02:59 -040066@__add_method(fontTools.ttLib.tables.otTables.Coverage)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040067def remap(self, coverage_map):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040068 "Remaps coverage."
69 self.glyphs = [self.glyphs[i] for i in coverage_map]
Behdad Esfahbod14374262013-08-08 22:26:49 -040070
Behdad Esfahbod616d36e2013-08-13 20:02:59 -040071@__add_method(fontTools.ttLib.tables.otTables.ClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040072def intersect(self, glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040073 "Returns ascending list of matching class values."
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040074 return unique_sorted(
75 ([0] if any(g not in self.classDefs for g in glyphs) else []) +
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040076 [v for g,v in self.classDefs.iteritems() if g in glyphs])
Behdad Esfahbodb8d55882013-07-23 22:17:39 -040077
Behdad Esfahbod616d36e2013-08-13 20:02:59 -040078@__add_method(fontTools.ttLib.tables.otTables.ClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040079def intersect_class(self, glyphs, klass):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040080 "Returns set of glyphs matching class."
81 if klass == 0:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040082 return set(g for g in glyphs if g not in self.classDefs)
83 return set(g for g,v in self.classDefs.iteritems()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040084 if v == klass and g in glyphs)
Behdad Esfahbodb8d55882013-07-23 22:17:39 -040085
Behdad Esfahbod616d36e2013-08-13 20:02:59 -040086@__add_method(fontTools.ttLib.tables.otTables.ClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040087def subset(self, glyphs, remap=False):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040088 "Returns ascending list of remaining classes."
89 self.classDefs = {g:v for g,v in self.classDefs.iteritems() if g in glyphs}
90 # Note: while class 0 has the special meaning of "not matched",
91 # if no glyph will ever /not match/, we can optimize class 0 out too.
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040092 indices = unique_sorted(
93 ([0] if any(g not in self.classDefs for g in glyphs) else []) +
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040094 self.classDefs.itervalues())
95 if remap:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040096 self.remap(indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040097 return indices
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -040098
Behdad Esfahbod616d36e2013-08-13 20:02:59 -040099@__add_method(fontTools.ttLib.tables.otTables.ClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400100def remap(self, class_map):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400101 "Remaps classes."
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400102 self.classDefs = {g:class_map.index(v)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400103 for g,v in self.classDefs.iteritems()}
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400104
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400105@__add_method(fontTools.ttLib.tables.otTables.SingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400106def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400107 if cur_glyphs == None: cur_glyphs = s.glyphs
108 if self.Format in [1, 2]:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400109 s.glyphs.update(v for g,v in self.mapping.iteritems() if g in cur_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400110 else:
111 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400112
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400113@__add_method(fontTools.ttLib.tables.otTables.SingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400114def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400115 if self.Format in [1, 2]:
116 self.mapping = {g:v for g,v in self.mapping.iteritems()
117 if g in s.glyphs and v in s.glyphs}
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400118 return bool(self.mapping)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400119 else:
120 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400121
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400122@__add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400123def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400124 if cur_glyphs == None: cur_glyphs = s.glyphs
125 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400126 indices = self.Coverage.intersect(cur_glyphs)
127 s.glyphs.update(*(self.Sequence[i].Substitute for i in indices))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400128 else:
129 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400130
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400131@__add_method(fontTools.ttLib.tables.otTables.MultipleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400132def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400133 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400134 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400135 self.Sequence = [self.Sequence[i] for i in indices]
136 # Now drop rules generating glyphs we don't want
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400137 indices = [i for i,seq in enumerate(self.Sequence)
138 if all(sub in s.glyphs for sub in seq.Substitute)]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400139 self.Sequence = [self.Sequence[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400140 self.Coverage.remap(indices)
141 self.SequenceCount = len(self.Sequence)
142 return bool(self.SequenceCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400143 else:
144 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400145
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400146@__add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400147def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400148 if cur_glyphs == None: cur_glyphs = s.glyphs
149 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400150 s.glyphs.update(*(vlist for g,vlist in self.alternates.iteritems()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400151 if g in cur_glyphs))
152 else:
153 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400154
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400155@__add_method(fontTools.ttLib.tables.otTables.AlternateSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400156def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400157 if self.Format == 1:
158 self.alternates = {g:vlist for g,vlist in self.alternates.iteritems()
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400159 if g in s.glyphs and all(v in s.glyphs for v in vlist)}
160 return bool(self.alternates)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400161 else:
162 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400163
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400164@__add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400165def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400166 if cur_glyphs == None: cur_glyphs = s.glyphs
167 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400168 s.glyphs.update(*([seq.LigGlyph for seq in seqs
169 if all(c in s.glyphs for c in seq.Component)]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400170 for g,seqs in self.ligatures.iteritems()
171 if g in cur_glyphs))
172 else:
173 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400174
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400175@__add_method(fontTools.ttLib.tables.otTables.LigatureSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400176def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400177 if self.Format == 1:
178 self.ligatures = {g:v for g,v in self.ligatures.iteritems()
179 if g in s.glyphs}
180 self.ligatures = {g:[seq for seq in seqs
181 if seq.LigGlyph in s.glyphs and
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400182 all(c in s.glyphs for c in seq.Component)]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400183 for g,seqs in self.ligatures.iteritems()}
184 self.ligatures = {g:v for g,v in self.ligatures.iteritems() if v}
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400185 return bool(self.ligatures)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400186 else:
187 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400188
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400189@__add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400190def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400191 if cur_glyphs == None: cur_glyphs = s.glyphs
192 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400193 indices = self.Coverage.intersect(cur_glyphs)
194 if(not indices or
195 not all(c.intersect(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400196 for c in self.LookAheadCoverage + self.BacktrackCoverage)):
197 return
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400198 s.glyphs.update(self.Substitute[i] for i in indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400199 else:
200 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400201
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400202@__add_method(fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400203def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400204 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400205 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400206 self.Substitute = [self.Substitute[i] for i in indices]
207 # Now drop rules generating glyphs we don't want
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400208 indices = [i for i,sub in enumerate(self.Substitute)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400209 if sub in s.glyphs]
210 self.Substitute = [self.Substitute[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400211 self.Coverage.remap(indices)
212 self.GlyphCount = len(self.Substitute)
213 return bool(self.GlyphCount and
214 all(c.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400215 for c in self.LookAheadCoverage+self.BacktrackCoverage))
216 else:
217 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400218
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400219@__add_method(fontTools.ttLib.tables.otTables.SinglePos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400220def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400221 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400222 return len(self.Coverage.subset(s.glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400223 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400224 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400225 self.Value = [self.Value[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400226 self.ValueCount = len(self.Value)
227 return bool(self.ValueCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400228 else:
229 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400230
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400231@__add_method(fontTools.ttLib.tables.otTables.PairPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400232def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400233 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400234 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400235 self.PairSet = [self.PairSet[i] for i in indices]
236 for p in self.PairSet:
237 p.PairValueRecord = [r for r in p.PairValueRecord
238 if r.SecondGlyph in s.glyphs]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400239 p.PairValueCount = len(p.PairValueRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400240 self.PairSet = [p for p in self.PairSet if p.PairValueCount]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400241 self.PairSetCount = len(self.PairSet)
242 return bool(self.PairSetCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400243 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400244 class1_map = self.ClassDef1.subset(s.glyphs, remap=True)
245 class2_map = self.ClassDef2.subset(s.glyphs, remap=True)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400246 self.Class1Record = [self.Class1Record[i] for i in class1_map]
247 for c in self.Class1Record:
248 c.Class2Record = [c.Class2Record[i] for i in class2_map]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400249 self.Class1Count = len(class1_map)
250 self.Class2Count = len(class2_map)
251 return bool(self.Class1Count and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400252 self.Class2Count and
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400253 self.Coverage.subset(s.glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400254 else:
255 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400256
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400257@__add_method(fontTools.ttLib.tables.otTables.CursivePos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400258def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400259 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400260 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400261 self.EntryExitRecord = [self.EntryExitRecord[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400262 self.EntryExitCount = len(self.EntryExitRecord)
263 return bool(self.EntryExitCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400264 else:
265 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400266
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400267@__add_method(fontTools.ttLib.tables.otTables.MarkBasePos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400268def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400269 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400270 mark_indices = self.MarkCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400271 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i]
272 for i in mark_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400273 self.MarkArray.MarkCount = len(self.MarkArray.MarkRecord)
274 base_indices = self.BaseCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400275 self.BaseArray.BaseRecord = [self.BaseArray.BaseRecord[i]
276 for i in base_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400277 self.BaseArray.BaseCount = len(self.BaseArray.BaseRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400278 # Prune empty classes
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400279 class_indices = unique_sorted(v.Class for v in self.MarkArray.MarkRecord)
280 self.ClassCount = len(class_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400281 for m in self.MarkArray.MarkRecord:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400282 m.Class = class_indices.index(m.Class)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400283 for b in self.BaseArray.BaseRecord:
284 b.BaseAnchor = [b.BaseAnchor[i] for i in class_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400285 return bool(self.ClassCount and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400286 self.MarkArray.MarkCount and
287 self.BaseArray.BaseCount)
288 else:
289 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400290
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400291@__add_method(fontTools.ttLib.tables.otTables.MarkLigPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400292def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400293 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400294 mark_indices = self.MarkCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400295 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i]
296 for i in mark_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400297 self.MarkArray.MarkCount = len(self.MarkArray.MarkRecord)
298 ligature_indices = self.LigatureCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400299 self.LigatureArray.LigatureAttach = [self.LigatureArray.LigatureAttach[i]
300 for i in ligature_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400301 self.LigatureArray.LigatureCount = len(self.LigatureArray.LigatureAttach)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400302 # Prune empty classes
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400303 class_indices = unique_sorted(v.Class for v in self.MarkArray.MarkRecord)
304 self.ClassCount = len(class_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400305 for m in self.MarkArray.MarkRecord:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400306 m.Class = class_indices.index(m.Class)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400307 for l in self.LigatureArray.LigatureAttach:
308 for c in l.ComponentRecord:
309 c.LigatureAnchor = [c.LigatureAnchor[i] for i in class_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400310 return bool(self.ClassCount and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400311 self.MarkArray.MarkCount and
312 self.LigatureArray.LigatureCount)
313 else:
314 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400315
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400316@__add_method(fontTools.ttLib.tables.otTables.MarkMarkPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400317def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400318 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400319 mark1_indices = self.Mark1Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400320 self.Mark1Array.MarkRecord = [self.Mark1Array.MarkRecord[i]
321 for i in mark1_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400322 self.Mark1Array.MarkCount = len(self.Mark1Array.MarkRecord)
323 mark2_indices = self.Mark2Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400324 self.Mark2Array.Mark2Record = [self.Mark2Array.Mark2Record[i]
325 for i in mark2_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400326 self.Mark2Array.MarkCount = len(self.Mark2Array.Mark2Record)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400327 # Prune empty classes
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400328 class_indices = unique_sorted(v.Class for v in self.Mark1Array.MarkRecord)
329 self.ClassCount = len(class_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400330 for m in self.Mark1Array.MarkRecord:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400331 m.Class = class_indices.index(m.Class)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400332 for b in self.Mark2Array.Mark2Record:
333 b.Mark2Anchor = [b.Mark2Anchor[i] for i in class_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400334 return bool(self.ClassCount and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400335 self.Mark1Array.MarkCount and
336 self.Mark2Array.MarkCount)
337 else:
338 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400339
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400340@__add_method(fontTools.ttLib.tables.otTables.SingleSubst,
341 fontTools.ttLib.tables.otTables.MultipleSubst,
342 fontTools.ttLib.tables.otTables.AlternateSubst,
343 fontTools.ttLib.tables.otTables.LigatureSubst,
344 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
345 fontTools.ttLib.tables.otTables.SinglePos,
346 fontTools.ttLib.tables.otTables.PairPos,
347 fontTools.ttLib.tables.otTables.CursivePos,
348 fontTools.ttLib.tables.otTables.MarkBasePos,
349 fontTools.ttLib.tables.otTables.MarkLigPos,
350 fontTools.ttLib.tables.otTables.MarkMarkPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400351def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400352 pass
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400353
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400354@__add_method(fontTools.ttLib.tables.otTables.SingleSubst,
355 fontTools.ttLib.tables.otTables.MultipleSubst,
356 fontTools.ttLib.tables.otTables.AlternateSubst,
357 fontTools.ttLib.tables.otTables.LigatureSubst,
358 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst,
359 fontTools.ttLib.tables.otTables.SinglePos,
360 fontTools.ttLib.tables.otTables.PairPos,
361 fontTools.ttLib.tables.otTables.CursivePos,
362 fontTools.ttLib.tables.otTables.MarkBasePos,
363 fontTools.ttLib.tables.otTables.MarkLigPos,
364 fontTools.ttLib.tables.otTables.MarkMarkPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400365def collect_lookups(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400366 return []
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400367
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400368@__add_method(fontTools.ttLib.tables.otTables.SingleSubst,
369 fontTools.ttLib.tables.otTables.AlternateSubst,
370 fontTools.ttLib.tables.otTables.ReverseChainSingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400371def may_have_non_1to1(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400372 return False
Behdad Esfahbodaeacc152013-08-12 20:24:33 -0400373
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400374@__add_method(fontTools.ttLib.tables.otTables.MultipleSubst,
375 fontTools.ttLib.tables.otTables.LigatureSubst,
376 fontTools.ttLib.tables.otTables.ContextSubst,
377 fontTools.ttLib.tables.otTables.ChainContextSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400378def may_have_non_1to1(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400379 return True
Behdad Esfahbodaeacc152013-08-12 20:24:33 -0400380
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400381@__add_method(fontTools.ttLib.tables.otTables.ContextSubst,
382 fontTools.ttLib.tables.otTables.ChainContextSubst,
383 fontTools.ttLib.tables.otTables.ContextPos,
384 fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400385def __classify_context(self):
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400386
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400387 class ContextHelper:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400388 def __init__(self, klass, Format):
389 if klass.__name__.endswith('Subst'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400390 Typ = 'Sub'
391 Type = 'Subst'
392 else:
393 Typ = 'Pos'
394 Type = 'Pos'
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400395 if klass.__name__.startswith('Chain'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400396 Chain = 'Chain'
397 else:
398 Chain = ''
399 ChainTyp = Chain+Typ
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400400
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400401 self.Typ = Typ
402 self.Type = Type
403 self.Chain = Chain
404 self.ChainTyp = ChainTyp
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400405
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400406 self.LookupRecord = Type+'LookupRecord'
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400407
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400408 if Format == 1:
409 Coverage = lambda r: r.Coverage
410 ChainCoverage = lambda r: r.Coverage
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400411 ContextData = lambda r:(None,)
412 ChainContextData = lambda r:(None, None, None)
413 RuleData = lambda r:(r.Input,)
414 ChainRuleData = lambda r:(r.Backtrack, r.Input, r.LookAhead)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400415 SetRuleData = None
416 ChainSetRuleData = None
417 elif Format == 2:
418 Coverage = lambda r: r.Coverage
419 ChainCoverage = lambda r: r.Coverage
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400420 ContextData = lambda r:(r.ClassDef,)
421 ChainContextData = lambda r:(r.LookAheadClassDef,
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400422 r.InputClassDef,
423 r.BacktrackClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400424 RuleData = lambda r:(r.Class,)
425 ChainRuleData = lambda r:(r.LookAhead, r.Input, r.Backtrack)
426 def SetRuleData(r, d):(r.Class,) = d
427 def ChainSetRuleData(r, d):(r.LookAhead, r.Input, r.Backtrack) = d
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400428 elif Format == 3:
429 Coverage = lambda r: r.Coverage[0]
430 ChainCoverage = lambda r: r.InputCoverage[0]
431 ContextData = None
432 ChainContextData = None
433 RuleData = lambda r: r.Coverage
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400434 ChainRuleData = lambda r:(r.LookAheadCoverage +
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400435 r.InputCoverage +
436 r.BacktrackCoverage)
437 SetRuleData = None
438 ChainSetRuleData = None
439 else:
440 assert 0, "unknown format: %s" % Format
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400441
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400442 if Chain:
443 self.Coverage = ChainCoverage
444 self.ContextData = ChainContextData
445 self.RuleData = ChainRuleData
446 self.SetRuleData = ChainSetRuleData
447 else:
448 self.Coverage = Coverage
449 self.ContextData = ContextData
450 self.RuleData = RuleData
451 self.SetRuleData = SetRuleData
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400452
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400453 if Format == 1:
454 self.Rule = ChainTyp+'Rule'
455 self.RuleCount = ChainTyp+'RuleCount'
456 self.RuleSet = ChainTyp+'RuleSet'
457 self.RuleSetCount = ChainTyp+'RuleSetCount'
458 self.Intersect = lambda glyphs, c, r: [r] if r in glyphs else []
459 elif Format == 2:
460 self.Rule = ChainTyp+'ClassRule'
461 self.RuleCount = ChainTyp+'ClassRuleCount'
462 self.RuleSet = ChainTyp+'ClassSet'
463 self.RuleSetCount = ChainTyp+'ClassSetCount'
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400464 self.Intersect = lambda glyphs, c, r: c.intersect_class(glyphs, r)
Behdad Esfahbod89987002013-07-23 23:07:42 -0400465
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400466 self.ClassDef = 'InputClassDef' if Chain else 'ClassDef'
Behdad Esfahbod27108392013-07-23 16:40:47 -0400467
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400468 if self.Format not in [1, 2, 3]:
469 return None # Don't shoot the messenger; let it go
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400470 if not hasattr(self.__class__, "__ContextHelpers"):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400471 self.__class__.__ContextHelpers = {}
472 if self.Format not in self.__class__.__ContextHelpers:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400473 helper = ContextHelper(self.__class__, self.Format)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400474 self.__class__.__ContextHelpers[self.Format] = helper
475 return self.__class__.__ContextHelpers[self.Format]
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400476
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400477@__add_method(fontTools.ttLib.tables.otTables.ContextSubst,
478 fontTools.ttLib.tables.otTables.ChainContextSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400479def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400480 if cur_glyphs == None: cur_glyphs = s.glyphs
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400481 c = self.__classify_context()
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400482
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400483 indices = c.Coverage(self).intersect(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400484 if not indices:
485 return []
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400486 cur_glyphs = c.Coverage(self).intersect_glyphs(s.glyphs);
Behdad Esfahbod1d4fa132013-08-08 22:59:32 -0400487
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400488 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400489 ContextData = c.ContextData(self)
490 rss = getattr(self, c.RuleSet)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400491 for i in indices:
492 if not rss[i]: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400493 for r in getattr(rss[i], c.Rule):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400494 if not r: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400495 if all(all(c.Intersect(s.glyphs, cd, k) for k in klist)
496 for cd,klist in zip(ContextData, c.RuleData(r))):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400497 chaos = False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400498 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400499 if not ll: continue
500 seqi = ll.SequenceIndex
501 if seqi == 0:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400502 pos_glyphs = set(c.Coverage(self).glyphs[i])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400503 else:
504 if chaos:
505 pos_glyphs = s.glyphs
506 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400507 pos_glyphs = set(r.Input[seqi - 1])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400508 lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400509 chaos = chaos or lookup.may_have_non_1to1()
510 lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400511 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400512 ClassDef = getattr(self, c.ClassDef)
513 indices = ClassDef.intersect(cur_glyphs)
514 ContextData = c.ContextData(self)
515 rss = getattr(self, c.RuleSet)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400516 for i in indices:
517 if not rss[i]: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400518 for r in getattr(rss[i], c.Rule):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400519 if not r: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400520 if all(all(c.Intersect(s.glyphs, cd, k) for k in klist)
521 for cd,klist in zip(ContextData, c.RuleData(r))):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400522 chaos = False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400523 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400524 if not ll: continue
525 seqi = ll.SequenceIndex
526 if seqi == 0:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400527 pos_glyphs = ClassDef.intersect_class(cur_glyphs, i)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400528 else:
529 if chaos:
530 pos_glyphs = s.glyphs
531 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400532 pos_glyphs = ClassDef.intersect_class(s.glyphs,
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400533 r.Input[seqi - 1])
534 lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400535 chaos = chaos or lookup.may_have_non_1to1()
536 lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400537 elif self.Format == 3:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400538 if not all(x.intersect(s.glyphs) for x in c.RuleData(self)):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400539 return []
540 r = self
541 chaos = False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400542 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400543 if not ll: continue
544 seqi = ll.SequenceIndex
545 if seqi == 0:
546 pos_glyphs = cur_glyphs
547 else:
548 if chaos:
549 pos_glyphs = s.glyphs
550 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400551 pos_glyphs = r.InputCoverage[seqi].intersect_glyphs(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400552 lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400553 chaos = chaos or lookup.may_have_non_1to1()
554 lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400555 else:
556 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod00776972013-07-23 15:33:00 -0400557
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400558@__add_method(fontTools.ttLib.tables.otTables.ContextSubst,
559 fontTools.ttLib.tables.otTables.ContextPos,
560 fontTools.ttLib.tables.otTables.ChainContextSubst,
561 fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400562def subset_glyphs(self, s):
563 c = self.__classify_context()
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400564
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400565 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400566 indices = self.Coverage.subset(s.glyphs)
567 rss = getattr(self, c.RuleSet)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400568 rss = [rss[i] for i in indices]
569 for rs in rss:
570 if not rs: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400571 ss = getattr(rs, c.Rule)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400572 ss = [r for r in ss
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400573 if r and all(all(g in s.glyphs for g in glist)
574 for glist in c.RuleData(r))]
575 setattr(rs, c.Rule, ss)
576 setattr(rs, c.RuleCount, len(ss))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400577 # Prune empty subrulesets
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400578 rss = [rs for rs in rss if rs and getattr(rs, c.Rule)]
579 setattr(self, c.RuleSet, rss)
580 setattr(self, c.RuleSetCount, len(rss))
581 return bool(rss)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400582 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400583 if not self.Coverage.subset(s.glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400584 return False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400585 indices = getattr(self, c.ClassDef).subset(self.Coverage.glyphs,
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400586 remap=False)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400587 rss = getattr(self, c.RuleSet)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400588 rss = [rss[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400589 ContextData = c.ContextData(self)
590 klass_maps = [x.subset(s.glyphs, remap=True) for x in ContextData]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400591 for rs in rss:
592 if not rs: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400593 ss = getattr(rs, c.Rule)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400594 ss = [r for r in ss
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400595 if r and all(all(k in klass_map for k in klist)
596 for klass_map,klist in zip(klass_maps, c.RuleData(r)))]
597 setattr(rs, c.Rule, ss)
598 setattr(rs, c.RuleCount, len(ss))
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400599
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400600 # Remap rule classes
601 for r in ss:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400602 c.SetRuleData(r, [[klass_map.index(k) for k in klist]
603 for klass_map,klist in zip(klass_maps, c.RuleData(r))])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400604 # Prune empty subrulesets
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400605 rss = [rs for rs in rss if rs and getattr(rs, c.Rule)]
606 setattr(self, c.RuleSet, rss)
607 setattr(self, c.RuleSetCount, len(rss))
608 return bool(rss)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400609 elif self.Format == 3:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400610 return all(x.subset(s.glyphs) for x in c.RuleData(self))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400611 else:
612 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400613
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400614@__add_method(fontTools.ttLib.tables.otTables.ContextSubst,
615 fontTools.ttLib.tables.otTables.ChainContextSubst,
616 fontTools.ttLib.tables.otTables.ContextPos,
617 fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400618def subset_lookups(self, lookup_indices):
619 c = self.__classify_context()
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400620
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400621 if self.Format in [1, 2]:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400622 for rs in getattr(self, c.RuleSet):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400623 if not rs: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400624 for r in getattr(rs, c.Rule):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400625 if not r: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400626 setattr(r, c.LookupRecord,
627 [ll for ll in getattr(r, c.LookupRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400628 if ll and ll.LookupListIndex in lookup_indices])
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400629 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400630 if not ll: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400631 ll.LookupListIndex = lookup_indices.index(ll.LookupListIndex)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400632 elif self.Format == 3:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400633 setattr(self, c.LookupRecord,
634 [ll for ll in getattr(self, c.LookupRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400635 if ll and ll.LookupListIndex in lookup_indices])
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400636 for ll in getattr(self, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400637 if not ll: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400638 ll.LookupListIndex = lookup_indices.index(ll.LookupListIndex)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400639 else:
640 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400641
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400642@__add_method(fontTools.ttLib.tables.otTables.ContextSubst,
643 fontTools.ttLib.tables.otTables.ChainContextSubst,
644 fontTools.ttLib.tables.otTables.ContextPos,
645 fontTools.ttLib.tables.otTables.ChainContextPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400646def collect_lookups(self):
647 c = self.__classify_context()
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400648
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400649 if self.Format in [1, 2]:
650 return [ll.LookupListIndex
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400651 for rs in getattr(self, c.RuleSet) if rs
652 for r in getattr(rs, c.Rule) if r
653 for ll in getattr(r, c.LookupRecord) if ll]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400654 elif self.Format == 3:
655 return [ll.LookupListIndex
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400656 for ll in getattr(self, c.LookupRecord) if ll]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400657 else:
658 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400659
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400660@__add_method(fontTools.ttLib.tables.otTables.ExtensionSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400661def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400662 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400663 self.ExtSubTable.closure_glyphs(s, cur_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400664 else:
665 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400666
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400667@__add_method(fontTools.ttLib.tables.otTables.ExtensionSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400668def may_have_non_1to1(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400669 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400670 return self.ExtSubTable.may_have_non_1to1()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400671 else:
672 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbodaeacc152013-08-12 20:24:33 -0400673
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400674@__add_method(fontTools.ttLib.tables.otTables.ExtensionSubst,
675 fontTools.ttLib.tables.otTables.ExtensionPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400676def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400677 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400678 return self.ExtSubTable.subset_glyphs(s)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400679 else:
680 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400681
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400682@__add_method(fontTools.ttLib.tables.otTables.ExtensionSubst,
683 fontTools.ttLib.tables.otTables.ExtensionPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400684def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400685 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400686 return self.ExtSubTable.subset_lookups(lookup_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400687 else:
688 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400689
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400690@__add_method(fontTools.ttLib.tables.otTables.ExtensionSubst,
691 fontTools.ttLib.tables.otTables.ExtensionPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400692def collect_lookups(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400693 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400694 return self.ExtSubTable.collect_lookups()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400695 else:
696 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400697
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400698@__add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400699def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400700 for st in self.SubTable:
701 if not st: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400702 st.closure_glyphs(s, cur_glyphs)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400703
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400704@__add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400705def subset_glyphs(self, s):
706 self.SubTable = [st for st in self.SubTable if st and st.subset_glyphs(s)]
707 self.SubTableCount = len(self.SubTable)
708 return bool(self.SubTableCount)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400709
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400710@__add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400711def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400712 for s in self.SubTable:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400713 s.subset_lookups(lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400714
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400715@__add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400716def collect_lookups(self):
717 return unique_sorted(sum((st.collect_lookups() for st in self.SubTable
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400718 if st), []))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400719
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400720@__add_method(fontTools.ttLib.tables.otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400721def may_have_non_1to1(self):
722 return any(st.may_have_non_1to1() for st in self.SubTable if st)
Behdad Esfahbodaeacc152013-08-12 20:24:33 -0400723
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400724@__add_method(fontTools.ttLib.tables.otTables.LookupList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400725def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400726 "Returns the indices of nonempty lookups."
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400727 return [i for(i,l) in enumerate(self.Lookup) if l and l.subset_glyphs(s)]
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400728
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400729@__add_method(fontTools.ttLib.tables.otTables.LookupList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400730def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400731 self.Lookup = [self.Lookup[i] for i in lookup_indices
732 if i < self.LookupCount]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400733 self.LookupCount = len(self.Lookup)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400734 for l in self.Lookup:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400735 l.subset_lookups(lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400736
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400737@__add_method(fontTools.ttLib.tables.otTables.LookupList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400738def closure_lookups(self, lookup_indices):
739 lookup_indices = unique_sorted(lookup_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400740 recurse = lookup_indices
741 while True:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400742 recurse_lookups = sum((self.Lookup[i].collect_lookups()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400743 for i in recurse if i < self.LookupCount), [])
744 recurse_lookups = [l for l in recurse_lookups
745 if l not in lookup_indices and l < self.LookupCount]
746 if not recurse_lookups:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400747 return unique_sorted(lookup_indices)
748 recurse_lookups = unique_sorted(recurse_lookups)
749 lookup_indices.extend(recurse_lookups)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400750 recurse = recurse_lookups
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400751
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400752@__add_method(fontTools.ttLib.tables.otTables.Feature)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400753def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400754 self.LookupListIndex = [l for l in self.LookupListIndex
755 if l in lookup_indices]
756 # Now map them.
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400757 self.LookupListIndex = [lookup_indices.index(l)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400758 for l in self.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400759 self.LookupCount = len(self.LookupListIndex)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400760 return self.LookupCount
Behdad Esfahbod54660612013-07-21 18:16:55 -0400761
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400762@__add_method(fontTools.ttLib.tables.otTables.Feature)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400763def collect_lookups(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400764 return self.LookupListIndex[:]
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400765
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400766@__add_method(fontTools.ttLib.tables.otTables.FeatureList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400767def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400768 "Returns the indices of nonempty features."
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400769 feature_indices = [i for(i,f) in enumerate(self.FeatureRecord)
770 if f.Feature.subset_lookups(lookup_indices)]
771 self.subset_features(feature_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400772 return feature_indices
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400773
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400774@__add_method(fontTools.ttLib.tables.otTables.FeatureList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400775def collect_lookups(self, feature_indices):
776 return unique_sorted(sum((self.FeatureRecord[i].Feature.collect_lookups()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400777 for i in feature_indices
778 if i < self.FeatureCount), []))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400779
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400780@__add_method(fontTools.ttLib.tables.otTables.FeatureList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400781def subset_features(self, feature_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400782 self.FeatureRecord = [self.FeatureRecord[i] for i in feature_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400783 self.FeatureCount = len(self.FeatureRecord)
784 return bool(self.FeatureCount)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400785
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400786@__add_method(fontTools.ttLib.tables.otTables.DefaultLangSys,
787 fontTools.ttLib.tables.otTables.LangSys)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400788def subset_features(self, feature_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400789 if self.ReqFeatureIndex in feature_indices:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400790 self.ReqFeatureIndex = feature_indices.index(self.ReqFeatureIndex)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400791 else:
792 self.ReqFeatureIndex = 65535
793 self.FeatureIndex = [f for f in self.FeatureIndex if f in feature_indices]
794 # Now map them.
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400795 self.FeatureIndex = [feature_indices.index(f) for f in self.FeatureIndex
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400796 if f in feature_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400797 self.FeatureCount = len(self.FeatureIndex)
798 return bool(self.FeatureCount or self.ReqFeatureIndex != 65535)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400799
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400800@__add_method(fontTools.ttLib.tables.otTables.DefaultLangSys,
801 fontTools.ttLib.tables.otTables.LangSys)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400802def collect_features(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400803 feature_indices = self.FeatureIndex[:]
804 if self.ReqFeatureIndex != 65535:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400805 feature_indices.append(self.ReqFeatureIndex)
806 return unique_sorted(feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400807
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400808@__add_method(fontTools.ttLib.tables.otTables.Script)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400809def subset_features(self, feature_indices):
810 if(self.DefaultLangSys and
811 not self.DefaultLangSys.subset_features(feature_indices)):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400812 self.DefaultLangSys = None
813 self.LangSysRecord = [l for l in self.LangSysRecord
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400814 if l.LangSys.subset_features(feature_indices)]
815 self.LangSysCount = len(self.LangSysRecord)
816 return bool(self.LangSysCount or self.DefaultLangSys)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400817
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400818@__add_method(fontTools.ttLib.tables.otTables.Script)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400819def collect_features(self):
820 feature_indices = [l.LangSys.collect_features() for l in self.LangSysRecord]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400821 if self.DefaultLangSys:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400822 feature_indices.append(self.DefaultLangSys.collect_features())
823 return unique_sorted(sum(feature_indices, []))
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400824
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400825@__add_method(fontTools.ttLib.tables.otTables.ScriptList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400826def subset_features(self, feature_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400827 self.ScriptRecord = [s for s in self.ScriptRecord
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400828 if s.Script.subset_features(feature_indices)]
829 self.ScriptCount = len(self.ScriptRecord)
830 return bool(self.ScriptCount)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400831
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400832@__add_method(fontTools.ttLib.tables.otTables.ScriptList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400833def collect_features(self):
834 return unique_sorted(sum((s.Script.collect_features()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400835 for s in self.ScriptRecord), []))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400836
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400837@__add_method(fontTools.ttLib.getTableClass('GSUB'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400838def closure_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400839 s.table = self.table
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400840 feature_indices = self.table.ScriptList.collect_features()
841 lookup_indices = self.table.FeatureList.collect_lookups(feature_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400842 while True:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400843 orig_glyphs = s.glyphs.copy()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400844 for i in lookup_indices:
845 if i >= self.table.LookupList.LookupCount: continue
846 if not self.table.LookupList.Lookup[i]: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400847 self.table.LookupList.Lookup[i].closure_glyphs(s)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400848 if orig_glyphs == s.glyphs:
849 break
850 del s.table
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400851
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400852@__add_method(fontTools.ttLib.getTableClass('GSUB'),
853 fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400854def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400855 s.glyphs = s.glyphs_gsubed
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400856 lookup_indices = self.table.LookupList.subset_glyphs(s)
857 self.subset_lookups(lookup_indices)
858 self.prune_lookups()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400859 return True
Behdad Esfahbod02b92062013-07-21 18:40:59 -0400860
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400861@__add_method(fontTools.ttLib.getTableClass('GSUB'),
862 fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400863def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400864 """Retrains specified lookups, then removes empty features, language
865 systems, and scripts."""
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400866 self.table.LookupList.subset_lookups(lookup_indices)
867 feature_indices = self.table.FeatureList.subset_lookups(lookup_indices)
868 self.table.ScriptList.subset_features(feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400869
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400870@__add_method(fontTools.ttLib.getTableClass('GSUB'),
871 fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400872def prune_lookups(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400873 "Remove unreferenced lookups"
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400874 feature_indices = self.table.ScriptList.collect_features()
875 lookup_indices = self.table.FeatureList.collect_lookups(feature_indices)
876 lookup_indices = self.table.LookupList.closure_lookups(lookup_indices)
877 self.subset_lookups(lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400878
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400879@__add_method(fontTools.ttLib.getTableClass('GSUB'),
880 fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400881def subset_feature_tags(self, feature_tags):
882 feature_indices = [i for(i,f) in
883 enumerate(self.table.FeatureList.FeatureRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400884 if f.FeatureTag in feature_tags]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400885 self.table.FeatureList.subset_features(feature_indices)
886 self.table.ScriptList.subset_features(feature_indices)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400887
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400888@__add_method(fontTools.ttLib.getTableClass('GSUB'),
889 fontTools.ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400890def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400891 if options.layout_features and '*' not in options.layout_features:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400892 self.subset_feature_tags(options.layout_features)
893 self.prune_lookups()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400894 return True
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400895
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400896@__add_method(fontTools.ttLib.getTableClass('GDEF'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400897def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400898 glyphs = s.glyphs_gsubed
899 table = self.table
900 if table.LigCaretList:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400901 indices = table.LigCaretList.Coverage.subset(glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400902 table.LigCaretList.LigGlyph = [table.LigCaretList.LigGlyph[i]
903 for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400904 table.LigCaretList.LigGlyphCount = len(table.LigCaretList.LigGlyph)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400905 if not table.LigCaretList.LigGlyphCount:
906 table.LigCaretList = None
907 if table.MarkAttachClassDef:
908 table.MarkAttachClassDef.classDefs = {g:v for g,v in
909 table.MarkAttachClassDef.classDefs.iteritems()
910 if g in glyphs}
911 if not table.MarkAttachClassDef.classDefs:
912 table.MarkAttachClassDef = None
913 if table.GlyphClassDef:
914 table.GlyphClassDef.classDefs = {g:v for g,v in
915 table.GlyphClassDef.classDefs.iteritems()
916 if g in glyphs}
917 if not table.GlyphClassDef.classDefs:
918 table.GlyphClassDef = None
919 if table.AttachList:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400920 indices = table.AttachList.Coverage.subset(glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400921 table.AttachList.AttachPoint = [table.AttachList.AttachPoint[i]
922 for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400923 table.AttachList.GlyphCount = len(table.AttachList.AttachPoint)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400924 if not table.AttachList.GlyphCount:
925 table.AttachList = None
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400926 return bool(table.LigCaretList or
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400927 table.MarkAttachClassDef or
928 table.GlyphClassDef or
929 table.AttachList)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400930
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400931@__add_method(fontTools.ttLib.getTableClass('kern'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400932def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400933 # Prune unknown kern table types
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400934 self.kernTables = [t for t in self.kernTables if hasattr(t, 'kernTable')]
935 return bool(self.kernTables)
Behdad Esfahbodd4e33a72013-07-24 18:51:05 -0400936
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400937@__add_method(fontTools.ttLib.getTableClass('kern'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400938def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400939 glyphs = s.glyphs_gsubed
940 for t in self.kernTables:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400941 t.kernTable = {(a,b):v for((a,b),v) in t.kernTable.iteritems()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400942 if a in glyphs and b in glyphs}
943 self.kernTables = [t for t in self.kernTables if t.kernTable]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400944 return bool(self.kernTables)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -0400945
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400946@__add_method(fontTools.ttLib.getTableClass('hmtx'),
947 fontTools.ttLib.getTableClass('vmtx'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400948def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400949 self.metrics = {g:v for g,v in self.metrics.iteritems() if g in s.glyphs}
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400950 return bool(self.metrics)
Behdad Esfahbodc7160442013-07-22 14:29:08 -0400951
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400952@__add_method(fontTools.ttLib.getTableClass('hdmx'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400953def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400954 self.hdmx = {sz:{g:v for g,v in l.iteritems() if g in s.glyphs}
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400955 for(sz,l) in self.hdmx.iteritems()}
956 return bool(self.hdmx)
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -0400957
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400958@__add_method(fontTools.ttLib.getTableClass('VORG'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400959def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400960 self.VOriginRecords = {g:v for g,v in self.VOriginRecords.iteritems()
961 if g in s.glyphs}
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400962 self.numVertOriginYMetrics = len(self.VOriginRecords)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400963 return True # Never drop; has default metrics
Behdad Esfahbode45d6af2013-07-22 15:29:17 -0400964
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400965@__add_method(fontTools.ttLib.getTableClass('post'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400966def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400967 if not options.glyph_names:
968 self.formatType = 3.0
969 return True
Behdad Esfahbod42648242013-07-23 12:56:06 -0400970
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400971@__add_method(fontTools.ttLib.getTableClass('post'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400972def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400973 self.extraNames = [] # This seems to do it
974 return True
Behdad Esfahbod653e9742013-07-22 15:17:12 -0400975
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -0400976# Copied from _g_l_y_f.py
977ARG_1_AND_2_ARE_WORDS = 0x0001 # if set args are words otherwise they are bytes
978ARGS_ARE_XY_VALUES = 0x0002 # if set args are xy values, otherwise they are points
979ROUND_XY_TO_GRID = 0x0004 # for the xy values if above is true
980WE_HAVE_A_SCALE = 0x0008 # Sx = Sy, otherwise scale == 1.0
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400981NON_OVERLAPPING = 0x0010 # set to same value for all components(obsolete!)
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -0400982MORE_COMPONENTS = 0x0020 # indicates at least one more glyph after this one
983WE_HAVE_AN_X_AND_Y_SCALE = 0x0040 # Sx, Sy
984WE_HAVE_A_TWO_BY_TWO = 0x0080 # t00, t01, t10, t11
985WE_HAVE_INSTRUCTIONS = 0x0100 # instructions follow
986USE_MY_METRICS = 0x0200 # apply these metrics to parent glyph
987OVERLAP_COMPOUND = 0x0400 # used by Apple in GX fonts
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400988SCALED_COMPONENT_OFFSET = 0x0800 # composite designed to have the component offset scaled(designed for Apple)
989UNSCALED_COMPONENT_OFFSET = 0x1000 # composite designed not to have the component offset scaled(designed for MS)
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -0400990
Behdad Esfahbod616d36e2013-08-13 20:02:59 -0400991@__add_method(fontTools.ttLib.getTableModule('glyf').Glyph)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400992def getComponentNamesFast(self, glyfTable):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400993 if struct.unpack(">h", self.data[:2])[0] >= 0:
994 return [] # Not composite
995 data = self.data
996 i = 10
997 components = []
998 more = 1
999 while more:
1000 flags, glyphID = struct.unpack(">HH", data[i:i+4])
1001 i += 4
1002 flags = int(flags)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001003 components.append(glyfTable.getGlyphName(int(glyphID)))
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -04001004
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001005 if flags & ARG_1_AND_2_ARE_WORDS: i += 4
1006 else: i += 2
1007 if flags & WE_HAVE_A_SCALE: i += 2
1008 elif flags & WE_HAVE_AN_X_AND_Y_SCALE: i += 4
1009 elif flags & WE_HAVE_A_TWO_BY_TWO: i += 8
1010 more = flags & MORE_COMPONENTS
1011 return components
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -04001012
Behdad Esfahbod616d36e2013-08-13 20:02:59 -04001013@__add_method(fontTools.ttLib.getTableModule('glyf').Glyph)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001014def remapComponentsFast(self, indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001015 if struct.unpack(">h", self.data[:2])[0] >= 0:
1016 return # Not composite
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001017 data = bytearray(self.data)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001018 i = 10
1019 more = 1
1020 while more:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001021 flags =(data[i] << 8) | data[i+1]
1022 glyphID =(data[i+2] << 8) | data[i+3]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001023 # Remap
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001024 glyphID = indices.index(glyphID)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001025 data[i+2] = glyphID >> 8
1026 data[i+3] = glyphID & 0xFF
1027 i += 4
1028 flags = int(flags)
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -04001029
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001030 if flags & ARG_1_AND_2_ARE_WORDS: i += 4
1031 else: i += 2
1032 if flags & WE_HAVE_A_SCALE: i += 2
1033 elif flags & WE_HAVE_AN_X_AND_Y_SCALE: i += 4
1034 elif flags & WE_HAVE_A_TWO_BY_TWO: i += 8
1035 more = flags & MORE_COMPONENTS
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001036 self.data = str(data)
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -04001037
Behdad Esfahbod616d36e2013-08-13 20:02:59 -04001038@__add_method(fontTools.ttLib.getTableModule('glyf').Glyph)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001039def dropInstructionsFast(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001040 numContours = struct.unpack(">h", self.data[:2])[0]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001041 data = bytearray(self.data)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001042 i = 10
1043 if numContours >= 0:
1044 i += 2 * numContours # endPtsOfContours
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001045 instructionLen =(data[i] << 8) | data[i+1]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001046 # Zero it
1047 data[i] = data [i+1] = 0
1048 i += 2
1049 if instructionLen:
1050 # Splice it out
1051 data = data[:i] + data[i+instructionLen:]
1052 else:
1053 more = 1
1054 while more:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001055 flags =(data[i] << 8) | data[i+1]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001056 # Turn instruction flag off
1057 flags &= ~WE_HAVE_INSTRUCTIONS
1058 data[i+0] = flags >> 8
1059 data[i+1] = flags & 0xFF
1060 i += 4
1061 flags = int(flags)
Behdad Esfahbod6ec88542013-07-24 16:52:47 -04001062
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001063 if flags & ARG_1_AND_2_ARE_WORDS: i += 4
1064 else: i += 2
1065 if flags & WE_HAVE_A_SCALE: i += 2
1066 elif flags & WE_HAVE_AN_X_AND_Y_SCALE: i += 4
1067 elif flags & WE_HAVE_A_TWO_BY_TWO: i += 8
1068 more = flags & MORE_COMPONENTS
1069 # Cut off
1070 data = data[:i]
1071 if len(data) % 4:
1072 # add pad bytes
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001073 nPadBytes = 4 -(len(data) % 4)
1074 for i in range(nPadBytes):
1075 data.append(0)
1076 self.data = str(data)
Behdad Esfahbod6ec88542013-07-24 16:52:47 -04001077
Behdad Esfahbod616d36e2013-08-13 20:02:59 -04001078@__add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001079def closure_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001080 decompose = s.glyphs
1081 # I don't know if component glyphs can be composite themselves.
1082 # We handle them anyway.
1083 while True:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001084 components = set()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001085 for g in decompose:
1086 if g not in self.glyphs:
1087 continue
1088 gl = self.glyphs[g]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001089 if hasattr(gl, "data"):
1090 for c in gl.getComponentNamesFast(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001091 if c not in s.glyphs:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001092 components.add(c)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001093 else:
1094 # TTX seems to expand gid0..3 always
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001095 if gl.isComposite():
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001096 for c in gl.components:
1097 if c.glyphName not in s.glyphs:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001098 components.add(c.glyphName)
1099 components = set(c for c in components if c not in s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001100 if not components:
1101 break
1102 decompose = components
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001103 s.glyphs.update(components)
Behdad Esfahbodabb50a12013-07-23 12:58:37 -04001104
Behdad Esfahbod616d36e2013-08-13 20:02:59 -04001105@__add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001106def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001107 self.glyphs = {g:v for g,v in self.glyphs.iteritems() if g in s.glyphs}
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001108 indices = [i for i,g in enumerate(self.glyphOrder) if g in s.glyphs]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001109 for v in self.glyphs.itervalues():
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001110 if hasattr(v, "data"):
1111 v.remapComponentsFast(indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001112 else:
1113 pass # No need
1114 self.glyphOrder = [g for g in self.glyphOrder if g in s.glyphs]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001115 return bool(self.glyphs)
Behdad Esfahbod861d9152013-07-22 16:47:24 -04001116
Behdad Esfahbod616d36e2013-08-13 20:02:59 -04001117@__add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001118def prune_post_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001119 if not options.hinting:
1120 for v in self.glyphs.itervalues():
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001121 if hasattr(v, "data"):
1122 v.dropInstructionsFast()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001123 else:
1124 v.program = fontTools.ttLib.tables.ttProgram.Program()
1125 v.program.fromBytecode([])
1126 return True
Behdad Esfahboded98c612013-07-23 12:37:41 -04001127
Behdad Esfahbod616d36e2013-08-13 20:02:59 -04001128@__add_method(fontTools.ttLib.getTableClass('CFF '))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001129def prune_pre_subset(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001130 cff = self.cff
1131 # CFF table should have one font only
1132 cff.fontNames = cff.fontNames[:1]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001133 return bool(cff.fontNames)
Behdad Esfahbod1a4e72e2013-08-13 15:46:37 -04001134
Behdad Esfahbod616d36e2013-08-13 20:02:59 -04001135@__add_method(fontTools.ttLib.getTableClass('CFF '))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001136def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001137 cff = self.cff
1138 for fontname in cff.keys():
1139 font = cff[fontname]
1140 cs = font.CharStrings
1141 if cs.charStringsAreIndexed:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001142 indices = [i for i,g in enumerate(font.charset) if g in s.glyphs]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001143 # Load all glyphs
1144 for g in font.charset:
1145 if g not in s.glyphs: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001146 cs.getItemAndSelector(g)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001147 csi = cs.charStringsIndex
1148 csi.items = [csi.items[i] for i in indices]
1149 csi.offsets = [] # Don't need it; loaded all glyphs
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001150 if hasattr(font, "FDSelect"):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001151 sel = font.FDSelect
1152 sel.format = None
1153 sel.gidArray = [font.FDSelect.gidArray[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001154 cs.charStrings = {g:indices.index(v)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001155 for g,v in cs.charStrings.iteritems()
1156 if g in s.glyphs}
1157 else:
1158 cs.charStrings = {g:v
1159 for g,v in cs.charStrings.iteritems()
1160 if g in s.glyphs}
1161 font.charset = [g for g in font.charset if g in s.glyphs]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001162 font.numGlyphs = len(font.charset)
1163 return any(cff[fontname].numGlyphs for fontname in cff.keys())
Behdad Esfahbod1a4e72e2013-08-13 15:46:37 -04001164
Behdad Esfahbod616d36e2013-08-13 20:02:59 -04001165@__add_method(fontTools.ttLib.getTableClass('glyf'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001166def prune_post_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001167 if not options.hinting:
1168 pass # Drop hints
1169 return True
Behdad Esfahbod2b677c82013-07-23 13:37:13 -04001170
Behdad Esfahbod616d36e2013-08-13 20:02:59 -04001171@__add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001172def closure_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001173 tables = [t for t in self.tables
1174 if t.platformID == 3 and t.platEncID in [1, 10]]
1175 for u in s.unicodes_requested:
1176 found = False
1177 for table in tables:
1178 if u in table.cmap:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001179 s.glyphs.add(table.cmap[u])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001180 found = True
1181 break
1182 if not found:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001183 s.log("No glyph for Unicode value %s; skipping." % u)
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04001184
Behdad Esfahbod616d36e2013-08-13 20:02:59 -04001185@__add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001186def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001187 if not options.legacy_cmap:
1188 # Drop non-Unicode / non-Symbol cmaps
1189 self.tables = [t for t in self.tables
1190 if t.platformID == 3 and t.platEncID in [0, 1, 10]]
1191 if not options.symbol_cmap:
1192 self.tables = [t for t in self.tables
1193 if t.platformID == 3 and t.platEncID in [1, 10]]
1194 # TODO Only keep one subtable?
1195 # For now, drop format=0 which can't be subset_glyphs easily?
1196 self.tables = [t for t in self.tables if t.format != 0]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001197 return bool(self.tables)
Behdad Esfahbodabb50a12013-07-23 12:58:37 -04001198
Behdad Esfahbod616d36e2013-08-13 20:02:59 -04001199@__add_method(fontTools.ttLib.getTableClass('cmap'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001200def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001201 s.glyphs = s.glyphs_cmaped
1202 for t in self.tables:
1203 # For reasons I don't understand I need this here
1204 # to force decompilation of the cmap format 14.
1205 try:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001206 getattr(t, "asdf")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001207 except AttributeError:
1208 pass
1209 if t.format == 14:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001210 # XXX We drop all the default-UVS mappings(g==None)
1211 t.uvsDict = {v:[(u,g) for(u,g) in l if g in s.glyphs]
1212 for(v,l) in t.uvsDict.iteritems()}
1213 t.uvsDict = {v:l for(v,l) in t.uvsDict.iteritems() if l}
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001214 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001215 t.cmap = {u:g for(u,g) in t.cmap.iteritems()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001216 if g in s.glyphs_requested or u in s.unicodes_requested}
1217 self.tables = [t for t in self.tables
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001218 if(t.cmap if t.format != 14 else t.uvsDict)]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001219 # XXX Convert formats when needed
1220 # In particular, if we have a format=12 without non-BMP
1221 # characters, either drop format=12 one or convert it
1222 # to format=4 if there's not one.
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001223 return bool(self.tables)
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001224
Behdad Esfahbod616d36e2013-08-13 20:02:59 -04001225@__add_method(fontTools.ttLib.getTableClass('name'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001226def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001227 if '*' not in options.name_IDs:
1228 self.names = [n for n in self.names if n.nameID in options.name_IDs]
1229 if not options.name_legacy:
1230 self.names = [n for n in self.names
1231 if n.platformID == 3 and n.platEncID == 1]
1232 if '*' not in options.name_languages:
1233 self.names = [n for n in self.names if n.langID in options.name_languages]
1234 return True # Retain even if empty
Behdad Esfahbod653e9742013-07-22 15:17:12 -04001235
Behdad Esfahbod8c646f62013-07-22 15:06:23 -04001236
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -04001237# TODO OS/2 ulUnicodeRange / ulCodePageRange?
Behdad Esfahbodf71267b2013-07-23 12:59:13 -04001238# TODO Drop unneeded GSUB/GPOS Script/LangSys entries
Behdad Esfahbod398d3892013-07-23 15:29:40 -04001239# TODO Avoid recursing too much
Behdad Esfahbode94aa0e2013-07-23 13:22:04 -04001240# TODO Text direction considerations
1241# TODO Text script / language considerations
Behdad Esfahbodb3ee60c2013-07-24 19:21:40 -04001242# TODO Drop unknown tables? Using DefaultTable.prune?
Behdad Esfahbod8c4f7cc2013-07-24 17:58:29 -04001243# TODO Drop GPOS Device records if not hinting?
Behdad Esfahbod93e26362013-08-09 14:22:48 -04001244# TODO Move font name loading hack to Subsetter?
Behdad Esfahbod56ebd042013-07-22 13:02:24 -04001245
Behdad Esfahbod8c486d82013-07-24 13:34:47 -04001246
Behdad Esfahbod3d513b72013-07-31 14:11:40 -04001247class Subsetter:
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001248
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001249 class Options:
Behdad Esfahbod26d9ee72013-08-13 16:55:01 -04001250
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001251 class UnknownOptionError(Exception):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001252 pass
Behdad Esfahbod26d9ee72013-08-13 16:55:01 -04001253
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001254 drop_tables_default = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC',
1255 'PCLT', 'LTSH']
1256 drop_tables_default += ['Feat', 'Glat', 'Gloc', 'Silf', 'Sill'] # Graphite
1257 drop_tables_default += ['CBLC', 'CBDT', 'sbix', 'COLR', 'CPAL'] # Color
1258 no_subset_tables_default = ['gasp', 'head', 'hhea', 'maxp', 'vhea', 'OS/2',
1259 'loca', 'name', 'cvt ', 'fpgm', 'prep']
1260 hinting_tables_default = ['cvt ', 'fpgm', 'prep', 'hdmx', 'VDMX']
Behdad Esfahbod9eeeb4e2013-08-13 16:58:50 -04001261
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001262 # Based on HarfBuzz shapers
1263 layout_features_groups = {
1264 # Default shaper
Behdad Esfahbode9f0b152013-08-13 19:54:25 -04001265 'common': ['ccmp', 'liga', 'locl', 'mark', 'mkmk', 'rlig'],
1266 'horizontal': ['calt', 'clig', 'curs', 'kern', 'rclt'],
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001267 'vertical': ['valt', 'vert', 'vkrn', 'vpal', 'vrt2'],
Behdad Esfahbode9f0b152013-08-13 19:54:25 -04001268 'ltr': ['ltra', 'ltrm'],
1269 'rtl': ['rtla', 'rtlm'],
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001270 # Complex shapers
Behdad Esfahbode9f0b152013-08-13 19:54:25 -04001271 'arabic': ['init', 'medi', 'fina', 'isol', 'med2', 'fin2', 'fin3',
1272 'cswh', 'mset'],
1273 'hangul': ['ljmo', 'vjmo', 'tjmo'],
1274 'tibetal': ['abvs', 'blws', 'abvm', 'blwm'],
1275 'indic': ['nukt', 'akhn', 'rphf', 'rkrf', 'pref', 'blwf', 'half',
1276 'abvf', 'pstf', 'cfar', 'vatu', 'cjct', 'init', 'pres',
1277 'abvs', 'blws', 'psts', 'haln', 'dist', 'abvm', 'blwm'],
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001278 }
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001279 layout_features_default = unique_sorted(sum(
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001280 layout_features_groups.itervalues(), []))
Behdad Esfahbod9eeeb4e2013-08-13 16:58:50 -04001281
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001282 drop_tables = drop_tables_default
1283 no_subset_tables = no_subset_tables_default
1284 hinting_tables = hinting_tables_default
1285 layout_features = layout_features_default
1286 hinting = False
1287 glyph_names = False
1288 legacy_cmap = False
1289 symbol_cmap = False
1290 name_IDs = [1, 2] # Family and Style
1291 name_legacy = False
1292 name_languages = [0x0409] # English
1293 mandatory_glyphs = True # First four for TrueType, .notdef for CFF
1294 recalc_bboxes = False # Slows us down
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001295
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001296 def __init__(self, **kwargs):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001297
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001298 self.set(**kwargs)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001299
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001300 def set(self, **kwargs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001301 for k,v in kwargs.iteritems():
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001302 if not hasattr(self, k):
1303 raise self.UnknownOptionError("Unknown option '%s'" % k)
1304 setattr(self, k, v)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001305
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001306 def parse_opts(self, argv, ignore_unknown=False):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001307 ret = []
1308 opts = {}
1309 for a in argv:
1310 orig_a = a
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001311 if not a.startswith('--'):
1312 ret.append(a)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001313 continue
1314 a = a[2:]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001315 i = a.find('=')
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001316 if i == -1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001317 if a.startswith("no-"):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001318 k = a[3:]
1319 v = False
1320 else:
1321 k = a
1322 v = True
1323 else:
1324 k = a[:i]
1325 v = a[i+1:]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001326 k = k.replace('-', '_')
1327 if not hasattr(self, k):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001328 if ignore_unknown == True or k in ignore_unknown:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001329 ret.append(orig_a)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001330 continue
1331 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001332 raise self.UnknownOptionError("Unknown option '%s'" % a)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001333
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001334 ov = getattr(self, k)
1335 if isinstance(ov, bool):
1336 v = bool(v)
1337 elif isinstance(ov, int):
1338 v = int(v)
1339 elif isinstance(ov, list):
1340 v = v.split(',')
1341 v = [int(x, 0) if x[0] in range(10) else x for x in v]
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001342
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001343 opts[k] = v
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001344 self.set(**opts)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001345
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001346 return ret
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001347
1348
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001349 def __init__(self, options=None, log=None):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001350
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001351 if not log:
1352 log = Logger()
1353 if not options:
1354 options = Options()
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001355
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001356 self.options = options
1357 self.log = log
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001358 self.unicodes_requested = set()
1359 self.glyphs_requested = set()
1360 self.glyphs = set()
Behdad Esfahbod3d513b72013-07-31 14:11:40 -04001361
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001362 def populate(self, glyphs=[], unicodes=[], text=""):
1363 self.unicodes_requested.update(unicodes)
1364 if isinstance(text, str):
1365 text = text.decode("utf8")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001366 for u in text:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001367 self.unicodes_requested.add(ord(u))
1368 self.glyphs_requested.update(glyphs)
1369 self.glyphs.update(glyphs)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -04001370
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001371 def pre_prune(self, font):
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001372
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001373 for tag in font.keys():
1374 if tag == 'GlyphOrder': continue
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001375
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001376 if(tag in self.options.drop_tables or
1377 (tag in self.options.hinting_tables and not self.options.hinting)):
1378 self.log(tag, "dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001379 del font[tag]
1380 continue
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001381
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001382 clazz = fontTools.ttLib.getTableClass(tag)
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001383
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001384 if hasattr(clazz, 'prune_pre_subset'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001385 table = font[tag]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001386 retain = table.prune_pre_subset(self.options)
1387 self.log.lapse("prune '%s'" % tag)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001388 if not retain:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001389 self.log(tag, "pruned to empty; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001390 del font[tag]
1391 continue
1392 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001393 self.log(tag, "pruned")
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001394
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001395 def closure_glyphs(self, font):
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001396
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001397 self.glyphs = self.glyphs_requested.copy()
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001398
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001399 if 'cmap' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001400 font['cmap'].closure_glyphs(self)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001401 self.glyphs_cmaped = self.glyphs
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001402
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001403 if self.options.mandatory_glyphs:
1404 if 'glyf' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001405 for i in range(4):
1406 self.glyphs.add(font.getGlyphName(i))
1407 self.log("Added first four glyphs to subset")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001408 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001409 self.glyphs.add('.notdef')
1410 self.log("Added .notdef glyph to subset")
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001411
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001412 if 'GSUB' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001413 self.log("Closing glyph list over 'GSUB': %d glyphs before" %
1414 len(self.glyphs))
1415 self.log.glyphs(self.glyphs, font=font)
1416 font['GSUB'].closure_glyphs(self)
1417 self.log("Closed glyph list over 'GSUB': %d glyphs after" %
1418 len(self.glyphs))
1419 self.log.glyphs(self.glyphs, font=font)
1420 self.log.lapse("close glyph list over 'GSUB'")
1421 self.glyphs_gsubed = self.glyphs.copy()
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001422
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001423 if 'glyf' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001424 self.log("Closing glyph list over 'glyf': %d glyphs before" %
1425 len(self.glyphs))
1426 self.log.glyphs(self.glyphs, font=font)
1427 font['glyf'].closure_glyphs(self)
1428 self.log("Closed glyph list over 'glyf': %d glyphs after" %
1429 len(self.glyphs))
1430 self.log.glyphs(self.glyphs, font=font)
1431 self.log.lapse("close glyph list over 'glyf'")
1432 self.glyphs_glyfed = self.glyphs.copy()
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001433
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001434 self.glyphs_all = self.glyphs.copy()
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04001435
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001436 self.log("Retaining %d glyphs: " % len(self.glyphs_all))
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001437
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001438 def subset_glyphs(self, font):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001439 for tag in font.keys():
1440 if tag == 'GlyphOrder': continue
1441 clazz = fontTools.ttLib.getTableClass(tag)
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001442
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001443 if tag in self.options.no_subset_tables:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001444 self.log(tag, "subsetting not needed")
1445 elif hasattr(clazz, 'subset_glyphs'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001446 table = font[tag]
1447 self.glyphs = self.glyphs_all
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001448 retain = table.subset_glyphs(self)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001449 self.glyphs = self.glyphs_all
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001450 self.log.lapse("subset '%s'" % tag)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001451 if not retain:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001452 self.log(tag, "subsetted to empty; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001453 del font[tag]
1454 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001455 self.log(tag, "subsetted")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001456 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001457 self.log(tag, "NOT subset; don't know how to subset; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001458 del font[tag]
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001459
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001460 glyphOrder = font.getGlyphOrder()
1461 glyphOrder = [g for g in glyphOrder if g in self.glyphs_all]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001462 font.setGlyphOrder(glyphOrder)
1463 font._buildReverseGlyphOrderDict()
1464 self.log.lapse("subset GlyphOrder")
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001465
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001466 def post_prune(self, font):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001467 for tag in font.keys():
1468 if tag == 'GlyphOrder': continue
1469 clazz = fontTools.ttLib.getTableClass(tag)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001470 if hasattr(clazz, 'prune_post_subset'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001471 table = font[tag]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001472 retain = table.prune_post_subset(self.options)
1473 self.log.lapse("prune '%s'" % tag)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001474 if not retain:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001475 self.log(tag, "pruned to empty; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001476 del font[tag]
1477 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001478 self.log(tag, "pruned")
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001479
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001480 def subset(self, font):
Behdad Esfahbod756af492013-08-01 12:05:26 -04001481
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001482 font.recalcBBoxes = self.options.recalc_bboxes
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001483
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001484 self.pre_prune(font)
1485 self.closure_glyphs(font)
1486 self.subset_glyphs(font)
1487 self.post_prune(font)
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001488
Behdad Esfahbod756af492013-08-01 12:05:26 -04001489
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001490import sys, time
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001491
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001492class Logger:
1493
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001494 def __init__(self, verbose=False, xml=False, timing=False):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001495 self.verbose = verbose
1496 self.xml = xml
1497 self.timing = timing
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001498 self.last_time = self.start_time = time.time()
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001499
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001500 def parse_opts(self, argv):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001501 argv = argv[:]
1502 for v in ['verbose', 'xml', 'timing']:
1503 if "--"+v in argv:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001504 setattr(self, v, True)
1505 argv.remove("--"+v)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001506 return argv
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001507
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001508 def __call__(self, *things):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001509 if not self.verbose:
1510 return
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001511 print ' '.join(str(x) for x in things)
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001512
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001513 def lapse(self, *things):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001514 if not self.timing:
1515 return
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001516 new_time = time.time()
1517 print "Took %0.3fs to %s" %(new_time - self.last_time,
1518 ' '.join(str(x) for x in things))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001519 self.last_time = new_time
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001520
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001521 def glyphs(self, glyphs, glyph_names=True, font=None):
1522 self("Names: ", sorted(glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001523 if font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001524 reverseGlyphMap = font.getReverseGlyphMap()
1525 self("Gids : ", sorted(reverseGlyphMap[g] for g in glyphs))
Behdad Esfahbodf5497842013-08-08 21:57:02 -04001526
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001527 def font(self, font, file=sys.stdout):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001528 if not self.xml:
1529 return
1530 import xmlWriter
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001531 writer = xmlWriter.XMLWriter(file)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001532 font.disassembleInstructions = False # Work around ttx bug
1533 for tag in font.keys():
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001534 writer.begintag(tag)
1535 writer.newline()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001536 font[tag].toXML(writer, font)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001537 writer.endtag(tag)
1538 writer.newline()
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001539
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04001540
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001541def load_font(fontfile, dont_load_glyph_names=False):
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04001542
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001543 # TODO Option for ignoreDecompileErrors?
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04001544
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001545 font = fontTools.ttx.TTFont(fontfile)
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04001546
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001547 # Hack:
1548 #
1549 # If we don't need glyph names, change 'post' class to not try to
1550 # load them. It avoid lots of headache with broken fonts as well
1551 # as loading time.
1552 #
1553 # Ideally ttLib should provide a way to ask it to skip loading
1554 # glyph names. But it currently doesn't provide such a thing.
1555 #
1556 if dont_load_glyph_names:
1557 post = fontTools.ttLib.getTableClass('post')
1558 saved = post.decode_format_2_0
1559 post.decode_format_2_0 = post.decode_format_3_0
1560 f = font['post']
1561 if f.formatType == 2.0:
1562 f.formatType = 3.0
1563 post.decode_format_2_0 = saved
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04001564
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001565 return font
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04001566
1567
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001568def main(args):
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001569
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001570 log = Logger()
1571 args = log.parse_opts(args)
Behdad Esfahbod4ae81712013-07-22 11:57:13 -04001572
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001573 options = Subsetter.Options()
1574 args = options.parse_opts(args, ignore_unknown=['text'])
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001575
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001576 if len(args) < 2:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001577 print >>sys.stderr, "usage: pyotlss.py font-file glyph..."
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001578 sys.exit(1)
Behdad Esfahbod02b92062013-07-21 18:40:59 -04001579
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001580 fontfile = args[0]
1581 args = args[1:]
Behdad Esfahbod02b92062013-07-21 18:40:59 -04001582
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001583 dont_load_glyph_names =(not options.glyph_names and
1584 all(any(g.startswith(p)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001585 for p in ['gid', 'glyph', 'uni', 'U+'])
1586 for g in args))
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04001587
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001588 font = load_font(fontfile, dont_load_glyph_names=dont_load_glyph_names)
1589 subsetter = Subsetter(options=options, log=log)
1590 log.lapse("load font")
Behdad Esfahbod02b92062013-07-21 18:40:59 -04001591
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001592 names = font.getGlyphNames()
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001593 log.lapse("loading glyph names")
Behdad Esfahbode7f5a892013-07-31 19:58:59 -04001594
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001595 glyphs = []
1596 unicodes = []
1597 text = ""
1598 for g in args:
1599 if g in names:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001600 glyphs.append(g)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001601 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001602 if g.startswith('--text='):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001603 text += g[7:]
1604 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001605 if g.startswith('uni') or g.startswith('U+'):
1606 if g.startswith('uni') and len(g) > 3:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001607 g = g[3:]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001608 elif g.startswith('U+') and len(g) > 2:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001609 g = g[2:]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001610 u = int(g, 16)
1611 unicodes.append(u)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001612 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001613 if g.startswith('gid') or g.startswith('glyph'):
1614 if g.startswith('gid') and len(g) > 3:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001615 g = g[3:]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001616 elif g.startswith('glyph') and len(g) > 5:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001617 g = g[5:]
1618 try:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001619 glyphs.append(font.getGlyphName(int(g), requireReal=1))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001620 except ValueError:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001621 raise Exception("Invalid glyph identifier: %s" % g)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001622 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001623 raise Exception("Invalid glyph identifier: %s" % g)
1624 log.lapse("compile glyph list")
1625 log("Unicodes:", unicodes)
1626 log("Glyphs:", glyphs)
Behdad Esfahbod6df089a2013-07-31 19:27:14 -04001627
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001628 subsetter.populate(glyphs=glyphs, unicodes=unicodes, text=text)
1629 subsetter.subset(font)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -04001630
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001631 font.save(fontfile + '.subset')
1632 log.lapse("compile and save font")
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04001633
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001634 log.last_time = log.start_time
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001635 log.lapse("make one with everything(TOTAL TIME)")
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04001636
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001637 log.font(font)
Behdad Esfahbod8c486d82013-07-24 13:34:47 -04001638
1639if __name__ == '__main__':
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001640 main(sys.argv[1:])