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