blob: f65a5a360e105cdac4cc078e95fc0cdd04095f45 [file] [log] [blame]
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04001# Copyright 2013 Google, Inc. All Rights Reserved.
2#
Behdad Esfahbod0fe6a512013-07-23 11:17:35 -04003# Google Author(s): Behdad Esfahbod
Behdad Esfahbod616d36e2013-08-13 20:02:59 -04004
5"""Python OpenType Layout Subsetter.
6
7Later grown into full OpenType subsetter, supporting all standard tables.
8"""
9
Behdad Esfahbod1ae29592014-01-14 15:07:50 +080010from __future__ import print_function, division, absolute_import
Behdad Esfahbodcfeafd72013-11-27 17:27:35 -050011from fontTools.misc.py23 import *
Behdad Esfahbod46d260f2013-09-19 20:36:49 -040012from fontTools import ttLib
13from fontTools.ttLib.tables import otTables
14from fontTools.misc import psCharStrings
15from fontTools.pens import basePen
Behdad Esfahbodcfeafd72013-11-27 17:27:35 -050016import sys
17import struct
18import time
19import array
Behdad Esfahbod54660612013-07-21 18:16:55 -040020
Behdad Esfahbod54660612013-07-21 18:16:55 -040021
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040022def _add_method(*clazzes):
Behdad Esfahbod616d36e2013-08-13 20:02:59 -040023 """Returns a decorator function that adds a new method to one or
24 more classes."""
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040025 def wrapper(method):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040026 for clazz in clazzes:
27 assert clazz.__name__ != 'DefaultTable', 'Oops, table class not found.'
Behdad Esfahbod553c3bb2013-11-27 02:24:11 -050028 assert not hasattr(clazz, method.__name__), \
Behdad Esfahbodd77f1572013-08-15 19:24:36 -040029 "Oops, class '%s' has method '%s'." % (clazz.__name__,
Behdad Esfahbod553c3bb2013-11-27 02:24:11 -050030 method.__name__)
31 setattr(clazz, method.__name__, method)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040032 return None
33 return wrapper
Behdad Esfahbod54660612013-07-21 18:16:55 -040034
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040035def _uniq_sort(l):
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040036 return sorted(set(l))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -040037
Behdad Esfahbod42d4f2b2013-08-16 16:16:22 -040038def _set_update(s, *others):
39 # Jython's set.update only takes one other argument.
40 # Emulate real set.update...
41 for other in others:
42 s.update(other)
43
Behdad Esfahbod78661bb2013-07-23 10:23:42 -040044
Behdad Esfahbod46d260f2013-09-19 20:36:49 -040045@_add_method(otTables.Coverage)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040046def intersect(self, glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040047 "Returns ascending list of matching coverage values."
Behdad Esfahbod4734be52013-08-14 19:47:42 -040048 return [i for i,g in enumerate(self.glyphs) if g in glyphs]
Behdad Esfahbod610b0552013-07-23 14:52:18 -040049
Behdad Esfahbod46d260f2013-09-19 20:36:49 -040050@_add_method(otTables.Coverage)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040051def intersect_glyphs(self, glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040052 "Returns set of intersecting glyphs."
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040053 return set(g for g in self.glyphs if g in glyphs)
Behdad Esfahbod849d25c2013-08-12 19:24:24 -040054
Behdad Esfahbod46d260f2013-09-19 20:36:49 -040055@_add_method(otTables.Coverage)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040056def subset(self, glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040057 "Returns ascending list of remaining coverage values."
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040058 indices = self.intersect(glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040059 self.glyphs = [g for g in self.glyphs if g in glyphs]
60 return indices
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -040061
Behdad Esfahbod46d260f2013-09-19 20:36:49 -040062@_add_method(otTables.Coverage)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040063def remap(self, coverage_map):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040064 "Remaps coverage."
65 self.glyphs = [self.glyphs[i] for i in coverage_map]
Behdad Esfahbod14374262013-08-08 22:26:49 -040066
Behdad Esfahbod46d260f2013-09-19 20:36:49 -040067@_add_method(otTables.ClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040068def intersect(self, glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040069 "Returns ascending list of matching class values."
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040070 return _uniq_sort(
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040071 ([0] if any(g not in self.classDefs for g in glyphs) else []) +
Behdad Esfahbod6890d052013-11-27 06:26:35 -050072 [v for g,v in self.classDefs.items() if g in glyphs])
Behdad Esfahbodb8d55882013-07-23 22:17:39 -040073
Behdad Esfahbod46d260f2013-09-19 20:36:49 -040074@_add_method(otTables.ClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040075def intersect_class(self, glyphs, klass):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040076 "Returns set of glyphs matching class."
77 if klass == 0:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040078 return set(g for g in glyphs if g not in self.classDefs)
Behdad Esfahbod6890d052013-11-27 06:26:35 -050079 return set(g for g,v in self.classDefs.items()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040080 if v == klass and g in glyphs)
Behdad Esfahbodb8d55882013-07-23 22:17:39 -040081
Behdad Esfahbod46d260f2013-09-19 20:36:49 -040082@_add_method(otTables.ClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040083def subset(self, glyphs, remap=False):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040084 "Returns ascending list of remaining classes."
Behdad Esfahbod6890d052013-11-27 06:26:35 -050085 self.classDefs = dict((g,v) for g,v in self.classDefs.items() if g in glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040086 # Note: while class 0 has the special meaning of "not matched",
87 # if no glyph will ever /not match/, we can optimize class 0 out too.
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -040088 indices = _uniq_sort(
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040089 ([0] if any(g not in self.classDefs for g in glyphs) else []) +
Behdad Esfahbod6890d052013-11-27 06:26:35 -050090 list(self.classDefs.values()))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040091 if remap:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040092 self.remap(indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040093 return indices
Behdad Esfahbod4aa6ce32013-07-22 12:15:36 -040094
Behdad Esfahbod46d260f2013-09-19 20:36:49 -040095@_add_method(otTables.ClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -040096def remap(self, class_map):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -040097 "Remaps classes."
Behdad Esfahbodd73f2252013-08-16 10:58:25 -040098 self.classDefs = dict((g,class_map.index(v))
Behdad Esfahbod6890d052013-11-27 06:26:35 -050099 for g,v in self.classDefs.items())
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400100
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400101@_add_method(otTables.SingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400102def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e6ef942013-12-04 16:31:44 -0500103 if cur_glyphs is None: cur_glyphs = s.glyphs
Behdad Esfahbod45ed5722013-12-17 06:01:08 -0500104 s.glyphs.update(v for g,v in self.mapping.items() if g in cur_glyphs)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400105
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400106@_add_method(otTables.SingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400107def subset_glyphs(self, s):
Behdad Esfahbod45ed5722013-12-17 06:01:08 -0500108 self.mapping = dict((g,v) for g,v in self.mapping.items()
109 if g in s.glyphs and v in s.glyphs)
110 return bool(self.mapping)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400111
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400112@_add_method(otTables.MultipleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400113def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e6ef942013-12-04 16:31:44 -0500114 if cur_glyphs is None: cur_glyphs = s.glyphs
Behdad Esfahbod45ed5722013-12-17 06:01:08 -0500115 indices = self.Coverage.intersect(cur_glyphs)
116 _set_update(s.glyphs, *(self.Sequence[i].Substitute for i in indices))
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400117
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400118@_add_method(otTables.MultipleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400119def subset_glyphs(self, s):
Behdad Esfahbod45ed5722013-12-17 06:01:08 -0500120 indices = self.Coverage.subset(s.glyphs)
121 self.Sequence = [self.Sequence[i] for i in indices]
122 # Now drop rules generating glyphs we don't want
123 indices = [i for i,seq in enumerate(self.Sequence)
124 if all(sub in s.glyphs for sub in seq.Substitute)]
125 self.Sequence = [self.Sequence[i] for i in indices]
126 self.Coverage.remap(indices)
127 self.SequenceCount = len(self.Sequence)
128 return bool(self.SequenceCount)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400129
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400130@_add_method(otTables.AlternateSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400131def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e6ef942013-12-04 16:31:44 -0500132 if cur_glyphs is None: cur_glyphs = s.glyphs
Behdad Esfahbod45ed5722013-12-17 06:01:08 -0500133 _set_update(s.glyphs, *(vlist for g,vlist in self.alternates.items()
134 if g in cur_glyphs))
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400135
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400136@_add_method(otTables.AlternateSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400137def subset_glyphs(self, s):
Behdad Esfahbod45ed5722013-12-17 06:01:08 -0500138 self.alternates = dict((g,vlist)
139 for g,vlist in self.alternates.items()
140 if g in s.glyphs and
141 all(v in s.glyphs for v in vlist))
142 return bool(self.alternates)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400143
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400144@_add_method(otTables.LigatureSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400145def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e6ef942013-12-04 16:31:44 -0500146 if cur_glyphs is None: cur_glyphs = s.glyphs
Behdad Esfahbod45ed5722013-12-17 06:01:08 -0500147 _set_update(s.glyphs, *([seq.LigGlyph for seq in seqs
148 if all(c in s.glyphs for c in seq.Component)]
149 for g,seqs in self.ligatures.items()
150 if g in cur_glyphs))
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400151
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400152@_add_method(otTables.LigatureSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400153def subset_glyphs(self, s):
Behdad Esfahbod45ed5722013-12-17 06:01:08 -0500154 self.ligatures = dict((g,v) for g,v in self.ligatures.items()
155 if g in s.glyphs)
156 self.ligatures = dict((g,[seq for seq in seqs
157 if seq.LigGlyph in s.glyphs and
158 all(c in s.glyphs for c in seq.Component)])
159 for g,seqs in self.ligatures.items())
160 self.ligatures = dict((g,v) for g,v in self.ligatures.items() if v)
161 return bool(self.ligatures)
Behdad Esfahbod54660612013-07-21 18:16:55 -0400162
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400163@_add_method(otTables.ReverseChainSingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400164def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e6ef942013-12-04 16:31:44 -0500165 if cur_glyphs is None: cur_glyphs = s.glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400166 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400167 indices = self.Coverage.intersect(cur_glyphs)
168 if(not indices or
169 not all(c.intersect(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400170 for c in self.LookAheadCoverage + self.BacktrackCoverage)):
171 return
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400172 s.glyphs.update(self.Substitute[i] for i in indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400173 else:
174 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400175
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400176@_add_method(otTables.ReverseChainSingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400177def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400178 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400179 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400180 self.Substitute = [self.Substitute[i] for i in indices]
181 # Now drop rules generating glyphs we don't want
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400182 indices = [i for i,sub in enumerate(self.Substitute)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400183 if sub in s.glyphs]
184 self.Substitute = [self.Substitute[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400185 self.Coverage.remap(indices)
186 self.GlyphCount = len(self.Substitute)
187 return bool(self.GlyphCount and
188 all(c.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400189 for c in self.LookAheadCoverage+self.BacktrackCoverage))
190 else:
191 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400192
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400193@_add_method(otTables.SinglePos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400194def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400195 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400196 return len(self.Coverage.subset(s.glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400197 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400198 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400199 self.Value = [self.Value[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400200 self.ValueCount = len(self.Value)
201 return bool(self.ValueCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400202 else:
203 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400204
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400205@_add_method(otTables.SinglePos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400206def prune_post_subset(self, options):
207 if not options.hinting:
208 # Drop device tables
209 self.ValueFormat &= ~0x00F0
210 return True
211
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400212@_add_method(otTables.PairPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400213def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400214 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400215 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400216 self.PairSet = [self.PairSet[i] for i in indices]
217 for p in self.PairSet:
218 p.PairValueRecord = [r for r in p.PairValueRecord
219 if r.SecondGlyph in s.glyphs]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400220 p.PairValueCount = len(p.PairValueRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400221 self.PairSet = [p for p in self.PairSet if p.PairValueCount]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400222 self.PairSetCount = len(self.PairSet)
223 return bool(self.PairSetCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400224 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400225 class1_map = self.ClassDef1.subset(s.glyphs, remap=True)
226 class2_map = self.ClassDef2.subset(s.glyphs, remap=True)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400227 self.Class1Record = [self.Class1Record[i] for i in class1_map]
228 for c in self.Class1Record:
229 c.Class2Record = [c.Class2Record[i] for i in class2_map]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400230 self.Class1Count = len(class1_map)
231 self.Class2Count = len(class2_map)
232 return bool(self.Class1Count and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400233 self.Class2Count and
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400234 self.Coverage.subset(s.glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400235 else:
236 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400237
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400238@_add_method(otTables.PairPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400239def prune_post_subset(self, options):
240 if not options.hinting:
241 # Drop device tables
242 self.ValueFormat1 &= ~0x00F0
243 self.ValueFormat2 &= ~0x00F0
244 return True
245
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400246@_add_method(otTables.CursivePos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400247def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400248 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400249 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400250 self.EntryExitRecord = [self.EntryExitRecord[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400251 self.EntryExitCount = len(self.EntryExitRecord)
252 return bool(self.EntryExitCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400253 else:
254 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400255
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400256@_add_method(otTables.Anchor)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400257def prune_hints(self):
258 # Drop device tables / contour anchor point
Behdad Esfahbod6c51f502013-12-15 23:12:26 -0500259 self.ensureDecompiled()
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400260 self.Format = 1
261
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400262@_add_method(otTables.CursivePos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400263def prune_post_subset(self, options):
264 if not options.hinting:
265 for rec in self.EntryExitRecord:
266 if rec.EntryAnchor: rec.EntryAnchor.prune_hints()
267 if rec.ExitAnchor: rec.ExitAnchor.prune_hints()
268 return True
269
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400270@_add_method(otTables.MarkBasePos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400271def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400272 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400273 mark_indices = self.MarkCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400274 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i]
275 for i in mark_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400276 self.MarkArray.MarkCount = len(self.MarkArray.MarkRecord)
277 base_indices = self.BaseCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400278 self.BaseArray.BaseRecord = [self.BaseArray.BaseRecord[i]
279 for i in base_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400280 self.BaseArray.BaseCount = len(self.BaseArray.BaseRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400281 # Prune empty classes
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400282 class_indices = _uniq_sort(v.Class for v in self.MarkArray.MarkRecord)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400283 self.ClassCount = len(class_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400284 for m in self.MarkArray.MarkRecord:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400285 m.Class = class_indices.index(m.Class)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400286 for b in self.BaseArray.BaseRecord:
287 b.BaseAnchor = [b.BaseAnchor[i] for i in class_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400288 return bool(self.ClassCount and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400289 self.MarkArray.MarkCount and
290 self.BaseArray.BaseCount)
291 else:
292 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400293
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400294@_add_method(otTables.MarkBasePos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400295def prune_post_subset(self, options):
296 if not options.hinting:
297 for m in self.MarkArray.MarkRecord:
Behdad Esfahbode1a010c2013-10-09 15:57:22 +0200298 if m.MarkAnchor:
299 m.MarkAnchor.prune_hints()
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400300 for b in self.BaseArray.BaseRecord:
301 for a in b.BaseAnchor:
Behdad Esfahbode1a010c2013-10-09 15:57:22 +0200302 if a:
303 a.prune_hints()
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400304 return True
305
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400306@_add_method(otTables.MarkLigPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400307def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400308 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400309 mark_indices = self.MarkCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400310 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i]
311 for i in mark_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400312 self.MarkArray.MarkCount = len(self.MarkArray.MarkRecord)
313 ligature_indices = self.LigatureCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400314 self.LigatureArray.LigatureAttach = [self.LigatureArray.LigatureAttach[i]
315 for i in ligature_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400316 self.LigatureArray.LigatureCount = len(self.LigatureArray.LigatureAttach)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400317 # Prune empty classes
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400318 class_indices = _uniq_sort(v.Class for v in self.MarkArray.MarkRecord)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400319 self.ClassCount = len(class_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400320 for m in self.MarkArray.MarkRecord:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400321 m.Class = class_indices.index(m.Class)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400322 for l in self.LigatureArray.LigatureAttach:
323 for c in l.ComponentRecord:
324 c.LigatureAnchor = [c.LigatureAnchor[i] for i in class_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400325 return bool(self.ClassCount and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400326 self.MarkArray.MarkCount and
327 self.LigatureArray.LigatureCount)
328 else:
329 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400330
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400331@_add_method(otTables.MarkLigPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400332def prune_post_subset(self, options):
333 if not options.hinting:
334 for m in self.MarkArray.MarkRecord:
Behdad Esfahbode1a010c2013-10-09 15:57:22 +0200335 if m.MarkAnchor:
336 m.MarkAnchor.prune_hints()
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400337 for l in self.LigatureArray.LigatureAttach:
338 for c in l.ComponentRecord:
339 for a in c.LigatureAnchor:
Behdad Esfahbode1a010c2013-10-09 15:57:22 +0200340 if a:
341 a.prune_hints()
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400342 return True
343
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400344@_add_method(otTables.MarkMarkPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400345def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400346 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400347 mark1_indices = self.Mark1Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400348 self.Mark1Array.MarkRecord = [self.Mark1Array.MarkRecord[i]
349 for i in mark1_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400350 self.Mark1Array.MarkCount = len(self.Mark1Array.MarkRecord)
351 mark2_indices = self.Mark2Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400352 self.Mark2Array.Mark2Record = [self.Mark2Array.Mark2Record[i]
353 for i in mark2_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400354 self.Mark2Array.MarkCount = len(self.Mark2Array.Mark2Record)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400355 # Prune empty classes
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400356 class_indices = _uniq_sort(v.Class for v in self.Mark1Array.MarkRecord)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400357 self.ClassCount = len(class_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400358 for m in self.Mark1Array.MarkRecord:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400359 m.Class = class_indices.index(m.Class)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400360 for b in self.Mark2Array.Mark2Record:
361 b.Mark2Anchor = [b.Mark2Anchor[i] for i in class_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400362 return bool(self.ClassCount and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400363 self.Mark1Array.MarkCount and
364 self.Mark2Array.MarkCount)
365 else:
366 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400367
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400368@_add_method(otTables.MarkMarkPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400369def prune_post_subset(self, options):
370 if not options.hinting:
371 # Drop device tables or contour anchor point
372 for m in self.Mark1Array.MarkRecord:
Behdad Esfahbode1a010c2013-10-09 15:57:22 +0200373 if m.MarkAnchor:
374 m.MarkAnchor.prune_hints()
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400375 for b in self.Mark2Array.Mark2Record:
Behdad Esfahbod0ec17d92013-09-15 18:30:41 -0400376 for m in b.Mark2Anchor:
Behdad Esfahbode1a010c2013-10-09 15:57:22 +0200377 if m:
378 m.prune_hints()
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400379 return True
380
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400381@_add_method(otTables.SingleSubst,
382 otTables.MultipleSubst,
383 otTables.AlternateSubst,
384 otTables.LigatureSubst,
385 otTables.ReverseChainSingleSubst,
386 otTables.SinglePos,
387 otTables.PairPos,
388 otTables.CursivePos,
389 otTables.MarkBasePos,
390 otTables.MarkLigPos,
391 otTables.MarkMarkPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400392def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400393 pass
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400394
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400395@_add_method(otTables.SingleSubst,
396 otTables.MultipleSubst,
397 otTables.AlternateSubst,
398 otTables.LigatureSubst,
399 otTables.ReverseChainSingleSubst,
400 otTables.SinglePos,
401 otTables.PairPos,
402 otTables.CursivePos,
403 otTables.MarkBasePos,
404 otTables.MarkLigPos,
405 otTables.MarkMarkPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400406def collect_lookups(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400407 return []
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400408
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400409@_add_method(otTables.SingleSubst,
410 otTables.MultipleSubst,
411 otTables.AlternateSubst,
412 otTables.LigatureSubst,
413 otTables.ContextSubst,
414 otTables.ChainContextSubst,
415 otTables.ReverseChainSingleSubst,
416 otTables.SinglePos,
417 otTables.PairPos,
418 otTables.CursivePos,
419 otTables.MarkBasePos,
420 otTables.MarkLigPos,
421 otTables.MarkMarkPos,
422 otTables.ContextPos,
423 otTables.ChainContextPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400424def prune_pre_subset(self, options):
425 return True
426
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400427@_add_method(otTables.SingleSubst,
428 otTables.MultipleSubst,
429 otTables.AlternateSubst,
430 otTables.LigatureSubst,
431 otTables.ReverseChainSingleSubst,
432 otTables.ContextSubst,
433 otTables.ChainContextSubst,
434 otTables.ContextPos,
435 otTables.ChainContextPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400436def prune_post_subset(self, options):
437 return True
438
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400439@_add_method(otTables.SingleSubst,
440 otTables.AlternateSubst,
441 otTables.ReverseChainSingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400442def may_have_non_1to1(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400443 return False
Behdad Esfahbodaeacc152013-08-12 20:24:33 -0400444
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400445@_add_method(otTables.MultipleSubst,
446 otTables.LigatureSubst,
447 otTables.ContextSubst,
448 otTables.ChainContextSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400449def may_have_non_1to1(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400450 return True
Behdad Esfahbodaeacc152013-08-12 20:24:33 -0400451
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400452@_add_method(otTables.ContextSubst,
453 otTables.ChainContextSubst,
454 otTables.ContextPos,
455 otTables.ChainContextPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400456def __classify_context(self):
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400457
Behdad Esfahbod3d4c4712013-08-13 20:10:17 -0400458 class ContextHelper(object):
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400459 def __init__(self, klass, Format):
460 if klass.__name__.endswith('Subst'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400461 Typ = 'Sub'
462 Type = 'Subst'
463 else:
464 Typ = 'Pos'
465 Type = 'Pos'
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400466 if klass.__name__.startswith('Chain'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400467 Chain = 'Chain'
468 else:
469 Chain = ''
470 ChainTyp = Chain+Typ
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400471
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400472 self.Typ = Typ
473 self.Type = Type
474 self.Chain = Chain
475 self.ChainTyp = ChainTyp
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400476
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400477 self.LookupRecord = Type+'LookupRecord'
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400478
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400479 if Format == 1:
480 Coverage = lambda r: r.Coverage
481 ChainCoverage = lambda r: r.Coverage
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400482 ContextData = lambda r:(None,)
483 ChainContextData = lambda r:(None, None, None)
484 RuleData = lambda r:(r.Input,)
485 ChainRuleData = lambda r:(r.Backtrack, r.Input, r.LookAhead)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400486 SetRuleData = None
487 ChainSetRuleData = None
488 elif Format == 2:
489 Coverage = lambda r: r.Coverage
490 ChainCoverage = lambda r: r.Coverage
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400491 ContextData = lambda r:(r.ClassDef,)
492 ChainContextData = lambda r:(r.LookAheadClassDef,
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400493 r.InputClassDef,
494 r.BacktrackClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400495 RuleData = lambda r:(r.Class,)
496 ChainRuleData = lambda r:(r.LookAhead, r.Input, r.Backtrack)
497 def SetRuleData(r, d):(r.Class,) = d
498 def ChainSetRuleData(r, d):(r.LookAhead, r.Input, r.Backtrack) = d
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400499 elif Format == 3:
500 Coverage = lambda r: r.Coverage[0]
501 ChainCoverage = lambda r: r.InputCoverage[0]
502 ContextData = None
503 ChainContextData = None
504 RuleData = lambda r: r.Coverage
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400505 ChainRuleData = lambda r:(r.LookAheadCoverage +
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400506 r.InputCoverage +
507 r.BacktrackCoverage)
508 SetRuleData = None
509 ChainSetRuleData = None
510 else:
511 assert 0, "unknown format: %s" % Format
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400512
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400513 if Chain:
514 self.Coverage = ChainCoverage
515 self.ContextData = ChainContextData
516 self.RuleData = ChainRuleData
517 self.SetRuleData = ChainSetRuleData
518 else:
519 self.Coverage = Coverage
520 self.ContextData = ContextData
521 self.RuleData = RuleData
522 self.SetRuleData = SetRuleData
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400523
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400524 if Format == 1:
525 self.Rule = ChainTyp+'Rule'
526 self.RuleCount = ChainTyp+'RuleCount'
527 self.RuleSet = ChainTyp+'RuleSet'
528 self.RuleSetCount = ChainTyp+'RuleSetCount'
529 self.Intersect = lambda glyphs, c, r: [r] if r in glyphs else []
530 elif Format == 2:
531 self.Rule = ChainTyp+'ClassRule'
532 self.RuleCount = ChainTyp+'ClassRuleCount'
533 self.RuleSet = ChainTyp+'ClassSet'
534 self.RuleSetCount = ChainTyp+'ClassSetCount'
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400535 self.Intersect = lambda glyphs, c, r: c.intersect_class(glyphs, r)
Behdad Esfahbod89987002013-07-23 23:07:42 -0400536
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400537 self.ClassDef = 'InputClassDef' if Chain else 'ClassDef'
Behdad Esfahbod98b60752013-10-14 17:49:19 +0200538 self.ClassDefIndex = 1 if Chain else 0
Behdad Esfahbod11763302013-08-14 15:33:08 -0400539 self.Input = 'Input' if Chain else 'Class'
Behdad Esfahbod27108392013-07-23 16:40:47 -0400540
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400541 if self.Format not in [1, 2, 3]:
Behdad Esfahbod318adc02013-08-13 20:09:28 -0400542 return None # Don't shoot the messenger; let it go
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400543 if not hasattr(self.__class__, "__ContextHelpers"):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400544 self.__class__.__ContextHelpers = {}
545 if self.Format not in self.__class__.__ContextHelpers:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400546 helper = ContextHelper(self.__class__, self.Format)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400547 self.__class__.__ContextHelpers[self.Format] = helper
548 return self.__class__.__ContextHelpers[self.Format]
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400549
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400550@_add_method(otTables.ContextSubst,
551 otTables.ChainContextSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400552def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e6ef942013-12-04 16:31:44 -0500553 if cur_glyphs is None: cur_glyphs = s.glyphs
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400554 c = self.__classify_context()
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400555
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400556 indices = c.Coverage(self).intersect(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400557 if not indices:
558 return []
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400559 cur_glyphs = c.Coverage(self).intersect_glyphs(s.glyphs);
Behdad Esfahbod1d4fa132013-08-08 22:59:32 -0400560
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400561 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400562 ContextData = c.ContextData(self)
563 rss = getattr(self, c.RuleSet)
Behdad Esfahbod11174452013-11-18 20:20:49 -0500564 rssCount = getattr(self, c.RuleSetCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400565 for i in indices:
Behdad Esfahbod11174452013-11-18 20:20:49 -0500566 if i >= rssCount or not rss[i]: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400567 for r in getattr(rss[i], c.Rule):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400568 if not r: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400569 if all(all(c.Intersect(s.glyphs, cd, k) for k in klist)
570 for cd,klist in zip(ContextData, c.RuleData(r))):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400571 chaos = False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400572 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400573 if not ll: continue
574 seqi = ll.SequenceIndex
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400575 if chaos:
576 pos_glyphs = s.glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400577 else:
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400578 if seqi == 0:
579 pos_glyphs = set([c.Coverage(self).glyphs[i]])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400580 else:
Behdad Esfahbodd3fdcc72013-08-14 17:59:31 -0400581 pos_glyphs = set([r.Input[seqi - 1]])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400582 lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400583 chaos = chaos or lookup.may_have_non_1to1()
584 lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400585 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400586 ClassDef = getattr(self, c.ClassDef)
587 indices = ClassDef.intersect(cur_glyphs)
588 ContextData = c.ContextData(self)
589 rss = getattr(self, c.RuleSet)
Behdad Esfahbod11174452013-11-18 20:20:49 -0500590 rssCount = getattr(self, c.RuleSetCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400591 for i in indices:
Behdad Esfahbod11174452013-11-18 20:20:49 -0500592 if i >= rssCount or not rss[i]: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400593 for r in getattr(rss[i], c.Rule):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400594 if not r: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400595 if all(all(c.Intersect(s.glyphs, cd, k) for k in klist)
596 for cd,klist in zip(ContextData, c.RuleData(r))):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400597 chaos = False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400598 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400599 if not ll: continue
600 seqi = ll.SequenceIndex
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400601 if chaos:
602 pos_glyphs = s.glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400603 else:
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400604 if seqi == 0:
605 pos_glyphs = ClassDef.intersect_class(cur_glyphs, i)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400606 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400607 pos_glyphs = ClassDef.intersect_class(s.glyphs,
Behdad Esfahbod11763302013-08-14 15:33:08 -0400608 getattr(r, c.Input)[seqi - 1])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400609 lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400610 chaos = chaos or lookup.may_have_non_1to1()
611 lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400612 elif self.Format == 3:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400613 if not all(x.intersect(s.glyphs) for x in c.RuleData(self)):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400614 return []
615 r = self
616 chaos = False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400617 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400618 if not ll: continue
619 seqi = ll.SequenceIndex
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400620 if chaos:
621 pos_glyphs = s.glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400622 else:
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400623 if seqi == 0:
624 pos_glyphs = cur_glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400625 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400626 pos_glyphs = r.InputCoverage[seqi].intersect_glyphs(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400627 lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400628 chaos = chaos or lookup.may_have_non_1to1()
629 lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400630 else:
631 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod00776972013-07-23 15:33:00 -0400632
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400633@_add_method(otTables.ContextSubst,
634 otTables.ContextPos,
635 otTables.ChainContextSubst,
636 otTables.ChainContextPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400637def subset_glyphs(self, s):
638 c = self.__classify_context()
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400639
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400640 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400641 indices = self.Coverage.subset(s.glyphs)
642 rss = getattr(self, c.RuleSet)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400643 rss = [rss[i] for i in indices]
644 for rs in rss:
645 if not rs: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400646 ss = getattr(rs, c.Rule)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400647 ss = [r for r in ss
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400648 if r and all(all(g in s.glyphs for g in glist)
649 for glist in c.RuleData(r))]
650 setattr(rs, c.Rule, ss)
651 setattr(rs, c.RuleCount, len(ss))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400652 # Prune empty subrulesets
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400653 rss = [rs for rs in rss if rs and getattr(rs, c.Rule)]
654 setattr(self, c.RuleSet, rss)
655 setattr(self, c.RuleSetCount, len(rss))
656 return bool(rss)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400657 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400658 if not self.Coverage.subset(s.glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400659 return False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400660 ContextData = c.ContextData(self)
661 klass_maps = [x.subset(s.glyphs, remap=True) for x in ContextData]
Behdad Esfahbod98b60752013-10-14 17:49:19 +0200662
663 # Keep rulesets for class numbers that survived.
664 indices = klass_maps[c.ClassDefIndex]
665 rss = getattr(self, c.RuleSet)
666 rssCount = getattr(self, c.RuleSetCount)
667 rss = [rss[i] for i in indices if i < rssCount]
668 del rssCount
669 # Delete, but not renumber, unreachable rulesets.
670 indices = getattr(self, c.ClassDef).intersect(self.Coverage.glyphs)
671 rss = [rss if i in indices else None for i,rss in enumerate(rss)]
Behdad Esfahbod9e6ef942013-12-04 16:31:44 -0500672 while rss and rss[-1] is None:
Behdad Esfahbod98b60752013-10-14 17:49:19 +0200673 del rss[-1]
674
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400675 for rs in rss:
676 if not rs: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400677 ss = getattr(rs, c.Rule)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400678 ss = [r for r in ss
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400679 if r and all(all(k in klass_map for k in klist)
680 for klass_map,klist in zip(klass_maps, c.RuleData(r)))]
681 setattr(rs, c.Rule, ss)
682 setattr(rs, c.RuleCount, len(ss))
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400683
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400684 # Remap rule classes
685 for r in ss:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400686 c.SetRuleData(r, [[klass_map.index(k) for k in klist]
687 for klass_map,klist in zip(klass_maps, c.RuleData(r))])
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400688 return bool(rss)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400689 elif self.Format == 3:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400690 return all(x.subset(s.glyphs) for x in c.RuleData(self))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400691 else:
692 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400693
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400694@_add_method(otTables.ContextSubst,
695 otTables.ChainContextSubst,
696 otTables.ContextPos,
697 otTables.ChainContextPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400698def subset_lookups(self, lookup_indices):
699 c = self.__classify_context()
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400700
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400701 if self.Format in [1, 2]:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400702 for rs in getattr(self, c.RuleSet):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400703 if not rs: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400704 for r in getattr(rs, c.Rule):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400705 if not r: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400706 setattr(r, c.LookupRecord,
707 [ll for ll in getattr(r, c.LookupRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400708 if ll and ll.LookupListIndex in lookup_indices])
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400709 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400710 if not ll: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400711 ll.LookupListIndex = lookup_indices.index(ll.LookupListIndex)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400712 elif self.Format == 3:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400713 setattr(self, c.LookupRecord,
714 [ll for ll in getattr(self, c.LookupRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400715 if ll and ll.LookupListIndex in lookup_indices])
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400716 for ll in getattr(self, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400717 if not ll: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400718 ll.LookupListIndex = lookup_indices.index(ll.LookupListIndex)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400719 else:
720 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400721
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400722@_add_method(otTables.ContextSubst,
723 otTables.ChainContextSubst,
724 otTables.ContextPos,
725 otTables.ChainContextPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400726def collect_lookups(self):
727 c = self.__classify_context()
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400728
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400729 if self.Format in [1, 2]:
730 return [ll.LookupListIndex
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400731 for rs in getattr(self, c.RuleSet) if rs
732 for r in getattr(rs, c.Rule) if r
733 for ll in getattr(r, c.LookupRecord) if ll]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400734 elif self.Format == 3:
735 return [ll.LookupListIndex
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400736 for ll in getattr(self, c.LookupRecord) if ll]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400737 else:
738 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400739
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400740@_add_method(otTables.ExtensionSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400741def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400742 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400743 self.ExtSubTable.closure_glyphs(s, cur_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400744 else:
745 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400746
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400747@_add_method(otTables.ExtensionSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400748def may_have_non_1to1(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400749 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400750 return self.ExtSubTable.may_have_non_1to1()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400751 else:
752 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbodaeacc152013-08-12 20:24:33 -0400753
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400754@_add_method(otTables.ExtensionSubst,
755 otTables.ExtensionPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400756def prune_pre_subset(self, options):
757 if self.Format == 1:
758 return self.ExtSubTable.prune_pre_subset(options)
759 else:
760 assert 0, "unknown format: %s" % self.Format
761
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400762@_add_method(otTables.ExtensionSubst,
763 otTables.ExtensionPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400764def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400765 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400766 return self.ExtSubTable.subset_glyphs(s)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400767 else:
768 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400769
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400770@_add_method(otTables.ExtensionSubst,
771 otTables.ExtensionPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400772def prune_post_subset(self, options):
773 if self.Format == 1:
774 return self.ExtSubTable.prune_post_subset(options)
775 else:
776 assert 0, "unknown format: %s" % self.Format
777
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400778@_add_method(otTables.ExtensionSubst,
779 otTables.ExtensionPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400780def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400781 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400782 return self.ExtSubTable.subset_lookups(lookup_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400783 else:
784 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400785
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400786@_add_method(otTables.ExtensionSubst,
787 otTables.ExtensionPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400788def collect_lookups(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400789 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400790 return self.ExtSubTable.collect_lookups()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400791 else:
792 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400793
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400794@_add_method(otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400795def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400796 for st in self.SubTable:
797 if not st: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400798 st.closure_glyphs(s, cur_glyphs)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400799
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400800@_add_method(otTables.Lookup)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400801def prune_pre_subset(self, options):
802 ret = False
803 for st in self.SubTable:
804 if not st: continue
805 if st.prune_pre_subset(options): ret = True
806 return ret
807
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400808@_add_method(otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400809def subset_glyphs(self, s):
810 self.SubTable = [st for st in self.SubTable if st and st.subset_glyphs(s)]
811 self.SubTableCount = len(self.SubTable)
812 return bool(self.SubTableCount)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400813
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400814@_add_method(otTables.Lookup)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400815def prune_post_subset(self, options):
816 ret = False
817 for st in self.SubTable:
818 if not st: continue
819 if st.prune_post_subset(options): ret = True
820 return ret
821
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400822@_add_method(otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400823def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400824 for s in self.SubTable:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400825 s.subset_lookups(lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400826
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400827@_add_method(otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400828def collect_lookups(self):
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400829 return _uniq_sort(sum((st.collect_lookups() for st in self.SubTable
830 if st), []))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400831
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400832@_add_method(otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400833def may_have_non_1to1(self):
834 return any(st.may_have_non_1to1() for st in self.SubTable if st)
Behdad Esfahbodaeacc152013-08-12 20:24:33 -0400835
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400836@_add_method(otTables.LookupList)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400837def prune_pre_subset(self, options):
838 ret = False
839 for l in self.Lookup:
840 if not l: continue
841 if l.prune_pre_subset(options): ret = True
842 return ret
843
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400844@_add_method(otTables.LookupList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400845def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400846 "Returns the indices of nonempty lookups."
Behdad Esfahbod4734be52013-08-14 19:47:42 -0400847 return [i for i,l in enumerate(self.Lookup) if l and l.subset_glyphs(s)]
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400848
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400849@_add_method(otTables.LookupList)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400850def prune_post_subset(self, options):
851 ret = False
852 for l in self.Lookup:
853 if not l: continue
854 if l.prune_post_subset(options): ret = True
855 return ret
856
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400857@_add_method(otTables.LookupList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400858def subset_lookups(self, lookup_indices):
Behdad Esfahbod6c51f502013-12-15 23:12:26 -0500859 self.ensureDecompiled()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400860 self.Lookup = [self.Lookup[i] for i in lookup_indices
861 if i < self.LookupCount]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400862 self.LookupCount = len(self.Lookup)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400863 for l in self.Lookup:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400864 l.subset_lookups(lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400865
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400866@_add_method(otTables.LookupList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400867def closure_lookups(self, lookup_indices):
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400868 lookup_indices = _uniq_sort(lookup_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400869 recurse = lookup_indices
870 while True:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400871 recurse_lookups = sum((self.Lookup[i].collect_lookups()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400872 for i in recurse if i < self.LookupCount), [])
873 recurse_lookups = [l for l in recurse_lookups
874 if l not in lookup_indices and l < self.LookupCount]
875 if not recurse_lookups:
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400876 return _uniq_sort(lookup_indices)
877 recurse_lookups = _uniq_sort(recurse_lookups)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400878 lookup_indices.extend(recurse_lookups)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400879 recurse = recurse_lookups
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400880
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400881@_add_method(otTables.Feature)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400882def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400883 self.LookupListIndex = [l for l in self.LookupListIndex
884 if l in lookup_indices]
885 # Now map them.
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400886 self.LookupListIndex = [lookup_indices.index(l)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400887 for l in self.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400888 self.LookupCount = len(self.LookupListIndex)
Behdad Esfahbodd214f202013-11-26 17:42:13 -0500889 return self.LookupCount or self.FeatureParams
Behdad Esfahbod54660612013-07-21 18:16:55 -0400890
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400891@_add_method(otTables.Feature)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400892def collect_lookups(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400893 return self.LookupListIndex[:]
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400894
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400895@_add_method(otTables.FeatureList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400896def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400897 "Returns the indices of nonempty features."
Behdad Esfahbod0be386e2013-12-16 20:52:52 -0500898 # Note: Never ever drop feature 'pref', even if it's empty.
899 # HarfBuzz chooses shaper for Khmer based on presence of this
900 # feature. See thread at:
901 # http://lists.freedesktop.org/archives/harfbuzz/2012-November/002660.html
Behdad Esfahbod4734be52013-08-14 19:47:42 -0400902 feature_indices = [i for i,f in enumerate(self.FeatureRecord)
Behdad Esfahbod0be386e2013-12-16 20:52:52 -0500903 if (f.Feature.subset_lookups(lookup_indices) or
904 f.FeatureTag == 'pref')]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400905 self.subset_features(feature_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400906 return feature_indices
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400907
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400908@_add_method(otTables.FeatureList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400909def collect_lookups(self, feature_indices):
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400910 return _uniq_sort(sum((self.FeatureRecord[i].Feature.collect_lookups()
911 for i in feature_indices
Behdad Esfahbod1ee298d2013-08-13 20:07:09 -0400912 if i < self.FeatureCount), []))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400913
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400914@_add_method(otTables.FeatureList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400915def subset_features(self, feature_indices):
Behdad Esfahbod6c51f502013-12-15 23:12:26 -0500916 self.ensureDecompiled()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400917 self.FeatureRecord = [self.FeatureRecord[i] for i in feature_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400918 self.FeatureCount = len(self.FeatureRecord)
919 return bool(self.FeatureCount)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400920
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400921@_add_method(otTables.DefaultLangSys,
922 otTables.LangSys)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400923def subset_features(self, feature_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400924 if self.ReqFeatureIndex in feature_indices:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400925 self.ReqFeatureIndex = feature_indices.index(self.ReqFeatureIndex)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400926 else:
927 self.ReqFeatureIndex = 65535
928 self.FeatureIndex = [f for f in self.FeatureIndex if f in feature_indices]
929 # Now map them.
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400930 self.FeatureIndex = [feature_indices.index(f) for f in self.FeatureIndex
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400931 if f in feature_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400932 self.FeatureCount = len(self.FeatureIndex)
933 return bool(self.FeatureCount or self.ReqFeatureIndex != 65535)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400934
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400935@_add_method(otTables.DefaultLangSys,
936 otTables.LangSys)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400937def collect_features(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400938 feature_indices = self.FeatureIndex[:]
939 if self.ReqFeatureIndex != 65535:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400940 feature_indices.append(self.ReqFeatureIndex)
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400941 return _uniq_sort(feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400942
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400943@_add_method(otTables.Script)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400944def subset_features(self, feature_indices):
945 if(self.DefaultLangSys and
946 not self.DefaultLangSys.subset_features(feature_indices)):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400947 self.DefaultLangSys = None
948 self.LangSysRecord = [l for l in self.LangSysRecord
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400949 if l.LangSys.subset_features(feature_indices)]
950 self.LangSysCount = len(self.LangSysRecord)
951 return bool(self.LangSysCount or self.DefaultLangSys)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400952
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400953@_add_method(otTables.Script)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400954def collect_features(self):
955 feature_indices = [l.LangSys.collect_features() for l in self.LangSysRecord]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400956 if self.DefaultLangSys:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400957 feature_indices.append(self.DefaultLangSys.collect_features())
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400958 return _uniq_sort(sum(feature_indices, []))
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400959
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400960@_add_method(otTables.ScriptList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400961def subset_features(self, feature_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400962 self.ScriptRecord = [s for s in self.ScriptRecord
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400963 if s.Script.subset_features(feature_indices)]
964 self.ScriptCount = len(self.ScriptRecord)
965 return bool(self.ScriptCount)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400966
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400967@_add_method(otTables.ScriptList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400968def collect_features(self):
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400969 return _uniq_sort(sum((s.Script.collect_features()
970 for s in self.ScriptRecord), []))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400971
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400972@_add_method(ttLib.getTableClass('GSUB'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400973def closure_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400974 s.table = self.table
Behdad Esfahbod19d7cf22013-12-04 21:13:11 -0500975 if self.table.ScriptList:
976 feature_indices = self.table.ScriptList.collect_features()
977 else:
978 feature_indices = []
Behdad Esfahbod7e972472013-11-15 17:57:15 -0500979 if self.table.FeatureList:
980 lookup_indices = self.table.FeatureList.collect_lookups(feature_indices)
981 else:
982 lookup_indices = []
983 if self.table.LookupList:
984 while True:
985 orig_glyphs = s.glyphs.copy()
986 for i in lookup_indices:
987 if i >= self.table.LookupList.LookupCount: continue
988 if not self.table.LookupList.Lookup[i]: continue
989 self.table.LookupList.Lookup[i].closure_glyphs(s)
990 if orig_glyphs == s.glyphs:
991 break
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400992 del s.table
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400993
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400994@_add_method(ttLib.getTableClass('GSUB'),
995 ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400996def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400997 s.glyphs = s.glyphs_gsubed
Behdad Esfahbod7e972472013-11-15 17:57:15 -0500998 if self.table.LookupList:
999 lookup_indices = self.table.LookupList.subset_glyphs(s)
1000 else:
1001 lookup_indices = []
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001002 self.subset_lookups(lookup_indices)
1003 self.prune_lookups()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001004 return True
Behdad Esfahbod02b92062013-07-21 18:40:59 -04001005
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001006@_add_method(ttLib.getTableClass('GSUB'),
1007 ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001008def subset_lookups(self, lookup_indices):
Behdad Esfahbodbaa97d62013-12-06 21:11:22 -05001009 """Retains specified lookups, then removes empty features, language
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001010 systems, and scripts."""
Behdad Esfahbod7e972472013-11-15 17:57:15 -05001011 if self.table.LookupList:
1012 self.table.LookupList.subset_lookups(lookup_indices)
1013 if self.table.FeatureList:
1014 feature_indices = self.table.FeatureList.subset_lookups(lookup_indices)
1015 else:
1016 feature_indices = []
Behdad Esfahbod19d7cf22013-12-04 21:13:11 -05001017 if self.table.ScriptList:
1018 self.table.ScriptList.subset_features(feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -04001019
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001020@_add_method(ttLib.getTableClass('GSUB'),
1021 ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001022def prune_lookups(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001023 "Remove unreferenced lookups"
Behdad Esfahbod19d7cf22013-12-04 21:13:11 -05001024 if self.table.ScriptList:
1025 feature_indices = self.table.ScriptList.collect_features()
1026 else:
1027 feature_indices = []
Behdad Esfahbod7e972472013-11-15 17:57:15 -05001028 if self.table.FeatureList:
1029 lookup_indices = self.table.FeatureList.collect_lookups(feature_indices)
1030 else:
1031 lookup_indices = []
1032 if self.table.LookupList:
1033 lookup_indices = self.table.LookupList.closure_lookups(lookup_indices)
1034 else:
1035 lookup_indices = []
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001036 self.subset_lookups(lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -04001037
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001038@_add_method(ttLib.getTableClass('GSUB'),
1039 ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001040def subset_feature_tags(self, feature_tags):
Behdad Esfahbod7e972472013-11-15 17:57:15 -05001041 if self.table.FeatureList:
1042 feature_indices = [i for i,f in
1043 enumerate(self.table.FeatureList.FeatureRecord)
1044 if f.FeatureTag in feature_tags]
1045 self.table.FeatureList.subset_features(feature_indices)
1046 else:
1047 feature_indices = []
Behdad Esfahbod19d7cf22013-12-04 21:13:11 -05001048 if self.table.ScriptList:
1049 self.table.ScriptList.subset_features(feature_indices)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -04001050
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001051@_add_method(ttLib.getTableClass('GSUB'),
1052 ttLib.getTableClass('GPOS'))
Behdad Esfahbodec9436d2013-12-06 21:58:41 -05001053def prune_features(self):
1054 "Remove unreferenced featurs"
1055 if self.table.ScriptList:
1056 feature_indices = self.table.ScriptList.collect_features()
1057 else:
1058 feature_indices = []
1059 if self.table.FeatureList:
1060 self.table.FeatureList.subset_features(feature_indices)
1061 if self.table.ScriptList:
1062 self.table.ScriptList.subset_features(feature_indices)
1063
1064@_add_method(ttLib.getTableClass('GSUB'),
1065 ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001066def prune_pre_subset(self, options):
Behdad Esfahbod9d2481b2013-12-06 21:42:36 -05001067 # Drop undesired features
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001068 if '*' not in options.layout_features:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001069 self.subset_feature_tags(options.layout_features)
Behdad Esfahbod9d2481b2013-12-06 21:42:36 -05001070 # Drop unreferenced lookups
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001071 self.prune_lookups()
Behdad Esfahbod9d2481b2013-12-06 21:42:36 -05001072 # Prune lookups themselves
Behdad Esfahbod7e972472013-11-15 17:57:15 -05001073 if self.table.LookupList:
1074 self.table.LookupList.prune_pre_subset(options);
Behdad Esfahbodd77f1572013-08-15 19:24:36 -04001075 return True
1076
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001077@_add_method(ttLib.getTableClass('GSUB'),
1078 ttLib.getTableClass('GPOS'))
Behdad Esfahbod3db5e112013-12-07 12:54:44 -05001079def remove_redundant_langsys(self):
1080 table = self.table
1081 if not table.ScriptList or not table.FeatureList:
1082 return
1083
1084 features = table.FeatureList.FeatureRecord
1085
1086 for s in table.ScriptList.ScriptRecord:
1087 d = s.Script.DefaultLangSys
1088 if not d:
1089 continue
1090 for lr in s.Script.LangSysRecord[:]:
1091 l = lr.LangSys
1092 # Compare d and l
1093 if len(d.FeatureIndex) != len(l.FeatureIndex):
1094 continue
1095 if (d.ReqFeatureIndex == 65535) != (l.ReqFeatureIndex == 65535):
1096 continue
1097
1098 if d.ReqFeatureIndex != 65535:
1099 if features[d.ReqFeatureIndex] != features[l.ReqFeatureIndex]:
1100 continue
1101
1102 for i in range(len(d.FeatureIndex)):
1103 if features[d.FeatureIndex[i]] != features[l.FeatureIndex[i]]:
Behdad Esfahbod3db5e112013-12-07 12:54:44 -05001104 break
1105 else:
1106 # LangSys and default are equal; delete LangSys
1107 s.Script.LangSysRecord.remove(lr)
1108
1109@_add_method(ttLib.getTableClass('GSUB'),
1110 ttLib.getTableClass('GPOS'))
Behdad Esfahbodd77f1572013-08-15 19:24:36 -04001111def prune_post_subset(self, options):
Behdad Esfahbod9fe4eef2013-11-25 04:28:37 -05001112 table = self.table
Behdad Esfahbodec9436d2013-12-06 21:58:41 -05001113
1114 # LookupList looks good. Just prune lookups themselves
Behdad Esfahbod9fe4eef2013-11-25 04:28:37 -05001115 if table.LookupList:
1116 table.LookupList.prune_post_subset(options);
Behdad Esfahbod92af6a52013-12-10 17:51:32 -05001117 # XXX Next two lines disabled because OTS is stupid and
1118 # doesn't like NULL offsetse here.
1119 #if not table.LookupList.Lookup:
1120 # table.LookupList = None
Behdad Esfahbodec9436d2013-12-06 21:58:41 -05001121
1122 if not table.LookupList:
1123 table.FeatureList = None
1124
1125 if table.FeatureList:
Behdad Esfahbod3db5e112013-12-07 12:54:44 -05001126 self.remove_redundant_langsys()
Behdad Esfahbodec9436d2013-12-06 21:58:41 -05001127 # Remove unreferenced features
1128 self.prune_features()
1129
Behdad Esfahbod92af6a52013-12-10 17:51:32 -05001130 # XXX Next two lines disabled because OTS is stupid and
1131 # doesn't like NULL offsetse here.
1132 #if table.FeatureList and not table.FeatureList.FeatureRecord:
1133 # table.FeatureList = None
Behdad Esfahbodec9436d2013-12-06 21:58:41 -05001134
1135 # Never drop scripts themselves as them just being available
1136 # holds semantic significance.
Behdad Esfahbod92af6a52013-12-10 17:51:32 -05001137 # XXX Next two lines disabled because OTS is stupid and
1138 # doesn't like NULL offsetse here.
1139 #if table.ScriptList and not table.ScriptList.ScriptRecord:
1140 # table.ScriptList = None
Behdad Esfahbodec9436d2013-12-06 21:58:41 -05001141
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001142 return True
Behdad Esfahbod356c42e2013-07-23 12:10:46 -04001143
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001144@_add_method(ttLib.getTableClass('GDEF'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001145def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001146 glyphs = s.glyphs_gsubed
1147 table = self.table
1148 if table.LigCaretList:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001149 indices = table.LigCaretList.Coverage.subset(glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001150 table.LigCaretList.LigGlyph = [table.LigCaretList.LigGlyph[i]
1151 for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001152 table.LigCaretList.LigGlyphCount = len(table.LigCaretList.LigGlyph)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001153 if table.MarkAttachClassDef:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001154 table.MarkAttachClassDef.classDefs = dict((g,v) for g,v in
1155 table.MarkAttachClassDef.
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001156 classDefs.items()
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001157 if g in glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001158 if table.GlyphClassDef:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001159 table.GlyphClassDef.classDefs = dict((g,v) for g,v in
1160 table.GlyphClassDef.
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001161 classDefs.items()
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001162 if g in glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001163 if table.AttachList:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001164 indices = table.AttachList.Coverage.subset(glyphs)
Behdad Esfahbod98769432013-11-19 14:40:57 -05001165 GlyphCount = table.AttachList.GlyphCount
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001166 table.AttachList.AttachPoint = [table.AttachList.AttachPoint[i]
Behdad Esfahbod98769432013-11-19 14:40:57 -05001167 for i in indices
1168 if i < GlyphCount]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001169 table.AttachList.GlyphCount = len(table.AttachList.AttachPoint)
Behdad Esfahbod5aea27d2013-11-25 04:19:42 -05001170 if hasattr(table, "MarkGlyphSetsDef") and table.MarkGlyphSetsDef:
1171 for coverage in table.MarkGlyphSetsDef.Coverage:
1172 coverage.subset(glyphs)
Behdad Esfahbod05da9702013-11-25 05:23:07 -05001173 # TODO: The following is disabled. If enabling, we need to go fixup all
1174 # lookups that use MarkFilteringSet and map their set.
1175 #indices = table.MarkGlyphSetsDef.Coverage = [c for c in table.MarkGlyphSetsDef.Coverage if c.glyphs]
Behdad Esfahbod5aea27d2013-11-25 04:19:42 -05001176 return True
1177
1178@_add_method(ttLib.getTableClass('GDEF'))
1179def prune_post_subset(self, options):
1180 table = self.table
Behdad Esfahbodfa95e872013-12-15 22:02:20 -05001181 # XXX check these against OTS
Behdad Esfahbod5aea27d2013-11-25 04:19:42 -05001182 if table.LigCaretList and not table.LigCaretList.LigGlyphCount:
1183 table.LigCaretList = None
1184 if table.MarkAttachClassDef and not table.MarkAttachClassDef.classDefs:
1185 table.MarkAttachClassDef = None
1186 if table.GlyphClassDef and not table.GlyphClassDef.classDefs:
1187 table.GlyphClassDef = None
1188 if table.AttachList and not table.AttachList.GlyphCount:
1189 table.AttachList = None
1190 if hasattr(table, "MarkGlyphSetsDef") and table.MarkGlyphSetsDef and not table.MarkGlyphSetsDef.Coverage:
1191 table.MarkGlyphSetsDef = None
Behdad Esfahboda030a0d2013-11-27 17:46:15 -05001192 if table.Version == 0x00010002/0x10000:
Behdad Esfahbod5aea27d2013-11-25 04:19:42 -05001193 table.Version = 1.0
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001194 return bool(table.LigCaretList or
Behdad Esfahbod5aea27d2013-11-25 04:19:42 -05001195 table.MarkAttachClassDef or
1196 table.GlyphClassDef or
1197 table.AttachList or
Behdad Esfahboda030a0d2013-11-27 17:46:15 -05001198 (table.Version >= 0x00010002/0x10000 and table.MarkGlyphSetsDef))
Behdad Esfahbodefb984a2013-07-21 22:26:16 -04001199
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001200@_add_method(ttLib.getTableClass('kern'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001201def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001202 # Prune unknown kern table types
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001203 self.kernTables = [t for t in self.kernTables if hasattr(t, 'kernTable')]
1204 return bool(self.kernTables)
Behdad Esfahbodd4e33a72013-07-24 18:51:05 -04001205
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001206@_add_method(ttLib.getTableClass('kern'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001207def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001208 glyphs = s.glyphs_gsubed
1209 for t in self.kernTables:
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001210 t.kernTable = dict(((a,b),v) for (a,b),v in t.kernTable.items()
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001211 if a in glyphs and b in glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001212 self.kernTables = [t for t in self.kernTables if t.kernTable]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001213 return bool(self.kernTables)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -04001214
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001215@_add_method(ttLib.getTableClass('vmtx'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001216def subset_glyphs(self, s):
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001217 self.metrics = dict((g,v) for g,v in self.metrics.items() if g in s.glyphs)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001218 return bool(self.metrics)
Behdad Esfahbodc7160442013-07-22 14:29:08 -04001219
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001220@_add_method(ttLib.getTableClass('hmtx'))
1221def subset_glyphs(self, s):
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001222 self.metrics = dict((g,v) for g,v in self.metrics.items() if g in s.glyphs)
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001223 return True # Required table
1224
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001225@_add_method(ttLib.getTableClass('hdmx'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001226def subset_glyphs(self, s):
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001227 self.hdmx = dict((sz,dict((g,v) for g,v in l.items() if g in s.glyphs))
1228 for sz,l in self.hdmx.items())
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001229 return bool(self.hdmx)
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -04001230
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001231@_add_method(ttLib.getTableClass('VORG'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001232def subset_glyphs(self, s):
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001233 self.VOriginRecords = dict((g,v) for g,v in self.VOriginRecords.items()
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001234 if g in s.glyphs)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001235 self.numVertOriginYMetrics = len(self.VOriginRecords)
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001236 return True # Never drop; has default metrics
Behdad Esfahbode45d6af2013-07-22 15:29:17 -04001237
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001238@_add_method(ttLib.getTableClass('post'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001239def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001240 if not options.glyph_names:
1241 self.formatType = 3.0
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001242 return True # Required table
Behdad Esfahbod42648242013-07-23 12:56:06 -04001243
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001244@_add_method(ttLib.getTableClass('post'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001245def subset_glyphs(self, s):
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001246 self.extraNames = [] # This seems to do it
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001247 return True # Required table
Behdad Esfahbod653e9742013-07-22 15:17:12 -04001248
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001249@_add_method(ttLib.getTableModule('glyf').Glyph)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001250def remapComponentsFast(self, indices):
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001251 if not self.data or struct.unpack(">h", self.data[:2])[0] >= 0:
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001252 return # Not composite
Behdad Esfahbodd816b7f2013-08-16 16:18:40 -04001253 data = array.array("B", self.data)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001254 i = 10
1255 more = 1
1256 while more:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001257 flags =(data[i] << 8) | data[i+1]
1258 glyphID =(data[i+2] << 8) | data[i+3]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001259 # Remap
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001260 glyphID = indices.index(glyphID)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001261 data[i+2] = glyphID >> 8
1262 data[i+3] = glyphID & 0xFF
1263 i += 4
1264 flags = int(flags)
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -04001265
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04001266 if flags & 0x0001: i += 4 # ARG_1_AND_2_ARE_WORDS
Behdad Esfahbod574ce792013-08-13 20:51:44 -04001267 else: i += 2
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04001268 if flags & 0x0008: i += 2 # WE_HAVE_A_SCALE
1269 elif flags & 0x0040: i += 4 # WE_HAVE_AN_X_AND_Y_SCALE
1270 elif flags & 0x0080: i += 8 # WE_HAVE_A_TWO_BY_TWO
1271 more = flags & 0x0020 # MORE_COMPONENTS
1272
Behdad Esfahbodd816b7f2013-08-16 16:18:40 -04001273 self.data = data.tostring()
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -04001274
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001275@_add_method(ttLib.getTableClass('glyf'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001276def closure_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001277 decompose = s.glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001278 while True:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001279 components = set()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001280 for g in decompose:
1281 if g not in self.glyphs:
1282 continue
1283 gl = self.glyphs[g]
Behdad Esfahbod043108c2013-09-27 12:59:47 -04001284 for c in gl.getComponentNames(self):
Behdad Esfahbod626107c2013-09-20 14:10:31 -04001285 if c not in s.glyphs:
1286 components.add(c)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001287 components = set(c for c in components if c not in s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001288 if not components:
1289 break
1290 decompose = components
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001291 s.glyphs.update(components)
Behdad Esfahbodabb50a12013-07-23 12:58:37 -04001292
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001293@_add_method(ttLib.getTableClass('glyf'))
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001294def prune_pre_subset(self, options):
1295 if options.notdef_glyph and not options.notdef_outline:
1296 g = self[self.glyphOrder[0]]
1297 # Yay, easy!
1298 g.__dict__.clear()
1299 g.data = ""
1300 return True
1301
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001302@_add_method(ttLib.getTableClass('glyf'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001303def subset_glyphs(self, s):
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001304 self.glyphs = dict((g,v) for g,v in self.glyphs.items() if g in s.glyphs)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001305 indices = [i for i,g in enumerate(self.glyphOrder) if g in s.glyphs]
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001306 for v in self.glyphs.values():
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001307 if hasattr(v, "data"):
1308 v.remapComponentsFast(indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001309 else:
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001310 pass # No need
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001311 self.glyphOrder = [g for g in self.glyphOrder if g in s.glyphs]
Behdad Esfahbodb69b6712013-08-29 18:17:31 -04001312 # Don't drop empty 'glyf' tables, otherwise 'loca' doesn't get subset.
1313 return True
Behdad Esfahbod861d9152013-07-22 16:47:24 -04001314
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001315@_add_method(ttLib.getTableClass('glyf'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001316def prune_post_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001317 if not options.hinting:
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001318 for v in self.glyphs.values():
Behdad Esfahbod626107c2013-09-20 14:10:31 -04001319 v.removeHinting()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001320 return True
Behdad Esfahboded98c612013-07-23 12:37:41 -04001321
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001322@_add_method(ttLib.getTableClass('CFF '))
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001323def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001324 cff = self.cff
Behdad Esfahbode0622072013-09-10 14:33:19 -04001325 # CFF table must have one font only
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001326 cff.fontNames = cff.fontNames[:1]
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001327
1328 if options.notdef_glyph and not options.notdef_outline:
1329 for fontname in cff.keys():
1330 font = cff[fontname]
1331 c,_ = font.CharStrings.getItemAndSelector('.notdef')
Behdad Esfahbod21582e92013-09-12 16:47:52 -04001332 # XXX we should preserve the glyph width
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001333 c.bytecode = '\x0e' # endchar
1334 c.program = None
1335
Behdad Esfahbod50f83ef2013-08-29 18:18:17 -04001336 return True # bool(cff.fontNames)
Behdad Esfahbod1a4e72e2013-08-13 15:46:37 -04001337
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001338@_add_method(ttLib.getTableClass('CFF '))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001339def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001340 cff = self.cff
1341 for fontname in cff.keys():
1342 font = cff[fontname]
1343 cs = font.CharStrings
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001344
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001345 # Load all glyphs
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001346 for g in font.charset:
1347 if g not in s.glyphs: continue
1348 c,sel = cs.getItemAndSelector(g)
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001349
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001350 if cs.charStringsAreIndexed:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001351 indices = [i for i,g in enumerate(font.charset) if g in s.glyphs]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001352 csi = cs.charStringsIndex
1353 csi.items = [csi.items[i] for i in indices]
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001354 csi.count = len(csi.items)
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001355 del csi.file, csi.offsets
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001356 if hasattr(font, "FDSelect"):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001357 sel = font.FDSelect
1358 sel.format = None
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001359 sel.gidArray = [sel.gidArray[i] for i in indices]
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001360 cs.charStrings = dict((g,indices.index(v))
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001361 for g,v in cs.charStrings.items()
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001362 if g in s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001363 else:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001364 cs.charStrings = dict((g,v)
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001365 for g,v in cs.charStrings.items()
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001366 if g in s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001367 font.charset = [g for g in font.charset if g in s.glyphs]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001368 font.numGlyphs = len(font.charset)
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001369
Behdad Esfahbod50f83ef2013-08-29 18:18:17 -04001370 return True # any(cff[fontname].numGlyphs for fontname in cff.keys())
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001371
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001372@_add_method(psCharStrings.T2CharString)
Behdad Esfahbodb4ea9532013-08-14 19:37:39 -04001373def subset_subroutines(self, subrs, gsubrs):
1374 p = self.program
Behdad Esfahbode0622072013-09-10 14:33:19 -04001375 assert len(p)
Behdad Esfahbodb466efe2013-11-27 03:34:35 -05001376 for i in range(1, len(p)):
Behdad Esfahbodb4ea9532013-08-14 19:37:39 -04001377 if p[i] == 'callsubr':
Behdad Esfahbodc2e2e832013-11-27 04:15:27 -05001378 assert isinstance(p[i-1], int)
Behdad Esfahbodb4ea9532013-08-14 19:37:39 -04001379 p[i-1] = subrs._used.index(p[i-1] + subrs._old_bias) - subrs._new_bias
1380 elif p[i] == 'callgsubr':
Behdad Esfahbodc2e2e832013-11-27 04:15:27 -05001381 assert isinstance(p[i-1], int)
Behdad Esfahbodb4ea9532013-08-14 19:37:39 -04001382 p[i-1] = gsubrs._used.index(p[i-1] + gsubrs._old_bias) - gsubrs._new_bias
1383
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001384@_add_method(psCharStrings.T2CharString)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001385def drop_hints(self):
1386 hints = self._hints
1387
1388 if hints.has_hint:
1389 self.program = self.program[hints.last_hint:]
Behdad Esfahbod285d7b82013-09-10 20:30:47 -04001390 if hasattr(self, 'width'):
1391 # Insert width back if needed
1392 if self.width != self.private.defaultWidthX:
Behdad Esfahbod99536852013-09-12 00:23:11 -04001393 self.program.insert(0, self.width - self.private.nominalWidthX)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001394
1395 if hints.has_hintmask:
1396 i = 0
1397 p = self.program
1398 while i < len(p):
1399 if p[i] in ['hintmask', 'cntrmask']:
1400 assert i + 1 <= len(p)
1401 del p[i:i+2]
1402 continue
1403 i += 1
1404
Behdad Esfahbod2a70f4a2013-10-28 15:18:07 +01001405 # TODO: we currently don't drop calls to "empty" subroutines.
1406
Behdad Esfahbode0622072013-09-10 14:33:19 -04001407 assert len(self.program)
1408
1409 del self._hints
1410
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001411class _MarkingT2Decompiler(psCharStrings.SimpleT2Decompiler):
Behdad Esfahbode0622072013-09-10 14:33:19 -04001412
1413 def __init__(self, localSubrs, globalSubrs):
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001414 psCharStrings.SimpleT2Decompiler.__init__(self,
1415 localSubrs,
1416 globalSubrs)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001417 for subrs in [localSubrs, globalSubrs]:
1418 if subrs and not hasattr(subrs, "_used"):
1419 subrs._used = set()
1420
1421 def op_callsubr(self, index):
1422 self.localSubrs._used.add(self.operandStack[-1]+self.localBias)
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001423 psCharStrings.SimpleT2Decompiler.op_callsubr(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001424
1425 def op_callgsubr(self, index):
1426 self.globalSubrs._used.add(self.operandStack[-1]+self.globalBias)
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001427 psCharStrings.SimpleT2Decompiler.op_callgsubr(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001428
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001429class _DehintingT2Decompiler(psCharStrings.SimpleT2Decompiler):
Behdad Esfahbode0622072013-09-10 14:33:19 -04001430
Behdad Esfahbod1f262892013-11-28 14:26:39 -05001431 class Hints(object):
Behdad Esfahbode0622072013-09-10 14:33:19 -04001432 def __init__(self):
1433 # Whether calling this charstring produces any hint stems
1434 self.has_hint = False
1435 # Index to start at to drop all hints
1436 self.last_hint = 0
1437 # Index up to which we know more hints are possible. Only
1438 # relevant if status is 0 or 1.
1439 self.last_checked = 0
1440 # The status means:
1441 # 0: after dropping hints, this charstring is empty
1442 # 1: after dropping hints, there may be more hints continuing after this
1443 # 2: no more hints possible after this charstring
1444 self.status = 0
1445 # Has hintmask instructions; not recursive
1446 self.has_hintmask = False
1447 pass
1448
1449 def __init__(self, css, localSubrs, globalSubrs):
1450 self._css = css
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001451 psCharStrings.SimpleT2Decompiler.__init__(self,
1452 localSubrs,
1453 globalSubrs)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001454
1455 def execute(self, charString):
1456 old_hints = charString._hints if hasattr(charString, '_hints') else None
1457 charString._hints = self.Hints()
1458
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001459 psCharStrings.SimpleT2Decompiler.execute(self, charString)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001460
1461 hints = charString._hints
1462
1463 if hints.has_hint or hints.has_hintmask:
1464 self._css.add(charString)
1465
1466 if hints.status != 2:
1467 # Check from last_check, make sure we didn't have any operators.
Behdad Esfahbodb466efe2013-11-27 03:34:35 -05001468 for i in range(hints.last_checked, len(charString.program) - 1):
Behdad Esfahbodc2e2e832013-11-27 04:15:27 -05001469 if isinstance(charString.program[i], str):
Behdad Esfahbode0622072013-09-10 14:33:19 -04001470 hints.status = 2
1471 break;
1472 else:
1473 hints.status = 1 # There's *something* here
1474 hints.last_checked = len(charString.program)
1475
1476 if old_hints:
1477 assert hints.__dict__ == old_hints.__dict__
1478
1479 def op_callsubr(self, index):
1480 subr = self.localSubrs[self.operandStack[-1]+self.localBias]
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001481 psCharStrings.SimpleT2Decompiler.op_callsubr(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001482 self.processSubr(index, subr)
1483
1484 def op_callgsubr(self, index):
1485 subr = self.globalSubrs[self.operandStack[-1]+self.globalBias]
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001486 psCharStrings.SimpleT2Decompiler.op_callgsubr(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001487 self.processSubr(index, subr)
1488
1489 def op_hstem(self, index):
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001490 psCharStrings.SimpleT2Decompiler.op_hstem(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001491 self.processHint(index)
1492 def op_vstem(self, index):
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001493 psCharStrings.SimpleT2Decompiler.op_vstem(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001494 self.processHint(index)
1495 def op_hstemhm(self, index):
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001496 psCharStrings.SimpleT2Decompiler.op_hstemhm(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001497 self.processHint(index)
1498 def op_vstemhm(self, index):
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001499 psCharStrings.SimpleT2Decompiler.op_vstemhm(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001500 self.processHint(index)
1501 def op_hintmask(self, index):
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001502 psCharStrings.SimpleT2Decompiler.op_hintmask(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001503 self.processHintmask(index)
1504 def op_cntrmask(self, index):
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001505 psCharStrings.SimpleT2Decompiler.op_cntrmask(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001506 self.processHintmask(index)
1507
1508 def processHintmask(self, index):
1509 cs = self.callingStack[-1]
1510 hints = cs._hints
1511 hints.has_hintmask = True
1512 if hints.status != 2 and hints.has_hint:
1513 # Check from last_check, see if we may be an implicit vstem
Behdad Esfahbodb466efe2013-11-27 03:34:35 -05001514 for i in range(hints.last_checked, index - 1):
Behdad Esfahbodc2e2e832013-11-27 04:15:27 -05001515 if isinstance(cs.program[i], str):
Behdad Esfahbod84763142013-09-10 19:00:48 -04001516 hints.status = 2
Behdad Esfahbode0622072013-09-10 14:33:19 -04001517 break;
Behdad Esfahbod84763142013-09-10 19:00:48 -04001518 if hints.status != 2:
Behdad Esfahbode0622072013-09-10 14:33:19 -04001519 # We are an implicit vstem
1520 hints.last_hint = index + 1
Behdad Esfahbod84763142013-09-10 19:00:48 -04001521 hints.status = 0
Behdad Esfahbod2a70f4a2013-10-28 15:18:07 +01001522 hints.last_checked = index + 1
Behdad Esfahbode0622072013-09-10 14:33:19 -04001523
1524 def processHint(self, index):
1525 cs = self.callingStack[-1]
1526 hints = cs._hints
1527 hints.has_hint = True
1528 hints.last_hint = index
1529 hints.last_checked = index
1530
1531 def processSubr(self, index, subr):
1532 cs = self.callingStack[-1]
1533 hints = cs._hints
1534 subr_hints = subr._hints
1535
1536 if subr_hints.has_hint:
Behdad Esfahbod285d7b82013-09-10 20:30:47 -04001537 if hints.status != 2:
1538 hints.has_hint = True
Behdad Esfahbod99536852013-09-12 00:23:11 -04001539 hints.last_checked = index
1540 hints.status = subr_hints.status
Behdad Esfahbod285d7b82013-09-10 20:30:47 -04001541 # Decide where to chop off from
1542 if subr_hints.status == 0:
Behdad Esfahbod99536852013-09-12 00:23:11 -04001543 hints.last_hint = index
Behdad Esfahbod285d7b82013-09-10 20:30:47 -04001544 else:
Behdad Esfahbod99536852013-09-12 00:23:11 -04001545 hints.last_hint = index - 2 # Leave the subr call in
Behdad Esfahbode0622072013-09-10 14:33:19 -04001546 else:
Behdad Esfahbod285d7b82013-09-10 20:30:47 -04001547 # In my understanding, this is a font bug. Ie. it has hint stems
1548 # *after* path construction. I've seen this in widespread fonts.
1549 # Best to ignore the hints I suppose...
1550 pass
1551 #assert 0
Behdad Esfahbode0622072013-09-10 14:33:19 -04001552 else:
1553 hints.status = max(hints.status, subr_hints.status)
1554 if hints.status != 2:
1555 # Check from last_check, make sure we didn't have
1556 # any operators.
Behdad Esfahbodb466efe2013-11-27 03:34:35 -05001557 for i in range(hints.last_checked, index - 1):
Behdad Esfahbodc2e2e832013-11-27 04:15:27 -05001558 if isinstance(cs.program[i], str):
Behdad Esfahbode0622072013-09-10 14:33:19 -04001559 hints.status = 2
1560 break;
1561 hints.last_checked = index
Behdad Esfahbod2a70f4a2013-10-28 15:18:07 +01001562 if hints.status != 2:
1563 # Decide where to chop off from
1564 if subr_hints.status == 0:
1565 hints.last_hint = index
1566 else:
1567 hints.last_hint = index - 2 # Leave the subr call in
Behdad Esfahbode0622072013-09-10 14:33:19 -04001568
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001569@_add_method(ttLib.getTableClass('CFF '))
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001570def prune_post_subset(self, options):
1571 cff = self.cff
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001572 for fontname in cff.keys():
1573 font = cff[fontname]
1574 cs = font.CharStrings
1575
Behdad Esfahbode0622072013-09-10 14:33:19 -04001576
1577 #
Behdad Esfahbod3c20a132013-08-14 19:39:00 -04001578 # Drop unused FontDictionaries
Behdad Esfahbode0622072013-09-10 14:33:19 -04001579 #
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001580 if hasattr(font, "FDSelect"):
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001581 sel = font.FDSelect
1582 indices = _uniq_sort(sel.gidArray)
1583 sel.gidArray = [indices.index (ss) for ss in sel.gidArray]
1584 arr = font.FDArray
Behdad Esfahbod3c20a132013-08-14 19:39:00 -04001585 arr.items = [arr[i] for i in indices]
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001586 arr.count = len(arr.items)
1587 del arr.file, arr.offsets
Behdad Esfahbod1a4e72e2013-08-13 15:46:37 -04001588
Behdad Esfahbode0622072013-09-10 14:33:19 -04001589
1590 #
1591 # Drop hints if not needed
1592 #
1593 if not options.hinting:
1594
1595 #
1596 # This can be tricky, but doesn't have to. What we do is:
1597 #
1598 # - Run all used glyph charstrings and recurse into subroutines,
1599 # - For each charstring (including subroutines), if it has any
1600 # of the hint stem operators, we mark it as such. Upon returning,
1601 # for each charstring we note all the subroutine calls it makes
1602 # that (recursively) contain a stem,
1603 # - Dropping hinting then consists of the following two ops:
1604 # * Drop the piece of the program in each charstring before the
1605 # last call to a stem op or a stem-calling subroutine,
1606 # * Drop all hintmask operations.
1607 # - It's trickier... A hintmask right after hints and a few numbers
1608 # will act as an implicit vstemhm. As such, we track whether
1609 # we have seen any non-hint operators so far and do the right
1610 # thing, recursively... Good luck understanding that :(
1611 #
1612 css = set()
1613 for g in font.charset:
1614 c,sel = cs.getItemAndSelector(g)
1615 # Make sure it's decompiled. We want our "decompiler" to walk
1616 # the program, not the bytecode.
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001617 c.draw(basePen.NullPen())
Behdad Esfahbode0622072013-09-10 14:33:19 -04001618 subrs = getattr(c.private, "Subrs", [])
1619 decompiler = _DehintingT2Decompiler(css, subrs, c.globalSubrs)
1620 decompiler.execute(c)
1621 for charstring in css:
1622 charstring.drop_hints()
1623
Behdad Esfahbod16fc3232013-09-30 15:09:27 -04001624 # Drop font-wide hinting values
1625 all_privs = []
1626 if hasattr(font, 'FDSelect'):
1627 all_privs.extend(fd.Private for fd in font.FDArray)
1628 else:
1629 all_privs.append(font.Private)
1630 for priv in all_privs:
Behdad Esfahbod4d99d142013-10-28 13:15:08 +01001631 for k in ['BlueValues', 'OtherBlues', 'FamilyBlues', 'FamilyOtherBlues',
1632 'BlueScale', 'BlueShift', 'BlueFuzz',
1633 'StemSnapH', 'StemSnapV', 'StdHW', 'StdVW']:
Behdad Esfahbod16fc3232013-09-30 15:09:27 -04001634 if hasattr(priv, k):
1635 setattr(priv, k, None)
1636
Behdad Esfahbode0622072013-09-10 14:33:19 -04001637
1638 #
1639 # Renumber subroutines to remove unused ones
1640 #
1641
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001642 # Mark all used subroutines
1643 for g in font.charset:
1644 c,sel = cs.getItemAndSelector(g)
1645 subrs = getattr(c.private, "Subrs", [])
1646 decompiler = _MarkingT2Decompiler(subrs, c.globalSubrs)
1647 decompiler.execute(c)
1648
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001649 all_subrs = [font.GlobalSubrs]
Behdad Esfahbod83f1f5c2013-08-30 16:20:08 -04001650 if hasattr(font, 'FDSelect'):
Behdad Esfahbode0622072013-09-10 14:33:19 -04001651 all_subrs.extend(fd.Private.Subrs for fd in font.FDArray if hasattr(fd.Private, 'Subrs') and fd.Private.Subrs)
1652 elif hasattr(font.Private, 'Subrs') and font.Private.Subrs:
Behdad Esfahbodcbcaccf2013-08-30 16:21:38 -04001653 all_subrs.append(font.Private.Subrs)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001654
1655 subrs = set(subrs) # Remove duplicates
1656
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001657 # Prepare
1658 for subrs in all_subrs:
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001659 if not hasattr(subrs, '_used'):
1660 subrs._used = set()
1661 subrs._used = _uniq_sort(subrs._used)
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001662 subrs._old_bias = psCharStrings.calcSubrBias(subrs)
1663 subrs._new_bias = psCharStrings.calcSubrBias(subrs._used)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001664
Behdad Esfahboded107712013-08-14 19:54:13 -04001665 # Renumber glyph charstrings
1666 for g in font.charset:
1667 c,sel = cs.getItemAndSelector(g)
1668 subrs = getattr(c.private, "Subrs", [])
1669 c.subset_subroutines (subrs, font.GlobalSubrs)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001670
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001671 # Renumber subroutines themselves
1672 for subrs in all_subrs:
Behdad Esfahbode0622072013-09-10 14:33:19 -04001673
1674 if subrs == font.GlobalSubrs:
1675 if not hasattr(font, 'FDSelect') and hasattr(font.Private, 'Subrs'):
1676 local_subrs = font.Private.Subrs
Behdad Esfahbod83f1f5c2013-08-30 16:20:08 -04001677 else:
Behdad Esfahbode0622072013-09-10 14:33:19 -04001678 local_subrs = []
1679 else:
1680 local_subrs = subrs
1681
1682 subrs.items = [subrs.items[i] for i in subrs._used]
1683 subrs.count = len(subrs.items)
1684 del subrs.file
1685 if hasattr(subrs, 'offsets'):
1686 del subrs.offsets
1687
Behdad Esfahbodb466efe2013-11-27 03:34:35 -05001688 for i in range (subrs.count):
Behdad Esfahbod83f1f5c2013-08-30 16:20:08 -04001689 subrs[i].subset_subroutines (local_subrs, font.GlobalSubrs)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001690
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001691 # Cleanup
1692 for subrs in all_subrs:
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001693 del subrs._used, subrs._old_bias, subrs._new_bias
1694
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001695 return True
Behdad Esfahbod2b677c82013-07-23 13:37:13 -04001696
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001697@_add_method(ttLib.getTableClass('cmap'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001698def closure_glyphs(self, s):
Behdad Esfahbod2007a492014-03-12 12:26:41 -07001699 tables = [t for t in self.tables if t.isUnicode()]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001700 for u in s.unicodes_requested:
1701 found = False
1702 for table in tables:
1703 if u in table.cmap:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001704 s.glyphs.add(table.cmap[u])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001705 found = True
1706 break
1707 if not found:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001708 s.log("No glyph for Unicode value %s; skipping." % u)
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04001709
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001710@_add_method(ttLib.getTableClass('cmap'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001711def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001712 if not options.legacy_cmap:
1713 # Drop non-Unicode / non-Symbol cmaps
Behdad Esfahbod2007a492014-03-12 12:26:41 -07001714 self.tables = [t for t in self.tables if t.isUnicode() or t.isSymbol()]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001715 if not options.symbol_cmap:
Behdad Esfahbod2007a492014-03-12 12:26:41 -07001716 self.tables = [t for t in self.tables if not t.isSymbol()]
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001717 # TODO(behdad) Only keep one subtable?
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001718 # For now, drop format=0 which can't be subset_glyphs easily?
1719 self.tables = [t for t in self.tables if t.format != 0]
Behdad Esfahbodfd92d4c2013-09-19 19:43:09 -04001720 self.numSubTables = len(self.tables)
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001721 return True # Required table
Behdad Esfahbodabb50a12013-07-23 12:58:37 -04001722
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001723@_add_method(ttLib.getTableClass('cmap'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001724def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001725 s.glyphs = s.glyphs_cmaped
1726 for t in self.tables:
1727 # For reasons I don't understand I need this here
1728 # to force decompilation of the cmap format 14.
1729 try:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001730 getattr(t, "asdf")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001731 except AttributeError:
1732 pass
1733 if t.format == 14:
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001734 # TODO(behdad) XXX We drop all the default-UVS mappings(g==None).
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001735 t.uvsDict = dict((v,[(u,g) for u,g in l if g in s.glyphs])
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001736 for v,l in t.uvsDict.items())
1737 t.uvsDict = dict((v,l) for v,l in t.uvsDict.items() if l)
Behdad Esfahbod11580c52014-03-13 17:34:35 -07001738 elif t.isUnicode():
1739 t.cmap = dict((u,g) for u,g in t.cmap.items()
1740 if g in s.glyphs_requested or u in s.unicodes_requested)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001741 else:
Behdad Esfahbod11580c52014-03-13 17:34:35 -07001742 t.cmap = dict((u,g) for u,g in t.cmap.items()
1743 if g in s.glyphs_requested)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001744 self.tables = [t for t in self.tables
Behdad Esfahbod4734be52013-08-14 19:47:42 -04001745 if (t.cmap if t.format != 14 else t.uvsDict)]
Behdad Esfahbodfd92d4c2013-09-19 19:43:09 -04001746 self.numSubTables = len(self.tables)
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001747 # TODO(behdad) Convert formats when needed.
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001748 # In particular, if we have a format=12 without non-BMP
1749 # characters, either drop format=12 one or convert it
1750 # to format=4 if there's not one.
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001751 return True # Required table
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001752
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001753@_add_method(ttLib.getTableClass('name'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001754def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001755 if '*' not in options.name_IDs:
1756 self.names = [n for n in self.names if n.nameID in options.name_IDs]
1757 if not options.name_legacy:
Behdad Esfahboda08b1b12014-03-12 12:33:40 -07001758 self.names = [n for n in self.names if n.isUnicode()]
1759 # TODO(behdad) Option to keep only one platform's
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001760 if '*' not in options.name_languages:
Behdad Esfahboda08b1b12014-03-12 12:33:40 -07001761 # TODO(behdad) This is Windows-platform specific!
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001762 self.names = [n for n in self.names if n.langID in options.name_languages]
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001763 return True # Required table
Behdad Esfahbod653e9742013-07-22 15:17:12 -04001764
Behdad Esfahbod8c646f62013-07-22 15:06:23 -04001765
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001766# TODO(behdad) OS/2 ulUnicodeRange / ulCodePageRange?
Behdad Esfahbod26560d22013-10-26 22:03:35 +02001767# TODO(behdad) Drop AAT tables.
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001768# TODO(behdad) Drop unneeded GSUB/GPOS Script/LangSys entries.
Behdad Esfahbod852e8a52013-08-29 18:19:22 -04001769# TODO(behdad) Drop empty GSUB/GPOS, and GDEF if no GSUB/GPOS left
1770# TODO(behdad) Drop GDEF subitems if unused by lookups
Behdad Esfahbod10195332013-08-14 19:55:24 -04001771# TODO(behdad) Avoid recursing too much (in GSUB/GPOS and in CFF)
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001772# TODO(behdad) Text direction considerations.
1773# TODO(behdad) Text script / language considerations.
Behdad Esfahbodcc8fc782013-11-26 22:53:04 -05001774# TODO(behdad) Optionally drop 'kern' table if GPOS available
Behdad Esfahbodfa95e872013-12-15 22:02:20 -05001775# TODO(behdad) Implement --unicode='*' to choose all cmap'ed
1776# TODO(behdad) Drop old-spec Indic scripts
1777
Behdad Esfahbod8c486d82013-07-24 13:34:47 -04001778
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001779class Options(object):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001780
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001781 class UnknownOptionError(Exception):
1782 pass
Behdad Esfahbod26d9ee72013-08-13 16:55:01 -04001783
Behdad Esfahboda17743f2013-08-28 17:14:53 -04001784 _drop_tables_default = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', 'SVG ',
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001785 'PCLT', 'LTSH']
1786 _drop_tables_default += ['Feat', 'Glat', 'Gloc', 'Silf', 'Sill'] # Graphite
1787 _drop_tables_default += ['CBLC', 'CBDT', 'sbix', 'COLR', 'CPAL'] # Color
1788 _no_subset_tables_default = ['gasp', 'head', 'hhea', 'maxp', 'vhea', 'OS/2',
1789 'loca', 'name', 'cvt ', 'fpgm', 'prep']
1790 _hinting_tables_default = ['cvt ', 'fpgm', 'prep', 'hdmx', 'VDMX']
Behdad Esfahbod26d9ee72013-08-13 16:55:01 -04001791
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001792 # Based on HarfBuzz shapers
1793 _layout_features_groups = {
1794 # Default shaper
1795 'common': ['ccmp', 'liga', 'locl', 'mark', 'mkmk', 'rlig'],
1796 'horizontal': ['calt', 'clig', 'curs', 'kern', 'rclt'],
1797 'vertical': ['valt', 'vert', 'vkrn', 'vpal', 'vrt2'],
1798 'ltr': ['ltra', 'ltrm'],
1799 'rtl': ['rtla', 'rtlm'],
1800 # Complex shapers
1801 'arabic': ['init', 'medi', 'fina', 'isol', 'med2', 'fin2', 'fin3',
1802 'cswh', 'mset'],
1803 'hangul': ['ljmo', 'vjmo', 'tjmo'],
Behdad Esfahbod3977d3e2013-10-14 17:49:12 +02001804 'tibetan': ['abvs', 'blws', 'abvm', 'blwm'],
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001805 'indic': ['nukt', 'akhn', 'rphf', 'rkrf', 'pref', 'blwf', 'half',
1806 'abvf', 'pstf', 'cfar', 'vatu', 'cjct', 'init', 'pres',
1807 'abvs', 'blws', 'psts', 'haln', 'dist', 'abvm', 'blwm'],
1808 }
1809 _layout_features_default = _uniq_sort(sum(
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001810 iter(_layout_features_groups.values()), []))
Behdad Esfahbod9eeeb4e2013-08-13 16:58:50 -04001811
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001812 drop_tables = _drop_tables_default
1813 no_subset_tables = _no_subset_tables_default
1814 hinting_tables = _hinting_tables_default
1815 layout_features = _layout_features_default
Behdad Esfahbodfe6bc4c2013-11-02 11:10:23 +00001816 hinting = True
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001817 glyph_names = False
1818 legacy_cmap = False
1819 symbol_cmap = False
1820 name_IDs = [1, 2] # Family and Style
1821 name_legacy = False
1822 name_languages = [0x0409] # English
Behdad Esfahbod04f3a192013-08-29 16:56:06 -04001823 notdef_glyph = True # gid0 for TrueType / .notdef for CFF
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001824 notdef_outline = False # No need for notdef to have an outline really
Behdad Esfahbod04f3a192013-08-29 16:56:06 -04001825 recommended_glyphs = False # gid1, gid2, gid3 for TrueType
Behdad Esfahbode911de12013-08-16 12:42:34 -04001826 recalc_bounds = False # Recalculate font bounding boxes
Behdad Esfahbodf09164a2014-05-01 15:16:14 -07001827 recalc_timestamp = False # Recalculate font modified timestamp
Behdad Esfahbod03d78da2013-08-29 16:42:00 -04001828 canonical_order = False # Order tables as recommended
Behdad Esfahbodc6c3bb82013-08-15 17:46:20 -04001829 flavor = None # May be 'woff'
Behdad Esfahbod9eeeb4e2013-08-13 16:58:50 -04001830
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001831 def __init__(self, **kwargs):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001832
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001833 self.set(**kwargs)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001834
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001835 def set(self, **kwargs):
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001836 for k,v in kwargs.items():
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001837 if not hasattr(self, k):
Behdad Esfahbodac10d812013-09-03 18:29:58 -04001838 raise self.UnknownOptionError("Unknown option '%s'" % k)
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001839 setattr(self, k, v)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001840
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001841 def parse_opts(self, argv, ignore_unknown=False):
1842 ret = []
1843 opts = {}
1844 for a in argv:
1845 orig_a = a
1846 if not a.startswith('--'):
1847 ret.append(a)
1848 continue
1849 a = a[2:]
1850 i = a.find('=')
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001851 op = '='
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001852 if i == -1:
1853 if a.startswith("no-"):
1854 k = a[3:]
1855 v = False
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001856 else:
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001857 k = a
1858 v = True
1859 else:
1860 k = a[:i]
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001861 if k[-1] in "-+":
1862 op = k[-1]+'=' # Ops is '-=' or '+=' now.
1863 k = k[:-1]
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001864 v = a[i+1:]
1865 k = k.replace('-', '_')
1866 if not hasattr(self, k):
Behdad Esfahbod9e6ef942013-12-04 16:31:44 -05001867 if ignore_unknown is True or k in ignore_unknown:
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001868 ret.append(orig_a)
1869 continue
1870 else:
1871 raise self.UnknownOptionError("Unknown option '%s'" % a)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001872
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001873 ov = getattr(self, k)
1874 if isinstance(ov, bool):
1875 v = bool(v)
1876 elif isinstance(ov, int):
1877 v = int(v)
1878 elif isinstance(ov, list):
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001879 vv = v.split(',')
1880 if vv == ['']:
1881 vv = []
Behdad Esfahbod87c8c502013-08-16 14:44:09 -04001882 vv = [int(x, 0) if len(x) and x[0] in "0123456789" else x for x in vv]
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001883 if op == '=':
1884 v = vv
1885 elif op == '+=':
1886 v = ov
1887 v.extend(vv)
1888 elif op == '-=':
1889 v = ov
1890 for x in vv:
1891 if x in v:
1892 v.remove(x)
1893 else:
Behdad Esfahbod153ec402013-12-04 01:15:46 -05001894 assert False
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001895
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001896 opts[k] = v
1897 self.set(**opts)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001898
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001899 return ret
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001900
1901
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001902class Subsetter(object):
1903
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001904 def __init__(self, options=None, log=None):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001905
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001906 if not log:
1907 log = Logger()
1908 if not options:
1909 options = Options()
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001910
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001911 self.options = options
1912 self.log = log
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001913 self.unicodes_requested = set()
1914 self.glyphs_requested = set()
1915 self.glyphs = set()
Behdad Esfahbod3d513b72013-07-31 14:11:40 -04001916
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001917 def populate(self, glyphs=[], unicodes=[], text=""):
1918 self.unicodes_requested.update(unicodes)
Behdad Esfahbodb21c9d32013-11-27 18:09:08 -05001919 if isinstance(text, bytes):
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001920 text = text.decode("utf8")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001921 for u in text:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001922 self.unicodes_requested.add(ord(u))
1923 self.glyphs_requested.update(glyphs)
1924 self.glyphs.update(glyphs)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -04001925
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04001926 def _prune_pre_subset(self, font):
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001927
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001928 for tag in font.keys():
1929 if tag == 'GlyphOrder': continue
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001930
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001931 if(tag in self.options.drop_tables or
1932 (tag in self.options.hinting_tables and not self.options.hinting)):
1933 self.log(tag, "dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001934 del font[tag]
1935 continue
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001936
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001937 clazz = ttLib.getTableClass(tag)
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001938
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001939 if hasattr(clazz, 'prune_pre_subset'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001940 table = font[tag]
Behdad Esfahbod010c5f92013-09-10 20:54:46 -04001941 self.log.lapse("load '%s'" % tag)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001942 retain = table.prune_pre_subset(self.options)
1943 self.log.lapse("prune '%s'" % tag)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001944 if not retain:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001945 self.log(tag, "pruned to empty; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001946 del font[tag]
1947 continue
1948 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001949 self.log(tag, "pruned")
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001950
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04001951 def _closure_glyphs(self, font):
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001952
Behdad Esfahbod05a28622013-12-04 23:05:59 -05001953 realGlyphs = set(font.getGlyphOrder())
1954
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001955 self.glyphs = self.glyphs_requested.copy()
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001956
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001957 if 'cmap' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001958 font['cmap'].closure_glyphs(self)
Behdad Esfahbod05a28622013-12-04 23:05:59 -05001959 self.glyphs.intersection_update(realGlyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001960 self.glyphs_cmaped = self.glyphs
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001961
Behdad Esfahbod04f3a192013-08-29 16:56:06 -04001962 if self.options.notdef_glyph:
1963 if 'glyf' in font:
1964 self.glyphs.add(font.getGlyphName(0))
1965 self.log("Added gid0 to subset")
1966 else:
1967 self.glyphs.add('.notdef')
1968 self.log("Added .notdef to subset")
1969 if self.options.recommended_glyphs:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001970 if 'glyf' in font:
Behdad Esfahbod10a3fff2013-12-08 15:22:32 -05001971 for i in range(min(4, len(font.getGlyphOrder()))):
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001972 self.glyphs.add(font.getGlyphName(i))
1973 self.log("Added first four glyphs to subset")
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001974
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001975 if 'GSUB' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001976 self.log("Closing glyph list over 'GSUB': %d glyphs before" %
1977 len(self.glyphs))
1978 self.log.glyphs(self.glyphs, font=font)
1979 font['GSUB'].closure_glyphs(self)
Behdad Esfahbod05a28622013-12-04 23:05:59 -05001980 self.glyphs.intersection_update(realGlyphs)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001981 self.log("Closed glyph list over 'GSUB': %d glyphs after" %
1982 len(self.glyphs))
1983 self.log.glyphs(self.glyphs, font=font)
1984 self.log.lapse("close glyph list over 'GSUB'")
1985 self.glyphs_gsubed = self.glyphs.copy()
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001986
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001987 if 'glyf' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001988 self.log("Closing glyph list over 'glyf': %d glyphs before" %
1989 len(self.glyphs))
1990 self.log.glyphs(self.glyphs, font=font)
1991 font['glyf'].closure_glyphs(self)
Behdad Esfahbod05a28622013-12-04 23:05:59 -05001992 self.glyphs.intersection_update(realGlyphs)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001993 self.log("Closed glyph list over 'glyf': %d glyphs after" %
1994 len(self.glyphs))
1995 self.log.glyphs(self.glyphs, font=font)
1996 self.log.lapse("close glyph list over 'glyf'")
1997 self.glyphs_glyfed = self.glyphs.copy()
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001998
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001999 self.glyphs_all = self.glyphs.copy()
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04002000
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002001 self.log("Retaining %d glyphs: " % len(self.glyphs_all))
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04002002
Behdad Esfahbodebcad972013-12-04 23:00:52 -05002003 del self.glyphs
2004
2005
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04002006 def _subset_glyphs(self, font):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002007 for tag in font.keys():
2008 if tag == 'GlyphOrder': continue
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04002009 clazz = ttLib.getTableClass(tag)
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04002010
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002011 if tag in self.options.no_subset_tables:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002012 self.log(tag, "subsetting not needed")
2013 elif hasattr(clazz, 'subset_glyphs'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002014 table = font[tag]
2015 self.glyphs = self.glyphs_all
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002016 retain = table.subset_glyphs(self)
Behdad Esfahbodebcad972013-12-04 23:00:52 -05002017 del self.glyphs
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002018 self.log.lapse("subset '%s'" % tag)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002019 if not retain:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002020 self.log(tag, "subsetted to empty; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002021 del font[tag]
2022 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002023 self.log(tag, "subsetted")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002024 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002025 self.log(tag, "NOT subset; don't know how to subset; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002026 del font[tag]
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04002027
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002028 glyphOrder = font.getGlyphOrder()
2029 glyphOrder = [g for g in glyphOrder if g in self.glyphs_all]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002030 font.setGlyphOrder(glyphOrder)
2031 font._buildReverseGlyphOrderDict()
2032 self.log.lapse("subset GlyphOrder")
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04002033
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04002034 def _prune_post_subset(self, font):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002035 for tag in font.keys():
2036 if tag == 'GlyphOrder': continue
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04002037 clazz = ttLib.getTableClass(tag)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002038 if hasattr(clazz, 'prune_post_subset'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002039 table = font[tag]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002040 retain = table.prune_post_subset(self.options)
2041 self.log.lapse("prune '%s'" % tag)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002042 if not retain:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002043 self.log(tag, "pruned to empty; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002044 del font[tag]
2045 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002046 self.log(tag, "pruned")
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04002047
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002048 def subset(self, font):
Behdad Esfahbod756af492013-08-01 12:05:26 -04002049
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04002050 self._prune_pre_subset(font)
2051 self._closure_glyphs(font)
2052 self._subset_glyphs(font)
2053 self._prune_post_subset(font)
Behdad Esfahbod98259f22013-07-31 20:16:24 -04002054
Behdad Esfahbod756af492013-08-01 12:05:26 -04002055
Behdad Esfahbod3d4c4712013-08-13 20:10:17 -04002056class Logger(object):
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04002057
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002058 def __init__(self, verbose=False, xml=False, timing=False):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002059 self.verbose = verbose
2060 self.xml = xml
2061 self.timing = timing
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002062 self.last_time = self.start_time = time.time()
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04002063
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002064 def parse_opts(self, argv):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002065 argv = argv[:]
2066 for v in ['verbose', 'xml', 'timing']:
2067 if "--"+v in argv:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002068 setattr(self, v, True)
2069 argv.remove("--"+v)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002070 return argv
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04002071
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002072 def __call__(self, *things):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002073 if not self.verbose:
2074 return
Behdad Esfahbod4cd467c2013-11-27 04:57:06 -05002075 print(' '.join(str(x) for x in things))
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04002076
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002077 def lapse(self, *things):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002078 if not self.timing:
2079 return
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002080 new_time = time.time()
Behdad Esfahbod4cd467c2013-11-27 04:57:06 -05002081 print("Took %0.3fs to %s" %(new_time - self.last_time,
2082 ' '.join(str(x) for x in things)))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002083 self.last_time = new_time
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04002084
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04002085 def glyphs(self, glyphs, font=None):
Behdad Esfahbod57fb7262013-12-04 21:56:53 -05002086 if not self.verbose:
2087 return
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002088 self("Names: ", sorted(glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002089 if font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002090 reverseGlyphMap = font.getReverseGlyphMap()
2091 self("Gids : ", sorted(reverseGlyphMap[g] for g in glyphs))
Behdad Esfahbodf5497842013-08-08 21:57:02 -04002092
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002093 def font(self, font, file=sys.stdout):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002094 if not self.xml:
2095 return
Behdad Esfahbod28fc4982013-09-18 19:01:16 -04002096 from fontTools.misc import xmlWriter
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002097 writer = xmlWriter.XMLWriter(file)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002098 for tag in font.keys():
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002099 writer.begintag(tag)
2100 writer.newline()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002101 font[tag].toXML(writer, font)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002102 writer.endtag(tag)
2103 writer.newline()
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04002104
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002105
Behdad Esfahbod85da2682013-08-15 12:17:21 -04002106def load_font(fontFile,
Behdad Esfahbodadc47fd2013-08-15 18:29:25 -04002107 options,
Behdad Esfahbod6bd43242013-12-04 21:34:05 -05002108 allowVID=False,
Behdad Esfahbod85da2682013-08-15 12:17:21 -04002109 checkChecksums=False,
Behdad Esfahbod283fb262013-12-16 00:50:48 -05002110 dontLoadGlyphNames=False,
2111 lazy=True):
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002112
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04002113 font = ttLib.TTFont(fontFile,
Behdad Esfahbod6bd43242013-12-04 21:34:05 -05002114 allowVID=allowVID,
2115 checkChecksums=checkChecksums,
Behdad Esfahbod283fb262013-12-16 00:50:48 -05002116 recalcBBoxes=options.recalc_bounds,
Behdad Esfahbodf09164a2014-05-01 15:16:14 -07002117 recalcTimestamp=options.recalc_timestamp,
Behdad Esfahbod283fb262013-12-16 00:50:48 -05002118 lazy=lazy)
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002119
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002120 # Hack:
2121 #
2122 # If we don't need glyph names, change 'post' class to not try to
2123 # load them. It avoid lots of headache with broken fonts as well
2124 # as loading time.
2125 #
2126 # Ideally ttLib should provide a way to ask it to skip loading
2127 # glyph names. But it currently doesn't provide such a thing.
2128 #
Behdad Esfahbod85da2682013-08-15 12:17:21 -04002129 if dontLoadGlyphNames:
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04002130 post = ttLib.getTableClass('post')
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002131 saved = post.decode_format_2_0
2132 post.decode_format_2_0 = post.decode_format_3_0
2133 f = font['post']
2134 if f.formatType == 2.0:
2135 f.formatType = 3.0
2136 post.decode_format_2_0 = saved
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002137
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002138 return font
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002139
Behdad Esfahbode911de12013-08-16 12:42:34 -04002140def save_font(font, outfile, options):
Behdad Esfahbodc6c3bb82013-08-15 17:46:20 -04002141 if options.flavor and not hasattr(font, 'flavor'):
2142 raise Exception("fonttools version does not support flavors.")
2143 font.flavor = options.flavor
Behdad Esfahbode911de12013-08-16 12:42:34 -04002144 font.save(outfile, reorderTables=options.canonical_order)
Behdad Esfahbod41de4cc2013-08-15 12:09:55 -04002145
Behdad Esfahbodb69400f2013-08-29 18:40:53 -04002146def main(args):
Behdad Esfahbod610b0552013-07-23 14:52:18 -04002147
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002148 log = Logger()
2149 args = log.parse_opts(args)
Behdad Esfahbod4ae81712013-07-22 11:57:13 -04002150
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04002151 options = Options()
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002152 args = options.parse_opts(args, ignore_unknown=['text'])
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04002153
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002154 if len(args) < 2:
Behdad Esfahbodcfeafd72013-11-27 17:27:35 -05002155 print("usage: pyftsubset font-file glyph... [--text=ABC]... [--option=value]...", file=sys.stderr)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002156 sys.exit(1)
Behdad Esfahbod02b92062013-07-21 18:40:59 -04002157
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002158 fontfile = args[0]
2159 args = args[1:]
Behdad Esfahbod02b92062013-07-21 18:40:59 -04002160
Behdad Esfahbod85da2682013-08-15 12:17:21 -04002161 dontLoadGlyphNames =(not options.glyph_names and
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002162 all(any(g.startswith(p)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002163 for p in ['gid', 'glyph', 'uni', 'U+'])
2164 for g in args))
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002165
Behdad Esfahbodadc47fd2013-08-15 18:29:25 -04002166 font = load_font(fontfile, options, dontLoadGlyphNames=dontLoadGlyphNames)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002167 log.lapse("load font")
Behdad Esfahbodb640f742013-09-19 20:12:56 -04002168 subsetter = Subsetter(options=options, log=log)
Behdad Esfahbod02b92062013-07-21 18:40:59 -04002169
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002170 names = font.getGlyphNames()
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002171 log.lapse("loading glyph names")
Behdad Esfahbode7f5a892013-07-31 19:58:59 -04002172
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002173 glyphs = []
2174 unicodes = []
2175 text = ""
2176 for g in args:
Behdad Esfahbod2be33d92013-09-10 19:28:59 -04002177 if g == '*':
2178 glyphs.extend(font.getGlyphOrder())
2179 continue
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002180 if g in names:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002181 glyphs.append(g)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002182 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002183 if g.startswith('--text='):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002184 text += g[7:]
2185 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002186 if g.startswith('uni') or g.startswith('U+'):
2187 if g.startswith('uni') and len(g) > 3:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002188 g = g[3:]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002189 elif g.startswith('U+') and len(g) > 2:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002190 g = g[2:]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002191 u = int(g, 16)
2192 unicodes.append(u)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002193 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002194 if g.startswith('gid') or g.startswith('glyph'):
2195 if g.startswith('gid') and len(g) > 3:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002196 g = g[3:]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002197 elif g.startswith('glyph') and len(g) > 5:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002198 g = g[5:]
2199 try:
Behdad Esfahboddc873722013-12-04 21:28:50 -05002200 glyphs.append(font.getGlyphName(int(g), requireReal=True))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002201 except ValueError:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002202 raise Exception("Invalid glyph identifier: %s" % g)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002203 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002204 raise Exception("Invalid glyph identifier: %s" % g)
2205 log.lapse("compile glyph list")
2206 log("Unicodes:", unicodes)
2207 log("Glyphs:", glyphs)
Behdad Esfahbod6df089a2013-07-31 19:27:14 -04002208
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002209 subsetter.populate(glyphs=glyphs, unicodes=unicodes, text=text)
2210 subsetter.subset(font)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -04002211
Behdad Esfahbod34426c12013-08-14 18:30:09 -04002212 outfile = fontfile + '.subset'
2213
Behdad Esfahbodc6c3bb82013-08-15 17:46:20 -04002214 save_font (font, outfile, options)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002215 log.lapse("compile and save font")
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04002216
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002217 log.last_time = log.start_time
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002218 log.lapse("make one with everything(TOTAL TIME)")
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04002219
Behdad Esfahbod34426c12013-08-14 18:30:09 -04002220 if log.verbose:
2221 import os
2222 log("Input font: %d bytes" % os.path.getsize(fontfile))
2223 log("Subset font: %d bytes" % os.path.getsize(outfile))
2224
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002225 log.font(font)
Behdad Esfahbod8c486d82013-07-24 13:34:47 -04002226
Behdad Esfahbodc56bf482013-08-13 20:13:33 -04002227 font.close()
2228
Behdad Esfahbod39a39ac2013-08-22 18:10:17 -04002229
2230__all__ = [
Behdad Esfahbodb69400f2013-08-29 18:40:53 -04002231 'Options',
2232 'Subsetter',
2233 'Logger',
2234 'load_font',
2235 'save_font',
2236 'main'
Behdad Esfahbod39a39ac2013-08-22 18:10:17 -04002237]
2238
Behdad Esfahbod8c486d82013-07-24 13:34:47 -04002239if __name__ == '__main__':
Behdad Esfahbodb69400f2013-08-29 18:40:53 -04002240 main(sys.argv[1:])