blob: ce6cf4080ebd2f2a4dafef290f8e018f8ae4f925 [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 Esfahboda030a0d2013-11-27 17:46:15 -050010from __future__ import print_function, division
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 Esfahbod9e856ea2013-08-13 19:50:38 -0400103 if cur_glyphs == None: cur_glyphs = s.glyphs
104 if self.Format in [1, 2]:
Behdad Esfahbod6890d052013-11-27 06:26:35 -0500105 s.glyphs.update(v for g,v in self.mapping.items() if g in cur_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400106 else:
107 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400108
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400109@_add_method(otTables.SingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400110def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400111 if self.Format in [1, 2]:
Behdad Esfahbod6890d052013-11-27 06:26:35 -0500112 self.mapping = dict((g,v) for g,v in self.mapping.items()
Behdad Esfahbodd73f2252013-08-16 10:58:25 -0400113 if g in s.glyphs and v in s.glyphs)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400114 return bool(self.mapping)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400115 else:
116 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400117
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400118@_add_method(otTables.MultipleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400119def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400120 if cur_glyphs == None: cur_glyphs = s.glyphs
121 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400122 indices = self.Coverage.intersect(cur_glyphs)
Behdad Esfahboda9bfec12013-08-16 16:21:25 -0400123 _set_update(s.glyphs, *(self.Sequence[i].Substitute for i in indices))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400124 else:
125 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400126
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400127@_add_method(otTables.MultipleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400128def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400129 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400130 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400131 self.Sequence = [self.Sequence[i] for i in indices]
132 # Now drop rules generating glyphs we don't want
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400133 indices = [i for i,seq in enumerate(self.Sequence)
134 if all(sub in s.glyphs for sub in seq.Substitute)]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400135 self.Sequence = [self.Sequence[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400136 self.Coverage.remap(indices)
137 self.SequenceCount = len(self.Sequence)
138 return bool(self.SequenceCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400139 else:
140 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400141
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400142@_add_method(otTables.AlternateSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400143def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400144 if cur_glyphs == None: cur_glyphs = s.glyphs
145 if self.Format == 1:
Behdad Esfahbod6890d052013-11-27 06:26:35 -0500146 _set_update(s.glyphs, *(vlist for g,vlist in self.alternates.items()
Behdad Esfahbod42d4f2b2013-08-16 16:16:22 -0400147 if g in cur_glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400148 else:
149 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400150
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400151@_add_method(otTables.AlternateSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400152def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400153 if self.Format == 1:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -0400154 self.alternates = dict((g,vlist)
Behdad Esfahbod6890d052013-11-27 06:26:35 -0500155 for g,vlist in self.alternates.items()
Behdad Esfahbodd73f2252013-08-16 10:58:25 -0400156 if g in s.glyphs and
157 all(v in s.glyphs for v in vlist))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400158 return bool(self.alternates)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400159 else:
160 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400161
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400162@_add_method(otTables.LigatureSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400163def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400164 if cur_glyphs == None: cur_glyphs = s.glyphs
165 if self.Format == 1:
Behdad Esfahbod42d4f2b2013-08-16 16:16:22 -0400166 _set_update(s.glyphs, *([seq.LigGlyph for seq in seqs
167 if all(c in s.glyphs for c in seq.Component)]
Behdad Esfahbod6890d052013-11-27 06:26:35 -0500168 for g,seqs in self.ligatures.items()
Behdad Esfahbod42d4f2b2013-08-16 16:16:22 -0400169 if g in cur_glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400170 else:
171 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400172
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400173@_add_method(otTables.LigatureSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400174def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400175 if self.Format == 1:
Behdad Esfahbod6890d052013-11-27 06:26:35 -0500176 self.ligatures = dict((g,v) for g,v in self.ligatures.items()
Behdad Esfahbodd73f2252013-08-16 10:58:25 -0400177 if g in s.glyphs)
178 self.ligatures = dict((g,[seq for seq in seqs
179 if seq.LigGlyph in s.glyphs and
180 all(c in s.glyphs for c in seq.Component)])
Behdad Esfahbod6890d052013-11-27 06:26:35 -0500181 for g,seqs in self.ligatures.items())
182 self.ligatures = dict((g,v) for g,v in self.ligatures.items() if v)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400183 return bool(self.ligatures)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400184 else:
185 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400186
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400187@_add_method(otTables.ReverseChainSingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400188def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400189 if cur_glyphs == None: cur_glyphs = s.glyphs
190 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400191 indices = self.Coverage.intersect(cur_glyphs)
192 if(not indices or
193 not all(c.intersect(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400194 for c in self.LookAheadCoverage + self.BacktrackCoverage)):
195 return
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400196 s.glyphs.update(self.Substitute[i] for i in indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400197 else:
198 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400199
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400200@_add_method(otTables.ReverseChainSingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400201def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400202 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400203 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400204 self.Substitute = [self.Substitute[i] for i in indices]
205 # Now drop rules generating glyphs we don't want
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400206 indices = [i for i,sub in enumerate(self.Substitute)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400207 if sub in s.glyphs]
208 self.Substitute = [self.Substitute[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400209 self.Coverage.remap(indices)
210 self.GlyphCount = len(self.Substitute)
211 return bool(self.GlyphCount and
212 all(c.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400213 for c in self.LookAheadCoverage+self.BacktrackCoverage))
214 else:
215 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400216
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400217@_add_method(otTables.SinglePos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400218def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400219 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400220 return len(self.Coverage.subset(s.glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400221 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400222 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400223 self.Value = [self.Value[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400224 self.ValueCount = len(self.Value)
225 return bool(self.ValueCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400226 else:
227 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400228
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400229@_add_method(otTables.SinglePos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400230def prune_post_subset(self, options):
231 if not options.hinting:
232 # Drop device tables
233 self.ValueFormat &= ~0x00F0
234 return True
235
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400236@_add_method(otTables.PairPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400237def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400238 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400239 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400240 self.PairSet = [self.PairSet[i] for i in indices]
241 for p in self.PairSet:
242 p.PairValueRecord = [r for r in p.PairValueRecord
243 if r.SecondGlyph in s.glyphs]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400244 p.PairValueCount = len(p.PairValueRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400245 self.PairSet = [p for p in self.PairSet if p.PairValueCount]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400246 self.PairSetCount = len(self.PairSet)
247 return bool(self.PairSetCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400248 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400249 class1_map = self.ClassDef1.subset(s.glyphs, remap=True)
250 class2_map = self.ClassDef2.subset(s.glyphs, remap=True)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400251 self.Class1Record = [self.Class1Record[i] for i in class1_map]
252 for c in self.Class1Record:
253 c.Class2Record = [c.Class2Record[i] for i in class2_map]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400254 self.Class1Count = len(class1_map)
255 self.Class2Count = len(class2_map)
256 return bool(self.Class1Count and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400257 self.Class2Count and
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400258 self.Coverage.subset(s.glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400259 else:
260 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400261
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400262@_add_method(otTables.PairPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400263def prune_post_subset(self, options):
264 if not options.hinting:
265 # Drop device tables
266 self.ValueFormat1 &= ~0x00F0
267 self.ValueFormat2 &= ~0x00F0
268 return True
269
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400270@_add_method(otTables.CursivePos)
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 indices = self.Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400274 self.EntryExitRecord = [self.EntryExitRecord[i] for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400275 self.EntryExitCount = len(self.EntryExitRecord)
276 return bool(self.EntryExitCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400277 else:
278 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400279
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400280@_add_method(otTables.Anchor)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400281def prune_hints(self):
282 # Drop device tables / contour anchor point
283 self.Format = 1
284
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400285@_add_method(otTables.CursivePos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400286def prune_post_subset(self, options):
287 if not options.hinting:
288 for rec in self.EntryExitRecord:
289 if rec.EntryAnchor: rec.EntryAnchor.prune_hints()
290 if rec.ExitAnchor: rec.ExitAnchor.prune_hints()
291 return True
292
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400293@_add_method(otTables.MarkBasePos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400294def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400295 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400296 mark_indices = self.MarkCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400297 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i]
298 for i in mark_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400299 self.MarkArray.MarkCount = len(self.MarkArray.MarkRecord)
300 base_indices = self.BaseCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400301 self.BaseArray.BaseRecord = [self.BaseArray.BaseRecord[i]
302 for i in base_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400303 self.BaseArray.BaseCount = len(self.BaseArray.BaseRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400304 # Prune empty classes
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400305 class_indices = _uniq_sort(v.Class for v in self.MarkArray.MarkRecord)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400306 self.ClassCount = len(class_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400307 for m in self.MarkArray.MarkRecord:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400308 m.Class = class_indices.index(m.Class)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400309 for b in self.BaseArray.BaseRecord:
310 b.BaseAnchor = [b.BaseAnchor[i] for i in class_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400311 return bool(self.ClassCount and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400312 self.MarkArray.MarkCount and
313 self.BaseArray.BaseCount)
314 else:
315 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400316
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400317@_add_method(otTables.MarkBasePos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400318def prune_post_subset(self, options):
319 if not options.hinting:
320 for m in self.MarkArray.MarkRecord:
Behdad Esfahbode1a010c2013-10-09 15:57:22 +0200321 if m.MarkAnchor:
322 m.MarkAnchor.prune_hints()
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400323 for b in self.BaseArray.BaseRecord:
324 for a in b.BaseAnchor:
Behdad Esfahbode1a010c2013-10-09 15:57:22 +0200325 if a:
326 a.prune_hints()
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400327 return True
328
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400329@_add_method(otTables.MarkLigPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400330def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400331 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400332 mark_indices = self.MarkCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400333 self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i]
334 for i in mark_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400335 self.MarkArray.MarkCount = len(self.MarkArray.MarkRecord)
336 ligature_indices = self.LigatureCoverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400337 self.LigatureArray.LigatureAttach = [self.LigatureArray.LigatureAttach[i]
338 for i in ligature_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400339 self.LigatureArray.LigatureCount = len(self.LigatureArray.LigatureAttach)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400340 # Prune empty classes
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400341 class_indices = _uniq_sort(v.Class for v in self.MarkArray.MarkRecord)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400342 self.ClassCount = len(class_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400343 for m in self.MarkArray.MarkRecord:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400344 m.Class = class_indices.index(m.Class)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400345 for l in self.LigatureArray.LigatureAttach:
346 for c in l.ComponentRecord:
347 c.LigatureAnchor = [c.LigatureAnchor[i] for i in class_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400348 return bool(self.ClassCount and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400349 self.MarkArray.MarkCount and
350 self.LigatureArray.LigatureCount)
351 else:
352 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400353
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400354@_add_method(otTables.MarkLigPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400355def prune_post_subset(self, options):
356 if not options.hinting:
357 for m in self.MarkArray.MarkRecord:
Behdad Esfahbode1a010c2013-10-09 15:57:22 +0200358 if m.MarkAnchor:
359 m.MarkAnchor.prune_hints()
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400360 for l in self.LigatureArray.LigatureAttach:
361 for c in l.ComponentRecord:
362 for a in c.LigatureAnchor:
Behdad Esfahbode1a010c2013-10-09 15:57:22 +0200363 if a:
364 a.prune_hints()
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400365 return True
366
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400367@_add_method(otTables.MarkMarkPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400368def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400369 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400370 mark1_indices = self.Mark1Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400371 self.Mark1Array.MarkRecord = [self.Mark1Array.MarkRecord[i]
372 for i in mark1_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400373 self.Mark1Array.MarkCount = len(self.Mark1Array.MarkRecord)
374 mark2_indices = self.Mark2Coverage.subset(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400375 self.Mark2Array.Mark2Record = [self.Mark2Array.Mark2Record[i]
376 for i in mark2_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400377 self.Mark2Array.MarkCount = len(self.Mark2Array.Mark2Record)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400378 # Prune empty classes
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400379 class_indices = _uniq_sort(v.Class for v in self.Mark1Array.MarkRecord)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400380 self.ClassCount = len(class_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400381 for m in self.Mark1Array.MarkRecord:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400382 m.Class = class_indices.index(m.Class)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400383 for b in self.Mark2Array.Mark2Record:
384 b.Mark2Anchor = [b.Mark2Anchor[i] for i in class_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400385 return bool(self.ClassCount and
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400386 self.Mark1Array.MarkCount and
387 self.Mark2Array.MarkCount)
388 else:
389 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400390
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400391@_add_method(otTables.MarkMarkPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400392def prune_post_subset(self, options):
393 if not options.hinting:
394 # Drop device tables or contour anchor point
395 for m in self.Mark1Array.MarkRecord:
Behdad Esfahbode1a010c2013-10-09 15:57:22 +0200396 if m.MarkAnchor:
397 m.MarkAnchor.prune_hints()
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400398 for b in self.Mark2Array.Mark2Record:
Behdad Esfahbod0ec17d92013-09-15 18:30:41 -0400399 for m in b.Mark2Anchor:
Behdad Esfahbode1a010c2013-10-09 15:57:22 +0200400 if m:
401 m.prune_hints()
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400402 return True
403
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400404@_add_method(otTables.SingleSubst,
405 otTables.MultipleSubst,
406 otTables.AlternateSubst,
407 otTables.LigatureSubst,
408 otTables.ReverseChainSingleSubst,
409 otTables.SinglePos,
410 otTables.PairPos,
411 otTables.CursivePos,
412 otTables.MarkBasePos,
413 otTables.MarkLigPos,
414 otTables.MarkMarkPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400415def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400416 pass
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400417
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400418@_add_method(otTables.SingleSubst,
419 otTables.MultipleSubst,
420 otTables.AlternateSubst,
421 otTables.LigatureSubst,
422 otTables.ReverseChainSingleSubst,
423 otTables.SinglePos,
424 otTables.PairPos,
425 otTables.CursivePos,
426 otTables.MarkBasePos,
427 otTables.MarkLigPos,
428 otTables.MarkMarkPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400429def collect_lookups(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400430 return []
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400431
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400432@_add_method(otTables.SingleSubst,
433 otTables.MultipleSubst,
434 otTables.AlternateSubst,
435 otTables.LigatureSubst,
436 otTables.ContextSubst,
437 otTables.ChainContextSubst,
438 otTables.ReverseChainSingleSubst,
439 otTables.SinglePos,
440 otTables.PairPos,
441 otTables.CursivePos,
442 otTables.MarkBasePos,
443 otTables.MarkLigPos,
444 otTables.MarkMarkPos,
445 otTables.ContextPos,
446 otTables.ChainContextPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400447def prune_pre_subset(self, options):
448 return True
449
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400450@_add_method(otTables.SingleSubst,
451 otTables.MultipleSubst,
452 otTables.AlternateSubst,
453 otTables.LigatureSubst,
454 otTables.ReverseChainSingleSubst,
455 otTables.ContextSubst,
456 otTables.ChainContextSubst,
457 otTables.ContextPos,
458 otTables.ChainContextPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400459def prune_post_subset(self, options):
460 return True
461
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400462@_add_method(otTables.SingleSubst,
463 otTables.AlternateSubst,
464 otTables.ReverseChainSingleSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400465def may_have_non_1to1(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400466 return False
Behdad Esfahbodaeacc152013-08-12 20:24:33 -0400467
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400468@_add_method(otTables.MultipleSubst,
469 otTables.LigatureSubst,
470 otTables.ContextSubst,
471 otTables.ChainContextSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400472def may_have_non_1to1(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400473 return True
Behdad Esfahbodaeacc152013-08-12 20:24:33 -0400474
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400475@_add_method(otTables.ContextSubst,
476 otTables.ChainContextSubst,
477 otTables.ContextPos,
478 otTables.ChainContextPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400479def __classify_context(self):
Behdad Esfahbodb178dca2013-07-23 22:51:50 -0400480
Behdad Esfahbod3d4c4712013-08-13 20:10:17 -0400481 class ContextHelper(object):
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400482 def __init__(self, klass, Format):
483 if klass.__name__.endswith('Subst'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400484 Typ = 'Sub'
485 Type = 'Subst'
486 else:
487 Typ = 'Pos'
488 Type = 'Pos'
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400489 if klass.__name__.startswith('Chain'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400490 Chain = 'Chain'
491 else:
492 Chain = ''
493 ChainTyp = Chain+Typ
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400494
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400495 self.Typ = Typ
496 self.Type = Type
497 self.Chain = Chain
498 self.ChainTyp = ChainTyp
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400499
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400500 self.LookupRecord = Type+'LookupRecord'
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400501
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400502 if Format == 1:
503 Coverage = lambda r: r.Coverage
504 ChainCoverage = lambda r: r.Coverage
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400505 ContextData = lambda r:(None,)
506 ChainContextData = lambda r:(None, None, None)
507 RuleData = lambda r:(r.Input,)
508 ChainRuleData = lambda r:(r.Backtrack, r.Input, r.LookAhead)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400509 SetRuleData = None
510 ChainSetRuleData = None
511 elif Format == 2:
512 Coverage = lambda r: r.Coverage
513 ChainCoverage = lambda r: r.Coverage
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400514 ContextData = lambda r:(r.ClassDef,)
515 ChainContextData = lambda r:(r.LookAheadClassDef,
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400516 r.InputClassDef,
517 r.BacktrackClassDef)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400518 RuleData = lambda r:(r.Class,)
519 ChainRuleData = lambda r:(r.LookAhead, r.Input, r.Backtrack)
520 def SetRuleData(r, d):(r.Class,) = d
521 def ChainSetRuleData(r, d):(r.LookAhead, r.Input, r.Backtrack) = d
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400522 elif Format == 3:
523 Coverage = lambda r: r.Coverage[0]
524 ChainCoverage = lambda r: r.InputCoverage[0]
525 ContextData = None
526 ChainContextData = None
527 RuleData = lambda r: r.Coverage
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400528 ChainRuleData = lambda r:(r.LookAheadCoverage +
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400529 r.InputCoverage +
530 r.BacktrackCoverage)
531 SetRuleData = None
532 ChainSetRuleData = None
533 else:
534 assert 0, "unknown format: %s" % Format
Behdad Esfahbod452ab6c2013-07-23 22:57:43 -0400535
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400536 if Chain:
537 self.Coverage = ChainCoverage
538 self.ContextData = ChainContextData
539 self.RuleData = ChainRuleData
540 self.SetRuleData = ChainSetRuleData
541 else:
542 self.Coverage = Coverage
543 self.ContextData = ContextData
544 self.RuleData = RuleData
545 self.SetRuleData = SetRuleData
Behdad Esfahbod9e735722013-07-23 16:35:23 -0400546
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400547 if Format == 1:
548 self.Rule = ChainTyp+'Rule'
549 self.RuleCount = ChainTyp+'RuleCount'
550 self.RuleSet = ChainTyp+'RuleSet'
551 self.RuleSetCount = ChainTyp+'RuleSetCount'
552 self.Intersect = lambda glyphs, c, r: [r] if r in glyphs else []
553 elif Format == 2:
554 self.Rule = ChainTyp+'ClassRule'
555 self.RuleCount = ChainTyp+'ClassRuleCount'
556 self.RuleSet = ChainTyp+'ClassSet'
557 self.RuleSetCount = ChainTyp+'ClassSetCount'
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400558 self.Intersect = lambda glyphs, c, r: c.intersect_class(glyphs, r)
Behdad Esfahbod89987002013-07-23 23:07:42 -0400559
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400560 self.ClassDef = 'InputClassDef' if Chain else 'ClassDef'
Behdad Esfahbod98b60752013-10-14 17:49:19 +0200561 self.ClassDefIndex = 1 if Chain else 0
Behdad Esfahbod11763302013-08-14 15:33:08 -0400562 self.Input = 'Input' if Chain else 'Class'
Behdad Esfahbod27108392013-07-23 16:40:47 -0400563
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400564 if self.Format not in [1, 2, 3]:
Behdad Esfahbod318adc02013-08-13 20:09:28 -0400565 return None # Don't shoot the messenger; let it go
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400566 if not hasattr(self.__class__, "__ContextHelpers"):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400567 self.__class__.__ContextHelpers = {}
568 if self.Format not in self.__class__.__ContextHelpers:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400569 helper = ContextHelper(self.__class__, self.Format)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400570 self.__class__.__ContextHelpers[self.Format] = helper
571 return self.__class__.__ContextHelpers[self.Format]
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400572
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400573@_add_method(otTables.ContextSubst,
574 otTables.ChainContextSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400575def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400576 if cur_glyphs == None: cur_glyphs = s.glyphs
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400577 c = self.__classify_context()
Behdad Esfahbod1ab2dbf2013-07-23 17:17:21 -0400578
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400579 indices = c.Coverage(self).intersect(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400580 if not indices:
581 return []
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400582 cur_glyphs = c.Coverage(self).intersect_glyphs(s.glyphs);
Behdad Esfahbod1d4fa132013-08-08 22:59:32 -0400583
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400584 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400585 ContextData = c.ContextData(self)
586 rss = getattr(self, c.RuleSet)
Behdad Esfahbod11174452013-11-18 20:20:49 -0500587 rssCount = getattr(self, c.RuleSetCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400588 for i in indices:
Behdad Esfahbod11174452013-11-18 20:20:49 -0500589 if i >= rssCount or not rss[i]: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400590 for r in getattr(rss[i], c.Rule):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400591 if not r: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400592 if all(all(c.Intersect(s.glyphs, cd, k) for k in klist)
593 for cd,klist in zip(ContextData, c.RuleData(r))):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400594 chaos = False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400595 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400596 if not ll: continue
597 seqi = ll.SequenceIndex
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400598 if chaos:
599 pos_glyphs = s.glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400600 else:
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400601 if seqi == 0:
602 pos_glyphs = set([c.Coverage(self).glyphs[i]])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400603 else:
Behdad Esfahbodd3fdcc72013-08-14 17:59:31 -0400604 pos_glyphs = set([r.Input[seqi - 1]])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400605 lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400606 chaos = chaos or lookup.may_have_non_1to1()
607 lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400608 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400609 ClassDef = getattr(self, c.ClassDef)
610 indices = ClassDef.intersect(cur_glyphs)
611 ContextData = c.ContextData(self)
612 rss = getattr(self, c.RuleSet)
Behdad Esfahbod11174452013-11-18 20:20:49 -0500613 rssCount = getattr(self, c.RuleSetCount)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400614 for i in indices:
Behdad Esfahbod11174452013-11-18 20:20:49 -0500615 if i >= rssCount or not rss[i]: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400616 for r in getattr(rss[i], c.Rule):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400617 if not r: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400618 if all(all(c.Intersect(s.glyphs, cd, k) for k in klist)
619 for cd,klist in zip(ContextData, c.RuleData(r))):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400620 chaos = False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400621 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400622 if not ll: continue
623 seqi = ll.SequenceIndex
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400624 if chaos:
625 pos_glyphs = s.glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400626 else:
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400627 if seqi == 0:
628 pos_glyphs = ClassDef.intersect_class(cur_glyphs, i)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400629 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400630 pos_glyphs = ClassDef.intersect_class(s.glyphs,
Behdad Esfahbod11763302013-08-14 15:33:08 -0400631 getattr(r, c.Input)[seqi - 1])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400632 lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400633 chaos = chaos or lookup.may_have_non_1to1()
634 lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400635 elif self.Format == 3:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400636 if not all(x.intersect(s.glyphs) for x in c.RuleData(self)):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400637 return []
638 r = self
639 chaos = False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400640 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400641 if not ll: continue
642 seqi = ll.SequenceIndex
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400643 if chaos:
644 pos_glyphs = s.glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400645 else:
Behdad Esfahbod348f8582013-08-20 11:50:04 -0400646 if seqi == 0:
647 pos_glyphs = cur_glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400648 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400649 pos_glyphs = r.InputCoverage[seqi].intersect_glyphs(s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400650 lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400651 chaos = chaos or lookup.may_have_non_1to1()
652 lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400653 else:
654 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod00776972013-07-23 15:33:00 -0400655
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400656@_add_method(otTables.ContextSubst,
657 otTables.ContextPos,
658 otTables.ChainContextSubst,
659 otTables.ChainContextPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400660def subset_glyphs(self, s):
661 c = self.__classify_context()
Behdad Esfahbodd8c7e102013-07-23 17:07:06 -0400662
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400663 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400664 indices = self.Coverage.subset(s.glyphs)
665 rss = getattr(self, c.RuleSet)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400666 rss = [rss[i] for i in indices]
667 for rs in rss:
668 if not rs: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400669 ss = getattr(rs, c.Rule)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400670 ss = [r for r in ss
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400671 if r and all(all(g in s.glyphs for g in glist)
672 for glist in c.RuleData(r))]
673 setattr(rs, c.Rule, ss)
674 setattr(rs, c.RuleCount, len(ss))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400675 # Prune empty subrulesets
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400676 rss = [rs for rs in rss if rs and getattr(rs, c.Rule)]
677 setattr(self, c.RuleSet, rss)
678 setattr(self, c.RuleSetCount, len(rss))
679 return bool(rss)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400680 elif self.Format == 2:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400681 if not self.Coverage.subset(s.glyphs):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400682 return False
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400683 ContextData = c.ContextData(self)
684 klass_maps = [x.subset(s.glyphs, remap=True) for x in ContextData]
Behdad Esfahbod98b60752013-10-14 17:49:19 +0200685
686 # Keep rulesets for class numbers that survived.
687 indices = klass_maps[c.ClassDefIndex]
688 rss = getattr(self, c.RuleSet)
689 rssCount = getattr(self, c.RuleSetCount)
690 rss = [rss[i] for i in indices if i < rssCount]
691 del rssCount
692 # Delete, but not renumber, unreachable rulesets.
693 indices = getattr(self, c.ClassDef).intersect(self.Coverage.glyphs)
694 rss = [rss if i in indices else None for i,rss in enumerate(rss)]
695 while rss and rss[-1] == None:
696 del rss[-1]
697
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400698 for rs in rss:
699 if not rs: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400700 ss = getattr(rs, c.Rule)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400701 ss = [r for r in ss
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400702 if r and all(all(k in klass_map for k in klist)
703 for klass_map,klist in zip(klass_maps, c.RuleData(r)))]
704 setattr(rs, c.Rule, ss)
705 setattr(rs, c.RuleCount, len(ss))
Behdad Esfahbode9a3bd62013-07-23 22:41:11 -0400706
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400707 # Remap rule classes
708 for r in ss:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400709 c.SetRuleData(r, [[klass_map.index(k) for k in klist]
710 for klass_map,klist in zip(klass_maps, c.RuleData(r))])
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400711 return bool(rss)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400712 elif self.Format == 3:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400713 return all(x.subset(s.glyphs) for x in c.RuleData(self))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400714 else:
715 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400716
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400717@_add_method(otTables.ContextSubst,
718 otTables.ChainContextSubst,
719 otTables.ContextPos,
720 otTables.ChainContextPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400721def subset_lookups(self, lookup_indices):
722 c = self.__classify_context()
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400723
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400724 if self.Format in [1, 2]:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400725 for rs in getattr(self, c.RuleSet):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400726 if not rs: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400727 for r in getattr(rs, c.Rule):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400728 if not r: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400729 setattr(r, c.LookupRecord,
730 [ll for ll in getattr(r, c.LookupRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400731 if ll and ll.LookupListIndex in lookup_indices])
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400732 for ll in getattr(r, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400733 if not ll: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400734 ll.LookupListIndex = lookup_indices.index(ll.LookupListIndex)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400735 elif self.Format == 3:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400736 setattr(self, c.LookupRecord,
737 [ll for ll in getattr(self, c.LookupRecord)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400738 if ll and ll.LookupListIndex in lookup_indices])
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400739 for ll in getattr(self, c.LookupRecord):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400740 if not ll: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400741 ll.LookupListIndex = lookup_indices.index(ll.LookupListIndex)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400742 else:
743 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400744
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400745@_add_method(otTables.ContextSubst,
746 otTables.ChainContextSubst,
747 otTables.ContextPos,
748 otTables.ChainContextPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400749def collect_lookups(self):
750 c = self.__classify_context()
Behdad Esfahbod44c2b3c2013-07-23 16:00:32 -0400751
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400752 if self.Format in [1, 2]:
753 return [ll.LookupListIndex
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400754 for rs in getattr(self, c.RuleSet) if rs
755 for r in getattr(rs, c.Rule) if r
756 for ll in getattr(r, c.LookupRecord) if ll]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400757 elif self.Format == 3:
758 return [ll.LookupListIndex
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400759 for ll in getattr(self, c.LookupRecord) if ll]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400760 else:
761 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod59dfc132013-07-23 15:39:20 -0400762
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400763@_add_method(otTables.ExtensionSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400764def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400765 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400766 self.ExtSubTable.closure_glyphs(s, cur_glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400767 else:
768 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400769
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400770@_add_method(otTables.ExtensionSubst)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400771def may_have_non_1to1(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400772 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400773 return self.ExtSubTable.may_have_non_1to1()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400774 else:
775 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbodaeacc152013-08-12 20:24:33 -0400776
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400777@_add_method(otTables.ExtensionSubst,
778 otTables.ExtensionPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400779def prune_pre_subset(self, options):
780 if self.Format == 1:
781 return self.ExtSubTable.prune_pre_subset(options)
782 else:
783 assert 0, "unknown format: %s" % self.Format
784
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400785@_add_method(otTables.ExtensionSubst,
786 otTables.ExtensionPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400787def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400788 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400789 return self.ExtSubTable.subset_glyphs(s)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400790 else:
791 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod54660612013-07-21 18:16:55 -0400792
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400793@_add_method(otTables.ExtensionSubst,
794 otTables.ExtensionPos)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400795def prune_post_subset(self, options):
796 if self.Format == 1:
797 return self.ExtSubTable.prune_post_subset(options)
798 else:
799 assert 0, "unknown format: %s" % self.Format
800
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400801@_add_method(otTables.ExtensionSubst,
802 otTables.ExtensionPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400803def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400804 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400805 return self.ExtSubTable.subset_lookups(lookup_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400806 else:
807 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400808
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400809@_add_method(otTables.ExtensionSubst,
810 otTables.ExtensionPos)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400811def collect_lookups(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400812 if self.Format == 1:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400813 return self.ExtSubTable.collect_lookups()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400814 else:
815 assert 0, "unknown format: %s" % self.Format
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400816
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400817@_add_method(otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400818def closure_glyphs(self, s, cur_glyphs=None):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400819 for st in self.SubTable:
820 if not st: continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400821 st.closure_glyphs(s, cur_glyphs)
Behdad Esfahbod610b0552013-07-23 14:52:18 -0400822
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400823@_add_method(otTables.Lookup)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400824def prune_pre_subset(self, options):
825 ret = False
826 for st in self.SubTable:
827 if not st: continue
828 if st.prune_pre_subset(options): ret = True
829 return ret
830
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400831@_add_method(otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400832def subset_glyphs(self, s):
833 self.SubTable = [st for st in self.SubTable if st and st.subset_glyphs(s)]
834 self.SubTableCount = len(self.SubTable)
835 return bool(self.SubTableCount)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -0400836
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400837@_add_method(otTables.Lookup)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400838def prune_post_subset(self, options):
839 ret = False
840 for st in self.SubTable:
841 if not st: continue
842 if st.prune_post_subset(options): ret = True
843 return ret
844
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400845@_add_method(otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400846def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400847 for s in self.SubTable:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400848 s.subset_lookups(lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400849
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400850@_add_method(otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400851def collect_lookups(self):
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400852 return _uniq_sort(sum((st.collect_lookups() for st in self.SubTable
853 if st), []))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400854
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400855@_add_method(otTables.Lookup)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400856def may_have_non_1to1(self):
857 return any(st.may_have_non_1to1() for st in self.SubTable if st)
Behdad Esfahbodaeacc152013-08-12 20:24:33 -0400858
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400859@_add_method(otTables.LookupList)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400860def prune_pre_subset(self, options):
861 ret = False
862 for l in self.Lookup:
863 if not l: continue
864 if l.prune_pre_subset(options): ret = True
865 return ret
866
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400867@_add_method(otTables.LookupList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400868def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400869 "Returns the indices of nonempty lookups."
Behdad Esfahbod4734be52013-08-14 19:47:42 -0400870 return [i for i,l in enumerate(self.Lookup) if l and l.subset_glyphs(s)]
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400871
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400872@_add_method(otTables.LookupList)
Behdad Esfahbodd77f1572013-08-15 19:24:36 -0400873def prune_post_subset(self, options):
874 ret = False
875 for l in self.Lookup:
876 if not l: continue
877 if l.prune_post_subset(options): ret = True
878 return ret
879
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400880@_add_method(otTables.LookupList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400881def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400882 self.Lookup = [self.Lookup[i] for i in lookup_indices
883 if i < self.LookupCount]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400884 self.LookupCount = len(self.Lookup)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400885 for l in self.Lookup:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400886 l.subset_lookups(lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400887
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400888@_add_method(otTables.LookupList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400889def closure_lookups(self, lookup_indices):
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400890 lookup_indices = _uniq_sort(lookup_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400891 recurse = lookup_indices
892 while True:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400893 recurse_lookups = sum((self.Lookup[i].collect_lookups()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400894 for i in recurse if i < self.LookupCount), [])
895 recurse_lookups = [l for l in recurse_lookups
896 if l not in lookup_indices and l < self.LookupCount]
897 if not recurse_lookups:
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400898 return _uniq_sort(lookup_indices)
899 recurse_lookups = _uniq_sort(recurse_lookups)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400900 lookup_indices.extend(recurse_lookups)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400901 recurse = recurse_lookups
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400902
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400903@_add_method(otTables.Feature)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400904def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400905 self.LookupListIndex = [l for l in self.LookupListIndex
906 if l in lookup_indices]
907 # Now map them.
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400908 self.LookupListIndex = [lookup_indices.index(l)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400909 for l in self.LookupListIndex]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400910 self.LookupCount = len(self.LookupListIndex)
Behdad Esfahbodd214f202013-11-26 17:42:13 -0500911 return self.LookupCount or self.FeatureParams
Behdad Esfahbod54660612013-07-21 18:16:55 -0400912
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400913@_add_method(otTables.Feature)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400914def collect_lookups(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400915 return self.LookupListIndex[:]
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400916
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400917@_add_method(otTables.FeatureList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400918def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400919 "Returns the indices of nonempty features."
Behdad Esfahbod4734be52013-08-14 19:47:42 -0400920 feature_indices = [i for i,f in enumerate(self.FeatureRecord)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400921 if f.Feature.subset_lookups(lookup_indices)]
922 self.subset_features(feature_indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400923 return feature_indices
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400924
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400925@_add_method(otTables.FeatureList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400926def collect_lookups(self, feature_indices):
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400927 return _uniq_sort(sum((self.FeatureRecord[i].Feature.collect_lookups()
928 for i in feature_indices
Behdad Esfahbod1ee298d2013-08-13 20:07:09 -0400929 if i < self.FeatureCount), []))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400930
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400931@_add_method(otTables.FeatureList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400932def subset_features(self, feature_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400933 self.FeatureRecord = [self.FeatureRecord[i] for i in feature_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400934 self.FeatureCount = len(self.FeatureRecord)
935 return bool(self.FeatureCount)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -0400936
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400937@_add_method(otTables.DefaultLangSys,
938 otTables.LangSys)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400939def subset_features(self, feature_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400940 if self.ReqFeatureIndex in feature_indices:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400941 self.ReqFeatureIndex = feature_indices.index(self.ReqFeatureIndex)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400942 else:
943 self.ReqFeatureIndex = 65535
944 self.FeatureIndex = [f for f in self.FeatureIndex if f in feature_indices]
945 # Now map them.
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400946 self.FeatureIndex = [feature_indices.index(f) for f in self.FeatureIndex
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400947 if f in feature_indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400948 self.FeatureCount = len(self.FeatureIndex)
949 return bool(self.FeatureCount or self.ReqFeatureIndex != 65535)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400950
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400951@_add_method(otTables.DefaultLangSys,
952 otTables.LangSys)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400953def collect_features(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400954 feature_indices = self.FeatureIndex[:]
955 if self.ReqFeatureIndex != 65535:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400956 feature_indices.append(self.ReqFeatureIndex)
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400957 return _uniq_sort(feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400958
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400959@_add_method(otTables.Script)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400960def subset_features(self, feature_indices):
961 if(self.DefaultLangSys and
962 not self.DefaultLangSys.subset_features(feature_indices)):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400963 self.DefaultLangSys = None
964 self.LangSysRecord = [l for l in self.LangSysRecord
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400965 if l.LangSys.subset_features(feature_indices)]
966 self.LangSysCount = len(self.LangSysRecord)
967 return bool(self.LangSysCount or self.DefaultLangSys)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400968
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400969@_add_method(otTables.Script)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400970def collect_features(self):
971 feature_indices = [l.LangSys.collect_features() for l in self.LangSysRecord]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400972 if self.DefaultLangSys:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400973 feature_indices.append(self.DefaultLangSys.collect_features())
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400974 return _uniq_sort(sum(feature_indices, []))
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400975
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400976@_add_method(otTables.ScriptList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400977def subset_features(self, feature_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400978 self.ScriptRecord = [s for s in self.ScriptRecord
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400979 if s.Script.subset_features(feature_indices)]
980 self.ScriptCount = len(self.ScriptRecord)
981 return bool(self.ScriptCount)
Behdad Esfahbod77cda412013-07-22 11:46:50 -0400982
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400983@_add_method(otTables.ScriptList)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400984def collect_features(self):
Behdad Esfahbod22f5cfc2013-08-13 20:25:37 -0400985 return _uniq_sort(sum((s.Script.collect_features()
986 for s in self.ScriptRecord), []))
Behdad Esfahbod78661bb2013-07-23 10:23:42 -0400987
Behdad Esfahbod46d260f2013-09-19 20:36:49 -0400988@_add_method(ttLib.getTableClass('GSUB'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400989def closure_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -0400990 s.table = self.table
Behdad Esfahbod77a2b282013-08-13 19:53:30 -0400991 feature_indices = self.table.ScriptList.collect_features()
Behdad Esfahbod7e972472013-11-15 17:57:15 -0500992 if self.table.FeatureList:
993 lookup_indices = self.table.FeatureList.collect_lookups(feature_indices)
994 else:
995 lookup_indices = []
996 if self.table.LookupList:
997 while True:
998 orig_glyphs = s.glyphs.copy()
999 for i in lookup_indices:
1000 if i >= self.table.LookupList.LookupCount: continue
1001 if not self.table.LookupList.Lookup[i]: continue
1002 self.table.LookupList.Lookup[i].closure_glyphs(s)
1003 if orig_glyphs == s.glyphs:
1004 break
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001005 del s.table
Behdad Esfahbod610b0552013-07-23 14:52:18 -04001006
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001007@_add_method(ttLib.getTableClass('GSUB'),
1008 ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001009def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001010 s.glyphs = s.glyphs_gsubed
Behdad Esfahbod7e972472013-11-15 17:57:15 -05001011 if self.table.LookupList:
1012 lookup_indices = self.table.LookupList.subset_glyphs(s)
1013 else:
1014 lookup_indices = []
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001015 self.subset_lookups(lookup_indices)
1016 self.prune_lookups()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001017 return True
Behdad Esfahbod02b92062013-07-21 18:40:59 -04001018
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001019@_add_method(ttLib.getTableClass('GSUB'),
1020 ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001021def subset_lookups(self, lookup_indices):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001022 """Retrains specified lookups, then removes empty features, language
1023 systems, and scripts."""
Behdad Esfahbod7e972472013-11-15 17:57:15 -05001024 if self.table.LookupList:
1025 self.table.LookupList.subset_lookups(lookup_indices)
1026 if self.table.FeatureList:
1027 feature_indices = self.table.FeatureList.subset_lookups(lookup_indices)
1028 else:
1029 feature_indices = []
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001030 self.table.ScriptList.subset_features(feature_indices)
Behdad Esfahbod77cda412013-07-22 11:46:50 -04001031
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001032@_add_method(ttLib.getTableClass('GSUB'),
1033 ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001034def prune_lookups(self):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001035 "Remove unreferenced lookups"
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001036 feature_indices = self.table.ScriptList.collect_features()
Behdad Esfahbod7e972472013-11-15 17:57:15 -05001037 if self.table.FeatureList:
1038 lookup_indices = self.table.FeatureList.collect_lookups(feature_indices)
1039 else:
1040 lookup_indices = []
1041 if self.table.LookupList:
1042 lookup_indices = self.table.LookupList.closure_lookups(lookup_indices)
1043 else:
1044 lookup_indices = []
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001045 self.subset_lookups(lookup_indices)
Behdad Esfahbod78661bb2013-07-23 10:23:42 -04001046
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001047@_add_method(ttLib.getTableClass('GSUB'),
1048 ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001049def subset_feature_tags(self, feature_tags):
Behdad Esfahbod7e972472013-11-15 17:57:15 -05001050 if self.table.FeatureList:
1051 feature_indices = [i for i,f in
1052 enumerate(self.table.FeatureList.FeatureRecord)
1053 if f.FeatureTag in feature_tags]
1054 self.table.FeatureList.subset_features(feature_indices)
1055 else:
1056 feature_indices = []
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001057 self.table.ScriptList.subset_features(feature_indices)
Behdad Esfahbod356c42e2013-07-23 12:10:46 -04001058
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001059@_add_method(ttLib.getTableClass('GSUB'),
1060 ttLib.getTableClass('GPOS'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001061def prune_pre_subset(self, options):
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001062 if '*' not in options.layout_features:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001063 self.subset_feature_tags(options.layout_features)
1064 self.prune_lookups()
Behdad Esfahbod7e972472013-11-15 17:57:15 -05001065 if self.table.LookupList:
1066 self.table.LookupList.prune_pre_subset(options);
Behdad Esfahbodd77f1572013-08-15 19:24:36 -04001067 return True
1068
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001069@_add_method(ttLib.getTableClass('GSUB'),
1070 ttLib.getTableClass('GPOS'))
Behdad Esfahbodd77f1572013-08-15 19:24:36 -04001071def prune_post_subset(self, options):
Behdad Esfahbod9fe4eef2013-11-25 04:28:37 -05001072 table = self.table
1073 if table.ScriptList and not table.ScriptList.ScriptRecord:
1074 table.ScriptList = None
1075 if table.FeatureList and not table.FeatureList.FeatureRecord:
1076 table.FeatureList = None
1077 if table.LookupList:
1078 table.LookupList.prune_post_subset(options);
1079 if not table.LookupList.Lookup:
1080 table.LookupList = None
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001081 return True
Behdad Esfahbod356c42e2013-07-23 12:10:46 -04001082
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001083@_add_method(ttLib.getTableClass('GDEF'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001084def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001085 glyphs = s.glyphs_gsubed
1086 table = self.table
1087 if table.LigCaretList:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001088 indices = table.LigCaretList.Coverage.subset(glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001089 table.LigCaretList.LigGlyph = [table.LigCaretList.LigGlyph[i]
1090 for i in indices]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001091 table.LigCaretList.LigGlyphCount = len(table.LigCaretList.LigGlyph)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001092 if table.MarkAttachClassDef:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001093 table.MarkAttachClassDef.classDefs = dict((g,v) for g,v in
1094 table.MarkAttachClassDef.
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001095 classDefs.items()
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001096 if g in glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001097 if table.GlyphClassDef:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001098 table.GlyphClassDef.classDefs = dict((g,v) for g,v in
1099 table.GlyphClassDef.
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001100 classDefs.items()
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001101 if g in glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001102 if table.AttachList:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001103 indices = table.AttachList.Coverage.subset(glyphs)
Behdad Esfahbod98769432013-11-19 14:40:57 -05001104 GlyphCount = table.AttachList.GlyphCount
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001105 table.AttachList.AttachPoint = [table.AttachList.AttachPoint[i]
Behdad Esfahbod98769432013-11-19 14:40:57 -05001106 for i in indices
1107 if i < GlyphCount]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001108 table.AttachList.GlyphCount = len(table.AttachList.AttachPoint)
Behdad Esfahbod5aea27d2013-11-25 04:19:42 -05001109 if hasattr(table, "MarkGlyphSetsDef") and table.MarkGlyphSetsDef:
1110 for coverage in table.MarkGlyphSetsDef.Coverage:
1111 coverage.subset(glyphs)
Behdad Esfahbod05da9702013-11-25 05:23:07 -05001112 # TODO: The following is disabled. If enabling, we need to go fixup all
1113 # lookups that use MarkFilteringSet and map their set.
1114 #indices = table.MarkGlyphSetsDef.Coverage = [c for c in table.MarkGlyphSetsDef.Coverage if c.glyphs]
Behdad Esfahbod5aea27d2013-11-25 04:19:42 -05001115 return True
1116
1117@_add_method(ttLib.getTableClass('GDEF'))
1118def prune_post_subset(self, options):
1119 table = self.table
1120 if table.LigCaretList and not table.LigCaretList.LigGlyphCount:
1121 table.LigCaretList = None
1122 if table.MarkAttachClassDef and not table.MarkAttachClassDef.classDefs:
1123 table.MarkAttachClassDef = None
1124 if table.GlyphClassDef and not table.GlyphClassDef.classDefs:
1125 table.GlyphClassDef = None
1126 if table.AttachList and not table.AttachList.GlyphCount:
1127 table.AttachList = None
1128 if hasattr(table, "MarkGlyphSetsDef") and table.MarkGlyphSetsDef and not table.MarkGlyphSetsDef.Coverage:
1129 table.MarkGlyphSetsDef = None
Behdad Esfahboda030a0d2013-11-27 17:46:15 -05001130 if table.Version == 0x00010002/0x10000:
Behdad Esfahbod5aea27d2013-11-25 04:19:42 -05001131 table.Version = 1.0
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001132 return bool(table.LigCaretList or
Behdad Esfahbod5aea27d2013-11-25 04:19:42 -05001133 table.MarkAttachClassDef or
1134 table.GlyphClassDef or
1135 table.AttachList or
Behdad Esfahboda030a0d2013-11-27 17:46:15 -05001136 (table.Version >= 0x00010002/0x10000 and table.MarkGlyphSetsDef))
Behdad Esfahbodefb984a2013-07-21 22:26:16 -04001137
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001138@_add_method(ttLib.getTableClass('kern'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001139def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001140 # Prune unknown kern table types
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001141 self.kernTables = [t for t in self.kernTables if hasattr(t, 'kernTable')]
1142 return bool(self.kernTables)
Behdad Esfahbodd4e33a72013-07-24 18:51:05 -04001143
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001144@_add_method(ttLib.getTableClass('kern'))
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 for t in self.kernTables:
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001148 t.kernTable = dict(((a,b),v) for (a,b),v in t.kernTable.items()
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001149 if a in glyphs and b in glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001150 self.kernTables = [t for t in self.kernTables if t.kernTable]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001151 return bool(self.kernTables)
Behdad Esfahbodefb984a2013-07-21 22:26:16 -04001152
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001153@_add_method(ttLib.getTableClass('vmtx'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001154def subset_glyphs(self, s):
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001155 self.metrics = dict((g,v) for g,v in self.metrics.items() if g in s.glyphs)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001156 return bool(self.metrics)
Behdad Esfahbodc7160442013-07-22 14:29:08 -04001157
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001158@_add_method(ttLib.getTableClass('hmtx'))
1159def subset_glyphs(self, s):
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001160 self.metrics = dict((g,v) for g,v in self.metrics.items() if g in s.glyphs)
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001161 return True # Required table
1162
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001163@_add_method(ttLib.getTableClass('hdmx'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001164def subset_glyphs(self, s):
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001165 self.hdmx = dict((sz,dict((g,v) for g,v in l.items() if g in s.glyphs))
1166 for sz,l in self.hdmx.items())
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001167 return bool(self.hdmx)
Behdad Esfahbod75e14fc2013-07-22 14:49:54 -04001168
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001169@_add_method(ttLib.getTableClass('VORG'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001170def subset_glyphs(self, s):
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001171 self.VOriginRecords = dict((g,v) for g,v in self.VOriginRecords.items()
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001172 if g in s.glyphs)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001173 self.numVertOriginYMetrics = len(self.VOriginRecords)
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001174 return True # Never drop; has default metrics
Behdad Esfahbode45d6af2013-07-22 15:29:17 -04001175
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001176@_add_method(ttLib.getTableClass('post'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001177def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001178 if not options.glyph_names:
1179 self.formatType = 3.0
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001180 return True # Required table
Behdad Esfahbod42648242013-07-23 12:56:06 -04001181
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001182@_add_method(ttLib.getTableClass('post'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001183def subset_glyphs(self, s):
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001184 self.extraNames = [] # This seems to do it
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001185 return True # Required table
Behdad Esfahbod653e9742013-07-22 15:17:12 -04001186
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001187@_add_method(ttLib.getTableModule('glyf').Glyph)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001188def remapComponentsFast(self, indices):
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001189 if not self.data or struct.unpack(">h", self.data[:2])[0] >= 0:
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001190 return # Not composite
Behdad Esfahbodd816b7f2013-08-16 16:18:40 -04001191 data = array.array("B", self.data)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001192 i = 10
1193 more = 1
1194 while more:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001195 flags =(data[i] << 8) | data[i+1]
1196 glyphID =(data[i+2] << 8) | data[i+3]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001197 # Remap
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001198 glyphID = indices.index(glyphID)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001199 data[i+2] = glyphID >> 8
1200 data[i+3] = glyphID & 0xFF
1201 i += 4
1202 flags = int(flags)
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -04001203
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04001204 if flags & 0x0001: i += 4 # ARG_1_AND_2_ARE_WORDS
Behdad Esfahbod574ce792013-08-13 20:51:44 -04001205 else: i += 2
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04001206 if flags & 0x0008: i += 2 # WE_HAVE_A_SCALE
1207 elif flags & 0x0040: i += 4 # WE_HAVE_AN_X_AND_Y_SCALE
1208 elif flags & 0x0080: i += 8 # WE_HAVE_A_TWO_BY_TWO
1209 more = flags & 0x0020 # MORE_COMPONENTS
1210
Behdad Esfahbodd816b7f2013-08-16 16:18:40 -04001211 self.data = data.tostring()
Behdad Esfahbod4cf7a802013-07-24 16:08:35 -04001212
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001213@_add_method(ttLib.getTableClass('glyf'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001214def closure_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001215 decompose = s.glyphs
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001216 while True:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001217 components = set()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001218 for g in decompose:
1219 if g not in self.glyphs:
1220 continue
1221 gl = self.glyphs[g]
Behdad Esfahbod043108c2013-09-27 12:59:47 -04001222 for c in gl.getComponentNames(self):
Behdad Esfahbod626107c2013-09-20 14:10:31 -04001223 if c not in s.glyphs:
1224 components.add(c)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001225 components = set(c for c in components if c not in s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001226 if not components:
1227 break
1228 decompose = components
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001229 s.glyphs.update(components)
Behdad Esfahbodabb50a12013-07-23 12:58:37 -04001230
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001231@_add_method(ttLib.getTableClass('glyf'))
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001232def prune_pre_subset(self, options):
1233 if options.notdef_glyph and not options.notdef_outline:
1234 g = self[self.glyphOrder[0]]
1235 # Yay, easy!
1236 g.__dict__.clear()
1237 g.data = ""
1238 return True
1239
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001240@_add_method(ttLib.getTableClass('glyf'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001241def subset_glyphs(self, s):
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001242 self.glyphs = dict((g,v) for g,v in self.glyphs.items() if g in s.glyphs)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001243 indices = [i for i,g in enumerate(self.glyphOrder) if g in s.glyphs]
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001244 for v in self.glyphs.values():
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001245 if hasattr(v, "data"):
1246 v.remapComponentsFast(indices)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001247 else:
Behdad Esfahbod318adc02013-08-13 20:09:28 -04001248 pass # No need
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001249 self.glyphOrder = [g for g in self.glyphOrder if g in s.glyphs]
Behdad Esfahbodb69b6712013-08-29 18:17:31 -04001250 # Don't drop empty 'glyf' tables, otherwise 'loca' doesn't get subset.
1251 return True
Behdad Esfahbod861d9152013-07-22 16:47:24 -04001252
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001253@_add_method(ttLib.getTableClass('glyf'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001254def prune_post_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001255 if not options.hinting:
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001256 for v in self.glyphs.values():
Behdad Esfahbod626107c2013-09-20 14:10:31 -04001257 v.removeHinting()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001258 return True
Behdad Esfahboded98c612013-07-23 12:37:41 -04001259
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001260@_add_method(ttLib.getTableClass('CFF '))
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001261def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001262 cff = self.cff
Behdad Esfahbode0622072013-09-10 14:33:19 -04001263 # CFF table must have one font only
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001264 cff.fontNames = cff.fontNames[:1]
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001265
1266 if options.notdef_glyph and not options.notdef_outline:
1267 for fontname in cff.keys():
1268 font = cff[fontname]
1269 c,_ = font.CharStrings.getItemAndSelector('.notdef')
Behdad Esfahbod21582e92013-09-12 16:47:52 -04001270 # XXX we should preserve the glyph width
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001271 c.bytecode = '\x0e' # endchar
1272 c.program = None
1273
Behdad Esfahbod50f83ef2013-08-29 18:18:17 -04001274 return True # bool(cff.fontNames)
Behdad Esfahbod1a4e72e2013-08-13 15:46:37 -04001275
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001276@_add_method(ttLib.getTableClass('CFF '))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001277def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001278 cff = self.cff
1279 for fontname in cff.keys():
1280 font = cff[fontname]
1281 cs = font.CharStrings
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001282
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001283 # Load all glyphs
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001284 for g in font.charset:
1285 if g not in s.glyphs: continue
1286 c,sel = cs.getItemAndSelector(g)
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001287
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001288 if cs.charStringsAreIndexed:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001289 indices = [i for i,g in enumerate(font.charset) if g in s.glyphs]
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001290 csi = cs.charStringsIndex
1291 csi.items = [csi.items[i] for i in indices]
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001292 csi.count = len(csi.items)
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001293 del csi.file, csi.offsets
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001294 if hasattr(font, "FDSelect"):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001295 sel = font.FDSelect
1296 sel.format = None
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001297 sel.gidArray = [sel.gidArray[i] for i in indices]
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001298 cs.charStrings = dict((g,indices.index(v))
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001299 for g,v in cs.charStrings.items()
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001300 if g in s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001301 else:
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001302 cs.charStrings = dict((g,v)
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001303 for g,v in cs.charStrings.items()
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001304 if g in s.glyphs)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001305 font.charset = [g for g in font.charset if g in s.glyphs]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001306 font.numGlyphs = len(font.charset)
Behdad Esfahbod9290fb42013-08-14 17:48:31 -04001307
Behdad Esfahbod50f83ef2013-08-29 18:18:17 -04001308 return True # any(cff[fontname].numGlyphs for fontname in cff.keys())
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001309
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001310@_add_method(psCharStrings.T2CharString)
Behdad Esfahbodb4ea9532013-08-14 19:37:39 -04001311def subset_subroutines(self, subrs, gsubrs):
1312 p = self.program
Behdad Esfahbode0622072013-09-10 14:33:19 -04001313 assert len(p)
Behdad Esfahbodb466efe2013-11-27 03:34:35 -05001314 for i in range(1, len(p)):
Behdad Esfahbodb4ea9532013-08-14 19:37:39 -04001315 if p[i] == 'callsubr':
Behdad Esfahbodc2e2e832013-11-27 04:15:27 -05001316 assert isinstance(p[i-1], int)
Behdad Esfahbodb4ea9532013-08-14 19:37:39 -04001317 p[i-1] = subrs._used.index(p[i-1] + subrs._old_bias) - subrs._new_bias
1318 elif p[i] == 'callgsubr':
Behdad Esfahbodc2e2e832013-11-27 04:15:27 -05001319 assert isinstance(p[i-1], int)
Behdad Esfahbodb4ea9532013-08-14 19:37:39 -04001320 p[i-1] = gsubrs._used.index(p[i-1] + gsubrs._old_bias) - gsubrs._new_bias
1321
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001322@_add_method(psCharStrings.T2CharString)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001323def drop_hints(self):
1324 hints = self._hints
1325
1326 if hints.has_hint:
1327 self.program = self.program[hints.last_hint:]
Behdad Esfahbod285d7b82013-09-10 20:30:47 -04001328 if hasattr(self, 'width'):
1329 # Insert width back if needed
1330 if self.width != self.private.defaultWidthX:
Behdad Esfahbod99536852013-09-12 00:23:11 -04001331 self.program.insert(0, self.width - self.private.nominalWidthX)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001332
1333 if hints.has_hintmask:
1334 i = 0
1335 p = self.program
1336 while i < len(p):
1337 if p[i] in ['hintmask', 'cntrmask']:
1338 assert i + 1 <= len(p)
1339 del p[i:i+2]
1340 continue
1341 i += 1
1342
Behdad Esfahbod2a70f4a2013-10-28 15:18:07 +01001343 # TODO: we currently don't drop calls to "empty" subroutines.
1344
Behdad Esfahbode0622072013-09-10 14:33:19 -04001345 assert len(self.program)
1346
1347 del self._hints
1348
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001349class _MarkingT2Decompiler(psCharStrings.SimpleT2Decompiler):
Behdad Esfahbode0622072013-09-10 14:33:19 -04001350
1351 def __init__(self, localSubrs, globalSubrs):
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001352 psCharStrings.SimpleT2Decompiler.__init__(self,
1353 localSubrs,
1354 globalSubrs)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001355 for subrs in [localSubrs, globalSubrs]:
1356 if subrs and not hasattr(subrs, "_used"):
1357 subrs._used = set()
1358
1359 def op_callsubr(self, index):
1360 self.localSubrs._used.add(self.operandStack[-1]+self.localBias)
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001361 psCharStrings.SimpleT2Decompiler.op_callsubr(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001362
1363 def op_callgsubr(self, index):
1364 self.globalSubrs._used.add(self.operandStack[-1]+self.globalBias)
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001365 psCharStrings.SimpleT2Decompiler.op_callgsubr(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001366
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001367class _DehintingT2Decompiler(psCharStrings.SimpleT2Decompiler):
Behdad Esfahbode0622072013-09-10 14:33:19 -04001368
Behdad Esfahbod1f262892013-11-28 14:26:39 -05001369 class Hints(object):
Behdad Esfahbode0622072013-09-10 14:33:19 -04001370 def __init__(self):
1371 # Whether calling this charstring produces any hint stems
1372 self.has_hint = False
1373 # Index to start at to drop all hints
1374 self.last_hint = 0
1375 # Index up to which we know more hints are possible. Only
1376 # relevant if status is 0 or 1.
1377 self.last_checked = 0
1378 # The status means:
1379 # 0: after dropping hints, this charstring is empty
1380 # 1: after dropping hints, there may be more hints continuing after this
1381 # 2: no more hints possible after this charstring
1382 self.status = 0
1383 # Has hintmask instructions; not recursive
1384 self.has_hintmask = False
1385 pass
1386
1387 def __init__(self, css, localSubrs, globalSubrs):
1388 self._css = css
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001389 psCharStrings.SimpleT2Decompiler.__init__(self,
1390 localSubrs,
1391 globalSubrs)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001392
1393 def execute(self, charString):
1394 old_hints = charString._hints if hasattr(charString, '_hints') else None
1395 charString._hints = self.Hints()
1396
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001397 psCharStrings.SimpleT2Decompiler.execute(self, charString)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001398
1399 hints = charString._hints
1400
1401 if hints.has_hint or hints.has_hintmask:
1402 self._css.add(charString)
1403
1404 if hints.status != 2:
1405 # Check from last_check, make sure we didn't have any operators.
Behdad Esfahbodb466efe2013-11-27 03:34:35 -05001406 for i in range(hints.last_checked, len(charString.program) - 1):
Behdad Esfahbodc2e2e832013-11-27 04:15:27 -05001407 if isinstance(charString.program[i], str):
Behdad Esfahbode0622072013-09-10 14:33:19 -04001408 hints.status = 2
1409 break;
1410 else:
1411 hints.status = 1 # There's *something* here
1412 hints.last_checked = len(charString.program)
1413
1414 if old_hints:
1415 assert hints.__dict__ == old_hints.__dict__
1416
1417 def op_callsubr(self, index):
1418 subr = self.localSubrs[self.operandStack[-1]+self.localBias]
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001419 psCharStrings.SimpleT2Decompiler.op_callsubr(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001420 self.processSubr(index, subr)
1421
1422 def op_callgsubr(self, index):
1423 subr = self.globalSubrs[self.operandStack[-1]+self.globalBias]
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001424 psCharStrings.SimpleT2Decompiler.op_callgsubr(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001425 self.processSubr(index, subr)
1426
1427 def op_hstem(self, index):
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001428 psCharStrings.SimpleT2Decompiler.op_hstem(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001429 self.processHint(index)
1430 def op_vstem(self, index):
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001431 psCharStrings.SimpleT2Decompiler.op_vstem(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001432 self.processHint(index)
1433 def op_hstemhm(self, index):
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001434 psCharStrings.SimpleT2Decompiler.op_hstemhm(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001435 self.processHint(index)
1436 def op_vstemhm(self, index):
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001437 psCharStrings.SimpleT2Decompiler.op_vstemhm(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001438 self.processHint(index)
1439 def op_hintmask(self, index):
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001440 psCharStrings.SimpleT2Decompiler.op_hintmask(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001441 self.processHintmask(index)
1442 def op_cntrmask(self, index):
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001443 psCharStrings.SimpleT2Decompiler.op_cntrmask(self, index)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001444 self.processHintmask(index)
1445
1446 def processHintmask(self, index):
1447 cs = self.callingStack[-1]
1448 hints = cs._hints
1449 hints.has_hintmask = True
1450 if hints.status != 2 and hints.has_hint:
1451 # Check from last_check, see if we may be an implicit vstem
Behdad Esfahbodb466efe2013-11-27 03:34:35 -05001452 for i in range(hints.last_checked, index - 1):
Behdad Esfahbodc2e2e832013-11-27 04:15:27 -05001453 if isinstance(cs.program[i], str):
Behdad Esfahbod84763142013-09-10 19:00:48 -04001454 hints.status = 2
Behdad Esfahbode0622072013-09-10 14:33:19 -04001455 break;
Behdad Esfahbod84763142013-09-10 19:00:48 -04001456 if hints.status != 2:
Behdad Esfahbode0622072013-09-10 14:33:19 -04001457 # We are an implicit vstem
1458 hints.last_hint = index + 1
Behdad Esfahbod84763142013-09-10 19:00:48 -04001459 hints.status = 0
Behdad Esfahbod2a70f4a2013-10-28 15:18:07 +01001460 hints.last_checked = index + 1
Behdad Esfahbode0622072013-09-10 14:33:19 -04001461
1462 def processHint(self, index):
1463 cs = self.callingStack[-1]
1464 hints = cs._hints
1465 hints.has_hint = True
1466 hints.last_hint = index
1467 hints.last_checked = index
1468
1469 def processSubr(self, index, subr):
1470 cs = self.callingStack[-1]
1471 hints = cs._hints
1472 subr_hints = subr._hints
1473
1474 if subr_hints.has_hint:
Behdad Esfahbod285d7b82013-09-10 20:30:47 -04001475 if hints.status != 2:
1476 hints.has_hint = True
Behdad Esfahbod99536852013-09-12 00:23:11 -04001477 hints.last_checked = index
1478 hints.status = subr_hints.status
Behdad Esfahbod285d7b82013-09-10 20:30:47 -04001479 # Decide where to chop off from
1480 if subr_hints.status == 0:
Behdad Esfahbod99536852013-09-12 00:23:11 -04001481 hints.last_hint = index
Behdad Esfahbod285d7b82013-09-10 20:30:47 -04001482 else:
Behdad Esfahbod99536852013-09-12 00:23:11 -04001483 hints.last_hint = index - 2 # Leave the subr call in
Behdad Esfahbode0622072013-09-10 14:33:19 -04001484 else:
Behdad Esfahbod285d7b82013-09-10 20:30:47 -04001485 # In my understanding, this is a font bug. Ie. it has hint stems
1486 # *after* path construction. I've seen this in widespread fonts.
1487 # Best to ignore the hints I suppose...
1488 pass
1489 #assert 0
Behdad Esfahbode0622072013-09-10 14:33:19 -04001490 else:
1491 hints.status = max(hints.status, subr_hints.status)
1492 if hints.status != 2:
1493 # Check from last_check, make sure we didn't have
1494 # any operators.
Behdad Esfahbodb466efe2013-11-27 03:34:35 -05001495 for i in range(hints.last_checked, index - 1):
Behdad Esfahbodc2e2e832013-11-27 04:15:27 -05001496 if isinstance(cs.program[i], str):
Behdad Esfahbode0622072013-09-10 14:33:19 -04001497 hints.status = 2
1498 break;
1499 hints.last_checked = index
Behdad Esfahbod2a70f4a2013-10-28 15:18:07 +01001500 if hints.status != 2:
1501 # Decide where to chop off from
1502 if subr_hints.status == 0:
1503 hints.last_hint = index
1504 else:
1505 hints.last_hint = index - 2 # Leave the subr call in
Behdad Esfahbode0622072013-09-10 14:33:19 -04001506
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001507@_add_method(ttLib.getTableClass('CFF '))
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001508def prune_post_subset(self, options):
1509 cff = self.cff
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001510 for fontname in cff.keys():
1511 font = cff[fontname]
1512 cs = font.CharStrings
1513
Behdad Esfahbode0622072013-09-10 14:33:19 -04001514
1515 #
Behdad Esfahbod3c20a132013-08-14 19:39:00 -04001516 # Drop unused FontDictionaries
Behdad Esfahbode0622072013-09-10 14:33:19 -04001517 #
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001518 if hasattr(font, "FDSelect"):
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001519 sel = font.FDSelect
1520 indices = _uniq_sort(sel.gidArray)
1521 sel.gidArray = [indices.index (ss) for ss in sel.gidArray]
1522 arr = font.FDArray
Behdad Esfahbod3c20a132013-08-14 19:39:00 -04001523 arr.items = [arr[i] for i in indices]
Behdad Esfahbodd315c912013-08-14 18:18:51 -04001524 arr.count = len(arr.items)
1525 del arr.file, arr.offsets
Behdad Esfahbod1a4e72e2013-08-13 15:46:37 -04001526
Behdad Esfahbode0622072013-09-10 14:33:19 -04001527
1528 #
1529 # Drop hints if not needed
1530 #
1531 if not options.hinting:
1532
1533 #
1534 # This can be tricky, but doesn't have to. What we do is:
1535 #
1536 # - Run all used glyph charstrings and recurse into subroutines,
1537 # - For each charstring (including subroutines), if it has any
1538 # of the hint stem operators, we mark it as such. Upon returning,
1539 # for each charstring we note all the subroutine calls it makes
1540 # that (recursively) contain a stem,
1541 # - Dropping hinting then consists of the following two ops:
1542 # * Drop the piece of the program in each charstring before the
1543 # last call to a stem op or a stem-calling subroutine,
1544 # * Drop all hintmask operations.
1545 # - It's trickier... A hintmask right after hints and a few numbers
1546 # will act as an implicit vstemhm. As such, we track whether
1547 # we have seen any non-hint operators so far and do the right
1548 # thing, recursively... Good luck understanding that :(
1549 #
1550 css = set()
1551 for g in font.charset:
1552 c,sel = cs.getItemAndSelector(g)
1553 # Make sure it's decompiled. We want our "decompiler" to walk
1554 # the program, not the bytecode.
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001555 c.draw(basePen.NullPen())
Behdad Esfahbode0622072013-09-10 14:33:19 -04001556 subrs = getattr(c.private, "Subrs", [])
1557 decompiler = _DehintingT2Decompiler(css, subrs, c.globalSubrs)
1558 decompiler.execute(c)
1559 for charstring in css:
1560 charstring.drop_hints()
1561
Behdad Esfahbod16fc3232013-09-30 15:09:27 -04001562 # Drop font-wide hinting values
1563 all_privs = []
1564 if hasattr(font, 'FDSelect'):
1565 all_privs.extend(fd.Private for fd in font.FDArray)
1566 else:
1567 all_privs.append(font.Private)
1568 for priv in all_privs:
Behdad Esfahbod4d99d142013-10-28 13:15:08 +01001569 for k in ['BlueValues', 'OtherBlues', 'FamilyBlues', 'FamilyOtherBlues',
1570 'BlueScale', 'BlueShift', 'BlueFuzz',
1571 'StemSnapH', 'StemSnapV', 'StdHW', 'StdVW']:
Behdad Esfahbod16fc3232013-09-30 15:09:27 -04001572 if hasattr(priv, k):
1573 setattr(priv, k, None)
1574
Behdad Esfahbode0622072013-09-10 14:33:19 -04001575
1576 #
1577 # Renumber subroutines to remove unused ones
1578 #
1579
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001580 # Mark all used subroutines
1581 for g in font.charset:
1582 c,sel = cs.getItemAndSelector(g)
1583 subrs = getattr(c.private, "Subrs", [])
1584 decompiler = _MarkingT2Decompiler(subrs, c.globalSubrs)
1585 decompiler.execute(c)
1586
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001587 all_subrs = [font.GlobalSubrs]
Behdad Esfahbod83f1f5c2013-08-30 16:20:08 -04001588 if hasattr(font, 'FDSelect'):
Behdad Esfahbode0622072013-09-10 14:33:19 -04001589 all_subrs.extend(fd.Private.Subrs for fd in font.FDArray if hasattr(fd.Private, 'Subrs') and fd.Private.Subrs)
1590 elif hasattr(font.Private, 'Subrs') and font.Private.Subrs:
Behdad Esfahbodcbcaccf2013-08-30 16:21:38 -04001591 all_subrs.append(font.Private.Subrs)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001592
1593 subrs = set(subrs) # Remove duplicates
1594
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001595 # Prepare
1596 for subrs in all_subrs:
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001597 if not hasattr(subrs, '_used'):
1598 subrs._used = set()
1599 subrs._used = _uniq_sort(subrs._used)
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001600 subrs._old_bias = psCharStrings.calcSubrBias(subrs)
1601 subrs._new_bias = psCharStrings.calcSubrBias(subrs._used)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001602
Behdad Esfahboded107712013-08-14 19:54:13 -04001603 # Renumber glyph charstrings
1604 for g in font.charset:
1605 c,sel = cs.getItemAndSelector(g)
1606 subrs = getattr(c.private, "Subrs", [])
1607 c.subset_subroutines (subrs, font.GlobalSubrs)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001608
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001609 # Renumber subroutines themselves
1610 for subrs in all_subrs:
Behdad Esfahbode0622072013-09-10 14:33:19 -04001611
1612 if subrs == font.GlobalSubrs:
1613 if not hasattr(font, 'FDSelect') and hasattr(font.Private, 'Subrs'):
1614 local_subrs = font.Private.Subrs
Behdad Esfahbod83f1f5c2013-08-30 16:20:08 -04001615 else:
Behdad Esfahbode0622072013-09-10 14:33:19 -04001616 local_subrs = []
1617 else:
1618 local_subrs = subrs
1619
1620 subrs.items = [subrs.items[i] for i in subrs._used]
1621 subrs.count = len(subrs.items)
1622 del subrs.file
1623 if hasattr(subrs, 'offsets'):
1624 del subrs.offsets
1625
Behdad Esfahbodb466efe2013-11-27 03:34:35 -05001626 for i in range (subrs.count):
Behdad Esfahbod83f1f5c2013-08-30 16:20:08 -04001627 subrs[i].subset_subroutines (local_subrs, font.GlobalSubrs)
Behdad Esfahbode0622072013-09-10 14:33:19 -04001628
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001629 # Cleanup
1630 for subrs in all_subrs:
Behdad Esfahbod2f3a4b92013-08-14 19:18:50 -04001631 del subrs._used, subrs._old_bias, subrs._new_bias
1632
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001633 return True
Behdad Esfahbod2b677c82013-07-23 13:37:13 -04001634
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001635@_add_method(ttLib.getTableClass('cmap'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001636def closure_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001637 tables = [t for t in self.tables
1638 if t.platformID == 3 and t.platEncID in [1, 10]]
1639 for u in s.unicodes_requested:
1640 found = False
1641 for table in tables:
1642 if u in table.cmap:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001643 s.glyphs.add(table.cmap[u])
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001644 found = True
1645 break
1646 if not found:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001647 s.log("No glyph for Unicode value %s; skipping." % u)
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04001648
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001649@_add_method(ttLib.getTableClass('cmap'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001650def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001651 if not options.legacy_cmap:
1652 # Drop non-Unicode / non-Symbol cmaps
1653 self.tables = [t for t in self.tables
1654 if t.platformID == 3 and t.platEncID in [0, 1, 10]]
1655 if not options.symbol_cmap:
1656 self.tables = [t for t in self.tables
1657 if t.platformID == 3 and t.platEncID in [1, 10]]
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001658 # TODO(behdad) Only keep one subtable?
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001659 # For now, drop format=0 which can't be subset_glyphs easily?
1660 self.tables = [t for t in self.tables if t.format != 0]
Behdad Esfahbodfd92d4c2013-09-19 19:43:09 -04001661 self.numSubTables = len(self.tables)
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001662 return True # Required table
Behdad Esfahbodabb50a12013-07-23 12:58:37 -04001663
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001664@_add_method(ttLib.getTableClass('cmap'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001665def subset_glyphs(self, s):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001666 s.glyphs = s.glyphs_cmaped
1667 for t in self.tables:
1668 # For reasons I don't understand I need this here
1669 # to force decompilation of the cmap format 14.
1670 try:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001671 getattr(t, "asdf")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001672 except AttributeError:
1673 pass
1674 if t.format == 14:
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001675 # TODO(behdad) XXX We drop all the default-UVS mappings(g==None).
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001676 t.uvsDict = dict((v,[(u,g) for u,g in l if g in s.glyphs])
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001677 for v,l in t.uvsDict.items())
1678 t.uvsDict = dict((v,l) for v,l in t.uvsDict.items() if l)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001679 else:
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001680 t.cmap = dict((u,g) for u,g in t.cmap.items()
Behdad Esfahbodd73f2252013-08-16 10:58:25 -04001681 if g in s.glyphs_requested or u in s.unicodes_requested)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001682 self.tables = [t for t in self.tables
Behdad Esfahbod4734be52013-08-14 19:47:42 -04001683 if (t.cmap if t.format != 14 else t.uvsDict)]
Behdad Esfahbodfd92d4c2013-09-19 19:43:09 -04001684 self.numSubTables = len(self.tables)
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001685 # TODO(behdad) Convert formats when needed.
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001686 # In particular, if we have a format=12 without non-BMP
1687 # characters, either drop format=12 one or convert it
1688 # to format=4 if there's not one.
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001689 return True # Required table
Behdad Esfahbod61addb42013-07-23 11:03:49 -04001690
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001691@_add_method(ttLib.getTableClass('name'))
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001692def prune_pre_subset(self, options):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001693 if '*' not in options.name_IDs:
1694 self.names = [n for n in self.names if n.nameID in options.name_IDs]
1695 if not options.name_legacy:
1696 self.names = [n for n in self.names
1697 if n.platformID == 3 and n.platEncID == 1]
1698 if '*' not in options.name_languages:
1699 self.names = [n for n in self.names if n.langID in options.name_languages]
Behdad Esfahboda6241e62013-10-28 13:09:25 +01001700 return True # Required table
Behdad Esfahbod653e9742013-07-22 15:17:12 -04001701
Behdad Esfahbod8c646f62013-07-22 15:06:23 -04001702
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001703# TODO(behdad) OS/2 ulUnicodeRange / ulCodePageRange?
Behdad Esfahbod26560d22013-10-26 22:03:35 +02001704# TODO(behdad) Drop AAT tables.
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001705# TODO(behdad) Drop unneeded GSUB/GPOS Script/LangSys entries.
Behdad Esfahbod852e8a52013-08-29 18:19:22 -04001706# TODO(behdad) Drop empty GSUB/GPOS, and GDEF if no GSUB/GPOS left
1707# TODO(behdad) Drop GDEF subitems if unused by lookups
Behdad Esfahbod10195332013-08-14 19:55:24 -04001708# TODO(behdad) Avoid recursing too much (in GSUB/GPOS and in CFF)
Behdad Esfahbod71f7c742013-08-13 20:16:16 -04001709# TODO(behdad) Text direction considerations.
1710# TODO(behdad) Text script / language considerations.
Behdad Esfahbodcc8fc782013-11-26 22:53:04 -05001711# TODO(behdad) Optionally drop 'kern' table if GPOS available
Behdad Esfahbod8c486d82013-07-24 13:34:47 -04001712
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001713class Options(object):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001714
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001715 class UnknownOptionError(Exception):
1716 pass
Behdad Esfahbod26d9ee72013-08-13 16:55:01 -04001717
Behdad Esfahboda17743f2013-08-28 17:14:53 -04001718 _drop_tables_default = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', 'SVG ',
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001719 'PCLT', 'LTSH']
1720 _drop_tables_default += ['Feat', 'Glat', 'Gloc', 'Silf', 'Sill'] # Graphite
1721 _drop_tables_default += ['CBLC', 'CBDT', 'sbix', 'COLR', 'CPAL'] # Color
1722 _no_subset_tables_default = ['gasp', 'head', 'hhea', 'maxp', 'vhea', 'OS/2',
1723 'loca', 'name', 'cvt ', 'fpgm', 'prep']
1724 _hinting_tables_default = ['cvt ', 'fpgm', 'prep', 'hdmx', 'VDMX']
Behdad Esfahbod26d9ee72013-08-13 16:55:01 -04001725
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001726 # Based on HarfBuzz shapers
1727 _layout_features_groups = {
1728 # Default shaper
1729 'common': ['ccmp', 'liga', 'locl', 'mark', 'mkmk', 'rlig'],
1730 'horizontal': ['calt', 'clig', 'curs', 'kern', 'rclt'],
1731 'vertical': ['valt', 'vert', 'vkrn', 'vpal', 'vrt2'],
1732 'ltr': ['ltra', 'ltrm'],
1733 'rtl': ['rtla', 'rtlm'],
1734 # Complex shapers
1735 'arabic': ['init', 'medi', 'fina', 'isol', 'med2', 'fin2', 'fin3',
1736 'cswh', 'mset'],
1737 'hangul': ['ljmo', 'vjmo', 'tjmo'],
Behdad Esfahbod3977d3e2013-10-14 17:49:12 +02001738 'tibetan': ['abvs', 'blws', 'abvm', 'blwm'],
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001739 'indic': ['nukt', 'akhn', 'rphf', 'rkrf', 'pref', 'blwf', 'half',
1740 'abvf', 'pstf', 'cfar', 'vatu', 'cjct', 'init', 'pres',
1741 'abvs', 'blws', 'psts', 'haln', 'dist', 'abvm', 'blwm'],
1742 }
1743 _layout_features_default = _uniq_sort(sum(
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001744 iter(_layout_features_groups.values()), []))
Behdad Esfahbod9eeeb4e2013-08-13 16:58:50 -04001745
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001746 drop_tables = _drop_tables_default
1747 no_subset_tables = _no_subset_tables_default
1748 hinting_tables = _hinting_tables_default
1749 layout_features = _layout_features_default
Behdad Esfahbodfe6bc4c2013-11-02 11:10:23 +00001750 hinting = True
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001751 glyph_names = False
1752 legacy_cmap = False
1753 symbol_cmap = False
1754 name_IDs = [1, 2] # Family and Style
1755 name_legacy = False
1756 name_languages = [0x0409] # English
Behdad Esfahbod04f3a192013-08-29 16:56:06 -04001757 notdef_glyph = True # gid0 for TrueType / .notdef for CFF
Behdad Esfahbod2d82c322013-08-29 18:02:48 -04001758 notdef_outline = False # No need for notdef to have an outline really
Behdad Esfahbod04f3a192013-08-29 16:56:06 -04001759 recommended_glyphs = False # gid1, gid2, gid3 for TrueType
Behdad Esfahbode911de12013-08-16 12:42:34 -04001760 recalc_bounds = False # Recalculate font bounding boxes
Behdad Esfahbod03d78da2013-08-29 16:42:00 -04001761 canonical_order = False # Order tables as recommended
Behdad Esfahbodc6c3bb82013-08-15 17:46:20 -04001762 flavor = None # May be 'woff'
Behdad Esfahbod9eeeb4e2013-08-13 16:58:50 -04001763
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001764 def __init__(self, **kwargs):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001765
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001766 self.set(**kwargs)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001767
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001768 def set(self, **kwargs):
Behdad Esfahbod6890d052013-11-27 06:26:35 -05001769 for k,v in kwargs.items():
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001770 if not hasattr(self, k):
Behdad Esfahbodac10d812013-09-03 18:29:58 -04001771 raise self.UnknownOptionError("Unknown option '%s'" % k)
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001772 setattr(self, k, v)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001773
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001774 def parse_opts(self, argv, ignore_unknown=False):
1775 ret = []
1776 opts = {}
1777 for a in argv:
1778 orig_a = a
1779 if not a.startswith('--'):
1780 ret.append(a)
1781 continue
1782 a = a[2:]
1783 i = a.find('=')
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001784 op = '='
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001785 if i == -1:
1786 if a.startswith("no-"):
1787 k = a[3:]
1788 v = False
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001789 else:
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001790 k = a
1791 v = True
1792 else:
1793 k = a[:i]
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001794 if k[-1] in "-+":
1795 op = k[-1]+'=' # Ops is '-=' or '+=' now.
1796 k = k[:-1]
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001797 v = a[i+1:]
1798 k = k.replace('-', '_')
1799 if not hasattr(self, k):
1800 if ignore_unknown == True or k in ignore_unknown:
1801 ret.append(orig_a)
1802 continue
1803 else:
1804 raise self.UnknownOptionError("Unknown option '%s'" % a)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001805
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001806 ov = getattr(self, k)
1807 if isinstance(ov, bool):
1808 v = bool(v)
1809 elif isinstance(ov, int):
1810 v = int(v)
1811 elif isinstance(ov, list):
Behdad Esfahbod0fc55022013-08-15 19:06:48 -04001812 vv = v.split(',')
1813 if vv == ['']:
1814 vv = []
Behdad Esfahbod87c8c502013-08-16 14:44:09 -04001815 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 -04001816 if op == '=':
1817 v = vv
1818 elif op == '+=':
1819 v = ov
1820 v.extend(vv)
1821 elif op == '-=':
1822 v = ov
1823 for x in vv:
1824 if x in v:
1825 v.remove(x)
1826 else:
Behdad Esfahbod153ec402013-12-04 01:15:46 -05001827 assert False
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001828
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001829 opts[k] = v
1830 self.set(**opts)
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001831
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001832 return ret
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001833
1834
Behdad Esfahbod5d4f99d2013-08-13 20:57:59 -04001835class Subsetter(object):
1836
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001837 def __init__(self, options=None, log=None):
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001838
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001839 if not log:
1840 log = Logger()
1841 if not options:
1842 options = Options()
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04001843
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001844 self.options = options
1845 self.log = log
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001846 self.unicodes_requested = set()
1847 self.glyphs_requested = set()
1848 self.glyphs = set()
Behdad Esfahbod3d513b72013-07-31 14:11:40 -04001849
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001850 def populate(self, glyphs=[], unicodes=[], text=""):
1851 self.unicodes_requested.update(unicodes)
Behdad Esfahbodb21c9d32013-11-27 18:09:08 -05001852 if isinstance(text, bytes):
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001853 text = text.decode("utf8")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001854 for u in text:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001855 self.unicodes_requested.add(ord(u))
1856 self.glyphs_requested.update(glyphs)
1857 self.glyphs.update(glyphs)
Behdad Esfahbod3d513b72013-07-31 14:11:40 -04001858
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04001859 def _prune_pre_subset(self, font):
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001860
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001861 for tag in font.keys():
1862 if tag == 'GlyphOrder': continue
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001863
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001864 if(tag in self.options.drop_tables or
1865 (tag in self.options.hinting_tables and not self.options.hinting)):
1866 self.log(tag, "dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001867 del font[tag]
1868 continue
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001869
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001870 clazz = ttLib.getTableClass(tag)
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001871
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001872 if hasattr(clazz, 'prune_pre_subset'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001873 table = font[tag]
Behdad Esfahbod010c5f92013-09-10 20:54:46 -04001874 self.log.lapse("load '%s'" % tag)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001875 retain = table.prune_pre_subset(self.options)
1876 self.log.lapse("prune '%s'" % tag)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001877 if not retain:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001878 self.log(tag, "pruned to empty; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001879 del font[tag]
1880 continue
1881 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001882 self.log(tag, "pruned")
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001883
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04001884 def _closure_glyphs(self, font):
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001885
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001886 self.glyphs = self.glyphs_requested.copy()
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001887
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001888 if 'cmap' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001889 font['cmap'].closure_glyphs(self)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001890 self.glyphs_cmaped = self.glyphs
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001891
Behdad Esfahbod04f3a192013-08-29 16:56:06 -04001892 if self.options.notdef_glyph:
1893 if 'glyf' in font:
1894 self.glyphs.add(font.getGlyphName(0))
1895 self.log("Added gid0 to subset")
1896 else:
1897 self.glyphs.add('.notdef')
1898 self.log("Added .notdef to subset")
1899 if self.options.recommended_glyphs:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001900 if 'glyf' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001901 for i in range(4):
1902 self.glyphs.add(font.getGlyphName(i))
1903 self.log("Added first four glyphs to subset")
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001904
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001905 if 'GSUB' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001906 self.log("Closing glyph list over 'GSUB': %d glyphs before" %
1907 len(self.glyphs))
1908 self.log.glyphs(self.glyphs, font=font)
1909 font['GSUB'].closure_glyphs(self)
1910 self.log("Closed glyph list over 'GSUB': %d glyphs after" %
1911 len(self.glyphs))
1912 self.log.glyphs(self.glyphs, font=font)
1913 self.log.lapse("close glyph list over 'GSUB'")
1914 self.glyphs_gsubed = self.glyphs.copy()
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001915
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001916 if 'glyf' in font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001917 self.log("Closing glyph list over 'glyf': %d glyphs before" %
1918 len(self.glyphs))
1919 self.log.glyphs(self.glyphs, font=font)
1920 font['glyf'].closure_glyphs(self)
1921 self.log("Closed glyph list over 'glyf': %d glyphs after" %
1922 len(self.glyphs))
1923 self.log.glyphs(self.glyphs, font=font)
1924 self.log.lapse("close glyph list over 'glyf'")
1925 self.glyphs_glyfed = self.glyphs.copy()
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001926
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001927 self.glyphs_all = self.glyphs.copy()
Behdad Esfahbod8c8ff452013-07-31 19:47:37 -04001928
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001929 self.log("Retaining %d glyphs: " % len(self.glyphs_all))
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001930
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04001931 def _subset_glyphs(self, font):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001932 for tag in font.keys():
1933 if tag == 'GlyphOrder': continue
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001934 clazz = ttLib.getTableClass(tag)
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001935
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001936 if tag in self.options.no_subset_tables:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001937 self.log(tag, "subsetting not needed")
1938 elif hasattr(clazz, 'subset_glyphs'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001939 table = font[tag]
1940 self.glyphs = self.glyphs_all
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001941 retain = table.subset_glyphs(self)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001942 self.glyphs = self.glyphs_all
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001943 self.log.lapse("subset '%s'" % tag)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001944 if not retain:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001945 self.log(tag, "subsetted to empty; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001946 del font[tag]
1947 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001948 self.log(tag, "subsetted")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001949 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001950 self.log(tag, "NOT subset; don't know how to subset; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001951 del font[tag]
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001952
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001953 glyphOrder = font.getGlyphOrder()
1954 glyphOrder = [g for g in glyphOrder if g in self.glyphs_all]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001955 font.setGlyphOrder(glyphOrder)
1956 font._buildReverseGlyphOrderDict()
1957 self.log.lapse("subset GlyphOrder")
Behdad Esfahbod2fb90e22013-07-31 20:04:08 -04001958
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04001959 def _prune_post_subset(self, font):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001960 for tag in font.keys():
1961 if tag == 'GlyphOrder': continue
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04001962 clazz = ttLib.getTableClass(tag)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001963 if hasattr(clazz, 'prune_post_subset'):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001964 table = font[tag]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001965 retain = table.prune_post_subset(self.options)
1966 self.log.lapse("prune '%s'" % tag)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001967 if not retain:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001968 self.log(tag, "pruned to empty; dropped")
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001969 del font[tag]
1970 else:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001971 self.log(tag, "pruned")
Behdad Esfahbod063a2db2013-07-31 15:22:02 -04001972
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001973 def subset(self, font):
Behdad Esfahbod756af492013-08-01 12:05:26 -04001974
Behdad Esfahbodb7f460b2013-08-13 20:48:33 -04001975 self._prune_pre_subset(font)
1976 self._closure_glyphs(font)
1977 self._subset_glyphs(font)
1978 self._prune_post_subset(font)
Behdad Esfahbod98259f22013-07-31 20:16:24 -04001979
Behdad Esfahbod756af492013-08-01 12:05:26 -04001980
Behdad Esfahbod3d4c4712013-08-13 20:10:17 -04001981class Logger(object):
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001982
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001983 def __init__(self, verbose=False, xml=False, timing=False):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001984 self.verbose = verbose
1985 self.xml = xml
1986 self.timing = timing
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001987 self.last_time = self.start_time = time.time()
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001988
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001989 def parse_opts(self, argv):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001990 argv = argv[:]
1991 for v in ['verbose', 'xml', 'timing']:
1992 if "--"+v in argv:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001993 setattr(self, v, True)
1994 argv.remove("--"+v)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001995 return argv
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04001996
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04001997 def __call__(self, *things):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04001998 if not self.verbose:
1999 return
Behdad Esfahbod4cd467c2013-11-27 04:57:06 -05002000 print(' '.join(str(x) for x in things))
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04002001
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002002 def lapse(self, *things):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002003 if not self.timing:
2004 return
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002005 new_time = time.time()
Behdad Esfahbod4cd467c2013-11-27 04:57:06 -05002006 print("Took %0.3fs to %s" %(new_time - self.last_time,
2007 ' '.join(str(x) for x in things)))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002008 self.last_time = new_time
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04002009
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04002010 def glyphs(self, glyphs, font=None):
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002011 self("Names: ", sorted(glyphs))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002012 if font:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002013 reverseGlyphMap = font.getReverseGlyphMap()
2014 self("Gids : ", sorted(reverseGlyphMap[g] for g in glyphs))
Behdad Esfahbodf5497842013-08-08 21:57:02 -04002015
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002016 def font(self, font, file=sys.stdout):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002017 if not self.xml:
2018 return
Behdad Esfahbod28fc4982013-09-18 19:01:16 -04002019 from fontTools.misc import xmlWriter
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002020 writer = xmlWriter.XMLWriter(file)
Behdad Esfahbod45a84602013-08-19 14:44:49 -04002021 font.disassembleInstructions = False # Work around ttLib bug
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002022 for tag in font.keys():
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002023 writer.begintag(tag)
2024 writer.newline()
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002025 font[tag].toXML(writer, font)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002026 writer.endtag(tag)
2027 writer.newline()
Behdad Esfahboddf3d7572013-07-31 15:03:43 -04002028
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002029
Behdad Esfahbod85da2682013-08-15 12:17:21 -04002030def load_font(fontFile,
Behdad Esfahbodadc47fd2013-08-15 18:29:25 -04002031 options,
Behdad Esfahbod85da2682013-08-15 12:17:21 -04002032 checkChecksums=False,
Behdad Esfahbod85da2682013-08-15 12:17:21 -04002033 dontLoadGlyphNames=False):
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002034
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04002035 font = ttLib.TTFont(fontFile,
Behdad Esfahbod45a84602013-08-19 14:44:49 -04002036 checkChecksums=checkChecksums,
2037 recalcBBoxes=options.recalc_bounds)
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002038
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002039 # Hack:
2040 #
2041 # If we don't need glyph names, change 'post' class to not try to
2042 # load them. It avoid lots of headache with broken fonts as well
2043 # as loading time.
2044 #
2045 # Ideally ttLib should provide a way to ask it to skip loading
2046 # glyph names. But it currently doesn't provide such a thing.
2047 #
Behdad Esfahbod85da2682013-08-15 12:17:21 -04002048 if dontLoadGlyphNames:
Behdad Esfahbod46d260f2013-09-19 20:36:49 -04002049 post = ttLib.getTableClass('post')
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002050 saved = post.decode_format_2_0
2051 post.decode_format_2_0 = post.decode_format_3_0
2052 f = font['post']
2053 if f.formatType == 2.0:
2054 f.formatType = 3.0
2055 post.decode_format_2_0 = saved
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002056
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002057 return font
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002058
Behdad Esfahbode911de12013-08-16 12:42:34 -04002059def save_font(font, outfile, options):
Behdad Esfahbodc6c3bb82013-08-15 17:46:20 -04002060 if options.flavor and not hasattr(font, 'flavor'):
2061 raise Exception("fonttools version does not support flavors.")
2062 font.flavor = options.flavor
Behdad Esfahbode911de12013-08-16 12:42:34 -04002063 font.save(outfile, reorderTables=options.canonical_order)
Behdad Esfahbod41de4cc2013-08-15 12:09:55 -04002064
Behdad Esfahbodb69400f2013-08-29 18:40:53 -04002065def main(args):
Behdad Esfahbod610b0552013-07-23 14:52:18 -04002066
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002067 log = Logger()
2068 args = log.parse_opts(args)
Behdad Esfahbod4ae81712013-07-22 11:57:13 -04002069
Behdad Esfahbod80c8a652013-08-14 12:55:42 -04002070 options = Options()
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002071 args = options.parse_opts(args, ignore_unknown=['text'])
Behdad Esfahbod97e17b82013-07-31 15:59:21 -04002072
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002073 if len(args) < 2:
Behdad Esfahbodcfeafd72013-11-27 17:27:35 -05002074 print("usage: pyftsubset font-file glyph... [--text=ABC]... [--option=value]...", file=sys.stderr)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002075 sys.exit(1)
Behdad Esfahbod02b92062013-07-21 18:40:59 -04002076
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002077 fontfile = args[0]
2078 args = args[1:]
Behdad Esfahbod02b92062013-07-21 18:40:59 -04002079
Behdad Esfahbod85da2682013-08-15 12:17:21 -04002080 dontLoadGlyphNames =(not options.glyph_names and
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002081 all(any(g.startswith(p)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002082 for p in ['gid', 'glyph', 'uni', 'U+'])
2083 for g in args))
Behdad Esfahbodf6b668e2013-08-13 12:20:59 -04002084
Behdad Esfahbodadc47fd2013-08-15 18:29:25 -04002085 font = load_font(fontfile, options, dontLoadGlyphNames=dontLoadGlyphNames)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002086 subsetter = Subsetter(options=options, log=log)
2087 log.lapse("load font")
Behdad Esfahbod02b92062013-07-21 18:40:59 -04002088
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002089 names = font.getGlyphNames()
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002090 log.lapse("loading glyph names")
Behdad Esfahbode7f5a892013-07-31 19:58:59 -04002091
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002092 glyphs = []
2093 unicodes = []
2094 text = ""
2095 for g in args:
Behdad Esfahbod2be33d92013-09-10 19:28:59 -04002096 if g == '*':
2097 glyphs.extend(font.getGlyphOrder())
2098 continue
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002099 if g in names:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002100 glyphs.append(g)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002101 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002102 if g.startswith('--text='):
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002103 text += g[7:]
2104 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002105 if g.startswith('uni') or g.startswith('U+'):
2106 if g.startswith('uni') and len(g) > 3:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002107 g = g[3:]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002108 elif g.startswith('U+') and len(g) > 2:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002109 g = g[2:]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002110 u = int(g, 16)
2111 unicodes.append(u)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002112 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002113 if g.startswith('gid') or g.startswith('glyph'):
2114 if g.startswith('gid') and len(g) > 3:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002115 g = g[3:]
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002116 elif g.startswith('glyph') and len(g) > 5:
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002117 g = g[5:]
2118 try:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002119 glyphs.append(font.getGlyphName(int(g), requireReal=1))
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002120 except ValueError:
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002121 raise Exception("Invalid glyph identifier: %s" % g)
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002122 continue
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002123 raise Exception("Invalid glyph identifier: %s" % g)
2124 log.lapse("compile glyph list")
2125 log("Unicodes:", unicodes)
2126 log("Glyphs:", glyphs)
Behdad Esfahbod6df089a2013-07-31 19:27:14 -04002127
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002128 subsetter.populate(glyphs=glyphs, unicodes=unicodes, text=text)
2129 subsetter.subset(font)
Behdad Esfahbodd1d41bc2013-07-21 23:15:32 -04002130
Behdad Esfahbod34426c12013-08-14 18:30:09 -04002131 outfile = fontfile + '.subset'
2132
Behdad Esfahbodc6c3bb82013-08-15 17:46:20 -04002133 save_font (font, outfile, options)
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002134 log.lapse("compile and save font")
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04002135
Behdad Esfahbod9e856ea2013-08-13 19:50:38 -04002136 log.last_time = log.start_time
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002137 log.lapse("make one with everything(TOTAL TIME)")
Behdad Esfahbodde71dca2013-07-24 12:40:54 -04002138
Behdad Esfahbod34426c12013-08-14 18:30:09 -04002139 if log.verbose:
2140 import os
2141 log("Input font: %d bytes" % os.path.getsize(fontfile))
2142 log("Subset font: %d bytes" % os.path.getsize(outfile))
2143
Behdad Esfahbod77a2b282013-08-13 19:53:30 -04002144 log.font(font)
Behdad Esfahbod8c486d82013-07-24 13:34:47 -04002145
Behdad Esfahbodc56bf482013-08-13 20:13:33 -04002146 font.close()
2147
Behdad Esfahbod39a39ac2013-08-22 18:10:17 -04002148
2149__all__ = [
Behdad Esfahbodb69400f2013-08-29 18:40:53 -04002150 'Options',
2151 'Subsetter',
2152 'Logger',
2153 'load_font',
2154 'save_font',
2155 'main'
Behdad Esfahbod39a39ac2013-08-22 18:10:17 -04002156]
2157
Behdad Esfahbod8c486d82013-07-24 13:34:47 -04002158if __name__ == '__main__':
Behdad Esfahbodb69400f2013-08-29 18:40:53 -04002159 main(sys.argv[1:])