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