subset.py revision 92af6a58b3cdcbe64c7c66f64e3485bc6f118278
1# Copyright 2013 Google, Inc. All Rights Reserved.
2#
3# Google Author(s): Behdad Esfahbod
4
5"""Python OpenType Layout Subsetter.
6
7Later grown into full OpenType subsetter, supporting all standard tables.
8"""
9
10from __future__ import print_function, division
11from fontTools.misc.py23 import *
12from fontTools import ttLib
13from fontTools.ttLib.tables import otTables
14from fontTools.misc import psCharStrings
15from fontTools.pens import basePen
16import sys
17import struct
18import time
19import array
20
21
22def _add_method(*clazzes):
23  """Returns a decorator function that adds a new method to one or
24  more classes."""
25  def wrapper(method):
26    for clazz in clazzes:
27      assert clazz.__name__ != 'DefaultTable', 'Oops, table class not found.'
28      assert not hasattr(clazz, method.__name__), \
29          "Oops, class '%s' has method '%s'." % (clazz.__name__,
30                                                 method.__name__)
31      setattr(clazz, method.__name__, method)
32    return None
33  return wrapper
34
35def _uniq_sort(l):
36  return sorted(set(l))
37
38def _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
44
45@_add_method(otTables.Coverage)
46def intersect(self, glyphs):
47  "Returns ascending list of matching coverage values."
48  return [i for i,g in enumerate(self.glyphs) if g in glyphs]
49
50@_add_method(otTables.Coverage)
51def intersect_glyphs(self, glyphs):
52  "Returns set of intersecting glyphs."
53  return set(g for g in self.glyphs if g in glyphs)
54
55@_add_method(otTables.Coverage)
56def subset(self, glyphs):
57  "Returns ascending list of remaining coverage values."
58  indices = self.intersect(glyphs)
59  self.glyphs = [g for g in self.glyphs if g in glyphs]
60  return indices
61
62@_add_method(otTables.Coverage)
63def remap(self, coverage_map):
64  "Remaps coverage."
65  self.glyphs = [self.glyphs[i] for i in coverage_map]
66
67@_add_method(otTables.ClassDef)
68def intersect(self, glyphs):
69  "Returns ascending list of matching class values."
70  return _uniq_sort(
71     ([0] if any(g not in self.classDefs for g in glyphs) else []) +
72      [v for g,v in self.classDefs.items() if g in glyphs])
73
74@_add_method(otTables.ClassDef)
75def intersect_class(self, glyphs, klass):
76  "Returns set of glyphs matching class."
77  if klass == 0:
78    return set(g for g in glyphs if g not in self.classDefs)
79  return set(g for g,v in self.classDefs.items()
80              if v == klass and g in glyphs)
81
82@_add_method(otTables.ClassDef)
83def subset(self, glyphs, remap=False):
84  "Returns ascending list of remaining classes."
85  self.classDefs = dict((g,v) for g,v in self.classDefs.items() if g in glyphs)
86  # 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.
88  indices = _uniq_sort(
89     ([0] if any(g not in self.classDefs for g in glyphs) else []) +
90      list(self.classDefs.values()))
91  if remap:
92    self.remap(indices)
93  return indices
94
95@_add_method(otTables.ClassDef)
96def remap(self, class_map):
97  "Remaps classes."
98  self.classDefs = dict((g,class_map.index(v))
99                         for g,v in self.classDefs.items())
100
101@_add_method(otTables.SingleSubst)
102def closure_glyphs(self, s, cur_glyphs=None):
103  if cur_glyphs is None: cur_glyphs = s.glyphs
104  if self.Format in [1, 2]:
105    s.glyphs.update(v for g,v in self.mapping.items() if g in cur_glyphs)
106  else:
107    assert 0, "unknown format: %s" % self.Format
108
109@_add_method(otTables.SingleSubst)
110def subset_glyphs(self, s):
111  if self.Format in [1, 2]:
112    self.mapping = dict((g,v) for g,v in self.mapping.items()
113                        if g in s.glyphs and v in s.glyphs)
114    return bool(self.mapping)
115  else:
116    assert 0, "unknown format: %s" % self.Format
117
118@_add_method(otTables.MultipleSubst)
119def closure_glyphs(self, s, cur_glyphs=None):
120  if cur_glyphs is None: cur_glyphs = s.glyphs
121  if self.Format == 1:
122    indices = self.Coverage.intersect(cur_glyphs)
123    _set_update(s.glyphs, *(self.Sequence[i].Substitute for i in indices))
124  else:
125    assert 0, "unknown format: %s" % self.Format
126
127@_add_method(otTables.MultipleSubst)
128def subset_glyphs(self, s):
129  if self.Format == 1:
130    indices = self.Coverage.subset(s.glyphs)
131    self.Sequence = [self.Sequence[i] for i in indices]
132    # Now drop rules generating glyphs we don't want
133    indices = [i for i,seq in enumerate(self.Sequence)
134         if all(sub in s.glyphs for sub in seq.Substitute)]
135    self.Sequence = [self.Sequence[i] for i in indices]
136    self.Coverage.remap(indices)
137    self.SequenceCount = len(self.Sequence)
138    return bool(self.SequenceCount)
139  else:
140    assert 0, "unknown format: %s" % self.Format
141
142@_add_method(otTables.AlternateSubst)
143def closure_glyphs(self, s, cur_glyphs=None):
144  if cur_glyphs is None: cur_glyphs = s.glyphs
145  if self.Format == 1:
146    _set_update(s.glyphs, *(vlist for g,vlist in self.alternates.items()
147                            if g in cur_glyphs))
148  else:
149    assert 0, "unknown format: %s" % self.Format
150
151@_add_method(otTables.AlternateSubst)
152def subset_glyphs(self, s):
153  if self.Format == 1:
154    self.alternates = dict((g,vlist)
155                           for g,vlist in self.alternates.items()
156                           if g in s.glyphs and
157                              all(v in s.glyphs for v in vlist))
158    return bool(self.alternates)
159  else:
160    assert 0, "unknown format: %s" % self.Format
161
162@_add_method(otTables.LigatureSubst)
163def closure_glyphs(self, s, cur_glyphs=None):
164  if cur_glyphs is None: cur_glyphs = s.glyphs
165  if self.Format == 1:
166    _set_update(s.glyphs, *([seq.LigGlyph for seq in seqs
167                             if all(c in s.glyphs for c in seq.Component)]
168                            for g,seqs in self.ligatures.items()
169                            if g in cur_glyphs))
170  else:
171    assert 0, "unknown format: %s" % self.Format
172
173@_add_method(otTables.LigatureSubst)
174def subset_glyphs(self, s):
175  if self.Format == 1:
176    self.ligatures = dict((g,v) for g,v in self.ligatures.items()
177                          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)])
181                           for g,seqs in self.ligatures.items())
182    self.ligatures = dict((g,v) for g,v in self.ligatures.items() if v)
183    return bool(self.ligatures)
184  else:
185    assert 0, "unknown format: %s" % self.Format
186
187@_add_method(otTables.ReverseChainSingleSubst)
188def closure_glyphs(self, s, cur_glyphs=None):
189  if cur_glyphs is None: cur_glyphs = s.glyphs
190  if self.Format == 1:
191    indices = self.Coverage.intersect(cur_glyphs)
192    if(not indices or
193        not all(c.intersect(s.glyphs)
194                 for c in self.LookAheadCoverage + self.BacktrackCoverage)):
195      return
196    s.glyphs.update(self.Substitute[i] for i in indices)
197  else:
198    assert 0, "unknown format: %s" % self.Format
199
200@_add_method(otTables.ReverseChainSingleSubst)
201def subset_glyphs(self, s):
202  if self.Format == 1:
203    indices = self.Coverage.subset(s.glyphs)
204    self.Substitute = [self.Substitute[i] for i in indices]
205    # Now drop rules generating glyphs we don't want
206    indices = [i for i,sub in enumerate(self.Substitute)
207         if sub in s.glyphs]
208    self.Substitute = [self.Substitute[i] for i in indices]
209    self.Coverage.remap(indices)
210    self.GlyphCount = len(self.Substitute)
211    return bool(self.GlyphCount and
212                 all(c.subset(s.glyphs)
213                      for c in self.LookAheadCoverage+self.BacktrackCoverage))
214  else:
215    assert 0, "unknown format: %s" % self.Format
216
217@_add_method(otTables.SinglePos)
218def subset_glyphs(self, s):
219  if self.Format == 1:
220    return len(self.Coverage.subset(s.glyphs))
221  elif self.Format == 2:
222    indices = self.Coverage.subset(s.glyphs)
223    self.Value = [self.Value[i] for i in indices]
224    self.ValueCount = len(self.Value)
225    return bool(self.ValueCount)
226  else:
227    assert 0, "unknown format: %s" % self.Format
228
229@_add_method(otTables.SinglePos)
230def prune_post_subset(self, options):
231  if not options.hinting:
232    # Drop device tables
233    self.ValueFormat &= ~0x00F0
234  return True
235
236@_add_method(otTables.PairPos)
237def subset_glyphs(self, s):
238  if self.Format == 1:
239    indices = self.Coverage.subset(s.glyphs)
240    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]
244      p.PairValueCount = len(p.PairValueRecord)
245    self.PairSet = [p for p in self.PairSet if p.PairValueCount]
246    self.PairSetCount = len(self.PairSet)
247    return bool(self.PairSetCount)
248  elif self.Format == 2:
249    class1_map = self.ClassDef1.subset(s.glyphs, remap=True)
250    class2_map = self.ClassDef2.subset(s.glyphs, remap=True)
251    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]
254    self.Class1Count = len(class1_map)
255    self.Class2Count = len(class2_map)
256    return bool(self.Class1Count and
257                 self.Class2Count and
258                 self.Coverage.subset(s.glyphs))
259  else:
260    assert 0, "unknown format: %s" % self.Format
261
262@_add_method(otTables.PairPos)
263def 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
270@_add_method(otTables.CursivePos)
271def subset_glyphs(self, s):
272  if self.Format == 1:
273    indices = self.Coverage.subset(s.glyphs)
274    self.EntryExitRecord = [self.EntryExitRecord[i] for i in indices]
275    self.EntryExitCount = len(self.EntryExitRecord)
276    return bool(self.EntryExitCount)
277  else:
278    assert 0, "unknown format: %s" % self.Format
279
280@_add_method(otTables.Anchor)
281def prune_hints(self):
282  # Drop device tables / contour anchor point
283  self.Format = 1
284
285@_add_method(otTables.CursivePos)
286def 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
293@_add_method(otTables.MarkBasePos)
294def subset_glyphs(self, s):
295  if self.Format == 1:
296    mark_indices = self.MarkCoverage.subset(s.glyphs)
297    self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i]
298                                 for i in mark_indices]
299    self.MarkArray.MarkCount = len(self.MarkArray.MarkRecord)
300    base_indices = self.BaseCoverage.subset(s.glyphs)
301    self.BaseArray.BaseRecord = [self.BaseArray.BaseRecord[i]
302                                 for i in base_indices]
303    self.BaseArray.BaseCount = len(self.BaseArray.BaseRecord)
304    # Prune empty classes
305    class_indices = _uniq_sort(v.Class for v in self.MarkArray.MarkRecord)
306    self.ClassCount = len(class_indices)
307    for m in self.MarkArray.MarkRecord:
308      m.Class = class_indices.index(m.Class)
309    for b in self.BaseArray.BaseRecord:
310      b.BaseAnchor = [b.BaseAnchor[i] for i in class_indices]
311    return bool(self.ClassCount and
312                 self.MarkArray.MarkCount and
313                 self.BaseArray.BaseCount)
314  else:
315    assert 0, "unknown format: %s" % self.Format
316
317@_add_method(otTables.MarkBasePos)
318def prune_post_subset(self, options):
319    if not options.hinting:
320      for m in self.MarkArray.MarkRecord:
321        if m.MarkAnchor:
322          m.MarkAnchor.prune_hints()
323      for b in self.BaseArray.BaseRecord:
324        for a in b.BaseAnchor:
325          if a:
326            a.prune_hints()
327    return True
328
329@_add_method(otTables.MarkLigPos)
330def subset_glyphs(self, s):
331  if self.Format == 1:
332    mark_indices = self.MarkCoverage.subset(s.glyphs)
333    self.MarkArray.MarkRecord = [self.MarkArray.MarkRecord[i]
334                                 for i in mark_indices]
335    self.MarkArray.MarkCount = len(self.MarkArray.MarkRecord)
336    ligature_indices = self.LigatureCoverage.subset(s.glyphs)
337    self.LigatureArray.LigatureAttach = [self.LigatureArray.LigatureAttach[i]
338                                         for i in ligature_indices]
339    self.LigatureArray.LigatureCount = len(self.LigatureArray.LigatureAttach)
340    # Prune empty classes
341    class_indices = _uniq_sort(v.Class for v in self.MarkArray.MarkRecord)
342    self.ClassCount = len(class_indices)
343    for m in self.MarkArray.MarkRecord:
344      m.Class = class_indices.index(m.Class)
345    for l in self.LigatureArray.LigatureAttach:
346      for c in l.ComponentRecord:
347        c.LigatureAnchor = [c.LigatureAnchor[i] for i in class_indices]
348    return bool(self.ClassCount and
349                 self.MarkArray.MarkCount and
350                 self.LigatureArray.LigatureCount)
351  else:
352    assert 0, "unknown format: %s" % self.Format
353
354@_add_method(otTables.MarkLigPos)
355def prune_post_subset(self, options):
356    if not options.hinting:
357      for m in self.MarkArray.MarkRecord:
358        if m.MarkAnchor:
359          m.MarkAnchor.prune_hints()
360      for l in self.LigatureArray.LigatureAttach:
361        for c in l.ComponentRecord:
362          for a in c.LigatureAnchor:
363            if a:
364              a.prune_hints()
365    return True
366
367@_add_method(otTables.MarkMarkPos)
368def subset_glyphs(self, s):
369  if self.Format == 1:
370    mark1_indices = self.Mark1Coverage.subset(s.glyphs)
371    self.Mark1Array.MarkRecord = [self.Mark1Array.MarkRecord[i]
372                                  for i in mark1_indices]
373    self.Mark1Array.MarkCount = len(self.Mark1Array.MarkRecord)
374    mark2_indices = self.Mark2Coverage.subset(s.glyphs)
375    self.Mark2Array.Mark2Record = [self.Mark2Array.Mark2Record[i]
376                                   for i in mark2_indices]
377    self.Mark2Array.MarkCount = len(self.Mark2Array.Mark2Record)
378    # Prune empty classes
379    class_indices = _uniq_sort(v.Class for v in self.Mark1Array.MarkRecord)
380    self.ClassCount = len(class_indices)
381    for m in self.Mark1Array.MarkRecord:
382      m.Class = class_indices.index(m.Class)
383    for b in self.Mark2Array.Mark2Record:
384      b.Mark2Anchor = [b.Mark2Anchor[i] for i in class_indices]
385    return bool(self.ClassCount and
386                 self.Mark1Array.MarkCount and
387                 self.Mark2Array.MarkCount)
388  else:
389    assert 0, "unknown format: %s" % self.Format
390
391@_add_method(otTables.MarkMarkPos)
392def 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:
396        if m.MarkAnchor:
397          m.MarkAnchor.prune_hints()
398      for b in self.Mark2Array.Mark2Record:
399        for m in b.Mark2Anchor:
400          if m:
401            m.prune_hints()
402    return True
403
404@_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)
415def subset_lookups(self, lookup_indices):
416  pass
417
418@_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)
429def collect_lookups(self):
430  return []
431
432@_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)
447def prune_pre_subset(self, options):
448  return True
449
450@_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)
459def prune_post_subset(self, options):
460  return True
461
462@_add_method(otTables.SingleSubst,
463             otTables.AlternateSubst,
464             otTables.ReverseChainSingleSubst)
465def may_have_non_1to1(self):
466  return False
467
468@_add_method(otTables.MultipleSubst,
469             otTables.LigatureSubst,
470             otTables.ContextSubst,
471             otTables.ChainContextSubst)
472def may_have_non_1to1(self):
473  return True
474
475@_add_method(otTables.ContextSubst,
476             otTables.ChainContextSubst,
477             otTables.ContextPos,
478             otTables.ChainContextPos)
479def __classify_context(self):
480
481  class ContextHelper(object):
482    def __init__(self, klass, Format):
483      if klass.__name__.endswith('Subst'):
484        Typ = 'Sub'
485        Type = 'Subst'
486      else:
487        Typ = 'Pos'
488        Type = 'Pos'
489      if klass.__name__.startswith('Chain'):
490        Chain = 'Chain'
491      else:
492        Chain = ''
493      ChainTyp = Chain+Typ
494
495      self.Typ = Typ
496      self.Type = Type
497      self.Chain = Chain
498      self.ChainTyp = ChainTyp
499
500      self.LookupRecord = Type+'LookupRecord'
501
502      if Format == 1:
503        Coverage = lambda r: r.Coverage
504        ChainCoverage = lambda r: r.Coverage
505        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)
509        SetRuleData = None
510        ChainSetRuleData = None
511      elif Format == 2:
512        Coverage = lambda r: r.Coverage
513        ChainCoverage = lambda r: r.Coverage
514        ContextData = lambda r:(r.ClassDef,)
515        ChainContextData = lambda r:(r.LookAheadClassDef,
516                                      r.InputClassDef,
517                                      r.BacktrackClassDef)
518        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
522      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
528        ChainRuleData = lambda r:(r.LookAheadCoverage +
529                                   r.InputCoverage +
530                                   r.BacktrackCoverage)
531        SetRuleData = None
532        ChainSetRuleData = None
533      else:
534        assert 0, "unknown format: %s" % Format
535
536      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
546
547      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'
558        self.Intersect = lambda glyphs, c, r: c.intersect_class(glyphs, r)
559
560        self.ClassDef = 'InputClassDef' if Chain else 'ClassDef'
561        self.ClassDefIndex = 1 if Chain else 0
562        self.Input = 'Input' if Chain else 'Class'
563
564  if self.Format not in [1, 2, 3]:
565    return None  # Don't shoot the messenger; let it go
566  if not hasattr(self.__class__, "__ContextHelpers"):
567    self.__class__.__ContextHelpers = {}
568  if self.Format not in self.__class__.__ContextHelpers:
569    helper = ContextHelper(self.__class__, self.Format)
570    self.__class__.__ContextHelpers[self.Format] = helper
571  return self.__class__.__ContextHelpers[self.Format]
572
573@_add_method(otTables.ContextSubst,
574             otTables.ChainContextSubst)
575def closure_glyphs(self, s, cur_glyphs=None):
576  if cur_glyphs is None: cur_glyphs = s.glyphs
577  c = self.__classify_context()
578
579  indices = c.Coverage(self).intersect(s.glyphs)
580  if not indices:
581    return []
582  cur_glyphs = c.Coverage(self).intersect_glyphs(s.glyphs);
583
584  if self.Format == 1:
585    ContextData = c.ContextData(self)
586    rss = getattr(self, c.RuleSet)
587    rssCount = getattr(self, c.RuleSetCount)
588    for i in indices:
589      if i >= rssCount or not rss[i]: continue
590      for r in getattr(rss[i], c.Rule):
591        if not r: continue
592        if all(all(c.Intersect(s.glyphs, cd, k) for k in klist)
593          for cd,klist in zip(ContextData, c.RuleData(r))):
594          chaos = False
595          for ll in getattr(r, c.LookupRecord):
596            if not ll: continue
597            seqi = ll.SequenceIndex
598            if chaos:
599              pos_glyphs = s.glyphs
600            else:
601              if seqi == 0:
602                pos_glyphs = set([c.Coverage(self).glyphs[i]])
603              else:
604                pos_glyphs = set([r.Input[seqi - 1]])
605            lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
606            chaos = chaos or lookup.may_have_non_1to1()
607            lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
608  elif self.Format == 2:
609    ClassDef = getattr(self, c.ClassDef)
610    indices = ClassDef.intersect(cur_glyphs)
611    ContextData = c.ContextData(self)
612    rss = getattr(self, c.RuleSet)
613    rssCount = getattr(self, c.RuleSetCount)
614    for i in indices:
615      if i >= rssCount or not rss[i]: continue
616      for r in getattr(rss[i], c.Rule):
617        if not r: continue
618        if all(all(c.Intersect(s.glyphs, cd, k) for k in klist)
619          for cd,klist in zip(ContextData, c.RuleData(r))):
620          chaos = False
621          for ll in getattr(r, c.LookupRecord):
622            if not ll: continue
623            seqi = ll.SequenceIndex
624            if chaos:
625              pos_glyphs = s.glyphs
626            else:
627              if seqi == 0:
628                pos_glyphs = ClassDef.intersect_class(cur_glyphs, i)
629              else:
630                pos_glyphs = ClassDef.intersect_class(s.glyphs,
631                                                      getattr(r, c.Input)[seqi - 1])
632            lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
633            chaos = chaos or lookup.may_have_non_1to1()
634            lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
635  elif self.Format == 3:
636    if not all(x.intersect(s.glyphs) for x in c.RuleData(self)):
637      return []
638    r = self
639    chaos = False
640    for ll in getattr(r, c.LookupRecord):
641      if not ll: continue
642      seqi = ll.SequenceIndex
643      if chaos:
644        pos_glyphs = s.glyphs
645      else:
646        if seqi == 0:
647          pos_glyphs = cur_glyphs
648        else:
649          pos_glyphs = r.InputCoverage[seqi].intersect_glyphs(s.glyphs)
650      lookup = s.table.LookupList.Lookup[ll.LookupListIndex]
651      chaos = chaos or lookup.may_have_non_1to1()
652      lookup.closure_glyphs(s, cur_glyphs=pos_glyphs)
653  else:
654    assert 0, "unknown format: %s" % self.Format
655
656@_add_method(otTables.ContextSubst,
657             otTables.ContextPos,
658             otTables.ChainContextSubst,
659             otTables.ChainContextPos)
660def subset_glyphs(self, s):
661  c = self.__classify_context()
662
663  if self.Format == 1:
664    indices = self.Coverage.subset(s.glyphs)
665    rss = getattr(self, c.RuleSet)
666    rss = [rss[i] for i in indices]
667    for rs in rss:
668      if not rs: continue
669      ss = getattr(rs, c.Rule)
670      ss = [r for r in ss
671            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))
675    # Prune empty subrulesets
676    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)
680  elif self.Format == 2:
681    if not self.Coverage.subset(s.glyphs):
682      return False
683    ContextData = c.ContextData(self)
684    klass_maps = [x.subset(s.glyphs, remap=True) for x in ContextData]
685
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] is None:
696      del rss[-1]
697
698    for rs in rss:
699      if not rs: continue
700      ss = getattr(rs, c.Rule)
701      ss = [r for r in ss
702            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))
706
707      # Remap rule classes
708      for r in ss:
709        c.SetRuleData(r, [[klass_map.index(k) for k in klist]
710               for klass_map,klist in zip(klass_maps, c.RuleData(r))])
711    return bool(rss)
712  elif self.Format == 3:
713    return all(x.subset(s.glyphs) for x in c.RuleData(self))
714  else:
715    assert 0, "unknown format: %s" % self.Format
716
717@_add_method(otTables.ContextSubst,
718             otTables.ChainContextSubst,
719             otTables.ContextPos,
720             otTables.ChainContextPos)
721def subset_lookups(self, lookup_indices):
722  c = self.__classify_context()
723
724  if self.Format in [1, 2]:
725    for rs in getattr(self, c.RuleSet):
726      if not rs: continue
727      for r in getattr(rs, c.Rule):
728        if not r: continue
729        setattr(r, c.LookupRecord,
730                 [ll for ll in getattr(r, c.LookupRecord)
731                  if ll and ll.LookupListIndex in lookup_indices])
732        for ll in getattr(r, c.LookupRecord):
733          if not ll: continue
734          ll.LookupListIndex = lookup_indices.index(ll.LookupListIndex)
735  elif self.Format == 3:
736    setattr(self, c.LookupRecord,
737             [ll for ll in getattr(self, c.LookupRecord)
738              if ll and ll.LookupListIndex in lookup_indices])
739    for ll in getattr(self, c.LookupRecord):
740      if not ll: continue
741      ll.LookupListIndex = lookup_indices.index(ll.LookupListIndex)
742  else:
743    assert 0, "unknown format: %s" % self.Format
744
745@_add_method(otTables.ContextSubst,
746             otTables.ChainContextSubst,
747             otTables.ContextPos,
748             otTables.ChainContextPos)
749def collect_lookups(self):
750  c = self.__classify_context()
751
752  if self.Format in [1, 2]:
753    return [ll.LookupListIndex
754      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]
757  elif self.Format == 3:
758    return [ll.LookupListIndex
759      for ll in getattr(self, c.LookupRecord) if ll]
760  else:
761    assert 0, "unknown format: %s" % self.Format
762
763@_add_method(otTables.ExtensionSubst)
764def closure_glyphs(self, s, cur_glyphs=None):
765  if self.Format == 1:
766    self.ExtSubTable.closure_glyphs(s, cur_glyphs)
767  else:
768    assert 0, "unknown format: %s" % self.Format
769
770@_add_method(otTables.ExtensionSubst)
771def may_have_non_1to1(self):
772  if self.Format == 1:
773    return self.ExtSubTable.may_have_non_1to1()
774  else:
775    assert 0, "unknown format: %s" % self.Format
776
777@_add_method(otTables.ExtensionSubst,
778             otTables.ExtensionPos)
779def 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
785@_add_method(otTables.ExtensionSubst,
786             otTables.ExtensionPos)
787def subset_glyphs(self, s):
788  if self.Format == 1:
789    return self.ExtSubTable.subset_glyphs(s)
790  else:
791    assert 0, "unknown format: %s" % self.Format
792
793@_add_method(otTables.ExtensionSubst,
794             otTables.ExtensionPos)
795def 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
801@_add_method(otTables.ExtensionSubst,
802             otTables.ExtensionPos)
803def subset_lookups(self, lookup_indices):
804  if self.Format == 1:
805    return self.ExtSubTable.subset_lookups(lookup_indices)
806  else:
807    assert 0, "unknown format: %s" % self.Format
808
809@_add_method(otTables.ExtensionSubst,
810             otTables.ExtensionPos)
811def collect_lookups(self):
812  if self.Format == 1:
813    return self.ExtSubTable.collect_lookups()
814  else:
815    assert 0, "unknown format: %s" % self.Format
816
817@_add_method(otTables.Lookup)
818def closure_glyphs(self, s, cur_glyphs=None):
819  for st in self.SubTable:
820    if not st: continue
821    st.closure_glyphs(s, cur_glyphs)
822
823@_add_method(otTables.Lookup)
824def 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
831@_add_method(otTables.Lookup)
832def 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)
836
837@_add_method(otTables.Lookup)
838def 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
845@_add_method(otTables.Lookup)
846def subset_lookups(self, lookup_indices):
847  for s in self.SubTable:
848    s.subset_lookups(lookup_indices)
849
850@_add_method(otTables.Lookup)
851def collect_lookups(self):
852  return _uniq_sort(sum((st.collect_lookups() for st in self.SubTable
853                         if st), []))
854
855@_add_method(otTables.Lookup)
856def may_have_non_1to1(self):
857  return any(st.may_have_non_1to1() for st in self.SubTable if st)
858
859@_add_method(otTables.LookupList)
860def 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
867@_add_method(otTables.LookupList)
868def subset_glyphs(self, s):
869  "Returns the indices of nonempty lookups."
870  return [i for i,l in enumerate(self.Lookup) if l and l.subset_glyphs(s)]
871
872@_add_method(otTables.LookupList)
873def 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
880@_add_method(otTables.LookupList)
881def subset_lookups(self, lookup_indices):
882  self.Lookup = [self.Lookup[i] for i in lookup_indices
883                 if i < self.LookupCount]
884  self.LookupCount = len(self.Lookup)
885  for l in self.Lookup:
886    l.subset_lookups(lookup_indices)
887
888@_add_method(otTables.LookupList)
889def closure_lookups(self, lookup_indices):
890  lookup_indices = _uniq_sort(lookup_indices)
891  recurse = lookup_indices
892  while True:
893    recurse_lookups = sum((self.Lookup[i].collect_lookups()
894                            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:
898      return _uniq_sort(lookup_indices)
899    recurse_lookups = _uniq_sort(recurse_lookups)
900    lookup_indices.extend(recurse_lookups)
901    recurse = recurse_lookups
902
903@_add_method(otTables.Feature)
904def subset_lookups(self, lookup_indices):
905  self.LookupListIndex = [l for l in self.LookupListIndex
906                          if l in lookup_indices]
907  # Now map them.
908  self.LookupListIndex = [lookup_indices.index(l)
909                          for l in self.LookupListIndex]
910  self.LookupCount = len(self.LookupListIndex)
911  return self.LookupCount or self.FeatureParams
912
913@_add_method(otTables.Feature)
914def collect_lookups(self):
915  return self.LookupListIndex[:]
916
917@_add_method(otTables.FeatureList)
918def subset_lookups(self, lookup_indices):
919  "Returns the indices of nonempty features."
920  feature_indices = [i for i,f in enumerate(self.FeatureRecord)
921                     if f.Feature.subset_lookups(lookup_indices)]
922  self.subset_features(feature_indices)
923  return feature_indices
924
925@_add_method(otTables.FeatureList)
926def collect_lookups(self, feature_indices):
927  return _uniq_sort(sum((self.FeatureRecord[i].Feature.collect_lookups()
928                         for i in feature_indices
929                          if i < self.FeatureCount), []))
930
931@_add_method(otTables.FeatureList)
932def subset_features(self, feature_indices):
933  self.FeatureRecord = [self.FeatureRecord[i] for i in feature_indices]
934  self.FeatureCount = len(self.FeatureRecord)
935  return bool(self.FeatureCount)
936
937@_add_method(otTables.DefaultLangSys,
938             otTables.LangSys)
939def subset_features(self, feature_indices):
940  if self.ReqFeatureIndex in feature_indices:
941    self.ReqFeatureIndex = feature_indices.index(self.ReqFeatureIndex)
942  else:
943    self.ReqFeatureIndex = 65535
944  self.FeatureIndex = [f for f in self.FeatureIndex if f in feature_indices]
945  # Now map them.
946  self.FeatureIndex = [feature_indices.index(f) for f in self.FeatureIndex
947                       if f in feature_indices]
948  self.FeatureCount = len(self.FeatureIndex)
949  return bool(self.FeatureCount or self.ReqFeatureIndex != 65535)
950
951@_add_method(otTables.DefaultLangSys,
952             otTables.LangSys)
953def collect_features(self):
954  feature_indices = self.FeatureIndex[:]
955  if self.ReqFeatureIndex != 65535:
956    feature_indices.append(self.ReqFeatureIndex)
957  return _uniq_sort(feature_indices)
958
959@_add_method(otTables.Script)
960def subset_features(self, feature_indices):
961  if(self.DefaultLangSys and
962      not self.DefaultLangSys.subset_features(feature_indices)):
963    self.DefaultLangSys = None
964  self.LangSysRecord = [l for l in self.LangSysRecord
965                        if l.LangSys.subset_features(feature_indices)]
966  self.LangSysCount = len(self.LangSysRecord)
967  return bool(self.LangSysCount or self.DefaultLangSys)
968
969@_add_method(otTables.Script)
970def collect_features(self):
971  feature_indices = [l.LangSys.collect_features() for l in self.LangSysRecord]
972  if self.DefaultLangSys:
973    feature_indices.append(self.DefaultLangSys.collect_features())
974  return _uniq_sort(sum(feature_indices, []))
975
976@_add_method(otTables.ScriptList)
977def subset_features(self, feature_indices):
978  self.ScriptRecord = [s for s in self.ScriptRecord
979                       if s.Script.subset_features(feature_indices)]
980  self.ScriptCount = len(self.ScriptRecord)
981  return bool(self.ScriptCount)
982
983@_add_method(otTables.ScriptList)
984def collect_features(self):
985  return _uniq_sort(sum((s.Script.collect_features()
986                         for s in self.ScriptRecord), []))
987
988@_add_method(ttLib.getTableClass('GSUB'))
989def closure_glyphs(self, s):
990  s.table = self.table
991  if self.table.ScriptList:
992    feature_indices = self.table.ScriptList.collect_features()
993  else:
994    feature_indices = []
995  if self.table.FeatureList:
996    lookup_indices = self.table.FeatureList.collect_lookups(feature_indices)
997  else:
998    lookup_indices = []
999  if self.table.LookupList:
1000    while True:
1001      orig_glyphs = s.glyphs.copy()
1002      for i in lookup_indices:
1003        if i >= self.table.LookupList.LookupCount: continue
1004        if not self.table.LookupList.Lookup[i]: continue
1005        self.table.LookupList.Lookup[i].closure_glyphs(s)
1006      if orig_glyphs == s.glyphs:
1007        break
1008  del s.table
1009
1010@_add_method(ttLib.getTableClass('GSUB'),
1011             ttLib.getTableClass('GPOS'))
1012def subset_glyphs(self, s):
1013  s.glyphs = s.glyphs_gsubed
1014  if self.table.LookupList:
1015    lookup_indices = self.table.LookupList.subset_glyphs(s)
1016  else:
1017    lookup_indices = []
1018  self.subset_lookups(lookup_indices)
1019  self.prune_lookups()
1020  return True
1021
1022@_add_method(ttLib.getTableClass('GSUB'),
1023             ttLib.getTableClass('GPOS'))
1024def subset_lookups(self, lookup_indices):
1025  """Retains specified lookups, then removes empty features, language
1026     systems, and scripts."""
1027  if self.table.LookupList:
1028    self.table.LookupList.subset_lookups(lookup_indices)
1029  if self.table.FeatureList:
1030    feature_indices = self.table.FeatureList.subset_lookups(lookup_indices)
1031  else:
1032    feature_indices = []
1033  if self.table.ScriptList:
1034    self.table.ScriptList.subset_features(feature_indices)
1035
1036@_add_method(ttLib.getTableClass('GSUB'),
1037             ttLib.getTableClass('GPOS'))
1038def prune_lookups(self):
1039  "Remove unreferenced lookups"
1040  if self.table.ScriptList:
1041    feature_indices = self.table.ScriptList.collect_features()
1042  else:
1043    feature_indices = []
1044  if self.table.FeatureList:
1045    lookup_indices = self.table.FeatureList.collect_lookups(feature_indices)
1046  else:
1047    lookup_indices = []
1048  if self.table.LookupList:
1049    lookup_indices = self.table.LookupList.closure_lookups(lookup_indices)
1050  else:
1051    lookup_indices = []
1052  self.subset_lookups(lookup_indices)
1053
1054@_add_method(ttLib.getTableClass('GSUB'),
1055             ttLib.getTableClass('GPOS'))
1056def subset_feature_tags(self, feature_tags):
1057  if self.table.FeatureList:
1058    feature_indices = [i for i,f in
1059                       enumerate(self.table.FeatureList.FeatureRecord)
1060                       if f.FeatureTag in feature_tags]
1061    self.table.FeatureList.subset_features(feature_indices)
1062  else:
1063    feature_indices = []
1064  if self.table.ScriptList:
1065    self.table.ScriptList.subset_features(feature_indices)
1066
1067@_add_method(ttLib.getTableClass('GSUB'),
1068             ttLib.getTableClass('GPOS'))
1069def prune_features(self):
1070  "Remove unreferenced featurs"
1071  if self.table.ScriptList:
1072    feature_indices = self.table.ScriptList.collect_features()
1073  else:
1074    feature_indices = []
1075  if self.table.FeatureList:
1076    self.table.FeatureList.subset_features(feature_indices)
1077  if self.table.ScriptList:
1078    self.table.ScriptList.subset_features(feature_indices)
1079
1080@_add_method(ttLib.getTableClass('GSUB'),
1081             ttLib.getTableClass('GPOS'))
1082def prune_pre_subset(self, options):
1083  # Drop undesired features
1084  if '*' not in options.layout_features:
1085    self.subset_feature_tags(options.layout_features)
1086  # Drop unreferenced lookups
1087  self.prune_lookups()
1088  # Prune lookups themselves
1089  if self.table.LookupList:
1090    self.table.LookupList.prune_pre_subset(options);
1091  return True
1092
1093@_add_method(ttLib.getTableClass('GSUB'),
1094             ttLib.getTableClass('GPOS'))
1095def remove_redundant_langsys(self):
1096  table = self.table
1097  if not table.ScriptList or not table.FeatureList:
1098    return
1099
1100  features = table.FeatureList.FeatureRecord
1101
1102  for s in table.ScriptList.ScriptRecord:
1103    d = s.Script.DefaultLangSys
1104    if not d:
1105      continue
1106    for lr in s.Script.LangSysRecord[:]:
1107      l = lr.LangSys
1108      # Compare d and l
1109      if len(d.FeatureIndex) != len(l.FeatureIndex):
1110        continue
1111      if (d.ReqFeatureIndex == 65535) != (l.ReqFeatureIndex == 65535):
1112        continue
1113
1114      if d.ReqFeatureIndex != 65535:
1115        if features[d.ReqFeatureIndex] != features[l.ReqFeatureIndex]:
1116          continue
1117
1118      for i in range(len(d.FeatureIndex)):
1119        if features[d.FeatureIndex[i]] != features[l.FeatureIndex[i]]:
1120          break
1121      else:
1122        # LangSys and default are equal; delete LangSys
1123        s.Script.LangSysRecord.remove(lr)
1124
1125@_add_method(ttLib.getTableClass('GSUB'),
1126             ttLib.getTableClass('GPOS'))
1127def prune_post_subset(self, options):
1128  table = self.table
1129
1130  # LookupList looks good.  Just prune lookups themselves
1131  if table.LookupList:
1132    table.LookupList.prune_post_subset(options);
1133    # XXX Next two lines disabled because OTS is stupid and
1134    # doesn't like NULL offsetse here.
1135    #if not table.LookupList.Lookup:
1136    #  table.LookupList = None
1137
1138  if not table.LookupList:
1139    table.FeatureList = None
1140
1141  if table.FeatureList:
1142    self.remove_redundant_langsys()
1143    # Remove unreferenced features
1144    self.prune_features()
1145
1146  # XXX Next two lines disabled because OTS is stupid and
1147  # doesn't like NULL offsetse here.
1148  #if table.FeatureList and not table.FeatureList.FeatureRecord:
1149  #  table.FeatureList = None
1150
1151  # Never drop scripts themselves as them just being available
1152  # holds semantic significance.
1153  # XXX Next two lines disabled because OTS is stupid and
1154  # doesn't like NULL offsetse here.
1155  #if table.ScriptList and not table.ScriptList.ScriptRecord:
1156  #  table.ScriptList = None
1157
1158  return True
1159
1160@_add_method(ttLib.getTableClass('GDEF'))
1161def subset_glyphs(self, s):
1162  glyphs = s.glyphs_gsubed
1163  table = self.table
1164  if table.LigCaretList:
1165    indices = table.LigCaretList.Coverage.subset(glyphs)
1166    table.LigCaretList.LigGlyph = [table.LigCaretList.LigGlyph[i]
1167                                   for i in indices]
1168    table.LigCaretList.LigGlyphCount = len(table.LigCaretList.LigGlyph)
1169  if table.MarkAttachClassDef:
1170    table.MarkAttachClassDef.classDefs = dict((g,v) for g,v in
1171                                              table.MarkAttachClassDef.
1172                                                classDefs.items()
1173                                              if g in glyphs)
1174  if table.GlyphClassDef:
1175    table.GlyphClassDef.classDefs = dict((g,v) for g,v in
1176                                         table.GlyphClassDef.
1177                                           classDefs.items()
1178                                         if g in glyphs)
1179  if table.AttachList:
1180    indices = table.AttachList.Coverage.subset(glyphs)
1181    GlyphCount = table.AttachList.GlyphCount
1182    table.AttachList.AttachPoint = [table.AttachList.AttachPoint[i]
1183                                    for i in indices
1184                                    if i < GlyphCount]
1185    table.AttachList.GlyphCount = len(table.AttachList.AttachPoint)
1186  if hasattr(table, "MarkGlyphSetsDef") and table.MarkGlyphSetsDef:
1187    for coverage in table.MarkGlyphSetsDef.Coverage:
1188      coverage.subset(glyphs)
1189    # TODO: The following is disabled.  If enabling, we need to go fixup all
1190    # lookups that use MarkFilteringSet and map their set.
1191    #indices = table.MarkGlyphSetsDef.Coverage = [c for c in table.MarkGlyphSetsDef.Coverage if c.glyphs]
1192  return True
1193
1194@_add_method(ttLib.getTableClass('GDEF'))
1195def prune_post_subset(self, options):
1196  table = self.table
1197  if table.LigCaretList and not table.LigCaretList.LigGlyphCount:
1198    table.LigCaretList = None
1199  if table.MarkAttachClassDef and not table.MarkAttachClassDef.classDefs:
1200    table.MarkAttachClassDef = None
1201  if table.GlyphClassDef and not table.GlyphClassDef.classDefs:
1202    table.GlyphClassDef = None
1203  if table.AttachList and not table.AttachList.GlyphCount:
1204    table.AttachList = None
1205  if hasattr(table, "MarkGlyphSetsDef") and table.MarkGlyphSetsDef and not table.MarkGlyphSetsDef.Coverage:
1206    table.MarkGlyphSetsDef = None
1207    if table.Version == 0x00010002/0x10000:
1208      table.Version = 1.0
1209  return bool(table.LigCaretList or
1210              table.MarkAttachClassDef or
1211              table.GlyphClassDef or
1212              table.AttachList or
1213              (table.Version >= 0x00010002/0x10000 and table.MarkGlyphSetsDef))
1214
1215@_add_method(ttLib.getTableClass('kern'))
1216def prune_pre_subset(self, options):
1217  # Prune unknown kern table types
1218  self.kernTables = [t for t in self.kernTables if hasattr(t, 'kernTable')]
1219  return bool(self.kernTables)
1220
1221@_add_method(ttLib.getTableClass('kern'))
1222def subset_glyphs(self, s):
1223  glyphs = s.glyphs_gsubed
1224  for t in self.kernTables:
1225    t.kernTable = dict(((a,b),v) for (a,b),v in t.kernTable.items()
1226                       if a in glyphs and b in glyphs)
1227  self.kernTables = [t for t in self.kernTables if t.kernTable]
1228  return bool(self.kernTables)
1229
1230@_add_method(ttLib.getTableClass('vmtx'))
1231def subset_glyphs(self, s):
1232  self.metrics = dict((g,v) for g,v in self.metrics.items() if g in s.glyphs)
1233  return bool(self.metrics)
1234
1235@_add_method(ttLib.getTableClass('hmtx'))
1236def subset_glyphs(self, s):
1237  self.metrics = dict((g,v) for g,v in self.metrics.items() if g in s.glyphs)
1238  return True # Required table
1239
1240@_add_method(ttLib.getTableClass('hdmx'))
1241def subset_glyphs(self, s):
1242  self.hdmx = dict((sz,dict((g,v) for g,v in l.items() if g in s.glyphs))
1243                   for sz,l in self.hdmx.items())
1244  return bool(self.hdmx)
1245
1246@_add_method(ttLib.getTableClass('VORG'))
1247def subset_glyphs(self, s):
1248  self.VOriginRecords = dict((g,v) for g,v in self.VOriginRecords.items()
1249                             if g in s.glyphs)
1250  self.numVertOriginYMetrics = len(self.VOriginRecords)
1251  return True  # Never drop; has default metrics
1252
1253@_add_method(ttLib.getTableClass('post'))
1254def prune_pre_subset(self, options):
1255  if not options.glyph_names:
1256    self.formatType = 3.0
1257  return True # Required table
1258
1259@_add_method(ttLib.getTableClass('post'))
1260def subset_glyphs(self, s):
1261  self.extraNames = []  # This seems to do it
1262  return True # Required table
1263
1264@_add_method(ttLib.getTableModule('glyf').Glyph)
1265def remapComponentsFast(self, indices):
1266  if not self.data or struct.unpack(">h", self.data[:2])[0] >= 0:
1267    return  # Not composite
1268  data = array.array("B", self.data)
1269  i = 10
1270  more = 1
1271  while more:
1272    flags =(data[i] << 8) | data[i+1]
1273    glyphID =(data[i+2] << 8) | data[i+3]
1274    # Remap
1275    glyphID = indices.index(glyphID)
1276    data[i+2] = glyphID >> 8
1277    data[i+3] = glyphID & 0xFF
1278    i += 4
1279    flags = int(flags)
1280
1281    if flags & 0x0001: i += 4  # ARG_1_AND_2_ARE_WORDS
1282    else: i += 2
1283    if flags & 0x0008: i += 2  # WE_HAVE_A_SCALE
1284    elif flags & 0x0040: i += 4  # WE_HAVE_AN_X_AND_Y_SCALE
1285    elif flags & 0x0080: i += 8  # WE_HAVE_A_TWO_BY_TWO
1286    more = flags & 0x0020  # MORE_COMPONENTS
1287
1288  self.data = data.tostring()
1289
1290@_add_method(ttLib.getTableClass('glyf'))
1291def closure_glyphs(self, s):
1292  decompose = s.glyphs
1293  while True:
1294    components = set()
1295    for g in decompose:
1296      if g not in self.glyphs:
1297        continue
1298      gl = self.glyphs[g]
1299      for c in gl.getComponentNames(self):
1300        if c not in s.glyphs:
1301          components.add(c)
1302    components = set(c for c in components if c not in s.glyphs)
1303    if not components:
1304      break
1305    decompose = components
1306    s.glyphs.update(components)
1307
1308@_add_method(ttLib.getTableClass('glyf'))
1309def prune_pre_subset(self, options):
1310  if options.notdef_glyph and not options.notdef_outline:
1311    g = self[self.glyphOrder[0]]
1312    # Yay, easy!
1313    g.__dict__.clear()
1314    g.data = ""
1315  return True
1316
1317@_add_method(ttLib.getTableClass('glyf'))
1318def subset_glyphs(self, s):
1319  self.glyphs = dict((g,v) for g,v in self.glyphs.items() if g in s.glyphs)
1320  indices = [i for i,g in enumerate(self.glyphOrder) if g in s.glyphs]
1321  for v in self.glyphs.values():
1322    if hasattr(v, "data"):
1323      v.remapComponentsFast(indices)
1324    else:
1325      pass  # No need
1326  self.glyphOrder = [g for g in self.glyphOrder if g in s.glyphs]
1327  # Don't drop empty 'glyf' tables, otherwise 'loca' doesn't get subset.
1328  return True
1329
1330@_add_method(ttLib.getTableClass('glyf'))
1331def prune_post_subset(self, options):
1332  if not options.hinting:
1333    for v in self.glyphs.values():
1334      v.removeHinting()
1335  return True
1336
1337@_add_method(ttLib.getTableClass('CFF '))
1338def prune_pre_subset(self, options):
1339  cff = self.cff
1340  # CFF table must have one font only
1341  cff.fontNames = cff.fontNames[:1]
1342
1343  if options.notdef_glyph and not options.notdef_outline:
1344    for fontname in cff.keys():
1345      font = cff[fontname]
1346      c,_ = font.CharStrings.getItemAndSelector('.notdef')
1347      # XXX we should preserve the glyph width
1348      c.bytecode = '\x0e' # endchar
1349      c.program = None
1350
1351  return True # bool(cff.fontNames)
1352
1353@_add_method(ttLib.getTableClass('CFF '))
1354def subset_glyphs(self, s):
1355  cff = self.cff
1356  for fontname in cff.keys():
1357    font = cff[fontname]
1358    cs = font.CharStrings
1359
1360    # Load all glyphs
1361    for g in font.charset:
1362      if g not in s.glyphs: continue
1363      c,sel = cs.getItemAndSelector(g)
1364
1365    if cs.charStringsAreIndexed:
1366      indices = [i for i,g in enumerate(font.charset) if g in s.glyphs]
1367      csi = cs.charStringsIndex
1368      csi.items = [csi.items[i] for i in indices]
1369      csi.count = len(csi.items)
1370      del csi.file, csi.offsets
1371      if hasattr(font, "FDSelect"):
1372        sel = font.FDSelect
1373        sel.format = None
1374        sel.gidArray = [sel.gidArray[i] for i in indices]
1375      cs.charStrings = dict((g,indices.index(v))
1376                            for g,v in cs.charStrings.items()
1377                            if g in s.glyphs)
1378    else:
1379      cs.charStrings = dict((g,v)
1380                            for g,v in cs.charStrings.items()
1381                            if g in s.glyphs)
1382    font.charset = [g for g in font.charset if g in s.glyphs]
1383    font.numGlyphs = len(font.charset)
1384
1385  return True # any(cff[fontname].numGlyphs for fontname in cff.keys())
1386
1387@_add_method(psCharStrings.T2CharString)
1388def subset_subroutines(self, subrs, gsubrs):
1389  p = self.program
1390  assert len(p)
1391  for i in range(1, len(p)):
1392    if p[i] == 'callsubr':
1393      assert isinstance(p[i-1], int)
1394      p[i-1] = subrs._used.index(p[i-1] + subrs._old_bias) - subrs._new_bias
1395    elif p[i] == 'callgsubr':
1396      assert isinstance(p[i-1], int)
1397      p[i-1] = gsubrs._used.index(p[i-1] + gsubrs._old_bias) - gsubrs._new_bias
1398
1399@_add_method(psCharStrings.T2CharString)
1400def drop_hints(self):
1401  hints = self._hints
1402
1403  if hints.has_hint:
1404    self.program = self.program[hints.last_hint:]
1405    if hasattr(self, 'width'):
1406      # Insert width back if needed
1407      if self.width != self.private.defaultWidthX:
1408        self.program.insert(0, self.width - self.private.nominalWidthX)
1409
1410  if hints.has_hintmask:
1411    i = 0
1412    p = self.program
1413    while i < len(p):
1414      if p[i] in ['hintmask', 'cntrmask']:
1415        assert i + 1 <= len(p)
1416        del p[i:i+2]
1417        continue
1418      i += 1
1419
1420  # TODO: we currently don't drop calls to "empty" subroutines.
1421
1422  assert len(self.program)
1423
1424  del self._hints
1425
1426class _MarkingT2Decompiler(psCharStrings.SimpleT2Decompiler):
1427
1428  def __init__(self, localSubrs, globalSubrs):
1429    psCharStrings.SimpleT2Decompiler.__init__(self,
1430                                              localSubrs,
1431                                              globalSubrs)
1432    for subrs in [localSubrs, globalSubrs]:
1433      if subrs and not hasattr(subrs, "_used"):
1434        subrs._used = set()
1435
1436  def op_callsubr(self, index):
1437    self.localSubrs._used.add(self.operandStack[-1]+self.localBias)
1438    psCharStrings.SimpleT2Decompiler.op_callsubr(self, index)
1439
1440  def op_callgsubr(self, index):
1441    self.globalSubrs._used.add(self.operandStack[-1]+self.globalBias)
1442    psCharStrings.SimpleT2Decompiler.op_callgsubr(self, index)
1443
1444class _DehintingT2Decompiler(psCharStrings.SimpleT2Decompiler):
1445
1446  class Hints(object):
1447    def __init__(self):
1448      # Whether calling this charstring produces any hint stems
1449      self.has_hint = False
1450      # Index to start at to drop all hints
1451      self.last_hint = 0
1452      # Index up to which we know more hints are possible.  Only
1453      # relevant if status is 0 or 1.
1454      self.last_checked = 0
1455      # The status means:
1456      # 0: after dropping hints, this charstring is empty
1457      # 1: after dropping hints, there may be more hints continuing after this
1458      # 2: no more hints possible after this charstring
1459      self.status = 0
1460      # Has hintmask instructions; not recursive
1461      self.has_hintmask = False
1462    pass
1463
1464  def __init__(self, css, localSubrs, globalSubrs):
1465    self._css = css
1466    psCharStrings.SimpleT2Decompiler.__init__(self,
1467                                              localSubrs,
1468                                              globalSubrs)
1469
1470  def execute(self, charString):
1471    old_hints = charString._hints if hasattr(charString, '_hints') else None
1472    charString._hints = self.Hints()
1473
1474    psCharStrings.SimpleT2Decompiler.execute(self, charString)
1475
1476    hints = charString._hints
1477
1478    if hints.has_hint or hints.has_hintmask:
1479      self._css.add(charString)
1480
1481    if hints.status != 2:
1482      # Check from last_check, make sure we didn't have any operators.
1483      for i in range(hints.last_checked, len(charString.program) - 1):
1484        if isinstance(charString.program[i], str):
1485          hints.status = 2
1486          break;
1487        else:
1488          hints.status = 1 # There's *something* here
1489      hints.last_checked = len(charString.program)
1490
1491    if old_hints:
1492      assert hints.__dict__ == old_hints.__dict__
1493
1494  def op_callsubr(self, index):
1495    subr = self.localSubrs[self.operandStack[-1]+self.localBias]
1496    psCharStrings.SimpleT2Decompiler.op_callsubr(self, index)
1497    self.processSubr(index, subr)
1498
1499  def op_callgsubr(self, index):
1500    subr = self.globalSubrs[self.operandStack[-1]+self.globalBias]
1501    psCharStrings.SimpleT2Decompiler.op_callgsubr(self, index)
1502    self.processSubr(index, subr)
1503
1504  def op_hstem(self, index):
1505    psCharStrings.SimpleT2Decompiler.op_hstem(self, index)
1506    self.processHint(index)
1507  def op_vstem(self, index):
1508    psCharStrings.SimpleT2Decompiler.op_vstem(self, index)
1509    self.processHint(index)
1510  def op_hstemhm(self, index):
1511    psCharStrings.SimpleT2Decompiler.op_hstemhm(self, index)
1512    self.processHint(index)
1513  def op_vstemhm(self, index):
1514    psCharStrings.SimpleT2Decompiler.op_vstemhm(self, index)
1515    self.processHint(index)
1516  def op_hintmask(self, index):
1517    psCharStrings.SimpleT2Decompiler.op_hintmask(self, index)
1518    self.processHintmask(index)
1519  def op_cntrmask(self, index):
1520    psCharStrings.SimpleT2Decompiler.op_cntrmask(self, index)
1521    self.processHintmask(index)
1522
1523  def processHintmask(self, index):
1524    cs = self.callingStack[-1]
1525    hints = cs._hints
1526    hints.has_hintmask = True
1527    if hints.status != 2 and hints.has_hint:
1528      # Check from last_check, see if we may be an implicit vstem
1529      for i in range(hints.last_checked, index - 1):
1530        if isinstance(cs.program[i], str):
1531          hints.status = 2
1532          break;
1533      if hints.status != 2:
1534        # We are an implicit vstem
1535        hints.last_hint = index + 1
1536        hints.status = 0
1537    hints.last_checked = index + 1
1538
1539  def processHint(self, index):
1540    cs = self.callingStack[-1]
1541    hints = cs._hints
1542    hints.has_hint = True
1543    hints.last_hint = index
1544    hints.last_checked = index
1545
1546  def processSubr(self, index, subr):
1547    cs = self.callingStack[-1]
1548    hints = cs._hints
1549    subr_hints = subr._hints
1550
1551    if subr_hints.has_hint:
1552      if hints.status != 2:
1553        hints.has_hint = True
1554        hints.last_checked = index
1555        hints.status = subr_hints.status
1556        # Decide where to chop off from
1557        if subr_hints.status == 0:
1558          hints.last_hint = index
1559        else:
1560          hints.last_hint = index - 2 # Leave the subr call in
1561      else:
1562        # In my understanding, this is a font bug.  Ie. it has hint stems
1563        # *after* path construction.  I've seen this in widespread fonts.
1564        # Best to ignore the hints I suppose...
1565        pass
1566        #assert 0
1567    else:
1568      hints.status = max(hints.status, subr_hints.status)
1569      if hints.status != 2:
1570        # Check from last_check, make sure we didn't have
1571        # any operators.
1572        for i in range(hints.last_checked, index - 1):
1573          if isinstance(cs.program[i], str):
1574            hints.status = 2
1575            break;
1576        hints.last_checked = index
1577      if hints.status != 2:
1578        # Decide where to chop off from
1579        if subr_hints.status == 0:
1580          hints.last_hint = index
1581        else:
1582          hints.last_hint = index - 2 # Leave the subr call in
1583
1584@_add_method(ttLib.getTableClass('CFF '))
1585def prune_post_subset(self, options):
1586  cff = self.cff
1587  for fontname in cff.keys():
1588    font = cff[fontname]
1589    cs = font.CharStrings
1590
1591
1592    #
1593    # Drop unused FontDictionaries
1594    #
1595    if hasattr(font, "FDSelect"):
1596      sel = font.FDSelect
1597      indices = _uniq_sort(sel.gidArray)
1598      sel.gidArray = [indices.index (ss) for ss in sel.gidArray]
1599      arr = font.FDArray
1600      arr.items = [arr[i] for i in indices]
1601      arr.count = len(arr.items)
1602      del arr.file, arr.offsets
1603
1604
1605    #
1606    # Drop hints if not needed
1607    #
1608    if not options.hinting:
1609
1610      #
1611      # This can be tricky, but doesn't have to.  What we do is:
1612      #
1613      # - Run all used glyph charstrings and recurse into subroutines,
1614      # - For each charstring (including subroutines), if it has any
1615      #   of the hint stem operators, we mark it as such.  Upon returning,
1616      #   for each charstring we note all the subroutine calls it makes
1617      #   that (recursively) contain a stem,
1618      # - Dropping hinting then consists of the following two ops:
1619      #   * Drop the piece of the program in each charstring before the
1620      #     last call to a stem op or a stem-calling subroutine,
1621      #   * Drop all hintmask operations.
1622      # - It's trickier... A hintmask right after hints and a few numbers
1623      #   will act as an implicit vstemhm.  As such, we track whether
1624      #   we have seen any non-hint operators so far and do the right
1625      #   thing, recursively...  Good luck understanding that :(
1626      #
1627      css = set()
1628      for g in font.charset:
1629        c,sel = cs.getItemAndSelector(g)
1630        # Make sure it's decompiled.  We want our "decompiler" to walk
1631        # the program, not the bytecode.
1632        c.draw(basePen.NullPen())
1633        subrs = getattr(c.private, "Subrs", [])
1634        decompiler = _DehintingT2Decompiler(css, subrs, c.globalSubrs)
1635        decompiler.execute(c)
1636      for charstring in css:
1637        charstring.drop_hints()
1638
1639      # Drop font-wide hinting values
1640      all_privs = []
1641      if hasattr(font, 'FDSelect'):
1642        all_privs.extend(fd.Private for fd in font.FDArray)
1643      else:
1644        all_privs.append(font.Private)
1645      for priv in all_privs:
1646        for k in ['BlueValues', 'OtherBlues', 'FamilyBlues', 'FamilyOtherBlues',
1647                  'BlueScale', 'BlueShift', 'BlueFuzz',
1648                  'StemSnapH', 'StemSnapV', 'StdHW', 'StdVW']:
1649          if hasattr(priv, k):
1650            setattr(priv, k, None)
1651
1652
1653    #
1654    # Renumber subroutines to remove unused ones
1655    #
1656
1657    # Mark all used subroutines
1658    for g in font.charset:
1659      c,sel = cs.getItemAndSelector(g)
1660      subrs = getattr(c.private, "Subrs", [])
1661      decompiler = _MarkingT2Decompiler(subrs, c.globalSubrs)
1662      decompiler.execute(c)
1663
1664    all_subrs = [font.GlobalSubrs]
1665    if hasattr(font, 'FDSelect'):
1666      all_subrs.extend(fd.Private.Subrs for fd in font.FDArray if hasattr(fd.Private, 'Subrs') and fd.Private.Subrs)
1667    elif hasattr(font.Private, 'Subrs') and font.Private.Subrs:
1668      all_subrs.append(font.Private.Subrs)
1669
1670    subrs = set(subrs) # Remove duplicates
1671
1672    # Prepare
1673    for subrs in all_subrs:
1674      if not hasattr(subrs, '_used'):
1675        subrs._used = set()
1676      subrs._used = _uniq_sort(subrs._used)
1677      subrs._old_bias = psCharStrings.calcSubrBias(subrs)
1678      subrs._new_bias = psCharStrings.calcSubrBias(subrs._used)
1679
1680    # Renumber glyph charstrings
1681    for g in font.charset:
1682      c,sel = cs.getItemAndSelector(g)
1683      subrs = getattr(c.private, "Subrs", [])
1684      c.subset_subroutines (subrs, font.GlobalSubrs)
1685
1686    # Renumber subroutines themselves
1687    for subrs in all_subrs:
1688
1689      if subrs == font.GlobalSubrs:
1690        if not hasattr(font, 'FDSelect') and hasattr(font.Private, 'Subrs'):
1691          local_subrs = font.Private.Subrs
1692        else:
1693          local_subrs = []
1694      else:
1695        local_subrs = subrs
1696
1697      subrs.items = [subrs.items[i] for i in subrs._used]
1698      subrs.count = len(subrs.items)
1699      del subrs.file
1700      if hasattr(subrs, 'offsets'):
1701        del subrs.offsets
1702
1703      for i in range (subrs.count):
1704        subrs[i].subset_subroutines (local_subrs, font.GlobalSubrs)
1705
1706    # Cleanup
1707    for subrs in all_subrs:
1708      del subrs._used, subrs._old_bias, subrs._new_bias
1709
1710  return True
1711
1712@_add_method(ttLib.getTableClass('cmap'))
1713def closure_glyphs(self, s):
1714  tables = [t for t in self.tables
1715            if t.platformID == 3 and t.platEncID in [1, 10]]
1716  for u in s.unicodes_requested:
1717    found = False
1718    for table in tables:
1719      if u in table.cmap:
1720        s.glyphs.add(table.cmap[u])
1721        found = True
1722        break
1723    if not found:
1724      s.log("No glyph for Unicode value %s; skipping." % u)
1725
1726@_add_method(ttLib.getTableClass('cmap'))
1727def prune_pre_subset(self, options):
1728  if not options.legacy_cmap:
1729    # Drop non-Unicode / non-Symbol cmaps
1730    self.tables = [t for t in self.tables
1731                   if t.platformID == 3 and t.platEncID in [0, 1, 10]]
1732  if not options.symbol_cmap:
1733    self.tables = [t for t in self.tables
1734                   if t.platformID == 3 and t.platEncID in [1, 10]]
1735  # TODO(behdad) Only keep one subtable?
1736  # For now, drop format=0 which can't be subset_glyphs easily?
1737  self.tables = [t for t in self.tables if t.format != 0]
1738  self.numSubTables = len(self.tables)
1739  return True # Required table
1740
1741@_add_method(ttLib.getTableClass('cmap'))
1742def subset_glyphs(self, s):
1743  s.glyphs = s.glyphs_cmaped
1744  for t in self.tables:
1745    # For reasons I don't understand I need this here
1746    # to force decompilation of the cmap format 14.
1747    try:
1748      getattr(t, "asdf")
1749    except AttributeError:
1750      pass
1751    if t.format == 14:
1752      # TODO(behdad) XXX We drop all the default-UVS mappings(g==None).
1753      t.uvsDict = dict((v,[(u,g) for u,g in l if g in s.glyphs])
1754                       for v,l in t.uvsDict.items())
1755      t.uvsDict = dict((v,l) for v,l in t.uvsDict.items() if l)
1756    else:
1757      t.cmap = dict((u,g) for u,g in t.cmap.items()
1758                    if g in s.glyphs_requested or u in s.unicodes_requested)
1759  self.tables = [t for t in self.tables
1760                 if (t.cmap if t.format != 14 else t.uvsDict)]
1761  self.numSubTables = len(self.tables)
1762  # TODO(behdad) Convert formats when needed.
1763  # In particular, if we have a format=12 without non-BMP
1764  # characters, either drop format=12 one or convert it
1765  # to format=4 if there's not one.
1766  return True # Required table
1767
1768@_add_method(ttLib.getTableClass('name'))
1769def prune_pre_subset(self, options):
1770  if '*' not in options.name_IDs:
1771    self.names = [n for n in self.names if n.nameID in options.name_IDs]
1772  if not options.name_legacy:
1773    self.names = [n for n in self.names
1774                  if n.platformID == 3 and n.platEncID == 1]
1775  if '*' not in options.name_languages:
1776    self.names = [n for n in self.names if n.langID in options.name_languages]
1777  return True  # Required table
1778
1779
1780# TODO(behdad) OS/2 ulUnicodeRange / ulCodePageRange?
1781# TODO(behdad) Drop AAT tables.
1782# TODO(behdad) Drop unneeded GSUB/GPOS Script/LangSys entries.
1783# TODO(behdad) Drop empty GSUB/GPOS, and GDEF if no GSUB/GPOS left
1784# TODO(behdad) Drop GDEF subitems if unused by lookups
1785# TODO(behdad) Avoid recursing too much (in GSUB/GPOS and in CFF)
1786# TODO(behdad) Text direction considerations.
1787# TODO(behdad) Text script / language considerations.
1788# TODO(behdad) Optionally drop 'kern' table if GPOS available
1789
1790class Options(object):
1791
1792  class UnknownOptionError(Exception):
1793    pass
1794
1795  _drop_tables_default = ['BASE', 'JSTF', 'DSIG', 'EBDT', 'EBLC', 'EBSC', 'SVG ',
1796                          'PCLT', 'LTSH']
1797  _drop_tables_default += ['Feat', 'Glat', 'Gloc', 'Silf', 'Sill']  # Graphite
1798  _drop_tables_default += ['CBLC', 'CBDT', 'sbix', 'COLR', 'CPAL']  # Color
1799  _no_subset_tables_default = ['gasp', 'head', 'hhea', 'maxp', 'vhea', 'OS/2',
1800                               'loca', 'name', 'cvt ', 'fpgm', 'prep']
1801  _hinting_tables_default = ['cvt ', 'fpgm', 'prep', 'hdmx', 'VDMX']
1802
1803  # Based on HarfBuzz shapers
1804  _layout_features_groups = {
1805    # Default shaper
1806    'common': ['ccmp', 'liga', 'locl', 'mark', 'mkmk', 'rlig'],
1807    'horizontal': ['calt', 'clig', 'curs', 'kern', 'rclt'],
1808    'vertical':  ['valt', 'vert', 'vkrn', 'vpal', 'vrt2'],
1809    'ltr': ['ltra', 'ltrm'],
1810    'rtl': ['rtla', 'rtlm'],
1811    # Complex shapers
1812    'arabic': ['init', 'medi', 'fina', 'isol', 'med2', 'fin2', 'fin3',
1813               'cswh', 'mset'],
1814    'hangul': ['ljmo', 'vjmo', 'tjmo'],
1815    'tibetan': ['abvs', 'blws', 'abvm', 'blwm'],
1816    'indic': ['nukt', 'akhn', 'rphf', 'rkrf', 'pref', 'blwf', 'half',
1817              'abvf', 'pstf', 'cfar', 'vatu', 'cjct', 'init', 'pres',
1818              'abvs', 'blws', 'psts', 'haln', 'dist', 'abvm', 'blwm'],
1819  }
1820  _layout_features_default = _uniq_sort(sum(
1821      iter(_layout_features_groups.values()), []))
1822
1823  drop_tables = _drop_tables_default
1824  no_subset_tables = _no_subset_tables_default
1825  hinting_tables = _hinting_tables_default
1826  layout_features = _layout_features_default
1827  hinting = True
1828  glyph_names = False
1829  legacy_cmap = False
1830  symbol_cmap = False
1831  name_IDs = [1, 2]  # Family and Style
1832  name_legacy = False
1833  name_languages = [0x0409]  # English
1834  notdef_glyph = True # gid0 for TrueType / .notdef for CFF
1835  notdef_outline = False # No need for notdef to have an outline really
1836  recommended_glyphs = False  # gid1, gid2, gid3 for TrueType
1837  recalc_bounds = False # Recalculate font bounding boxes
1838  canonical_order = False # Order tables as recommended
1839  flavor = None # May be 'woff'
1840
1841  def __init__(self, **kwargs):
1842
1843    self.set(**kwargs)
1844
1845  def set(self, **kwargs):
1846    for k,v in kwargs.items():
1847      if not hasattr(self, k):
1848        raise self.UnknownOptionError("Unknown option '%s'" % k)
1849      setattr(self, k, v)
1850
1851  def parse_opts(self, argv, ignore_unknown=False):
1852    ret = []
1853    opts = {}
1854    for a in argv:
1855      orig_a = a
1856      if not a.startswith('--'):
1857        ret.append(a)
1858        continue
1859      a = a[2:]
1860      i = a.find('=')
1861      op = '='
1862      if i == -1:
1863        if a.startswith("no-"):
1864          k = a[3:]
1865          v = False
1866        else:
1867          k = a
1868          v = True
1869      else:
1870        k = a[:i]
1871        if k[-1] in "-+":
1872          op = k[-1]+'='  # Ops is '-=' or '+=' now.
1873          k = k[:-1]
1874        v = a[i+1:]
1875      k = k.replace('-', '_')
1876      if not hasattr(self, k):
1877        if ignore_unknown is True or k in ignore_unknown:
1878          ret.append(orig_a)
1879          continue
1880        else:
1881          raise self.UnknownOptionError("Unknown option '%s'" % a)
1882
1883      ov = getattr(self, k)
1884      if isinstance(ov, bool):
1885        v = bool(v)
1886      elif isinstance(ov, int):
1887        v = int(v)
1888      elif isinstance(ov, list):
1889        vv = v.split(',')
1890        if vv == ['']:
1891          vv = []
1892        vv = [int(x, 0) if len(x) and x[0] in "0123456789" else x for x in vv]
1893        if op == '=':
1894          v = vv
1895        elif op == '+=':
1896          v = ov
1897          v.extend(vv)
1898        elif op == '-=':
1899          v = ov
1900          for x in vv:
1901            if x in v:
1902              v.remove(x)
1903        else:
1904          assert False
1905
1906      opts[k] = v
1907    self.set(**opts)
1908
1909    return ret
1910
1911
1912class Subsetter(object):
1913
1914  def __init__(self, options=None, log=None):
1915
1916    if not log:
1917      log = Logger()
1918    if not options:
1919      options = Options()
1920
1921    self.options = options
1922    self.log = log
1923    self.unicodes_requested = set()
1924    self.glyphs_requested = set()
1925    self.glyphs = set()
1926
1927  def populate(self, glyphs=[], unicodes=[], text=""):
1928    self.unicodes_requested.update(unicodes)
1929    if isinstance(text, bytes):
1930      text = text.decode("utf8")
1931    for u in text:
1932      self.unicodes_requested.add(ord(u))
1933    self.glyphs_requested.update(glyphs)
1934    self.glyphs.update(glyphs)
1935
1936  def _prune_pre_subset(self, font):
1937
1938    for tag in font.keys():
1939      if tag == 'GlyphOrder': continue
1940
1941      if(tag in self.options.drop_tables or
1942         (tag in self.options.hinting_tables and not self.options.hinting)):
1943        self.log(tag, "dropped")
1944        del font[tag]
1945        continue
1946
1947      clazz = ttLib.getTableClass(tag)
1948
1949      if hasattr(clazz, 'prune_pre_subset'):
1950        table = font[tag]
1951        self.log.lapse("load '%s'" % tag)
1952        retain = table.prune_pre_subset(self.options)
1953        self.log.lapse("prune  '%s'" % tag)
1954        if not retain:
1955          self.log(tag, "pruned to empty; dropped")
1956          del font[tag]
1957          continue
1958        else:
1959          self.log(tag, "pruned")
1960
1961  def _closure_glyphs(self, font):
1962
1963    realGlyphs = set(font.getGlyphOrder())
1964
1965    self.glyphs = self.glyphs_requested.copy()
1966
1967    if 'cmap' in font:
1968      font['cmap'].closure_glyphs(self)
1969      self.glyphs.intersection_update(realGlyphs)
1970    self.glyphs_cmaped = self.glyphs
1971
1972    if self.options.notdef_glyph:
1973      if 'glyf' in font:
1974        self.glyphs.add(font.getGlyphName(0))
1975        self.log("Added gid0 to subset")
1976      else:
1977        self.glyphs.add('.notdef')
1978        self.log("Added .notdef to subset")
1979    if self.options.recommended_glyphs:
1980      if 'glyf' in font:
1981        for i in range(min(4, len(font.getGlyphOrder()))):
1982          self.glyphs.add(font.getGlyphName(i))
1983        self.log("Added first four glyphs to subset")
1984
1985    if 'GSUB' in font:
1986      self.log("Closing glyph list over 'GSUB': %d glyphs before" %
1987                len(self.glyphs))
1988      self.log.glyphs(self.glyphs, font=font)
1989      font['GSUB'].closure_glyphs(self)
1990      self.glyphs.intersection_update(realGlyphs)
1991      self.log("Closed  glyph list over 'GSUB': %d glyphs after" %
1992                len(self.glyphs))
1993      self.log.glyphs(self.glyphs, font=font)
1994      self.log.lapse("close glyph list over 'GSUB'")
1995    self.glyphs_gsubed = self.glyphs.copy()
1996
1997    if 'glyf' in font:
1998      self.log("Closing glyph list over 'glyf': %d glyphs before" %
1999                len(self.glyphs))
2000      self.log.glyphs(self.glyphs, font=font)
2001      font['glyf'].closure_glyphs(self)
2002      self.glyphs.intersection_update(realGlyphs)
2003      self.log("Closed  glyph list over 'glyf': %d glyphs after" %
2004                len(self.glyphs))
2005      self.log.glyphs(self.glyphs, font=font)
2006      self.log.lapse("close glyph list over 'glyf'")
2007    self.glyphs_glyfed = self.glyphs.copy()
2008
2009    self.glyphs_all = self.glyphs.copy()
2010
2011    self.log("Retaining %d glyphs: " % len(self.glyphs_all))
2012
2013    del self.glyphs
2014
2015
2016  def _subset_glyphs(self, font):
2017    for tag in font.keys():
2018      if tag == 'GlyphOrder': continue
2019      clazz = ttLib.getTableClass(tag)
2020
2021      if tag in self.options.no_subset_tables:
2022        self.log(tag, "subsetting not needed")
2023      elif hasattr(clazz, 'subset_glyphs'):
2024        table = font[tag]
2025        self.glyphs = self.glyphs_all
2026        retain = table.subset_glyphs(self)
2027        del self.glyphs
2028        self.log.lapse("subset '%s'" % tag)
2029        if not retain:
2030          self.log(tag, "subsetted to empty; dropped")
2031          del font[tag]
2032        else:
2033          self.log(tag, "subsetted")
2034      else:
2035        self.log(tag, "NOT subset; don't know how to subset; dropped")
2036        del font[tag]
2037
2038    glyphOrder = font.getGlyphOrder()
2039    glyphOrder = [g for g in glyphOrder if g in self.glyphs_all]
2040    font.setGlyphOrder(glyphOrder)
2041    font._buildReverseGlyphOrderDict()
2042    self.log.lapse("subset GlyphOrder")
2043
2044  def _prune_post_subset(self, font):
2045    for tag in font.keys():
2046      if tag == 'GlyphOrder': continue
2047      clazz = ttLib.getTableClass(tag)
2048      if hasattr(clazz, 'prune_post_subset'):
2049        table = font[tag]
2050        retain = table.prune_post_subset(self.options)
2051        self.log.lapse("prune  '%s'" % tag)
2052        if not retain:
2053          self.log(tag, "pruned to empty; dropped")
2054          del font[tag]
2055        else:
2056          self.log(tag, "pruned")
2057
2058  def subset(self, font):
2059
2060    self._prune_pre_subset(font)
2061    self._closure_glyphs(font)
2062    self._subset_glyphs(font)
2063    self._prune_post_subset(font)
2064
2065
2066class Logger(object):
2067
2068  def __init__(self, verbose=False, xml=False, timing=False):
2069    self.verbose = verbose
2070    self.xml = xml
2071    self.timing = timing
2072    self.last_time = self.start_time = time.time()
2073
2074  def parse_opts(self, argv):
2075    argv = argv[:]
2076    for v in ['verbose', 'xml', 'timing']:
2077      if "--"+v in argv:
2078        setattr(self, v, True)
2079        argv.remove("--"+v)
2080    return argv
2081
2082  def __call__(self, *things):
2083    if not self.verbose:
2084      return
2085    print(' '.join(str(x) for x in things))
2086
2087  def lapse(self, *things):
2088    if not self.timing:
2089      return
2090    new_time = time.time()
2091    print("Took %0.3fs to %s" %(new_time - self.last_time,
2092                                 ' '.join(str(x) for x in things)))
2093    self.last_time = new_time
2094
2095  def glyphs(self, glyphs, font=None):
2096    if not self.verbose:
2097      return
2098    self("Names: ", sorted(glyphs))
2099    if font:
2100      reverseGlyphMap = font.getReverseGlyphMap()
2101      self("Gids : ", sorted(reverseGlyphMap[g] for g in glyphs))
2102
2103  def font(self, font, file=sys.stdout):
2104    if not self.xml:
2105      return
2106    from fontTools.misc import xmlWriter
2107    writer = xmlWriter.XMLWriter(file)
2108    for tag in font.keys():
2109      writer.begintag(tag)
2110      writer.newline()
2111      font[tag].toXML(writer, font)
2112      writer.endtag(tag)
2113      writer.newline()
2114
2115
2116def load_font(fontFile,
2117              options,
2118              allowVID=False,
2119              checkChecksums=False,
2120              dontLoadGlyphNames=False):
2121
2122  font = ttLib.TTFont(fontFile,
2123                      allowVID=allowVID,
2124                      checkChecksums=checkChecksums,
2125                      recalcBBoxes=options.recalc_bounds)
2126
2127  # Hack:
2128  #
2129  # If we don't need glyph names, change 'post' class to not try to
2130  # load them.  It avoid lots of headache with broken fonts as well
2131  # as loading time.
2132  #
2133  # Ideally ttLib should provide a way to ask it to skip loading
2134  # glyph names.  But it currently doesn't provide such a thing.
2135  #
2136  if dontLoadGlyphNames:
2137    post = ttLib.getTableClass('post')
2138    saved = post.decode_format_2_0
2139    post.decode_format_2_0 = post.decode_format_3_0
2140    f = font['post']
2141    if f.formatType == 2.0:
2142      f.formatType = 3.0
2143    post.decode_format_2_0 = saved
2144
2145  return font
2146
2147def save_font(font, outfile, options):
2148  if options.flavor and not hasattr(font, 'flavor'):
2149    raise Exception("fonttools version does not support flavors.")
2150  font.flavor = options.flavor
2151  font.save(outfile, reorderTables=options.canonical_order)
2152
2153def main(args):
2154
2155  log = Logger()
2156  args = log.parse_opts(args)
2157
2158  options = Options()
2159  args = options.parse_opts(args, ignore_unknown=['text'])
2160
2161  if len(args) < 2:
2162    print("usage: pyftsubset font-file glyph... [--text=ABC]... [--option=value]...", file=sys.stderr)
2163    sys.exit(1)
2164
2165  fontfile = args[0]
2166  args = args[1:]
2167
2168  dontLoadGlyphNames =(not options.glyph_names and
2169         all(any(g.startswith(p)
2170             for p in ['gid', 'glyph', 'uni', 'U+'])
2171              for g in args))
2172
2173  font = load_font(fontfile, options, dontLoadGlyphNames=dontLoadGlyphNames)
2174  subsetter = Subsetter(options=options, log=log)
2175  log.lapse("load font")
2176
2177  names = font.getGlyphNames()
2178  log.lapse("loading glyph names")
2179
2180  glyphs = []
2181  unicodes = []
2182  text = ""
2183  for g in args:
2184    if g == '*':
2185      glyphs.extend(font.getGlyphOrder())
2186      continue
2187    if g in names:
2188      glyphs.append(g)
2189      continue
2190    if g.startswith('--text='):
2191      text += g[7:]
2192      continue
2193    if g.startswith('uni') or g.startswith('U+'):
2194      if g.startswith('uni') and len(g) > 3:
2195        g = g[3:]
2196      elif g.startswith('U+') and len(g) > 2:
2197        g = g[2:]
2198      u = int(g, 16)
2199      unicodes.append(u)
2200      continue
2201    if g.startswith('gid') or g.startswith('glyph'):
2202      if g.startswith('gid') and len(g) > 3:
2203        g = g[3:]
2204      elif g.startswith('glyph') and len(g) > 5:
2205        g = g[5:]
2206      try:
2207        glyphs.append(font.getGlyphName(int(g), requireReal=True))
2208      except ValueError:
2209        raise Exception("Invalid glyph identifier: %s" % g)
2210      continue
2211    raise Exception("Invalid glyph identifier: %s" % g)
2212  log.lapse("compile glyph list")
2213  log("Unicodes:", unicodes)
2214  log("Glyphs:", glyphs)
2215
2216  subsetter.populate(glyphs=glyphs, unicodes=unicodes, text=text)
2217  subsetter.subset(font)
2218
2219  outfile = fontfile + '.subset'
2220
2221  save_font (font, outfile, options)
2222  log.lapse("compile and save font")
2223
2224  log.last_time = log.start_time
2225  log.lapse("make one with everything(TOTAL TIME)")
2226
2227  if log.verbose:
2228    import os
2229    log("Input  font: %d bytes" % os.path.getsize(fontfile))
2230    log("Subset font: %d bytes" % os.path.getsize(outfile))
2231
2232  log.font(font)
2233
2234  font.close()
2235
2236
2237__all__ = [
2238  'Options',
2239  'Subsetter',
2240  'Logger',
2241  'load_font',
2242  'save_font',
2243  'main'
2244]
2245
2246if __name__ == '__main__':
2247  main(sys.argv[1:])
2248